ATLAS Offline Software
xAODTruthParticleSlimmerTau.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 
11 
12 #include "GaudiKernel/MsgStream.h"
13 #include "GaudiKernel/DataSvc.h"
14 #include "GaudiKernel/PhysicalConstants.h"
15 
19 
21 
24 
25 using namespace std;
26 
28  : AthAlgorithm(name, svcLoc), m_classifier("MCTruthClassifier/MCTruthClassifier")
29 {
30  declareProperty("xAODTruthParticleContainerName", m_xaodTruthParticleContainerName = "TruthParticles");
31  declareProperty("xAODTruthTauParticleContainerName", m_xaodTruthTauParticleContainerName = "TruthTaus");
32  declareProperty("ForceRerun", m_forceRerun = false);
33  declareProperty("tau_pt_selection", m_tau_pt_selection = 0.001 * Gaudi::Units::GeV); //User provides units in MeV!
34  declareProperty("abseta_selection", m_abseta_selection = 10.);
35 }
36 
38 {
39  ATH_CHECK(m_classifier.retrieve());
40 
41  ATH_MSG_INFO("xAOD input TruthParticleContainer name = " << m_xaodTruthParticleContainerName);
42  ATH_MSG_INFO("xAOD output TruthTauParticleContainer name = " << m_xaodTruthTauParticleContainerName);
43  return StatusCode::SUCCESS;
44 }
45 
47 {
48  CLHEP::HepLorentzVector nu(0, 0, 0, 0);
49  if (((std::abs(part->pdgId()) == 12) || (std::abs(part->pdgId()) == 14) || (std::abs(part->pdgId()) == 16)) && MC::isPhysical(part))
50  {
51  nu.setPx(part->px());
52  nu.setPy(part->py());
53  nu.setPz(part->pz());
54  nu.setE(part->e());
55  }
56  if (!part->hasDecayVtx())
57  return nu;
58 
59  for (size_t n = 0; n < part->nChildren(); ++n)
60  nu += sumDaughterNeutrinos(part->child(n));
61 
62  return nu;
63 }
64 
66 {
67 
68  CLHEP::HepLorentzVector nutau;
69 
70  // If the containers already exists then assume that nothing needs to be done
71  if (evtStore()->contains<xAOD::TruthParticleContainer>(m_xaodTruthTauParticleContainerName) &&
72  !m_forceRerun)
73  {
74  ATH_MSG_WARNING("xAOD Tau Truth Particles are already available in the event");
75  return StatusCode::SUCCESS;
76  }
77 
78  // Create new output container
79  xAOD::TruthParticleContainer *xTruthTauParticleContainer = new xAOD::TruthParticleContainer();
80  CHECK(evtStore()->record(xTruthTauParticleContainer, m_xaodTruthTauParticleContainerName));
81  xAOD::TruthParticleAuxContainer *xTruthTauParticleAuxContainer = new xAOD::TruthParticleAuxContainer();
82  CHECK(evtStore()->record(xTruthTauParticleAuxContainer, m_xaodTruthTauParticleContainerName + "Aux."));
83  xTruthTauParticleContainer->setStore(xTruthTauParticleAuxContainer);
84  ATH_MSG_INFO("Recorded TruthTauParticleContainer with key: " << m_xaodTruthTauParticleContainerName);
85 
86  // Retrieve full TruthParticle container
87  const xAOD::TruthParticleContainer *xTruthParticleContainer;
88  if (evtStore()->retrieve(xTruthParticleContainer, m_xaodTruthParticleContainerName).isFailure())
89  {
90  ATH_MSG_ERROR("No TruthParticle collection with name " << m_xaodTruthParticleContainerName << " found in StoreGate!");
91  return StatusCode::FAILURE;
92  }
93 
94  // Set up decorators
95 
96  const static SG::AuxElement::Decorator<unsigned int> originDecorator("classifierParticleOrigin");
97  const static SG::AuxElement::Decorator<unsigned int> typeDecorator("classifierParticleType");
98  const static SG::AuxElement::Decorator<unsigned int> outcomeDecorator("classifierParticleOutCome");
99  const static SG::AuxElement::Decorator<unsigned int> classificationDecorator("Classification");
100  const static SG::AuxElement::Decorator<int> parenthadronPIDDecorator("parentHadronID");
101 
102  // sum of neutrinos 4-vector in tau decay products
103  const static SG::AuxElement::Decorator<CLHEP::HepLorentzVector> nuDecorator("nuVector");
104  const static SG::AuxElement::Decorator<int> tauTypeDecorator("tauType");
105 
106  // Loop over full TruthParticle container
107 
108  std::vector<int> uniqueID_list;
109  int zero_uniqueID=0;
110  int dup_uniqueID = 0;
111 
112  unsigned int nParticles = xTruthParticleContainer->size();
113 
114  for (unsigned int iPart = 0; iPart < nParticles; ++iPart)
115  {
116  ElementLink<xAOD::TruthParticleContainer> eltp(*xTruthParticleContainer, iPart);
117  const xAOD::TruthParticle *theParticle = (*xTruthParticleContainer)[iPart];
118 
119  int my_uniqueID = HepMC::uniqueID(theParticle);
120  if ( my_uniqueID == HepMC::UNDEFINED_ID ) {
121  zero_uniqueID++;
122  continue;
123  }
124  bool found = false;
125  if (uniqueID_list.size() > 0){
126  found = (std::find(uniqueID_list.begin(), uniqueID_list.end(), my_uniqueID) != uniqueID_list.end());
127  if(found) {
128  dup_uniqueID++;
129  continue;}
130  }
131  uniqueID_list.push_back(my_uniqueID);
132 
133  float this_abseta = theParticle->abseta();
134  float this_pt = theParticle->pt();
135 
136 
137  //Save Taus above 0.001 GeV, & with any eta (may be changed on JOs level eg. to dectector acceptance of eta 4.5)
138  // see GeneratorFilters/share/common/xAODTauFilter_Common.py
139  // we want to avoid status 3 taus
140  if (MC::isPhysical(theParticle) && MC::isTau(theParticle) && this_pt >= m_tau_pt_selection && this_abseta < m_abseta_selection)
141  {
142  xAOD::TruthParticle *xTruthParticle = new xAOD::TruthParticle();
143 
144  xTruthTauParticleContainer->push_back(xTruthParticle);
145  // Fill with numerical content
146  *xTruthParticle=*theParticle;
147  const xAOD::TruthParticle *tau = theParticle;
148  nutau = sumDaughterNeutrinos(tau);
149 
150  nuDecorator(*xTruthParticle) = nutau;
151 
152  int tauType = 0;
153  for (size_t n = 0; n < tau->nChildren(); ++n)
154  {
155  if (tau->child(n)->absPdgId() == 12)
156  tauType = 1; //Tau decays into an electron
157  else if (tau->child(n)->absPdgId() == 14)
158  tauType = 2; //Tau decays into a muon
159  else if (MC::isTau(tau->child(n)))
160  tauType = 11; //Tau radiates a particle and decays into another tau
161  }
162  tauTypeDecorator(*xTruthParticle) = tauType;
163 
164 unsigned int particleOutCome;
165 unsigned int result;
166 unsigned int particleType;
167 unsigned int particleOrigin;
168 int hadron_pdg;
169 Common::classify(m_classifier,theParticle,particleOutCome,result,hadron_pdg,particleType,particleOrigin );
170  typeDecorator(*xTruthParticle) = particleType;
171  originDecorator(*xTruthParticle) = particleOrigin;
172  outcomeDecorator(*xTruthParticle) = particleOutCome;
173 
174  classificationDecorator(*xTruthParticle) = result;
175  parenthadronPIDDecorator(*xTruthParticle) = hadron_pdg;
176  }
177 
178  } //end of loop over particles
179  if(zero_uniqueID!=0 || dup_uniqueID != 0) ATH_MSG_INFO("Found " << zero_uniqueID << " uniqueID=0 particles and " << dup_uniqueID << " duplicated");
180  return StatusCode::SUCCESS;
181 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
xAOD::TruthParticle_v1::absPdgId
int absPdgId() const
Absolute PDG ID code (often useful)
get_generator_info.result
result
Definition: get_generator_info.py:21
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
Common::classify
void classify(ToolHandle< IMCTruthClassifier > &m_classif, const xAOD::TruthParticle *theParticle, unsigned int &particleOutCome, unsigned int &result, int &hadron_pdg, unsigned int &particleType, unsigned int &particleOrigin)
Definition: Common.cxx:96
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TruthParticleContainer.h
xAODTruthParticleSlimmerTau::m_forceRerun
bool m_forceRerun
a flag to force rerunning (useful for rerunning on ESDs)
Definition: xAODTruthParticleSlimmerTau.h:52
xAODTruthParticleSlimmerTau::m_abseta_selection
double m_abseta_selection
Definition: xAODTruthParticleSlimmerTau.h:49
particleType
Definition: particleType.h:29
xAODTruthParticleSlimmerTau::xAODTruthParticleSlimmerTau
xAODTruthParticleSlimmerTau(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
Definition: xAODTruthParticleSlimmerTau.cxx:27
xAODTruthParticleSlimmerTau::m_tau_pt_selection
double m_tau_pt_selection
Selection values for keeping taus and leptons.
Definition: xAODTruthParticleSlimmerTau.h:48
TruthParticleAuxContainer.h
MC::isPhysical
bool isPhysical(const T &p)
Definition: HepMCHelpers.h:32
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
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
McEventCollection.h
beamspotman.n
n
Definition: beamspotman.py:731
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
xAODTruthParticleSlimmerTau::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: xAODTruthParticleSlimmerTau.cxx:37
xAODTruthParticleSlimmerTau::m_xaodTruthParticleContainerName
std::string m_xaodTruthParticleContainerName
Definition: xAODTruthParticleSlimmerTau.h:45
xAODTruthParticleSlimmerTau.h
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
xAODTruthParticleSlimmerTau::execute
virtual StatusCode execute()
Function executing the algorithm.
Definition: xAODTruthParticleSlimmerTau.cxx:65
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
isTau
bool isTau(const T &p)
Definition: AtlasPID.h:148
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:55
xAODTruthParticleSlimmerTau::m_classifier
ToolHandle< IMCTruthClassifier > m_classifier
Definition: xAODTruthParticleSlimmerTau.h:54
xAOD::TruthParticle_v1::nChildren
size_t nChildren() const
Number of children of this particle.
Definition: TruthParticle_v1.cxx:140
Common.h
xAODTruthParticleSlimmerTau::m_xaodTruthTauParticleContainerName
std::string m_xaodTruthTauParticleContainerName
The key for the output xAOD truth containers.
Definition: xAODTruthParticleSlimmerTau.h:44
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
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::TruthParticle_v1::child
const TruthParticle_v1 * child(size_t i=0) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
Definition: TruthParticle_v1.cxx:149
xAOD::TruthParticle_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: TruthParticle_v1.cxx:166
xAODTruthParticleSlimmerTau::sumDaughterNeutrinos
CLHEP::HepLorentzVector sumDaughterNeutrinos(const xAOD::TruthParticle *tau)
Definition: xAODTruthParticleSlimmerTau.cxx:46
TruthParticle.h
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
HepMCHelpers.h
xAOD::TruthParticle_v1::abseta
double abseta() const
The absolute pseudorapidity ( ) of the particle.
Definition: TruthParticle_v1.cxx:218