3 //*************************************************************************
4 // This file is property of and copyright by the ALICE HLT Project *
5 // ALICE Experiment at CERN, All rights reserved. *
7 // Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 // Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 // Jochen Thaeder <thaeder@kip.uni-heidelberg.de> *
10 // for The ALICE HLT Project. *
12 // Permission to use, copy, modify and distribute this software and its *
13 // documentation strictly for non-commercial purposes is hereby granted *
14 // without fee, provided that the above copyright notice appears in all *
15 // copies and that both the copyright notice and this permission notice *
16 // appear in the supporting documentation. The authors make no claims *
17 // about the suitability of this software for any purpose. It is *
18 // provided "as is" without express or implied warranty. *
19 //*************************************************************************/
21 /** @file AliHLTTPCDigitReaderUnpacked.cxx
22 @author Timm Steinbeck, Jochen Thaeder, Matthias Richter
24 @brief A digit reader implementation for unpacked TPC data.
32 #include "AliHLTTPCDigitReaderUnpacked.h"
33 #include "AliHLTTPCDigitData.h"
34 #include "AliHLTTPCTransform.h"
35 #include "AliHLTStdIncludes.h"
36 #include "AliHLTTPCMapping.h"
38 ClassImp(AliHLTTPCDigitReaderUnpacked)
40 AliHLTTPCDigitReaderUnpacked::AliHLTTPCDigitReaderUnpacked()
55 fEndOfDataReached(kFALSE),
56 fEndOfChannelReached(kFALSE),
58 fEndTimeBinOfBunch(0),
62 fNextChannelIsAlreadyConfirmed(kFALSE),
65 fBinRowPositionSorted(),
68 // see header file for class documentation
70 // refer to README to build package
72 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
75 AliHLTTPCDigitReaderUnpacked::~AliHLTTPCDigitReaderUnpacked(){
76 // see header file for class documentation
83 int AliHLTTPCDigitReaderUnpacked::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
84 // see header file for class documentation
86 AliHLTTPCUnpackedRawData *tmpptr=NULL;
91 fEndOfDataReached=kFALSE;
92 fEndOfChannelReached=kFALSE;
98 fNextChannelIsAlreadyConfirmed=kFALSE;
99 fDigitsVector.clear();
101 tmpptr = reinterpret_cast<AliHLTTPCUnpackedRawData*>(fPtr);
102 fDigitRowData = (AliHLTTPCDigitRowData*) tmpptr->fDigits;
103 fActRowData = fDigitRowData;
106 fMapping = new AliHLTTPCMapping(patch);
109 while (fActRowData && ((iResult=GetNextRowData(fActRowData))>=0)) {/* empty body */};
112 fActRowData = fDigitRowData;
116 AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetFirstRow(patch), dummy, fFirstRow);
117 AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetLastRow(patch), dummy, fLastRow);
121 if ((Int_t)fActRowData->fRow != fRow){
122 HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
130 int AliHLTTPCDigitReaderUnpacked::GetNextRowData(AliHLTTPCDigitRowData*& pRow) const
132 // get new row data from the current row data
134 AliHLTTPCDigitRowData* pCurrent=pRow;
137 Byte_t *tmp = (Byte_t*) pCurrent;
138 Int_t size = sizeof(AliHLTTPCDigitRowData) + pCurrent->fNDigit*sizeof(AliHLTTPCDigitData);
140 pRow = reinterpret_cast<AliHLTTPCDigitRowData*>(tmp);
142 // check if the new pointer is within the range
143 if (((Byte_t*)fPtr) + fSize <= tmp){
144 if (((Byte_t*)fPtr) + fSize < tmp) {
145 // if the increment does not match exactly there is a format error
146 HLTError("input format not recognized: buffer %p %d, current row data %p, %d digits", fPtr, fSize, pCurrent, pCurrent->fNDigit);
151 // check if the current row structure has the right format
152 size = sizeof(AliHLTTPCDigitRowData) + pRow->fNDigit*sizeof(AliHLTTPCDigitData);
154 if (((Byte_t*)fPtr) + fSize < tmp){
155 HLTError("Current row data not recognized %p (buffer %p %d) %d digits", pRow, fPtr, fSize, pRow->fNDigit);
163 void AliHLTTPCDigitReaderUnpacked::SortBunchBinVector(){
164 fBinRowPositionSorted.clear();
166 fBinRowPositionSorted.push_back(0);
168 Int_t beginningOfPadIndex=0;
170 for(Int_t i=1;i<(Int_t)fActRowData->fNDigit;i++){
171 if(fData[i-1].fPad == fData[i].fPad){// means that these sinals belong to the same pad
172 if(fData[i-1].fTime+1 == fData[i].fTime){ //means that the signal belong to the same bunch
175 fBinRowPositionSorted.insert(fBinRowPositionSorted.begin()+beginningOfPadIndex+nAdded,i);
177 else{//we have a new bunch on this pad, put it in fornt of the previous bunch
180 fBinRowPositionSorted.insert(fBinRowPositionSorted.begin()+beginningOfPadIndex,i);
185 beginningOfPadIndex=totalAdded;
186 fBinRowPositionSorted.push_back(i);
192 bool AliHLTTPCDigitReaderUnpacked::NextSignal(){
193 // see header file for class documentation
194 if (fActRowData==NULL) return false;
196 bool rreadvalue = true;
200 while ( fBin >= (Int_t)fActRowData->fNDigit ){
202 if ((fRow >= fFirstRow) && (fRow <= fLastRow)){
205 if (GetNextRowData(fActRowData)<0) {
218 if ((Int_t)fActRowData->fRow != fRow){
219 HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
223 fData = fActRowData->fDigitData;
226 SortBunchBinVector();
230 fPrevPad = GetSortedPad();
236 int AliHLTTPCDigitReaderUnpacked::GetRow(){
237 // see header file for class documentation
240 if(fUnsorted == kFALSE){
244 rrow = fRow-AliHLTTPCTransform::GetFirstRow(fPatch);
246 rrow += AliHLTTPCTransform::GetFirstRow(2);
253 int AliHLTTPCDigitReaderUnpacked::GetPad(){
254 // see header file for class documentation
256 if(fUnsorted == kFALSE){
257 rpad = GetSortedPad();
265 int AliHLTTPCDigitReaderUnpacked::GetSignal(){
266 // see header file for class documentation
268 if(fUnsorted == kFALSE){
269 rsignal = GetSortedSignal();
272 rsignal = fPrevSignal;
277 int AliHLTTPCDigitReaderUnpacked::GetTime(){
278 // see header file for class documentation
280 if(fUnsorted==kFALSE){
281 rtime = GetSortedTime();
284 rtime = fPrevTime+1-fDataBunch.size();
289 AliHLTUInt32_t AliHLTTPCDigitReaderUnpacked::GetAltroBlockHWaddr() const
291 // see header file for class documentation
292 return (AliHLTUInt32_t)(fMapping->GetHwAddress((UInt_t)GetSortedRow(),(UInt_t)GetSortedPad()));//fTPCRawStream->GetHWAddress();
295 AliHLTTPCDigitData AliHLTTPCDigitReaderUnpacked::GetSortedDigit(){
296 // see header file for class documentation
298 if (!fData) return AliHLTTPCDigitData();
299 return fData[fBinRowPositionSorted.at(fBin)];
302 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedTime(){
303 // see header file for class documentation
305 if (!fData) return -1;
307 rtime = (int)fData[fBinRowPositionSorted.at(fBin)].fTime;
308 if(fDataBunch.size()>1){
309 fEndTimeBinOfBunch=rtime;
314 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedSignal(){
315 // see header file for class documentation
317 if (!fData) return -1;
319 rsignal = (int)fData[fBinRowPositionSorted.at(fBin)].fCharge;
324 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedPad() const{
325 // see header file for class documentation
327 if (!fData) return -1;
329 rpad = (int)fData[fBinRowPositionSorted.at(fBin)].fPad;
333 int AliHLTTPCDigitReaderUnpacked::GetSortedRow() const {
334 // see header file for class documentation
336 rrow = fRow-AliHLTTPCTransform::GetFirstRow(fPatch);
338 rrow += AliHLTTPCTransform::GetFirstRow(2);
343 bool AliHLTTPCDigitReaderUnpacked::NextChannel()
345 // see header file for class documentation
347 // If the next channel is already confirmed by the next bunch function
348 // or there are more signals (this will only be for the first signal)
350 if(fEndOfDataReached == kTRUE){
353 if(fNextChannelIsAlreadyConfirmed){
354 fNextChannelIsAlreadyConfirmed = kFALSE;
355 fPrevTime=GetSortedTime();
356 fPrevSignal=GetSortedSignal();
357 fPrevPad = GetSortedPad();
358 fPrevRow = GetSortedRow();
360 if(GetAltroBlockHWaddr() == (UInt_t)-1){
362 return NextChannel();
367 else if(NextSignal()) { // there is data
369 if(GetAltroBlockHWaddr() == (UInt_t)-1){
371 return NextChannel();
378 int AliHLTTPCDigitReaderUnpacked::NextBunch()
380 // see header file for class documentation
382 if(fEndOfDataReached == kTRUE || fEndOfChannelReached == kTRUE){
383 // sets fEndOfChannelReached back to false, for the next channel
384 // and returns 0 to tell stop the NextBunch calls for this channel.
385 fEndOfChannelReached=kFALSE;
391 fDigitsVector.clear();
393 //adding the first signal (will always be the leftover from either NextChannel call or previous bunch)
394 fPrevTime=GetSortedTime();
395 fPrevSignal=GetSortedSignal();
396 fPrevPad = GetSortedPad();
397 fPrevRow = GetSortedRow();
398 fDataBunch.push_back(GetSortedSignal());
399 fDigitsVector.push_back(GetSortedDigit());
403 if((fPrevPad == GetSortedPad()) && (fPrevRow == GetSortedRow())){//check if there is a change in channel(new pad or row)
404 if(fPrevTime+1 == GetSortedTime()){//if true means that we have consecutive signals
405 fPrevTime = GetSortedTime();
406 //fDataBunch.insert(fDataBunch.begin(), GetSortedSignal());// add the signal to the beginning of the buffer
407 fDataBunch.push_back(GetSortedSignal());// add the signal to the beginning of the buffer
408 fDigitsVector.push_back(GetSortedDigit());
410 else{//end of bunch but not of channel
415 // end of channel, last bunch will be completed
416 fEndOfChannelReached = kTRUE;
417 // the next channel is already confirmed since the next channel returned true
418 fNextChannelIsAlreadyConfirmed = kTRUE;
423 // end of data, but there is one bunch to be completed.
424 fEndOfDataReached = kTRUE;
429 return fDataBunch.size();
432 int AliHLTTPCDigitReaderUnpacked::GetBunchSize(){
433 // see header file for class documentation
434 return fDataBunch.size();
437 const UInt_t* AliHLTTPCDigitReaderUnpacked::GetSignals()
439 // see header file for class documentation
440 return &fDataBunch[0];
443 const AliHLTTPCDigitData* AliHLTTPCDigitReaderUnpacked::GetBunchDigits()
445 // see header file for class documentation
446 return &fDigitsVector[0];
448 int AliHLTTPCDigitReaderUnpacked::GetRowOffset() const
450 // see header file for class documentation
451 return AliHLTTPCTransform::GetFirstRow(fPatch);