]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/comp/AliHLTTPCCompModelInflater.cxx
adding more monitoring histograms, correcting axis labels (Alberica)
[u/mrichter/AliRoot.git] / HLT / TPCLib / comp / AliHLTTPCCompModelInflater.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
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 //**************************************************************************
18
19 /** @file   AliHLTTPCCompModelInflater.cxx
20     @author Timm Steinbeck
21     @date   
22     @brief  A copy processing component for the HLT.
23     * 
24     * The model inflater is the counterpart of the deflater component
25     * which decompresses the data after a conversion and compression.
26     * The deflater is followed by a deconverter in order to get the
27     * original standard HLT cluster track format back
28     *
29  */
30
31 #if __GNUC__ >= 3
32 using namespace std;
33 #endif
34
35 #include "AliHLTTPCCompModelInflater.h"
36 #include "AliHLTTPCTransform.h"
37 #include "AliHLTTPCTrack.h"
38 #include "AliHLTTPCModelTrack.h"
39 #include "AliHLTTPCCompDataCompressorHelper.h"
40 #include "AliHLTDataTypes.h"
41 #include <cerrno>
42
43 AliHLTTPCCompModelInflater::AliHLTTPCCompModelInflater():
44     fBitDataCurrentWord(0),
45     fBitDataCurrentPosInWord(0),
46     fBitDataCurrentInput(0),
47     fBitDataCurrentInputStart(0),
48     fBitDataCurrentInputEnd(0)
49     {
50       // see header file for class documentation
51     }
52
53 AliHLTTPCCompModelInflater::~AliHLTTPCCompModelInflater()
54     {
55       // see header file for class documentation
56     }
57
58 void AliHLTTPCCompModelInflater::InitBitDataInput( AliHLTUInt8_t* input, UInt_t inputSize )
59     {
60       // sse header file for class documentation
61       fBitDataCurrentWord = 0;
62       fBitDataCurrentPosInWord = 7;
63       fBitDataCurrentInput = fBitDataCurrentInputStart = input;
64       fBitDataCurrentInputEnd = input+inputSize;
65       fBitDataCurrentWord = *fBitDataCurrentInput;
66     }
67
68 bool AliHLTTPCCompModelInflater::InputBit( AliHLTUInt8_t & value )
69     {
70       // see header file for class documenation
71       if ( fBitDataCurrentInput>=fBitDataCurrentInputEnd )
72         return false;
73       value = (fBitDataCurrentWord >> fBitDataCurrentPosInWord) & 1;
74       if ( fBitDataCurrentPosInWord )
75         fBitDataCurrentPosInWord--;
76       else
77         {
78           fBitDataCurrentInput++;
79           if ( fBitDataCurrentInput<fBitDataCurrentInputEnd )
80             {
81               fBitDataCurrentWord = *fBitDataCurrentInput;
82               fBitDataCurrentPosInWord = 7;
83             }
84         }
85       return true;
86     }
87
88 bool AliHLTTPCCompModelInflater::InputBits( AliHLTUInt8_t & value, UInt_t const & bitCount )
89     {
90       // see header file for clas documentation
91       if ( bitCount>8 )
92         {
93           HLTFatal( "Internal error: Attempt to write more than 32 bits (%u)", (unsigned)bitCount );
94           return false;
95         }
96       AliHLTUInt64_t temp;
97       if ( !InputBits( temp, bitCount ) )
98         return false;
99       value = (AliHLTUInt8_t)( temp & (AliHLTUInt64_t)0xFFFFFFFFULL );
100       return true;
101     }
102
103 bool AliHLTTPCCompModelInflater::InputBits( AliHLTUInt16_t & value, UInt_t const & bitCount )
104     {
105       // see header file for class documenation
106       if ( bitCount>16 )
107         {
108           HLTFatal( "Internal error: Attempt to write more than 32 bits (%u)", (unsigned)bitCount );
109           return false;
110         }
111       AliHLTUInt64_t temp;
112       if ( !InputBits( temp, bitCount ) )
113         return false;
114       value = (AliHLTUInt16_t)( temp & (AliHLTUInt64_t)0xFFFFFFFFULL );
115       return true;
116     }
117
118 bool AliHLTTPCCompModelInflater::InputBits( AliHLTUInt32_t & value, UInt_t const & bitCount )
119     {
120       // see header file for class documentation
121       if ( bitCount>32 )
122         {
123           HLTFatal( "Internal error: Attempt to write more than 32 bits (%u)", (unsigned)bitCount );
124           return false;
125         }
126       AliHLTUInt64_t temp;
127       if ( !InputBits( temp, bitCount ) )
128         return false;
129       value = (AliHLTUInt32_t)( temp & (AliHLTUInt64_t)0xFFFFFFFFULL );
130       return true;
131     }
132
133 bool AliHLTTPCCompModelInflater::InputBits( Int_t & value, UInt_t const & bitCount )
134     {
135       // see header file for class documentation
136       if ( bitCount>32 )
137         {
138           HLTFatal( "Internal error: Attempt to write more than 32 bits (%u)", (unsigned)bitCount );
139           return false;
140         }
141       AliHLTUInt64_t temp;
142       if ( !InputBits( temp, bitCount ) )
143         return false;
144       value = (Int_t)( temp & (AliHLTUInt64_t)0xFFFFFFFFULL );
145       return true;
146     }
147
148 bool AliHLTTPCCompModelInflater::InputBits( AliHLTUInt64_t & value, UInt_t const & bitCount )
149     {
150       // see header file for class documenation
151       if ( bitCount>64 )
152         {
153           HLTFatal( "Internal error: Attempt to write more than 64 bits (%u)", (unsigned)bitCount );
154           return false;
155         }
156       UInt_t bitsToRead=bitCount;
157       UInt_t curBitCount;
158       value = 0;
159       while ( bitsToRead>0 )
160         {
161           if ( fBitDataCurrentInput>=fBitDataCurrentInputEnd )
162             return false;
163           if ( bitsToRead >= fBitDataCurrentPosInWord+1 )
164             curBitCount = fBitDataCurrentPosInWord+1;
165           else
166             curBitCount = bitsToRead;
167           value = (value << curBitCount) | ( (fBitDataCurrentWord >> (fBitDataCurrentPosInWord-curBitCount+1)) & ((1 << curBitCount)-1) );
168           if ( fBitDataCurrentPosInWord < curBitCount )
169             {
170               fBitDataCurrentInput++;
171               if ( fBitDataCurrentInput<fBitDataCurrentInputEnd )
172                 {
173                   fBitDataCurrentWord = *fBitDataCurrentInput;
174                   fBitDataCurrentPosInWord = 7;
175                 }
176             }
177           else
178             fBitDataCurrentPosInWord -= curBitCount;
179           bitsToRead -= curBitCount;
180         }
181       return true;
182     }
183
184 void AliHLTTPCCompModelInflater::Pad8Bits()
185     {
186       // see header file for class documenation
187       if ( fBitDataCurrentPosInWord == 7 )
188         return;
189       fBitDataCurrentInput++;
190       if ( fBitDataCurrentInput<fBitDataCurrentInputEnd )
191         {
192           fBitDataCurrentWord = *fBitDataCurrentInput;
193           fBitDataCurrentPosInWord = 7;
194         }
195     }
196
197 bool AliHLTTPCCompModelInflater::InputBytes( AliHLTUInt8_t* data, UInt_t const & byteCount )
198     {
199       // see header file for class documenation
200       Pad8Bits();
201       if ( fBitDataCurrentInput+byteCount>fBitDataCurrentInputEnd )
202         return false;
203       memcpy( data, fBitDataCurrentInput, byteCount );
204       fBitDataCurrentInput += byteCount;
205       if ( fBitDataCurrentInput<fBitDataCurrentInputEnd )
206         {
207           fBitDataCurrentWord = *fBitDataCurrentInput;
208           fBitDataCurrentPosInWord = 7;
209         }
210       return true;
211     }
212
213 int AliHLTTPCCompModelInflater::DecompressTracks( AliHLTUInt8_t* inData, UInt_t const& inputSize, AliHLTUInt8_t* output, UInt_t& outputSize )
214     {
215       // see header file for class documentation
216       AliHLTUInt8_t* inputPtr = inData;
217       //AliHLTUInt8_t* inputEndPtr = inData+inputSize;
218       AliHLTUInt8_t* outputPtr = output;
219       AliHLTUInt8_t* outputEndPtr = output+outputSize;
220       
221       printf( "outuptSize: %lu\n", (unsigned long)outputSize );
222       
223       InitBitDataInput( inputPtr, inputSize );
224       AliHLTUInt8_t version;
225       if ( !InputBits( version, 4 ) ) // Version information
226         {
227           HLTError( "Corrupt input data. Cannot read data version number at position %u",
228                     (unsigned)(inputPtr-inData) );
229           return EIO;
230         }
231       if ( version != 0 )
232         {
233           HLTError( "Unsupported version %hu. Only version 0 supported currently.", version );
234           return EIO;
235         }
236       AliHLTUInt8_t readShape;
237       if ( !InputBit( readShape ) ) // Data format flag
238         {
239           HLTError( "Corrupt input data. Cannot read shape flag at position %u",
240                     (unsigned)(inputPtr-inData) );
241           return EIO;
242         }
243       
244       if ( outputPtr+sizeof(AliHLTUInt32_t)>outputEndPtr )
245         {
246           HLTError( "Not enough space to write decompressed data. %lu already written",
247                     (unsigned)(outputPtr-output) );
248           return ENOBUFS;
249         }
250       *(AliHLTUInt32_t*)outputPtr = 0; // Write format version number
251       outputPtr += sizeof(AliHLTUInt32_t);
252       
253       Pad8Bits();
254       
255       AliHLTTPCClusterModel *cluster;
256       
257       Int_t chargeo,padshapeo,timeshapeo;
258       chargeo=padshapeo=timeshapeo=0;
259       unsigned trackCnt=0;
260       while( !EndOfBitInput() )
261         {
262           if ( outputPtr+sizeof(AliHLTTPCTrackModel)>outputEndPtr )
263             {
264               HLTError( "Not enough space to write decompressed data. %lu already written",
265                         (unsigned)(outputPtr-output) );
266               return ENOBUFS;
267             }
268           if ( !InputBytes( outputPtr, sizeof(AliHLTTPCTrackModel) ) )
269             {
270               HLTError( "Corrupt input data. Cannot read track model data at position %u",
271                         (unsigned)(inputPtr-inData) );
272               return EIO;
273             }
274           outputPtr += sizeof(AliHLTTPCTrackModel);
275           
276           Int_t clustercount=0;
277           AliHLTUInt32_t slice;
278           for(Int_t i=0; i<AliHLTTPCTransform::GetNRows(); i++)
279             {
280               if ( outputPtr+sizeof(AliHLTTPCClusterModel)>outputEndPtr )
281                 {
282                   HLTError( "Not enough space to write decompressed data. %lu already written",
283                             (unsigned)(outputPtr-output) );
284                   return ENOBUFS;
285                 }
286               cluster = (AliHLTTPCClusterModel*)outputPtr;
287               outputPtr += sizeof(AliHLTTPCClusterModel);
288               
289               // Read present flag:
290               AliHLTUInt8_t present;
291               if ( !InputBit( present ) )
292                 {
293                   HLTError( "Corrupt input data. Cannot read cluster presence bit at position %u",
294                             (unsigned)(inputPtr-inData) );
295                   return EIO;
296                 }
297               HLTDebug( "Cluster for row %d %s", i, (present ? "present" : "not present") );
298               cluster->fPresent = present;
299               if ( !present )
300                 continue;
301               
302               
303               //Read slice number of first point
304               if ( clustercount==0 )
305                 {
306                   if ( !InputBits( slice,6 ) ) //Need 6 bits to encode slice number
307                     {
308                       HLTError( "Corrupt input data. Cannot read cluster slice number at position %u",
309                                 (unsigned)(inputPtr-inData) );
310                       return EIO;
311                     }
312                 }
313               else
314                 {
315                   AliHLTUInt8_t sliceChange;
316                   if ( !InputBit( sliceChange ) )
317                     {
318                       HLTError( "Corrupt input data. Cannot read cluster slice change bit at position %u",
319                                 (unsigned)(inputPtr-inData) );
320                       return EIO;
321                     }
322                   if ( sliceChange )
323                     {  //Change of slice
324                       if ( !InputBits( slice, 6 ) )
325                         {
326                           HLTError( "Corrupt input data. Cannot read cluster slice number at position %u",
327                                     (unsigned)(inputPtr-inData) );
328                           return EIO;
329                         }
330                     }
331                 }
332               HLTDebug( "Slice: %d", slice );
333               cluster->fSlice = slice;
334               if ( cluster->fSlice<0 || cluster->fSlice>35 )
335                 {
336                   HLTError( "Inconsistent slice number %u (track %u, cluster %d)", cluster->fSlice, trackCnt, i );
337                   printf( "TRACE: %s:%d\n", __FILE__, __LINE__ );
338                   return EINVAL;
339                 }
340               
341               AliHLTUInt8_t signBit;
342               Int_t sign;
343               AliHLTUInt64_t temp;
344               Int_t val;
345               //Read time information:
346               if ( !InputBit( signBit ) )
347                 {
348                   HLTError( "Corrupt input data. Cannot read DTime sign bit at position %u",
349                             (unsigned)(inputPtr-inData) );
350                   return EIO;
351                 }
352               sign = signBit;
353               sign = -1+sign*2;
354               if ( !InputBits( temp, AliHLTTPCCompDataCompressorHelper::GetNTimeBits()-1 ) )
355                 {
356                   HLTError( "Corrupt input data. Cannot read DTime data at position %u",
357                             (unsigned)(inputPtr-inData) );
358                   return EIO;
359                 }
360               val = (Int_t)temp;
361               cluster->fDTime = val*sign;
362               
363               //Read pad information:
364               if ( !InputBit( signBit ) )
365                 {
366                   HLTError( "Corrupt input data. Cannot read DPad sign bit at position %u",
367                             (unsigned)(inputPtr-inData) );
368                   return EIO;
369                 }
370               sign = signBit;
371               sign = -1+sign*2;
372               if ( !InputBits( temp, AliHLTTPCCompDataCompressorHelper::GetNPadBits()-1 ) )
373                 {
374                   HLTError( "Corrupt input data. Cannot read DPad data at position %u",
375                             (unsigned)(inputPtr-inData) );
376                   return EIO;
377                 }
378               
379               val = (Int_t)temp;
380               cluster->fDPad = val*sign;
381               
382               // Read charge information:
383               if ( !InputBits( temp, AliHLTTPCCompDataCompressorHelper::GetNChargeBits() ) )
384                 {
385                   HLTError( "Corrupt input data. Cannot read charge data at position %u",
386                             (unsigned)(inputPtr-inData) );
387                   return EIO;
388                 }
389               cluster->fDCharge = temp;
390               
391               if ( readShape )
392                 {
393                   // Read shape information:
394                   if ( !InputBit( signBit ) )
395                     {
396                       HLTError( "Corrupt input data. Cannot read DSigmaY sign bit at position %u",
397                                 (unsigned)(inputPtr-inData) );
398                       return EIO;
399                     }
400                   sign = signBit;
401                   sign = -1+sign*2;
402                   if ( !InputBits( temp, AliHLTTPCCompDataCompressorHelper::GetNShapeBits()-1 ) )
403                     {
404                       HLTError( "Corrupt input data. Cannot read DSigmaY data at position %u",
405                                 (unsigned)(inputPtr-inData) );
406                       return EIO;
407                     }
408                   
409                   val = (Int_t)temp;
410                   cluster->fDSigmaY = val*sign;
411                   //HLTInfo("DSigmaY: %f", cluster->fDSigmaY);
412                   
413                   
414                   if ( !InputBit( signBit ) )
415                     {
416                       HLTError( "Corrupt input data. Cannot read DSigmaZ sign bit at position %u",
417                                 (unsigned)(inputPtr-inData) );
418                       return EIO;
419                     }
420                   sign = signBit;
421                   sign = -1+sign*2;
422                   if ( !InputBits( temp, AliHLTTPCCompDataCompressorHelper::GetNShapeBits()-1 ) )
423                     {
424                       HLTError( "Corrupt input data. Cannot read DSigmaZ data at position %u",
425                                 (unsigned)(inputPtr-inData) );
426                       return EIO;
427                     }
428                   val = (Int_t)temp;
429                   cluster->fDSigmaZ = val*sign;
430                 }
431               
432               clustercount++;
433             }
434           Pad8Bits();
435           HLTDebug( "Track %u: %d clusters", trackCnt, clustercount );
436         }
437       
438       outputSize = (UInt_t)( outputPtr - output );
439       return 0;
440     }
441
442 int AliHLTTPCCompModelInflater::DecompressRemainingClusters( AliHLTUInt8_t* inData, UInt_t const& inputSize, AliHLTUInt8_t* output, UInt_t& outputSize )
443     {
444       // see header file for class documentation
445       AliHLTUInt8_t* inputPtr = inData;
446       AliHLTUInt8_t* outputPtr = output;
447       AliHLTUInt8_t* outputEndPtr = output+outputSize;
448       
449       InitBitDataInput( inputPtr, inputSize );
450       AliHLTUInt8_t version;
451       if ( !InputBits( version, 4 ) ) // Version information
452         {
453           HLTError( "Corrupt input data. Cannot read data version number at position %u",
454                     (unsigned)(inputPtr-inData) );
455           return EIO;
456         }
457       if ( version != 0 )
458         {
459           HLTError( "Unsupported version %hu. Only version 0 supported currently.", version );
460         }
461       Pad8Bits();
462       
463       if ( outputPtr+sizeof(AliHLTUInt32_t)>outputEndPtr )
464         {
465           HLTError( "Not enough space to write uncompressed data. %lu already written",
466                     (unsigned long)(outputPtr-output) );
467           outputSize = (unsigned long)(outputPtr-output);
468           return ENOBUFS;
469         }
470       
471       *(AliHLTUInt32_t*)outputPtr = 0; // Write format version
472       outputPtr += sizeof(AliHLTUInt32_t);
473       
474       //Read the remaining clusters in a compressed format.
475       
476       for(Int_t slice=0; slice<=35; slice++)
477         {
478           for(Int_t patch=0; patch < 6; patch++)
479             {
480               UInt_t i;
481               HLTDebug( "slice %u patch %u: %u padrows",
482                         (unsigned)slice, (unsigned)patch, (unsigned)*inputPtr );
483               //Write number of padrows with clusters
484               if ( outputPtr>=outputEndPtr )
485                 {
486                   HLTError( "Not enough space to write uncompressed data. %lu already written",
487                             (unsigned long)(outputPtr-output) );
488                   outputSize = (unsigned long)(outputPtr-output);
489                   return ENOBUFS;
490                 }
491               if ( !InputBits( *outputPtr,8 ) )
492                 {
493                   HLTError( "Corrupt input data. Cannot read padrow count at position %u",
494                             (unsigned)(inputPtr-inData) );
495                   return EIO;
496                 }
497               if ( !*outputPtr )
498                 {
499                   outputPtr++;
500                   continue;
501                 }
502               UInt_t nRows=(UInt_t)*outputPtr;
503               outputPtr++;
504               if ( outputPtr>=outputEndPtr )
505                 {
506                   HLTError( "Not enough space to write uncompressed data. %lu already written",
507                             (unsigned long)(outputPtr-output) );
508                   outputSize = (unsigned long)(outputPtr-output);
509                   return ENOBUFS;
510                 }
511               
512               for ( UInt_t jj=0; jj<nRows; jj++ )
513                 {
514                   
515                   AliHLTTPCRemainingRow *thisRow = (AliHLTTPCRemainingRow*)outputPtr;
516                   if ( outputPtr+sizeof(AliHLTTPCRemainingRow)>outputEndPtr )
517                     {
518                       HLTError( "Not enough space to write uncompressed data. %lu already written",
519                                 (unsigned long)(outputPtr-output) );
520                       outputSize = (unsigned long)(outputPtr-output);
521                       return ENOBUFS;
522                     }
523                   AliHLTTPCRemainingCluster *cl = thisRow->fClusters;
524                   if ( !InputBits(thisRow->fPadRow,8) ) //Read padrow #
525                     {
526                       HLTError( "Corrupt input data. Cannot read padrow number at position %u",
527                                 (unsigned)(inputPtr-inData) );
528                       return EIO;
529                     }
530                   if ( !InputBits(thisRow->fNClusters,10) )//Read number of clusters on this padrow
531                     {
532                       HLTError( "Corrupt input data. Cannot read cluster count at position %u",
533                                 (unsigned)(inputPtr-inData) );
534                       return EIO;
535                     }
536                   if ( outputPtr+sizeof(AliHLTTPCRemainingRow)+thisRow->fNClusters*sizeof(AliHLTTPCRemainingCluster)>outputEndPtr )
537                     {
538                       HLTError( "Not enough space to write uncompressed data. %lu already written",
539                                 (unsigned long)(outputPtr-output) );
540                       outputSize = (unsigned long)(outputPtr-output);
541                       return ENOBUFS;
542                     }
543                   for ( i=0; i<thisRow->fNClusters; i++ )
544                     {
545                       //Read pad
546                       Int_t buff;
547                       if ( !InputBits(buff,AliHLTTPCCompDataCompressorHelper::GetNPadBitsRemaining()) )
548                         {
549                           HLTError( "Corrupt input data. Cannot read cluster count at position %u",
550                                     (unsigned)(inputPtr-inData) );
551                           return EIO;
552                         }
553                       cl[i].fPad = (Float_t)( ((double)buff) / AliHLTTPCCompDataCompressorHelper::GetPadPrecisionFactor() );
554                       
555                       //Read time
556                       if ( !InputBits(buff,AliHLTTPCCompDataCompressorHelper::GetNTimeBitsRemaining()) )
557                         {
558                           HLTError( "Corrupt input data. Cannot read cluster count at position %u",
559                                     (unsigned)(inputPtr-inData) );
560                           return EIO;
561                         }
562                       cl[i].fTime = (Float_t)( ((double)buff) / AliHLTTPCCompDataCompressorHelper::GetTimePrecisionFactor() );
563                       
564                       //Read widths
565                       if ( !InputBits(buff,AliHLTTPCCompDataCompressorHelper::GetNShapeBitsRemaining()) )
566                         {
567                           HLTError( "Corrupt input data. Cannot read cluster count at position %u",
568                                     (unsigned)(inputPtr-inData) );
569                           return EIO;
570                         }
571                       
572                       //HLTInfo("fDSgimaY = %d",buff);
573                       
574                       Float_t padw = (Float_t)( ((double)buff) / AliHLTTPCCompDataCompressorHelper::GetPadPrecisionFactor() );
575                       cl[i].fSigmaY2 = padw*padw;
576                       //HLTInfo("sigmaY2: %f", cl[i].fSigmaY2);
577                       
578                       if ( !InputBits(buff,AliHLTTPCCompDataCompressorHelper::GetNShapeBitsRemaining()) )
579                         {
580                           HLTError( "Corrupt input data. Cannot read cluster count at position %u",
581                                     (unsigned)(inputPtr-inData) );
582                           return EIO;
583                         }
584                       Float_t timew = (Float_t)( ((double)buff) / AliHLTTPCCompDataCompressorHelper::GetTimePrecisionFactor() );
585                       cl[i].fSigmaZ2 = timew*timew;
586                       
587                       //Read charge 
588                       if ( !InputBits(buff,AliHLTTPCCompDataCompressorHelper::GetNChargeBits()) )
589                         {
590                           HLTError( "Corrupt input data. Cannot read cluster count at position %u",
591                                     (unsigned)(inputPtr-inData) );
592                           return EIO;
593                         }
594                       cl[i].fCharge = buff;
595                     }
596                   outputPtr += sizeof(AliHLTTPCRemainingRow)+thisRow->fNClusters*sizeof(AliHLTTPCRemainingCluster);
597                 }
598             }
599           
600         }
601       outputSize = (UInt_t)( outputPtr - output );
602       return 0;
603     }