FairShip
Loading...
Searching...
No Matches
CaloScoringPlanePID Class Reference

Energy-dependent electron / hadron / muon identification from confusion matrices. More...

#include <CaloScoringPlanePID.h>

Public Types

enum  Species : Int_t { kUnknown = -1 , kElectron = 0 , kHadron = 1 , kMuon = 2 }

Public Member Functions

 CaloScoringPlanePID ()
void Set (const std::vector< Double_t > &energies, const std::vector< Double_t > &matrices, const std::map< Int_t, Int_t > &overrides={}, Bool_t logInterpolation=kFALSE)
Species TrueSpecies (Int_t pdg) const
Double_t Probability (Species trueSpecies, Species recoSpecies, Double_t energy) const
std::vector< Double_t > Matrix (Double_t energy) const
Species Identify (Int_t pdg, Double_t energy, Double_t u) const
Species Identify (Int_t pdg, Double_t energy, TRandom &rng) const
Species Identify (const CaloScoringPlanePoint &point, TRandom &rng) const
const std::vector< Double_t > & GetEnergies () const
Bool_t IsLogInterpolation () const
void Print () const

Static Public Member Functions

static Double_t Energy (const CaloScoringPlanePoint &point)
static const char * SpeciesName (Int_t species)

Static Public Attributes

static constexpr Int_t kNSpecies = 3

Private Types

using Row = std::array<Double_t, kNSpecies>

Private Member Functions

void SetIdentity ()
Row InterpolatedRow (Double_t energy, Int_t trueSpecies) const
Double_t At (size_t iEnergy, Int_t i, Int_t j) const

Private Attributes

std::vector< Double_t > fEnergies
 GeV, strictly increasing.
std::vector< Double_t > fMatrices
 9 per energy, row-major, internal order
std::map< Int_t, Int_t > fOverrides
 |pdg| -> Species
Bool_t fLogInterpolation = kFALSE

Detailed Description

Energy-dependent electron / hadron / muon identification from confusion matrices.

The model is a list of 3x3 confusion matrices given at increasing energies. M(E)[i][j] is the probability that a particle of true species i is identified as species j; each row sums to 1. For a particle of total energy E the matrix is interpolated element by element between the two neighbouring energy points (linearly in E, or in log E); below the first or above the last point, that point's matrix is used. Interpolating row-stochastic matrices this way keeps every row normalised and non-negative.

True species from the PDG code: |pdg| = 11 electron, 13 muon; any meson or baryon (|pdg| < 9900000 with a non-zero quark digit n_q2) is a hadron; anything else (photons, neutrinos, nuclei, BSM codes) is kUnknown and is never identified. Overrides change this per |pdg|.

The model is given with Set(), which validates it. Reconstruction reads it from a YAML file in Python (python/caloScoringPlanePID.py; format in CaloScoringPlane/README.md, example in geometry/caloScoringPlane_pid_provided.yaml).

Usage from C++ (link against the CaloScoringPlane library):

and from Python:

from caloScoringPlanePID import load_pid
pid = load_pid("$FAIRSHIP/geometry/caloScoringPlane_pid_provided.yaml")
if pid.Identify(point, rng) == ROOT.CaloScoringPlanePID.kMuon: ...

Definition at line 53 of file CaloScoringPlanePID.h.

Member Typedef Documentation

◆ Row

using CaloScoringPlanePID::Row = std::array<Double_t, kNSpecies>
private

Definition at line 120 of file CaloScoringPlanePID.h.

Member Enumeration Documentation

◆ Species

Enumerator
kUnknown 
kElectron 
kHadron 
kMuon 

Definition at line 55 of file CaloScoringPlanePID.h.

Constructor & Destructor Documentation

◆ CaloScoringPlanePID()

CaloScoringPlanePID::CaloScoringPlanePID ( )

Perfect identification (identity matrix at all energies).

Definition at line 82 of file CaloScoringPlanePID.cxx.

Member Function Documentation

◆ At()

Double_t CaloScoringPlanePID::At ( size_t iEnergy,
Int_t i,
Int_t j ) const
inlineprivate

Definition at line 125 of file CaloScoringPlanePID.h.

125 {
126 return fMatrices[9 * iEnergy + static_cast<size_t>(3 * i + j)];
127 }
std::vector< Double_t > fMatrices
9 per energy, row-major, internal order
int i
Definition ShipAna.py:108

◆ Energy()

Double_t CaloScoringPlanePID::Energy ( const CaloScoringPlanePoint & point)
static

Total energy (GeV) at the point, using the PDG mass (0 if unknown).

Definition at line 240 of file CaloScoringPlanePID.cxx.

240 {
241 const double p2 = point.GetPx() * point.GetPx() +
242 point.GetPy() * point.GetPy() +
243 point.GetPz() * point.GetPz();
244 double mass = 0.;
245 if (const TParticlePDG* particle =
246 TDatabasePDG::Instance()->GetParticle(point.PdgCode())) {
247 mass = particle->Mass();
248 }
249 return std::sqrt(p2 + mass * mass);
250}
Int_t PdgCode() const

◆ GetEnergies()

const std::vector< Double_t > & CaloScoringPlanePID::GetEnergies ( ) const
inline

Definition at line 115 of file CaloScoringPlanePID.h.

115{ return fEnergies; }
std::vector< Double_t > fEnergies
GeV, strictly increasing.

◆ Identify() [1/3]

CaloScoringPlanePID::Species CaloScoringPlanePID::Identify ( const CaloScoringPlanePoint & point,
TRandom & rng ) const

Same, with the PDG code and total energy taken from the point.

Definition at line 233 of file CaloScoringPlanePID.cxx.

234 {
235 // Skip the energy (a TDatabasePDG lookup) for photons, nuclei, ...
236 if (TrueSpecies(point.PdgCode()) == kUnknown) return kUnknown;
237 return Identify(point.PdgCode(), Energy(point), rng);
238}
static Double_t Energy(const CaloScoringPlanePoint &point)
Species Identify(Int_t pdg, Double_t energy, Double_t u) const
Species TrueSpecies(Int_t pdg) const

◆ Identify() [2/3]

CaloScoringPlanePID::Species CaloScoringPlanePID::Identify ( Int_t pdg,
Double_t energy,
Double_t u ) const

Draw the PID outcome with a given uniform number u in [0, 1]. A non-finite energy gives kUnknown, as for the overloads below.

Definition at line 213 of file CaloScoringPlanePID.cxx.

215 {
216 const Species s = TrueSpecies(pdg);
217 if (s == kUnknown || !std::isfinite(energy)) return kUnknown;
218 return Draw(InterpolatedRow(energy, s), s, u);
219}
Row InterpolatedRow(Double_t energy, Int_t trueSpecies) const

◆ Identify() [3/3]

CaloScoringPlanePID::Species CaloScoringPlanePID::Identify ( Int_t pdg,
Double_t energy,
TRandom & rng ) const

Draw the PID outcome with the generator rng. No number is drawn when the outcome is fixed: for an unknown species, or a matrix row with a single non-zero entry (as everywhere with perfect identification).

Definition at line 221 of file CaloScoringPlanePID.cxx.

223 {
224 const Species s = TrueSpecies(pdg);
225 if (s == kUnknown || !std::isfinite(energy)) return kUnknown;
226 const Row row = InterpolatedRow(energy, s);
227 // Draw only when the outcome can vary, so that a fixed outcome leaves the
228 // generator's sequence untouched.
229 if (const Species only = OnlyOutcome(row); only != kUnknown) return only;
230 return Draw(row, s, rng.Rndm());
231}
std::array< Double_t, kNSpecies > Row

◆ InterpolatedRow()

CaloScoringPlanePID::Row CaloScoringPlanePID::InterpolatedRow ( Double_t energy,
Int_t trueSpecies ) const
private

Interpolated matrix row of trueSpecies (not kUnknown).

Definition at line 161 of file CaloScoringPlanePID.cxx.

162 {
163 // A NaN would pass both clamps below and make upper_bound return end().
164 RequireFinite(energy);
165 Row row{};
166 const auto rowAt = [&](size_t k) {
167 for (Int_t j = 0; j < kNSpecies; ++j) row[j] = At(k, trueSpecies, j);
168 return row;
169 };
170 const size_t n = fEnergies.size();
171 if (n == 1 || energy <= fEnergies.front()) return rowAt(0);
172 if (energy >= fEnergies.back()) return rowAt(n - 1);
173 const auto hi = static_cast<size_t>(
174 std::upper_bound(fEnergies.begin(), fEnergies.end(), energy) -
175 fEnergies.begin());
176 const size_t lo = hi - 1;
177 const double w =
179 ? std::log(energy / fEnergies[lo]) /
180 std::log(fEnergies[hi] / fEnergies[lo])
181 : (energy - fEnergies[lo]) / (fEnergies[hi] - fEnergies[lo]);
182 for (Int_t j = 0; j < kNSpecies; ++j) {
183 row[j] = (1. - w) * At(lo, trueSpecies, j) + w * At(hi, trueSpecies, j);
184 }
185 return row;
186}
static constexpr Int_t kNSpecies
Double_t At(size_t iEnergy, Int_t i, Int_t j) const

◆ IsLogInterpolation()

Bool_t CaloScoringPlanePID::IsLogInterpolation ( ) const
inline

Definition at line 116 of file CaloScoringPlanePID.h.

116{ return fLogInterpolation; }

◆ Matrix()

std::vector< Double_t > CaloScoringPlanePID::Matrix ( Double_t energy) const

Interpolated 3x3 matrix, row-major (9 values). Throws std::invalid_argument for a non-finite energy.

Definition at line 188 of file CaloScoringPlanePID.cxx.

188 {
189 std::vector<Double_t> m(9);
190 for (Int_t i = 0; i < kNSpecies; ++i) {
191 const Row row = InterpolatedRow(energy, i);
192 std::copy(row.begin(), row.end(), m.begin() + 3L * i);
193 }
194 return m;
195}
constexpr Double_t m

◆ Print()

void CaloScoringPlanePID::Print ( ) const

Definition at line 265 of file CaloScoringPlanePID.cxx.

265 {
266 LOG(info) << "CaloScoringPlanePID: " << fEnergies.size()
267 << " energy point(s), " << (fLogInterpolation ? "log" : "linear")
268 << " interpolation; rows = true, columns = identified "
269 "(electron, hadron, muon)";
270 for (size_t k = 0; k < fEnergies.size(); ++k) {
271 for (Int_t i = 0; i < kNSpecies; ++i) {
272 LOG(info) << " E = " << fEnergies[k] << " GeV " << SpeciesName(i)
273 << ": " << At(k, i, 0) << " " << At(k, i, 1) << " "
274 << At(k, i, 2);
275 }
276 }
277 for (const auto& [pdg, s] : fOverrides) {
278 LOG(info) << " override |pdg| " << pdg << " -> " << SpeciesName(s);
279 }
280}
static const char * SpeciesName(Int_t species)
std::map< Int_t, Int_t > fOverrides
|pdg| -> Species

◆ Probability()

Double_t CaloScoringPlanePID::Probability ( Species trueSpecies,
Species recoSpecies,
Double_t energy ) const

Interpolated probability P(identified as reco | true species, E). Throws std::out_of_range for a species other than kUnknown, kElectron, kHadron or kMuon, and std::invalid_argument for a non-finite energy.

Definition at line 197 of file CaloScoringPlanePID.cxx.

199 {
200 for (const Int_t s :
201 {static_cast<Int_t>(trueSpecies), static_cast<Int_t>(recoSpecies)}) {
202 if (s < kUnknown || s >= kNSpecies) {
203 throw std::out_of_range("CaloScoringPlanePID: species " +
204 std::to_string(s) + " out of range");
205 }
206 }
207 RequireFinite(energy);
208 if (trueSpecies == kUnknown) return recoSpecies == kUnknown ? 1. : 0.;
209 if (recoSpecies == kUnknown) return 0.;
210 return InterpolatedRow(energy, trueSpecies)[recoSpecies];
211}

◆ Set()

void CaloScoringPlanePID::Set ( const std::vector< Double_t > & energies,
const std::vector< Double_t > & matrices,
const std::map< Int_t, Int_t > & overrides = {},
Bool_t logInterpolation = kFALSE )

Replace the model.

Parameters
energiestotal energies in GeV, finite and strictly increasing (and positive with log interpolation)
matrices9 values per energy, row-major; rows are the true and columns the identified species, both in the order electron, hadron, muon. Entries are finite and non-negative, each row sums to 1.
overrides|pdg| -> species (kUnknown allowed); a negative key applies to |pdg| as well.
logInterpolationinterpolate linearly in log E instead of E

Throws std::invalid_argument, naming the energy point and row, if the model is invalid; the previous model is then kept.

Definition at line 92 of file CaloScoringPlanePID.cxx.

95 {
96 // Validate everything before changing the model, so that a rejected model
97 // leaves the previous one in place.
98 const size_t n = energies.size();
99 if (n == 0) Invalid("no energy points");
100 if (matrices.size() != 9 * n) {
101 Invalid("expected " + std::to_string(9 * n) +
102 " matrix values (9 per energy point), got " +
103 std::to_string(matrices.size()));
104 }
105 for (size_t k = 0; k < n; ++k) {
106 std::ostringstream where;
107 where << "energy point " << k << " (" << energies[k] << " GeV)";
108 if (!std::isfinite(energies[k])) Invalid(where.str() + ": not finite");
109 if (k > 0 && energies[k] <= energies[k - 1]) {
110 Invalid(where.str() + ": energies must be strictly increasing");
111 }
112 if (logInterpolation && energies[k] <= 0.) {
113 Invalid(where.str() + ": log interpolation needs positive energies");
114 }
115 for (Int_t i = 0; i < kNSpecies; ++i) {
116 const std::string row = where.str() + ", true " + SpeciesName(i) + " row";
117 double sum = 0.;
118 for (Int_t j = 0; j < kNSpecies; ++j) {
119 const double x = matrices[9 * k + static_cast<size_t>(3 * i + j)];
120 if (!std::isfinite(x)) Invalid(row + ": entry not finite");
121 if (x < 0.) Invalid(row + ": negative entry");
122 sum += x;
123 }
124 if (std::fabs(sum - 1.) > kRowSumTolerance) {
125 std::ostringstream os;
126 os << row << " sums to " << sum << ", expected 1";
127 Invalid(os.str());
128 }
129 }
130 }
131 std::map<Int_t, Int_t> byAbsPdg;
132 for (const auto& [pdg, species] : overrides) {
133 if (species < kUnknown || species >= kNSpecies) {
134 Invalid("override for PDG code " + std::to_string(pdg) + ": species " +
135 std::to_string(species) +
136 " is not -1 (unknown), 0 (electron), 1 (hadron) or 2 (muon)");
137 }
138 const auto [it, inserted] = byAbsPdg.emplace(std::abs(pdg), species);
139 if (!inserted && it->second != species) {
140 Invalid("conflicting overrides for |PDG code| " +
141 std::to_string(std::abs(pdg)));
142 }
143 }
144 fEnergies = energies;
145 fMatrices = matrices;
146 fOverrides = std::move(byAbsPdg);
147 fLogInterpolation = logInterpolation;
148}

◆ SetIdentity()

void CaloScoringPlanePID::SetIdentity ( )
private

Definition at line 84 of file CaloScoringPlanePID.cxx.

84 {
85 fEnergies = {1.0};
86 fMatrices.assign(9, 0.);
87 for (Int_t i = 0; i < kNSpecies; ++i) fMatrices[3 * i + i] = 1.;
88 fOverrides.clear();
89 fLogInterpolation = kFALSE;
90}

◆ SpeciesName()

const char * CaloScoringPlanePID::SpeciesName ( Int_t species)
static

Definition at line 252 of file CaloScoringPlanePID.cxx.

252 {
253 switch (species) {
254 case kElectron:
255 return "electron";
256 case kHadron:
257 return "hadron";
258 case kMuon:
259 return "muon";
260 default:
261 return "unknown";
262 }
263}

◆ TrueSpecies()

CaloScoringPlanePID::Species CaloScoringPlanePID::TrueSpecies ( Int_t pdg) const

True species for a PDG code (overrides applied).

Definition at line 150 of file CaloScoringPlanePID.cxx.

150 {
151 const Int_t a = std::abs(pdg);
152 if (auto it = fOverrides.find(a); it != fOverrides.end()) {
153 return static_cast<Species>(it->second);
154 }
155 if (a == 11) return kElectron;
156 if (a == 13) return kMuon;
157 if (IsHadronCode(a)) return kHadron;
158 return kUnknown;
159}

Member Data Documentation

◆ fEnergies

std::vector<Double_t> CaloScoringPlanePID::fEnergies
private

GeV, strictly increasing.

Definition at line 129 of file CaloScoringPlanePID.h.

◆ fLogInterpolation

Bool_t CaloScoringPlanePID::fLogInterpolation = kFALSE
private

Definition at line 132 of file CaloScoringPlanePID.h.

◆ fMatrices

std::vector<Double_t> CaloScoringPlanePID::fMatrices
private

9 per energy, row-major, internal order

Definition at line 130 of file CaloScoringPlanePID.h.

◆ fOverrides

std::map<Int_t, Int_t> CaloScoringPlanePID::fOverrides
private

|pdg| -> Species

Definition at line 131 of file CaloScoringPlanePID.h.

◆ kNSpecies

Int_t CaloScoringPlanePID::kNSpecies = 3
staticconstexpr

Definition at line 56 of file CaloScoringPlanePID.h.


The documentation for this class was generated from the following files: