]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCClusterFinderComponent.cxx
code documentation
[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 // see header file for class documentation                                   //
28 // or                                                                        //
29 // refer to README to build package                                          //
30 // or                                                                        //
31 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt                          //
32
33 #if __GNUC__>= 3
34 using namespace std;
35 #endif
36 #include "AliHLTTPCClusterFinderComponent.h"
37 #include "AliHLTTPCDigitReaderPacked.h"
38 #include "AliHLTTPCDigitReaderUnpacked.h"
39 #include "AliHLTTPCDigitReaderRaw.h"
40 #include "AliHLTTPCDigitReaderDecoder.h"
41 #include "AliHLTTPCClusterFinder.h"
42 #include "AliHLTTPCSpacePointData.h"
43 #include "AliHLTTPCClusterDataFormat.h"
44 #include "AliHLTTPCTransform.h"
45 #include "AliHLTTPCClusters.h"
46 #include "AliHLTTPCDefinitions.h"
47 #include "AliCDBEntry.h"
48 #include "AliCDBManager.h"
49
50 #include <cstdlib>
51 #include <cerrno>
52 #include "TString.h"
53 #include "TObjString.h"
54 #include <sys/time.h>
55
56 // this is a global object used for automatic component registration, do not use this
57 // use fPackedSwitch = true for packed inputtype "gkDDLPackedRawDataType"
58 // use fPackedSwitch = false for unpacked inputtype "gkUnpackedRawDataType"
59 AliHLTTPCClusterFinderComponent gAliHLTTPCClusterFinderComponentPacked(AliHLTTPCClusterFinderComponent::kClusterFinderPacked);
60 AliHLTTPCClusterFinderComponent gAliHLTTPCClusterFinderComponentUnpacked(AliHLTTPCClusterFinderComponent::kClusterFinderUnpacked);
61 AliHLTTPCClusterFinderComponent gAliHLTTPCClusterFinderComponentDecoder(AliHLTTPCClusterFinderComponent::kClusterFinderDecoder);
62
63 /** ROOT macro for the implementation of ROOT specific class methods */
64 ClassImp(AliHLTTPCClusterFinderComponent)
65
66 AliHLTTPCClusterFinderComponent::AliHLTTPCClusterFinderComponent(int mode)
67   :
68   fClusterFinder(NULL),
69   fReader(NULL),
70   fClusterDeconv(true),
71   fXYClusterError(-1),
72   fZClusterError(-1),
73   fModeSwitch(mode),
74   fUnsorted(0),
75   fPatch(0),
76   fPadArray(NULL),
77   fGetActivePads(0)
78 {
79   // see header file for class documentation
80   // or
81   // refer to README to build package
82   // or
83   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
84 }
85
86 AliHLTTPCClusterFinderComponent::~AliHLTTPCClusterFinderComponent()
87 {
88   // see header file for class documentation
89 }
90
91 // Public functions to implement AliHLTComponent's interface.
92 // These functions are required for the registration process
93
94 const char* AliHLTTPCClusterFinderComponent::GetComponentID()
95 {
96   // see header file for class documentation
97   switch(fModeSwitch){
98   case kClusterFinderPacked:
99     return "TPCClusterFinderPacked";
100     break;
101   case kClusterFinderUnpacked:
102     return "TPCClusterFinderUnpacked";
103     break;
104   case kClusterFinderDecoder:
105     return "TPCClusterFinderDecoder";
106     break;
107   }
108 }
109
110 void AliHLTTPCClusterFinderComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
111 {
112   // see header file for class documentation
113   list.clear(); 
114   switch(fModeSwitch){
115   case kClusterFinderPacked:
116     list.push_back( kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC );
117     break;
118   case kClusterFinderUnpacked:
119     list.push_back( AliHLTTPCDefinitions::fgkUnpackedRawDataType );
120     break;
121   case kClusterFinderDecoder:
122     list.push_back( kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC );
123     break;
124   }
125 }
126
127 AliHLTComponentDataType AliHLTTPCClusterFinderComponent::GetOutputDataType()
128 {
129   // see header file for class documentation
130   return kAliHLTMultipleDataType;
131 }
132
133 int AliHLTTPCClusterFinderComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
134
135 {
136   // see header file for class documentation
137   tgtList.clear();
138   tgtList.push_back(AliHLTTPCDefinitions::fgkClustersDataType);
139   tgtList.push_back(AliHLTTPCDefinitions::fgkActivePadsDataType);
140   return tgtList.size();
141 }
142
143 void AliHLTTPCClusterFinderComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
144 {
145   // see header file for class documentation
146   // XXX TODO: Find more realistic values.  
147   constBase = 0;
148   switch(fModeSwitch){
149   case 0:
150     inputMultiplier = (6 * 0.4);
151     break;
152   case 1:
153     inputMultiplier = 0.4;
154     break;
155   case 2:
156     inputMultiplier = (6 * 0.4);
157     break;
158   }
159 }
160
161 AliHLTComponent* AliHLTTPCClusterFinderComponent::Spawn()
162 {
163   // see header file for class documentation
164   return new AliHLTTPCClusterFinderComponent(fModeSwitch);
165 }
166         
167 int AliHLTTPCClusterFinderComponent::DoInit( int argc, const char** argv )
168 {
169   // see header file for class documentation
170   if ( fClusterFinder )
171     return EINPROGRESS;
172
173   fClusterFinder = new AliHLTTPCClusterFinder();
174
175   Int_t rawreadermode =  -1;
176   Int_t sigthresh = -1;
177   Double_t sigmathresh= -1;
178   Float_t occulimit = 1.0;
179   Int_t oldRCUFormat=0;
180   // Data Format version numbers:
181   // 0: RCU Data format as delivered during TPC commissioning, pads/padrows are sorted, RCU trailer is one 32 bit word.
182   // 1: As 0, but pads/padrows are delivered "as is", without sorting
183   // 2: As 0, but RCU trailer is 3 32 bit words.
184   // 3: As 1, but RCU trailer is 3 32 bit words.
185   // -1: use offline raw reader
186
187   Int_t i = 0;
188   Char_t* cpErr;
189
190   while ( i < argc ) {      
191
192     // -- raw reader mode option
193     if ( !strcmp( argv[i], "rawreadermode" ) ) {
194       if ( argc <= i+1 ) {
195         Logging( kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Missing rawreadermode", "Raw Reader Mode not specified" );
196         return ENOTSUP;
197       }
198
199       // Decodes the rawreader mode: either number or string and returns the rawreadermode
200       // -1 on failure, -2 for offline
201       rawreadermode = AliHLTTPCDigitReaderRaw::DecodeMode( argv[i+1] );
202
203       if (rawreadermode == -1 ) {
204         Logging( kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Missing rawreadermode", "Cannot convert rawreadermode specifier '%s'.", argv[i+1] );
205         return EINVAL;
206       }
207
208       i += 2;
209       continue;
210     }
211
212     // -- pp run option
213     if ( !strcmp( argv[i], "pp-run" ) ) {
214       fClusterDeconv = false;
215       i++;
216       continue;
217     }
218
219     // -- zero suppression threshold
220     if ( !strcmp( argv[i], "adc-threshold" ) ) {
221       sigthresh = strtoul( argv[i+1], &cpErr ,0);
222       if ( *cpErr ) {
223         HLTError("Cannot convert threshold specifier '%s'.", argv[i+1]);
224         return EINVAL;
225       }
226       i+=2;
227       continue;
228     }
229
230     // -- pad occupancy limit
231     if ( !strcmp( argv[i], "occupancy-limit" ) ) {
232       occulimit = strtod( argv[i+1], &cpErr);
233       if ( *cpErr ) {
234         HLTError("Cannot convert occupancy specifier '%s'.", argv[i+1]);
235         return EINVAL;
236       }
237       i+=2;
238       continue;
239     }
240
241     // -- number of timebins (default 1024)
242     if ( !strcmp( argv[i], "timebins" ) ) {
243       TString parameter(argv[i+1]);
244       parameter.Remove(TString::kLeading, ' '); // remove all blanks
245       if (parameter.IsDigit()) {
246         AliHLTTPCTransform::SetNTimeBins(parameter.Atoi());
247         HLTInfo("number of timebins set to %d, zbin=%f", AliHLTTPCTransform::GetNTimeBins(), AliHLTTPCTransform::GetZWidth());
248       } else {
249         HLTError("Cannot timebin specifier '%s'.", argv[i+1]);
250         return EINVAL;
251       }
252       i+=2;
253       continue;
254     }
255
256     // -- checking for rcu format
257     if ( !strcmp( argv[i], "oldrcuformat" ) ) {
258       oldRCUFormat = strtoul( argv[i+1], &cpErr ,0);
259       if ( *cpErr ){
260         HLTError("Cannot convert oldrcuformat specifier '%s'. Should  be 0(off) or 1(on), must be integer", argv[i+1]);
261         return EINVAL;
262       }
263       i+=2;
264       continue;
265     }
266       
267     // -- checking for unsorted clusterfinding
268     if ( !strcmp( argv[i], "unsorted" ) ) {
269       fUnsorted = strtoul( argv[i+1], &cpErr ,0);
270       if ( *cpErr ){
271         HLTError("Cannot convert unsorted specifier '%s'. Should  be 0(off) or 1(on), must be integer", argv[i+1]);
272         return EINVAL;
273       }
274       i+=2;
275       continue;
276     }
277       
278     // -- checking for active pads, used in 2007 December run
279     if ( !strcmp( argv[i], "activepads" ) ) {
280       fGetActivePads = strtoul( argv[i+1], &cpErr ,0);
281       if ( *cpErr ){
282         HLTError("Cannot convert activepads specifier '%s'. Should  be 0(off) or 1(on), must be integer", argv[i+1]);
283         return EINVAL;
284       }
285       i+=2;
286       continue;
287     }
288
289     // -- checking for nsigma-threshold, used in 2007 December run in ZeroSuppression
290     if ( !strcmp( argv[i], "nsigma-threshold" ) ) {
291        sigmathresh = strtoul( argv[i+1], &cpErr ,0);
292       if ( *cpErr ){
293         HLTError("Cannot convert nsigma-threshold specifier '%s'. Must be integer", argv[i+1]);
294         return EINVAL;
295       }
296       i+=2;
297       continue;
298     }
299
300     Logging(kHLTLogError, "HLT::TPCClusterFinder::DoInit", "Unknown Option", "Unknown option '%s'", argv[i] );
301     return EINVAL;
302
303   }
304
305   // Choose reader
306   if (fModeSwitch==kClusterFinderPacked) { 
307     if (rawreadermode == -2) {
308       HLTDebug("using AliHLTTPCDigitReaderPacked");
309       fReader = new AliHLTTPCDigitReaderPacked();
310       if(oldRCUFormat==1){
311         fReader->SetOldRCUFormat(kTRUE);
312       }
313       else if(oldRCUFormat!=0){
314         HLTWarning("Wrong oldrcuformat specifier %d; oldrcuformat set to default(kFALSE)",oldRCUFormat);
315       }
316       if(fUnsorted==1){
317         fReader->SetUnsorted(kTRUE);
318       }
319       fClusterFinder->SetReader(fReader);
320     } 
321     else {
322 #if defined(HAVE_TPC_MAPPING)
323       HLTDebug("using AliHLTTPCDigitReaderRaw mode %d", rawreadermode);
324       fReader = new AliHLTTPCDigitReaderRaw(rawreadermode);
325       fClusterFinder->SetReader(fReader);
326 #else //! defined(HAVE_TPC_MAPPING)
327       HLTFatal("DigitReaderRaw not available - check your build");
328       return -ENODEV;
329 #endif //defined(HAVE_TPC_MAPPING)
330     }
331   }
332   else if(fModeSwitch==kClusterFinderUnpacked){
333     HLTDebug("using AliHLTTPCDigitReaderUnpacked");
334     fReader = new AliHLTTPCDigitReaderUnpacked();
335     fClusterFinder->SetReader(fReader);
336   }
337   else if(fModeSwitch==kClusterFinderDecoder){
338     fReader = new AliHLTTPCDigitReaderDecoder();
339     fClusterFinder->SetReader(fReader);
340   }
341   else{
342     HLTFatal("No mode set for clusterfindercomponent");
343   }
344   // if pp-run use occupancy limit else set to 1. ==> use all 
345   if ( !fClusterDeconv )
346     fClusterFinder->SetOccupancyLimit(occulimit);
347   else 
348     fClusterFinder->SetOccupancyLimit(1.0);
349       
350   // Variables to setup the Clusterfinder
351   // TODO: this sounds strange and has to be verified; is the cluster finder not working when
352   // fClusterDeconv = false ?
353   fClusterDeconv = true;
354   fXYClusterError = -1;
355   fZClusterError = -1;
356
357  
358   fClusterFinder->SetDeconv( fClusterDeconv );
359   fClusterFinder->SetXYError( fXYClusterError );
360   fClusterFinder->SetZError( fZClusterError );
361   if ( (fXYClusterError>0) && (fZClusterError>0) )
362     fClusterFinder->SetCalcErr( false );
363   fClusterFinder->SetSignalThreshold(sigthresh);
364   fClusterFinder->SetNSigmaThreshold(sigmathresh);
365
366   return 0;
367 }
368
369 int AliHLTTPCClusterFinderComponent::DoDeinit()
370 {
371   // see header file for class documentation
372
373   if ( fClusterFinder )
374     delete fClusterFinder;
375   fClusterFinder = NULL;
376  
377   if ( fReader )
378     delete fReader;
379   fReader = NULL;
380     
381   return 0;
382 }
383
384 int AliHLTTPCClusterFinderComponent::DoEvent( const AliHLTComponentEventData& evtData, 
385                                               const AliHLTComponentBlockData* blocks, 
386                                               AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, 
387                                               AliHLTUInt32_t& size, 
388                                               vector<AliHLTComponentBlockData>& outputBlocks )
389 {
390   // see header file for class documentation
391
392   //  == init iter (pointer to datablock)
393   const AliHLTComponentBlockData* iter = NULL;
394   unsigned long ndx;
395
396   //  == OUTdatatype pointer
397   AliHLTTPCClusterData* outPtr;
398
399   AliHLTUInt8_t* outBPtr;
400   UInt_t offset, mysize, nSize, tSize = 0;
401
402   outBPtr = outputPtr;
403   outPtr = (AliHLTTPCClusterData*)outBPtr;
404
405   Int_t slice, patch, row[2];
406   unsigned long maxPoints, realPoints = 0;
407
408   for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
409     {
410       iter = blocks+ndx;
411       mysize = 0;
412       offset = tSize;
413
414
415       if (fModeSwitch==0 || fModeSwitch==2) {
416         HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
417                  evtData.fEventID, evtData.fEventID, 
418                  DataType2Text( iter->fDataType).c_str(), 
419                  DataType2Text(kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC).c_str());
420
421         if (iter->fDataType == AliHLTTPCDefinitions::fgkDDLPackedRawDataType &&
422             GetEventCount()<2) {
423           HLTWarning("data type %s is depricated, use %s (kAliHLTDataTypeDDLRaw)!",
424                      DataType2Text(AliHLTTPCDefinitions::fgkDDLPackedRawDataType).c_str(),
425                      DataType2Text(kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC).c_str());
426           }
427
428         if ( iter->fDataType != (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTPC) &&
429              iter->fDataType != AliHLTTPCDefinitions::fgkDDLPackedRawDataType ) continue;
430
431       }
432       else if(fModeSwitch==1){
433         HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
434                  evtData.fEventID, evtData.fEventID, 
435                  DataType2Text( iter->fDataType).c_str(), 
436                  DataType2Text(AliHLTTPCDefinitions::fgkUnpackedRawDataType).c_str());
437
438         if ( iter->fDataType != AliHLTTPCDefinitions::fgkUnpackedRawDataType ) continue;
439
440       }
441         
442       slice = AliHLTTPCDefinitions::GetMinSliceNr( *iter );
443       patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
444       row[0] = AliHLTTPCTransform::GetFirstRow( patch );
445       row[1] = AliHLTTPCTransform::GetLastRow( patch );
446
447
448       if(fUnsorted){
449         fClusterFinder->SetUnsorted(fUnsorted);
450         fClusterFinder->SetPatch(patch);
451       }
452       /*      if(fUnsorted){
453               if(fPadArray==NULL){
454               fClusterFinder->SetUnsorted(fUnsorted);
455               //fPadArray = new AliHLTTPCPadArray(patch);
456               //fPadArray->InitializeVector(fModeSwitch);
457               }
458               else if(fPadArray->GetPatch()!=patch||fPadArray->GetPatch()==-1){
459               if (GetEventCount()<3) {
460               HLTWarning("pad array not initialized for data of specification 0x%08x, block skipped", iter->fSpecification);
461               } else if ((GetEventCount()%5000)==0) { // assuming 0.5 to 1kHz this gives a message rate of 0.1 to 0.5 Hz
462               HLTWarning("reminder: pad array not initialized for data of specification 0x%08x", iter->fSpecification);
463               }
464               continue;
465               }
466               }
467       */
468       
469
470       outPtr = (AliHLTTPCClusterData*)outBPtr;
471
472       maxPoints = (size-tSize-sizeof(AliHLTTPCClusterData))/sizeof(AliHLTTPCSpacePointData);
473
474       fClusterFinder->InitSlice( slice, patch, row[0], row[1], maxPoints );
475       fClusterFinder->SetOutputArray( (AliHLTTPCSpacePointData*)outPtr->fSpacePoints );
476         
477       if(fUnsorted){
478         fClusterFinder->ReadDataUnsorted(iter->fPtr, iter->fSize, fModeSwitch);
479
480         fClusterFinder->FindClusters();
481       }
482       else{
483         fClusterFinder->Read(iter->fPtr, iter->fSize );
484         fClusterFinder->ProcessDigits();
485       }
486       realPoints = fClusterFinder->GetNumberOfClusters();
487         
488       outPtr->fSpacePointCnt = realPoints;
489       nSize = sizeof(AliHLTTPCSpacePointData)*realPoints;
490       mysize += nSize+sizeof(AliHLTTPCClusterData);
491
492       Logging( kHLTLogDebug, "HLT::TPCClusterFinder::DoEvent", "Spacepoints", 
493                "Number of spacepoints: %lu Slice/Patch/RowMin/RowMax: %d/%d/%d/%d.",
494                realPoints, slice, patch, row[0], row[1] );
495       AliHLTComponentBlockData bd;
496       FillBlockData( bd );
497       bd.fOffset = offset;
498       bd.fSize = mysize;
499       bd.fSpecification = iter->fSpecification;
500       bd.fDataType = AliHLTTPCDefinitions::fgkClustersDataType;
501       //AliHLTSubEventDescriptor::FillBlockAttributes( bd.fAttributes );
502       outputBlocks.push_back( bd );
503         
504       tSize += mysize;
505       outBPtr += mysize;
506       outPtr = (AliHLTTPCClusterData*)outBPtr;
507         
508       /*      if(fGetActivePads){
509               AliHLTTPCPadArray::AliHLTTPCActivePads* outPtrActive;
510               UInt_t activePadsSize, activePadsN = 0;
511               outPtrActive = (AliHLTTPCPadArray::AliHLTTPCActivePads*)outBPtr;
512               offset=tSize;
513               Int_t maxActivePads = (size-tSize)/sizeof(AliHLTTPCPadArray::AliHLTTPCActivePads);
514               activePadsSize= fClusterFinder->GetActivePads((AliHLTTPCPadArray::AliHLTTPCActivePads*)outPtrActive,maxActivePads)*sizeof(AliHLTTPCPadArray::AliHLTTPCActivePads);
515         
516               AliHLTComponentBlockData bdActive;
517               FillBlockData( bdActive );
518               bdActive.fOffset = offset;
519               bdActive.fSize = activePadsSize;
520               bdActive.fSpecification = iter->fSpecification;
521               bdActive.fDataType = AliHLTTPCDefinitions::fgkActivePadsDataType;
522               outputBlocks.push_back( bdActive );
523         
524               tSize+=activePadsSize;
525               outBPtr += activePadsSize;
526               outPtrActive = (AliHLTTPCPadArray::AliHLTTPCActivePads*)outBPtr;
527               }
528       */
529  
530
531       if ( tSize > size )
532         {
533           Logging( kHLTLogFatal, "HLT::TPCClusterFinder::DoEvent", "Too much data", 
534                    "Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
535                    tSize, size );
536           return EMSGSIZE;
537         }
538     }
539     
540   size = tSize;
541
542   return 0;
543 }
544
545 int AliHLTTPCClusterFinderComponent::Reconfigure(const char* cdbEntry, const char* chainId)
546 {
547   // see header file for class documentation
548   const char* path="HLT/ConfigTPC";
549   if (cdbEntry) path=cdbEntry;
550   if (path) {
551     HLTInfo("reconfigure from entry %s, chain id %s", path, (chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
552     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
553     if (pEntry) {
554       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
555       if (pString) {
556         HLTInfo("received configuration object: %s", pString->GetString().Data());
557       } else {
558         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
559       }
560     } else {
561       HLTError("can not fetch object \"%s\" from CDB", path);
562     }
563   }
564 }