1 //****************************************************************************
2 //* This file is property of and copyright by the ALICE HLT Project *
3 //* ALICE Experiment at CERN, All rights reserved. *
5 //* Primary Authors: Sergey Gorbunov, Torsten Alt *
6 //* Developers: Sergey Gorbunov <sergey.gorbunov@fias.uni-frankfurt.de> *
7 //* Torsten Alt <talt@cern.ch> *
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 AliHLTTPCHWCFSupport.cxx
20 // @author Sergey Gorbunov <sergey.gorbunov@fias.uni-frankfurt.de>
21 // @author Torsten Alt <talt@cern.ch>
22 // @brief Input interfaces for FPGA ClusterFinder Emulator for TPC
23 // @brief ( see AliHLTTPCHWCFEmulator class )
27 #include "AliHLTTPCHWCFSupport.h"
28 #include "AliHLTDataTypes.h"
29 #include "AliHLTTPCMapping.h"
30 #include "AliHLTTPCDigitReaderUnpacked.h"
31 #include "AliHLTTPCTransform.h"
32 #include "AliHLTTPCDefinitions.h"
33 #include "AliRawDataHeader.h"
34 #include "AliHLTTPCHWCFEmulator.h"
46 AliHLTTPCHWCFSupport::AliHLTTPCHWCFSupport()
52 // see header file for class documentation
53 for( int i=0; i<fgkNPatches; i++ ) fMapping[i] = 0;
57 AliHLTTPCHWCFSupport::~AliHLTTPCHWCFSupport()
59 // see header file for class documentation
60 for( int i=0; i<fgkNPatches; i++ ) delete[] fMapping[i];
61 delete[] fEventMemory;
62 delete[] fEventMCMemory;
65 AliHLTTPCHWCFSupport::AliHLTTPCHWCFSupport(const AliHLTTPCHWCFSupport&)
74 AliHLTTPCHWCFSupport& AliHLTTPCHWCFSupport::operator=(const AliHLTTPCHWCFSupport&){
80 const AliHLTUInt32_t *AliHLTTPCHWCFSupport::GetMapping( int patch )
82 // see header file for class documentation
83 if( patch<0 || patch>= fgkNPatches ){
84 HLTFatal("Wrong patch number %d, no mapping is provided.", patch);
87 if( !fMapping[patch] ) fMapping[patch] = ReadMapping(patch);
88 return fMapping[patch];
92 AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int patch, const char *mappingFileName ) const
94 // Create mapping array for one patch
95 // If no mapping file provided, reads from default file
96 // Output: mapping [] array of type AliHLTUInt32_t, where :
98 // mapping[0] == N hardware adresses in the array (mapping size is maping[0] + 1 )
99 // mapping[hwAddress] == configWord
101 // configWord consist of:
103 // bits 0-7: pad number
104 // bits 8-13: row number
105 // bit 14 : flag for border pad
106 // bit 15 : is the pad active
107 // bits 16->28 : gain calibration as 13 bit fixed point,
108 // with 1 bit position before decimal point
110 const AliHLTUInt32_t kBorderFlag = (1 << 14);
111 const AliHLTUInt32_t kActiveFlag = (1 << 15);
113 if( patch<0 || patch>5 ){
114 HLTFatal("Wrong patch number %d, no mapping is provided.", patch);
119 if( mappingFileName ){
120 filename = mappingFileName;
122 const char* basePath=getenv("ALICE_ROOT");
123 if (basePath) filename.Form("%s/TPC/mapping/Patch%d.data", basePath,patch);
127 inFile.open(filename.Data());
129 HLTFatal("Unable to open mapping file: %s This means no mapping is provided.", filename.Data());
133 // AliHLTTPCTransform::GetFirstRow returns first row in scheme A.
134 // We have to transform to scheme B by AliHLTTPCTransform::Slice2Sector.
136 UInt_t offsetSchemeB=0;
138 Int_t tmp1=0, tmp2 = 0;
139 AliHLTTPCTransform::Slice2Sector(0, AliHLTTPCTransform::GetFirstRow(patch),
141 offsetSchemeB = (UInt_t) tmp2;
144 AliHLTUInt32_t *mapping = 0;
145 AliHLTUInt32_t *rowBranchPadHw = 0;
152 if( !(inFile >> nHWAdd ) || !(inFile >> maxHWAdd) ){
153 HLTError("Mapping file for patch %d corrupted &s", patch,filename.Data());
157 if( maxHWAdd > 0xFFF ){
158 HLTError("Max hardware address exceeded for patch %d, max number is %d, number from mapping file is %d.",patch, 0xFFF, maxHWAdd+1);
162 if(nHWAdd > maxHWAdd ){
163 HLTError("Too large number of hardware addresses for patch %d: max number is %d, number from mapping file is %d.",patch, maxHWAdd, nHWAdd );
167 mapping = new AliHLTUInt32_t[maxHWAdd+2];
168 rowBranchPadHw = new AliHLTUInt32_t[nHWAdd];
169 if( !mapping || !rowBranchPadHw ){
170 HLTError("Can not allocate &d bytes of memory", (maxHWAdd+1+nHWAdd)*sizeof(AliHLTUInt32_t));
174 for( unsigned int i=0; i<maxHWAdd+2; i++ ) mapping[i] = 0;
175 for( unsigned int i=0; i<nHWAdd; i++ ) rowBranchPadHw[i] = 0;
176 mapping[0] = maxHWAdd+1;
183 if( !(inFile>>hwAdd) || !(inFile>>row) || !(inFile>>pad) ) break;
187 if ( nRead >= nHWAdd ){
188 HLTError("Too many hardware addresses: %d, expected %d, mapping file %s corrupted?", nRead+1, nHWAdd, filename.Data());
191 if (hwAdd>maxHWAdd) {
192 HLTError("hardware address exceeds max hwAddress %d, mapping file %s corrupted?", maxHWAdd, filename.Data());
196 if( row < offsetSchemeB ){
197 HLTError("row number %d below minimum %d for patch %d, mapping file %s corrupted?", row, offsetSchemeB, patch, filename.Data());
201 row -= offsetSchemeB;
204 HLTError("row number %d withing patch exceed the maximum %d for patch %d, mapping file %s corrupted?", row, 0x3F, patch, filename.Data());
209 HLTError("pad number %d exceed the maximum %d for patch %d, mapping file %s corrupted?", pad, 0xFF, patch, filename.Data());
213 bool active = true; // Currently all channels are always active
214 AliHLTUInt32_t gainCalib = (1 << 12); // Gain calib identical for all pads currently
216 AliHLTUInt32_t configWord = ( (row & 0x3F) << 8 ) | (pad & 0xFF);
217 if ( active ) configWord |= kActiveFlag;
218 configWord |= (gainCalib & 0x1FFF) << 16;
220 mapping[1+hwAdd] = configWord;
222 AliHLTUInt32_t branch = (hwAdd >> 11) & 0x1;
223 rowBranchPadHw[nRead] = (row<<25) | (branch<<24) | (pad<<16) | hwAdd;
230 if ( nRead!= nHWAdd ){
231 HLTError("Too less hardware addresses: %d, expected %d, mapping file %s corrupted?", nRead, nHWAdd, filename.Data());
236 // mark pads at borders of A/B branches
238 std::sort(rowBranchPadHw, rowBranchPadHw + nHWAdd);
239 int rowBranchLast = -1;
240 for( unsigned int i=0; i<nHWAdd; i++ ){
241 int rowBranch = rowBranchPadHw[i]>>24;
242 if( rowBranch != rowBranchLast ){
243 mapping[1+(rowBranchPadHw[i] & 0xFFF)] |= kBorderFlag;
244 rowBranchLast = rowBranch;
245 if( i>0 ) mapping[1+(rowBranchPadHw[i-1] & 0xFFF)] |= kBorderFlag;
248 mapping[1+(rowBranchPadHw[nRead-1] & 0xFFF)] |= kBorderFlag;
254 delete[] rowBranchPadHw;
264 int AliHLTTPCHWCFSupport::CreateRawEvent
265 ( const AliHLTComponentBlockData* block,
266 const AliHLTUInt32_t *&rawEvent, AliHLTUInt32_t &rawEventSize32,
267 const AliHLTTPCClusterMCLabel *&mcLabels, AliHLTUInt32_t &nMCLabels
270 // the method creates TPC raw data out of the input block
271 // MC labels are provided if possible
274 delete[] fEventMemory;
275 delete[] fEventMCMemory;
282 if( block->fPtr==NULL ){
283 HLTWarning("NULL pointer to the data block");
287 Int_t slice = AliHLTTPCDefinitions::GetMinSliceNr( *block );
288 Int_t patch = AliHLTTPCDefinitions::GetMinPatchNr( *block );
289 AliHLTTPCMapping mapping(patch);
291 const char *str=Form("slice %d patch %d:", slice, patch);
293 if ( block->fDataType == (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC) )
295 // already raw format -> only set the pointers and estimate the size
297 // read CDH header, estimate size of the data
299 AliHLTUInt64_t headerSize = sizeof(AliRawDataHeader);
301 AliRawDataHeader *cdhHeader = reinterpret_cast<AliRawDataHeader*>(block->fPtr);
303 AliHLTUInt64_t blockSize = block->fSize; // size of the raw data in bytes
305 if( cdhHeader->fSize!=0xFFFFFFFF ){ // use size information from the header
306 blockSize = cdhHeader->fSize;
307 if( blockSize > block->fSize ){
308 HLTWarning("%s Could not find a valid DDL header!",str);
313 if( blockSize < headerSize ){
314 HLTWarning("%s Buffer size is smaller than CDH header size", str);
318 rawEvent = reinterpret_cast<AliHLTUInt32_t*> (reinterpret_cast<UChar_t*>(block->fPtr)+headerSize);
319 rawEventSize32 = ( blockSize - headerSize )/sizeof(AliHLTUInt32_t);
322 else if ( block->fDataType == AliHLTTPCDefinitions::fgkUnpackedRawDataType )
325 AliHLTTPCDigitReaderUnpacked digitReader;
326 digitReader.SetUnsorted(kTRUE);
328 if( digitReader.InitBlock(block->fPtr,block->fSize,patch,slice)<0 ) {
329 HLTWarning("failed setting up digit reader (InitBlock)");
333 int nDigitsTotal = 0;
334 int nBunchesTotal = 0;
336 while( digitReader.NextChannel() ){
337 while(digitReader.NextBunch()){
339 nDigitsTotal+=digitReader.GetBunchSize();
345 if( digitReader.InitBlock(block->fPtr,block->fSize,patch,slice)<0) {
346 HLTWarning("failed setting up digit reader (InitBlock)");
350 Int_t nPadsTotal = 0;
351 Int_t firstRow = AliHLTTPCTransform::GetFirstRow(patch);
352 Int_t nRows = AliHLTTPCTransform::GetNRows(patch);
354 for( int i=0; i<nRows; i++ ){
355 nPadsTotal += AliHLTTPCTransform::GetNPads(firstRow+i);
358 AliHLTUInt32_t totalSize32 = (nDigitsTotal + nBunchesTotal*2)/3+2*nPadsTotal + 10;
359 AliHLTUInt32_t totalNMC = nDigitsTotal + 10;
361 fEventMemory = new AliHLTUInt32_t[totalSize32];
363 HLTWarning("Not enougth memory: can not allocate %d bytes",totalSize32*8);
367 fEventMCMemory = new AliHLTTPCClusterMCLabel[totalNMC];
368 if( !fEventMCMemory ){
369 HLTWarning("Not enougth memory: can not allocate %d bytes",totalNMC*sizeof(AliHLTTPCClusterMCLabel));
370 delete[] fEventMemory;
374 AliHLTUInt32_t nWords32 = 0;
375 AliHLTUInt32_t mcIndex = 0;
378 AliHLTTPCDigitData tmpDigit;
379 tmpDigit.fTrackID[0] = -1;
380 tmpDigit.fTrackID[1] = -1;
381 tmpDigit.fTrackID[2] = -1;
383 while( !err && digitReader.NextChannel() ){
385 Int_t row=digitReader.GetRow();
386 Int_t pad=digitReader.GetPad();
390 if( nWords32 >= totalSize32){ err = 1; break; }
392 AliHLTUInt32_t *header = fEventMemory + nWords32;
396 int prevTime = 10000000;
398 while(digitReader.NextBunch()){
400 Int_t nSignals = digitReader.GetBunchSize();
402 HLTWarning("Empty bunch received");
406 Int_t time = digitReader.GetTime() + nSignals-1;
408 if( time-nSignals+1<0 || time>=AliHLTTPCTransform::GetNTimeBins() ){
409 HLTWarning("Wrong time bins received: %d-%d for row %d pad %d", time-nSignals+1, time, row, pad);
413 if( time >= prevTime ){
414 HLTWarning("Unexpected order of TPC bunches in row %d, pad %d", row, pad);
418 prevTime = time-nSignals+1;
420 if( nWords32+( 2+nSignals)/3+1 >= totalSize32 ){ err = 1; break; }
421 if( mcIndex + nSignals >= totalNMC ){ err = 1; break; }
423 if( nWords10 + 2 + nSignals > 0x3FF ){
424 HLTWarning("Too much data in row %d, pad %d", row, pad);
428 nWords10 += 2 + nSignals;
430 Add10Word( nWords32, seek10, nSignals + 2 );
431 Add10Word( nWords32, seek10, time );
433 const UInt_t *bunchData = digitReader.GetSignals();
434 const AliHLTTPCDigitData *mcDigits = digitReader.GetBunchDigits();
436 HLTWarning("No MC labels found for a bunch of digits");
439 for(Int_t is=nSignals-1; is>=0; is--){
440 Add10Word( nWords32, seek10, bunchData[is] );
441 const AliHLTTPCDigitData &digit = mcDigits ?mcDigits[is] :tmpDigit;
443 for( int i=0; i<3; i++ ) if( digit.fTrackID[i] >=0 ) nmc++;
444 for( int i=0; i<3; i++ ){
445 fEventMCMemory[mcIndex].fClusterID[i].fMCID = digit.fTrackID[i];
446 fEventMCMemory[mcIndex].fClusterID[i].fWeight = 0;
447 if( digit.fTrackID[i] >=0 ){
448 fEventMCMemory[mcIndex].fClusterID[i].fWeight = ((float)bunchData[is])/nmc;
456 *header = (1<<30) | ((nWords10&0x3FF)<<16) | (mapping.GetHwAddress(row, pad) & 0xFFF);
461 HLTError("Internal error: too less memory allocated");
463 for( AliHLTUInt32_t i=0; i<nWords32; i++ ) fEventMemory[i] = AliHLTTPCHWCFEmulator::WriteBigEndian(fEventMemory[i]);
464 rawEvent = fEventMemory;
465 rawEventSize32 = nWords32;
466 mcLabels = fEventMCMemory;
470 } // unpacked data type
476 void AliHLTTPCHWCFSupport::Add10Word( AliHLTUInt32_t &nWords32, int &seek10, UInt_t data )
478 // add 10-bit data to the 32-bit word
479 // fEventMemory [nWords32] --- current 32-bit word
480 // *seek10 --- 10-bit position withing the word
481 // pointers are increased, a new word is first initialised to 0
483 data = data & 0x3FF; // truncate to 10 bits
487 fEventMemory[nWords32-1] = data<<20;
489 } else if( seek10 == 1 ){
490 fEventMemory[nWords32-1] &= 0xFFF003FF;
491 fEventMemory[nWords32-1] |= (data<<10);
493 } else if( seek10 == 0 ){
494 fEventMemory[nWords32-1] &= 0xFFFFFC00;
495 fEventMemory[nWords32-1] |= data;
502 int AliHLTTPCHWCFSupport::CheckRawData( const AliHLTUInt32_t *buffer,
503 unsigned long bufferSize32, int patch, int slice )
506 // The procedure checks consistency of the data
509 const unsigned int headerSize32 = 8;
511 if (!buffer) return 0;
513 const char *str=Form("slice %d patch %d:", slice, patch);
515 if( bufferSize32 < headerSize32 ){
516 HLTWarning("%s Buffer size is smaller than CDH header size", str);
522 AliHLTUInt32_t blockSize32 = bufferSize32; // size of the raw data in words
524 if( buffer[0]!=0xFFFFFFFF ) blockSize32 = buffer[0]/4; // use size information from the header
525 if( blockSize32 > bufferSize32 ){
526 HLTWarning(Form("%s Could not find a valid DDL header!",str));
530 UChar_t rcuVer = (UChar_t)( (buffer[1] >> 24) & 0xFF );
533 HLTWarning("%s Old data format, RCU version %d", str,rcuVer);
537 // is the block valid
538 //AliHLTUInt32_t blockAttributes = buffer[3]; // block attributes (bits 24-31) and participating sub detectors
539 //cout<<blockAttributes<<" "<<(blockAttributes >> 24)<<endl;
540 //if ( !( (blockAttributes >> 24) & 1) ) return 0;
543 const AliHLTUInt32_t* fData = buffer + headerSize32;
544 unsigned long dataSize32 = blockSize32 - headerSize32;
546 // Read the RCU trailer according to the RCU formware version specified in CDH
547 // Cross-check with version found in the trailer
548 // The two major bit should be 11 (identifies the end of the trailer)
550 AliHLTUInt32_t word = fData[dataSize32 - 1];
552 if ((word >> 30) != 3) {
553 HLTWarning("%s Last RCU trailer word not found!", str);
557 UChar_t ver = (word >> 16) & 0xFF;
558 //AliHLTUInt32_t rcuId = (Int_t)((word >> 7) & 0x1FF);
559 AliHLTUInt32_t rcuTrailerSize32 = (word & 0x7F); // size of RCU trailer data in words
562 HLTWarning("%s Wrong RCU firmware version detected: %d != %d",
567 if (rcuTrailerSize32 < 2) {
568 HLTWarning(Form("Invalid trailer size found (%d bytes) !",
569 rcuTrailerSize32*4));
573 if( rcuTrailerSize32 > dataSize32 ){
574 HLTWarning(Form("%s Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%ld bytes)!",
575 str, rcuTrailerSize32*4,dataSize32*4));
581 Int_t trailerIndex = dataSize32 - rcuTrailerSize32;
583 for( unsigned int i=trailerIndex; i<dataSize32-1; i++){
584 if ((fData[i] >> 30) != 2) {
585 HLTWarning("%s Missing RCU trailer identifier pattern!",str);
590 // Read the payload size
592 Int_t rcuPayloadSize32 = fData[trailerIndex] & 0x3FFFFFF;
594 if ( rcuPayloadSize32 + rcuTrailerSize32 != dataSize32) {
595 HLTWarning(Form("%s Inconsistent raw data size ! Raw data size - %ld bytes (from CDH), RCU trailer - %d bytes, raw data size (from RCU trailer) - %d bytes !",
597 (rcuTrailerSize32)*4,
598 rcuPayloadSize32*4));
603 //AliHLTTPCMapping *mapping = new AliHLTTPCMapping(patch);
604 const int kMaxNTimeBins = 2000;
606 UShort_t *channelData10 = new UShort_t[kMaxNTimeBins]; // cache for the decoded altro payload
608 Int_t position = 0; // current position (32-bit words) in fData
612 // Search for the next Altro channel
615 while( position < rcuPayloadSize32 ){
616 word = fData[position++];
617 if( (word >> 30) == 1) break;
619 if (position >= rcuPayloadSize32 ) break; // no next channel found
621 // extract channel payload and hw address
623 Int_t channelPayloadSize10 = (word >> 16) & 0x3FF; // payload size in 10-bit words
624 Int_t channelPayloadSize32 = (channelPayloadSize10+2)/3;
625 Bool_t channelIOErrors = (word >> 29) & 0x1; // check for readout errors
626 Short_t channelHWAddress = word & 0xFFF;
628 if( position + channelPayloadSize32-1> rcuPayloadSize32 ){
629 HLTWarning(Form("%s Inconsistent channel payload data size: expected <= %d bytes from RCU trailer, found %d bytes in the channel header!",
630 str,(rcuPayloadSize32 - position)*4, channelPayloadSize32*4 ));
634 //bool channelBranchAB = ((channelHWAddress >> 11) & 0x1);
635 // int channelFEC = ((channelHWAddress >> 7) & 0xF); // front-end card index
636 //int channelAltro = ((channelHWAddress >> 4) & 0x7); // altro chip index
637 //int channelIndex = (channelHWAddress & 0xF); // channel index
638 //int channelRow = mapping->GetRow(channelHWAddress);
639 //int channelPad = mapping->GetPad(channelHWAddress);
641 // Now unpack the Altro data: 10-bit words to 16 bit-words
643 Int_t channelData10Index = 0;// current position in the payload
645 for (Int_t iword = 0; iword < channelPayloadSize32; iword++) {
646 word = fData[position++];
647 if ((word >> 30) != 0) {
648 HLTWarning(Form("%s Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
649 str, channelHWAddress,word));
654 channelData10[channelData10Index++] = (word >> 20) & 0x3FF;
655 channelData10[channelData10Index++] = (word >> 10) & 0x3FF;
656 channelData10[channelData10Index++] = word & 0x3FF;
659 if ( channelIOErrors ) continue;
663 Int_t prevTimeBin = 1024;
664 channelData10Index = 0;
668 // Read next Altro bunch
670 if ((channelData10Index+1 >= channelPayloadSize10) ) break;
672 Int_t bunchLength = channelData10[channelData10Index++];
673 Int_t bunchStartTimeBin = channelData10[channelData10Index++];
675 if (bunchLength <= 2) {
676 // Invalid bunch size
677 HLTWarning(Form("%s Too short bunch length (%d) in Address=0x%x!",
678 str, bunchLength,channelHWAddress));
681 if( channelData10Index + bunchLength - 2 > channelPayloadSize10 ){
682 // Too long bunch detected
683 HLTWarning(Form("%s Too long bunch detected in Address=0x%x! Expected <= %d 10-bit words, found %d !",
684 str,channelHWAddress,channelPayloadSize10-channelData10Index,bunchLength));
688 if( bunchStartTimeBin-bunchLength+1 < 0) {
689 HLTWarning(Form("%s Invalid start time-bin in Address=0x%x ! (%d-%d+1) < 0",
690 str,channelHWAddress,bunchStartTimeBin,bunchLength));
693 if (bunchStartTimeBin >= prevTimeBin) {
694 HLTWarning(Form("%s Invalid start time-bin in Address=0x%x! (%d>=%d)",
695 str,channelHWAddress,bunchStartTimeBin,prevTimeBin));
699 prevTimeBin = bunchStartTimeBin-bunchLength+1;
703 //UShort_t* bunchData = &channelData10[channelData10Index]; // pointer to the current bunch samples
704 channelData10Index += bunchLength;
708 delete[] channelData10;