]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/comp/AliHLTCOMPHuffmanAltroCalibComponent.cxx
data specification and meta data for CDB entries (Jenny)
[u/mrichter/AliRoot.git] / HLT / comp / AliHLTCOMPHuffmanAltroCalibComponent.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Authors: Jochen Thaeder <thaeder@kip.uni-heidelberg.de>                *
5  *          for The ALICE Off-line Project.                               *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /** @file   AliHLTCOMPHuffmanAltroCalibComponent.cxx
17     @author Jochen Thaeder
18     @date   
19     @brief  A calibration component for the Huffman code creation.
20 */
21
22 #if __GNUC__>= 3
23 using namespace std;
24 #endif
25
26 #include "AliHLTCOMPHuffmanAltroCalibComponent.h"
27 #include "AliHLTCOMPHuffmanAltro.h"
28 #include "AliHLTCompDefinitions.h"
29 #include "AliHLTStdIncludes.h"
30 #include "TFile.h" // necessary for HuffmanData writing
31
32 ClassImp(AliHLTCOMPHuffmanAltroCalibComponent)
33
34 AliHLTCOMPHuffmanAltroCalibComponent::AliHLTCOMPHuffmanAltroCalibComponent()
35   :
36   fHuffmanCompressor(NULL),
37   fHuffmanData(NULL),
38   fOrigin(kAliHLTVoidDataOrigin),
39   fRunNumber(0),
40   fSpecification(0),
41   fTablePath(),
42   fNRCUTrailerWords(0) {
43   // see header file for class documentation
44   // or
45   // refer to README to build package
46   // or
47   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
48 }
49
50 AliHLTCOMPHuffmanAltroCalibComponent::~AliHLTCOMPHuffmanAltroCalibComponent() {
51   // see header file for class documentation
52 }
53
54 // Public functions to implement AliHLTComponent's interface.
55 // These functions are required for the registration process
56
57 const char* AliHLTCOMPHuffmanAltroCalibComponent::GetComponentID() {
58   // see header file for class documentation
59
60   return "COMPHuffmanTrainer";
61 }
62
63 void AliHLTCOMPHuffmanAltroCalibComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list) {
64   // see header file for class documentation
65   list.clear(); 
66   list.push_back( kAliHLTDataTypeDDLRaw );
67 }
68
69 AliHLTComponentDataType AliHLTCOMPHuffmanAltroCalibComponent::GetOutputDataType() {
70   // see header file for class documentation
71   AliHLTComponentDataType dt=AliHLTCompDefinitions::fgkHuffmanAltroCalDataType;
72   if (!fOrigin.IsNull()) dt=dt|fOrigin.Data();
73   return dt;
74  
75 }
76
77 void AliHLTCOMPHuffmanAltroCalibComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) {
78   // see header file for class documentation
79   constBase = sizeof(AliHLTCOMPHuffmanData);
80   inputMultiplier = (0.0);
81 }
82
83 AliHLTComponent* AliHLTCOMPHuffmanAltroCalibComponent::Spawn() {
84   // see header file for class documentation
85
86   return new AliHLTCOMPHuffmanAltroCalibComponent();
87 }  
88
89
90 Int_t AliHLTCOMPHuffmanAltroCalibComponent::ScanArgument( Int_t argc, const char** argv ) {
91   // see header file for class documentation
92
93   Int_t iResult = 0;
94   TString argument = " ";
95   TString parameter = " ";
96
97   if ( !argc ) 
98     return -EINVAL;
99
100   argument = argv[iResult];
101   
102   if ( argument.IsNull() ) 
103     return -EINVAL;
104
105   // get data origin
106   if ( argument.CompareTo("-origin") == 0 ) 
107     {
108
109       if ( ++iResult >= argc  ) 
110         {
111           iResult = -EPROTO;
112         }
113       else 
114         {
115           fOrigin = argv[1];
116
117           while(fOrigin.Length() <  kAliHLTComponentDataTypefOriginSize)
118             {
119               fOrigin.Append(" ");
120             }
121         
122           HLTInfo("Origin is set to %s.", fOrigin.Data());        
123
124         }
125     }
126
127   else
128     {
129       // get run number   
130       if ( argument.CompareTo("-runnumber") == 0 ) 
131         {
132           
133         if ( ++iResult >= argc  ) 
134           {
135             iResult = -EPROTO;
136           }
137         else 
138           {
139             parameter = argv[1];
140             
141             // get run number
142          
143             fRunNumber =  atoi(parameter.Data());
144                
145             HLTInfo( "Run number is set to %d (Dec) = %X (Hex).", fRunNumber, fRunNumber ); 
146           }
147         }
148       else
149         {
150           // get data specification
151           if(argument.CompareTo("-dataspec") == 0 ) 
152             {
153               if ( ++iResult >= argc  ) 
154                 {
155                   iResult = -EPROTO;
156                 }
157               else 
158                 {
159                   // get data specification
160                   fSpecification = strtoul( argv[1], NULL, 16);
161                   
162                   HLTInfo( "Specification is set to %d (Dec) = %08X (Hex).", fSpecification, fSpecification ); 
163                 }
164             }
165           // get number of trailer words
166           else 
167             {
168               
169               if ( argument.CompareTo("-tablepath") == 0)
170                 {
171                   if ( ++iResult >= argc  ) 
172                     {
173                       iResult = -EPROTO;
174                     }
175                   else 
176                     {
177                       // get table path
178                       fTablePath = argv[1];
179                       HLTInfo( "Path for Huffman table output is set to %s.", fTablePath.Data() ); 
180                       if (!fTablePath.IsNull() && !fTablePath.EndsWith("/"))
181                         fTablePath+="/";                      
182                     }
183                 }
184
185               else 
186                 {
187                   if ( argument.CompareTo("-trailerwords") == 0 ) 
188                     { 
189                       
190                       if ( ++iResult >= argc  ) 
191                         {
192                           iResult = -EPROTO;
193                         }
194                       else 
195                         {
196                           parameter = argv[1];
197                           if ( parameter.CompareTo("1") == 0 ) 
198                             {
199                               fNRCUTrailerWords = 1;
200                               HLTInfo( "Number of trailer words is set to 1." );
201                             }
202                           else if ( parameter.CompareTo("2") == 0 ) 
203                             {
204                               fNRCUTrailerWords = 2;
205                           HLTInfo( "Number of trailer words is set to 2." );
206                             }
207                           else if ( parameter.CompareTo("3") == 0 ) 
208                             {
209                               fNRCUTrailerWords = 3;
210                           HLTInfo( "Number of trailer words is set to 3." );
211                             }
212                           else 
213                             {
214                               HLTError( "Invalid number of trailerwords: '%s'.", argv[1] );
215                               iResult = -EPROTO;
216                             }
217                         } 
218                     }
219                   else 
220                     {
221                       iResult = -EINVAL;
222                     }
223                 }
224             }
225         }
226     }
227   
228   return iResult;
229 }
230
231 Int_t AliHLTCOMPHuffmanAltroCalibComponent::InitCalibration() {
232   // see header file for class documentation
233     
234   // ** Create a calibration instance to train the Huffman code table
235   if ( fHuffmanCompressor )
236     return EINPROGRESS;
237   
238   if ( fHuffmanData )
239     return EINPROGRESS;
240
241   // create a new instance of HuffmanData to write results from training in
242   fHuffmanData = new AliHLTCOMPHuffmanData();
243
244   fHuffmanCompressor = new AliHLTCOMPHuffmanAltro(kTRUE, kTRUE, NULL, fNRCUTrailerWords);
245
246   // initialise new training table
247   fHuffmanCompressor->InitNewTrainingTable(); 
248  
249   return 0;
250 }
251
252 Int_t AliHLTCOMPHuffmanAltroCalibComponent::DeinitCalibration() {
253   // see header file for class documentation
254
255   if ( fHuffmanCompressor )
256     delete fHuffmanCompressor; 
257   fHuffmanCompressor = NULL;
258
259   if ( fHuffmanData )
260     delete fHuffmanData; 
261   fHuffmanData = NULL;
262
263   return 0;
264 }
265
266 /** function to do the calibration */
267 Int_t AliHLTCOMPHuffmanAltroCalibComponent::ProcessCalibration( const AliHLTComponentEventData& evtData, AliHLTComponentTriggerData& trigData ) {
268   // see header file for class documentation
269  
270   const AliHLTComponentBlockData* iter = NULL;
271
272   //AliHLTUInt8_t slice, patch;
273  
274   // ** Loop over all input blocks and specify which data format should be read - only select Raw Data
275   iter = GetFirstInputBlock( kAliHLTDataTypeDDLRaw );
276
277   if ( iter != NULL ) do {
278     
279     // ** Print Debug output which data format was received
280     HLTDebug ( "Event received - Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s", 
281                evtData.fEventID, evtData.fEventID, DataType2Text(iter->fDataType).c_str(), DataType2Text(kAliHLTDataTypeDDLRaw).c_str());
282
283     
284     TString blockorigin("");
285     blockorigin.Insert(0, iter->fDataType.fOrigin, kAliHLTComponentDataTypefOriginSize);
286     
287     if (fOrigin.IsNull())
288       {
289         // if origin is not explicitly set by command line, take origin from data block
290         fOrigin=blockorigin;
291         HLTDebug("Origin of current data block set by block itself is %s.", blockorigin.Data());
292       }
293     else
294       {
295         // if set origin is not equal to current block origin, printout warning! 
296         if(fOrigin.CompareTo(blockorigin)!=0) {
297           HLTWarning("Origin %s of current data block does not match origin set by command line argument %s.", blockorigin.Data(), fOrigin.Data());
298           continue;
299         }
300       }
301
302     // ** Get DDL ID in order to tell the memory reader which slice/patch to use
303     //fSlice = AliHLTCompDefinitions::GetMinSliceNr( *iter );
304     //fPatch = AliHLTCompDefinitions::GetMinPatchNr( *iter );
305
306     //HLTDebug ( "Input Raw Data - Slice/Patch: %d/%d.", fSlice, fPatch);
307
308     fHuffmanCompressor->SetInputData(iter->fPtr, iter->fSize);
309
310     // only necessary for output in binary file
311     //fHuffmanCompressor->SetSlice(fSlice);
312     //fHuffmanCompressor->SetPatch(fPatch);
313    
314     fHuffmanCompressor->ProcessData();
315   
316     // ** Get next input block, with the same specification as defined in GetFirstInputBlock()
317   } while ( (iter = GetNextInputBlock()) != NULL );
318
319   // ** Get output specification
320   // commented out for the moment to read spec in from command line argument
321   //fSpecification = AliHLTCompDefinitions::EncodeDataSpecification( fSlice, fSlice, fPatch, fPatch );
322   //fSpecification = fSlice<<24 | fSlice<<16 | fPatch<<8 | fPatch;
323
324   // ** PushBack data to shared memory ... 
325
326   // DATA TYE to DEFINE !!! XXXX
327   PushBack( (TObject*) fHuffmanData, AliHLTCompDefinitions::fgkHuffmanAltroCalDataType|fOrigin.Data(), fSpecification);
328  
329   return 0;
330 } // Int_t AliHLTCOMPHuffmanAltroCalibComponent::ProcessCalibration( const AliHLTComponentEventData& evtData, AliHLTComponentTriggerData& trigData ) {
331
332
333 Int_t AliHLTCOMPHuffmanAltroCalibComponent::ShipDataToFXS( const AliHLTComponentEventData& evtData, AliHLTComponentTriggerData& trigData ) {
334   // see header file for class documentation
335
336   // create code from training table
337   fHuffmanCompressor->CreateCodeTable();
338
339   // write code table and occurrence table to HuffmanData instance
340   fHuffmanCompressor->SetHuffmanData(fHuffmanData);
341
342   TString rootfilename;
343   if(fTablePath.IsNull() )
344     {
345       // if there is no explicit table path, take current path
346       rootfilename.Form("huffmanData_%s_%08X_%08X.root", fOrigin.Data(), fRunNumber, fSpecification);      
347     }
348   else
349     {
350       rootfilename.Form("%shuffmanData_%s_%08X_%08X.root", fTablePath.Data(), fOrigin.Data(), fRunNumber, fSpecification);
351     }
352  
353   TFile* huffmanrootfile = new TFile(rootfilename, "RECREATE");
354   huffmanrootfile->WriteObject(fHuffmanData,"HuffmanData");
355   huffmanrootfile->Write();
356   huffmanrootfile->Close();
357
358   // ** PushBack data to FXS ...
359   // currently specification has to be put in by command line argument!
360   Int_t dataspec = (Int_t) fSpecification;
361
362   fHuffmanData->SetOCDBSpecifications(fOrigin, dataspec);
363   PushToFXS( (TObject*) fHuffmanData, "TPC", "HuffmanData" ) ;
364   
365   return 0;
366 } // Int_t AliHLTCOMPHuffmanAltroCalibComponent::ShipDataToFXS( const AliHLTComponentEventData& evtData, AliHLTComponentTriggerData& trigData ) {