ATLAS Offline Software
Functions
postProcessIDPVMHistos.cxx File Reference
#include "InDetPhysValMonitoring/ResolutionHelper.h"
#include "TFile.h"
#include "TSystem.h"
#include "TH1.h"
#include "TH2.h"
#include "TObject.h"
#include <iostream>
#include <memory>
#include <string>
Include dependency graph for postProcessIDPVMHistos.cxx:

Go to the source code of this file.

Functions

bool file_exists (const string &p_name)
 
bool isResolutionHelper (TObject *entry)
 
std::pair< std::string, std::string > getObservableAndReso (const TObject *resHelper)
 
std::string getResoType (const TObject *resHelper)
 
TH1 * cloneExisting (const std::string &name)
 
std::pair< std::string, std::string > getPullAndResoNames (const std::string &type)
 
int postProcessHistos (TObject *resHelper, IDPVM::ResolutionHelper &theHelper)
 
int postProcessDir (TDirectory *dir, IDPVM::ResolutionHelper &theHelper)
 
int pproc_file (const std::string &p_infile)
 
int main (int argc, char *argv[])
 

Detailed Description


Author
: Max Goblirsch Based on code by Liza Mijovic, Soeren Prell

Goal: Update resolutions extracted from 2D histograms after merging several output files (typically after grid running)


Definition in file postProcessIDPVMHistos.cxx.

Function Documentation

◆ cloneExisting()

TH1* cloneExisting ( const std::string &  name)

Definition at line 60 of file postProcessIDPVMHistos.cxx.

60  {
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 }

◆ file_exists()

bool file_exists ( const string &  p_name)

Definition at line 31 of file postProcessIDPVMHistos.cxx.

31  {
32  return !gSystem->AccessPathName(p_name.c_str(), kFileExists);
33 }

◆ getObservableAndReso()

std::pair<std::string, std::string> getObservableAndReso ( const TObject *  resHelper)

Definition at line 43 of file postProcessIDPVMHistos.cxx.

43  {
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 }

◆ getPullAndResoNames()

std::pair<std::string, std::string> getPullAndResoNames ( const std::string &  type)

Definition at line 74 of file postProcessIDPVMHistos.cxx.

74  {
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 }

◆ getResoType()

std::string getResoType ( const TObject *  resHelper)

Definition at line 54 of file postProcessIDPVMHistos.cxx.

54  {
55  std::string aux{resHelper->GetName()};
56  return aux.substr(0, aux.find("Helper"));
57 }

◆ isResolutionHelper()

bool isResolutionHelper ( TObject *  entry)

Definition at line 36 of file postProcessIDPVMHistos.cxx.

36  {
37  const std::string objName{entry->GetName()};
38  return ((objName.find("resHelper") == 0 || objName.find("pullHelper") == 0) && dynamic_cast<TH1*>(entry));
39 }

◆ main()

int main ( int  argc,
char *  argv[] 
)

Standard usage. The user passes a file they wish to update.

hadd imitation. Ugly, but allows this to run in PhysVal merge transforms. We deliberately ignore the first arg ("-f") in the following. This is because the physval merge step requires passing a "-f" ...

copy the input file to the output file

and mark the output for postprocessing. Input stays invariant in this mode

check if the input exists

and post-process if it does

Definition at line 152 of file postProcessIDPVMHistos.cxx.

152  {
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 }

◆ postProcessDir()

int postProcessDir ( TDirectory *  dir,
IDPVM::ResolutionHelper theHelper 
)

Definition at line 111 of file postProcessIDPVMHistos.cxx.

111  {
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 }

◆ postProcessHistos()

int postProcessHistos ( TObject *  resHelper,
IDPVM::ResolutionHelper theHelper 
)

Definition at line 87 of file postProcessIDPVMHistos.cxx.

87  {
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 }

◆ pproc_file()

int pproc_file ( const std::string &  p_infile)

Definition at line 137 of file postProcessIDPVMHistos.cxx.

137  {
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 }
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
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
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