Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_rroot_rall 5 #define tools_rroot_rall 6 7 #include "streamers" 8 #include "fac" 9 #include "tree" 10 #include "../words" //for annotations. 11 12 #define TOOLS_RROOT_NOT_OSC 13 14 #ifndef TOOLS_RROOT_NOT_OSC 15 #include "osc" 16 #endif 17 18 #include "THistogram" 19 20 namespace tools { 21 namespace rroot { 22 23 inline TDirectory* find_dir(directory& a_dir,c 24 std::ostream& out = a_dir.file().out(); 25 key* k = a_dir.find_key(a_name); 26 if(!k) { 27 //out << "tools::rroot::find_dir :" 28 // << " " << a_name << " not a key." 29 // << std::endl; 30 return 0; 31 } 32 33 if(k->object_class()!=TDirectory_cls()) { 34 out << "tools::rroot::find_dir :" 35 << " key " << a_name << " not a TDirec 36 << std::endl; 37 return 0; 38 } 39 uint32 sz; 40 char* buf = k->get_object_buffer(a_dir.file( 41 if(!buf) { 42 out << "tools::rroot::find_dir :" 43 << " can't get directory data buffer. 44 << std::endl; 45 return 0; 46 } 47 buffer b(out,a_dir.file().byte_swap(),sz,buf 48 TDirectory* tdir = new TDirectory(a_dir.file 49 if(!tdir) return 0; 50 if(!tdir->stream(b)) { 51 out << "tools::rroot::find_dir :" 52 << " can't stream TDirectory." 53 << std::endl; 54 delete tdir; 55 return 0; 56 } 57 return tdir; 58 } 59 60 inline directory* find_path_dir(directory& a_f 61 if(a_path.empty()) return 0; 62 std::vector<std::string> ws; 63 words(a_path,"/",false,ws); 64 directory* cur_dir = &a_from; 65 for(unsigned int index=0;index<ws.size();ind 66 TDirectory* _dir = find_dir(*cur_dir,ws[in 67 if(!_dir) return 0; 68 if(index==(ws.size()-1)) return _dir; 69 if(index) delete cur_dir; 70 cur_dir = _dir; 71 } 72 return 0; 73 } 74 75 inline bool read_key(std::ostream& a_out,ifile 76 unsigned int sz; 77 char* buf = a_key.get_object_buffer(a_file,s 78 if(!buf) { 79 a_out << "tools::rroot::read_key : can't g 80 return false; 81 } 82 83 //a_out << "tools::rroot::read_key :" 84 // << " get data buffer size " << sz << 85 // << std::endl; 86 87 buffer b(a_out,a_file.byte_swap(),sz,buf,a_k 88 89 if(a_key.object_class()==TNamed_cls()) { 90 std::string name,title; 91 if(!Named_stream(b,name,title)) { 92 a_out << "tools::rroot::read_key : TName 93 } else { 94 if(a_dump) { 95 a_out << "Named : name = " << sout(nam 96 } 97 } 98 99 } else if(a_key.object_class()==TH1F_cls()) 100 histo::h1d* h = TH1F_stream(b); 101 if(!h) { 102 a_out << "tools::rroot::read_key : TH1F 103 } else { 104 if(a_dump) h->hprint(a_out); 105 } 106 delete h; 107 108 } else if(a_key.object_class()==TH1D_cls()) 109 histo::h1d* h = TH1D_stream(b); 110 if(!h) { 111 a_out << "tools::rroot::read_key :" 112 << " TH1D streaming failed" 113 << std::endl; 114 } else { 115 if(a_dump) h->hprint(a_out); 116 } 117 delete h; 118 119 } else if(a_key.object_class()==TH2F_cls()) 120 histo::h2d* h = TH2F_stream(b); 121 if(!h) { 122 a_out << "tools::rroot::read_key :" 123 << " TH2F streaming failed" 124 << std::endl; 125 } else { 126 if(a_dump) h->hprint(a_out); 127 } 128 delete h; 129 130 } else if(a_key.object_class()==TH2D_cls()) 131 histo::h2d* h = TH2D_stream(b); //we get o 132 if(!h) { 133 a_out << "tools::rroot::read_key :" 134 << " TH2D streaming failed" 135 << std::endl; 136 } else { 137 if(a_dump) h->hprint(a_out); 138 } 139 delete h; 140 141 } else if(a_key.object_class()==TH3D_cls()) 142 histo::h3d* h = TH3D_stream(b); //we get o 143 if(!h) { 144 a_out << "tools::rroot::read_key :" 145 << " TH3D streaming failed" 146 << std::endl; 147 } else { 148 if(a_dump) h->hprint(a_out); 149 } 150 delete h; 151 152 } else if(a_key.object_class()==TProfile_cls 153 histo::p1d* p = TProfile_stream(b); 154 if(!p) { 155 a_out << "tools::rroot::read_key :" 156 << " TProfile streaming failed" 157 << std::endl; 158 } else { 159 if(a_dump) p->hprint(a_out); 160 } 161 delete p; 162 163 } else if(a_key.object_class()==TProfile2D_c 164 histo::p2d* p = TProfile2D_stream(b); 165 if(!p) { 166 a_out << "tools::rroot::read_key :" 167 << " TProfile2D streaming failed" 168 << std::endl; 169 } else { 170 if(a_dump) p->hprint(a_out); 171 } 172 delete p; 173 174 } else if(a_key.object_class()==TTree_cls()) 175 b.set_map_objs(true); //for "root_ls -ls" 176 fac _fac(a_out); 177 tree tree(a_file,_fac); 178 if(!tree.stream(b)) { 179 a_out << "tools::rroot::read_key :" 180 << " TTree streaming failed" 181 << std::endl; 182 } else { 183 //tree->dump(a_out); 184 if(a_dump) { 185 tree.dump(a_out,""," "); 186 187 uint64 entries = tree.entries(); 188 189 /* 190 {for(uint32 j=0;j<10;j++){ //to test me 191 for(uint32 i=0;i<entries;i++){ 192 uint32 n; 193 if(!tree.find_entry(i,n)) { 194 a_out << " can't find entry " << i 195 << std::endl; 196 } 197 } 198 }} 199 */ 200 201 {for(uint32 i=0;i<5;i++){ 202 if(!tree.show(a_out,i)) { 203 a_out << " show failed for entry " 204 << std::endl; 205 } 206 }} 207 {for(uint64 i=mx<int64>(5,entries-5);i< 208 if(!tree.show(a_out,(uint32)i)) { 209 a_out << " show failed for entry " 210 << std::endl; 211 } 212 }} 213 214 } 215 } 216 217 #ifndef TOOLS_RROOT_NOT_OSC 218 } else if(a_key.object_class()==osc::s_h1d() 219 220 histo::h1d h("",10,0,1); 221 if(!from_osc(b,osc::s_h1d(),h)) { 222 a_out << "tools::rroot::read_key :" 223 << " " << osc::s_h1d() << " stream 224 << std::endl; 225 } else { 226 if(a_dump) h.hprint(a_out); 227 } 228 229 } else if(a_key.object_class()==osc::s_h2d() 230 231 histo::h2d h("",10,0,1,10,0,1); 232 if(!from_osc(b,osc::s_h2d(),h)) { 233 a_out << "tools::rroot::read_key :" 234 << " " << osc::s_h2d() << " stream 235 << std::endl; 236 } else { 237 if(a_dump) h.hprint(a_out); 238 } 239 240 } else if(a_key.object_class()==osc::s_h3d() 241 242 histo::h3d h("",10,0,1,10,0,1,10,0,1); 243 if(!from_osc(b,osc::s_h3d(),h)) { 244 a_out << "tools::rroot::read_key :" 245 << " " << osc::s_h3d() << " stream 246 << std::endl; 247 } else { 248 if(a_dump) h.hprint(a_out); 249 } 250 251 } else if(a_key.object_class()==osc::s_p1d() 252 253 histo::p1d h("",10,0,1); 254 if(!from_osc(b,osc::s_p1d(),h)) { 255 a_out << "tools::rroot::read_key :" 256 << " " << osc::s_p1d() << " stream 257 << std::endl; 258 } else { 259 if(a_dump) h.hprint(a_out); 260 } 261 262 } else if(a_key.object_class()==osc::s_p2d() 263 264 histo::p2d h("",10,0,1,10,0,1); 265 if(!from_osc(b,osc::s_p2d(),h)) { 266 a_out << "tools::rroot::read_key :" 267 << " " << osc::s_p2d() << " stream 268 << std::endl; 269 } else { 270 if(a_dump) h.hprint(a_out); 271 } 272 #endif 273 274 } else if(a_key.object_class()==THistogram_c 275 276 pd_data_t data; 277 annotations_t annotations; 278 if(!read_THistogram(b,data,annotations)) { 279 a_out << "tools::rroot::read_key : read_ 280 } else { 281 if(histo::h1d* _h1d = THistogram_to_h1d( 282 if(a_dump) _h1d->hprint(a_out); 283 delete _h1d; 284 } else if(histo::h2d* _h2d = THistogram_ 285 if(a_dump) _h2d->hprint(a_out); 286 delete _h2d; 287 } else if(histo::p1d* _p1d = THistogram_ 288 if(a_dump) _p1d->hprint(a_out); 289 delete _p1d; 290 } else if(histo::p2d* _p2d = THistogram_ 291 if(a_dump) _p2d->hprint(a_out); 292 delete _p2d; 293 } else { 294 a_out << "tools::rroot::read_key : can 295 } 296 } 297 298 } else if(a_key.object_class()==TDirectory_c 299 300 //we should not pass here. 301 302 } else { 303 a_out << "tools::rroot::read_key :" 304 << " dont't know how to read key wit 305 << sout(a_key.object_class()) 306 << std::endl; 307 } 308 return true; 309 } 310 311 //typedef std::map<std::string,std::string> an 312 inline bool key_to_annotations(ifile& a_file,k 313 a_annotations.clear(); 314 std::ostream& out = a_key.out(); 315 if(a_key.object_class()!=TNamed_cls()) { 316 if(a_warn) out << "tools::rroot::key_to_an 317 return false; 318 } 319 unsigned int sz; 320 char* buf = a_key.get_object_buffer(a_file,s 321 if(!buf) { 322 out << "tools::rroot::key_to_annotations : 323 return false; 324 } 325 buffer b(out,a_file.byte_swap(),sz,buf,a_key 326 std::string histo_name; 327 std::string sas; 328 if(!Named_stream(b,histo_name,sas)) return f 329 std::vector<std::string> ws; 330 words(sas,"\n",true,ws); 331 size_t wordn = ws.size(); 332 if(2*(wordn/2)!=wordn) { 333 out << "tools::rroot::key_to_annotations : 334 return false; 335 } 336 std::vector<std::string>::const_iterator it; 337 for(it=ws.begin();it!=ws.end();) { 338 const std::string& key = *it;it++; 339 const std::string& value = *it;it++; 340 a_annotations[key] = value; 341 } 342 return true; 343 } 344 345 inline histo::h1d* key_to_h1d(ifile& a_file,ke 346 std::ostream& out = a_key.out(); 347 if( (a_key.object_class()!=TH1D_cls()) && (a 348 if(a_warn) out << "tools::rroot::key_to_h1 349 return 0; 350 } 351 unsigned int sz; 352 char* buf = a_key.get_object_buffer(a_file,s 353 if(!buf) { 354 out << "tools::rroot::key_to_h1d : can't g 355 return 0; 356 } 357 buffer b(out,a_file.byte_swap(),sz,buf,a_key 358 if(a_key.object_class()==TH1D_cls()) { 359 return TH1D_stream(b); 360 } else { 361 return TH1F_stream(b); 362 } 363 } 364 365 inline histo::h2d* key_to_h2d(ifile& a_file,ke 366 std::ostream& out = a_key.out(); 367 if( (a_key.object_class()!=TH2D_cls()) && (a 368 if(a_warn) out << "tools::rroot::key_to_h2 369 return 0; 370 } 371 unsigned int sz; 372 char* buf = a_key.get_object_buffer(a_file,s 373 if(!buf) { 374 out << "tools::rroot::key_to_h2d : can't g 375 return 0; 376 } 377 buffer b(out,a_file.byte_swap(),sz,buf,a_key 378 if(a_key.object_class()==TH2D_cls()) { 379 return TH2D_stream(b); 380 } else { 381 return TH2F_stream(b); 382 } 383 } 384 385 inline histo::h3d* key_to_h3d(ifile& a_file,ke 386 std::ostream& out = a_key.out(); 387 if(a_key.object_class()!=TH3D_cls()) { 388 out << "tools::rroot::key_to_h3d :" 389 << " key not a TH3D." 390 << std::endl; 391 return 0; 392 } 393 unsigned int sz; 394 char* buf = a_key.get_object_buffer(a_file,s 395 if(!buf) { 396 out << "tools::rroot::key_to_h3d :" 397 << " can't get data buffer of " << a_k 398 << std::endl; 399 return 0; 400 } 401 buffer b(out,a_file.byte_swap(),sz,buf,a_key 402 return TH3D_stream(b); 403 } 404 405 inline histo::p1d* key_to_p1d(ifile& a_file,ke 406 std::ostream& out = a_key.out(); 407 if(a_key.object_class()!=TProfile_cls()) { 408 out << "tools::rroot::key_to_p1d :" 409 << " key not a TProfile." 410 << std::endl; 411 return 0; 412 } 413 unsigned int sz; 414 char* buf = a_key.get_object_buffer(a_file,s 415 if(!buf) { 416 out << "tools::rroot::key_to_p1d :" 417 << " can't get data buffer of " << a_k 418 << std::endl; 419 return 0; 420 } 421 buffer b(out,a_file.byte_swap(),sz,buf,a_key 422 return TProfile_stream(b); 423 } 424 425 inline histo::p2d* key_to_p2d(ifile& a_file,ke 426 std::ostream& out = a_key.out(); 427 if(a_key.object_class()!=TProfile2D_cls()) { 428 out << "tools::rroot::key_to_p2d :" 429 << " key not a TProfile2D." 430 << std::endl; 431 return 0; 432 } 433 unsigned int sz; 434 char* buf = a_key.get_object_buffer(a_file,s 435 if(!buf) { 436 out << "tools::rroot::key_to_p2d :" 437 << " can't get data buffer of " << a_k 438 << std::endl; 439 return 0; 440 } 441 buffer b(out,a_file.byte_swap(),sz,buf,a_key 442 return TProfile2D_stream(b); 443 } 444 445 inline tree* key_to_tree(ifile& a_file,ifac& a 446 std::ostream& out = a_key.out(); 447 if(a_key.object_class()!=TTree_cls()) { 448 if(a_warn) out << "tools::rroot::key_to_tr 449 return 0; 450 } 451 unsigned int sz; 452 char* buf = a_key.get_object_buffer(a_file,s 453 if(!buf) { 454 out << "tools::rroot::key_to_tree : can't 455 return 0; 456 } 457 buffer b(out,a_file.byte_swap(),sz,buf,a_key 458 b.set_map_objs(true); //for ioda/comres/tree 459 tree* _tree = new tree(a_file,a_fac); 460 if(!_tree->stream(b)) { 461 out << "tools::rroot::key_to_tree : TTree 462 delete _tree; 463 return 0; 464 } 465 return _tree; 466 } 467 468 inline void read(std::ostream& a_out, 469 ifile& a_file, 470 const std::vector<key*>& a_ke 471 bool a_recursive, 472 bool a_ls, 473 bool a_dump, 474 unsigned int a_spaces) { 475 476 {std::vector<key*>::const_iterator it; 477 for(it=a_keys.begin();it!=a_keys.end();++it) 478 key& k = *(*it); 479 if(k.object_class()!=TDirectory_cls()) { 480 if(a_ls||a_dump) { 481 {for(unsigned int index=0;index<a_spac 482 std::string label = k.object_name(); 483 a_out << "object : " << sout(label) 484 << ", class : " << k.object_clas 485 << std::endl; 486 //k.dump(a_out); 487 } 488 if(!read_key(a_out,a_file,k,a_dump)) ret 489 } 490 }} 491 492 {std::vector<key*>::const_iterator it; 493 for(it=a_keys.begin();it!=a_keys.end();++it) 494 key& k = *(*it); 495 if(k.object_class()==TDirectory_cls()) { 496 497 if(a_ls||a_dump) { 498 {for(unsigned int index=0;index<a_spac 499 std::string label = k.object_name(); 500 a_out << "directory : " << label << st 501 } 502 503 if(!a_recursive) continue; 504 505 uint32 sz; 506 char* buf = k.get_object_buffer(a_file,s 507 if(!buf) { 508 a_out << "read :" 509 << " can't get directory data b 510 << std::endl; 511 } else { 512 buffer b(a_out,a_file.byte_swap(),sz,b 513 TDirectory dir(a_file); 514 if(!dir.stream(b)) { 515 a_out << "read :" 516 << " can't stream TDirectory." 517 << std::endl; 518 } else { 519 const std::vector<key*>& keys = dir. 520 read(a_out,a_file,keys,a_recursive,a 521 } 522 } 523 } 524 }} 525 } 526 527 }} 528 529 #endif