ATLAS Offline Software
TauThinningAlg.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 "TauThinningAlg.h"
6 #include "StoreGate/ReadHandle.h"
9 
14 {
15  ATH_CHECK( m_taus.initialize(m_streamName) );
16  ATH_CHECK( m_tauTracks.initialize(m_streamName) );
17  ATH_CHECK( m_neutralPFOs.initialize(m_streamName) );
18  ATH_CHECK( m_pi0clusters.initialize(m_streamName) );
20  ATH_CHECK( m_finalPi0s.initialize(m_streamName) );
21  ATH_CHECK( m_shotPFOs.initialize(m_streamName) );
22  ATH_CHECK( m_shotclusters.initialize(m_streamName) );
24  ATH_CHECK( m_hadronicPFOs.initialize(m_streamName) );
28 
29  return StatusCode::SUCCESS;
30 }
31 
36 StatusCode TauThinningAlg::execute (const EventContext& ctx) const
37 {
39  taus.thinAll();
40 
42  tauTracks.thinAll();
43 
45  neutralPFOs.thinAll();
46 
48  pi0clusters.thinAll();
49 
51  pi0CellLinks.thinAll();
52 
54  shotPFOs.thinAll();
55 
57  hadronicPFOs.thinAll();
58 
60  secondaryVertices.thinAll();
61 
63  cells.thinAll();
64 
66  tauCellLinks.thinAll();
67 
68  // The three following containers didn't exist in r21.
69  // Make them optional, so we won't crash processing a r21 ESD.
70 
71  std::optional<SG::ThinningHandle<xAOD::ParticleContainer> > finalPi0sOpt;
72  if (evtStore()->contains<xAOD::ParticleContainer> (m_finalPi0s.key())) {
73  finalPi0sOpt.emplace (m_finalPi0s, ctx);
74  finalPi0sOpt->thinAll();
75  }
76 
77  std::optional<SG::ThinningHandle<xAOD::CaloClusterContainer> > shotclustersOpt;
78  if (evtStore()->contains<xAOD::CaloClusterContainer> (m_shotclusters.key())) {
79  shotclustersOpt.emplace (m_shotclusters, ctx);
80  shotclustersOpt->thinAll();
81  }
82 
83  std::optional<SG::ThinningHandle<CaloClusterCellLinkContainer> > shotCellLinksOpt;
84  if (evtStore()->contains<CaloClusterCellLinkContainer> (m_shotCellLinks.key())) {
85  shotCellLinksOpt.emplace (m_shotCellLinks, ctx);
86  shotCellLinksOpt->thinAll();
87  }
88 
89  static const SG::ConstAccessor<char> acc_passThinning("passThinning");
90 
91  for (const xAOD::TauJet* tau : *taus) {
92 
93  if (!acc_passThinning(*tau)) continue;
94 
95  // keep tau
96  taus.keep(tau->index());
97 
98  // keep tau tracks
99  for (const xAOD::TauTrack* track : tau->allTracks()) {
100  tauTracks.keep(track->index());
101  }
102 
103  // keep tau cluster cell links and cells within 0.2 of the tau axis
104  TLorentzVector tauAxis = tauRecTools::getTauAxis(*tau, m_doVertexCorrection);
105  const xAOD::Vertex* tauVertex = tau->vertex();
106 
107  for (const xAOD::IParticle* particle : tau->clusters()) {
108  const xAOD::CaloCluster* cluster = static_cast<const xAOD::CaloCluster*>(particle);
109  TLorentzVector clusterP4 = cluster->p4();
110 
111  // correct the four momentum to point to the tau vertex
112  if (tauVertex) {
113  xAOD::CaloVertexedTopoCluster vertexedCluster(*cluster, tauVertex->position());
114  clusterP4 = vertexedCluster.p4();
115  }
116 
117  if (clusterP4.DeltaR(tauAxis) > 0.2) continue;
118 
119  const CaloClusterCellLink* cellLinks = cluster->getCellLinks();
120  if (!cellLinks) {
121  ATH_MSG_WARNING( "Skipping cluster without cell links." );
122  continue;
123  }
124 
125  // cluster cell links
126  CaloClusterCellLinkContainer::const_iterator cellLinks_it = std::find(tauCellLinks->begin(), tauCellLinks->end(), cellLinks);
127  if(cellLinks_it != tauCellLinks->end()) {
128  size_t link_index = std::distance(tauCellLinks->begin(), cellLinks_it);
129  tauCellLinks.keep(link_index);
130  }
131  else {
132  ATH_MSG_WARNING( "Could not find cluster cell link in " << m_tauCellLinks.key() << ", skipping cluster." );
133  continue;
134  }
135 
136  // cells
139  for (; it != end; ++it) {
140  if (it.index() >= cells->size()) {
141  ATH_MSG_WARNING( "Cell index " << it.index() << " is larger than the number of cells in " << m_cells.key() << " (" << cells->size() << ")" );
142  continue;
143  }
144  cells.keep (it.index());
145  }
146  }
147 
148  // keep neutral PFOs, pi0 clusters, cell links and cells
149  for(size_t i=0; i<tau->nNeutralPFOs(); i++) {
150  // neutral PFOs
151  neutralPFOs.keep(tau->neutralPFO(i)->index());
152 
153  // pi0 clusters
154  const xAOD::CaloCluster* cluster = tau->neutralPFO(i)->cluster(0);
155  pi0clusters.keep(cluster->index());
156 
157  // pi0 cell links
158  const CaloClusterCellLink* cellLinks = cluster->getCellLinks();
159  CaloClusterCellLinkContainer::const_iterator cellLinks_it = std::find(pi0CellLinks->begin(), pi0CellLinks->end(), cellLinks);
160  if(cellLinks_it != pi0CellLinks->end()) {
161  size_t link_index = std::distance(pi0CellLinks->begin(), cellLinks_it);
162  pi0CellLinks.keep(link_index);
163  }
164  else {
165  ATH_MSG_WARNING( "Could not find cluster cell link in " << m_pi0CellLinks.key() << ", won't be saved in xAOD." );
166  }
167 
168  // pi0 cells
171  for (; it != end; ++it) {
172  if (it.index() >= cells->size()) {
173  ATH_MSG_WARNING( "Cell index " << it.index() << " is larger than the number of cells in " << m_cells.key() << " (" << cells->size() << ")" );
174  continue;
175  }
176  cells.keep (it.index());
177  }
178  }
179 
180  // keep final pi0s
181  if (finalPi0sOpt) {
182  for(size_t i=0; i<tau->nPi0s(); i++) {
183  finalPi0sOpt->keep(tau->pi0(i)->index());
184  }
185  }
186 
187  // keep shot PFOs, clusters, cell links and cells
188  for(size_t i=0; i<tau->nShotPFOs(); i++) {
189  // shot PFOs
190  shotPFOs.keep(tau->shotPFO(i)->index());
191 
192  // shot clusters
193  const xAOD::CaloCluster* cluster = tau->shotPFO(i)->cluster(0);
194  if (!cluster) continue;
195  if (shotclustersOpt) {
196  shotclustersOpt->keep(cluster->index());
197  }
198 
199  // shot cell links
200  const CaloClusterCellLink* cellLinks = cluster->getCellLinks();
201  if (shotCellLinksOpt) {
202  CaloClusterCellLinkContainer::const_iterator cellLinks_it = std::find((*shotCellLinksOpt)->begin(), (*shotCellLinksOpt)->end(), cellLinks);
203  if(cellLinks_it != (*shotCellLinksOpt)->end()) {
204  size_t link_index = std::distance((*shotCellLinksOpt)->begin(), cellLinks_it);
205  shotCellLinksOpt->keep(link_index);
206  }
207  else {
208  ATH_MSG_WARNING( "Could not find cluster cell link in " << m_shotCellLinks.key() << ", won't be saved in xAOD." );
209  }
210  }
211 
212  // shot cells
215  for (; it != end; ++it) {
216  if (it.index() >= cells->size()) {
217  ATH_MSG_WARNING( "Cell index " << it.index() << " is larger than the number of cells in " << m_cells.key() << " (" << cells->size() << ")" );
218  continue;
219  }
220  cells.keep (it.index());
221  }
222  }
223 
224  // keep hadronic PFOs
225  for(size_t i=0; i<tau->nHadronicPFOs(); i++) {
226  hadronicPFOs.keep(tau->hadronicPFO(i)->index());
227  }
228 
229  // keep secondary vertex when present
230  if (tau->secondaryVertex() != nullptr) {
231  secondaryVertices.keep(tau->secondaryVertex()->index());
232  }
233  }
234 
235  return StatusCode::SUCCESS;
236 }
SG::ThinningHandleBase::thinAll
void thinAll()
Mark that all elements should be thinned away.
Definition: ThinningHandleBase.cxx:136
RunTileCalibRec.cells
cells
Definition: RunTileCalibRec.py:271
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
xAOD::CaloVertexedClusterBase::p4
virtual FourMom_t p4() const final
The full 4-momentum of the particle.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloVertexedClusterBase.h:88
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
TauThinningAlg::initialize
virtual StatusCode initialize() override
Gaudi initialize method.
Definition: TauThinningAlg.cxx:13
TauThinningAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Execute the algorithm.
Definition: TauThinningAlg.cxx:36
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
TauThinningAlg::m_finalPi0s
SG::ThinningHandleKey< xAOD::ParticleContainer > m_finalPi0s
Definition: TauThinningAlg.h:75
ThinningHandle.h
Handle for requesting thinning for a data object.
tauRecTools::getTauAxis
TLorentzVector getTauAxis(const xAOD::TauJet &tau, bool doVertexCorrection=true)
Return the four momentum of the tau axis The tau axis is widely used to select clusters and cells in ...
Definition: Reconstruction/tauRecTools/Root/HelperFunctions.cxx:33
TauThinningAlg::m_neutralPFOs
SG::ThinningHandleKey< xAOD::PFOContainer > m_neutralPFOs
Definition: TauThinningAlg.h:63
skel.it
it
Definition: skel.GENtoEVGEN.py:396
TauThinningAlg::m_shotPFOs
SG::ThinningHandleKey< xAOD::PFOContainer > m_shotPFOs
Definition: TauThinningAlg.h:79
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
SG::ConstAccessor< char >
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
SG::ThinningHandleKey::initialize
StatusCode initialize(const std::string &stream, const std::string &qualifier, bool used=true)
Should be called during the initialize phase.
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
TauThinningAlg::m_streamName
StringProperty m_streamName
Definition: TauThinningAlg.h:51
TauThinningAlg::m_cells
SG::ThinningHandleKey< CaloCellContainer > m_cells
Definition: TauThinningAlg.h:99
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
SG::ThinningHandleBase::keep
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
Definition: ThinningHandleBase.cxx:68
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
TauThinningAlg::m_doVertexCorrection
Gaudi::Property< bool > m_doVertexCorrection
Definition: TauThinningAlg.h:47
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TauThinningAlg::m_tauCellLinks
SG::ThinningHandleKey< CaloClusterCellLinkContainer > m_tauCellLinks
Definition: TauThinningAlg.h:103
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
TauThinningAlg::m_shotclusters
SG::ThinningHandleKey< xAOD::CaloClusterContainer > m_shotclusters
Definition: TauThinningAlg.h:83
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TauThinningAlg::m_pi0CellLinks
SG::ThinningHandleKey< CaloClusterCellLinkContainer > m_pi0CellLinks
Definition: TauThinningAlg.h:71
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
TauThinningAlg::m_shotCellLinks
SG::ThinningHandleKey< CaloClusterCellLinkContainer > m_shotCellLinks
Definition: TauThinningAlg.h:87
xAOD::CaloCluster_v1::getCellLinks
const CaloClusterCellLink * getCellLinks() const
Get a pointer to the CaloClusterCellLink object (const version)
Definition: CaloCluster_v1.cxx:905
xAOD::CaloCluster_v1::p4
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition: CaloCluster_v1.cxx:465
TauThinningAlg::m_tauTracks
SG::ThinningHandleKey< xAOD::TauTrackContainer > m_tauTracks
Definition: TauThinningAlg.h:59
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::TauTrack_v1
Definition: TauTrack_v1.h:27
HelperFunctions.h
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::CaloVertexedTopoCluster
Evaluate cluster kinematics with a different vertex / signal state.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloVertexedTopoCluster.h:38
TauThinningAlg::m_pi0clusters
SG::ThinningHandleKey< xAOD::CaloClusterContainer > m_pi0clusters
Definition: TauThinningAlg.h:67
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
TauThinningAlg::m_secondaryVertices
SG::ThinningHandleKey< xAOD::VertexContainer > m_secondaryVertices
Definition: TauThinningAlg.h:95
TauThinningAlg.h
TauThinningAlg::m_hadronicPFOs
SG::ThinningHandleKey< xAOD::PFOContainer > m_hadronicPFOs
Definition: TauThinningAlg.h:91
TauThinningAlg::m_taus
SG::ThinningHandleKey< xAOD::TauJetContainer > m_taus
Definition: TauThinningAlg.h:55