ATLAS Offline Software
Loading...
Searching...
No Matches
JetPFlowSelectionAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 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
40
41StatusCode JetPFlowSelectionAlg::execute(const EventContext& ctx) const {
42 ATH_MSG_DEBUG(" execute() ... ");
43
44 //Check for invalid combinations of flags
46 ATH_MSG_ERROR("excludeChargedElectronFE and includeChargedMuonFE are mutually exclusive!");
47 return StatusCode::FAILURE;
48 }
49
51 ATH_MSG_ERROR("excludeChargedElectronFE and includeChargedElectronFE are mutually exclusive!");
52 return StatusCode::FAILURE;
53 }
54
56 if (! ChargedPFlowObjects.isValid()){
57 ATH_MSG_ERROR("Can't retrieve input container "<< m_ChargedPFlowContainerKey);
58 return StatusCode::FAILURE;
59 }
60
62 if (! NeutralPFlowObjects.isValid()){
63 ATH_MSG_ERROR("Can't retrieve input container "<< m_NeutralPFlowContainerKey);
64 return StatusCode::FAILURE;
65 }
66
68 if (!chargedFE_ElectronLinks.isValid()){
69 ATH_MSG_ERROR("Can't retrieve input decoration " << chargedFE_ElectronLinks.key());
70 return StatusCode::FAILURE;
71 }
72
74 if (!chargedFE_MuonLinks.isValid()){
75 ATH_MSG_ERROR("Can't retrieve input decoration "<< chargedFE_MuonLinks.key());
76 return StatusCode::FAILURE;
77 }
78
80 if (!neutralFE_ElectronLinks.isValid()){
81 ATH_MSG_ERROR("Can't retrieve input decoration "<< neutralFE_ElectronLinks.key());
82 return StatusCode::FAILURE;
83 }
84
86 if (!neutralFE_MuonLinks.isValid()){
87 ATH_MSG_ERROR("Can't retrieve input decoration "<< neutralFE_MuonLinks.key());
88 return StatusCode::FAILURE;
89 }
90
91 auto selectedChargedPFlowObjects = std::make_unique<xAOD::FlowElementContainer>(); // SG::VIEW_ELEMENTS
92 auto selectedChargedPFlowObjectsAux = std::make_unique<xAOD::FlowElementAuxContainer>();
93 selectedChargedPFlowObjects->setStore(selectedChargedPFlowObjectsAux.get());
94
95 auto selectedNeutralPFlowObjects = std::make_unique<xAOD::FlowElementContainer>();
96 auto selectedNeutralPFlowObjectsAux = std::make_unique<xAOD::FlowElementAuxContainer>();
97 selectedNeutralPFlowObjects->setStore(selectedNeutralPFlowObjectsAux.get());
98
99 // To store the charged FE objects matched to an electron/muon
100 std::vector< const xAOD::FlowElement* > ChargedPFlowObjects_matched;
101
102 FEMuonHelper muonHelper;
103 FEElectronHelper electronHelper;
104
105 // Loop over Charged FE objects
106 for ( const xAOD::FlowElement* fe : *ChargedPFlowObjects ) {
107
108 bool isMuonToExclude = muonHelper.checkMuonLinks(chargedFE_MuonLinks(*fe), m_muonIDToExclude);
109 bool isMuonToInclude = muonHelper.checkMuonLinks(chargedFE_MuonLinks(*fe), m_muonIDToInclude);
110 bool isElectronToInclude = electronHelper.checkElectronLinks(chargedFE_ElectronLinks(*fe), m_electronIDToInclude);
111 bool isElectronToExclude = electronHelper.checkElectronLinks(chargedFE_ElectronLinks(*fe), m_electronIDToExclude);
112
113 bool excludeChargedFE = false;
114
115 //If we have only an exclude list we only need to do:
116 if (m_excludeChargedElectronFE && isElectronToExclude) excludeChargedFE = true;
117 if (m_excludeChargedMuonFE && isMuonToExclude) excludeChargedFE = true;
118
119 //If we have an include list and the include ID is passed, we keep the FE object
120 //Whilst if it otherwise passes excludeID we exclude it
121 //e.g if we want medium we should only include leptons that pass medium ID,
122 //whilst excluding those that pass loose and NOT medium.
123 if (m_includeChargedElectronFE && isElectronToInclude) excludeChargedFE = false;
124 else if (m_includeChargedElectronFE && isElectronToExclude) excludeChargedFE = true;
125
126 if (m_includeChargedMuonFE && isMuonToInclude) excludeChargedFE = false;
127 else if (m_includeChargedMuonFE && isMuonToExclude) excludeChargedFE = true;
128
129 // Select FE object if not matched to an electron or muon, to exclude, via links
130 if ( !excludeChargedFE) {
131 xAOD::FlowElement* selectedFE = new xAOD::FlowElement();
132 selectedChargedPFlowObjects->push_back(selectedFE);
133 *selectedFE = *fe; // copies auxdata
134 }
135 else { // Use the matched object to put back its energy later
136 ChargedPFlowObjects_matched.push_back(fe);
137 }
138
139 } // End loop over Charged FE Objects
140
141 // Loop over Neutral FE objects
142 for ( const xAOD::FlowElement* fe : *NeutralPFlowObjects ) {
143
144 bool isMuonToExclude = muonHelper.checkMuonLinks(neutralFE_MuonLinks(*fe), m_muonIDToExclude);
145 bool isMuonToInclude = muonHelper.checkMuonLinks(neutralFE_MuonLinks(*fe), m_muonIDToInclude);
146 bool isElectronToInclude = electronHelper.checkElectronLinks(neutralFE_ElectronLinks(*fe), m_electronIDToInclude);
147 bool isElectronToExclude = electronHelper.checkElectronLinks(neutralFE_ElectronLinks(*fe), m_electronIDToExclude);
148
149 //If we specify to exclude, then we exclude if exclude ID is passed
150 //If we specify to include, then we include if include ID is passed - in this case
151 //we might want to e.g include medium electrons, whilst excluding that poass loose ID
152 if (m_excludeNeutralElectronFE && isElectronToExclude) continue;
153 bool vetoElectron_IncludeMode = false;
154 if (m_includeNeutralElectronFE && isElectronToInclude) vetoElectron_IncludeMode = false;
155 else if (m_includeNeutralElectronFE && isElectronToExclude) vetoElectron_IncludeMode = true;
156 if (vetoElectron_IncludeMode) continue;
157
158 xAOD::FlowElement* selectedFE = new xAOD::FlowElement();
159 selectedNeutralPFlowObjects->push_back(selectedFE);
160 *selectedFE = *fe;
161
162 bool excludeNeutralMuon = false;
163 //Muons can have both exclude and include lists
164 //Exclude case is the same as electron exclude case
165 if (m_excludeNeutralMuonFE && isMuonToExclude) excludeNeutralMuon = true;
166 //Include case is more complex - if muon passes includeID we keep it
167 //Whilst if it otherwise passes excludeID we exclude it
168 //e.g if we want medium muons we should only include muons that pass medium ID,
169 //whilst excluding those that pass loose and NOT medium.
170 if (m_includeNeutralMuonFE && isMuonToInclude) excludeNeutralMuon = false;
171 else if (m_includeNeutralMuonFE && isMuonToExclude) excludeNeutralMuon = true;
172
173 if (excludeNeutralMuon) {
174 // If we are excluding muons, we need to adjust the energy of the neutral FE object
175 // by subtracting the muon energy in this calorimeter cluster
177 selectedFE->setP4(muonHelper.adjustNeutralCaloEnergy(clusterMuonEnergyFracs(*fe),*fe));
178 }
179
180 } // End loop over Neutral FE Objects
181
182 // Add the energy from removed charged FE clusters to neutral FE object
183 // if shared clusters exist, create the new neutral FE object otherwise
184 for ( const xAOD::FlowElement* chargedFE : ChargedPFlowObjects_matched ){
185
186 // Get charged FE topoclusters and weights
187 std::vector<std::pair<const xAOD::IParticle*,float> > theOtherPairs_charged = chargedFE->otherObjectsAndWeights();
188 std::vector<ElementLink<xAOD::IParticleContainer>> theOtherLinks_charged = chargedFE->otherObjectLinks();
189
190 // Loop over charged FE topoclusters
191 for (unsigned int iCluster = 0; iCluster < chargedFE->nOtherObjects(); ++iCluster){
192
193 bool thisCluster_matched = false;
194
195 std::pair<const xAOD::IParticle*,float> theOtherPair_charged = theOtherPairs_charged[iCluster];
196 const xAOD::IParticle* theCluster_charged = theOtherPair_charged.first;
197 float theClusterWeight_charged = theOtherPair_charged.second;
198
199 // Loop over neutral FE objects
200 for ( xAOD::FlowElement* neutralFE : *selectedNeutralPFlowObjects ) {
201 if (thisCluster_matched) continue;
202
203 // Loop over neutral FE topoclusters
204 std::vector<std::pair<const xAOD::IParticle*,float> > theOtherPairs_neutral = neutralFE->otherObjectsAndWeights();
205 for (auto& [theCluster_neutral, theClusterWeight_neutral] : theOtherPairs_neutral){
206
207 // If topoclusters are matched, add the energy to the neutral FE object
208 if (theCluster_charged == theCluster_neutral){
209
210 // Add the energy to the neutral FE object
211 float newEnergy = neutralFE->e() + theClusterWeight_charged;
212 neutralFE->setP4(newEnergy/cosh(neutralFE->eta()),
213 neutralFE->eta(),
214 neutralFE->phi(),
215 neutralFE->m());
216
217 ATH_MSG_DEBUG("Updated neutral FlowElement with E, pt, eta and phi: "
218 << neutralFE->e() << ", " << neutralFE->pt() << ", "
219 << neutralFE->eta() << " and " << neutralFE->phi());
220
221 thisCluster_matched = true;
222 }
223
224 } // End loop over neutral FE clusters
225 } // End loop over neutral FE objects
226
227 // If a topocluster is left unmatched, create a neutral FE object.
228 // Ignore topoclusters with nullptr
229 if ( !thisCluster_matched && theCluster_charged ){
230
231 //check if charged cluster belongs to an electron, before we put it back as neutral
232 bool belongsToElectron = false;
234
235 //get container index of charged cluster and compare to indices of electron topoclusters
236 unsigned int chargedClusterIndex = theCluster_charged->index();
237
239 if (!electronReadHandle.isValid()){
240 ATH_MSG_ERROR("Can't retrieve electron container "<< m_electronContainerKey.key());
241 return StatusCode::FAILURE;
242 }
243
244 for (auto thisElectron : *electronReadHandle){
245 const std::vector<const xAOD::CaloCluster*> electronTopoClusters = xAOD::EgammaHelpers::getAssociatedTopoClusters(thisElectron->caloCluster());
246 for (auto thisElectronTopoCluster : electronTopoClusters){
247 if (thisElectronTopoCluster->index() == chargedClusterIndex){
248 belongsToElectron = true;
249 break;
250 }
251 }
252 }
253 }
254
255 if (belongsToElectron) continue;
256
257 double muonCaloEnergy = 0.0;
260 std::vector<double> muonCaloEnergies = chargedFE_energy_match_muonReadHandle(*chargedFE);
261 muonCaloEnergy = muonCaloEnergies[iCluster];
262 }
263
265 selectedNeutralPFlowObjects->push_back(newFE);
266
267 newFE->setP4((theClusterWeight_charged - muonCaloEnergy) / cosh(theCluster_charged->eta()), // using energy from charged FE weight, not cluster->e()
268 theCluster_charged->eta(),
269 theCluster_charged->phi(),
270 theCluster_charged->m());
271 newFE->setCharge(0);
273
274 ATH_MSG_DEBUG("Created neutral FlowElement with E, pt, eta and phi: "
275 << newFE->e() << ", " << newFE->pt() << ", "
276 << newFE->eta() << " and " << newFE->phi());
277
278 std::vector<ElementLink<xAOD::IParticleContainer>> theClusters;
280 theIParticleLink.resetWithKeyAndIndex(theOtherLinks_charged[iCluster].persKey(), theOtherLinks_charged[iCluster].persIndex());
281
282 theClusters.push_back(theIParticleLink);
283 newFE->setOtherObjectLinks(theClusters);
284
285 //Add Standard data to these new FlowElements
287 const xAOD::CaloCluster* castCluster_charged = dynamic_cast<const xAOD::CaloCluster*>(theCluster_charged);
288 FEFiller.addStandardMoments(*newFE,*castCluster_charged);
289 FEFiller.addStandardSamplingEnergies(*newFE,*castCluster_charged);
290
291 float layerEnergy_TileBar0 = castCluster_charged->eSample(xAOD::CaloCluster::CaloSample::TileBar0);
292 float layerEnergy_TileExt0 = castCluster_charged->eSample(xAOD::CaloCluster::CaloSample::TileExt0);
293 const static SG::AuxElement::Accessor<float> accFloatTIle0E("LAYERENERGY_TILE0");
294 accFloatTIle0E(*newFE) = layerEnergy_TileBar0 + layerEnergy_TileExt0;
295
296 const static SG::AuxElement::Accessor<float> accFloatTiming("TIMING");
297 accFloatTiming(*newFE) = castCluster_charged->time();
298 }
299
300 } // End loop over topoclusters of removed charged FE objects
301 } // End loop over removed charged FE objects
302
303
304 auto handle_ChargedPFlow_out = SG::makeHandle(m_outputChargedPFlowHandleKey, ctx);
305 if (!handle_ChargedPFlow_out.record(std::move(selectedChargedPFlowObjects), std::move(selectedChargedPFlowObjectsAux)) ){
306 ATH_MSG_ERROR("Can't record output PFlow container "<< m_outputChargedPFlowHandleKey);
307 return StatusCode::FAILURE;
308 }
309
310 auto handle_NeutralPFlow_out = SG::makeHandle(m_outputNeutralPFlowHandleKey, ctx);
311 if (!handle_NeutralPFlow_out.record(std::move(selectedNeutralPFlowObjects), std::move(selectedNeutralPFlowObjectsAux)) ){
312 ATH_MSG_ERROR("Can't record output PFlow container "<< m_outputNeutralPFlowHandleKey);
313 return StatusCode::FAILURE;
314 }
315
316 return StatusCode::SUCCESS;
317}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(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
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.
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
size_t index() const
Return the index of this element within its container.
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