Belle II Software development
PIDPriors.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 * This test file for the PID prior classes *
10 ****************************************************************/
11
12#include <analysis/dbobjects/PIDPriorsTable.h>
13#include <analysis/dbobjects/PIDPriors.h>
14
15#include <framework/gearbox/Const.h>
16#include <framework/utilities/TestHelpers.h>
17#include <TRandom3.h>
18#include <TH2F.h>
19
20#include <gtest/gtest.h>
21
22using namespace std;
23
24namespace Belle2 {
30 class PIDPriorsTest : public ::testing::Test {
31 protected:
32 };
33
35 TEST_F(PIDPriorsTest, PIDPriorsTableTest)
36 {
37
38 std::vector<float> edgesX = {0., 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.7, 1.9, 2.2, 2.5, 3., 3.5, 4., 5., 6., 7., 10.};
39 std::vector<float> edgesY = { -1., -0.95, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1.};
40
41 PIDPriorsTable PIDTable = PIDPriorsTable();
42 PIDTable.setBinEdges(edgesX, edgesY);
43
44 PIDTable.printPrior();
45
46 PIDTable.setPriorValue(1.1, 0.54, 0.1);
47 PIDTable.setPriorValue(9.5, -0.67, 0.3);
48
49 std::cout << "after setting (1.1, 0.54) amd (9.5, -0.67)" << std::endl;
50 PIDTable.printPrior();
51
52 EXPECT_B2WARNING(PIDTable.setPriorValue(11.0, -10.01, 3.));
53 EXPECT_B2WARNING(PIDTable.setPriorValue(4.23, -0.01, 4.));
54
55 std::cout << " ----- Setting the priors from outside ------ " << std::endl;
56
57 std::vector<float> prior;
58
59 for (int j = 0; j < (int)edgesY.size() - 1; j++) {
60 for (int i = 0; i < (int)edgesX.size() - 1; i++) {
61 prior.push_back(0.001 * (i + j * (edgesX.size() - 1)));
62 }
63 }
64
65
66 PIDTable.setPriorsTable(prior);
67
68 PIDTable.printPrior();
69
70 EXPECT_FLOAT_EQ(PIDTable.getPriorValue(0.1, -0.98), 0.001 * (0 + 0 * (edgesX.size() - 1)));
71 EXPECT_FLOAT_EQ(PIDTable.getPriorValue(0.71, -0.65), 0.001 * (5 + 4 * (edgesX.size() - 1)));
72 EXPECT_FLOAT_EQ(PIDTable.getPriorValue(0.71, -1.65), 0.);
73 EXPECT_FLOAT_EQ(PIDTable.getPriorValue(11.71, 0.876), 0.);
74 EXPECT_B2WARNING(PIDTable.getPriorValue(0.71, -1.65));
75 EXPECT_B2WARNING(PIDTable.getPriorValue(11.71, 0.876));
76 EXPECT_FLOAT_EQ(0., PIDTable.getPriorValue(11.71, 0.876));
77
78
79
80 // now let's try to construct a table with no or on edge only
81 std::vector<float> badEdges1 = {0.};
82 std::vector<float> badEdges2 = {};
83
84
85 PIDPriorsTable PIDTable2 = PIDPriorsTable();
86
87 EXPECT_B2WARNING(PIDTable2.setBinEdges(edgesX, badEdges1));
88 EXPECT_B2WARNING(PIDTable2.setBinEdges(edgesX, badEdges2));
89
90
91 PIDTable2.setBinEdges(edgesX, badEdges2);
92
93 PIDTable2.printPrior();
94
95 PIDTable2.setPriorValue(1.1, 0.54, 0.3);
96 PIDTable2.setPriorValue(9.5, -0.67, 0.3);
97
98 std::cout << "after setting (1.1, 0.54) and (9.5, -0.67)" << std::endl;
99 PIDTable2.printPrior();
100
101 EXPECT_B2WARNING(PIDTable2.setPriorValue(11.0, -10.01, 0.3));
102 EXPECT_FLOAT_EQ(PIDTable2.getPriorValue(9.516, 0.), 0.3);
103
104 }
105
106
108 TEST_F(PIDPriorsTest, PIDPriorTest)
109 {
110
111 // Creates 6 prior objects in different formats (array, PIDPriorsTable, TH2F) to test different setters
112
113 std::vector<float> edgesX = {0., 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.};
114 std::vector<float> edgesY = { -1., -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.};
115
116 std::vector<float> prior1;
117 std::vector<float> prior2;
118 std::vector<float> prior3;
119 std::vector<float> prior4;
120 std::vector<float> prior5;
121 std::vector<float> prior6;
122
123 TH2F* counts1 = new TH2F("counts1", "counts1", 10, 0, 10., 20, -1, 1);
124 TH2F* counts2 = new TH2F("counts2", "counts2", 10, 0, 10., 20, -1, 1);
125 TH2F* counts3 = new TH2F("counts3", "counts3", 10, 0, 10., 20, -1, 1);
126 TH2F* counts4 = new TH2F("counts4", "counts4", 10, 0, 10., 20, -1, 1);
127 TH2F* counts5 = new TH2F("counts5", "counts5", 10, 0, 10., 20, -1, 1);
128 TH2F* counts6 = new TH2F("counts6", "counts6", 10, 0, 10., 20, -1, 1);
129
130 TH2F* norms = new TH2F("norms", "norms", 10, 0, 10., 20, -1, 1);
131
132 TRandom3* rn = new TRandom3();
133
134
135
136 for (int i = 0; i < (int)edgesX.size() - 1; i++) {
137 for (int j = 0; j < (int)edgesY.size() - 1; j++) {
138
139 float n1 = (float)rn->Poisson(100);
140 float n2 = (float)rn->Poisson(100);
141 float n3 = (float)rn->Poisson(700);
142 float n4 = (float)rn->Poisson(200);
143 float n5 = (float)rn->Poisson(100);
144 float n6 = (float)rn->Poisson(10);
145
146 float n = n1 + n2 + n3 + n4 + n5 + n6;
147
148 prior1.push_back(n1 / n);
149 prior2.push_back(n2 / n);
150 prior3.push_back(n3 / n);
151 prior4.push_back(n4 / n);
152 prior5.push_back(n5 / n);
153 prior6.push_back(n6 / n);
154
155 counts1->SetBinContent(i + 1, j + 1, n1);
156 counts2->SetBinContent(i + 1, j + 1, n2);
157 counts3->SetBinContent(i + 1, j + 1, n3);
158 counts4->SetBinContent(i + 1, j + 1, n4);
159 counts5->SetBinContent(i + 1, j + 1, n5);
160 counts6->SetBinContent(i + 1, j + 1, n6);
161 norms->SetBinContent(i + 1, j + 1, n);
162
163 }
164 }
165
166
167 PIDPriorsTable PIDTable1 = PIDPriorsTable();
168 PIDTable1.setBinEdges(edgesX, edgesY);
169 PIDTable1.setPriorsTable(prior1);
170
171 PIDPriorsTable PIDTable2 = PIDPriorsTable();
172 PIDTable2.setBinEdges(edgesX, edgesY);
173 PIDTable2.setPriorsTable(prior2);
174
175 PIDPriorsTable PIDTable3 = PIDPriorsTable();
176 PIDTable3.setBinEdges(edgesX, edgesY);
177 PIDTable3.setPriorsTable(prior3);
178
179 PIDPriorsTable PIDTable4 = PIDPriorsTable();
180 PIDTable4.setBinEdges(edgesX, edgesY);
181 PIDTable4.setPriorsTable(prior4);
182
183 PIDPriorsTable PIDTable5 = PIDPriorsTable();
184 PIDTable5.setBinEdges(edgesX, edgesY);
185 PIDTable5.setPriorsTable(prior5);
186
187 PIDPriorsTable PIDTable6 = PIDPriorsTable();
188 PIDTable6.setBinEdges(edgesX, edgesY);
189 PIDTable6.setPriorsTable(prior6);
190
196 Const::ChargedStable part_d = Const::ChargedStable(1000010020);
197
198
199 std::cout << "setting the tables " << std::endl;
200 // setter with the priortable
201 PIDPriors priors = PIDPriors();
202 priors.setPriors(part_e, PIDTable1);
203 priors.setPriors(part_mu, PIDTable2);
204 priors.setPriors(part_pi, PIDTable3);
205 priors.setPriors(part_K, PIDTable4);
206 priors.setPriors(part_p, PIDTable5);
207 priors.setPriors(part_d, PIDTable6);
208
209 // setter with the count histograms
210 PIDPriors priors2 = PIDPriors();
211 priors2.setPriors(part_e, counts1, norms);
212 priors2.setPriors(part_mu, counts2, norms);
213 priors2.setPriors(part_pi, counts3, norms);
214 priors2.setPriors(part_K, counts4, norms);
215 priors2.setPriors(part_p, counts5, norms);
216 priors2.setPriors(part_d, counts6, norms);
217
218 counts1->Divide(norms);
219 counts2->Divide(norms);
220 counts3->Divide(norms);
221 counts4->Divide(norms);
222 counts5->Divide(norms);
223 counts6->Divide(norms);
224
225 std::cout << "division done" << std::endl;
226 // setter with priors stored in a TH2F
227 std::cout << "setting the TH2 " << std::endl;
228 PIDPriors priors3 = PIDPriors();
229 priors3.setPriors(part_e, counts1);
230 priors3.setPriors(part_mu, counts2);
231 priors3.setPriors(part_pi, counts3);
232 priors3.setPriors(part_K, counts4);
233 priors3.setPriors(part_p, counts5);
234 priors3.setPriors(part_d, counts6);
235
236
237 std::vector<Const::ChargedStable> codes = {part_e, part_mu, part_pi, part_K, part_p, part_d};
238 for (auto code : codes) {
239 for (int i = 0; i < 3; i++) {
240 double p = rn->Uniform(0., 10);
241 double c = rn->Uniform(-1., 1);
242 EXPECT_FLOAT_EQ(priors.getPriorValue(code, p, c), priors2.getPriorValue(code, p, c));
243 EXPECT_FLOAT_EQ(priors.getPriorValue(code, p, c), priors3.getPriorValue(code, p, c));
244 }
245 }
246
247 EXPECT_FLOAT_EQ(0., priors.getPriorValue(part_pi, 11.1, 0.));
248 EXPECT_FLOAT_EQ(0., priors.getPriorValue(part_pi, -1, 0.5));
249 EXPECT_FLOAT_EQ(0., priors.getPriorValue(part_pi, 0.98, -1.001));
250
251 EXPECT_FLOAT_EQ(0., priors2.getPriorValue(part_pi, 11.1, 0.));
252 EXPECT_FLOAT_EQ(0., priors2.getPriorValue(part_pi, -1, 0.5));
253 EXPECT_FLOAT_EQ(0., priors2.getPriorValue(part_pi, 0.98, -1.001));
254
255 EXPECT_FLOAT_EQ(0., priors3.getPriorValue(part_pi, 11.1, 0.));
256 EXPECT_FLOAT_EQ(0., priors3.getPriorValue(part_pi, -1, 0.5));
257 EXPECT_FLOAT_EQ(0., priors3.getPriorValue(part_pi, 0.98, -1.001));
258
259 }
260
262} // namespace
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:589
This class holds the prior distribution for a single particle species.
void printPrior() const
Prints the content of the table and the axes.
void setBinEdges(const std::vector< float > &binEdgesX, const std::vector< float > &binEdgesY)
Sets the bin edges arrays.
float getPriorValue(float x, float y) const
Returns the prior probability for a given value of (x,y).
void setPriorValue(float x, float y, float value)
Sets the prior value for a single bin.
void setPriorsTable(const std::vector< float > &priors)
Sets the priors table from a 2D std::vector.
A database class to hold the prior probability for the particle identification.
Definition: PIDPriors.h:31
void setPriors(const Const::ChargedStable &particle, const PIDPriorsTable &table)
Sets the prior table for a particle species from a PIDPriorsTable object.
Definition: PIDPriors.h:52
float getPriorValue(const Const::ChargedStable &particle, float x, float y) const
Returns the prior probability associated to a particle with defined species and parameters.
Definition: PIDPriors.h:177
TString rn()
Get random string.
Definition: tools.h:38
Abstract base class for different kinds of events.
STL namespace.