Index: trunk/MagicSoft/Mars/mfileio/MCT1ReadAscii.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MCT1ReadAscii.cc	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MCT1ReadAscii.cc	(revision 1668)
@@ -127,5 +127,7 @@
     const char *name = file->GetName();
 
-    fIn = new ifstream(gSystem->ExpandPathName(name));
+    const char *expname = gSystem->ExpandPathName(name);
+    fIn = new ifstream(expname);
+    delete [] expname;
 
     const Bool_t noexist = !(*fIn);
Index: trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc	(revision 1668)
@@ -16,8 +16,7 @@
 !
 !
-!   Author(s): Thomas Bretz  12/2000 <mailto:tbretz@uni-sw.gwdg.de>
-!   Author(s): Harald Kornmayer 1/2001 (harald@mppmu.mpg.de)
+!   Author(s): Thomas Bretz 11/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2001
+!   Copyright: MAGIC Software Development, 2000-2002
 !
 !
@@ -29,4 +28,8 @@
 //
 // Reads a output file of the CT1 preproc.
+//
+// Implements usage of a selector (see MRead) Use such a filter to skip
+// events before reading! But never use a filter which needs read data
+// as input...
 //
 //  Input Containers:
@@ -58,4 +61,7 @@
 #include "MLogManip.h"
 
+#include "MTime.h"
+#include "MFilter.h"
+
 #include "MParList.h"
 #include "MCerPhotEvt.h"
@@ -112,5 +118,7 @@
 void MCT1ReadPreProc::AddFile(const char *txt)
 {
-    const TString fname = gSystem->ExpandPathName(txt);
+    const char *name = gSystem->ExpandPathName(txt);
+    TString fname(name);
+    delete [] name;
 
     if (!CheckHeader(fname))
@@ -502,6 +510,9 @@
     // open the file which is the first one in the chain
     //
-    const TString name  = file->GetName();
-    const TString fname = gSystem->ExpandPathName(name);
+    const TString name = file->GetName();
+
+    const char *expname = gSystem->ExpandPathName(name);
+    const TString fname(expname);
+    delete [] expname;
 
     //
@@ -648,4 +659,11 @@
 
     //
+    //  look for the time class in the plist
+    //
+    fTime = (MTime*)pList->FindCreateObj("MTime");
+    if (!fTime)
+        return kFALSE;
+
+    //
     //  look for the pedestal class in the plist
     //
@@ -685,5 +703,5 @@
     fNumRuns       = 0;
 
-    return kTRUE;
+    return GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
 }
 
@@ -695,8 +713,11 @@
 void MCT1ReadPreProc::ProcessEvent(const struct eventrecord &event)
 {
+    //  int   isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
+    //  int   isecfrac_200ns;     // fractional part of isecs_since_midday
+    fTime->SetTime(event.isecfrac_200ns, event.isecs_since_midday);
+    fTime->SetReadyToSave();
+
     /*
      --- USEFULL? NEEDED? ---
-     int   isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
-     int   isecfrac_200ns;     // fractional part of isecs_since_midday
      short snot_ok_flags;      // the bits in these two bytes are flags for additional information on the event: Everything OK =: all Bits = 0
 
@@ -862,4 +883,33 @@
             return kFALSE;
 
+    //
+    // Check for a selector. If one is given and returns kFALSE
+    // skip this event.
+    //
+    if (GetSelector())
+    {
+        //
+        // Make sure selector is processed
+        //
+        if (!GetSelector()->CallProcess())
+        {
+            *fLog << err << dbginf << "Processing Selector failed." << endl;
+            return kFALSE;
+        }
+
+        //
+        // Skip Event
+        //
+        if (!GetSelector()->IsExpressionTrue())
+        {
+            fIn->seekg(sizeof(struct eventrecord), ios::cur);
+
+            fNumEvents++;
+            fNumEventsInRun++;
+
+            return kCONTINUE;
+        }
+    }
+
     // event data to be read from the file
     struct eventrecord event;
@@ -890,4 +940,4 @@
     }
 
-    return kTRUE;
-}
+    return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
+}
Index: trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.h	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.h	(revision 1668)
@@ -7,4 +7,5 @@
 
 class TList;
+class MTime;
 class MMcEvt;
 class MMcTrig;
@@ -27,4 +28,5 @@
     MCerPhotEvt  *fNphot;   // the data container for all data.
     MPedestalCam *fPedest;  // ct1 pedestals
+    MTime        *fTime;    // event time
     MMcEvt       *fMcEvt;   // monte carlo data container for MC files
     MMcTrig      *fMcTrig;  // mc data container for trigger information
@@ -52,4 +54,8 @@
     void   ProcessEvent(const struct eventrecord &event);
 
+    Bool_t PreProcess(MParList *pList);
+    Bool_t Process();
+    Bool_t PostProcess();
+
 public:
     MCT1ReadPreProc(const char *filename=NULL,
@@ -61,8 +67,4 @@
     void AddFile(const char *fname);
 
-    Bool_t PreProcess(MParList *pList);
-    Bool_t Process();
-    Bool_t PostProcess();
-
     UInt_t GetEntries() { return fEntries; }
 
Index: trunk/MagicSoft/Mars/mfileio/MRead.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MRead.cc	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MRead.cc	(revision 1668)
@@ -29,4 +29,9 @@
 // Base class for all reading tasks                                        //
 //                                                                         //
+// You can set a selector. Depending on the impelementation in the derived //
+// class it can be used to skip events, if the filter return kFALSE.       //
+// Make sure that the selector (filter) doesn't need information which     //
+// doesn't exist before reading an event!                                  //
+//                                                                         //
 /////////////////////////////////////////////////////////////////////////////
 #include "MRead.h"
Index: trunk/MagicSoft/Mars/mfileio/MRead.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MRead.h	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MRead.h	(revision 1668)
@@ -6,9 +6,18 @@
 #endif
 
+class MFilter;
+
 class MRead : public MTask
 {
+private:
+    MFilter *fSelector;
+
 public:
+    MRead() : fSelector(NULL) {}
 
     virtual UInt_t GetEntries() = 0;
+
+    void SetSelector(MFilter *f) { fSelector = f; }
+    MFilter *GetSelector() { return fSelector; }
 
     ClassDef(MRead, 0)	// Base class for a reading task
Index: trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc	(revision 1668)
@@ -98,5 +98,5 @@
 //  chains -1 is returned, otherwise the number of files which were added.
 //
-Int_t MReadMarsFile::AddFile(const char *fname)
+Int_t MReadMarsFile::AddFile(const char *fname, Int_t entries)
 {
     //
@@ -107,5 +107,5 @@
     //
     Int_t n1 = fRun->AddFile(fname);
-    Int_t n2 = MReadTree::AddFile(fname);
+    Int_t n2 = MReadTree::AddFile(fname, entries);
 
     return n1 != n2 ? -1 : n1;
Index: trunk/MagicSoft/Mars/mfileio/MReadMarsFile.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadMarsFile.h	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MReadMarsFile.h	(revision 1668)
@@ -24,5 +24,5 @@
     ~MReadMarsFile();
 
-    Int_t AddFile(const char *fname);
+    Int_t AddFile(const char *fname, Int_t entries=-1);
 
     ClassDef(MReadMarsFile, 1)	// Reads a tree from file(s) and the information from the 'RunHeader'-tree
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 1668)
@@ -191,14 +191,44 @@
 //  AddFile returns the number of files added to the chain.
 //
-Int_t MReadTree::AddFile(const char *fname)
-{
+//  For more information see TChain::Add
+//
+Int_t MReadTree::AddFile(const char *fname, Int_t entries)
+{
+#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
+    //
+    // This is a workaround to get rid of crashed if the file doesn't
+    // exist due to non initialized TFile::fProcessIDs
+    //
+    //  (Code taken from TFile::TFile
+    //
+    const char *name;
+
+    TString newname;
+
+    if ((name = gSystem->ExpandPathName(fname)))
+    {
+        newname = name;
+        delete [] name;
+    }
+
+    if (newname.IsNull())
+    {
+        *fLog << err << dbginf << "Error expanding path " << fname << "." << endl;
+        return 0;
+    }
+
+    if (gSystem->AccessPathName(newname, kFileExists))
+    {
+        *fLog << err << "ERROR - File '" << fname << "' does not exist." << endl;
+        return 0;
+    }
+
+    fname = newname.Data();
+#endif
+
     //
     // FIXME! A check is missing whether the file already exists or not.
     //
-    //
-    // returns the number of file which were added
-    //
-
-    const Int_t numfiles = fChain->Add(fname);
+    const Int_t numfiles = fChain->Add(fname, entries);
 
     if (numfiles>0)
@@ -208,4 +238,14 @@
 }
 
+/*
+ // --------------------------------------------------------------------------
+ //
+ //
+ Int_t MReadTree::AddFile(const TChainElement &obj)
+ {
+     return AddFile(obj.GetTitle(), obj.GetEntries());
+ }
+*/
+
 // --------------------------------------------------------------------------
 //
@@ -216,13 +256,17 @@
 Int_t MReadTree::AddFiles(const MReadTree &read)
 {
-    Int_t rc = 0;
-
-    TIter Next(read.fChain->GetListOfFiles());
-    TObject *obj = NULL;
-    while ((obj=Next()))
-        rc += AddFile(obj->GetTitle());
+    const Int_t rc = fChain->Add(read.fChain);
 
     if (rc>0)
         SetBit(kChainWasChanged);
+
+    /*
+     Int_t rc = 0;
+
+     TIter Next(read.fChain->GetListOfFiles());
+     TObject *obj = NULL;
+     while ((obj=Next()))
+         rc += AddFile(*(TChainElement*)obj);
+    */
 
     return rc;
@@ -241,5 +285,5 @@
         return;
 
-    *fLog << inf << GetDescriptor() << ": Branch choosing method enabled (only enabled branches are read)." << endl;
+    *fLog << inf << GetDescriptor() << ": Branch choosing enabled (only enabled branches are read)." << endl;
     fChain->SetBranchStatus("*", kFALSE);
     fBranchChoosing = kTRUE;
@@ -255,4 +299,11 @@
 void MReadTree::EnableBranch(const char *name)
 {
+    if (fChain->GetListOfFiles()->GetEntries()==0)
+    {
+        *fLog << err << "Chain contains no file... Branch '";
+        *fLog << name << "' ignored." << endl;
+        return;
+    }
+
     EnableBranchChoosing();
 
@@ -449,4 +500,6 @@
 //  root environment) the branch is skipped and an error message is printed
 //  out.
+//  If a selector is specified it is preprocessed after the
+//  MReadTree::PreProcess
 //
 Bool_t MReadTree::PreProcess(MParList *pList)
@@ -576,5 +629,5 @@
     fChain->SetNotify(this);
 
-    return kTRUE;
+    return GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
 }
 
@@ -626,4 +679,6 @@
 //  (Remark: The position can also be set by some member functions
 //  If the end of the file is reached the Eventloop is stopped.
+//  In case an event selector is given its value is checked before
+//  reading the event. If it returns kAFLSE the event is skipped.
 //
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
@@ -658,5 +713,26 @@
 #endif
 
-    Bool_t rc = fChain->GetEntry(fNumEntry++) != 0;
+    if (GetSelector())
+    {
+        //
+        // Make sure selector is processed
+        //
+        if (!GetSelector()->CallProcess())
+        {
+            *fLog << err << dbginf << "Processing Selector failed." << endl;
+            return kFALSE;
+        }
+
+        //
+        // Skip Event
+        //
+        if (!GetSelector()->IsExpressionTrue())
+        {
+            fNumEntry++;
+            return kCONTINUE;
+        }
+    }
+
+    const Bool_t rc = fChain->GetEntry(fNumEntry++) != 0;
 
     if (rc)
@@ -664,4 +740,13 @@
 
     return rc;
+}
+
+// --------------------------------------------------------------------------
+//
+//  If a selector is given the selector is post processed
+//
+Bool_t MReadTree::PostProcess()
+{
+    return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
 }
 
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 1667)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 1668)
@@ -64,9 +64,10 @@
     virtual void   Print(Option_t *opt="") const;
 
-    virtual Int_t  AddFile(const char *fname);
+    virtual Int_t  AddFile(const char *fname, Int_t entries=-1);
     virtual Int_t  AddFiles(const MReadTree &read);
 
     virtual Bool_t PreProcess(MParList *pList);
     virtual Bool_t Process();
+    virtual Bool_t PostProcess();
 
     virtual Bool_t Notify();
