ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticleCnvTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6// TruthParticleCnvTool.cxx
7// Implementation file for class TruthParticleCnvTool
8// Author: S.Binet<binet@cern.ch>
9// P.A Delsart <delsart in2p3 dot fr> (pile-up compatibility)
11
12// STL includes
13#include <sstream>
14
15// FrameWork includes
16#include "GaudiKernel/ThreadLocalContext.h"
17
18// CLHEP/HepMC includes
19#include "AtlasHepMC/GenEvent.h"
24
25// McParticleKernel includes
27
28// McParticleEvent includes
30
31// McParticleTools includes
33
34// GeneratorObjects includes
36
38
39
43
47 const std::string& name,
48 const IInterface* parent ) :
49 base_class( type, name, parent ),
53{
54 //
55 // Property declaration
56 //
57
58 declareProperty( "DataType",
60 "Type of data we are dealing with (Full/Fast/Truth...)" );
61
62 declareProperty( "TruthIsolationTool",
63 m_isolationTool = IsolTool_t( "TruthIsolationTool" ),
64 "Pointer to the TruthIsolationTool to be able to compute "
65 "transverse energy isolations for various isolation cones cuts. "
66 "See McParticleEvent/TruthParticleParameters.h for cone cuts." );
67
68
69 declareProperty( "DoEtIsolations",
70 m_doEtIsolation = false,
71 "Switch to compute or not the Et-isolations for TruthParticle "
72 "(and their underlying @c HepMC::GenParticle).\n"
73 "Default is to not compute these Et-isolations (and save CPU)." );
74
76
77 declareProperty( "SelectSignalType",
79 "Switch to select different type of signal\n"
80 " hard scatter 0\n"
81 " hard scatter plus minbias 1\n"
82 " only minbias -1\n"
83 "Default is hard scatter only." );
84
85 declareInterface<ITruthParticleCnvTool>(this);
86}
87
94
98{
99 ATH_MSG_INFO("Initializing " << name() << "...");
100
101 // retrieve the TruthIsolation tool only if asked for.
102 if ( m_doEtIsolation.value() ) {
103 ATH_CHECK(m_isolationTool.retrieve());
104 }
105 else {
106 m_isolationTool.disable();
107 }
108
109 //initialize DataHandleKeys
112
113 // for compatibility with earlier version we accept <0 values :
116
117
119 (" DoEtIsolations: [" << std::boolalpha
120 << m_doEtIsolation.value() << "]" << endmsg
121 << " McEvents: [" << m_mcEventsReadHandleKey.key() << "]" << endmsg
122 << " TruthParticlesOutput: [" << m_mcPartsOutputWriteHandleKey.key() << "]"
123 << endmsg
124 << " SelectSignalType: [" << m_selectSignalType << "]");
125
126 return StatusCode::SUCCESS;
127}
128
130{
131 return execute (Gaudi::Hive::currentContext());
132}
133
134
135StatusCode TruthParticleCnvTool::execute (const EventContext& ctx) const
136{
137 ATH_MSG_DEBUG("Executing " << name() << "...");
138
139 //Setup WriteHandle, and record new TruthParticleContainer
141 ATH_CHECK(mcPartsOutputWriteHandle.record(std::make_unique<TruthParticleContainer>()));
142
143 if (!mcPartsOutputWriteHandle.isValid()){
144 ATH_MSG_ERROR("Invalid WriteHandle for TruthParticleContainer with key: " << m_mcPartsOutputWriteHandleKey.key());
145 return StatusCode::FAILURE;
146 }
147
148 //Setup ReadHandle for input McEventCollection
150
151 if (!mcEventsReadHandle.isValid()){
152 ATH_MSG_WARNING("Invalid ReadHandle for McEventCollection with key ["
153 << m_mcEventsReadHandleKey.key() << "] !!"
154 << endmsg
155 << "TruthParticleContainer [" << m_mcPartsOutputWriteHandleKey.key()
156 << "] will be EMPTY !!");
157 return StatusCode::RECOVERABLE;
158 }
159
160 ATH_MSG_DEBUG(" Found McEventCollection of size = "<< mcEventsReadHandle->size() );
161 // do the actual convertion (it is merely an interface adaptation now...)
162 // selection for particles from minbias copied from the JetsFromTruthTool
163
164 std::size_t genEventIndex = 0;
165 const ITruthParticleVisitor* dummyVisitor = nullptr;
166 bool all_good = true;
167
168 McEventCollection::const_iterator fEvt = mcEventsReadHandle->begin();
169 McEventCollection::const_iterator lEvt = mcEventsReadHandle->end();
171
172 ATH_MSG_DEBUG(" Found McEventCollection iterators : "<< (fEvt-mcEventsReadHandle->begin()) << " to "<< (lEvt-mcEventsReadHandle->begin()) );
173
174 for(McEventCollection::const_iterator it=fEvt ; it != lEvt; ++it){
175 const HepMC::GenEvent* evt = *it;
176 // there are holes in a McEventCollection when pile-up is enabled
177 // so deal with it
178 if (nullptr == evt) {
179 continue;
180 }
181 genEventIndex = (it - mcEventsReadHandle->begin());
182 ATH_MSG_DEBUG(" adding event id="<< HepMC::signal_process_id(evt)<<" genEventIndex="<< genEventIndex );
183
184 if( HepMC::signal_process_id(evt) == 0 ) continue;
185 if (!this->convert( mcEventsReadHandle.ptr(), genEventIndex, mcPartsOutputWriteHandle.ptr(), dummyVisitor ).isSuccess()) {
186 ATH_MSG_DEBUG("Failed to convert an event...");
187 all_good = false;
188 }
189 }
190
191 // VERY IMPORTANT ! Reset the index to the first GenEvent included
192 // in this TruthParticleContainer. This will be used when read back from disk.
193 mcPartsOutputWriteHandle->setGenEvent( mcEventsReadHandle.ptr(), (fEvt - mcEventsReadHandle->begin() ) );
194
195
196 return all_good
197 ? StatusCode::SUCCESS
198 : StatusCode::RECOVERABLE;
199}
200
201
205
206StatusCode
208 const unsigned int genEventIndex,
209 TruthParticleContainer * container,
210 const ITruthParticleVisitor* visitor ) const
211{
212 ATH_MSG_DEBUG("Converting McEventCollection to TruthParticleContainer");
213
214
215 if ( nullptr == mcCollection ) {
216 ATH_MSG_WARNING("Null pointer to McEventCollection !");
217 return StatusCode::RECOVERABLE;
218 }
219
220 if ( mcCollection->size() <= genEventIndex ) {
221 ATH_MSG_WARNING("McEventCollection size: " << mcCollection->size()
222 << endmsg
223 << "Requested element nbr : " << genEventIndex << " !!");
224 return StatusCode::RECOVERABLE;
225 }
226
227 IProxyDict* sg = SG::CurrentEventStore::store();
228
230 ATH_MSG_DEBUG("Retrieve the GenEvent from given McEventCollection");
231 const HepMC::GenEvent * evt = (*mcCollection)[genEventIndex];
232 container->setGenEvent( mcCollection, genEventIndex, sg );
233
234 // reserve enough space for the container so we don't have to relocate it
235 container->reserve( container->size() + evt->particles_size() );
236
238 TruthParticleContainer::Map_t bcToMcPart = container->m_particles;
239
240
241 // Process particles in barcode order.
242 auto bcmapatt = evt->attribute<HepMC::GenEventBarcodes>(HepMCStr::barcodes);
243 if (!bcmapatt){
244 ATH_MSG_ERROR("TruthParticleCnvTool.cxx: Event does not contain barcodes attribute");
245 return StatusCode::FAILURE;
246 }
247 std::map<int, HepMC3::ConstGenParticlePtr> bcmap = bcmapatt->barcode_to_particle_map();
248 for (const auto &[bc,hepMcPart]: bcmap) {
249
250 TruthParticle * mcPart = new TruthParticle( hepMcPart, container );
251 container->push_back( mcPart );
252
253 if ( visitor ) {
254 visitor->visit( mcPart );
255 }
256
257 mcPart->setCharge( MC::charge( int(mcPart->pdgId())) );
258 mcPart->setGenEventIndex( genEventIndex);
259
260 if ( hepMcPart != mcPart->genParticle() ) {
261 ATH_MSG_ERROR("TruthParticle is not wrapping the GenParticle : "
262 << hepMcPart << " !!");
263 }
264 HepMcParticleLink mcLink( bc, genEventIndex, HepMcParticleLink::IS_POSITION, HepMcParticleLink::IS_BARCODE, sg ); // FIXME barcode-based
265 bcToMcPart[ mcLink.compress() ] = mcPart;
266
267 }//> end loop over particles
268
269 // at this point the whole GenEvent has been proxied.
270 // so we can setup its VectorMap
271 container->setParticles( bcToMcPart );
272
273 // connect the TruthParticleContainer to the container of TruthEtIsolations
274 // if it exists and if we are asked for
275 if ( m_doEtIsolation.value() ) {
276 const std::string& etIsolName
277 = m_isolationTool->etIsolationsName( container->genEventName() );
278 if ( etIsolName.empty() ) {
279 ATH_MSG_WARNING("Could not retrieve the name of the "
280 "TruthEtIsolations container (requested: ["
281 << container->genEventName() << "])");
282 return StatusCode::RECOVERABLE;
283 }
284
285 const TruthEtIsolationsContainer* etIsols = nullptr;
286 if ( !evtStore()->retrieve( etIsols, etIsolName ).isSuccess() ) {
287 ATH_MSG_WARNING("Could not retrieve the TruthEtIsolations container at ["
288 << etIsolName << "] !!");
289 return StatusCode::RECOVERABLE;
290 }
291
292 // index of HepMC::GenEvent within the McEventCollection is the same
293 // than the one of the TruthEtIsolations within the TruthEtIsolationsCont.
294 container->setEtIsolations( etIsols, genEventIndex );
295 }
296
297 return StatusCode::SUCCESS;
298}
#define True
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
ATLAS-specific HepMC functions.
Hold a pointer to the current event store.
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
size_type size() const noexcept
Returns the number of elements in the collection.
std::map< int, ConstGenParticlePtr > barcode_to_particle_map() const
Definition GenEvent.h:248
virtual void visit(TruthParticle *truthParticle) const =0
The method to visit a TruthParticle to apply further modifications to the instance at hand.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
virtual int pdgId() const
Return enum indicating particle id the enum file is available in Event/EventKernel/PdtPdg....
PileuType_t
define some Pile-up classification Important : this classification is copied in McParticleAlgs/python...
static void findEventIterators(PileuType_t putype, McEventCollection::const_iterator &fEvt, McEventCollection::const_iterator &lEvt)
Find interval [fEvt,lEvt] containing all GenEvents of type putype from the McEventCollection.
const_pointer_type ptr()
Dereference the pointer.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
pointer_type ptr()
Dereference the pointer.
int m_selectSignalTypeProp
Type of truth particle we want to create (property to be set by jobO).
virtual ~TruthParticleCnvTool()
Destructor:
TruthParticleCnvTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
SG::WriteHandleKey< TruthParticleContainer > m_mcPartsOutputWriteHandleKey
Output TruthParticle WriteHandleKey (built from the McEventCollection).
ToolHandle< ITruthIsolationTool > IsolTool_t
virtual StatusCode execute() const override
ParticleDataType::DataType m_dataType
Type of data we are dealing with (Full/Fast/Truth/...).
IsolTool_t m_isolationTool
Pointer to the ITruthIsolationTool to be able to retrieve the previously computed transverse energy i...
void setDataType(const int type)
Inline methods:
virtual StatusCode initialize() override
Athena Algorithm's Hooks.
PileUpClassification::PileuType_t m_selectSignalType
Type of truth particle we want to create.
StatusCode convert(const McEventCollection *mcEvts, const unsigned int genEvtIndex, TruthParticleContainer *mcParts, const ITruthParticleVisitor *visitor) const override
Converts a McEventCollection into an TruthParticleContainer (ie: converts it into an AOD compliant co...
BooleanProperty m_doEtIsolation
Switch to compute or not the Et-isolations for TruthParticle (and their underlying HepMC::GenParticle...
SG::ReadHandleKey< McEventCollection > m_mcEventsReadHandleKey
ReadHandleKey for the McEventCollection the TruthParticles will be made from.
std::unordered_map< long, const TruthParticle * > Map_t
barcode to TruthParticle dictionary
HepMC::ConstGenParticlePtr genParticle() const
Retrieve the GenParticle this TruthParticle has been made from (if any).
void setCharge(const ChargeType charge)
Set the charge of this TruthParticle.
int signal_process_id(const GenEvent &evt)
Definition GenEvent.h:572
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
double charge(const T &p)
Information about type of data used to fill particle.