Index: /trunk/MagicSoft/Mars/mfileio/MReadTree.cc
===================================================================
--- /trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 9033)
+++ /trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 9034)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2005
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -60,5 +60,7 @@
 #include <TFile.h>           // TFile::GetName
 #include <TSystem.h>         // gSystem->ExpandPath
+#include <TLeafElement.h>
 #include <TChainElement.h>
+#include <TFriendElement.h>
 #include <TOrdCollection.h>
 
@@ -67,9 +69,9 @@
 #include "MParList.h"
 #include "MTaskList.h"
+#include "MParameters.h"
 #include "MStatusDisplay.h"
 
 #include "MLog.h"
 #include "MLogManip.h"
-
 
 ClassImp(MReadTree);
@@ -150,4 +152,19 @@
 }
 
+Bool_t MReadTree::IsOwned(const TChainElement &e) const
+{
+    // We cannot be the owner, because it is a basic type like Int, etc.
+    if (!e.GetBaddressIsPtr())
+        return kFALSE;
+
+    // We cannot be the owener, because there is no class
+    // we could have created
+    if (TString(e.GetBaddressClassName()).IsNull())
+        return kFALSE;
+
+    // Let's hope the best that we are the owner
+    return kTRUE;
+}
+
 // --------------------------------------------------------------------------
 //
@@ -165,16 +182,15 @@
         TIter Next(fChain->GetStatus());
 
+        // Pointers which we delete must be
+        //  1) Really pointers
+        //  2) Have a class name (belong to a loaded class)
         TChainElement *element = NULL;
         while ((element=(TChainElement*)Next()))
-            if (element->GetBaddress())
-                delete (MParContainer**)element->GetBaddress();
+            if (IsOwned(*element))
+                delete (void**)element->GetBaddress();
 
         //
         // Delete the chain and the veto list
         //
-#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,00)
-        if (fChain->GetFile())
-            delete fChain->GetFile();
-#endif
         delete fChain;
     }
@@ -346,49 +362,16 @@
     }
 
-#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
-    //
-    TString newname; // char-array must overcome comming block
-
-    if (strrchr(fname, '?') || strrchr(fname, '*'))
-    {
-        *fLog << warn;
-        *fLog<< "WARNING: Using widcards with older root versions:" << endl;
-        *fLog << "  You may encounter crashes closing the files..." << endl;
-    }
-    else
-    {
-        const char *name;
-
-        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
+    TString newname(fname); // char-array must overcome comming block
+    gSystem->ExpandPathName(newname);
 
     //
     // FIXME! A check is missing whether the file already exists or not.
     //
-    const Int_t numfiles = fChain->Add(fname, entries<0?TChain::kBigNumber:entries);
+    const Int_t numfiles = fChain->Add(newname, entries<0?TChain::kBigNumber:entries);
+
+    //TIter Next(fChain->GetListOfFriends());
+    //TFriendElement *el = 0;
+    //while ((el=(TFriendElement*)Next()))
+    //    static_cast<TChain*>(el->GetTree())->Add(newname, entries<0?TChain::kBigNumber:entries);
 
     if (numfiles>0)
@@ -399,7 +382,20 @@
     }
     else
-        *fLog << warn << "WARNING: '" << fname << "' not added to " << GetDescriptor() << endl;
+        *fLog << warn << "WARNING: '" << newname << "' not added to " << GetDescriptor() << endl;
 
     return numfiles;
+}
+
+Bool_t MReadTree::AddFriend(TChain &c)
+{
+//    if (!fChain)
+//    {
+//        *fLog << err << "MReadTree::AddFriend - ERROR: You cannot add a friend, because MReadTree" << endl;
+//        *fLog <<        "  is supposed to read its tree from memory or its default or its" << endl;
+//        *fLog <<        "  default constructor was called (no tree name was given)." << endl;
+//        return 0;
+//    }
+
+    return fChain->AddFriend(&c, "")!=0;
 }
 
@@ -924,7 +920,4 @@
     }
 
-    //
-    // Set the ready to save status of this task (used?), too
-    //
     MTask::SetReadyToSave(flag);
 }
@@ -939,35 +932,6 @@
 //  reading the event. If it returns kAFLSE the event is skipped.
 //
-#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
-#include "MRawEvtData.h"
-#endif
 Int_t MReadTree::Process()
 {
-    //
-    // This is necessary due to a bug in TChain::LoadTree in root.
-    // will be fixed in 3.03
-    //
-#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
-    if (fNumEntry >= GetEntries())
-        return kFALSE;
-#endif
-
-#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
-    //
-    // This fixes 99.9% of a memory leak using a root version prior
-    // to 3.02/??
-    //
-    TChainElement *element=NULL;
-    TIter Next(fChain->GetStatus());
-    while ((element=(TChainElement*)Next()))
-    {
-        MParContainer **c = (MParContainer**)element->GetBaddress();
-        if (!c) continue;
-        if ((*c)->InheritsFrom(MRawEvtData::Class()))
-            static_cast<MRawEvtData*>(*c)->DeletePixels(kFALSE);
-
-    }
-#endif
-
     if (GetSelector())
     {
@@ -1033,5 +997,5 @@
     // memory address or a pending address set long before.
     // So we delete the stuff in PostProcess and not the destructor
-    // (which might seg faullt if PreProcess has never been called)
+    // (which might seg fault if PreProcess has never been called)
     if (!fChain)
     {
Index: /trunk/MagicSoft/Mars/mfileio/MReadTree.h
===================================================================
--- /trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 9033)
+++ /trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 9034)
@@ -7,4 +7,6 @@
 
 class MChain;
+class TChain;
+class TChainElement;
 class TBranch;
 class TTree;
@@ -38,4 +40,6 @@
     void SetBranchStatus(TObject *branch, Bool_t status);
     void SetBranchStatus(const char *name, Bool_t status);
+
+    Bool_t IsOwned(const TChainElement &e) const;
 
     void DisableSubBranches(TBranch *b);
@@ -85,4 +89,5 @@
     virtual Int_t AddFiles(const MReadTree &read);
     Int_t AddFiles(MDirIter &dir) { return MRead::AddFiles(dir); }
+    Bool_t AddFriend(TChain &c);
 
     Int_t PreProcess(MParList *pList);
