ATLAS Offline Software
Classes | Macros | Functions | Variables
LArQuickHistMerge.cxx File Reference
#include "CxxUtils/checker_macros.h"
#include "TSystem.h"
#include "TH1.h"
#include "TH2.h"
#include "TFile.h"
#include "TKey.h"
#include "TTree.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "DataQualityUtils/MonitoringFile.h"

Go to the source code of this file.

Classes

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

Macros

#define MAXSKIPPEDFILES   10
 
#define MAXSKIPPEDFILESFRACTION   0.1
 

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)
 
void identical (TObject *a, const TObject *b)
 
std::vector< std::string > splitString (const std::string &in, const std::string &delim)
 
bool readFromTextFile (const char *filename, std::vector< std::string > &out)
 
int main (int argc, char **argv)
 

Variables

 ATLAS_NO_CHECK_FILE_THREAD_SAFETY
 

Macro Definition Documentation

◆ MAXSKIPPEDFILES

#define MAXSKIPPEDFILES   10

Definition at line 31 of file LArQuickHistMerge.cxx.

◆ MAXSKIPPEDFILESFRACTION

#define MAXSKIPPEDFILESFRACTION   0.1

Definition at line 32 of file LArQuickHistMerge.cxx.

Function Documentation

◆ ATLAS_NOT_THREAD_SAFE()

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

Definition at line 145 of file LArQuickHistMerge.cxx.

145  {
146  TH1* a1=(dynamic_cast<TH1*>(a));
147  const TH1* b1=dynamic_cast<const TH1*>(b);
148  if (!b1 || !a1)
149  std::cout << "ERROR in weightedAverageTH1: Object not of type TH1" << std::endl;
150  else
152  return;
153 }

◆ defaultMerge()

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

Definition at line 140 of file LArQuickHistMerge.cxx.

140  {
141  ((HIST*)a)->Add((HIST*)b);
142  return;
143 }

◆ identical()

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

Definition at line 215 of file LArQuickHistMerge.cxx.

215  {
216  TH1* a1=(dynamic_cast<TH1*>(a));
217  const TH1* b1=dynamic_cast<const TH1*>(b);
218  TH2* c1=(dynamic_cast<TH2*>(a));
219  const TH2* d1=dynamic_cast<const TH2*>(b);
220  if (a1 and b1){
222  } else if (c1 and d1){
224  } else {
225  std::cout << "ERROR in identical: Object not of type THist" << std::endl;
226  std::cout << a1 << " " << b1 << " " << c1 << " " <<d1 <<std::endl;
227  }
228  return;
229 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 472 of file LArQuickHistMerge.cxx.

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

◆ openWithRetry()

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

Definition at line 33 of file LArQuickHistMerge.cxx.

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

◆ readFromTextFile()

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

Definition at line 447 of file LArQuickHistMerge.cxx.

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

◆ splitString()

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

Definition at line 434 of file LArQuickHistMerge.cxx.

434  {
435  std::vector<std::string> retvec;
436  size_t pos1=0,pos2=0;
437  while (pos2!=std::string::npos) {
438  pos2=in.find_first_of(delim,pos1);
439  const std::string sub=in.substr(pos1,pos2-pos1);
440  if (!sub.empty())
441  retvec.push_back(sub);
442  pos1=pos2+1;
443  }
444  return retvec;
445 }

Variable Documentation

◆ ATLAS_NO_CHECK_FILE_THREAD_SAFETY

ATLAS_NO_CHECK_FILE_THREAD_SAFETY

Definition at line 7 of file LArQuickHistMerge.cxx.

histCollection
Definition: LArQuickHistMerge.cxx:56
checkFileSG.line
line
Definition: checkFileSG.py:75
splitString
std::vector< std::string > splitString(const std::string &in, const std::string &delim)
Definition: LArQuickHistMerge.cxx:434
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
openWithRetry
TFile * openWithRetry(const char *path, const unsigned nTrys=3)
Definition: LArQuickHistMerge.cxx:33
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
MAXSKIPPEDFILESFRACTION
#define MAXSKIPPEDFILESFRACTION
Definition: LArQuickHistMerge.cxx:31
extractSporadic.c1
c1
Definition: extractSporadic.py:134
skel.it
it
Definition: skel.GENtoEVGEN.py:396
MAXSKIPPEDFILES
#define MAXSKIPPEDFILES
Definition: LArQuickHistMerge.cxx:30
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
dq_defect_virtual_defect_validation.d1
d1
Definition: dq_defect_virtual_defect_validation.py:79
CombineRootFiles.inFileNames
inFileNames
Definition: CombineRootFiles.py:22
DumpGeoConfig.outFileName
string outFileName
Definition: DumpGeoConfig.py:252
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
hist_file_dump.f
f
Definition: hist_file_dump.py:135
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
beamspotman.dir
string dir
Definition: beamspotman.py:623
dqutils::MonitoringFile::merge_weightedAverage
static void merge_weightedAverage(TH1 &a, const TH1 &b)
Definition: MonitoringFile_MergeAlgs.cxx:290
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
a
TList * a
Definition: liststreamerinfos.cxx:10
dqutils::MonitoringFile::merge_identical
static void merge_identical(TH1 &a, const TH1 &b)
Definition: MonitoringFile_MergeAlgs.cxx:694
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
readFromTextFile
bool readFromTextFile(const char *filename, std::vector< std::string > &out)
Definition: LArQuickHistMerge.cxx:447
CscCalibQuery.nFiles
int nFiles
Definition: CscCalibQuery.py:332
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37