ATLAS Offline Software
xAODTruthParticleSlimmerMET.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 
22 
24 
25 xAODTruthParticleSlimmerMET::xAODTruthParticleSlimmerMET(const std::string &name, ISvcLocator *svcLoc)
26  : AthAlgorithm(name, svcLoc)
27  , m_classif("MCTruthClassifier/DFCommonTruthClassifier")
28 {
29  declareProperty("xAODTruthParticleContainerName", m_xaodTruthParticleContainerName = "TruthParticles");
30  declareProperty("xAODTruthParticleContainerNameMET", m_xaodTruthParticleContainerNameMET = "TruthMET");
31  declareProperty("xAODTruthEventContainerName", m_xaodTruthEventContainerName = "TruthEvents");
32 }
33 
35 {
36  ATH_MSG_INFO("xAOD input TruthParticleContainer name = " << m_xaodTruthParticleContainerName);
37  ATH_MSG_INFO("xAOD output TruthParticleContainerMET name = " << m_xaodTruthParticleContainerNameMET);
38 
39  ATH_CHECK(m_classif.retrieve());
40 
41  return StatusCode::SUCCESS;
42 }
43 
45 {
46  // If the containers already exists then assume that nothing needs to be done
47  if (evtStore()->contains<xAOD::TruthParticleContainer>(m_xaodTruthParticleContainerNameMET))
48  {
49  ATH_MSG_WARNING("xAOD MET Truth Particles are already available in the event");
50  return StatusCode::SUCCESS;
51  }
52 
53  // Create new output container
54  xAOD::TruthParticleContainer *xTruthParticleContainerMET = new xAOD::TruthParticleContainer();
55  CHECK(evtStore()->record(xTruthParticleContainerMET, m_xaodTruthParticleContainerNameMET));
56  xAOD::TruthParticleAuxContainer *xTruthParticleAuxContainerMET = new xAOD::TruthParticleAuxContainer();
57  CHECK(evtStore()->record(xTruthParticleAuxContainerMET, m_xaodTruthParticleContainerNameMET + "Aux."));
58  xTruthParticleContainerMET->setStore(xTruthParticleAuxContainerMET);
59  ATH_MSG_INFO("Recorded TruthParticleContainerMET with key: " << m_xaodTruthParticleContainerNameMET);
60 
61  // Retrieve full TruthParticle container
62  const xAOD::TruthParticleContainer *xTruthParticleContainer;
63  if (evtStore()->retrieve(xTruthParticleContainer, m_xaodTruthParticleContainerName).isFailure())
64  {
65  ATH_MSG_ERROR("No TruthParticle collection with name " << m_xaodTruthParticleContainerName << " found in StoreGate!");
66  return StatusCode::FAILURE;
67  }
68  // Retrieve full TruthEventContainer container
69  const xAOD::TruthEventContainer *xTruthEventContainer=NULL;
70  if (evtStore()->retrieve(xTruthEventContainer, m_xaodTruthEventContainerName).isFailure())
71  {
72  ATH_MSG_ERROR("No TruthEvent collection with name " << m_xaodTruthEventContainerName << " found in StoreGate!");
73  return StatusCode::FAILURE;
74  }
75 
76  // Set up decorators if needed
77  const static SG::AuxElement::Decorator<bool> isPrompt("isPrompt");
78 
79  // Loop over full TruthParticle container
81  for (itr = xTruthEventContainer->begin(); itr!=xTruthEventContainer->end(); ++itr) {
82 
83  unsigned int nPart = (*itr)->nTruthParticles();
84  std::vector<int> uniqueID_list;
85  int zero_uniqueID=0;
86  int dup_uniqueID=0;
87 
88  for (unsigned int iPart = 0; iPart < nPart; ++iPart) {
89  const xAOD::TruthParticle* theParticle = (*itr)->truthParticle(iPart);
90 
91  int my_uniqueID = HepMC::uniqueID(theParticle);
92  if ( my_uniqueID == HepMC::UNDEFINED_ID ) {
93  zero_uniqueID++;
94  continue;
95  }
96  bool found = false;
97  if (uniqueID_list.size() > 0){
98  found = (std::find(uniqueID_list.begin(), uniqueID_list.end(), my_uniqueID) != uniqueID_list.end());
99  if(found) {
100  dup_uniqueID++;
101  continue;}
102  }
103  uniqueID_list.push_back(my_uniqueID);
104 
105 
106 
107  // stable and non-interacting, implemented from DerivationFramework
108  //https://gitlab.cern.ch/atlas/athena/-/blob/master/PhysicsAnalysis/DerivationFramework/DerivationFrameworkMCTruth/python/MCTruthCommon.py#L183
109  // which in turn use the implementation from Reconstruction
110  //https://gitlab.cern.ch/atlas/athena/blob/21.0/Reconstruction/MET/METReconstruction/Root/METTruthTool.cxx#L143
111  if (!theParticle->isGenStable()) continue;
112  if (MC::isInteracting(theParticle->pdgId())) continue;
113 
114 
115  xAOD::TruthParticle *xTruthParticle = new xAOD::TruthParticle();
116  xTruthParticleContainerMET->push_back( xTruthParticle );
117 
118  // Fill with numerical content
119  *xTruthParticle=*theParticle;
120 
121  //Decorate
122  isPrompt(*xTruthParticle) = Common::prompt(theParticle,m_classif);
123  }
124  if(zero_uniqueID != 0 || dup_uniqueID !=0 ) ATH_MSG_INFO("Found " << zero_uniqueID << " uniqueID=0 particles and " <<dup_uniqueID <<"duplicated");
125  }
126 
127  return StatusCode::SUCCESS;
128 }
129 
130 
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
xAODTruthParticleSlimmerMET::m_xaodTruthParticleContainerName
std::string m_xaodTruthParticleContainerName
Definition: xAODTruthParticleSlimmerMET.h:35
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TruthParticleContainer.h
xAODTruthParticleSlimmerMET::m_classif
ToolHandle< IMCTruthClassifier > m_classif
Definition: xAODTruthParticleSlimmerMET.h:38
xAOD::TruthParticle_v1::isGenStable
bool isGenStable() const
Check if this is generator stable particle.
Definition: TruthParticle_v1.cxx:316
Common::prompt
bool prompt(const xAOD::TruthParticle *part, ToolHandle< IMCTruthClassifier > &m_classif)
Definition: Common.cxx:7
xAODTruthParticleSlimmerMET::m_xaodTruthParticleContainerNameMET
std::string m_xaodTruthParticleContainerNameMET
The key for the output xAOD truth containers.
Definition: xAODTruthParticleSlimmerMET.h:34
xAODTruthParticleSlimmerMET::m_xaodTruthEventContainerName
std::string m_xaodTruthEventContainerName
Definition: xAODTruthParticleSlimmerMET.h:36
TruthParticleAuxContainer.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
xAODTruthParticleSlimmerMET.h
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
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
xAODTruthParticleSlimmerMET::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: xAODTruthParticleSlimmerMET.cxx:34
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
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
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
xAODTruthParticleSlimmerMET::execute
virtual StatusCode execute()
Function executing the algorithm.
Definition: xAODTruthParticleSlimmerMET.cxx:44
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
AthAlgorithm
Definition: AthAlgorithm.h:47
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:55
Common.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
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
MCTruthPartClassifier::isPrompt
int isPrompt(const unsigned int classify, bool allow_prompt_tau_decays=true)
Definition: TruthClassifiers.h:180
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAODTruthParticleSlimmerMET::xAODTruthParticleSlimmerMET
xAODTruthParticleSlimmerMET(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
Definition: xAODTruthParticleSlimmerMET.cxx:25
MC::isInteracting
bool isInteracting(const T &p)
Identify if the particle with given PDG ID would not interact with the detector, i....
Definition: HepMCHelpers.h:23
TruthParticle.h
xAOD::TruthParticle_v1::pdgId
int pdgId() const
PDG ID code.
HepMCHelpers.h
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.