Belle II Software  release-06-01-15
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::unique_ptr<TH3F> m_grid;
45 
49  std::vector<float> m_thetabins = {0.2164208, 0.5480334, 0.561996, 2.2462387, 2.2811453, 2.7070057};
50 
54  std::vector<float> m_pbins = {0.2, 0.6, 0.75, 1.0, 3.0, 7.0};
55 
59  std::vector<float> m_chbins = { -1.5, -0.5, 0.5, 1.5};
60 
64  std::string m_basename = "dummy_weightfile";
65 
71  std::vector<std::string> m_dummyfiles;
72 
73  protected:
74 
78  void SetUp() override
79  {
80 
81  m_grid = std::make_unique<TH3F>("theta_p_charge_binsgrid",
82  ";ECL cluster #theta;p_{lab}; Q",
83  m_thetabins.size() - 1,
84  m_thetabins.data(),
85  m_pbins.size() - 1,
86  m_pbins.data(),
87  m_chbins.size() - 1,
88  m_chbins.data());
89 
91 
92  std::vector<std::tuple<double, double, double>> gridBinCentres;
93 
94  for (unsigned int kch(0); kch < m_chbins.size() - 1; kch++) {
95  auto ch_bin_centre = (m_chbins.at(kch) + m_chbins.at(kch + 1)) / 2.0;
96  for (unsigned int ip(0); ip < m_pbins.size() - 1; ip++) {
97  auto p_bin_centre = (m_pbins.at(ip) + m_pbins.at(ip + 1)) / 2.0;
98  for (unsigned int jth(0); jth < m_thetabins.size() - 1; jth++) {
99  auto th_bin_centre = (m_thetabins.at(jth) + m_thetabins.at(jth + 1)) / 2.0;
100  auto fname = m_basename
101  + "__clusterTheta__" + std::to_string(m_thetabins.at(jth)) + "_" + std::to_string(m_thetabins.at(jth + 1))
102  + "__p__" + std::to_string(m_pbins.at(ip)) + "_" + std::to_string(m_pbins.at(ip + 1))
103  + "__charge__" + std::to_string(ch_bin_centre);
104 
105  std::replace(fname.begin(), fname.end(), '.', '_');
106  fname += ".xml";
107  std::ofstream dummyfile(fname);
108  dummyfile.close();
109  m_dummyfiles.push_back(fname);
110 
111  auto centre = std::make_tuple(th_bin_centre, p_bin_centre, ch_bin_centre);
112  gridBinCentres.push_back(centre);
113  }
114  }
115  }
117 
118  }
119 
123  void TearDown() override
124  {
125 
126  // Delete all dummy files.
127  for (const auto& fname : m_dummyfiles) {
128  if (remove(fname.c_str())) {
129  B2ERROR("Couldn't remove file: " << fname);
130  }
131  }
132 
133  }
134 
135  };
136 
142  {
143 
144  // Pick a random index in the clusterTheta, p, charge bins arrays.
145  // Exclude underflow and overflow.
146  std::random_device rd; // non-deterministic uniform int rand generator.
147  std::uniform_int_distribution<int> binx_idx_distr(1, m_thetabins.size() - 1);
148  std::uniform_int_distribution<int> biny_idx_distr(1, m_pbins.size() - 1);
149  std::uniform_int_distribution<int> binz_idx_distr(1, m_chbins.size() - 1);
150  int binx = binx_idx_distr(rd);
151  int biny = biny_idx_distr(rd);
152  int binz = binz_idx_distr(rd);
153 
154  // Pick each axis' bin centre as a test value for (clusterTheta, p, charge).
155  auto theta = m_grid->GetXaxis()->GetBinCenter(binx);
156  auto p = m_grid->GetYaxis()->GetBinCenter(biny);
157  auto charge = m_grid->GetZaxis()->GetBinCenter(binz);
158 
159  int jth, ip, kch;
160  auto jik = m_dbrep.getMVAWeightIdx(theta, p, charge, jth, ip, kch);
161 
162  EXPECT_EQ(jth, binx);
163  EXPECT_EQ(ip, biny);
164  EXPECT_EQ(kch, binz);
165 
166  auto thisfname = m_basename
167  + "__clusterTheta__" + std::to_string(m_thetabins.at(jth - 1)) + "_" + std::to_string(m_thetabins.at(jth))
168  + "__p__" + std::to_string(m_pbins.at(ip - 1)) + "_" + std::to_string(m_pbins.at(ip))
169  + "__charge__" + std::to_string(charge);
170  std::replace(thisfname.begin(), thisfname.end(), '.', '_');
171  thisfname += ".xml";
172 
173  EXPECT_EQ(thisfname, m_dummyfiles.at(jik));
174 
175  auto matchitr = std::find(m_dummyfiles.begin(), m_dummyfiles.end(), thisfname);
176  auto thisidx = std::distance(m_dummyfiles.begin(), matchitr);
177 
178  EXPECT_EQ(thisidx, jik);
179 
180  }
181 
183 } // namespace
Const::ChargedStable m_testHypo
The signal charged particle hypothesis to test.
void SetUp() override
Prepare resources for the tests.
std::vector< float > m_thetabins
The clusterTheta bin edges in [rad].
std::vector< float > m_pbins
The p bin edges in [GeV/c].
std::string m_basename
Base common name for all dummy weight files.
std::unique_ptr< TH3F > m_grid
The (clusterTheta, p, charge) grid for which xml files are stored in the payload.
ChargedPidMVAWeights m_dbrep
Database representation of MVA weightfiles.
void TearDown() override
Release all resources.
std::vector< float > m_chbins
The charge bin edges.
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(TH3F *h)
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:470
int getPDGCode() const
PDG code.
Definition: Const.h:354
static const ChargedStable electron
electron particle
Definition: Const.h:540
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.