Belle II Software  release-05-01-25
chargedParticleIdentificator.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marco Milesi *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/gearbox/Const.h>
13 #include <framework/gearbox/Unit.h>
14 
15 #include <analysis/dbobjects/ChargedPidMVAWeights.h>
16 
17 #include <gtest/gtest.h>
18 
19 #include <TH2F.h>
20 
21 namespace Belle2 {
28  class ChargedParticleIdentificatorTest : public ::testing::Test {
29 
30  public:
31 
35  ChargedPidMVAWeights m_dbrep;
36 
41 
45  std::unique_ptr<TH2F> m_grid;
46 
50  std::vector<float> m_thetabins = {0.2164208, 0.5480334, 0.561996, 2.2462387, 2.2811453, 2.7070057};
51 
55  std::vector<float> m_pbins = {0.0, 0.5, 0.75, 1.0, 3.0, 100.0};
56 
60  std::vector<int> m_eclregbins = {1, 11, 2, 13, 3};
61 
65  std::string m_basename = "dummy_weightfile";
66 
72  std::vector<std::string> m_dummyfiles;
73 
74  protected:
75 
79  void SetUp() override
80  {
81 
82  m_grid = std::make_unique<TH2F>("theta_p_binsgrid",
83  ";ECL cluster #theta;p_{lab}",
84  m_thetabins.size() - 1,
85  m_thetabins.data(),
86  m_pbins.size() - 1,
87  m_pbins.data());
88 
90 
93 
94  std::vector<std::pair<float, float>> gridBinCentres;
95 
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  + "__p__" + std::to_string(m_pbins.at(ip)) + "_" + std::to_string(m_pbins.at(ip + 1))
102  + "__clusterTheta__" + std::to_string(m_thetabins.at(jth)) + "_" + std::to_string(m_thetabins.at(jth + 1));
103  std::replace(fname.begin(), fname.end(), '.', '_');
104  fname += ".xml";
105  std::ofstream dummyfile(fname);
106  dummyfile.close();
107  m_dummyfiles.push_back(fname);
108 
109  auto centre = std::make_pair(th_bin_centre, p_bin_centre);
110  gridBinCentres.push_back(centre);
111  }
112  }
114 
115  }
116 
120  void TearDown() override
121  {
122 
123  // Delete all dummy files.
124  for (const auto& fname : m_dummyfiles) {
125  if (remove(fname.c_str())) {
126  B2ERROR("Couldn't remove file: " << fname);
127  }
128  }
129 
130  }
131 
132  };
133 
138  TEST_F(ChargedParticleIdentificatorTest, TestDBRep)
139  {
140 
141  // Pick a value for (clusterTheta, p) in the grid.
142  int binx = 3;
143  int biny = 4;
144  auto theta = m_grid->GetXaxis()->GetBinCenter(binx);
145  auto p = m_grid->GetYaxis()->GetBinCenter(biny);
146 
147  int jth, ip;
148  auto ji = m_dbrep.getMVAWeightIdx(theta, p, jth, ip);
149 
150  EXPECT_EQ(jth, binx);
151  EXPECT_EQ(ip, biny);
152 
153  auto thisfname = m_basename
154  + "__p__" + std::to_string(m_pbins.at(ip - 1)) + "_" + std::to_string(m_pbins.at(ip))
155  + "__clusterTheta__" + std::to_string(m_thetabins.at(jth - 1)) + "_" + std::to_string(m_thetabins.at(jth));
156  std::replace(thisfname.begin(), thisfname.end(), '.', '_');
157  thisfname += ".xml";
158 
159  EXPECT_EQ(thisfname, m_dummyfiles.at(ji));
160 
161  auto matchitr = std::find(m_dummyfiles.begin(), m_dummyfiles.end(), thisfname);
162  auto thisidx = std::distance(m_dummyfiles.begin(), matchitr);
163 
164  EXPECT_EQ(thisidx, ji);
165 
166  }
167 
169 } // namespace
Belle2::ChargedParticleIdentificatorTest::m_testHypo
Const::ChargedStable m_testHypo
The signal charged particle hypothesis to test.
Definition: chargedParticleIdentificator.cc:48
Belle2::ChargedParticleIdentificatorTest::m_eclregbins
std::vector< int > m_eclregbins
The clusterRegion bins.
Definition: chargedParticleIdentificator.cc:68
Belle2::ChargedParticleIdentificatorTest::SetUp
void SetUp() override
Prepare resources for the tests.
Definition: chargedParticleIdentificator.cc:87
Belle2::Const::electron
static const ChargedStable electron
electron particle
Definition: Const.h:533
Belle2::ChargedParticleIdentificatorTest::m_basename
std::string m_basename
Base common name for all dummy weight files.
Definition: chargedParticleIdentificator.cc:73
Belle2::Const::ParticleType::getPDGCode
int getPDGCode() const
PDG code.
Definition: Const.h:349
Belle2::ChargedParticleIdentificatorTest::m_thetabins
std::vector< float > m_thetabins
The clusterTheta bin edges in [rad].
Definition: chargedParticleIdentificator.cc:58
Belle2::ChargedParticleIdentificatorTest::m_grid
std::unique_ptr< TH2F > m_grid
The (clusterTheta, p) grid for which xml files are stored in the payload.
Definition: chargedParticleIdentificator.cc:53
Belle2::ChargedParticleIdentificatorTest::TearDown
void TearDown() override
Release all resources.
Definition: chargedParticleIdentificator.cc:128
Belle2::Unit::rad
static const double rad
Standard of [angle].
Definition: Unit.h:60
Belle2::ChargedParticleIdentificatorTest::m_dbrep
ChargedPidMVAWeights m_dbrep
Database representation of MVA weightfiles.
Definition: chargedParticleIdentificator.cc:43
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65
Belle2::ChargedPidMVAWeights::setWeightCategories
void setWeightCategories(TH2F *h)
Set the 2D (clusterTheta, p) grid representing the categories for which weightfiles are defined.
Definition: ChargedPidMVAWeights.h:107
Belle2::ChargedParticleIdentificatorTest::m_pbins
std::vector< float > m_pbins
The p bin edges in [GeV/c].
Definition: chargedParticleIdentificator.cc:63
Belle2::ChargedPidMVAWeights::setEnergyUnit
void setEnergyUnit(const double &unit)
Set the energy unit to ensure consistency w/ the one used to define the bins grid.
Definition: ChargedPidMVAWeights.h:94
Belle2::ChargedParticleIdentificatorTest::m_dummyfiles
std::vector< std::string > m_dummyfiles
List of dummy xml file names.
Definition: chargedParticleIdentificator.cc:80
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465
Belle2::Unit::GeV
static const double GeV
Standard of [energy, momentum, mass].
Definition: Unit.h:61
Belle2::ChargedPidMVAWeights::storeMVAWeights
void storeMVAWeights(const int pdg, const std::vector< std::string > &filepaths, const std::vector< std::pair< float, float >> &categoryBinCentres)
Given a particle mass hypothesis' pdgId, store the list of MVA weight files (one for each category) i...
Definition: ChargedPidMVAWeights.h:121
Belle2::ChargedPidMVAWeights::setAngularUnit
void setAngularUnit(const double &unit)
Set the angular unit to ensure consistency w/ the one used to define the bins grid.
Definition: ChargedPidMVAWeights.h:100