File indexing completed on 2026-12-04 10:46:44
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010
0011 #include <format>
0012 #include <map>
0013 #include <math.h>
0014 #include <algorithms/logger.h>
0015
0016
0017 #include <TVector3.h>
0018
0019
0020 #include <Evaluator/DD4hepUnits.h>
0021
0022
0023 #include <edm4eic/CherenkovParticleIDCollection.h>
0024 #include <edm4hep/ParticleIDCollection.h>
0025
0026 namespace eicrecon {
0027
0028
0029 namespace Tools {
0030
0031
0032
0033
0034 inline std::unordered_map<int, std::string> GetRadiatorIDs() {
0035 return std::unordered_map<int, std::string>{{0, "Aerogel"}, {1, "Gas"}};
0036 }
0037
0038 inline std::string GetRadiatorName(int id) {
0039 std::string name;
0040 try {
0041 name = GetRadiatorIDs().at(id);
0042 } catch (const std::out_of_range& e) {
0043 throw std::runtime_error(std::format(
0044 "RUNTIME ERROR: unknown radiator ID={} in algorithms/pid/Tools::GetRadiatorName", id));
0045 }
0046 return name;
0047 }
0048
0049 inline int GetRadiatorID(std::string name) {
0050 for (auto& [id, name_tmp] : GetRadiatorIDs())
0051 if (name == name_tmp)
0052 return id;
0053 throw std::runtime_error(std::format(
0054 "RUNTIME ERROR: unknown radiator '{}' in algorithms/pid/Tools::GetRadiatorID", name));
0055 return -1;
0056 }
0057
0058
0059
0060
0061
0062 inline std::vector<std::pair<double, double>>
0063 ApplyFineBinning(const std::vector<std::pair<double, double>>& input, unsigned nbins) {
0064 std::vector<std::pair<double, double>> ret;
0065
0066
0067 std::map<double, double> buffer;
0068
0069 for (auto entry : input)
0070 buffer[entry.first] = entry.second;
0071
0072
0073 if (buffer.size() < 2 || nbins < 2)
0074 return ret;
0075
0076 double from = (*buffer.begin()).first;
0077 double to = (*buffer.rbegin()).first;
0078
0079 double step = (to - from) / nbins;
0080
0081 for (auto entry : buffer) {
0082 double e1 = entry.first;
0083 double qe1 = entry.second;
0084
0085 if (!ret.size())
0086 ret.push_back(std::make_pair(e1, qe1));
0087 else {
0088 const auto& prev = ret[ret.size() - 1];
0089
0090 double e0 = prev.first;
0091 double qe0 = prev.second;
0092 double a = (qe1 - qe0) / (e1 - e0);
0093 double b = qe0 - a * e0;
0094
0095
0096 for (double e = e0 + step; e < e1; e += step)
0097 ret.push_back(std::make_pair(e, a * e + b));
0098 }
0099 }
0100
0101 return ret;
0102 }
0103
0104
0105
0106 inline bool GetFinelyBinnedTableEntry(const std::vector<std::pair<double, double>>& table,
0107 double argument, double* entry) {
0108
0109
0110 unsigned dim = table.size();
0111 if (dim < 2)
0112 return false;
0113
0114
0115 auto const& from = table[0];
0116 auto const& to = table[dim - 1];
0117 double emin = from.first;
0118 double emax = to.first;
0119 double step = (emax - emin) / (dim - 1);
0120 int ibin = (int)floor((argument - emin) / step);
0121
0122
0123
0124
0125 if (ibin < 0 || ibin >= int(dim))
0126 return false;
0127
0128 *entry = table[ibin].second;
0129 return true;
0130 }
0131
0132
0133
0134 template <class PodioVector3> TVector3 PodioVector3_to_TVector3(const PodioVector3 v) {
0135 return TVector3(v.x, v.y, v.z);
0136 }
0137
0138 template <class MathVector3> TVector3 MathVector3_to_TVector3(MathVector3 v) {
0139 return TVector3(v.x(), v.y(), v.z());
0140 }
0141
0142
0143
0144
0145 inline std::string PrintTVector3(std::string name, TVector3 vec, int nameBuffer = 30) {
0146 return std::format("{:>{}} = ( {:>10.2f} {:>10.2f} {:>10.2f} )", name, nameBuffer, vec.x(),
0147 vec.y(), vec.z());
0148 }
0149
0150
0151 inline std::string HypothesisTableHead(int indent = 4) {
0152 return std::format("{:{}}{:>6} {:>10} {:>10}", "", indent, "PDG", "Weight", "NPE");
0153 }
0154 inline std::string HypothesisTableLine(edm4eic::CherenkovParticleIDHypothesis hyp,
0155 int indent = 4) {
0156 return std::format("{:{}}{:>6} {:>10.8} {:>10.8}", "", indent, hyp.PDG, hyp.weight, hyp.npe);
0157 }
0158 inline std::string HypothesisTableLine(edm4hep::ParticleID hyp, int indent = 4) {
0159 float npe =
0160 hyp.parameters_size() > 0 ? hyp.getParameters(0) : -1;
0161 return std::format("{:{}}{:>6} {:>10.8} {:>10.8}", "", indent, hyp.getPDG(),
0162 hyp.getLikelihood(), npe);
0163 }
0164
0165 };
0166
0167 }