Belle II Software  release-05-01-25
ECLChargedPidPDFs.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Parameters of the ECL PDFs for charged particles. *
6  * *
7  * Author: The Belle II Collaboration *
8  * Contributors: Marco Milesi (marco.milesi@unimelb.edu.au) *
9  * *
10  * This software is provided "as is" without any warranty. *
11  **************************************************************************/
12 
13 #pragma once
14 
15 #include <framework/logging/Logger.h>
16 #include <framework/gearbox/Unit.h>
17 
18 #include <unordered_map>
19 
20 #include <TObject.h>
21 #include <TParameter.h>
22 #include <TH2F.h>
23 #include <TF1.h>
24 
25 namespace Belle2 {
34  class ECLChargedPidPDFs: public TObject {
35 
36  public:
37 
40  m_energy_unit("energyUnit", Unit::rad),
41  m_ang_unit("angularUnit", Unit::GeV),
43  m_pdfsmap(),
45  {};
46 
48  ~ECLChargedPidPDFs() {};
49 
53  enum class InputVar : unsigned int {
55  c_NONE = 0,
57  c_E1E9 = 1,
59  c_E9E21 = 2,
61  c_S2 = 3,
63  c_E = 4,
65  c_EoP = 5,
67  c_Z40 = 6,
69  c_Z51 = 7,
71  c_ZMVA = 8,
73  c_PSDMVA = 9,
75  c_DeltaL = 10,
77  c_LAT = 11
78  };
79 
85  class VarTransfoSettings {
86  public:
87 
88  VarTransfoSettings() {};
89  ~VarTransfoSettings() {};
91  int nVars;
92  std::string classPath;
93  std::vector<int> nDivisions;
94  int nDivisionsMax;
95  std::vector<double> cumulDist;
96  std::vector<double> x;
97  std::vector<double> covMatrix;
99  unsigned int ip;
100  unsigned int jth;
101  unsigned int gbin;
102  };
103 
104  typedef std::unordered_map<InputVar, TF1*> PdfsByVariable;
105  typedef std::unordered_map<int, PdfsByVariable > PdfsMapByCategory;
106  typedef std::unordered_map<int, PdfsMapByCategory > PdfsMapByParticle;
107  typedef std::unordered_map<int, std::vector<InputVar> > VariablesMapByCategory;
108  typedef std::unordered_map<int, VariablesMapByCategory > VariablesMapByParticle;
110  typedef std::unordered_map<int, VarTransfoSettings> VTSMapByCategory;
111  typedef std::unordered_map<int, VTSMapByCategory > VTSMapByParticle;
121  void setVars(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j,
122  const std::vector<InputVar>& vars)
123  {
124  auto ji = m_categories->GetBin(j, i);
125 
126  m_variablesmap_bycategory[ji] = vars;
127 
128  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
129 
131 
132  }
133 
141  const std::vector<InputVar>* getVars(const unsigned int pdg, const int charge, const double& p, const double& theta) const
142  {
143 
144  auto gbin = findBin(m_categories, theta, p);
145 
146  const int signed_pdg = pdg * charge / std::abs(charge);
147 
148  return &(m_variablesmap.at(signed_pdg).at(gbin));
149 
150  }
151 
155  void printPdfMap(const unsigned int pdg = 0,
156  const double& p = -1.0, const double& theta = -999.0,
157  const int true_charge = 1,
158  const InputVar varid = InputVar::c_NONE) const
159  {
160 
161  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
162 
163  std::cout << "Printing PDF info: " << std::endl;
164  if (pdg) std::cout << "-) |pdgId| * true_charge = " << signed_pdg << std::endl;
165  if (p != -1.0) std::cout << "-) p = " << p << " [GeV/c]" << std::endl;
166  if (theta != -999.0) std::cout << "-) clusterTheta = " << theta << " [rad]" << std::endl;
167  if (varid != InputVar::c_NONE) std::cout << "-) varid = " << static_cast<unsigned int>(varid) << std::endl;
168 
169  int x, y, z;
170  for (const auto& pair0 : m_pdfsmap) {
171 
172  if (signed_pdg && signed_pdg != pair0.first) continue;
173 
174  for (const auto& pair1 : pair0.second) {
175 
176  auto ji(-1);
177  if (p != -1.0 && theta != -999.0) {
178  ji = findBin(m_categories, theta, p);
179  m_categories->GetBinXYZ(ji, x, y, z);
180  }
181  if (ji > 0 && ji != pair1.first) continue;
182 
183  std::cout << "\tglobal_bin_idx: " << pair1.first << " (" << x << "," << y << ")" << std::endl;
184 
185  for (const auto& pair2 : pair1.second) {
186  if (varid != InputVar::c_NONE && varid != pair2.first) continue;
187  std::cout << "\t\tvarid: " << static_cast<unsigned int>(varid) << ", TF1: " << pair2.second->GetName() << std::endl;
188  }
189 
190  }
191  }
192  }
193 
196  void setEnergyUnit(const double& unit) { m_energy_unit.SetVal(unit); }
197 
200  void setAngularUnit(const double& unit) { m_ang_unit.SetVal(unit); }
201 
206  void setPdfCategories(TH2F* h)
207  {
208  m_categories = h;
209  }
210 
214  const TH2F* getPdfCategories() const
215  {
216  return m_categories;
217  }
218 
228  void add(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const InputVar varid, TF1* pdf)
229  {
230 
231  auto ji = m_categories->GetBin(j, i);
232 
233  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
234 
235  m_pdfsmap[signed_pdg][ji][varid] = pdf;
236 
237  }
238 
247  const TF1* getPdf(const unsigned int pdg, const int charge, const double& p, const double& theta, const InputVar varid) const
248  {
249 
250  const int signed_pdg = pdg * charge / std::abs(charge);
251 
252  double pp = p / m_energy_unit.GetVal();
253  double th = TMath::Abs(theta) / m_ang_unit.GetVal();
254 
255  int gbin = findBin(m_categories, th, pp);
256 
257  int x, y, z;
258  m_categories->GetBinXYZ(gbin, x, y, z);
259 
260  B2DEBUG(30, "\t\tAngular unit: " << m_ang_unit.GetVal());
261  B2DEBUG(30, "\t\tEnergy unit: " << m_energy_unit.GetVal());
262  B2DEBUG(30, "\t\t|pdgId| * reco_charge = " << signed_pdg << ", clusterTheta = " << th << ", p = " << pp);
263  B2DEBUG(30, "\t\tgbin = " << gbin << ", x,y = (" << x << "," << y << ")");
264  B2DEBUG(30, "\t\tvariable id = " << static_cast<unsigned int>(varid));
265 
266  return m_pdfsmap.at(signed_pdg).at(gbin).at(varid);
267  }
268 
272  inline bool doVarsTransfo() const { return m_do_varstransform; }
273 
287  void storeVarsTransfoSettings(const unsigned int pdg,
288  const int true_charge,
289  const unsigned int i, const unsigned int j,
290  const int nVars,
291  const std::string& classPath = "",
292  const std::vector<int>& nDivisions = std::vector<int>(),
293  const std::vector<double>& cumulDist = std::vector<double>(),
294  const std::vector<double>& x = std::vector<double>(),
295  const std::vector<double>& covMatrix = std::vector<double>())
296  {
297 
298  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
299 
300  m_do_varstransform = true;
301 
302  VarTransfoSettings vts;
303 
304  vts.nVars = nVars;
305  vts.classPath = classPath;
306  vts.nDivisionsMax = (!nDivisions.empty()) ? *(std::max_element(std::begin(nDivisions), std::end(nDivisions))) : 0;
307  vts.nDivisions = nDivisions;
308  vts.cumulDist = cumulDist;
309  vts.x = x;
310  vts.covMatrix = covMatrix;
311 
312  auto ji = m_categories->GetBin(j, i);
313  vts.gbin = ji;
314  vts.ip = i;
315  vts.jth = j;
316 
317  m_vtsmap_bycategory[ji] = vts;
318 
319  m_vtsmap[signed_pdg] = m_vtsmap_bycategory;
320 
321  }
322 
330  const VarTransfoSettings* getVTS(const unsigned int pdg, const int charge, const double& p, const double& theta) const
331  {
332 
333  const int signed_pdg = pdg * charge / std::abs(charge);
334 
335  auto gbin = findBin(m_categories, theta, p);
336 
337  return &(m_vtsmap.at(signed_pdg).at(gbin));
338 
339  }
340 
341  private:
342 
350  int findBin(const TH2F* h, const double& x, const double& y) const
351  {
352 
353  int nbinsx_vis = h->GetXaxis()->GetNbins();
354  int nbinsy_vis = h->GetYaxis()->GetNbins();
355 
356  double xx = x;
357  double yy = y;
358 
359  // If x, y are outside of the 2D hogram grid (visible) range, set their value to
360  // fall in the last (first) bin before (after) overflow (underflow).
361  if (x < h->GetXaxis()->GetBinLowEdge(1)) { xx = h->GetXaxis()->GetBinCenter(1); }
362  if (x >= h->GetXaxis()->GetBinLowEdge(nbinsx_vis + 1)) { xx = h->GetXaxis()->GetBinCenter(nbinsx_vis); }
363  if (y < h->GetYaxis()->GetBinLowEdge(1)) { yy = h->GetYaxis()->GetBinCenter(1); }
364  if (y >= h->GetYaxis()->GetBinLowEdge(nbinsy_vis + 1)) { yy = h->GetYaxis()->GetBinCenter(nbinsy_vis); }
365 
366  int nbinsx = h->GetXaxis()->GetNbins() + 2;
367  int j = h->GetXaxis()->FindBin(xx);
368  int i = h->GetYaxis()->FindBin(yy);
369 
370  return j + nbinsx * i;
371  }
372 
373  private:
374 
375  TParameter<double> m_energy_unit;
376  TParameter<double> m_ang_unit;
382  TH2F* m_categories = nullptr;
383 
390 
399 
404  bool m_do_varstransform = false;
405 
412 
419 
426 
433 
441 
448  };
450 } // end namespace Belle2
Belle2::ECLChargedPidPDFs::doVarsTransfo
bool doVarsTransfo() const
Check whether variables transformation is applied.
Definition: ECLChargedPidPDFs.h:282
Belle2::ECLChargedPidPDFs::InputVar::c_Z51
@ c_Z51
Zernike moment 51.
Belle2::ECLChargedPidPDFs::setAngularUnit
void setAngularUnit(const double &unit)
Set the angular unit to be consistent w/ the one used in the fit.
Definition: ECLChargedPidPDFs.h:210
Belle2::ECLChargedPidPDFs::VarTransfoSettings::jth
unsigned int jth
p bin index
Definition: ECLChargedPidPDFs.h:110
Belle2::ECLChargedPidPDFs::getVars
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...
Definition: ECLChargedPidPDFs.h:151
Belle2::ECLChargedPidPDFs::VarTransfoSettings::classPath
std::string classPath
Number of variables.
Definition: ECLChargedPidPDFs.h:102
Belle2::ECLChargedPidPDFs::setEnergyUnit
void setEnergyUnit(const double &unit)
Set the energy unit to be consistent w/ the one used in the fit.
Definition: ECLChargedPidPDFs.h:206
Belle2::ECLChargedPidPDFs::add
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).
Definition: ECLChargedPidPDFs.h:238
Belle2::ECLChargedPidPDFs::InputVar::c_PSDMVA
@ c_PSDMVA
PSD MVA.
Belle2::ECLChargedPidPDFs::m_categories
TH2F * m_categories
A 2D (x, y) = (clusterTheta, p) histogram whose bins represent the categories for which PDFs are defi...
Definition: ECLChargedPidPDFs.h:392
Belle2::ECLChargedPidPDFs::VariablesMapByParticle
std::unordered_map< int, VariablesMapByCategory > VariablesMapByParticle
Typedef.
Definition: ECLChargedPidPDFs.h:118
Belle2::ECLChargedPidPDFs::~ECLChargedPidPDFs
~ECLChargedPidPDFs()
Destructor.
Definition: ECLChargedPidPDFs.h:58
Belle2::ECLChargedPidPDFs::m_pdfsmap
PdfsMapByParticle m_pdfsmap
Internal map.
Definition: ECLChargedPidPDFs.h:421
Belle2::ECLChargedPidPDFs::getPdf
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),...
Definition: ECLChargedPidPDFs.h:257
Belle2::ECLChargedPidPDFs::VarTransfoSettings::gbin
unsigned int gbin
theta bin index
Definition: ECLChargedPidPDFs.h:111
Belle2::ECLChargedPidPDFs::m_vtsmap_bycategory
VTSMapByCategory m_vtsmap_bycategory
Internal map.
Definition: ECLChargedPidPDFs.h:450
Belle2::ECLChargedPidPDFs::getPdfCategories
const TH2F * getPdfCategories() const
Get the 2D (clusterTheta, p) grid representing the categories for which PDFs are defined.
Definition: ECLChargedPidPDFs.h:224
Belle2::ECLChargedPidPDFs::m_energy_unit
TParameter< double > m_energy_unit
The energy unit used for the binning.
Definition: ECLChargedPidPDFs.h:385
Belle2::ECLChargedPidPDFs::InputVar::c_ZMVA
@ c_ZMVA
Zernike MVA.
Belle2::ECLChargedPidPDFs::getVTS
const VarTransfoSettings * getVTS(const unsigned int pdg, const int charge, const double &p, const double &theta) const
Getter for variable transformation settings.
Definition: ECLChargedPidPDFs.h:340
Belle2::ECLChargedPidPDFs::findBin
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.
Definition: ECLChargedPidPDFs.h:360
Belle2::ECLChargedPidPDFs::ClassDef
ClassDef(ECLChargedPidPDFs, 2)
ClassDef.
Belle2::ECLChargedPidPDFs::VarTransfoSettings::covMatrix
std::vector< double > covMatrix
Variable value at each step.
Definition: ECLChargedPidPDFs.h:107
Belle2::ECLChargedPidPDFs::m_ang_unit
TParameter< double > m_ang_unit
The angular unit used for the binning.
Definition: ECLChargedPidPDFs.h:386
Belle2::ECLChargedPidPDFs::InputVar::c_EoP
@ c_EoP
E/p.
Belle2::ECLChargedPidPDFs::ECLChargedPidPDFs
ECLChargedPidPDFs()
Default constructor.
Definition: ECLChargedPidPDFs.h:49
Belle2::ECLChargedPidPDFs::printPdfMap
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.
Definition: ECLChargedPidPDFs.h:165
Belle2::ECLChargedPidPDFs::m_variablesmap
VariablesMapByParticle m_variablesmap
Internal map.
Definition: ECLChargedPidPDFs.h:399
Belle2::ECLChargedPidPDFs::m_pdfs_byvariable
PdfsByVariable m_pdfs_byvariable
Internal map.
Definition: ECLChargedPidPDFs.h:435
Belle2::ECLChargedPidPDFs::InputVar
InputVar
Enum type for observables for which PDFs are stored.
Definition: ECLChargedPidPDFs.h:63
Belle2::ECLChargedPidPDFs::VarTransfoSettings::nDivisions
std::vector< int > nDivisions
Path of the class used to get the variables transfo.
Definition: ECLChargedPidPDFs.h:103
Belle2::ECLChargedPidPDFs::VTSMapByCategory
std::unordered_map< int, VarTransfoSettings > VTSMapByCategory
Typedef.
Definition: ECLChargedPidPDFs.h:120
Belle2::ECLChargedPidPDFs::m_variablesmap_bycategory
VariablesMapByCategory m_variablesmap_bycategory
Internal map.
Definition: ECLChargedPidPDFs.h:408
Belle2::ECLChargedPidPDFs::VarTransfoSettings
Class to hold parameters needed to perform pre-processing of input variables (e.g....
Definition: ECLChargedPidPDFs.h:95
Belle2::ECLChargedPidPDFs::m_pdfsmap_bycategory
PdfsMapByCategory m_pdfsmap_bycategory
Internal map.
Definition: ECLChargedPidPDFs.h:428
Belle2::ECLChargedPidPDFs::m_vtsmap
VTSMapByParticle m_vtsmap
Internal map.
Definition: ECLChargedPidPDFs.h:442
Belle2::ECLChargedPidPDFs::storeVarsTransfoSettings
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),...
Definition: ECLChargedPidPDFs.h:297
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ECLChargedPidPDFs::InputVar::c_LAT
@ c_LAT
Lateral shower shape.
Belle2::ECLChargedPidPDFs::InputVar::c_E1E9
@ c_E1E9
E1/E9.
Belle2::ECLChargedPidPDFs::setVars
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...
Definition: ECLChargedPidPDFs.h:131
Belle2::ECLChargedPidPDFs::InputVar::c_E9E21
@ c_E9E21
E9/E21.
Belle2::ECLChargedPidPDFs::VTSMapByParticle
std::unordered_map< int, VTSMapByCategory > VTSMapByParticle
Typedef.
Definition: ECLChargedPidPDFs.h:121
Belle2::ECLChargedPidPDFs::PdfsMapByParticle
std::unordered_map< int, PdfsMapByCategory > PdfsMapByParticle
Typedef.
Definition: ECLChargedPidPDFs.h:116
Belle2::ECLChargedPidPDFs::VarTransfoSettings::nDivisionsMax
int nDivisionsMax
Number of steps in which each variable range is sub-divided.
Definition: ECLChargedPidPDFs.h:104
Belle2::ECLChargedPidPDFs::m_do_varstransform
bool m_do_varstransform
To be toggled on if input variables have been transformed (i.e., if storeVarsTransfoSettings() was ca...
Definition: ECLChargedPidPDFs.h:414
Belle2::ECLChargedPidPDFs::VarTransfoSettings::nVars
int nVars
Destructor.
Definition: ECLChargedPidPDFs.h:99
Belle2::ECLChargedPidPDFs
Class representing the DB payload w/ information about ECL PDF parameters for a set of particle hypot...
Definition: ECLChargedPidPDFs.h:44
Belle2::ECLChargedPidPDFs::InputVar::c_Z40
@ c_Z40
Zernike moment 40.
Belle2::ECLChargedPidPDFs::VarTransfoSettings::ip
unsigned int ip
Variables covariance matrix.
Definition: ECLChargedPidPDFs.h:109
Belle2::ECLChargedPidPDFs::InputVar::c_NONE
@ c_NONE
Null var.
Belle2::ECLChargedPidPDFs::InputVar::c_E
@ c_E
Energy of maxE shower.
Belle2::ECLChargedPidPDFs::InputVar::c_DeltaL
@ c_DeltaL
DeltaL (track depth)
Belle2::ECLChargedPidPDFs::PdfsMapByCategory
std::unordered_map< int, PdfsByVariable > PdfsMapByCategory
Typedef.
Definition: ECLChargedPidPDFs.h:115
Belle2::ECLChargedPidPDFs::VarTransfoSettings::~VarTransfoSettings
~VarTransfoSettings()
Constructor.
Definition: ECLChargedPidPDFs.h:99
Belle2::ECLChargedPidPDFs::VariablesMapByCategory
std::unordered_map< int, std::vector< InputVar > > VariablesMapByCategory
Typedef.
Definition: ECLChargedPidPDFs.h:117
Belle2::ECLChargedPidPDFs::VarTransfoSettings::cumulDist
std::vector< double > cumulDist
Maximal number of steps, across all variables.
Definition: ECLChargedPidPDFs.h:105
Belle2::ECLChargedPidPDFs::InputVar::c_S2
@ c_S2
Second moment.
Belle2::ECLChargedPidPDFs::setPdfCategories
void setPdfCategories(TH2F *h)
Set the 2D (clusterTheta, p) grid representing the categories for which PDFs are defined.
Definition: ECLChargedPidPDFs.h:216
Belle2::ECLChargedPidPDFs::PdfsByVariable
std::unordered_map< InputVar, TF1 * > PdfsByVariable
Typedef.
Definition: ECLChargedPidPDFs.h:114
Belle2::ECLChargedPidPDFs::VarTransfoSettings::x
std::vector< double > x
Cumulative density function at each step.
Definition: ECLChargedPidPDFs.h:106