Index: trunk/MagicSoft/Mars/mfileio/MReadTree.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 9034)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 9036)
@@ -60,5 +60,5 @@
 #include <TFile.h>           // TFile::GetName
 #include <TSystem.h>         // gSystem->ExpandPath
-#include <TLeafElement.h>
+#include <TLeaf.h>
 #include <TChainElement.h>
 #include <TFriendElement.h>
@@ -70,4 +70,5 @@
 #include "MTaskList.h"
 #include "MParameters.h"
+#include "MParEmulated.h"
 #include "MStatusDisplay.h"
 
@@ -226,4 +227,14 @@
 }
 
+void MReadTree::GetListOfBranches(TList &list) const
+{
+    list.AddAll(fTree->GetListOfBranches());
+
+    TIter NextF(fTree->GetListOfFriends());
+    TFriendElement *e = 0;
+    while ((e=(TFriendElement*)NextF()))
+        list.AddAll(e->GetTree()->GetListOfBranches());
+}
+
 // --------------------------------------------------------------------------
 //
@@ -234,28 +245,25 @@
 Bool_t MReadTree::CheckBranchSize()
 {
-    TArrayI entries(fTree->GetListOfBranches()->GetSize());
-    Int_t num=0;
-
-    TIter Next(fTree->GetListOfBranches());
-
-    TBranch *element = NULL;
-    while ((element=(TBranch*)Next()))
-        entries[num++] = (Int_t)element->GetEntries();
-
-    // Check the number of entries of the branches pair-wise
-    for (int i=0; i<num; i++)
-        for (int j=i; j<num; j++)
+    Int_t entries = -1;
+
+    TList list;
+    GetListOfBranches(list);
+
+    TIter Next(&list);
+    TBranch *b = NULL;
+    while ((b=(TBranch*)Next()))
+    {
+        if (entries>=0 && entries!=(Int_t)b->GetEntries())
         {
-            if (entries[i]==entries[j])
-                continue;
-
             *fLog << err << "ERROR - File corruption detected:" << endl;
-            *fLog << "  Due to several circumstances (such at a bug in MReadTree or wrong" << endl;
-            *fLog << "  usage of the file UPDATE mode) you may have produced a file in which" << endl;
-            *fLog << "  at least two branches in the same tree (" << fTree->GetName() << ") have different" << endl;
-            *fLog << "  number of entries. Sorry, but this file (" << GetFileName() << ")" << endl;
-            *fLog << "  is unusable." << endl;
+            *fLog << "  Due to several circumstances  (such at a bug in MReadTree or wrong usgae of" << endl;
+            *fLog << "  the file  UPDATE  mode)  you may have produced a file in which at least two" << endl;
+            *fLog << "  branches have different number of  entries.  Sorry, but this combination of" << endl;
+            *fLog << "  branches, trees and files is unusable." << endl;
             return kFALSE;
         }
+
+        entries = (Int_t)b->GetEntries();
+    }
 
     return kTRUE;
@@ -678,4 +686,78 @@
 
     SetBranchStatus(branch->GetListOfBranches(), kFALSE);
+}
+
+MParContainer *MReadTree::FindCreateObj(MParList &plist, const char *cname, const char *name)
+{
+    MParContainer *pcont=plist.FindCreateObj(cname, name);
+    if (!pcont)
+    {
+        //
+        // if class is not existing in the (root) environment
+        // we cannot proceed reading this branch
+        //
+        *fLog << err << dbginf << "ERROR - Class '" << cname;
+        *fLog << "' for " << name << " not existing in dictionary. Branch skipped." << endl;
+        return 0;
+    }
+
+    fParList.Add(pcont);
+    return pcont;
+}
+
+void *MReadTree::GetParameterPtr(MParList &plist, const TString &name, const char *parname)
+{
+    if (name=="Int_t"   || name=="UInt_t"   ||
+        name=="Short_t" || name=="UShort_t" ||
+        name=="Char_t"  || name=="UChar_t"  ||
+        name=="Bool_t")
+    {
+        MParameterI *par = (MParameterI*)FindCreateObj(plist, "MParameterI", parname);
+        return par ? par->GetPtr() : 0;
+    }
+
+    if (name=="Float_t" || name=="Double_t")
+    {
+        MParameterD *par = (MParameterD*)FindCreateObj(plist, "MParameterD", parname);
+        return par ? par->GetPtr() : 0;
+    }
+
+    // MParContainer **pcont= new MParContainer*;
+    // return FindCreateObj(plist, name, parname);
+
+    MParEmulated *par = (MParEmulated*)FindCreateObj(plist, "MParEmulated", parname);
+    if (!par)
+        return 0;
+
+    par->SetClassName(name);
+    return par->GetPtr();
+}
+
+Bool_t MReadTree::SetBranchAddress(TBranch &b, void *ptr, const char *prefix, const char *type)
+{
+    if (!ptr)
+    {
+        DisableSubBranches(&b);
+        return kFALSE;
+    }
+
+    if (fChain)
+    {
+        TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(b.GetName());
+        if (element && IsOwned(*element))
+            *fLog << warn << "WARNING - Branch address for " << b.GetName() << " was already setup by the user." << endl;
+    }
+
+    const TString bname = b.GetName();
+    const TString cname = type ? type : b.GetClassName();
+
+    fTree->SetBranchAddress(bname, ptr);
+
+    *fLog << inf2 << prefix << " address '" << bname << "' ";
+    if (bname!=cname)
+        *fLog << "[" << cname << "] ";
+    *fLog << "setup for reading." << endl;
+
+    return kTRUE;
 }
 
@@ -751,9 +833,13 @@
     *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
 
+    // Get all branches of this tree and all friends
+    TList blist;
+    GetListOfBranches(blist);
+
     //
     // Get all branches of this tree and
     // create the Iterator to loop over all branches
     //
-    TIter Next(fTree->GetListOfBranches()); // *CHANGED-fChain-to-fTree*
+    TIter Next(&blist); // *CHANGED-fChain-to-fTree*
     TBranch *branch=NULL;
 
@@ -784,73 +870,43 @@
         }
 
-        //
-        // Create a pointer to the pointer to the object in which the
-        // branch data is stored. The pointers are stored in the TChain
-        // object and we get the pointers from there to delete it.
-        //
-        MParContainer **pcont= new MParContainer*;
-
-#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
-        const char *classname = oname;
-#else
-        const char *classname = branch->GetClassName();
-#endif
-
-        //
-        // check if object is existing in the list
-        //
-        *pcont=pList->FindCreateObj(classname, oname);
-
-        if (!*pcont)
+        // Branch already setup/ FIXME: WHAT IF THIS IS A TREE???
+        if (fChain && fChain->GetStatus()->FindObject(branch->GetName()))
         {
-            //
-            // if class is not existing in the (root) environment
-            // we cannot proceed reading this branch
-            //
-            *fLog << warn << dbginf << "Warning: Class '" << classname;
-            *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
-            DisableSubBranches(branch);
+            *fLog << warn << "WARNING - Branch " << branch->GetName() << " already setup." << endl;
             continue;
         }
 
-        //
-        // Check whether a Pointer to a pointer already exists.
-        // If we created one already, delete it.
-        //
-        // *CHANGED-fChain-to-fTree*
-        if (fChain)
+        // Get the corresponding class
+        const TString classname = branch->GetClassName();
+
+        TClass *cls = gROOT->GetClass(classname);
+        if (!cls)
         {
-            TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
-            if (element)
-                delete (MParContainer**)element->GetBaddress();
+            // FIXME: With or without dot?
+            TLeaf *l = branch->GetLeaf(branch->GetName()); // FIXME: 1st leaf?
+            if (!l)
+                continue;
+
+            void *ptr = GetParameterPtr(*pList, l->GetTypeName(), oname);
+            if (SetBranchAddress(*branch, ptr, "Leaf", l->GetTypeName()))
+                num++;
+
+            continue;
         }
-        /* This can't be done here for memory trees - see
-           PostProcess for more details.
+
+        // The class is known in the dictionary and loaded!
+        if (!cls->IsLoaded() || !cls->InheritsFrom(MParContainer::Class()))
+        {
+            void *ptr = GetParameterPtr(*pList, classname, oname);
+            if (SetBranchAddress(*branch, ptr, "Emulated branch"))
+                num++;
+        }
         else
         {
-            TBranch *branch = (TBranch*)fTree->GetBranch(bname);
-            if (branch)
-            {
-                *fLog << bname << " " << (void*)branch->GetAddress() << " " << pcont << endl;
-                delete (MParContainer**)branch->GetAddress();
-            }
+            MParContainer **pcont= new MParContainer*;
+            *pcont=FindCreateObj(*pList, classname, oname);
+            if (SetBranchAddress(*branch, pcont, "Master branch"))
+                num++;
         }
-        */
-
-        //
-        // here pcont is a pointer the to container in which the data from
-        // the actual branch should be stored - enable branch.
-        //
-        fTree->SetBranchAddress(bname, pcont); // *CHANGED-fChain-to-fTree*
-
-        *fLog << inf2 << "Master branch address '" << bname << "' ";
-        if ((TString)bname!=(TString)classname)
-            *fLog << "[" << classname << "] ";
-        *fLog << "setup for reading." << endl;
-
-        //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
-        //branch->SetAutoDelete();
-
-        num++;
     }
 
@@ -889,34 +945,5 @@
 void MReadTree::SetReadyToSave(Bool_t flag)
 {
-    if (!fChain)
-        return;
-
-    TIter Next(fChain->GetStatus());
-
-    TChainElement *element = NULL;
-    while ((element=(TChainElement*)Next()))
-    {
-        //
-        // Check whether the branch is enabled
-        //
-        if (!element->GetStatus())
-            continue;
-
-        //
-        // Get the pointer to the pointer of the corresponding container
-        //
-        MParContainer **pcont = (MParContainer**)element->GetBaddress();
-
-        //
-        // Check whether the pointer is not NULL
-        //
-        if (!pcont || !*pcont)
-            continue;
-
-        //
-        // Set the ready to save status of the container.
-        //
-        (*pcont)->SetReadyToSave(flag);
-    }
+    fParList.R__FOR_EACH(MParContainer, SetReadyToSave)(flag);
 
     MTask::SetReadyToSave(flag);
