ATLAS Offline Software
Loading...
Searching...
No Matches
LArRodBlockPhysicsV1.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
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
74
84
85//For reading (to speed up the process)
87{
88 //Set pointers to data blocks (pesuming, they exist)
90 {
92 {
94 m_LowEPtr=reinterpret_cast<const int16_t*>(m_FlagPtr+m_NFlaggingWords);
95 }
96 else
97 {
98 //Bugfix, 9.8.2004, WL: Set pointer to dummy map to read FEB with only high energy block
100 m_LowEPtr=reinterpret_cast<const int16_t*>(m_FlagPtr);
101 }
104 else
105 m_HighEPtr=NULL;
106 }
107 else
108 {
109 m_FlagPtr=NULL;
110 m_LowEPtr=NULL;
111 m_HighEPtr=NULL;
112 }
113 return true;
114}
115
117{
118 uint16_t oldword=getVectorHeader16(NGainNSamples);
119 setHeader16(NGainNSamples,(oldword & 0xFF00) | n);
120}
121
123{
124 uint16_t oldword=getVectorHeader16(NGainNSamples);
125 setHeader16(NGainNSamples,(oldword & 0x00FF) | (n<<8));
126}
127
128
129void LArRodBlockPhysicsV1::setNextEnergy(const int channel, const int32_t energy,
130 const int32_t time, const int32_t quality, const uint32_t gain) {
131 int rcNb=FebToRodChannel(channel);
132 //rcNb ist supposed to equal or bigger than m_EIndex.
133 //In the latter case, we fill up the missing channels with zero
134 if (rcNb<m_EIndex) {
135 m_logstr << MSG::ERROR << "LArRODBlockStructure Error: Internal error. Channels not ordered correctly. rcNb=" << rcNb
136 << " m_EIndex=" << m_EIndex << endmsg;
137 return;
138 }
139 //Fill up missing channels with zeros:
140 while (m_EIndex<rcNb)
141 setNextEnergy(0,0,-1,0);
142 //Add data...
143 setNextEnergy(energy,time,quality,gain);
144}
145//Private function, expects channel number is rod-style ordering
146void LArRodBlockPhysicsV1::setNextEnergy(const int32_t energy, const int32_t time, const int32_t quality, const uint32_t gain)
147{
148 if (m_EIndex>=m_channelsPerFEB) //Use m_EIndex to count total number of channels
149 {
150 m_logstr << MSG::ERROR << "LArRodBlockStructure Error: Attempt to write Energy for channel "
151 << m_EIndex << " channels into a FEB!" <<endmsg;
152 return;
153 }
154 LARBSDBG("LArRodBlockStructure: Setting Energy for channel " << m_EIndex << ". E=" << energy);
155 if (quality<0 && energy<0x7FFE && gain==0) { //Write into Low Energy block
156 m_LowEIndex++; //Use m_LowEIndex to count the channels in the Low Energy block
157 ShortLong twoValues{};
158 twoValues.s[0]=0;
159 twoValues.s[1]=0;
160 if (m_LowEIndex%2==1) { //This is an odd number, simply add data at the bottom of the block
161 twoValues.s[0]=(int16_t)energy;
162 }
163 else { //Even number: Merging with previous block
164 uint32_t oneValue=m_LowEnergyBlock[m_LowEnergyBlock.size()-1]; //Take last element of vector
165 m_LowEnergyBlock.pop_back();
166 int16_t* valptr=reinterpret_cast<int16_t*>(&oneValue);
167 twoValues.s[0]=valptr[0];
168 twoValues.s[1]=(int16_t)energy;
169 }
170 LARBSDBG("Writing words: val0= " << twoValues.s[0] << " val1= " << twoValues.s[1]);
171 m_LowEnergyBlock.push_back(twoValues.l);
172 LARBSDBG("Writing Raw data to Low E block. E=" << energy);
173 }
174 else //Write into High Energy block
175 {
177 m_HighEnergyBlock.push_back(energy);
178 uint32_t t_sign;
179 uint32_t abs_time;
180 if (time<0)
181 t_sign=1;
182 else
183 t_sign=0;
184 abs_time=abs(time);
185 if (abs_time>0x1fff)
186 abs_time=0x1fff;
187 uint32_t gtQ = (gain << 30) | (t_sign<<29) | ((abs_time & 0x1fff)<<16) | (0xffff & quality);
188 m_HighEnergyBlock.push_back(gtQ);
189 LARBSDBG("Writing Raw data to High E block. E=" << energy << " Q=" << quality);
190 }
191 m_EIndex++;
192}
193
194
195void LArRodBlockPhysicsV1::setRawData(const int channel, const std::vector<short>& samples, const uint32_t gain) {
196 //Convert Feb to Rod Channel Number:
197 int rcNb=FebToRodChannel(channel);
198 if (rcNb>=m_channelsPerFEB)
199 {
200 m_logstr << MSG::ERROR << "Attempt to write Energy for channel " << rcNb << " channels into a FEB!" << endmsg;
201 return;
202 }
203 unsigned int nsamples = getVectorHeader16(NGainNSamples) & 0x00FF;
204 if(samples.size() != nsamples) {
205 m_logstr << MSG::ERROR << "Number of samples mismatch!\n";
206 m_logstr << " nsamples =" << nsamples;
207 m_logstr << " samples.size() =" << samples.size() << endmsg;
208 std::abort();
209 }
210
211 setBit(&m_RawDataBlock[0],rcNb);
212 //Samples have 12 bit and are shifted to the left by 2 bits.
213 // odd samples in high bits, even samples in low bits
214 if((nsamples/2)*2!=nsamples) { //odd number of samples - gain is alone
215 m_RawDataBlock.push_back((gain<<30) | samples[0]<<2);
216 for (unsigned int i=1;i<nsamples;i+=2)
217 m_RawDataBlock.push_back((samples[i+1]<<18) | samples[i]<<2);
218 }
219 else { //even number of samples - gain is packed with sample 0
220 m_RawDataBlock.push_back((gain<<30) | (samples[1]<<18) | samples[0]<<2);
221 for (unsigned int i=2;i<nsamples;i+=2)
222 m_RawDataBlock.push_back((samples[i+1]<<18) | samples[i]<<2);
223 }
224}
225
226
227//For writing: takes existing Fragment and splits it into Feb-Blocks
228void LArRodBlockPhysicsV1::initializeFragment(std::vector<uint32_t>& fragment)
229{
230 m_pRODblock=&fragment; //remember pointer to fragment
231 if (fragment.size()>m_iHeadBlockSize) { //Got filled fragment
232 unsigned int sizeRead=0;
233 //Store existing data in the FEB-Map
234 while (sizeRead<fragment.size()) {
235 std::vector<uint32_t>::iterator FebIter;
236 FebIter=fragment.begin()+sizeRead; //Store pointer to current Feb-Header
237 m_FebBlock=&(*FebIter); //Set m_FebBlock in order to use getHeader-functions.
238 uint32_t currFEBid=getHeader32(FEBID); //Get this FEB-ID
239 uint16_t currFebSize=getNumberOfWords(); //Size of this FEB-Block
240 if (FebIter+currFebSize>fragment.end()) {
241 fragment.clear(); //Clear existing vector
242 m_logstr << MSG::ERROR << "Got inconsistent ROD-Fragment!" << endmsg;
243 return;
244 }
245 m_mFebBlocks[currFEBid].assign(FebIter,FebIter+currFebSize); //Copy data from ROD-fragment into FEB-Block
246 sizeRead+=currFebSize+m_MiddleHeaderSize;//6 is the middle header size
247 LARBSDBG("Found FEB-id " << currFEBid << " in existing ROD-Fragment");
248 } // end while
249 }
250 fragment.clear(); //Clear existing vector
251 return;
252}
253
254//For writing: Initalizes a single FEB-Block
256{
258 if (m_vFragment->size()<m_iHeadBlockSize) //Got empty or spoiled fragment
259 {
260 m_vFragment->resize(m_iHeadBlockSize,0); //Initialize FEB-Header
261 setHeader32(FEBID,id); //Set Feb ID
262 }
263
267}
268
270{
271 //Complete non-complete Energy block
273 setNextEnergy(0,0,-1,0);//E=0,t=0,q=-1,G=0
274 // Energies
275 unsigned int n;
276 uint16_t BlockOffset;
277 //Low energy block....
278 n = m_LowEnergyBlock.size();
279 BlockOffset=getVectorHeader16(LowEBlkOffset);
280 //Check if Low Energy Block exists and is not yet part of the fragment
281 LARBSDBG("Checking Low Energy Block n=" << n << "BlockOffset=" << BlockOffset);
282 if (n>m_NFlaggingWords && !BlockOffset)
283 {
285 for (unsigned i=0;i<n;i++)
286 m_vFragment->push_back(m_LowEnergyBlock[i]);
287 }
288 //High energy block...
289 n = m_HighEnergyBlock.size();
291 LARBSDBG("Checking High Energy Block n=" << n << "BlockOffset=" << BlockOffset);
292 //Check if High Energy-Block exists and is not yet part of the fragment
293 if (n && !BlockOffset)
294 {
296 for(unsigned int i=0;i<n;i++)
297 m_vFragment->push_back(m_HighEnergyBlock[i]);
298 }
299
300 // Raw data
301 LARBSDBG("Checking Raw Data Block");
302 n = m_RawDataBlock.size();
304 LARBSDBG("Checking Raw Data Block. n=" << n << "BlockOffset=" << BlockOffset);
305 //Check if Raw Data block exists and is not yet part of the fragment
306 if (n>m_NFlaggingWords && !BlockOffset)
308 for(unsigned int i=0;i<n;i++)
309 m_vFragment->push_back(m_RawDataBlock[i]);
310 }
312 LARBSDBG("Offsets:" << std::endl
313 << "Raw Data: " << getVectorHeader16(RawDataBlkOffset) << std::endl
314 <<"Low Energy: " << getVectorHeader16(LowEBlkOffset)<< std::endl
315 << "High Energy: " << getVectorHeader16(HighEBlkOffset)<< std::endl
316 << "Energy-index:" << m_EIndex << std::endl
317 << "Filled channels: " << m_ECounter << std::endl);
318 clearBlocks();
319}
320
321
323{
324 FEBMAPTYPE::const_iterator feb_it_b=m_mFebBlocks.begin();
325 FEBMAPTYPE::const_iterator feb_it_e=m_mFebBlocks.end();
326 FEBMAPTYPE::const_iterator feb_it;
327 for (feb_it=feb_it_b;feb_it!=feb_it_e;++feb_it) {
328 if (feb_it!=feb_it_b) //Not first Feb
330
331 //Add feb data to rod data block
332 m_pRODblock->insert (m_pRODblock->end(),
333 feb_it->second.begin(), feb_it->second.end());
334 } //end for feb_it
335
336 m_mFebBlocks.clear();
337}
338
339int LArRodBlockPhysicsV1::getNextRawData(int& channelNumber, std::vector<short>& samples, uint32_t& gain)
340{
341 LARBSDBG("in LArRodBlockPhysicsV1::getNextRawData.");
342 LARBSDBG("m_RawDataCounter=" << m_RawDataCounter << " m_RawDataIndex="<< m_RawDataIndex);
343 LARBSDBG("m_channelsPerFEB=" << m_channelsPerFEB);
344 const int flags=(int) getHeader16(RawDataBlkOffset);
345 if (!flags)
346 return 0; //Block not existing
347 if (m_RawDataCounter>=m_channelsPerFEB) //Already beyond maximal number of channels
348 return 0;
349
350 LARBSDBG("Flags="<<flags);
351 while (!getBit(m_FebBlock+flags,m_RawDataCounter)) //Look for next filled channel
352 {
354 LARBSDBG("RawDataCounter ist now " << m_RawDataCounter);
355 if (m_RawDataCounter>=m_channelsPerFEB) //No more channel availible
356 return 0;
357 }
358 LARBSDBG("Found filled channel at positon " << m_RawDataCounter);
359 //Found next filled channel
360 channelNumber=(m_RawDataCounter>>4) + ((m_RawDataCounter&0xf)<<3); //Convert ROD to FEB channel ordering
361 unsigned int nsamples = getHeader16(NGainNSamples) & 0x00FF;
362 LARBSDBG("This run has " << nsamples << " samples");
363 int index = flags+m_NFlaggingWords+m_RawDataIndex*((int) (nsamples+1)/2);
364 LARBSDBG( "index="<<index);
366 LARBSDBG("In getnextRawData(). index= " << index);
367 if((nsamples/2)*2!=nsamples) { //odd number of samples
368 const uint32_t& x = m_FebBlock[index];
369 gain = x >> 30;
370 samples.push_back((short) (x>>2 & 0xfff));
371 for(unsigned int i=1;i<=(nsamples-1)/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 }
377 else { //even number of samples
378 const uint32_t& x = m_FebBlock[index];
379 gain = x >> 30;
380 samples.push_back((short) (x>>2 & 0xfff));
381
382 samples.push_back((short) ((x>>18) & 0xfff));
383
384 for (unsigned int i=1;i<nsamples/2;i++) {
385 const uint32_t& x = m_FebBlock[index+i];
386 samples.push_back((short) (x>>2 & 0xfff));
387 samples.push_back((short) (x >> 18));
388 }
389 } //end even numer of sample
391 return 1;
392}
393
394
395//Sort functions & ordering relation:
396template<class RAWDATA>
397bool LArRodBlockPhysicsV1::operator ()
398 (const RAWDATA* ch1, const RAWDATA* ch2) const
399{
400 HWIdentifier id1 = ch1->channelID();
401 HWIdentifier id2 = ch2->channelID();
402
403 HWIdentifier febId1= m_onlineHelper->feb_Id(id1);
404 HWIdentifier febId2= m_onlineHelper->feb_Id(id2);
405
406 if(febId1 == febId2 ){
407 int cId1 = m_onlineHelper->channel(id1);
408 int cId2 = m_onlineHelper->channel(id2);
409 return FebToRodChannel(cId1) < FebToRodChannel(cId2);
410 }
411
412 return febId1 < febId2 ;
413}
414
415
416void LArRodBlockPhysicsV1::sortDataVector(std::vector<const LArRawChannel*>& vRC)
417{
418 std::sort(vRC.begin(),vRC.end(),*this);
419}
420
421void LArRodBlockPhysicsV1::sortDataVector( std::vector<const LArDigit*>& vDigit)
422{
423 std::sort(vDigit.begin(),vDigit.end(),*this);
424}
#define endmsg
static Double_t sc
#define LARBSDBG(text)
This class provides decoding/encoding from/to ROD format.
#define x
virtual void initializeFEB(const uint32_t id)
virtual int getNextRawData(int &channelNumber, std::vector< short > &samples, uint32_t &gain)
static std::string BlockType()
const LArOnlineID * m_onlineHelper
static const uint32_t m_DummyBitMap[4]
virtual void setNumberOfGains(const uint8_t n)
LArRodBlockPhysicsV1(IMessageSvc *msgSvc)
virtual void initializeFragment(std::vector< uint32_t > &fragment)
int FebToRodChannel(int ch) const
std::vector< uint32_t > m_RawDataBlock
std::vector< uint32_t > m_LowEnergyBlock
virtual void setNextEnergy(const int channel, const int32_t energy, const int32_t time, const int32_t quality, const uint32_t gain)
virtual void setRawData(const int channel, const std::vector< short > &samples, const uint32_t gain)
virtual void setNumberOfSamples(const uint8_t n)
std::vector< uint32_t > m_HighEnergyBlock
virtual void sortDataVector(std::vector< const LArRawChannel * > &)
uint16_t getVectorHeader16(const unsigned n) const
int getBit(const uint32_t *const p, const unsigned chan) const
uint16_t getHeader16(const unsigned n) const
void setHeader32(const unsigned n, const uint32_t w)
uint32_t getHeader32(const unsigned n) const
void setBit(uint32_t *const p, const unsigned chan)
void setHeader16(const unsigned n, const uint16_t w)
std::vector< uint32_t > * m_pRODblock
LArRodBlockStructure(IMessageSvc *msgSvc, const std::string &blockType)
std::vector< uint32_t > * m_vFragment
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