ATLAS Offline Software
Loading...
Searching...
No Matches
LArQuickHistMerge.cxx File Reference
#include "TSystem.h"
#include "TH1.h"
#include "TH2.h"
#include "TFile.h"
#include "TKey.h"
#include "TTree.h"
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <fstream>
#include "CxxUtils/checker_macros.h"
#include "DataQualityUtils/MonitoringFile.h"

Go to the source code of this file.

Classes

class  histCollection
class  histCollection::histPerDir_t
struct  histCollection::histDir_t

Functions

TFile * openWithRetry (const char *path, const unsigned nTrys=3)
template<class HIST>
void defaultMerge (TObject *a, const TObject *b)
void weightedAverageTH1 ATLAS_NOT_THREAD_SAFE (TObject *a, const TObject *b)
std::vector< std::string > splitString (const std::string &in, std::string_view delim)
bool readFromTextFile (const char *filename, std::vector< std::string > &out)
int main (int argc, char **argv)

Variables

const int MAXSKIPPEDFILES = 10
const float MAXSKIPPEDFILESFRACTION = 0.1

Function Documentation

◆ ATLAS_NOT_THREAD_SAFE()

void identical ATLAS_NOT_THREAD_SAFE ( TObject * a,
const TObject * b )

Definition at line 140 of file LArQuickHistMerge.cxx.

140 {
141 TH1* a1=(dynamic_cast<TH1*>(a));
142 const TH1* b1=dynamic_cast<const TH1*>(b);
143 if (!b1 || !a1)
144 std::cout << "ERROR in weightedAverageTH1: Object not of type TH1" << std::endl;
145 else
147 return;
148}
static Double_t a
static void merge_weightedAverage(TH1 &a, const TH1 &b)

◆ defaultMerge()

template<class HIST>
void defaultMerge ( TObject * a,
const TObject * b )

Definition at line 135 of file LArQuickHistMerge.cxx.

135 {
136 static_cast<HIST*>(a)->Add(static_cast<const HIST*>(b));
137 return;
138}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 467 of file LArQuickHistMerge.cxx.

467 {
468
469 if (argc<2 || (argc>1 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help")))) {
470 std::cout << "Syntax:\n" << argv[0] << " [-v ] [-i inputTextFile] [-d directroy] <outfile> <infile1> <infile2> .... " << std::endl;
471 std::cout << " -v verbose " << std::endl;
472 return -1;
473 }
474
475 bool debug=false;
476 std::string outFileName;
477 std::vector<std::string> inFileNames;
478 std::vector<std::string> baseDirs;
479
480 for(int iArg=1;iArg<argc;++iArg) {
481 if (!strcmp(argv[iArg],"-v")) {
482 debug=true;
483 continue;
484 }
485
486 if (!strcmp(argv[iArg],"-d")) {
487 ++iArg;
488 if (argc<=iArg) {
489 std::cout << "ERROR: Expected a parameter (directory name) after '-d'" << std::endl;
490 return -1;
491 }
492 baseDirs.push_back(std::string(argv[iArg]));
493 continue;
494 }
495
496
497 if (!strcmp(argv[iArg],"-i")) {
498 ++iArg;
499 if (argc<=iArg) {
500 std::cout << "ERROR: Expected text file name after '-i'" << std::endl;
501 return -1;
502 }
503 readFromTextFile(argv[iArg],inFileNames);
504 continue;
505 }
506 if (outFileName.empty())
507 outFileName=argv[iArg];
508 else
509 inFileNames.push_back(std::string(argv[iArg]));
510 }//end loop over arguments
511
512
513 if (outFileName.empty()) {
514 std::cout << "ERROR: No output file name given!" << std::endl;
515 return -1;
516 }
517
518 if (!gSystem->AccessPathName(outFileName.c_str())) {
519 std::cout << "ERROR: Output file " << outFileName << " exists already!" << std::endl;
520 return -1;
521 }
522
523 if (inFileNames.empty()) {
524 std::cout << "WARNING: No input files given! Will produce empty output file" << std::endl;
525 }
526
527
528 std::cout << "Number of input files: " << inFileNames.size() << std::endl;
529 std::cout << "Output file: " << outFileName << std::endl;
530
531 if (baseDirs.empty()) {
532 baseDirs.push_back("LAr");
533 baseDirs.push_back("CaloMonitoring/LArCellMon_NoTrigSel");
534 baseDirs.push_back("CaloTopoClusters/CalBAR");
535 baseDirs.push_back("CaloTopoClusters/CalECA");
536 baseDirs.push_back("CaloTopoClusters/CalECC");
537 baseDirs.push_back("CaloTopoClusters/CalEMBAR");
538 baseDirs.push_back("CaloTopoClusters/CalEMECA");
539 baseDirs.push_back("CaloTopoClusters/CalEMECC");
540
541 }
542 else {
543 //clean superfluous slash characters
544 for (size_t i=0;i<baseDirs.size();++i) {
545 const std::vector<std::string> & dirtok=splitString(baseDirs[i],"/");
546 if (dirtok.empty()) {
547 baseDirs[i].clear();
548 continue;
549 }
550 baseDirs[i]=dirtok[0];
551 for (size_t j=1;j<dirtok.size();++j) {
552 baseDirs[i]+="/"+dirtok[j];
553 }//eld loop over dir tokens
554 }//end loop over directories
555 }
556
557 // Keeping track of which directories we found
558 std::vector<std::string> foundDirs;
559
560 histCollection listOfHists(debug);
561 listOfHists.addExclusion("/CaloMonitoring/LArCellMon_NoTrigSel/Sporadic");
562
563 TFile* in1=nullptr;
564 std::string runDir("/");
565 size_t fileIndex=0;
566 unsigned nSkippedFiles=0;
567
568 const size_t nFiles=inFileNames.size();
569
570 //Loop to find the first non-empty file
571 //for (;fileIndex<nFiles && runDir.size()==1;++fileIndex) {
572 for (;fileIndex<nFiles && foundDirs.size()!=baseDirs.size() ;++fileIndex) {
573 //in1=TFile::Open(inFileNames[fileIndex].c_str(),"READ");
574 in1=openWithRetry(inFileNames[fileIndex].c_str());
575 if (!in1) {
576 std::cout << "ERROR: Could not open first file " << inFileNames[fileIndex] << "after all attempts" << std::endl;
577 if (nSkippedFiles>MAXSKIPPEDFILES) {
578 std::cout << "Failed to read " << nSkippedFiles << " input files. Abort job ..." << std::endl;
579 return -1;
580 }
581 if (nSkippedFiles>nFiles*MAXSKIPPEDFILESFRACTION) {
582 std::cout << "Failed to read " << 100.0*nSkippedFiles/nFiles << "% of input files. Abort job ..." << std::endl;
583 return -1;
584 }
585 ++nSkippedFiles;
586 std::cout << "Continue without this file. Skipped " << nSkippedFiles << " input files so far." << std::endl;
587 continue;
588 }
589
590 std::cout << "First loop: Working on file " << inFileNames[fileIndex] <<std::endl;
591 //Get run_XXXX directory name
592 TIter next(in1->GetListOfKeys() );
593 TKey* key;
594 while((key=(TKey*)next())) {
595 const char* name=key->GetName();
596 if (!strncmp(name,"run_",4)) {
597 if (runDir.size()>1) {
598 std::cout << "ERROR More than one run_XXX directory found! Ignoring " << name << std::endl;
599 }
600 else
601 runDir.append(name);
602 }
603 }
604
605 if (runDir.size()==1) {
606 std::cout << "WARNING: No run_XXXX directory found. Empty File? Ignored." << std::endl;
607 in1->Close();
608 continue;
609 }
610
611 //Collect histogram directories
612 std::vector<std::string>::const_iterator dIt=baseDirs.begin();
613 std::vector<std::string>::const_iterator dIt_e=baseDirs.end();
614
615 for (;dIt!=dIt_e;++dIt) {
616 std::string dirName=runDir+"/"+(*dIt);
617 // In case we are looping over more files to find the directory
618
619 TDirectory* dir=dynamic_cast<TDirectory*>(in1->Get(dirName.c_str()));
620 if (!dir) {
621 std::cout << "Did not find directory " << dirName <<" in file "<<inFileNames[fileIndex].c_str()<<" !" << std::endl;
622 continue;
623 }
624 if (std::find(foundDirs.begin(), foundDirs.end(), dirName) != foundDirs.end())
625 {
626 std::cout<<"Already found "<<dirName<<" before"<<std::endl;
627 //continue;
628 }else{
629 foundDirs.push_back(dirName);
630 }
631 if (debug) std::cout << "Found directory " << dirName <<" in file "<<inFileNames[fileIndex].c_str()<<" !" << std::endl;
632 listOfHists.addDirectory(dir,dirName);
633 }
634
635 std::cout << "Number of directories: " << listOfHists.size() << std::endl;
636 std::cout<<"SIZE "<< listOfHists.size() <<std::endl;
637
638 if (debug) listOfHists.print();
639
640 //std::cout << "Closing first file" << std::endl;
641 //in1->Close("R");
642 //delete in1;
643 }//end loop to find first non-empty file
644
645 for (;fileIndex<nFiles;++fileIndex) { // Loop over remaining files
646 const char* filename=inFileNames[fileIndex].c_str();
647 //TFile* in=TFile::Open(filename,"READ");
648 TFile* in=openWithRetry(filename);
649 if (!in) {
650 std::cout << "ERROR: Could not open file " << filename << "after all attempts" << std::endl;
651 if (nSkippedFiles>MAXSKIPPEDFILES) {
652 std::cout << "Failed to read " << nSkippedFiles << " input files. Abort job ..." << std::endl;
653 return -1;
654 }
655 if (nSkippedFiles>nFiles*MAXSKIPPEDFILESFRACTION) {
656 std::cout << "Failed to read " << 100.0*nSkippedFiles/nFiles << "% of input files. Abort job ..." << std::endl;
657 return -1;
658 }
659 ++nSkippedFiles;
660 std::cout << "Continue without this file. Skipped " << nSkippedFiles << " input files so far." << std::endl;
661 continue;
662 }
663 std::cout << "Working on file " << filename << std::endl;
664 TObject* dirObj=in->Get(runDir.c_str()+1);
665 if (!dirObj) {
666 std::cout << "Directory " << runDir << " not found. Ignoring apprently empty file" << std::endl;
667 }
668 else
669 listOfHists.addFile(in);
670 in->Close();
671 delete in;
672 }
673
674 TFile* out=new TFile(outFileName.c_str(),"NEW");
675 if (!out || !out->IsOpen()) {
676 std::cout << "ERROR: Failed to open output file " << outFileName << " for writing" << std::endl;
677 return -1;
678 }
679 listOfHists.write(out);
680 out->Close();
681 delete out;
682
683 if (in1 && in1->IsOpen()) {
684 in1->Close();
685 delete in1;
686 }
687
688 if (nSkippedFiles>0) {
689 std::cout << "WARNING: Skipped " << nSkippedFiles << " input file(s)." << std::endl;
690 }
691
692 return 0;
693}
const int MAXSKIPPEDFILES
std::vector< std::string > splitString(const std::string &in, std::string_view delim)
TFile * openWithRetry(const char *path, const unsigned nTrys=3)
bool readFromTextFile(const char *filename, std::vector< std::string > &out)
const float MAXSKIPPEDFILESFRACTION
const bool debug
float j(const xAOD::IParticle &, const xAOD::TrackMeasurementValidation &hit, const Eigen::Matrix3d &jab_inv)

◆ openWithRetry()

TFile * openWithRetry ( const char * path,
const unsigned nTrys = 3 )

Definition at line 28 of file LArQuickHistMerge.cxx.

28 {
29 TFile* f=nullptr;
30 unsigned iTry=0;
31 unsigned sleepSeconds=30;
32 while (!f && iTry<nTrys) {
33 f=TFile::Open(path,"READ");
34 if (!f || !(f->IsOpen())) {
35 ++iTry;
36 std::cout << "WARNING: Failed to open file " << path << " on " << iTry << ". attempt." << std::endl;
37 if (iTry<nTrys) {
38 std::cout << "Will re-try after " << sleepSeconds << " seconds..." << std::endl;
39 sleep(sleepSeconds);
40 f=nullptr;
41 sleepSeconds*=2;
42 }
43 else {//no more attempts
44 std::cout << "No more retrys" << std::endl;
45 }
46 }//end if file not open
47 }// end while
48 return f;
49}
path
python interpreter configuration --------------------------------------—
Definition athena.py:130

◆ readFromTextFile()

bool readFromTextFile ( const char * filename,
std::vector< std::string > & out )

Definition at line 442 of file LArQuickHistMerge.cxx.

442 {
443 std::ifstream ifs (filename, std::ifstream::in );
444 if (!ifs.good()) {
445 std::cout << "Failed to open file " << filename << " for reading." << std::endl;
446 return false;
447 }
448 while (ifs.good()) {
449 std::string line;
450 getline(ifs,line);
451 //Bit of cleaning and splitting:
452 const std::vector<std::string> linetok=splitString(line," ,;\t\n");
453 std::vector<std::string>::const_iterator it=linetok.begin();
454 std::vector<std::string>::const_iterator it_e=linetok.end();
455 for(;it!=it_e;++it) {
456 if ((*it)[0]=='#') break; //ingore all characters behind '#'
457 out.push_back(*it);
458 }
459 //if (!line.size()) continue;
460 //out.push_back(line);
461 }
462 ifs.close();
463 return true;
464}

◆ splitString()

std::vector< std::string > splitString ( const std::string & in,
std::string_view delim )

Definition at line 429 of file LArQuickHistMerge.cxx.

429 {
430 std::vector<std::string> retvec;
431 size_t pos1=0,pos2=0;
432 while (pos2!=std::string::npos) {
433 pos2=in.find_first_of(delim,pos1);
434 const std::string sub=in.substr(pos1,pos2-pos1);
435 if (!sub.empty())
436 retvec.push_back(std::move(sub));
437 pos1=pos2+1;
438 }
439 return retvec;
440}

Variable Documentation

◆ MAXSKIPPEDFILES

const int MAXSKIPPEDFILES = 10

Definition at line 24 of file LArQuickHistMerge.cxx.

◆ MAXSKIPPEDFILESFRACTION

const float MAXSKIPPEDFILESFRACTION = 0.1

Definition at line 25 of file LArQuickHistMerge.cxx.