EICrecon
JANA based reconstruction for the EPIC detector
Loading...
Searching...
No Matches
Tools.h
Go to the documentation of this file.
1// Copyright 2023, Alexander Kiselev, Christopher Dilks
2// Subject to the terms in the LICENSE file found in the top-level directory.
3//
4// Common functions for PID algorithms
5// Several methods ported from Juggler's JugPID `IRTAlgorithmServices`
6//
7
8#pragma once
9
10// general
11#include <map>
12#include <math.h>
13#include <algorithms/logger.h>
14
15// ROOT
16#include <TVector3.h>
17
18// DD4hep
19#include <Evaluator/DD4hepUnits.h>
20
21// data model
22#include <edm4eic/CherenkovParticleIDCollection.h>
23#include <edm4hep/ParticleIDCollection.h>
24
25namespace eicrecon {
26
27// Tools namespace, filled with miscellaneous helper functions
28namespace Tools {
29
30 // -------------------------------------------------------------------------------------
31 // Radiator IDs
32
33 inline std::unordered_map<int, std::string> GetRadiatorIDs() {
34 return std::unordered_map<int, std::string>{{0, "Aerogel"}, {1, "Gas"}};
35 }
36
37 inline std::string GetRadiatorName(int id) {
38 std::string name;
39 try {
40 name = GetRadiatorIDs().at(id);
41 } catch (const std::out_of_range& e) {
42 throw std::runtime_error(fmt::format(
43 "RUNTIME ERROR: unknown radiator ID={} in algorithms/pid/Tools::GetRadiatorName", id));
44 }
45 return name;
46 }
47
48 inline int GetRadiatorID(std::string name) {
49 for (auto& [id, name_tmp] : GetRadiatorIDs())
50 if (name == name_tmp)
51 return id;
52 throw std::runtime_error(fmt::format(
53 "RUNTIME ERROR: unknown radiator '{}' in algorithms/pid/Tools::GetRadiatorID", name));
54 return -1;
55 }
56
57 // -------------------------------------------------------------------------------------
58 // Table rebinning and lookup
59
60 // Rebin input table `input` to have `nbins+1` equidistant bins; returns the rebinned table
61 inline std::vector<std::pair<double, double>>
62 ApplyFineBinning(const std::vector<std::pair<double, double>>& input, unsigned nbins) {
63 std::vector<std::pair<double, double>> ret;
64
65 // Well, could have probably just reordered the initial vector;
66 std::map<double, double> buffer;
67
68 for (auto entry : input)
69 buffer[entry.first] = entry.second;
70
71 // Sanity checks; return empty map in case do not pass them;
72 if (buffer.size() < 2 || nbins < 2)
73 return ret;
74
75 double from = (*buffer.begin()).first;
76 double to = (*buffer.rbegin()).first;
77 // Will be "nbins+1" equidistant entries;
78 double step = (to - from) / nbins;
79
80 for (auto entry : buffer) {
81 double e1 = entry.first;
82 double qe1 = entry.second;
83
84 if (!ret.size())
85 ret.push_back(std::make_pair(e1, qe1));
86 else {
87 const auto& prev = ret[ret.size() - 1];
88
89 double e0 = prev.first;
90 double qe0 = prev.second;
91 double a = (qe1 - qe0) / (e1 - e0);
92 double b = qe0 - a * e0;
93 // FIXME: check floating point accuracy when moving to a next point; do we actually
94 // care whether the overall number of bins will be "nbins+1" or more?;
95 for (double e = e0 + step; e < e1; e += step)
96 ret.push_back(std::make_pair(e, a * e + b));
97 } //if
98 } //for entry
99
100 return ret;
101 }
102
103 // Find the bin in table `table` that contains entry `argument` in the first column and
104 // sets `entry` to the corresponding element of the second column; returns true if successful
105 inline bool GetFinelyBinnedTableEntry(const std::vector<std::pair<double, double>>& table,
106 double argument, double* entry) {
107 // Get the tabulated table reference; perform sanity checks;
108 //const std::vector<std::pair<double, double>> &qe = u_quantumEfficiency.value();
109 unsigned dim = table.size();
110 if (dim < 2)
111 return false;
112
113 // Find a proper bin; no tricks, they are all equidistant;
114 auto const& from = table[0];
115 auto const& to = table[dim - 1];
116 double emin = from.first;
117 double emax = to.first;
118 double step = (emax - emin) / (dim - 1);
119 int ibin = (int)floor((argument - emin) / step);
120
121 //printf("%f vs %f, %f -> %d\n", ev, from.first, to. first, ibin);
122
123 // Out of range check;
124 if (ibin < 0 || ibin >= int(dim))
125 return false;
126
127 *entry = table[ibin].second;
128 return true;
129 }
130
131 // -------------------------------------------------------------------------------------
132 // convert PODIO vector datatype to ROOT TVector3
133 template <class PodioVector3> TVector3 PodioVector3_to_TVector3(const PodioVector3 v) {
134 return TVector3(v.x, v.y, v.z);
135 }
136 // convert ROOT::Math::Vector to ROOT TVector3
137 template <class MathVector3> TVector3 MathVector3_to_TVector3(MathVector3 v) {
138 return TVector3(v.x(), v.y(), v.z());
139 }
140
141 // -------------------------------------------------------------------------------------
142
143 // printing: vectors
144 inline std::string PrintTVector3(std::string name, TVector3 vec, int nameBuffer = 30) {
145 return fmt::format("{:>{}} = ( {:>10.2f} {:>10.2f} {:>10.2f} )", name, nameBuffer, vec.x(),
146 vec.y(), vec.z());
147 }
148
149 // printing: hypothesis tables
150 inline std::string HypothesisTableHead(int indent = 4) {
151 return fmt::format("{:{}}{:>6} {:>10} {:>10}", "", indent, "PDG", "Weight", "NPE");
152 }
153 inline std::string HypothesisTableLine(edm4eic::CherenkovParticleIDHypothesis hyp,
154 int indent = 4) {
155 return fmt::format("{:{}}{:>6} {:>10.8} {:>10.8}", "", indent, hyp.PDG, hyp.weight, hyp.npe);
156 }
157 inline std::string HypothesisTableLine(edm4hep::ParticleID hyp, int indent = 4) {
158 float npe =
159 hyp.parameters_size() > 0 ? hyp.getParameters(0) : -1; // assume NPE is the first parameter
160 return fmt::format("{:{}}{:>6} {:>10.8} {:>10.8}", "", indent, hyp.getPDG(),
161 hyp.getLikelihood(), npe);
162 }
163
164}; // namespace Tools
165
166} // namespace eicrecon
int GetRadiatorID(std::string name)
Definition Tools.h:48
std::unordered_map< int, std::string > GetRadiatorIDs()
Definition Tools.h:33
std::string HypothesisTableLine(edm4eic::CherenkovParticleIDHypothesis hyp, int indent=4)
Definition Tools.h:153
TVector3 MathVector3_to_TVector3(MathVector3 v)
Definition Tools.h:137
std::vector< std::pair< double, double > > ApplyFineBinning(const std::vector< std::pair< double, double > > &input, unsigned nbins)
Definition Tools.h:62
std::string HypothesisTableHead(int indent=4)
Definition Tools.h:150
TVector3 PodioVector3_to_TVector3(const PodioVector3 v)
Definition Tools.h:133
bool GetFinelyBinnedTableEntry(const std::vector< std::pair< double, double > > &table, double argument, double *entry)
Definition Tools.h:105
std::string GetRadiatorName(int id)
Definition Tools.h:37
std::string PrintTVector3(std::string name, TVector3 vec, int nameBuffer=30)
Definition Tools.h:144
-client
Definition CalorimeterClusterRecoCoG.cc:37