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