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