1 // $Id: AliHLTTRDTrackerV1Component.cxx 23618 2008-01-29 13:07:38Z hristov $
3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
8 * for The ALICE Off-line 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
20 @author Timm Steinbeck, Matthias Richter
22 @brief A TRDTrackerV1 processing component for the HLT. */
28 #include "AliHLTTRDTrackerV1Component.h"
29 #include "AliHLTTRDDefinitions.h"
30 #include "AliHLTTRDCluster.h"
31 #include "AliHLTTRDTrack.h"
36 #include "AliGeomManager.h"
37 #include "AliCDBManager.h"
38 #include "AliESDEvent.h"
40 #include "AliESDfriend.h"
42 #include "AliTRDcalibDB.h"
43 #include "AliTRDReconstructor.h"
44 #include "AliTRDtrackerV1.h"
45 #include "AliTRDrecoParam.h"
51 #ifdef HAVE_VALGRIND_CALLGRIND_H
52 #include <valgrind/callgrind.h>
54 #define CALLGRIND_START_INSTRUMENTATION() do { } while (0)
55 #define CALLGRIND_STOP_INSTRUMENTATION() do { } while (0)
58 ClassImp(AliHLTTRDTrackerV1Component);
60 AliHLTTRDTrackerV1Component::AliHLTTRDTrackerV1Component():
62 fOutputPercentage(100), // By default we copy to the output exactly what we got as input
63 fStrorageDBpath("local://$ALICE_ROOT/OCDB"),
65 fGeometryFileName(""),
66 fUseHLTClusters(kFALSE),
67 fUseHLTTracks(kFALSE),
72 // Default constructor
74 fGeometryFileName = getenv("ALICE_ROOT");
75 fGeometryFileName += "/HLT/TRD/geometry.root";
78 AliHLTTRDTrackerV1Component::~AliHLTTRDTrackerV1Component()
83 const char* AliHLTTRDTrackerV1Component::GetComponentID()
85 // Return the component ID const char *
86 return "TRDTrackerV1"; // The ID of this component
89 void AliHLTTRDTrackerV1Component::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
91 // Get the list of input data
92 list.clear(); // We do not have any requirements for our input data type(s).
93 list.push_back( AliHLTTRDDefinitions::fgkClusterDataType );
96 AliHLTComponent_DataType AliHLTTRDTrackerV1Component::GetOutputDataType()
98 // Get the output data type
99 return AliHLTTRDDefinitions::fgkClusterDataType;
102 void AliHLTTRDTrackerV1Component::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
104 // Get the output data size
106 inputMultiplier = ((double)fOutputPercentage)/100.0;
109 // Spawn function, return new instance of this class
110 AliHLTComponent* AliHLTTRDTrackerV1Component::Spawn()
112 return new AliHLTTRDTrackerV1Component;
116 * Convert AliTRDtrackV1 to AliHLTTRDTrack
117 * Add HLTTrack to the output, defined by pointer
118 * Fill block desctiptors
119 * Return size of the added to ouput objects
121 //============================================================================
122 AliHLTUInt32_t AliHLTTRDTrackerV1Component::AddToOutput(TClonesArray* inTrackArray, AliHLTUInt8_t* output)
124 cout << "\nWriting tracks to the Memory\n ============= \n";
125 AliTRDtrackV1* track = 0;
126 AliHLTUInt32_t addedSize = 0;
127 AliHLTUInt8_t *iterPtr = output;
128 AliHLTTRDTrack * outPtr = (AliHLTTRDTrack*)iterPtr;
131 Int_t nbTracks = inTrackArray->GetEntries();
132 for (Int_t iTrack = 0; iTrack<nbTracks; iTrack++){
133 AliHLTUInt32_t trackSize=0;
135 track = dynamic_cast<AliTRDtrackV1*>(inTrackArray->At(iTrack));
138 AliHLTTRDTrack *hltTrack = new (outPtr) AliHLTTRDTrack(track);
139 trackSize = hltTrack->GetSize();
140 addedSize += trackSize;
141 HLTDebug("addedSize %i, trackSize %i", addedSize, trackSize);
143 iterPtr += trackSize;
144 outPtr = (AliHLTTRDTrack*)iterPtr;
151 int AliHLTTRDTrackerV1Component::DoInit( int argc, const char** argv )
153 // perform initialization. We check whether our relative output size is specified in the arguments.
154 fOutputPercentage = 100;
159 Int_t iRecoParamType = -1; // default will be the low flux
160 Int_t iNtimeBins = -1; // number of time bins for the tracker to use
161 Int_t iMagneticField = -1; // magnetic field: 0==OFF and 1==ON
162 Bool_t bHLTMode = kTRUE, bWriteClusters = kFALSE;
166 HLTDebug("argv[%d] == %s", i, argv[i] );
167 if ( !strcmp( argv[i], "output_percentage" ) )
171 HLTError("Missing output_percentage parameter");
174 HLTDebug("argv[%d+1] == %s", i, argv[i+1] );
175 fOutputPercentage = strtoul( argv[i+1], &cpErr, 0 );
178 HLTError("Cannot convert output_percentage parameter '%s'", argv[i+1] );
181 HLTInfo("Output percentage set to %lu %%", fOutputPercentage );
186 else if ( !strcmp( argv[i], "-NTimeBins" ) )
190 HLTError("Missing -NTimeBins parameter");
193 HLTDebug("Arguments", "argv[%d+1] == %s", i, argv[i+1] );
194 iNtimeBins = strtoul( argv[i+1], &cpErr, 0 );
197 HLTError("Wrong Argument. Cannot convert -NTimeBins parameter '%s'", argv[i+1] );
204 else if ( strcmp( argv[i], "-cdb" ) == 0)
208 HLTError( "Missing -cdb argument");
211 fStrorageDBpath = argv[i+1];
212 HLTInfo("DB storage is %s", fStrorageDBpath.c_str() );
217 else if ( strcmp( argv[i], "-geometry" ) == 0)
221 HLTError("Missing -geometry argument");
224 fGeometryFileName = argv[i+1];
225 HLTInfo("GeomFile storage is %s",
226 fGeometryFileName.c_str() );
231 // the flux parametrizations
232 else if ( strcmp( argv[i], "-lowflux" ) == 0)
235 HLTDebug("Low flux reco selected.");
240 else if ( strcmp( argv[i], "-highflux" ) == 0)
243 HLTDebug("Low flux reco selected.");
247 else if ( strcmp( argv[i], "-cosmics" ) == 0)
250 HLTDebug("Cosmic test reco selected.");
254 else if ( strcmp( argv[i], "-magnetic_field_ON" ) == 0)
259 else if ( strcmp( argv[i], "-magnetic_field_OFF" ) == 0)
264 else if ( strcmp( argv[i], "-writeClusters" ) == 0)
266 bWriteClusters = kTRUE;
267 HLTDebug("input clusters are expected to be in a TTree.");
270 else if ( strcmp( argv[i], "-offlineMode" ) == 0)
273 HLTDebug("Using standard offline tracking.");
276 else if ( strcmp( argv[i], "-useHLTClusters" ) == 0)
278 fUseHLTClusters = kTRUE;
280 HLTInfo("expecting AliHLTCluster as input");
282 else if ( strcmp( argv[i], "-useHLTTracks" ) == 0)
284 fUseHLTTracks = kTRUE;
286 HLTInfo("Using AliHLTTrack to pass data further in the chain");
289 HLTError("Unknown option '%s'", argv[i] );
295 // THE "REAL" INIT COMES HERE
296 // offline condition data base
297 fCDB = AliCDBManager::Instance();
300 HLTError("Could not get CDB instance", "fCDB 0x%x", fCDB);
305 fCDB->SetRun(0); // THIS HAS TO BE RETRIEVED !!!
306 fCDB->SetDefaultStorage(fStrorageDBpath.c_str());
307 HLTDebug("CDB instance", "fCDB 0x%x", fCDB);
310 // check if the N of time bins make sense
313 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.");
317 if (iNtimeBins < 24 || iNtimeBins > 30)
319 HLTWarning("The number of time bins seems to be strange = %d. But okay. Let's try it...", iNtimeBins);
322 HLTDebug("The number of time bins = %d.", iNtimeBins);
323 AliTRDtrackerV1::SetNTimeBins(iNtimeBins);
325 // !!!! THIS IS IMPORTANT
326 // init alifield map - temporarly via parameter - should come from a DB or DCS ?
328 if (iMagneticField < 0)
331 HLTWarning("No magnetic field switch stated. Use -magnetic_field_ON or -magnetic_field_OFF flag. Defaulting to OFF = NO MAGNETIC FIELD");
334 if (!TGeoGlobalMagField::Instance()->IsLocked()) {
335 if (iMagneticField == 0)
337 // magnetic field OFF
338 AliMagF* field = new AliMagF("Maps","Maps",2,0.,0., 10.,AliMagF::k5kGUniform);
339 TGeoGlobalMagField::Instance()->SetField(field);
340 HLTDebug("Magnetic field is OFF.");
343 if (iMagneticField == 1)
346 AliMagF* field = new AliMagF("Maps","Maps",2,1.,1., 10.,AliMagF::k5kG);
347 TGeoGlobalMagField::Instance()->SetField(field);
348 HLTDebug("Magnetic field is ON.");
352 HLTError("Magnetic field is already set and locked, cannot redefine it." );
355 // reconstruction parameters
356 if (iRecoParamType < 0 || iRecoParamType > 2)
358 HLTWarning("No reco param selected. Use -lowflux -highflux -cosmics flags. Defaulting to low flux.");
362 if (iRecoParamType == 0)
364 fRecoParam = AliTRDrecoParam::GetLowFluxParam();
365 HLTDebug("Low flux params init.");
368 if (iRecoParamType == 1)
370 fRecoParam = AliTRDrecoParam::GetHighFluxParam();
371 HLTDebug("High flux params init.");
374 if (iRecoParamType == 2)
376 fRecoParam = AliTRDrecoParam::GetCosmicTestParam();
377 HLTDebug("Cosmic Test params init.");
382 HLTError("No reco params initialized. Sniffing big trouble!");
386 fReconstructor = new AliTRDReconstructor();
387 // fRecoParam->SetChi2Y(.1);
388 // fRecoParam->SetChi2Z(5.);
389 fReconstructor->SetRecoParam(fRecoParam);
390 // write clusters [cw] = true
391 // track seeding (stand alone tracking) [sa] = true
392 // PID method in reconstruction (NN) [nn] = true
393 // write online tracklets [tw] = false
394 // drift gas [ar] = false
395 // sl_tr_0 = StreamLevel_task_Level
396 // fReconstructor->SetOption("sa,!cw,hlt,sl_tr_0");
397 TString recoOptions="sa,sl_tr_0";
401 recoOptions += ",cw";
405 recoOptions += ",!cw";
408 recoOptions += ",hlt";
410 fReconstructor->SetOption(recoOptions.Data());
411 HLTDebug("Reconstructor options are: %s",recoOptions.Data());
413 if((AliGeomManager::GetGeometry()) == NULL){
415 if ( TFile::Open(fGeometryFileName.c_str())) {
416 AliGeomManager::LoadGeometry(fGeometryFileName.c_str());
419 HLTError("Cannot load geometry from file %s",fGeometryFileName.c_str());
424 HLTInfo("Geometry Already Loaded");
426 // create the tracker
427 fTracker = new AliTRDtrackerV1();
428 fTracker->SetReconstructor(fReconstructor);
429 HLTDebug("TRDTracker at 0x%x", fTracker);
433 HLTError("Unable to create the tracker!");
440 int AliHLTTRDTrackerV1Component::DoDeinit()
442 // Deinitialization of the component
444 fTracker->SetClustersOwner(kFALSE);
448 // We need to set clusters in Reconstructor to null to prevent from
449 // double deleting, since we delete TClonesArray by ourself in DoEvent.
450 fReconstructor->SetClusters(0x0);
451 delete fReconstructor;
452 fReconstructor = 0x0;
454 AliTRDcalibDB::Terminate();
459 int AliHLTTRDTrackerV1Component::DoEvent( const AliHLTComponentEventData& evtData,
460 const AliHLTComponentBlockData* blocks,
461 AliHLTComponent_TriggerData& /*trigData*/,
462 AliHLTUInt8_t* outputPtr,
463 AliHLTUInt32_t& size,
464 vector<AliHLTComponent_BlockData>& outputBlocks )
467 Bool_t bWriteClusters = fReconstructor->IsWritingClusters();
469 HLTDebug("NofBlocks %lu", evtData.fBlockCnt );
471 AliHLTUInt32_t totalSize = 0, offset = 0;
472 AliHLTUInt32_t dBlockSpecification = 0;
474 vector<AliHLTComponent_DataType> expectedDataTypes;
475 GetInputDataTypes(expectedDataTypes);
476 if (evtData.fEventID == 1)
477 CALLGRIND_START_INSTRUMENTATION();
478 for ( unsigned long iBlock = 0; iBlock < evtData.fBlockCnt; iBlock++ )
480 const AliHLTComponentBlockData &block = blocks[iBlock];
482 AliHLTComponentDataType inputDataType = block.fDataType;
483 Bool_t correctDataType = kFALSE;
485 for(UInt_t i = 0; i < expectedDataTypes.size(); i++)
486 if( expectedDataTypes.at(i) == inputDataType)
487 correctDataType = kTRUE;
488 if (!correctDataType)
490 HLTDebug( "Block # %i/%i; Event 0x%08LX (%Lu) Wrong received datatype: %s - Skipping",
491 iBlock, evtData.fBlockCnt-1,
492 evtData.fEventID, evtData.fEventID,
493 DataType2Text(inputDataType).c_str());
497 HLTDebug("We get the right data type: Block # %i/%i; Event 0x%08LX (%Lu) Received datatype: %s",
498 iBlock, evtData.fBlockCnt-1,
499 evtData.fEventID, evtData.fEventID,
500 DataType2Text(inputDataType).c_str());
503 TTree *clusterTree = 0x0;
504 TClonesArray* clusterArray = 0x0;
505 ReadAndLoadClusters(clusterTree, clusterArray, &block);
507 // maybe it is not so smart to create it each event? clear is enough ?
508 AliESDEvent *esd = new AliESDEvent();
509 esd->CreateStdContent();
510 fTracker->Clusters2Tracks(esd);
512 //here transport the esd tracks further
513 Int_t nTracks = esd->GetNumberOfTracks();
514 Int_t nTRDTracks = esd->GetNumberOfTrdTracks();
515 HLTInfo("Number of tracks == %d == Number of TRD tracks %d", nTracks, nTRDTracks);
517 TClonesArray* trdTracks = fTracker->GetListOfTracks();
520 totalSize += TransportTracks(trdTracks, outputPtr, outputBlocks, offset, dBlockSpecification);
522 HLTDebug("Bad array trdTracks = 0x%x", trdTracks);
523 HLTDebug("totalSize: %i", totalSize);
525 // if ( totalSize > allocSize )
527 // HLTError("Too much data; Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
528 // totalSize, size );
532 //here we are deleting clusters (but not the TClonesArray itself)
533 fTracker->UnloadClusters();
535 AliTRDReconstructor::SetClusters(0x0);
540 //clusterArray->Delete();
546 HLTDebug("Event is done. size written to the output is %i", size);
547 // CALLGRIND_STOP_INSTRUMENTATION();
553 * ReadClusters from the component input and load them to the tracker
555 //============================================================================
556 void AliHLTTRDTrackerV1Component::ReadAndLoadClusters(TTree *inClusterTree,
557 TClonesArray *inClusterArray, const AliHLTComponentBlockData *inBlock)
559 Bool_t bWriteClusters = fReconstructor->IsWritingClusters();
560 const AliHLTComponentBlockData &block = *inBlock;
563 TObject *tobjin = 0x0;
564 int ibForce = 0; // almost obsolet
567 tobjin = (TObject *)GetFirstInputObject( AliHLTTRDDefinitions::fgkClusterDataType, "TTree", ibForce);
568 HLTDebug("1stBLOCK; Pointer = 0x%x", tobjin);
569 inClusterTree = (TTree*)tobjin;
572 HLTDebug("CLUSTERS; Pointer to TTree = 0x%x Name = %s", inClusterTree, inClusterTree->GetName());
573 HLTDebug("TTree of clusters: nbEntries = %i", inClusterTree->GetEntriesFast());
574 fTracker->LoadClusters(inClusterTree);
578 HLTError("First Input Block is not a TTree 0x%x", tobjin);
581 else if (fUseHLTClusters)
583 inClusterArray = new TClonesArray("AliTRDcluster"); // would be nice to allocate memory for all clusters here.
584 ReadClusters(inClusterArray, block.fPtr, block.fSize);
585 HLTDebug("TClonesArray of clusters: nbEntries = %i", inClusterArray->GetEntriesFast());
586 fTracker->LoadClusters(inClusterArray);
590 tobjin = (TObject *)GetFirstInputObject( AliHLTTRDDefinitions::fgkClusterDataType, "TClonesArray", ibForce);
591 HLTDebug("1stBLOCK; Pointer = 0x%x", tobjin);
592 inClusterArray = (TClonesArray*)tobjin;
595 HLTDebug("CLUSTERS; Pointer to TClonesArray = 0x%x Name = %s", inClusterArray, inClusterArray->GetName());
596 HLTDebug("TClonesArray of clusters: nbEntries = %i", inClusterArray->GetEntriesFast());
597 fTracker->LoadClusters(inClusterArray);
601 HLTError("First Input Block not a TClonesArray 0x%x", tobjin);
607 * Read cluster to the TClonesArray from the memory
609 //============================================================================
610 Int_t AliHLTTRDTrackerV1Component::ReadClusters(TClonesArray *outArray, void* inputPtr, AliHLTUInt32_t size)
612 //HLTDebug("\nReading clusters from the Memory\n ============= \n");
613 AliHLTTRDCluster * curCluster;
614 UInt_t clusterSize = sizeof(AliHLTTRDCluster), curSize = 0;
617 curCluster = (AliHLTTRDCluster*) inputPtr;
618 while (curSize + clusterSize <= size)
620 // HLTDebug(" fX = %f; fY = %f; fZ = %f", curCluster->fX, curCluster->fY, curCluster->fZ);
622 AliTRDcluster* curTRDCluster = new((*outArray)[i]) AliTRDcluster();
623 curCluster->ExportTRDCluster(curTRDCluster);
624 // HLTDebug(" fX = %f; fY = %f; fZ = %f", curTRDCluster->GetX(), curTRDCluster->GetY(), curTRDCluster->GetZ());
625 curSize += clusterSize;
628 //cout << " current readed size is " << curSize << "/" << size << endl;
634 * Transport tracks to the next component
635 * Return Numbers of bytes written to the output
637 //============================================================================
638 AliHLTUInt32_t AliHLTTRDTrackerV1Component::TransportTracks(TClonesArray *inTracksArray, AliHLTUInt8_t* output,
639 vector<AliHLTComponent_BlockData>& outputBlocks, AliHLTUInt32_t inOffset, AliHLTUInt32_t inSpec)
641 Int_t nbTracks=inTracksArray->GetEntriesFast();
644 HLTDebug("We have an output array: pointer to inTracksArray = 0x%x, nbEntries = %i", inTracksArray, nbTracks);
646 // Using low-level interface
647 // with interface classes
648 AliHLTUInt32_t addedSize = AddToOutput(inTracksArray, output);
651 AliHLTComponentBlockData bd;
655 bd.fOffset = inOffset;
656 bd.fSize = addedSize;
657 bd.fSpecification = inSpec;
658 bd.fDataType = AliHLTTRDDefinitions::fgkTRDSATracksDataType;
659 outputBlocks.push_back( bd );
660 HLTDebug("BD fPtr 0x%x, fOffset %i, fSize %i, fSpec 0x%x", bd.fPtr, bd.fOffset, bd.fSize, bd.fSpecification);
665 inTracksArray->BypassStreamer(kFALSE);
666 PushBack(inTracksArray, AliHLTTRDDefinitions::fgkTRDSATracksDataType);