ATLAS Offline Software
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 
47 public:
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 
57  struct AddAccumulator {
58  auto operator()(const FPGATrackSimMultiTruth& result, const FPGATrackSimMultiTruth& a) const { return result.add(a); }
59  };
60 
61  struct MaxAccumulator {
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 
96 private:
97 
99  auto operator()(const FPGATrackSimMultiTruth::Weight& result, const TruthMap::value_type& a) const { return result + a.second; }
100  };
101 
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;
113  const FPGATrackSimMultiTruth maximize(const FPGATrackSimMultiTruth& rval) const;
114 
115  // matching probability definition and maximization logic
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 };
127 std::ostream& operator<<(std::ostream& o, const FPGATrackSimMultiTruth& mt);
128 
129 
130 #endif // TRIGFPGATrackSimOBJECTS_MULTITRUTH_H
FPGATrackSimMultiTruth::TruthMapWeightLt
Definition: FPGATrackSimMultiTruth.h:102
FPGATrackSimMultiTruth::FPGATrackSimMultiTruth
FPGATrackSimMultiTruth(const FPGATrackSimMultiTruth::Barcode &code, const FPGATrackSimMultiTruth::Weight weight=1.)
Definition: FPGATrackSimMultiTruth.h:54
get_generator_info.result
result
Definition: get_generator_info.py:21
max
#define max(a, b)
Definition: cfImp.cxx:41
FPGATrackSimMultiTruth::m_truth
TruthMap m_truth
Definition: FPGATrackSimMultiTruth.h:122
FPGATrackSimMultiTruth::begin
auto begin() const
Definition: FPGATrackSimMultiTruth.h:67
FPGATrackSimMultiTruth::Weight
float Weight
Definition: FPGATrackSimMultiTruth.h:50
FPGATrackSimMultiTruth::isEmpty
bool isEmpty() const
Definition: FPGATrackSimMultiTruth.h:70
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
FPGATrackSimMultiTruth::begin
auto begin()
Definition: FPGATrackSimMultiTruth.h:65
FPGATrackSimMultiTruth::TruthMapWeightLt::operator()
bool operator()(const TruthMap::value_type &a, const TruthMap::value_type &b) const
Definition: FPGATrackSimMultiTruth.h:104
FPGATrackSimMultiTruth::~FPGATrackSimMultiTruth
virtual ~FPGATrackSimMultiTruth()=default
FPGATrackSimMultiTruth::TruthMapWeightAcc::operator()
auto operator()(const FPGATrackSimMultiTruth::Weight &result, const TruthMap::value_type &a) const
Definition: FPGATrackSimMultiTruth.h:99
FPGATrackSimMultiTruth::best_barcode
unsigned long best_barcode() const
Definition: FPGATrackSimMultiTruth.cxx:16
FPGATrackSimMultiTruth::multiplicity
unsigned multiplicity() const
Definition: FPGATrackSimMultiTruth.h:82
FPGATrackSimMultiTruth::best
bool best(FPGATrackSimMultiTruth::Barcode &code, FPGATrackSimMultiTruth::Weight &weight) const
Definition: FPGATrackSimMultiTruth.h:86
histSizes.code
code
Definition: histSizes.py:129
lumiFormat.i
int i
Definition: lumiFormat.py:85
FPGATrackSimMultiTruth::add
void add(const FPGATrackSimMultiTruth::Barcode &code, const FPGATrackSimMultiTruth::Weight &weight)
Definition: FPGATrackSimMultiTruth.cxx:22
FPGATrackSimMultiTruth::Barcode
std::pair< unsigned long, unsigned long > Barcode
Definition: FPGATrackSimMultiTruth.h:49
FPGATrackSimMultiTruth::AddAccumulator::operator()
auto operator()(const FPGATrackSimMultiTruth &result, const FPGATrackSimMultiTruth &a) const
Definition: FPGATrackSimMultiTruth.h:58
FPGATrackSimMultiTruth::FPGATrackSimMultiTruth
FPGATrackSimMultiTruth()=default
FPGATrackSimMultiTruth::weight
FPGATrackSimMultiTruth::Weight weight(const FPGATrackSimMultiTruth::Barcode &code) const
Definition: FPGATrackSimMultiTruth.h:118
operator<<
std::ostream & operator<<(std::ostream &o, const FPGATrackSimMultiTruth &mt)
Definition: FPGATrackSimMultiTruth.cxx:74
FPGATrackSimMultiTruth::MaxAccumulator::operator()
auto operator()(const FPGATrackSimMultiTruth &result, const FPGATrackSimMultiTruth &a) const
Definition: FPGATrackSimMultiTruth.h:62
FPGATrackSimMultiTruth::maximize
void maximize(const FPGATrackSimMultiTruth::Barcode &code, const FPGATrackSimMultiTruth::Weight &weight)
Definition: FPGATrackSimMultiTruth.cxx:36
FPGATrackSimMultiTruth
Definition: FPGATrackSimMultiTruth.h:46
FPGATrackSimMultiTruth::AddAccumulator
Definition: FPGATrackSimMultiTruth.h:57
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
FPGATrackSimMultiTruth::TruthMapWeightAcc
Definition: FPGATrackSimMultiTruth.h:98
a
TList * a
Definition: liststreamerinfos.cxx:10
FPGATrackSimMultiTruth::TruthMap
std::map< Barcode, Weight > TruthMap
Definition: FPGATrackSimMultiTruth.h:51
FPGATrackSimMultiTruth::end
auto end()
Definition: FPGATrackSimMultiTruth.h:66
FPGATrackSimMultiTruth::MaxAccumulator
Definition: FPGATrackSimMultiTruth.h:61
FPGATrackSimMultiTruth::total_weight
FPGATrackSimMultiTruth::Weight total_weight() const
Definition: FPGATrackSimMultiTruth.h:116
FPGATrackSimMultiTruth::TruthMapWeightLt::bad
unsigned long int bad
Definition: FPGATrackSimMultiTruth.h:103
FPGATrackSimMultiTruth::assign_equal_normalization
void assign_equal_normalization()
Definition: FPGATrackSimMultiTruth.cxx:69
FPGATrackSimMultiTruth::end
auto end() const
Definition: FPGATrackSimMultiTruth.h:68