Belle II Software development
ECLChargedPidPDFs.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/logging/Logger.h>
12#include <framework/gearbox/Unit.h>
13
14#include <unordered_map>
15
16#include <TObject.h>
17#include <TParameter.h>
18#include <TH2F.h>
19#include <TF1.h>
20
21namespace Belle2 {
30 class ECLChargedPidPDFs: public TObject {
31
32 public:
33
36 m_energy_unit("energyUnit", Unit::rad),
37 m_ang_unit("angularUnit", Unit::GeV),
39 m_pdfsmap(),
40 m_vtsmap()
41 {};
42
45
49 enum class InputVar : unsigned int {
51 c_NONE = 0,
53 c_E1E9 = 1,
55 c_E9E21 = 2,
57 c_S2 = 3,
59 c_E = 4,
61 c_EoP = 5,
63 c_Z40 = 6,
65 c_Z51 = 7,
67 c_ZMVA = 8,
69 c_PSDMVA = 9,
71 c_DeltaL = 10,
73 c_LAT = 11
74 };
75
82 public:
83
85 VarTransfoSettings() : nVars(0), nDivisionsMax(0), ip(0), jth(0), gbin(0) {}
88
89 int nVars;
90 std::string classPath;
91 std::vector<int> nDivisions;
93 std::vector<double> cumulDist;
94 std::vector<double> x;
95 std::vector<double> covMatrix;
97 unsigned int ip;
98 unsigned int jth;
99 unsigned int gbin;
100 };
101
102 typedef std::unordered_map<InputVar, TF1*> PdfsByVariable;
103 typedef std::unordered_map<int, PdfsByVariable > PdfsMapByCategory;
104 typedef std::unordered_map<int, PdfsMapByCategory > PdfsMapByParticle;
105 typedef std::unordered_map<int, std::vector<InputVar> > VariablesMapByCategory;
106 typedef std::unordered_map<int, VariablesMapByCategory > VariablesMapByParticle;
108 typedef std::unordered_map<int, VarTransfoSettings> VTSMapByCategory;
109 typedef std::unordered_map<int, VTSMapByCategory > VTSMapByParticle;
119 void setVars(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j,
120 const std::vector<InputVar>& vars)
121 {
122 auto ji = m_categories->GetBin(j, i);
123
124 m_variablesmap_bycategory[ji] = vars;
125
126 const int signed_pdg = pdg * true_charge / std::abs(true_charge);
127
129
130 }
131
139 const std::vector<InputVar>* getVars(const unsigned int pdg, const int charge, const double& p, const double& theta) const
140 {
141
142 auto gbin = findBin(m_categories, theta, p);
143
144 const int signed_pdg = pdg * charge / std::abs(charge);
145
146 return &(m_variablesmap.at(signed_pdg).at(gbin));
147
148 }
149
153 void printPdfMap(const unsigned int pdg = 0,
154 const double& p = -1.0, const double& theta = -999.0,
155 const int true_charge = 1,
156 const InputVar varid = InputVar::c_NONE) const
157 {
158
159 const int signed_pdg = pdg * true_charge / std::abs(true_charge);
160
161 std::cout << "Printing PDF info: " << std::endl;
162 if (pdg) std::cout << "-) |pdgId| * true_charge = " << signed_pdg << std::endl;
163 if (p != -1.0) std::cout << "-) p = " << p << " [GeV/c]" << std::endl;
164 if (theta != -999.0) std::cout << "-) clusterTheta = " << theta << " [rad]" << std::endl;
165 if (varid != InputVar::c_NONE) std::cout << "-) varid = " << static_cast<unsigned int>(varid) << std::endl;
166
167 int x, y, z;
168 for (const auto& pair0 : m_pdfsmap) {
169
170 if (signed_pdg && signed_pdg != pair0.first) continue;
171
172 for (const auto& pair1 : pair0.second) {
173
174 auto ji(-1);
175 if (p != -1.0 && theta != -999.0) {
176 ji = findBin(m_categories, theta, p);
177 m_categories->GetBinXYZ(ji, x, y, z);
178 }
179 if (ji > 0 && ji != pair1.first) continue;
180
181 std::cout << "\tglobal_bin_idx: " << pair1.first << " (" << x << "," << y << ")" << std::endl;
182
183 for (const auto& pair2 : pair1.second) {
184 if (varid != InputVar::c_NONE && varid != pair2.first) continue;
185 std::cout << "\t\tvarid: " << static_cast<unsigned int>(varid) << ", TF1: " << pair2.second->GetName() << std::endl;
186 }
187
188 }
189 }
190 }
191
194 void setEnergyUnit(const double& unit) { m_energy_unit.SetVal(unit); }
195
198 void setAngularUnit(const double& unit) { m_ang_unit.SetVal(unit); }
199
204 void setPdfCategories(TH2F* h)
205 {
206 m_categories = h;
207 }
208
212 const TH2F* getPdfCategories() const
213 {
214 return m_categories;
215 }
216
226 void add(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const InputVar varid, TF1* pdf)
227 {
228
229 auto ji = m_categories->GetBin(j, i);
230
231 const int signed_pdg = pdg * true_charge / std::abs(true_charge);
232
233 m_pdfsmap[signed_pdg][ji][varid] = pdf;
234
235 }
236
245 const TF1* getPdf(const unsigned int pdg, const int charge, const double& p, const double& theta, const InputVar varid) const
246 {
247
248 const int signed_pdg = pdg * charge / std::abs(charge);
249
250 double pp = p / m_energy_unit.GetVal();
251 double th = TMath::Abs(theta) / m_ang_unit.GetVal();
252
253 int gbin = findBin(m_categories, th, pp);
254
255 int x, y, z;
256 m_categories->GetBinXYZ(gbin, x, y, z);
257
258 B2DEBUG(30, "\t\tAngular unit: " << m_ang_unit.GetVal());
259 B2DEBUG(30, "\t\tEnergy unit: " << m_energy_unit.GetVal());
260 B2DEBUG(30, "\t\t|pdgId| * reco_charge = " << signed_pdg << ", clusterTheta = " << th << ", p = " << pp);
261 B2DEBUG(30, "\t\tgbin = " << gbin << ", x,y = (" << x << "," << y << ")");
262 B2DEBUG(30, "\t\tvariable id = " << static_cast<unsigned int>(varid));
263
264 return m_pdfsmap.at(signed_pdg).at(gbin).at(varid);
265 }
266
270 inline bool doVarsTransfo() const { return m_do_varstransform; }
271
285 void storeVarsTransfoSettings(const unsigned int pdg,
286 const int true_charge,
287 const unsigned int i, const unsigned int j,
288 const int nVars,
289 const std::string& classPath = "",
290 const std::vector<int>& nDivisions = std::vector<int>(),
291 const std::vector<double>& cumulDist = std::vector<double>(),
292 const std::vector<double>& x = std::vector<double>(),
293 const std::vector<double>& covMatrix = std::vector<double>())
294 {
295
296 const int signed_pdg = pdg * true_charge / std::abs(true_charge);
297
298 m_do_varstransform = true;
299
301
302 vts.nVars = nVars;
303 vts.classPath = classPath;
304 vts.nDivisionsMax = (!nDivisions.empty()) ? *(std::max_element(std::begin(nDivisions), std::end(nDivisions))) : 0;
305 vts.nDivisions = nDivisions;
306 vts.cumulDist = cumulDist;
307 vts.x = x;
308 vts.covMatrix = covMatrix;
309
310 auto ji = m_categories->GetBin(j, i);
311 vts.gbin = ji;
312 vts.ip = i;
313 vts.jth = j;
314
315 m_vtsmap_bycategory[ji] = vts;
316
317 m_vtsmap[signed_pdg] = m_vtsmap_bycategory;
318
319 }
320
328 const VarTransfoSettings* getVTS(const unsigned int pdg, const int charge, const double& p, const double& theta) const
329 {
330
331 const int signed_pdg = pdg * charge / std::abs(charge);
332
333 auto gbin = findBin(m_categories, theta, p);
334
335 return &(m_vtsmap.at(signed_pdg).at(gbin));
336
337 }
338
339 private:
340
349 int findBin(const TH2F* h, const double& x, const double& y) const
350 {
351
352 int nbinsx_vis = h->GetXaxis()->GetNbins();
353 int nbinsy_vis = h->GetYaxis()->GetNbins();
354
355 double xx = x;
356 double yy = y;
357
358 // If x, y are outside of the 2D hogram grid (visible) range, set their value to
359 // fall in the last (first) bin before (after) overflow (underflow).
360 if (x < h->GetXaxis()->GetBinLowEdge(1)) { xx = h->GetXaxis()->GetBinCenter(1); }
361 if (x >= h->GetXaxis()->GetBinLowEdge(nbinsx_vis + 1)) { xx = h->GetXaxis()->GetBinCenter(nbinsx_vis); }
362 if (y < h->GetYaxis()->GetBinLowEdge(1)) { yy = h->GetYaxis()->GetBinCenter(1); }
363 if (y >= h->GetYaxis()->GetBinLowEdge(nbinsy_vis + 1)) { yy = h->GetYaxis()->GetBinCenter(nbinsy_vis); }
364
365 int nbinsx = h->GetXaxis()->GetNbins() + 2;
366 int j = h->GetXaxis()->FindBin(xx);
367 int i = h->GetYaxis()->FindBin(yy);
368
369 return j + nbinsx * i;
370 }
371
372 private:
373
374 TParameter<double> m_energy_unit;
375 TParameter<double> m_ang_unit;
381 TH2F* m_categories = nullptr;
382
389
398
403 bool m_do_varstransform = false;
404
411
418
425
432
440
447 };
449} // end namespace Belle2
Class to hold parameters needed to perform pre-processing of input variables (e.g....
unsigned int gbin
Global bin corresponding to (jth,ip)
std::vector< double > cumulDist
Cumulative density function at each step.
int nDivisionsMax
Maximal number of steps, across all variables.
std::vector< double > x
Variable value at each step.
std::string classPath
Path of the class used to get the variables transfo.
std::vector< int > nDivisions
Number of steps in which each variable range is sub-divided.
std::vector< double > covMatrix
Variables covariance matrix.
Class representing the DB payload w/ information about ECL PDF parameters for a set of particle hypot...
TParameter< double > m_energy_unit
The energy unit used for the binning.
InputVar
Enum type for observables for which PDFs are stored.
@ c_DeltaL
DeltaL (track depth)
@ c_E
Energy of maxE shower.
@ c_LAT
Lateral shower shape.
ClassDef(ECLChargedPidPDFs, 2)
ClassDef.
VTSMapByParticle m_vtsmap
Internal map.
bool doVarsTransfo() const
Check whether variables transformation is applied.
void add(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const InputVar varid, TF1 *pdf)
Fills the internal maps for a given hypothesis and category (clusterTheta, p).
std::unordered_map< int, std::vector< InputVar > > VariablesMapByCategory
Typedef.
std::unordered_map< int, VariablesMapByCategory > VariablesMapByParticle
Typedef.
const std::vector< InputVar > * getVars(const unsigned int pdg, const int charge, const double &p, const double &theta) const
Getter for list of input variables (enums) for which PDFs are stored for a given hypothesis and categ...
PdfsMapByCategory m_pdfsmap_bycategory
Internal map.
ECLChargedPidPDFs()
Default constructor.
std::unordered_map< int, PdfsMapByCategory > PdfsMapByParticle
Typedef.
void setPdfCategories(TH2F *h)
Set the 2D (clusterTheta, p) grid representing the categories for which PDFs are defined.
void printPdfMap(const unsigned int pdg=0, const double &p=-1.0, const double &theta=-999.0, const int true_charge=1, const InputVar varid=InputVar::c_NONE) const
Print out the content of the internal 'matrioska' maps.
VariablesMapByParticle m_variablesmap
Internal map.
VTSMapByCategory m_vtsmap_bycategory
Internal map.
std::unordered_map< int, VarTransfoSettings > VTSMapByCategory
Typedef.
TParameter< double > m_ang_unit
The angular unit used for the binning.
VariablesMapByCategory m_variablesmap_bycategory
Internal map.
const VarTransfoSettings * getVTS(const unsigned int pdg, const int charge, const double &p, const double &theta) const
Getter for variable transformation settings.
const TF1 * getPdf(const unsigned int pdg, const int charge, const double &p, const double &theta, const InputVar varid) const
Return the PDF of this observable for this candidate's reconstructed (p, clusterTheta),...
TH2F * m_categories
A 2D (x, y) = (clusterTheta, p) histogram whose bins represent the categories for which PDFs are defi...
int findBin(const TH2F *h, const double &x, const double &y) const
Find global bin index of a 2D histogram for the given (x, y) values.
std::unordered_map< int, VTSMapByCategory > VTSMapByParticle
Typedef.
PdfsMapByParticle m_pdfsmap
Internal map.
std::unordered_map< int, PdfsByVariable > PdfsMapByCategory
Typedef.
void setVars(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const std::vector< InputVar > &vars)
Set the names of the input variables for which PDFs are stored for a given hypothesis and category (p...
void storeVarsTransfoSettings(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const int nVars, const std::string &classPath="", const std::vector< int > &nDivisions=std::vector< int >(), const std::vector< double > &cumulDist=std::vector< double >(), const std::vector< double > &x=std::vector< double >(), const std::vector< double > &covMatrix=std::vector< double >())
Setup the variable transformations for a given hypothesis in a category (p, clusterTheta),...
std::unordered_map< InputVar, TF1 * > PdfsByVariable
Typedef.
void setAngularUnit(const double &unit)
Set the angular unit to be consistent w/ the one used in the fit.
bool m_do_varstransform
To be toggled on if input variables have been transformed (i.e., if storeVarsTransfoSettings() was ca...
void setEnergyUnit(const double &unit)
Set the energy unit to be consistent w/ the one used in the fit.
const TH2F * getPdfCategories() const
Get the 2D (clusterTheta, p) grid representing the categories for which PDFs are defined.
PdfsByVariable m_pdfs_byvariable
Internal map.
The Unit class.
Definition: Unit.h:40
Abstract base class for different kinds of events.