Belle II Software  release-05-01-25
eclChargedPID.cc
1 #include <framework/gearbox/Const.h>
2 #include <framework/gearbox/Unit.h>
3 #include <framework/datastore/StoreArray.h>
4 
5 #include <ecl/dataobjects/ECLPidLikelihood.h>
6 #include <ecl/dbobjects/ECLChargedPidPDFs.h>
7 
8 #include <utility>
9 #include <gtest/gtest.h>
10 
11 #include <TF1.h>
12 
13 namespace Belle2 {
20  class ECLChargedPIDTest : public ::testing::Test {
21 
22  protected:
24  virtual void SetUp()
25  {
27 
28  StoreArray<ECLPidLikelihood> ecl_likelihoods;
29  ecl_likelihoods.registerInDataStore();
30 
31  StoreArray<ECLPidLikelihood> ecl_likelihoods_plus;
32  ecl_likelihoods_plus.registerInDataStore();
33 
34  StoreArray<ECLPidLikelihood> ecl_likelihoods_minus;
35  ecl_likelihoods_minus.registerInDataStore();
36 
38  }
39 
41  virtual void TearDown()
42  {
44  }
45 
46  };
47 
49  TEST_F(ECLChargedPIDTest, ECLPidLikelihoodSettersAndGetters)
50  {
51 
52  StoreArray<ECLPidLikelihood> ecl_likelihoods;
53 
54  auto* lk = ecl_likelihoods.appendNew();
55 
57  lk->setLogLikelihood(Const::muon, 0.58);
58  lk->setLogLikelihood(Const::pion, 0.28);
59  lk->setLogLikelihood(Const::kaon, 0.38);
60  lk->setLogLikelihood(Const::proton, 0.48);
61 
62  EXPECT_FLOAT_EQ(lk->getLogLikelihood(Const::electron), 0.12);
63  EXPECT_FLOAT_EQ(lk->getLogLikelihood(Const::muon), 0.58);
64  EXPECT_FLOAT_EQ(lk->getLogLikelihood(Const::pion), 0.28);
65  EXPECT_FLOAT_EQ(lk->getLogLikelihood(Const::kaon), 0.38);
66  EXPECT_FLOAT_EQ(lk->getLogLikelihood(Const::proton), 0.48);
67 
68  } // Testcases for ECLPidLikelihood setters and getters.
69 
71  TEST_F(ECLChargedPIDTest, TestECLPdfs)
72  {
73 
74  ECLChargedPidPDFs eclPdfs;
75 
76  float pmin_vals[] = {0.2, 0.6, 1.0, 100.0};
77  float thetamin_vals[] = {0.21, 0.56, 2.24, 2.70};
78 
79  TH2F histgrid("binsgrid",
80  "bins grid",
81  sizeof(thetamin_vals) / sizeof(float) - 1,
82  thetamin_vals,
83  sizeof(pmin_vals) / sizeof(float) - 1,
84  pmin_vals);
85 
86  eclPdfs.setEnergyUnit(Unit::GeV);
87  eclPdfs.setAngularUnit(Unit::rad);
88 
89  eclPdfs.setPdfCategories(&histgrid);
90 
91  // Create a set of fictitious (normalised) PDFs of E/p, for various signed particle hypotheses.
92  // The first element in the pair is the true charge.
93 
94  typedef std::pair<int, TF1> pdfSet;
95 
96  pdfSet pdf_el = std::make_pair(-1, TF1("pdf_el", "TMath::Gaus(x, 1.0, 0.2, true)", 0.0, 1.3));
97  pdfSet pdf_elanti = std::make_pair(1, TF1("pdf_elanti", "TMath::Gaus(x, 0.9, 0.2, true)", 0.0, 1.2));
98 
99  pdfSet pdf_mu = std::make_pair(-1, TF1("pdf_mu", "TMath::Gaus(x, 0.3, 0.2, true)", 0.0, 1.0));
100  pdfSet pdf_muanti = std::make_pair(1, TF1("pdf_muanti", "TMath::Gaus(x, 0.35, 0.22, true)", 0.0, 1.0));
101 
102  pdfSet pdf_pi = std::make_pair(1, TF1("pdf_pi", "TMath::Gaus(x, 0.4, 0.1, true)", 0.0, 1.0));
103  pdfSet pdf_pianti = std::make_pair(-1, TF1("pdf_pianti", "TMath::Gaus(x, 0.38, 0.15, true)", 0.0, 1.0));
104 
105  pdfSet pdf_k = std::make_pair(1, TF1("pdf_k", "TMath::Gaus(x, 0.38, 0.2, true)", 0.0, 1.0));
106  pdfSet pdf_kanti = std::make_pair(-1, TF1("pdf_kanti", "TMath::Gaus(x, 0.5, 0.22, true)", 0.0, 1.0));
107 
108  pdfSet pdf_p = std::make_pair(1, TF1("pdf_p", "TMath::Gaus(x, 1.0, 0.4, true)", 0.0, 1.6));
109  pdfSet pdf_panti = std::make_pair(-1, TF1("pdf_panti", "TMath::Gaus(x, 1.3, 0.5, true)", 0.0, 2.0));
110 
111  pdfSet pdf_d = std::make_pair(1, TF1("pdf_d", "TMath::Gaus(x, 1.1, 0.45, true)", 0.0, 1.6));
112  pdfSet pdf_danti = std::make_pair(-1, TF1("pdf_danti", "TMath::Gaus(x, 1.2, 0.6, true)", 0.0, 2.0));
113 
114  std::map<unsigned int, std::vector<pdfSet>> pdfs = {
115  {Const::electron.getPDGCode(), std::vector<pdfSet>{pdf_el, pdf_elanti}},
116  {Const::muon.getPDGCode(), std::vector<pdfSet>{pdf_mu, pdf_muanti}},
117  {Const::pion.getPDGCode(), std::vector<pdfSet>{pdf_pi, pdf_pianti}},
118  {Const::kaon.getPDGCode(), std::vector<pdfSet>{pdf_k, pdf_kanti}},
119  {Const::proton.getPDGCode(), std::vector<pdfSet>{pdf_p, pdf_panti}},
120  {Const::deuteron.getPDGCode(), std::vector<pdfSet>{pdf_d, pdf_danti}},
121  };
122 
123  // E/p is the only variable considered in the test.
124  std::vector<ECLChargedPidPDFs::InputVar> varids = {ECLChargedPidPDFs::InputVar::c_EoP};
125 
126  // Fill the DB PDF map.
127  // (Use the same PDF for all (clusterTheta, p) bins for simplicity).
128  for (auto& [pdg, pdf_setlist] : pdfs) {
129  for (auto& pdf_set : pdf_setlist) {
130  for (int ip(1); ip <= histgrid.GetNbinsY(); ++ip) {
131  for (int jth(1); jth <= histgrid.GetNbinsX(); ++jth) {
132  for (const auto& varid : varids) {
133  eclPdfs.add(pdg, pdf_set.first, ip, jth, varid, &pdf_set.second);
134  }
135  }
136  }
137  }
138  }
139 
140  // Store the value of the PDFs in here, and pass it to the ECLPidLikelihood object later on.
141  float likelihoods_plus[Const::ChargedStable::c_SetSize];
142  float likelihoods_minus[Const::ChargedStable::c_SetSize];
143 
144  // Choose an arbitrary set of (clusterTheta, p), and of E/p.
145  double clusterTheta = 1.047; // (X) --> clusterTheta [rad] = 60 [deg]
146  double p = 0.85; // (Y) --> P [GeV] = 850 [MeV]
147  double eop = 0.87;
148 
149  // Now read the PDFs.
150  for (const auto& [pdg, pdf_setlist] : pdfs) {
151  for (const auto& pdf_set : pdf_setlist) {
152  float pdfval = eclPdfs.getPdf(pdg, pdf_set.first, p, clusterTheta, varids.at(0))->Eval(eop);
153  EXPECT_NEAR(pdf_set.second.Eval(eop), pdfval, 0.001);
154  if (pdf_set.first < 0) {
155  likelihoods_minus[Const::chargedStableSet.find(pdg).getIndex()] = log(pdfval);
156  } else {
157  likelihoods_plus[Const::chargedStableSet.find(pdg).getIndex()] = log(pdfval);
158  }
159  }
160  }
161 
162  // Set the ECL likelihood dataobjects
163  StoreArray<ECLPidLikelihood> ecl_likelihoods_minus;
164  const auto* lk_minus = ecl_likelihoods_minus.appendNew(likelihoods_minus);
165  StoreArray<ECLPidLikelihood> ecl_likelihoods_plus;
166  const auto* lk_plus = ecl_likelihoods_plus.appendNew(likelihoods_plus);
167 
168  for (const auto& [pdg, pdf_setlist] : pdfs) {
169  for (const auto& pdf_set : pdf_setlist) {
170  float logl;
171  float logl_expect = log(pdf_set.second.Eval(eop));
172  if (pdf_set.first < 0) {
173  logl = lk_minus->getLogLikelihood(Const::chargedStableSet.find(pdg));
174  } else {
175  logl = lk_plus->getLogLikelihood(Const::chargedStableSet.find(pdg));
176  }
177  EXPECT_NEAR(logl_expect, logl, 0.01);
178  }
179  }
180 
181  } // Testcases for ECL PDFs.
182 
184 } // namespace
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
Belle2::Const::ChargedStable::c_SetSize
static const unsigned int c_SetSize
Number of elements (for use in array bounds etc.)
Definition: Const.h:491
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::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::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::Const::electron
static const ChargedStable electron
electron particle
Definition: Const.h:533
Belle2::DataStore::setInitializeActive
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:94
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::Const::chargedStableSet
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:494
Belle2::Const::ParticleType::getPDGCode
int getPDGCode() const
PDG code.
Definition: Const.h:349
Belle2::ECLChargedPidPDFs::InputVar::c_EoP
@ c_EoP
E/p.
Belle2::Const::kaon
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:536
Belle2::DataStore::reset
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
Definition: DataStore.cc:86
Belle2::Unit::rad
static const double rad
Standard of [angle].
Definition: Unit.h:60
Belle2::Const::pion
static const ChargedStable pion
charged pion particle
Definition: Const.h:535
Belle2::Const::ParticleSet::find
const ParticleType & find(int pdg) const
Returns particle in set with given PDG code, or invalidParticle if not found.
Definition: Const.h:447
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Const::deuteron
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:538
Belle2::ECLChargedPIDTest::TearDown
virtual void TearDown()
Clear datastore.
Definition: eclChargedPID.cc:41
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65
Belle2::ECLPidLikelihood::setLogLikelihood
void setLogLikelihood(const Const::ChargedStable &type, float logl)
corresponding setter for m_logl.
Definition: ECLPidLikelihood.h:78
Belle2::ECLChargedPIDTest
Test the ECL charged PID.
Definition: eclChargedPID.cc:20
Belle2::Const::proton
static const ChargedStable proton
proton particle
Definition: Const.h:537
Belle2::ECLChargedPidPDFs
Class representing the DB payload w/ information about ECL PDF parameters for a set of particle hypot...
Definition: ECLChargedPidPDFs.h:44
Belle2::ECLChargedPIDTest::SetUp
virtual void SetUp()
Set up a few arrays and objects in the datastore.
Definition: eclChargedPID.cc:24
Belle2::Const::muon
static const ChargedStable muon
muon particle
Definition: Const.h:534
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::Unit::GeV
static const double GeV
Standard of [energy, momentum, mass].
Definition: Unit.h:61
Belle2::Const::ParticleType::getIndex
int getIndex() const
This particle's index in the associated set.
Definition: Const.h:337
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