]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/comp/AliHLTCOMPHuffmanAltro.h
Be sure to load mapping when needed
[u/mrichter/AliRoot.git] / HLT / comp / AliHLTCOMPHuffmanAltro.h
1 // $Id$
2
3 #ifndef ALIHLTCOMPHUFFMANALTRO_H
4 #define ALIHLTCOMPHUFFMANALTRO_H
5
6 /**************************************************************************
7  * This file is property of and copyright by the ALICE HLT Project        * 
8  * All rights reserved.                                                   *
9  *                                                                        *
10  * Primary Author: Jenny Wagner  (jwagner@cern.ch)                        *
11  *                                                                        *
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  **************************************************************************/
20
21 /** @file   AliHLTCOMPHuffmanAltro.h
22     @author Jenny Wagner
23     @date   20-11-2007
24     @brief  The Huffman compressor
25 */
26
27 #include "TObject.h"
28 #include "AliHLTLogging.h"
29 #include "AliHLTCOMPHuffmanData.h"
30
31 class AliRawReaderMemory;
32 class AliAltroRawStreamV3;
33
34 /** @class   AliHLTCOMPHuffmanAltro
35     @author Jenny Wagner
36     @date   20-11-2007
37     @brief  The Huffman Compressor with functions training (for Calibration), compress and decompress, calculate entropy  
38 */
39 class AliHLTCOMPHuffmanAltro : public TObject, public AliHLTLogging
40 {
41  public:
42
43   /** constructor for test use only */
44   AliHLTCOMPHuffmanAltro();
45
46   /** standard constructor
47    *
48    * @param compressionswitch   decides whether to compress or decompress (TRUE for calibration/training)
49    * @param trainingmode        decides whether to create a new Huffman table 
50    * @param translationtable    pointer to lookup Huffman table for compression and decompression
51    * @param nrcutrailerwords    number of NRCU trailer words (ranging from 1 to 3)
52    */
53   AliHLTCOMPHuffmanAltro(Bool_t compressionswitch, Bool_t trainingmode, AliHLTCOMPHuffmanCodeData::AliHLTCOMPHuffmanCodeStruct* translationtable, Int_t nrcutrailerwords);
54   /** destructor */
55   virtual ~AliHLTCOMPHuffmanAltro();
56
57   /** SetInputData 
58    * Data buffer has to be valid all the time, no internal copy.
59    *  @param memory       pointer to input data
60    *  @param size         size of input data
61    *  @param equipmentId  equipment (ddl) id
62    */
63   int AddInputData(UChar_t* memory, ULong_t size, Int_t equipmentId);
64   
65   /** SetOutputData
66    *  @param outputdata  pointer to output data
67    *  @param outputsize  size of output data
68    */
69   int SetOutputData(AliHLTUInt8_t* outputdata, unsigned long outputsize);
70
71   /**
72    * Reset and prepare for new event
73    */
74   int Reset();
75
76   /** GetOutputDataSize (which is unknown at first and has to be calculated
77    * @return output data size
78   */
79   unsigned long GetOutputDataSize();
80
81   /** initialise training table */
82   int InitNewTrainingTable();
83
84   /** write out new HuffmanData
85    * @param huffmandata   pointer to Huffman data 
86   */
87   int SetHuffmanData(AliHLTCOMPHuffmanData* huffmandata) const;
88
89   /** get training table from HuffmanData instance
90    * @param huffmandata  pointer to Huffman data
91   */
92   int GetTrainingTable(const AliHLTCOMPHuffmanData* huffmandata);
93
94   /** initialise the translation table
95    * @param huffmandata  pointer to Huffman data
96   */
97   int GetTranslationTable(const AliHLTCOMPHuffmanData* huffmandata);
98
99   /** function to compress or decompress data */
100   void ProcessData();
101
102  /** function to create Huffman code table by means of private functions
103   * @return zero upon success
104  */
105   Int_t CreateCodeTable();
106
107   /** calculate the entropy of the input data
108    * @return calculated entropy (in double precision)
109   */
110   Double_t GetEntropy();
111
112   /** function to read 10 bit data in and write same 10 bit data out (performance test) */
113   Int_t CopyData();
114
115   /** print content */
116   virtual void Print(Option_t* option = "") const;
117
118  private:
119
120   /** copy constructor prohibited */
121   AliHLTCOMPHuffmanAltro(const AliHLTCOMPHuffmanAltro&);
122   /** assignment operator prohibited */
123   AliHLTCOMPHuffmanAltro& operator=(const AliHLTCOMPHuffmanAltro&);
124
125   /** function to calculate the entropy of the incoming data */
126   /// FIXME: the size of the array is not known, everything assumes size TIMEBINS
127   Int_t CalcEntropy(const AliHLTCOMPHuffmanOccurrenceData::AliHLTCOMPHuffmanDataStruct* occurrencetable=NULL);
128
129   /** function for merge sorting the array data 
130    * @param unsortedarray   unsorted array of data from occurrence table
131    * @param n               number of entries in the unsorted array
132    * @return pointer to first element of sorted list
133    */
134   AliHLTCOMPHuffmanOccurrenceData::AliHLTCOMPHuffmanDataStruct* Mergesort(AliHLTCOMPHuffmanOccurrenceData::AliHLTCOMPHuffmanDataStruct* unsortedarray, Int_t n);
135
136   /** function to create the HuffmanCode and write it in a sorted array 
137    * @param treeroot           pointer to root of Huffman tree
138    * @param HuffmanCodearray   pointer to final Huffman code table (array)
139    * @return zero upon success
140    */ 
141  Int_t HuffmanCode(AliHLTCOMPHuffmanData::AliHLTCOMPHuffmanTreeDataStruct* treeroot, AliHLTCOMPHuffmanCodeData::AliHLTCOMPHuffmanCodeStruct* HuffmanCodearray);
142   
143   /** function to create the Huffmantree
144    * @param listroot   pointer to first element of sorted list to build Huffman tree from
145    * @param listend    pointer to last element of sorted list to build Huffman tree from
146    * @param n          number of entries in the list
147    * @return pointer to root of Huffman tree
148    */
149  AliHLTCOMPHuffmanData::AliHLTCOMPHuffmanTreeDataStruct* CreateHuffmanTree(AliHLTCOMPHuffmanData::AliHLTCOMPHuffmanTreeDataStruct* listroot,  AliHLTCOMPHuffmanData::AliHLTCOMPHuffmanTreeDataStruct* listend, Int_t n);
150   
151   /** entropy encoding function: read in data, table lookup to encode, write encoded data to output array and set fOutputDataSize
152    * @return zero upon success
153    */
154  Int_t EntropyCompression();
155
156   /** merge sorting function for translation table
157    * @param unsortedarray   Huffman code table which is not sorted for decompression yet
158    * @param n               number of entries in the unsorted array
159    * @return pointer to first element of sorted list
160    */
161  AliHLTCOMPHuffmanCodeData::AliHLTCOMPHuffmanCodeStruct* TTMergesort(AliHLTCOMPHuffmanCodeData::AliHLTCOMPHuffmanCodeStruct* unsortedarray, Int_t n);
162     
163   /** entropy decoding function
164    * @return zero upon success
165    */
166   Int_t EntropyDecompression();
167   
168   /** training function for the translation table
169    * @return zero upon success
170    */
171   Int_t TrainingData(); 
172
173   /// raw reader instance
174   AliRawReaderMemory* fpRawReader;                    //! transient
175   /// raw stream decoder
176   AliAltroRawStreamV3* fpAltroRawStream;          //! transient
177
178   /** boolean to decide whether to process data (FALSE) or create a new code table (TRUE) */
179   Bool_t fTrainingMode;                         // choice if new codetable is created or not
180   /** boolean to decide whether to compress (TRUE) or decompress (FALSE) incoming data (automatically TRUE for code creation) */
181   Bool_t fCompressionSwitch;                    // mode choice (training, compress, decompress)
182   /** pointer to input data */
183   AliHLTUInt8_t* fPointer2InData;               // pointer to input data (uncompressed raw data)
184   /** pointer to output data */
185   AliHLTUInt64_t* fPointer2OutData;             // pointer to output data (compressed data)
186   /** input data size */
187   unsigned long fInputDataSize;                 // input data size
188   /** output data size */
189   UInt_t fOutputDataSize;                        // output data size
190   /** number of NRCU trailer words */
191   UInt_t fNrcuTrailerwords;                      // number of RCU trailerwords
192   /** calculated entropy of input data */
193   Double_t fEntropy;                            // entropy of the file
194
195   int fVerbosity;                               //! verbosity
196
197   //AliHLTUInt8_t fSlice;                         // transient variables to specify
198   //AliHLTUInt8_t fPatch;                         // where output root file comes from
199  
200   /** pointer to occurrence table */
201   AliHLTCOMPHuffmanOccurrenceData::AliHLTCOMPHuffmanDataStruct* fTrainingTable;       // training table with amplitudes and resp. abundances
202   /** pointer to Huffman code table */
203   AliHLTCOMPHuffmanCodeData::AliHLTCOMPHuffmanCodeStruct* fTranslationTable;    // Huffman Entropy Code Table
204
205
206   ClassDef(AliHLTCOMPHuffmanAltro, 0)
207         };
208 #endif