Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 10:46:44

0001 // Copyright 2023, Alexander Kiselev, Christopher Dilks
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 //
0004 // Common functions for PID algorithms
0005 // Several methods ported from Juggler's JugPID `IRTAlgorithmServices`
0006 //
0007 
0008 #pragma once
0009 
0010 // general
0011 #include <format>
0012 #include <map>
0013 #include <math.h>
0014 #include <algorithms/logger.h>
0015 
0016 // ROOT
0017 #include <TVector3.h>
0018 
0019 // DD4hep
0020 #include <Evaluator/DD4hepUnits.h>
0021 
0022 // data model
0023 #include <edm4eic/CherenkovParticleIDCollection.h>
0024 #include <edm4hep/ParticleIDCollection.h>
0025 
0026 namespace eicrecon {
0027 
0028 // Tools namespace, filled with miscellaneous helper functions
0029 namespace Tools {
0030 
0031   // -------------------------------------------------------------------------------------
0032   // Radiator IDs
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   // Table rebinning and lookup
0060 
0061   // Rebin input table `input` to have `nbins+1` equidistant bins; returns the rebinned table
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     // Well, could have probably just reordered the initial vector;
0067     std::map<double, double> buffer;
0068 
0069     for (auto entry : input)
0070       buffer[entry.first] = entry.second;
0071 
0072     // Sanity checks; return empty map in case do not pass them;
0073     if (buffer.size() < 2 || nbins < 2)
0074       return ret;
0075 
0076     double from = (*buffer.begin()).first;
0077     double to   = (*buffer.rbegin()).first;
0078     // Will be "nbins+1" equidistant entries;
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         // FIXME: check floating point accuracy when moving to a next point; do we actually
0095         // care whether the overall number of bins will be "nbins+1" or more?;
0096         for (double e = e0 + step; e < e1; e += step)
0097           ret.push_back(std::make_pair(e, a * e + b));
0098       } //if
0099     } //for entry
0100 
0101     return ret;
0102   }
0103 
0104   // Find the bin in table `table` that contains entry `argument` in the first column and
0105   // sets `entry` to the corresponding element of the second column; returns true if successful
0106   inline bool GetFinelyBinnedTableEntry(const std::vector<std::pair<double, double>>& table,
0107                                         double argument, double* entry) {
0108     // Get the tabulated table reference; perform sanity checks;
0109     //const std::vector<std::pair<double, double>> &qe = u_quantumEfficiency.value();
0110     unsigned dim = table.size();
0111     if (dim < 2)
0112       return false;
0113 
0114     // Find a proper bin; no tricks, they are all equidistant;
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     //printf("%f vs %f, %f -> %d\n", ev, from.first, to. first, ibin);
0123 
0124     // Out of range check;
0125     if (ibin < 0 || ibin >= int(dim))
0126       return false;
0127 
0128     *entry = table[ibin].second;
0129     return true;
0130   }
0131 
0132   // -------------------------------------------------------------------------------------
0133   // convert PODIO vector datatype to ROOT TVector3
0134   template <class PodioVector3> TVector3 PodioVector3_to_TVector3(const PodioVector3 v) {
0135     return TVector3(v.x, v.y, v.z);
0136   }
0137   // convert ROOT::Math::Vector to ROOT TVector3
0138   template <class MathVector3> TVector3 MathVector3_to_TVector3(MathVector3 v) {
0139     return TVector3(v.x(), v.y(), v.z());
0140   }
0141 
0142   // -------------------------------------------------------------------------------------
0143 
0144   // printing: vectors
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   // printing: hypothesis tables
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; // assume NPE is the first parameter
0161     return std::format("{:{}}{:>6}  {:>10.8}  {:>10.8}", "", indent, hyp.getPDG(),
0162                        hyp.getLikelihood(), npe);
0163   }
0164 
0165 }; // namespace Tools
0166 
0167 } // namespace eicrecon