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