Coverity fix
[u/mrichter/AliRoot.git] / TRD / TRDbase / AliTRDReconstructor.cxx
CommitLineData
121a60bd 1/**************************************************************************
91834b8d 2* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3* *
4* Author: The ALICE Off-line Project. *
5* Contributors are mentioned in the code where appropriate. *
6* *
7* Permission to use, copy, modify and distribute this software and its *
8* documentation strictly for non-commercial purposes is hereby granted *
9* without fee, provided that the above copyright notice appears in all *
10* copies and that both the copyright notice and this permission notice *
11* appear in the supporting documentation. The authors make no claims *
12* about the suitability of this software for any purpose. It is *
13* provided "as is" without express or implied warranty. *
14**************************************************************************/
121a60bd 15
16/* $Id$ */
17
18///////////////////////////////////////////////////////////////////////////////
19// //
4fad09c9 20// Class for TRD reconstruction //
954135b1 21// //
22// For the special options which can be used during reconstruction and their //
23// default values pls. see function SetOption(). //
121a60bd 24// //
25///////////////////////////////////////////////////////////////////////////////
26
3a039a31 27#include <TObjString.h>
28#include <TObjArray.h>
d39401ad 29#include <TTreeStream.h>
30#include <TDirectory.h>
cb8b99ee 31#include <TRef.h>
121a60bd 32
3bc9d03e 33#include "AliRawReader.h"
cb8b99ee 34#include "AliLog.h"
3bc9d03e 35
36#include "AliTRDReconstructor.h"
3fe61b77 37#include "AliTRDclusterizer.h"
a8fbb161 38#include "AliTRDrawData.h"
8df1f8f5 39#include "AliTRDrawStream.h"
a8fbb161 40#include "AliTRDdigitsManager.h"
e4f2f73d 41#include "AliTRDtrackerV1.h"
cb8b99ee 42#include "AliESDEvent.h"
43#include "AliESDTrdTrack.h"
44#include "AliESDTrdTracklet.h"
c4daee41 45#include "AliESDTrdTrigger.h"
cb8b99ee 46#include "AliTRDtrackletWord.h"
03e2f460 47#include "AliTRDtrackletMCM.h"
ff4c44cb 48#include "AliTRDonlineTrackMatching.h"
d45335d1 49#include "AliTRDcalibDB.h"
29f95561 50
97b6dfac 51#define SETFLG(n,f) ((n) |= f)
52#define CLRFLG(n,f) ((n) &= ~f)
53
121a60bd 54ClassImp(AliTRDReconstructor)
55
2c7f43b6 56Int_t AliTRDReconstructor::fgStreamLevel = 1; // stream (debug) level
57
58
c4daee41 59AliESDTrdTrigger AliTRDReconstructor::fgTriggerFlags;
ff4c44cb 60AliTRDonlineTrackMatching AliTRDReconstructor::fgOnlineTrackMatcher;
828c6f80 61TClonesArray *AliTRDReconstructor::fgClusters = NULL;
62TClonesArray *AliTRDReconstructor::fgTracklets = NULL;
cb8b99ee 63TClonesArray *AliTRDReconstructor::fgTracks = NULL;
828c6f80 64Char_t const * AliTRDReconstructor::fgSteerNames[kNsteer] = {
b72f4eaf 65 "DigitsConversion "
b72f4eaf 66 ,"Write Clusters "
67 ,"Write Online Tracklets "
b72f4eaf 68 ,"Stand Alone Tracking "
8fc736d7 69 ,"HLT Mode "
70 ,"Process Online Trklts "
71 ,"Debug Streaming "
72 ,"Cl. Radial Correction "
b72f4eaf 73};
828c6f80 74Char_t const * AliTRDReconstructor::fgSteerFlags[kNsteer] = {
b72f4eaf 75 "dc"// digits conversion [false]
b72f4eaf 76 ,"cw"// write clusters [true]
77 ,"tw"// write online tracklets [false]
b72f4eaf 78 ,"sa"// track seeding (stand alone tracking) [true]
b72f4eaf 79 ,"hlt"// HLT reconstruction [false]
a5b99acd 80 ,"tp"// also use online tracklets for reconstruction [false]
a2fbb6ec 81 ,"deb"// Write debug stream [false]
b30d8c09 82 ,"cc" // Cluster radial correction during reconstruction [false]
b72f4eaf 83};
828c6f80 84Char_t const * AliTRDReconstructor::fgTaskNames[AliTRDrecoParam::kTRDreconstructionTasks] = {
a2fbb6ec 85 "Clusterizer"
b72f4eaf 86 ,"Tracker"
87 ,"PID"
88};
828c6f80 89Char_t const * AliTRDReconstructor::fgTaskFlags[AliTRDrecoParam::kTRDreconstructionTasks] = {
a2fbb6ec 90 "cl"
b72f4eaf 91 ,"tr"
92 ,"pd"
93};
c6f7c6cb 94Int_t AliTRDReconstructor::fgNTimeBins = -1;
952051c5 95const Float_t AliTRDReconstructor::fgkMinClustersInTrack = 0.5; //
96const Float_t AliTRDReconstructor::fgkLabelFraction = 0.8; //
97const Double_t AliTRDReconstructor::fgkMaxChi2 = 12.0; //
98const Double_t AliTRDReconstructor::fgkMaxSnp = 0.95; // Maximum local sine of the azimuthal angle
99const Double_t AliTRDReconstructor::fgkMaxStep = 2.0; // Maximal step size in propagation
100const Double_t AliTRDReconstructor::fgkEpsilon = 1.e-5; // Precision of radial coordinate
b72f4eaf 101
eb38ed55 102//_____________________________________________________________________________
3a039a31 103AliTRDReconstructor::AliTRDReconstructor()
104 :AliReconstructor()
97b6dfac 105 ,fSteerParam(0)
5127281e 106 ,fClusterizer(NULL)
3a039a31 107{
97b6dfac 108 // setting default "ON" steering parameters
89971837 109 // owner of debug streamers
110 SETFLG(fSteerParam, kOwner);
97b6dfac 111 // write clusters [cw]
112 SETFLG(fSteerParam, kWriteClusters);
113 // track seeding (stand alone tracking) [sa]
9dcc64cc 114 //SETFLG(fSteerParam, kSeeding);
8fc736d7 115 // Cluster radial correction during reconstruction [cc]
b30d8c09 116 //SETFLG(fSteerParam, kClRadialCorr);
a2fbb6ec 117 memset(fDebugStream, 0, sizeof(TTreeSRedirector *) * AliTRDrecoParam::kTRDreconstructionTasks);
eb38ed55 118}
119
3a039a31 120//_____________________________________________________________________________
48f8adf3 121AliTRDReconstructor::~AliTRDReconstructor()
122{
d39401ad 123 //
124 // Destructor
125 //
d45335d1 126 AliTRDcalibDB* calib = AliTRDcalibDB::Instance();
127 if (calib) calib->Invalidate();
128 //
d2eba04b 129 if(fClusterizer){
130 delete fClusterizer;
131 fClusterizer = NULL;
132 }
48f8adf3 133 if(fgClusters) {
6d54fcbf 134 fgClusters->Delete();
135 delete fgClusters;
136 fgClusters = NULL;
48f8adf3 137 }
a5b99acd 138 if(fgTracklets) {
6d54fcbf 139 fgTracklets->Delete();
140 delete fgTracklets;
141 fgTracklets = NULL;
a5b99acd 142 }
cb8b99ee 143 if(fgTracks) {
144 fgTracks->Delete();
145 delete fgTracks;
146 fgTracks = NULL;
147 }
2c7f43b6 148 if((fSteerParam&kOwner)|| AliTRDReconstructor::GetStreamLevel()>0){
a2fbb6ec 149 for(Int_t itask = 0; itask < AliTRDrecoParam::kTRDreconstructionTasks; itask++)
29f95561 150 if(fDebugStream[itask]) delete fDebugStream[itask];
151 }
18c05eb3 152}
48f8adf3 153
91834b8d 154
48f8adf3 155//_____________________________________________________________________________
91834b8d 156void AliTRDReconstructor::Init(){
18c05eb3 157 //
158 // Init Options
159 //
18c05eb3 160 SetOption(GetOption());
a2fbb6ec 161 Options(fSteerParam);
162
5127281e 163 if(!fClusterizer){
164 fClusterizer = new AliTRDclusterizer(fgTaskNames[AliTRDrecoParam::kClusterizer], fgTaskNames[AliTRDrecoParam::kClusterizer]);
165 fClusterizer->SetReconstructor(this);
d2eba04b 166 AliInfo(Form("Build TRD clusterizer[%p]", (void*)fClusterizer));
5127281e 167 }
168
a2fbb6ec 169 // Make Debug Streams when Debug Streaming
170 if(IsDebugStreaming()){
171 for(Int_t task = 0; task < AliTRDrecoParam::kTRDreconstructionTasks; task++){
172 TDirectory *savedir = gDirectory;
2d2d464a 173 fDebugStream[task] = new TTreeSRedirector(Form("TRD.Debug%s.root", fgTaskNames[task]),"recreate");
a2fbb6ec 174 savedir->cd();
175 SETFLG(fSteerParam, kOwner);
176 }
177 }
acd241e9 178}
0397ce53 179
121a60bd 180//_____________________________________________________________________________
a8fbb161 181void AliTRDReconstructor::ConvertDigits(AliRawReader *rawReader
91834b8d 182 , TTree *digitsTree) const
121a60bd 183{
3bc9d03e 184 //
25ca55ce 185 // Convert raw data digits into digit objects in a root tree
3bc9d03e 186 //
121a60bd 187
d2eba04b 188 AliDebug(1, "Convert raw data digits into digit objects [RawReader -> Digit TTree]");
189 AliDebug(2, Form("clusters[%p] tracklets[%p] tracks[%p]", (void*)fgClusters, (void*)fgTracklets, (void*)fgTracks));
91834b8d 190
3a0f6479 191 rawReader->Reset();
192 rawReader->Select("TRD");
d2eba04b 193 ResetContainers();
194
195 AliTRDrawData rawData;
d2eba04b 196
a8fbb161 197 AliTRDdigitsManager *manager = rawData.Raw2Digits(rawReader);
198 manager->MakeBranch(digitsTree);
199 manager->WriteDigits();
c0ab62ff 200 delete manager;
0ee00e25 201
d2eba04b 202 for (Int_t iSector = 0; iSector < 18; iSector++) fgTriggerFlags.SetFlags(iSector, rawData.GetTriggerFlags(iSector));
121a60bd 203}
204
205//_____________________________________________________________________________
25ca55ce 206void AliTRDReconstructor::Reconstruct(AliRawReader *rawReader
207 , TTree *clusterTree) const
208{
209 //
210 // Reconstruct clusters
211 //
212
d2eba04b 213 AliDebug(1, "Reconstruct TRD clusters from RAW data [RawReader -> Cluster TTree]");
214 AliDebug(2, Form("clusters[%p] tracklets[%p] tracks[%p]", (void*)fgClusters, (void*)fgTracklets, (void*)fgTracks));
5127281e 215 if(!fClusterizer){
216 AliFatal("Clusterizer not available!");
217 return;
218 }
d2eba04b 219 rawReader->Reset();
220 rawReader->Select("TRD");
221 ResetContainers();
5127281e 222 fClusterizer->OpenOutput(clusterTree);
5127281e 223 fClusterizer->SetUseLabels(kFALSE);
07897df4 224 fClusterizer->SetStoreRawSignals(kTRUE);
d2eba04b 225 fClusterizer->ResetRecPoints();
5127281e 226 fClusterizer->Raw2ClustersChamber(rawReader);
3e1f6592 227 fgNTimeBins = fClusterizer->GetNTimeBins();
d2eba04b 228 for (Int_t iSector = 0; iSector < 18; iSector++) fgTriggerFlags.SetFlags(iSector, fClusterizer->GetTriggerFlags(iSector));
25ca55ce 229}
230
231//_____________________________________________________________________________
a8fbb161 232void AliTRDReconstructor::Reconstruct(TTree *digitsTree
233 , TTree *clusterTree) const
25ca55ce 234{
235 //
236 // Reconstruct clusters
237 //
e4f2f73d 238
d2eba04b 239 AliDebug(1, "Reconstruct TRD clusters from Digits [Digit TTree -> Cluster TTree]");
240 AliDebug(2, Form("Start :: clusters[%p] tracklets[%p] tracks[%p]", (void*)fgClusters, (void*)fgTracklets, (void*)fgTracks));
241 if(!fClusterizer){
242 AliFatal("Clusterizer not available!");
243 return;
cb8b99ee 244 }
245
eea7663c 246 ResetContainers();
079a1515 247 // AliTRDclusterizer clusterizer(fgTaskNames[AliTRDrecoParam::kClusterizer], fgTaskNames[AliTRDrecoParam::kClusterizer]);
248 fClusterizer->SetReconstructor(this);
249 fClusterizer->SetUseLabels(kTRUE);
250 fClusterizer->SetStoreRawSignals(kTRUE);
251 fClusterizer->OpenOutput(clusterTree);
b9f62e5e 252 fClusterizer->ResetRecPoints();
079a1515 253 fClusterizer->ReadDigits(digitsTree);
254 fClusterizer->ReadTracklets();
255 fClusterizer->ReadTracks();
256 fClusterizer->MakeClusters();
257 fgNTimeBins = fClusterizer->GetNTimeBins();
25ca55ce 258}
259
260//_____________________________________________________________________________
44dbae42 261AliTracker *AliTRDReconstructor::CreateTracker() const
121a60bd 262{
3bc9d03e 263 //
264 // Create a TRD tracker
265 //
121a60bd 266
e4f2f73d 267 //return new AliTRDtracker(NULL);
3a039a31 268 AliTRDtrackerV1 *tracker = new AliTRDtrackerV1();
269 tracker->SetReconstructor(this);
d2eba04b 270 AliInfo(Form("Build TRD tracker[%p]", (void*)tracker));
3a039a31 271 return tracker;
25ca55ce 272
25ca55ce 273}
274
275//_____________________________________________________________________________
276void AliTRDReconstructor::FillESD(TTree* /*digitsTree*/
91834b8d 277 , TTree* /*clusterTree*/
cb8b99ee 278 , AliESDEvent* esd) const
25ca55ce 279{
280 //
af26ce80 281 // Fill ESD
25ca55ce 282 //
283
cb8b99ee 284 // ----- filling tracklets -----
d2eba04b 285 AliDebug(1, Form("Loading onl.tracklets(%i) to ESD", fgTracklets ? fgTracklets->GetEntriesFast() : 0));
5fedeee9 286 Int_t trackletIndex[1080] = { 0 };
287 TList trklList;
288 AliTRDrawStream::SortTracklets(fgTracklets, trklList, trackletIndex);
289 TIter trackletIter(&trklList);
03e2f460 290 while (AliTRDtrackletBase* tracklet = (AliTRDtrackletBase*) trackletIter()) {
291 Int_t label = -2; // mark raw tracklets with label -2
d2eba04b 292 if (AliTRDtrackletMCM *trklMCM = dynamic_cast<AliTRDtrackletMCM*> (tracklet)) label = trklMCM->GetLabel();
60557224 293 esd->AddTrdTracklet(tracklet->GetTrackletWord(), tracklet->GetHCId(), label);
cb8b99ee 294 }
295
296 // ----- filling GTU tracks -----
d2eba04b 297 AliDebug(1, Form("Loading gtu.tracks(%i) to ESD", fgTracks ? fgTracks->GetEntriesFast() : 0));
cb8b99ee 298 if (fgTracks) {
299 for (Int_t iTrack = 0; iTrack < fgTracks->GetEntriesFast(); iTrack++) {
300 AliESDTrdTrack *trdTrack = (AliESDTrdTrack*) ((*fgTracks)[iTrack]);
301
cb8b99ee 302 UInt_t stack = trdTrack->GetStack();
303
03e2f460 304 Int_t refIndex[6];
305 AliTRDrawStream::AssignTracklets(trdTrack, trackletIndex, refIndex);
306
307 for (Int_t iLayer = 0; iLayer < 6; ++iLayer) {
d2eba04b 308 Int_t det = trdTrack->GetSector()*30 + stack*6 + iLayer;
309 AliESDTrdTracklet *trkl = refIndex[iLayer] > -1 ? esd->GetTrdTracklet(refIndex[iLayer]) : 0x0;
310 if (trkl) {
311 AliDebug(5, Form("adding tracklet with index %i: 0x%08x",
312 refIndex[iLayer], trkl->GetTrackletWord()));
313 if (trkl->GetDetector() != det)
314 AliError(Form("inconsistent assignment of tracklet 0x%08x in det %i to track in %i",
315 trkl->GetTrackletWord(), trkl->GetDetector(), det));
316 trdTrack->AddTrackletReference(trkl, iLayer);
317 }
cb8b99ee 318 }
319 // only add the track when it's complete (including tracklet references)
320 esd->AddTrdTrack(trdTrack);
321 }
322 }
c4daee41 323 esd->SetTrdTrigger(&fgTriggerFlags);
ff4c44cb 324
325 // ----- matching GTU tracks to global tracks -----
326 AliDebug(1, Form("TRD track matching with %i ESD, %i TRD tracks",
327 esd->GetNumberOfTracks(), esd->GetNumberOfTrdTracks()));
328 fgOnlineTrackMatcher.ProcessEvent(esd);
25ca55ce 329}
3a039a31 330
828c6f80 331//_____________________________________________________________________________
3a039a31 332void AliTRDReconstructor::SetOption(Option_t *opt)
333{
d39401ad 334 //
335 // Read option string into the steer param.
336 //
954135b1 337 // The following string options are available during reconstruction.
338 // In square brackets the default values are given.
339 // "dc" : digits conversion [false]
340 // "cw" : write clusters [true]
341 // "tw" : write online tracklets [false]
342 // "sa" : track seeding (stand alone tracking) [true]
343 // "hlt" : HLT reconstruction [false]
344 // "tp" : also use online tracklets for reconstruction [false]
345 // "deb" : Write debug stream [false]
346 // "cc" : Cluster radial correction during reconstruction [false]
347 //
348 // To check the actual options used during reconstruction include the following line in your rec.C script
349 // AliLog::SetClassDebugLevel("AliTRDReconstructor", 1);
97b6dfac 350
351 AliReconstructor::SetOption(opt);
3a039a31 352
353 TString s(opt);
354 TObjArray *opar = s.Tokenize(",");
355 for(Int_t ipar=0; ipar<opar->GetEntriesFast(); ipar++){
d39401ad 356 Bool_t processed = kFALSE;
3a039a31 357 TString sopt(((TObjString*)(*opar)[ipar])->String());
b72f4eaf 358 for(Int_t iopt=0; iopt<kNsteer; iopt++){
359 if(!sopt.Contains(fgSteerFlags[iopt])) continue;
360 SETFLG(fSteerParam, BIT(iopt));
361 if(sopt.Contains("!")) CLRFLG(fSteerParam, BIT(iopt));
d39401ad 362 processed = kTRUE;
b72f4eaf 363 break;
364 }
d39401ad 365 if(processed) continue;
b72f4eaf 366
367 AliWarning(Form("Unknown option flag %s.", sopt.Data()));
3a039a31 368 }
d2eba04b 369 opar->Delete();
09d5920f 370 delete opar;
3a039a31 371}
372
acd241e9 373//_____________________________________________________________________________
a2fbb6ec 374void AliTRDReconstructor::Options(UInt_t steer)
b72f4eaf 375{
d39401ad 376 //
377 // Print the options
378 //
379
b72f4eaf 380 for(Int_t iopt=0; iopt<kNsteer; iopt++){
9e8fdff4 381 AliDebugGeneral("AliTRDReconstructor", 1, Form(" %s[%s]%s", fgSteerNames[iopt], fgSteerFlags[iopt], steer ?(((steer>>iopt)&1)?" : ON":" : OFF"):""));
b72f4eaf 382 }
b72f4eaf 383}
384
d2eba04b 385
386//_____________________________________________________________________________
387TClonesArray* AliTRDReconstructor::GetClusters()
388{
389// Build/ Retrieve cluster array
390 if(!fgClusters){
391 fgClusters = new TClonesArray("AliTRDcluster", Int_t(GetRecoParam()->GetNClusters()));
392 fgClusters->SetOwner();
393 AliInfoGeneral("AliTRDReconstructor", Form("Allocate cluster array @ %p", (void*)fgClusters));
394 }
395 return fgClusters;
396}
397
398//_____________________________________________________________________________
399TClonesArray* AliTRDReconstructor::GetTracklets(const char *trkltype)
400{
401// Build/ Retrieve online tracklets array
402
eea7663c 403 if (trkltype && strlen(trkltype) > 0) {
d2eba04b 404 if(fgTracklets && (TClass::GetClass(trkltype) != fgTracklets->GetClass())){
405 fgTracklets->Delete();
406 delete fgTracklets;
407 }
408 if (!fgTracklets) {
409 fgTracklets = new TClonesArray(trkltype, 200);
410 fgTracklets->SetOwner(kTRUE);
411 AliInfoGeneral("AliTRDReconstructor", Form("Allocate online tracklets[%s] array @ %p", trkltype, (void*)fgTracklets));
412 }
413 }
414 return fgTracklets;
415}
416
417//_____________________________________________________________________________
418TClonesArray* AliTRDReconstructor::GetTracks()
419{
420// Build/ Retrieve cluster array
421 if(!fgTracks){
422 fgTracks = new TClonesArray("AliESDTrdTrack", 100);
423 fgTracks->SetOwner();
424 AliInfoGeneral("AliTRDReconstructor", Form("Allocate online tracks array @ %p", (void*)fgTracks));
425 }
426 return fgTracks;
427}
428
429//_____________________________________________________________________________
430void AliTRDReconstructor::ResetContainers() const
431{
432// prepare data containers for a new event
433
434 if(fgClusters){
435 AliDebug(1, Form("Removing %5d clusters @ %p", fgClusters->GetEntriesFast(), (void*)fgClusters));
436 fgClusters->Clear();
437 }
438 if(fgTracklets){
439 AliDebug(1, Form("Removing %3d online tracklets @ %p", fgTracklets->GetEntriesFast(), (void*)fgTracklets));
440 fgTracklets->Clear();
441 }
442 if(fgTracks){
443 AliDebug(1, Form("Removing %3d online tracks @ %p", fgTracks->GetEntriesFast(), (void*)fgTracks));
444 fgTracks->Clear();
445 }
ab67cd8d
JK
446 for (Int_t iSector = 0; iSector < 18; iSector++)
447 fgTriggerFlags.SetFlags(iSector, 0);
d2eba04b 448}