Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 1113)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 1114)
@@ -1,3 +1,19 @@
                                                                   -*-*- END -*-*-
+
+ 2001/12/14: Thomas Bretz
+ 
+   * mbase/MReadMarsFile.[h,cc], mbase/MReadFild.[h,cc]:
+     - corrected handling of ReInit/Notify (at the moment I assume 
+       one run per file)
+     - made sure, that we don't get memory leaks when using MReadTree 
+       more than once because the pointer to the pointer isn't deleted.
+     - added a small class MChain which enhances TChain by a function to
+       reset fTree. This is used to control when notification are
+       happening
+
+   * mbase/MTask.cc:
+     - reset the number of executions before the preprocessing
+
+
 
  2001/12/11: Thomas Bretz
Index: /trunk/MagicSoft/Mars/mbase/MReadMarsFile.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MReadMarsFile.cc	(revision 1113)
+++ /trunk/MagicSoft/Mars/mbase/MReadMarsFile.cc	(revision 1114)
@@ -37,4 +37,8 @@
 
 #include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+#include "MTaskList.h"
 
 ClassImp(MReadMarsFile);
@@ -56,4 +60,10 @@
     //
     fRun = new MReadTree("RunHeaders", fname);
+
+    //
+    // This disables the auto scheme. because reading new runheader is done
+    // at a low frequency we don't loose time if we always read all
+    // runheaders
+    //
     fRun->DisableAutoScheme();
 }
@@ -98,12 +108,18 @@
 Bool_t MReadMarsFile::Notify()
 {
-    if (GetEventNum() == 0)
-        return kTRUE;
+    //
+    // Try to read the new run headers. If reading the new run header
+    // was successfull call the ReInits
+    //
+    if (fRun->Process())
+    {
+        *fLog << inf << "MReadMarsFile: Switching to '" << GetFileName() << "' ";
+        *fLog << "(before processing event #" << GetEventNum()-1 << ")" << endl;
 
-    *fLog << "MReadMarsFile: Switching to next file '" << GetFileName() << "' ";
-    *fLog << "(before Event #" << GetEventNum()-1 << ")" << endl;
-    fRun->Process();
-
-    MReadTree::Notify();
+        fTaskList->ReInit();
+        //MReadTree::Notify();
+    }
+    else
+        *fLog << warn << "Warning: Cannot read new runheaders after reading event #" << GetEventNum() << endl;
 
     return kTRUE;
@@ -117,11 +133,9 @@
 Bool_t MReadMarsFile::PreProcess(MParList *pList)
 {
+    fTaskList = (MTaskList*)pList->FindObject("MTaskList");
+
     if (!fRun->PreProcess(pList))
-        return kFALSE;
-
-    if (!fRun->Process())
         return kFALSE;
 
     return MReadTree::PreProcess(pList);
 }
-
Index: /trunk/MagicSoft/Mars/mbase/MReadMarsFile.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MReadMarsFile.h	(revision 1113)
+++ /trunk/MagicSoft/Mars/mbase/MReadMarsFile.h	(revision 1114)
@@ -6,8 +6,11 @@
 #endif
 
+class MTaskList;
+
 class MReadMarsFile : public MReadTree
 {
 private:
     MReadTree *fRun;
+    MTaskList *fTaskList; //! Tasklist for reinitialization
 
     Bool_t Notify();
Index: /trunk/MagicSoft/Mars/mbase/MReadTree.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MReadTree.cc	(revision 1113)
+++ /trunk/MagicSoft/Mars/mbase/MReadTree.cc	(revision 1114)
@@ -69,4 +69,13 @@
 ClassImp(MReadTree);
 
+class MChain : public TChain
+{
+public:
+    MChain() : TChain() {}
+    MChain(const char *name, const char *title="") : TChain(name, title) {}
+
+    void ResetTree() { fTree = 0; }
+};
+
 // --------------------------------------------------------------------------
 //
@@ -95,6 +104,5 @@
     // open the input stream
     //
-    fChain = new TChain(tname);
-    fChain->SetNotify(this);
+    fChain = new MChain(tname);
 
     // root 3.02:
@@ -149,10 +157,8 @@
 Bool_t MReadTree::Notify()
 {
-    //
-    // FIXME: This is correct!
-    //
-    //   fNotify->ForEach(TObject, Notify)();
-
-    fTaskList->ReInit();
+    *fLog << inf << "MReadTree: Notify '" << fChain->GetName() << "' ";
+    *fLog << "(before processing event #" << GetEventNum()-1 << ")" << endl;
+
+    //fNotify->Notify();
 
     return kTRUE;
@@ -347,5 +353,10 @@
 Bool_t MReadTree::PreProcess(MParList *pList)
 {
-    fTaskList = (MTaskList*)pList->FindObject("MTaskList");
+    //
+    // Make sure, that all the following calls doesn't result in
+    // Notifications. This may be dangerous, because the notified
+    // tasks are not preprocessed.
+    //
+    fChain->SetNotify(NULL);
 
     //
@@ -365,4 +376,6 @@
     *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
 
+    *fLog << all << fChain->GetListOfBranches() << endl;
+
     //
     // Get all branches of this tree and
@@ -373,4 +386,7 @@
 
     Int_t num=0;
+
+
+    *fLog << "Start..." << endl;
     //
     // loop over all tasks for processing
@@ -382,4 +398,6 @@
         //
         const char *bname = branch->GetName();
+
+        *fLog << all << bname << endl;
 
         TString oname(bname);
@@ -398,4 +416,17 @@
         }
 
+        MParContainer *obj = pList->FindCreateObj(oname);
+
+        if (!obj)
+        {
+            //
+            // if class is not existing in the (root) environment
+            // we cannot proceed reading this branch
+            //
+            *fLog << warn << dbginf << "Warning: Class '" << oname << "' not existing in dictionary. Branch skipped." << endl;
+            DisableSubBranches(branch);
+            continue;
+        }
+
         //
         // Create a pointer to the pointer to the object in which the
@@ -403,5 +434,5 @@
         // object and we get the pointers from there to delete it.
         //
-        MParContainer **pcont = new MParContainer*;
+        MParContainer **pcont= new MParContainer*;
 
         //
@@ -422,4 +453,12 @@
 
         //
+        // Check whether a Pointer to a pointer already exists, if
+        // we created one already delete it.
+        //
+        TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
+        if (element)
+            delete (MParContainer**)element->GetBaddress();
+
+        //
         // here pcont is a pointer the to container in which the data from
         // the actual branch should be stored - enable branch.
@@ -447,4 +486,14 @@
     if (fProgress)
         fProgress->SetRange(0, fNumEntries);
+
+    //
+    // Now we can start notifying. Reset tree makes sure, that TChain thinks
+    // that the correct file is not yet initialized and reinitilizes it
+    // as soon as the first event is read. This is necessary to call
+    // the notifiers when the first event is read, but after the
+    // PreProcess-function.
+    //
+    fChain->ResetTree();
+    fChain->SetNotify(this);
 
     return kTRUE;
@@ -501,4 +550,10 @@
 Bool_t MReadTree::Process()
 {
+    //
+    // This is necessary due to a bug in TCHain::LoadTree in root.
+    //
+    if (fNumEntry >= fNumEntries)
+        return kFALSE;
+
     Bool_t rc = fChain->GetEntry(fNumEntry++) != 0;
 
Index: /trunk/MagicSoft/Mars/mbase/MReadTree.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MReadTree.h	(revision 1113)
+++ /trunk/MagicSoft/Mars/mbase/MReadTree.h	(revision 1114)
@@ -6,7 +6,6 @@
 #endif
 
-class TChain;
+class MChain;
 class TBranch;
-class MTaskList;
 class TGProgressBar;
 
@@ -14,8 +13,8 @@
 {
 private:
-    TChain *fChain;            // Pointer to tree
+    MChain *fChain;            // Pointer to tree
 
-    UInt_t  fNumEntry;         // Number of actual entry
-    UInt_t  fNumEntries;       // Number of Events in Tree
+    UInt_t  fNumEntry;         // Number of actual entry in chain
+    UInt_t  fNumEntries;       // Number of Events in chain
 
     Bool_t  fBranchChoosing;   // Flag for branch choosing method
@@ -25,5 +24,5 @@
     TList  *fNotify;           // List of TObjects to notify when switching files
 
-    MTaskList      *fTaskList; //! Tasklist for reinitialization
+private:
     TGProgressBar  *fProgress; //! Possible display of status
 
Index: /trunk/MagicSoft/Mars/mbase/MTask.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MTask.cc	(revision 1113)
+++ /trunk/MagicSoft/Mars/mbase/MTask.cc	(revision 1114)
@@ -134,4 +134,6 @@
 Bool_t MTask::CallPreProcess(MParList *plist)
 {
+    fNumExecutions = 0;
+
     if (!PreProcess(plist))
         return kFALSE;
