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: Kenneth Aamodt <Kenneth.Aamodt@student.uib.no> *
8 * for The ALICE HLT Project. *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
19 /** @file AliHLTTPCZeroSuppressionComponent.cxx
20 @author Kenneth Aamodt
22 @brief The TPC ZeroSuppression component
25 // see header file for class documentation //
27 // refer to README to build package //
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt //
34 #include "AliHLTTPCZeroSuppressionComponent.h"
35 #include "AliHLTTPCDigitReaderDecoder.h"
36 #include "AliHLTTPCTransform.h"
37 #include "AliHLTTPCDefinitions.h"
38 #include "AliHLTTPCDigitData.h"
39 #include "AliHLTTPCMapping.h"
45 #include "AliHLTAltroEncoder.h"
46 #include "AliRawDataHeader.h"
48 /** ROOT macro for the implementation of ROOT specific class methods */
49 ClassImp(AliHLTTPCZeroSuppressionComponent)
51 AliHLTTPCZeroSuppressionComponent::AliHLTTPCZeroSuppressionComponent()
55 fNumberOfPadsInRow(NULL),
61 fEndTimeBin(AliHLTTPCTransform::GetNTimeBins()),
65 fMinimumNumberOfSignals(AliHLTTPCTransform::GetNTimeBins()/2),
68 fVectorInitialized(kFALSE),
69 fValueBelowAverage(5),
72 fGetActivePads(kFALSE),
73 fSkipSendingZSData(kFALSE),
77 // see header file for class documentation
79 // refer to README to build package
81 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
84 AliHLTTPCZeroSuppressionComponent::~AliHLTTPCZeroSuppressionComponent()
86 // see header file for class documentation
87 if(fVectorInitialized){
88 DeInitializePadArray();
90 if(fNumberOfPadsInRow){
91 delete [] fNumberOfPadsInRow;
92 fNumberOfPadsInRow=NULL;
100 // Public functions to implement AliHLTComponent's interface.
101 // These functions are required for the registration process
103 const char* AliHLTTPCZeroSuppressionComponent::GetComponentID()
105 // see header file for class documentation
106 return "TPCZeroSuppression";
109 void AliHLTTPCZeroSuppressionComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
111 // see header file for class documentation
113 list.push_back( kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC );
116 AliHLTComponentDataType AliHLTTPCZeroSuppressionComponent::GetOutputDataType()
118 // see header file for class documentation
119 return kAliHLTMultipleDataType;
120 //return kAliHLTDataTypeDDLRaw;
121 // return AliHLTTPCDefinitions::fgkUnpackedRawDataType;
124 int AliHLTTPCZeroSuppressionComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
126 // see header file for class documentation
128 tgtList.push_back(kAliHLTDataTypeDDLRaw);
129 tgtList.push_back(kAliHLTDataTypeHwAddr16);
130 return tgtList.size();
133 void AliHLTTPCZeroSuppressionComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
135 // see header file for class documentation
140 AliHLTComponent* AliHLTTPCZeroSuppressionComponent::Spawn()
142 // see header file for class documentation
143 return new AliHLTTPCZeroSuppressionComponent();
146 int AliHLTTPCZeroSuppressionComponent::DoInit( int argc, const char** argv )
148 // see header file for class documentation
155 // -- zero suppression threshold
156 if ( !strcmp( argv[i], "-signal-threshold" ) || !strcmp( argv[i], "signal-threshold" ) ) {
157 fSignalThreshold = strtoul( argv[i+1], &cpErr ,0);
159 HLTError("Cannot convert signal-threshold specifier '%s'.", argv[i+1]);
166 // -- checking for nsigma-threshold, used in 2007 December run in ZeroSuppression
167 if ( !strcmp( argv[i], "-rms-threshold" ) || !strcmp( argv[i], "rms-threshold" ) ) {
168 fNRMSThreshold = strtoul( argv[i+1], &cpErr ,0);
170 HLTError("Cannot convert rms-threshold specifier '%s'. Must be integer", argv[i+1]);
177 // -- number of timebins
178 if ( !strcmp( argv[i], "-timebins" ) || !strcmp( argv[i], "ntimebins" ) || !strcmp( argv[i], "-ntimebins" )) {
179 fNTimeBins = strtoul( argv[i+1], &cpErr ,0);
180 AliHLTTPCTransform::SetNTimeBins(fNTimeBins);
181 if(fEndTimeBin>AliHLTTPCTransform::GetNTimeBins()){
182 fEndTimeBin = AliHLTTPCTransform::GetNTimeBins();
185 HLTError("Cannot convert ntimebins specifier '%s'.", argv[i+1]);
193 if ( !strcmp( argv[i], "-start-timebin" ) || !strcmp( argv[i], "start-timebin" ) ) {
194 fStartTimeBin = strtoul( argv[i+1], &cpErr ,0);
196 HLTError("Cannot convert start-timebin specifier '%s'.", argv[i+1]);
204 if ( !strcmp( argv[i], "-end-timebin" ) || !strcmp( argv[i], "end-timebin" ) ) {
205 if(strtoul( argv[i+1], &cpErr ,0)<=(UInt_t)AliHLTTPCTransform::GetNTimeBins()){
206 fEndTimeBin = strtoul( argv[i+1], &cpErr ,0);
209 HLTError("Cannot convert end-timebin specifier '%s'.", argv[i+1]);
216 // -- timebins to keep left of signal
217 if ( !strcmp( argv[i], "-timebin-left" ) || !strcmp( argv[i], "timebin-left" ) ) {
218 fLeftTimeBin = strtoul( argv[i+1], &cpErr ,0);
220 HLTError("Cannot convert timebin-left specifier '%s'.", argv[i+1]);
227 // -- timebin to keep right of signal
228 if ( !strcmp( argv[i], "-timebin-right" ) || !strcmp( argv[i], "timebin-right" ) ) {
229 fRightTimeBin = strtoul( argv[i+1], &cpErr ,0);
231 HLTError("Cannot convert timebin-right specifier '%s'.", argv[i+1]);
238 // -- value below average to subtract
239 if ( !strcmp( argv[i], "-value-below-average" ) || !strcmp( argv[i], "value-below-average" ) ) {
240 fValueBelowAverage = strtoul( argv[i+1], &cpErr ,0);
242 HLTError("Cannot convert value-below-average specifier '%s'.", argv[i+1]);
249 // -- pad occupancy limit
250 if ( !strcmp( argv[i], "-occupancy-limit" ) || !strcmp( argv[i], "occupancy-limit" ) ) {
251 fMinimumNumberOfSignals = strtoul( argv[i+1], &cpErr ,0);
253 HLTError("Cannot convert occupancy-limit specifier '%s'.", argv[i+1]);
260 // -- checking for rcu format
261 if ( !strcmp( argv[i], "oldrcuformat" ) ) {
262 fOldRCUFormat = strtoul( argv[i+1], &cpErr ,0);
264 HLTError("Cannot convert oldrcuformat specifier '%s'. Should be 0(off) or 1(on), must be integer", argv[i+1]);
271 // -- checking for pad sorting
272 if ( !strcmp( argv[i], "-sort-pads" ) || !strcmp( argv[i], "sort-pads" ) ) {
273 fSortPads = strtoul( argv[i+1], &cpErr ,0);
275 HLTError("Cannot convert sort-pads specifier '%s'. Should be 0(off) or 1(on), must be integer", argv[i+1]);
282 // -- checking for skipZSdatashipping
283 if ( !strcmp( argv[i], "-skip-sending-data" ) ) {
284 fSkipSendingZSData = kTRUE;
289 // -- checking for hw address shipping
290 if ( !strcmp( argv[i], "-send-hw-list" ) ) {
296 Logging(kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Unknown Option", "Unknown option '%s'", argv[i] );
301 if(fSkipSendingZSData == kTRUE && fSendHWList == kFALSE){
302 HLTError("Component will have no output, check your configuration.");
306 HLTDebug("using AliHLTTPCDigitReaderDecoder");
307 fDigitReader = new AliHLTTPCDigitReaderDecoder();
309 fHwAddressList.clear();
314 int AliHLTTPCZeroSuppressionComponent::DoDeinit()
316 // see header file for class documentation
320 Int_t AliHLTTPCZeroSuppressionComponent::DeInitializePadArray()
322 // see header file for class documentation
323 if(fVectorInitialized){
324 for(Int_t i=0;i<fNumberOfRows;i++){
325 for(Int_t j=0;j<fNumberOfPadsInRow[i];j++){
326 delete fRowPadVector[i][j];
327 fRowPadVector[i][j]=NULL;
329 fRowPadVector[i].clear();
331 fRowPadVector.clear();
336 void AliHLTTPCZeroSuppressionComponent::InitializePadArray(){
337 // see header file for class documentation
339 HLTFatal("Patch is not set");
343 fFirstRow = AliHLTTPCTransform::GetFirstRow(fCurrentPatch);
344 fLastRow = AliHLTTPCTransform::GetLastRow(fCurrentPatch);
346 fNumberOfRows=fLastRow-fFirstRow+1;
347 fNumberOfPadsInRow= new Int_t[fNumberOfRows];
349 memset( fNumberOfPadsInRow, 0, sizeof(Int_t)*(fNumberOfRows));
351 for(Int_t i=0;i<fNumberOfRows;i++){
352 fNumberOfPadsInRow[i]=AliHLTTPCTransform::GetNPads(i+fFirstRow);
353 AliHLTTPCPadVector tmpRow;
354 for(Int_t j=0;j<fNumberOfPadsInRow[i];j++){
355 AliHLTTPCPad *tmpPad = new AliHLTTPCPad();
357 tmpRow.push_back(tmpPad);
359 fRowPadVector.push_back(tmpRow);
361 fVectorInitialized=kTRUE;
365 int AliHLTTPCZeroSuppressionComponent::DoEvent( const AliHLTComponentEventData& evtData,
366 const AliHLTComponentBlockData* blocks,
367 AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr,
368 AliHLTUInt32_t& size,
369 vector<AliHLTComponentBlockData>& outputBlocks )
371 // see header file for class documentation
373 if (!fDigitReader) return -ENODEV;
375 AliHLTUInt32_t capacity=size;
377 if (!IsDataEvent()) return 0;
379 // HLTInfo("Entered DoEvent in AliHLTTPCZeroSuppressionComponent");
381 // == init iter (pointer to datablock)
382 const AliHLTComponentBlockData* iter = NULL;
384 // HLTInfo("Number of blocks: ",evtData.fBlockCnt);
386 fHwAddressList.clear();
388 for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
392 HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
393 evtData.fEventID, evtData.fEventID,
394 DataType2Text( iter->fDataType).c_str(),
395 DataType2Text(kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC).c_str());
397 if ( iter->fDataType != (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC)){
401 if (iter->fSize<=sizeof(AliRawDataHeader)) {
402 // forward empty DDLs and create empty HW list
403 outputBlocks.push_back(*iter);
404 if(fSendHWList == kTRUE){
405 AliHLTComponentBlockData bd;
409 bd.fDataType = kAliHLTDataTypeHwAddr16|kAliHLTDataOriginTPC;
410 bd.fSpecification = iter->fSpecification;
411 outputBlocks.push_back( bd );
416 UInt_t slice = AliHLTTPCDefinitions::GetMinSliceNr( *iter );
417 UInt_t patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
419 if(!fVectorInitialized && !fSkipSendingZSData){
421 InitializePadArray();
424 if(fDigitReader->InitBlock(iter->fPtr,iter->fSize,patch,slice)<0){
425 HLTWarning("Decoder failed to initialize, event aborted.");
428 AliHLTTPCMapping mapping(patch);
430 int nTotalChannels=0;
431 int nSkippedChannels=0;
432 short lowestOccupancy=-1;
433 float sumOccupancy=0;
435 //Here the reading of the data and the zerosuppression takes place
436 while(fDigitReader->NextChannel()){//Pad
440 Int_t row=fDigitReader->GetRow();
441 Int_t pad=fDigitReader->GetPad();
442 if(row==1000 || pad==1000){
445 if(!fSkipSendingZSData && (row>=fNumberOfRows||row<0)){
448 else if(!fSkipSendingZSData && (pad>=fNumberOfPadsInRow[row]||pad<0)){
452 AliHLTTPCPad *tmpPad = NULL;
453 if (!fSkipSendingZSData) tmpPad=fRowPadVector[row][pad];
454 if (tmpPad) tmpPad->SetDataToDefault();
456 //reading data to pad
457 while(fDigitReader->NextBunch()){
458 const UInt_t *bunchData= fDigitReader->GetSignals();
459 Int_t time=fDigitReader->GetTime();
460 for(Int_t i=0;i<fDigitReader->GetBunchSize();i++){
461 if(bunchData[i]>0){// disregarding 0 data.
462 if(time+i>=fStartTimeBin && time+i<=fEndTimeBin){
463 if (tmpPad) tmpPad->SetDataSignal(time+i,bunchData[i]);
464 sumSignals+=bunchData[i];
465 if (maxSignal<(int)bunchData[i]) maxSignal=bunchData[i];
473 if (lowestOccupancy<0 || lowestOccupancy>nofSignals)
474 lowestOccupancy=nofSignals;
475 sumOccupancy+=nofSignals;
477 if(nofSignals>=fMinimumNumberOfSignals){
479 tmpPad->ZeroSuppress(fNRMSThreshold, fSignalThreshold, fMinimumNumberOfSignals, fStartTimeBin, fEndTimeBin, fLeftTimeBin, fRightTimeBin, fValueBelowAverage, fSkipSendingZSData);
480 if(tmpPad->GetNAddedSignals()>0){
481 assert((int)mapping.GetRow(fDigitReader->GetAltroBlockHWaddr())==row);
482 assert((int)mapping.GetPad(fDigitReader->GetAltroBlockHWaddr())==pad);
483 fHwAddressList.push_back((AliHLTUInt16_t)fDigitReader->GetAltroBlockHWaddr());
486 assert(fSkipSendingZSData);
487 if (nofSignals>0 && maxSignal>(sumSignals/nofSignals)+fSignalThreshold) {
488 fHwAddressList.push_back((AliHLTUInt16_t)fDigitReader->GetAltroBlockHWaddr());
495 if (nSkippedChannels>0) {
496 HLTWarning("skipped %d of %d channels because of low occupancy: average %.2f, lowest %d, threshold %d",
497 nSkippedChannels, nTotalChannels, sumOccupancy/nTotalChannels, lowestOccupancy, fMinimumNumberOfSignals);
500 AliHLTUInt32_t dataOffsetBeforeHW=0;
502 if(fSkipSendingZSData == kFALSE && iter->fSize>sizeof(AliRawDataHeader)) {
504 AliHLTAltroEncoder *altroEncoder = new AliHLTAltroEncoder;
505 altroEncoder->SetBuffer(outputPtr,capacity); //tests if one overwrite the buffer is done in the encoder
507 // set CDH from the beginning of buffer
508 altroEncoder->SetCDH((AliHLTUInt8_t*)iter->fPtr,sizeof(AliRawDataHeader));
510 UChar_t *RCUTrailer=NULL;
511 Int_t RCUTrailerSize=fDigitReader->GetRCUTrailerSize();
512 if (RCUTrailerSize<=0 || !fDigitReader->GetRCUTrailerData( RCUTrailer )) {
513 if(RCUTrailer==NULL){
514 HLTWarning("can not find RCU trailer for data block %s 0x%08x: skipping data block",
515 DataType2Text(iter->fDataType).c_str(), iter->fSpecification);
519 altroEncoder->SetRCUTrailer(RCUTrailer, RCUTrailerSize);
521 for(unsigned int channel=0; channel<fHwAddressList.size(); channel++){
522 int row=mapping.GetRow(fHwAddressList[channel]);
523 int pad=mapping.GetPad(fHwAddressList[channel]);
525 AliHLTTPCPad * zeroSuppressedPad= fRowPadVector[row][pad];
528 if(zeroSuppressedPad->GetNAddedSignals()>0){
529 while(zeroSuppressedPad->GetNextGoodSignal(currentTime, bunchSize)){
530 for(Int_t i=0;i<bunchSize;i++){
531 if (altroEncoder->AddSignal((AliHLTUInt16_t)(zeroSuppressedPad->GetDataSignal(currentTime+i)),(AliHLTUInt16_t)(currentTime+i))<0) {
532 // Matthias 01.10.2008: there is a problem with certain real data which produces the same
533 // bunch multiple times, needs investigation. I found an examplary case in run 53465
534 // (08000053465011.450.root) equipment 981.
535 // needs to be followed up.
536 // addon 02.10.2008 I just corrected a bug concerning the loop over the active channels.
537 // A double loop over all rows and pads also considered pads which had not even been
538 // filled, and thus not properly cleaned. Maybe the bug above is related to that.
539 HLTWarning("can not add channel: slice %d, partition %d, hw address %d, row %d, pad %d, time %d, bunch size %d",
540 slice, patch, fHwAddressList[channel], row, pad, currentTime+i, bunchSize);
545 altroEncoder->SetChannel(fHwAddressList[channel]);
550 int sizeOfData=altroEncoder->SetLength();
553 HLTError("data encoding failed");
557 if(sizeOfData>(int)capacity){
558 HLTWarning("Buffer too small too add the altrodata: %d of %d byte(s) already used", sizeOfData, capacity);
563 //Push back the zerosuppressed altro data to the output
564 if(true/* condition was deprecated but keep for the sake of formatting*/){
565 AliHLTComponentBlockData bd;
568 bd.fSize = sizeOfData;
569 bd.fDataType = kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC;
570 bd.fSpecification = iter->fSpecification;
571 outputBlocks.push_back( bd );
573 //Push back the list of hardware addresses to the output
574 dataOffsetBeforeHW=sizeOfData;
578 AliHLTUInt32_t sizeOfHWArray = 0;
579 if(fSendHWList == kTRUE){
581 if(true/* condition was deprecated but keep for the sake of formatting*/){
582 sizeOfHWArray = fHwAddressList.size()*sizeof(AliHLTUInt16_t);
584 if(dataOffsetBeforeHW+sizeOfHWArray>capacity){
585 HLTWarning("Buffer too small too add the active channels: %d of %d byte(s) already used", dataOffsetBeforeHW + sizeOfHWArray, capacity);
590 AliHLTUInt16_t*outputHWPtr=(AliHLTUInt16_t*)(outputPtr+dataOffsetBeforeHW);
591 memcpy(outputHWPtr,&fHwAddressList[0],sizeOfHWArray);
592 AliHLTComponentBlockData bdHW;
593 FillBlockData( bdHW );
594 bdHW.fOffset = dataOffsetBeforeHW;
595 bdHW.fSize = sizeOfHWArray;
596 bdHW.fDataType = kAliHLTDataTypeHwAddr16|kAliHLTDataOriginTPC;
597 bdHW.fSpecification = iter->fSpecification;
598 outputBlocks.push_back( bdHW );
601 size = dataOffsetBeforeHW+sizeOfHWArray;
602 fDigitReader->Reset();
606 fDigitReader->Reset();