ATLAS Offline Software
MeasurementToTruthAssociationAlg.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 ACTSTRKFINDING_MEASUREMENTTOTRUTHASSOCIATIONALG_H
6 #define ACTSTRKFINDING_MEASUREMENTTOTRUTHASSOCIATIONALG_H 1
7 
8 // Base Class
10 
11 // Gaudi includes
12 #include "Gaudi/Property.h"
13 
14 // Handle Keys
17 
18 #include "Identifier/Identifier.h"
20 
21 #include <string>
22 #include <memory>
23 #include <array>
24 #include <atomic>
25 #include <type_traits>
26 
27 #include <cmath>
28 #include <iomanip>
29 #include <ostream>
30 #include <string>
31 #include <sstream>
32 #include <vector>
33 
34 #include "ActsInterop/StatUtils.h"
35 
36 namespace Dbg {
37 using ActsUtils::Stat;
39 using ActsUtils::operator<<;
40 
41 struct HistTemp {
42  mutable Dbg::StatHist m_depositedEnergy ATLAS_THREAD_SAFE {20, -6., 14.};
43  mutable Dbg::StatHist m_particlesPerMeasurement ATLAS_THREAD_SAFE {20, -.5, 20.-.5};
44  mutable Dbg::StatHist m_measurementsPerParticle ATLAS_THREAD_SAFE {20, -0.5, 20.-0.5};
45  HistTemp() = default;
47  : m_depositedEnergy(a.m_depositedEnergy.createEmptyClone()),
48  m_particlesPerMeasurement( a.m_particlesPerMeasurement.createEmptyClone()),
49  m_measurementsPerParticle( a.m_measurementsPerParticle.createEmptyClone())
50  {
51  }
52 };
53 struct Hist : public HistTemp {
54  using HistTemp::HistTemp;
56 };
57 
58 struct Empty {
59  Empty() =default;
60  Empty(const Empty &) =default;
61 };
62 
63 }
64 
65 namespace ActsTrk
66 {
67 
68  // Helper class to emulate an RDO list for a type which provides a single RDO
69  template<class T>
70  class DummyRDOList {
71  private:
72  const T *m_ptr;
73  public:
74  DummyRDOList(const T &a) : m_ptr(&a) {}
75  struct const_iterator {
76  const T *m_ptr;
77  const_iterator(const T *a_ptr) : m_ptr(a_ptr) {}
78  Identifier operator*() const { return m_ptr->identifier(); }
79  bool operator!=(const const_iterator &other) const {
80  return m_ptr != other.m_ptr;
81  }
82  const_iterator &operator++() { ++m_ptr; return *this;}
83  };
85  return const_iterator(m_ptr);
86  }
87  const_iterator end() const {
88  return const_iterator(m_ptr+1);
89  }
90  };
91 
92  // helper templates to detect whether a type has the method rdoList
93  template <typename Object>
94  using rdoListFunc_t = decltype(std::declval<Object>().rdoList());
95 
96  // template being used for types that do not have a member function called rdoList
97  template <typename Object, typename = std::void_t<> >
98  struct has_rdoList : std::false_type{};
99 
100  // template being used for types that have the member function rdoList
101  template <typename Object >
102  struct has_rdoList<Object, std::void_t<rdoListFunc_t<Object> > > : std::true_type{};
103 
104  // helper template to get a list of RDOs associated to the given measurement
105  // if the measurement does not provide an RDO list but only is composed of a single measurement
106  // return something which behaves like a RDO list but contains only a single identifier.
107  // the optimizer (with O2) should take care of eliminating the bogus loop.
108  template <class T>
109  auto getRDOList(const T &a) {
110  if constexpr(has_rdoList<T>::value) {
111  return a.rdoList();
112  }
113  else {
114  return DummyRDOList(a);
115  }
116  }
117 
118  // specialisations need to be implemented for each T_TruthEventCollection used for the MeasurementToTruthAssociationAlg
119  // @return helper class to get TruthParticles from a deposit, and test whether a truth particle is from the hard scatter event
120  // The helper class has to implement getTruthParticle, isHardScatter which are functions from Deposits, where deposits are
121  // elements from the collection returned by getSimDataDeposits, which is a function of the sim data collection
122  template <class T_TruthEventCollection>
123  auto makeDepositToTruthParticleMap(const T_TruthEventCollection *truth_particle_links);
124 
125  // specialisation need to be implemented for each T_TruthEventCollection used for the MeasurementToTruthAssociationAlg
126  template <class T_TruthEventCollection>
127  inline const char *getInTruthPropertyName();
128 
129  // specialisations need to be implemented for each T_SimDataCollection/T_SimDataIterator used for the MeasurementToTruthAssociationAlg
130  template <class T_SimDataCollection, class T_SimDataIterator>
131  auto getSimDataDeposits(const T_SimDataCollection &sim_data_collection, T_SimDataIterator sim_data_iter_for_identifier);
132 
133  // specialisations need to be implemented for each T_SimDataCollection used for the MeasurementToTruthAssociationAlg
134  template <class T_Deposit>
135  float getDepositedEnergy(const T_Deposit &);
136 
139  template <class T_MeasurementCollection, class T_SimDataCollection, class T_TruthEventCollection, bool IsDebug=false >
141  {
142  public:
144  ISvcLocator *pSvcLocator);
145 
146  virtual StatusCode initialize() override;
147  virtual StatusCode finalize() override;
148  virtual StatusCode execute(const EventContext &ctx) const override;
149 
150  private:
152  {this, "Measurements",{}, "List of input measurement keys." };
154  {this, "SimData",{}, "List of input simulated data keys." };
156  {this,getInTruthPropertyName<T_TruthEventCollection>(),"","The key for the truth event collection e.g. McEventCollectionWrap if one is required."};
157 
159  {this, "AssociationMapOut","", "Output association map from measurements to generator particles." };
160 
161  Gaudi::Property<float> m_depositedEnergyMin
162  {this, "DepositedEnergyMin", 0., "Only consider gnerator particles which"
163  " deposed more than this amount of energy (in MeV)" };
164 
165  enum EStat {
172  };
173  // statistics counter
174  mutable std::array<std::atomic<std::size_t>,kNCategories> m_statRDO ATLAS_THREAD_SAFE;
175  // counter of deposits from truth particles of the hard scatter and pileup with or without
176  // associated xAOD truth particle.
177  mutable std::array<std::atomic<std::size_t>,4> m_depositCounts ATLAS_THREAD_SAFE {0u, 0u, 0u, 0u};
178  // optional histograms of deposits per particle, particles per measurement, and measurements per particle
180  };
181 
182 } // namespace
183 
184 #endif
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
Dbg::Hist
Definition: MeasurementToTruthAssociationAlg.h:53
ActsTrk::MeasurementToTruthAssociationAlg::kHasSimHit
@ kHasSimHit
Definition: MeasurementToTruthAssociationAlg.h:167
ActsTrk::MeasurementToTruthAssociationAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
ActsTrk::MeasurementToTruthAssociationAlg::kBeyondSmallVectorSize
@ kBeyondSmallVectorSize
Definition: MeasurementToTruthAssociationAlg.h:170
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
StatUtils.h
ActsTrk::makeDepositToTruthParticleMap
auto makeDepositToTruthParticleMap(const T_TruthEventCollection *truth_particle_links)
ActsUtils::StatHist
Extend Stat helper by an equidistant binned histogram.
Definition: StatUtils.h:80
ActsTrk::MeasurementToTruthAssociationAlg::kNCategories
@ kNCategories
Definition: MeasurementToTruthAssociationAlg.h:171
Dbg::Empty::Empty
Empty()=default
Dbg::HistTemp::HistTemp
HistTemp(const HistTemp &a)
Definition: MeasurementToTruthAssociationAlg.h:46
SG::ReadHandleKey< T_MeasurementCollection >
ActsTrk::MeasurementToTruthAssociationAlg::m_simDataKey
SG::ReadHandleKey< T_SimDataCollection > m_simDataKey
Definition: MeasurementToTruthAssociationAlg.h:154
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
Dbg::HistTemp
Definition: MeasurementToTruthAssociationAlg.h:41
Dbg::HistTemp::HistTemp
HistTemp()=default
Dbg::Empty::Empty
Empty(const Empty &)=default
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
ActsUtils::Stat
Simple class to gather statistics : min, max, mean, rms.
Definition: StatUtils.h:16
ActsTrk::MeasurementToTruthAssociationAlg
Algorithm template to associate measurements of a certain type to a xAOD truth particles using a sim ...
Definition: MeasurementToTruthAssociationAlg.h:141
SG::WriteHandleKey
Property holding a SG store/key/clid from which a WriteHandle is made.
Definition: StoreGate/StoreGate/WriteHandleKey.h:40
ActsTrk::getDepositedEnergy
float getDepositedEnergy(const T_Deposit &)
ActsTrk::MeasurementToTruthAssociationAlg::m_measurementKey
SG::ReadHandleKey< T_MeasurementCollection > m_measurementKey
Definition: MeasurementToTruthAssociationAlg.h:152
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ActsTrk::rdoListFunc_t
decltype(std::declval< Object >().rdoList()) rdoListFunc_t
Definition: MeasurementToTruthAssociationAlg.h:94
ActsTrk::DummyRDOList::end
const_iterator end() const
Definition: MeasurementToTruthAssociationAlg.h:87
ActsTrk::MeasurementToTruthAssociationAlg::ATLAS_THREAD_SAFE
std::array< std::atomic< std::size_t >, kNCategories > m_statRDO ATLAS_THREAD_SAFE
Definition: MeasurementToTruthAssociationAlg.h:174
ActsTrk::MeasurementToTruthAssociationAlg::kHasSimHitNoParticle
@ kHasSimHitNoParticle
Definition: MeasurementToTruthAssociationAlg.h:169
ActsTrk::MeasurementToTruthAssociationAlg< xAOD::StripClusterContainer, InDetSimDataCollection, xAODTruthParticleLinkVector, MeasurementToTruthAssociationDebugHistograms >::EStat
EStat
Definition: MeasurementToTruthAssociationAlg.h:165
ActsTrk::has_rdoList
Definition: MeasurementToTruthAssociationAlg.h:98
Dbg
Definition: MeasurementToTruthAssociationAlg.h:36
ActsTrk::MeasurementToTruthAssociationAlg::m_stat
std::conditional< IsDebug, Dbg::Hist, Dbg::Empty >::type m_stat
Definition: MeasurementToTruthAssociationAlg.h:179
AthReentrantAlgorithm.h
ActsTrk::DummyRDOList::m_ptr
const T * m_ptr
Definition: MeasurementToTruthAssociationAlg.h:72
ActsTrk::MeasurementToTruthAssociationAlg::m_truthEventCollectionKey
SG::ReadHandleKey< T_TruthEventCollection > m_truthEventCollectionKey
Definition: MeasurementToTruthAssociationAlg.h:156
WriteHandleKey.h
Property holding a SG store/key/clid from which a WriteHandle is made.
ActsTrk::DummyRDOList::const_iterator::operator!=
bool operator!=(const const_iterator &other) const
Definition: MeasurementToTruthAssociationAlg.h:79
ActsTrk::MeasurementToTruthAssociationAlg::m_depositedEnergyMin
Gaudi::Property< float > m_depositedEnergyMin
Definition: MeasurementToTruthAssociationAlg.h:162
ActsTrk::DummyRDOList::const_iterator::operator++
const_iterator & operator++()
Definition: MeasurementToTruthAssociationAlg.h:82
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ActsTrk::getRDOList
auto getRDOList(const T &a)
Definition: MeasurementToTruthAssociationAlg.h:109
ActsTrk::DummyRDOList::const_iterator
Definition: MeasurementToTruthAssociationAlg.h:75
ActsTrk::DummyRDOList::const_iterator::m_ptr
const T * m_ptr
Definition: MeasurementToTruthAssociationAlg.h:76
ActsTrk::MeasurementToTruthAssociationAlg::kNoTruth
@ kNoTruth
Definition: MeasurementToTruthAssociationAlg.h:166
a
TList * a
Definition: liststreamerinfos.cxx:10
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
ActsTrk::DummyRDOList
Definition: MeasurementToTruthAssociationAlg.h:70
ActsTrk::MeasurementToTruthAssociationAlg::kInvalidTruthLink
@ kInvalidTruthLink
Definition: MeasurementToTruthAssociationAlg.h:168
ActsTrk::getInTruthPropertyName
const char * getInTruthPropertyName()
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
ActsTrk::DummyRDOList::const_iterator::const_iterator
const_iterator(const T *a_ptr)
Definition: MeasurementToTruthAssociationAlg.h:77
ActsTrk::DummyRDOList::DummyRDOList
DummyRDOList(const T &a)
Definition: MeasurementToTruthAssociationAlg.h:74
ActsTrk::MeasurementToTruthAssociationAlg::MeasurementToTruthAssociationAlg
MeasurementToTruthAssociationAlg(const std::string &name, ISvcLocator *pSvcLocator)
Dbg::Hist::m_mutex
std::mutex m_mutex
Definition: MeasurementToTruthAssociationAlg.h:55
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:34
ActsTrk::MeasurementToTruthAssociationAlg::initialize
virtual StatusCode initialize() override
MeasurementToTruthParticleAssociation.h
ActsTrk::MeasurementToTruthAssociationAlg::finalize
virtual StatusCode finalize() override
Dbg::Empty
Definition: MeasurementToTruthAssociationAlg.h:58
ActsTrk::getSimDataDeposits
auto getSimDataDeposits(const T_SimDataCollection &sim_data_collection, T_SimDataIterator sim_data_iter_for_identifier)
ActsTrk::DummyRDOList::const_iterator::operator*
Identifier operator*() const
Definition: MeasurementToTruthAssociationAlg.h:78
Dbg::HistTemp::ATLAS_THREAD_SAFE
Dbg::StatHist m_depositedEnergy ATLAS_THREAD_SAFE
Definition: MeasurementToTruthAssociationAlg.h:42
ActsTrk::MeasurementToTruthAssociationAlg::m_associationOutKey
SG::WriteHandleKey< MeasurementToTruthParticleAssociation > m_associationOutKey
Definition: MeasurementToTruthAssociationAlg.h:159
ActsTrk::DummyRDOList::begin
const_iterator begin() const
Definition: MeasurementToTruthAssociationAlg.h:84