ATLAS Offline Software
Loading...
Searching...
No Matches
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>
11
12using 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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
ATLAS-specific HepMC functions.
DataVector adapter that acts like it holds const pointers.
SG::WriteHandleKey< ConstDataVector< xAOD::TruthParticleContainer > > m_outTruthPartKey
Key for output truth particles.
virtual bool classify(const xAOD::TruthParticle *tp) const =0
Classifier function(s)
CopyTruthParticles(const std::string &name)
Constructor.
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Gaudi::Property< float > m_ptmin
Minimum pT for particle selection (in MeV)
virtual int execute() const
Method to be called for each event.
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventKey
Key for input truth event.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
const TruthParticle * truthParticle(size_t index) const
Get a pointer to one of the truth particles.
size_t nTruthParticles() const
Get the number of truth particles.
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
STL namespace.
TruthEvent_v1 TruthEvent
Typedef to implementation.
Definition TruthEvent.h:17
TruthParticle_v1 TruthParticle
Typedef to implementation.