]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TRD/AliHLTTRDClusterizerComponent.cxx
Moving reconstruction settings to the reco params, new cosmics reco param OCDB entry...
[u/mrichter/AliRoot.git] / HLT / TRD / AliHLTTRDClusterizerComponent.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:                                                       *
8 //*                  for The ALICE HLT 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 
21     @date   
22     @brief  A TRDClusterizer processing component for the HLT. 
23 */
24
25 // see header file for class documentation                                   //
26 // or                                                                        //
27 // refer to README to build package                                          //
28 // or                                                                        //
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt                          //
30
31 #if __GNUC__ >= 3
32 using namespace std;
33 #endif
34
35 #include "TTree.h"
36 #include "TFile.h"
37 #include "TBranch.h"
38
39 #include "AliHLTTRDClusterizerComponent.h"
40 #include "AliHLTTRDDefinitions.h"
41 #include "AliHLTTRDCluster.h"
42
43 #include "AliGeomManager.h"
44 #include "AliTRDReconstructor.h"
45 #include "AliCDBManager.h"
46 #include "AliCDBStorage.h"
47 #include "AliCDBEntry.h"
48 #include "AliHLTTRDClusterizer.h"
49 #include "AliTRDrecoParam.h"
50 #include "AliTRDrawStreamBase.h"
51 #include "AliTRDcluster.h"
52
53 #include "AliRawReaderMemory.h"
54
55 #ifdef HAVE_VALGRIND_CALLGRIND_H
56 #include <valgrind/callgrind.h>
57 #else
58 #define CALLGRIND_START_INSTRUMENTATION do { } while (0)
59 #define CALLGRIND_STOP_INSTRUMENTATION do { } while (0)
60 #endif
61
62 #include <cstdlib>
63 #include <cerrno>
64 #include <string>
65
66 ClassImp(AliHLTTRDClusterizerComponent)
67    
68 AliHLTTRDClusterizerComponent::AliHLTTRDClusterizerComponent():
69   AliHLTProcessor(),
70   fOutputPercentage(500),
71   fOutputConst(0),
72   fClusterizer(NULL),
73   fRecoParam(NULL),
74   fMemReader(NULL),
75   fReconstructor(NULL),
76   fRecoParamType(-1),
77   fRecoDataType(-1),
78   fRawDataVersion(2),
79   fyPosMethod(1),
80   fgeometryFileName(""),
81   fProcessTracklets(kFALSE),
82   fHLTstreamer(kFALSE)
83 {
84   // Default constructor
85
86 }
87
88 AliHLTTRDClusterizerComponent::~AliHLTTRDClusterizerComponent()
89 {
90   // Destructor
91   // Work is Done in DoDeInit()
92 }
93
94
95 const char* AliHLTTRDClusterizerComponent::GetComponentID()
96 {
97   // Return the component ID const char *
98   return "TRDClusterizer"; // The ID of this component
99 }
100
101 void AliHLTTRDClusterizerComponent::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
102 {
103   // Get the list of input data
104   list.clear(); // We do not have any requirements for our input data type(s).
105   list.push_back( (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD) );
106 }
107
108 AliHLTComponent_DataType AliHLTTRDClusterizerComponent::GetOutputDataType()
109 {
110   // Get the output data type
111   return kAliHLTMultipleDataType;
112 }
113
114 int AliHLTTRDClusterizerComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
115 {
116   // Get the output data type
117   tgtList.clear();
118   tgtList.push_back(AliHLTTRDDefinitions::fgkClusterDataType);
119   tgtList.push_back(AliHLTTRDDefinitions::fgkMCMtrackletDataType);
120   return tgtList.size();
121 }
122
123
124 void AliHLTTRDClusterizerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
125 {
126   // Get the output data size
127   constBase = fOutputConst;
128   inputMultiplier = ((double)fOutputPercentage)/100.0;
129 }
130
131 AliHLTComponent* AliHLTTRDClusterizerComponent::Spawn()
132 {
133   // Spawn function, return new instance of this class
134   return new AliHLTTRDClusterizerComponent;
135 };
136
137 int AliHLTTRDClusterizerComponent::DoInit( int argc, const char** argv )
138 {
139   // perform initialization. We check whether our relative output size is specified in the arguments.
140   int iResult=0;
141   
142   fReconstructor = new AliTRDReconstructor();
143   HLTDebug("TRDReconstructor at 0x%x", fReconstructor);
144
145   TString configuration="";
146   TString argument="";
147   for (int i=0; i<argc && iResult>=0; i++) {
148     argument=argv[i];
149     if (!configuration.IsNull()) configuration+=" ";
150     configuration+=argument;
151   }
152
153   if (!configuration.IsNull()) {
154     iResult=Configure(configuration.Data());
155   } else {
156     iResult=Reconfigure(NULL, NULL);
157   }
158
159   if(!fClusterizer){
160     HLTFatal("Clusterizer was not initialized!");
161     return -1;
162   }
163
164   if(iResult<0) return iResult;
165
166   fMemReader = new AliRawReaderMemory;
167   fClusterizer->SetReconstructor(fReconstructor);
168   fClusterizer->SetUseLabels(kFALSE);
169
170   if(fReconstructor->IsProcessingTracklets())
171     fOutputConst = fClusterizer->GetTrMemBlockSize();
172   
173   return iResult;
174 }
175
176 int AliHLTTRDClusterizerComponent::DoDeinit()
177 {
178   // Deinitialization of the component
179   delete fMemReader;
180   fMemReader = 0;
181   delete fClusterizer;
182   fClusterizer = 0;
183   
184   fReconstructor->SetClusters(0x0);
185   delete fReconstructor;
186   fReconstructor = 0x0;
187   return 0;
188
189   if (fRecoParam)
190     {
191       HLTDebug("Deleting fRecoParam");
192       delete fRecoParam;
193       fRecoParam = 0;
194     }
195 }
196
197 int AliHLTTRDClusterizerComponent::DoEvent( const AliHLTComponentEventData& evtData, 
198                                             const AliHLTComponentBlockData* blocks, 
199                                             AliHLTComponent_TriggerData& /*trigData*/, 
200                                             AliHLTUInt8_t* outputPtr, 
201                                             AliHLTUInt32_t& size, 
202                                             vector<AliHLTComponent_BlockData>& outputBlocks )
203 {
204   // Process an event
205
206   if (evtData.fEventID == 1)
207     CALLGRIND_START_INSTRUMENTATION;
208
209   HLTDebug( "NofBlocks %i", evtData.fBlockCnt );
210   // Process an event
211   AliHLTUInt32_t totalSize = 0, offset = 0;
212
213   //implement a usage of the following
214   //   AliHLTUInt32_t triggerDataStructSize = trigData.fStructSize;
215   //   AliHLTUInt32_t triggerDataSize = trigData.fDataSize;
216   //   void *triggerData = trigData.fData;
217   //HLTDebug( "Trigger data received. Struct size %d Data size %d Data location 0x%x", trigData.fStructSize, trigData.fDataSize, (UInt_t*)trigData.fData);
218
219   // Loop over all input blocks in the event
220   AliHLTComponentDataType expectedDataType = (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD);
221   for ( UInt_t iBlock = 0; iBlock < evtData.fBlockCnt; iBlock++ )
222     {      
223       const AliHLTComponentBlockData &block = blocks[iBlock];
224       // lets not use the internal TRD data types here : AliHLTTRDDefinitions::fgkDDLRawDataType
225       // which is depreciated - we use HLT global defs instead
226       //      if ( block.fDataType != (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD) )
227       AliHLTComponentDataType inputDataType = block.fDataType;
228       if ( inputDataType != expectedDataType)
229         {
230           HLTDebug( "Block # %i/%i; Event 0x%08LX (%Lu) Wrong received datatype: %s - required datatype: %s; Skipping",
231                     iBlock, evtData.fBlockCnt,
232                     evtData.fEventID, evtData.fEventID, 
233                     DataType2Text(inputDataType).c_str(), 
234                     DataType2Text(expectedDataType).c_str());
235           continue;
236         }
237       else 
238         {
239           HLTDebug("We get the right data type: Block # %i/%i; Event 0x%08LX (%Lu) Received datatype: %s; Block Size: %i",
240                    iBlock, evtData.fBlockCnt,
241                    evtData.fEventID, evtData.fEventID, 
242                    DataType2Text(inputDataType).c_str(),
243                    block.fSize);
244         }
245       
246 #ifndef NDEBUG
247       unsigned long constBase;
248       double inputMultiplier;
249       GetOutputDataSize(constBase,inputMultiplier);
250       if(size<(constBase+block.fSize*inputMultiplier)){
251         HLTWarning("Memory Block given might be too small: %i < %i; Event %Lu", size, constBase+block.fSize*inputMultiplier, evtData.fEventID);
252       }
253 #endif
254
255       // fMemReader->Reset();
256       fMemReader->SetMemory((UChar_t*) block.fPtr, block.fSize);
257
258       AliHLTUInt32_t spec = block.fSpecification;
259       
260       Int_t id = 1024;
261       
262       for ( Int_t ii = 0; ii < 18 ; ii++ ) {
263         if ( spec & 0x1 ) {
264           id += ii;
265           break;
266         }
267         spec = spec >> 1 ;
268       }
269
270       fMemReader->SetEquipmentID( id ); 
271       
272       fClusterizer->SetMemBlock(outputPtr+offset);
273       Bool_t iclustered = fClusterizer->Raw2ClustersChamber(fMemReader);
274       if (iclustered == kTRUE)
275         {
276           HLTDebug( "Clustered successfully");
277         }
278       else
279         {
280           HLTError("Clustering ERROR");
281           return -1;
282         }
283
284       // put the tree into output
285       //fcTree->Print();
286       
287       AliHLTUInt32_t addedSize;
288       if(fReconstructor->IsProcessingTracklets()){
289         addedSize = fClusterizer->GetAddedTrSize();
290         totalSize += fClusterizer->GetTrMemBlockSize();  //if IsProcessingTracklets() is enabled we always reserve a data block of size GetTrMemBlockSize() for the tracklets
291         if (addedSize > 0){
292           // Using low-level interface 
293           // with interface classes
294           if ( totalSize > size )
295             {
296               HLTError("Too much data; Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
297                        totalSize, size );
298               return EMSGSIZE;
299             }
300
301           // Fill block 
302           AliHLTComponentBlockData bd;
303           FillBlockData( bd );
304           bd.fOffset = offset;
305           bd.fSize = addedSize;
306           bd.fSpecification = block.fSpecification;
307           bd.fDataType = AliHLTTRDDefinitions::fgkMCMtrackletDataType;
308           outputBlocks.push_back( bd );
309           HLTDebug( "BD fPtr 0x%x, fOffset %i, size %i, dataType %s, spec 0x%x ", bd.fPtr, bd.fOffset, bd.fSize, DataType2Text(bd.fDataType).c_str(), spec);
310         }
311         offset = totalSize;
312       }
313
314       addedSize = fClusterizer->GetAddedClSize();
315       if (addedSize > 0){
316         // Using low-level interface 
317         // with interface classes
318         totalSize += addedSize;
319         if ( totalSize > size )
320           {
321             HLTError("Too much data; Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
322                      totalSize, size );
323             return EMSGSIZE;
324           }
325                 
326         // Fill block 
327         AliHLTComponentBlockData bd;
328         FillBlockData( bd );
329         bd.fOffset = offset;
330         bd.fSize = addedSize;
331         bd.fSpecification = block.fSpecification;
332         bd.fDataType = AliHLTTRDDefinitions::fgkClusterDataType;
333         outputBlocks.push_back( bd );
334         HLTDebug( "BD fPtr 0x%x, fOffset %i, size %i, dataType %s, spec 0x%x ", bd.fPtr, bd.fOffset, bd.fSize, DataType2Text(bd.fDataType).c_str(), spec);
335         offset = totalSize;
336               
337       }
338       else 
339         HLTDebug("Array of clusters is empty!");
340     }
341   fReconstructor->SetClusters(0x0);
342
343   size = totalSize;
344   HLTDebug("Event is done. size written to the output is %i", size);
345   return 0;
346 }
347
348 void AliHLTTRDClusterizerComponent::PrintObject( TClonesArray* inClustersArray)
349 {
350   AliTRDcluster* cluster=0x0;
351   
352   for (Int_t i=0; i < inClustersArray->GetEntriesFast(); i++){
353     cluster = dynamic_cast<AliTRDcluster*>(inClustersArray->At(i));
354     HLTDebug("cluster[%i]",i);
355     HLTDebug("  PadCol = %i; PadRow = %i; PadTime = %i", cluster->GetPadCol(), cluster->GetPadRow(), cluster->GetPadTime());
356     HLTDebug("  Detector = %i, Amplitude = %f, Center = %f", cluster->GetDetector(), cluster->GetQ(), cluster->GetCenter());
357     HLTDebug("  LocalTimeBin =  %i; NPads = %i; maskedPosition: %s, status: %s", cluster->GetLocalTimeBin(), cluster->GetNPads(),cluster->GetPadMaskedPosition(),cluster->GetPadMaskedPosition());
358   }
359   
360 }
361
362 int AliHLTTRDClusterizerComponent::Configure(const char* arguments){
363   int iResult=0;
364   if (!arguments) return iResult;
365   
366   TString allArgs=arguments;
367   TString argument;
368   int bMissingParam=0;
369
370   TObjArray* pTokens=allArgs.Tokenize(" ");
371   if (pTokens) {
372     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
373       argument=((TObjString*)pTokens->At(i))->GetString();
374       if (argument.IsNull()) continue;
375       
376       if (argument.CompareTo("output_percentage")==0) {
377         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
378         HLTInfo("Setting output percentage to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
379         fOutputPercentage=((TObjString*)pTokens->At(i))->GetString().Atoi();
380         continue;
381       } 
382       else if (argument.CompareTo("-geometry")==0) {
383         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
384         HLTInfo("Setting geometry to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
385         fgeometryFileName=((TObjString*)pTokens->At(i))->GetString();
386         continue;
387       } 
388       if (argument.CompareTo("-lowflux")==0) {
389         fRecoParamType = 0;
390         HLTInfo("Low flux reconstruction selected");
391         continue;
392       }
393       if (argument.CompareTo("-highflux")==0) {
394         fRecoParamType = 1;
395         HLTInfo("High flux reconstruction selected");
396         continue;
397       }
398       if (argument.CompareTo("-cosmics")==0) {
399         fRecoParamType = 2;
400         HLTInfo("Cosmics reconstruction selected");
401         continue;
402       }
403       if (argument.CompareTo("-simulation")==0) {
404         fRecoDataType = 0;
405         HLTInfo("Awaiting simulated data");
406         continue;
407       }
408       if (argument.CompareTo("-experiment")==0) {
409         fRecoDataType = 1;
410         HLTInfo("Awaiting real data");
411         continue;
412       }
413       if (argument.CompareTo("-processTracklets")==0) {
414         fProcessTracklets = kTRUE;
415         HLTInfo("Processing L1 Tracklets");
416         continue;
417       }
418       if (argument.CompareTo("-noZS")==0) {
419         fOutputPercentage = 100;
420         HLTInfo("Awaiting non zero surpressed data");
421         continue;
422       }
423       if (argument.CompareTo("-faststreamer")==0) {
424         fHLTstreamer = kTRUE;
425         HLTInfo("Useing fast raw streamer");
426         continue;
427       }
428       else if (argument.CompareTo("-rawver")==0) {
429         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
430         HLTInfo("Raw data version is: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
431         fRawDataVersion=((TObjString*)pTokens->At(i))->GetString().Atoi();
432         continue;
433       } 
434       else if (argument.CompareTo("-yPosMethod")==0) {
435         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
436         TString toCompareTo=((TObjString*)pTokens->At(i))->GetString();
437         if (toCompareTo.CompareTo("COG")==0){
438           HLTInfo("Setting yPosMethod method to: %s", toCompareTo.Data());
439           fyPosMethod=0;
440         }
441         else if (toCompareTo.CompareTo("LUT")==0){
442           HLTInfo("Setting yPosMethod method to: %s", toCompareTo.Data());
443           fyPosMethod=1;
444         }
445         else if (toCompareTo.CompareTo("Gauss")==0){
446           HLTInfo("Setting yPosMethod method to: %s", toCompareTo.Data());
447           fyPosMethod=2;
448         }
449         else {
450           HLTError("unknown argument for yPosMethod: %s", toCompareTo.Data());
451           iResult=-EINVAL;
452           break;
453         }
454         continue;
455       } 
456       
457       else {
458         HLTError("unknown argument: %s", argument.Data());
459         iResult=-EINVAL;
460         break;
461       }
462     }
463     delete pTokens;
464   }
465   if (bMissingParam) {
466     HLTError("missing parameter for argument %s", argument.Data());
467     iResult=-EINVAL;
468   }
469   if(iResult>=0){
470     iResult=SetParams();
471   }
472   return iResult;
473 }
474
475 int AliHLTTRDClusterizerComponent::SetParams()
476 {
477   Int_t iResult=0;
478   if(!AliCDBManager::Instance()->IsDefaultStorageSet()){
479     HLTError("DefaultStorage is not set in CDBManager");
480     return -EINVAL;
481   }
482   if(AliCDBManager::Instance()->GetRun()<0){
483     HLTError("Run Number is not set in CDBManager");
484     return -EINVAL;
485   }
486   HLTInfo("CDB default storage: %s; RunNo: %i", (AliCDBManager::Instance()->GetDefaultStorage()->GetBaseFolder()).Data(), AliCDBManager::Instance()->GetRun());
487
488   if(!AliGeomManager::GetGeometry()){
489     if(fgeometryFileName.CompareTo("")==0 || !TFile::Open(fgeometryFileName.Data())){
490       HLTInfo("Loading standard geometry file");
491       AliGeomManager::LoadGeometry();
492     }else{
493       HLTWarning("Loading NON-standard geometry file");
494       AliGeomManager::LoadGeometry(fgeometryFileName.Data());
495     }
496     if(!AliGeomManager::GetGeometry()){
497       HLTError("Could not load geometry");
498       return -EINVAL;
499     }
500   }
501   else{
502     HLTInfo("Geometry Already Loaded!");
503   }
504
505   TString recoOptions="hlt,!cw,sl_cl_0";
506
507   switch(fRecoDataType){
508   case 0: recoOptions += ",tc"; break;
509   case 1: recoOptions += ",!tc"; break;
510   }
511   switch(fyPosMethod){
512   case 0: recoOptions += ",!gs,!lut"; break;
513   case 1: recoOptions += ",!gs,lut"; break;
514   case 2: recoOptions += ",gs,!lut"; break;
515   }
516   if(fProcessTracklets) recoOptions += ",tp";
517   else  recoOptions += ",!tp";
518
519
520   if (fRecoParamType == 0)
521     {
522       HLTDebug("Low flux params init.");
523       fRecoParam = AliTRDrecoParam::GetLowFluxParam();
524     }
525
526   if (fRecoParamType == 1)
527     {
528       HLTDebug("High flux params init.");
529       fRecoParam = AliTRDrecoParam::GetHighFluxParam();
530     }
531   
532   if (fRecoParamType == 2)
533     {
534       HLTDebug("Cosmic Test params init.");
535       fRecoParam = AliTRDrecoParam::GetCosmicTestParam();
536     }
537
538   if (fRecoParam == 0)
539     {
540       HLTError("No reco params initialized. Sniffing big trouble!");
541       return -EINVAL;
542     }
543
544   fRecoParam->SetStreamLevel(AliTRDrecoParam::kClusterizer, 0);
545   fReconstructor->SetRecoParam(fRecoParam);
546
547   HLTDebug("Reconstructor options are: %s",recoOptions.Data());
548   fReconstructor->SetOption(recoOptions.Data());
549
550   if (fRecoDataType < 0 || fRecoDataType > 1)
551     {
552       HLTWarning("No data type selected. Use -simulation or -experiment flag. Defaulting to simulation.");
553       fRecoDataType = 0;
554     }
555
556   if (fRecoDataType == 0)
557     {
558       AliTRDrawStreamBase::SetRawStreamVersion(AliTRDrawStreamBase::kTRDsimStream);
559       HLTDebug("Data type expected is SIMULATION!");
560     }
561
562   if (fRecoDataType == 1)
563     {
564       AliTRDrawStreamBase::SetRawStreamVersion(AliTRDrawStreamBase::kTRDrealStream);
565       HLTDebug("Data type expected is EXPERIMENT!");
566     }
567
568   if (fHLTstreamer)
569     {
570       AliTRDrawStreamBase::SetRawStreamVersion("FAST");
571       HLTDebug("fast rawstreamer used");  
572     }
573
574   if(!fClusterizer){
575     fClusterizer = new AliHLTTRDClusterizer("TRDCclusterizer", "TRDCclusterizer");  
576     HLTDebug("TRDClusterizer at 0x%x", fClusterizer);
577   }
578
579   fClusterizer->SetRawVersion(fRawDataVersion);
580
581   return iResult;
582 }
583
584 int AliHLTTRDClusterizerComponent::Reconfigure(const char* cdbEntry, const char* chainId)
585 {
586   // see header file for class documentation
587
588   int iResult=0;
589   const char* path="HLT/ConfigTRD/ClusterizerComponent";
590   const char* defaultNotify="";
591   if (cdbEntry) {
592     path=cdbEntry;
593     defaultNotify=" (default)";
594   }
595   if (path) {
596     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
597     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
598     if (pEntry) {
599       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
600       if (pString) {
601         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
602         iResult=Configure(pString->GetString().Data());
603       } else {
604         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
605       }
606     } else {
607       HLTError("cannot fetch object \"%s\" from CDB", path);
608     }
609   }
610
611   return iResult;
612 }