ATLAS Offline Software
Loading...
Searching...
No Matches
PFUnifiedSubtractionOnlyTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "PFData.h"
7
8#include "eflowCaloObject.h"
12#include "eflowRecTrack.h"
15
21
22using namespace eflowSubtract;
23
24PFUnifiedSubtractionOnlyTool::PFUnifiedSubtractionOnlyTool(const std::string &type, const std::string &name, const IInterface *parent) : base_class(type, name, parent),
26{
27}
28
30= default;
31
33{
34
35 ATH_CHECK(m_theEOverPTool.retrieve());
36
37 ATH_CHECK(m_theEOverPTool->fillBinnedParameters(m_binnedParameters.get()));
38
40 if (!m_trkpos)
41 {
42 ATH_MSG_ERROR("Failed to get TrackPositionProvider for cluster preselection!");
43 return StatusCode::FAILURE;
44 }
45
47
48
49 //Set the level of the helpers to the same as the tool here
50 m_pfSubtractionStatusSetter.msg().setLevel(this->msg().level());
51 m_pfSubtractionEnergyRatioCalculator.msg().setLevel(this->msg().level());
52 m_subtractor.m_facilitator.msg().setLevel(this->msg().level());
53
54
55 return StatusCode::SUCCESS;
56
57}
58
60 const EventContext& ctx,
62) const
63{
64
65 if (!data.caloObjects) {
66 ATH_MSG_ERROR("PFData::caloObjects is null; caller must set it before invoking the subtraction tool");
67 return StatusCode::FAILURE;
68 }
69
70 unsigned int numMatches;
71 unsigned int nCaloObj;
72
74 numMatches = data.nOrigCaloObj;
75 nCaloObj = data.nOrigCaloObj;
76 }
77 else{
78 numMatches = data.nMatches;
79 nCaloObj = 0;
80 }
81
82 if (!m_calcEOverP){
83 if (!m_recoverSplitShowers) performSubtraction(ctx, 0, nCaloObj, data);
84 else performSubtraction(ctx, numMatches, nCaloObj, data);
85 }
86 //in e/p mode we want to simulate showers for all eflowCaloObjects and then decorate the eflowTrack with the relevant e/p information, but not perform any subtraction.
87 //in particular simulateShowers calculates the LHED which is used for the binning of e/p when we measure it.
88 else simulateShowers(ctx, *(data.caloObjects), nCaloObj);
89
90 return StatusCode::SUCCESS;
91}
92
93void PFUnifiedSubtractionOnlyTool::simulateShowers(const EventContext& ctx, eflowCaloObjectContainer& eflowCaloObjects, const unsigned int& nCaloObj) const {
94
95 const double gaussianRadius = 0.032;
96 const double gaussianRadiusError = 1.0e-3;
97 const double maximumRadiusSigma = 3.0;
98
99 eflowLayerIntegrator integrator(gaussianRadius, gaussianRadiusError, maximumRadiusSigma, m_isHLLHC);
100
101 //For each eflowCaloObject we calculate the expected energy deposit in the calorimeter and cell ordering for subtraction.
102 for (unsigned int iCalo = nCaloObj; iCalo < eflowCaloObjects.size(); ++iCalo) {
103 eflowCaloObject* thisEflowCaloObject = eflowCaloObjects.at(iCalo);
104 thisEflowCaloObject->simulateShower(ctx, &integrator, m_binnedParameters.get(), m_useNNEnergy ? &(*m_NNEnergyPredictorTool) : nullptr, m_useLegacyEBinIndex);
105 }
106}
107
108void PFUnifiedSubtractionOnlyTool::performSubtraction(const EventContext& ctx, const unsigned int& startingPoint, const unsigned int& nCaloObj, PFData &data ) const{
109
111 if (!m_recoverSplitShowers && 0 != nCaloObj) ATH_MSG_WARNING("Not in Split Showers Mode and already have " << nCaloObj << " eflowCaloObjects");
112
113 simulateShowers(ctx, *(data.caloObjects), nCaloObj);
114
115 unsigned int nEFCaloObs = data.caloObjects->size();
116 for (unsigned int iCalo = startingPoint; iCalo < nEFCaloObs; ++iCalo) {
117 eflowCaloObject* thisEflowCaloObject = data.caloObjects->at(iCalo);
118 this->performSubtraction(*thisEflowCaloObject);
119 }
120}
121
123
124 ATH_MSG_DEBUG("In performSubtraction");
125
126 unsigned int nClusters = thisEflowCaloObject.nClusters();
127 unsigned int nTrackMatches = thisEflowCaloObject.nTracks();
128
129 ATH_MSG_DEBUG("Have got an eflowCaloObject with " << nClusters << " clusters and " << nTrackMatches << " track matches");
130
131 if (msgLevel(MSG::DEBUG)){
132 for (unsigned int iTrack = 0; iTrack < nTrackMatches; ++iTrack){
133 eflowRecTrack* thisTrack = thisEflowCaloObject.efRecTrack(iTrack);
134 ATH_MSG_DEBUG("eflowCaloObject has track with E, pt and eta " << thisTrack->getTrack()->e() << ", " << thisTrack->getTrack()->pt() << " and " << thisTrack->getTrack()->eta());
135 }
136 }
137
138 //To keep historical behaviour when in recover split showers mode allow tracks with no cluster matches to proceed.
139 if (!m_recoverSplitShowers && nClusters < 1) return;
140
141 //Need at least one track in this eflowCaloObject to continue.
142 if (nTrackMatches < 1) return;
143
144 double expectedEnergy = thisEflowCaloObject.getExpectedEnergy();
145 double clusterEnergy = thisEflowCaloObject.getClusterEnergy();
146 double expectedSigma = sqrt(thisEflowCaloObject.getExpectedVariance());
147
148 /* Check e/p, if on first pass - return if e/p not consistent with expected e/p */
150 if (isEOverPFail(expectedEnergy, expectedSigma, clusterEnergy)) return;
151 }
152
153 const std::vector<std::pair<eflowTrackClusterLink *, std::pair<float, float>>> &matchedTrackList = thisEflowCaloObject.efRecLink();
154
155 ATH_MSG_DEBUG("Matched Track List has size " << matchedTrackList.size());
156
157 if (msgLevel(MSG::DEBUG))
158 {
159 for (unsigned int iTrack = 0; iTrack < nTrackMatches; ++iTrack)
160 {
161 const xAOD::TrackParticle *thisTrack = thisEflowCaloObject.efRecTrack(iTrack)->getTrack();
162 ATH_MSG_DEBUG("eflowCaloObject has track match with E, pt and eta " << thisTrack->e() << ", " << thisTrack->pt() << " and " << thisTrack->eta());
163 }
164 }
165
166 ATH_MSG_DEBUG("About to perform subtraction for this eflowCaloObject");
167
168 bool wasAnnihilated = false;
169
170 //First deal with non-split showers mode
172 /* Check if we can annihilate right away - true if matched cluster has only the expected energy deposit */
173 if (canAnnihilate(expectedEnergy, expectedSigma, clusterEnergy)){
174
175 wasAnnihilated = true;
176
177 std::vector<std::pair<xAOD::CaloCluster *, bool>> clusterList;
178 std::map<xAOD::CaloCluster *, double> clusterEnergyMap;
179 unsigned nCluster = thisEflowCaloObject.nClusters();
180 for (unsigned iCluster = 0; iCluster < nCluster; ++iCluster){
181 clusterList.emplace_back(thisEflowCaloObject.efRecCluster(iCluster)->getCluster(), false);
182 }
183
184 ATH_MSG_DEBUG("We are going to annihilate. ExpectedEnergy, expectedSigma and clusterEnergy are " << expectedEnergy << ", " << expectedSigma << " and " << clusterEnergy);
185 if (msgLevel(MSG::DEBUG))
186 for (auto thisPair : clusterList)
187 ATH_MSG_DEBUG("Annihilating cluster with E and eta " << thisPair.first->e() << " and " << thisPair.first->eta());
188
189 m_pfSubtractionStatusSetter.markAllTracksAnnihStatus(thisEflowCaloObject);
190
191 //before we remove all the cells, we create a list of the removed cells if in doCPData mode
192 if (m_addCPData) this->addSubtractedCells(thisEflowCaloObject, clusterList);
193
195
196 if (msgLevel(MSG::DEBUG))
197 for (auto thisPair : clusterList)
198 ATH_MSG_DEBUG("Have Annihilated cluster with E and eta " << thisPair.first->e() << " and " << thisPair.first->eta());
199
200 /* Flag all tracks in this system as subtracted */
201 for (unsigned iTrack = 0; iTrack < thisEflowCaloObject.nTracks(); ++iTrack){
202 eflowRecTrack *thisEfRecTrack = (matchedTrackList[iTrack].first)->getTrack();
203 if (!thisEfRecTrack->isSubtracted()) thisEfRecTrack->setSubtracted();
204 }
205
206 }//if can annihilate this track-cluster systems matched cluster
207 }//split shower recovery mode or regular mode where above annihilation was not triggered
208 if (m_recoverSplitShowers || !wasAnnihilated){
209
210 for (unsigned iTrack = 0; iTrack < thisEflowCaloObject.nTracks(); ++iTrack){
211
212 eflowRecTrack *thisEfRecTrack = thisEflowCaloObject.efRecTrack(iTrack);
213
214 ATH_MSG_DEBUG("About to subtract track with e, pt, eta and phi of " << thisEfRecTrack->getTrack()->e() << ", " << thisEfRecTrack->getTrack()->pt() << ", " << thisEfRecTrack->getTrack()->eta() << " and "
215 << thisEfRecTrack->getTrack()->eta());
216
217 if (!thisEfRecTrack->hasBin()) continue;
218
219 ATH_MSG_DEBUG("Have bin for this eflowCaloObject");
220
221 if (thisEfRecTrack->isInDenseEnvironment() && !m_recoverSplitShowers) continue;
222
223 ATH_MSG_DEBUG("Am not in dense environment for this eflowCaloObject");
224
225 /* Get matched cluster via Links */
226 std::vector<eflowRecCluster *> matchedClusters;
227 const std::vector<eflowTrackClusterLink *>& links = thisEfRecTrack->getClusterMatches();
228 matchedClusters.reserve(links.size());
229 for (auto* thisEFlowTrackClusterLink : links)
230 matchedClusters.push_back(thisEFlowTrackClusterLink->getCluster());
232 std::sort(matchedClusters.begin(),
233 matchedClusters.end(),
235
236 if (msgLvl(MSG::DEBUG)) {
237 for (auto* thisClus : matchedClusters)
239 "Haved matched cluster "
240 << thisClus->getCluster()->index() << " with e,pt, eta and phi of "
241 << thisClus->getCluster()->e() << ", "
242 << thisClus->getCluster()->pt() << ", "
243 << thisClus->getCluster()->eta() << " and "
244 << thisClus->getCluster()->phi() << " will be subtracted");
245 }
246
247 /* Do subtraction */
248 std::vector<std::pair<xAOD::CaloCluster *, bool>> clusterSubtractionList;
249 clusterSubtractionList.reserve(matchedClusters.size());
250 std::map<xAOD::CaloCluster *, double> clusterEnergyMap;
251 for (auto *thisEFlowRecCluster : matchedClusters){
252 xAOD::CaloCluster *thisCluster = thisEFlowRecCluster->getCluster();
253 clusterSubtractionList.emplace_back(thisCluster, false);
254 clusterEnergyMap[thisCluster] = thisCluster->e();
255 }
256
257 ATH_MSG_DEBUG("Have filled clusterSubtractionList for this eflowCaloObject");
258
259 unsigned int trackIndex = thisEfRecTrack->getTrack()->index();
260
261 //Previously we only checked this in recover split showers, but makes sense to check it in both passes.
262 auto sumClusEnergy = [](double accumulator, std::pair<xAOD::CaloCluster *, bool> thisPair){ return accumulator += thisPair.first->e();};
263 double totalClusterEnergy = std::accumulate(clusterSubtractionList.begin(),clusterSubtractionList.end(),0.0,sumClusEnergy);
264
265 /* Check if we can annihilate right away - true if matched cluster has only the expected energy deposit */
266 if(canAnnihilate(thisEfRecTrack->getEExpect(),sqrt(thisEfRecTrack->getVarEExpect()),totalClusterEnergy)){
267
268 if (msgLevel(MSG::DEBUG))
269 for (auto thisPair : clusterSubtractionList)
270 ATH_MSG_DEBUG("Annihilating cluster with E and eta " << thisPair.first->e() << " and " << thisPair.first->eta());
271
272 //before we remove all the cells, we create a list of the removed cells if in doCPData mode
273 if (m_addCPData) this->addSubtractedCells(thisEflowCaloObject, clusterSubtractionList);
274
275 Subtractor::annihilateClusters(clusterSubtractionList);
276 //Now we should mark all of these clusters as being subtracted
277 //Now need to mark which clusters were modified in the subtraction procedure
278 std::vector<std::pair<float, float>> clusterSubtractedEnergyRatios;
279 m_pfSubtractionEnergyRatioCalculator.calculateSubtractedEnergyRatiosForAnnih(clusterSubtractionList, clusterEnergyMap, clusterSubtractedEnergyRatios);
280 m_pfSubtractionStatusSetter.markSubtractionStatus(clusterSubtractionList, clusterSubtractedEnergyRatios, thisEflowCaloObject, trackIndex);
281 }
282 else
283 {
284
285 /* Subtract the track from all matched clusters */
286 m_subtractor.subtractTracksFromClusters(thisEfRecTrack, clusterSubtractionList, m_addCPData);
287
288 //recalculate total cluster energy from the clusters afer subtraction
289 totalClusterEnergy = std::accumulate(clusterSubtractionList.begin(),clusterSubtractionList.end(),0.0,sumClusEnergy);
290
291 /* Annihilate the cluster(s) if the remnant is small (i.e. below k*sigma) */
292 if (canAnnihilate(0.0,sqrt(thisEfRecTrack->getVarEExpect()), totalClusterEnergy)){
293
294 if (msgLevel(MSG::DEBUG))
295 for (auto thisPair : clusterSubtractionList){
296 ATH_MSG_DEBUG("Annihilating remnant cluster with E and eta " << thisPair.first->e() << " and " << thisPair.first->eta());
297 }
298 eflowSubtract::Subtractor::annihilateClusters(clusterSubtractionList);
299 //Now we should mark all of these clusters as being subtracted
300 std::vector<std::pair<float, float>> clusterSubtractedEnergyRatios;
301 m_pfSubtractionEnergyRatioCalculator.calculateSubtractedEnergyRatiosForAnnih(clusterSubtractionList, clusterEnergyMap, clusterSubtractedEnergyRatios);
302 m_pfSubtractionStatusSetter.markSubtractionStatus(clusterSubtractionList, clusterSubtractedEnergyRatios, thisEflowCaloObject, trackIndex);
303 }//if remove the remnant after cell by cell subtraction
304 else
305 {
306 std::vector<std::pair<float, float>> clusterSubtractedEnergyRatios;
307 m_pfSubtractionEnergyRatioCalculator.calculateSubtractedEnergyRatios(clusterSubtractionList, clusterEnergyMap, clusterSubtractedEnergyRatios);
308 m_pfSubtractionStatusSetter.markSubtractionStatus(clusterSubtractionList, clusterSubtractedEnergyRatios, thisEflowCaloObject, trackIndex);
309 }//if don't remove the remnant after cell by cell subtraction
310
311 }//if not annihilating, and instead subtracting cell by cell
312
313 ATH_MSG_DEBUG("Have subtracted charged shower for this eflowRecTrack");
314
315 /* Flag tracks as subtracted */
316 if (!thisEfRecTrack->isSubtracted()) thisEfRecTrack->setSubtracted();
317
318 }//loop over tracks in eflowCaloObject
319 }//cell by cell subtraction
320
321}
322
323bool PFUnifiedSubtractionOnlyTool::isEOverPFail(double expectedEnergy, double sigma, double clusterEnergy) const
324{
325 if ((expectedEnergy == 0) && (clusterEnergy > 0)) return false;
326 return clusterEnergy < expectedEnergy - m_consistencySigmaCut * sigma;
327}
328
329bool PFUnifiedSubtractionOnlyTool::canAnnihilate(double expectedEnergy, double sigma, double clusterEnergy) const
330{
331 return clusterEnergy - expectedEnergy < m_subtractionSigmaCut * sigma;
332}
333
334void PFUnifiedSubtractionOnlyTool::addSubtractedCells(eflowCaloObject& thisEflowCaloObject, const std::vector<std::pair<xAOD::CaloCluster *, bool> >& clusterList) const{
335
336 unsigned int numTracks = thisEflowCaloObject.nTracks();
337
338 for (unsigned int iTrack = 0; iTrack < numTracks; ++iTrack){
339 eflowRecTrack* thisTrack = thisEflowCaloObject.efRecTrack(iTrack);
340 for (const auto& thisPair : clusterList){
341 xAOD::CaloCluster* thisCluster = thisPair.first;
342 const CaloClusterCellLink* theCellLink = thisCluster->getCellLinks();
343 CaloClusterCellLink::const_iterator theCell = theCellLink->begin();
344 CaloClusterCellLink::const_iterator lastCell = theCellLink->end();
345 for (; theCell != lastCell; ++theCell) thisTrack->addSubtractedCaloCell(ElementLink<CaloCellContainer>("AllCalo",theCell.index()),theCell.weight()/numTracks);
346 }
347 }
348}
#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)
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
Handle class for reading a decoration on an object.
const T * at(size_type n) const
Access an element, as an rvalue.
size_type size() const noexcept
Returns the number of elements in the collection.
static std::unique_ptr< IPositionProvider > Get(const std::string &positionType)
Gaudi::Property< bool > m_recoverSplitShowers
Toggle whether we are recovering split showers or not.
Gaudi::Property< bool > m_isHLLHC
Toggle whether we have the HLLHC setup.
PFUnifiedSubtractionOnlyTool(const std::string &type, const std::string &name, const IInterface *parent)
bool isEOverPFail(double expectedEnergy, double sigma, double clusterEnergy) const
Gaudi::Property< bool > m_useLegacyEBinIndex
Further discussion about why this flag exists can be found in https://its.cern.ch/jira/browse/ATLJETM...
virtual void performSubtraction(const EventContext &ctx, const unsigned int &startingPoint, const unsigned int &nCaloObj, PFData &data) const
Gaudi::Property< bool > m_calcEOverP
Toggle EOverP algorithm mode, whereby no charged shower subtraction is performed.
std::unique_ptr< eflowEEtaBinnedParameters > m_binnedParameters
ToolHandle< IEFlowCellEOverPTool > m_theEOverPTool
Tool for getting e/p values and hadronic shower cell ordering principle parameters.
Gaudi::Property< double > m_consistencySigmaCut
Parameter that controls whether a track, in a track-cluster system, will be processed by the split sh...
Gaudi::Property< bool > m_addCPData
Toggle whether to decorate eflowRecTrack with additional data for Combined Performance studies.
PFSubtractionStatusSetter m_pfSubtractionStatusSetter
Gaudi::Property< double > m_subtractionSigmaCut
Parameter that controls whether to use retain remaining calorimeter energy in track-cluster system,...
ToolHandle< PFEnergyPredictorTool > m_NNEnergyPredictorTool
Tool for getting predictiing the energy using an ONNX model.
virtual StatusCode processPFlowData(const EventContext &ctx, PFData &thePFData) const override
bool canAnnihilate(double expectedEnergy, double sigma, double clusterEnergy) const
virtual StatusCode initialize() override
void addSubtractedCells(eflowCaloObject &thisEflowCaloObject, const std::vector< std::pair< xAOD::CaloCluster *, bool > > &clusterList) const
std::unique_ptr< PFMatch::TrackEtaPhiInFixedLayersProvider > m_trkpos
Track position provider to be used to preselect clusters.
PFSubtractionEnergyRatioCalculator m_pfSubtractionEnergyRatioCalculator
void simulateShowers(const EventContext &ctx, eflowCaloObjectContainer &eflowCaloObjects, const unsigned int &nCaloObj) const
Gaudi::Property< bool > m_useNNEnergy
Toggle whether we use the neural net energy.
An internal EDM object which stores information about systems of associated tracks and calorimeter cl...
void simulateShower(const EventContext &ctx, eflowLayerIntegrator *integrator, const eflowEEtaBinnedParameters *binnedParameters, const PFEnergyPredictorTool *energyP, bool useLegacyEnergyBinIndexing)
double getExpectedVariance() const
double getClusterEnergy() const
unsigned nClusters() const
const eflowRecTrack * efRecTrack(int i) const
const std::vector< std::pair< eflowTrackClusterLink *, std::pair< float, float > > > & efRecLink() const
unsigned nTracks() const
double getExpectedEnergy() const
const eflowRecCluster * efRecCluster(int i) const
Inherits from eflowEEtaBinBase.
This class calculates the LHED (Layer of Highest Energy Density) in a cluster or group of clusters.
xAOD::CaloCluster * getCluster()
This class extends the information about a xAOD::Track.
double getEExpect() const
const std::vector< eflowTrackClusterLink * > & getClusterMatches() const
bool isSubtracted() const
const xAOD::TrackParticle * getTrack() const
void addSubtractedCaloCell(ElementLink< CaloCellContainer > theCellLink, const double &weight)
bool hasBin() const
bool isInDenseEnvironment() const
double getVarEExpect() const
static void annihilateClusters(std::vector< std::pair< xAOD::CaloCluster *, bool > > &clusters)
const CaloClusterCellLink * getCellLinks() const
Get a pointer to the CaloClusterCellLink object (const version).
virtual double e() const
The total energy of the particle.
virtual double pt() const override final
The transverse momentum ( ) of the particle.
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
virtual double e() const override final
The total energy of the particle.
static std::string release
Definition computils.h:50
STL namespace.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
MsgStream & msg
Definition testRead.cxx:32