ATLAS Offline Software
LArHitMerger.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 // algorithm to merge LArHit from different subevents from pileup service to
7 // a single event, assuming all subevents are at the same time and neglecting the
8 // individual hit times
9 // This is aimed to run at first stage for very large luminosity pileup
10 
11 #include "LArHitMerger.h"
13 #include "LArSimEvent/LArHit.h"
15 #include "CaloIdentifier/LArID.h"
17 #include "GaudiKernel/MsgStream.h"
18 //#include "LArDigitization/LArHitEMap.h"
20 
21 // Pile up
23 
24 LArHitMerger::LArHitMerger(const std::string& name, ISvcLocator* pSvcLocator)
25  : AthAlgorithm(name, pSvcLocator),
26  p_mergeSvc(nullptr),
27  m_SubDetectors ("LAr_All"),
28  m_EmBarrelHitContainerName ("LArHitEMB"),
29  m_EmEndCapHitContainerName ("LArHitEMEC"),
30  m_HecHitContainerName ("LArHitHEC"),
31  m_ForWardHitContainerName ("LArHitFCAL"),
32  m_larem_id(nullptr),m_larhec_id(nullptr),m_larfcal_id(nullptr)
33 {
34  //
35  // ........ declare the private data as properties
36  //
37  declareProperty("SubDetectors",m_SubDetectors,"subdetector selection");
38  declareProperty("EmBarrelHitContainerName",m_EmBarrelHitContainerName,"Hit container name for EMB");
39  declareProperty("EmEndCapHitContainerName",m_EmEndCapHitContainerName,"Hit container name for EMEC");
40  declareProperty("HecHitContainerName",m_HecHitContainerName,"Hit container name for HEC");
41  declareProperty("ForWardHitContainerName",m_ForWardHitContainerName,"Hit container name for FCAL");
42 }
43 
44 
45 LArHitMerger::~LArHitMerger() = default;
46 
47 
49 {
50 //
51 // ......... make the Sub-detector flag vector
52 //
53 
54  for (int i=0; i < LArIndex::NUMDET ; i++)
55  {
56  m_SubDetFlag.push_back(false);
57  }
58 
59 //
60 // ......... make the digit container name list
61 //
62 
63  if ( m_SubDetectors == "LAr_All" )
64  {
68 
72 
76 
80  }
81  else if ( m_SubDetectors == "LAr_Em" )
82  {
86 
90  }
91  else if ( m_SubDetectors == "LAr_EmBarrel" )
92  {
96  }
97  else if ( m_SubDetectors == "LAr_EmEndCap" )
98  {
102  }
103  else if ( m_SubDetectors == "LAr_HEC" )
104  {
108  }
109  else if ( m_SubDetectors == "LAr_Fcal" )
110  {
114  }
115  else if (m_SubDetectors == "LAr_EndCap")
116  {
120 
124 
128  }
129  else
130  {
131  ATH_MSG_ERROR("Invalid SubDetector propertie");
132  return(StatusCode::FAILURE);
133  }
134 
135 
136 //
137 // locate the PileUpMergeSvc and initialize our local ptr
138 //
139  if (!(service("PileUpMergeSvc", p_mergeSvc)).isSuccess() ||
140  nullptr == p_mergeSvc) {
141  ATH_MSG_ERROR("Could not find PileUpMergeSvc");
142  return StatusCode::FAILURE;
143  }
144  else
145  {
146  ATH_MSG_INFO("PileUpMergeSvc successfully initialized");
147  }
148 
149 
150  //retrieve ID helpers
151  const CaloIdManager* caloIdMgr = nullptr;
152  StatusCode sc = detStore()->retrieve(caloIdMgr);
153  if (sc.isFailure()) {
154  ATH_MSG_ERROR("Unable to retrieve CaloIdManager from DetectoreStore");
155  return StatusCode::FAILURE;
156  }
157  m_larem_id = caloIdMgr->getEM_ID();
158  m_larhec_id = caloIdMgr->getHEC_ID();
159  m_larfcal_id = caloIdMgr->getFCAL_ID();
160 
161  return StatusCode::SUCCESS;
162 
163 }
164 
166 {
167 
168  std::vector<double> eCells_EM;
169  std::vector<double> eCells_HEC;
170  std::vector<double> eCells_FCAL;
171 
172  unsigned int ncells_em = m_larem_id->channel_hash_max();
173  unsigned int ncells_hec = m_larhec_id->channel_hash_max();
174  unsigned int ncells_fcal = m_larfcal_id->channel_hash_max();
175 
176  ATH_MSG_INFO(" ncells " << ncells_em << " " << ncells_hec << " " << ncells_fcal);
177 
179  eCells_EM.resize(ncells_em,0.);
181  eCells_HEC.resize(ncells_hec,0.);
183  eCells_FCAL.resize(ncells_fcal,0.);
184 
185 //
186 // ....... create the new LAr Hit containers
187 //
188  LArHitContainer* larhit_emb=nullptr;
189  LArHitContainer* larhit_emec=nullptr;
190  LArHitContainer* larhit_hec=nullptr;
191  LArHitContainer* larhit_fcal=nullptr;
192 
193  StatusCode sc;
194 
196  larhit_emb = new LArHitContainer();
197  sc = evtStore()->record(larhit_emb,m_EmBarrelHitContainerName);
198  if (sc.isFailure()) {
199  ATH_MSG_ERROR(" cannot record LArHitEMB container ");
200  return sc;
201  }
202  }
204  larhit_emec = new LArHitContainer();
205  sc = evtStore()->record(larhit_emec,m_EmEndCapHitContainerName);
206  if (sc.isFailure()) {
207  ATH_MSG_ERROR(" cannot record LArHitEMEC container ");
208  return sc;
209  }
210  }
211 
213  larhit_hec = new LArHitContainer();
214  sc = evtStore()->record(larhit_hec,m_HecHitContainerName);
215  if (sc.isFailure()) {
216  ATH_MSG_ERROR("cannot record LArHitHEC container ");
217  return sc;
218  }
219  }
221  larhit_fcal = new LArHitContainer();
222  sc = evtStore()->record(larhit_fcal,m_ForWardHitContainerName);
223  if (sc.isFailure()) {
224  ATH_MSG_ERROR("cannot record LArHitEMEC container ");
225  return sc;
226  }
227  }
228 
229 //
230 // ............ loop over the wanted hit containers
231 //
232  int nhit_tot=0;
233 
234  for (unsigned int iHitContainer=0;iHitContainer<m_HitContainer.size();iHitContainer++)
235  {
236 
237  ATH_MSG_DEBUG(" asking for: " << m_HitContainer[iHitContainer]);
238 
239  int ical=0;
240  if (m_CaloType[iHitContainer] == LArIndex::EMBARREL_INDEX ||
241  m_CaloType[iHitContainer] == LArIndex::EMENDCAP_INDEX)
242  {
243  ical=1;
244  }
245  else if (m_CaloType[iHitContainer] == LArIndex::HADENDCAP_INDEX)
246  {
247  ical=2;
248  }
249  else if (m_CaloType[iHitContainer] == LArIndex::FORWARD_INDEX)
250  {
251  ical=3;
252  }
253  else
254  {
255  ATH_MSG_ERROR("unknown calo type ! " );
256  return StatusCode::FAILURE;
257  }
258 
259  typedef PileUpMergeSvc::TimedList<LArHitContainer>::type TimedHitContList;
260  TimedHitContList hitContList;
261 //
262 // retrieve list of pairs (time,container) from PileUp service
263 
264  if (!(p_mergeSvc->retrieveSubEvtsData(m_HitContainer[iHitContainer]
265  ,hitContList).isSuccess()) && hitContList.empty()) {
266  ATH_MSG_ERROR(" Could not fill TimedHitContList" );
267  return StatusCode::FAILURE;
268  }
269 
270 // loop over this list
271  TimedHitContList::iterator iFirstCont(hitContList.begin());
272  TimedHitContList::iterator iEndCont(hitContList.end());
273  while (iFirstCont != iEndCont) {
274 // get LArHitContainer for this subevent
275  const LArHitContainer& firstCont = *(iFirstCont->second);
276 // Loop over cells in this LArHitContainer
277  LArHitContainer::const_iterator f_cell=firstCont.begin();
278  LArHitContainer::const_iterator l_cell=firstCont.end();
279 
280  while (f_cell != l_cell) {
281  double energy = (*f_cell)->energy();
282  Identifier cellId = (*f_cell)->cellID();
283  ++f_cell;
284  nhit_tot++;
285  IdentifierHash idHash;
286  if (ical==1) {
287  idHash=m_larem_id->channel_hash(cellId);
288  if (idHash<ncells_em) eCells_EM[idHash] += energy;
289  }
290  else if(ical==2) {
291  idHash=m_larhec_id->channel_hash(cellId);
292  if (idHash<ncells_hec) eCells_HEC[idHash] += energy;
293  }
294  else if(ical==3) {
295  idHash=m_larfcal_id->channel_hash(cellId);
296  if (idHash<ncells_fcal) eCells_FCAL[idHash] += energy;
297  }
298  } // loop over hits
299  ++iFirstCont;
300  } // loop over subevent list
301 
302  } // .... end of loop over containers
303 
304  ATH_MSG_INFO(" total number of hits found " << nhit_tot);
305 
306 
307  double time=0.;
308 
310  int nhit=0;
311  for (unsigned int i=0;i<ncells_em;i++) {
312  IdentifierHash idHash = i;
313  Identifier cellId = m_larem_id->channel_id(idHash);
314  double energy = eCells_EM[i];
315  if (energy>1e-6) {
317  LArHit* hit = new LArHit(cellId,energy,time);
318  hit->finalize();
319  larhit_emb->push_back(hit);
320  nhit++;
321  }
323  LArHit* hit = new LArHit(cellId,energy,time);
324  hit->finalize();
325  larhit_emec->push_back(hit);
326  nhit++;
327  }
328  }
329  }
330  ATH_MSG_INFO(" Number of hits filled in LArHitEM containers " << nhit);
331  }
332 
334  int nhit=0;
335  for (unsigned int i=0;i<ncells_hec;i++) {
336  IdentifierHash idHash = i;
337  Identifier cellId = m_larhec_id->channel_id(idHash);
338  double energy = eCells_HEC[i];
339  if (energy>1e-6) {
340  LArHit* hit = new LArHit(cellId,energy,time);
341  hit->finalize();
342  larhit_hec->push_back(hit);
343  nhit++;
344  }
345  }
346  ATH_MSG_INFO(" Number of hits filled in LArHitHEC container " << nhit);
347  }
348 
350  int nhit=0;
351  for (unsigned int i=0;i<ncells_fcal;i++) {
352  IdentifierHash idHash = i;
353  Identifier cellId = m_larfcal_id->channel_id(idHash);
354  double energy = eCells_FCAL[i];
355  if (energy>1e-6) {
356  LArHit* hit = new LArHit(cellId,energy,time);
357  hit->finalize();
358  larhit_fcal->push_back(hit);
359  nhit++;
360  }
361  }
362  ATH_MSG_INFO(" Number of hits filled in LArHitFCAL container " << nhit);
363  }
364 
365 
366 
367  // lock container in StoreGate
368  if (larhit_emb) {
369  sc = evtStore()->setConst(larhit_emb);
370  if (sc.isFailure()) {
371  ATH_MSG_ERROR(" Cannot lock LArHitContainer ");
372  return(StatusCode::FAILURE);
373  }
374  }
375 
376  if (larhit_emec) {
377  sc = evtStore()->setConst(larhit_emec);
378  if (sc.isFailure()) {
379  ATH_MSG_ERROR(" Cannot lock LArHitContainer ");
380  return(StatusCode::FAILURE);
381  }
382  }
383 
384  if (larhit_hec) {
385  sc = evtStore()->setConst(larhit_hec);
386  if (sc.isFailure()) {
387  ATH_MSG_ERROR(" Cannot lock LArHitContainer ");
388  return(StatusCode::FAILURE);
389  }
390  }
391 
392  if (larhit_fcal) {
393  sc = evtStore()->setConst(larhit_fcal);
394  if (sc.isFailure()) {
395  ATH_MSG_ERROR(" Cannot lock LArHitContainer ");
396  return(StatusCode::FAILURE);
397  }
398  }
399 
400 
401  return StatusCode::SUCCESS;
402 
403 }
404 
406 {
407 //
408  ATH_MSG_DEBUG(" LArHitMerger finalize completed successfully");
409 
410 //
411  return StatusCode::SUCCESS;
412 
413 }
LArHitMerger::initialize
virtual StatusCode initialize()
Definition: LArHitMerger.cxx:48
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
EMENDCAP_INDEX
@ EMENDCAP_INDEX
Definition: LArIndexEnum.h:9
FORWARD_INDEX
@ FORWARD_INDEX
Definition: LArIndexEnum.h:11
CaloIDHelper::channel_hash_max
size_type channel_hash_max() const
One more than the largest channel (cell) hash code.
LArHitMerger::m_larem_id
const LArEM_ID * m_larem_id
Definition: LArHitMerger.h:44
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
LArHit::finalize
void finalize()
The method to be called at the end of event by SD.
Definition: LArHit.h:143
LArHitMerger::m_larhec_id
const LArHEC_ID * m_larhec_id
Definition: LArHitMerger.h:45
CaloIdManager::getEM_ID
const LArEM_ID * getEM_ID(void) const
Definition: CaloIdManager.cxx:80
LArHitMerger::m_EmEndCapHitContainerName
std::string m_EmEndCapHitContainerName
Definition: LArHitMerger.h:40
LArFCAL_Base_ID::channel_hash
IdentifierHash channel_hash(Identifier channelId) const
Convert a connected channel (cell) Identifier to a hash code.
LArHitContainer
Hit collection.
Definition: LArHitContainer.h:26
LArHitMerger::LArHitMerger
LArHitMerger(const std::string &name, ISvcLocator *pSvcLocator)
Definition: LArHitMerger.cxx:24
LArHitMerger::m_SubDetectors
std::string m_SubDetectors
Definition: LArHitMerger.h:38
LArHitMerger::p_mergeSvc
PileUpMergeSvc * p_mergeSvc
Definition: LArHitMerger.h:32
LArHitMerger::m_HecHitContainerName
std::string m_HecHitContainerName
Definition: LArHitMerger.h:41
LArEM_Base_ID::channel_id
Identifier channel_id(const ExpandedIdentifier &exp_id) const
Build a cell identifier from an expanded identifier.
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
AthenaHitsVector< LArHit >::const_iterator
boost::transform_iterator< make_const, typename CONT::const_iterator > const_iterator
Definition: AthenaHitsVector.h:58
PileUpMergeSvc::TimedList::type
std::list< value_t > type
type of the collection of timed data object
Definition: PileUpMergeSvc.h:75
NUMDET
@ NUMDET
Definition: LArIndexEnum.h:12
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
LArEM_Base_ID::channel_hash
IdentifierHash channel_hash(Identifier channelId) const
create hash id from channel id
CaloIdManager::getHEC_ID
const LArHEC_ID * getHEC_ID(void) const
Definition: CaloIdManager.cxx:95
AthenaHitsVector::push_back
void push_back(T *t)
Definition: AthenaHitsVector.h:153
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
CaloIdManager
This class initializes the Calo (LAr and Tile) offline identifiers.
Definition: CaloIdManager.h:45
lumiFormat.i
int i
Definition: lumiFormat.py:92
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
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
LArIndexEnum.h
LArFCAL_Base_ID::channel_id
Identifier channel_id(const ExpandedIdentifier &exp_id) const
cell identifier for a channel from ExpandedIdentifier
LArHitMerger::~LArHitMerger
~LArHitMerger()
CaloIdManager::getFCAL_ID
const LArFCAL_ID * getFCAL_ID(void) const
Definition: CaloIdManager.cxx:85
LArHitMerger::m_ForWardHitContainerName
std::string m_ForWardHitContainerName
Definition: LArHitMerger.h:42
LArHitMerger::m_larfcal_id
const LArFCAL_ID * m_larfcal_id
Definition: LArHitMerger.h:46
LArHEC_Base_ID::channel_hash
IdentifierHash channel_hash(Identifier channelId) const
create hash id from channel id
LArHitContainer
LArHitContainer
Definition: LArSimEventTPCnv.cxx:28
HADENDCAP_INDEX
@ HADENDCAP_INDEX
Definition: LArIndexEnum.h:10
AthAlgorithm
Definition: AthAlgorithm.h:47
LArHitMerger.h
LArHitMerger::execute
virtual StatusCode execute()
Definition: LArHitMerger.cxx:165
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
IdentifierHash.h
LArHEC_Base_ID::channel_id
Identifier channel_id(const ExpandedIdentifier &exp_id) const
channel identifier for a channel from ExpandedIdentifier
LArHit
Class to store hit energy and time in LAr cell from G4 simulation.
Definition: LArHit.h:25
LArHitMerger::finalize
virtual StatusCode finalize()
Definition: LArHitMerger.cxx:405
LArID.h
LArHit.h
LArHitMerger::m_SubDetFlag
std::vector< bool > m_SubDetFlag
Definition: LArHitMerger.h:34
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
EMBARREL_INDEX
@ EMBARREL_INDEX
Definition: LArIndexEnum.h:8
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
LArEM_Base_ID::is_em_barrel
bool is_em_barrel(const Identifier id) const
test if the id belongs to the EM barrel
LArHitContainer.h
PileUpMergeSvc.h
the preferred mechanism to access information from the different event stores in a pileup job.
LArHitMerger::m_HitContainer
std::vector< std::string > m_HitContainer
Definition: LArHitMerger.h:35
CaloIdManager.h
AthenaHitsVector::end
const_iterator end() const
Definition: AthenaHitsVector.h:143
IdentifierHash
Definition: IdentifierHash.h:38
LArHitMerger::m_EmBarrelHitContainerName
std::string m_EmBarrelHitContainerName
Definition: LArHitMerger.h:39
AthenaHitsVector::begin
const_iterator begin() const
Definition: AthenaHitsVector.h:139
LArEM_Base_ID::is_em_endcap
bool is_em_endcap(const Identifier id) const
test if the id belongs to the EM Endcap
PileUpMergeSvc::retrieveSubEvtsData
StatusCode retrieveSubEvtsData(const KEY &dataKey, TIMEDDATA &timedData)
retrieve keyed DATA objs for all sub-events and attach a time to them
LArHitMerger::m_CaloType
std::vector< int > m_CaloType
Definition: LArHitMerger.h:36