ATLAS Offline Software
CopyTruthParticles.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include <memory>
8 #include "AsgMessaging/Check.h"
11 
12 using namespace std;
13 
14 
16  : AsgTool(name) {}
17 
19 
20  ATH_CHECK(m_truthEventKey.initialize());
21  ATH_CHECK(m_outTruthPartKey.initialize());
22 
23  return StatusCode::SUCCESS;
24 }
25 
27  // Retrieve the xAOD truth objects
28  auto truthEvents = SG::makeHandle(m_truthEventKey);
29  if ( !truthEvents.isValid() ) {
30  ATH_MSG_ERROR("Failed to retrieve truth event container " << m_truthEventKey.key());
31  return 1;
32  }
33 
34  // Classify particles for tagging and add to the TruthParticleContainer
35  std::unique_ptr<ConstDataVector<xAOD::TruthParticleContainer> > ptruth(new ConstDataVector<xAOD::TruthParticleContainer>(SG::VIEW_ELEMENTS));
36  size_t numCopied = 0;
37  const xAOD::TruthEvent* hsevt = truthEvents->front();
38  if(!hsevt) {
39  ATH_MSG_ERROR("Null pointer received for first truth event!");
40  return 1;
41  }
42  for (size_t itp(0); itp<hsevt->nTruthParticles(); ++itp) {
43  const xAOD::TruthParticle* tp = hsevt->truthParticle(itp);
44  if (!tp) {
45  // This might result from a thinned truth collection, so is not
46  // an error state.
47  ATH_MSG_VERBOSE("Null pointer received for truth particle to be copied");
48  continue;
49  }
50  if (tp->pt() < m_ptmin)
51  continue;
52 
53  if (classify(tp)) {
54  ptruth->push_back(tp);
55  numCopied += 1;
56  }
57  }
58 
59  ATH_MSG_DEBUG("Copied " << numCopied << " truth particles into " << m_outTruthPartKey.key() << " TruthParticle container");
60 
61  // record
62  auto truthParticles_out = SG::makeHandle(m_outTruthPartKey);
63  ATH_MSG_DEBUG("Recorded truth particle collection " << m_outTruthPartKey.key());
64  // notify
65  if (!truthParticles_out.put(std::move(ptruth))) {
66  ATH_MSG_ERROR("Unable to write new TruthParticleContainer to event store: "
67  << m_outTruthPartKey.key());
68  } else {
69  ATH_MSG_DEBUG("Created new TruthParticleContainer in event store: "
70  << m_outTruthPartKey.key());
71  }
72 
73  return 0;
74 }
WriteHandle.h
Handle class for recording to StoreGate.
TruthTest.itp
itp
Definition: TruthTest.py:46
Check.h
CopyTruthParticles::initialize
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: CopyTruthParticles.cxx:18
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
xAOD::TruthEventBase_v1::truthParticle
const TruthParticle * truthParticle(size_t index) const
Get a pointer to one of the truth particles.
Definition: TruthEventBase_v1.cxx:50
ParticleTest.tp
tp
Definition: ParticleTest.py:25
CopyTruthParticles::execute
virtual int execute() const
Method to be called for each event.
Definition: CopyTruthParticles.cxx:26
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
CopyTruthParticles::CopyTruthParticles
CopyTruthParticles(const std::string &name)
Constructor.
Definition: CopyTruthParticles.cxx:15
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
CopyTruthParticles::m_outTruthPartKey
SG::WriteHandleKey< ConstDataVector< xAOD::TruthParticleContainer > > m_outTruthPartKey
Key for output truth particles.
Definition: CopyTruthParticles.h:46
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
xAOD::TruthEvent_v1
Class describing a signal truth event in the MC record.
Definition: TruthEvent_v1.h:35
CopyTruthParticles::classify
virtual bool classify(const xAOD::TruthParticle *tp) const =0
Classifier function(s)
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CopyTruthParticles::m_truthEventKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventKey
Key for input truth event.
Definition: CopyTruthParticles.h:43
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::TruthEventBase_v1::nTruthParticles
size_t nTruthParticles() const
Get the number of truth particles.
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
CopyTruthParticles::m_ptmin
Gaudi::Property< float > m_ptmin
Minimum pT for particle selection (in MeV)
Definition: CopyTruthParticles.h:40
CopyTruthParticles.h
HepMCHelpers.h