Belle II Software  release-06-01-15
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 
21 namespace 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 
76 
77  static const int iNaN = std::numeric_limits<int>::quiet_NaN();
78  static const int uNaN = std::numeric_limits<unsigned>::quiet_NaN();
79 
86  public:
87 
89  VarTransfoSettings() : nVars(iNaN), nDivisionsMax(iNaN), ip(uNaN), jth(uNaN), gbin(uNaN) {}
92 
93  int nVars;
94  std::string classPath;
95  std::vector<int> nDivisions;
97  std::vector<double> cumulDist;
98  std::vector<double> x;
99  std::vector<double> covMatrix;
101  unsigned int ip;
102  unsigned int jth;
103  unsigned int gbin;
104  };
105 
106  typedef std::unordered_map<InputVar, TF1*> PdfsByVariable;
107  typedef std::unordered_map<int, PdfsByVariable > PdfsMapByCategory;
108  typedef std::unordered_map<int, PdfsMapByCategory > PdfsMapByParticle;
109  typedef std::unordered_map<int, std::vector<InputVar> > VariablesMapByCategory;
110  typedef std::unordered_map<int, VariablesMapByCategory > VariablesMapByParticle;
112  typedef std::unordered_map<int, VarTransfoSettings> VTSMapByCategory;
113  typedef std::unordered_map<int, VTSMapByCategory > VTSMapByParticle;
123  void setVars(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j,
124  const std::vector<InputVar>& vars)
125  {
126  auto ji = m_categories->GetBin(j, i);
127 
128  m_variablesmap_bycategory[ji] = vars;
129 
130  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
131 
133 
134  }
135 
143  const std::vector<InputVar>* getVars(const unsigned int pdg, const int charge, const double& p, const double& theta) const
144  {
145 
146  auto gbin = findBin(m_categories, theta, p);
147 
148  const int signed_pdg = pdg * charge / std::abs(charge);
149 
150  return &(m_variablesmap.at(signed_pdg).at(gbin));
151 
152  }
153 
157  void printPdfMap(const unsigned int pdg = 0,
158  const double& p = -1.0, const double& theta = -999.0,
159  const int true_charge = 1,
160  const InputVar varid = InputVar::c_NONE) const
161  {
162 
163  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
164 
165  std::cout << "Printing PDF info: " << std::endl;
166  if (pdg) std::cout << "-) |pdgId| * true_charge = " << signed_pdg << std::endl;
167  if (p != -1.0) std::cout << "-) p = " << p << " [GeV/c]" << std::endl;
168  if (theta != -999.0) std::cout << "-) clusterTheta = " << theta << " [rad]" << std::endl;
169  if (varid != InputVar::c_NONE) std::cout << "-) varid = " << static_cast<unsigned int>(varid) << std::endl;
170 
171  int x, y, z;
172  for (const auto& pair0 : m_pdfsmap) {
173 
174  if (signed_pdg && signed_pdg != pair0.first) continue;
175 
176  for (const auto& pair1 : pair0.second) {
177 
178  auto ji(-1);
179  if (p != -1.0 && theta != -999.0) {
180  ji = findBin(m_categories, theta, p);
181  m_categories->GetBinXYZ(ji, x, y, z);
182  }
183  if (ji > 0 && ji != pair1.first) continue;
184 
185  std::cout << "\tglobal_bin_idx: " << pair1.first << " (" << x << "," << y << ")" << std::endl;
186 
187  for (const auto& pair2 : pair1.second) {
188  if (varid != InputVar::c_NONE && varid != pair2.first) continue;
189  std::cout << "\t\tvarid: " << static_cast<unsigned int>(varid) << ", TF1: " << pair2.second->GetName() << std::endl;
190  }
191 
192  }
193  }
194  }
195 
198  void setEnergyUnit(const double& unit) { m_energy_unit.SetVal(unit); }
199 
202  void setAngularUnit(const double& unit) { m_ang_unit.SetVal(unit); }
203 
208  void setPdfCategories(TH2F* h)
209  {
210  m_categories = h;
211  }
212 
216  const TH2F* getPdfCategories() const
217  {
218  return m_categories;
219  }
220 
230  void add(const unsigned int pdg, const int true_charge, const unsigned int i, const unsigned int j, const InputVar varid, TF1* pdf)
231  {
232 
233  auto ji = m_categories->GetBin(j, i);
234 
235  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
236 
237  m_pdfsmap[signed_pdg][ji][varid] = pdf;
238 
239  }
240 
249  const TF1* getPdf(const unsigned int pdg, const int charge, const double& p, const double& theta, const InputVar varid) const
250  {
251 
252  const int signed_pdg = pdg * charge / std::abs(charge);
253 
254  double pp = p / m_energy_unit.GetVal();
255  double th = TMath::Abs(theta) / m_ang_unit.GetVal();
256 
257  int gbin = findBin(m_categories, th, pp);
258 
259  int x, y, z;
260  m_categories->GetBinXYZ(gbin, x, y, z);
261 
262  B2DEBUG(30, "\t\tAngular unit: " << m_ang_unit.GetVal());
263  B2DEBUG(30, "\t\tEnergy unit: " << m_energy_unit.GetVal());
264  B2DEBUG(30, "\t\t|pdgId| * reco_charge = " << signed_pdg << ", clusterTheta = " << th << ", p = " << pp);
265  B2DEBUG(30, "\t\tgbin = " << gbin << ", x,y = (" << x << "," << y << ")");
266  B2DEBUG(30, "\t\tvariable id = " << static_cast<unsigned int>(varid));
267 
268  return m_pdfsmap.at(signed_pdg).at(gbin).at(varid);
269  }
270 
274  inline bool doVarsTransfo() const { return m_do_varstransform; }
275 
289  void storeVarsTransfoSettings(const unsigned int pdg,
290  const int true_charge,
291  const unsigned int i, const unsigned int j,
292  const int nVars,
293  const std::string& classPath = "",
294  const std::vector<int>& nDivisions = std::vector<int>(),
295  const std::vector<double>& cumulDist = std::vector<double>(),
296  const std::vector<double>& x = std::vector<double>(),
297  const std::vector<double>& covMatrix = std::vector<double>())
298  {
299 
300  const int signed_pdg = pdg * true_charge / std::abs(true_charge);
301 
302  m_do_varstransform = true;
303 
304  VarTransfoSettings vts;
305 
306  vts.nVars = nVars;
307  vts.classPath = classPath;
308  vts.nDivisionsMax = (!nDivisions.empty()) ? *(std::max_element(std::begin(nDivisions), std::end(nDivisions))) : 0;
309  vts.nDivisions = nDivisions;
310  vts.cumulDist = cumulDist;
311  vts.x = x;
312  vts.covMatrix = covMatrix;
313 
314  auto ji = m_categories->GetBin(j, i);
315  vts.gbin = ji;
316  vts.ip = i;
317  vts.jth = j;
318 
319  m_vtsmap_bycategory[ji] = vts;
320 
321  m_vtsmap[signed_pdg] = m_vtsmap_bycategory;
322 
323  }
324 
332  const VarTransfoSettings* getVTS(const unsigned int pdg, const int charge, const double& p, const double& theta) const
333  {
334 
335  const int signed_pdg = pdg * charge / std::abs(charge);
336 
337  auto gbin = findBin(m_categories, theta, p);
338 
339  return &(m_vtsmap.at(signed_pdg).at(gbin));
340 
341  }
342 
343  private:
344 
352  int findBin(const TH2F* h, const double& x, const double& y) const
353  {
354 
355  int nbinsx_vis = h->GetXaxis()->GetNbins();
356  int nbinsy_vis = h->GetYaxis()->GetNbins();
357 
358  double xx = x;
359  double yy = y;
360 
361  // If x, y are outside of the 2D hogram grid (visible) range, set their value to
362  // fall in the last (first) bin before (after) overflow (underflow).
363  if (x < h->GetXaxis()->GetBinLowEdge(1)) { xx = h->GetXaxis()->GetBinCenter(1); }
364  if (x >= h->GetXaxis()->GetBinLowEdge(nbinsx_vis + 1)) { xx = h->GetXaxis()->GetBinCenter(nbinsx_vis); }
365  if (y < h->GetYaxis()->GetBinLowEdge(1)) { yy = h->GetYaxis()->GetBinCenter(1); }
366  if (y >= h->GetYaxis()->GetBinLowEdge(nbinsy_vis + 1)) { yy = h->GetYaxis()->GetBinCenter(nbinsy_vis); }
367 
368  int nbinsx = h->GetXaxis()->GetNbins() + 2;
369  int j = h->GetXaxis()->FindBin(xx);
370  int i = h->GetYaxis()->FindBin(yy);
371 
372  return j + nbinsx * i;
373  }
374 
375  private:
376 
377  TParameter<double> m_energy_unit;
378  TParameter<double> m_ang_unit;
384  TH2F* m_categories = nullptr;
385 
392 
401 
406  bool m_do_varstransform = false;
407 
414 
421 
428 
435 
443 
450  };
452 } // end namespace Belle2
Class to hold parameters needed to perform pre-processing of input variables (e.g....
std::vector< double > cumulDist
Maximal number of steps, across all variables.
int nDivisionsMax
Number of steps in which each variable range is sub-divided.
std::vector< double > x
Cumulative density function at each step.
std::string classPath
Number of variables.
std::vector< int > nDivisions
Path of the class used to get the variables transfo.
std::vector< double > covMatrix
Variable value at each step.
unsigned int ip
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.
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.
const VarTransfoSettings * getVTS(const unsigned int pdg, const int charge, const double &p, const double &theta) const
Getter for variable transformation settings.
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.
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...
VariablesMapByCategory m_variablesmap_bycategory
Internal map.
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.
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),...
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...
const TH2F * getPdfCategories() const
Get the 2D (clusterTheta, p) grid representing the categories for which PDFs are defined.
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.
PdfsByVariable m_pdfs_byvariable
Internal map.
The Unit class.
Definition: Unit.h:40
Abstract base class for different kinds of events.