Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_rroot_tree 5 #define tools_rroot_tree 6 7 #include "ifac" 8 #include "branch_element" 9 #include "../sout" 10 #include "iobject" 11 12 namespace tools { 13 namespace rroot { 14 15 inline const std::string& TTree_cls(){ 16 static const std::string s_v("TTree"); 17 return s_v; 18 } 19 20 class tree : public virtual iobject { 21 public: 22 static const std::string& s_class() { 23 static const std::string s_v("tools::rroot::tree"); 24 return s_v; 25 } 26 virtual const std::string& s_cls() const {return s_class();} 27 public: //iobject 28 virtual const std::string& name() const {return m_name;} 29 virtual const std::string& title() const {return m_title;} 30 virtual const std::string& store_class_name() const { 31 static const std::string s_v("TTree"); 32 return s_v; 33 } 34 public: 35 tree(ifile& a_file,ifac& a_fac) 36 :m_file(a_file) 37 ,m_fac(a_fac) 38 ,m_out(a_file.out()) 39 ,m_name("") 40 ,m_title("") 41 ,m_branches(a_fac) 42 ,m_entries(0) 43 { 44 #ifdef TOOLS_MEM 45 mem::increment(s_class().c_str()); 46 #endif 47 } 48 virtual ~tree(){ 49 #ifdef TOOLS_MEM 50 mem::decrement(s_class().c_str()); 51 #endif 52 } 53 protected: 54 tree(const tree& a_from) 55 :iobject(a_from) 56 ,m_file(a_from.m_file) 57 ,m_fac(a_from.m_fac) 58 ,m_out(a_from.m_out) 59 ,m_branches(m_fac) 60 {} 61 tree& operator=(const tree&){return *this;} 62 public: 63 std::ostream& out() const {return m_out;} 64 65 ifile& file() {return m_file;} 66 ifac& fac() {return m_fac;} 67 68 const std::vector<branch*>& branches() const {return m_branches;} 69 70 bool find_entry(uint64 a_entry,uint32& a_nbytes) { 71 a_nbytes = 0; 72 if(a_entry>=m_entries) return false; 73 int nbytes = 0; 74 //fReadEntry = a_entry; 75 tools_vforit(branch*,m_branches,it) { 76 uint32 n; 77 if(!(*it)->find_entry(m_file,a_entry,n)) return false; 78 nbytes += n; 79 } 80 a_nbytes = nbytes; 81 return true; 82 } 83 84 void dump(std::ostream& a_out,const std::string& a_spaces = "",const std::string& a_indent = " "){ 85 a_out << a_spaces 86 << "tree :" 87 << " name=" << sout(m_name) 88 << " title=" << sout(m_title) 89 << " entries=" << m_entries 90 << std::endl; 91 _dump_branches(a_out,m_branches,a_spaces+a_indent,a_indent); 92 } 93 94 branch* find_branch(const std::string& a_name,bool a_recursive = false) const { 95 return _find_branch(m_branches,a_name,a_recursive); 96 } 97 98 branch_element* find_branch_element(const std::string& a_name,bool a_recursive = false) const { 99 branch* b = _find_branch(m_branches,a_name,a_recursive); 100 if(!b) return 0; 101 return id_cast<branch,branch_element>(*b); 102 } 103 104 base_leaf* find_leaf(const std::string& a_name,bool a_recursive = false) const { 105 return _find_leaf(m_branches,a_name,a_recursive); 106 } 107 108 void find_leaves(std::vector<base_leaf*>& a_leaves) const { 109 a_leaves.clear(); 110 _find_leaves(m_branches,a_leaves); 111 } 112 113 void find_branches(std::vector<branch*>& a_branches) const { 114 a_branches.clear(); 115 _find_branches(m_branches,a_branches); 116 } 117 118 branch* find_leaf_branch(const base_leaf& a_leaf) const {return _find_leaf_branch(m_branches,a_leaf);} 119 120 bool show(std::ostream& a_out,uint64 a_entry){ 121 a_out << "======> EVENT:" << a_entry << std::endl; 122 tools_vforit(branch*,m_branches,it) { 123 if(!(*it)->show(a_out,m_file,a_entry)) return false; 124 } 125 return true; 126 } 127 128 uint64 entries() const {return m_entries;} 129 130 virtual bool stream(buffer& a_buffer) { //virtual for iobject. 131 //uint64 m_tot_bytes; 132 //uint64 m_zip_bytes; 133 //uint64 m_saved_bytes; 134 135 short vers; 136 unsigned int _s,_c; 137 if(!a_buffer.read_version(vers,_s,_c)) return false; 138 139 //::printf("debug : tree::stream : version %d count %d\n",vers,c); 140 141 //if (vers > 4) { 142 //TTree::Class()->ReadBuffer(b, this, vers, s, c); 143 //if (fEstimate <= 10000) fEstimate = 1000000; 144 //m_saved_bytes = m_tot_bytes; 145 //fDirectory = gDirectory; 146 //gDirectory->Append(this); 147 //return; 148 //} 149 150 if(!Named_stream(a_buffer,m_name,m_title)) return false; 151 152 {short color,style,width; 153 if(!AttLine_stream(a_buffer,color,style,width)) return false;} 154 {short color,style; 155 if(!AttFill_stream(a_buffer,color,style)) return false;} 156 if(!AttMarker_stream(a_buffer)) return false; 157 158 if(vers<=4) { 159 int dummy_int; 160 161 if(!a_buffer.read(dummy_int)) return false; //fScanField 162 if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop 163 {int fMaxVirtualSize; 164 if(!a_buffer.read(fMaxVirtualSize)) return false;} 165 {double v; 166 if(!a_buffer.read(v)) return false; 167 m_entries = uint64(v);} 168 {double v; 169 if(!a_buffer.read(v)) return false; 170 //m_tot_bytes = uint64(v); 171 } 172 {double v; 173 if(!a_buffer.read(v)) return false; 174 //m_zip_bytes = uint64(v); 175 } 176 {int fAutoSave; 177 if(!a_buffer.read(fAutoSave)) return false;} 178 if(!a_buffer.read(dummy_int)) return false; //fEstimate 179 180 } else if(vers<=9) { 181 {double v; 182 if(!a_buffer.read(v)) return false; 183 m_entries = uint64(v);} 184 {double v; 185 if(!a_buffer.read(v)) return false; 186 //m_tot_bytes = uint64(v); 187 } 188 {double v; 189 if(!a_buffer.read(v)) return false; 190 //m_zip_bytes = uint64(v); 191 } 192 {double v; 193 if(!a_buffer.read(v)) return false; 194 //m_saved_bytes = uint64(v); 195 } 196 197 int dummy_int; 198 if(!a_buffer.read(dummy_int)) return false; //fTimerInterval 199 if(!a_buffer.read(dummy_int)) return false; //fScanField 200 if(!a_buffer.read(dummy_int)) return false; //fUpdate 201 if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop 202 203 {int fMaxVirtualSize; 204 if(!a_buffer.read(fMaxVirtualSize)) return false;} 205 {int fAutoSave; 206 if(!a_buffer.read(fAutoSave)) return false;} 207 if(!a_buffer.read(dummy_int)) return false; //fEstimate 208 209 } else if(vers<16) { //FIXME : what is the exact version ? 210 double dummy_double; 211 int dummy_int; 212 213 {double v; 214 if(!a_buffer.read(v)) return false; 215 m_entries = uint64(v);} 216 {double v; 217 if(!a_buffer.read(v)) return false; 218 //m_tot_bytes = uint64(v); 219 } 220 {double v; 221 if(!a_buffer.read(v)) return false; 222 //m_zip_bytes = uint64(v); 223 } 224 {double v; 225 if(!a_buffer.read(v)) return false; 226 //m_saved_bytes = uint64(v); 227 } 228 if(!a_buffer.read(dummy_double)) return false; //fWeight 229 if(!a_buffer.read(dummy_int)) return false; //fTimerInterval 230 if(!a_buffer.read(dummy_int)) return false; //fScanField 231 if(!a_buffer.read(dummy_int)) return false; //fUpdate 232 if(!a_buffer.read(dummy_int)) return false; //fMaxEntryLoop 233 234 {int fMaxVirtualSize; 235 if(!a_buffer.read(fMaxVirtualSize)) return false;} 236 {int fAutoSave; 237 if(!a_buffer.read(fAutoSave)) return false;} 238 if(!a_buffer.read(dummy_int)) return false; //fEstimate 239 240 } else { //vers>=16 241 double dummy_double; 242 int dummy_int; 243 int64 dummy_int64; 244 245 {uint64 v; 246 if(!a_buffer.read(v)) return false; 247 m_entries = v;} 248 {uint64 v; 249 if(!a_buffer.read(v)) return false; 250 //m_tot_bytes = v; 251 } 252 {uint64 v; 253 if(!a_buffer.read(v)) return false; 254 //m_zip_bytes = v; 255 } 256 {uint64 v; 257 if(!a_buffer.read(v)) return false; 258 //m_saved_bytes = v; 259 } 260 if(vers>=18) { 261 if(!a_buffer.read(dummy_int64)) return false; //fFlushedBytes 262 } 263 264 if(!a_buffer.read(dummy_double)) return false; //fWeight 265 266 if(!a_buffer.read(dummy_int)) return false; //fTimerInterval 267 if(!a_buffer.read(dummy_int)) return false; //fScanField 268 if(!a_buffer.read(dummy_int)) return false; //fUpdate 269 270 if(vers>=18) { 271 if(!a_buffer.read(dummy_int)) return false; //fDefaultEntryOffsetLen 272 } 273 int fNClusterRange; 274 if(vers>=20) { 275 if(!a_buffer.read(fNClusterRange)) return false; //fNClusterRange 276 } 277 278 if(!a_buffer.read(dummy_int64)) return false; //fMaxEntries 279 if(!a_buffer.read(dummy_int64)) return false; //fMaxEntryLoop 280 {uint64 fMaxVirtualSize; 281 if(!a_buffer.read(fMaxVirtualSize)) return false;} 282 {uint64 fAutoSave; 283 if(!a_buffer.read(fAutoSave)) return false;} 284 if(vers>=18) { 285 if(!a_buffer.read(dummy_int64)) return false; //fAutoFlush 286 } 287 if(!a_buffer.read(dummy_int64)) return false; //fEstimate 288 if(vers>=20) { 289 int64* fClusterRangeEnd = 0; ///<[fNClusterRange] Last entry of a cluster range. 290 if(!fixed_array_stream<int64>(a_buffer,fNClusterRange,fClusterRangeEnd)) return false; 291 delete [] fClusterRangeEnd; 292 int64* fClusterSize = 0; ///<[fNClusterRange] Number of entries in each cluster for a given range. 293 if(!fixed_array_stream<int64>(a_buffer,fNClusterRange,fClusterSize)) return false; 294 delete [] fClusterSize; 295 dummy _dummy; 296 if(!_dummy.stream(a_buffer)) { //TIOFeatures fIOFeatures 297 m_out << "tools::rroot::tree::stream : can't read (dummy) TIOFeatures." << std::endl; 298 return false; 299 } 300 } 301 } 302 303 //FIXME if (fEstimate <= 10000) fEstimate = 1000000; 304 305 //TObjArray 306 //The below m_branches.read will create leaves. 307 //::printf("debug : tree : read branches : begin\n"); 308 {ifac::args args; 309 if(!m_branches.stream(a_buffer,args)) { 310 m_out << "tools::rroot::tree::stream : " 311 << "can't read branches." 312 << std::endl; 313 return false; 314 }} 315 //::printf("debug : tree : read branches : end\n"); 316 317 //TObjArray 318 // We read leaves in order to keep streaming synchronisation. 319 // In fact m_leaves are references to existing leaves read by 320 // the branches in the upper line of code. 321 //::printf("debug : tree : read leaves : begin\n"); 322 {obj_array<base_leaf> m_leaves(m_fac); 323 //branch b(m_out,m_fac); 324 ifac::args args; 325 //args[ifac::arg_branch()] = &b; 326 if(!m_leaves.stream(a_buffer,args)) { 327 m_out << "tools::rroot::tree::stream : " 328 << "can't read leaves." 329 << std::endl; 330 return false; 331 }} 332 //::printf("debug : tree : read leaves : end\n"); 333 334 if(vers>=10) { 335 //TList* fAliases 336 if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) { 337 m_out << "tools::rroot::tree::stream : " 338 << "can't read fAliases." 339 << std::endl; 340 return false; 341 } 342 } 343 344 //m_saved_bytes = m_tot_bytes; 345 {std::vector<double> v; 346 if(!Array_stream<double>(a_buffer,v)) return false;} //fIndexValues TArrayD 347 348 {std::vector<int> v; 349 if(!Array_stream<int>(a_buffer,v)) return false;} // fIndex (TArrayI). 350 351 if(vers>=16) { 352 //TVirtualIndex* fTreeIndex //FIXME ??? 353 if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) { 354 m_out << "tools::rroot::tree::stream : " 355 << "can't read fTreeIndex." 356 << std::endl; 357 return false; 358 } 359 } 360 361 if(vers>=6) { 362 //TList* fFriends 363 if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) { 364 m_out << "tools::rroot::tree::stream : " 365 << "can't read fFriends." 366 << std::endl; 367 return false; 368 } 369 } 370 371 if(vers>=16) { 372 //TList* fUserInfo 373 if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) { 374 m_out << "tools::rroot::tree::stream : " 375 << "can't read fUserInfo." 376 << std::endl; 377 return false; 378 } 379 //TBranchRef* fBranchRef 380 if(!dummy_TXxx_pointer_stream(a_buffer,m_fac)) { 381 m_out << "tools::rroot::tree::stream : " 382 << "can't read fBranchRef." 383 << std::endl; 384 return false; 385 } 386 } 387 388 if(!a_buffer.check_byte_count(_s,_c,TTree_cls())) return false; 389 390 return true; 391 } 392 protected: 393 void _dump_branches(std::ostream& a_out, 394 const std::vector<branch*>& a_bs, 395 const std::string& a_spaces = "", 396 const std::string& a_indent = " "){ 397 tools_vforcit(branch*,a_bs,it) { 398 if(branch_element* be = safe_cast<branch,branch_element>(*(*it))) { 399 a_out << a_spaces 400 << "branch_element :" 401 << " name=" << sout((*it)->name()) 402 << " title=" << sout((*it)->title()) 403 << " entry_number=" << be->entry_number() 404 << " ref_cls=" << sout(be->class_name()) 405 << " (type=" << be->type() 406 << ",id=" << be->id() 407 << ",stype=" << be->streamer_type() 408 << ")." 409 << std::endl; 410 } else { 411 a_out << a_spaces 412 << "branch :" 413 << " name=" << sout((*it)->name()) 414 << " title=" << sout((*it)->title()) 415 << " entry_number=" << (*it)->entry_number() 416 << std::endl; 417 } 418 419 420 {const std::vector<base_leaf*>& lvs = (*it)->leaves(); 421 tools_vforcit(base_leaf*,lvs,itl) { 422 a_out << a_spaces << a_indent 423 << "leave :" 424 << " name=" << sout((*itl)->name()) 425 << " title=" << sout((*itl)->title()) 426 << " cls=" << sout((*itl)->s_cls()) 427 << std::endl; 428 }} 429 430 _dump_branches(a_out,(*it)->branches(),a_spaces+a_indent,a_indent); 431 } 432 } 433 434 static branch* _find_branch(const std::vector<branch*>& a_bs,const std::string& a_name,bool a_recursive) { 435 tools_vforcit(branch*,a_bs,it) { 436 if(rcmp((*it)->name(),a_name)) return *it; 437 if(a_recursive) { 438 branch* br = _find_branch((*it)->branches(),a_name,a_recursive); 439 if(br) return br; 440 } 441 } 442 return 0; 443 } 444 445 static base_leaf* _find_leaf(const std::vector<branch*>& a_bs,const std::string& a_name,bool a_recursive) { 446 tools_vforcit(branch*,a_bs,it) { 447 tools_vforcit(base_leaf*,(*it)->leaves(),itl) { 448 if(rcmp((*itl)->name(),a_name)) return *itl; 449 } 450 if(a_recursive) { 451 base_leaf* lf = _find_leaf((*it)->branches(),a_name,a_recursive); 452 if(lf) return lf; 453 } 454 } 455 return 0; 456 } 457 458 static void _find_leaves(const std::vector<branch*>& a_bs,std::vector<base_leaf*>& a_leaves) { 459 tools_vforcit(branch*,a_bs,it) { 460 tools_vforcit(base_leaf*,(*it)->leaves(),itl) a_leaves.push_back(*itl); 461 _find_leaves((*it)->branches(),a_leaves); 462 } 463 } 464 465 static void _find_branches(const std::vector<branch*>& a_bs,std::vector<branch*>& a_branches){ 466 tools_vforcit(branch*,a_bs,it) { 467 a_branches.push_back(*it); 468 _find_branches((*it)->branches(),a_branches); 469 } 470 } 471 472 static branch* _find_leaf_branch(const std::vector<branch*>& a_bs,const base_leaf& a_leaf) { 473 tools_vforcit(branch*,a_bs,itb) { 474 tools_vforcit(base_leaf*,(*itb)->leaves(),itl) {if(*itl==&a_leaf) return *itb;} 475 {branch* br = _find_leaf_branch((*itb)->branches(),a_leaf); 476 if(br) return br;} 477 } 478 return 0; 479 } 480 protected: 481 ifile& m_file; 482 ifac& m_fac; 483 std::ostream& m_out; 484 //Named 485 std::string m_name; 486 std::string m_title; 487 488 obj_array<branch> m_branches; 489 uint64 m_entries; // Number of entries 490 }; 491 492 }} 493 494 #endif