inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/rroot/tree
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_rroot_tree
00005 #define inlib_rroot_tree
00006 
00007 #include "ifac"
00008 #include "branch_element"
00009 #include "../sout"
00010 
00011 namespace inlib {
00012 namespace rroot {
00013 
00014 inline const std::string& TTree_cls(){
00015   static const std::string s_v("TTree");
00016   return s_v;  
00017 }
00018 
00019 class tree {
00020   static const std::string& s_class() {
00021     static const std::string s_v("inlib::rroot::tree");
00022     return s_v;
00023   }
00024 public:
00025   tree(ifile& a_file,ifac& a_fac)
00026   :m_file(a_file)
00027   ,m_fac(a_fac)
00028   ,m_out(a_file.out())
00029   ,m_name("")
00030   ,m_title("")
00031   ,m_branches(true)
00032   ,m_entries(0)
00033   {
00034 #ifdef INLIB_MEM
00035     mem::increment(s_class().c_str());
00036 #endif
00037   }
00038   virtual ~tree(){
00039 #ifdef INLIB_MEM
00040     mem::decrement(s_class().c_str());
00041 #endif
00042   }
00043 protected:
00044   tree(const tree& a_from)
00045   :m_file(a_from.m_file)
00046   ,m_fac(a_from.m_fac)
00047   ,m_out(a_from.m_out)
00048   ,m_branches(false)
00049   {}
00050   tree& operator=(const tree&){return *this;}
00051 public:
00052   ifile& file() {return m_file;}
00053 
00054   const std::string& name() const {return m_name;}
00055   const std::string& title() const {return m_title;}
00056 
00057   const std::vector<branch*>& branches() const {return m_branches;}
00058 
00059   bool find_entry(uint32 a_entry,uint32& a_nbytes) {
00060     a_nbytes = 0;
00061     if(a_entry>=m_entries) return false;
00062     int nbytes = 0;
00063     //fReadEntry = a_entry;
00064     std::vector<branch*>::const_iterator it;
00065     for(it=m_branches.begin();it!=m_branches.end();++it) {
00066       uint32 n;
00067       if(!(*it)->find_entry(a_entry,n)) return false;
00068       nbytes += n;
00069     }
00070     a_nbytes = nbytes;
00071     return true;
00072   }
00073 
00074   void dump(std::ostream& a_out,
00075                    const std::string& a_spaces = "",
00076                    const std::string& a_indent = " "){
00077     a_out << a_spaces
00078           << "tree :"
00079           << " name=" << sout(m_name)
00080           << " title=" << sout(m_title)
00081           << " entries=" << m_entries
00082           << std::endl;
00083     _dump_branches(a_out,m_branches,a_spaces+a_indent,a_indent);
00084   }
00085  
00086   branch* find_branch(const std::string& a_name,
00087                              bool a_recursive = false) const {
00088     return _find_branch(m_branches,a_name,a_recursive);
00089   }
00090 
00091   std::vector<base_leaf*> find_leaves(){
00092     std::vector<base_leaf*> leaves;
00093     _find_leaves(m_branches,leaves);
00094     return leaves;
00095   }
00096  
00097   //branch* find_leaf_branch(base_leaf* a_leaf){
00098   //  return _find_leaf_branch(m_branches,a_leaf);
00099   //}
00100  
00101   bool show(std::ostream& a_out,uint32 a_entry){
00102     a_out << "======> EVENT:" << a_entry << std::endl;
00103     std::vector<branch*>::const_iterator it;
00104     for(it=m_branches.begin();it!=m_branches.end();++it) {
00105       if(!(*it)->show(a_out,a_entry)) return false;
00106     }
00107     return true;
00108   }
00109 
00110   uint64 entries() const {return m_entries;}
00111 
00112   bool stream(buffer& a_buffer){
00113     uint64 m_tot_bytes;
00114     uint64 m_zip_bytes;
00115     uint64 m_saved_bytes;
00116 
00117     short v;
00118     unsigned int s, c;
00119     if(!a_buffer.read_version(v,s,c)) return false;
00120 
00121     //::printf("debug : tree::stream : version %d count %d\n",v,c);
00122 
00123     //if (v > 4) {
00124       //TTree::Class()->ReadBuffer(b, this, v, s, c);
00125       //if (fEstimate <= 10000) fEstimate = 1000000;
00126       //m_saved_bytes = m_tot_bytes;
00127       //fDirectory = gDirectory;
00128       //gDirectory->Append(this);
00129       //return;
00130     //}
00131 
00132     if(!Named_stream(a_buffer,m_name,m_title)) return false;
00133 
00134     if(!AttLine_stream(a_buffer)) return false;
00135     if(!AttFill_stream(a_buffer)) return false;
00136     if(!AttMarker_stream(a_buffer)) return false;
00137 
00138     if(v<=4) {
00139       int dummy_int;
00140 
00141       if(!a_buffer.read(dummy_int)) return false; //fScanField
00142       if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop
00143      {int fMaxVirtualSize;
00144       if(!a_buffer.read(fMaxVirtualSize)) return false;}
00145      {double v;
00146       if(!a_buffer.read(v)) return false;
00147       m_entries = uint64(v);}
00148      {double v;
00149       if(!a_buffer.read(v)) return false;
00150       m_tot_bytes = uint64(v);}
00151      {double v;
00152       if(!a_buffer.read(v)) return false;
00153       m_zip_bytes = uint64(v);}
00154      {int fAutoSave;
00155       if(!a_buffer.read(fAutoSave)) return false;}
00156       if(!a_buffer.read(dummy_int)) return false; //fEstimate
00157 
00158     } else if(v<=9) {
00159      {double v;
00160       if(!a_buffer.read(v)) return false;
00161       m_entries = uint64(v);}
00162      {double v;
00163       if(!a_buffer.read(v)) return false;
00164       m_tot_bytes = uint64(v);}
00165      {double v;
00166       if(!a_buffer.read(v)) return false;
00167       m_zip_bytes = uint64(v);}
00168      {double v;
00169       if(!a_buffer.read(v)) return false;
00170       m_saved_bytes = uint64(v);}
00171 
00172       int dummy_int;
00173       if(!a_buffer.read(dummy_int)) return false; //fTimerInterval
00174       if(!a_buffer.read(dummy_int)) return false; //fScanField
00175       if(!a_buffer.read(dummy_int)) return false; //fUpdate
00176       if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop
00177 
00178      {int fMaxVirtualSize;
00179       if(!a_buffer.read(fMaxVirtualSize)) return false;}
00180      {int fAutoSave;
00181       if(!a_buffer.read(fAutoSave)) return false;}
00182       if(!a_buffer.read(dummy_int)) return false; //fEstimate
00183 
00184     } else if(v<16) { //FIXME : what is the exact version ?
00185       double dummy_double;
00186       int dummy_int;
00187 
00188      {double v;
00189       if(!a_buffer.read(v)) return false;
00190       m_entries = uint64(v);}
00191      {double v;
00192       if(!a_buffer.read(v)) return false;
00193       m_tot_bytes = uint64(v);}
00194      {double v;
00195       if(!a_buffer.read(v)) return false;
00196       m_zip_bytes = uint64(v);}
00197      {double v;
00198       if(!a_buffer.read(v)) return false;
00199       m_saved_bytes = uint64(v);}
00200       if(!a_buffer.read(dummy_double)) return false; //fWeight
00201       if(!a_buffer.read(dummy_int)) return false; //fTimerInterval
00202       if(!a_buffer.read(dummy_int)) return false; //fScanField
00203       if(!a_buffer.read(dummy_int)) return false; //fUpdate
00204       if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop
00205 
00206      {int fMaxVirtualSize;
00207       if(!a_buffer.read(fMaxVirtualSize)) return false;}
00208      {int fAutoSave;
00209       if(!a_buffer.read(fAutoSave)) return false;}
00210       if(!a_buffer.read(dummy_int)) return false; //fEstimate
00211 
00212     } else { //v>=16
00213       double dummy_double;
00214       int dummy_int;
00215       int64 dummy_int64;
00216 
00217      {uint64 v;
00218       if(!a_buffer.read(v)) return false;
00219       m_entries = v;}
00220      {uint64 v;
00221       if(!a_buffer.read(v)) return false;
00222       m_tot_bytes = v;}
00223      {uint64 v;
00224       if(!a_buffer.read(v)) return false;
00225       m_zip_bytes = v;}
00226      {uint64 v;
00227       if(!a_buffer.read(v)) return false;
00228       m_saved_bytes = v;}
00229       if(!a_buffer.read(dummy_double)) return false; //fWeight
00230 
00231       if(!a_buffer.read(dummy_int)) return false; //fTimerInterval
00232       if(!a_buffer.read(dummy_int)) return false; //fScanField
00233       if(!a_buffer.read(dummy_int)) return false; //fUpdate
00234 
00235       if(!a_buffer.read(dummy_int64)) return false; //fMaxEntries
00236       if(!a_buffer.read(dummy_int64)) return false; //fMaxEntryLoop
00237      {uint64 fMaxVirtualSize;
00238       if(!a_buffer.read(fMaxVirtualSize)) return false;}
00239      {uint64 fAutoSave;
00240       if(!a_buffer.read(fAutoSave)) return false;}
00241       if(!a_buffer.read(dummy_int64)) return false; //fEstimate
00242 
00243     }
00244     //FIXME if (fEstimate <= 10000) fEstimate = 1000000;
00245 
00246     //TObjArray
00247     //The below m_branches.read will create leaves.
00248     //::printf("debug : tree : read branches : begin\n");
00249    {ifac::args args;
00250     if(!m_branches.stream(a_buffer,m_fac,args)) {
00251       m_out << "inlib::rroot::tree::stream : "
00252             << "can't read branches."
00253             << std::endl;
00254       return false;
00255     }}
00256     //::printf("debug : tree : read branches : end\n");
00257 
00258     //TObjArray
00259     // We read leaves in order to keep streaming synchronisation.
00260     // In fact m_leaves are references to existing leaves read by 
00261     // the branches in the upper line of code.
00262     //::printf("debug : tree : read leaves : begin\n");
00263    {ObjArray<base_leaf> m_leaves(true);
00264     ifac::args args;
00265     if(!m_leaves.stream(a_buffer,m_fac,args)) {
00266       m_out << "inlib::rroot::tree::stream : "
00267             << "can't read leaves."
00268             << std::endl;
00269       return false;
00270     }}
00271     //::printf("debug : tree : read leaves : end\n");
00272 
00273     if(v>=10) {
00274       //TList* fAliases
00275       ifac::args args;
00276       iro* obj;
00277       if(!a_buffer.read_object(m_fac,args,obj)) {
00278         m_out << "inlib::rroot::tree::stream : "
00279               << "can't read fAliases."
00280               << std::endl;
00281         return false;
00282       }
00283       delete obj;
00284     } 
00285 
00286     m_saved_bytes = m_tot_bytes;
00287    {std::vector<double> v;
00288     if(!Array_stream<double>(a_buffer,v)) return false;} //fIndexValues TArrayD
00289 
00290    {std::vector<int> v;
00291     if(!Array_stream<int>(a_buffer,v)) return false;}    // fIndex (TArrayI).
00292 
00293     if(v>=16) {
00294       //TVirtualIndex* fTreeIndex //FIXME ???
00295       ifac::args args;
00296       iro* obj;
00297       if(!a_buffer.read_object(m_fac,args,obj)) {
00298         m_out << "inlib::rroot::tree::stream : "
00299               << "can't read fAliases."
00300               << std::endl;
00301         return false;
00302       }
00303       delete obj;
00304     }
00305 
00306     if(v>=6) {
00307       //TList* fFriends
00308       ifac::args args;
00309       iro* obj;
00310       if(!a_buffer.read_object(m_fac,args,obj)) {
00311         m_out << "inlib::rroot::tree::stream : "
00312               << "can't read fFriends."
00313               << std::endl;
00314         return false;
00315       }
00316       delete obj;
00317     } 
00318     
00319     if(v>=16) {
00320       //TList* fUserInfo
00321      {ifac::args args;
00322       iro* obj;
00323       if(!a_buffer.read_object(m_fac,args,obj)) {
00324         m_out << "inlib::rroot::tree::stream : "
00325               << "can't read fUserInfo."
00326               << std::endl;
00327         return false;
00328       }
00329       delete obj;}
00330       //TBranchRef* fBranchRef
00331      {ifac::args args;
00332       iro* obj;
00333       if(!a_buffer.read_object(m_fac,args,obj)) {
00334         m_out << "inlib::rroot::tree::stream : "
00335               << "can't read fUserInfo."
00336               << std::endl;
00337         return false;
00338       }
00339       delete obj;}
00340     } 
00341     
00342     if(!a_buffer.check_byte_count(s,c,TTree_cls())) return false;
00343 
00344     return true;
00345   }    
00346 protected:
00347   void _dump_branches(std::ostream& a_out,
00348                              const std::vector<branch*>& a_bs,
00349                              const std::string& a_spaces = "",
00350                              const std::string& a_indent = " "){
00351     std::vector<branch*>::const_iterator it;
00352     for(it=a_bs.begin();it!=a_bs.end();++it) {
00353       if(branch_element* be = inlib::cast<branch,branch_element>(*(*it))) {
00354         a_out << a_spaces
00355               << "branch_element :"
00356               << " name=" << sout((*it)->name())
00357               << " title=" << sout((*it)->title())
00358               << " entry_number=" << be->entry_number()
00359               << " ref_cls=" << sout(be->class_name())
00360               << " (type=" << be->type()
00361               << ",id=" << be->id()
00362               << ",stype=" << be->streamer_type()
00363               << ")."
00364               << std::endl;
00365       } else {
00366         a_out << a_spaces
00367               << "branch :"
00368               << " name=" << sout((*it)->name())
00369               << " title=" << sout((*it)->title())
00370               << " entry_number=" << (*it)->entry_number()
00371               << std::endl;
00372       }
00373   
00374   
00375      {const std::vector<base_leaf*>& lvs = (*it)->leaves();
00376       std::vector<base_leaf*>::const_iterator itl;
00377       for(itl=lvs.begin();itl!=lvs.end();++itl) {
00378         a_out << a_spaces << a_indent
00379               << "leave :"
00380               << " name=" << sout((*itl)->name())
00381               << " title=" << sout((*itl)->title())
00382               << " cls=" << sout((*itl)->s_cls())
00383               << std::endl;
00384       }}
00385 
00386       _dump_branches(a_out,(*it)->branches(),a_spaces+a_indent,a_indent);
00387     }
00388   }
00389 
00390   branch* _find_branch(const std::vector<branch*>& a_bs,
00391                               const std::string& a_name,
00392                               bool a_recursive) const {
00393     std::vector<branch*>::const_iterator it;
00394     for(it=a_bs.begin();it!=a_bs.end();++it) {
00395       if((*it)->name()==a_name) return *it;
00396       if(a_recursive) {
00397         branch* br = _find_branch((*it)->branches(),a_name,a_recursive);
00398         if(br) return br;
00399       }
00400     }
00401     return 0;
00402   }
00403 
00404   void _find_leaves(const std::vector<branch*>& a_bs,
00405                            std::vector<base_leaf*>& a_leaves){
00406     std::vector<branch*>::const_iterator it;
00407     for(it=a_bs.begin();it!=a_bs.end();++it) {
00408      {const std::vector<base_leaf*>& lvs = (*it)->leaves();
00409       std::vector<base_leaf*>::const_iterator itl;
00410       for(itl=lvs.begin();itl!=lvs.end();++itl) {
00411         a_leaves.push_back(*itl);
00412       }}
00413 
00414       _find_leaves((*it)->branches(),a_leaves);
00415     }
00416   }
00417 
00418   //branch* _find_leaf_branch(const std::vector<branch*>& a_bs,
00419   //                                 base_leaf* a_leaf){
00420   //  std::vector<branch*>::const_iterator it;
00421   //  for(it=a_bs.begin();it!=a_bs.end();++it) {
00422   //   {const std::vector<base_leaf*>& lvs = (*it)->leaves();
00423   //    std::vector<base_leaf*>::const_iterator itl;
00424   //    for(itl=lvs.begin();itl!=lvs.end();++itl) {
00425   //      if(*itl==a_leaf) return *it;
00426   //    }}
00427   //   {branch* br = _find_leaf_branch((*it)->branches(),a_leaf);
00428   //    if(br) return br;}
00429   //  }
00430   //  return 0;
00431   //}
00432 protected:
00433   ifile& m_file;
00434   ifac& m_fac;
00435   std::ostream& m_out;
00436   //Named
00437   std::string m_name;
00438   std::string m_title;
00439 
00440   ObjArray<branch> m_branches;
00441   uint64 m_entries;   // Number of entries
00442 };
00443 
00444 }}
00445 
00446 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines