ATLAS Offline Software
xAODHTFilter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Header for this module
8 
9 // Framework Related Headers
10 #include "GaudiKernel/MsgStream.h"
11 #include "GaudiKernel/SystemOfUnits.h"
12 
13 // EDM includes
15 
16 // Used for retrieving the collection
17 #include "xAODJet/JetContainer.h"
19 #include "xAODTruth/TruthVertex.h"
20 #include "StoreGate/StoreGateSvc.h"
22 
23 // Other classes used by this class
25 #include "AtlasHepMC/GenEvent.h"
26 // #include "GeneratorObjects/McEventCollection.h"
28 
32 // Tool handle interface
34 
35 //--------------------------------------------------------------------------
36 
37 xAODHTFilter::xAODHTFilter(const std::string &name, ISvcLocator *pSvcLocator)
38  : GenFilter(name, pSvcLocator), m_total(0), m_passed(0), m_ptfailed(0)
39  , m_classif("MCTruthClassifier/DFCommonTruthClassifier")
40 {
41  declareProperty("MinJetPt", m_MinJetPt = 0 * Gaudi::Units::GeV);
42  declareProperty("MaxJetEta", m_MaxJetEta = 10.0);
43  declareProperty("TruthJetContainer", m_TruthJetContainerName = "AntiKt4TruthWZJets");
44  declareProperty("MinHT", m_MinHT = 20. * Gaudi::Units::GeV);
45  declareProperty("MaxHT", m_MaxHT = 14000. * Gaudi::Units::GeV);
46  declareProperty("UseNeutrinosFromWZTau", m_UseNu = false, "Include neutrinos from W/Z/tau decays in the calculation of HT");
47  declareProperty("UseLeptonsFromWZTau", m_UseLep = false, "Include e/mu from W/Z/tau decays in the HT");
48  declareProperty("MinLeptonPt", m_MinLepPt = 0 * Gaudi::Units::GeV);
49  declareProperty("MaxLeptonEta", m_MaxLepEta = 10.0);
50  declareProperty("EventInfoName",m_eventInfoName="EventInfo");
51 }
52 
53 //--------------------------------------------------------------------------
54 
56 {
57 }
58 
59 //---------------------------------------------------------------------------
60 
62 {
67  if (m_MaxHT < 0)
68  m_MaxHT = 9e9;
69 
70  ATH_MSG_INFO("Configured with " << m_MinJetPt << "<p_T GeV and abs(eta)<" << m_MaxJetEta << " for jets in " << m_TruthJetContainerName);
71  ATH_MSG_INFO("Will require H_T in range " << m_MinHT << " < H_T < " << m_MaxHT);
72  if (m_UseNu)
73  ATH_MSG_INFO(" including neutrinos");
74  if (m_UseLep)
75  ATH_MSG_INFO(" including W/Z/tau leptons in range " << m_MinLepPt << "<p_T GeV and abs(eta)<" << m_MaxLepEta);
76 
78  ATH_CHECK(m_classif.retrieve());
79 
80  return StatusCode::SUCCESS;
81 }
82 
83 //---------------------------------------------------------------------------
84 
86 {
87  ATH_MSG_INFO("Total efficiency: " << 100. * double(m_passed) / double(m_total) << "% ("
88  << 100. * double(m_ptfailed) / double(m_total) << "% failed p_T cuts)");
89  return StatusCode::SUCCESS;
90 }
91 
92 //---------------------------------------------------------------------------
93 
95 {
96  m_total++; // Book keeping
97 
98  // Get jet container out
99  const xAOD::JetContainer *truthjetTES = 0;
100  if (!evtStore()->contains<xAOD::JetContainer>(m_TruthJetContainerName) ||
101  evtStore()->retrieve(truthjetTES, m_TruthJetContainerName).isFailure() || !truthjetTES)
102  {
103  ATH_MSG_INFO("No xAOD::JetContainer found in StoreGate with key " << m_TruthJetContainerName);
104 #ifdef HEPMC3
105  setFilterPassed(m_MinHT < 1. || keepAll());
106 #else
107  setFilterPassed(m_MinHT < 1.);
108 #endif
109  return StatusCode::SUCCESS;
110  }
111 
112  // Get HT
113  double HT = -1;
114  for (xAOD::JetContainer::const_iterator it_truth = (*truthjetTES).begin(); it_truth != (*truthjetTES).end(); ++it_truth)
115  {
116  if (!(*it_truth))
117  continue;
118  if ((*it_truth)->pt() > m_MinJetPt * Gaudi::Units::GeV && std::abs((*it_truth)->eta()) < m_MaxJetEta)
119  {
120  ATH_MSG_VERBOSE("Adding truth jet with pt " << (*it_truth)->pt()
121  << ", eta " << (*it_truth)->eta()
122  << ", phi " << (*it_truth)->phi()
123  << ", nconst = " << (*it_truth)->numConstituents());
124  HT += (*it_truth)->pt();
125  }
126  }
127 
128  // If we are asked to include neutrinos or leptons...
129  if (m_UseLep || m_UseNu)
130  {
131 
132 // Retrieve TruthGen container from xAOD Gen slimmer, contains all particles witout barcode_zero and
133 // duplicated barcode ones
134  const xAOD::TruthParticleContainer* xTruthParticleContainer;
135  if (evtStore()->retrieve(xTruthParticleContainer, "TruthGen").isFailure()) {
136  ATH_MSG_ERROR("No TruthParticle collection with name " << "TruthGen" << " found in StoreGate!");
137  return StatusCode::FAILURE;
138  }
139 
140  std::vector<const xAOD::TruthParticle *> WZleptons;
141  WZleptons.reserve(10);
142 
143  // Loop over full TruthParticle container
144  unsigned int nPart = xTruthParticleContainer->size();
145  for (unsigned int iPart = 0; iPart < nPart; ++iPart) {
146  const xAOD::TruthParticle* theParticle = (*xTruthParticleContainer)[iPart];
147  if (!theParticle)
148  continue;
149  int pdgid = theParticle->pdgId();
150 
151  if (m_UseNu && MC::isNeutrino(pdgid) && (theParticle->isGenStable()))
152  {
153  if (Common::prompt(theParticle,m_classif))
154  {
155  HT += theParticle->pt();
156  }
157  }
158 
159  // pick muons and electrons specifically -- isLepton selects both charged leptons and neutrinos
160  if ( m_UseLep && (std::abs(pdgid) == 11 || std::abs(pdgid) == 13) && theParticle->isGenStable() && (theParticle)->pt() > m_MinLepPt * Gaudi::Units::GeV && std::abs(theParticle->eta()) < m_MaxLepEta)
161  {
162  if (Common::prompt(theParticle,m_classif))
163  {
164  HT += theParticle->pt();
165  }
166  }
167  } // End loop over particles
168  }
169 
170  HT /= Gaudi::Units::GeV; // Make sure we're in GeV
171  ATH_MSG_DEBUG("HT: " << HT);
172 
173 #ifdef HEPMC3
174  // fill the HT value
175  // Event passed. Will add HT to xAOD::EventInfo
176  // Get MC event collection for setting weight
177  const McEventCollection* mecc = 0;
178  if ( evtStore()->retrieve( mecc ).isFailure() || !mecc ){
179  setFilterPassed(false);
180  ATH_MSG_ERROR("Could not retrieve MC Event Collection - might not work");
181  return StatusCode::SUCCESS;
182  }
183 
184  McEventCollection* mec = const_cast<McEventCollection*> (&(*mecc));
185  for (unsigned int i = 0; i < mec->size(); ++i) {
186  if (!(*mec)[i]) continue;
187 
188  (*mec)[i]->add_attribute("filterHT", std::make_shared<HepMC3::DoubleAttribute>(HT));
189  }
190 
191  if ((HT < m_MinHT || HT >= m_MaxHT) && (!keepAll()))
192 #else
193  if ((HT < m_MinHT || HT >= m_MaxHT) )
194 #endif
195  {
196  ATH_MSG_DEBUG("Failed filter on HT: " << HT << " is not between " << m_MinHT << " and " << m_MaxHT);
197  setFilterPassed(false);
198  }
199  else
200  {
201  // Made it to the end - success!
202  m_passed++;
203  setFilterPassed(true);
204  }
205  return StatusCode::SUCCESS;
206 }
207 
208 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAODHTFilter::m_TruthJetContainerName
std::string m_TruthJetContainerName
Name of the truth jet container.
Definition: xAODHTFilter.h:45
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
GenEvent.h
xAODHTFilter::m_MinLepPt
double m_MinLepPt
Min pT for the truth jets.
Definition: xAODHTFilter.h:40
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAODHTFilter::m_passed
long m_passed
Number of events passing all cuts.
Definition: xAODHTFilter.h:49
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
xAODHTFilter::m_mcFilterHTKey
SG::WriteDecorHandleKey< xAOD::EventInfo > m_mcFilterHTKey
Definition: xAODHTFilter.h:54
TruthParticleContainer.h
xAOD::TruthParticle_v1::isGenStable
bool isGenStable() const
Check if this is generator stable particle.
Definition: TruthParticle_v1.cxx:316
isNeutrino
bool isNeutrino(const T &p)
APID: the fourth generation neutrinos are neutrinos.
Definition: AtlasPID.h:152
xAODHTFilter::filterFinalize
virtual StatusCode filterFinalize()
Definition: xAODHTFilter.cxx:85
xAODHTFilter::m_UseNu
bool m_UseNu
Use neutrinos in HT.
Definition: xAODHTFilter.h:42
Common::prompt
bool prompt(const xAOD::TruthParticle *part, ToolHandle< IMCTruthClassifier > &m_classif)
Definition: Common.cxx:7
xAODHTFilter::m_MinJetPt
double m_MinJetPt
Min pT for the truth jets.
Definition: xAODHTFilter.h:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAODHTFilter::m_MaxJetEta
double m_MaxJetEta
Max eta for the truth jets.
Definition: xAODHTFilter.h:37
xAODHTFilter::filterEvent
virtual StatusCode filterEvent()
Definition: xAODHTFilter.cxx:94
xAODHTFilter::m_MaxLepEta
double m_MaxLepEta
Max eta for the truth jets.
Definition: xAODHTFilter.h:41
xAODHTFilter::m_MinHT
double m_MinHT
Min HT for events.
Definition: xAODHTFilter.h:38
xAODHTFilter.h
TruthParticleAuxContainer.h
xAODHTFilter::filterInitialize
virtual StatusCode filterInitialize()
Definition: xAODHTFilter.cxx:61
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
GenFilter
Base class for event generator filtering modules.
Definition: GenFilter.h:30
IMCTruthClassifier.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
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
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
WriteDecorHandle.h
Handle class for adding a decoration to an object.
xAODHTFilter::m_UseLep
bool m_UseLep
Use leptons in HT.
Definition: xAODHTFilter.h:43
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAODHTFilter::m_MaxHT
double m_MaxHT
Max HT for events.
Definition: xAODHTFilter.h:39
TruthVertex.h
Common.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
xAOD::TruthParticle_v1::eta
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition: TruthParticle_v1.cxx:174
EventInfo.h
xAODHTFilter::m_ptfailed
long m_ptfailed
Number of events failing the pT cuts.
Definition: xAODHTFilter.h:50
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
JetContainer.h
xAODHTFilter::~xAODHTFilter
virtual ~xAODHTFilter()
Definition: xAODHTFilter.cxx:55
xAOD::TruthParticle_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: TruthParticle_v1.cxx:166
xAODHTFilter::m_total
long m_total
Total number of events tested.
Definition: xAODHTFilter.h:48
TruthParticle.h
xAOD::TruthParticle_v1::pdgId
int pdgId() const
PDG ID code.
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
xAODHTFilter::xAODHTFilter
xAODHTFilter(const std::string &name, ISvcLocator *pSvcLocator)
Definition: xAODHTFilter.cxx:37
StoreGateSvc.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAODHTFilter::m_eventInfoName
std::string m_eventInfoName
Definition: xAODHTFilter.h:46
xAODHTFilter::m_classif
ToolHandle< IMCTruthClassifier > m_classif
Definition: xAODHTFilter.h:52
HepMCHelpers.h