ATLAS Offline Software
RedoTruthLinksAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "xAODTruth/TruthEvent.h"
9 #include "StoreGate/ReadHandle.h"
10 #include "StoreGate/WriteHandle.h"
11 
12 #include "RedoTruthLinksAlg.h"
13 
14 
15 using namespace std;
16 
17 namespace xAODMaker {
18 
19 
20  RedoTruthLinksAlg::RedoTruthLinksAlg( const string& name, ISvcLocator* svcLoc )
21  : AthAlgorithm( name, svcLoc )
22  {
23  }
24 
25 
27 
28  // initialize handles
31 
32  ATH_MSG_DEBUG("xAOD linksOnlyTruthEventContainer name = " << m_linksOnlyTruthEventContainerKey.key() );
33  ATH_MSG_DEBUG("xAOD TruthParticleLinksContainer name = " << m_truthLinkContainerKey.key());
34 
35 
36  return StatusCode::SUCCESS;
37  }
38 
39 
41 
43  ATH_CHECK(truthLinkVec.record(std::make_unique<xAODTruthParticleLinkVector>()));
44 
45 
47  // validity check is only really needed for serial running. Remove when MT is only way.
48  if (!xTruthEventContainer.isValid()) {
49  ATH_MSG_ERROR("Could not retrieve xAOD::TruthEventContainer with key:" <<
51  return StatusCode::FAILURE;
52  } else {
53  ATH_MSG_DEBUG( "Retrieved for reading xAOD::TruthEventContainer key: " <<
55  }
56  // Loop over events and particles
57  for (const xAOD::TruthEvent* evt : *xTruthEventContainer) {
58  for (const auto& par : evt->truthParticleLinks()) {
59  if ( !par.isValid() ) {
60  // This can happen if particles have been thinned.
61  ATH_MSG_VERBOSE("Found invalid particle element link in TruthEvent"); //< @todo Use HepMC evt number?
62  continue;
63  }
64  // Create link between HepMC and xAOD truth
66  truthLinkVec->push_back(new xAODTruthParticleLink(HepMcParticleLink(HepMC::barcode(*par), 0, HepMcParticleLink::IS_POSITION, HepMcParticleLink::IS_BARCODE), par)); // FIXME is barcode-based - using barcodes until xAOD::TruthParticle::id() is available
67  }
68  }
69 
70  std::stable_sort(truthLinkVec->begin(), truthLinkVec->end(), SortTruthParticleLink());
71  ATH_MSG_VERBOSE("Summarizing truth link size: " << truthLinkVec->size() );
72 
73  return StatusCode::SUCCESS;
74  }
75 
76 } // namespace xAODMaker
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
RedoTruthLinksAlg.h
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
xAODMaker
Definition: StoreGateSvc.h:72
xAODMaker::RedoTruthLinksAlg::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: RedoTruthLinksAlg.cxx:26
std::stable_sort
void stable_sort(std::reverse_iterator< DataModel_detail::iterator< DVL > > beg, std::reverse_iterator< DataModel_detail::iterator< DVL > > end, Compare comp)
Specialization of stable_sort for DataVector/List.
Definition: DVL_algorithms.h:711
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
xAODMaker::RedoTruthLinksAlg::m_truthLinkContainerKey
SG::WriteHandleKey< xAODTruthParticleLinkVector > m_truthLinkContainerKey
The key for the output xAOD truth containers.
Definition: RedoTruthLinksAlg.h:43
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
xAOD::TruthEvent_v1
Class describing a signal truth event in the MC record.
Definition: TruthEvent_v1.h:35
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAODMaker::RedoTruthLinksAlg::execute
virtual StatusCode execute()
Function executing the algorithm.
Definition: RedoTruthLinksAlg.cxx: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
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
errorcheck.h
Helpers for checking error return status codes and reporting errors.
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ReadHandle.h
Handle class for reading from StoreGate.
TruthEventContainer.h
xAODMaker::RedoTruthLinksAlg::m_linksOnlyTruthEventContainerKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_linksOnlyTruthEventContainerKey
Definition: RedoTruthLinksAlg.h:47
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TruthEvent.h
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.