ATLAS Offline Software
Loading...
Searching...
No Matches
LArRodBlockPhysicsV0.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Implementation of the LArRODBlockStructure_3 class
6// This version contains LArRawChannels (=E,t,Q)
7// and LArDigits (=5 time samples)
8// See .h file for more details.
9
14#include "GaudiKernel/Bootstrap.h"
15#include "GaudiKernel/ISvcLocator.h"
16#include "GaudiKernel/IToolSvc.h"
17#include <stdlib.h>
18#include <cstdio>
19#include <iostream>
20
21#ifdef LARBSDBGOUTPUT
22#define LARBSDBG(text) m_logstr<<MSG::DEBUG<<text<<endmsg
23#else
24#define LARBSDBG(text)
25#endif
26
27namespace {
28union ShortLong {
29 uint16_t s[2];
30 uint32_t l;
31};
32}
33
34const uint32_t LArRodBlockPhysicsV0::m_DummyBitMap[4]={0xffffffff,0xffffffff,0xffffffff,0xffffffff};
35
38{
39 // retrieve onlineHelper
40 const LArOnlineID* online_id;
41 SmartIF<StoreGateSvc> detStore{Gaudi::svcLocator()->service("DetectorStore")};
42 if (!detStore) {
43 m_logstr << MSG::ERROR << "Unable to locate DetectorStore" << endmsg;
44 std::abort();
45 }
46 StatusCode sc = detStore->retrieve(online_id, "LArOnlineID");
47 if (sc.isFailure()) {
48 m_logstr << MSG::FATAL << "Could not get LArOnlineID helper !" << endmsg;
49 std::abort();
50 }
51 else {
52 m_onlineHelper=online_id;
53 m_logstr << MSG::DEBUG << " Found the LArOnlineID helper. " << endmsg;
54 }
55
56 m_iHeadBlockSize=endtag/2; // The implicit cast rounds down to the right size
59 m_vFragment=NULL;
60 m_FebBlock=NULL;
61 m_pRODblock=NULL;
62 m_FlagPtr=NULL;
63 m_HighEPtr=NULL;
64 m_LowEPtr=NULL;
65}
66
67// clear temporary block vectors
73
82
83//For reading (to speed up the process)
85{//Set pointers to data blocks (presuming, they exist)
89 m_LowEPtr=reinterpret_cast<const int16_t*>(m_FlagPtr+m_NFlaggingWords);
90 }
91 else
92 {
93 //Bugfix, 9.8.2004, WL: Set pointer to dummy map to read FEB with only high energy block
95 m_LowEPtr=reinterpret_cast<const int16_t*>(m_FlagPtr);
96 }
99 else
100 m_HighEPtr=NULL;
101 }
102 else
103 {m_FlagPtr=NULL;
104 m_LowEPtr=NULL;
105 m_HighEPtr=NULL;
106 }
107 return true;
108}
109
111{ uint16_t oldword=LE_getVectorHeader16(NGainNSamples);
112 LE_setHeader16(NGainNSamples,(oldword & 0xFF00) | n);
113}
114
116{ uint16_t oldword=LE_getVectorHeader16(NGainNSamples);
117 LE_setHeader16(NGainNSamples,(oldword & 0x00FF) | (n<<8));
118}
119
120
121void LArRodBlockPhysicsV0::setNextEnergy(const int channel, const int32_t energy,
122 const int32_t time, const int32_t quality, const uint32_t gain) {
123 int rcNb=FebToRodChannel(channel);
124 //rcNb ist supposed to equal or bigger than m_EIndex.
125 //In the latter case, we fill up the missing channels with zero
126 if (rcNb<m_EIndex) {
127 m_logstr << MSG::ERROR << "LArRODBlockStructure Error: Internal error. Channels not ordered correctly. rcNb=" << rcNb
128 << " m_EIndex=" << m_EIndex << endmsg;
129 return;
130 }
131 //Fill up missing channels with zeros:
132 while (m_EIndex<rcNb)
133 setNextEnergy(0,0,-1,0);
134 //Add data...
135 setNextEnergy(energy,time,quality,gain);
136 return;
137}
138//Private function, expects channel number is rod-style ordering
139void LArRodBlockPhysicsV0::setNextEnergy(const int32_t energy, const int32_t time, const int32_t quality, const uint32_t gain)
140{
141 if (m_EIndex>=m_channelsPerFEB) //Use m_EIndex to count total number of channels
142 {m_logstr << MSG::ERROR << "LArRodBlockStructure Error: Attempt to write Energy for channel "
143 << m_EIndex << " channels into a FEB!" <<endmsg;
144 return;
145 }
146 LARBSDBG("LArRodBlockStructure: Setting Energy for channel " << m_EIndex << ". E=" << energy);
147 if (quality<0 && energy<0x7FFE && gain==0) { //Write into Low Energy block
148 m_LowEIndex++; //Use m_LowEIndex to count the channels in the Low Energy block
149 ShortLong twoValues{};
150 twoValues.s[0]=0;
151 twoValues.s[1]=0;
152 if (m_LowEIndex%2==1) { //This is an odd number, simply add data at the bottom of the block
153 twoValues.s[0]=(int16_t)energy;
154 }
155 else { //Even number: Merging with previous block
156 uint32_t oneValue=m_LowEnergyBlock[m_LowEnergyBlock.size()-1]; //Take last element of vector
157 m_LowEnergyBlock.pop_back();
158 int16_t* valptr=reinterpret_cast<int16_t*>(&oneValue);
159 twoValues.s[0]=valptr[0];
160 twoValues.s[1]=(int16_t)energy;
161 }
162 LARBSDBG("Writing words: val0= " << twoValues.s[0] << " val1= " << twoValues.s[1]);
163 m_LowEnergyBlock.push_back(twoValues.l);
164 LARBSDBG("Writing Raw data to Low E block. E=" << energy);
165 }
166 else //Write into High Energy block
168 m_HighEnergyBlock.push_back(*((uint32_t*)(&energy)));
169 uint32_t t_sign;
170 uint32_t abs_time;
171 if (time<0)
172 t_sign=1;
173 else
174 t_sign=0;
175 abs_time=abs(time);
176 if (abs_time>0x1fff)
177 abs_time=0x1fff;
178 uint32_t gtQ = (gain << 30) | (t_sign<<29) | ((abs_time & 0x1fff)<<16) | (0xffff & quality);
179 m_HighEnergyBlock.push_back(gtQ);
180 LARBSDBG("Writing Raw data to High E block. E=" << energy << " Q=" << quality);
181 }
182 m_EIndex++;
183}
184
185
186void LArRodBlockPhysicsV0::setRawData(const int channel, const std::vector<short>& samples, const uint32_t gain) {
187 //Convert Feb to Rod Channel Number:
188 int rcNb=FebToRodChannel(channel);
189 if (rcNb>=m_channelsPerFEB)
190 {m_logstr << MSG::ERROR << "Attempt to write Energy for channel " << rcNb << " channels into a FEB!" << endmsg;
191 return;
192 }
193 unsigned int nsamples = LE_getVectorHeader16(NGainNSamples) & 0x00FF;
194 if(samples.size() != nsamples) {
195 m_logstr << MSG::ERROR << "Number of samples mismatch!\n";
196 m_logstr << " nsamples =" << nsamples;
197 m_logstr << " samples.size() =" << samples.size() << endmsg;
198 std::abort();
199 }
200
201 setBit(&m_RawDataBlock[0],rcNb);
202 //Samples have 12 bit and are shifted to the left by 2 bits.
203 // odd samples in high bits, even samples in low bits
204 if((nsamples/2)*2!=nsamples) { //odd number of samples - gain is alone
205 m_RawDataBlock.push_back((gain<<30) | samples[0]<<2);
206 for (unsigned int i=1;i<nsamples;i+=2)
207 m_RawDataBlock.push_back((samples[i+1]<<18) | samples[i]<<2);
208 }
209 else { //even number of samples - gain is packed with sample 0
210 m_RawDataBlock.push_back((gain<<30) | (samples[1]<<18) | samples[0]<<2);
211 for (unsigned int i=2;i<nsamples;i+=2)
212 m_RawDataBlock.push_back((samples[i+1]<<18) | samples[i]<<2);
213 }
214}
215
216
217//For writing: takes existing Fragment and splits it into Feb-Blocks
218void LArRodBlockPhysicsV0::initializeFragment(std::vector<uint32_t>& fragment)
219{
220 m_pRODblock=&fragment; //remember pointer to fragment
221 if (fragment.size()>m_iHeadBlockSize) { //Got filled fragment
222 unsigned int sizeRead=0;
223 //Store existing data in the FEB-Map
224 while (sizeRead<fragment.size()) {
225 std::vector<uint32_t>::iterator FebIter;
226 FebIter=fragment.begin()+sizeRead; //Store pointer to current Feb-Header
227 m_FebBlock=&(*FebIter); //Set m_FebBlock in order to use getHeader-functions.
228 uint32_t currFEBid=getHeader32(FEBID); //Get this FEB-ID
229 uint16_t currFebSize=getNumberOfWords(); //Size of this FEB-Block
230 if (FebIter+currFebSize>fragment.end()) {
231 fragment.clear(); //Clear existing vector
232 m_logstr << MSG::ERROR << "Got inconsistent ROD-Fragment!" << endmsg;
233 return;
234 }
235 m_mFebBlocks[currFEBid].assign(FebIter,FebIter+currFebSize); //Copy data from ROD-fragment into FEB-Block
236 sizeRead+=currFebSize+m_MiddleHeaderSize;//6 is the middle header size
237 LARBSDBG("Found FEB-id " << currFEBid << " in existing ROD-Fragment");
238 } // end while
239 }
240 fragment.clear(); //Clear existing vector
241 return;
242}
243
244//For writing: Initalizes a single FEB-Block
247 if (m_vFragment->size()<m_iHeadBlockSize) //Got empty or spoiled fragment
248 {m_vFragment->resize(m_iHeadBlockSize,0); //Initialize FEB-Header
249 setHeader32(FEBID,id); //Set Feb ID
250 }
251
255}
256
258{
259//Complete non-complete Energy block
261 setNextEnergy(0,0,-1,0);//E=0,t=0,q=-1,G=0
262 // Energies
263 unsigned int n;
264 uint16_t BlockOffset;
265 //Low energy block....
266 n = m_LowEnergyBlock.size();
268 //Check if Low Energy Block exists and is not yet part of the fragment
269 LARBSDBG("Checking Low Energy Block n=" << n << "BlockOffset=" << BlockOffset);
270 if (n>m_NFlaggingWords && !BlockOffset)
272 for (unsigned i=0;i<n;i++)
273 m_vFragment->push_back(m_LowEnergyBlock[i]);
274 }
275 //High energy block...
276 n = m_HighEnergyBlock.size();
278 LARBSDBG("Checking High Energy Block n=" << n << "BlockOffset=" << BlockOffset);
279 //Check if High Energy-Block exists and is not yet part of the fragment
280 if (n && !BlockOffset)
282 for(unsigned int i=0;i<n;i++)
283 m_vFragment->push_back(m_HighEnergyBlock[i]);
284 }
285
286 // Raw data
287 LARBSDBG("Checking Raw Data Block");
288 n = m_RawDataBlock.size();
290 LARBSDBG("Checking Raw Data Block. n=" << n << "BlockOffset=" << BlockOffset);
291 //Check if Raw Data block exists and is not yet part of the fragment
292 if (n>m_NFlaggingWords && !BlockOffset)
294 for(unsigned int i=0;i<n;i++)
295 m_vFragment->push_back(m_RawDataBlock[i]);
296 }
298 LARBSDBG("Offsets:" << std::endl
299 << "Raw Data: " << LE_getVectorHeader16(RawDataBlkOffset) << std::endl
300 <<"Low Energy: " << LE_getVectorHeader16(LowEBlkOffset)<< std::endl
301 << "High Energy: " << LE_getVectorHeader16(HighEBlkOffset)<< std::endl
302 << "Energy-index:" << m_EIndex << std::endl
303 << "Filled channels: " << m_ECounter << std::endl);
304 clearBlocks();
305 return;
306}
307
308
310{
311 FEBMAPTYPE::const_iterator feb_it_b=m_mFebBlocks.begin();
312 FEBMAPTYPE::const_iterator feb_it_e=m_mFebBlocks.end();
313 FEBMAPTYPE::const_iterator feb_it;
314 for (feb_it=feb_it_b;feb_it!=feb_it_e;++feb_it) {
315 if (feb_it!=feb_it_b) //Not first Feb
317
318 //Add feb data to rod data block
319 m_pRODblock->insert (m_pRODblock->end(),
320 feb_it->second.begin(), feb_it->second.end());
321 } //end for feb_it
322
323 m_mFebBlocks.clear();
324 return;
325}
326
327int LArRodBlockPhysicsV0::getNextRawData(int& channelNumber, std::vector<short>& samples, uint32_t& gain)
328{
329 LARBSDBG("in LArRodBlockPhysicsV0::getNextRawData.");
330 LARBSDBG("m_RawDataCounter=" << m_RawDataCounter << " m_RawDataIndex="<< m_RawDataIndex);
331 LARBSDBG("m_channelsPerFEB=" << m_channelsPerFEB);
332 const int flags=(int) LE_getHeader16(RawDataBlkOffset);
333 if (!flags)
334 return 0; //Block not existing
335 if (m_RawDataCounter>=m_channelsPerFEB) //Already beyond maximal number of channels
336 return 0;
337
338 LARBSDBG("Flags="<<flags);
339 while (!getBit(m_FebBlock+flags,m_RawDataCounter)) //Look for next filled channel
341 LARBSDBG("RawDataCounter ist now " << m_RawDataCounter);
342 if (m_RawDataCounter>=m_channelsPerFEB) //No more channel availible
343 return 0;
344 }
345 LARBSDBG("Found filled channel at positon " << m_RawDataCounter);
346 //Found next filled channel
347 channelNumber=(m_RawDataCounter>>4) + ((m_RawDataCounter&0xf)<<3); //Convert ROD to FEB channel ordering
348 unsigned int nsamples = LE_getHeader16(NGainNSamples) & 0x00FF;
349 LARBSDBG("This run has " << nsamples << " samples");
350 int index = flags+m_NFlaggingWords+m_RawDataIndex*((int) (nsamples+1)/2);
351 LARBSDBG( "index="<<index);
353 LARBSDBG("In getnextRawData(). index= " << index);
354 if((nsamples/2)*2!=nsamples) { //odd number of samples
355 const uint32_t& x = m_FebBlock[index];
356 gain = x >> 30;
357 samples.push_back((short) (x>>2 & 0xfff));
358 for(unsigned int i=1;i<=(nsamples-1)/2;i++) {
359 const uint32_t& x = m_FebBlock[index+i];
360 samples.push_back((short) (x>>2 & 0xfff));
361 samples.push_back((short) (x>>18));
362 }
363 }
364 else { //even number of samples
365 const uint32_t& x = m_FebBlock[index];
366 gain = x >> 30;
367 samples.push_back((short) (x>>2 & 0xfff));
368
369 samples.push_back((short) ((x>>18) & 0xfff));
370
371 for (unsigned int i=1;i<nsamples/2;i++) {
372 const uint32_t& x = m_FebBlock[index+i];
373 samples.push_back((short) (x>>2 & 0xfff));
374 samples.push_back((short) (x >> 18));
375 }
376 } //end even numer of sample
378 return 1;
379}
380
381
382//Sort functions & ordering relation:
383template<class RAWDATA>
384bool LArRodBlockPhysicsV0::operator ()
385 (const RAWDATA* ch1, const RAWDATA* ch2) const
386{
387 HWIdentifier id1 = ch1->channelID();
388 HWIdentifier id2 = ch2->channelID();
389
390 HWIdentifier febId1= m_onlineHelper->feb_Id(id1);
391 HWIdentifier febId2= m_onlineHelper->feb_Id(id2);
392
393 if(febId1 == febId2 ){
394 int cId1 = m_onlineHelper->channel(id1);
395 int cId2 = m_onlineHelper->channel(id2);
396 return FebToRodChannel(cId1) < FebToRodChannel(cId2);
397 }
398
399 return febId1 < febId2 ;
400}
401
402
403void LArRodBlockPhysicsV0::sortDataVector(std::vector<const LArRawChannel*>& vRC)
404{std::sort(vRC.begin(),vRC.end(),*this);}
405
406void LArRodBlockPhysicsV0::sortDataVector( std::vector<const LArDigit*>& vDigit)
407{std::sort(vDigit.begin(),vDigit.end(),*this);
408}
#define endmsg
static Double_t sc
#define LARBSDBG(text)
This class provides decoding/encoding from/to ROD format.
#define x
std::vector< uint32_t > m_RawDataBlock
virtual int getNextRawData(int &channelNumber, std::vector< short > &samples, uint32_t &gain)
virtual void setRawData(const int channel, const std::vector< short > &samples, const uint32_t gain)
LArRodBlockPhysicsV0(IMessageSvc *msgSvc)
virtual void initializeFEB(const uint32_t id)
virtual void setNumberOfSamples(const uint8_t n)
int FebToRodChannel(int ch) const
static std::string BlockType()
static const uint32_t m_DummyBitMap[4]
virtual void setNextEnergy(const int channel, const int32_t energy, const int32_t time, const int32_t quality, const uint32_t gain)
virtual void sortDataVector(std::vector< const LArRawChannel * > &)
const LArOnlineID * m_onlineHelper
virtual void initializeFragment(std::vector< uint32_t > &fragment)
virtual void setNumberOfGains(const uint8_t n)
std::vector< uint32_t > m_LowEnergyBlock
std::vector< uint32_t > m_HighEnergyBlock
int getBit(const uint32_t *const p, const unsigned chan) const
void setHeader32(const unsigned n, const uint32_t w)
uint16_t LE_getVectorHeader16(const unsigned n) const
void LE_setHeader16(const unsigned n, const uint16_t w)
uint32_t getHeader32(const unsigned n) const
void setBit(uint32_t *const p, const unsigned chan)
std::vector< uint32_t > * m_pRODblock
LArRodBlockStructure(IMessageSvc *msgSvc, const std::string &blockType)
std::vector< uint32_t > * m_vFragment
uint16_t LE_getHeader16(const unsigned n) const
uint32_t getNumberOfWords() const
Definition index.py:1
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
setEventNumber uint32_t