ATLAS Offline Software
Loading...
Searching...
No Matches
JetPFlowSelectionAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
9
12
13#include "xAODEgamma/Electron.h"
15#include "xAODMuon/Muon.h"
18#include "xAODPFlow/FEHelpers.h"
19
21 ATH_MSG_DEBUG("Initializing " );
22
25 if (!m_electronContainerKey.empty()) {
27 }
36
37 if (m_isEgammaPEB) ATH_MSG_WARNING("Using EgammaPEB PFlow! Checks for MuonFEs will be skipped.");
38
39 return StatusCode::SUCCESS;
40}
41
42StatusCode JetPFlowSelectionAlg::execute(const EventContext& ctx) const {
43 ATH_MSG_DEBUG(" execute() ... ");
44
45 //Check for invalid combinations of flags
47 ATH_MSG_ERROR("excludeChargedElectronFE and includeChargedMuonFE are mutually exclusive!");
48 return StatusCode::FAILURE;
49 }
50
52 ATH_MSG_ERROR("excludeChargedElectronFE and includeChargedElectronFE are mutually exclusive!");
53 return StatusCode::FAILURE;
54 }
55
57 if (! ChargedPFlowObjects.isValid()){
58 ATH_MSG_ERROR("Can't retrieve input container "<< m_ChargedPFlowContainerKey);
59 return StatusCode::FAILURE;
60 }
61
63 if (! NeutralPFlowObjects.isValid()){
64 ATH_MSG_ERROR("Can't retrieve input container "<< m_NeutralPFlowContainerKey);
65 return StatusCode::FAILURE;
66 }
67
69 if (!chargedFE_ElectronLinks.isValid()){
70 ATH_MSG_ERROR("Can't retrieve input decoration " << chargedFE_ElectronLinks.key());
71 return StatusCode::FAILURE;
72 }
73
75 if (!chargedFE_MuonLinks.isValid()){
76 ATH_MSG_ERROR("Can't retrieve input decoration "<< chargedFE_MuonLinks.key());
77 return StatusCode::FAILURE;
78 }
79
81 if (!neutralFE_ElectronLinks.isValid()){
82 ATH_MSG_ERROR("Can't retrieve input decoration "<< neutralFE_ElectronLinks.key());
83 return StatusCode::FAILURE;
84 }
85
87 if (!neutralFE_MuonLinks.isValid()){
88 ATH_MSG_ERROR("Can't retrieve input decoration "<< neutralFE_MuonLinks.key());
89 return StatusCode::FAILURE;
90 }
91
92 auto selectedChargedPFlowObjects = std::make_unique<xAOD::FlowElementContainer>(); // SG::VIEW_ELEMENTS
93 auto selectedChargedPFlowObjectsAux = std::make_unique<xAOD::FlowElementAuxContainer>();
94 selectedChargedPFlowObjects->setStore(selectedChargedPFlowObjectsAux.get());
95
96 auto selectedNeutralPFlowObjects = std::make_unique<xAOD::FlowElementContainer>();
97 auto selectedNeutralPFlowObjectsAux = std::make_unique<xAOD::FlowElementAuxContainer>();
98 selectedNeutralPFlowObjects->setStore(selectedNeutralPFlowObjectsAux.get());
99
100 // To store the charged FE objects matched to an electron/muon
101 std::vector< const xAOD::FlowElement* > ChargedPFlowObjects_matched;
102
103 FEMuonHelper muonHelper;
104 FEElectronHelper electronHelper;
105
106 // Loop over Charged FE objects
107 for ( const xAOD::FlowElement* fe : *ChargedPFlowObjects ) {
108
109 bool isMuonToExclude;
110 bool isMuonToInclude;
111 if (m_isEgammaPEB) {
112 isMuonToExclude = false;
113 isMuonToInclude = false;
114 } else {
115 isMuonToExclude = muonHelper.checkMuonLinks(chargedFE_MuonLinks(*fe), m_muonIDToExclude);
116 isMuonToInclude = muonHelper.checkMuonLinks(chargedFE_MuonLinks(*fe), m_muonIDToInclude);
117 }
118 bool isElectronToInclude = electronHelper.checkElectronLinks(chargedFE_ElectronLinks(*fe), m_electronIDToInclude);
119 bool isElectronToExclude = electronHelper.checkElectronLinks(chargedFE_ElectronLinks(*fe), m_electronIDToExclude);
120
121 bool excludeChargedFE = false;
122
123 //If we have only an exclude list we only need to do:
124 if (m_excludeChargedElectronFE && isElectronToExclude) excludeChargedFE = true;
125 if (m_excludeChargedMuonFE && isMuonToExclude) excludeChargedFE = true;
126
127 //If we have an include list and the include ID is passed, we keep the FE object
128 //Whilst if it otherwise passes excludeID we exclude it
129 //e.g if we want medium we should only include leptons that pass medium ID,
130 //whilst excluding those that pass loose and NOT medium.
131 if (m_includeChargedElectronFE && isElectronToInclude) excludeChargedFE = false;
132 else if (m_includeChargedElectronFE && isElectronToExclude) excludeChargedFE = true;
133
134 if (m_includeChargedMuonFE && isMuonToInclude) excludeChargedFE = false;
135 else if (m_includeChargedMuonFE && isMuonToExclude) excludeChargedFE = true;
136
137 // Select FE object if not matched to an electron or muon, to exclude, via links
138 if ( !excludeChargedFE) {
139 xAOD::FlowElement* selectedFE = new xAOD::FlowElement();
140 selectedChargedPFlowObjects->push_back(selectedFE);
141 *selectedFE = *fe; // copies auxdata
142 }
143 else { // Use the matched object to put back its energy later
144 ChargedPFlowObjects_matched.push_back(fe);
145 }
146
147 } // End loop over Charged FE Objects
148
149 // Loop over Neutral FE objects
150 for ( const xAOD::FlowElement* fe : *NeutralPFlowObjects ) {
151
152 bool isMuonToExclude;
153 bool isMuonToInclude;
154 if (m_isEgammaPEB) {
155 isMuonToExclude = false;
156 isMuonToInclude = false;
157 } else {
158 isMuonToExclude = muonHelper.checkMuonLinks(neutralFE_MuonLinks(*fe), m_muonIDToExclude);
159 isMuonToInclude = muonHelper.checkMuonLinks(neutralFE_MuonLinks(*fe), m_muonIDToInclude);
160 }
161 bool isElectronToInclude = electronHelper.checkElectronLinks(neutralFE_ElectronLinks(*fe), m_electronIDToInclude);
162 bool isElectronToExclude = electronHelper.checkElectronLinks(neutralFE_ElectronLinks(*fe), m_electronIDToExclude);
163
164 //If we specify to exclude, then we exclude if exclude ID is passed
165 //If we specify to include, then we include if include ID is passed - in this case
166 //we might want to e.g include medium electrons, whilst excluding that poass loose ID
167 if (m_excludeNeutralElectronFE && isElectronToExclude) continue;
168 bool vetoElectron_IncludeMode = false;
169 if (m_includeNeutralElectronFE && isElectronToInclude) vetoElectron_IncludeMode = false;
170 else if (m_includeNeutralElectronFE && isElectronToExclude) vetoElectron_IncludeMode = true;
171 if (vetoElectron_IncludeMode) continue;
172
173 xAOD::FlowElement* selectedFE = new xAOD::FlowElement();
174 selectedNeutralPFlowObjects->push_back(selectedFE);
175 *selectedFE = *fe;
176
177 bool excludeNeutralMuon = false;
178 //Muons can have both exclude and include lists
179 //Exclude case is the same as electron exclude case
180 if (m_excludeNeutralMuonFE && isMuonToExclude) excludeNeutralMuon = true;
181 //Include case is more complex - if muon passes includeID we keep it
182 //Whilst if it otherwise passes excludeID we exclude it
183 //e.g if we want medium muons we should only include muons that pass medium ID,
184 //whilst excluding those that pass loose and NOT medium.
185 if (m_includeNeutralMuonFE && isMuonToInclude) excludeNeutralMuon = false;
186 else if (m_includeNeutralMuonFE && isMuonToExclude) excludeNeutralMuon = true;
187
188 if (excludeNeutralMuon) {
189 // If we are excluding muons, we need to adjust the energy of the neutral FE object
190 // by subtracting the muon energy in this calorimeter cluster
192 selectedFE->setP4(muonHelper.adjustNeutralCaloEnergy(clusterMuonEnergyFracs(*fe),*fe));
193 }
194
195 } // End loop over Neutral FE Objects
196
197 // Add the energy from removed charged FE clusters to neutral FE object
198 // if shared clusters exist, create the new neutral FE object otherwise
199 for ( const xAOD::FlowElement* chargedFE : ChargedPFlowObjects_matched ){
200
201 // Get charged FE topoclusters and weights
202 std::vector<std::pair<const xAOD::IParticle*,float> > theOtherPairs_charged = chargedFE->otherObjectsAndWeights();
203 std::vector<ElementLink<xAOD::IParticleContainer>> theOtherLinks_charged = chargedFE->otherObjectLinks();
204
205 // Loop over charged FE topoclusters
206 for (unsigned int iCluster = 0; iCluster < chargedFE->nOtherObjects(); ++iCluster){
207
208 bool thisCluster_matched = false;
209
210 std::pair<const xAOD::IParticle*,float> theOtherPair_charged = theOtherPairs_charged[iCluster];
211 const xAOD::IParticle* theCluster_charged = theOtherPair_charged.first;
212 float theClusterWeight_charged = theOtherPair_charged.second;
213
214 // Loop over neutral FE objects
215 for ( xAOD::FlowElement* neutralFE : *selectedNeutralPFlowObjects ) {
216 if (thisCluster_matched) continue;
217
218 // Loop over neutral FE topoclusters
219 std::vector<std::pair<const xAOD::IParticle*,float> > theOtherPairs_neutral = neutralFE->otherObjectsAndWeights();
220 for (auto& [theCluster_neutral, theClusterWeight_neutral] : theOtherPairs_neutral){
221
222 // If topoclusters are matched, add the energy to the neutral FE object
223 if (theCluster_charged == theCluster_neutral){
224
225 // Add the energy to the neutral FE object
226 float newEnergy = neutralFE->e() + theClusterWeight_charged;
227 neutralFE->setP4(newEnergy/cosh(neutralFE->eta()),
228 neutralFE->eta(),
229 neutralFE->phi(),
230 neutralFE->m());
231
232 ATH_MSG_DEBUG("Updated neutral FlowElement with E, pt, eta and phi: "
233 << neutralFE->e() << ", " << neutralFE->pt() << ", "
234 << neutralFE->eta() << " and " << neutralFE->phi());
235
236 thisCluster_matched = true;
237 }
238
239 } // End loop over neutral FE clusters
240 } // End loop over neutral FE objects
241
242 // If a topocluster is left unmatched, create a neutral FE object.
243 // Ignore topoclusters with nullptr
244 if ( !thisCluster_matched && theCluster_charged ){
245
246 //check if charged cluster belongs to an electron, before we put it back as neutral
247 bool belongsToElectron = false;
249
250 //get container index of charged cluster and compare to indices of electron topoclusters
251 unsigned int chargedClusterIndex = theCluster_charged->index();
252
254 if (!electronReadHandle.isValid()){
255 ATH_MSG_ERROR("Can't retrieve electron container "<< m_electronContainerKey.key());
256 return StatusCode::FAILURE;
257 }
258
259 for (auto thisElectron : *electronReadHandle){
260 const std::vector<const xAOD::CaloCluster*> electronTopoClusters = xAOD::EgammaHelpers::getAssociatedTopoClusters(thisElectron->caloCluster());
261 for (auto thisElectronTopoCluster : electronTopoClusters){
262 if (thisElectronTopoCluster->index() == chargedClusterIndex){
263 belongsToElectron = true;
264 break;
265 }
266 }
267 }
268 }
269
270 if (belongsToElectron) continue;
271
272 double muonCaloEnergy = 0.0;
275 std::vector<double> muonCaloEnergies = chargedFE_energy_match_muonReadHandle(*chargedFE);
276 muonCaloEnergy = muonCaloEnergies[iCluster];
277 }
278
280 selectedNeutralPFlowObjects->push_back(newFE);
281
282 newFE->setP4((theClusterWeight_charged - muonCaloEnergy) / cosh(theCluster_charged->eta()), // using energy from charged FE weight, not cluster->e()
283 theCluster_charged->eta(),
284 theCluster_charged->phi(),
285 theCluster_charged->m());
286 newFE->setCharge(0);
288
289 ATH_MSG_DEBUG("Created neutral FlowElement with E, pt, eta and phi: "
290 << newFE->e() << ", " << newFE->pt() << ", "
291 << newFE->eta() << " and " << newFE->phi());
292
293 std::vector<ElementLink<xAOD::IParticleContainer>> theClusters;
295 theIParticleLink.resetWithKeyAndIndex(theOtherLinks_charged[iCluster].persKey(), theOtherLinks_charged[iCluster].persIndex());
296
297 theClusters.push_back(theIParticleLink);
298 newFE->setOtherObjectLinks(theClusters);
299
300 //Add Standard data to these new FlowElements
302 const xAOD::CaloCluster* castCluster_charged = dynamic_cast<const xAOD::CaloCluster*>(theCluster_charged);
303 FEFiller.addStandardMoments(*newFE,*castCluster_charged);
304 FEFiller.addStandardSamplingEnergies(*newFE,*castCluster_charged);
305
306 float layerEnergy_TileBar0 = castCluster_charged->eSample(xAOD::CaloCluster::CaloSample::TileBar0);
307 float layerEnergy_TileExt0 = castCluster_charged->eSample(xAOD::CaloCluster::CaloSample::TileExt0);
308 const static SG::AuxElement::Accessor<float> accFloatTIle0E("LAYERENERGY_TILE0");
309 accFloatTIle0E(*newFE) = layerEnergy_TileBar0 + layerEnergy_TileExt0;
310
311 const static SG::AuxElement::Accessor<float> accFloatTiming("TIMING");
312 accFloatTiming(*newFE) = castCluster_charged->time();
313 }
314
315 } // End loop over topoclusters of removed charged FE objects
316 } // End loop over removed charged FE objects
317
318
319 auto handle_ChargedPFlow_out = SG::makeHandle(m_outputChargedPFlowHandleKey, ctx);
320 if (!handle_ChargedPFlow_out.record(std::move(selectedChargedPFlowObjects), std::move(selectedChargedPFlowObjectsAux)) ){
321 ATH_MSG_ERROR("Can't record output PFlow container "<< m_outputChargedPFlowHandleKey);
322 return StatusCode::FAILURE;
323 }
324
325 auto handle_NeutralPFlow_out = SG::makeHandle(m_outputNeutralPFlowHandleKey, ctx);
326 if (!handle_NeutralPFlow_out.record(std::move(selectedNeutralPFlowObjects), std::move(selectedNeutralPFlowObjectsAux)) ){
327 ATH_MSG_ERROR("Can't record output PFlow container "<< m_outputNeutralPFlowHandleKey);
328 return StatusCode::FAILURE;
329 }
330
331 return StatusCode::SUCCESS;
332}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading a decoration on an object.
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
bool checkElectronLinks(const std::vector< ElementLink< xAOD::ElectronContainer > > &FE_ElectronLinks, const std::string &qualityString) const
void addStandardSamplingEnergies(xAOD::FlowElement &theFE, const xAOD::CaloCluster &theCluster)
void addStandardMoments(xAOD::FlowElement &theFE, const xAOD::CaloCluster &theCluster)
bool checkMuonLinks(const std::vector< ElementLink< xAOD::MuonContainer > > &FE_MuonLinks, const std::string &qualityString) const
Verify if at least one combined muon in a list passes the relevant quality criteria Details of muon w...
TLorentzVector adjustNeutralCaloEnergy(const std::vector< double > &clusterMuonEnergyFracs, const xAOD::FlowElement &theFE) const
SG::WriteHandleKey< xAOD::FlowElementContainer > m_outputNeutralPFlowHandleKey
SG::ReadDecorHandleKey< xAOD::FlowElementContainer > m_neutralFEElectronsReadDecorKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronContainerKey
SG::ReadHandleKey< xAOD::FlowElementContainer > m_ChargedPFlowContainerKey
Gaudi::Property< std::string > m_electronIDToInclude
Gaudi::Property< std::string > m_muonIDToInclude
Gaudi::Property< bool > m_includeChargedElectronFE
Gaudi::Property< bool > m_excludeNeutralMuonFE
SG::ReadDecorHandleKey< xAOD::FlowElementContainer > m_chargedFEMuonsReadDecorKey
Gaudi::Property< bool > m_excludeNeutralElectronFE
SG::ReadDecorHandleKey< xAOD::FlowElementContainer > m_neutralFEMuonsReadDecorKey
Gaudi::Property< bool > m_includeChargedMuonFE
Gaudi::Property< bool > m_includeNeutralMuonFE
SG::ReadDecorHandleKey< xAOD::FlowElementContainer > m_chargedFE_energy_match_muonReadHandleKey
Gaudi::Property< std::string > m_muonIDToExclude
Gaudi::Property< bool > m_excludeChargedElectronFE
Gaudi::Property< bool > m_excludeChargedMuonFE
SG::ReadDecorHandleKey< xAOD::FlowElementContainer > m_neutralFEMuons_efrac_match_DecorKey
Gaudi::Property< bool > m_isEgammaPEB
StatusCode execute(const EventContext &ctx) const override
Gaudi::Property< bool > m_includeNeutralElectronFE
SG::ReadHandleKey< xAOD::FlowElementContainer > m_NeutralPFlowContainerKey
Gaudi::Property< std::string > m_electronIDToExclude
SG::WriteHandleKey< xAOD::FlowElementContainer > m_outputChargedPFlowHandleKey
SG::ReadDecorHandleKey< xAOD::FlowElementContainer > m_chargedFEElectronsReadDecorKey
StatusCode initialize() override
Athena algorithm's Hooks.
Handle class for reading a decoration on an object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
flt_t time() const
Access cluster time.
float eSample(const CaloSample sampling) const
virtual double pt() const override
void setP4(float pt, float eta, float phi, float m)
virtual double phi() const override
The azimuthal angle ( ) of the particle.
void setCharge(float c)
virtual double eta() const override
The pseudorapidity ( ) of the particle.
void setSignalType(signal_t t)
void setOtherObjectLinks(const std::vector< ElementLink< IParticleContainer > > &elV)
virtual double e() const override
The total energy of the particle.
Class providing the definition of the 4-vector interface.
virtual double eta() const =0
The pseudorapidity ( ) of the particle.
virtual double m() const =0
The invariant mass of the particle.
virtual double phi() const =0
The azimuthal angle ( ) of the particle.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
std::vector< const xAOD::CaloCluster * > getAssociatedTopoClusters(const xAOD::CaloCluster *cluster)
Return a vector of all the topo clusters associated with the egamma cluster.
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
FlowElement_v1 FlowElement
Definition of the current "pfo version".
Definition FlowElement.h:16