ATLAS Offline Software
GenFilterTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Class header file
6 #include "GenFilterTool.h"
7 
8 // EDM includes
11 
12 // Tool handle interface
14 
15 namespace DerivationFramework {
16 
17  using namespace MCTruthPartClassifier;
18 
19  static const SG::AuxElement::Decorator<float> dec_genFiltHT("GenFiltHT");
20  static const SG::AuxElement::Decorator<float> dec_genFiltHTinclNu("GenFiltHTinclNu");
21  static const SG::AuxElement::Decorator<float> dec_genFiltMET("GenFiltMET");
22  static const SG::AuxElement::Decorator<float> dec_genFiltPTZ("GenFiltPTZ");
23  static const SG::AuxElement::Decorator<float> dec_genFiltFatJ("GenFiltFatJ");
24  static const SG::AuxElement::ConstAccessor<unsigned int> acc_PartOrigin("classifierParticleOrigin");
25 
26  static bool isFromWZTau(const xAOD::TruthParticle* tp) {
27  ParticleOrigin orig = static_cast<ParticleOrigin>(acc_PartOrigin(*tp));
28 
29  switch(orig) {
33  return true;
34  default:
35  return false;
36  }
37  return false;
38  }
39 
40 
41  GenFilterTool::GenFilterTool(const std::string& t, const std::string& n, const IInterface* p)
42  : AthAlgTool(t,n,p) {
43 
44  declareInterface<DerivationFramework::IAugmentationTool>(this);
45 
46  }
47 
48 
50 
52  {
53  ParticleOrigin orig = m_classif->particleTruthClassifier( tp ).second;
54  ATH_MSG_VERBOSE("Particle has origin " << orig);
55 
56  switch(orig) {
57  case NonDefined:
58  case PhotonConv:
59  case DalitzDec:
60  case ElMagProc:
61  case Mu:
62  case LightMeson:
63  case StrangeMeson:
64  case CharmedMeson:
65  case BottomMeson:
66  case CCbarMeson:
67  case JPsi:
68  case BBbarMeson:
69  case LightBaryon:
70  case StrangeBaryon:
71  case CharmedBaryon:
72  case BottomBaryon:
73  case PionDecay:
74  case KaonDecay:
75  return false;
76  default:
77  break;
78  }
79  return true;
80  }
84  ATH_CHECK(m_truthJetsKey.initialize());
85  ATH_CHECK(m_truthFatJetsKey.initialize());
86  for (const SG::AuxElement::Decorator<float>& dec : {
87  dec_genFiltHT, dec_genFiltHTinclNu, dec_genFiltMET, dec_genFiltPTZ, dec_genFiltFatJ
88  }) {
89  m_decorKeys.emplace_back(m_eventInfoKey.key() + "." + SG::AuxTypeRegistry::instance().getName(dec.auxid()));
90  }
91  ATH_CHECK(m_decorKeys.initialize());
92  m_mcReadDecor = m_mcKey.key() + "." + SG::AuxTypeRegistry::instance().getName(acc_PartOrigin.auxid());
94  return StatusCode::SUCCESS;
95  }
97  ATH_MSG_VERBOSE("GenFilterTool::addBranches()");
98  const EventContext& ctx = Gaudi::Hive::currentContext();
100  if (!eventInfo.isValid()) {
101  ATH_MSG_ERROR("could not retrieve event info " <<m_eventInfoKey.fullKey());
102  return StatusCode::FAILURE;
103  }
104 
105 
106  float genFiltHT{0.f}, genFiltHTinclNu{0.f}, genFiltMET{0.f}, genFiltPTZ{0.f}, genFiltFatJ{0.f};
107  ATH_CHECK( getGenFiltVars(ctx, genFiltHT, genFiltHTinclNu, genFiltMET, genFiltPTZ, genFiltFatJ) );
108 
109  ATH_MSG_DEBUG("Computed generator filter quantities: HT " << genFiltHT/1e3 << ", HTinclNu " << genFiltHTinclNu/1e3 << ", MET " << genFiltMET/1e3 << ", PTZ " << genFiltPTZ/1e3 << ", FatJ " << genFiltFatJ/1e3 );
110 
111  dec_genFiltHT(*eventInfo) = genFiltHT;
112  dec_genFiltHTinclNu(*eventInfo) = genFiltHTinclNu;
113  dec_genFiltMET(*eventInfo) = genFiltMET;
114  dec_genFiltPTZ(*eventInfo) = genFiltPTZ;
115  dec_genFiltFatJ(*eventInfo) = genFiltFatJ;
116 
117  return StatusCode::SUCCESS;
118  }
119 
120  StatusCode GenFilterTool::getGenFiltVars(const EventContext& ctx, float& genFiltHT, float& genFiltHTinclNu, float& genFiltMET, float& genFiltPTZ, float& genFiltFatJ) const {
121  // Get jet container out
122 
124  if (!tpc.isValid()) {
125  ATH_MSG_ERROR("WARNING could not retrieve TruthParticleContainer " <<m_mcKey.fullKey());
126  return StatusCode::FAILURE;
127  }
128 
130  if (!truthjets.isValid()){
131  ATH_MSG_ERROR( "No xAOD::JetContainer found in StoreGate with key " << m_truthJetsKey );
132  return StatusCode::FAILURE;
133  }
134 
135  // Get HT
136  genFiltHT = 0.;
137  genFiltHTinclNu = 0.; // this HT definition includes neutrinos from W/Z/tau
138  for (const auto *const tj : *truthjets) {
139  if ( tj->pt()>m_MinJetPt && std::abs(tj->eta())<m_MaxJetEta ) {
140  ATH_MSG_VERBOSE("Adding truth jet with pt " << tj->pt()
141  << ", eta " << tj->eta()
142  << ", phi " << tj->phi()
143  << ", nconst = " << tj->numConstituents());
144  genFiltHT += tj->pt();
145  genFiltHTinclNu += tj->pt();
146  }
147  }
148 
149  // Get MET and add leptons to HT
150  float MEx(0.), MEy(0.);
151  for (const auto *const tp : *tpc){
152  int pdgid = tp->pdgId();
153  if (HepMC::is_simulation_particle(tp)) continue; // Particle is from G4
154  if (MC::isZeroEnergyPhoton(tp)) continue; // Work around for an old generator bug
155  if ( !MC::isStable(tp)) continue; // Stable!
156 
157  if ((std::abs(pdgid)==11 || std::abs(pdgid)==13) && tp->pt()>m_MinLepPt && std::abs(tp->eta())<m_MaxLepEta) {
158  if( isPrompt(tp) ) {
159  ATH_MSG_VERBOSE("Adding prompt lepton with pt " << tp->pt()
160  << ", eta " << tp->eta()
161  << ", phi " << tp->phi()
162  << ", status " << tp->status()
163  << ", pdgId " << pdgid);
164  genFiltHT += tp->pt();
165  genFiltHTinclNu += tp->pt();
166  }
167  }
168 
169  // include neutrinos from W/Z/Tau in one of the HT definitions
170  // this corresponds to a subset of HT-sliced samples where the HT filter
171  // was configured to include these particles
172  if (tp->isNeutrino() && isFromWZTau(tp)) {
173  ATH_MSG_VERBOSE("Adding neutrino from W/Z/Tau with pt " << tp->pt()
174  << ", eta " << tp->eta()
175  << ", phi " << tp->phi()
176  << ", status " << tp->status()
177  << ", pdgId " << pdgid);
178  genFiltHTinclNu += tp->pt();
179  }
180 
182  ATH_MSG_VERBOSE("Found prompt nonInteracting particle with pt " << tp->pt()
183  << ", eta " << tp->eta()
184  << ", phi " << tp->phi()
185  << ", status " << tp->status()
186  << ", pdgId " << pdgid);
187  MEx += tp->px();
188  MEy += tp->py();
189  }
190  }
191  genFiltMET = sqrt(MEx*MEx+MEy*MEy);
192 
193  // Get PTZ
194  float PtZ(.0);
195  float MinPt_PTZ(5000.), MaxEta_PTZ(5.0), MinMass_PTZ(20000.), MaxMass_PTZ(14000000.);
196  bool AllowElecMu_PTZ = false;
197  bool AllowSameCharge_PTZ = false;
198  for (const xAOD::TruthParticle* pitr1 : *tpc){
199  int pdgId1 = pitr1->pdgId();
200  if (HepMC::is_simulation_particle(pitr1)) continue;
201  if (!MC::isStable(pitr1)) continue;
202  // Pick electrons or muons with Pt > MinPt_PTZ and |eta| < m_maxEta
203  if (std::abs(pdgId1) == 11 || std::abs(pdgId1) == 13) {
204  if (pitr1->pt() >= MinPt_PTZ && std::abs(pitr1->eta()) <= MaxEta_PTZ){
205  for (const xAOD::TruthParticle* pitr2 : *tpc){
206  if (pitr2==pitr1) continue;
207  if (HepMC::is_simulation_particle(pitr2)) continue;
208  if (!MC::isStable(pitr2)) continue;
209  int pdgId2 = pitr2->pdgId();
210  // Pick electrons or muons with Pt > MinPt_PTZ and |eta| < MaxEta_PTZ
211  // If AllowSameCharge_PTZ is not true only pick those with opposite charge to the first particle
212  // If AllowElecMu_PTZ is true allow also Z -> emu compinations (with charge requirements as above)
213  if ((AllowSameCharge_PTZ && (std::abs(pdgId2) == std::abs(pdgId1) || (AllowElecMu_PTZ && (std::abs(pdgId2) == 11 || std::abs(pdgId2) == 13) ) ) ) ||
214  (!AllowSameCharge_PTZ && (pdgId2 == -1*pdgId1 || (AllowElecMu_PTZ && (pdgId2 == (pdgId1 < 0 ? 1 : -1) * 11 || (pdgId1 < 0 ? 1 : -1) * pdgId2 == 13) ) ) ) ) {
215  if (pitr2->pt() >= MinPt_PTZ && std::abs(pitr2->eta()) <= MaxEta_PTZ){
216  double invMass = (pitr1->p4()+pitr2->p4()).M();
217  double dilepPt = (pitr1->p4()+pitr2->p4()).Pt();
218  // Only consider pair that fall in the mass window
219  if (MinMass_PTZ < invMass && invMass < MaxMass_PTZ) {
220  if (dilepPt > PtZ) PtZ = dilepPt;
221  }
222  }
223  }
224  }
225  }
226  }
227  }
228  genFiltPTZ = PtZ;
229 
230  //Get FatJ
231  // Get correct jet container
233  if ( !truthjets10.isValid()){
234  ATH_MSG_ERROR( "No xAOD::JetContainer found in StoreGate with key "<<m_truthFatJetsKey.fullKey() );
235  return StatusCode::FAILURE;
236  }
237  genFiltFatJ=0.;
238  for (const auto *const j : *truthjets10) {
239  if (j->pt()>genFiltFatJ) genFiltFatJ=j->pt();
240  }
241 
242 
243  return StatusCode::SUCCESS;
244  }
245 
246 
247 }
DerivationFramework::GenFilterTool::GenFilterTool
GenFilterTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: GenFilterTool.cxx:41
TauLep
@ TauLep
Definition: TruthClasses.h:63
StrangeMeson
@ StrangeMeson
Definition: TruthClasses.h:80
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
P4Helpers::invMass
double invMass(const I4Momentum &pA, const I4Momentum &pB)
invariant mass from two I4momentum references
Definition: P4Helpers.h:239
Mu
@ Mu
Definition: TruthClasses.h:62
BBbarMeson
@ BBbarMeson
Definition: TruthClasses.h:85
BottomMeson
@ BottomMeson
Definition: TruthClasses.h:82
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:49
MC::isSpecialNonInteracting
bool isSpecialNonInteracting(const T &p)
Definition: HepMCHelpers.h:57
PionDecay
@ PionDecay
Definition: TruthClasses.h:90
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
ElMagProc
@ ElMagProc
Definition: TruthClasses.h:61
ParticleTest.tp
tp
Definition: ParticleTest.py:25
PhotonConv
@ PhotonConv
Definition: TruthClasses.h:59
DerivationFramework::GenFilterTool::m_truthFatJetsKey
SG::ReadHandleKey< xAOD::JetContainer > m_truthFatJetsKey
Definition: GenFilterTool.h:51
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
ZBoson
@ ZBoson
Definition: TruthClasses.h:67
DerivationFramework::GenFilterTool::m_mcReadDecor
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_mcReadDecor
Definition: GenFilterTool.h:53
DerivationFramework::GenFilterTool::m_MaxJetEta
Gaudi::Property< float > m_MaxJetEta
Max eta for the truth jets.
Definition: GenFilterTool.h:58
GenFilterTool.h
WBoson
@ WBoson
Definition: TruthClasses.h:66
DerivationFramework::GenFilterTool::m_MinJetPt
Gaudi::Property< float > m_MinJetPt
Min pT for the truth jets.
Definition: GenFilterTool.h:57
CCbarMeson
@ CCbarMeson
Definition: TruthClasses.h:83
NonDefined
@ NonDefined
Definition: TruthClasses.h:52
DerivationFramework::GenFilterTool::m_decorKeys
SG::WriteDecorHandleKeyArray< xAOD::EventInfo > m_decorKeys
Definition: GenFilterTool.h:56
DerivationFramework::GenFilterTool::m_classif
PublicToolHandle< IMCTruthClassifier > m_classif
Definition: GenFilterTool.h:62
CharmedBaryon
@ CharmedBaryon
Definition: TruthClasses.h:88
IMCTruthClassifier.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DerivationFramework::GenFilterTool::~GenFilterTool
~GenFilterTool()
CheckAppliedSFs.e3
e3
Definition: CheckAppliedSFs.py:264
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:299
SG::ReadDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
DerivationFramework::GenFilterTool::getGenFiltVars
StatusCode getGenFiltVars(const EventContext &ctx, float &genFiltHT, float &genFiltHTinclNu, float &genFiltMET, float &genFiltPTZ, float &genFiltFatJ) const
Definition: GenFilterTool.cxx:120
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ParticleOrigin
ParticleOrigin
Definition: TruthClasses.h:51
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
StrangeBaryon
@ StrangeBaryon
Definition: TruthClasses.h:87
LightMeson
@ LightMeson
Definition: TruthClasses.h:79
DerivationFramework::GenFilterTool::m_MaxLepEta
Gaudi::Property< float > m_MaxLepEta
Max eta for the truth leptons.
Definition: GenFilterTool.h:60
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
JPsi
@ JPsi
Definition: TruthClasses.h:84
CharmedMeson
@ CharmedMeson
Definition: TruthClasses.h:81
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DerivationFramework::GenFilterTool::m_truthJetsKey
SG::ReadHandleKey< xAOD::JetContainer > m_truthJetsKey
Definition: GenFilterTool.h:50
DerivationFramework::GenFilterTool::isPrompt
bool isPrompt(const xAOD::TruthParticle *tp) const
Definition: GenFilterTool.cxx:51
MagicNumbers.h
BottomBaryon
@ BottomBaryon
Definition: TruthClasses.h:89
LightBaryon
@ LightBaryon
Definition: TruthClasses.h:86
DerivationFramework::GenFilterTool::m_mcKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_mcKey
Definition: GenFilterTool.h:49
DerivationFramework::GenFilterTool::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: GenFilterTool.h:48
MC::isStable
bool isStable(const T &p)
Definition: HepMCHelpers.h:30
Prompt::Def::Pt
@ Pt
Definition: VarHolder.h:76
MCTruthPartClassifier
Definition: TruthClassifiers.h:12
DerivationFramework::GenFilterTool::m_MinLepPt
Gaudi::Property< float > m_MinLepPt
Min pT for the truth leptons.
Definition: GenFilterTool.h:59
DalitzDec
@ DalitzDec
Definition: TruthClasses.h:60
DerivationFramework::GenFilterTool::addBranches
virtual StatusCode addBranches() const override final
Pass the thinning service
Definition: GenFilterTool.cxx:96
MC::isZeroEnergyPhoton
bool isZeroEnergyPhoton(const T &p)
Identify a photon with zero energy. Probably a workaround for a generator bug.
Definition: HepMCHelpers.h:53
AthAlgTool
Definition: AthAlgTool.h:26
KaonDecay
@ KaonDecay
Definition: TruthClasses.h:91
DerivationFramework::GenFilterTool::initialize
virtual StatusCode initialize() override final
Definition: GenFilterTool.cxx:81
HepMCHelpers.h