ATLAS Offline Software
TruthParticleHitCountAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
10 #include <iomanip>
11 #include <cmath>
12 #include <type_traits>
13 #include <typeinfo>
14 #include <numeric>
15 
16 // for erase_if detection i.e. >=c++20
17 #include <version>
18 
19 namespace {
20  template <typename T_EnumClass >
21  constexpr typename std::underlying_type<T_EnumClass>::type to_underlying(T_EnumClass an_enum) {
22  return static_cast<typename std::underlying_type<T_EnumClass>::type>(an_enum);
23  }
24 }
25 
26 #ifndef __cpp_lib_erase_if
27 namespace std {
28  template <class T_container, class T_Func>
29  inline std::size_t erase_if(T_container &container, T_Func pred) {
30  std::size_t orig_size = container->size();
31  for ( typename T_container::iterator iter = container.begin();
32  iter != container.end();
33  /* increment only if nothing was erased! */ ) {
34  if (pred(*iter)) {
35  iter = erase(iter);
36  }
37  else {
38  ++iter;
39  }
40  }
41  return orig_size - container->size();
42  }
43 }
44 #endif
45 
46 namespace ActsTrk
47 {
48  // to dump
49  inline MsgStream &operator<<(MsgStream &out, const ActsUtils::Stat &stat) {
51  return out;
52  }
53 
55  ISvcLocator *pSvcLocator)
56  : AthReentrantAlgorithm(name, pSvcLocator)
57  {
58  }
59 
61  {
62  ATH_CHECK( m_trackingGeometryTool.retrieve() );
63  ATH_CHECK( m_pixelClustersToTruth.initialize() );
64  ATH_CHECK( m_stripClustersToTruth.initialize() );
65 
67 
69  return StatusCode::SUCCESS;
70  }
71 
72  template <bool IsDebug>
73  template <class T_OutStream>
75  if constexpr(IsDebug) {
76  out << "Measurements per truth particle :" << m_measPerTruthParticle << std::endl
77  << m_measPerTruthParticle.histogramToString();
78  }
79  }
80  template <bool IsDebug>
81  void inline TruthParticleHitCountAlg::AssociationCounter<IsDebug>::fillStatistics(unsigned int n_measurements) const {
82  if constexpr(IsDebug) {
83  std::lock_guard<std::mutex> lock(m_mutex);
84  m_measPerTruthParticle.add(n_measurements);
85  }
86  }
87 
89  {
90  ATH_MSG_INFO("Number of truth particles with hits : " << m_nTruthParticlesWithHits);
91  if (msgLvl(MSG::INFO)) {
94  msg() << std::endl;
96  }
97  msg() << endmsg;
98  }
99  return StatusCode::SUCCESS;
100  }
101 
102  StatusCode TruthParticleHitCountAlg::execute(const EventContext &ctx) const
103  {
104  std::unique_ptr<TruthParticleHitCounts>
105  truth_particle_hit_counts( std::make_unique<TruthParticleHitCounts>() );
106 
108  if (!pixelClustersToTruthAssociation.isValid()) {
109  ATH_MSG_ERROR("No pixel clusterss for key " << m_pixelClustersToTruth.key() );
110  return StatusCode::FAILURE;
111  }
113  if (!stripClustersToTruthAssociation.isValid()) {
114  ATH_MSG_ERROR("No strip clusterss for key " << m_stripClustersToTruth.key() );
115  return StatusCode::FAILURE;
116  }
117  Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
118 
121  measurement_to_truth_association_maps{};
122  measurement_to_truth_association_maps[to_underlying(xAOD::UncalibMeasType::PixelClusterType)]=pixelClustersToTruthAssociation.cptr();
123  measurement_to_truth_association_maps[to_underlying(xAOD::UncalibMeasType::StripClusterType)]=stripClustersToTruthAssociation.cptr();
124  ATH_MSG_DEBUG("Measurement association entries: "
125  << measurement_to_truth_association_maps[to_underlying(xAOD::UncalibMeasType::PixelClusterType)]->size()
126  << " + " << measurement_to_truth_association_maps[to_underlying(xAOD::UncalibMeasType::StripClusterType)]->size());
127 
128  unsigned int measurement_type_i=0;
129  --measurement_type_i;
130  for( const ActsTrk::MeasurementToTruthParticleAssociation *associated_truth_particles : measurement_to_truth_association_maps ) {
131  ++measurement_type_i;
132  if (!associated_truth_particles) continue;
133  for (const ActsTrk::ParticleVector &truth_particles : *associated_truth_particles) {
134  for (const xAOD::TruthParticle *truth_particle : truth_particles) {
135  assert (truth_particle);
136  const xAOD::TruthParticle *mother_particle = m_elasticDecayUtil.getMother(*truth_particle, m_maxEnergyLoss.value());
137  if (mother_particle) {
138  assert(measurement_type_i < (*truth_particle_hit_counts)[mother_particle].size());
139  ++(*truth_particle_hit_counts)[mother_particle][measurement_type_i];
140  }
141  }
142  }
143  }
144  unsigned int n_hits_min = m_nHitsMin.value();
145  unsigned int truth_particles_without_enough_measurements
146  = std::erase_if( *truth_particle_hit_counts,
147  [this, n_hits_min](const std::pair<const xAOD::TruthParticle * const,HitCounterArray> &elm) {
148  unsigned int n_measurements=std::accumulate(elm.second.begin(), elm.second.end(),0u);
149  this->m_associationCounter.fillStatistics(n_measurements);
150  return n_measurements<n_hits_min;
151  });
152 
153  m_nTruthParticlesWithHits += truth_particle_hit_counts->size();
154 
155  ATH_MSG_DEBUG("Truth particles with hits:" << truth_particle_hit_counts->size()
156  << ", without enough hits: " << truth_particles_without_enough_measurements);
157 
158  SG::WriteHandle<TruthParticleHitCounts> truth_particle_hit_counts_out_handle(m_truthHitCountsOut, ctx);
159  if (truth_particle_hit_counts_out_handle.record( std::move(truth_particle_hit_counts)).isFailure()) {
160  ATH_MSG_ERROR("Failed to record track to truth assocition with key " << m_truthHitCountsOut.key() );
161  return StatusCode::FAILURE;
162  }
163 
164  return StatusCode::SUCCESS;
165  }
166 
167 } // namespace
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ActsTrk::TruthParticleHitCountAlg::m_trackingGeometryTool
ToolHandle< IActsTrackingGeometryTool > m_trackingGeometryTool
Definition: TruthParticleHitCountAlg.h:56
ActsTrk::TruthParticleHitCountAlg::initialize
virtual StatusCode initialize() override
Definition: TruthParticleHitCountAlg.cxx:60
createSimpleDistributions.IsDebug
bool IsDebug
Definition: createSimpleDistributions.py:12
ActsTrk::TruthParticleHitCountAlg::m_pixelClustersToTruth
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_pixelClustersToTruth
Definition: TruthParticleHitCountAlg.h:59
ActsTrk::TruthParticleHitCountAlg::AssociationCounter::fillStatistics
void fillStatistics(unsigned int n_measurements) const
Definition: TruthParticleHitCountAlg.cxx:81
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ElasticDecayUtil::getMother
const xAOD::TruthParticle * getMother(const xAOD::TruthParticle &truth_particle, float max_energy_loss) const
follow decay chain upwards assuming quasi elastic processes.
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
ActsTrk::TruthParticleHitCountAlg::m_maxEnergyLoss
Gaudi::Property< float > m_maxEnergyLoss
Definition: TruthParticleHitCountAlg.h:67
ActsTrk::detail::to_underlying
constexpr std::underlying_type< T_EnumClass >::type to_underlying(T_EnumClass an_enum)
Helper to convert class enum into an integer.
Definition: HitSummaryDataUtils.h:25
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAOD::UncalibMeasType::StripClusterType
@ StripClusterType
ActsTrk::TruthParticleHitCountAlg::TruthParticleHitCountAlg
TruthParticleHitCountAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TruthParticleHitCountAlg.cxx:54
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
ActsTrk::TruthParticleHitCountAlg::AssociationCounter::dumpStatistics
void dumpStatistics(T_OutStream &out) const
Definition: TruthParticleHitCountAlg.cxx:74
ActsTrk::TruthParticleHitCountAlg::m_truthHitCountsOut
SG::WriteHandleKey< TruthParticleHitCounts > m_truthHitCountsOut
Definition: TruthParticleHitCountAlg.h:64
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
ActsTrk::TruthParticleHitCountAlg::m_nHitsMin
Gaudi::Property< unsigned int > m_nHitsMin
Definition: TruthParticleHitCountAlg.h:69
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
ActsTrk::TruthParticleHitCountDebugHists
constexpr bool TruthParticleHitCountDebugHists
Definition: TruthParticleHitCountAlg.h:42
ActsTrk::MeasurementToTruthParticleAssociation
Definition: MeasurementToTruthParticleAssociation.h:18
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
ActsTrk::TruthParticleHitCountAlg::finalize
virtual StatusCode finalize() override
Definition: TruthParticleHitCountAlg.cxx:88
ElasticDecayUtil::dumpStatistics
void dumpStatistics(T_OutStream &out) const
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
ActsUtils::Stat
Simple class to gather statistics : min, max, mean, rms.
Definition: StatUtils.h:16
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ActsUtils::dumpStat
void dumpStat(T_Stream &out, const Stat &stat)
Dump the given statistics object to the given output stream.
Definition: StatUtils.h:62
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ActsTrk::TruthParticleHitCountAlg::m_elasticDecayUtil
ElasticDecayUtil< TruthParticleHitCountDebugHists > m_elasticDecayUtil
Definition: TruthParticleHitCountAlg.h:71
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
ActsTrk::TruthParticleHitCountAlg::m_associationCounter
AssociationCounter< TruthParticleHitCountDebugHists > m_associationCounter
Definition: TruthParticleHitCountAlg.h:95
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ActsTrk::operator<<
MsgStream & operator<<(MsgStream &out, const ActsUtils::Stat &stat)
Definition: TrackToTruthAssociationAlg.cxx:25
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
TruthParticleHitCountAlg.h
beamspotman.stat
stat
Definition: beamspotman.py:266
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
std::erase_if
std::size_t erase_if(T_container &container, T_Func pred)
Definition: TruthParticleHitCountAlg.cxx:29
ActsTrk::ParticleVector
boost::container::small_vector< const xAOD::TruthParticle *, NTruthParticlesPerMeasurement > ParticleVector
Definition: MeasurementToTruthParticleAssociation.h:17
ActsTrk::TruthParticleHitCountAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: TruthParticleHitCountAlg.cxx:102
TruthVertex.h
lumiFormat.array
array
Definition: lumiFormat.py:91
xAOD::UncalibMeasType::nTypes
@ nTypes
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.Dumpers.typename
def typename(t)
Definition: Dumpers.py:194
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ActsTrk::TruthParticleHitCountAlg::m_energyLossBinning
std::conditional< TruthParticleHitCountDebugHists, Gaudi::Property< std::vector< float > >, EmptyProperty >::type m_energyLossBinning
Definition: TruthParticleHitCountAlg.h:76
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
AthCommonMsg< Gaudi::Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:54
TruthParticle.h
ElasticDecayUtil::setEnergyLossBinning
void setEnergyLossBinning(const typename std::conditional< IsDebug, std::vector< float >, EmptyProperty >::type &binning)
xAOD::UncalibMeasType::PixelClusterType
@ PixelClusterType
ActsTrk::TruthParticleHitCountAlg::m_stripClustersToTruth
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_stripClustersToTruth
Definition: TruthParticleHitCountAlg.h:61