ATLAS Offline Software
CaloCell2ClusterMapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //-----------------------------------------------------------------------
6 // File and Version Information:
7 // $Id: CaloCell2ClusterMapper.cxx,v 1.6 2009-04-18 02:56:18 ssnyder Exp $
8 //
9 // Description: see CaloCell2ClusterMapper.h
10 //
11 // Environment:
12 // Software developed for the ATLAS Detector at CERN LHC
13 //
14 // Author List:
15 // Sven Menke
16 //
17 //-----------------------------------------------------------------------
18 
19 //-----------------------
20 // This Class's Header --
21 //-----------------------
22 #include "CaloCell2ClusterMapper.h"
23 
24 //----------------------------
25 // This Class's Base Header --
26 //----------------------------
27 
28 //---------------
29 // C++ Headers --
30 //---------------
31 #include <list>
32 #include <iterator>
33 #include <sstream>
36 #include "CaloEvent/CaloClusterContainer.h"
37 #include "AthAllocators/DataPool.h"
39 
40 
41 //###############################################################################
43  ISvcLocator* pSvcLocator)
44  : AthReentrantAlgorithm(name, pSvcLocator),
45  m_mapOutputKey(""),
46  m_clusterKey("")
47 {
48 
49  // Name of Cell2Cluster Map to be registered in TDS
50  declareProperty("MapOutputName",m_mapOutputKey);
51 
52  // Name of CaloClusterContainer to use as input
53  declareProperty("ClustersName",m_clusterKey);
54 }
55 
56 //###############################################################################
57 
59 = default;
60 
61 //###############################################################################
62 
64  return StatusCode::SUCCESS;
65 }
66 
67 //###############################################################################
68 
70 {
71  return StatusCode::SUCCESS;
72 }
73 
74 //###############################################################################
75 
76 StatusCode CaloCell2ClusterMapper::execute(const EventContext& ctx) const {
77 
78  // make a DataVector of Navigable CaloClusterContainer
79  ATH_MSG_DEBUG(" Recording Cell2Cluster Map " << m_mapOutputKey.key());
80 
82  ATH_CHECK( cell2ClusterMap.record(std::make_unique<CaloCell2ClusterMap>
83  (SG::VIEW_ELEMENTS)) );
84 
85  const CaloCell_ID* calo_id = nullptr;
86  ATH_CHECK(detStore()->retrieve(calo_id,"CaloCell_ID"));
87 
88  // resize it to total range of IdentifierHash for all calos
89  Identifier::size_type maxRange = calo_id->calo_cell_hash_max();
90  cell2ClusterMap->resize(maxRange);
91 
93 
94 
95  ATH_MSG_DEBUG("CaloCluster container: " << clusColl.name()
96  <<" contains " << clusColl->size() << " clusters");
97 
98  std::vector<double> weightedESum;
99  weightedESum.resize(clusColl->size());
100 
101  std::vector<int> numberOfCells;
102  numberOfCells.resize(clusColl->size());
103 
104  DataPool<Navigable<CaloClusterContainer> > navPool(maxRange);
105 
106  // loop over cluster collection and add each cluster to the map for
107  // each member cell
108  unsigned int iClus = 0;
109  for (const CaloCluster* clus : *clusColl) {
110  // loop over its cell members
111  CaloCluster::cell_iterator cellIter = clus->cell_begin();
112  CaloCluster::cell_iterator cellIterEnd = clus->cell_end();
113  for (; cellIter != cellIterEnd; cellIter++) {
114  // look up the IdentifierHash for the current cell
115  IdentifierHash myHashId = calo_id->calo_cell_hash((*cellIter)->ID());
116  // get the existing? Navigable for this cell
117  Navigable<CaloClusterContainer> *theNav = (*cell2ClusterMap)[myHashId];
118  if (!theNav) {
119  // create a new Navigable if it doesn't exist
120  theNav = navPool.nextElementPtr();
121  theNav->removeAll();
122  // and store it in the vector
123  (*cell2ClusterMap)[myHashId] = theNav;
124  }
125  // add the current cluster to the list of clusters for this cell
126  theNav->putElement(clusColl.cptr(), clus);
127  // add the energy*weight for this cell to the weightedESum
128  //if ( messageService()->outputLevel(name()) <= MSG::DEBUG) {
129  if (msgLvl(MSG::DEBUG)) {
130  weightedESum[iClus] += (clus->getCellWeight(*cellIter))
131  * ((*cellIter)->energy());
132  numberOfCells[iClus]++;
133  }
134  }
135  }
136 
137  // compare weighted sums per cluster from the map with cluster energies
138  if (msgLvl(MSG::DEBUG)) {
139  double epsilon = 0.001;
140  unsigned int iClus = 0;
141  for (const CaloCluster* clus : *clusColl) {
142  if (fabs(weightedESum[iClus] - clus->e())
143  > epsilon * fabs(clus->e()))
144  {
145  //FIXME: WARNING printed only if msglevel <=
146  ATH_MSG_WARNING( "CaloCluster has E = " << clus->e()
147  << " MeV, while weighted sum of cells gives E = "
148  << weightedESum[iClus]
149  << " MeV! Complain with the creator of the CaloClusterContainer named "
150  << m_clusterKey.key() );
151  } else {
152  ATH_MSG_DEBUG( "CaloCluster and weighted sum of cell members have E = "
153  << clus->e() << " MeV, good!" );
154  }
155  if (numberOfCells[iClus] != static_cast<int>(clus->getNumberOfCells())) {
156  //FIXME: WARNING printed only if msglevel <=
157  ATH_MSG_WARNING( "CaloCluster has N = "
158  << clus->getNumberOfCells() << " cells, while N = "
159  << numberOfCells[iClus]
160  << " cells are in the map! Something is wrong with the creation of the Map!" );
161  } else {
162  ATH_MSG_DEBUG( "CaloCluster with N = "
163  << clus->getNumberOfCells() << " cells is in the map, good!" );
164  }
165  } //end loop over clusters
166  } //end if DEBUG
167 
168  // print the Map
169  if (msgLvl(MSG::VERBOSE)) {
170  for (unsigned int iHash = 0; iHash < maxRange; iHash++) {
171  // get the existing? Navigable for this cell
172  Navigable<CaloClusterContainer> *theNav = (*cell2ClusterMap)[iHash];
173  if (theNav) {
174  msg(MSG::VERBOSE) << "CaloCell with hashID " << iHash << " belongs to";
175  for (const CaloCluster* pClus : *theNav) {
176  // the next line assumes all cells of the cluster belong to the same
177  // cell container ...
178  const CaloCellContainer *cellColl = pClus->getCellContainer(
179  *(pClus->cell_begin()));
180  const CaloCell* pCell = cellColl->findCell(iHash);
181  if (!pCell) {
182  // need to find the cell in clusters member list because the
183  // cluster is made of more than one cell container
184  CaloCluster::cell_iterator cellIter = pClus->cell_begin();
185  CaloCluster::cell_iterator cellIterEnd = pClus->cell_end();
186  for (; cellIter != cellIterEnd; cellIter++) {
187  pCell = (*cellIter);
188  // look up the IdentifierHash for the current cell
189  IdentifierHash myHashId = calo_id->calo_cell_hash(pCell->ID());
190  if (((unsigned int) myHashId) == iHash)
191  break;
192  else
193  pCell = nullptr;
194  }
195  }
196  if (pCell)
197  msg() << " Cluster with E_clus = " << pClus->e()
198  << " MeV with weight w_cell = " << pClus->getCellWeight(pCell)
199  << " ";
200  }
201  msg() << endmsg;
202  }
203  }
204  }
205 
206  return StatusCode::SUCCESS;
207 }
208 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
CaloCell_Base_ID::calo_cell_hash
IdentifierHash calo_cell_hash(const Identifier cellId) const
create hash id from 'global' cell id
Navigable::putElement
void putElement(const CONT *objectContainer, const constituent_type *constituentObject, const RPAR &objectParameter=RPAR(), size_t sizeHint=0)
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
Navigable::removeAll
bool removeAll()
Definition: Navigable.h:237
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
CaloCell2ClusterMapper::m_clusterKey
SG::ReadHandleKey< CaloClusterContainer > m_clusterKey
Name of the CaloClusterContainer to use.
Definition: CaloCell2ClusterMapper.h:52
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.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
CaloCell2ClusterMapper::m_mapOutputKey
SG::WriteHandleKey< CaloCell2ClusterMap > m_mapOutputKey
Name of the CaloCell2ClusterMap in StoreGate.
Definition: CaloCell2ClusterMapper.h:45
CaloCell_ID.h
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
CaloCell2ClusterMapper::finalize
virtual StatusCode finalize() override
Definition: CaloCell2ClusterMapper.cxx:69
CaloCluster::cell_iterator
CaloCompositeCellBase< CaloClusterNavigable >::cell_iterator cell_iterator
Iterator on CaloCell s.
Definition: Calorimeter/CaloEvent/CaloEvent/CaloCluster.h:115
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
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
CaloCell2ClusterMap.h
CaloCluster
Principal data class for CaloCell clusters.
Definition: Calorimeter/CaloEvent/CaloEvent/CaloCluster.h:79
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CaloCell_ID
Helper class for offline cell identifiers.
Definition: CaloCell_ID.h:34
CaloCell2ClusterMapper::~CaloCell2ClusterMapper
virtual ~CaloCell2ClusterMapper()
DataPool::nextElementPtr
pointer nextElementPtr()
obtain the next available element in pool by pointer pool is resized if its limit has been reached On...
CaloCell2ClusterMapper::initialize
virtual StatusCode initialize() override
Definition: CaloCell2ClusterMapper.cxx:63
DataPool.h
CaloCellContainer::findCell
const CaloCell * findCell(const IdentifierHash theHash) const
fast find method given identifier hash.
Definition: CaloCellContainer.cxx:345
DataVector::resize
void resize(size_type sz)
Resizes the collection to the specified number of elements.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
errorcheck.h
Helpers for checking error return status codes and reporting errors.
CaloCell2ClusterMapper::CaloCell2ClusterMapper
CaloCell2ClusterMapper(const std::string &name, ISvcLocator *pSvcLocator)
Definition: CaloCell2ClusterMapper.cxx:42
CaloCell::ID
Identifier ID() const
get ID (from cached data member) non-virtual and inline for fast access
Definition: CaloCell.h:279
CaloCellContainer
Container class for CaloCell.
Definition: CaloCellContainer.h:55
Navigable
Navigable template generalization to handle navigation.
Definition: Navigable.h:93
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
CaloCell
Data object for each calorimeter readout cell.
Definition: CaloCell.h:57
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
DEBUG
#define DEBUG
Definition: page_access.h:11
AthCommonMsg< Gaudi::Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
CaloCell2ClusterMapper::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: CaloCell2ClusterMapper.cxx:76
DataPool
a typed memory pool that saves time spent allocation small object. This is typically used by containe...
Definition: DataPool.h:47
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
Identifier::size_type
unsigned long long size_type
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:29
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
CaloCell2ClusterMapper.h
CaloCell_Base_ID::calo_cell_hash_max
size_type calo_cell_hash_max(void) const
cell 'global' hash table max size