]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TRD/AliHLTTRDClusterizerComponent.cxx
Update and cleanup triggered by the offline developments.
[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 #if __GNUC__ >= 3
25 using namespace std;
26 #endif
27
28 #include "TTree.h"
29 #include "TFile.h"
30 #include "TBranch.h"
31
32 #include "AliHLTTRDClusterizerComponent.h"
33 #include "AliHLTTRDDefinitions.h"
34
35 #include "AliCDBManager.h"
36 #include "AliTRDclusterizerHLT.h"
37 #include "AliRawReaderMemory.h"
38
39 #include <cstdlib>
40 #include <cerrno>
41 #include <string>
42
43 // this is a global object used for automatic component registration, do not use this
44 AliHLTTRDClusterizerComponent gAliHLTTRDClusterizerComponent;
45
46 ClassImp(AliHLTTRDClusterizerComponent);
47    
48 AliHLTTRDClusterizerComponent::AliHLTTRDClusterizerComponent()
49   : AliHLTProcessor()
50   , fOutputPercentage(100) // By default we copy to the output exactly what we got as input  
51   , fStrorageDBpath("local://$ALICE_ROOT")
52   , fClusterizer(NULL)
53   , fCDB(NULL)
54   , fMemReader(NULL)
55   , fGeometryFileName("")
56   , fGeometryFile(NULL)
57   , fGeoManager(NULL)
58 {
59   // Default constructor
60
61   fGeometryFileName = getenv("ALICE_ROOT");
62   fGeometryFileName += "/HLT/TRD/geometry.root";
63 }
64
65 AliHLTTRDClusterizerComponent::~AliHLTTRDClusterizerComponent()
66 {
67   // Destructor
68   ;
69 }
70
71 const char* AliHLTTRDClusterizerComponent::GetComponentID()
72 {
73   // Return the component ID const char *
74   return "TRDClusterizer"; // The ID of this component
75 }
76
77 void AliHLTTRDClusterizerComponent::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
78 {
79   // Get the list of input data
80   list.clear(); // We do not have any requirements for our input data type(s).
81   list.push_back( AliHLTTRDDefinitions::fgkDDLRawDataType );
82 }
83
84 AliHLTComponent_DataType AliHLTTRDClusterizerComponent::GetOutputDataType()
85 {
86   // Get the output data type
87   return AliHLTTRDDefinitions::fgkClusterDataType;
88 }
89
90 void AliHLTTRDClusterizerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
91 {
92   // Get the output data size
93   constBase = 0;
94   inputMultiplier = ((double)fOutputPercentage)/100.0;
95 }
96
97 AliHLTComponent* AliHLTTRDClusterizerComponent::Spawn()
98 {
99   // Spawn function, return new instance of this class
100   return new AliHLTTRDClusterizerComponent;
101 };
102
103 int AliHLTTRDClusterizerComponent::DoInit( int argc, const char** argv )
104 {
105   // perform initialization. We check whether our relative output size is specified in the arguments.
106   fOutputPercentage = 100;
107   Int_t fRawDataVersion = 2;
108   int i = 0;
109   char* cpErr;
110   while ( i < argc )
111     {
112       Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoInit", "Arguments", "argv[%d] == %s", i, argv[i] );
113       if ( !strcmp( argv[i], "output_percentage" ) )
114         {
115           if ( i+1>=argc )
116             {
117               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Missing Argument", "Missing output_percentage parameter");
118               return ENOTSUP;
119             }
120           Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoInit", "Arguments", "argv[%d+1] == %s", i, argv[i+1] );
121           fOutputPercentage = strtoul( argv[i+1], &cpErr, 0 );
122           if ( *cpErr )
123             {
124               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Wrong Argument", "Cannot convert output_percentage parameter '%s'", argv[i+1] );
125               return EINVAL;
126             }
127           Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoInit", "Output percentage set", "Output percentage set to %lu %%", fOutputPercentage );
128           i += 2;
129           continue;
130         }
131
132       if ( strcmp( argv[i], "-cdb" ) == 0)
133         {
134           if ( i+1 >= argc )
135             {
136               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Missing Argument", "Missing -cdb argument");
137               return ENOTSUP;         
138             }
139           fStrorageDBpath = argv[i+1];
140           Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoInit", "DB storage set", "DB storage is %s", fStrorageDBpath.c_str() );   
141           i += 2;
142           continue;
143         }      
144
145       if ( strcmp( argv[i], "-rawver" ) == 0)
146         {
147           if ( i+1 >= argc )
148             {
149               Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Missing Argument", "Missing -rawver argument");
150               return ENOTSUP;         
151             }
152           fRawDataVersion = atoi( argv[i+1] );
153           Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoInit", "Raw Data", "Version is %d", fRawDataVersion );    
154           i += 2;
155           continue;
156         }      
157
158       if ( strcmp( argv[i], "-geometry" ) == 0)
159         {
160           if ( i+1 >= argc )
161             {
162               Logging(kHLTLogError, "HLT::TRDTracker::DoInit", "Missing Argument", "Missing -geometry argument");
163               return ENOTSUP;         
164             }
165           fGeometryFileName = argv[i+1];
166           Logging( kHLTLogInfo, "HLT::TRDTracker::DoInit", "GeomFile storage set", "GeomFile storage is %s", 
167                    fGeometryFileName.c_str() );   
168           i += 2;
169           continue;
170         }      
171
172       Logging(kHLTLogError, "HLT::TRDClusterizer::DoInit", "Unknown Option", "Unknown option '%s'", argv[i] );
173       return EINVAL;
174     }
175
176   fCDB = AliCDBManager::Instance();
177   if (!fCDB)
178     {
179       Logging(kHLTLogError, "HLT::TRDCalibration::DoInit", "Could not get CDB instance", "fCDB 0x%x", fCDB);
180     }
181   else
182     {
183       fCDB->SetRun(0); // THIS HAS TO BE RETRIEVED !!!
184       fCDB->SetDefaultStorage(fStrorageDBpath.c_str());
185       Logging(kHLTLogDebug, "HLT::TRDCalibration::DoInit", "CDB instance", "fCDB 0x%x", fCDB);
186     }
187
188   fGeometryFile = TFile::Open(fGeometryFileName.c_str());
189   if (fGeometryFile)
190     {
191       fGeoManager = (TGeoManager *)fGeometryFile->Get("Geometry");
192     }
193   else
194     {
195       Logging(kHLTLogError, "HLT::TRDTracker::DoInit", "fGeometryFile", "Unable to open file. FATAL!");
196       return -1;
197     }
198
199   fMemReader = new AliRawReaderMemory;
200
201   fClusterizer = new AliTRDclusterizerHLT("TRDCclusterizer", "TRDCclusterizer");
202   fClusterizer->SetRawVersion(fRawDataVersion);
203   fClusterizer->InitClusterTree();
204   return 0;
205 }
206
207 int AliHLTTRDClusterizerComponent::DoDeinit()
208 {
209   // Deinitialization of the component
210   delete fMemReader;
211   fMemReader = 0;
212   delete fClusterizer;
213   fClusterizer = 0;
214   return 0;
215
216   if (fGeometryFile)
217     {
218       fGeometryFile->Close();
219       delete fGeometryFile;
220       fGeometryFile = 0;
221     }
222
223   if (fCDB)
224     {
225       Logging( kHLTLogDebug, "HLT::TRDCalibration::DoDeinit", "destroy", "fCDB");
226       fCDB->Destroy();
227       fCDB = 0;
228     }
229 }
230
231 int AliHLTTRDClusterizerComponent::DoEvent( const AliHLTComponent_EventData& evtData, const AliHLTComponent_BlockData* blocks, 
232                                             AliHLTComponent_TriggerData& trigData, AliHLTUInt8_t* outputPtr, 
233                                             AliHLTUInt32_t& size, vector<AliHLTComponent_BlockData>& outputBlocks )
234 {
235   // Process an event
236 //   Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "Output percentage set", "Output percentage set to %lu %%", fOutputPercentage );
237   Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoEvent", "BLOCKS", "NofBlocks %lu", evtData.fBlockCnt );
238   // Process an event
239   unsigned long totalSize = 0;
240   AliHLTUInt32_t fDblock_Specification = 0;
241
242   //implement a usage of the following
243 //   AliHLTUInt32_t triggerDataStructSize = trigData.fStructSize;
244 //   AliHLTUInt32_t triggerDataSize = trigData.fDataSize;
245 //   void *triggerData = trigData.fData;
246   Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoEvent", "Trigger data received", 
247            "Struct size %d Data size %d Data location 0x%x", trigData.fStructSize, trigData.fDataSize, (UInt_t*)trigData.fData);
248   Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoEvent", "Output status", 
249            "Output pointer at 0x%x Output vector blocks at 0x%x", outputPtr, &outputBlocks);
250
251   // Loop over all input blocks in the event
252   for ( unsigned long i = 0; i < evtData.fBlockCnt; i++ )
253     {
254       char tmp1[14], tmp2[14];
255       DataType2Text( blocks[i].fDataType, tmp1 );
256       DataType2Text( AliHLTTRDDefinitions::fgkDDLRawDataType, tmp2 );      
257       Logging( kHLTLogDebug, "HLT::TRDClusterizer::DoEvent", "Event received", 
258                "Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
259                evtData.fEventID, evtData.fEventID, tmp1, tmp2 );
260
261       if ( blocks[i].fDataType != AliHLTTRDDefinitions::fgkDDLRawDataType ) 
262         {
263           Logging (kHLTLogError, "HLT::TRDClusterizer::DoEvent", "COMPARE FAILED", "type=%d is type=%d",
264                    blocks[i].fDataType, AliHLTTRDDefinitions::fgkDDLRawDataType);
265           continue;
266         }
267       fDblock_Specification = blocks[i].fSpecification;
268 //       Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "CHECKSPEC", "fDblock_Spec %d %d", i, fDblock_Specification);
269       unsigned long blockSize = blocks[i].fSize;
270       totalSize += blockSize;
271     }
272
273   void *memBufIn = calloc(totalSize, 1);
274   AliHLTUInt8_t *pBuf = (AliHLTUInt8_t *)memBufIn;
275   if (memBufIn == NULL)
276     {
277       Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "MEMORY", "Unable to allocate %lu bytes", totalSize);
278       return -1;
279     }
280
281   // Make the memory continuous
282   unsigned long copied = 0;
283   for ( unsigned long i = 0; i < evtData.fBlockCnt; i++ )
284     {
285       if ( blocks[i].fDataType != AliHLTTRDDefinitions::fgkDDLRawDataType ) 
286         continue;
287
288       void *pos = (void*)(pBuf + copied);
289       void *copyret = memcpy(pos, blocks[i].fPtr, blocks[i].fSize);
290       if (copyret < 0)
291         {
292           Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "MEMORY", "Unable to copy %lu bytes", blocks[i].fSize);
293           return -1;
294         }
295       copied += blocks[i].fSize;
296     }
297
298 //   Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "COPY STATS", "total=%lu copied=%lu", totalSize, copied);
299
300   fMemReader->Reset();
301   fMemReader->SetMemory((UChar_t*)memBufIn, totalSize);
302   //fMemReader->SelectEquipment(0, 1024, 1041);
303   fMemReader->SetEquipmentID(1024);
304   //fMemReader->Reset();
305 //   Bool_t ihead = fMemReader->ReadHeader();
306 //   if (ihead == kTRUE)
307 //     {
308 //       Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "HEADER", "Header read successfully");
309 //     }
310 //   else
311 //     {
312 //       Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "HEADER", "Header read ERROR");
313 //       //return -1; -- not FATAL
314 //     }
315
316   fClusterizer->ResetTree();  
317   Bool_t iclustered = fClusterizer->Raw2ClustersChamber(fMemReader);
318   if (iclustered == kTRUE)
319     {
320       Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "CLUSTERS", "Clustered successfully");
321     }
322   else
323     {
324       Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "CLUSTERS", "Clustering ERROR");
325       return -1;
326     }
327
328 //   AliRawReaderMemory reader;
329 //   reader.Reset();
330 //   reader.SetMemory((UChar_t*)memBufIn, totalSize);
331 //   //reader->Reset();
332 //   Bool_t ihead = reader.ReadHeader();
333 // //   if (ihead == kTRUE)
334 // //     {
335 // //       Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "HEADER", "Header read successfully");
336 // //     }
337 // //   else
338 // //     {
339 // //       Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "HEADER", "Header read ERROR");
340 // //       //return -1; -- not FATAL
341 // //     }
342
343 //   fClusterizer->ResetTree();
344   
345 //   Bool_t iclustered = fClusterizer->Raw2ClustersChamber(&reader);
346 //   if (iclustered == kTRUE)
347 //     {
348 //       Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "CLUSTERS", "Clustered successfully");
349 //     }
350 //   else
351 //     {
352 //       Logging( kHLTLogError, "HLT::TRDClusterizer::DoEvent", "CLUSTERS", "Clustering ERROR");
353 //       return -1;
354 //     }
355   
356   free(memBufIn);
357   
358   // put the tree into output blocks of TObjArrays
359   TTree *fcTree = fClusterizer->GetClusterTree();
360   
361   PushBack(fcTree, AliHLTTRDDefinitions::fgkClusterDataType, fDblock_Specification);
362   
363   Logging( kHLTLogInfo, "HLT::TRDClusterizer::DoEvent", "DONE", "Output size %d", size);
364   return 0;
365 }