ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
InnerDetector
InDetValidation
InDetTrackPerfMon
util
postProcessIDTPMHistos.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2021-2025 CERN for the benefit of the ATLAS collaboration
3
*/
4
15
16
#include "
InDetPhysValMonitoring/ResolutionHelper.h
"
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 type ("res" or "pull") string and the substring
42
// after "Helper" in the 2D histo name, which specifies the x axis observableand resolution (y axis)
43
// Relies on the conventions within IDTPM
44
std::pair< std::string, std::string >
getTypeAndVars
(
const
TObject* resHelper ) {
45
const
std::string name{ resHelper->GetName() };
46
const
std::string keyWord{
"Helper"
};
47
const
size_t
pos = name.find( keyWord );
48
const
size_t
pos2 = pos + keyWord.size();
49
return
{ name.substr( 0, pos ), name.substr( pos2 ) };
50
}
51
52
// clone an existing histogram of a known name
53
TH1*
cloneExisting
(
const
std::string & name){
54
auto
*
h
= gDirectory->Get(name.c_str());
55
if
(!
h
){
56
std::cerr <<
"Could not find existing histogram "
<<name<<
" - will not postprocess "
<<std::endl;
57
return
nullptr
;
58
}
59
auto
*ret =
dynamic_cast<
TH1*
>
(
h
->Clone(name.c_str()));
60
if
(!ret){
61
std::cerr <<
"Found an existing object "
<<name<<
", but it is not a histogram ("
<<
h
->IsA()->GetName()<<
") - will not postprocess "
<<std::endl;
62
}
63
return
ret;
// will also catch ret == nullptr
64
}
65
66
// get the names of the 1D histograms following IDTPM conventions.
67
std::pair<std::string, std::string>
getPullAndResoNames
(
const
std::string &
type
){
68
if
(
type
==
"res"
){
69
return
{
"resolution"
,
"resmean"
};
70
}
71
else
if
(
type
==
"pull"
){
72
return
{
"pullwidth"
,
"pullmean"
};
73
}
74
else
{
75
std::cerr <<
" Not able to identify the histogram names for a resolution type "
<<
type
<<
" - supported are 'res' and 'pull'. "
<<std::endl;
76
}
77
return
{
""
,
""
};
78
}
79
80
int
postProcessHistos
(
81
TObject* resHelper,
82
IDPVM::ResolutionHelper
& theHelper,
83
IDPVM::ResolutionHelper::methods
& theMethod )
84
{
85
// here we have to rely on the naming conventions of IDTPM to identify what we are looking at
86
std::string
type
=
getTypeAndVars
( resHelper ).first;
87
std::string vars =
getTypeAndVars
( resHelper ).second;
88
89
// cast to TH2
90
TH2* resHelper2D =
dynamic_cast<
TH2*
>
(resHelper);
91
if
(!resHelper2D){
92
std::cerr <<
"Unable to reduce the histogram "
<<resHelper->GetName()<<
" to a TH2 - this histo can not yet be postprocessed! "
<<std::endl;
93
return
1;
94
}
95
const
auto
& oneDimNames =
getPullAndResoNames
(
type
);
96
// get the corresponding 1D histos by cloning the existing ones in the same folder
97
TH1* h_width =
cloneExisting
( oneDimNames.first + vars );
98
TH1* h_mean =
cloneExisting
( oneDimNames.second + vars );
99
// then call the resolution helper as done in "online" IDTPM
100
theHelper.
makeResolutions
( resHelper2D, h_width, h_mean, theMethod );
101
// update our 1D histos
102
h_width->Write();
103
h_mean->Write();
104
// and we are done
105
return
0;
106
}
107
108
// recursively parse a directory tree, post-processing any histos seen along the way
109
int
postProcessDir
(
110
TDirectory* dir,
111
IDPVM::ResolutionHelper
& theHelper,
112
IDPVM::ResolutionHelper::methods
& theMethod )
113
{
114
int
outcome = 0;
115
auto
theCWD = gDirectory;
116
// walk through all keys in this directory
117
dir->cd();
118
auto
*keys = dir->GetListOfKeys();
119
for
(
auto
*
const
key : *keys){
120
// Check if it's a directory and handle separately
121
TDirectory* theDir =
dynamic_cast<
TDirectory*
>
(dir->Get(key->GetName()));
122
if
(theDir){
123
outcome |=
postProcessDir
( theDir, theHelper, theMethod );
124
continue
;
// Do NOT delete directories, ROOT manages them
125
}
126
127
// If it's a histogram, wrap it in a unique_ptr
128
std::unique_ptr<TObject> gotIt(dir->Get(key->GetName()));
129
if
(
isResolutionHelper
(gotIt.get())){
130
outcome |=
postProcessHistos
( gotIt.get(), theHelper, theMethod );
131
}
132
}
133
theCWD->Purge();
// deleting old cycles
134
theCWD->cd();
135
return
outcome;
136
}
137
138
// function driving the postprocessing for this file
139
int
pproc_file
(
const
std::string& p_infile,
const
std::string& methodStr ) {
140
141
IDPVM::ResolutionHelper
theHelper;
142
IDPVM::ResolutionHelper::methods
theMethod;
143
144
std::unique_ptr<TFile> infile(TFile::Open(p_infile.c_str(),
"UPDATE"
));
145
if
(!infile || infile->IsZombie()) {
146
std::cerr <<
"could not open input file "
<<p_infile<<
" for updating "
<< std::endl;
147
return
1;
148
}
149
151
using
methodMap_t = std::unordered_map<
152
std::string,
IDPVM::ResolutionHelper::methods
>;
153
methodMap_t methodMap = {
154
{
"iterRMS"
,
IDPVM::ResolutionHelper::iterRMS_convergence
},
155
{
"gaussFit"
,
IDPVM::ResolutionHelper::Gauss_fit
},
156
{
"iterRMSgaussFit"
,
IDPVM::ResolutionHelper::fusion_iterRMS_Gaussfit
},
157
{
"iterGaussFit"
,
IDPVM::ResolutionHelper::iterGaussFit_convergence
}
158
};
159
160
methodMap_t::const_iterator mitr = methodMap.find( methodStr );
161
if
( mitr == methodMap.end() ) {
162
std::cerr <<
"Error: Method "
<< methodStr <<
163
" not found. Using iterRMS by default."
<< std::endl;
164
theMethod =
IDPVM::ResolutionHelper::iterRMS_convergence
;
165
}
else
{
166
theMethod = mitr->second;
167
}
168
169
int
res
=
postProcessDir
( infile.get(), theHelper, theMethod );
// recursively post-process the directory tree, starting from the root.
170
return
res
;
171
}
172
173
174
int
main
(
int
argc,
char
* argv[]) {
175
176
std::string infile{
""
};
177
std::string methodStr{
"iterRMS"
};
179
if
( argc >= 2 ) {
180
infile = argv[1];
181
if
( argc == 3 ) methodStr = argv[2];
182
}
183
else
{
184
std::cerr<<
" Usage: postProcessIDTPMHistos <File to post-process> <resolution method>"
<<std::endl;
185
std::cerr<<
" where the file is typically obtained by hadding"
<< std::endl;
186
std::cerr<<
" outputs of several independent IDTPM runs."
<< std::endl;
187
std::cerr<<
" The resolution method (optional) is set by default to iterRMS_convergence."
<< std::endl;
188
return
1;
189
}
191
if
(!
file_exists
(infile)) {
192
std::cerr <<
"Error: invalid input file: "
<< infile << std::endl;
193
return
1;
194
}
196
std::cout <<
" Post-processing file "
<< infile <<
"\n"
<< std::endl;
197
return
pproc_file
( infile, methodStr );
198
}
ResolutionHelper.h
res
std::pair< std::vector< unsigned int >, bool > res
Definition
JetGroupProductTest.cxx:11
h
Header file for AthHistogramAlgorithm.
IDPVM::ResolutionHelper
Definition
InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:28
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:382
IDPVM::ResolutionHelper::methods
methods
methods acc to which mean&RMS can be evaluated
Definition
InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:36
IDPVM::ResolutionHelper::Gauss_fit
@ Gauss_fit
Definition
InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:36
IDPVM::ResolutionHelper::iterGaussFit_convergence
@ iterGaussFit_convergence
Definition
InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:36
IDPVM::ResolutionHelper::fusion_iterRMS_Gaussfit
@ fusion_iterRMS_Gaussfit
Definition
InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:36
IDPVM::ResolutionHelper::iterRMS_convergence
@ iterRMS_convergence
Definition
InnerDetector/InDetValidation/InDetPhysValMonitoring/InDetPhysValMonitoring/ResolutionHelper.h:36
main
int main()
Definition
hello.cxx:18
std
STL namespace.
getTypeAndVars
std::pair< std::string, std::string > getTypeAndVars(const TObject *resHelper)
Definition
postProcessIDTPMHistos.cxx:44
postProcessHistos
int postProcessHistos(TObject *resHelper, IDPVM::ResolutionHelper &theHelper, IDPVM::ResolutionHelper::methods &theMethod)
Definition
postProcessIDTPMHistos.cxx:80
cloneExisting
TH1 * cloneExisting(const std::string &name)
Definition
postProcessIDTPMHistos.cxx:53
pproc_file
int pproc_file(const std::string &p_infile, const std::string &methodStr)
Definition
postProcessIDTPMHistos.cxx:139
isResolutionHelper
bool isResolutionHelper(TObject *entry)
Definition
postProcessIDTPMHistos.cxx:36
getPullAndResoNames
std::pair< std::string, std::string > getPullAndResoNames(const std::string &type)
Definition
postProcessIDTPMHistos.cxx:67
file_exists
bool file_exists(const string &p_name)
Definition
postProcessIDTPMHistos.cxx:31
postProcessDir
int postProcessDir(TDirectory *dir, IDPVM::ResolutionHelper &theHelper, IDPVM::ResolutionHelper::methods &theMethod)
Definition
postProcessIDTPMHistos.cxx:109
type
Generated on
for ATLAS Offline Software by
1.17.0