Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/rroot/tree is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_rroot_tree
0005 #define tools_rroot_tree
0006 
0007 #include "ifac"
0008 #include "branch_element"
0009 #include "../sout"
0010 #include "iobject"
0011 
0012 namespace tools {
0013 namespace rroot {
0014 
0015 inline const std::string& TTree_cls(){
0016   static const std::string s_v("TTree");
0017   return s_v;
0018 }
0019 
0020 class tree : public virtual iobject {
0021 public:
0022   static const std::string& s_class() {
0023     static const std::string s_v("tools::rroot::tree");
0024     return s_v;
0025   }
0026   virtual const std::string& s_cls() const {return s_class();}
0027 public: //iobject
0028   virtual const std::string& name() const {return m_name;}
0029   virtual const std::string& title() const {return m_title;}
0030   virtual const std::string& store_class_name() const {
0031     static const std::string s_v("TTree");
0032     return s_v;
0033   }
0034 public:
0035   tree(ifile& a_file,ifac& a_fac)
0036   :m_file(a_file)
0037   ,m_fac(a_fac)
0038   ,m_out(a_file.out())
0039   ,m_name("")
0040   ,m_title("")
0041   ,m_branches(a_fac)
0042   ,m_entries(0)
0043   {
0044 #ifdef TOOLS_MEM
0045     mem::increment(s_class().c_str());
0046 #endif
0047   }
0048   virtual ~tree(){
0049 #ifdef TOOLS_MEM
0050     mem::decrement(s_class().c_str());
0051 #endif
0052   }
0053 protected:
0054   tree(const tree& a_from)
0055   :iobject(a_from)
0056   ,m_file(a_from.m_file)
0057   ,m_fac(a_from.m_fac)
0058   ,m_out(a_from.m_out)
0059   ,m_branches(m_fac)
0060   {}
0061   tree& operator=(const tree&){return *this;}
0062 public:
0063   std::ostream& out() const {return m_out;}
0064 
0065   ifile& file() {return m_file;}
0066   ifac& fac() {return m_fac;}
0067 
0068   const std::vector<branch*>& branches() const {return m_branches;}
0069 
0070   bool find_entry(uint64 a_entry,uint32& a_nbytes) {
0071     a_nbytes = 0;
0072     if(a_entry>=m_entries) return false;
0073     int nbytes = 0;
0074     //fReadEntry = a_entry;
0075     tools_vforit(branch*,m_branches,it) {
0076       uint32 n;
0077       if(!(*it)->find_entry(m_file,a_entry,n)) return false;
0078       nbytes += n;
0079     }
0080     a_nbytes = nbytes;
0081     return true;
0082   }
0083 
0084   void dump(std::ostream& a_out,const std::string& a_spaces = "",const std::string& a_indent = " "){
0085     a_out << a_spaces
0086           << "tree :"
0087           << " name=" << sout(m_name)
0088           << " title=" << sout(m_title)
0089           << " entries=" << m_entries
0090           << std::endl;
0091     _dump_branches(a_out,m_branches,a_spaces+a_indent,a_indent);
0092   }
0093 
0094   branch* find_branch(const std::string& a_name,bool a_recursive = false) const {
0095     return _find_branch(m_branches,a_name,a_recursive);
0096   }
0097 
0098   branch_element* find_branch_element(const std::string& a_name,bool a_recursive = false) const {
0099     branch* b = _find_branch(m_branches,a_name,a_recursive);
0100     if(!b) return 0;
0101     return id_cast<branch,branch_element>(*b);
0102   }
0103 
0104   base_leaf* find_leaf(const std::string& a_name,bool a_recursive = false) const {
0105     return _find_leaf(m_branches,a_name,a_recursive);
0106   }
0107 
0108   void find_leaves(std::vector<base_leaf*>& a_leaves) const {
0109     a_leaves.clear();
0110     _find_leaves(m_branches,a_leaves);
0111   }
0112 
0113   void find_branches(std::vector<branch*>& a_branches) const {
0114     a_branches.clear();
0115     _find_branches(m_branches,a_branches);
0116   }
0117 
0118   branch* find_leaf_branch(const base_leaf& a_leaf) const {return _find_leaf_branch(m_branches,a_leaf);}
0119 
0120   bool show(std::ostream& a_out,uint64 a_entry){
0121     a_out << "======> EVENT:" << a_entry << std::endl;
0122     tools_vforit(branch*,m_branches,it) {
0123       if(!(*it)->show(a_out,m_file,a_entry)) return false;
0124     }
0125     return true;
0126   }
0127 
0128   uint64 entries() const {return m_entries;}
0129 
0130   virtual bool stream(buffer& a_buffer) { //virtual for iobject.
0131     //uint64 m_tot_bytes;
0132     //uint64 m_zip_bytes;
0133     //uint64 m_saved_bytes;
0134 
0135     short vers;
0136     unsigned int _s,_c;
0137     if(!a_buffer.read_version(vers,_s,_c)) return false;
0138 
0139     //::printf("debug : tree::stream : version %d count %d\n",vers,c);
0140 
0141     //if (vers > 4) {
0142       //TTree::Class()->ReadBuffer(b, this, vers, s, c);
0143       //if (fEstimate <= 10000) fEstimate = 1000000;
0144       //m_saved_bytes = m_tot_bytes;
0145       //fDirectory = gDirectory;
0146       //gDirectory->Append(this);
0147       //return;
0148     //}
0149 
0150     if(!Named_stream(a_buffer,m_name,m_title)) return false;
0151 
0152    {short color,style,width;
0153     if(!AttLine_stream(a_buffer,color,style,width)) return false;}
0154    {short color,style;
0155     if(!AttFill_stream(a_buffer,color,style)) return false;}
0156     if(!AttMarker_stream(a_buffer)) return false;
0157 
0158     if(vers<=4) {
0159       int dummy_int;
0160 
0161       if(!a_buffer.read(dummy_int)) return false; //fScanField
0162       if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop
0163      {int fMaxVirtualSize;
0164       if(!a_buffer.read(fMaxVirtualSize)) return false;}
0165      {double v;
0166       if(!a_buffer.read(v)) return false;
0167       m_entries = uint64(v);}
0168      {double v;
0169       if(!a_buffer.read(v)) return false;
0170       //m_tot_bytes = uint64(v);
0171       }
0172      {double v;
0173       if(!a_buffer.read(v)) return false;
0174       //m_zip_bytes = uint64(v);
0175       }
0176      {int fAutoSave;
0177       if(!a_buffer.read(fAutoSave)) return false;}
0178       if(!a_buffer.read(dummy_int)) return false; //fEstimate
0179 
0180     } else if(vers<=9) {
0181      {double v;
0182       if(!a_buffer.read(v)) return false;
0183       m_entries = uint64(v);}
0184      {double v;
0185       if(!a_buffer.read(v)) return false;
0186       //m_tot_bytes = uint64(v);
0187       }
0188      {double v;
0189       if(!a_buffer.read(v)) return false;
0190       //m_zip_bytes = uint64(v);
0191       }
0192      {double v;
0193       if(!a_buffer.read(v)) return false;
0194       //m_saved_bytes = uint64(v);
0195       }
0196 
0197       int dummy_int;
0198       if(!a_buffer.read(dummy_int)) return false; //fTimerInterval
0199       if(!a_buffer.read(dummy_int)) return false; //fScanField
0200       if(!a_buffer.read(dummy_int)) return false; //fUpdate
0201       if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop
0202 
0203      {int fMaxVirtualSize;
0204       if(!a_buffer.read(fMaxVirtualSize)) return false;}
0205      {int fAutoSave;
0206       if(!a_buffer.read(fAutoSave)) return false;}
0207       if(!a_buffer.read(dummy_int)) return false; //fEstimate
0208 
0209     } else if(vers<16) { //FIXME : what is the exact version ?
0210       double dummy_double;
0211       int dummy_int;
0212 
0213      {double v;
0214       if(!a_buffer.read(v)) return false;
0215       m_entries = uint64(v);}
0216      {double v;
0217       if(!a_buffer.read(v)) return false;
0218       //m_tot_bytes = uint64(v);
0219       }
0220      {double v;
0221       if(!a_buffer.read(v)) return false;
0222       //m_zip_bytes = uint64(v);
0223       }
0224      {double v;
0225       if(!a_buffer.read(v)) return false;
0226       //m_saved_bytes = uint64(v);
0227       }
0228       if(!a_buffer.read(dummy_double)) return false; //fWeight
0229       if(!a_buffer.read(dummy_int)) return false; //fTimerInterval
0230       if(!a_buffer.read(dummy_int)) return false; //fScanField
0231       if(!a_buffer.read(dummy_int)) return false; //fUpdate
0232       if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop
0233 
0234      {int fMaxVirtualSize;
0235       if(!a_buffer.read(fMaxVirtualSize)) return false;}
0236      {int fAutoSave;
0237       if(!a_buffer.read(fAutoSave)) return false;}
0238       if(!a_buffer.read(dummy_int)) return false; //fEstimate
0239 
0240     } else { //vers>=16
0241       double dummy_double;
0242       int dummy_int;
0243       int64 dummy_int64;
0244 
0245      {uint64 v;
0246       if(!a_buffer.read(v)) return false;
0247       m_entries = v;}
0248      {uint64 v;
0249       if(!a_buffer.read(v)) return false;
0250       //m_tot_bytes = v;
0251       }
0252      {uint64 v;
0253       if(!a_buffer.read(v)) return false;
0254       //m_zip_bytes = v;
0255       }
0256      {uint64 v;
0257       if(!a_buffer.read(v)) return false;
0258       //m_saved_bytes = v;
0259       }
0260       if(vers>=18) {
0261         if(!a_buffer.read(dummy_int64)) return false; //fFlushedBytes
0262       }
0263 
0264       if(!a_buffer.read(dummy_double)) return false; //fWeight
0265 
0266       if(!a_buffer.read(dummy_int)) return false; //fTimerInterval
0267       if(!a_buffer.read(dummy_int)) return false; //fScanField
0268       if(!a_buffer.read(dummy_int)) return false; //fUpdate
0269 
0270       if(vers>=18) {
0271         if(!a_buffer.read(dummy_int)) return false; //fDefaultEntryOffsetLen
0272       }
0273       int fNClusterRange;
0274       if(vers>=20) {
0275         if(!a_buffer.read(fNClusterRange)) return false; //fNClusterRange
0276       }
0277 
0278       if(!a_buffer.read(dummy_int64)) return false; //fMaxEntries
0279       if(!a_buffer.read(dummy_int64)) return false; //fMaxEntryLoop
0280      {uint64 fMaxVirtualSize;
0281       if(!a_buffer.read(fMaxVirtualSize)) return false;}
0282      {uint64 fAutoSave;
0283       if(!a_buffer.read(fAutoSave)) return false;}
0284       if(vers>=18) {
0285         if(!a_buffer.read(dummy_int64)) return false; //fAutoFlush
0286       }
0287       if(!a_buffer.read(dummy_int64)) return false; //fEstimate
0288       if(vers>=20) {
0289         int64* fClusterRangeEnd = 0;       ///<[fNClusterRange] Last entry of a cluster range.
0290         if(!fixed_array_stream<int64>(a_buffer,fNClusterRange,fClusterRangeEnd)) return false;
0291         delete [] fClusterRangeEnd;
0292         int64* fClusterSize = 0;           ///<[fNClusterRange] Number of entries in each cluster for a given range.
0293         if(!fixed_array_stream<int64>(a_buffer,fNClusterRange,fClusterSize)) return false;
0294         delete [] fClusterSize;
0295         dummy _dummy;
0296         if(!_dummy.stream(a_buffer)) { //TIOFeatures fIOFeatures
0297           m_out << "tools::rroot::tree::stream : can't read (dummy) TIOFeatures." << std::endl;
0298           return false;
0299         }
0300       }
0301     }
0302 
0303     //FIXME if (fEstimate <= 10000) fEstimate = 1000000;
0304 
0305     //TObjArray
0306     //The below m_branches.read will create leaves.
0307     //::printf("debug : tree : read branches : begin\n");
0308    {ifac::args args;
0309     if(!m_branches.stream(a_buffer,args)) {
0310       m_out << "tools::rroot::tree::stream : "
0311             << "can't read branches."
0312             << std::endl;
0313       return false;
0314     }}
0315     //::printf("debug : tree : read branches : end\n");
0316 
0317     //TObjArray
0318     // We read leaves in order to keep streaming synchronisation.
0319     // In fact m_leaves are references to existing leaves read by
0320     // the branches in the upper line of code.
0321     //::printf("debug : tree : read leaves : begin\n");
0322    {obj_array<base_leaf> m_leaves(m_fac);
0323     //branch b(m_out,m_fac);
0324     ifac::args args;
0325     //args[ifac::arg_branch()] = &b;
0326     if(!m_leaves.stream(a_buffer,args)) {
0327       m_out << "tools::rroot::tree::stream : "
0328             << "can't read leaves."
0329             << std::endl;
0330       return false;
0331     }}
0332     //::printf("debug : tree : read leaves : end\n");
0333 
0334     if(vers>=10) {
0335       //TList* fAliases
0336       if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) {
0337         m_out << "tools::rroot::tree::stream : "
0338               << "can't read fAliases."
0339               << std::endl;
0340         return false;
0341       }
0342     }
0343 
0344     //m_saved_bytes = m_tot_bytes;
0345    {std::vector<double> v;
0346     if(!Array_stream<double>(a_buffer,v)) return false;} //fIndexValues TArrayD
0347 
0348    {std::vector<int> v;
0349     if(!Array_stream<int>(a_buffer,v)) return false;}    // fIndex (TArrayI).
0350 
0351     if(vers>=16) {
0352       //TVirtualIndex* fTreeIndex //FIXME ???
0353       if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) {
0354         m_out << "tools::rroot::tree::stream : "
0355               << "can't read fTreeIndex."
0356               << std::endl;
0357         return false;
0358       }
0359     }
0360 
0361     if(vers>=6) {
0362       //TList* fFriends
0363       if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) {
0364         m_out << "tools::rroot::tree::stream : "
0365               << "can't read fFriends."
0366               << std::endl;
0367         return false;
0368       }
0369     }
0370 
0371     if(vers>=16) {
0372       //TList* fUserInfo
0373       if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) {
0374         m_out << "tools::rroot::tree::stream : "
0375               << "can't read fUserInfo."
0376               << std::endl;
0377         return false;
0378       }
0379       //TBranchRef* fBranchRef
0380       if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) {
0381         m_out << "tools::rroot::tree::stream : "
0382               << "can't read fBranchRef."
0383               << std::endl;
0384         return false;
0385       }
0386     }
0387 
0388     if(!a_buffer.check_byte_count(_s,_c,TTree_cls())) return false;
0389 
0390     return true;
0391   }
0392 protected:
0393   void _dump_branches(std::ostream& a_out,
0394                              const std::vector<branch*>& a_bs,
0395                              const std::string& a_spaces = "",
0396                              const std::string& a_indent = " "){
0397     tools_vforcit(branch*,a_bs,it) {
0398       if(branch_element* be = safe_cast<branch,branch_element>(*(*it))) {
0399         a_out << a_spaces
0400               << "branch_element :"
0401               << " name=" << sout((*it)->name())
0402               << " title=" << sout((*it)->title())
0403               << " entry_number=" << be->entry_number()
0404               << " ref_cls=" << sout(be->class_name())
0405               << " (type=" << be->type()
0406               << ",id=" << be->id()
0407               << ",stype=" << be->streamer_type()
0408               << ")."
0409               << std::endl;
0410       } else {
0411         a_out << a_spaces
0412               << "branch :"
0413               << " name=" << sout((*it)->name())
0414               << " title=" << sout((*it)->title())
0415               << " entry_number=" << (*it)->entry_number()
0416               << std::endl;
0417       }
0418 
0419 
0420      {const std::vector<base_leaf*>& lvs = (*it)->leaves();
0421       tools_vforcit(base_leaf*,lvs,itl) {
0422         a_out << a_spaces << a_indent
0423               << "leave :"
0424               << " name=" << sout((*itl)->name())
0425               << " title=" << sout((*itl)->title())
0426               << " cls=" << sout((*itl)->s_cls())
0427               << std::endl;
0428       }}
0429 
0430       _dump_branches(a_out,(*it)->branches(),a_spaces+a_indent,a_indent);
0431     }
0432   }
0433 
0434   static branch* _find_branch(const std::vector<branch*>& a_bs,const std::string& a_name,bool a_recursive) {
0435     tools_vforcit(branch*,a_bs,it) {
0436       if(rcmp((*it)->name(),a_name)) return *it;
0437       if(a_recursive) {
0438         branch* br = _find_branch((*it)->branches(),a_name,a_recursive);
0439         if(br) return br;
0440       }
0441     }
0442     return 0;
0443   }
0444 
0445   static base_leaf* _find_leaf(const std::vector<branch*>& a_bs,const std::string& a_name,bool a_recursive) {
0446     tools_vforcit(branch*,a_bs,it) {
0447       tools_vforcit(base_leaf*,(*it)->leaves(),itl) {
0448         if(rcmp((*itl)->name(),a_name)) return *itl;
0449       }
0450       if(a_recursive) {
0451         base_leaf* lf = _find_leaf((*it)->branches(),a_name,a_recursive);
0452         if(lf) return lf;
0453       }
0454     }
0455     return 0;
0456   }
0457 
0458   static void _find_leaves(const std::vector<branch*>& a_bs,std::vector<base_leaf*>& a_leaves) {
0459     tools_vforcit(branch*,a_bs,it) {
0460       tools_vforcit(base_leaf*,(*it)->leaves(),itl) a_leaves.push_back(*itl);
0461       _find_leaves((*it)->branches(),a_leaves);
0462     }
0463   }
0464 
0465   static void _find_branches(const std::vector<branch*>& a_bs,std::vector<branch*>& a_branches){
0466     tools_vforcit(branch*,a_bs,it) {
0467       a_branches.push_back(*it);
0468       _find_branches((*it)->branches(),a_branches);
0469     }
0470   }
0471 
0472   static branch* _find_leaf_branch(const std::vector<branch*>& a_bs,const base_leaf& a_leaf) {
0473     tools_vforcit(branch*,a_bs,itb) {
0474       tools_vforcit(base_leaf*,(*itb)->leaves(),itl) {if(*itl==&a_leaf) return *itb;}
0475      {branch* br = _find_leaf_branch((*itb)->branches(),a_leaf);
0476       if(br) return br;}
0477     }
0478     return 0;
0479   }
0480 protected:
0481   ifile& m_file;
0482   ifac& m_fac;
0483   std::ostream& m_out;
0484   //Named
0485   std::string m_name;
0486   std::string m_title;
0487 
0488   obj_array<branch> m_branches;
0489   uint64 m_entries;   // Number of entries
0490 };
0491 
0492 }}
0493 
0494 #endif