ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticleID/McParticleTools/src/TruthIsolationTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6// STL includes
7#include <cmath>
8#include <stdexcept>
9#include <sstream>
10#include <fstream>
11
12// CLHEP/HepMC includes
13#include "AtlasHepMC/GenEvent.h"
18#include "CLHEP/Units/SystemOfUnits.h"
19#include "CLHEP/Vector/LorentzVector.h"
20
21// McParticle includes
23
24// McParticleTools includes
25#include "TruthIsolationTool.h"
26
27using CLHEP::GeV;
28
29using CLHEP::HepLorentzVector;
30namespace {
31 inline
32 HepLorentzVector svToLv( const HepMC::FourVector& v )
33 { return { v.x(), v.y(), v.z(), v.t() }; }
34}
35
36using GenParticles_t = std::list<HepMC::ConstGenParticlePtr>;
37
39 const std::string& name,
40 const IInterface* parent ) :
41 AthAlgTool ( type, name, parent )
42{
43 //
44 // Property declaration
45 //
46
47 declareProperty( "ptGammaMin",
48 m_ptGamMin = 0.5*GeV,
49 "Minimum transverse energy of gammas to be taken into "
50 "account into the isolation computation." );
51
52 declareProperty( "TruthEtIsolationsPrefix",
53 m_prefix = "TruthEtIsol",
54 "Prefix for the TruthEtIsolations container. This is the "
55 "string which will be prepended to the key of the "
56 "McEventCollection to build the (StoreGate) output "
57 "location for the TruthEtIsolations.\nie: \"GEN_EVENT\" "
58 "--> \"<prefix>_GEN_EVENT\"" );
59 m_prefix.declareUpdateHandler
61 this );
62
63 declareProperty( "McEventsOutput",
64 m_mcEventsOutputName = "GEN_AOD",
65 "Name of the McEventCollection we should attach "
66 "isolations to" );
67 m_mcEventsOutputName.declareUpdateHandler
69 this );
70
71 declareInterface<ITruthIsolationTool>(this);
72}
73
75= default;
76
78{
79 // retrieve StoreGate
80 if ( !evtStore().retrieve().isSuccess() ) {
81 ATH_MSG_ERROR("Could not get a handle on StoreGateSvc !!");
82 return StatusCode::FAILURE;
83 }
84
85 ATH_MSG_INFO(" McEventsOutput: [" << m_mcEventsOutputName.value() << "]");
86
87 return StatusCode::SUCCESS;
88}
89
90const std::string&
91TruthIsolationTool::etIsolationsName( const std::string& mcEvtName ) const
92{
93 static const std::string s_emptyString = "";
94 const EtIsolMap_t::const_iterator i = m_etIsolMap.find(mcEvtName);
95 if ( i != m_etIsolMap.end() ) {
96 return i->second;
97 }
98
99 return s_emptyString;
100}
101
102StatusCode
103TruthIsolationTool::buildEtIsolations( const std::string& mcEvtName,
105{
106 // retrieve collection
107 const McEventCollection* mcEvts = nullptr;
108 if ( !evtStore()->retrieve( mcEvts, mcEvtName ).isSuccess() ) {
109 ATH_MSG_WARNING("Could not retrieve a McEventCollection at ["
110 << mcEvtName << "] !!" << endmsg
111 << "No Et-isolations will be computed !");
112 return StatusCode::RECOVERABLE;
113 }
114
115 // the name of the output (filtered) McEventCollection we want to attach
116 // the Et-isolations to
117 const std::string& outMcEvtName = m_mcEventsOutputName.value();
118
119 // create the container of TruthEtIsolations
120 std::ostringstream truthEtIsolName;
121 truthEtIsolName << m_prefix.value()
123 ? "Charged"
124 : "")
125 << "_" << outMcEvtName;
126
128 if ( !evtStore()->record( etIsolations,
129 truthEtIsolName.str() ).isSuccess() ) {
130 delete etIsolations;
131 etIsolations = nullptr;
132 ATH_MSG_WARNING("Could not record a TruthEtIsolations container at ["
133 << truthEtIsolName.str() << "] !!");
134 return StatusCode::RECOVERABLE;
135 }
136 if ( !evtStore()->setConst( etIsolations ).isSuccess() ) {
137 ATH_MSG_WARNING("Could not setConst the TruthEtIsolations container at ["
138 << truthEtIsolName.str() << "] !!");
139 }
140
141 // update our registry of EtIsol StoreGate locations
142 m_etIsolMap[outMcEvtName] = truthEtIsolName.str();
143
144 bool allGood = true;
145 for ( std::size_t iMc = 0, iMax = mcEvts->size(); iMc != iMax; ++iMc ) {
146 TruthEtIsolations * etIsols = new TruthEtIsolations( outMcEvtName, iMc );
147 etIsolations->push_back( etIsols );
148 if ( !buildEtIsolations( mcEvtName, (*mcEvts)[iMc], iMc,
149 *etIsols, partSel ).isSuccess() ) {
150 msg(MSG::WARNING)
151 << "Problem encountered while computing Et-isolations for idx=["
152 << iMc << "] of McEventCollection [" << mcEvtName << "] !!"
153 << endmsg;
154 allGood = false;
155 }
156 }
157 return allGood ? StatusCode::SUCCESS : StatusCode::RECOVERABLE;
158}
159
160StatusCode
161TruthIsolationTool::buildEtIsolations( const std::string& mcEvtName,
162 const HepMC::GenEvent* genEvt,
163 const std::size_t genIdx,
164 TruthEtIsolations& etIsols,
166{
167 if ( nullptr == genEvt ) {
168 msg(MSG::WARNING)
169 << "Null pointer to GenEvent (idx = [" << genIdx << "] from "
170 << "McEventCollection [" << mcEvtName << "]) !!"
171 << endmsg;
172 return StatusCode::RECOVERABLE;
173 }
174
175 // create a reduced list of particles
176 GenParticles_t particles;
177 for ( const auto& i: *genEvt) {
178 if ( MC::isGenStable(i) && MC::isSimInteracting(i) ) {
179 particles.push_back( i );
180 }
181 }
182
183 for ( const auto& i: *genEvt) {
184 const HepMC::FourVector hlv = i->momentum();
185 const int ida = std::abs(i->pdg_id());
186 const double pt = hlv.perp();
187
188 // Compute isolation only for photon, electron, muon or tau.
189 // Not for documentation particle
190 const bool doComputeIso = ( ( ida == 22 && pt > m_ptGamMin ) ||
191 ida == 11 || ida == 13 || ida == 15 ) &&
193 if ( doComputeIso ) {
194 computeIso( particles, i, etIsols, partSel );
195 }
196 }
197
198 return StatusCode::SUCCESS;
199}
200
201void
203 const HepMC::ConstGenParticlePtr& part,
204 TruthEtIsolations& etIsolations,
206{
207 const HepLorentzVector hlv = ::svToLv(part->momentum());
208 const int ida = std::abs(part->pdg_id());
209
210 McAod::EtIsolations etIsol = { {0.*GeV, 0.*GeV, 0.*GeV, 0.*GeV,
211 0.*GeV, 0.*GeV, 0.*GeV, 0.*GeV} };
212 McAod::EtIsolations pxi = etIsol;
213 McAod::EtIsolations pyi = etIsol;
214
215 int barcodepart = HepMC::barcode(part);
216 for (const auto & particle : particles) {
217 if ( HepMC::barcode(particle) == barcodepart ) {
218 continue;
219 }
220 if( partSel == ITruthIsolationTool::UseChargedOnly ) {
221 double particleCharge = MC::charge(particle->pdg_id());
222 if( std::abs(particleCharge)<1.e-2 )
223 continue;
224 }
225 const HepLorentzVector itrHlv = ::svToLv(particle->momentum());
226 const double r = hlv.deltaR(itrHlv);
227 for ( std::size_t iCone = 0;
229 ++iCone ) {
231 pxi[iCone] += itrHlv.px();
232 pyi[iCone] += itrHlv.py();
233 }
234 }
235 }
236
237 //
238 // Correction for tau (as was done in the old tool for the time being)
239 double pxv = 0.*GeV;
240 double pyv = 0.*GeV;
241 auto decVtx = part->end_vertex();
242 if (ida == 15 && decVtx) {
243 for (const auto& child: *decVtx) {
244 if ( MC::isSimInteracting(child) ) {
245 if( partSel == ITruthIsolationTool::UseChargedOnly ) {
246 double particleCharge = MC::charge(child->pdg_id());
247 if( std::abs(particleCharge)<1.e-2 )
248 continue;
249 }
250 const HepMC::FourVector childHlv = child->momentum();
251 pxv += childHlv.px();
252 pyv += childHlv.py();
253 }
254 }
255 }
256
257 for ( std::size_t i = 0;
258 i != static_cast<std::size_t>(TruthParticleParameters::NbrOfCones);
259 ++i ) {
260 pxi[i] -= pxv;
261 pyi[i] -= pyv;
262 etIsol[i] = std::sqrt(pxi[i]*pxi[i]+pyi[i]*pyi[i]);
263 }
264
265 etIsolations.setEtIsol( part, etIsol );
266}
267
268void
269TruthIsolationTool::setupTruthEtIsolationsPrefix( Gaudi::Details::PropertyBase& /*truthEtIsolationsPrefix*/ )
270{
271 // no-op for now
272}
273
274void
275TruthIsolationTool::setupMcEventsOutput( Gaudi::Details::PropertyBase& /*mcEventsOutputName*/ )
276{
277 // no-op for now
278}
279
280StatusCode
281TruthIsolationTool::registerAlias( const std::string& originalMcEvtColl,
282 const std::string& aliasMcEvtColl )
283{
284 m_etIsolMap[aliasMcEvtColl] = m_etIsolMap[originalMcEvtColl];
285 return StatusCode::SUCCESS;
286}
#define endmsg
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
ATLAS-specific HepMC functions.
std::list< HepMC::ConstGenParticlePtr > GenParticles_t
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
MsgStream & msg() const
value_type push_back(value_type pElem)
Add an element to the end of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
ParticleSelect
enumerator to decide which strategy to apply in the selection of particles during the Et-isolation co...
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
container which holds isolation informations for a given HepMC::GenParticle (labelled by barcode) for...
void setEtIsol(const HepMC::ConstGenParticlePtr &genParticle, const TruthParticleParameters::ConeSize coneIdx, const double etIsol)
Set the transverse energy isolation of a HepMC::GenParticle for a given Cone size.
void setupMcEventsOutput(Gaudi::Details::PropertyBase &mcEventsOutputName)
Callback method to ensure consistency of output McEventCollection key.
StringProperty m_mcEventsOutputName
Name of the McEventCollection we should attach isolations to.
EtIsolMap_t m_etIsolMap
A map of McEventCollection StoreGate locations to the according StoreGate location of TruthEtIsolatio...
StatusCode registerAlias(const std::string &originalMcEvtColl, const std::string &aliasMcEvtColl)
Make an alias in the map of isolation energies.
StatusCode buildEtIsolations(const std::string &mcEvtName, ITruthIsolationTool::ParticleSelect sel)
Computes the isolation energies for each of the HepMC::GenEvent contained into the McEventCollection.
void computeIso(const std::list< HepMC::ConstGenParticlePtr > &parts, const HepMC::ConstGenParticlePtr &p, TruthEtIsolations &etIsolations, ITruthIsolationTool::ParticleSelect sel)
Computes and stores the list of transverse isolation energies for various cone sizes into the TruthEt...
void setupTruthEtIsolationsPrefix(Gaudi::Details::PropertyBase &truthEtIsolationsPrefix)
Callback method to ensure consistency of the TruthEtIsolations prefix key.
TruthIsolationTool(const std::string &type, const std::string &name, const IInterface *parent)
const std::string & etIsolationsName(const std::string &mcEvtName) const
Return the name of the TruthEtIsolations container (ie: its StoreGate location) given the StoreGate l...
StringProperty m_prefix
Prefix for the TruthEtIsolations container.
DoubleProperty m_ptGamMin
Minimum transverse energy of gammas to be taken into account into the isolation computation.
virtual ~TruthIsolationTool()
int r
Definition globals.cxx:22
int barcode(const T *p)
Definition Barcode.h:15
HepMC3::FourVector FourVector
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
Definition GenParticle.h:20
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
bool isSimInteracting(const T &p)
Identify if the particle could interact with the detector during the simulation, e....
double charge(const T &p)
bool isGenStable(const T &p)
Determine if the particle is stable at the generator (not det-sim) level,.
bool isPhysical(const T &p)
Identify if the particle is physical, i.e. is stable or decayed.
std::array< double, TruthParticleParameters::NbrOfCones > EtIsolations
An array of doubles of fixed size to modelize the Et isolations for different values of isolation rad...
double coneCut(const TruthParticleParameters::ConeSize idx)
The actual definition of delta R cuts for each cone.
ConeSize
Enum for Cone size indexes (for isolation).