ATLAS Offline Software
listroot.cxx
Go to the documentation of this file.
1 
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 
33 int ir = 0;
34 
35 bool dirflag = true;
36 bool dbg = true;
37 
38 std::string cwd;
39 
40 static TFile* fout = 0;
41 
42 bool overlay = false;
43 
44 bool plot = false;
45 bool logy = false;
46 
47 bool drawmean = true;
48 
49 double entries = 0;
50 
51 int colours[4] = { kBlack, kRed, kBlue, kMagenta };
52 int styles[4] = { 1, 2, 3, 4 };
53 
54 int istyle = 0;
55 
56 bool drawn = false;
57 
58 bool binwidth = false;
59 
60 double xmin = 0;
61 double xmax = 0;
62 
63 double ymin = 0;
64 double ymax = 0;
65 
66 
67 bool contains( const std::string& s, const std::string& regex ) { return s.find(regex)!=std::string::npos; }
68 
69 
70 
71 
72 
73 template<typename T>
74 std::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 
80 void 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 
88 template<typename T>
89 std::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 
94 void 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 
115 double 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 
155 class inode {
156 public:
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 
177 std::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 
185 std::vector<std::string> directories;
186 
187 std::vector<std::string> patterns;
188 
189 std::vector<std::string> Patterns;
190 
191 std::vector<std::string> veto;
192 
193 double minmean = 0;
194 double maxmean = 1e10;
195 
196 bool 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 
202 int maxdepth = 3;
203 
204 class TCck {
205 
206 public:
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 
219 protected:
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 = inspace;
396  }
397 
398 protected:
399 
400  TDirectory* m_dir;
401  std::vector<inode> m_nodes;
402 
403  int m_depth;
404 
405  static std::string spacer;
406 
407 };
408 
409 
410 
411 std::string TCck::spacer = "";
412 // int TCck::depth = 0;
413 
414 
415 
416 std::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( s );
429 
430  return out;
431 
432 }
433 
434 
435 
436 
437 
438 
439 
440 void 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 
456 int 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( arg );
535 
536  }
537 
538 
539  if ( dir!="" ) directories = split( 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 
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
ymin
double ymin
Definition: listroot.cxx:63
egammaParameters::depth
@ depth
pointing depth of the shower as calculated in egammaqgcld
Definition: egammaParamDefs.h:276
inode::m_dir
TDirectory * m_dir
if this node is, itself a directory
Definition: listroot.cxx:171
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
beamspotPlotBcids.k
k
Definition: beamspotPlotBcids.py:524
directories
std::vector< std::string > directories
Definition: listroot.cxx:185
TIDDirectory::pop
void pop()
Definition: TIDDirectory.h:79
colours
int colours[4]
Definition: listroot.cxx:51
TCck::pscan
void pscan(int depth=0)
Definition: listroot.cxx:221
patterns
std::vector< std::string > patterns
Definition: listroot.cxx:187
hist_file_dump.d
d
Definition: hist_file_dump.py:137
PlotCalibFromCool.label
label
Definition: PlotCalibFromCool.py:78
dirflag
bool dirflag
Definition: listroot.cxx:35
binwidth
bool binwidth
Definition: listroot.cxx:58
CutsMETMaker::accept
StatusCode accept(const xAOD::Muon *mu)
Definition: CutsMETMaker.cxx:18
ir
int ir
Definition: listroot.cxx:33
TH1D
Definition: rootspy.cxx:342
chop
std::string chop(std::string &s1, const std::string &s2)
Definition: hcg.cxx:158
TCck::m_nodes
std::vector< inode > m_nodes
Definition: listroot.cxx:401
fitman.mx
mx
Definition: fitman.py:520
TCck::TCck
TCck(TDirectory *d, int depth=0)
Definition: listroot.cxx:208
minmean
double minmean
these patterns are vetoed
Definition: listroot.cxx:193
isvetoed
bool isvetoed(const std::string &s)
Definition: listroot.cxx:196
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
TCck::m_depth
int m_depth
Definition: listroot.cxx:403
plot
Definition: PhysicsAnalysis/ElectronPhotonID/ElectronPhotonFourMomentumCorrection/python/plot.py:1
inode::m_name
std::string m_name
Definition: listroot.cxx:166
inode::m_type
std::string m_type
Definition: listroot.cxx:167
maxdepth
int maxdepth
Definition: listroot.cxx:202
SH::Meta::dbg
std::string dbg(const Meta &obj, unsigned verbosity=0)
the debugging info of this object
Definition: Meta.cxx:28
DrawLabel
int DrawLabel(float xstart, float ystart, string label)
Definition: GraphToolKit.h:13
binWidth
void binWidth(TH1 *h)
Definition: listroot.cxx:80
logy
bool logy
Definition: listroot.cxx:45
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
TIDDirectory
Definition: TIDDirectory.h:25
TIDDirectory.h
class to keep a directory for each object in a root sort of way, but needed to keep the root objects ...
utils.h
contains
bool contains(const std::string &s, const std::string &regex)
does a string contain the substring
Definition: listroot.cxx:67
Patterns
std::vector< std::string > Patterns
these patterns are ored
Definition: listroot.cxx:189
inode::m_key
TKey * m_key
Definition: listroot.cxx:168
TCck::~TCck
~TCck()
Definition: listroot.cxx:212
TCck
Definition: listroot.cxx:204
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
lumiFormat.i
int i
Definition: lumiFormat.py:92
xmin
double xmin
Definition: listroot.cxx:60
beamspotman.n
n
Definition: beamspotman.py:731
simpletimer.h
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
main
int main(int argc, char **argv)
Definition: listroot.cxx:456
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
operator<<
std::ostream & operator<<(std::ostream &s, const std::vector< T * > &t)
Definition: listroot.cxx:74
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
istyle
int istyle
Definition: listroot.cxx:54
styles
int styles[4]
Definition: listroot.cxx:52
TCck::scan
std::vector< inode > scan(int depth=0)
Definition: listroot.cxx:214
inode::m_nodes
std::vector< inode > m_nodes
Definition: listroot.cxx:172
beamspotman.dir
string dir
Definition: beamspotman.py:623
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
inode::inode
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
create_dcsc_inputs_sqlite.arg
list arg
Definition: create_dcsc_inputs_sqlite.py:48
TCck::spacer
static std::string spacer
Definition: listroot.cxx:405
label.h
drawmean
bool drawmean
Definition: listroot.cxx:47
TH1D::SetBinContent
void SetBinContent(int, double)
Definition: rootspy.cxx:348
mn
const bool mn
Definition: TDVCollectionProxy.cxx:28
TIDDirectory::push
void push()
Definition: TIDDirectory.h:78
maxmean
double maxmean
Definition: listroot.cxx:194
test_pythinning.out
out
Definition: test_pythinning.py:94
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
cwd
std::string cwd
Definition: listroot.cxx:38
minlog
void minlog(TH1 *h)
Definition: listroot.cxx:94
h
scan
void scan(TDirectory *td=0, int depth=0)
Definition: listroot.cxx:440
TCck::m_dir
TDirectory * m_dir
Definition: listroot.cxx:400
TH1
Definition: rootspy.cxx:268
inode::~inode
~inode()
Definition: listroot.cxx:164
xmax
double xmax
Definition: listroot.cxx:61
entries
double entries
Definition: listroot.cxx:49
hmax
double hmax(TH1 *&h)
Definition: listroot.cxx:115
split
std::vector< std::string > split(std::string s, const std::string &regex)
Definition: listroot.cxx:416
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
DrawLabel.h
dqt_zlumi_alleff_HIST.tl
tl
Definition: dqt_zlumi_alleff_HIST.py:73
veto
std::vector< std::string > veto
these patterns are anded
Definition: listroot.cxx:191
drawn
bool drawn
Definition: listroot.cxx:56
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
overlay
bool overlay
Definition: listroot.cxx:42
ymax
double ymax
Definition: listroot.cxx:64
inode
Definition: listroot.cxx:155