/
ShowerCalibrationGaloreFromPID.cxx
338 lines (279 loc) · 11.9 KB
/
ShowerCalibrationGaloreFromPID.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @file ShowerCalibrationGaloreFromPID.cxx
* @brief Shower energy calibration according to particle type
* @author Gianluca Petrillo (petrillo@fnal.gov)
* @date April 28, 2016
* @see ShowerCalibrationGaloreFromPID.h
* @ingroup ShowerCalibrationGalore
*
* Implementation file. Nothing needed here.
*/
/// LArSoft libraries
#include "larexamples/Services/ShowerCalibrationGalore/Providers/ShowerCalibrationGaloreFromPID.h"
/// framework and utility libraries
#include "cetlib/search_path.h"
#include "cetlib_except/exception.h"
// ROOT libraries
#include "TDirectory.h"
#include "TFile.h"
#include "TGraphErrors.h"
// C/C++ standard libraries
#include <algorithm> // std::lower_bound(), std::sort(), std::min(), std::max()
#include <memory> // std::make_unique()
#include <tuple> // std::tie()
#include <utility> // std::pair<>
//------------------------------------------------------------------------------
float lar::example::ShowerCalibrationGaloreFromPID::correctionFactor(
recob::Shower const& shower,
PDGID_t id /* = unknownID */) const
{
CalibrationInfo_t const& corr = selectCorrection(id);
double const E = shower.Energy().at(shower.best_plane());
return (float)corr.evalFactor(E);
} // lar::example::ShowerCalibrationGaloreFromPID::correctionFactor()
//------------------------------------------------------------------------------
lar::example::ShowerCalibrationGalore::Correction_t
lar::example::ShowerCalibrationGaloreFromPID::correction(recob::Shower const& shower,
PDGID_t id /* = unknownID */) const
{
CalibrationInfo_t const& corr = selectCorrection(id);
double const E = shower.Energy().at(shower.best_plane());
return {(float)corr.evalFactor(E), (float)corr.evalError(E)};
} // lar::example::ShowerCalibrationGaloreFromPID::correction()
//------------------------------------------------------------------------------
void lar::example::ShowerCalibrationGaloreFromPID::readCalibration(std::string path)
{
//
// open the input file
//
TDirectory* CalibDir = nullptr;
try {
CalibDir = OpenROOTdirectory(path);
}
catch (cet::exception const& e) {
// wrap the exception with context information
throw cet::exception("ShowerCalibrationGaloreFromPID", "readCalibration()", e)
<< "Reading calibration from: '" << path << "'";
}
if (!CalibDir) { // this would be actually a bug
throw cet::exception("ShowerCalibrationGaloreFromPID", "readCalibration()")
<< "Null directory reading calibration from: '" << path << "'";
}
// make sure that when this is over we are done with the ROOT file
std::unique_ptr<TFile> InputFile(CalibDir->GetFile());
//
// read each calibration object and associate it with its particle category
//
Calibration_pi0 = readParticleCalibration(CalibDir, "Pi0", 111);
Calibration_photon = readParticleCalibration(CalibDir, "Photon", 22);
Calibration_electron = readParticleCalibration(CalibDir, "Electron", {-11, 11});
Calibration_muon = readParticleCalibration(CalibDir, "Muon", {-13, 13});
Calibration_other = readParticleCalibration(CalibDir, "Default", unknownID);
//
// release resources
//
// TFile freed by its unique pointer
} // lar::example::ShowerCalibrationGaloreFromPID::readCalibration()
//------------------------------------------------------------------------------
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t const&
lar::example::ShowerCalibrationGaloreFromPID::selectCorrection(PDGID_t id) const
{
switch (id) {
case 111: // pi0
return Calibration_pi0;
case 22: // photon
return Calibration_photon;
case -11: // electron
case +11: // positron
return Calibration_electron;
case -13: // muon
case +13: // antimuon
return Calibration_muon;
case unknownID:
default: return Calibration_other;
} // switch
} // lar::example::ShowerCalibrationGaloreFromPID::selectCorrection()
//------------------------------------------------------------------------------
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t
lar::example::ShowerCalibrationGaloreFromPID::readParticleCalibration(TDirectory* SourceDir,
std::string GraphName) const
{
CalibrationInfo_t info;
// apply list is left empty
//
// retrieve the object
//
auto graph = details::readROOTobject<TGraphErrors>(SourceDir, GraphName);
verifyOrder(graph.get());
size_t const N = (size_t)graph->GetN();
if (N == 0) {
throw cet::exception("ShowerCalibrationGaloreFromPID")
<< "No point in graph " << SourceDir->GetPath() << "/" << GraphName << "\n";
}
// include the "error" on x in the full range
info.minE = graph->GetX()[0];
info.maxE = graph->GetX()[N - 1];
// a spline; if there are at least 5 points, use AKIMA spline, that is
// "stable" for outliers (reducing over/undershoot)
// set to zero
info.factor = createInterpolator(N, graph->GetX(), graph->GetY());
// compute the error in the same way; kind of an approximation here
info.error = createInterpolator(N, graph->GetX(), graph->GetEY());
return info;
} // lar::example::ShowerCalibrationGaloreFromPID::readParticleCalibration()
//------------------------------------------------------------------------------
void lar::example::ShowerCalibrationGaloreFromPID::verifyOrder(TGraph const* graph)
{
if (!graph) {
throw cet::exception("ShowerCalibrationGaloreFromPID")
<< "VerifyOrder(): invalid graph specified\n";
}
size_t const N = graph->GetN();
if (N < 2) return;
Double_t const* x = graph->GetX();
for (size_t i = 1; i < N; ++i) {
if (x[i - 1] > x[i]) {
throw cet::exception("ShowerCalibrationGaloreFromPID")
<< "VerifyOrder(): points in graph '" << graph->GetName()
<< "' are not sorted in abscissa\n";
}
} // while
} // lar::example::ShowerCalibrationGaloreFromPID::verifyOrder()
//------------------------------------------------------------------------------
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t
lar::example::ShowerCalibrationGaloreFromPID::readParticleCalibration(TDirectory* SourceDir,
std::string GraphName,
PDGID_t id) const
{
CalibrationInfo_t info = readParticleCalibration(SourceDir, GraphName);
info.applyTo(id);
return info;
} // lar::example::ShowerCalibrationGaloreFromPID::readParticleCalibration(ID)
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t
lar::example::ShowerCalibrationGaloreFromPID::readParticleCalibration(
TDirectory* SourceDir,
std::string GraphName,
std::initializer_list<PDGID_t> ids) const
{
CalibrationInfo_t info = readParticleCalibration(SourceDir, GraphName);
info.applyTo(ids);
return info;
} // lar::example::ShowerCalibrationGaloreFromPID::readParticleCalibration(IDs)
//------------------------------------------------------------------------------
TDirectory* lar::example::ShowerCalibrationGaloreFromPID::OpenROOTdirectory(std::string path)
{
//
// split the data file path
//
std::string filePath, ROOTdirPath;
std::tie(filePath, ROOTdirPath) = splitROOTpath(path);
//
// find the ROOT file in the search path
//
std::string fullFilePath;
cet::search_path sp("FW_SEARCH_PATH");
// if we can't find the file in FW_SEARCH_PATH, we try in current directory
if (!sp.find_file(filePath, fullFilePath)) fullFilePath = filePath;
//
// open the ROOT file (created new)
//
auto inputFile = std::make_unique<TFile>(fullFilePath.c_str(), "READ");
if (!(inputFile->IsOpen())) {
throw cet::exception("ShowerCalibrationGaloreFromPID")
<< "ShowerCalibrationGaloreFromPID::OpenROOTdirectory() can't read '" << fullFilePath
<< "' (from '" << filePath << "' specification)\n";
}
//
// get the ROOT directory
//
TDirectory* dir =
ROOTdirPath.empty() ? inputFile.get() : inputFile->GetDirectory(ROOTdirPath.c_str());
if (!dir) {
throw cet::exception("ShowerCalibrationGaloreFromPID")
<< "ShowerCalibrationGaloreFromPID::OpenROOTdirectory() can't find '" << ROOTdirPath
<< "' in ROOT file '" << inputFile->GetPath() << "'\n";
}
//
// return the directory
//
inputFile.release(); // do not delete the file any more
return dir;
} // lar::example::ShowerCalibrationGaloreFromPID::OpenROOTdirectory()
//------------------------------------------------------------------------------
// lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t
//
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t&
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t::applyTo(PDGID_t id)
{
auto it = std::lower_bound(appliesTo.begin(), appliesTo.end(), id);
if ((it == appliesTo.end()) || (*it != id)) appliesTo.insert(it, id);
return *this;
} // ShowerCalibrationGaloreFromPID::CalibrationInfo_t::applyTo(ID)
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t&
lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t::applyTo(
std::initializer_list<PDGID_t> ids)
{
appliesTo.insert(appliesTo.begin(), ids.begin(), ids.end());
std::sort(appliesTo.begin(), appliesTo.end());
return *this;
} // ShowerCalibrationGaloreFromPID::CalibrationInfo_t::applyTo(IDs)
//--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ---
double lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t::evalFactor(double E) const
{
double const boundE = std::min(maxE, std::max(minE, E));
return factor->Eval(boundE);
}
double lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t::evalError(double E) const
{
double const boundE = std::min(maxE, std::max(minE, E));
return error->Eval(boundE);
}
//------------------------------------------------------------------------------
std::unique_ptr<ROOT::Math::Interpolator>
lar::example::ShowerCalibrationGaloreFromPID::createInterpolator(unsigned int N,
double const* x,
double const* y)
{
// decide the type of interpolation based on the available number of points
ROOT::Math::Interpolation::Type type;
if (N >= 5)
type = ROOT::Math::Interpolation::kAKIMA;
else if (N >= 3)
type = ROOT::Math::Interpolation::kCSPLINE;
else
type = ROOT::Math::Interpolation::kLINEAR;
auto interp = std::make_unique<ROOT::Math::Interpolator>(std::max(N, 2U), type);
if (N > 1)
interp->SetData(N, x, y);
else { // we need to make up the second point
double const x_p[2] = {*x, *x + 1.};
double const y_p[2] = {*y, *y};
interp->SetData(2, x_p, y_p);
}
return interp;
} // lar::example::ShowerCalibrationGaloreFromPID::createInterpolator()
//------------------------------------------------------------------------------
std::pair<std::string, std::string> lar::example::splitROOTpath(std::string path)
{
const std::string suffix = ".root";
// find the ROOT file name
std::string::size_type iSuffix = std::string::npos;
do {
iSuffix = path.rfind(suffix, iSuffix);
if (iSuffix == std::string::npos) return {}; // failure: no suffix
// if it's not "suffix:" or "suffix/" or at end of string, it's not it
auto iAfter = iSuffix + suffix.length();
if ((iAfter < path.length()) && (path[iAfter] != '/') && (path[iAfter] != ':')) {
// oops... this suffix is invalid; keep searching
if (iSuffix == 0) return {}; // failure: no suffix
--iSuffix;
continue;
}
// we found a proper suffix
std::pair<std::string, std::string> result;
result.first = path.substr(0U, iAfter); // file path
if (iAfter < path.length()) result.second = path.substr(iAfter + 1, path.length()); // ROOT dir
return result;
} while (true);
} // lar::example::splitROOTpath()
//------------------------------------------------------------------------------