ATLAS Offline Software
LArDigitContainerCnv_p3.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include "Identifier/Identifier.h"
9 #include "GaudiKernel/GaudiException.h"
11 #include "StoreGate/StoreGateSvc.h"
12 
13 #include "AthAllocators/DataPool.h"
14 
15 #include <algorithm>
16 #include <cstdlib>
17 
18 namespace {
19 
20  void writeTwoBits(std::vector<unsigned char>& vec, const unsigned idx, const unsigned char val) {
21  const auto qr=std::div(idx,4);
22  const unsigned char maskval=(val&0x3) << 2*qr.rem;
23  vec[qr.quot]|=maskval;
24  return;
25  }
26 
27  unsigned char readTwoBits(const std::vector<unsigned char>& vec, const unsigned idx) {
28  const auto qr=std::div(idx,4);
29  const unsigned char& v=vec[qr.quot];
30  return (v>>2*qr.rem) & 0x3;
31  }
32 }
33 
35  const LArOnlineID_Base* idSCHelper,
36  const StoreGateSvc* storeGateSvc) :
37  m_idHelper(idHelper),
38  m_idSCHelper(idSCHelper),
39  m_storeGateSvc(storeGateSvc)
40 {}
41 
42 void
44  LArDigitContainer* trans, MsgStream &/*log*/) const
45 {
46 
47  DataPool<LArDigit> dataItems;
48  if (pers->m_nSamples==0 || pers->m_samples.size()==0) {
49  //No data
50  return;
51  }
52 
53  const unsigned nMaxChannels=4*(pers->m_gain.size());
54  trans->clear(SG::VIEW_ELEMENTS);
55  size_t numElements = pers->m_samples.size()/pers->m_nSamples;
56  //
57  dataItems.reserve(numElements);
58  trans->reserve(numElements);
59 
60  const LArOnlineID_Base* idHelper = nullptr;
61  if (!pers->m_this_is_slar) idHelper = m_idHelper;
62  else idHelper = m_idSCHelper;
63 
64  std::vector<short>::const_iterator samplesIt=pers->m_samples.begin();
65  for(unsigned idx=0;idx<nMaxChannels;idx++) {
66  const unsigned char gain=readTwoBits(pers->m_gain,idx);
67  if (gain!=0) {
68  const HWIdentifier hwid=idHelper->channel_Id(IdentifierHash(idx));
69  std::vector<short>::const_iterator samplesIt_e=samplesIt+pers->m_nSamples;
70  std::vector<short> samples(samplesIt,samplesIt_e);
71  LArDigit* lardigi = dataItems.nextElementPtr();
72  (*lardigi) =
73  LArDigit(hwid, (CaloGain::CaloGain)(gain - 1), std::move(samples));
74  trans->push_back(lardigi);
75  samplesIt = samplesIt_e;
76  }//end if gain!=0, eg channel present
77  }//end loop over all hashes
78 }
79 
80 
81 void
83  LArDigitContainer_p3* pers, MsgStream &log) const {
84 
85  const unsigned int nChannels=trans->size();
86  if (nChannels==0) {
87  pers->m_nSamples=0; //Avoid undefined value in output file
88  return;
89  }
90 
91  //Store the number of ADC samples from first digit
92  pers->m_nSamples=(*(trans->begin()))->samples().size();
93 
94  //Determine whether we are dealing with standard cells or supercells
95  const LArOnlineID_Base* idHelper = nullptr;
96  const SG::DataProxy* proxy = m_storeGateSvc->proxy(trans);
97  std::string containerName = proxy->name();
98  if (containerName.find("SC")==std::string::npos) {
99  idHelper = m_idHelper;
100  pers->m_this_is_slar = false;
101  }
102  else {
103  idHelper = m_idSCHelper;
104  pers->m_this_is_slar = true;
105  }
106 
107  const unsigned hashMax=idHelper->channelHashMax();
108 
109  //Resize persistant storage
110  pers->m_gain.assign(hashMax/4,0);
111  pers->m_samples.assign(pers->m_nSamples*nChannels,0);
112 
113  //Copy pointer to digits into a hash-indexed vector, nullptr for non-existing digits
114  std::vector<const LArDigit*> digitsvsHash(hashMax,nullptr);
115  for (const LArDigit* transDigit : *trans) {
116  const IdentifierHash h=idHelper->channel_Hash(transDigit->hardwareID());
117  if (ATH_UNLIKELY(transDigit->samples().size()!=pers->m_nSamples)) {
118  log << MSG::ERROR << "Encountered LArDigit Container with varying numbers of ADC samples" << endmsg;
119  throw GaudiException("Encountered LArDigit Container with varying numbers of ADC samples",
120  "LArDigitContainerCnv_p3",StatusCode::FAILURE);
121  }
122  digitsvsHash[h]=transDigit;
123  }
124 
125  unsigned idx=0;
126  std::vector<short>::iterator writeIt=pers->m_samples.begin();
127  std::vector<const LArDigit*>::const_iterator it=digitsvsHash.begin();
128  std::vector<const LArDigit*>::const_iterator it_e=digitsvsHash.end();
129  for (;it!=it_e;++it,++idx) {
130  const LArDigit* dig=*it;
131  if (dig!=nullptr) {
132  //A gain value of 0 means no data for this hash-value
133  //Store gain+1
134  writeTwoBits(pers->m_gain,idx,1+dig->gain());
135  //Copy ADC samples
136  writeIt=std::copy(dig->samples().begin(),dig->samples().end(),writeIt);
137  }
138  }
139 }
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
LArDigitContainerCnv_p3::transToPers
virtual void transToPers(const LArDigitContainer *trans, LArDigitContainer_p3 *pers, MsgStream &log) const override
Definition: LArDigitContainerCnv_p3.cxx:82
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
LArOnlineID_Base::channel_Hash
IdentifierHash channel_Hash(HWIdentifier channelId) const
Create channel_hash from channel_Id.
Definition: LArOnlineID_Base.cxx:1636
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
DataPool::reserve
void reserve(unsigned int size)
Set the desired capacity.
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
LArOnlineID_Base.h
CaloCondBlobAlgs_fillNoiseFromASCII.gain
gain
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:110
LArDigit::samples
const std::vector< short > & samples() const
Definition: LArDigit.h:78
skel.it
it
Definition: skel.GENtoEVGEN.py:423
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
HWIdentifier
Definition: HWIdentifier.h:13
LArDigitContainerCnv_p3::m_idSCHelper
const LArOnlineID_Base * m_idSCHelper
Definition: LArDigitContainerCnv_p3.h:34
LArDigitContainer_p3::m_this_is_slar
bool m_this_is_slar
Definition: LArDigitContainer_p3.h:12
AthUnlikelyMacros.h
LArDigitContainerCnv_p3::persToTrans
virtual void persToTrans(const LArDigitContainer_p3 *pers, LArDigitContainer *trans, MsgStream &log) const override
Definition: LArDigitContainerCnv_p3.cxx:43
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:128
LArDigit
Liquid Argon digit base class.
Definition: LArDigit.h:25
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
extractSporadic.h
list h
Definition: extractSporadic.py:97
01SubmitToGrid.samples
samples
Definition: 01SubmitToGrid.py:58
LArOnlineID_Base::channel_Id
HWIdentifier channel_Id(int barrel_ec, int pos_neg, int feedthrough, int slot, int channel) const
create channel identifier from fields
Definition: LArOnlineID_Base.cxx:1569
LArDigitContainerCnv_p3::LArDigitContainerCnv_p3
LArDigitContainerCnv_p3(const LArOnlineID_Base *idHelper, const LArOnlineID_Base *idSCHelper, const StoreGateSvc *m_storeGateSvc)
Definition: LArDigitContainerCnv_p3.cxx:34
LArDigitContainer_p3::m_gain
std::vector< unsigned char > m_gain
Definition: LArDigitContainer_p3.h:14
LArDigitContainer_p3::m_nSamples
unsigned char m_nSamples
Definition: LArDigitContainer_p3.h:13
DataPool::nextElementPtr
pointer nextElementPtr()
obtain the next available element in pool by pointer pool is resized if its limit has been reached On...
DataVector::clear
void clear()
Erase all the elements in the collection.
TrigCaloRecConfig.storeGateSvc
storeGateSvc
Definition: TrigCaloRecConfig.py:564
DataPool.h
LArOnlineID_Base
Helper for the Liquid Argon Calorimeter cell identifiers.
Definition: LArOnlineID_Base.h:105
StoreGateSvc::proxy
virtual SG::DataProxy * proxy(const void *const pTransient) const override final
get proxy for a given data object address in memory
LArOnlineID_Base::channelHashMax
size_type channelHashMax(void) const
Define channel hash tables max size.
Definition: LArOnlineID_Base.cxx:1901
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
LArDigitContainer.h
CaloGain::CaloGain
CaloGain
Definition: CaloGain.h:11
python.PyAthena.v
v
Definition: PyAthena.py:157
LArDigit::gain
CaloGain::CaloGain gain() const
Definition: LArDigit.h:72
LArDigitContainerCnv_p3::m_idHelper
const LArOnlineID_Base * m_idHelper
Definition: LArDigitContainerCnv_p3.h:33
h
LArDigitContainerCnv_p3.h
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
DataPool
a typed memory pool that saves time spent allocation small object. This is typically used by containe...
Definition: DataPool.h:47
LArDigitContainer
Container class for LArDigit.
Definition: LArDigitContainer.h:24
LArDigitContainer_p3::m_samples
std::vector< short > m_samples
Definition: LArDigitContainer_p3.h:15
LArDigitContainerCnv_p3::m_storeGateSvc
const StoreGateSvc * m_storeGateSvc
Definition: LArDigitContainerCnv_p3.h:35
calibdata.copy
bool copy
Definition: calibdata.py:27
LArDigitContainer_p3
Definition: LArDigitContainer_p3.h:10
IdentifierHash
Definition: IdentifierHash.h:38
SG::DataProxy
Definition: DataProxy.h:44
StoreGateSvc.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.