Loading [MathJax]/jax/output/SVG/config.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Classes | Functions | Variables
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 <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, const std::string &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 139 of file LArQuickHistMerge.cxx.

139  {
140  TH1* a1=(dynamic_cast<TH1*>(a));
141  const TH1* b1=dynamic_cast<const TH1*>(b);
142  if (!b1 || !a1)
143  std::cout << "ERROR in weightedAverageTH1: Object not of type TH1" << std::endl;
144  else
146  return;
147 }

◆ defaultMerge()

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

Definition at line 134 of file LArQuickHistMerge.cxx.

134  {
135  ((HIST*)a)->Add((HIST*)b);
136  return;
137 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 466 of file LArQuickHistMerge.cxx.

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

◆ openWithRetry()

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

Definition at line 27 of file LArQuickHistMerge.cxx.

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

◆ readFromTextFile()

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

Definition at line 441 of file LArQuickHistMerge.cxx.

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

◆ splitString()

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

Definition at line 428 of file LArQuickHistMerge.cxx.

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

Variable Documentation

◆ MAXSKIPPEDFILES

const int MAXSKIPPEDFILES = 10

Definition at line 23 of file LArQuickHistMerge.cxx.

◆ MAXSKIPPEDFILESFRACTION

const float MAXSKIPPEDFILESFRACTION = 0.1

Definition at line 24 of file LArQuickHistMerge.cxx.

histCollection
Definition: LArQuickHistMerge.cxx:50
splitString
std::vector< std::string > splitString(const std::string &in, const std::string &delim)
Definition: LArQuickHistMerge.cxx:428
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
openWithRetry
TFile * openWithRetry(const char *path, const unsigned nTrys=3)
Definition: LArQuickHistMerge.cxx:27
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
collListGuids.line
string line
Definition: collListGuids.py:77
skel.it
it
Definition: skel.GENtoEVGEN.py:407
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
MAXSKIPPEDFILESFRACTION
const float MAXSKIPPEDFILESFRACTION
Definition: LArQuickHistMerge.cxx:24
PrintTrkAnaSummary.dirName
dirName
Definition: PrintTrkAnaSummary.py:126
CombineRootFiles.inFileNames
inFileNames
Definition: CombineRootFiles.py:22
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:141
MAXSKIPPEDFILES
const int MAXSKIPPEDFILES
Definition: LArQuickHistMerge.cxx:23
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:301
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
dumpNswErrorDb.outFileName
string outFileName
Definition: dumpNswErrorDb.py:131
a
TList * a
Definition: liststreamerinfos.cxx:10
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
readFromTextFile
bool readFromTextFile(const char *filename, std::vector< std::string > &out)
Definition: LArQuickHistMerge.cxx:441
CscCalibQuery.nFiles
int nFiles
Definition: CscCalibQuery.py:332
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37