]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TRD/AliHLTTRDClusterizerComponent.cxx
Uncommented method arguments for debug usage.
[u/mrichter/AliRoot.git] / HLT / TRD / AliHLTTRDClusterizerComponent.cxx
1 // $Id$
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
7  *          Timm Steinbeck <timm@kip.uni-heidelberg.de>                   *
8  *          for The ALICE Off-line 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   AliHLTTRDClusterizerComponent.cxx
20     @author Timm Steinbeck, Matthias Richter
21     @date   
22     @brief  A TRDClusterizer processing component for the HLT. */
23
24 // see header file for class documentation                                   //
25 // or                                                                        //
26 // refer to README to build package                                          //
27 // or                                                                        //
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt                          //
29
30 #if __GNUC__ >= 3
31 using namespace std;
32 #endif
33
34 #include "TTree.h"
35 #include "TFile.h"
36 #include "TBranch.h"
37
38 #include "AliHLTTRDClusterizerComponent.h"
39 #include "AliHLTTRDDefinitions.h"
40
41 #include "AliCDBManager.h"
42 #include "AliTRDclusterizerHLT.h"
43 #include "AliTRDReconstructor.h"
44 #include "AliTRDrecoParam.h"
45 #include "AliTRDrawStreamBase.h"
46
47 #include "AliRawReaderMemory.h"
48
49 #include <cstdlib>
50 #include <cerrno>
51 #include <string>
52
53 // this is a global object used for automatic component registration, do not use this
54 AliHLTTRDClusterizerComponent gAliHLTTRDClusterizerComponent;
55
56 ClassImp(AliHLTTRDClusterizerComponent);
57    
58 AliHLTTRDClusterizerComponent::AliHLTTRDClusterizerComponent()
59   : AliHLTProcessor()
60   , fOutputPercentage(100) // By default we copy to the output exactly what we got as input  
61   , fStrorageDBpath("local://$ALICE_ROOT")
62   , fClusterizer(NULL)
63   , fRecoParam(NULL)
64   , fCDB(NULL)
65   , fMemReader(NULL)
66   , fGeometryFileName("")
67   , fGeometryFile(NULL)
68   , fGeoManager(NULL)
69 {
70   // Default constructor
71
72   fGeometryFileName = getenv("ALICE_ROOT");
73   fGeometryFileName += "/HLT/TRD/geometry.root";
74 }
75
76 AliHLTTRDClusterizerComponent::~AliHLTTRDClusterizerComponent()
77 {
78   // Destructor
79   ;
80 }
81
82 const char* AliHLTTRDClusterizerComponent::GetComponentID()
83 {
84   // Return the component ID const char *
85   return "TRDClusterizer"; // The ID of this component
86 }
87
88 void AliHLTTRDClusterizerComponent::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
89 {
90   // Get the list of input data
91   list.clear(); // We do not have any requirements for our input data type(s).
92   list.push_back( AliHLTTRDDefinitions::fgkDDLRawDataType );
93 }
94
95 AliHLTComponent_DataType AliHLTTRDClusterizerComponent::GetOutputDataType()
96 {
97   // Get the output data type
98   return AliHLTTRDDefinitions::fgkClusterDataType;
99 }
100
101 void AliHLTTRDClusterizerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
102 {
103   // Get the output data size
104   constBase = 0;
105   inputMultiplier = ((double)fOutputPercentage)/100.0;
106 }
107
108 AliHLTComponent* AliHLTTRDClusterizerComponent::Spawn()
109 {
110   // Spawn function, return new instance of this class
111   return new AliHLTTRDClusterizerComponent;
112 };
113
114 int AliHLTTRDClusterizerComponent::DoInit( int argc, const char** argv )
115 {
116   // perform initialization. We check whether our relative output size is specified in the arguments.
117   fOutputPercentage = 100;
118   Int_t iRawDataVersion = 2;
119   int i = 0;
120   char* cpErr;
121
122   Int_t iRecoParamType = -1; // default will be the low flux
123
124   // the data type will become obsolete as soon as the formats are established
125   Int_t iRecoDataType = -1; // default will be simulation
126   
127   while ( i < argc )
128     {
129       Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoInit", "Arguments", "argv[%d] == %s", i, argv[i] );
130       if ( !strcmp( argv[i], "output_percentage" ) )
131         {
132           if ( i+1>=argc )
133             {
134               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Missing Argument", "Missing output_percentage parameter");
135               return ENOTSUP;
136             }
137           Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoInit", "Arguments", "argv[%d+1] == %s", i, argv[i+1] );
138           fOutputPercentage = strtoul( argv[i+1], &cpErr, 0 );
139           if ( *cpErr )
140             {
141               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Wrong Argument", "Cannot convert output_percentage parameter '%s'", argv[i+1] );
142               return EINVAL;
143             }
144           Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoInit", "Output percentage set", "Output percentage set to %lu %%", fOutputPercentage );
145           i += 2;
146           continue;
147         }
148
149       if ( strcmp( argv[i], "-cdb" ) == 0)
150         {
151           if ( i+1 >= argc )
152             {
153               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Missing Argument", "Missing -cdb argument");
154               return ENOTSUP;         
155             }
156           fStrorageDBpath = argv[i+1];
157           Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoInit", "DB storage set", "DB storage is %s", fStrorageDBpath.c_str() );   
158           i += 2;
159           continue;
160         }      
161
162       // the flux parametrizations
163       if ( strcmp( argv[i], "-lowflux" ) == 0)
164         {
165           iRecoParamType = 0;     
166           HLTDebug("Low flux reco selected.");
167           i++;
168           continue;
169         }      
170
171       if ( strcmp( argv[i], "-highflux" ) == 0)
172         {
173           iRecoParamType = 1;     
174           HLTDebug("Low flux reco selected.");
175           i++;
176           continue;
177         }      
178
179       // raw data type - sim or experiment
180       if ( strcmp( argv[i], "-simulation" ) == 0)
181         {
182           iRecoDataType = 0;
183           i++;
184           continue;
185         }
186
187       if ( strcmp( argv[i], "-experiment" ) == 0)
188         {
189           iRecoDataType = 1;
190           i++;
191           continue;
192         }
193
194       if ( strcmp( argv[i], "-rawver" ) == 0)
195         {
196           if ( i+1 >= argc )
197             {
198               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Missing Argument", "Missing -rawver argument");
199               return ENOTSUP;         
200             }
201           iRawDataVersion = atoi( argv[i+1] );
202           Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoInit", "Raw Data", "Version is %d", iRawDataVersion );    
203           i += 2;
204           continue;
205         }      
206
207       if ( strcmp( argv[i], "-geometry" ) == 0)
208         {
209           if ( i+1 >= argc )
210             {
211               Logging(kHLTLogError, "HLT::TRDTracker::DoInit", "Missing Argument", "Missing -geometry argument");
212               return ENOTSUP;         
213             }
214           fGeometryFileName = argv[i+1];
215           Logging( kHLTLogInfo, "HLT::TRDTracker::DoInit", "GeomFile storage set", "GeomFile storage is %s", 
216                    fGeometryFileName.c_str() );   
217           i += 2;
218           continue;
219         }      
220
221       Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Unknown Option", "Unknown option '%s'", argv[i] );
222       return EINVAL;
223     }
224
225   // THE "REAL" INIT COMES HERE
226
227   if (iRecoParamType < 0 || iRecoParamType > 1)
228     {
229       HLTWarning("No reco param selected. Use -lowflux or -highflux flag. Defaulting to low flux.");
230       iRecoParamType = 0;
231     }
232
233   if (iRecoParamType == 0)
234     {
235       fRecoParam = AliTRDrecoParam::GetLowFluxParam();
236       HLTDebug("Low flux params init.");
237     }
238
239   if (iRecoParamType == 1)
240     {
241       fRecoParam = AliTRDrecoParam::GetHighFluxParam();
242       HLTDebug("High flux params init.");
243     }
244
245   if (fRecoParam == 0)
246     {
247       HLTError("No reco params initialized. Sniffing big trouble!");
248       return -1;
249     }
250
251   AliTRDReconstructor::SetRecoParam(fRecoParam);
252
253   // init the raw data type to be used...
254   // the switch here will become obsolete as soon as the data structures is fixed 
255   // both: in sim and reality
256   if (iRecoDataType < 0 || iRecoDataType > 1)
257     {
258       HLTWarning("No data type selected. Use -simulation or -experiment flag. Defaulting to simulation.");
259       iRecoDataType = 0;
260     }
261
262   if (iRecoDataType == 0)
263     {
264       AliTRDrawStreamBase::SetRawStreamVersion(AliTRDrawStreamBase::kTRDsimStream);
265       HLTDebug("Data type expected is SIMULATION!");
266     }
267
268   if (iRecoDataType == 1)
269     {
270       AliTRDrawStreamBase::SetRawStreamVersion(AliTRDrawStreamBase::kTRDrealStream);
271       HLTDebug("Data type expected is EXPERIMENT!");
272     }
273
274   // the DATA BASE STUFF
275   fCDB = AliCDBManager::Instance();
276   if (!fCDB)
277     {
278       Logging(kHLTLogError, "HLT::TRDCalibration::DoInit", "Could not get CDB instance", "fCDB 0x%x", fCDB);
279     }
280   else
281     {
282       fCDB->SetRun(0); // THIS HAS TO BE RETRIEVED !!!
283       fCDB->SetDefaultStorage(fStrorageDBpath.c_str());
284       Logging(kHLTLogDebug, "HLT::TRDCalibration::DoInit", "CDB instance", "fCDB 0x%x", fCDB);
285     }
286
287   fGeometryFile = TFile::Open(fGeometryFileName.c_str());
288   if (fGeometryFile)
289     {
290       fGeoManager = (TGeoManager *)fGeometryFile->Get("Geometry");
291     }
292   else
293     {
294       Logging(kHLTLogError, "HLT::TRDTracker::DoInit", "fGeometryFile", "Unable to open file. FATAL!");
295       return -1;
296     }
297
298   fMemReader = new AliRawReaderMemory;
299
300   fClusterizer = new AliTRDclusterizerHLT("TRDCclusterizer", "TRDCclusterizer");
301   fClusterizer->SetRawVersion(iRawDataVersion);
302   fClusterizer->InitClusterTree();
303   return 0;
304 }
305
306 int AliHLTTRDClusterizerComponent::DoDeinit()
307 {
308   // Deinitialization of the component
309   delete fMemReader;
310   fMemReader = 0;
311   delete fClusterizer;
312   fClusterizer = 0;
313   return 0;
314
315   if (fGeometryFile)
316     {
317       fGeometryFile->Close();
318       delete fGeometryFile;
319       fGeometryFile = 0;
320     }
321
322   if (fCDB)
323     {
324       Logging( kHLTLogDebug, "HLT::TRDCalibration::DoDeinit", "destroy", "fCDB");
325       fCDB->Destroy();
326       fCDB = 0;
327     }
328
329   if (fRecoParam)
330     {
331       HLTDebug("Deleting fRecoParam");
332       delete fRecoParam;
333       fRecoParam = 0;
334     }
335 }
336
337 int AliHLTTRDClusterizerComponent::DoEvent( const AliHLTComponent_EventData& evtData, 
338                                             const AliHLTComponent_BlockData* blocks, 
339                                             AliHLTComponent_TriggerData& trigData, 
340                                             AliHLTUInt8_t* outputPtr, 
341                                             AliHLTUInt32_t& size, 
342                                             vector<AliHLTComponent_BlockData>& outputBlocks )
343 {
344   // Process an event
345   HLTDebug("NofBlocks %lu", evtData.fBlockCnt );
346   // Process an event
347   unsigned long totalSize = 0;
348   AliHLTUInt32_t dBlockSpecification = 0;
349
350   //implement a usage of the following
351   //   AliHLTUInt32_t triggerDataStructSize = trigData.fStructSize;
352   //   AliHLTUInt32_t triggerDataSize = trigData.fDataSize;
353   //   void *triggerData = trigData.fData;
354   HLTDebug("Trigger data received. Struct size %d Data size %d Data location 0x%x", trigData.fStructSize, trigData.fDataSize, (UInt_t*)trigData.fData);
355   HLTDebug("Output status. Output pointer at 0x%x Output vector blocks at 0x%x", outputPtr, &outputBlocks);
356
357   // Loop over all input blocks in the event
358   for ( unsigned long i = 0; i < evtData.fBlockCnt; i++ )
359     {
360       // lets not use the internal TRD data types here : AliHLTTRDDefinitions::fgkDDLRawDataType
361       // which is depreciated - we use HLT global defs instead
362       if ( blocks[i].fDataType != (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD) )
363         {
364           HLTWarning("COMPARE FAILED received type=%d expected-or-type=%d",
365                      blocks[i].fDataType, kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD);
366           continue;
367         }
368
369       dBlockSpecification = blocks[i].fSpecification;
370       unsigned long blockSize = blocks[i].fSize;
371       totalSize += blockSize;
372     }
373
374   void *memBufIn = calloc(totalSize, 1);
375   AliHLTUInt8_t *pBuf = (AliHLTUInt8_t *)memBufIn;
376   if (memBufIn == NULL)
377     {
378       HLTError("Unable to allocate %lu bytes", totalSize);
379       return -1;
380     }
381
382   // Make the memory continuous
383   unsigned long copied = 0;
384   for ( unsigned long i = 0; i < evtData.fBlockCnt; i++ )
385     {
386       // we process only the raw data from TRD
387       if ( blocks[i].fDataType != (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD) )
388         continue;
389
390       void *pos = (void*)(pBuf + copied);
391       void *copyret = memcpy(pos, blocks[i].fPtr, blocks[i].fSize);
392       if (copyret < 0)
393         {
394           //Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "MEMORY", "Unable to copy %lu bytes", blocks[i].fSize);
395           // so here i am not sure which log scheme finaly to use...
396           HLTError("MEMORY Unable to copy %lu bytes", blocks[i].fSize);
397           // maybe put a reasonable return value here...
398           return -1;
399         }
400       copied += blocks[i].fSize;
401     }
402   
403   // lets see what we copied...
404   HLTDebug("COPY STATS total=%lu copied=%lu", totalSize, copied);
405
406   fMemReader->Reset();
407   fMemReader->SetMemory((UChar_t*)memBufIn, totalSize);
408
409   //fMemReader->SelectEquipment(0, 1024, 1041);
410
411   // 1024 is good for SM 0 - it should be good for any other too
412   // but in principle the EquipmentID should come with the data
413   fMemReader->SetEquipmentID(1024);
414
415   fClusterizer->ResetTree();  
416   Bool_t iclustered = fClusterizer->Raw2ClustersChamber(fMemReader);
417   if (iclustered == kTRUE)
418     {
419       HLTDebug("Clustered successfully");
420     }
421   else
422     {
423       HLTError("Clustering ERROR");
424       return -1;
425     }
426
427   // free the memory
428   free(memBufIn);
429   
430   // put the tree into output blocks
431   TTree *fcTree = fClusterizer->GetClusterTree();
432   
433   PushBack(fcTree, AliHLTTRDDefinitions::fgkClusterDataType, dBlockSpecification);
434  
435   HLTDebug("Output size %d", size);
436   return 0;
437 }