FairShip
Loading...
Searching...
No Matches
test_calo_scoring_plane_pid.cxx File Reference
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
#include "CaloScoringPlanePID.h"
#include "CaloScoringPlanePoint.h"
#include "TRandom3.h"
#include "TVector3.h"
Include dependency graph for test_calo_scoring_plane_pid.cxx:

Go to the source code of this file.

Functions

int main ()

Function Documentation

◆ main()

int main ( )

Definition at line 70 of file test_calo_scoring_plane_pid.cxx.

70 {
71 const double nan = std::numeric_limits<double>::quiet_NaN();
72 const double inf = std::numeric_limits<double>::infinity();
73
74 // --- default: perfect PID ---
75 {
76 S pid;
77 check(pid.Identify(11, 5., 0.5) == S::kElectron, "identity e");
78 check(pid.Identify(-13, 5., 0.5) == S::kMuon, "identity mu");
79 check(pid.Identify(211, 5., 0.99) == S::kHadron, "identity pi");
80 check(pid.Identify(22, 5., 0.5) == S::kUnknown, "photon unknown");
81 }
82
83 // --- true species from PDG codes ---
84 {
85 S pid;
86 check(pid.TrueSpecies(-11) == S::kElectron, "e+");
87 check(pid.TrueSpecies(13) == S::kMuon, "mu-");
88 for (int h : {211, -211, 321, 130, 310, 2212, -2212, 2112, 3122, 111}) {
89 check(pid.TrueSpecies(h) == S::kHadron, "hadron " + std::to_string(h));
90 }
91 for (int u : {22, 12, -14, 16, 1000010020, 9900015, 0}) {
92 check(pid.TrueSpecies(u) == S::kUnknown, "unknown " + std::to_string(u));
93 }
94 }
95
96 // --- linear interpolation and clamping ---
97 {
98 S pid;
99 pid.Set({10., 20.}, Concat(Identity(), ElectronAsHadron()));
100 check(close(pid.Probability(S::kElectron, S::kElectron, 15.), 0.5),
101 "linear midpoint");
102 check(close(pid.Probability(S::kElectron, S::kHadron, 12.5), 0.25),
103 "linear quarter");
104 check(close(pid.Probability(S::kElectron, S::kElectron, 1.), 1.0),
105 "clamp low");
106 check(close(pid.Probability(S::kElectron, S::kElectron, 1e4), 0.0),
107 "clamp high");
108 auto m = pid.Matrix(15.);
109 for (size_t i = 0; i < 3; ++i) {
110 check(close(m[3 * i] + m[3 * i + 1] + m[3 * i + 2], 1.),
111 "interpolated row sums to 1");
112 }
113 // deterministic draws at E = 15: electron row = (0.5, 0.5, 0)
114 check(pid.Identify(11, 15., 0.49) == S::kElectron, "draw below 0.5");
115 check(pid.Identify(11, 15., 0.51) == S::kHadron, "draw above 0.5");
116 check(pid.Identify(11, 15., 1.0) == S::kHadron, "u == 1");
117 check(pid.Identify(13, 15., 1.0) == S::kMuon, "u == 1, diagonal row");
118 }
119
120 // --- log interpolation ---
121 {
122 S pid;
123 pid.Set({10., 20.}, Concat(Identity(), ElectronAsHadron()), {}, kTRUE);
124 check(pid.IsLogInterpolation(), "log interpolation set");
125 const double e = std::sqrt(10. * 20.); // geometric mean -> weight 0.5
126 check(close(pid.Probability(S::kElectron, S::kElectron, e), 0.5),
127 "log midpoint");
128 }
129
130 // --- frequencies ---
131 {
132 S pid;
133 pid.Set({1.}, {0.8, 0.2, 0.0, 0.3, 0.6, 0.1, 0.0, 0.0, 1.0});
134 TRandom3 rng(4357);
135 const int n = 200000;
136 int nE = 0, nMu = 0;
137 for (int k = 0; k < n; ++k) {
138 const auto s = pid.Identify(211, 3., rng);
139 nE += s == S::kElectron;
140 nMu += s == S::kMuon;
141 }
142 check(std::fabs(nE / static_cast<double>(n) - 0.3) < 0.005,
143 "pi->e frequency");
144 check(std::fabs(nMu / static_cast<double>(n) - 0.1) < 0.005,
145 "pi->mu frequency");
146 }
147
148 // --- a fixed outcome draws no random number ---
149 {
150 S perfect;
151 TRandom3 used(1), reference(1);
152 for (int pdg : {11, -13, 211, 22, 12}) perfect.Identify(pdg, 5., used);
153 check(used.Rndm() == reference.Rndm(),
154 "perfect PID and unknown species leave the generator untouched");
155
156 S pid; // electron row (0.5, 0.5, 0) at 15 GeV, (0, 1, 0) from 20 GeV
157 pid.Set({10., 20.}, Concat(Identity(), ElectronAsHadron()));
158 TRandom3 a(2), b(2);
159 check(pid.Identify(11, 50., a) == S::kHadron, "single non-zero entry");
160 check(a.Rndm() == b.Rndm(), "single non-zero entry draws nothing");
161 pid.Identify(11, 15., a); // two possible outcomes: one draw
162 b.Rndm();
163 check(a.Rndm() == b.Rndm(), "a varying outcome draws exactly once");
164 }
165
166 // --- overrides ---
167 {
168 S pid;
169 pid.Set({1.}, Identity(), {{22, S::kElectron}, {-211, S::kUnknown}});
170 check(pid.TrueSpecies(22) == S::kElectron, "override photon");
171 check(pid.TrueSpecies(211) == S::kUnknown, "override pi to unknown");
172 check(pid.TrueSpecies(-211) == S::kUnknown, "override applies to |pdg|");
173 }
174
175 // --- energy from a point ---
176 {
177 CaloScoringPlanePoint p(0, 1, 1, TVector3(0, 0, 0), TVector3(0, 3, 4), 0.,
178 0., 0., 13);
179 const double m = 0.1056583755;
180 check(std::fabs(S::Energy(p) - std::sqrt(25. + m * m)) < 1e-6,
181 "muon energy");
182 }
183
184 // --- non-finite energies and out-of-range species ---
185 {
186 S pid;
187 pid.Set({10., 20.}, Concat(Identity(), ElectronAsHadron()));
188 TRandom3 used(3), reference(3);
189 for (double e : {nan, inf, -inf}) {
190 check(pid.Identify(11, e, 0.5) == S::kUnknown, "non-finite energy: u");
191 check(pid.Identify(11, e, used) == S::kUnknown,
192 "non-finite energy: generator");
193 }
194 check(used.Rndm() == reference.Rndm(), "non-finite energy draws nothing");
195 bool threw = false;
196 try {
197 pid.Matrix(nan);
198 } catch (const std::invalid_argument&) {
199 threw = true;
200 }
201 check(threw, "Matrix(NaN) throws");
202 threw = false;
203 try {
204 pid.Probability(S::kElectron, S::kElectron, nan);
205 } catch (const std::invalid_argument&) {
206 threw = true;
207 }
208 check(threw, "Probability(NaN) throws");
209 for (int bad : {3, -2}) {
210 threw = false;
211 try {
212 pid.Probability(static_cast<S::Species>(bad), S::kMuon, 1.);
213 } catch (const std::out_of_range&) {
214 threw = true;
215 }
216 check(threw, "true species " + std::to_string(bad) + " throws");
217 threw = false;
218 try {
219 pid.Probability(S::kMuon, static_cast<S::Species>(bad), 1.);
220 } catch (const std::out_of_range&) {
221 threw = true;
222 }
223 check(threw, "identified species " + std::to_string(bad) + " throws");
224 }
225 check(pid.Probability(S::kUnknown, S::kUnknown, 1.) == 1.,
226 "unknown -> unknown still allowed");
227 }
228
229 // --- validation in Set() ---
230 expectInvalid([](S& p) { p.Set({}, {}); }, "no energy points");
231 expectInvalid([](S& p) { p.Set({1.}, {1, 0, 0}); }, "too few values");
232 expectInvalid([](S& p) { p.Set({2., 1.}, Concat(Identity(), Identity())); },
233 "decreasing energies");
234 expectInvalid([](S& p) { p.Set({1., 1.}, Concat(Identity(), Identity())); },
235 "repeated energy");
236 expectInvalid([](S& p) { p.Set({1.}, {0.5, 0.4, 0, 0, 1, 0, 0, 0, 1}); },
237 "row sum");
238 expectInvalid([](S& p) { p.Set({1.}, {1.2, -0.2, 0, 0, 1, 0, 0, 0, 1}); },
239 "negative entry");
240 expectInvalid([&](S& p) { p.Set({1.}, {nan, 1, 0, 0, 1, 0, 0, 0, 1}); },
241 "NaN entry");
242 expectInvalid([&](S& p) { p.Set({nan}, Identity()); }, "NaN energy");
243 expectInvalid([&](S& p) { p.Set({1., inf}, Concat(Identity(), Identity())); },
244 "infinite energy");
245 expectInvalid([](S& p) { p.Set({0.}, Identity(), {}, kTRUE); },
246 "log with zero energy");
247 expectInvalid([](S& p) { p.Set({1.}, Identity(), {{22, 3}}); },
248 "override species out of range");
249 expectInvalid(
250 [](S& p) {
251 p.Set({1.}, Identity(), {{211, S::kHadron}, {-211, S::kMuon}});
252 },
253 "conflicting overrides");
254
255 if (failures) {
256 std::cerr << failures << " check(s) failed" << std::endl;
257 return 1;
258 }
259 std::cout << "CaloScoringPlanePID: all checks passed" << std::endl;
260 return 0;
261}
constexpr Double_t m
int i
Definition ShipAna.py:108