ATLAS Offline Software
xAODTruthParticleSlimmerGen.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 #include "AthLinks/ElementLink.h"
7 
9 
10 #include "GaudiKernel/MsgStream.h"
11 #include "GaudiKernel/DataSvc.h"
12 #include "GaudiKernel/PhysicalConstants.h"
13 
17 
19 
21 
23 
24 xAODTruthParticleSlimmerGen::xAODTruthParticleSlimmerGen(const std::string &name, ISvcLocator *svcLoc)
25  : AthAlgorithm(name, svcLoc)
26  , m_classif("MCTruthClassifier/DFCommonTruthClassifier")
27 {
28  declareProperty("xAODTruthParticleContainerName", m_xaodTruthParticleContainerName = "TruthParticles");
29  declareProperty("xAODTruthParticleContainerNameGen", m_xaodTruthParticleContainerNameGen = "TruthGen");
30  declareProperty("xAODTruthEventContainerName", m_xaodTruthEventContainerName = "TruthEvents");
31 }
32 
34 {
35  ATH_MSG_INFO("xAOD input TruthParticleContainer name = " << m_xaodTruthParticleContainerName);
36  ATH_MSG_INFO("xAOD output TruthParticleContainerGen name = " << m_xaodTruthParticleContainerNameGen);
37 
38  ATH_CHECK(m_classif.retrieve());
39 
40  return StatusCode::SUCCESS;
41 }
42 
44 {
45  // If the containers already exists then assume that nothing needs to be done
46  if (evtStore()->contains<xAOD::TruthParticleContainer>(m_xaodTruthParticleContainerNameGen))
47  {
48  ATH_MSG_WARNING("xAOD Gen Truth Particles are already available in the event");
49  return StatusCode::SUCCESS;
50  }
51 
52  // Create new output container
53  xAOD::TruthParticleContainer *xTruthParticleContainerGen = new xAOD::TruthParticleContainer();
54  CHECK(evtStore()->record(xTruthParticleContainerGen, m_xaodTruthParticleContainerNameGen));
55  xAOD::TruthParticleAuxContainer *xTruthParticleAuxContainerGen = new xAOD::TruthParticleAuxContainer();
56  CHECK(evtStore()->record(xTruthParticleAuxContainerGen, m_xaodTruthParticleContainerNameGen + "Aux."));
57  xTruthParticleContainerGen->setStore(xTruthParticleAuxContainerGen);
58  ATH_MSG_INFO("Recorded TruthParticleContainerGen with key: " << m_xaodTruthParticleContainerNameGen);
59 
60  // Retrieve full TruthParticle container
61  const xAOD::TruthParticleContainer *xTruthParticleContainer;
62  if (evtStore()->retrieve(xTruthParticleContainer, m_xaodTruthParticleContainerName).isFailure())
63  {
64  ATH_MSG_ERROR("No TruthParticle collection with name " << m_xaodTruthParticleContainerName << " found in StoreGate!");
65  return StatusCode::FAILURE;
66  }
67  // Retrieve full TruthEventContainer container
68  const xAOD::TruthEventContainer *xTruthEventContainer=NULL;
69  if (evtStore()->retrieve(xTruthEventContainer, m_xaodTruthEventContainerName).isFailure())
70  {
71  ATH_MSG_ERROR("No TruthEvent collection with name " << m_xaodTruthEventContainerName << " found in StoreGate!");
72  return StatusCode::FAILURE;
73  }
74 
75  // Loop over full TruthParticle container
77  for (itr = xTruthEventContainer->begin(); itr!=xTruthEventContainer->end(); ++itr) {
78 
79  unsigned int nPart = (*itr)->nTruthParticles();
80  std::vector<int> uniqueID_list;
81  int zero_uniqueID=0;
82  int dup_uniqueID=0;
83 
84  for (unsigned int iPart = 0; iPart < nPart; ++iPart) {
85  const xAOD::TruthParticle* theParticle = (*itr)->truthParticle(iPart);
86 
87  const int my_uniqueID = HepMC::uniqueID(theParticle);
88  if (my_uniqueID == HepMC::UNDEFINED_ID ) {
89  zero_uniqueID++;
90  continue;
91  }
92  bool found = false;
93  if (uniqueID_list.size() > 0){
94  found = (std::find(uniqueID_list.begin(), uniqueID_list.end(), my_uniqueID) != uniqueID_list.end());
95  if(found) {
96  dup_uniqueID++;
97  continue;}
98  }
99  uniqueID_list.push_back(my_uniqueID);
100 
101 
102 
103  xAOD::TruthParticle *xTruthParticle = new xAOD::TruthParticle();
104  xTruthParticleContainerGen->push_back( xTruthParticle );
105  // Fill with numerical content
106  *xTruthParticle=*theParticle;
107 
108  }
109  if (zero_uniqueID != 0 || dup_uniqueID != 0) ATH_MSG_INFO("Found " << zero_uniqueID << " uniqueID=0 particles and " << dup_uniqueID << " duplicated");
110  }
111 
112  return StatusCode::SUCCESS;
113 }
114 
115 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
xAODTruthParticleSlimmerGen.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TruthParticleContainer.h
xAODTruthParticleSlimmerGen::execute
virtual StatusCode execute()
Function executing the algorithm.
Definition: xAODTruthParticleSlimmerGen.cxx:43
xAODTruthParticleSlimmerGen::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: xAODTruthParticleSlimmerGen.cxx:33
TruthParticleAuxContainer.h
xAODTruthParticleSlimmerGen::m_xaodTruthParticleContainerName
std::string m_xaodTruthParticleContainerName
Definition: xAODTruthParticleSlimmerGen.h:33
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
xAOD::TruthParticleAuxContainer_v1
Auxiliary store for the truth vertices.
Definition: TruthParticleAuxContainer_v1.h:27
xAOD::TruthParticleAuxContainer
TruthParticleAuxContainer_v1 TruthParticleAuxContainer
Declare the latest version of the truth particle auxiliary container.
Definition: TruthParticleAuxContainer.h:16
IMCTruthClassifier.h
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
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
xAOD::TruthParticle
TruthParticle_v1 TruthParticle
Typedef to implementation.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticle.h:15
xAODTruthParticleSlimmerGen::m_xaodTruthEventContainerName
std::string m_xaodTruthEventContainerName
Definition: xAODTruthParticleSlimmerGen.h:34
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
xAODTruthParticleSlimmerGen::m_classif
ToolHandle< IMCTruthClassifier > m_classif
Definition: xAODTruthParticleSlimmerGen.h:36
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
AthAlgorithm
Definition: AthAlgorithm.h:47
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:55
xAODTruthParticleSlimmerGen::xAODTruthParticleSlimmerGen
xAODTruthParticleSlimmerGen(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
Definition: xAODTruthParticleSlimmerGen.cxx:24
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
xAODTruthParticleSlimmerGen::m_xaodTruthParticleContainerNameGen
std::string m_xaodTruthParticleContainerNameGen
The key for the output xAOD truth containers.
Definition: xAODTruthParticleSlimmerGen.h:32
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.
xAOD::TruthParticleContainer
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticleContainer.h:17
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TruthParticle.h
HepMCHelpers.h
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.