ATLAS Offline Software
TruthParticleCnvTool.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 // 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/IPartPropSvc.h"
17 #include "GaudiKernel/ThreadLocalContext.h"
18 
19 // CLHEP/HepMC includes
20 #include "AtlasHepMC/GenEvent.h"
21 #include "AtlasHepMC/GenParticle.h"
22 #include "AtlasHepMC/GenVertex.h"
25 #include "HepPDT/ParticleData.hh"
26 
27 // McParticleKernel includes
29 
30 // McParticleEvent includes
32 
33 // McParticleTools includes
34 #include "TruthParticleCnvTool.h"
35 
36 // GeneratorObjects includes
38 
40 
41 
45 
49  const std::string& name,
50  const IInterface* parent ) :
52  m_dataType ( ParticleDataType::True ),
53  m_vxCandidatesName ( ),
54  m_pdt ( nullptr ),
55  m_selectSignalTypeProp ( 0 )
56 {
57  //
58  // Property declaration
59  //
60 
61  declareProperty( "DataType",
62  m_dataType_int = 4,
63  "Type of data we are dealing with (Full/Fast/Truth...)" );
64 
65  declareProperty( "TruthIsolationTool",
66  m_isolationTool = IsolTool_t( "TruthIsolationTool" ),
67  "Pointer to the TruthIsolationTool to be able to compute "
68  "transverse energy isolations for various isolation cones cuts. "
69  "See McParticleEvent/TruthParticleParameters.h for cone cuts." );
70 
71 
72  declareProperty( "DoEtIsolations",
73  m_doEtIsolation = false,
74  "Switch to compute or not the Et-isolations for TruthParticle "
75  "(and their underlying @c HepMC::GenParticle).\n"
76  "Default is to not compute these Et-isolations (and save CPU)." );
77 
79 
80  declareProperty( "SelectSignalType",
82  "Switch to select different type of signal\n"
83  " hard scatter 0\n"
84  " hard scatter plus minbias 1\n"
85  " only minbias -1\n"
86  "Default is hard scatter only." );
87 
88  declareInterface<ITruthParticleCnvTool>(this);
89 }
90 
94 {
95  ATH_MSG_DEBUG("Calling destructor");
96 }
97 
101 {
102  ATH_MSG_INFO("Initializing " << name() << "...");
103 
104  // Get the Particle Properties Service
105  ServiceHandle<IPartPropSvc> partPropSvc("PartPropSvc", name());
106  if ( !partPropSvc.retrieve().isSuccess() ) {
107  ATH_MSG_ERROR(" Could not initialize Particle Properties Service");
108  return StatusCode::FAILURE;
109  }
110 
111  m_pdt = partPropSvc->PDT();
112  if ( nullptr == m_pdt ) {
113  ATH_MSG_ERROR("Could not retrieve HepPDT::ParticleDataTable from "\
114  "ParticleProperties Service !!");
115  return StatusCode::FAILURE;
116  }
117 
118  // retrieve the TruthIsolation tool only if asked for.
119  if ( m_doEtIsolation.value() ) {
120  ATH_CHECK(m_isolationTool.retrieve());
121  }
122  else {
123  m_isolationTool.disable();
124  }
125 
126  //initialize DataHandleKeys
129 
130  // for compatibility with earlier version we accept <0 values :
133 
134 
136  (" DoEtIsolations: [" << std::boolalpha
137  << m_doEtIsolation.value() << "]" << endmsg
138  << " McEvents: [" << m_mcEventsReadHandleKey.key() << "]" << endmsg
139  << " TruthParticlesOutput: [" << m_mcPartsOutputWriteHandleKey.key() << "]"
140  << endmsg
141  << " SelectSignalType: [" << m_selectSignalType << "]");
142 
143  return StatusCode::SUCCESS;
144 }
145 
147 {
148  return execute (Gaudi::Hive::currentContext());
149 }
150 
151 
152 StatusCode TruthParticleCnvTool::execute (const EventContext& ctx) const
153 {
154  ATH_MSG_DEBUG("Executing " << name() << "...");
155 
156  //Setup WriteHandle, and record new TruthParticleContainer
158  ATH_CHECK(mcPartsOutputWriteHandle.record(std::make_unique<TruthParticleContainer>()));
159 
160  if (!mcPartsOutputWriteHandle.isValid()){
161  ATH_MSG_ERROR("Invalid WriteHandle for TruthParticleContainer with key: " << m_mcPartsOutputWriteHandleKey.key());
162  return StatusCode::FAILURE;
163  }
164 
165  //Setup ReadHandle for input McEventCollection
167 
168  if (!mcEventsReadHandle.isValid()){
169  ATH_MSG_WARNING("Invalid ReadHandle for McEventCollection with key ["
170  << m_mcEventsReadHandleKey.key() << "] !!"
171  << endmsg
172  << "TruthParticleContainer [" << m_mcPartsOutputWriteHandleKey.key()
173  << "] will be EMPTY !!");
174  return StatusCode::RECOVERABLE;
175  }
176 
177  ATH_MSG_DEBUG(" Found McEventCollection of size = "<< mcEventsReadHandle->size() );
178  // do the actual convertion (it is merely an interface adaptation now...)
179  // selection for particles from minbias copied from the JetsFromTruthTool
180 
181  std::size_t genEventIndex = 0;
182  const ITruthParticleVisitor* dummyVisitor = nullptr;
183  bool all_good = true;
184 
185  McEventCollection::const_iterator fEvt = mcEventsReadHandle->begin();
186  McEventCollection::const_iterator lEvt = mcEventsReadHandle->end();
188 
189  ATH_MSG_DEBUG(" Found McEventCollection iterators : "<< (fEvt-mcEventsReadHandle->begin()) << " to "<< (lEvt-mcEventsReadHandle->begin()) );
190 
191  for(McEventCollection::const_iterator it=fEvt ; it != lEvt; ++it){
192  const HepMC::GenEvent* evt = *it;
193  // there are holes in a McEventCollection when pile-up is enabled
194  // so deal with it
195  if (nullptr == evt) {
196  continue;
197  }
198  genEventIndex = (it - mcEventsReadHandle->begin());
199  ATH_MSG_DEBUG(" adding event id="<< HepMC::signal_process_id(evt)<<" genEventIndex="<< genEventIndex );
200 
201  if( HepMC::signal_process_id(evt) == 0 ) continue;
202  if (!this->convert( mcEventsReadHandle.ptr(), genEventIndex, mcPartsOutputWriteHandle.ptr(), dummyVisitor ).isSuccess()) {
203  ATH_MSG_DEBUG("Failed to convert an event...");
204  all_good = false;
205  }
206  }
207 
208  // VERY IMPORTANT ! Reset the index to the first GenEvent included
209  // in this TruthParticleContainer. This will be used when read back from disk.
210  mcPartsOutputWriteHandle->setGenEvent( mcEventsReadHandle.ptr(), (fEvt - mcEventsReadHandle->begin() ) );
211 
212 
213  return all_good
214  ? StatusCode::SUCCESS
215  : StatusCode::RECOVERABLE;
216 }
217 
218 
222 
225  const unsigned int genEventIndex,
226  TruthParticleContainer * container,
227  const ITruthParticleVisitor* visitor ) const
228 {
229  ATH_MSG_DEBUG("Converting McEventCollection to TruthParticleContainer");
230 
231  if ( nullptr == m_pdt ) {
232  ATH_MSG_ERROR("Could not convert McEventCollection into "\
233  "TruthParticleContainer if NO ParticleDataTable is "\
234  "available !!");
235  return StatusCode::FAILURE;
236  }
237 
238  if ( nullptr == mcCollection ) {
239  ATH_MSG_WARNING("Null pointer to McEventCollection !");
240  return StatusCode::RECOVERABLE;
241  }
242 
243  if ( mcCollection->size() <= genEventIndex ) {
244  ATH_MSG_WARNING("McEventCollection size: " << mcCollection->size()
245  << endmsg
246  << "Requested element nbr : " << genEventIndex << " !!");
247  return StatusCode::RECOVERABLE;
248  }
249 
251 
253  ATH_MSG_DEBUG("Retrieve the GenEvent from given McEventCollection");
254  const HepMC::GenEvent * evt = (*mcCollection)[genEventIndex];
255  container->setGenEvent( mcCollection, genEventIndex, sg );
256 
257  // reserve enough space for the container so we don't have to relocate it
258  container->reserve( container->size() + evt->particles_size() );
259 
261  TruthParticleContainer::Map_t bcToMcPart = container->m_particles;
262 
263 
264 #ifdef HEPMC3
265  // Process particles in barcode order.
266  auto bcmapatt = evt->attribute<HepMC::GenEventBarcodes>("barcodes");
267  if (!bcmapatt) ATH_MSG_ERROR("TruthParticleCnvTool.cxx: Event does not contain barcodes attribute");
268  std::map<int, HepMC3::ConstGenParticlePtr> bcmap = bcmapatt->barcode_to_particle_map();
269  for (const auto &[bc,hepMcPart]: bcmap) {
270 #else
271  for (auto hepMcPart: *evt) {
272  int bc = HepMC::barcode(hepMcPart);
273 #endif
274 
275  TruthParticle * mcPart = new TruthParticle( hepMcPart, container );
276  container->push_back( mcPart );
277 
278  if ( visitor ) {
279  visitor->visit( mcPart );
280  }
281 
282  mcPart->setCharge( MC::charge( int(mcPart->pdgId())) );
283  mcPart->setGenEventIndex( genEventIndex);
284 
285  if ( hepMcPart != mcPart->genParticle() ) {
286  ATH_MSG_ERROR("TruthParticle is not wrapping the GenParticle : "
287  << hepMcPart << " !!");
288  }
289  HepMcParticleLink mcLink( bc, genEventIndex, HepMcParticleLink::IS_POSITION, HepMcParticleLink::IS_BARCODE, sg ); // FIXME barcode-based
290  bcToMcPart[ mcLink.compress() ] = mcPart;
291 
292  }//> end loop over particles
293 
294  // at this point the whole GenEvent has been proxied.
295  // so we can setup its VectorMap
296  container->setParticles( bcToMcPart );
297 
298  // connect the TruthParticleContainer to the container of TruthEtIsolations
299  // if it exists and if we are asked for
300  if ( m_doEtIsolation.value() ) {
301  const std::string& etIsolName
302  = m_isolationTool->etIsolationsName( container->genEventName() );
303  if ( etIsolName.empty() ) {
304  ATH_MSG_WARNING("Could not retrieve the name of the "
305  "TruthEtIsolations container (requested: ["
306  << container->genEventName() << "])");
307  return StatusCode::RECOVERABLE;
308  }
309 
310  const TruthEtIsolationsContainer* etIsols = nullptr;
311  if ( !evtStore()->retrieve( etIsols, etIsolName ).isSuccess() ) {
312  ATH_MSG_WARNING("Could not retrieve the TruthEtIsolations container at ["
313  << etIsolName << "] !!");
314  return StatusCode::RECOVERABLE;
315  }
316 
317  // index of HepMC::GenEvent within the McEventCollection is the same
318  // than the one of the TruthEtIsolations within the TruthEtIsolationsCont.
319  container->setEtIsolations( etIsols, genEventIndex );
320  }
321 
322  return StatusCode::SUCCESS;
323 }
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
CurrentEventStore.h
Hold a pointer to the current event store.
TruthParticleCnvTool::m_mcEventsReadHandleKey
SG::ReadHandleKey< McEventCollection > m_mcEventsReadHandleKey
ReadHandleKey for the McEventCollection the TruthParticles will be made from.
Definition: TruthParticleCnvTool.h:118
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
GenEvent.h
TruthParticleContainer::setEtIsolations
void setEtIsolations(const ElementLink< TruthEtIsolationsContainer > &etIsolations)
Setup the persistent pointer toward the TruthEtIsolations.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:307
ParticleImpl::pdgId
virtual int pdgId() const
Return enum indicating particle id the enum file is available in Event/EventKernel/PdtPdg....
Definition: ParticleImpl.h:738
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TruthParticleContainer::m_particles
Map_t m_particles
Dictionary to ease the extraction of a TruthParticle (contained by this collection) from the HepMC::G...
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:205
TruthParticleCnvTool::m_pdt
const HepPDT::ParticleDataTable * m_pdt
Particle Property service.
Definition: TruthParticleCnvTool.h:126
True
#define True
Definition: ALFA_SvdCalc.h:37
SG::ReadHandle< McEventCollection >
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:513
GenVertex.h
TruthParticleCnvTool.h
TruthEtIsolationsContainer
Definition: TruthEtIsolationsContainer.h:26
skel.it
it
Definition: skel.GENtoEVGEN.py:423
TruthParticle
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
TruthParticleCnvTool::IsolTool_t
ToolHandle< ITruthIsolationTool > IsolTool_t
Definition: TruthParticleCnvTool.h:150
PileUpClassification::PileuType_t
PileuType_t
define some Pile-up classification Important : this classification is copied in McParticleAlgs/python...
Definition: PileUpClassification.h:29
SG::CurrentEventStore::store
static IProxyDict * store()
Fetch the current store.
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
TruthParticleCnvTool::m_isolationTool
IsolTool_t m_isolationTool
Pointer to the ITruthIsolationTool to be able to retrieve the previously computed transverse energy i...
Definition: TruthParticleCnvTool.h:155
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
TruthParticle::genParticle
HepMC::ConstGenParticlePtr genParticle() const
Retrieve the GenParticle this TruthParticle has been made from (if any)
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:279
TruthParticleContainer
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:42
TruthParticle::setGenEventIndex
void setGenEventIndex(size_t index)
set the GenEvent index
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:338
IProxyDict
A proxy dictionary.
Definition: AthenaKernel/AthenaKernel/IProxyDict.h:51
GenParticle.h
TruthParticleCnvTool::TruthParticleCnvTool
TruthParticleCnvTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Definition: TruthParticleCnvTool.cxx:48
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
TruthParticleCnvTool::m_doEtIsolation
BooleanProperty m_doEtIsolation
Switch to compute or not the Et-isolations for TruthParticle (and their underlying HepMC::GenParticle...
Definition: TruthParticleCnvTool.h:148
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ITruthParticleVisitor
Definition: ITruthParticleVisitor.h:30
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
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
TruthParticleCnvTool::execute
virtual StatusCode execute() const override
Definition: TruthParticleCnvTool.cxx:146
TruthParticleCnvTool::m_selectSignalType
PileUpClassification::PileuType_t m_selectSignalType
Type of truth particle we want to create.
Definition: TruthParticleCnvTool.h:130
xAOD::TruthParticle
TruthParticle_v1 TruthParticle
Typedef to implementation.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticle.h:15
test_pyathena.parent
parent
Definition: test_pyathena.py:15
TruthParticleContainer::setGenEvent
void setGenEvent(const ElementLink< McEventCollection > &genEvent)
Setup the persistent pointer toward the HepMC::GenEvent this TruthParticleContainer is proxying.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:290
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TruthParticle.h
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TruthParticleContainer::Map_t
std::unordered_map< long, const TruthParticle * > Map_t
barcode to TruthParticle dictionary
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:59
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ParticleDataType
Information about type of data used to fill particle.
Definition: Event/EventKernel/EventKernel/IParticle.h:35
charge
double charge(const T &p)
Definition: AtlasPID.h:494
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
TruthParticleCnvTool::initialize
virtual StatusCode initialize() override
Athena Algorithm's Hooks.
Definition: TruthParticleCnvTool.cxx:100
SG::ReadHandle::ptr
const_pointer_type ptr()
Dereference the pointer.
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
TruthParticleCnvTool::~TruthParticleCnvTool
virtual ~TruthParticleCnvTool()
Destructor:
Definition: TruthParticleCnvTool.cxx:93
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
TruthParticleCnvTool::m_selectSignalTypeProp
int m_selectSignalTypeProp
Type of truth particle we want to create (property to be set by jobO)
Definition: TruthParticleCnvTool.h:134
TruthParticleContainer::setParticles
void setParticles(const Map_t &parts)
Setup the dictionary of barcodes-to-TruthParticle.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:323
TruthParticleContainer::genEventName
const std::string & genEventName() const
Retrieve the name (StoreGate location) of the HepMC::GenEvent this TruthParticleContainer is proxying...
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:249
ITruthParticleVisitor::visit
virtual void visit(TruthParticle *truthParticle) const =0
The method to visit a TruthParticle to apply further modifications to the instance at hand.
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
PileUpClassification::ALLMINBIAS
@ ALLMINBIAS
Definition: PileUpClassification.h:31
AthAlgTool
Definition: AthAlgTool.h:26
Polarization.h
TruthParticleCnvTool::m_mcPartsOutputWriteHandleKey
SG::WriteHandleKey< TruthParticleContainer > m_mcPartsOutputWriteHandleKey
Output TruthParticle WriteHandleKey (built from the McEventCollection)
Definition: TruthParticleCnvTool.h:122
TruthParticleCnvTool::setDataType
void setDataType(const int type)
Inline methods:
Definition: TruthParticleCnvTool.h:162
TruthParticle::setCharge
void setCharge(const ChargeType charge)
Set the charge of this TruthParticle.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:327
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
HepMCHelpers.h
TruthParticleCnvTool::convert
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...
Definition: TruthParticleCnvTool.cxx:224
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
ITruthIsolationTool.h
PileUpClassification::findEventIterators
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.
Definition: PileUpClassification.cxx:8
ServiceHandle< IPartPropSvc >
TruthParticleCnvTool::m_dataType_int
int m_dataType_int
Definition: TruthParticleCnvTool.h:112