ATLAS Offline Software
Loading...
Searching...
No Matches
hcg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4//
5// @file hanconfig.cxx
6// navigates through the directory structure of a monitoring
7// root files and write out some han config boiler plate
8//
9// @author M.Sutton
10//
11//
12//
13
14
15#include <iostream>
16#include <vector>
17#include <string>
18#include <map>
19#include <set>
20
21#include <cstdlib>
22#include <cstdio>
23
24#include <fstream>
25
26#include <sys/stat.h>
27
28#include "simpletimer.h"
29#include "node.h"
30#include "addnode.h"
31#include "spacer.h"
32
33#include "TStyle.h"
34#include "TCanvas.h"
35#include "TKey.h"
36#include "TH1D.h"
37#include "TH2D.h"
38#include "TProfile.h"
39#include "TFile.h"
40#include "TClass.h"
41#include "TDirectory.h"
42
43#include "TPad.h"
44#include <algorithm>
45
48
50std::vector<std::string> files;
51std::vector<TFile*> fptr;
52
53std::vector<std::string> savedhistos;
54std::vector<std::string> mapped;
55
56
58std::string date() {
59 time_t t;
60 time(&t);
61 std::string a = ctime(&t);
62 std::string b = "";
63 for ( unsigned i=0 ; i<a.size()-1 ; i++ ) b+=a[i];
64 return b;
65}
66
67
68bool file_exists( const std::string& file ) {
69 struct stat buff;
70 return stat(file.c_str(),&buff)==0;
71}
72
73bool verbose = false;
74
76std::ostream* outp = &std::cout;
77
78
79bool allhists = true;
80
81std::string base = "HLT";
82
83std::string outref = "";
84
85std::string algorithm = "HLT_Histogram_Not_Empty&GatherData";
86
88// struct timeval global_timer;
89
90
91std::string description = "https://twiki.cern.ch/twiki/bin/view/Atlas/HltTrackingDataQualityMonitoring#Tier0";
92
93
95std::map<std::string, std::string> remap;
96
98std::set<std::string> exclude;
99
102std::map<std::string, int> dirs;
103
104
105std::vector<std::string> tags;
106
107template<typename T>
108std::ostream& operator<<( std::ostream& s, const std::vector<T>& v ) {
109 if ( v.empty() ) return s;
110 for ( size_t i=0 ; i<v.size() ; i++ ) s << v[i] << "\n";
111 return s;
112}
113
114bool contains( const std::string& s, const std::string& regx ) { return s.find(regx)!=std::string::npos; }
115
116
117
118template<typename T>
119std::ostream& operator<<( std::ostream& s, const std::vector<T*>& v ) {
120 if ( v.empty() ) return s;
121 for ( int i=0 ; i<v.size() ; i++ ) s << *v[i] << "\n";
122 return s;
123}
124
125
126
129template<class T>
130T* get( TKey* tobj ) {
131 TObject* a = tobj->ReadObj()->Clone();
132 static_cast<TH1*>(a)->SetDirectory(0);
133 return static_cast<T*>(a);
134}
135
136
138std::string find( const std::string& s ) {
139 std::map<std::string, std::string>::const_iterator itr = remap.find(s);
140 if ( itr!=remap.end() ) return itr->second;
141 else return s;
142}
143
144
146int count( std::string s, const std::string& regx ) {
147 size_t p = s.find( regx );
148 int i=0;
149 while ( p!=std::string::npos ) {
150 i++;
151 s.erase( 0, p+1 );
152 p = s.find( regx );
153 }
154 return i;
155}
156
157
158
159
160// chop tokens off the front of a string
161std::string chop(std::string& s1, const std::string& s2)
162{
163 std::string::size_type pos = s1.find(s2);
164 std::string s3;
165 if ( pos == std::string::npos ) {
166 s3 = s1;
167 s1.erase(0, s1.size());
168 }
169 else {
170 s3 = s1.substr(0, pos);
171 s1.erase(0, pos+s2.size());
172 }
173 return s3;
174}
175
176
177std::vector<std::string> split( const std::string& s, const std::string& t=":" ) {
178
179 std::string s2 = s;
180 size_t pos = s2.find(t);
181
182 std::vector<std::string> tags;
183
184 while ( pos!=std::string::npos ) {
185 tags.push_back( chop(s2,t) );
186 pos = s2.find(t);
187 }
188
189 tags.push_back(std::move(s2));
190
191 return tags;
192}
193
194
195// chop tokens off the front of a string but not including
196// chop character
197std::string chopex(std::string& s1, const std::string& s2)
198{
199 std::string::size_type pos = s1.find(s2);
200 std::string s3;
201 if ( pos == std::string::npos ) {
202 s3 = s1;
203 s1.clear();
204 }
205 else {
206 s3 = s1.substr(0, pos);
207 s1.erase(0, pos+s2.size());
208 }
209 return s3;
210}
211
212
213// chomp them off the end
214std::string chomp(std::string& s1, const std::string& s2)
215{
216 std::string::size_type pos = s1.find(s2);
217 std::string s3;
218 if ( pos == std::string::npos ) {
219 s3 = s1;
220 s1.clear();
221 }
222 else {
223 s3 = s1.substr(pos+s2.size(),s1.size());
224 s1.erase(pos,s1.size());
225 }
226 return s3;
227}
228
229
230
231// chop tokens off the end of a string, leave string unchanged
232// return choped string
233std::string choptoken(std::string& s1, const std::string& s2)
234{
235 std::string s3 = "";
236 std::string::size_type pos = s1.find(s2);
237 if ( pos != std::string::npos ) {
238 s3 = s1.substr(0, pos+s2.size());
239 s1.erase(0, pos+s2.size());
240 }
241 return s3;
242}
243
244
245// chop tokens off the end of a string, leave string unchanged
246// return choped string
247std::string chomptoken(std::string& s1, const std::string& s2)
248{
249 std::string s3 = "";
250 std::string::size_type pos = s1.find(s2);
251 if ( pos != std::string::npos ) {
252 s3 = s1.substr(pos, s1.size());
253 s1.erase(pos, s1.size());
254 }
255 return s3;
256}
257
258
259// chop tokens off the front of a string
260std::string chopfirst(std::string& s1, const std::string& s2)
261{
262 std::string s3;
263 std::string::size_type pos = s1.find_first_not_of(s2);
264 if ( pos != std::string::npos ) {
265 s3 = s1.substr(0, pos);
266 s1.erase(0, pos);
267 }
268 else {
269 s3 = s1;
270 s1.clear();
271 }
272 return s3;
273}
274
275
276std::string choplast(std::string& s1, const std::string& s2)
277{
278 std::string s3 = "";
279 std::string::size_type pos = s1.find_last_not_of(s2);
280 if ( pos != std::string::npos ) {
281 s3 = s1.substr(pos+1, s1.size());
282 s1.erase(pos+1, s1.size());
283 }
284 return s3;
285}
286
287
288
289// chop tokens off the front and end of a string
290std::string chopends(std::string& s1, const std::string& s2)
291{
292 chopfirst(s1, s2);
293 choplast(s1, s2);
294 return s1;
295}
296
297
298
299// remove strings from a string
300void removespace(std::string& s, const std::string& s2)
301{
302 std::string::size_type pos;
303 while ( (pos = s.find(s2))!=std::string::npos ) {
304 s.erase(pos, s2.size());
305 }
306}
307
308
309// replace from a string
310std::string replace( std::string s, const std::string& s2, const std::string& s3) {
311 std::string::size_type pos;
312 // while ( (pos = s.find(" "))!=std::string::npos ) {
313 // s.replace(pos, 1, "-");
314 while ( (pos = s.find(s2))!=std::string::npos ) {
315 s.replace(pos, s2.size(), s3);
316 if ( contains(s3,s2) ) break;
317 }
318 return s;
319}
320
321
322// remove regx from a string
323void depunctuate(std::string& s, const std::string& regex=":")
324{
325 std::string::size_type pos;
326 while ( (pos = s.find(regex))!=std::string::npos ) {
327 s.erase(pos, regex.size());
328 }
329}
330
331
332
333std::vector<std::string> maphist( const std::vector<std::string>& v ) {
334 mapped.clear();
335 for ( unsigned i=0 ; i<v.size() ; i++ ) {
336 if ( contains( v[i], "Expert" ) ){
337 std::string tmp = v[i];
338 std::string path = chop( tmp, "Expert/" );
339 path += "Chains/";
340 tmp = replace( tmp, "/", "__" );
341 path += tmp;
342 mapped.push_back( std::move(path) );
343 }
344 else {
345 mapped.push_back( v[i] );
346 }
347 }
348
349 return mapped;
350}
351
352
353
354
355
357bool match( std::string s1, std::string s2 ) {
358
359 int i1 = std::count( s1.begin(), s1.end(), '/' );
360 int i2 = std::count( s2.begin(), s2.end(), '/' );
361
362 int i = ( i1<i2 ? i1 : i2 );
363
364 // std::cerr << "match s1 " << s1 << " " << s2 << std::endl;
365
366 for ( i++ ; i-- ; ) {
367 size_t p1 = s1.find('/');
368 size_t p2 = s2.find('/');
369
370 std::string ss1 = s1.substr( 0, p1 );
371 std::string ss2 = s2.substr( 0, p2 );
372
373 s1.erase( 0, p1+1 );
374 s2.erase( 0, p2+1 );
375
376 // std::cerr << i << "\tmatch :" << ss1 << ":" << ss2 << "::\t " << s1 << ":" << s2 << "\tresult " << (ss1!=ss2 ) << std::endl;
377
378 if ( ss1!=ss2 ) return false;
379
380 }
381
382 return true;
383
384}
385
386
389
390bool matchdir( const std::string& s ) {
391 bool matched = false;
392 // int idepth = count( s, "/" );
393 std::map<std::string,int>::const_iterator itr = dirs.begin();
394 while ( itr!=dirs.end() ) {
395 if ( match( s, itr->first) ) matched = true;
396 // std::cerr << "\tmatchdir :" << s << "::" << itr->first << ": " << itr->second << std::endl;
397 if ( matched ) return true;
398 ++itr;
399 }
400 return false;
401}
402
403bool matchcwd( const std::string& s ) {
404 if ( dirs.size()==0 ) return true;
405 std::map<std::string,int>::const_iterator itr = dirs.begin();
406 while ( itr!=dirs.end() ) {
407 if ( s.find(itr->first)!=std::string::npos ) return true;
408 ++itr;
409 }
410 return false;
411}
412
413
414
415std::string matchcwdstr( const std::string& s ) {
416 if ( dirs.size()==0 ) return "";
417 std::map<std::string,int>::const_iterator itr = dirs.begin();
418 while ( itr!=dirs.end() ) {
419 if ( s.find(itr->first)!=std::string::npos ) return itr->first;
420 ++itr;
421 }
422 return "";
423}
424
425
426std::map<std::string,int>::const_iterator matchcwditr( const std::string& s ) {
427 if ( dirs.size()==0 ) return dirs.end();
428 std::map<std::string,int>::const_iterator itr = dirs.begin();
429 while ( itr!=dirs.end() ) {
430 if ( s.find(itr->first)!=std::string::npos ) return itr;
431 ++itr;
432 }
433 return dirs.end();
434}
435
436
437
438class reference {
439
440public:
441
442 reference( const std::string& n, const std::string& f ) :
443 m_name(n), m_file(f) {
444
446
447 std::cerr << "opening file " << f << std::endl;
448
449 TFile* r = TFile::Open(f.c_str());
450 if ( r==0 ) {
451 std::cerr << "cannot open root file " << f << std::endl;
452 std::exit(-1);
453 }
454
455 r->cd();
456
457 TList* tl = gDirectory->GetListOfKeys();
458
460
461 for ( int i=0 ; i<tl->GetSize() ; i++ ) {
462
463 TKey* tobj = (TKey*)tl->At(i);
464
465 if ( std::string(tobj->GetClassName()).find("TDirectory")!=std::string::npos ) {
466 // (*outp) << ns << "Directory " << tobj->GetName() << std::endl;
467
468 TDirectory* tnd = (TDirectory*)tobj->ReadObj();
469
470 std::string dir = tnd->GetName();
471
472 if ( contains( dir, "run_" ) ) {
473 dir.erase( 0, std::string( "run_").size() );
474 m_run = std::atoi( dir.c_str() );
475
476 break;
477 }
478
479 }
480 }
481
482 r->Close();
483 }
484
485
487
488
489 const std::string& name() const { return m_name; }
490 const std::string& file() const { return m_file; }
491
492 int run() const { return m_run; }
493
494private:
495
496 std::string m_name;
497 std::string m_file;
498
499 int m_run{};
500
501};
502
503
504
505std::ostream& operator<<( std::ostream& s, const reference& r ) {
506 static bool first = true;
507 if ( first ) {
508 s << "##########################\n";
509 s << "# References\n";
510 s << "##########################\n\n";
511 first = false;
512 }
513 s << "reference " << r.name() << " {\n";
514 s << "\t\tfile = " << r.file() << "\n";
515 s << "\t\tpath = run_" << r.run() << "\n";
516 s << "\t\tname = same_name" << "\n";
517 s << "}\n\n";
518 return s;
519}
520
521
522
523std::vector<reference> references;
524
525
526
527class header {
528
529public:
530
531 header( ) {
532
533 std::string user = std::getenv("USER");
534
535 (*outp) << "######################################################################\n";
536 (*outp) << "# $Id: collisions_run.config " << date() << " " << user << " $\n";
537 (*outp) << "######################################################################\n";
538
539 (*outp) << "\n";
540 (*outp) << "#######################\n";
541 (*outp) << "# HLTidtrk\n";
542 (*outp) << "#######################\n";
543
544 (*outp) << "\n\n";
545 }
546
547};
548
549
551
552class menu {
553
554public:
555
556 menu( node& n ) {
557
558 (*outp) << "\n\n";
559 (*outp) << "#######################\n";
560 (*outp) << "# Output\n";
561 (*outp) << "#######################\n\n\n";
562
563 makemenu( n );
564
565 }
566
567
568 void makemenu( node& n, const std::string& space="", std::string path="", std::string rawpath="", bool found=false ) {
569
570 bool print = false;
571
572 if ( n.name()==base ) found = true;
573
574 if ( n.type()==node::DIRECTORY ) print = found;
575 if ( n.name()=="top_level" ) print = true;
576
577 if ( n.size() ) {
578
580
581 // bool exclude_dir = false;
582
583 if ( exclude.find(n.name())!=exclude.end() ) {
584 print = false;
585 // exclude_dir = true;
586 return;
587 }
588
589 // if ( found && ( dirs.size() && dirs.find(n.name())==dirs.end() ) ) print = false;
590
591 std::string newspacer = space;
592
593 if ( print ) newspacer += spacer;
594
595 std::string output_name = find(n.name());
596
597
598 if ( print && n.type()==node::DIRECTORY ) {
599 if ( path=="" ) path += output_name;
600 else path += "/" + output_name;
601 if ( rawpath=="" ) rawpath += n.name();
602 else rawpath += "/" + n.name();
603 }
604
605 // std::cerr << "path " << path << "\tmatchdir " << matchdir( path ) << std::endl;
606
607 if ( found && ( dirs.size() && (!matchdir( path ) && !matchdir( rawpath ) ) ) ) return;
608
609 if ( print ) (*outp) << space << "output " << output_name << " {" << "\n"; // " \t\t(" << path << " size " << n.size() << ")\n";
610
611 for ( unsigned i=0 ; i<n.size() ; i++ ) {
612 if ( n[i]->type()!=node::HISTOGRAM ) makemenu( *n[i], newspacer, path, rawpath, found ) ;
613 }
614
615 if ( print ) (*outp) << space << "}\n";
616 }
617
618 }
619
620};
621
622
623
624
626
627class ass {
628
629public:
630
631 ass( node& n, bool ah=true ) : m_allhists(ah) {
632 (*outp) << "\n\n";
633 (*outp) << "#######################\n";
634 (*outp) << "# Histogram Assessments\n";
635 (*outp) << "#######################\n\n";
636 makeass( n );
637 }
638
639
640 void makeass( node& n, const std::string& space="", std::string path="", std::string rawpath="", bool found=false ) {
641
642 static std::string savedir = "";
643
644 bool print = false;
645
646 if ( n.name()==base ) found = true;
647
648 if ( n.type()==node::DIRECTORY ) print = found;
650
651 if ( n.size() ) {
652
654
655 std::string newspacer = space;
656
657 if ( exclude.find(n.name())!=exclude.end() ) {
658 print = false;
659 return;
660 }
661
662 // if ( found && dirs.size() && dirs.find(n.name())==dirs.end() ) print = false;
663
664 if ( print ) newspacer += spacer;
665
666 std::string output_name = find(n.name());
667
668 if ( print && n.type()==node::DIRECTORY ) {
669 if ( path=="" ) path += output_name;
670 else path += "/" + output_name;
671 if ( rawpath=="" ) rawpath += n.name();
672 else rawpath += "/" + n.name();
673 }
674
675 if ( found && ( dirs.size() && !matchdir( path ) && !matchdir( rawpath ) ) ) return;
676
677 if ( print ) (*outp) << space << "dir " << n.name() << " {" << "\n"; // " \t\t(" << path << ")\n";
678
679 bool first_hists = true;
680
681 for ( unsigned i=0 ; i<n.size() ; i++ ) {
682 if ( n[i]->type()!=node::HISTOGRAM ) makeass( *n[i], newspacer, path, rawpath, found ) ;
683 else if ( n[i]->type()==node::HISTOGRAM ) {
684 if ( !m_allhists ) {
685 if ( first_hists ) {
686 (*outp) << space << "\t" << "hist .* {\n";
687 (*outp) << space << "\t\t" << "regex \t= 1\n";
688 (*outp) << space << "\t\t" << "algorithm \t= " << algorithm << "\n";
689 (*outp) << space << "\t\t" << "description \t= " << description << "\n";
690 (*outp) << space << "\t\t" << "output \t= " << path << "\n";
691 (*outp) << space << "\t\t" << "display \t= StatBox\n";
693 for ( unsigned it=0 ; it<tags.size() ; it++ ) (*outp) << space << "\t\t" << replace(tags[it],"=","\t=") << "\n";
694 (*outp) << space << "\t" << "}\n";
695 }
696 first_hists = false;
697 }
698 else {
699 (*outp) << space << "\t" << "hist " << n[i]->name() << " {\n";
700 (*outp) << space << "\t\t" << "algorithm \t= " << algorithm << "\n";
701 (*outp) << space << "\t\t" << "description \t= " << description << "\n";
702 (*outp) << space << "\t\t" << "output \t= " << path << "\n";
703 (*outp) << space << "\t\t" << "display \t= StatBox\n";
705 for ( unsigned it=0 ; it<tags.size() ; it++ ) (*outp) << space << "\t\t" << replace(tags[it],"=","\t=") << "\n";
706 (*outp) << space << "\t" << "}\n";
707
708 // TH1* ase = (TH1*)(n[i]->object());
709 // if ( ase ) std::cerr << space << "\t\t" << "entries = " << ase->GetEntries() << "\n";
710 // if ( ase ) std::cerr << space << "\t\t" << "entries = \"" << ase->GetTitle() << "\"\n";
711
712 }
713 }
714 }
715
716 if ( print ) (*outp) << space << "}\n";
717 }
718
719 }
720
721private:
722
724
725};
726
727
728
729
730
731
732bool found_dir = false;
733
734std::string currentfile = "";
735
736
738
739void search( TDirectory* td, const std::string& s, std::string cwd, node* n ) {
740
742 if ( td==0 ) return;
743
744 if ( std::string(td->GetName()).find("_LB")!=std::string::npos ) return;
745 if ( std::string(td->GetName()).find("lb_")!=std::string::npos ) return;
746
747 // std::cout << "search() in " << s << td->GetName() << ": :" << cwd << ":" << std::endl;
748
749 static int ir = 0;
750
751 ir++;
752
754
755 if ( ir>10 ) {
756 std::cerr << "search() WARNING too many levels of directories (max 10) !!!\n";
757 return;
758 }
759
760
761 TDirectory* here = gDirectory;
762
763 // gDirectory->pwd();
764
765 td->cd();
766
767 if ( cwd!="" ) cwd += "/";
768 cwd += td->GetName();
769
770 node* np = n;
771
772 std::string fulldir = td->GetName();
773
774
775 bool first_found = false;
776 if ( !found_dir && matchcwd( cwd ) ) {
777
778 std::string ase = matchcwdstr( cwd );
779
780 if ( (cwd+"/").find( ase+"/" )!=std::string::npos ) {
781
782 found_dir = true;
783 first_found = true;
784
785 std::cerr << "matched dirs " << cwd << " " << matchdir( cwd ) << std::endl;
786
787
788 std::map<std::string,int>::const_iterator fitr = matchcwditr( cwd );
789
790 if ( fitr!=dirs.end() ) {
791
792 if ( fitr->second>0 ) {
793
794 std::vector<std::string> subpath;
795
796 std::string sp = fitr->first;
797
798 while( sp.size() ) subpath.push_back( chop(sp,"/") );
799
800 for ( unsigned ip=0 ; ip<subpath.size()-1 ; ip++ ) {
801 // std::cerr << "subpath " << ip << " " << subpath[ip] << std::endl;
802
803 node* np_ = addnode( np, subpath[ip] );
804
805 np = np_;
806
807 }
808 }
809 }
810
811 }
812 }
813
814
815 if ( found_dir ) {
816 node* np_ = addnode( np, fulldir, td );
817 np = np_;
818 }
819
820 if ( found_dir && verbose ) std::cerr << s << cwd << std::endl;
821
822
823 TList* tl = gDirectory->GetListOfKeys();
824
825 // struct timeval tv = simpletimer_start();
826
827 // (*outp) << "\ttl " << tl << std::endl;
828
830
831 for ( int i=0 ; i<tl->GetSize() ; i++ ) {
832
833 TKey* tobj = (TKey*)tl->At(i);
834
835 if ( tobj==0 ) continue;
836
837 // (*outp) << "tobj " << tobj;
838 // if ( tobj ) (*outp) << " : \t" << tobj->GetName();
839 // (*outp) << std::endl;
840
841 if ( std::string(tobj->GetClassName()).find("TDirectory")!=std::string::npos ) {
842 // (*outp) << ns << "Directory " << tobj->GetName() << std::endl;
843
844 TDirectory* tnd = (TDirectory*)tobj->ReadObj();
845
846
848 if ( tnd ) search( tnd, s+spacer, cwd, np );
849
850 }
851 else {
852
854
855 if ( found_dir ) {
856 if ( std::string(tobj->GetClassName()).find("TH1")!=std::string::npos ||
857 std::string(tobj->GetClassName()).find("TH2")!=std::string::npos ||
858 std::string(tobj->GetClassName()).find("TProfile")!=std::string::npos ) {
859
860 // TH1* th = (TH1*)tobj->ReadObj();
861
862 // node* h = addnode( np, "", get<TObject>(tobj), node::HISTOGRAM );
863 // node* h =
864 addnode( np, tobj->GetName(), get<TObject>(tobj), node::HISTOGRAM );
865
866
868 std::string subdir = cwd;
869 chop( subdir, currentfile+"/" );
870
872 savedhistos.push_back( subdir+"/"+tobj->GetName() );
873
875 if ( std::string(tobj->GetName())=="Chain" ) {
876 double N = static_cast<TH1*>(get<TObject>(tobj))->GetEntries();
877
878 // std::cout << "entries " << np->name() << " " << " " << np->parent()->name() << " " << N << std::endl;
879 // std::cout << "\tentries " << np->parent()->name() << "/" << np->name() << "\t" << N << std::endl;
880 std::cout << "\t" << subdir << "\t" << N << std::endl;
881
882 node* p = np->parent();
883 if ( p && p->name()!="Shifter" ) {
884
885 p->addrate( p->name(), N );
886
887 node* p2 = p->parent();
888 if ( p2 ) p2->addrate( p->name(), N );
889 }
890 }
891
892 }
893
894 // if ( !status ) std::cerr << "bad status" << std::endl;
895 }
896 }
897
898 }
899
900
901 // double _t = simpletimer_stop(tv);
902
903 // double global_time = simpletimer_stop(global_timer);
904
905 ir--;
906
907 here->cd();
908
909 if ( first_found ) found_dir = false;
910
911}
912
913
914
915
916
917
918
919
920
921
922int cost( std::vector<std::string>& files, node& n, const std::string& directory="", bool deleteref=false, bool relocate=false ) {
923
924 std::cerr << "processing ..." << std::endl;
925
926 // std::cerr << "opening root files" << std::endl;
927
928 fptr.resize(files.size());
929
930
931 for ( unsigned i=0 ; i<files.size() ; i++ ) {
932
933 currentfile = files[i];
934
935 std::cerr << "opening " << currentfile << std::endl;
936
937 if ( !contains( files[i], "root://eosatlas") && !file_exists( files[i] ) ){
938 std::cerr << "file " << files[i] << " does not exist" << std::endl;
939 return -1;
940 }
941
943 fptr[i] = TFile::Open( files[i].c_str() );
944
945 if ( fptr[i]==0 || fptr[i]->IsZombie() ) {
946 std::cerr << "file " << files[i] << " cannot be opened" << std::endl;
947 return -1;
948 }
949
950 fptr[i]->cd();
951
952 if ( directory!="" ) fptr[i]->cd(directory.c_str());
953
954 TDirectory* here = gDirectory;
955
956 // global_timer = simpletimer_start();
957
958 // int tcount = 0;
959
962
963 search( gDirectory, "", "", &n );
964
965 here->cd();
966
967
971 if ( deleteref || relocate ) {
972
973 std::cerr << "remapping" << std::endl;
974
975 if ( relocate ) mapped = maphist( savedhistos );
976
977 if ( relocate && !deleteref ) std::cerr << "saving histograms to file .newhist.root ... " << std::endl;
978
979 if ( outref=="" ) outref = ".newhist.root";
980 TFile* fnew = new TFile( outref.c_str(), "recreate" );
981 fnew->cd();
982
983 TDirectory* base = gDirectory;
984
985 if ( mapped.size() != savedhistos.size() ) mapped = savedhistos;
986
987 for ( unsigned ih=0 ; ih<savedhistos.size() ; ih++ ) {
988
989 std::vector<std::string> dirs = split( mapped[ih], "/" );
990
991 for ( unsigned jh=0 ; jh<dirs.size()-1 ; jh++ ) {
993 TDirectory* renedir = gDirectory->GetDirectory( dirs[jh].c_str() );
994 if ( renedir==0 ) gDirectory->mkdir( dirs[jh].c_str() );
995 gDirectory->cd( dirs[jh].c_str() );
996 }
997
998 TH1* href = (TH1*)fptr[i]->Get( savedhistos[ih].c_str() );
999 if ( href ) {
1000 // std::cerr << ih << " " << savedhistos[ih] << " 0x" << href << std::endl;
1001 href->Write( dirs.back().c_str() );
1002 }
1003
1004 base->cd();
1005 }
1006
1007
1008 }
1009
1010 std::cerr << "closing files" << std::endl;
1011
1012 fptr[i]->Close();
1013
1014 delete fptr[i];
1015
1016 if ( deleteref ) {
1017 if ( outref==".newhist.root" ) {
1018 std::cerr << "replacing histogram file" << std::endl;
1019 std::string cmd = std::string("mv ") + files[i] + " " + files[i] + ".bak";
1020 std::system( cmd.c_str() );
1021 cmd = std::string("mv .newhist.root ") + files[i];
1022 std::system( cmd.c_str() );
1023 }
1024 }
1025
1026 }
1027
1028 return 0;
1029}
1030
1031
1032
1033
1034
1035int usage(std::ostream& s, int , char** argv, int status=-1) {
1036 s << "Usage: " << argv[0] << " [OPTIONS] input1.root ... inputN.root\n\n";
1037 s << " -o FILENAME \tname of output (filename required)\n";
1038 s << " -b, --base DIR \tuse directory DIR as the base for the han config\n";
1039 s << " -d, --dir DIR \tonly directories below DIR where DIR is a structure such as HLT/TRIDT etc\n";
1040 s << " -x, DIR \texclude directory DIR\n";
1041 s << " -r SRC DST \tremap directory SRC to directory DST\n";
1042 s << " -ds, --desc DESCRIP \tuse DESCRIP as the description\n";
1043 s << " -t, --tag VALUE \tadd the VALUE to the list of command per histogram\n";
1044 s << " -a, --algorithm VALUE \tuse VALUE as the execution algorithm for each histogram\n";
1045 s << " -wc, --wildcard \tprint use hist * rather than a separate entry for each histogram\n";
1046 s << " -dr, --deleteref \tdelete unselected histograms\n";
1047 s << " -or, --outref FILENAME \tdelete file to write reduced output to (overwrites input otherwise) \n";
1048 s << " -rh, --relocate \trelocate selected histograms\n";
1049 s << " -ref, --reference TAG FILE \tadd FILE as a reference file with tag TAG\n";
1050 s << " -rc, --refconf FILE \tadd FILE to the config as a reference block\n";
1051 s << " -v, --verbose \tprint verbose output\n";
1052 s << " -h, --help \tdisplay this help\n";
1053 s << std::endl;
1054 return status;
1055}
1056
1057
1058std::vector<std::string> refblock;
1059
1060void referenceblock( const std::string& file ) {
1061 std::ifstream strm(file.c_str());
1062 for ( std::string line ; getline( strm, line ); ) refblock.push_back( line );
1063}
1064
1065
1066int main(int argc, char** argv) {
1067
1068 // std::string cock = "HLT_j150_bperf_split/InDetTrigTrackingxAODCnv_BjetPrmVtx_FTF_SuperRoi/Chain";
1069
1070 // replace
1071
1072 // std::cout << replace
1073
1074 gStyle->SetPadRightMargin(0.05);
1075 gStyle->SetPadTopMargin(0.075);
1076
1077 // TCanvas* tg = new TCanvas("tg", "tg", 650, 900 );
1078 TCanvas* tg = new TCanvas("tg", "tg", 700, 600 );
1079 tg->cd();
1080
1081 gStyle->SetStatY(0.4);
1082 // Set y-position (fraction of pad size)
1083 gStyle->SetStatX(0.89);
1084 // Set x-position (fraction of pad size)
1085 gStyle->SetStatW(0.25);
1086 // Set width of stat-box (fraction of pad size)
1087 gStyle->SetStatH(0.16);
1088
1089
1090 // if ( argc<3 ) usage( std::cerr << "not enough command options", argc, argv );
1091 if ( argc<2 ) return usage( std::cerr, argc, argv );
1092
1093
1094 for ( int i=1 ; i<argc ; i++ ) {
1095 if ( std::string(argv[i])=="-h" || std::string(argv[i])=="--help" ) return usage( *outp, argc, argv, 0 );
1096 }
1097
1098 std::string dir = "";
1099
1100 std::vector<std::string> subdirs;
1101
1102
1103 bool deleteref = false;
1104 bool relocate = false;
1105
1106 std::string outfile = "";
1107
1108 int offset = 1;
1109
1110
1111 for ( int i=1 ; i<argc ; i++ ) {
1112 if ( std::string(argv[i])=="-v" || std::string(argv[i])=="--verbose" ) verbose = true;
1113 else if ( std::string(argv[i])=="-o" ) {
1114 ++i;
1115 if ( i<argc-offset ) outfile = argv[i];
1116 else return usage( std::cerr, argc, argv );
1117 }
1118 else if ( std::string(argv[i])=="-or" || std::string(argv[i])=="--outrefr" ) {
1119 ++i;
1120 if ( i<argc-offset ) outref = argv[i];
1121 else return usage( std::cerr, argc, argv );
1122 }
1123 else if ( std::string(argv[i])=="-ref" || std::string(argv[i])=="--reference" ) {
1124 std::string reftag;
1125 std::string reffile;
1126 ++i;
1127 if ( i<argc-offset ) reftag = argv[i];
1128 else return usage( std::cerr, argc, argv );
1129 ++i;
1130 if ( i<argc-offset ) reffile = argv[i];
1131 else return usage( std::cerr, argc, argv );
1132 references.push_back( reference( reftag, reffile ) );
1133 // std::cerr << references.back() << std::endl;
1134 }
1135 else if ( std::string(argv[i])=="-rc" || std::string(argv[i])=="-refconf" ) {
1136 ++i;
1137 if ( i<argc-offset ) referenceblock( argv[i] );
1138 else return usage( std::cerr, argc, argv );
1139 }
1140 else if ( std::string(argv[i])=="-dr" || std::string(argv[i])=="--deleteref" ) deleteref = true;
1141 else if ( std::string(argv[i])=="-rh" || std::string(argv[i])=="--relocate" ) relocate = true;
1142 else if ( std::string(argv[i])=="-wc" || std::string(argv[i])=="--wildcard" ) allhists = false;
1143 else if ( std::string(argv[i])=="-d" || std::string(argv[i])=="--dir" ) {
1144 ++i;
1145
1146 if ( i<argc-offset ) {
1147 std::string stringdir(argv[i]);
1148 dirs.insert( std::map<std::string,int>::value_type( stringdir, std::count( stringdir.begin(), stringdir.end(), '/' ) ) );
1149
1150 std::string tdir = argv[i];
1151
1152 // std::cerr << "dirs " << argv[i] << std::endl;
1153
1154 do {
1155 subdirs.push_back( chop( tdir, "/" ) );
1156 // std::cerr << "chop " << subdirs.back() << std::endl;
1157 }
1158 while ( tdir.size() );
1159 }
1160 else return usage( std::cerr, argc, argv );
1161 }
1162 else if ( std::string(argv[i])=="-x" ) {
1163 ++i;
1164 if ( i<argc-offset ) exclude.insert( argv[i] );
1165 else return usage( std::cerr, argc, argv );
1166 }
1167 else if ( std::string(argv[i])=="-ds" || std::string(argv[i]).find("--desc")==0 ) {
1168 ++i;
1169 if ( i<argc-offset ) description = argv[i];
1170 else return usage( std::cerr, argc, argv );
1171 }
1172 else if ( std::string(argv[i])=="-b" || std::string(argv[i])=="--base" ) {
1173 ++i;
1174 if ( i<argc-offset ) base = argv[i] ;
1175 else return usage( std::cerr, argc, argv );
1176 }
1177 else if ( std::string(argv[i])=="-a" || std::string(argv[i])=="--algorithm" ) {
1178 ++i;
1179 if ( i<argc-offset ) algorithm = argv[i] ;
1180 else return usage( std::cerr, argc, argv );
1181 }
1182 // else if ( std::string(argv[i])=="-o" ) {
1183 // ++i;
1184 // if ( i<argc ) output_file = argv[i];
1185 // else return usage( std::cerr, argc, argv );
1186 // }
1187 else if ( std::string(argv[i])=="-t" || std::string(argv[i])=="--tag" ) {
1188 ++i;
1189 if ( i<argc-offset ) tags.push_back( argv[i] );
1190 else return usage( std::cerr, argc, argv );
1191 }
1192 else if ( std::string(argv[i])=="-r" ) {
1193 std::string src;
1194 std::string dest;
1195 if ( i<argc+2-offset ) {
1196 src = argv[++i];
1197 dest = argv[++i];
1198 remap.insert( std::map<std::string,std::string>::value_type( src, dest ) );
1199 } else return usage( std::cerr, argc, argv );
1200 }
1201 else {
1202 offset = 0;
1203 files.push_back(argv[i]);
1204 }
1205 }
1206
1207
1208 if ( base == "" ) base = std::move(dir);
1209
1210
1212
1213 if ( files.size()<1 ) return usage( std::cerr, argc, argv );
1214
1215
1216 // time the actual running of the cost() routine
1217
1218 if ( verbose ) std::cerr << "timing" << std::endl;
1219
1220 struct timeval tv = simpletimer_start();
1221
1223
1224 node n(0, "" );
1225 n.name( "top_level" );
1226
1227 int status = cost( files, n, "", deleteref, relocate );
1228
1229 // std::cerr << "\n\nnodes " << n << std::endl;
1230
1231 if ( status ) return status;
1232
1233 if ( outfile!="" ) outp = new std::ofstream(outfile.c_str());
1234
1235 header h;
1236
1237 for ( unsigned ir=0 ; ir<references.size() ; ir++ ) (*outp) << references[ir] << std::endl;
1238
1239 if ( refblock.size() ) (*outp) << refblock << std::endl;
1240
1242 menu m( n );
1243
1245 ass( n, allhists );
1246
1248 (*outp) << std::endl;
1249
1250 double t = simpletimer_stop(tv);
1251
1252 if ( t>1000 ) std::cerr << "total time " << t*0.001 << " s" << std::endl;
1253 else std::cerr << "total time " << t << " ms" << std::endl;
1254
1255 return 0;
1256}
struct timeval simpletimer_start(void)
double simpletimer_stop(const struct timeval &start_time)
static Double_t sp
static Double_t a
void print(char *figname, TCanvas *c1)
TGraphErrors * GetEntries(TH2F *histo)
node * addnode(node *np, const std::string &name, TObject *td, node::TYPE t)
add a submode with a specific name, or return the existing node if one already exists
Definition addnode.cxx:28
Define macros for attributes used to control the static checker.
#define ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Header file for AthHistogramAlgorithm.
make the histogram assessment part of the config
Definition hcg.cxx:627
bool m_allhists
Definition hcg.cxx:723
void makeass(node &n, const std::string &space="", std::string path="", std::string rawpath="", bool found=false)
Definition hcg.cxx:640
ass(node &n, bool ah=true)
Definition hcg.cxx:631
header()
Definition hcg.cxx:531
make the sidebar many part of the config
Definition hcg.cxx:552
menu(node &n)
Definition hcg.cxx:556
void makemenu(node &n, const std::string &space="", std::string path="", std::string rawpath="", bool found=false)
Definition hcg.cxx:568
Definition node.h:24
@ DIRECTORY
Definition node.h:28
@ HISTOGRAM
Definition node.h:28
const std::string & name() const
Definition hcg.cxx:489
int m_run
Definition hcg.cxx:499
std::string m_name
Definition hcg.cxx:496
const std::string & file() const
Definition hcg.cxx:490
int run() const
Definition hcg.cxx:492
reference(const reference &r)
Definition hcg.cxx:486
std::string m_file
Definition hcg.cxx:497
reference(const std::string &n, const std::string &f)
Definition hcg.cxx:442
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
int ir
counter of the current depth
Definition fastadd.cxx:49
StatusCode usage()
int r
Definition globals.cxx:22
bool matchcwd(const std::string &s)
Definition hcg.cxx:403
std::vector< TFile * > fptr
Definition hcg.cxx:51
bool allhists
Definition hcg.cxx:79
bool matchdir(const std::string &s)
see whether this directory matches any of the directories we are explicitly being asked to match
Definition hcg.cxx:390
bool found_dir
Definition hcg.cxx:732
std::string outref
Definition hcg.cxx:83
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:91
std::ostream & operator<<(std::ostream &s, const std::vector< T > &v)
Definition hcg.cxx:108
std::vector< std::string > maphist(const std::vector< std::string > &v)
Definition hcg.cxx:333
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
std::string chopex(std::string &s1, const std::string &s2)
Definition hcg.cxx:197
std::vector< std::string > savedhistos
Definition hcg.cxx:53
std::string date()
sadly, includes a return at the end
Definition hcg.cxx:58
void depunctuate(std::string &s, const std::string &regex=":")
Definition hcg.cxx:323
std::vector< std::string > tags
Definition hcg.cxx:105
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310
std::vector< std::string > mapped
Definition hcg.cxx:54
std::map< std::string, int >::const_iterator matchcwditr(const std::string &s)
Definition hcg.cxx:426
void referenceblock(const std::string &file)
Definition hcg.cxx:1060
std::string choptoken(std::string &s1, const std::string &s2)
Definition hcg.cxx:233
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
void search(TDirectory *td, const std::string &s, std::string cwd, node *n)
recursive directory search for TH1 and TH2 and TProfiles
Definition hcg.cxx:739
std::vector< reference > references
Definition hcg.cxx:523
std::ostream * outp
send output to here ...
Definition hcg.cxx:76
int cost(std::vector< std::string > &files, node &n, const std::string &directory="", bool deleteref=false, bool relocate=false)
Definition hcg.cxx:922
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
bool file_exists(const std::string &file)
Definition hcg.cxx:68
std::string chopends(std::string &s1, const std::string &s2)
Definition hcg.cxx:290
std::string chomp(std::string &s1, const std::string &s2)
Definition hcg.cxx:214
void removespace(std::string &s, const std::string &s2)
Definition hcg.cxx:300
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
std::set< std::string > exclude
list of directories to be excluded
Definition hcg.cxx:98
std::string base
Definition hcg.cxx:81
std::string algorithm
Definition hcg.cxx:85
bool verbose
Definition hcg.cxx:73
std::string chopfirst(std::string &s1, const std::string &s2)
Definition hcg.cxx:260
std::map< std::string, std::string > remap
list of directories to be explicitly remapped
Definition hcg.cxx:95
std::string choplast(std::string &s1, const std::string &s2)
Definition hcg.cxx:276
std::map< std::string, int > dirs
list of directories to be explicitly included, together with corresponding depths of subdirectories
Definition hcg.cxx:102
std::string matchcwdstr(const std::string &s)
Definition hcg.cxx:415
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
std::string currentfile
Definition hcg.cxx:734
std::vector< std::string > refblock
Definition hcg.cxx:1058
std::string chomptoken(std::string &s1, const std::string &s2)
Definition hcg.cxx:247
std::string chop(std::string &s1, const std::string &s2)
Definition hcg.cxx:161
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357
int main()
Definition hello.cxx:18
std::string cwd
Definition listroot.cxx:38
const std::string spacer
Definition spacer.h:27
TFile * file