Belle II Software development
trackIsoScoreCalculator.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/utilities/TestHelpers.h>
12
13#include <analysis/dbobjects/PIDDetectorWeights.h>
14
15#include <gtest/gtest.h>
16
17namespace Belle2 {
24 class TrackIsoScoreCalculatorTest : public ::testing::Test {
25
26 public:
27
32
37
41 int m_layer = 0;
42
48 std::string m_dummyFile = "dummyFile.root";
49
50
51 protected:
52
58 {
59
60 unsigned int nEntries(6);
61
62 ROOT::RDataFrame rdf(nEntries);
63
64 std::vector<double> pdgIds(nEntries, static_cast<double>(m_testHypo.getPDGCode()));
65 std::vector<double> pMinEdges = {1.0, 1.0, 1.0, 1.5, 1.5, 1.5};
66 std::vector<double> pMaxEdges = {1.5, 1.5, 1.5, 3.0, 3.0, 3.0};
67 std::vector<double> thetaMinEdges = {0.22, 0.56, 2.23, 0.22, 0.56, 2.23};
68 std::vector<double> thetaMaxEdges = {0.56, 2.23, 2.71, 0.56, 2.23, 2.71};
69 std::vector<double> pBinIdxs = {1.0, 1.0, 1.0, 2.0, 2.0, 2.0};
70 std::vector<double> thetaBinIdxs = {1.0, 2.0, 3.0, 1.0, 2.0, 3.0};
71 std::vector<std::string> regLabels = {"FWD", "Barrel", "BWD", "FWD", "Barrel", "BWD"};
72 std::vector<double> weights_SVD = {0., 0., -0.003, 0.031, 0.017, 0.077};
73 std::vector<double> weights_CDC = {-0.061, -0.084, -0.162, -0.073, -0.073, -0.05, -0.121};
74 std::vector<double> weights_TOP = {0., 0., 0., 0., 0., 0.};
75 std::vector<double> weights_ARICH = {0., 0., 0., -0.004, 0., 0.};
76 std::vector<double> weights_ECL = {-0.062, -0.118, -0.136, -0.22, -0.255, -0.377};
77 std::vector<double> weights_KLM = {0., 0., 0.006, -0.003, 0.003, 0.002};
78
79 unsigned int iEntry(0);
80 rdf.Define("pdgId", [&]() { auto x = pdgIds[iEntry]; return x; })
81 .Define("p_min", [&]() { auto x = pMinEdges[iEntry]; return x; })
82 .Define("p_max", [&]() { auto x = pMaxEdges[iEntry]; return x; })
83 .Define("theta_min", [&]() { auto x = thetaMinEdges[iEntry]; return x; })
84 .Define("theta_max", [&]() { auto x = thetaMaxEdges[iEntry]; return x; })
85 .Define("p_bin_idx", [&]() { auto x = pBinIdxs[iEntry]; return x; })
86 .Define("theta_bin_idx", [&]() { auto x = thetaBinIdxs[iEntry]; return x; })
87 .Define("reg_label", [&]() { auto x = regLabels[iEntry]; return x; })
88 .Define("ablat_s_SVD", [&]() { auto x = weights_SVD[iEntry]; return x; })
89 .Define("ablat_s_CDC", [&]() { auto x = weights_CDC[iEntry]; return x; })
90 .Define("ablat_s_TOP", [&]() { auto x = weights_TOP[iEntry]; return x; })
91 .Define("ablat_s_ARICH", [&]() { auto x = weights_ARICH[iEntry]; return x; })
92 .Define("ablat_s_ECL", [&]() { auto x = weights_ECL[iEntry]; return x; })
93 .Define("ablat_s_KLM", [&]() { auto x = weights_KLM[iEntry]; ++iEntry; return x; }) // Only the last call in the chain must increment the entry counter!
94 .Snapshot("tree", m_dummyFile);
95
96 };
97
103 {
104
105 unsigned int nEntries(6);
106
107 ROOT::RDataFrame rdf(nEntries);
108
109 std::vector<double> pdgIds(nEntries, static_cast<double>(m_testHypo.getPDGCode()));
110 std::vector<double> pMinEdges = {1.0, 1.0, 1.0, 1.5, 1.5, 1.5};
111 std::vector<double> pMaxEdges = {1.8, 1.8, 1.8, 3.0, 3.0, 3.0}; // note the non-contiguous edges wrt. pMinEdges
112 std::vector<double> thetaMinEdges = {0.22, 0.56, 2.23, 0.22, 0.56, 2.23};
113 std::vector<double> thetaMaxEdges = {0.56, 2.23, 2.71, 0.56, 2.23, 2.71};
114 std::vector<double> pBinIdxs = {1.0, 1.0, 1.0, 2.0, 2.0, 2.0};
115 std::vector<double> thetaBinIdxs = {1.0, 2.0, 3.0, 1.0, 2.0, 3.0};
116
117 // Tell snapshot to update file.
118 ROOT::RDF::RSnapshotOptions opt;
119 opt.fMode = "UPDATE";
120
121 unsigned int iEntry(0);
122 rdf.Define("pdgId", [&]() { auto x = pdgIds[iEntry]; return x; })
123 .Define("p_min", [&]() { auto x = pMinEdges[iEntry]; return x; })
124 .Define("p_max", [&]() { auto x = pMaxEdges[iEntry]; return x; })
125 .Define("theta_min", [&]() { auto x = thetaMinEdges[iEntry]; return x; })
126 .Define("theta_max", [&]() { auto x = thetaMaxEdges[iEntry]; return x; })
127 .Define("p_bin_idx", [&]() { auto x = pBinIdxs[iEntry]; return x; })
128 .Define("theta_bin_idx", [&]() { auto x = thetaBinIdxs[iEntry]; ++iEntry; return x; })
129 .Snapshot("tree_broken", m_dummyFile, "", opt);
130
131 };
132
136 void SetUp() override
137 {
140 }
141
145 void TearDown() override
146 {
147 // Delete all dummy files.
148 if (remove(m_dummyFile.c_str())) {
149 B2ERROR("Couldn't remove file: " << m_dummyFile);
150 }
151 }
152
153 };
154
160 {
161
162 PIDDetectorWeights dbrep("tree", m_dummyFile);
163
164 // Test for correct filling of the RDataFrame.
165 auto pdgIds = dbrep.getWeightsRDF().Take<double>("pdgId").GetValue();
166 for (const auto& pdgId : pdgIds) {
167 EXPECT_EQ(pdgId, m_testHypo.getPDGCode());
168 }
169
170 // Retrieve weight for a (p, theta) pair in the available weights range.
171 auto p = 1.23; // GeV/c
172 auto theta = 1.34; // rad
173 auto weight = dbrep.getWeight(m_testHypo, m_detector, p, theta);
174 EXPECT_EQ(weight, -0.118);
175
176 // Test for weight in case of out-of-range p and/or theta values.
177 p = 1.46;
178 theta = 0.12;
179 weight = dbrep.getWeight(m_testHypo, m_detector, p, theta);
180 EXPECT_TRUE(std::isnan(weight));
181
182 // Trigger a FATAL if reading an ill-defined source table.
183 EXPECT_B2FATAL(PIDDetectorWeights("tree_broken", m_dummyFile));
184
185 }
186
188} // namespace
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
EDetector
Enum for identifying the detector components (detector and subdetector).
Definition: Const.h:42
static const ChargedStable electron
electron particle
Definition: Const.h:659
Class for handling the PID weights per detector, used to calculate the track helix isolation score pe...
ROOT::RDataFrame getWeightsRDF() const
Get the RDataFrame of detector weights.
double getWeight(Const::ChargedStable hypo, Const::EDetector det, double p, double theta) const
Lookup the weight from the internal map structures.
Test the calculation of the track helix-based isolation score per particle.
Const::ChargedStable m_testHypo
The charged particle hypothesis to test.
Const::EDetector m_detector
The detector to test.
void SetUp() override
Prepare resources for the tests.
void createDummyBrokenTTree()
Create a ROOT::TTree with a "broken" bin edges structure, i.e.
void createDummyTTree()
Create a ROOT::TTree for the detector weights w/ a few entries.
void TearDown() override
Release all resources.
std::string m_dummyFile
Dummy ROOT file name.
int m_layer
The detector layer to test.
Abstract base class for different kinds of events.