Belle II Software development
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
12#include <analysis/dbobjects/ChargedPidMVAWeights.h>
13
14#include <gtest/gtest.h>
15#include <random>
16
17namespace Belle2 {
24 class ChargedParticleIdentificatorTest : public ::testing::Test {
25
26 public:
27
32
37
41 std::vector<double> m_thetabins = {0.2164208, 0.5480334, 0.561996, 2.2462387, 2.2811453, 2.7070057};
42
46 std::vector<double> m_pbins = {0.2, 0.6, 0.75, 1.0, 3.0, 7.0};
47
51 std::vector<double> m_chbins = { -1.5, -0.5, 0.5, 1.5};
52
56 std::string m_basename = "dummy_weightfile";
57
63 std::vector<std::string> m_dummyfiles;
64
65 protected:
66
70 void SetUp() override
71 {
72
74 m_pbins.data(), m_pbins.size() - 1,
75 m_chbins.data(), m_chbins.size() - 1);
76
77 std::vector<std::tuple<double, double, double>> gridBinCentres;
78
79 for (unsigned int kch(0); kch < m_chbins.size() - 1; kch++) {
80 auto ch_bin_centre = (m_chbins.at(kch) + m_chbins.at(kch + 1)) / 2.0;
81 for (unsigned int ip(0); ip < m_pbins.size() - 1; ip++) {
82 auto p_bin_centre = (m_pbins.at(ip) + m_pbins.at(ip + 1)) / 2.0;
83 for (unsigned int jth(0); jth < m_thetabins.size() - 1; jth++) {
84 auto th_bin_centre = (m_thetabins.at(jth) + m_thetabins.at(jth + 1)) / 2.0;
85 auto fname = m_basename
86 + "__clusterTheta__" + std::to_string(m_thetabins.at(jth)) + "_" + std::to_string(m_thetabins.at(jth + 1))
87 + "__p__" + std::to_string(m_pbins.at(ip)) + "_" + std::to_string(m_pbins.at(ip + 1))
88 + "__charge__" + std::to_string(ch_bin_centre);
89
90 std::replace(fname.begin(), fname.end(), '.', '_');
91 fname += ".xml";
92 std::ofstream dummyfile(fname);
93 dummyfile.close();
94 m_dummyfiles.push_back(fname);
95
96 auto centre = std::make_tuple(th_bin_centre, p_bin_centre, ch_bin_centre);
97 gridBinCentres.push_back(centre);
98 }
99 }
100 }
102
103 }
104
108 void TearDown() override
109 {
110
111 // Delete all dummy files.
112 for (const auto& fname : m_dummyfiles) {
113 if (remove(fname.c_str())) {
114 B2ERROR("Couldn't remove file: " << fname);
115 }
116 }
117
118 }
119
120 };
121
127 {
128
129 // Pick a random index in the clusterTheta, p, charge bins arrays.
130 // Exclude underflow and overflow.
131 std::random_device rd; // non-deterministic uniform int rand generator.
132 std::uniform_int_distribution<int> binx_idx_distr(1, m_thetabins.size() - 1);
133 std::uniform_int_distribution<int> biny_idx_distr(1, m_pbins.size() - 1);
134 std::uniform_int_distribution<int> binz_idx_distr(1, m_chbins.size() - 1);
135 int binx = binx_idx_distr(rd);
136 int biny = biny_idx_distr(rd);
137 int binz = binz_idx_distr(rd);
138
139 // Pick each axis' bin centre as a test value for (clusterTheta, p, charge).
140 auto theta = m_dbrep.getWeightCategories()->GetXaxis()->GetBinCenter(binx);
141 auto p = m_dbrep.getWeightCategories()->GetYaxis()->GetBinCenter(biny);
142 auto charge = m_dbrep.getWeightCategories()->GetZaxis()->GetBinCenter(binz);
143
144 int jth, ip, kch;
145 auto jik = m_dbrep.getMVAWeightIdx(theta, p, charge, jth, ip, kch);
146
147 EXPECT_EQ(jth, binx);
148 EXPECT_EQ(ip, biny);
149 EXPECT_EQ(kch, binz);
150
151 auto thisfname = m_basename
152 + "__clusterTheta__" + std::to_string(m_thetabins.at(jth - 1)) + "_" + std::to_string(m_thetabins.at(jth))
153 + "__p__" + std::to_string(m_pbins.at(ip - 1)) + "_" + std::to_string(m_pbins.at(ip))
154 + "__charge__" + std::to_string(charge);
155 std::replace(thisfname.begin(), thisfname.end(), '.', '_');
156 thisfname += ".xml";
157
158 EXPECT_EQ(thisfname, m_dummyfiles.at(jik));
159
160 auto matchitr = std::find(m_dummyfiles.begin(), m_dummyfiles.end(), thisfname);
161 auto thisidx = std::distance(m_dummyfiles.begin(), matchitr);
162
163 EXPECT_EQ(thisidx, jik);
164
165 }
166
168} // 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:589
int getPDGCode() const
PDG code.
Definition: Const.h:473
static const ChargedStable electron
electron particle
Definition: Const.h:659
Abstract base class for different kinds of events.