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