Ignore:
Timestamp:
07/22/08 19:56:42 (16 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mfileio/MReadTree.cc

    r9034 r9036  
    6060#include <TFile.h>           // TFile::GetName
    6161#include <TSystem.h>         // gSystem->ExpandPath
    62 #include <TLeafElement.h>
     62#include <TLeaf.h>
    6363#include <TChainElement.h>
    6464#include <TFriendElement.h>
     
    7070#include "MTaskList.h"
    7171#include "MParameters.h"
     72#include "MParEmulated.h"
    7273#include "MStatusDisplay.h"
    7374
     
    226227}
    227228
     229void MReadTree::GetListOfBranches(TList &list) const
     230{
     231    list.AddAll(fTree->GetListOfBranches());
     232
     233    TIter NextF(fTree->GetListOfFriends());
     234    TFriendElement *e = 0;
     235    while ((e=(TFriendElement*)NextF()))
     236        list.AddAll(e->GetTree()->GetListOfBranches());
     237}
     238
    228239// --------------------------------------------------------------------------
    229240//
     
    234245Bool_t MReadTree::CheckBranchSize()
    235246{
    236     TArrayI entries(fTree->GetListOfBranches()->GetSize());
    237     Int_t num=0;
    238 
    239     TIter Next(fTree->GetListOfBranches());
    240 
    241     TBranch *element = NULL;
    242     while ((element=(TBranch*)Next()))
    243         entries[num++] = (Int_t)element->GetEntries();
    244 
    245     // Check the number of entries of the branches pair-wise
    246     for (int i=0; i<num; i++)
    247         for (int j=i; j<num; j++)
     247    Int_t entries = -1;
     248
     249    TList list;
     250    GetListOfBranches(list);
     251
     252    TIter Next(&list);
     253    TBranch *b = NULL;
     254    while ((b=(TBranch*)Next()))
     255    {
     256        if (entries>=0 && entries!=(Int_t)b->GetEntries())
    248257        {
    249             if (entries[i]==entries[j])
    250                 continue;
    251 
    252258            *fLog << err << "ERROR - File corruption detected:" << endl;
    253             *fLog << "  Due to several circumstances (such at a bug in MReadTree or wrong" << endl;
    254             *fLog << "  usage of the file UPDATE mode) you may have produced a file in which" << endl;
    255             *fLog << "  at least two branches in the same tree (" << fTree->GetName() << ") have different" << endl;
    256             *fLog << "  number of entries. Sorry, but this file (" << GetFileName() << ")" << endl;
    257             *fLog << "  is unusable." << endl;
     259            *fLog << "  Due to several circumstances  (such at a bug in MReadTree or wrong usgae of" << endl;
     260            *fLog << "  the file  UPDATE  mode)  you may have produced a file in which at least two" << endl;
     261            *fLog << "  branches have different number of  entries.  Sorry, but this combination of" << endl;
     262            *fLog << "  branches, trees and files is unusable." << endl;
    258263            return kFALSE;
    259264        }
     265
     266        entries = (Int_t)b->GetEntries();
     267    }
    260268
    261269    return kTRUE;
     
    678686
    679687    SetBranchStatus(branch->GetListOfBranches(), kFALSE);
     688}
     689
     690MParContainer *MReadTree::FindCreateObj(MParList &plist, const char *cname, const char *name)
     691{
     692    MParContainer *pcont=plist.FindCreateObj(cname, name);
     693    if (!pcont)
     694    {
     695        //
     696        // if class is not existing in the (root) environment
     697        // we cannot proceed reading this branch
     698        //
     699        *fLog << err << dbginf << "ERROR - Class '" << cname;
     700        *fLog << "' for " << name << " not existing in dictionary. Branch skipped." << endl;
     701        return 0;
     702    }
     703
     704    fParList.Add(pcont);
     705    return pcont;
     706}
     707
     708void *MReadTree::GetParameterPtr(MParList &plist, const TString &name, const char *parname)
     709{
     710    if (name=="Int_t"   || name=="UInt_t"   ||
     711        name=="Short_t" || name=="UShort_t" ||
     712        name=="Char_t"  || name=="UChar_t"  ||
     713        name=="Bool_t")
     714    {
     715        MParameterI *par = (MParameterI*)FindCreateObj(plist, "MParameterI", parname);
     716        return par ? par->GetPtr() : 0;
     717    }
     718
     719    if (name=="Float_t" || name=="Double_t")
     720    {
     721        MParameterD *par = (MParameterD*)FindCreateObj(plist, "MParameterD", parname);
     722        return par ? par->GetPtr() : 0;
     723    }
     724
     725    // MParContainer **pcont= new MParContainer*;
     726    // return FindCreateObj(plist, name, parname);
     727
     728    MParEmulated *par = (MParEmulated*)FindCreateObj(plist, "MParEmulated", parname);
     729    if (!par)
     730        return 0;
     731
     732    par->SetClassName(name);
     733    return par->GetPtr();
     734}
     735
     736Bool_t MReadTree::SetBranchAddress(TBranch &b, void *ptr, const char *prefix, const char *type)
     737{
     738    if (!ptr)
     739    {
     740        DisableSubBranches(&b);
     741        return kFALSE;
     742    }
     743
     744    if (fChain)
     745    {
     746        TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(b.GetName());
     747        if (element && IsOwned(*element))
     748            *fLog << warn << "WARNING - Branch address for " << b.GetName() << " was already setup by the user." << endl;
     749    }
     750
     751    const TString bname = b.GetName();
     752    const TString cname = type ? type : b.GetClassName();
     753
     754    fTree->SetBranchAddress(bname, ptr);
     755
     756    *fLog << inf2 << prefix << " address '" << bname << "' ";
     757    if (bname!=cname)
     758        *fLog << "[" << cname << "] ";
     759    *fLog << "setup for reading." << endl;
     760
     761    return kTRUE;
    680762}
    681763
     
    751833    *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
    752834
     835    // Get all branches of this tree and all friends
     836    TList blist;
     837    GetListOfBranches(blist);
     838
    753839    //
    754840    // Get all branches of this tree and
    755841    // create the Iterator to loop over all branches
    756842    //
    757     TIter Next(fTree->GetListOfBranches()); // *CHANGED-fChain-to-fTree*
     843    TIter Next(&blist); // *CHANGED-fChain-to-fTree*
    758844    TBranch *branch=NULL;
    759845
     
    784870        }
    785871
    786         //
    787         // Create a pointer to the pointer to the object in which the
    788         // branch data is stored. The pointers are stored in the TChain
    789         // object and we get the pointers from there to delete it.
    790         //
    791         MParContainer **pcont= new MParContainer*;
    792 
    793 #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
    794         const char *classname = oname;
    795 #else
    796         const char *classname = branch->GetClassName();
    797 #endif
    798 
    799         //
    800         // check if object is existing in the list
    801         //
    802         *pcont=pList->FindCreateObj(classname, oname);
    803 
    804         if (!*pcont)
     872        // Branch already setup/ FIXME: WHAT IF THIS IS A TREE???
     873        if (fChain && fChain->GetStatus()->FindObject(branch->GetName()))
    805874        {
    806             //
    807             // if class is not existing in the (root) environment
    808             // we cannot proceed reading this branch
    809             //
    810             *fLog << warn << dbginf << "Warning: Class '" << classname;
    811             *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
    812             DisableSubBranches(branch);
     875            *fLog << warn << "WARNING - Branch " << branch->GetName() << " already setup." << endl;
    813876            continue;
    814877        }
    815878
    816         //
    817         // Check whether a Pointer to a pointer already exists.
    818         // If we created one already, delete it.
    819         //
    820         // *CHANGED-fChain-to-fTree*
    821         if (fChain)
     879        // Get the corresponding class
     880        const TString classname = branch->GetClassName();
     881
     882        TClass *cls = gROOT->GetClass(classname);
     883        if (!cls)
    822884        {
    823             TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
    824             if (element)
    825                 delete (MParContainer**)element->GetBaddress();
     885            // FIXME: With or without dot?
     886            TLeaf *l = branch->GetLeaf(branch->GetName()); // FIXME: 1st leaf?
     887            if (!l)
     888                continue;
     889
     890            void *ptr = GetParameterPtr(*pList, l->GetTypeName(), oname);
     891            if (SetBranchAddress(*branch, ptr, "Leaf", l->GetTypeName()))
     892                num++;
     893
     894            continue;
    826895        }
    827         /* This can't be done here for memory trees - see
    828            PostProcess for more details.
     896
     897        // The class is known in the dictionary and loaded!
     898        if (!cls->IsLoaded() || !cls->InheritsFrom(MParContainer::Class()))
     899        {
     900            void *ptr = GetParameterPtr(*pList, classname, oname);
     901            if (SetBranchAddress(*branch, ptr, "Emulated branch"))
     902                num++;
     903        }
    829904        else
    830905        {
    831             TBranch *branch = (TBranch*)fTree->GetBranch(bname);
    832             if (branch)
    833             {
    834                 *fLog << bname << " " << (void*)branch->GetAddress() << " " << pcont << endl;
    835                 delete (MParContainer**)branch->GetAddress();
    836             }
     906            MParContainer **pcont= new MParContainer*;
     907            *pcont=FindCreateObj(*pList, classname, oname);
     908            if (SetBranchAddress(*branch, pcont, "Master branch"))
     909                num++;
    837910        }
    838         */
    839 
    840         //
    841         // here pcont is a pointer the to container in which the data from
    842         // the actual branch should be stored - enable branch.
    843         //
    844         fTree->SetBranchAddress(bname, pcont); // *CHANGED-fChain-to-fTree*
    845 
    846         *fLog << inf2 << "Master branch address '" << bname << "' ";
    847         if ((TString)bname!=(TString)classname)
    848             *fLog << "[" << classname << "] ";
    849         *fLog << "setup for reading." << endl;
    850 
    851         //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
    852         //branch->SetAutoDelete();
    853 
    854         num++;
    855911    }
    856912
     
    889945void MReadTree::SetReadyToSave(Bool_t flag)
    890946{
    891     if (!fChain)
    892         return;
    893 
    894     TIter Next(fChain->GetStatus());
    895 
    896     TChainElement *element = NULL;
    897     while ((element=(TChainElement*)Next()))
    898     {
    899         //
    900         // Check whether the branch is enabled
    901         //
    902         if (!element->GetStatus())
    903             continue;
    904 
    905         //
    906         // Get the pointer to the pointer of the corresponding container
    907         //
    908         MParContainer **pcont = (MParContainer**)element->GetBaddress();
    909 
    910         //
    911         // Check whether the pointer is not NULL
    912         //
    913         if (!pcont || !*pcont)
    914             continue;
    915 
    916         //
    917         // Set the ready to save status of the container.
    918         //
    919         (*pcont)->SetReadyToSave(flag);
    920     }
     947    fParList.R__FOR_EACH(MParContainer, SetReadyToSave)(flag);
    921948
    922949    MTask::SetReadyToSave(flag);
Note: See TracChangeset for help on using the changeset viewer.