Belle II Software  release-05-02-19
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 
80 
81  static const int iNaN = std::numeric_limits<int>::quiet_NaN();
82  static const int uNaN = std::numeric_limits<unsigned>::quiet_NaN();
83 
89  class VarTransfoSettings {
90  public:
91 
93  VarTransfoSettings() : nVars(iNaN), nDivisionsMax(iNaN), ip(uNaN), jth(uNaN), gbin(uNaN) {}
96 
97  int nVars;
98  std::string classPath;
99  std::vector<int> nDivisions;
100  int nDivisionsMax;
101  std::vector<double> cumulDist;
102  std::vector<double> x;
103  std::vector<double> covMatrix;
105  unsigned int ip;
106  unsigned int jth;
107  unsigned int gbin;
108  };
109 
110  typedef std::unordered_map<InputVar, TF1*> PdfsByVariable;
111  typedef std::unordered_map<int, PdfsByVariable > PdfsMapByCategory;
112  typedef std::unordered_map<int, PdfsMapByCategory > PdfsMapByParticle;
113  typedef std::unordered_map<int, std::vector<InputVar> > VariablesMapByCategory;
114  typedef std::unordered_map<int, VariablesMapByCategory > VariablesMapByParticle;
116  typedef std::unordered_map<int, VarTransfoSettings> VTSMapByCategory;
117  typedef std::unordered_map<int, VTSMapByCategory > VTSMapByParticle;
127  void setVars(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j,
128  const std::vector<InputVar>& vars)
129  {
130  auto ji = m_categories->GetBin(j, i);
131 
132  m_variablesmap_bycategory[ji] = vars;
133 
134  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
135 
137 
138  }
139 
147  const std::vector<InputVar>* getVars(const unsigned int pdg, const int charge, const double& p, const double& theta) const
148  {
149 
150  auto gbin = findBin(m_categories, theta, p);
151 
152  const int signed_pdg = pdg * charge / std::abs(charge);
153 
154  return &(m_variablesmap.at(signed_pdg).at(gbin));
155 
156  }
157 
161  void printPdfMap(const unsigned int pdg = 0,
162  const double& p = -1.0, const double& theta = -999.0,
163  const int true_charge = 1,
164  const InputVar varid = InputVar::c_NONE) const
165  {
166 
167  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
168 
169  std::cout << "Printing PDF info: " << std::endl;
170  if (pdg) std::cout << "-) |pdgId| * true_charge = " << signed_pdg << std::endl;
171  if (p != -1.0) std::cout << "-) p = " << p << " [GeV/c]" << std::endl;
172  if (theta != -999.0) std::cout << "-) clusterTheta = " << theta << " [rad]" << std::endl;
173  if (varid != InputVar::c_NONE) std::cout << "-) varid = " << static_cast<unsigned int>(varid) << std::endl;
174 
175  int x, y, z;
176  for (const auto& pair0 : m_pdfsmap) {
177 
178  if (signed_pdg && signed_pdg != pair0.first) continue;
179 
180  for (const auto& pair1 : pair0.second) {
181 
182  auto ji(-1);
183  if (p != -1.0 && theta != -999.0) {
184  ji = findBin(m_categories, theta, p);
185  m_categories->GetBinXYZ(ji, x, y, z);
186  }
187  if (ji > 0 && ji != pair1.first) continue;
188 
189  std::cout << "\tglobal_bin_idx: " << pair1.first << " (" << x << "," << y << ")" << std::endl;
190 
191  for (const auto& pair2 : pair1.second) {
192  if (varid != InputVar::c_NONE && varid != pair2.first) continue;
193  std::cout << "\t\tvarid: " << static_cast<unsigned int>(varid) << ", TF1: " << pair2.second->GetName() << std::endl;
194  }
195 
196  }
197  }
198  }
199 
202  void setEnergyUnit(const double& unit) { m_energy_unit.SetVal(unit); }
203 
206  void setAngularUnit(const double& unit) { m_ang_unit.SetVal(unit); }
207 
212  void setPdfCategories(TH2F* h)
213  {
214  m_categories = h;
215  }
216 
220  const TH2F* getPdfCategories() const
221  {
222  return m_categories;
223  }
224 
234  void add(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const InputVar varid, TF1* pdf)
235  {
236 
237  auto ji = m_categories->GetBin(j, i);
238 
239  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
240 
241  m_pdfsmap[signed_pdg][ji][varid] = pdf;
242 
243  }
244 
253  const TF1* getPdf(const unsigned int pdg, const int charge, const double& p, const double& theta, const InputVar varid) const
254  {
255 
256  const int signed_pdg = pdg * charge / std::abs(charge);
257 
258  double pp = p / m_energy_unit.GetVal();
259  double th = TMath::Abs(theta) / m_ang_unit.GetVal();
260 
261  int gbin = findBin(m_categories, th, pp);
262 
263  int x, y, z;
264  m_categories->GetBinXYZ(gbin, x, y, z);
265 
266  B2DEBUG(30, "\t\tAngular unit: " << m_ang_unit.GetVal());
267  B2DEBUG(30, "\t\tEnergy unit: " << m_energy_unit.GetVal());
268  B2DEBUG(30, "\t\t|pdgId| * reco_charge = " << signed_pdg << ", clusterTheta = " << th << ", p = " << pp);
269  B2DEBUG(30, "\t\tgbin = " << gbin << ", x,y = (" << x << "," << y << ")");
270  B2DEBUG(30, "\t\tvariable id = " << static_cast<unsigned int>(varid));
271 
272  return m_pdfsmap.at(signed_pdg).at(gbin).at(varid);
273  }
274 
278  inline bool doVarsTransfo() const { return m_do_varstransform; }
279 
293  void storeVarsTransfoSettings(const unsigned int pdg,
294  const int true_charge,
295  const unsigned int i, const unsigned int j,
296  const int nVars,
297  const std::string& classPath = "",
298  const std::vector<int>& nDivisions = std::vector<int>(),
299  const std::vector<double>& cumulDist = std::vector<double>(),
300  const std::vector<double>& x = std::vector<double>(),
301  const std::vector<double>& covMatrix = std::vector<double>())
302  {
303 
304  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
305 
306  m_do_varstransform = true;
307 
308  VarTransfoSettings vts;
309 
310  vts.nVars = nVars;
311  vts.classPath = classPath;
312  vts.nDivisionsMax = (!nDivisions.empty()) ? *(std::max_element(std::begin(nDivisions), std::end(nDivisions))) : 0;
313  vts.nDivisions = nDivisions;
314  vts.cumulDist = cumulDist;
315  vts.x = x;
316  vts.covMatrix = covMatrix;
317 
318  auto ji = m_categories->GetBin(j, i);
319  vts.gbin = ji;
320  vts.ip = i;
321  vts.jth = j;
322 
323  m_vtsmap_bycategory[ji] = vts;
324 
325  m_vtsmap[signed_pdg] = m_vtsmap_bycategory;
326 
327  }
328 
336  const VarTransfoSettings* getVTS(const unsigned int pdg, const int charge, const double& p, const double& theta) const
337  {
338 
339  const int signed_pdg = pdg * charge / std::abs(charge);
340 
341  auto gbin = findBin(m_categories, theta, p);
342 
343  return &(m_vtsmap.at(signed_pdg).at(gbin));
344 
345  }
346 
347  private:
348 
356  int findBin(const TH2F* h, const double& x, const double& y) const
357  {
358 
359  int nbinsx_vis = h->GetXaxis()->GetNbins();
360  int nbinsy_vis = h->GetYaxis()->GetNbins();
361 
362  double xx = x;
363  double yy = y;
364 
365  // If x, y are outside of the 2D hogram grid (visible) range, set their value to
366  // fall in the last (first) bin before (after) overflow (underflow).
367  if (x < h->GetXaxis()->GetBinLowEdge(1)) { xx = h->GetXaxis()->GetBinCenter(1); }
368  if (x >= h->GetXaxis()->GetBinLowEdge(nbinsx_vis + 1)) { xx = h->GetXaxis()->GetBinCenter(nbinsx_vis); }
369  if (y < h->GetYaxis()->GetBinLowEdge(1)) { yy = h->GetYaxis()->GetBinCenter(1); }
370  if (y >= h->GetYaxis()->GetBinLowEdge(nbinsy_vis + 1)) { yy = h->GetYaxis()->GetBinCenter(nbinsy_vis); }
371 
372  int nbinsx = h->GetXaxis()->GetNbins() + 2;
373  int j = h->GetXaxis()->FindBin(xx);
374  int i = h->GetYaxis()->FindBin(yy);
375 
376  return j + nbinsx * i;
377  }
378 
379  private:
380 
381  TParameter<double> m_energy_unit;
382  TParameter<double> m_ang_unit;
388  TH2F* m_categories = nullptr;
389 
396 
405 
410  bool m_do_varstransform = false;
411 
418 
425 
432 
439 
447 
454  };
456 } // end namespace Belle2
Belle2::ECLChargedPidPDFs::doVarsTransfo
bool doVarsTransfo() const
Check whether variables transformation is applied.
Definition: ECLChargedPidPDFs.h:288
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:216
Belle2::ECLChargedPidPDFs::VarTransfoSettings::jth
unsigned int jth
p bin index
Definition: ECLChargedPidPDFs.h:116
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:157
Belle2::ECLChargedPidPDFs::VarTransfoSettings::classPath
std::string classPath
Number of variables.
Definition: ECLChargedPidPDFs.h:108
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:212
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:244
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:398
Belle2::ECLChargedPidPDFs::VariablesMapByParticle
std::unordered_map< int, VariablesMapByCategory > VariablesMapByParticle
Typedef.
Definition: ECLChargedPidPDFs.h:124
Belle2::ECLChargedPidPDFs::~ECLChargedPidPDFs
~ECLChargedPidPDFs()
Destructor.
Definition: ECLChargedPidPDFs.h:58
Belle2::ECLChargedPidPDFs::m_pdfsmap
PdfsMapByParticle m_pdfsmap
Internal map.
Definition: ECLChargedPidPDFs.h:427
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:263
Belle2::ECLChargedPidPDFs::VarTransfoSettings::gbin
unsigned int gbin
theta bin index
Definition: ECLChargedPidPDFs.h:117
Belle2::ECLChargedPidPDFs::m_vtsmap_bycategory
VTSMapByCategory m_vtsmap_bycategory
Internal map.
Definition: ECLChargedPidPDFs.h:456
Belle2::ECLChargedPidPDFs::getPdfCategories
const TH2F * getPdfCategories() const
Get the 2D (clusterTheta, p) grid representing the categories for which PDFs are defined.
Definition: ECLChargedPidPDFs.h:230
Belle2::ECLChargedPidPDFs::m_energy_unit
TParameter< double > m_energy_unit
The energy unit used for the binning.
Definition: ECLChargedPidPDFs.h:391
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:346
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:366
Belle2::ECLChargedPidPDFs::ClassDef
ClassDef(ECLChargedPidPDFs, 2)
ClassDef.
Belle2::ECLChargedPidPDFs::VarTransfoSettings::covMatrix
std::vector< double > covMatrix
Variable value at each step.
Definition: ECLChargedPidPDFs.h:113
Belle2::ECLChargedPidPDFs::m_ang_unit
TParameter< double > m_ang_unit
The angular unit used for the binning.
Definition: ECLChargedPidPDFs.h:392
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:171
Belle2::ECLChargedPidPDFs::m_variablesmap
VariablesMapByParticle m_variablesmap
Internal map.
Definition: ECLChargedPidPDFs.h:405
Belle2::ECLChargedPidPDFs::m_pdfs_byvariable
PdfsByVariable m_pdfs_byvariable
Internal map.
Definition: ECLChargedPidPDFs.h:441
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:109
Belle2::ECLChargedPidPDFs::VTSMapByCategory
std::unordered_map< int, VarTransfoSettings > VTSMapByCategory
Typedef.
Definition: ECLChargedPidPDFs.h:126
Belle2::ECLChargedPidPDFs::m_variablesmap_bycategory
VariablesMapByCategory m_variablesmap_bycategory
Internal map.
Definition: ECLChargedPidPDFs.h:414
Belle2::ECLChargedPidPDFs::VarTransfoSettings
Class to hold parameters needed to perform pre-processing of input variables (e.g....
Definition: ECLChargedPidPDFs.h:99
Belle2::ECLChargedPidPDFs::m_pdfsmap_bycategory
PdfsMapByCategory m_pdfsmap_bycategory
Internal map.
Definition: ECLChargedPidPDFs.h:434
Belle2::ECLChargedPidPDFs::m_vtsmap
VTSMapByParticle m_vtsmap
Internal map.
Definition: ECLChargedPidPDFs.h:448
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:303
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::VarTransfoSettings::VarTransfoSettings
VarTransfoSettings()
Constructor.
Definition: ECLChargedPidPDFs.h:103
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:137
Belle2::ECLChargedPidPDFs::InputVar::c_E9E21
@ c_E9E21
E9/E21.
Belle2::ECLChargedPidPDFs::VTSMapByParticle
std::unordered_map< int, VTSMapByCategory > VTSMapByParticle
Typedef.
Definition: ECLChargedPidPDFs.h:127
Belle2::ECLChargedPidPDFs::PdfsMapByParticle
std::unordered_map< int, PdfsMapByCategory > PdfsMapByParticle
Typedef.
Definition: ECLChargedPidPDFs.h:122
Belle2::ECLChargedPidPDFs::VarTransfoSettings::nDivisionsMax
int nDivisionsMax
Number of steps in which each variable range is sub-divided.
Definition: ECLChargedPidPDFs.h:110
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:420
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:115
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:121
Belle2::ECLChargedPidPDFs::VarTransfoSettings::~VarTransfoSettings
~VarTransfoSettings()
Destructor.
Definition: ECLChargedPidPDFs.h:105
Belle2::ECLChargedPidPDFs::VariablesMapByCategory
std::unordered_map< int, std::vector< InputVar > > VariablesMapByCategory
Typedef.
Definition: ECLChargedPidPDFs.h:123
Belle2::ECLChargedPidPDFs::VarTransfoSettings::cumulDist
std::vector< double > cumulDist
Maximal number of steps, across all variables.
Definition: ECLChargedPidPDFs.h:111
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:222
Belle2::ECLChargedPidPDFs::PdfsByVariable
std::unordered_map< InputVar, TF1 * > PdfsByVariable
Typedef.
Definition: ECLChargedPidPDFs.h:120
Belle2::ECLChargedPidPDFs::VarTransfoSettings::x
std::vector< double > x
Cumulative density function at each step.
Definition: ECLChargedPidPDFs.h:112