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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  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,const std::string& a_name) {
 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 TDirectory."
 36         << std::endl;
 37     return 0;
 38   }
 39   uint32 sz;
 40   char* buf = k->get_object_buffer(a_dir.file(),sz); //we don't have ownership of buf.
 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,k->key_length(),false);
 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_from,const std::string& a_path) {
 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();index++) {
 66     TDirectory* _dir = find_dir(*cur_dir,ws[index]);
 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& a_file,key& a_key,bool a_dump){
 76   unsigned int sz;
 77   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
 78   if(!buf) {
 79     a_out << "tools::rroot::read_key : can't get data buffer of " << a_key.object_name() << "." << std::endl;
 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_key.key_length(),false);
 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 : TNamed streaming failed" << std::endl;
 93     } else {
 94       if(a_dump) {
 95         a_out << "Named : name = " << sout(name) << ", title = " << sout(title) << std::endl;
 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 streaming failed" << std::endl;
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 ownership of h.
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 ownership of h.
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_cls()) {
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" on esb evetest.root files.
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 memory.
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 " << i
204                   << std::endl;
205           }
206         }}
207        {for(uint64 i=mx<int64>(5,entries-5);i<entries;i++){
208           if(!tree.show(a_out,(uint32)i)) {
209             a_out << " show failed for entry " << i
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() << " streaming failed"
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() << " streaming failed"
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() << " streaming failed"
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() << " streaming failed"
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() << " streaming failed"
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_cls()) { //produced with osc/AIDA through BatchLab/Rio/THistogram.
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_THistogram() failed." << std::endl;
280     } else {
281       if(histo::h1d* _h1d = THistogram_to_h1d(data)) {
282         if(a_dump) _h1d->hprint(a_out);
283         delete _h1d;
284       } else if(histo::h2d* _h2d = THistogram_to_h2d(data)) {
285         if(a_dump) _h2d->hprint(a_out);
286         delete _h2d;
287       } else if(histo::p1d* _p1d = THistogram_to_p1d(data)) {
288         if(a_dump) _p1d->hprint(a_out);
289         delete _p1d;
290       } else if(histo::p2d* _p2d = THistogram_to_p2d(data)) {
291         if(a_dump) _p2d->hprint(a_out);
292         delete _p2d;
293       } else {
294         a_out << "tools::rroot::read_key : can't convert THistogram dat to h1d,h2d,p1d or p2d." << std::endl;
295       }
296     }
297 
298   } else if(a_key.object_class()==TDirectory_cls()) {
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 with object class "
305           << sout(a_key.object_class())
306           << std::endl;
307   }
308   return true;
309 }
310 
311 //typedef std::map<std::string,std::string> annotations_t;
312 inline bool key_to_annotations(ifile& a_file,key& a_key,std::map<std::string,std::string>& a_annotations,bool a_warn = true) {
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_annotations : key not a TNamed." << std::endl;
317     return false;
318   }
319   unsigned int sz;
320   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
321   if(!buf) {
322     out << "tools::rroot::key_to_annotations : can't get data buffer of " << a_key.object_name() << "." << std::endl;
323     return false;
324   }
325   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
326   std::string histo_name;
327   std::string sas;
328   if(!Named_stream(b,histo_name,sas)) return false;
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 : wordn should be even in " << sout(sas) << "." << std::endl;
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,key& a_key,bool a_warn = true) {
346   std::ostream& out = a_key.out();
347   if( (a_key.object_class()!=TH1D_cls()) && (a_key.object_class()!=TH1F_cls()) ) {
348     if(a_warn) out << "tools::rroot::key_to_h1d : key not a TH1D and not a TH1F." << std::endl;
349     return 0;
350   }
351   unsigned int sz;
352   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
353   if(!buf) {
354     out << "tools::rroot::key_to_h1d : can't get data buffer of " << a_key.object_name() << "." << std::endl;
355     return 0;
356   }
357   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
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,key& a_key,bool a_warn = true){
366   std::ostream& out = a_key.out();
367   if( (a_key.object_class()!=TH2D_cls()) && (a_key.object_class()!=TH2F_cls()) ) {
368     if(a_warn) out << "tools::rroot::key_to_h2d : key not a TH2D and not a TH2F." << std::endl;
369     return 0;
370   }
371   unsigned int sz;
372   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
373   if(!buf) {
374     out << "tools::rroot::key_to_h2d : can't get data buffer of " << a_key.object_name() << "." << std::endl;
375     return 0;
376   }
377   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
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,key& a_key){
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,sz); //we don't have ownership of buf.
395   if(!buf) {
396     out << "tools::rroot::key_to_h3d :"
397         << " can't get data buffer of " << a_key.object_name() << "."
398         << std::endl;
399     return 0;
400   }
401   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
402   return TH3D_stream(b);
403 }
404 
405 inline histo::p1d* key_to_p1d(ifile& a_file,key& a_key){
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,sz); //we don't have ownership of buf.
415   if(!buf) {
416     out << "tools::rroot::key_to_p1d :"
417         << " can't get data buffer of " << a_key.object_name() << "."
418         << std::endl;
419     return 0;
420   }
421   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
422   return TProfile_stream(b);
423 }
424 
425 inline histo::p2d* key_to_p2d(ifile& a_file,key& a_key){
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,sz); //we don't have ownership of buf.
435   if(!buf) {
436     out << "tools::rroot::key_to_p2d :"
437         << " can't get data buffer of " << a_key.object_name() << "."
438         << std::endl;
439     return 0;
440   }
441   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
442   return TProfile2D_stream(b);
443 }
444 
445 inline tree* key_to_tree(ifile& a_file,ifac& a_fac,key& a_key,bool a_warn = true) {
446   std::ostream& out = a_key.out();
447   if(a_key.object_class()!=TTree_cls()) {
448     if(a_warn) out << "tools::rroot::key_to_tree : key not a TTree." << std::endl;
449     return 0;
450   }
451   unsigned int sz;
452   char* buf = a_key.get_object_buffer(a_file,sz); //we don't have ownership of buf.
453   if(!buf) {
454     out << "tools::rroot::key_to_tree : can't get data buffer of " << a_key.object_name() << "." << std::endl;
455     return 0;
456   }
457   buffer b(out,a_file.byte_swap(),sz,buf,a_key.key_length(),false);
458   b.set_map_objs(true); //for ioda/comres/tree.py, tree.lua.
459   tree* _tree = new tree(a_file,a_fac);
460   if(!_tree->stream(b)) {
461     out << "tools::rroot::key_to_tree : TTree streaming failed" << std::endl;
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_keys,
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_spaces;index++) a_out << " ";}
482         std::string label = k.object_name();
483         a_out << "object : " << sout(label)
484               << ", class : " << k.object_class()
485               << std::endl;
486         //k.dump(a_out);
487       }
488       if(!read_key(a_out,a_file,k,a_dump)) return;
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_spaces;index++) a_out << " ";}
499         std::string label = k.object_name();
500         a_out << "directory : " << label << std::endl;
501       }
502 
503       if(!a_recursive) continue;
504 
505       uint32 sz;
506       char* buf = k.get_object_buffer(a_file,sz);
507       if(!buf) {
508         a_out  << "read :"
509                << " can't get directory data buffer."
510                << std::endl;
511       } else {
512         buffer b(a_out,a_file.byte_swap(),sz,buf,k.key_length(),false);
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.keys();
520           read(a_out,a_file,keys,a_recursive,a_ls,a_dump,a_spaces+1);
521         }
522       }
523     }
524   }}
525 }
526 
527 }}
528 
529 #endif