ATLAS Offline Software
Loading...
Searching...
No Matches
listroot.cxx
Go to the documentation of this file.
1
9
10
11#include "simpletimer.h"
12
13#include <string>
14#include <iostream>
15#include <cstdio>
16#include <cstdlib>
17
18#include "TH1F.h"
19#include "TFile.h"
20#include "TList.h"
21#include "TKey.h"
22#include "TPad.h"
23#include "TStyle.h"
24
25#include "TDirectory.h"
27
28#include "utils.h"
29
30#include "DrawLabel.h"
31#include "label.h"
32
33int ir = 0;
34
35bool dirflag = true;
36bool dbg = true;
37
38std::string cwd;
39
40static TFile* fout = 0;
41
42bool overlay = false;
43
44bool plot = false;
45bool logy = false;
46
47bool drawmean = true;
48
49double entries = 0;
50
51int colours[4] = { kBlack, kRed, kBlue, kMagenta };
52int styles[4] = { 1, 2, 3, 4 };
53
54int istyle = 0;
55
56bool drawn = false;
57
58bool binwidth = false;
59
60double xmin = 0;
61double xmax = 0;
62
63double ymin = 0;
64double ymax = 0;
65
66
67bool contains( const std::string& s, const std::string& regex ) { return s.find(regex)!=std::string::npos; }
68
69
70
71
72
73template<typename T>
74std::ostream& operator<<( std::ostream& s, const std::vector<T*>& t ) {
75 for ( size_t i=0 ; i<t.size() ; i++ ) s << *t[i] << "\n";
76 return s;
77}
78
79
80void binWidth( TH1* h ) {
81 for ( int i=1 ; i<=h->GetNbinsX() ; i++ ) {
82 double d = h->GetBinLowEdge(i+1)-h->GetBinLowEdge(i);
83 h->SetBinContent( i, h->GetBinContent(i)/d );
84 h->SetBinError( i, h->GetBinError(i)/d );
85 }
86}
87
88template<typename T>
89std::ostream& operator<<( std::ostream& s, const std::vector<T>& t ) {
90 for ( size_t i=0 ; i<t.size() ; i++ ) s << t[i] << "\n";
91 return s;
92}
93
94void minlog( TH1* h ) {
95 double mn = 1;
96 double mx = 1;
97 bool unset = true;
98 for ( int i=h->GetNbinsX()+1 ; i-- ; ) {
99 double d = h->GetBinContent(i);
100 if ( d!=0 && unset ) {
101 unset = false;
102 mn = d;
103 mx = d;
104 }
105 if ( d!=0 ) {
106 if ( d<mn ) mn = d;
107 if ( d>mx ) mx = d;
108 }
109 }
110 h->SetMinimum(mn*0.1);
111 h->SetMaximum(mx*10);
112}
113
114
115double hmax( TH1*& h ) {
116
117 if ( h && h->GetEntries()>0 && h->GetNbinsX()>0 ) {
118
119 h->SetDirectory(0);
120
121#if 0
122 TH1D* nh = new TH1D( h->GetName(), h->GetTitle(),
123 h->GetNbinsX(), h->GetBinLowEdge(1), h->GetBinLowEdge( h->GetNbinsX()+1 ) );
124#endif
125
126 TH1D* nh = (TH1D*)h->Clone();
127
128 double n = 0;
129
130 for ( int i=1 ; i<h->GetNbinsX()+1 ; i++ ) {
131 n += h->GetBinContent(i);
132 nh->SetBinContent( i, h->GetBinContent(i) );
133 }
134
135 nh->SetEntries( n );
136
137 // nh->GetXAxis()->SetTitle( h->GetXaxis()->GetTitle() );
138 // nh->GetYAxis()->SetTitle( h->GetYaxis()->GetTitle() );
139
140 h = nh;
141
142
143
144 double vhmax = h->GetBinContent(1);
145 for ( int i=2 ; i<h->GetNbinsX() ; i++ ) {
146 double vm = h->GetBinContent(i);
147 if ( vm>vhmax ) vhmax = vm;
148 }
149 return vhmax;
150 }
151 return 0;
152}
153
154
155class inode {
156public:
157 inode( const std::string& n,
158 const std::string& t,
159 TKey* k, TDirectory* d=0,
160 const std::vector<inode>& in=std::vector<inode>() )
161 : m_name(n), m_type(t), m_key(k), m_dir(d), m_nodes(in) { }
162
163 // ~inode() { std::cout << "delete " << m_name << std::endl; }
164 ~inode() { }
165
166 std::string m_name;
167 std::string m_type;
168 TKey* m_key;
169
171 TDirectory* m_dir;
172 std::vector<inode> m_nodes;
173};
174
175
176
177std::ostream& operator<<( std::ostream& s, const inode& t ) {
178 s << t.m_name << " " << t.m_type;
179 // if ( t.m_dir ) s << "\n" << t.m_nodes;
180 return s;
181}
182
183
184
185std::vector<std::string> directories;
186
187std::vector<std::string> patterns;
188
189std::vector<std::string> Patterns;
190
191std::vector<std::string> veto;
192
193double minmean = 0;
194double maxmean = 1e10;
195
196bool isvetoed( const std::string& s ) {
197 for ( size_t i=0 ; i<veto.size() ; i++ ) if ( contains(s,veto[i]) ) return true;
198 return false;
199}
200
201
202int maxdepth = 3;
203
204class TCck {
205
206public:
207
208 TCck( TDirectory* d, int depth=0 ) : m_dir(d), m_depth(depth) { }
209
210 // ~TCck() { if ( m_dir ) std::cout << "delete dir " << m_dir->GetName() << std::endl; }
211
212 ~TCck() { }
213
214 std::vector<inode> scan( int depth=0 ) {
215 if ( m_nodes.size()==0 ) pscan(depth);
216 return m_nodes;
217 }
218
219protected:
220
221 void pscan( int depth=0 ) {
222
223 if ( depth>maxdepth ) return;
224
225 std::string inspace = spacer;
226
227 TDirectory* cck = gDirectory;
228
229 int deptht = depth-1;
230
231 bool accept = true;
232
233
234 // std::cout << "GOLLY! " << m_dir->GetName() << "\tdepth " << depth << " " << deptht << "\tsize " << directories.size() << " " << directories << std::endl;
235
236
237 if ( directories.size()>size_t(0) ) {
238 if ( deptht>=0 && size_t(deptht)<directories.size() ) {
239 // std::cout << "directory[" << deptht << "] = " << directories[deptht] << " " << std::string(_dir->GetName()) << std::endl;
240 if ( directories[deptht]!=std::string(m_dir->GetName() ) &&
241 !( // contains( m_dir->GetName(), "SMK_" ) &&
242 contains( m_dir->GetName(), directories[deptht] ) ) ) accept = false;
243 }
244 }
245
246 // std::cout << "accept " << accept << std::endl;
247
248
249 if ( !accept ) return;
250
251 // std::cout << spacer << m_dir->GetName() << "\tdepth " << _depth << std::endl;
252
253 spacer += "\t";
254
255
256 m_dir->cd();
257
258
259 TList* tl = m_dir->GetListOfKeys();
260
261
262 for ( int i=tl->GetSize() ; i-- ; ) {
263
264 TKey* tobj = (TKey*)tl->At(i);
265 if ( tobj ) {
266 TDirectory* tnd = 0;
267 std::vector<inode> nodes;
268
269 // if ( depth>1 ) continue;
270
271 // std::cout << "(" << depth << ")\t" << spacer << tobj->GetName() << "\t: " << tobj->GetClassName() << std::endl;
272
273 if ( std::string(tobj->GetClassName()).find("TDirectory")!=std::string::npos ) {
274 tnd = (TDirectory*)tobj->ReadObj();
275 nodes = TCck( tnd, depth+1 ).scan( depth+1 );
276 m_nodes.push_back( inode( tobj->GetName(), tobj->GetClassName(), tobj, tnd, nodes ) );
277 }
278 else {
279
280 bool accept = false;
281
282 if ( patterns.empty() ) accept = true;
283 else {
284 for ( unsigned ip=patterns.size() ; ip-- ; ) {
285 if ( std::string( tobj->GetName()).find( patterns[ip] )!=std::string::npos ) accept = true;
286 // if ( accept ) std::cout << "match " << patterns[ip] << std::endl;
287 }
288 }
289
290 for ( unsigned ip=Patterns.size() ; ip-- ; ) {
291 if ( std::string( tobj->GetName()).find( Patterns[ip] )==std::string::npos ) accept = false;
292 // if ( accept ) std::cout << "match " << Patterns[ip] << std::endl;
293 }
294
295 if ( accept && isvetoed(tobj->GetName()) ) accept = false;
296
297 if ( accept ) {
298 m_nodes.push_back( inode( tobj->GetName(), tobj->GetClassName(), tobj ) );
299
300
301 // bool display = true;
302
303 std::cout << "(" << depth << ")\t" << spacer << m_nodes.back(); // << " " << tobj->GetName();
304
305 if ( std::string( tobj->GetClassName()).find("TH1")!=std::string::npos ) {
306 TH1* h = (TH1*)tobj->ReadObj();
307 h->SetDirectory(0);
308
309 if ( entries!=0 && h->GetEntries()>entries ) {
310 std::cout << "\tentries " << h->GetEntries() << " \tmean " << h->GetMean() << "\tplot " << plot << "\t"; // << std::endl;
311 }
312
313
314 if ( plot ) {
315 // std::cout << "plotting " << tobj->GetName() << "\tmean " << h->GetMean() << "\tentries " << h->GetEntries() << "\tis vetoed" << isvetoed( tobj->GetName() ) << std::endl;
316 if ( h->GetMean()>=minmean && h->GetMean()<=maxmean ) {
317 // std::cout << "plotting " << tobj->GetName() << "\tmean " << h->GetMean() << std::endl;
318 std::cout << "plotting min " << minmean << "\tmax " << maxmean; // << tobj->GetName() << "\tmean " << h->GetMean() << std::endl;
319 h->SetMaximum( hmax(h)*1.2 );
320
321 std::string t = tobj->GetName();
322
323 if ( t.find("____")!=std::string::npos ) t.erase( 0, t.find("___")+4);
324 if ( t.find("_FirstTime")!=std::string::npos ) t.erase( t.find("_FirstTime"), t.size());
325
326 h->SetTitle(t.c_str());
327
328 if ( logy ) minlog( h );
329
330 h->SetLineColor( colours[istyle%4] );
331 h->SetLineStyle( styles[(istyle/4)%4] );
332
333 if ( binwidth ) binWidth( h );
334
335 if ( !drawn || !overlay ) {
336 if ( xmax!=xmin ) h->GetXaxis()->SetRangeUser( xmin, xmax );
337 if ( ymax!=ymin ) {
338 h->SetMaximum( ymax );
339 if ( logy && ymin!=0 ) h->SetMinimum( ymin );
340 }
341 h->DrawCopy("hist");
342 }
343
344 h->DrawCopy("samehist");
345
346
347 if ( drawmean ) DrawLabel( 0.5, 0.8-istyle*0.05, label("mean = %5.2lf #pm %5.2lf ms", h->GetMean(), h->GetMeanError() ), colours[istyle%4] );
348
349 if ( logy ) gPad->SetLogy(true);
350 gPad->Print( (std::string(tobj->GetName())+".pdf").c_str() );
351
352
353 if ( overlay ) istyle++;
354
355 drawn = true;
356 }
357 }
358
359 std::cout << std::endl;
360
361 // else std::cout << std::endl;
362
363 if ( fout ) {
364 std::string cckname = tobj->GetName();
365 TDirectory* cck = gDirectory;
366 fout->cd();
367
368 std::string tdir = chop( cckname, "____" );
369
370 if ( cckname.size()==0 ) {
371 h->SetName( tdir.c_str() );
372 h->Write();
373 }
374 else {
375 TIDDirectory dcck(tdir);
376 dcck.push();
377
378 h->SetName( cckname.c_str() );
379 h->Write();
380 dcck.pop();
381 }
382 cck->cd();
383 }
384 }
385 else {
386 std::cout << std::endl;
387 }
388 }
389 }
390 }
391 }
392
393 if ( cck ) cck->cd();
394
395 spacer = std::move(inspace);
396 }
397
398protected:
399
400 TDirectory* m_dir;
401 std::vector<inode> m_nodes;
402
404
405 static std::string spacer;
406
407};
408
409
410
411std::string TCck::spacer = "";
412// int TCck::depth = 0;
413
414
415
416std::vector<std::string> split( std::string s, const std::string& regex ) {
417
418 std::vector<std::string> out;
419
420 size_t pos = s.find(regex);
421
422 while ( pos!=std::string::npos ) {
423 out.push_back( s.substr( 0, pos ) );
424 s.erase( 0, pos+1 );
425 pos = s.find(regex);
426 }
427
428 out.push_back( std::move(s) );
429
430 return out;
431
432}
433
434
435
436
437
438
439
440void scan(TDirectory* td=0, int depth=0 ) {
441
442 if ( td ) {
443 TCck t( td, depth );
444 std::cout << t.scan( depth ) << std::endl;
445 }
446
447 // if ( std::string(tobj->GetClassName()).find("TH1")!=std::string::npos ) add<TH1>( objname.c_str(), tobj, depth+"\t" );
448 // if ( std::string(tobj->GetClassName()).find("TH2")!=std::string::npos ) add<TH2>( objname.c_str(), tobj, depth+"\t" );
449 // if ( std::string(tobj->GetClassName()).find("TProfile")!=std::string::npos ) add<TProfile>( objname.c_str(), tobj, depth+"\t" );
450
451}
452
453
454
455
456int main(int argc, char** argv) {
457
458 gStyle->SetPadLeftMargin(0.15);
459 gStyle->SetPadBottomMargin(0.15);
460
461 std::vector<std::string> files;
462
463 std::string dir = "";
464
466
467 std::string outfile = "";
468
469
470 for ( int i=1 ; i<argc ; i++ ) {
471
472 std::string arg = argv[i];
473
474 if ( arg=="-o" ) {
475 if (++i < argc) outfile = argv[i];
476 else return -1;
477 }
478 else if ( arg=="--depth" ) {
479 if (++i < argc) maxdepth = std::atoi(argv[i]);
480 else return -1;
481 }
482 else if ( arg=="-d" ) {
483 if (++i < argc) dir = argv[i];
484 else return -1;
485 }
486 else if ( arg=="-p" ) {
487 if (++i < argc) patterns.push_back( argv[i] );
488 else return -1;
489 }
490 else if ( arg=="-v" ) {
491 if (++i < argc) veto.push_back( argv[i] );
492 else return -1;
493 }
494 else if ( arg=="--min" ) {
495 if (++i < argc) minmean = std::atof( argv[i] );
496 else return -1;
497 }
498 else if ( arg=="--max" ) {
499 if (++i < argc) maxmean = std::atof( argv[i] );
500 else return -1;
501 }
502 else if ( arg=="--entries" ) {
503 if (++i < argc) entries = std::atof( argv[i] );
504 else return -1;
505 }
506 else if ( arg=="-P" ) {
507 if (++i < argc) Patterns.push_back( argv[i] );
508 else return -1;
509 }
510 else if ( arg=="--xmax" ) {
511 if (++i < argc) xmax = std::atof( argv[i] );
512 else return -1;
513 }
514 else if ( arg=="--xmax" ) {
515 if (++i < argc) xmax = std::atof( argv[i] );
516 else return -1;
517 }
518 else if ( arg=="--xmin" ) {
519 if (++i < argc) xmin = std::atof( argv[i] );
520 else return -1;
521 }
522 else if ( arg=="--ymin" ) {
523 if (++i < argc) ymin = std::atof( argv[i] );
524 else return -1;
525 }
526 else if ( arg=="--ymax" ) {
527 if (++i < argc) ymax = std::atof( argv[i] );
528 else return -1;
529 }
530 else if ( arg=="--plot" ) plot = true;
531 else if ( arg=="--logy" ) logy = true;
532 else if ( arg=="--overlay" ) overlay = true;
533 else if ( arg=="--binwidth" ) binwidth = true;
534 else files.push_back( std::move(arg) );
535
536 }
537
538
539 if ( dir!="" ) directories = split( std::move(dir), "/" );
540
542
543 if ( outfile!="" ) {
544 fout = new TFile( outfile.c_str(), "recreate" );
545 std::cout << "saving to file: " << outfile << std::endl;
546 }
547
548
549 for ( size_t i=0 ; i<files.size() ; i++ ) {
550
551 TFile* f = new TFile( files[i].c_str() );
552 f->cd();
553
554 scan( gDirectory, 0 ); //, dir );
555
556 std::cout << "closing" << std::endl;
557
558 // std::cout << "root is extremely annoying, why is it taking so long to close this file?" << std::endl;
559
560 // f->Close();
561
562 std::cout << "done" << std::endl;
563
564 }
565
566 if ( fout ) {
567 fout->Write();
568 fout->Close();
569 }
570
571 return 0;
572}
573
574
575
int DrawLabel(float xstart, float ystart, string label)
const bool mn
these functions have a precision of about 0.001 ms
Header file for AthHistogramAlgorithm.
std::vector< inode > scan(int depth=0)
Definition listroot.cxx:214
static std::string spacer
Definition listroot.cxx:405
TDirectory * m_dir
Definition listroot.cxx:400
void pscan(int depth=0)
Definition listroot.cxx:221
int m_depth
Definition listroot.cxx:403
std::vector< inode > m_nodes
Definition listroot.cxx:401
~TCck()
Definition listroot.cxx:212
TCck(TDirectory *d, int depth=0)
Definition listroot.cxx:208
TKey * m_key
Definition listroot.cxx:168
TDirectory * m_dir
if this node is, itself a directory
Definition listroot.cxx:171
std::string m_type
Definition listroot.cxx:167
std::vector< inode > m_nodes
Definition listroot.cxx:172
inode(const std::string &n, const std::string &t, TKey *k, TDirectory *d=0, const std::vector< inode > &in=std::vector< inode >())
Definition listroot.cxx:157
std::string m_name
Definition listroot.cxx:166
int colours[6]
Definition computils.cxx:46
int ir
counter of the current depth
Definition fastadd.cxx:49
std::string depth
tag string for intendation
Definition fastadd.cxx:46
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
std::string chop(std::string &s1, const std::string &s2)
Definition hcg.cxx:161
int main()
Definition hello.cxx:18
std::string label(const std::string &format, int i)
Definition label.h:19
std::vector< std::string > veto
these patterns are anded
Definition listroot.cxx:191
double minmean
these patterns are vetoed
Definition listroot.cxx:193
double xmax
Definition listroot.cxx:61
std::vector< std::string > directories
Definition listroot.cxx:185
std::vector< std::string > Patterns
these patterns are ored
Definition listroot.cxx:189
std::string cwd
Definition listroot.cxx:38
bool logy
Definition listroot.cxx:45
int istyle
Definition listroot.cxx:54
std::ostream & operator<<(std::ostream &s, const std::vector< T * > &t)
Definition listroot.cxx:74
int colours[4]
Definition listroot.cxx:51
double maxmean
Definition listroot.cxx:194
void scan(TDirectory *td=0, int depth=0)
Definition listroot.cxx:440
void binWidth(TH1 *h)
Definition listroot.cxx:80
bool contains(const std::string &s, const std::string &regex)
does a string contain the substring
Definition listroot.cxx:67
bool isvetoed(const std::string &s)
Definition listroot.cxx:196
bool overlay
Definition listroot.cxx:42
bool drawmean
Definition listroot.cxx:47
double ymin
Definition listroot.cxx:63
bool drawn
Definition listroot.cxx:56
bool dirflag
Definition listroot.cxx:35
double entries
Definition listroot.cxx:49
static TFile * fout
Definition listroot.cxx:40
int maxdepth
Definition listroot.cxx:202
double xmin
Definition listroot.cxx:60
std::vector< std::string > split(std::string s, const std::string &regex)
Definition listroot.cxx:416
bool binwidth
Definition listroot.cxx:58
double hmax(TH1 *&h)
Definition listroot.cxx:115
int styles[4]
Definition listroot.cxx:52
std::vector< std::string > patterns
Definition listroot.cxx:187
void minlog(TH1 *h)
Definition listroot.cxx:94
double ymax
Definition listroot.cxx:64