ATLAS Offline Software
CaloRingerReaderUtils.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef CALORINGERTOOLS_CALORINGERREADERUTILS_ICC
6 #define CALORINGERTOOLS_CALORINGERREADERUTILS_ICC
7 
8 
9 // Kernel includes:
10 #include "GaudiKernel/StatusCode.h"
11 #include "GaudiKernel/ToolHandle.h"
12 
13 // Interface includes:
14 #include "CaloRingerTools/ICaloRingsBuilder.h"
15 
16 // xAOD includes:
17 #include "xAODCaloEvent/CaloClusterContainer.h"
18 #include "xAODCaloRings/CaloRingsContainer.h"
19 #include "xAODCaloRings/RingSetContainer.h"
20 #include "xAODCaloRings/CaloRingsAuxContainer.h"
21 #include "xAODCaloRings/RingSetAuxContainer.h"
22 #include "xAODCaloRings/tools/getCaloRingsDecorator.h"
23 
24 
25 #include "CaloRingerTools/ICaloRingerInputReader.h"
26 
27 namespace Ringer {
28 
29 // =====================================================================================
30 template<typename container_t>
31 StatusCode BuildCaloRingsFctor<container_t>::prepareToLoopFor( std::size_t nParticles ) {
32  // Call overridden method
33  ATH_CHECK( base_t::prepareToLoopFor( nParticles ) );
34  // Make the decorator key
35  m_decor = new decor_t( m_decorKey );
36  return StatusCode::SUCCESS;
37 }
38 
39 // =====================================================================================
40 template<typename container_t>
41 void BuildCaloRingsFctor<container_t>::checkRelease() {
42  // Call overridden method
43  base_t::checkRelease();
44  if ( m_part_counter == m_part_size ) {
45  delete m_decor; m_decor = nullptr;
46  }
47 }
48 
49 // =====================================================================================
50 template<typename container_t>
51 void BuildCaloRingsFctor<container_t>::displayLoopingMessage() const {
52  if (msgLvl(MSG::DEBUG)) {
53  std::string str = ((m_particle_has_cluster)?"(with cluster access)":
54  "(without cluster access)");
55  msg() << MSG::DEBUG << " ---------- Reading "
56  << m_particle_name << " " << str << " candidate "
57  << this->m_part_counter << "/" << this->m_part_size
58  << " ----------- " << endmsg;
59  }
60 }
61 
62 
63 // =====================================================================================
64 template< typename container_t >
65 void BuildCaloRingsFctor<container_t>::operator() ( const particle_t *part )
66 {
67 
68  auto ringsELReader = xAOD::getCaloRingsReader();
69  const xAOD::CaloRingsLinks *caloRingsLinks(nullptr);
70 
71  try {
72  caloRingsLinks = &(ringsELReader(*part));
73  } catch ( const std::exception &e) {
74  ATH_MSG_DEBUG("Couldn't retrieve CaloRingsELVec. Reason: " << e.what());
75  }
76 
77  incrementCounter(); displayLoopingMessage();
78 
79  // Retrieve clusters element links:
80  const auto &clustersLinks = part->caloClusterLinks();
81 
82  // Make CaloRings ElementLink Holder:
83  xAOD::CaloRingsLinks clRingsLinks;
84 
85  if ( m_particle_has_cluster ) {
86  // We'll assume to have one CaloRings for each cluster, reserve memory for
87  // it:
88  clRingsLinks.reserve( clustersLinks.size() );
89 
90  unsigned int cl_counter = 0;
91 
92  // Loop over them to build CaloRings:
93  for ( const auto& clusLink : clustersLinks) {
94 
95  ATH_MSG_DEBUG("Reading particle cluster " << ++cl_counter << "/"
96  << clustersLinks.size());
97 
98  // This cluster CaloRings:
99  ElementLink<xAOD::CaloRingsContainer> ringsEL;
100 
101  if (this->m_builder->execute( **clusLink, ringsEL ).isFailure()){
102  throw std::runtime_error("Error while executing CaloRings builder.");
103  }
104 
105  // Push it back:
106  clRingsLinks.push_back( ringsEL );
107 
108  }
109 
110  } else {
111 
112  // The CaloRings ElementLink:
113  ElementLink<xAOD::CaloRingsContainer> ringsEL;
114 
115  if (m_builder->execute( *part, ringsEL ).isFailure()){
116  throw std::runtime_error("Error while executing CaloRings builder.");
117  }
118 
119  // Push it back:
120  clRingsLinks.push_back( ringsEL );
121  }
122 
123  if(!caloRingsLinks){
124  // Decorate particle with CaloRings:
125  m_decor->operator()( *part ) = clRingsLinks;
126  }
127 
128  checkRelease();
129 }
130 
131 // =====================================================================================
132 template<typename container_t>
133 StatusCode BuildCaloRingsFctor<container_t>::initialize()
134 {
135  ATH_CHECK( m_decorKey.initialize() );
136  m_decorKey.owner()->declare(m_decorKey);
137  return StatusCode::SUCCESS;
138 }
139 
140 
141 } // namespace Ringer
142 
143 #endif // CALORINGERTOOLS_RINGERREADERUTILS_ICC
144 
145 // vim: filetype=cpp: