1 // $Id: AliHLTTRDTrackerV1Component.cxx 23618 2008-01-29 13:07:38Z hristov $
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 /** @file AliHLTTRDTrackerV1Component.cxx
22 @brief A TRDTrackerV1 processing component for the HLT.
29 #include "AliHLTTRDTrackerV1Component.h"
30 #include "AliHLTTRDDefinitions.h"
31 #include "AliHLTTRDCluster.h"
32 #include "AliHLTTRDTrack.h"
33 #include "AliHLTTRDUtils.h"
38 #include "AliGeomManager.h"
39 #include "AliCDBManager.h"
40 #include "AliCDBStorage.h"
41 #include "AliCDBEntry.h"
42 #include "AliESDEvent.h"
44 #include "AliESDfriend.h"
46 #include "AliTRDcalibDB.h"
47 #include "AliTRDReconstructor.h"
48 #include "AliTRDtrackerV1.h"
49 #include "AliTRDrecoParam.h"
55 #ifdef HAVE_VALGRIND_CALLGRIND_H
56 #include <valgrind/callgrind.h>
58 #define CALLGRIND_START_INSTRUMENTATION do { } while (0)
59 #define CALLGRIND_STOP_INSTRUMENTATION do { } while (0)
62 ClassImp(AliHLTTRDTrackerV1Component)
64 AliHLTTRDTrackerV1Component::AliHLTTRDTrackerV1Component():
66 fOutputPercentage(100), // By default we copy to the output exactly what we got as input
76 fgeometryFileName(""),
78 fSlowTracking(kFALSE),
79 fOutputV1Tracks(kTRUE)
81 // Default constructor
85 AliHLTTRDTrackerV1Component::~AliHLTTRDTrackerV1Component()
90 const char* AliHLTTRDTrackerV1Component::GetComponentID()
92 // Return the component ID const char *
93 return "TRDTrackerV1"; // The ID of this component
96 void AliHLTTRDTrackerV1Component::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
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);
103 AliHLTComponentDataType AliHLTTRDTrackerV1Component::GetOutputDataType()
105 // Get the output data type
106 return kAliHLTMultipleDataType;
109 int AliHLTTRDTrackerV1Component::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
111 // Get the output data types
113 //tgtList.push_back(AliHLTTRDDefinitions::fgkTimeBinPropagationDataType);
114 tgtList.push_back(kAliHLTDataTypeTrack | kAliHLTDataOriginTRD);
115 tgtList.push_back(AliHLTTRDDefinitions::fgkTRDSATracksDataType);
116 return tgtList.size();
119 void AliHLTTRDTrackerV1Component::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
121 // Get the output data size
123 inputMultiplier = ((double)fOutputPercentage)/100.0;
126 // Spawn function, return new instance of this class
127 AliHLTComponent* AliHLTTRDTrackerV1Component::Spawn()
129 return new AliHLTTRDTrackerV1Component;
133 int AliHLTTRDTrackerV1Component::DoInit( int argc, const char** argv )
135 // perform initialization. We check whether our relative output size is specified in the arguments.
138 fReconstructor = new AliTRDReconstructor();
139 HLTDebug("TRDReconstructor at 0x%x", fReconstructor);
141 fESD = new AliESDEvent;
142 fESD->CreateStdContent();
144 TString configuration="";
146 for (int i=0; i<argc && iResult>=0; i++) {
148 if (!configuration.IsNull()) configuration+=" ";
149 configuration+=argument;
152 if (!configuration.IsNull()) {
153 iResult=Configure(configuration.Data());
155 iResult=Reconfigure(NULL, NULL);
158 if(iResult<0) return iResult;
160 fTracker = new AliTRDtrackerV1();
161 HLTDebug("TRDTracker at 0x%x", fTracker);
162 fTracker->SetReconstructor(fReconstructor);
164 fClusterArray = new TClonesArray("AliTRDcluster"); // would be nice to allocate memory for all clusters here.
169 int AliHLTTRDTrackerV1Component::DoDeinit()
171 // Deinitialization of the component
173 fTracker->SetClustersOwner(kFALSE);
177 fClusterArray->Delete();
178 delete fClusterArray;
179 fClusterArray = NULL;
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;
189 AliTRDcalibDB::Terminate();
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 )
203 if (evtData.fEventID == 1)
204 CALLGRIND_START_INSTRUMENTATION;
206 HLTDebug("NofBlocks %i", evtData.fBlockCnt );
209 //fESD->SetMagneticField(fSolenoidBz);
211 AliHLTUInt32_t totalSize = 0, offset = 0;
213 vector<AliHLTComponent_DataType> expectedDataTypes;
214 GetInputDataTypes(expectedDataTypes);
215 for ( unsigned long iBlock = 0; iBlock < evtData.fBlockCnt; iBlock++ )
217 const AliHLTComponentBlockData &block = blocks[iBlock];
218 AliHLTComponentDataType inputDataType = block.fDataType;
219 Bool_t correctDataType = kFALSE;
221 for(UInt_t i = 0; i < expectedDataTypes.size(); i++){
222 if( expectedDataTypes.at(i) == inputDataType)
223 correctDataType = kTRUE;
225 if (!correctDataType)
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());
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(),
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);
250 AliHLTTRDUtils::ReadClusters(fClusterArray, block.fPtr, block.fSize, &fNtimeBins);
251 HLTDebug("Reading number of time bins from input block. Changing number of timebins to %d", fNtimeBins);
252 AliTRDtrackerV1::SetNTimeBins(fNtimeBins);
254 HLTDebug("TClonesArray of clusters: nbEntries = %i", fClusterArray->GetEntriesFast());
255 fTracker->LoadClusters(fClusterArray);
257 fTracker->Clusters2Tracks(fESD);
259 Int_t nTracks = fESD->GetNumberOfTracks();
260 HLTInfo("Number of tracks == %d ==", nTracks);
262 TClonesArray* trdTracks;
263 trdTracks = fTracker->GetListOfTracks();
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;
271 AliHLTComponentBlockData bd;
273 //bd.fPtr = outputPtr;
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);
282 if (fOutputV1Tracks && trdTracks){
283 HLTDebug("We have an output array: pointer to trdTracks = 0x%x, nbEntries = %i", trdTracks, trdTracks->GetEntriesFast());
285 addedSize = AliHLTTRDUtils::AddTracksToOutput(trdTracks, outputPtr+offset, fNtimeBins);
286 totalSize += addedSize;
290 //bd.fPtr = outputPtr;
292 bd.fSize = addedSize;
293 bd.fSpecification = block.fSpecification;
294 bd.fDataType = AliHLTTRDDefinitions::fgkTRDSATracksDataType;
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);
301 HLTDebug("totalSize: %i", totalSize);
303 // if ( totalSize > allocSize )
305 // HLTError("Too much data; Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
306 // totalSize, size );
310 //here we are deleting clusters (but not the TClonesArray itself)
311 fTracker->UnloadClusters();
312 AliTRDReconstructor::SetClusters(0x0);
313 fClusterArray->Delete();
318 HLTDebug("Event is done. size written to the output is %i", size);
322 int AliHLTTRDTrackerV1Component::Configure(const char* arguments){
324 if (!arguments) return iResult;
326 TString allArgs=arguments;
330 TObjArray* pTokens=allArgs.Tokenize(" ");
332 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
333 argument=((TObjString*)pTokens->At(i))->GetString();
334 if (argument.IsNull()) continue;
336 if (argument.CompareTo("output_percentage")==0) {
337 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
338 HLTInfo("Setting output percentage to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
339 fOutputPercentage=((TObjString*)pTokens->At(i))->GetString().Atoi();
342 else if (argument.CompareTo("-solenoidBz")==0) {
343 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
344 fieldStrength=((TObjString*)pTokens->At(i))->GetString().Atof();
345 HLTInfo("Setting Magnetic field to %.1f KGauss", fieldStrength);
348 else if (argument.CompareTo("-NTimeBins")==0) {
349 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
350 HLTInfo("Setting number of time bins to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
351 fNtimeBins=((TObjString*)pTokens->At(i))->GetString().Atoi();
354 else if (argument.CompareTo("-geometry")==0) {
355 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
356 HLTInfo("Setting geometry to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
357 fgeometryFileName=((TObjString*)pTokens->At(i))->GetString();
360 else if (argument.CompareTo("-lowflux")==0) {
362 HLTInfo("Low flux reconstruction selected");
365 else if (argument.CompareTo("-highflux")==0) {
367 HLTInfo("High flux reconstruction selected");
370 else if (argument.CompareTo("-cosmics")==0) {
372 HLTInfo("Cosmics reconstruction selected");
375 else if (argument.CompareTo("-magnetic_field_ON")==0) {
377 HLTInfo("Reconstructon with magnetic field");
380 else if (argument.CompareTo("-magnetic_field_OFF")==0) {
382 HLTInfo("Reconstructon without magnetic field");
385 else if (argument.CompareTo("-slowTracking")==0) {
386 fSlowTracking = kTRUE;
387 HLTInfo("Using slow tracking");
390 else if (argument.CompareTo("-outputV1Tracks")==0) {
391 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
392 TString toCompareTo=((TObjString*)pTokens->At(i))->GetString();
393 if (toCompareTo.CompareTo("yes")==0){
394 HLTInfo("Setting OutputV1Tracks to: %s", toCompareTo.Data());
395 fOutputV1Tracks=kTRUE;
397 else if (toCompareTo.CompareTo("no")==0){
398 HLTInfo("Setting OutputV1Tracks to: %s", toCompareTo.Data());
399 fOutputV1Tracks=kFALSE;
402 HLTError("unknown argument for OutputV1Tracks: %s", toCompareTo.Data());
408 else if (argument.CompareTo("-PIDmethod")==0) {
409 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
410 TString toCompareTo=((TObjString*)pTokens->At(i))->GetString();
411 if (toCompareTo.CompareTo("LH")==0){
412 HLTInfo("Setting PID method to: %s", toCompareTo.Data());
415 else if (toCompareTo.CompareTo("NN")==0){
416 HLTInfo("Setting PID method to: %s", toCompareTo.Data());
419 else if (toCompareTo.CompareTo("TM")==0){
420 HLTInfo("Setting PID method to: %s", toCompareTo.Data());
424 HLTError("unknown argument for PID method: %s", toCompareTo.Data());
432 HLTError("unknown argument: %s", argument.Data());
440 HLTError("missing parameter for argument %s", argument.Data());
449 int AliHLTTRDTrackerV1Component::SetParams()
452 if(!AliCDBManager::Instance()->IsDefaultStorageSet()){
453 HLTError("DefaultStorage is not set in CDBManager");
456 if(AliCDBManager::Instance()->GetRun()<0){
457 HLTError("Run Number is not set in CDBManager");
460 HLTInfo("CDB default storage: %s; RunNo: %i", (AliCDBManager::Instance()->GetDefaultStorage()->GetBaseFolder()).Data(), AliCDBManager::Instance()->GetRun());
462 if(!AliGeomManager::GetGeometry()){
463 if(fgeometryFileName.CompareTo("")==0 || !TFile::Open(fgeometryFileName.Data())){
464 HLTInfo("Loading standard geometry file");
465 AliGeomManager::LoadGeometry();
467 HLTWarning("Loading NON-standard geometry file");
468 AliGeomManager::LoadGeometry(fgeometryFileName.Data());
470 if(!AliGeomManager::GetGeometry()){
471 HLTError("Could not load geometry");
476 HLTInfo("Geometry Already Loaded!");
481 HLTError("Sorry. Tracker needs number of time bins. At the moment you have to provide it with -NTimeBins <value>. The simulation always had 24 and the real data 30. Take your pick. Make sure the information is correct. Ask offline to implement how to propagate this information into clusters/cluster tree.");
484 if (fNtimeBins < 24 || fNtimeBins > 30)
486 HLTWarning("The number of time bins seems to be strange = %d. But okay. Let's try it...", fNtimeBins);
488 if (fNtimeBins != 24)
490 HLTWarning("All PID methods eagerly await 24 time bins, so PID will NOT work!", fNtimeBins);
492 HLTDebug("Setting number of time bins of the tracker to: %i", fNtimeBins);
493 AliTRDtrackerV1::SetNTimeBins(fNtimeBins);
495 TString recoOptions="sa,sl_tr_0,!cw";
498 recoOptions += ",hlt";
501 case 0: recoOptions += ",!nn"; break;
502 case 1: recoOptions += ",nn"; break;
503 case 2: recoOptions += ",!nn"; break;
506 if (fRecoParamType == 0)
508 HLTDebug("Low flux params init.");
509 fRecoParam = AliTRDrecoParam::GetLowFluxParam();
512 if (fRecoParamType == 1)
514 HLTDebug("High flux params init.");
515 fRecoParam = AliTRDrecoParam::GetHighFluxParam();
518 if (fRecoParamType == 2)
520 HLTDebug("Cosmic Test params init.");
521 fRecoParam = AliTRDrecoParam::GetCosmicTestParam();
526 HLTError("No reco params initialized. Sniffing big trouble!");
530 fReconstructor->SetRecoParam(fRecoParam);
532 HLTDebug("Reconstructor options are: %s",recoOptions.Data());
533 fReconstructor->SetOption(recoOptions.Data());
535 if (fMagneticField >= 0)
537 HLTWarning("Setting magnetic field by hand!");
539 if (!TGeoGlobalMagField::Instance()->IsLocked()) {
541 if (fMagneticField == 0){
542 // magnetic field OFF
543 field = new AliMagF("Maps","Maps",2,0.,0., 10.,AliMagF::k5kGUniform);
544 TGeoGlobalMagField::Instance()->SetField(field);
545 HLTDebug("Magnetic field is OFF.");
548 field = new AliMagF("Maps","Maps",2,1.,1., 10.,AliMagF::k5kG);
549 TGeoGlobalMagField::Instance()->SetField(field);
550 HLTDebug("Magnetic field is ON.");
551 if( fMagneticField < 0 )
552 iResult=ReconfigureField();
555 HLTError("Magnetic field is already set and locked, cannot redefine it." );
560 int AliHLTTRDTrackerV1Component::ReconfigureField()
563 if(fieldStrength<-100){
564 const char* pathBField=kAliHLTCDBSolenoidBz;
567 HLTInfo("reconfigure B-Field from entry %s", pathBField);
568 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(pathBField/*,GetRunNo()*/);
570 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
572 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
573 TObjArray* pTokens=pString->GetString().Tokenize(" ");
577 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
578 argument=((TObjString*)pTokens->At(i))->GetString();
579 if (argument.IsNull()) continue;
581 if (argument.CompareTo("-solenoidBz")==0) {
582 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
583 HLTDebug("Magnetic field in CDB: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
584 fieldStrength=((TObjString*)pTokens->At(i))->GetString().Atof();
587 HLTError("unknown argument %s", argument.Data());
595 HLTError("configuration object \"%s\" has wrong type, required TObjString", pathBField);
598 HLTError("cannot fetch object \"%s\" from CDB", pathBField);
603 if(fieldStrength>=-100){
604 AliMagF* field = (AliMagF *) TGeoGlobalMagField::Instance()->GetField();
605 HLTDebug("Magnetic field before change: %f KGauss", field->SolenoidField());
606 field->SetFactorSol(1);
607 Double_t initialFieldStrengh=field->SolenoidField();
608 field->SetFactorSol(fieldStrength/initialFieldStrengh);
609 HLTDebug("Magnetic field was changed to %f KGauss.", field->SolenoidField());
614 int AliHLTTRDTrackerV1Component::Reconfigure(const char* cdbEntry, const char* chainId)
616 // see header file for class documentation
619 const char* path="HLT/ConfigTRD/TrackerV1Component";
620 const char* defaultNotify="";
623 defaultNotify=" (default)";
626 HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
627 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
629 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
631 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
632 iResult=Configure(pString->GetString().Data());
634 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
637 HLTError("cannot fetch object \"%s\" from CDB", path);
641 const char* pathBField=kAliHLTCDBSolenoidBz;
644 HLTInfo("reconfigure B-Field from entry %s, chain id %s", pathBField,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
645 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(pathBField/*,GetRunNo()*/);
647 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
649 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
650 iResult=Configure(pString->GetString().Data());
652 HLTError("configuration object \"%s\" has wrong type, required TObjString", pathBField);
655 HLTError("cannot fetch object \"%s\" from CDB", pathBField);
663 int AliHLTTRDTrackerV1Component::ReadPreprocessorValues(const char* modules)
665 // see header file for class documentation
668 TString str(modules);
669 if(str.Contains("HLT") || str.Contains("TRD") || str.Contains("GRP")){
671 const char* pathBField=kAliHLTCDBSolenoidBz;
674 HLTInfo("reconfigure B-Field from entry %s, modules %s", pathBField,(modules!=NULL && modules[0]!=0)?modules:"<none>");
675 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(pathBField/*,GetRunNo()*/);
677 // AliCDBPath path(pathBField);
679 // AliCDBStorage *stor = AliCDBManager::Instance()->GetDefaultStorage();
680 // Int_t version = stor->GetLatestVersion(pathBField, GetRunNo());
681 // Int_t subVersion = stor->GetLatestSubVersion(pathBField, GetRunNo(), version);
682 // AliCDBEntry *pEntry = stor->Get(path,GetRunNo(), version, subVersion);
684 // HLTInfo("RunNo %d, Version %d, subversion %d", GetRunNo(), version, subVersion);
687 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
689 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
690 iResult=Configure(pString->GetString().Data());
692 HLTError("configuration object \"%s\" has wrong type, required TObjString", pathBField);
695 HLTError("cannot fetch object \"%s\" from CDB", pathBField);