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