]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCClusterFinderComponent.cxx
TPC cluster finder speeded up, can process unsorted data (Kenneth); not yet enabled...
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCClusterFinderComponent.cxx
1 // $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8  *                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
9  *                  Jochen Thaeder <thaeder@kip.uni-heidelberg.de>        *
10  *                  for The ALICE HLT Project.                            *
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   AliHLTTPCClusterFinderComponent.cxx
22     @author Timm Steinbeck, Matthias Richter, Jochen Thaeder, Kenneth Aamodt
23     @date   
24     @brief  The TPC cluster finder processing component
25 */
26
27 #if __GNUC__>= 3
28 using namespace std;
29 #endif
30 #include "AliHLTTPCLogging.h"
31 #include "AliHLTTPCClusterFinderComponent.h"
32 #include "AliHLTTPCDigitReaderPacked.h"
33 #include "AliHLTTPCDigitReaderUnpacked.h"
34 #include "AliHLTTPCDigitReaderRaw.h"
35 #include "AliHLTTPCClusterFinder.h"
36 #include "AliHLTTPCSpacePointData.h"
37 #include "AliHLTTPCRawDataFormat.h"
38 #include "AliHLTTPCClusterDataFormat.h"
39 #include "AliHLTTPCTransform.h"
40 #include "AliHLTTPCClusters.h"
41 #include <stdlib.h>
42 #include <errno.h>
43 #include "TString.h"
44 #include "TStopwatch.h"
45 #include <sys/time.h>
46
47 // this is a global object used for automatic component registration, do not use this
48 // use fPackedSwitch = true for packed inputtype "gkDDLPackedRawDataType"
49 // use fPackedSwitch = false for unpacked inputtype "gkUnpackedRawDataType"
50 AliHLTTPCClusterFinderComponent gAliHLTTPCClusterFinderComponentPacked(true);
51 AliHLTTPCClusterFinderComponent gAliHLTTPCClusterFinderComponentUnpacked(false);
52
53 ClassImp(AliHLTTPCClusterFinderComponent)
54
55 AliHLTTPCClusterFinderComponent::AliHLTTPCClusterFinderComponent(bool packed)
56   :
57   fClusterFinder(NULL),
58   fReader(NULL),
59   fClusterDeconv(true),
60   fXYClusterError(-1),
61   fZClusterError(-1),
62   fPackedSwitch(packed),
63   fPatch(0),
64   fUnsorted(0),
65   fPadArray(NULL)
66 {
67   // see header file for class documentation
68   // or
69   // refer to README to build package
70   // or
71   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
72 }
73
74 AliHLTTPCClusterFinderComponent::AliHLTTPCClusterFinderComponent(const AliHLTTPCClusterFinderComponent&)
75   :
76   fClusterFinder(NULL),
77   fReader(NULL),
78   fClusterDeconv(true),
79   fXYClusterError(-1),
80   fZClusterError(-1),
81   fPackedSwitch(0),
82   fPatch(0),
83   fUnsorted(0),
84   fPadArray(NULL)
85 {
86   // see header file for class documentation
87   HLTFatal("copy constructor untested");
88 }
89
90 AliHLTTPCClusterFinderComponent& AliHLTTPCClusterFinderComponent::operator=(const AliHLTTPCClusterFinderComponent&)
91
92   // see header file for class documentation
93   HLTFatal("assignment operator untested");
94   return *this;
95 }
96
97 AliHLTTPCClusterFinderComponent::~AliHLTTPCClusterFinderComponent()
98 {
99   // see header file for class documentation
100 }
101
102 // Public functions to implement AliHLTComponent's interface.
103 // These functions are required for the registration process
104
105 const char* AliHLTTPCClusterFinderComponent::GetComponentID()
106 {
107   // see header file for class documentation
108   if (fPackedSwitch) return "TPCClusterFinderPacked";
109   else return "TPCClusterFinderUnpacked";
110 }
111
112 void AliHLTTPCClusterFinderComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
113 {
114   // see header file for class documentation
115   list.clear(); 
116   if (fPackedSwitch) list.push_back( AliHLTTPCDefinitions::fgkDDLPackedRawDataType );
117   else list.push_back( AliHLTTPCDefinitions::fgkUnpackedRawDataType );
118    
119 }
120
121 AliHLTComponentDataType AliHLTTPCClusterFinderComponent::GetOutputDataType()
122 {
123   // see header file for class documentation
124   return AliHLTTPCDefinitions::fgkClustersDataType;
125 }
126
127 void AliHLTTPCClusterFinderComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
128 {
129   // see header file for class documentation
130   // XXX TODO: Find more realistic values.  
131   constBase = 0;
132   if (fPackedSwitch)  inputMultiplier = (6 * 0.4);
133   else  inputMultiplier = 0.4;
134 }
135
136 AliHLTComponent* AliHLTTPCClusterFinderComponent::Spawn()
137 {
138   // see header file for class documentation
139   return new AliHLTTPCClusterFinderComponent(fPackedSwitch);
140 }
141         
142 int AliHLTTPCClusterFinderComponent::DoInit( int argc, const char** argv )
143 {
144   // see header file for class documentation
145   if ( fClusterFinder )
146     return EINPROGRESS;
147
148   fClusterFinder = new AliHLTTPCClusterFinder();
149
150   Int_t rawreadermode =  -1;
151   Int_t sigthresh = -1;
152   Float_t occulimit = 1.0;
153   Int_t oldRCUFormat=0;
154   // Data Format version numbers:
155   // 0: RCU Data format as delivered during TPC commissioning, pads/padrows are sorted, RCU trailer is one 32 bit word.
156   // 1: As 0, but pads/padrows are delivered "as is", without sorting
157   // 2: As 0, but RCU trailer is 3 32 bit words.
158   // 3: As 1, but RCU trailer is 3 32 bit words.
159   // -1: use offline raw reader
160
161   Int_t i = 0;
162   Char_t* cpErr;
163
164   while ( i < argc ) {      
165
166     // -- raw reader mode option
167     if ( !strcmp( argv[i], "rawreadermode" ) ) {
168       if ( argc <= i+1 ) {
169         Logging( kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Missing rawreadermode", "Raw Reader Mode not specified" );
170         return ENOTSUP;
171       }
172
173       // Decodes the rawreader mode: either number or string and returns the rawreadermode
174       // -1 on failure, -2 for offline
175       rawreadermode = AliHLTTPCDigitReaderRaw::DecodeMode( argv[i+1] );
176
177       if (rawreadermode == -1 ) {
178         Logging( kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Missing rawreadermode", "Cannot convert rawreadermode specifier '%s'.", argv[i+1] );
179         return EINVAL;
180       }
181
182       i += 2;
183       continue;
184     }
185
186     // -- pp run option
187     if ( !strcmp( argv[i], "pp-run" ) ) {
188       fClusterDeconv = false;
189       i++;
190       continue;
191     }
192
193     // -- zero suppression threshold
194     if ( !strcmp( argv[i], "adc-threshold" ) ) {
195       sigthresh = strtoul( argv[i+1], &cpErr ,0);
196       if ( *cpErr ) {
197         HLTError("Cannot convert threshold specifier '%s'.", argv[i+1]);
198         return EINVAL;
199       }
200       i+=2;
201       continue;
202     }
203
204     // -- pad occupancy limit
205     if ( !strcmp( argv[i], "occupancy-limit" ) ) {
206       occulimit = strtof( argv[i+1], &cpErr);
207       if ( *cpErr ) {
208         HLTError("Cannot convert occupancy specifier '%s'.", argv[i+1]);
209         return EINVAL;
210       }
211       i+=2;
212       continue;
213     }
214
215     // -- number of timebins (default 1024)
216     if ( !strcmp( argv[i], "timebins" ) ) {
217       TString parameter(argv[i+1]);
218       parameter.Remove(TString::kLeading, ' '); // remove all blanks
219       if (parameter.IsDigit()) {
220         AliHLTTPCTransform::SetNTimeBins(parameter.Atoi());
221         HLTInfo("number of timebins set to %d", AliHLTTPCTransform::GetNTimeBins());
222       } else {
223         HLTError("Cannot timebin specifier '%s'.", argv[i+1]);
224         return EINVAL;
225       }
226       i+=2;
227       continue;
228     }
229
230     // -- checking for rcu format
231     if ( !strcmp( argv[i], "oldrcuformat" ) ) {
232       oldRCUFormat = strtoul( argv[i+1], &cpErr ,0);
233       if ( *cpErr ){
234         HLTError("Cannot convert oldrcuformat specifier '%s'. Should  be 0(off) or 1(on), must be integer", argv[i+1]);
235         return EINVAL;
236       }
237       i+=2;
238       continue;
239     }
240       
241     // -- checking for unsorted clusterfinding
242     if ( !strcmp( argv[i], "unsorted" ) ) {
243       fUnsorted = strtoul( argv[i+1], &cpErr ,0);
244       if ( *cpErr ){
245         HLTError("Cannot convert unsorted specifier '%s'. Should  be 0(off) or 1(on), must be integer", argv[i+1]);
246         return EINVAL;
247       }
248       i+=2;
249       continue;
250     }
251       
252     // -- checking for unsorted clusterfinding
253     if ( !strcmp( argv[i], "patch" ) ) {
254       fPatch = strtoul( argv[i+1], &cpErr ,0);
255       if ( *cpErr ){
256         HLTError("Cannot convert patch specifier '%s'. Should  be between 0 and 5, must be integer", argv[i+1]);
257         return EINVAL;
258       }
259       i+=2;
260       continue;
261     }
262
263     Logging(kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Unknown Option", "Unknown option '%s'", argv[i] );
264     return EINVAL;
265
266   }
267
268   // Choose reader
269
270   if (fPackedSwitch) { 
271     if (rawreadermode == -2) {
272 #if defined(HAVE_ALIRAWDATA) && defined(HAVE_ALITPCRAWSTREAM_H)
273       fReader = new AliHLTTPCDigitReaderPacked();
274       if(oldRCUFormat==1){
275         fReader->SetOldRCUFormat(kTRUE);
276       }
277       else if(oldRCUFormat!=0){
278         HLTWarning("Wrong oldrcuformat specifier %d; oldrcuformat set to default(kFALSE)",oldRCUFormat);
279       }
280       if(fUnsorted){
281         fReader->SetUnsorted(kTRUE);
282       }
283       fClusterFinder->SetReader(fReader);
284 #else // ! defined(HAVE_ALIRAWDATA) && defined(HAVE_ALITPCRAWSTREAM_H)
285       HLTFatal("DigitReaderPacked not available - check your build");
286       return -ENODEV;
287 #endif //  defined(HAVE_ALIRAWDATA) && defined(HAVE_ALITPCRAWSTREAM_H)
288     } else {
289 #if defined(HAVE_TPC_MAPPING)
290       fReader = new AliHLTTPCDigitReaderRaw(rawreadermode);
291       fClusterFinder->SetReader(fReader);
292 #else //! defined(HAVE_TPC_MAPPING)
293       HLTFatal("DigitReaderRaw not available - check your build");
294       return -ENODEV;
295 #endif //defined(HAVE_TPC_MAPPING)
296     }
297   }
298   else {
299     fReader = new AliHLTTPCDigitReaderUnpacked();
300     fClusterFinder->SetReader(fReader);
301   }
302
303   // if pp-run use occupancy limit else set to 1. ==> use all 
304   if ( !fClusterDeconv )
305     fClusterFinder->SetOccupancyLimit(occulimit);
306   else 
307     fClusterFinder->SetOccupancyLimit(1.0);
308       
309   // Variables to setup the Clusterfinder
310   // TODO: this sounds strange and has to be verified; is the cluster finder not working when
311   // fClusterDeconv = false ?
312   fClusterDeconv = true;
313   fXYClusterError = -1;
314   fZClusterError = -1;
315
316  
317   fClusterFinder->SetDeconv( fClusterDeconv );
318   fClusterFinder->SetXYError( fXYClusterError );
319   fClusterFinder->SetZError( fZClusterError );
320   if ( (fXYClusterError>0) && (fZClusterError>0) )
321     fClusterFinder->SetCalcErr( false );
322   fClusterFinder->SetSignalThreshold(sigthresh);
323     
324   if(fUnsorted&&fPatch>-1&&fPatch<6){
325     fPadArray = new AliHLTTPCPadArray(fPatch);
326     fPadArray->InitializeVector();
327   }
328
329   return 0;
330 }
331
332 int AliHLTTPCClusterFinderComponent::DoDeinit()
333 {
334   // see header file for class documentation
335
336   if ( fClusterFinder )
337     delete fClusterFinder;
338   fClusterFinder = NULL;
339  
340   if ( fReader )
341     delete fReader;
342   fReader = NULL;
343     
344   return 0;
345 }
346
347 int AliHLTTPCClusterFinderComponent::DoEvent( const AliHLTComponentEventData& evtData, 
348                                               const AliHLTComponentBlockData* blocks, 
349                                               AliHLTComponentTriggerData& trigData, AliHLTUInt8_t* outputPtr, 
350                                               AliHLTUInt32_t& size, 
351                                               vector<AliHLTComponentBlockData>& outputBlocks )
352 {
353   // see header file for class documentation
354
355   //  == init iter (pointer to datablock)
356   const AliHLTComponentBlockData* iter = NULL;
357   unsigned long ndx;
358
359   //  == OUTdatatype pointer
360   AliHLTTPCClusterData* outPtr;
361
362   AliHLTUInt8_t* outBPtr;
363   UInt_t offset, mysize, nSize, tSize = 0;
364
365   outBPtr = outputPtr;
366   outPtr = (AliHLTTPCClusterData*)outBPtr;
367
368   Int_t slice, patch, row[2];
369   unsigned long maxPoints, realPoints = 0;
370
371   for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
372     {
373       iter = blocks+ndx;
374       mysize = 0;
375       offset = tSize;
376
377
378       if (fPackedSwitch) {      
379         char tmp1[14], tmp2[14];
380         DataType2Text( iter->fDataType, tmp1 );
381         DataType2Text( AliHLTTPCDefinitions::fgkDDLPackedRawDataType, tmp2 );
382         Logging( kHLTLogDebug, "HLT::TPCClusterFinder::DoEvent", "Event received", 
383                  "Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
384                  evtData.fEventID, evtData.fEventID, tmp1, tmp2 );
385
386         if ( iter->fDataType != AliHLTTPCDefinitions::fgkDDLPackedRawDataType ) continue;
387
388       }
389       else {
390         char tmp1[14], tmp2[14];
391         DataType2Text( iter->fDataType, tmp1 );
392         DataType2Text( AliHLTTPCDefinitions::fgkUnpackedRawDataType, tmp2 );
393         Logging( kHLTLogDebug, "HLT::TPCClusterFinder::DoEvent", "Event received", 
394                  "Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
395                  evtData.fEventID, evtData.fEventID, tmp1, tmp2 );
396
397         if ( iter->fDataType != AliHLTTPCDefinitions::fgkUnpackedRawDataType ) continue;
398
399       }
400         
401       slice = AliHLTTPCDefinitions::GetMinSliceNr( *iter );
402       patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
403       row[0] = AliHLTTPCTransform::GetFirstRow( patch );
404       row[1] = AliHLTTPCTransform::GetLastRow( patch );
405         
406       Logging( kHLTLogDebug, "HLT::TPCClusterFinder::DoEvent", "Input Spacepoints", 
407                "Input: Number of spacepoints: %lu Slice/Patch/RowMin/RowMax: %d/%d/%d/%d.",
408                realPoints, slice, patch, row[0], row[1] );
409         
410       outPtr = (AliHLTTPCClusterData*)outBPtr;
411
412 #ifndef KENNETH
413       maxPoints = (size-tSize-sizeof(AliHLTTPCClusterData))/sizeof(AliHLTTPCSpacePointData);
414 #else
415       maxPoints = (size-tSize-sizeof(AliHLTTPCClusters))/sizeof(AliHLTTPCSpacePointData);
416 #endif 
417
418       fClusterFinder->InitSlice( slice, patch, row[0], row[1], maxPoints );
419       fClusterFinder->SetOutputArray( outPtr->fSpacePoints );
420         
421       if(fUnsorted){
422
423
424         fClusterFinder->SetPadArray(fPadArray);
425           
426         double totalT=0;
427         struct timeval startT, endT;
428         gettimeofday( &startT, NULL );
429
430         fClusterFinder->ReadDataUnsorted(iter->fPtr, iter->fSize );
431
432         gettimeofday( &endT, NULL );
433         unsigned long long dt;
434         dt = endT.tv_sec-startT.tv_sec;
435         dt *= 1000000ULL;
436         dt += endT.tv_usec-startT.tv_usec;
437         double dtd = ((double)dt);
438         totalT += dtd;
439         //        dtd = dtd / (double)eventIterations;
440         //        if ( iterations<=1 )
441         cout<<endl;
442         printf( "Time needed to read data: %f microsec. / %f millisec. / %f s\n", 
443                 dtd, dtd/1000.0, dtd/1000000.0 );
444           
445         cout<<endl;
446         fClusterFinder->FindClusters();
447       }
448       else{
449         fClusterFinder->Read(iter->fPtr, iter->fSize );
450         fClusterFinder->ProcessDigits();
451       }
452       realPoints = fClusterFinder->GetNumberOfClusters();
453         
454       Logging( kHLTLogDebug, "HLT::TPCClusterFinder::DoEvent", "Spacepoints", 
455                "Number of spacepoints found: %lu.", realPoints );
456         
457       outPtr->fSpacePointCnt = realPoints;
458       nSize = sizeof(AliHLTTPCSpacePointData)*realPoints;
459 #ifndef KENNETH
460       mysize += nSize+sizeof(AliHLTTPCClusterData);
461 #else
462       mysize += nSize+sizeof(AliHLTTPCClusters);
463 #endif 
464         
465       Logging( kHLTLogDebug, "HLT::TPCClusterFinder::DoEvent", "Input Spacepoints", 
466                "Number of spacepoints: %lu Slice/Patch/RowMin/RowMax: %d/%d/%d/%d.",
467                realPoints, slice, patch, row[0], row[1] );
468       AliHLTComponentBlockData bd;
469       FillBlockData( bd );
470       bd.fOffset = offset;
471       bd.fSize = mysize;
472       bd.fSpecification = iter->fSpecification;
473       //AliHLTSubEventDescriptor::FillBlockAttributes( bd.fAttributes );
474       outputBlocks.push_back( bd );
475         
476       tSize += mysize;
477       outBPtr += mysize;
478       outPtr = (AliHLTTPCClusterData*)outBPtr;
479         
480       if ( tSize > size )
481         {
482           Logging( kHLTLogFatal, "HLT::TPCClusterFinder::DoEvent", "Too much data", 
483                    "Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
484                    tSize, size );
485           return EMSGSIZE;
486         }
487     }
488     
489   size = tSize;
490
491   return 0;
492 }