ATLAS Offline Software
TruthDressingTool.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 // TruthDressingTool.cxx
7 // Author: Kevin Finelli (kevin.finelli@cern.ch)
8 // Create dressed (i.e. including FSR photons) 4-vectors of truth objects
9 
16 
18 
19 #include "fastjet/PseudoJet.hh"
20 #include "fastjet/ClusterSequence.hh"
21 #include <vector>
22 #include <string>
23 #include <algorithm>
24 #include <memory>
25 namespace {
26  static const SG::ConstAccessor<unsigned int> acc_origin("Classification");
27 }
28 // Constructor
30  const std::string& n,
31  const IInterface* p )
32  : AthAlgTool(t,n,p)
33 {
34  declareInterface<DerivationFramework::IAugmentationTool>(this);
35 }
36 
37 // Destructor
39 }
40 
41 // Athena initialize and finalize
43 {
44  // Initialise handle keys
45  ATH_CHECK(m_particlesKey.initialize());
46  ATH_CHECK(m_dressParticlesKey.initialize());
47  m_decorator_eKey = m_dressParticlesKey.key() + ".e_dressed";
48  ATH_CHECK(m_decorator_eKey.initialize());
49  m_decorator_ptKey = m_dressParticlesKey.key() + ".pt_dressed";
50  ATH_CHECK(m_decorator_ptKey.initialize());
51  m_decorator_etaKey= m_dressParticlesKey.key() + ".eta_dressed";
52  ATH_CHECK(m_decorator_etaKey.initialize());
53  m_decorator_phiKey= m_dressParticlesKey.key() + ".phi_dressed";
54  ATH_CHECK(m_decorator_phiKey.initialize());
55  m_decorator_pt_visKey = m_dressParticlesKey.key() + ".pt_vis_dressed";
56  ATH_CHECK(m_decorator_pt_visKey.initialize());
57  m_decorator_eta_visKey= m_dressParticlesKey.key() + ".eta_vis_dressed";
58  ATH_CHECK(m_decorator_eta_visKey.initialize());
59  m_decorator_phi_visKey= m_dressParticlesKey.key() + ".phi_vis_dressed";
60  ATH_CHECK(m_decorator_phi_visKey.initialize());
61  m_decorator_m_visKey= m_dressParticlesKey.key() + ".m_vis_dressed";
62  ATH_CHECK(m_decorator_m_visKey.initialize());
63  m_decorator_nphotonKey = m_dressParticlesKey.key() + ".nPhotons_dressed";
64  ATH_CHECK(m_decorator_nphotonKey.initialize());
65  if (!m_decorationName.empty()) {m_decorationKey = m_particlesKey.key()+"."+m_decorationName;}
66  else {m_decorationKey = m_particlesKey.key()+".unusedPhotonDecoration";}
67  ATH_CHECK(m_decorationKey.initialize());
68  m_truthClassKey = m_dressParticlesKey.key() + "." + SG::AuxTypeRegistry::instance().getName(acc_origin.auxid());
69  ATH_CHECK(m_truthClassKey.initialize());
70  return StatusCode::SUCCESS;
71 }
72 
73 // Function to do dressing, implements interface in IAugmentationTool
75 {
76  // Get the event context
77  const EventContext& ctx = Gaudi::Hive::currentContext();
78 
79  // Retrieve the truth collections
80  SG::ReadHandle<xAOD::TruthParticleContainer> truthParticles(m_particlesKey,ctx);
81  if (!truthParticles.isValid()) {
82  ATH_MSG_ERROR("Couldn't retrieve TruthParticle collection with name " << m_particlesKey);
83  return StatusCode::FAILURE;
84  }
85 
86  SG::ReadHandle<xAOD::TruthParticleContainer> dressTruthParticles(m_dressParticlesKey,ctx);
87  if (!dressTruthParticles.isValid()) {
88  ATH_MSG_ERROR("Couldn't retrieve TruthParticle collection with name " << m_dressParticlesKey);
89  return StatusCode::FAILURE;
90  }
91 
92  // Decorators
93  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_e(m_decorator_eKey, ctx);
94  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_pt(m_decorator_ptKey, ctx);
95  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_eta(m_decorator_etaKey, ctx);
96  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_phi(m_decorator_phiKey, ctx);
97  // for truth taus, use 'vis' in the decoration name to avoid prompt/visible tau momentum ambiguity
98  // use (pt,eta,phi,m) for taus, for consistency with other TauAnalysisTools decorations
99  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_pt_vis(m_decorator_pt_visKey, ctx);
100  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_eta_vis(m_decorator_eta_visKey, ctx);
101  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_phi_vis(m_decorator_phi_visKey, ctx);
102  SG::WriteDecorHandle< xAOD::TruthParticleContainer,float > decorator_m_vis(m_decorator_m_visKey, ctx);
103  SG::WriteDecorHandle< xAOD::TruthParticleContainer,int > decorator_nphoton(m_decorator_nphotonKey, ctx);
104  // One for the photons as well
105  SG::WriteDecorHandle< xAOD::TruthParticleContainer, char > dressDec (m_decorationKey, ctx);
106  // If we want to decorate, then we need to decorate everything with false to begin with
107  if (!m_decorationKey.key().empty()){
108  if (!dressDec.isAvailable()) {
109  for (const auto * particle : *truthParticles){
110  dressDec(*particle);
111  }
112  } // Loop over particles
113  } // We are using the decoration
114 
115  //get struct of helper functions
117 
118  std::vector<const xAOD::TruthParticle*> listOfParticlesToDress;
119  std::vector<xAOD::TruthParticle::FourMom_t> listOfDressedParticles;
120  std::vector<int> dressedParticlesNPhot;
121 
122  if(m_listOfPIDs.size()==1 && abs(m_listOfPIDs[0])==15) {
123  // when dressing only truth taus, it is assumed that the truth tau container has
124  // been built beforehand and is used as input
125  for (auto *pItr : *dressTruthParticles) {
126  listOfParticlesToDress.push_back(pItr);
127  }
128  }
129  else {
130  // non-prompt particles are still included here to ensure all particles
131  // will get the decoration; however further down only the prompt particles
132  // are actually dressed depending on the value of m_useLeptonsFromHadrons
133  decayHelper.constructListOfFinalParticles(dressTruthParticles.ptr(), listOfParticlesToDress, m_listOfPIDs, true);
134  }
135 
136  //initialize list of dressed particles
137  for (const auto& part : listOfParticlesToDress) {
138  listOfDressedParticles.push_back(part->p4());
139  dressedParticlesNPhot.push_back(0);
140  }
141 
142  //fill the photon list
143  std::vector<const xAOD::TruthParticle*> photonsFSRList;
144  std::vector<int> photonPID{22};
145  const bool pass = decayHelper.constructListOfFinalParticles(truthParticles.ptr(), photonsFSRList,
146  photonPID, m_usePhotonsFromHadrons);
147  if (!pass) {
148  ATH_MSG_WARNING("Cannot construct the list of final state particles "<<m_truthClassKey.fullKey());
149  }
150 
151  static const SG::ConstAccessor<double> pt_visAcc("pt_vis");
152  static const SG::ConstAccessor<double> eta_visAcc("eta_vis");
153  static const SG::ConstAccessor<double> phi_visAcc("phi_vis");
154  static const SG::ConstAccessor<double> mvisAcc("m_vis");
155 
156  // Do dR-based photon dressing (default)
157  if (!m_useAntiKt){
158  //loop over photons, uniquely associate each to nearest bare particle
159  for (const auto& phot : photonsFSRList ) {
160  double dRmin = m_coneSize;
161  int idx = -1;
162 
163  for (size_t i = 0; i < listOfParticlesToDress.size(); ++i) {
164  if (!m_useLeptonsFromHadrons) {
165  if (!acc_origin.isAvailable(*listOfParticlesToDress[i])) {
166  ATH_MSG_WARNING("MCTruthClassifier "<<m_truthClassKey.fullKey() <<" not available, cannot apply notFromHadron veto!");
167  }
168  unsigned int result = acc_origin(*listOfParticlesToDress[i]);
169  const bool isPrompt = MCTruthPartClassifier::isPrompt(result, true);
170  if (!isPrompt) continue;
171  }
173  if(listOfParticlesToDress[i]->isTau()) {
174 
175  if( !pt_visAcc.isAvailable(*listOfParticlesToDress[i]) ||
176  !eta_visAcc.isAvailable(*listOfParticlesToDress[i]) ||
177  !phi_visAcc.isAvailable(*listOfParticlesToDress[i]) ||
178  !mvisAcc.isAvailable(*listOfParticlesToDress[i])) {
179  ATH_MSG_ERROR("Visible momentum not available for truth taus, cannot perform dressing!");
180  return StatusCode::FAILURE;
181  }
182 
183  bare_part.SetPtEtaPhiM(pt_visAcc(*listOfParticlesToDress[i]),
184  eta_visAcc(*listOfParticlesToDress[i]),
185  phi_visAcc(*listOfParticlesToDress[i]),
186  mvisAcc(*listOfParticlesToDress[i]));
187  }
188  else {
189  bare_part = listOfParticlesToDress[i]->p4();
190  }
191 
192  double dR = bare_part.DeltaR(phot->p4());
193  if (dR < dRmin) {
194  dRmin = dR;
195  idx = i;
196  }
197  }
198 
199  if(idx > -1) {
200  listOfDressedParticles[idx] += phot->p4();
201  dressedParticlesNPhot[idx]++;
202  if (!m_decorationName.empty()){
203  dressDec(*phot) = 1;
204  }
205  }
206  }
207 
208  //loop over particles and add decorators
209  //for (const auto& part : listOfDressedParticles) {
210  for (size_t i = 0; i < listOfParticlesToDress.size(); ++i) {
211  const xAOD::TruthParticle* part = listOfParticlesToDress[i];
212  xAOD::TruthParticle::FourMom_t& dressedVec = listOfDressedParticles[i];
213 
214  if(part->isTau()) {
215  decorator_pt_vis(*part) = dressedVec.Pt();
216  decorator_eta_vis(*part) = dressedVec.Eta();
217  decorator_phi_vis(*part) = dressedVec.Phi();
218  decorator_m_vis(*part) = dressedVec.M();
219  }
220  else {
221  decorator_e(*part) = dressedVec.E();
222  decorator_pt(*part) = dressedVec.Pt();
223  decorator_eta(*part) = dressedVec.Eta();
224  decorator_phi(*part) = dressedVec.Phi();
225  }
226  decorator_nphoton(*part) = dressedParticlesNPhot[i];
227  }
228  } // end of the dR matching part
229 
230  //build the anti-kt jet list
231  if (m_useAntiKt) {
232  std::vector<fastjet::PseudoJet> sorted_jets;
233  std::vector<fastjet::PseudoJet> fj_particles;
234  for (const auto& part : listOfParticlesToDress) {
235 
236  if(part->isTau()) {
237  if(!pt_visAcc.isAvailable(*part) || !eta_visAcc.isAvailable(*part)
238  || !phi_visAcc.isAvailable(*part) || !mvisAcc.isAvailable(*part)) {
239  ATH_MSG_ERROR("Visible momentum not available for truth taus, cannot perform dressing!");
240  return StatusCode::FAILURE;
241  }
242 
243  TLorentzVector tauvis;
244  tauvis.SetPtEtaPhiM(pt_visAcc(*part),
245  eta_visAcc(*part),
246  phi_visAcc(*part),
247  mvisAcc(*part));
248  fj_particles.emplace_back(tauvis.Px(), tauvis.Py(), tauvis.Pz(), tauvis.E());
249  }
250  else {
251  fj_particles.emplace_back(part->px(), part->py(), part->pz(), part->e());
252  }
253 
254  fj_particles.back().set_user_index(HepMC::uniqueID(part));
255  }
256  for (const auto& part : photonsFSRList) {
257  fj_particles.emplace_back(part->px(), part->py(), part->pz(), part->e());
258  fj_particles.back().set_user_index(HepMC::uniqueID(part));
259  }
260 
261  //run the clustering
262  fastjet::JetAlgorithm alg=fastjet::antikt_algorithm;
263  const fastjet::JetDefinition jet_def(alg, m_coneSize);
264  fastjet::ClusterSequence cseq(fj_particles, jet_def);
265  sorted_jets = sorted_by_pt(cseq.inclusive_jets(0));
266  //associate clustered jets back to bare particles
267  std::vector<int> photon_uniqueIDs(50);
268  photon_uniqueIDs.clear();
269  for (const auto& part : listOfParticlesToDress) {
270  //loop over fastjet pseudojets and associate one with this particle
271  bool found=false;
272  auto pjItr=sorted_jets.begin();
273  while(!found && pjItr!=sorted_jets.end()) {
274  std::vector<fastjet::PseudoJet> constituents = pjItr->constituents();
275  for(const auto& constit : constituents) {
276  if (HepMC::uniqueID(part)==constit.user_index()) {
277 
278  // shall we count the number of photons among the pseudojet constituents
279  // to decorate leptons with the number of dressing photons found by the anti-kt algorithm?
280 
281  if(part->isTau()) {
282  decorator_pt_vis(*part) = pjItr->pt();
283  decorator_eta_vis(*part) = pjItr->pseudorapidity();
284  decorator_phi_vis(*part) = pjItr->phi_std(); //returns phi in [-pi,pi]
285  decorator_m_vis(*part) = pjItr->m();
286  }
287  else {
288  decorator_e(*part) = pjItr->e();
289  decorator_pt(*part) = pjItr->pt();
290  decorator_eta(*part) = pjItr->pseudorapidity();
291  decorator_phi(*part) = pjItr->phi_std(); //returns phi in [-pi,pi]
292  }
293  found=true;
294  break;
295  } // Found the matching unique ID
296  } // Loop over the jet constituents
297  if (found){
298  for(const auto& constit : constituents) {
299  photon_uniqueIDs.push_back(constit.user_index());
300  } // Loop over the constituents
301  } // Found one of the key leptons in this jet
302  ++pjItr;
303  }
304  if (!found) {
305  if(part->isTau()) {
306  decorator_pt_vis(*part) = 0.;
307  decorator_eta_vis(*part) = 0.;
308  decorator_phi_vis(*part) = 0.;
309  decorator_m_vis(*part) = 0.;
310  }
311  else {
312  decorator_e(*part) = 0;
313  decorator_pt(*part) = 0;
314  decorator_eta(*part) = 0;
315  decorator_phi(*part) = 0;
316  }
317  ATH_MSG_WARNING("Bare particle not found in constituents ");
318  }
319  }
320  // Check if we wanted to decorate photons used for dressing
321  if (!m_decorationName.empty()){
322  //loop over photons, uniquely associate each to nearest bare particle
323  for (const auto& phot : photonsFSRList ) {
324  bool found = std::find(photon_uniqueIDs.begin(), photon_uniqueIDs.end(), HepMC::uniqueID(phot)) != photon_uniqueIDs.end();
325  if (found) {
326  dressDec(*phot) = 1;
327  }
328  } // End of loop over photons
329  } // End of decoration of photons used in dressing
330  } // End of anti-kT dressing
331 
332  return StatusCode::SUCCESS;
333 }
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
DerivationFramework::TruthDressingTool::initialize
StatusCode initialize()
Definition: TruthDressingTool.cxx:42
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
SGout2dot.alg
alg
Definition: SGout2dot.py:243
get_generator_info.result
result
Definition: get_generator_info.py:21
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
SG::WriteDecorHandle::isAvailable
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
DerivationFramework::TruthDressingTool::addBranches
virtual StatusCode addBranches() const
Pass the thinning service
Definition: TruthDressingTool.cxx:74
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:49
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
SG::AuxTypeRegistry::getName
std::string getName(SG::auxid_t auxid) const
Return the name of an aux data item.
Definition: AuxTypeRegistry.cxx:277
SG::ConstAccessor< unsigned int >
MCTruthClassifier.h
DerivationFramework::DecayGraphHelper::constructListOfFinalParticles
bool constructListOfFinalParticles(const xAOD::TruthParticleContainer *allParticles, std::vector< const xAOD::TruthParticle * > &selectedlist, const std::vector< int > &pdgId, bool allowFromHadron=false, bool chargedOnly=false) const
Definition: DecayGraphHelper.h:218
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
jet::ClusterSequence
fastjet::ClusterSequence ClusterSequence
Definition: ClusterSequence.h:21
lumiFormat.i
int i
Definition: lumiFormat.py:92
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
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
WriteDecorHandle.h
Handle class for adding a decoration to an object.
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TruthDressingTool.h
DerivationFramework::TruthDressingTool::TruthDressingTool
TruthDressingTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: TruthDressingTool.cxx:29
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
isTau
bool isTau(const T &p)
Definition: AtlasPID.h:148
MagicNumbers.h
DerivationFramework::DecayGraphHelper
Definition: DecayGraphHelper.h:26
xAOD::JetAlgorithmType::antikt_algorithm
@ antikt_algorithm
Definition: JetContainerInfo.h:33
SG::ReadHandle::ptr
const_pointer_type ptr()
Dereference the pointer.
DerivationFramework::TruthDressingTool::~TruthDressingTool
~TruthDressingTool()
Definition: TruthDressingTool.cxx:38
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
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
ReadDecorHandle.h
Handle class for reading a decoration on an object.
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
AthAlgTool
Definition: AthAlgTool.h:26
TruthEventContainer.h
xAOD::TruthParticle_v1::FourMom_t
IParticle::FourMom_t FourMom_t
Definition of the 4-momentum type.
Definition: TruthParticle_v1.h:151