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