ATLAS Offline Software
egammaForwardBuilder.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 #include "egammaForwardBuilder.h"
13 
16 #include "xAODEgamma/Electron.h"
17 
19 #include "PATCore/AcceptData.h"
20 
21 #include <algorithm>
22 #include <cmath>
23 
24 namespace {
25  constexpr float cellEtaSize = 0.1;
26  constexpr float cellPhiSize = 0.1;
27 }
28 
30  ISvcLocator* pSvcLocator)
31  : AthReentrantAlgorithm(name, pSvcLocator)
32 {}
33 
35 {
36  m_CookieCutPars.maxDelEta = m_maxDelEtaCells * cellEtaSize * 0.5;
37  m_CookieCutPars.maxDelPhi = m_maxDelPhiCells * cellPhiSize * 0.5;
38  m_CookieCutPars.maxDelR2 = m_maxDelR * m_maxDelR; // Square now to avoid a slow sqrt later.
39 
40  // The data handle keys.
45  if (m_outClusterContainerCellLinkKey.key().empty()) {
47  }
49 
50  // Retrieve object quality tool.
51  if (!m_objectQualityTool.empty()) {
52  ATH_CHECK(m_objectQualityTool.retrieve());
53  }
54 
55  else {
56  m_objectQualityTool.disable();
57  }
58 
60 
61  if (
64  ) {
66  "Number of selectors doesn't match number of given fwd-electron selector names"
67  );
68 
69  return StatusCode::FAILURE;
70  }
71 
72  // Retrieve track match builder.
74 
75  ATH_MSG_DEBUG("Initialization completed successfully");
76 
77  return StatusCode::SUCCESS;
78 }
79 
81 {
82 
83  ATH_MSG_INFO("====> Forward Egamma Statistics =============");
84  ATH_MSG_INFO(" All Clusters " << m_AllClusters);
85  ATH_MSG_INFO(" Matched Clusters " << m_MatchedClusters);
86  ATH_MSG_INFO("=============================================");
87 
88  return StatusCode::SUCCESS;
89 }
90 
91 StatusCode egammaForwardBuilder::execute(const EventContext& ctx) const
92 {
93  // Create an egamma container and register it.
95  ATH_CHECK(xaodFrwd.record(
96  std::make_unique<xAOD::ElectronContainer>(),
97  std::make_unique<xAOD::ElectronAuxContainer>())
98  );
99 
100  // Create the relevant cluster output and register it.
103  ctx
104  );
105 
107  SG::WriteHandle<CaloClusterCellLinkContainer> outClusterContainerCellLink(
109  ctx
110  );
111 
112  ATH_CHECK(outClusterContainerCellLink.record(
113  std::make_unique<CaloClusterCellLinkContainer>())
114  );
115 
116  // Topo cluster container.
118 
119  // Check is only used for serial running, remove when MT scheduler used.
120  ATH_CHECK(inputClusters.isValid());
121 
122  // Calorimeter description.
123  SG::ReadCondHandle<CaloDetDescrManager> caloDetDescrMgrHandle{
125  };
126  ATH_CHECK(caloDetDescrMgrHandle.isValid());
127  const CaloDetDescrManager* calodetdescrmgr = *caloDetDescrMgrHandle;
128 
129  static const SG::AuxElement::Accessor<
130  std::vector<ElementLink<xAOD::CaloClusterContainer>>
131  > caloClusterLinks("constituentClusterLinks");
132 
133  // Prepare to create clusters.
134  EgammaRecContainer egammaRecsFwd;
135  size_t origClusterIndex = 0;
136 
137  // Loop over input cluster container and create egRecs to store the electrons.
138  for (const xAOD::CaloCluster* cluster : *inputClusters) {
139 
140  // Create links back to the original clusters.
141  std::vector<ElementLink<xAOD::CaloClusterContainer>> constituentLinks;
142 
143  // The constituent links should contain a CaloCal cluster. When not running
144  // in ITk mode this is the default for the forward clusters used by egamma
145  // so no sister link is needed to get the CaloCal. When running in ITk mode
146  // the clusters used are CaloTopoClusters so need to access the sister
147  // cluster to maintain consistency.
148  if (m_doTrackMatching) {
150  cluster->getSisterClusterLink();
151 
152  if (sisterCluster) {
153  constituentLinks.push_back(sisterCluster);
154  } else {
155  ATH_MSG_WARNING("No sister Link available");
156  }
157  } else {
158  constituentLinks.emplace_back(*inputClusters, origClusterIndex, ctx);
159  }
160 
161  const DataLink<CaloCellContainer>& cellCont =
162  cluster->getCellLinks()->getCellContainerLink();
163 
164  // Create the new cluster.
165  std::unique_ptr<xAOD::CaloCluster> newCluster =
167  egammaClusterCookieCut::cookieCut(*cluster, *calodetdescrmgr,
168  cellCont, m_CookieCutPars) :
169  std::make_unique<xAOD::CaloCluster>(*cluster);
170 
171  if (!newCluster) {
172  continue;
173  }
174 
175  caloClusterLinks(*newCluster) = constituentLinks;
176  outClusterContainer->push_back(std::move(newCluster));
177 
178  size_t index = outClusterContainer->size() - 1;
179  const ElementLink<xAOD::CaloClusterContainer> clusterLink(*outClusterContainer, index, ctx);
180  const std::vector<ElementLink<xAOD::CaloClusterContainer>> clusterLinkVector{clusterLink};
181 
182  // Now create the egamma Rec
183  egammaRecsFwd.push_back(std::make_unique<egammaRec>(clusterLinkVector));
184 
185  ++origClusterIndex;
186  }
187 
188  // Add track-cluster matching information if requested.
189  if (m_doTrackMatching) {
190  ATH_CHECK(m_trackMatchBuilder->executeRec(ctx, &egammaRecsFwd));
191  }
192 
193  auto buff_AllClusters = m_AllClusters.buffer();
194  auto buff_MatchedClusters = m_MatchedClusters.buffer();
195 
196  //Loop over the egamma Rec creating electrons
197  for (const egammaRec* egRec : egammaRecsFwd) {
198  if (!egRec) {
199  return StatusCode::FAILURE;
200  }
201 
202  ++buff_AllClusters;
203 
204  if (m_doTrackMatching && egRec->getNumberOfTrackParticles() == 0) {
205  // Later we may want to use these for so called forward photons.
206  ATH_MSG_DEBUG("EgammaRec without track particle");
207  continue;
208  }
209 
210  //common part
211  xAOD::Electron* el = xaodFrwd->push_back(std::make_unique<xAOD::Electron>());
213  el->setCaloClusterLinks(egRec->caloClusterElementLinks());
214 
215  // from here one, we need both track matching and
216  // having tracks .
217  if (m_doTrackMatching) {
218 
219  ++buff_MatchedClusters;
220  el->setTrackParticleLinks(egRec->trackParticleElementLinks());
221 
222  const xAOD::TrackParticle* trackParticle = el->trackParticle();
223  if (trackParticle) {
224  el->setCharge(trackParticle->charge());
225  } else {
226  ATH_MSG_WARNING("Forward electron without track particle, whereas"
227  " corresponding egammaRec has at least one");
228  }
229 
230  // Set DeltaEta, DeltaPhi, DeltaPhiRescaled.
231  el->setTrackCaloMatchValues(
232  egRec->deltaEta(),
233  egRec->deltaPhi(),
234  egRec->deltaPhiRescaled(),
235  egRec->deltaPhiLast()
236  );
237  }
238 
241 
242  // Apply the Forward Electron selectors.
243  for (size_t i = 0; i < m_forwardElectronIsEMSelectors.size(); ++i) {
246 
247  // Save the bool result.
248  const asg::AcceptData accept = selector->accept(ctx, el);
249  el->setPassSelection(static_cast<bool>(accept), name);
250 
251  // Save the isem.
252  el->setSelectionisEM(accept.getCutResultInverted(), "isEM" + name);
253  }
254 
255  }//end of loop over egammaRecs
256 
258  ctx,
259  outClusterContainer,
260  outClusterContainerCellLink);
261 
262  return StatusCode::SUCCESS;
263 }
264 
267  const EventContext& ctx,
269 ) const {
270  // Protection in case tool is not available return success as algorithm can run without it.
271  if (!m_objectQualityTool.isEnabled()) { return StatusCode::SUCCESS; }
272 
273  return m_objectQualityTool->execute(ctx,*eg);
274 }
275 
278 {
279  if (!m_doTrackMatching) {
280  m_trackMatchBuilder.disable();
281  return StatusCode::SUCCESS;
282  }
283 
284  if (m_trackMatchBuilder.empty()) {
285  ATH_MSG_ERROR("EMTrackMatchBuilder is empty, but track matching is enabled");
286  return StatusCode::FAILURE;
287  }
288 
289  if (m_trackMatchBuilder.retrieve().isFailure()) {
290  ATH_MSG_ERROR("Unable to retrieve " << m_trackMatchBuilder);
291  return StatusCode::FAILURE;
292  }
293 
294  return StatusCode::SUCCESS;
295 }
CaloClusterStoreHelper::finalizeClusters
static StatusCode finalizeClusters(SG::WriteHandle< CaloClusterCellLinkContainer > &h, xAOD::CaloClusterContainer *pClusterColl)
Finalize clusters (move CaloClusterCellLink to a separate container).
Definition: CaloClusterStoreHelper.cxx:64
egammaForwardBuilder::m_CookieCutPars
egammaClusterCookieCut::CookieCutPars m_CookieCutPars
Definition: egammaForwardBuilder.h:182
IAsgForwardElectronIsEMSelector.h
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CaloClusterStoreHelper::AddContainerWriteHandle
static StatusCode AddContainerWriteHandle(SG::WriteHandle< xAOD::CaloClusterContainer > &clusColl)
Creates a new xAOD::CaloClusterContainer in the given WriteHandle + CaloClusterAuxContainer and recor...
Definition: CaloClusterStoreHelper.cxx:53
ParticleTest.eg
eg
Definition: ParticleTest.py:29
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
xAOD::TrackParticle_v1::charge
float charge() const
Returns the charge.
Definition: TrackParticle_v1.cxx:151
egammaClusterCookieCut::CookieCutPars::maxDelR2
double maxDelR2
Definition: egammaClusterCookieCut.h:14
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
index
Definition: index.py:1
egammaForwardBuilder::m_forwardElectronIsEMSelectors
ToolHandleArray< IAsgForwardElectronIsEMSelector > m_forwardElectronIsEMSelectors
Handle to the selectors.
Definition: egammaForwardBuilder.h:186
egammaForwardBuilder::egammaForwardBuilder
egammaForwardBuilder(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition: egammaForwardBuilder.cxx:29
egammaClusterCookieCut::CookieCutPars::maxDelEta
double maxDelEta
Definition: egammaClusterCookieCut.h:12
CutsMETMaker::accept
StatusCode accept(const xAOD::Muon *mu)
Definition: CutsMETMaker.cxx:18
egammaForwardBuilder::m_AllClusters
Gaudi::Accumulators::Counter m_AllClusters
Definition: egammaForwardBuilder.h:179
egammaForwardBuilder::m_doCookieCutting
Gaudi::Property< bool > m_doCookieCutting
Private member flag to do cookie cutting.
Definition: egammaForwardBuilder.h:148
egammaForwardBuilder::RetrieveEMTrackMatchBuilder
StatusCode RetrieveEMTrackMatchBuilder()
Definition: egammaForwardBuilder.cxx:277
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
egammaForwardBuilder::ExecObjectQualityTool
StatusCode ExecObjectQualityTool(const EventContext &ctx, xAOD::Egamma *eg) const
Definition: egammaForwardBuilder.cxx:266
xAOD::EgammaParameters::AuthorFwdElectron
const uint16_t AuthorFwdElectron
Electron reconstructed by the Forward cluster-based algorithm.
Definition: EgammaDefs.h:30
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
egammaForwardBuilder::m_forwardElectronIsEMSelectorResultNames
Gaudi::Property< std::vector< std::string > > m_forwardElectronIsEMSelectorResultNames
Definition: egammaForwardBuilder.h:193
CaloDetDescrManager.h
Definition of CaloDetDescrManager.
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
egammaForwardBuilder::m_maxDelR
Gaudi::Property< float > m_maxDelR
Size of cone to cookie cut on FCal.
Definition: egammaForwardBuilder.h:172
egammaForwardBuilder::m_outClusterContainerCellLinkKey
SG::WriteHandleKey< CaloClusterCellLinkContainer > m_outClusterContainerCellLinkKey
Output cluster container cell links: name taken from containter name.
Definition: egammaForwardBuilder.h:134
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:62
egammaForwardBuilder::initialize
virtual StatusCode initialize() override final
Initialize method.
Definition: egammaForwardBuilder.cxx:34
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ElectronContainer.h
egammaForwardBuilder::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Execute method.
Definition: egammaForwardBuilder.cxx:91
lumiFormat.i
int i
Definition: lumiFormat.py:85
CaloCluster.h
ElectronAuxContainer.h
egammaClusterCookieCut::cookieCut
std::unique_ptr< xAOD::CaloCluster > cookieCut(const xAOD::CaloCluster &cluster, const CaloDetDescrManager &mgr, const DataLink< CaloCellContainer > &cellCont, const egammaClusterCookieCut::CookieCutPars &pars)
Definition: egammaClusterCookieCut.cxx:28
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
egammaForwardBuilder::m_doTrackMatching
Gaudi::Property< bool > m_doTrackMatching
Private member flag to do the track matching.
Definition: egammaForwardBuilder.h:140
egammaClusterCookieCut::CookieCutPars::maxDelPhi
double maxDelPhi
Definition: egammaClusterCookieCut.h:13
egammaForwardBuilder::m_maxDelPhiCells
Gaudi::Property< int > m_maxDelPhiCells
Size of maximum search window in phi.
Definition: egammaForwardBuilder.h:164
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
EMFourMomBuilder.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
AcceptData.h
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
egammaForwardBuilder::m_outClusterContainerKey
SG::WriteHandleKey< xAOD::CaloClusterContainer > m_outClusterContainerKey
Output cluster container.
Definition: egammaForwardBuilder.h:126
egammaForwardBuilder.h
egammaForwardBuilder::finalize
virtual StatusCode finalize() override final
Finalize method.
Definition: egammaForwardBuilder.cxx:80
EMFourMomBuilder::calculate
void calculate(xAOD::Electron &electron)
Definition: EMFourMomBuilder.cxx:69
CookieCutterHelpers.h
egammaForwardBuilder::m_trackMatchBuilder
ToolHandle< IEMTrackMatchBuilder > m_trackMatchBuilder
Tool to perform track-cluster matching.
Definition: egammaForwardBuilder.h:94
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
IegammaBaseTool.h
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
xAOD::Electron_v1
Definition: Electron_v1.h:34
CaloClusterStoreHelper.h
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
python.selector.AtlRunQuerySelectorLhcOlc.selector
selector
Definition: AtlRunQuerySelectorLhcOlc.py:610
CaloDetDescrManager
This class provides the client interface for accessing the detector description information common to...
Definition: CaloDetDescrManager.h:473
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
egammaForwardBuilder::m_topoClusterKey
SG::ReadHandleKey< xAOD::CaloClusterContainer > m_topoClusterKey
Input topo cluster type.
Definition: egammaForwardBuilder.h:102
CaloClusterContainer.h
egammaRec
Definition: egammaRec.h:31
Electron.h
egammaForwardBuilder::m_caloDetDescrMgrKey
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloDetDescrMgrKey
Calorimeter description.
Definition: egammaForwardBuilder.h:110
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
asg::AcceptData
Definition: AcceptData.h:30
egammaForwardBuilder::m_MatchedClusters
Gaudi::Accumulators::Counter m_MatchedClusters
Definition: egammaForwardBuilder.h:180
egammaForwardBuilder::m_objectQualityTool
ToolHandle< IegammaOQFlagsBuilder > m_objectQualityTool
Tool to perform object quality.
Definition: egammaForwardBuilder.h:86
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
egammaForwardBuilder::m_maxDelEtaCells
Gaudi::Property< int > m_maxDelEtaCells
Size of maximum search window in eta.
Definition: egammaForwardBuilder.h:156
egammaForwardBuilder::m_electronOutputKey
SG::WriteHandleKey< xAOD::ElectronContainer > m_electronOutputKey
Output electron container.
Definition: egammaForwardBuilder.h:118