ATLAS Offline Software
xAODTruthParticleSlimmerMuon.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 
10 
11 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/DataSvc.h"
13 #include "GaudiKernel/PhysicalConstants.h"
14 
18 
20 
21 xAODTruthParticleSlimmerMuon::xAODTruthParticleSlimmerMuon(const std::string &name, ISvcLocator *svcLoc)
22  : AthAlgorithm(name, svcLoc)
23 {
24  declareProperty("xAODTruthParticleContainerName", m_xaodTruthParticleContainerName = "TruthParticles");
25  declareProperty("xAODTruthParticleContainerNameMuon", m_xaodTruthParticleContainerNameMuon = "TruthMuons");
26  declareProperty("xAODTruthEventContainerName", m_xaodTruthEventContainerName = "TruthEvents");
27 }
28 
30 {
31  ATH_MSG_INFO("xAOD input TruthParticleContainer name = " << m_xaodTruthParticleContainerName);
32  ATH_MSG_INFO("xAOD output TruthParticleContainerMuon name = " << m_xaodTruthParticleContainerNameMuon);
33  ATH_MSG_INFO("xAOD input xAODTruthEventContainerName name = " << m_xaodTruthEventContainerName);
34  return StatusCode::SUCCESS;
35 }
36 
38 {
39  // If the containers already exists then assume that nothing needs to be done
40  if (evtStore()->contains<xAOD::TruthParticleContainer>(m_xaodTruthParticleContainerNameMuon))
41  {
42  ATH_MSG_WARNING("xAOD Muon Truth Particles are already available in the event");
43  return StatusCode::SUCCESS;
44  }
45 
46  // Create new output container
47  xAOD::TruthParticleContainer *xTruthParticleContainerMuon = new xAOD::TruthParticleContainer();
48  CHECK(evtStore()->record(xTruthParticleContainerMuon, m_xaodTruthParticleContainerNameMuon));
49  xAOD::TruthParticleAuxContainer *xTruthParticleAuxContainerMuon = new xAOD::TruthParticleAuxContainer();
50  CHECK(evtStore()->record(xTruthParticleAuxContainerMuon, m_xaodTruthParticleContainerNameMuon + "Aux."));
51  xTruthParticleContainerMuon->setStore(xTruthParticleAuxContainerMuon);
52  ATH_MSG_INFO("Recorded TruthParticleContainerMuon with key: " << m_xaodTruthParticleContainerNameMuon);
53 
54  // Retrieve full TruthEventContainer container
55  const xAOD::TruthEventContainer *xTruthEventContainer=NULL;
56  if (evtStore()->retrieve(xTruthEventContainer, m_xaodTruthEventContainerName).isFailure())
57  {
58  ATH_MSG_ERROR("No TruthEvent collection with name " << m_xaodTruthEventContainerName << " found in StoreGate!");
59  return StatusCode::FAILURE;
60  }
61  // Set up decorators if needed
63  for (itr = xTruthEventContainer->begin(); itr!=xTruthEventContainer->end(); ++itr) {
64 
65  std::vector<int> uniqueID_list;
66  int zero_uniqueID=0;
67  int dup_uniqueID=0;
68  unsigned int nPart = (*itr)->nTruthParticles();
69  for (unsigned int iPart = 0; iPart < nPart; ++iPart) {
70  const xAOD::TruthParticle* theParticle = (*itr)->truthParticle(iPart);
71 
72  int my_uniqueID = HepMC::uniqueID(theParticle);
73  if ( my_uniqueID == HepMC::UNDEFINED_ID ) {
74  zero_uniqueID++;
75  continue;
76  }
77  bool found = false;
78  if (uniqueID_list.size() > 0){
79  found = (std::find(uniqueID_list.begin(), uniqueID_list.end(), my_uniqueID) != uniqueID_list.end());
80  if(found) {
81  dup_uniqueID++;
82  continue;}
83  }
84  uniqueID_list.push_back(my_uniqueID);
85 
86 
87  //Save stable Muons
88  if (MC::isStable(theParticle) && MC::isMuon(theParticle))
89  {
90  xAOD::TruthParticle *xTruthParticle = new xAOD::TruthParticle();
91  xTruthParticleContainerMuon->push_back( xTruthParticle );
92  // Fill with numerical content
93  *xTruthParticle=*theParticle;
94  }
95  }
96  ATH_MSG_INFO("Found "<< zero_uniqueID << " uniqueID-zero partciles and " << dup_uniqueID << " duplicates");
97  }
98 
99  return StatusCode::SUCCESS;
100 }
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
xAODTruthParticleSlimmerMuon::xAODTruthParticleSlimmerMuon
xAODTruthParticleSlimmerMuon(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
Definition: xAODTruthParticleSlimmerMuon.cxx:21
xAODTruthParticleSlimmerMuon::m_xaodTruthParticleContainerName
std::string m_xaodTruthParticleContainerName
Definition: xAODTruthParticleSlimmerMuon.h:34
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
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TruthParticleContainer.h
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
xAODTruthParticleSlimmerMuon::m_xaodTruthEventContainerName
std::string m_xaodTruthEventContainerName
Definition: xAODTruthParticleSlimmerMuon.h:35
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
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
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
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
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
errorcheck.h
Helpers for checking error return status codes and reporting errors.
xAODTruthParticleSlimmerMuon::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: xAODTruthParticleSlimmerMuon.cxx:29
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
xAODTruthParticleSlimmerMuon::execute
virtual StatusCode execute()
Function executing the algorithm.
Definition: xAODTruthParticleSlimmerMuon.cxx:37
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.
MC::isStable
bool isStable(const T &p)
Definition: HepMCHelpers.h:30
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAODTruthParticleSlimmerMuon.h
TruthParticle.h
HepMCHelpers.h
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
isMuon
bool isMuon(const T &p)
Definition: AtlasPID.h:145
xAODTruthParticleSlimmerMuon::m_xaodTruthParticleContainerNameMuon
std::string m_xaodTruthParticleContainerNameMuon
The key for the output xAOD truth containers.
Definition: xAODTruthParticleSlimmerMuon.h:33