]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TRD/AliHLTTRDTrackerV1Component.cxx
ab333c3084dde47e4e5aa97306449cf007f4fc92
[u/mrichter/AliRoot.git] / HLT / TRD / AliHLTTRDTrackerV1Component.cxx
1 // $Id: AliHLTTRDTrackerV1Component.cxx 23618 2008-01-29 13:07:38Z hristov $
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   AliHLTTRDTrackerV1Component.cxx
20     @author 
21     @date   
22     @brief  A TRDTrackerV1 processing component for the HLT.
23 */
24
25 #if __GNUC__ >= 3
26 using namespace std;
27 #endif
28
29 #include "AliHLTTRDTrackerV1Component.h"
30 #include "AliHLTTRDDefinitions.h"
31 #include "AliHLTTRDCluster.h"
32 #include "AliHLTTRDTrack.h"
33 #include "AliHLTTRDUtils.h"
34
35 #include "TFile.h"
36 #include "TChain.h"
37
38 #include "AliGeomManager.h"
39 #include "AliCDBManager.h"
40 #include "AliCDBStorage.h"
41 #include "AliCDBEntry.h"
42 #include "AliESDEvent.h"
43 #include "AliESDfriend.h"
44
45 #include "AliTRDcalibDB.h"
46 #include "AliTRDReconstructor.h"
47 #include "AliTRDtrackerV1.h"
48 #include "AliTRDrecoParam.h"
49
50 #include <cstdlib>
51 #include <cerrno>
52 #include <string>
53
54 #ifdef HAVE_VALGRIND_CALLGRIND_H
55 #include <valgrind/callgrind.h>
56 #else
57 #define CALLGRIND_START_INSTRUMENTATION do { } while (0)
58 #define CALLGRIND_STOP_INSTRUMENTATION do { } while (0)
59 #endif
60
61 ClassImp(AliHLTTRDTrackerV1Component)
62     
63 AliHLTTRDTrackerV1Component::AliHLTTRDTrackerV1Component():
64   AliHLTProcessor(),
65   fOutputPercentage(100), // By default we copy to the output exactly what we got as input 
66   fTracker(NULL),
67   fRecoParam(NULL),
68   fReconstructor(NULL),
69   fESD(NULL),
70   fClusterArray(NULL),
71   fRecoParamType(-1),
72   fNtimeBins(-1),
73   fMagneticField(-1),
74   fPIDmethod(1),
75   fgeometryFileName(""),
76   fieldStrength(-101),
77   fSlowTracking(kFALSE),
78   fOutputV1Tracks(kTRUE)
79 {
80   // Default constructor
81
82 }
83
84 AliHLTTRDTrackerV1Component::~AliHLTTRDTrackerV1Component()
85 {
86   // Destructor
87 }
88
89 const char* AliHLTTRDTrackerV1Component::GetComponentID()
90 {
91   // Return the component ID const char *
92   return "TRDTrackerV1"; // The ID of this component
93 }
94
95 void AliHLTTRDTrackerV1Component::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
96 {
97   // Get the list of input data  
98   list.clear(); // We do not have any requirements for our input data type(s).
99   list.push_back(AliHLTTRDDefinitions::fgkClusterDataType);
100 }
101
102 AliHLTComponentDataType AliHLTTRDTrackerV1Component::GetOutputDataType()
103 {
104   // Get the output data type
105   return kAliHLTMultipleDataType;
106 }
107
108 int AliHLTTRDTrackerV1Component::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
109 {
110   // Get the output data types
111   tgtList.clear();
112   //tgtList.push_back(AliHLTTRDDefinitions::fgkTimeBinPropagationDataType);
113   tgtList.push_back(kAliHLTDataTypeTrack | kAliHLTDataOriginTRD);
114   tgtList.push_back(AliHLTTRDDefinitions::fgkTRDSATracksDataType);
115   return tgtList.size();
116 }
117
118 void AliHLTTRDTrackerV1Component::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
119 {
120   // Get the output data size
121   constBase = 0;
122   inputMultiplier = fOutputV1Tracks ? 2*((double)fOutputPercentage)/100.0 : 0.5*((double)fOutputPercentage)/100.0;
123 }
124
125 // Spawn function, return new instance of this class
126 AliHLTComponent* AliHLTTRDTrackerV1Component::Spawn()
127 {
128   return new AliHLTTRDTrackerV1Component;
129 };
130
131
132 int AliHLTTRDTrackerV1Component::DoInit( int argc, const char** argv )
133 {
134   // perform initialization. We check whether our relative output size is specified in the arguments.
135   int iResult=0;
136
137   fReconstructor = new AliTRDReconstructor();
138   HLTDebug("TRDReconstructor at 0x%x", fReconstructor);
139
140   fESD = new AliESDEvent;
141   fESD->CreateStdContent();
142   
143   TString configuration="";
144   TString argument="";
145   for (int i=0; i<argc && iResult>=0; i++) {
146     argument=argv[i];
147     if (!configuration.IsNull()) configuration+=" ";
148     configuration+=argument;
149   }
150
151   if (!configuration.IsNull()) {
152     iResult=Configure(configuration.Data());
153   } else {
154     iResult=Reconfigure(NULL, NULL);
155   }
156
157   if(iResult<0) return iResult;
158
159   fTracker = new AliTRDtrackerV1();
160   HLTDebug("TRDTracker at 0x%x", fTracker);
161   fTracker->SetReconstructor(fReconstructor);
162
163   fClusterArray = new TClonesArray("AliTRDcluster"); // would be nice to allocate memory for all clusters here.
164
165   return iResult;
166 }
167
168 int AliHLTTRDTrackerV1Component::DoDeinit()
169 {
170   // Deinitialization of the component
171
172   fTracker->SetClustersOwner(kFALSE);
173   delete fTracker;
174   fTracker = NULL;
175
176   fClusterArray->Delete();
177   delete fClusterArray;
178   fClusterArray = NULL;
179   
180   // We need to set clusters in Reconstructor to null to prevent from 
181   // double deleting, since we delete TClonesArray by ourself.
182   fReconstructor->SetClusters(0x0);
183   delete fReconstructor;
184   fReconstructor = NULL;
185   delete fESD;
186   fESD = NULL;
187   
188   AliTRDcalibDB::Terminate();
189
190   return 0;
191 }
192
193 int AliHLTTRDTrackerV1Component::DoEvent( const AliHLTComponentEventData& evtData, 
194                                           const AliHLTComponentBlockData* blocks, 
195                                           AliHLTComponent_TriggerData& /*trigData*/, 
196                                           AliHLTUInt8_t* outputPtr, 
197                                           AliHLTUInt32_t& size, 
198                                           vector<AliHLTComponent_BlockData>& outputBlocks )
199 {
200   // Process an event
201
202   if (evtData.fEventID == 1)
203     CALLGRIND_START_INSTRUMENTATION;
204
205   HLTDebug("NofBlocks %i", evtData.fBlockCnt );
206   
207   fESD->Reset();
208   //fESD->SetMagneticField(GetBz());
209
210   AliHLTUInt32_t totalSize = 0, offset = 0;
211
212   vector<AliHLTComponent_DataType> expectedDataTypes;
213   GetInputDataTypes(expectedDataTypes);
214   for ( unsigned long iBlock = 0; iBlock < evtData.fBlockCnt; iBlock++ ) 
215     {
216       const AliHLTComponentBlockData &block = blocks[iBlock];
217       AliHLTComponentDataType inputDataType = block.fDataType;
218       Bool_t correctDataType = kFALSE;
219
220       for(UInt_t i = 0; i < expectedDataTypes.size(); i++){
221         if( expectedDataTypes.at(i) == inputDataType)
222           correctDataType = kTRUE;
223       }
224       if (!correctDataType)
225         {
226           HLTDebug( "Block # %i/%i; Event 0x%08LX (%Lu) Wrong received datatype: %s - Skipping",
227                     iBlock, evtData.fBlockCnt-1,
228                     evtData.fEventID, evtData.fEventID, 
229                     DataType2Text(inputDataType).c_str());
230           continue;
231         }
232       else {
233         HLTDebug("We get the right data type: Block # %i/%i; Event 0x%08LX (%Lu) Received datatype: %s; Block Size: %i",
234                  iBlock, evtData.fBlockCnt-1,
235                  evtData.fEventID, evtData.fEventID, 
236                  DataType2Text(inputDataType).c_str(),
237                  block.fSize);
238       }
239
240 #ifndef NDEBUG
241       unsigned long constBase;
242       double inputMultiplier;
243       GetOutputDataSize(constBase,inputMultiplier);
244       if(size<(constBase+block.fSize*inputMultiplier)){
245         HLTWarning("Memory Block given might be too small: %i < %i; Event %Lu", size, constBase+block.fSize*inputMultiplier, evtData.fEventID);
246       }
247 #endif      
248
249       AliHLTTRDUtils::ReadClusters(fClusterArray, block.fPtr, block.fSize, &fNtimeBins);
250       HLTDebug("Reading number of time bins from input block. Setting number of timebins to %d", fNtimeBins);
251       AliTRDtrackerV1::SetNTimeBins(fNtimeBins);
252
253       HLTDebug("TClonesArray of clusters: nbEntries = %i", fClusterArray->GetEntriesFast());
254       fTracker->LoadClusters(fClusterArray);
255
256       fTracker->Clusters2Tracks(fESD);
257
258       Int_t nTracks = fESD->GetNumberOfTracks();
259       HLTInfo("Number of tracks  == %d ==", nTracks);  
260
261       TClonesArray* trdTracks;
262       trdTracks = fTracker->GetListOfTracks();
263       
264       if(nTracks>0){
265         HLTDebug("We have an output ESDEvent: 0x%x with %i tracks", fESD, nTracks);
266         AliHLTUInt32_t addedSize = AliHLTTRDUtils::AddESDToOutput(fESD, outputPtr+offset);
267         totalSize += addedSize;
268           
269         // Fill block 
270         AliHLTComponentBlockData bd;
271         FillBlockData( bd );
272         //bd.fPtr = outputPtr;
273         bd.fOffset = offset;
274         bd.fSize = addedSize;
275         bd.fSpecification = block.fSpecification;
276         bd.fDataType = kAliHLTDataTypeTrack | kAliHLTDataOriginTRD;
277         outputBlocks.push_back( bd );
278         HLTDebug("BD ptr 0x%x, offset %i, size %i, datav1Type %s, spec 0x%x ", bd.fPtr, bd.fOffset, bd.fSize, DataType2Text(bd.fDataType).c_str(), bd.fSpecification);
279         offset = totalSize;
280
281         if (fOutputV1Tracks && trdTracks){
282           HLTDebug("We have an output array: pointer to trdTracks = 0x%x, nbEntries = %i", trdTracks, trdTracks->GetEntriesFast());
283           
284           addedSize = AliHLTTRDUtils::AddTracksToOutput(trdTracks, outputPtr+offset, fNtimeBins);
285           totalSize += addedSize;
286           
287           // Fill block 
288           FillBlockData( bd );
289           //bd.fPtr = outputPtr;
290           bd.fOffset = offset;
291           bd.fSize = addedSize;
292           bd.fSpecification = block.fSpecification;
293           bd.fDataType = AliHLTTRDDefinitions::fgkTRDSATracksDataType;
294           outputBlocks.push_back( bd );
295           HLTDebug("BD ptr 0x%x, offset %i, size %i, dataType %s, spec 0x%x ", bd.fPtr, bd.fOffset, bd.fSize, DataType2Text(bd.fDataType).c_str(), bd.fSpecification);
296           offset = totalSize;
297         }
298       }
299       
300       HLTDebug("totalSize: %i", totalSize);
301       
302 //       if ( totalSize > allocSize )
303 //      {
304 //        HLTError("Too much data; Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
305 //        totalSize, size );
306 //        return EMSGSIZE;
307 //      }
308
309       //here we are deleting clusters (but not the TClonesArray itself)
310       fTracker->UnloadClusters();
311       AliTRDReconstructor::SetClusters(0x0);
312       fClusterArray->Delete();
313       
314     }
315       
316   size = totalSize;
317   HLTDebug("Event is done. size written to the output is %i", size);
318   return 0;
319 }
320
321 int AliHLTTRDTrackerV1Component::Configure(const char* arguments){
322   int iResult=0;
323   if (!arguments) return iResult;
324   
325   TString allArgs=arguments;
326   TString argument;
327   int bMissingParam=0;
328
329   TObjArray* pTokens=allArgs.Tokenize(" ");
330   if (pTokens) {
331     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
332       argument=((TObjString*)pTokens->At(i))->GetString();
333       if (argument.IsNull()) continue;
334       
335       if (argument.CompareTo("output_percentage")==0) {
336         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
337         HLTInfo("Setting output percentage to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
338         fOutputPercentage=((TObjString*)pTokens->At(i))->GetString().Atoi();
339         continue;
340       } 
341       else if (argument.CompareTo("-solenoidBz")==0) {
342         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
343         HLTWarning("argument -solenoidBz is deprecated, magnetic field set up globally (%f)", GetBz());
344         continue;
345       } 
346       else if (argument.CompareTo("-NTimeBins")==0) {
347         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
348         HLTInfo("Option depreceated");
349         continue;
350       } 
351       else if (argument.CompareTo("-geometry")==0) {
352         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
353         HLTInfo("Setting geometry to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
354         fgeometryFileName=((TObjString*)pTokens->At(i))->GetString();
355         continue;
356       } 
357       else if (argument.CompareTo("-lowflux")==0) {
358         fRecoParamType = 0;
359         HLTInfo("Low flux reconstruction selected");
360         continue;
361       }
362       else if (argument.CompareTo("-highflux")==0) {
363         fRecoParamType = 1;
364         HLTInfo("High flux reconstruction selected");
365         continue;
366       }
367       else if (argument.CompareTo("-cosmics")==0) {
368         fRecoParamType = 2;
369         HLTInfo("Cosmics reconstruction selected");
370         continue;
371       }
372       else if (argument.CompareTo("-magnetic_field_ON")==0) {
373         fMagneticField = 1;
374         HLTInfo("Reconstructon with magnetic field");
375         continue;
376       }
377       else if (argument.CompareTo("-magnetic_field_OFF")==0) {
378         fMagneticField = 0;
379         HLTInfo("Reconstructon without magnetic field");
380         continue;
381       }
382       else if (argument.CompareTo("-slowTracking")==0) {
383         fSlowTracking = kTRUE;
384         HLTInfo("Using slow tracking");
385         continue;
386       }
387       else if (argument.CompareTo("-outputV1Tracks")==0) {
388         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
389         TString toCompareTo=((TObjString*)pTokens->At(i))->GetString();
390         if (toCompareTo.CompareTo("yes")==0){
391           HLTInfo("Setting OutputV1Tracks to: %s", toCompareTo.Data());
392           fOutputV1Tracks=kTRUE;
393         }
394         else if (toCompareTo.CompareTo("no")==0){
395           HLTInfo("Setting OutputV1Tracks to: %s", toCompareTo.Data());
396           fOutputV1Tracks=kFALSE;
397         }
398         else {
399           HLTError("unknown argument for OutputV1Tracks: %s", toCompareTo.Data());
400           iResult=-EINVAL;
401           break;
402         }
403         continue;
404       } 
405       else if (argument.CompareTo("-PIDmethod")==0) {
406         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
407         TString toCompareTo=((TObjString*)pTokens->At(i))->GetString();
408         if (toCompareTo.CompareTo("LH")==0){
409           HLTInfo("Setting PID method to: %s", toCompareTo.Data());
410           fPIDmethod=0;
411         }
412         else if (toCompareTo.CompareTo("NN")==0){
413           HLTInfo("Setting PID method to: %s", toCompareTo.Data());
414           fPIDmethod=1;
415         }
416         else if (toCompareTo.CompareTo("TM")==0){
417           HLTInfo("Setting PID method to: %s", toCompareTo.Data());
418           fPIDmethod=2;
419         }
420         else {
421           HLTError("unknown argument for PID method: %s", toCompareTo.Data());
422           iResult=-EINVAL;
423           break;
424         }
425         continue;
426       } 
427       
428       else {
429         HLTError("unknown argument: %s", argument.Data());
430         iResult=-EINVAL;
431         break;
432       }
433     }
434     delete pTokens;
435   }
436   if (bMissingParam) {
437     HLTError("missing parameter for argument %s", argument.Data());
438     iResult=-EINVAL;
439   }
440   if(iResult>=0){
441     iResult=SetParams();
442   }
443   return iResult;
444 }
445
446 int AliHLTTRDTrackerV1Component::SetParams()
447 {
448   Int_t iResult=0;
449   if(!AliCDBManager::Instance()->IsDefaultStorageSet()){
450     HLTError("DefaultStorage is not set in CDBManager");
451     return -EINVAL;
452   }
453   if(AliCDBManager::Instance()->GetRun()<0){
454     HLTError("Run Number is not set in CDBManager");
455     return -EINVAL;
456   }
457   HLTInfo("CDB default storage: %s; RunNo: %i", (AliCDBManager::Instance()->GetDefaultStorage()->GetBaseFolder()).Data(), AliCDBManager::Instance()->GetRun());
458
459   if(!AliGeomManager::GetGeometry()){
460     if(fgeometryFileName.CompareTo("")==0 || !TFile::Open(fgeometryFileName.Data())){
461       HLTInfo("Loading standard geometry file");
462       AliGeomManager::LoadGeometry();
463     }else{
464       HLTWarning("Loading NON-standard geometry file");
465       AliGeomManager::LoadGeometry(fgeometryFileName.Data());
466     }
467     if(!AliGeomManager::GetGeometry()){
468       HLTError("Could not load geometry");
469       return -EINVAL;
470     }
471     HLTInfo("Applying Alignment from CDB object");
472     AliGeomManager::ApplyAlignObjsFromCDB("TRD");
473   }
474   else{
475     HLTInfo("Geometry Already Loaded!");
476   }
477   
478   if (fRecoParamType == 0)
479     {
480       HLTDebug("Low flux params init.");
481       fRecoParam = AliTRDrecoParam::GetLowFluxParam();
482     }
483
484   if (fRecoParamType == 1)
485     {
486       HLTDebug("High flux params init.");
487       fRecoParam = AliTRDrecoParam::GetHighFluxParam();
488     }
489   
490   if (fRecoParamType == 2)
491     {
492       HLTDebug("Cosmic Test params init.");
493       fRecoParam = AliTRDrecoParam::GetCosmicTestParam();
494     }
495
496   if (fRecoParam == 0)
497     {
498       HLTError("No reco params initialized. Sniffing big trouble!");
499       return -EINVAL;
500     }
501
502   // backward compatibility to AliTRDrecoParam < r34995
503 # ifndef HAVE_NOT_ALITRDRECOPARAM_r34995
504 #   define AliTRDRecoParamSetPIDNeuralNetwork(b) fRecoParam->SetPIDNeuralNetwork(b)
505 # else
506 #   define AliTRDRecoParamSetPIDNeuralNetwork(b) fRecoParam->SetPIDNeuralNetwork()
507 # endif
508
509   switch(fPIDmethod){
510   case 0: AliTRDRecoParamSetPIDNeuralNetwork(kFALSE); break;
511   case 1: AliTRDRecoParamSetPIDNeuralNetwork(kTRUE); break;
512   case 2: AliTRDRecoParamSetPIDNeuralNetwork(kFALSE); break;
513   }
514
515   fRecoParam->SetStreamLevel(AliTRDrecoParam::kTracker, 0);
516   fReconstructor->SetRecoParam(fRecoParam);
517
518   TString recoOptions="sa,!cw";
519   
520   if(!fSlowTracking)
521     recoOptions += ",hlt";
522
523   HLTDebug("Reconstructor options are: %s",recoOptions.Data());
524   fReconstructor->SetOption(recoOptions.Data());
525
526   return iResult;
527 }
528
529 int AliHLTTRDTrackerV1Component::Reconfigure(const char* cdbEntry, const char* chainId)
530 {
531   // see header file for class documentation
532
533   int iResult=0;
534   const char* path="HLT/ConfigTRD/TrackerV1Component";
535   const char* defaultNotify="";
536   if (cdbEntry) {
537     path=cdbEntry;
538     defaultNotify=" (default)";
539   }
540   if (path) {
541     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
542     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
543     if (pEntry) {
544       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
545       if (pString) {
546         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
547         iResult=Configure(pString->GetString().Data());
548       } else {
549         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
550       }
551     } else {
552       HLTError("cannot fetch object \"%s\" from CDB", path);
553     }
554   }
555
556   return iResult;
557
558 }
559
560 int AliHLTTRDTrackerV1Component::ReadPreprocessorValues(const char* modules)
561 {
562   // see header file for class documentation
563   
564   int iResult = 0;
565   TString str(modules);
566   if(str.Contains("HLT") || str.Contains("TRD") || str.Contains("GRP")){
567   
568   }  
569   return iResult;
570 }