ATLAS Offline Software
Loading...
Searching...
No Matches
MetMakerAlg.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
7
8
9//
10// includes
11//
12
14
17
20
21//
22// method implementations
23//
24
25namespace CP
26{
27
28 StatusCode MetMakerAlg ::
29 initialize ()
30 {
31 ANA_CHECK (m_makerTool.retrieve());
32
36 }
42
43 // Initialize invisible selections - need to pair each selection with its corresponding handle
44 if (m_invisSelectionKeys.size() != m_invisHandles.size()) {
45 ATH_MSG_ERROR("Number of invisible selections (" << m_invisSelectionKeys.size()
46 << ") doesn't match number of invisible handles (" << m_invisHandles.size() << ")");
47 return StatusCode::FAILURE;
48 }
49 m_invisSelections.clear();
50 m_invisSelections.reserve(m_invisHandles.size());
51 for (size_t i = 0; i < m_invisHandles.size(); ++i) {
52 m_invisSelections.emplace_back(m_invisSelectionKeys[i], this);
54 }
55
58
59 if (!m_systematicsTool.empty())
60 {
61 ANA_CHECK (m_systematicsTool.retrieve());
63 }
64
65 ANA_CHECK (m_systematicsList.initialize());
66
67 return StatusCode::SUCCESS;
68 }
69
70
71
72 StatusCode MetMakerAlg ::
73 execute (const EventContext& ctx)
74 {
75 const xAOD::MissingETContainer* metcore {nullptr};
76 ANA_CHECK (evtStore()->retrieve(metcore, m_metCoreName));
77
78 const xAOD::MissingETAssociationMap* metMap {nullptr};
79 ANA_CHECK (evtStore()->retrieve(metMap, m_metAssociationName));
80
81 // Helper keeps track of object selection flags for this map
82 xAOD::MissingETAssociationHelper metHelper(&(*metMap));
83
84 for (const auto& sys : m_systematicsList.systematicsVector())
85 {
86 auto met = std::make_unique<xAOD::MissingETContainer> ();
87 auto aux = std::make_unique<xAOD::MissingETAuxContainer> ();
88 met->setStore (aux.get());
89
90 metHelper.resetObjSelectionFlags();
91
93 for (size_t i = 0; i < m_invisHandles.size(); ++i) {
94 const xAOD::IParticleContainer* invisible = nullptr;
95 ATH_CHECK( m_invisHandles.at(i).retrieve(invisible, sys, ctx) );
96 for (const xAOD::IParticle *invisParticle : *invisible) {
97 if (m_invisSelections.at(i).getBool(*invisParticle, sys))
98 invisSelected.push_back(invisParticle);
99 }
100 }
101 if (invisSelected.size() > 0)
102 ANA_CHECK (m_makerTool->markInvisible (invisSelected.asDataVector(), metHelper, met.get() ) );
103
104 // Lambda helping with calculating the MET terms coming from the leptons
105 // (and photons).
106 auto processParticles =
110 const std::string& term) -> StatusCode {
111 if (!handle) {
112 // The NN MET input vector needs a term for every hard-object type,
113 // so a missing handle is a configuration error on the NN path.
114 if (m_evaluateNNMET) {
115 ANA_MSG_ERROR ("evaluateNNMET requires a container for every "
116 "hard-object type, but the handle for term \""
117 << term << "\" is not configured");
118 return StatusCode::FAILURE;
119 }
120 return StatusCode::SUCCESS;
121 }
122 const xAOD::IParticleContainer* particles = nullptr;
123 ANA_CHECK (handle.retrieve (particles, sys));
125 for (const xAOD::IParticle *particle : *particles)
126 if (selection.getBool(*particle, sys))
127 selected.push_back(particle);
128 ANA_CHECK (m_makerTool->rebuildMET (term, type, met.get(),
129 selected.asDataVector(), metHelper));
130 return StatusCode::SUCCESS;
131 };
132
133 // Calculate the terms coming from the user's selected objects.
136 ANA_CHECK (processParticles (m_photonsHandle, m_photonsSelection,
138 // a muon overlapping with tau is not removed. So if a true muon passes an (extremely loose) tau ID selection, it is double-counted. # https://its.cern.ch/jira/browse/ATLHMBS-651
140 {
141 ANA_CHECK (processParticles (m_tausHandle, m_tausSelection,
143 }
144 ANA_CHECK (processParticles (m_muonsHandle, m_muonsSelection,
147 {
148 ANA_CHECK (processParticles (m_tausHandle, m_tausSelection,
150 }
151
152
153 const xAOD::JetContainer *jets {nullptr};
154 ANA_CHECK (m_jetsHandle.retrieve (jets, sys, ctx));
155
156 if (m_doTrackMet)
157 {
158 ANA_CHECK (m_makerTool->rebuildTrackMET (m_jetsKey, m_softTermKey, met.get(), jets, metcore, metHelper, m_doJetJVT));
159 } else
160 {
161 ANA_CHECK (m_makerTool->rebuildJetMET (m_jetsKey, m_softTermKey, met.get(), jets, metcore, metHelper, m_doJetJVT));
162 }
163
164 // Optionally run the NN-based MET (e.g. met::METNet). For NN tools,
165 // rebuildJetMET only assembles the network inputs; evaluateNNMET runs the
166 // inference and adds the total term directly. We then decorate met/phi on
167 // every term exactly as MetBuilderAlg would, so the NN path needs no
168 // builder afterwards (a builder sum would clobber the NN Final term).
169 if (m_evaluateNNMET)
170 {
171 ANA_CHECK (m_makerTool->evaluateNNMET (m_finalKey, met.get()));
172 static const SG::Decorator<float> met_met_dec ("met");
173 static const SG::Decorator<float> met_phi_dec ("phi");
174 for (const xAOD::MissingET *metTerm : *met)
175 {
176 if (!metTerm) continue;
177 met_met_dec (*metTerm) = metTerm->met();
178 met_phi_dec (*metTerm) = metTerm->phi();
179 }
180 }
181
182 // Systematics
183 if (!m_systematicsTool.empty())
184 {
185 ANA_CHECK (m_systematicsTool->applySystematicVariation (sys));
186
187 xAOD::MissingET *softTerm = (*met)[m_softTermKey];
188 if (softTerm == nullptr)
189 {
190 ANA_MSG_ERROR ("failed to find MET soft-term \"" << m_softTermKey << "\"");
191 return StatusCode::FAILURE;
192 }
193
194 // Set the seed for reproducibility
195 m_systematicsTool->setRandomSeed(static_cast<int>(1e6 * softTerm->phi()));
196
197 // This returns a `CorrectionCode`, so in principle this could
198 // return an `OutOfValidity` result, but I have no idea what
199 // that would mean or how to handle it, so I'm implicitly
200 // converting it into a `FAILURE` instead.
201 ANA_CHECK (m_systematicsTool->applyCorrection (*softTerm, metHelper));
202 }
203
204 ANA_CHECK (m_metHandle.record (std::move (met), std::move (aux), sys, ctx));
205 }
206
207 return StatusCode::SUCCESS;
208 }
209}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
DataVector adapter that acts like it holds const pointers.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
Helper class to provide type-safe access to aux data.
ServiceHandle< StoreGateSvc > & evtStore()
Gaudi::Property< bool > m_doTrackMet
whether to use track-met instead of jet-met
SysReadSelectionHandle m_muonsSelection
the selection on the input muons
SysReadHandle< xAOD::IParticleContainer > m_tausHandle
the electron container to use
SysWriteHandle< xAOD::MissingETContainer, xAOD::MissingETAuxContainer > m_metHandle
the met collection we run on
SysReadHandle< xAOD::IParticleContainer > m_electronsHandle
the electron container to use
Definition MetMakerAlg.h:69
Gaudi::Property< std::vector< std::string > > m_invisSelectionKeys
the selection on the invisible container
Gaudi::Property< std::string > m_electronsKey
the key for m_electronsHandle
Definition MetMakerAlg.h:79
Gaudi::Property< std::string > m_photonsKey
the key for m_photonsHandle
Definition MetMakerAlg.h:93
ToolHandle< IMETSystematicsTool > m_systematicsTool
the systematics tool
Definition MetMakerAlg.h:53
Gaudi::Property< std::string > m_softTermKey
the soft term key
SysReadSelectionHandle m_photonsSelection
the selection on the input photons
Definition MetMakerAlg.h:88
std::vector< SysReadSelectionHandle > m_invisSelections
ToolHandle< IMETMaker > m_makerTool
the maker tool
Definition MetMakerAlg.h:49
SysHandleArray< SysReadHandle< xAOD::IParticleContainer > > m_invisHandles
the container to be marked as invisible particles
Gaudi::Property< std::string > m_metCoreName
the name of the core MissingETContainer
Definition MetMakerAlg.h:57
Gaudi::Property< std::string > m_tausKey
the key for m_tausHandle
Gaudi::Property< std::string > m_finalKey
the name of the total MET term written by evaluateNNMET
Gaudi::Property< std::string > m_jetsKey
the key for m_jetsHandle
SysReadHandle< xAOD::IParticleContainer > m_photonsHandle
the photon container to use
Definition MetMakerAlg.h:83
Gaudi::Property< bool > m_doJetJVT
whether to do jet JVT
SysReadHandle< xAOD::IParticleContainer > m_muonsHandle
the muon container to use
Definition MetMakerAlg.h:97
SysReadSelectionHandle m_tausSelection
the selection on the input taus
Gaudi::Property< bool > m_switchTauMuonOrder
whether to switch order of taus and muons
Gaudi::Property< std::string > m_metAssociationName
the name of the MissingETAssociationMap
Definition MetMakerAlg.h:61
Gaudi::Property< bool > m_evaluateNNMET
whether to run NN-based MET inference (evaluateNNMET) after rebuildJetMET
SysReadSelectionHandle m_electronsSelection
the selection on the input electrons
Definition MetMakerAlg.h:74
SysReadHandle< xAOD::JetContainer > m_jetsHandle
the input jet collection we run on
Gaudi::Property< std::string > m_muonsKey
the key for m_muonsHandle
SysListHandle m_systematicsList
the systematics list we run
Definition MetMakerAlg.h:65
a data handle for reading systematics varied input data
a data handle for reading systematically varied selection properties from objects
DataVector adapter that acts like it holds const pointers.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
const DV * asDataVector() const
Return a pointer to this object, as a const DataVector.
void handle(const Incident &inc)
receive the given incident
Class providing the definition of the 4-vector interface.
float phi() const
Returns .
const std::string selection
Select isolated Photons, Electrons and Muons.
static const SG::Decorator< float > met_phi_dec("phi")
static const SG::Decorator< float > met_met_dec("met")
SG::Decorator< T, ALLOC > Decorator
Helper class to provide type-safe access to aux data, specialized for JaggedVecElt.
Definition AuxElement.h:576
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
ObjectType
Type of objects that have a representation in the xAOD EDM.
Definition ObjectType.h:32
@ Photon
The object is a photon.
Definition ObjectType.h:47
@ Muon
The object is a muon.
Definition ObjectType.h:48
@ Electron
The object is an electron.
Definition ObjectType.h:46
@ Tau
The object is a tau (jet).
Definition ObjectType.h:49
MissingET_v1 MissingET
Version control by type defintion.
JetContainer_v1 JetContainer
Definition of the current "jet container version".
MissingETAssociationMap_v1 MissingETAssociationMap
Version control by type defintion.
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.