ATLAS Offline Software
TruthDecoratorAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
10 #include "TruthDecoratorAlg.h"
12 
13 
18  const std::string& name,
19  ISvcLocator* pSvcLocator ) :
20  AthReentrantAlgorithm( name, pSvcLocator ) { }
21 
22 
27 
28  ATH_CHECK( m_truthParticlesName.initialize(
29  not m_truthParticlesName.key().empty() ) );
30 
31  ATH_CHECK( m_truthClassifier.retrieve() );
32 
35  *this, m_truthParticlesName,
36  m_prefix.value(), m_decor_truth_names, m_decor_truth );
37 
38  if( m_decor_truth.size() != NDecorations ) {
39  ATH_MSG_ERROR( "Incorrect booking of truth class decorations" );
40  return StatusCode::FAILURE;
41  }
42 
43  return StatusCode::SUCCESS;
44 }
45 
46 
50 StatusCode IDTPM::TruthDecoratorAlg::execute( const EventContext& ctx ) const {
51 
53  SG::ReadHandle< xAOD::TruthParticleContainer > ptruths( m_truthParticlesName, ctx );
54  if( not ptruths.isValid() ) {
55  ATH_MSG_ERROR( "Failed to retrieve truth particles container" );
56  return StatusCode::FAILURE;
57  }
58 
60  if( IDTPM::decorationsAllExist( *ptruths, m_decor_truth ) ) {
61  ATH_MSG_DEBUG( "All decorations already exist. Exiting gracefully" );
62  return StatusCode::SUCCESS;
63  }
64 
66  std::vector< IDTPM::OptionalDecoration<xAOD::TruthParticleContainer, int> >
67  truth_decor( IDTPM::createDecoratorsIfNeeded( *ptruths, m_decor_truth, ctx ) );
68 
69  if( truth_decor.empty() ) {
70  ATH_MSG_ERROR( "Failed to book truth class decorations" );
71  return StatusCode::FAILURE;
72  }
73 
74  for( const xAOD::TruthParticle* truth : *ptruths ) {
76  ATH_CHECK( decorateTruthParticle( *truth, truth_decor ) );
77  }
78 
79  return StatusCode::SUCCESS;
80 }
81 
82 
87  const xAOD::TruthParticle& truth,
89  int>>& truth_decor) const {
90 
92  auto truthClass = m_truthClassifier->particleTruthClassifier( &truth );
93  int type = static_cast<int>( truthClass.first );
94  int origin = static_cast<int>( truthClass.second );
95 
97  IDTPM::decorateOrRejectQuietly( truth, truth_decor[Type], type );
98  IDTPM::decorateOrRejectQuietly( truth, truth_decor[Origin], origin );
99 
100  return StatusCode::SUCCESS;
101 }
SG::ReadHandle< xAOD::TruthParticleContainer >
IDTPM::decorateOrRejectQuietly
void decorateOrRejectQuietly(const T_Cont_Elm &particle, OptionalDecoration< T_Cont, T > &decorator, const T &value)
Safe method to fill the decoration if decor flag is true.
Definition: SafeDecorator.h:221
IDTPM::TruthDecoratorAlg::initialize
virtual StatusCode initialize() override
Definition: TruthDecoratorAlg.cxx:26
IDTPM::createDecoratorsIfNeeded
std::vector< OptionalDecoration< T_Cont, T > > createDecoratorsIfNeeded(const T_Cont &container, const std::vector< WriteKeyAccessorPair< T_Cont, T > > &keys, const EventContext &ctx, bool verbose=false)
create/book the decorations if they do not exist already
Definition: SafeDecorator.h:138
IDTPM::TruthDecoratorAlg::TruthDecoratorAlg
TruthDecoratorAlg(const std::string &name, ISvcLocator *pSvcLocator)
Local includes.
Definition: TruthDecoratorAlg.cxx:17
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
TruthDecoratorAlg.h
Algorithm to decorate truth particles with their origin and type classes from MCTruthClassifier.
IDTPM::TruthDecoratorAlg::decorateTruthParticle
StatusCode decorateTruthParticle(const xAOD::TruthParticle &truth, std::vector< IDTPM::OptionalDecoration< xAOD::TruthParticleContainer, int >> &truth_decor) const
Definition: TruthDecoratorAlg.cxx:86
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
IDTPM::decorationsAllExist
bool decorationsAllExist(const T_Cont &container, const std::vector< WriteKeyAccessorPair< T_Cont, T > > &keys, bool verbose=false)
Like above - FIXME: maybe not needed.
Definition: SafeDecorator.h:113
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
IDTPM::TruthDecoratorAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: TruthDecoratorAlg.cxx:50
xAODType
Definition: ObjectType.h:13
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
IDTPM::createDecoratorKeysAndAccessor
void createDecoratorKeysAndAccessor(T_Parent &parent, const SG::ReadHandleKey< T_Cont > &container_key, const std::string &prefix, const std::vector< std::string > &decor_names, std::vector< WriteKeyAccessorPair< T_Cont, T > > &decor_out)
create a pair composed of a WriteDecorHandleKey to create a decorator handle and an accessor to check...
Definition: SafeDecorator.h:52
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
IDTPM::OptionalDecoration
std::pair< SG::WriteDecorHandle< ContainerType, VariableType >, bool > OptionalDecoration
Definition: SafeDecorator.h:47