3 #ifndef ALIHLTTPCDATACOMPRESSIONDECODER_H
4 #define ALIHLTTPCDATACOMPRESSIONDECODER_H
5 //* This file is property of and copyright by the *
6 //* ALICE Experiment at CERN, All rights reserved. *
7 //* See cxx source for full Copyright notice *
9 /// @file AliHLTTPCDataCompressionDecoder.h
10 /// @author Matthias Richter
12 /// @brief Generic decoder class for compressed TPC data, works on a container
13 /// class implementation which fills the actual target data struct
15 #include "AliHLTLogging.h"
16 #include "AliHLTMisc.h"
17 #include "AliHLTTPCDataCompressionComponent.h"
18 #include "AliHLTTPCDefinitions.h"
19 #include "AliHLTTPCClusterDataFormat.h"
20 #include "AliHLTTPCRawCluster.h"
21 #include "AliHLTTPCTransform.h"
22 #include "AliHLTTPCTrackGeometry.h"
23 #include "AliHLTDataInflater.h"
24 #include "AliHLTTPCHWClusterMerger.h"
28 * @class AliHLTTPCDataCompressionDecoder
29 * Generic decoder class for compressed TPC data, works on a container
30 * class implementation which fills the actual target data struct
32 class AliHLTTPCDataCompressionDecoder : public AliHLTLogging {
34 AliHLTTPCDataCompressionDecoder();
35 ~AliHLTTPCDataCompressionDecoder();
38 int ReadRemainingClustersCompressed(T& c, const AliHLTUInt8_t* pData, int dataSize, AliHLTUInt32_t specification);
41 int ReadRemainingClustersCompressed(T& c, AliHLTDataInflater* pInflater, int nofClusters, AliHLTUInt32_t specification, int formatVersion=0);
44 int ReadTrackModelClustersCompressed(T& c, const AliHLTUInt8_t* pData, int dataSize, AliHLTUInt32_t specification);
47 int ReadTrackClustersCompressed(T& c, AliHLTDataInflater* pInflater, AliHLTTPCTrackGeometry* pTrackPoints);
50 int ReadClustersPartition(T& c, const AliHLTUInt8_t* pData, unsigned dataSize, AliHLTUInt32_t specification);
52 AliHLTDataInflater* CreateInflater(int deflater, int mode) const;
54 void SetPadShift(float padShift) {fPadShift=padShift;}
55 float PadShift() const {return fPadShift;}
56 void SetVerbosity(int verbosity) {fVerbosity=verbosity;}
58 int InitPartitionClusterDecoding(AliHLTUInt32_t specification);
59 int InitTrackModelClusterClusterDecoding();
60 int AddCompressionDescriptor(const AliHLTComponentBlockData* pDesc);
61 int AddClusterMCData(const AliHLTComponentBlockData* pDesc);
62 int AddClusterIds(const AliHLTComponentBlockData* pDesc);
63 AliHLTUInt32_t GetClusterId(int clusterNo) const;
64 const AliHLTTPCClusterMCLabel* GetMCLabel(AliHLTUInt32_t clusterId) const;
66 void Clear(const char* option="");
68 struct AliClusterIdBlock {
69 AliClusterIdBlock() : fIds(NULL), fSize(0) {}
70 AliHLTUInt32_t* fIds; //!
71 AliHLTUInt32_t fSize; //!
73 void Clear() {fIds=NULL; fSize=0;}
78 AliHLTTPCDataCompressionDecoder(const AliHLTTPCDataCompressionDecoder&);
79 AliHLTTPCDataCompressionDecoder& operator=(const AliHLTTPCDataCompressionDecoder&);
81 float fPadShift; //! pad shift
82 int fVerbosity; //! verbosity level
83 Bool_t fUseClusterMerger; // flag to run the cluster merger
84 AliHLTDataInflater* fpDataInflaterPartition; //! instance of inflater for partition clusters
85 AliHLTDataInflater* fpDataInflaterTrack; //! instance of inflater for track clusters
86 AliHLTTPCHWClusterMerger* fpClusterMerger; //! merger instance
88 vector<AliClusterIdBlock> fPartitionClusterIds; //! clusters ids for clusters of individual partitions
89 AliClusterIdBlock fTrackModelClusterIds; //! cluster ids for track model clusters
90 AliClusterIdBlock* fCurrentClusterIds; //! id block currently active in the iteration
91 vector<const AliHLTTPCClusterMCData*> fClusterMCData; //! references to MC data blocks
93 ClassDef(AliHLTTPCDataCompressionDecoder, 0)
97 int AliHLTTPCDataCompressionDecoder::ReadRemainingClustersCompressed(T& c, const AliHLTUInt8_t* pData, int dataSize, AliHLTUInt32_t specification)
99 // read cluster data from AliHLTTPCClusterData
101 if (!pData || dataSize<4) return -EINVAL;
103 const AliHLTUInt8_t* pBuffer=pData;
104 AliHLTUInt32_t size=dataSize;
105 const AliHLTTPCRawClusterData* clusterData = reinterpret_cast<const AliHLTTPCRawClusterData*>(pBuffer);
106 Int_t nCount = (Int_t) clusterData->fCount;
110 switch (clusterData->fVersion) {
111 case 1: deflaterMode=1; formatVersion=0; break;
112 case 2: deflaterMode=2; formatVersion=0; break;
113 case 3: deflaterMode=1; formatVersion=1; break;
114 case 4: deflaterMode=2; formatVersion=1; break;
118 if (!fpDataInflaterPartition)
119 fpDataInflaterPartition=CreateInflater(deflaterMode, 1);
121 fpDataInflaterPartition->Clear();
122 if (!fpDataInflaterPartition) return -ENODEV;
124 if ((iResult=fpDataInflaterPartition->InitBitDataInput(reinterpret_cast<const AliHLTUInt8_t*>(clusterData->fClusters),
125 size-sizeof(AliHLTTPCRawClusterData)))<0) {
129 iResult=ReadRemainingClustersCompressed(c, fpDataInflaterPartition, nCount, specification, formatVersion);
131 HLTError("cluster decoding of block 0x%08x failed, error %d", specification, iResult);
138 int AliHLTTPCDataCompressionDecoder::ReadRemainingClustersCompressed(T& c, AliHLTDataInflater* pInflater, int nofClusters, AliHLTUInt32_t specification, int formatVersion)
143 if (!pInflater) return -EINVAL;
145 if ((iResult= InitPartitionClusterDecoding(specification))<0)
148 int slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
149 int partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
150 if (slice<0 || slice>=AliHLTTPCTransform::GetNSlice()) {
151 HLTError("invalid slice %d decoded from specification 0x%08x", slice, specification);
154 if (partition<0 || partition>=AliHLTTPCTransform::GetNumberOfPatches()) {
155 HLTError("invalid partition %d decoded from specification 0x%08x", partition, specification);
158 // the compressed format stores the difference of the local row number in
159 // the partition to the row of the last cluster
160 // add the first row in the partition to get global row number
161 int rowOffset=AliHLTTPCTransform::GetFirstRow(partition);
163 int parameterId=pInflater->NextParameter();
164 if (parameterId<0) return parameterId;
165 int decodedClusterCnt=0;
167 AliHLTUInt64_t value=0;
168 AliHLTUInt32_t length=0;
169 AliHLTUInt32_t lastPadRow=0;
170 AliHLTUInt64_t lastPad64=0;
171 AliHLTUInt64_t lastTime64=0;
172 AliHLTUInt8_t isSinglePad=0;
173 AliHLTUInt8_t sign=0;
174 bool bNextCluster=true;
175 bool bReadSuccess=true;
176 AliHLTTPCRawCluster rawCluster;
178 if( fUseClusterMerger && !fpClusterMerger ){
179 fpClusterMerger = new AliHLTTPCHWClusterMerger;
183 fpClusterMerger->Clear();
185 while (decodedClusterCnt<nofClusters && bReadSuccess && pInflater->NextValue(value, length)) {
187 // switch to next cluster
191 const AliHLTTPCDefinitions::AliClusterParameter& parameter
192 =AliHLTTPCDefinitions::fgkClusterParameterDefinitions[parameterId];
194 if (parameter.fBitLength!=(int)length) {
195 HLTError("decode error: expecting length %d for parameter %s, but got %d",
196 parameter.fBitLength, parameter.fName, length);
200 switch (parameterId) {
201 case AliHLTTPCDefinitions::kPadRow:
202 {rawCluster.SetPadRow(value+lastPadRow); lastPadRow+=value;break;}
203 case AliHLTTPCDefinitions::kPad: {
204 if (formatVersion==1) {
205 bReadSuccess=bReadSuccess && pInflater->InputBit(isSinglePad);
206 if (isSinglePad==0) {
207 bReadSuccess=bReadSuccess && pInflater->InputBit(sign);
209 value=lastPad64-value;
217 if (isSinglePad==0) pad/=parameter.fScale;
218 else pad/=2; // for the sake of the 0.5 pad offset (see AliHLTTPCHWCFSpacePointContainer::WriteSorted for details)
219 rawCluster.SetPad(pad+PadShift());
222 case AliHLTTPCDefinitions::kTime: {
223 if (formatVersion==1) {
224 bReadSuccess=bReadSuccess && pInflater->InputBit(sign);
226 value=lastTime64-value;
232 float time=value; time/=parameter.fScale;
233 rawCluster.SetTime(time);
236 case AliHLTTPCDefinitions::kSigmaY2:
237 {float sigmaY2=value; sigmaY2/=parameter.fScale; rawCluster.SetSigmaY2(sigmaY2); break;}
238 case AliHLTTPCDefinitions::kSigmaZ2:
239 {float sigmaZ2=value; sigmaZ2/=parameter.fScale; rawCluster.SetSigmaZ2(sigmaZ2); break;}
240 case AliHLTTPCDefinitions::kCharge:
241 {rawCluster.SetCharge(value); break;}
242 case AliHLTTPCDefinitions::kQMax:
243 {rawCluster.SetQMax(value); break;}
245 if (parameterId>=AliHLTTPCDefinitions::kLast) {
246 AliHLTUInt32_t id=GetClusterId(decodedClusterCnt);
247 const AliHLTTPCClusterMCLabel* pMC=GetMCLabel(id);
248 if (fUseClusterMerger && fpClusterMerger && fpClusterMerger->CheckCandidate(slice, partition, rawCluster)) {
249 fpClusterMerger->AddCandidate(slice, partition, id, rawCluster, pMC);
251 c.Next(slice, partition);
252 c.SetPadRow(rawCluster.GetPadRow()+rowOffset);
253 c.SetPad(rawCluster.GetPad());
254 c.SetTime(rawCluster.GetTime());
255 c.SetSigmaY2(rawCluster.GetSigmaY2());
256 c.SetSigmaZ2(rawCluster.GetSigmaZ2());
257 c.SetCharge(rawCluster.GetCharge());
258 c.SetQMax(rawCluster.GetQMax());
259 if (pMC) c.SetMC(pMC);
265 parameterId=pInflater->NextParameter();
266 if (parameterId==AliHLTTPCDefinitions::kSigmaY2 && isSinglePad==1) {
267 // skip sigmaY for single pad clusters in format version 1
268 parameterId=pInflater->NextParameter();
270 rawCluster.SetSigmaY2(0.);
273 pInflater->Pad8Bits();
275 if (pInflater->InputBit(bit)) {
276 HLTWarning("format error of compressed clusters, there is more data than expected");
278 pInflater->CloseBitDataInput();
279 int mergedClusterCnt=0;
280 if (fUseClusterMerger && fpClusterMerger) {
281 mergedClusterCnt=fpClusterMerger->Merge();
283 if (mergedClusterCnt>=0) {
284 for (AliHLTTPCHWClusterMerger::iterator i=fpClusterMerger->begin();
285 i!=fpClusterMerger->end(); i++) {
286 c.Next((*i).GetSlice(), (*i).GetPartition());
287 const AliHLTTPCRawCluster& mergedCluster=(*i).GetCluster();
288 const AliHLTTPCClusterMCLabel& mc=(*i).MCLabel();
289 c.SetPadRow(mergedCluster.GetPadRow()+rowOffset);
290 c.SetPad(mergedCluster.GetPad());
291 c.SetTime(mergedCluster.GetTime());
292 c.SetSigmaY2(mergedCluster.GetSigmaY2());
293 c.SetSigmaZ2(mergedCluster.GetSigmaZ2());
294 c.SetCharge(mergedCluster.GetCharge());
295 c.SetQMax(mergedCluster.GetQMax());
301 iResult=mergedClusterCnt;
303 HLTDebug("copied %d cluster(s) from merger, %d merged, specification 0x%08x", remainingCnt, mergedClusterCnt, specification);
304 fpClusterMerger->Clear();
306 if (iResult>=0 && nofClusters!=outClusterCnt+mergedClusterCnt) {
308 HLTError("error reading compressed cluster format of block 0x%08x: expected %d, read %d cluster(s), merged %d cluster(s)", specification, nofClusters, outClusterCnt, mergedClusterCnt);
311 if (iResult<0) return iResult;
312 return outClusterCnt;
316 int AliHLTTPCDataCompressionDecoder::ReadTrackModelClustersCompressed(T& c, const AliHLTUInt8_t* pData, int dataSize, AliHLTUInt32_t /*specification*/)
318 // read cluster data from the track model data block
320 int dataOffset=sizeof(AliHLTTPCDataCompressionComponent::AliHLTTPCTrackModelBlock);
321 if (!pData || dataSize<dataOffset) return -EINVAL;
323 const AliHLTTPCDataCompressionComponent::AliHLTTPCTrackModelBlock* trackModelBlock=reinterpret_cast<const AliHLTTPCDataCompressionComponent::AliHLTTPCTrackModelBlock*>(pData);
324 if (trackModelBlock->fVersion!=1) {
325 HLTError("unknown version %d", trackModelBlock->fVersion);
328 if (!fpDataInflaterTrack)
329 fpDataInflaterTrack=CreateInflater(trackModelBlock->fDeflaterMode, 2);
331 fpDataInflaterTrack->Clear();
332 if (!fpDataInflaterTrack) {
333 HLTError("failed to create the data inflater for mode %d", trackModelBlock->fDeflaterMode);
336 int nofTracks=trackModelBlock->fTrackCount;
337 dataOffset+=trackModelBlock->fGlobalParameterCnt*sizeof(trackModelBlock->fGlobalParameters);
338 if (dataSize<dataOffset) {
339 HLTError("inconsistent data block, size %d, expecting at least %d to read AliHLTTPCTrackModelBlock with %d global parameters", dataSize, dataOffset, trackModelBlock->fGlobalParameterCnt);
343 float driftTimeFactorA=0.;
344 float driftTimeOffsetA=0.;
345 float driftTimeFactorC=0.;
346 float driftTimeOffsetC=0.;
348 AliHLTUInt32_t parameterIndex=0;
349 switch (trackModelBlock->fGlobalParameterCnt) {
351 bz =trackModelBlock->fGlobalParameters[parameterIndex++];
352 driftTimeFactorA=trackModelBlock->fGlobalParameters[parameterIndex++];
353 driftTimeOffsetA=trackModelBlock->fGlobalParameters[parameterIndex++];
354 driftTimeFactorC=trackModelBlock->fGlobalParameters[parameterIndex++];
355 driftTimeOffsetC=trackModelBlock->fGlobalParameters[parameterIndex++];
358 HLTError("unknown version of global parameters %d", trackModelBlock->fGlobalParameterCnt);
362 if (parameterIndex!=trackModelBlock->fGlobalParameterCnt) {
363 HLTError("internal error, size of parameter array has changed without providing all values");
367 for (int trackno=0; trackno<nofTracks; trackno++) {
368 AliHLTTPCTrackGeometry trackpoints;
369 trackpoints.InitDriftTimeTransformation(driftTimeFactorA, driftTimeOffsetA, driftTimeFactorC, driftTimeOffsetC);
370 AliHLTUInt32_t clusterBlockSize=0;
371 if ((iResult=trackpoints.Read(pData+dataOffset, dataSize-dataOffset, bz, clusterBlockSize))<0) {
375 if (dataSize-dataOffset<(int)clusterBlockSize) {
376 HLTError("to little data in buffer to read cluster block of size %d for track no %d", clusterBlockSize, trackno);
379 if ((iResult=fpDataInflaterTrack->InitBitDataInput(pData+dataOffset, clusterBlockSize))<0) {
382 if ((iResult=ReadTrackClustersCompressed(c, fpDataInflaterTrack, &trackpoints))<0) {
383 HLTError("reading of associated clusters failed for track %d", trackno);
386 fpDataInflaterTrack->Pad8Bits();
388 if (fpDataInflaterTrack->InputBit(bit)) {
389 HLTWarning("format error of compressed clusters, there is more data than expected");
391 fpDataInflaterTrack->CloseBitDataInput();
392 dataOffset+=clusterBlockSize;
399 int AliHLTTPCDataCompressionDecoder::ReadTrackClustersCompressed(T& c, AliHLTDataInflater* pInflater, AliHLTTPCTrackGeometry* pTrackPoints)
404 if (!pInflater || !pTrackPoints) return -EINVAL;
406 if ((iResult= InitTrackModelClusterClusterDecoding())<0)
409 const vector<AliHLTTrackGeometry::AliHLTTrackPoint>& rawTrackPoints=pTrackPoints->GetRawPoints();
410 vector<AliHLTTrackGeometry::AliHLTTrackPoint>::const_iterator currentTrackPoint=rawTrackPoints.begin();
412 bool bReadSuccess=true;
413 AliHLTUInt32_t clusterCountBitLength=AliHLTTPCDefinitions::fgkClusterParameterDefinitions[AliHLTTPCDefinitions::kClusterCount].fBitLength;
414 //unsigned long dataPosition=pInflater->GetCurrentByteInputPosition();
415 for (unsigned row=0; row<159 && bReadSuccess; row++) {
416 AliHLTUInt8_t haveClusters=0;
417 // 1 bit for clusters on that padrow
418 bReadSuccess=bReadSuccess && pInflater->InputBit(haveClusters);
419 if (!haveClusters) continue;
422 if (currentTrackPoint==rawTrackPoints.end()) {
423 if (bEscape || rawTrackPoints.begin()==rawTrackPoints.end()) break;
424 currentTrackPoint=rawTrackPoints.begin();
427 if (AliHLTTPCTransform::GetFirstRow(AliHLTTPCSpacePointData::GetPatch(currentTrackPoint->GetId())) +
428 AliHLTTPCSpacePointData::GetNumber(currentTrackPoint->GetId()) == row) {
433 if (currentTrackPoint==rawTrackPoints.end()) {
434 HLTError("decoding error, can not find track point on row %d", row);
437 AliHLTUInt8_t slice = AliHLTTPCSpacePointData::GetSlice(currentTrackPoint->GetId());
438 AliHLTUInt8_t partition = AliHLTTPCSpacePointData::GetPatch(currentTrackPoint->GetId());
439 AliHLTUInt8_t nofClusters=0;
440 bReadSuccess=bReadSuccess && pInflater->InputBits(nofClusters, clusterCountBitLength);
441 if (!bReadSuccess) break;
442 HLTDebug("slice %02d partition %d row %03d: %d cluster(s)", slice, partition, row, nofClusters);
444 static const AliHLTTPCDefinitions::AliClusterParameterId_t kParameterIdMapping[] = {
445 AliHLTTPCDefinitions::kResidualPad,
446 AliHLTTPCDefinitions::kResidualTime,
447 AliHLTTPCDefinitions::kSigmaY2,
448 AliHLTTPCDefinitions::kSigmaZ2,
449 AliHLTTPCDefinitions::kCharge,
450 AliHLTTPCDefinitions::kQMax,
455 AliHLTUInt64_t value=0;
456 AliHLTUInt32_t length=0;
457 bool bNextCluster=true;
458 while (bReadSuccess && inClusterCnt<nofClusters && pInflater->NextValue(value, length)) {
460 // switch to next cluster
461 c.Next(slice, partition);
465 const AliHLTTPCDefinitions::AliClusterParameter& parameter
466 =AliHLTTPCDefinitions::fgkClusterParameterDefinitions[kParameterIdMapping[parameterId]];
468 if (parameter.fBitLength!=(int)length) {
469 HLTError("decode error: expecting length %d for parameter %s, but got %d",
470 parameter.fBitLength, parameter.fName, length);
474 bool lastParameter=false;
475 switch (kParameterIdMapping[parameterId]) {
476 case AliHLTTPCDefinitions::kResidualPad:
478 AliHLTUInt8_t sign=0;
479 bReadSuccess=bReadSuccess && pInflater->InputBit(sign);
480 AliHLTUInt64_t trackpad64=0;
481 double trackpad=currentTrackPoint->GetU();
482 trackpad*=AliHLTTPCDefinitions::fgkClusterParameterDefinitions[AliHLTTPCDefinitions::kResidualPad].fScale;
483 if (currentTrackPoint->GetU()>0.) trackpad64=(AliHLTUInt64_t)round(trackpad);
485 value=trackpad64-value;
489 float pad=((float)value)/parameter.fScale;
490 c.SetPad(pad+PadShift());
493 case AliHLTTPCDefinitions::kResidualTime:
495 AliHLTUInt8_t sign=0;
496 bReadSuccess=bReadSuccess && pInflater->InputBit(sign);
497 AliHLTUInt64_t tracktime64=0;
498 double tracktime=currentTrackPoint->GetV();
499 tracktime*=AliHLTTPCDefinitions::fgkClusterParameterDefinitions[AliHLTTPCDefinitions::kResidualTime].fScale;
500 if (currentTrackPoint->GetV()>0.) tracktime64=(AliHLTUInt64_t)round(tracktime);
502 value=tracktime64-value;
506 float time=((float)value)/parameter.fScale;
510 case AliHLTTPCDefinitions::kSigmaY2:
511 {float sigmaY2=value; sigmaY2/=parameter.fScale; c.SetSigmaY2(sigmaY2); break;}
512 case AliHLTTPCDefinitions::kSigmaZ2:
513 {float sigmaZ2=value; sigmaZ2/=parameter.fScale; c.SetSigmaZ2(sigmaZ2); break;}
514 case AliHLTTPCDefinitions::kCharge:
515 {c.SetCharge(value); break;}
516 case AliHLTTPCDefinitions::kQMax:
517 {c.SetQMax(value); lastParameter=true; break;}
520 HLTError("parameter %d not expected", kParameterIdMapping[parameterId]);
524 // switch to next cluster
531 if (iResult>=0 && nofClusters!=inClusterCnt) {
533 HLTError("error reading track model compressed cluster format of track: expected %d, read only %d cluster(s)", nofClusters, inClusterCnt);
542 int AliHLTTPCDataCompressionDecoder::ReadClustersPartition(T& c, const AliHLTUInt8_t* pData, unsigned dataSize, AliHLTUInt32_t specification)
544 // read raw cluster data
545 if (!pData) return -EINVAL;
546 if (dataSize<sizeof(AliHLTTPCRawClusterData)) return -ENODATA;
547 const AliHLTTPCRawClusterData* clusterData = reinterpret_cast<const AliHLTTPCRawClusterData*>(pData);
548 Int_t nCount = (Int_t) clusterData->fCount;
549 if (clusterData->fVersion!=0) {
550 int iResult=ReadRemainingClustersCompressed(c, pData, dataSize, specification);
551 if (iResult>=0 && fVerbosity>0) {
552 HLTInfo("extracted %d cluster(s) from block 0x%08x", iResult, specification);
555 HLTError("cluster decoding of block 0x%08x failed, error %d", specification, iResult);
559 if (nCount*sizeof(AliHLTTPCRawCluster) + sizeof(AliHLTTPCRawClusterData) != dataSize) return -EBADF;
560 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
561 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
563 const AliHLTTPCRawCluster *clusters = clusterData->fClusters;
564 for (int i=0; i<nCount; i++) {
565 AliHLTUInt32_t id=GetClusterId(i);
566 const AliHLTTPCClusterMCLabel* pMC=GetMCLabel(id);
567 c.Next(slice, partition);
568 c.SetPadRow(clusters[i].GetPadRow());
569 c.SetPad(clusters[i].GetPad()+PadShift());
570 c.SetTime(clusters[i].GetTime());
571 c.SetSigmaY2(clusters[i].GetSigmaY2());
572 c.SetSigmaZ2(clusters[i].GetSigmaZ2());
573 c.SetCharge(clusters[i].GetCharge());
574 c.SetQMax(clusters[i].GetQMax());
575 if (pMC) c.SetMC(pMC);