Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/rroot/rall 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_rall
0005 #define tools_rroot_rall
0006 
0007 #include "streamers"
0008 #include "fac"
0009 #include "tree"
0010 #include "../tokenize" //for annotations.
0011 
0012 #include "THistogram"
0013 
0014 namespace tools {
0015 namespace rroot {
0016 
0017 inline TDirectory* find_dir(directory& a_dir,const std::string& a_name) {
0018   std::ostream& out = a_dir.file().out();
0019   key* k = a_dir.find_key(a_name);
0020   if(!k) {
0021     return 0;
0022   }
0023 
0024   if(k->object_class()!=TDirectory_cls()) {
0025     out << "tools::rroot::find_dir :"
0026         << " key " << a_name << " not a TDirectory."
0027         << std::endl;
0028     return 0;
0029   }
0030   uint32 sz;
0031   char* buf = k->get_object_buffer(a_dir.file(),sz); //we don't have ownership of buf.
0032   if(!buf) {
0033     out  << "tools::rroot::find_dir :"
0034          << " can't get directory data buffer."
0035          << std::endl;
0036     return 0;
0037   }
0038   buffer b(out,a_dir.file().byte_swap(),sz,buf,k->key_length(),false);
0039   TDirectory* tdir = new TDirectory(a_dir.file());
0040   if(!tdir) return 0;
0041   if(!tdir->stream(b)) {
0042     out << "tools::rroot::find_dir :"
0043         << " can't stream TDirectory."
0044         << std::endl;
0045     delete tdir;
0046     return 0;
0047   }
0048   return tdir;
0049 }
0050 
0051 inline directory* find_path_dir(directory& a_from,const std::string& a_path) {
0052   if(a_path.empty()) return 0;
0053   std::vector<std::string> ws;
0054   words(a_path,"/",false,ws);
0055   directory* cur_dir = &a_from;
0056   for(unsigned int index=0;index<ws.size();index++) {
0057     TDirectory* _dir = find_dir(*cur_dir,ws[index]);
0058     if(!_dir) return 0;
0059     if(index==(ws.size()-1)) return _dir;
0060     if(index) delete cur_dir;
0061     cur_dir = _dir;
0062   }
0063   return 0;
0064 }
0065 
0066 inline bool read_key(std::ostream& a_out,ifile& a_file,key& a_key,bool a_dump){
0067   unsigned int sz;
0068   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0069   if(!buf) {
0070     a_out << "tools::rroot::read_key : can't get data buffer of " << a_key.object_name() << "." << std::endl;
0071     return false;
0072   }
0073 
0074   buffer b(a_out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0075 
0076   if(a_key.object_class()==TNamed_cls()) {
0077     std::string name,title;
0078     if(!Named_stream(b,name,title)) {
0079       a_out << "tools::rroot::read_key : TNamed streaming failed" << std::endl;
0080     } else {
0081       if(a_dump) {
0082         a_out << "Named : name = " << sout(name) << ", title = " << sout(title) << std::endl;
0083       }
0084     }
0085 
0086   } else if(a_key.object_class()==TH1F_cls()) {
0087     histo::h1d* h = TH1F_stream(b);
0088     if(!h) {
0089       a_out << "tools::rroot::read_key : TH1F streaming failed" << std::endl;
0090     } else {
0091       if(a_dump) h->hprint(a_out);
0092     }
0093     delete h;
0094 
0095   } else if(a_key.object_class()==TH1D_cls()) {
0096     histo::h1d* h = TH1D_stream(b);
0097     if(!h) {
0098       a_out << "tools::rroot::read_key :"
0099             << " TH1D streaming failed"
0100             << std::endl;
0101     } else {
0102       if(a_dump) h->hprint(a_out);
0103     }
0104     delete h;
0105 
0106   } else if(a_key.object_class()==TH2F_cls()) {
0107     histo::h2d* h = TH2F_stream(b);
0108     if(!h) {
0109       a_out << "tools::rroot::read_key :"
0110             << " TH2F streaming failed"
0111             << std::endl;
0112     } else {
0113       if(a_dump) h->hprint(a_out);
0114     }
0115     delete h;
0116 
0117   } else if(a_key.object_class()==TH2D_cls()) {
0118     histo::h2d* h = TH2D_stream(b); //we get ownership of h.
0119     if(!h) {
0120       a_out << "tools::rroot::read_key :"
0121             << " TH2D streaming failed"
0122             << std::endl;
0123     } else {
0124       if(a_dump) h->hprint(a_out);
0125     }
0126     delete h;
0127 
0128   } else if(a_key.object_class()==TH3D_cls()) {
0129     histo::h3d* h = TH3D_stream(b); //we get ownership of h.
0130     if(!h) {
0131       a_out << "tools::rroot::read_key :"
0132             << " TH3D streaming failed"
0133             << std::endl;
0134     } else {
0135       if(a_dump) h->hprint(a_out);
0136     }
0137     delete h;
0138 
0139   } else if(a_key.object_class()==TProfile_cls()) {
0140     histo::p1d* p = TProfile_stream(b);
0141     if(!p) {
0142       a_out << "tools::rroot::read_key :"
0143             << " TProfile streaming failed"
0144             << std::endl;
0145     } else {
0146       if(a_dump) p->hprint(a_out);
0147     }
0148     delete p;
0149 
0150   } else if(a_key.object_class()==TProfile2D_cls()) {
0151     histo::p2d* p = TProfile2D_stream(b);
0152     if(!p) {
0153       a_out << "tools::rroot::read_key :"
0154             << " TProfile2D streaming failed"
0155             << std::endl;
0156     } else {
0157       if(a_dump) p->hprint(a_out);
0158     }
0159     delete p;
0160 
0161   } else if(a_key.object_class()==TTree_cls()) {
0162     b.set_map_objs(true); //for "root_ls -ls" on esb evetest.root files.
0163     fac _fac(a_out);
0164     tree tree(a_file,_fac);
0165     if(!tree.stream(b)) {
0166       a_out << "tools::rroot::read_key :"
0167             << " TTree streaming failed"
0168             << std::endl;
0169     } else {
0170       //tree->dump(a_out);
0171       if(a_dump) {
0172         tree.dump(a_out,"","  ");
0173 
0174         uint64 entries = tree.entries();
0175 
0176        {for(uint32 i=0;i<5;i++){
0177           if(!tree.show(a_out,i)) {
0178             a_out << " show failed for entry " << i
0179                   << std::endl;
0180           }
0181         }}
0182        {for(uint64 i=max_of<int64>(5,entries-5);i<entries;i++){
0183           if(!tree.show(a_out,(uint32)i)) {
0184             a_out << " show failed for entry " << i
0185                   << std::endl;
0186           }
0187         }}
0188 
0189       }
0190     }
0191 
0192   } else if(a_key.object_class()==THistogram_cls()) { //produced with osc/AIDA through BatchLab/Rio/THistogram.
0193 
0194     pd_data_t data;
0195     annotations_t annotations;
0196     if(!read_THistogram(b,data,annotations)) {
0197       a_out << "tools::rroot::read_key : read_THistogram() failed." << std::endl;
0198     } else {
0199       if(histo::h1d* _h1d = THistogram_to_h1d(data)) {
0200         if(a_dump) _h1d->hprint(a_out);
0201         delete _h1d;
0202       } else if(histo::h2d* _h2d = THistogram_to_h2d(data)) {
0203         if(a_dump) _h2d->hprint(a_out);
0204         delete _h2d;
0205       } else if(histo::p1d* _p1d = THistogram_to_p1d(data)) {
0206         if(a_dump) _p1d->hprint(a_out);
0207         delete _p1d;
0208       } else if(histo::p2d* _p2d = THistogram_to_p2d(data)) {
0209         if(a_dump) _p2d->hprint(a_out);
0210         delete _p2d;
0211       } else {
0212         a_out << "tools::rroot::read_key : can't convert THistogram dat to h1d,h2d,p1d or p2d." << std::endl;
0213       }
0214     }
0215 
0216   } else if(a_key.object_class()==TDirectory_cls()) {
0217 
0218     //we should not pass here.
0219 
0220   } else {
0221     a_out << "tools::rroot::read_key :"
0222           << " dont't know how to read key with object class "
0223           << sout(a_key.object_class())
0224           << std::endl;
0225   }
0226   return true;
0227 }
0228 
0229 inline bool key_to_annotations(ifile& a_file,key& a_key,std::map<std::string,std::string>& a_annotations,bool a_warn = true) {
0230   a_annotations.clear();
0231   std::ostream& out = a_key.out();
0232   if(a_key.object_class()!=TNamed_cls()) {
0233     if(a_warn) out << "tools::rroot::key_to_annotations : key not a TNamed." << std::endl;
0234     return false;
0235   }
0236   unsigned int sz;
0237   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0238   if(!buf) {
0239     out << "tools::rroot::key_to_annotations : can't get data buffer of " << a_key.object_name() << "." << std::endl;
0240     return false;
0241   }
0242   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0243   std::string histo_name;
0244   std::string sas;
0245   if(!Named_stream(b,histo_name,sas)) return false;
0246   std::vector<std::string> ws;
0247   words(sas,"\n",true,ws);
0248   size_t wordn = ws.size();
0249   if(2*(wordn/2)!=wordn) {
0250     out << "tools::rroot::key_to_annotations : wordn should be even in " << sout(sas) << "." << std::endl;
0251     return false;
0252   }
0253   std::vector<std::string>::const_iterator it;
0254   for(it=ws.begin();it!=ws.end();) {
0255     const std::string& key = *it;it++;
0256     const std::string& value = *it;it++;
0257     a_annotations[key] = value;
0258   }
0259   return true;
0260 }
0261 
0262 inline histo::h1d* key_to_h1d(ifile& a_file,key& a_key,bool a_warn = true) {
0263   std::ostream& out = a_key.out();
0264   if( (a_key.object_class()!=TH1D_cls()) && (a_key.object_class()!=TH1F_cls()) ) {
0265     if(a_warn) out << "tools::rroot::key_to_h1d : key not a TH1D and not a TH1F." << std::endl;
0266     return 0;
0267   }
0268   unsigned int sz;
0269   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0270   if(!buf) {
0271     out << "tools::rroot::key_to_h1d : can't get data buffer of " << a_key.object_name() << "." << std::endl;
0272     return 0;
0273   }
0274   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0275   if(a_key.object_class()==TH1D_cls()) {
0276     return TH1D_stream(b);
0277   } else {
0278     return TH1F_stream(b);
0279   }
0280 }
0281 
0282 inline histo::h2d* key_to_h2d(ifile& a_file,key& a_key,bool a_warn = true){
0283   std::ostream& out = a_key.out();
0284   if( (a_key.object_class()!=TH2D_cls()) && (a_key.object_class()!=TH2F_cls()) ) {
0285     if(a_warn) out << "tools::rroot::key_to_h2d : key not a TH2D and not a TH2F." << std::endl;
0286     return 0;
0287   }
0288   unsigned int sz;
0289   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0290   if(!buf) {
0291     out << "tools::rroot::key_to_h2d : can't get data buffer of " << a_key.object_name() << "." << std::endl;
0292     return 0;
0293   }
0294   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0295   if(a_key.object_class()==TH2D_cls()) {
0296     return TH2D_stream(b);
0297   } else {
0298     return TH2F_stream(b);
0299   }
0300 }
0301 
0302 inline histo::h3d* key_to_h3d(ifile& a_file,key& a_key){
0303   std::ostream& out = a_key.out();
0304   if(a_key.object_class()!=TH3D_cls()) {
0305     out << "tools::rroot::key_to_h3d :"
0306         << " key not a TH3D."
0307         << std::endl;
0308     return 0;
0309   }
0310   unsigned int sz;
0311   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0312   if(!buf) {
0313     out << "tools::rroot::key_to_h3d :"
0314         << " can't get data buffer of " << a_key.object_name() << "."
0315         << std::endl;
0316     return 0;
0317   }
0318   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0319   return TH3D_stream(b);
0320 }
0321 
0322 inline histo::p1d* key_to_p1d(ifile& a_file,key& a_key){
0323   std::ostream& out = a_key.out();
0324   if(a_key.object_class()!=TProfile_cls()) {
0325     out << "tools::rroot::key_to_p1d :"
0326         << " key not a TProfile."
0327         << std::endl;
0328     return 0;
0329   }
0330   unsigned int sz;
0331   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0332   if(!buf) {
0333     out << "tools::rroot::key_to_p1d :"
0334         << " can't get data buffer of " << a_key.object_name() << "."
0335         << std::endl;
0336     return 0;
0337   }
0338   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0339   return TProfile_stream(b);
0340 }
0341 
0342 inline histo::p2d* key_to_p2d(ifile& a_file,key& a_key){
0343   std::ostream& out = a_key.out();
0344   if(a_key.object_class()!=TProfile2D_cls()) {
0345     out << "tools::rroot::key_to_p2d :"
0346         << " key not a TProfile2D."
0347         << std::endl;
0348     return 0;
0349   }
0350   unsigned int sz;
0351   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0352   if(!buf) {
0353     out << "tools::rroot::key_to_p2d :"
0354         << " can't get data buffer of " << a_key.object_name() << "."
0355         << std::endl;
0356     return 0;
0357   }
0358   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0359   return TProfile2D_stream(b);
0360 }
0361 
0362 inline tree* key_to_tree(ifile& a_file,ifac& a_fac,key& a_key,bool a_warn = true) {
0363   std::ostream& out = a_key.out();
0364   if(a_key.object_class()!=TTree_cls()) {
0365     if(a_warn) out << "tools::rroot::key_to_tree : key not a TTree." << std::endl;
0366     return 0;
0367   }
0368   unsigned int sz;
0369   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
0370   if(!buf) {
0371     out << "tools::rroot::key_to_tree : can't get data buffer of " << a_key.object_name() << "." << std::endl;
0372     return 0;
0373   }
0374   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
0375   b.set_map_objs(true); //for ioda/comres/tree.py, tree.lua.
0376   tree* _tree = new tree(a_file,a_fac);
0377   if(!_tree->stream(b)) {
0378     out << "tools::rroot::key_to_tree : TTree streaming failed" << std::endl;
0379     delete _tree;
0380     return 0;
0381   }
0382   return _tree;
0383 }
0384 
0385 inline void read(std::ostream& a_out,
0386                  ifile& a_file,
0387                  const std::vector<key*>& a_keys,
0388                  bool a_recursive,
0389                  bool a_ls,
0390                  bool a_dump,
0391                  unsigned int a_spaces) {
0392 
0393  {std::vector<key*>::const_iterator it;
0394   for(it=a_keys.begin();it!=a_keys.end();++it) {
0395     key& k = *(*it);
0396     if(k.object_class()!=TDirectory_cls()) {
0397       if(a_ls||a_dump) {
0398         {for(unsigned int index=0;index<a_spaces;index++) a_out << " ";}
0399         std::string label = k.object_name();
0400         a_out << "object : " << sout(label)
0401               << ", class : " << k.object_class()
0402               << std::endl;
0403         //k.dump(a_out);
0404       }
0405       if(!read_key(a_out,a_file,k,a_dump)) return;
0406     }
0407   }}
0408 
0409  {std::vector<key*>::const_iterator it;
0410   for(it=a_keys.begin();it!=a_keys.end();++it) {
0411     key& k = *(*it);
0412     if(k.object_class()==TDirectory_cls()) {
0413 
0414       if(a_ls||a_dump) {
0415         {for(unsigned int index=0;index<a_spaces;index++) a_out << " ";}
0416         std::string label = k.object_name();
0417         a_out << "directory : " << label << std::endl;
0418       }
0419 
0420       if(!a_recursive) continue;
0421 
0422       uint32 sz;
0423       char* buf = k.get_object_buffer(a_file,sz);
0424       if(!buf) {
0425         a_out  << "read :"
0426                << " can't get directory data buffer."
0427                << std::endl;
0428       } else {
0429         buffer b(a_out,a_file.byte_swap(),sz,buf,k.key_length(),false);
0430         TDirectory dir(a_file);
0431         if(!dir.stream(b)) {
0432           a_out << "read :"
0433                 << " can't stream TDirectory."
0434                 << std::endl;
0435         } else {
0436           const std::vector<key*>& keys = dir.keys();
0437           read(a_out,a_file,keys,a_recursive,a_ls,a_dump,a_spaces+1);
0438         }
0439       }
0440     }
0441   }}
0442 }
0443 
0444 }}
0445 
0446 #endif