ATLAS Offline Software
postProcessIDPVMHistos.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
17 
18 #include "TFile.h"
19 #include "TSystem.h"
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TObject.h"
23 
24 #include <iostream>
25 #include <memory>
26 #include <string>
27 
28 using namespace std;
29 
30 
31 bool file_exists(const string & p_name) {
32  return !gSystem->AccessPathName(p_name.c_str(), kFileExists);
33 }
34 
35 // check if the name of an object matches what we expect from a resolution helper
36 bool isResolutionHelper(TObject* entry){
37  const std::string objName{entry->GetName()};
38  return ((objName.find("resHelper") == 0 || objName.find("pullHelper") == 0) && dynamic_cast<TH1*>(entry));
39 }
40 
41 // get the x axis observable and resolution (y axis) from the name of a 2D histo.
42 // Relies on the conventions within IDPVM
43 std::pair<std::string, std::string> getObservableAndReso(const TObject* resHelper){
44  const std::string name{resHelper->GetName()};
45  const std::string keyWord {"Helper_"};
46  const size_t offset = keyWord.size();
47  auto start = name.find(keyWord)+offset;
48  auto sep = name.find('_',start);
49  return {name.substr(start, sep-start), name.substr(sep+1)};
50 
51 }
52 
53 // get the resolution type (pull or res) - taken from the prefix of the 2D histo name
54 std::string getResoType(const TObject* resHelper){
55  std::string aux{resHelper->GetName()};
56  return aux.substr(0, aux.find("Helper"));
57 }
58 
59 // clone an existing histogram of a known name
60 TH1* cloneExisting(const std::string & name){
61  auto *h = gDirectory->Get(name.c_str());
62  if (!h){
63  std::cerr << "Could not find existing histogram "<<name<<" - will not postprocess "<<std::endl;
64  return nullptr;
65  }
66  auto *ret = dynamic_cast<TH1*>(h->Clone(name.c_str()));
67  if (!ret){
68  std::cerr << "Found an existing object "<<name<<", but it is not a histogram ("<<h->IsA()->GetName()<<") - will not postprocess "<<std::endl;
69  }
70  return ret; // will also catch ret == nullptr
71 }
72 
73 // get the names of the 1D histograms following IDPVM conventions.
74 std::pair<std::string, std::string> getPullAndResoNames(const std::string & type){
75  if (type == "res"){
76  return {"resolution","resmean"};
77  }
78  else if (type == "pull"){
79  return {"pullwidth","pullmean"};
80  }
81  else {
82  std::cerr << " Not able to identify the histogram names for a resolution type "<<type<<" - supported are 'res' and 'pull'. "<<std::endl;
83  }
84  return {"",""};
85 }
86 
87 int postProcessHistos(TObject* resHelper, IDPVM::ResolutionHelper & theHelper){
88  // here we have to rely on the naming conventions of IDPVM to identify what we are looking at
89  auto vars = getObservableAndReso(resHelper);
90  auto type = getResoType(resHelper);
91  // cast to TH2
92  TH2* resHelper2D = dynamic_cast<TH2*>(resHelper);
93  if (!resHelper2D){
94  std::cerr <<"Unable to reduce the histogram "<<resHelper->GetName()<<" to a TH2 - this histo can not yet be postprocessed! " <<std::endl;
95  return 1;
96  }
97  const auto & oneDimNames = getPullAndResoNames(type);
98  // get the corresponding 1D histos by cloning the existing ones in the same folder
99  TH1* h_width = cloneExisting(oneDimNames.first+"_vs_"+vars.first+"_"+vars.second);
100  TH1* h_mean = cloneExisting(oneDimNames.second+"_vs_"+vars.first+"_"+vars.second);
101  // then call the resolution helper as done in "online" IDPVM
102  theHelper.makeResolutions(resHelper2D, h_width, h_mean);
103  // update our 1D histos
104  h_width->Write();
105  h_mean->Write();
106  // and we are done
107  return 0;
108 }
109 
110 // recursively parse a directory tree, post-processing any histos seen along the way
111 int postProcessDir(TDirectory* dir, IDPVM::ResolutionHelper & theHelper){
112 
113  int outcome = 0;
114  auto theCWD = gDirectory;
115  // walk through all keys in this directory
116  dir->cd();
117  auto *keys = dir->GetListOfKeys();
118  for (auto *const key : *keys){
119  std::unique_ptr<TObject> gotIt(dir->Get(key->GetName()));
120 
121  // if we encounter a directory, descend into it and repeat the process
122  TDirectory* theDir = dynamic_cast<TDirectory*>(gotIt.get());
123  if (theDir){
124  outcome |= postProcessDir(theDir, theHelper);
125  }
126 
127  // if we encounter a histogram that could be a resolution input, post-process it
128  if (isResolutionHelper(gotIt.get())){
129  outcome |= postProcessHistos(gotIt.get(), theHelper);
130  }
131  }
132  theCWD->cd();
133  return outcome;
134 }
135 
136 // function driving the postprocessing for this file
137 int pproc_file(const std::string & p_infile) {
138 
139  IDPVM::ResolutionHelper theHelper;
140 
141  std::unique_ptr<TFile> infile(TFile::Open(p_infile.c_str(),"UPDATE"));
142  if (!infile || infile->IsZombie()) {
143  std::cerr << "could not open input file "<<p_infile<<" for updating "<< std::endl;
144  return 1;
145  }
146 
147  int res = postProcessDir(infile.get(), theHelper); // recursively post-process the directory tree, starting from the root.
148  return res;
149 }
150 
151 
152 int main(int argc, char* argv[]) {
153 
154  std::string infile{""};
156  if (argc == 2){
157  infile = argv[1];
158  }
163  else if (argc == 4 && std::string(argv[1]) == std::string{"-f"}){
165  gSystem->CopyFile(argv[3],argv[2],true);
167  infile = argv[2];
168  }
169  else {
170  std::cerr<<" Usage: postProcessIDPVMHistos <File to post-process>"<<std::endl;
171  std::cerr<< " where the file is typically obtained by hadding" << std::endl;
172  std::cerr<< " outputs of several independent IDPVM runs." << std::endl;
173  std::cerr<<" Alternative usage: postProcessIDPVMHistos -f <desired output file name> <File to post-process>"<<std::endl;
174  std::cerr<< " imitates a hadd-like signature for PhysVal merging." << std::endl;
175  return 1;
176  }
178  if (!file_exists(infile)) {
179  std::cerr << "Error: invalid input file: " << infile << std::endl;
180  return 1;
181  }
183  std::cout << " Post-processing file " << infile << "\n" << std::endl;
184  return pproc_file(infile);
185 }
isResolutionHelper
bool isResolutionHelper(TObject *entry)
Definition: postProcessIDPVMHistos.cxx:36
run.infile
string infile
Definition: run.py:13
IDPVM::ResolutionHelper::makeResolutions
void makeResolutions(const TH2 *h_input2D, TH1 *hwidth, TH1 *hmean, TH1 *hproj[], bool saveProjections, IDPVM::ResolutionHelper::methods theMethod=IDPVM::ResolutionHelper::iterRMS_convergence)
extract 1D resolution plots from a 2D "residual vs observable" histogram.
Definition: InnerDetector/InDetValidation/InDetPhysValMonitoring/src/ResolutionHelper.cxx:288
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
postProcessHistos
int postProcessHistos(TObject *resHelper, IDPVM::ResolutionHelper &theHelper)
Definition: postProcessIDPVMHistos.cxx:87
cloneExisting
TH1 * cloneExisting(const std::string &name)
Definition: postProcessIDPVMHistos.cxx:60
pproc_file
int pproc_file(const std::string &p_infile)
Definition: postProcessIDPVMHistos.cxx:137
IDPVM::ResolutionHelper
Definition: InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:28
ResolutionHelper.h
getPullAndResoNames
std::pair< std::string, std::string > getPullAndResoNames(const std::string &type)
Definition: postProcessIDPVMHistos.cxx:74
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
h
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
file_exists
bool file_exists(const string &p_name)
Definition: postProcessIDPVMHistos.cxx:31
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
main
int main(int argc, char *argv[])
Definition: postProcessIDPVMHistos.cxx:152
beamspotman.dir
string dir
Definition: beamspotman.py:623
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
grepfile.sep
sep
Definition: grepfile.py:38
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
getObservableAndReso
std::pair< std::string, std::string > getObservableAndReso(const TObject *resHelper)
Definition: postProcessIDPVMHistos.cxx:43
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
getResoType
std::string getResoType(const TObject *resHelper)
Definition: postProcessIDPVMHistos.cxx:54
postProcessDir
int postProcessDir(TDirectory *dir, IDPVM::ResolutionHelper &theHelper)
Definition: postProcessIDPVMHistos.cxx:111
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37