Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 6372)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 6373)
@@ -22,4 +22,16 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2005/02/11 Thomas Bretz
+
+  * mfileio/MWriteRootFile.[h,cc]:
+    - added the possibility to write to an already existing file.
+      This doesn't affect the old behaviour, but using this new feature
+      might still have problems.
+
+  * mfileio/MReadReports.cc: 
+    - fixed column-level in PrintStatistics
+
+
+
  2005/02/10 Abelardo Moralejo 
 
@@ -29,4 +41,5 @@
    * macros/starmc2.C
      - Added comment about value of cleaning levels
+
 
 
@@ -52,4 +65,5 @@
      - make sure that all variables are set correctly independent on 
        the order of their initialization by the user
+
 
 
Index: /trunk/MagicSoft/Mars/mfileio/MReadReports.cc
===================================================================
--- /trunk/MagicSoft/Mars/mfileio/MReadReports.cc	(revision 6372)
+++ /trunk/MagicSoft/Mars/mfileio/MReadReports.cc	(revision 6373)
@@ -425,4 +425,4 @@
 {
     MRead::PrintStatistics(lvl, title, time);
-    fTrees->PrintStatistics(lvl, title, GetCpuTime());
-}
+    fTrees->PrintStatistics(lvl+1, title, GetCpuTime());
+}
Index: /trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- /trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 6372)
+++ /trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 6373)
@@ -97,4 +97,7 @@
     //
     //fTrees.SetOwner();
+
+    gROOT->GetListOfCleanups()->Add(this); // To remove fDisplay
+    SetBit(kMustCleanup);
 }
 
@@ -188,5 +191,17 @@
     // Open the rootfile
     //
-    fOut = new TFile(str, opt, ftitle, comp);
+    TObject *find = gROOT->FindObject(str);
+    if (find && find->InheritsFrom(TFile::Class()))
+    {
+        fOut = (TFile*)find;
+        fOut->SetBit(kMustCleanup);
+        SetBit(kIsNotOwner);
+
+        *fLog << inf << "File '" << fname << "' already open... using." << endl;
+        *fLog << "Make sure that you do NOT write to trees which are" << endl;
+        *fLog << "scheduled already by a different MWriteRootFile..." << endl;
+    }
+    else
+        fOut = new TFile(str, opt, ftitle, comp);
 }
 
@@ -203,5 +218,5 @@
     Print();
 
-    if (fOut)
+    if (fOut && !TestBit(kIsNotOwner))
     {
         //
@@ -216,15 +231,15 @@
         //
         delete fOut;
-        fOut = 0;
-    }
-
-    //
-    // Remark:
-    // - Trees are automatically deleted by the the file
-    //   (unless file.SetDirectory(0) was called)
-    // - Branches are automatically deleted by the tree destructor
-    //
-
-    *fLog << inf << "Output File closed and object deleted." << endl;
+
+        //
+        // Remark:
+        // - Trees are automatically deleted by the the file
+        //   (unless file.SetDirectory(0) was called)
+        // - Branches are automatically deleted by the tree destructor
+        //
+        *fLog << inf << "Output File closed and object deleted." << endl;
+    }
+
+    fOut = 0;
 }
 
@@ -244,4 +259,7 @@
 void MWriteRootFile::Print(Option_t *) const
 {
+    if (!fOut)
+        return;
+
     *fLog << all << underline << "File: " << GetFileName() << endl;
 
@@ -267,5 +285,5 @@
         name += branch->GetName();
 
-        *fLog << " " << name.Strip(TString::kTrailing, '.') << ": \t" << (ULong_t)branch->GetEntries() << " entries." << endl;
+        *fLog << " + " << name.Strip(TString::kTrailing, '.') << ": \t" << (ULong_t)branch->GetEntries() << " entries." << endl;
     }
 
@@ -274,6 +292,19 @@
     while ((t=(TTree*)NextTree()))
         if (t->TestBit(kIsNewTree))
-            *fLog << " " << t->GetName() << ": \t" << (ULong_t)t->GetEntries() << " entries." << endl;
+            *fLog << " + " << t->GetName() << ": \t" << (ULong_t)t->GetEntries() << " entries." << endl;
+
+    TIter NextKey(fOut->GetListOfKeys());
+    while ((obj=NextKey()))
+    {
+        if (!obj->InheritsFrom(TTree::Class()))
+            continue;
+
+        if (fTrees.FindObject(obj) && obj->TestBit(kIsNewTree))
+            continue;
+
+        *fLog << " - " << obj->GetName() << ": \t" << (ULong_t)((TTree*)t)->GetEntries() << " entries." << endl;
+    }
     *fLog << endl;
+
 }
 
@@ -662,4 +693,19 @@
 Bool_t MWriteRootFile::ChangeFile(const char *fname)
 {
+    if (!fOut)
+    {
+        TObject *find = gROOT->FindObject(fname);
+        if (find && find->InheritsFrom(TFile::Class()))
+        {
+            fOut = (TFile*)find;
+            SetBit(kIsNotOwner);
+            return kTRUE;
+        }
+
+        *fLog << err << "MWriteRootFile::ChangeFile: Expecting file '" << fname;
+        *fLog << "' in gROOT->FindObject... not found." << endl;
+        return kFALSE;
+    }
+
     //
     // The following code is more or less a copy of TTree::ChangeFile
@@ -879,4 +925,13 @@
 }
 
+void MWriteRootFile::RecursiveRemove(TObject *obj)
+{
+    if (obj==fOut && TestBit(kIsNotOwner))
+    {
+        ResetBit(kIsNotOwner);
+        fOut=0;
+    }
+}
+
 // --------------------------------------------------------------------------
 //
@@ -925,2 +980,3 @@
     }
 }
+
Index: /trunk/MagicSoft/Mars/mfileio/MWriteRootFile.h
===================================================================
--- /trunk/MagicSoft/Mars/mfileio/MWriteRootFile.h	(revision 6372)
+++ /trunk/MagicSoft/Mars/mfileio/MWriteRootFile.h	(revision 6373)
@@ -82,9 +82,10 @@
 
     enum {
-        kFillTree  = BIT(14),
+        kIsNotOwner = BIT(14), // MWriteRootFile is not owner of fOut
+        kFillTree   = BIT(14),
         // Be carefull these bits are already in use!
         // TBranch::kAutoDelete = BIT(15)
         // TBranchElement::kDeleteObject = BIT(16)
-        kIsNewTree = BIT(17)
+        kIsNewTree  = BIT(17)
     };
 
@@ -130,4 +131,6 @@
     Bool_t cd(const char *path=0);
 
+    void RecursiveRemove(TObject *obj);
+
     ClassDef(MWriteRootFile, 1)	// Task to write data into a root file
 };
