ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimMultiTruth.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef TRIGFPGATrackSimOBJECTS_MULTITRUTH_H
6#define TRIGFPGATrackSimOBJECTS_MULTITRUTH_H
7
8#include <TObject.h>
9
10#include <algorithm>
11#include <cassert>
12#include <cmath>
13#include <iomanip>
14#include <iostream>
15#include <limits>
16#include <map>
17#include <numeric>
18#include <string>
19#include <utility>
20#include <vector>
21
22// FPGATrackSimMultiTruth
23// ================================================================
24// code to match clusters and tracks to GEANT charge deposition information
25// ================================================================
26// 20-04-2009 Antonio Boveia (boveia@hep.uchicago.edu)
27//
28// class which represents the relative contributions of one or more
29// truth particles (identified by a unique "barcode") to some
30// measurement. for example, a single silicon pixel may record the
31// summed ionization from two different charged particles passing
32// through it during the same event. one particle may (and likely
33// will) contribute more charge than another, so the class stores a
34// weight for each barcode which can be used to incorporate this
35// information.
36//
37// there are two ways of combining the information: the "add" function
38// and the "maximize" function. "add" sums the weights of individual
39// contributions, appropriate for combining the truth for individual
40// hits to obtain the truth for a track. in this case, "best" then
41// returns the barcode with the largest sum of weights. "maximize"
42// simply remembers the barcode with the greatest weight, and "best"
43// then returns that barcode.
44//
45
47public:
48
49 typedef std::pair<unsigned long, unsigned long> Barcode; // = (event index, barcode)
50 typedef float Weight;
51 typedef std::map<Barcode, Weight> TruthMap;
52
55 virtual ~FPGATrackSimMultiTruth() = default;
56
58 auto operator()(const FPGATrackSimMultiTruth& result, const FPGATrackSimMultiTruth& a) const { return result.add(a); }
59 };
60
62 auto operator()(const FPGATrackSimMultiTruth& result, const FPGATrackSimMultiTruth& a) const { return result.maximize(a); }
63 };
64
65 auto begin() { return m_truth.begin(); }
66 auto end() { return m_truth.end(); }
67 auto begin() const { return m_truth.begin(); }
68 auto end() const { return m_truth.end(); }
69
70 bool isEmpty() const { return m_truth.empty(); }
71
72 unsigned long best_barcode() const;
73
75 void add(const FPGATrackSimMultiTruth& rval);
76
78 void maximize(const FPGATrackSimMultiTruth& rval);
79
81
82 inline unsigned multiplicity() const { return m_truth.size(); }
83
84 // Finds the best barcode and its normalized weight, returning them by reference.
85 // Returns true on success.
87 {
88 if (m_truth.empty()) return false;
89 auto i = std::max_element(m_truth.begin(), m_truth.end(), TruthMapWeightLt());
90 code = i->first;
91 weight = total_weight() > 0. ? (i->second) / total_weight() : 0.;
92 return true;
93 }
94
95
96private:
97
99 auto operator()(const FPGATrackSimMultiTruth::Weight& result, const TruthMap::value_type& a) const { return result + a.second; }
100 };
101
103 unsigned long int bad = std::numeric_limits<unsigned long>::max();
104 bool operator()(const TruthMap::value_type& a, const TruthMap::value_type& b) const {
105 const bool a_info = (a.first.first != bad) && (a.first.second != bad);
106 const bool b_info = (b.first.first != bad) && (b.first.second != bad);
107 return a_info && b_info ? a.second < b.second : b_info;
108 }
109 };
110
111 // add and mult for std::accumulate
112 const FPGATrackSimMultiTruth add(const FPGATrackSimMultiTruth& rval) const;
114
115 // matching probability definition and maximization logic
116 inline FPGATrackSimMultiTruth::Weight total_weight() const { return std::accumulate(m_truth.begin(), m_truth.end(), 0., TruthMapWeightAcc()); }
117
119 return m_truth.empty() || (m_truth.find(code) == m_truth.end()) ? 0. : ((m_truth.find(code))->second) / total_weight();
120 }
121
123
124
125 ClassDefNV(FPGATrackSimMultiTruth, 3)
126};
127std::ostream& operator<<(std::ostream& o, const FPGATrackSimMultiTruth& mt);
128
129
130#endif // TRIGFPGATrackSimOBJECTS_MULTITRUTH_H
std::ostream & operator<<(std::ostream &o, const FPGATrackSimMultiTruth &mt)
static Double_t a
void add(const FPGATrackSimMultiTruth::Barcode &code, const FPGATrackSimMultiTruth::Weight &weight)
FPGATrackSimMultiTruth()=default
void maximize(const FPGATrackSimMultiTruth::Barcode &code, const FPGATrackSimMultiTruth::Weight &weight)
bool best(FPGATrackSimMultiTruth::Barcode &code, FPGATrackSimMultiTruth::Weight &weight) const
std::pair< unsigned long, unsigned long > Barcode
virtual ~FPGATrackSimMultiTruth()=default
FPGATrackSimMultiTruth(const FPGATrackSimMultiTruth::Barcode &code, const FPGATrackSimMultiTruth::Weight weight=1.)
unsigned long best_barcode() const
FPGATrackSimMultiTruth::Weight total_weight() const
FPGATrackSimMultiTruth::Weight weight(const FPGATrackSimMultiTruth::Barcode &code) const
std::map< Barcode, Weight > TruthMap
auto operator()(const FPGATrackSimMultiTruth &result, const FPGATrackSimMultiTruth &a) const
auto operator()(const FPGATrackSimMultiTruth &result, const FPGATrackSimMultiTruth &a) const
auto operator()(const FPGATrackSimMultiTruth::Weight &result, const TruthMap::value_type &a) const
bool operator()(const TruthMap::value_type &a, const TruthMap::value_type &b) const