Belle II Software  release-08-01-10
chargedParticleIdentificator.cc
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 #include <framework/logging/Logger.h>
10 #include <framework/gearbox/Const.h>
11 #include <framework/gearbox/Unit.h>
12 
13 #include <analysis/dbobjects/ChargedPidMVAWeights.h>
14 
15 #include <gtest/gtest.h>
16 #include <random>
17 
18 #include <TH2F.h>
19 
20 namespace Belle2 {
27  class ChargedParticleIdentificatorTest : public ::testing::Test {
28 
29  public:
30 
35 
40 
44  std::vector<double> m_thetabins = {0.2164208, 0.5480334, 0.561996, 2.2462387, 2.2811453, 2.7070057};
45 
49  std::vector<double> m_pbins = {0.2, 0.6, 0.75, 1.0, 3.0, 7.0};
50 
54  std::vector<double> m_chbins = { -1.5, -0.5, 0.5, 1.5};
55 
59  std::string m_basename = "dummy_weightfile";
60 
66  std::vector<std::string> m_dummyfiles;
67 
68  protected:
69 
73  void SetUp() override
74  {
75 
77  m_pbins.data(), m_pbins.size() - 1,
78  m_chbins.data(), m_chbins.size() - 1);
79 
80  std::vector<std::tuple<double, double, double>> gridBinCentres;
81 
82  for (unsigned int kch(0); kch < m_chbins.size() - 1; kch++) {
83  auto ch_bin_centre = (m_chbins.at(kch) + m_chbins.at(kch + 1)) / 2.0;
84  for (unsigned int ip(0); ip < m_pbins.size() - 1; ip++) {
85  auto p_bin_centre = (m_pbins.at(ip) + m_pbins.at(ip + 1)) / 2.0;
86  for (unsigned int jth(0); jth < m_thetabins.size() - 1; jth++) {
87  auto th_bin_centre = (m_thetabins.at(jth) + m_thetabins.at(jth + 1)) / 2.0;
88  auto fname = m_basename
89  + "__clusterTheta__" + std::to_string(m_thetabins.at(jth)) + "_" + std::to_string(m_thetabins.at(jth + 1))
90  + "__p__" + std::to_string(m_pbins.at(ip)) + "_" + std::to_string(m_pbins.at(ip + 1))
91  + "__charge__" + std::to_string(ch_bin_centre);
92 
93  std::replace(fname.begin(), fname.end(), '.', '_');
94  fname += ".xml";
95  std::ofstream dummyfile(fname);
96  dummyfile.close();
97  m_dummyfiles.push_back(fname);
98 
99  auto centre = std::make_tuple(th_bin_centre, p_bin_centre, ch_bin_centre);
100  gridBinCentres.push_back(centre);
101  }
102  }
103  }
105 
106  }
107 
111  void TearDown() override
112  {
113 
114  // Delete all dummy files.
115  for (const auto& fname : m_dummyfiles) {
116  if (remove(fname.c_str())) {
117  B2ERROR("Couldn't remove file: " << fname);
118  }
119  }
120 
121  }
122 
123  };
124 
130  {
131 
132  // Pick a random index in the clusterTheta, p, charge bins arrays.
133  // Exclude underflow and overflow.
134  std::random_device rd; // non-deterministic uniform int rand generator.
135  std::uniform_int_distribution<int> binx_idx_distr(1, m_thetabins.size() - 1);
136  std::uniform_int_distribution<int> biny_idx_distr(1, m_pbins.size() - 1);
137  std::uniform_int_distribution<int> binz_idx_distr(1, m_chbins.size() - 1);
138  int binx = binx_idx_distr(rd);
139  int biny = biny_idx_distr(rd);
140  int binz = binz_idx_distr(rd);
141 
142  // Pick each axis' bin centre as a test value for (clusterTheta, p, charge).
143  auto theta = m_dbrep.getWeightCategories()->GetXaxis()->GetBinCenter(binx);
144  auto p = m_dbrep.getWeightCategories()->GetYaxis()->GetBinCenter(biny);
145  auto charge = m_dbrep.getWeightCategories()->GetZaxis()->GetBinCenter(binz);
146 
147  int jth, ip, kch;
148  auto jik = m_dbrep.getMVAWeightIdx(theta, p, charge, jth, ip, kch);
149 
150  EXPECT_EQ(jth, binx);
151  EXPECT_EQ(ip, biny);
152  EXPECT_EQ(kch, binz);
153 
154  auto thisfname = m_basename
155  + "__clusterTheta__" + std::to_string(m_thetabins.at(jth - 1)) + "_" + std::to_string(m_thetabins.at(jth))
156  + "__p__" + std::to_string(m_pbins.at(ip - 1)) + "_" + std::to_string(m_pbins.at(ip))
157  + "__charge__" + std::to_string(charge);
158  std::replace(thisfname.begin(), thisfname.end(), '.', '_');
159  thisfname += ".xml";
160 
161  EXPECT_EQ(thisfname, m_dummyfiles.at(jik));
162 
163  auto matchitr = std::find(m_dummyfiles.begin(), m_dummyfiles.end(), thisfname);
164  auto thisidx = std::distance(m_dummyfiles.begin(), matchitr);
165 
166  EXPECT_EQ(thisidx, jik);
167 
168  }
169 
171 } // namespace
Const::ChargedStable m_testHypo
The signal charged particle hypothesis to test.
std::vector< double > m_chbins
The charge bin edges.
void SetUp() override
Prepare resources for the tests.
std::vector< double > m_pbins
The p bin edges in [GeV/c].
std::string m_basename
Base common name for all dummy weight files.
ChargedPidMVAWeights m_dbrep
Database representation of MVA weightfiles.
void TearDown() override
Release all resources.
std::vector< double > m_thetabins
The clusterTheta bin edges in [rad].
std::vector< std::string > m_dummyfiles
List of dummy xml file names.
Class to contain the payload of MVA weightfiles needed for charged particle identification.
void setWeightCategories(const double *clusterThetaBins, const int nClusterThetaBins, const double *pBins, const int nPBins, const double *chargeBins, const int nChargeBins)
Set the 3D (clusterTheta, p, charge) grid representing the categories for which weightfiles are defin...
void storeMVAWeights(const int pdg, const std::vector< std::string > &filepaths, const std::vector< std::tuple< double, double, double >> &categoryBinCentres)
Given a particle mass hypothesis' pdgId, store the list of MVA weight files (one for each category) i...
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:580
int getPDGCode() const
PDG code.
Definition: Const.h:464
static const ChargedStable electron
electron particle
Definition: Const.h:650
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:72
Abstract base class for different kinds of events.