Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8298)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8299)
@@ -53,4 +53,8 @@
    * msignal/MExtractTimeAndChargeSpline.cc:
      - set the correct numbers for rise- and fall-time as default
+
+   * mbase/MStatusArray.[h,cc]:
+     - improved workaround in Read() and Write() for the Reset
+       of the kCandelete bit in TH1
 
 
Index: trunk/MagicSoft/Mars/mbase/MStatusArray.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MStatusArray.cc	(revision 8298)
+++ trunk/MagicSoft/Mars/mbase/MStatusArray.cc	(revision 8299)
@@ -237,5 +237,5 @@
             if (lvl>0)
                 gLog << setw(lvl) << ' ';
-            gLog << o->ClassName() << ": " << o->GetName() << " <" << Next.GetOption() << "> (" << o << ")" << endl;
+            gLog << " " << o->ClassName() << ": " << o->GetName() << " <" << Next.GetOption() << "> (" << o << ") " << (int)o->TestBit(kCanDelete) << endl;
         }
 
@@ -245,4 +245,17 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Print recursively all objects in this and sub-pads. If !option.IsNull()
+// only objects in the corresponding pad are printed.
+//
+void MStatusArray::Print(Option_t *option) const
+{
+    const TString opt(option);
+
+    PrintObjectsInPad(this, opt);
+}
+
+/*
 // --------------------------------------------------------------------------
 //
@@ -261,4 +274,91 @@
         else
             o->SetBit(kCanDelete|kMustCleanup);
+    }
+}
+*/
+
+// --------------------------------------------------------------------------
+//
+// Set kCanDelete for all objects for which kMyCanDelete is set. This
+// is a STUPID workaruond for an ANNOYING root bug which is that
+// the streamer function of TH1 resets the KCanDelete bit after reading.
+//
+void MStatusArray::SetCanDelete(const TCollection *list) const
+{
+    TIter Next(list);
+    TObject *o=0;
+    while ((o=Next()))
+    {
+        if (o->InheritsFrom(TVirtualPad::Class()))
+            SetCanDelete(((TVirtualPad*)o)->GetListOfPrimitives());
+        else
+        {
+            if (o->TestBit(kMyCanDelete) && o->InheritsFrom("TH1"))
+            {
+                o->SetBit(kCanDelete);
+                o->ResetBit(kMyCanDelete);
+            }
+        }
+    }
+}
+
+void MStatusArray::EnableTH1Workaround(const TCollection *list) const
+{
+    TIter Next(list?list:this);
+    TObject *o=0;
+    while ((o=Next()))
+    {
+        if (o->InheritsFrom(TVirtualPad::Class()))
+            EnableTH1Workaround(((TVirtualPad*)o)->GetListOfPrimitives());
+        else
+            if (o->InheritsFrom("TH1"))
+                o->SetBit(kCanDelete);
+    }
+}
+
+// --------------------------------------------------------------------------
+//
+// Set kMyCanDelete for all objects for which kCanDelete is set. This
+// is a STUPID workaruond for an ANNOYING root bug which is that
+// the streamer function of TH1 resets the KCanDelete bit after reading.
+//
+void MStatusArray::SetMyCanDelete(const TCollection *list) const
+{
+    TIter Next(list);
+    TObject *o=0;
+    while ((o=Next()))
+    {
+        if (o->InheritsFrom(TVirtualPad::Class()))
+            SetMyCanDelete(((TVirtualPad*)o)->GetListOfPrimitives());
+        else
+        {
+            if (o->TestBit(kMyCanDelete) && o->InheritsFrom("TH1"))
+                gLog << warn << "WARNING - MStatusArray::Write - " << o->GetName() << " [" << o->ClassName() << "] has BIT(30) already set!" << endl;
+
+            if (o->TestBit(kCanDelete) && o->InheritsFrom("TH1"))
+                o->SetBit(kMyCanDelete);
+        }
+    }
+}
+
+// --------------------------------------------------------------------------
+//
+// Reset kMyCanDelete for all objects for which kMyCanDelete is set. This
+// is a STUPID workaruond for an ANNOYING root bug which is that
+// the streamer function of TH1 resets the KCanDelete bit after reading.
+//
+void MStatusArray::ResetMyCanDelete(const TCollection *list) const
+{
+    TIter Next(list);
+    TObject *o=0;
+    while ((o=Next()))
+    {
+        if (o->InheritsFrom(TVirtualPad::Class()))
+            ResetMyCanDelete(((TVirtualPad*)o)->GetListOfPrimitives());
+        else
+        {
+            if (o->TestBit(kMyCanDelete) && o->InheritsFrom("TH1"))
+                o->ResetBit(kMyCanDelete);
+        }
     }
 }
@@ -280,4 +380,22 @@
     TH1::AddDirectory(store);
 
+    // All objects in the list (TNamed, TCanvas, etc) do not have
+    // the kCanDelete bit set. Make sure that it is set to make
+    // them deleted by the destructor of this list
+    TIter Next(this);
+    TObject *o=0;
+    while ((o=Next()))
+    {
+        if (o->InheritsFrom(TVirtualPad::Class()))
+        {
+            TIter Next2(((TVirtualPad*)o)->GetListOfPrimitives());
+            TObject *o2=0;
+            while ((o2=Next2()))
+                if (o2->InheritsFrom("MParContainer"))
+                    o2->SetBit(kCanDelete);
+        }
+        o->SetBit(kCanDelete);
+    }
+
     // Make sure that all kCanDelete bits are properly set
     SetCanDelete(this);
@@ -289,11 +407,13 @@
 // --------------------------------------------------------------------------
 //
-// Print recursively all objects in this and sub-pads. If !option.IsNull()
-// only objects in the corresponding pad are printed.
-//
-void MStatusArray::Print(Option_t *option) const
-{
-    const TString opt(option);
-
-    PrintObjectsInPad(this, opt);
-}
+// Switch off adding histograms to current directory before reading.
+// Switch back
+//
+Int_t MStatusArray::Write(const char *name, Int_t option, Int_t bufsize) const
+{
+    SetMyCanDelete(this);
+    const Int_t rc = TObjArray::Write(name, option, bufsize);
+    ResetMyCanDelete(this);
+
+    return rc;
+}
Index: trunk/MagicSoft/Mars/mbase/MStatusArray.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MStatusArray.h	(revision 8298)
+++ trunk/MagicSoft/Mars/mbase/MStatusArray.h	(revision 8299)
@@ -15,5 +15,11 @@
 {
 private:
+    enum {
+        kMyCanDelete = BIT(30)
+    };
+
     void     SetCanDelete(const TCollection *list) const;
+    void     SetMyCanDelete(const TCollection *list) const;
+    void     ResetMyCanDelete(const TCollection *list) const;
     void     PrintObjectsInPad(const TCollection *list, const TString &name, Int_t lvl=0) const;
     TObject *FindObjectInPad(TVirtualPad *pad, const char *object, TClass *base) const;
@@ -43,4 +49,11 @@
 
     Int_t Read(const char *name=NULL);
+    Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0) const;
+    Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
+    {
+        return const_cast<const MStatusArray*>(this)->Write(name, option, bufsize);
+    }
+
+    void EnableTH1Workaround(const TCollection *list=0) const;
 
     ClassDef(MStatusArray, 0) // Helper class for status display
