Memory leak corrected.
[u/mrichter/AliRoot.git] / TRD / AliTRDtrackerV1.cxx
CommitLineData
e4f2f73d 1/**************************************************************************
972ef65e 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**************************************************************************/
e4f2f73d 15
16/* $Id$ */
17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// Track finder //
21// //
22// Authors: //
23// Alex Bercuci <A.Bercuci@gsi.de> //
24// Markus Fasel <M.Fasel@gsi.de> //
25// //
26///////////////////////////////////////////////////////////////////////////////
27
e4f2f73d 28#include <TBranch.h>
bb56afff 29#include <TDirectory.h>
e4f2f73d 30#include <TLinearFitter.h>
e4f2f73d 31#include <TTree.h>
32#include <TClonesArray.h>
e4f2f73d 33#include <TTreeStream.h>
eb2b4f91 34#include <TGeoMatrix.h>
35#include <TGeoManager.h>
e4f2f73d 36
37#include "AliLog.h"
fac58f00 38#include "AliMathBase.h"
e4f2f73d 39#include "AliESDEvent.h"
bb56afff 40#include "AliGeomManager.h"
e4f2f73d 41#include "AliRieman.h"
42#include "AliTrackPointArray.h"
43
e4f2f73d 44#include "AliTRDgeometry.h"
45#include "AliTRDpadPlane.h"
e4f2f73d 46#include "AliTRDcalibDB.h"
e4f2f73d 47#include "AliTRDReconstructor.h"
48#include "AliTRDCalibraFillHisto.h"
e4f2f73d 49#include "AliTRDrecoParam.h"
bb56afff 50
51#include "AliTRDcluster.h"
828c6f80 52#include "AliTRDdigitsParam.h"
e4f2f73d 53#include "AliTRDseedV1.h"
0906e73e 54#include "AliTRDtrackV1.h"
bb56afff 55#include "AliTRDtrackerV1.h"
56#include "AliTRDtrackerDebug.h"
57#include "AliTRDtrackingChamber.h"
58#include "AliTRDchamberTimeBin.h"
59
e4f2f73d 60ClassImp(AliTRDtrackerV1)
903326c1 61ClassImp(AliTRDtrackerV1::AliTRDLeastSquare)
62ClassImp(AliTRDtrackerV1::AliTRDtrackFitterRieman)
eb38ed55 63
eb38ed55 64const Float_t AliTRDtrackerV1::fgkMinClustersInTrack = 0.5; //
65const Float_t AliTRDtrackerV1::fgkLabelFraction = 0.8; //
66const Double_t AliTRDtrackerV1::fgkMaxChi2 = 12.0; //
67const Double_t AliTRDtrackerV1::fgkMaxSnp = 0.95; // Maximum local sine of the azimuthal angle
68const Double_t AliTRDtrackerV1::fgkMaxStep = 2.0; // Maximal step size in propagation
d76231c8 69Double_t AliTRDtrackerV1::fgTopologicQA[kNConfigs] = {
6e39bde4 70 0.5112, 0.5112, 0.5112, 0.0786, 0.0786,
41702fec 71 0.0786, 0.0786, 0.0579, 0.0579, 0.0474,
72 0.0474, 0.0408, 0.0335, 0.0335, 0.0335
e165b64b 73};
74const Double_t AliTRDtrackerV1::fgkX0[kNPlanes] = {
75 300.2, 312.8, 325.4, 338.0, 350.6, 363.2};
2985ffcb 76Int_t AliTRDtrackerV1::fgNTimeBins = 0;
4d6aee34 77AliRieman* AliTRDtrackerV1::fgRieman = NULL;
78TLinearFitter* AliTRDtrackerV1::fgTiltedRieman = NULL;
79TLinearFitter* AliTRDtrackerV1::fgTiltedRiemanConstrained = NULL;
e4f2f73d 80
81//____________________________________________________________________
3a039a31 82AliTRDtrackerV1::AliTRDtrackerV1(AliTRDReconstructor *rec)
41702fec 83 :AliTracker()
4d6aee34 84 ,fkReconstructor(NULL)
85 ,fGeom(NULL)
86 ,fClusters(NULL)
87 ,fTracklets(NULL)
88 ,fTracks(NULL)
b1135f96 89 ,fTracksESD(NULL)
41702fec 90 ,fSieveSeeding(0)
e4f2f73d 91{
41702fec 92 //
93 // Default constructor.
94 //
eb2b4f91 95
96 SetReconstructor(rec); // initialize reconstructor
97
98 // initialize geometry
99 if(!AliGeomManager::GetGeometry()){
100 AliFatal("Could not get geometry.");
101 }
102 fGeom = new AliTRDgeometry();
103 fGeom->CreateClusterMatrixArray();
4d6aee34 104 TGeoHMatrix *matrix = NULL;
eb2b4f91 105 Double_t loc[] = {0., 0., 0.};
106 Double_t glb[] = {0., 0., 0.};
107 for(Int_t ily=kNPlanes; ily--;){
e165b64b 108 Int_t ism = 0;
109 while(!(matrix = fGeom->GetClusterMatrix(AliTRDgeometry::GetDetector(ily, 2, ism)))) ism++;
110 if(!matrix){
111 AliError(Form("Could not get transformation matrix for layer %d. Use default.", ily));
112 fR[ily] = fgkX0[ily];
eb2b4f91 113 continue;
114 }
115 matrix->LocalToMaster(loc, glb);
116 fR[ily] = glb[0]+ AliTRDgeometry::AnodePos()-.5*AliTRDgeometry::AmThick() - AliTRDgeometry::DrThick();
117 }
118
eb2b4f91 119 // initialize cluster containers
053767a4 120 for (Int_t isector = 0; isector < AliTRDgeometry::kNsector; isector++) new(&fTrSec[isector]) AliTRDtrackingSector(fGeom, isector);
3a039a31 121
eb2b4f91 122 // initialize arrays
123 memset(fTrackQuality, 0, kMaxTracksStack*sizeof(Double_t));
124 memset(fSeedLayer, 0, kMaxTracksStack*sizeof(Int_t));
125 memset(fSeedTB, 0, kNSeedPlanes*sizeof(AliTRDchamberTimeBin*));
b1135f96 126 fTracksESD = new TClonesArray("AliESDtrack", 2*kMaxTracksStack);
127 fTracksESD->SetOwner();
eb38ed55 128}
129
e4f2f73d 130//____________________________________________________________________
131AliTRDtrackerV1::~AliTRDtrackerV1()
132{
41702fec 133 //
134 // Destructor
135 //
136
4d6aee34 137 if(fgRieman) delete fgRieman; fgRieman = NULL;
138 if(fgTiltedRieman) delete fgTiltedRieman; fgTiltedRieman = NULL;
139 if(fgTiltedRiemanConstrained) delete fgTiltedRiemanConstrained; fgTiltedRiemanConstrained = NULL;
d611c74f 140 for(Int_t isl =0; isl<kNSeedPlanes; isl++) if(fSeedTB[isl]) delete fSeedTB[isl];
b1135f96 141 if(fTracksESD){ fTracksESD->Delete(); delete fTracksESD; }
41702fec 142 if(fTracks) {fTracks->Delete(); delete fTracks;}
143 if(fTracklets) {fTracklets->Delete(); delete fTracklets;}
48f8adf3 144 if(fClusters) {
145 fClusters->Delete(); delete fClusters;
146 }
41702fec 147 if(fGeom) delete fGeom;
e4f2f73d 148}
149
150//____________________________________________________________________
151Int_t AliTRDtrackerV1::Clusters2Tracks(AliESDEvent *esd)
152{
41702fec 153 //
154 // Steering stand alone tracking for full TRD detector
155 //
156 // Parameters :
157 // esd : The ESD event. On output it contains
158 // the ESD tracks found in TRD.
159 //
160 // Output :
161 // Number of tracks found in the TRD detector.
162 //
163 // Detailed description
164 // 1. Launch individual SM trackers.
165 // See AliTRDtrackerV1::Clusters2TracksSM() for details.
166 //
167
4d6aee34 168 if(!fkReconstructor->GetRecoParam() ){
3a039a31 169 AliError("Reconstruction configuration not initialized. Call first AliTRDReconstructor::SetRecoParam().");
41702fec 170 return 0;
171 }
172
173 //AliInfo("Start Track Finder ...");
174 Int_t ntracks = 0;
053767a4 175 for(int ism=0; ism<AliTRDgeometry::kNsector; ism++){
41702fec 176 // for(int ism=1; ism<2; ism++){
177 //AliInfo(Form("Processing supermodule %i ...", ism));
178 ntracks += Clusters2TracksSM(ism, esd);
179 }
980d5a2a 180 AliInfo(Form("Number of tracks: !TRDin[%d]", ntracks));
41702fec 181 return ntracks;
e4f2f73d 182}
183
0906e73e 184
185//_____________________________________________________________________________
eb38ed55 186Bool_t AliTRDtrackerV1::GetTrackPoint(Int_t index, AliTrackPoint &p) const
0906e73e 187{
41702fec 188 //AliInfo(Form("Asking for tracklet %d", index));
189
84eab75a 190 // reset position of the point before using it
191 p.SetXYZ(0., 0., 0.);
2f7514a6 192 AliTRDseedV1 *tracklet = GetTracklet(index);
193 if (!tracklet) return kFALSE;
84eab75a 194
41702fec 195 // get detector for this tracklet
e165b64b 196 Int_t det = tracklet->GetDetector();
197 Int_t sec = fGeom->GetSector(det);
198 Double_t alpha = (sec+.5)*AliTRDgeometry::GetAlpha(),
199 sinA = TMath::Sin(alpha),
200 cosA = TMath::Cos(alpha);
41702fec 201 Double_t local[3];
e165b64b 202 local[0] = tracklet->GetX();
203 local[1] = tracklet->GetY();
204 local[2] = tracklet->GetZ();
41702fec 205 Double_t global[3];
e165b64b 206 fGeom->RotateBack(det, local, global);
207
208 Double_t cov2D[3]; Float_t cov[6];
209 tracklet->GetCovAt(local[0], cov2D);
210 cov[0] = cov2D[0]*sinA*sinA;
211 cov[1] =-cov2D[0]*sinA*cosA;
212 cov[2] =-cov2D[1]*sinA;
213 cov[3] = cov2D[0]*cosA*cosA;
214 cov[4] = cov2D[1]*cosA;
215 cov[5] = cov2D[2];
216 // store the global position of the tracklet and its covariance matrix in the track point
217 p.SetXYZ(global[0],global[1],global[2], cov);
41702fec 218
219 // setting volume id
e165b64b 220 AliGeomManager::ELayerID iLayer = AliGeomManager::ELayerID(AliGeomManager::kTRD1+fGeom->GetLayer(det));
221 Int_t modId = fGeom->GetSector(det) * AliTRDgeometry::kNstack + fGeom->GetStack(det);
41702fec 222 UShort_t volid = AliGeomManager::LayerToVolUID(iLayer, modId);
223 p.SetVolumeID(volid);
224
225 return kTRUE;
0906e73e 226}
227
eb38ed55 228//____________________________________________________________________
229TLinearFitter* AliTRDtrackerV1::GetTiltedRiemanFitter()
230{
41702fec 231 if(!fgTiltedRieman) fgTiltedRieman = new TLinearFitter(4, "hyp4");
232 return fgTiltedRieman;
eb38ed55 233}
0906e73e 234
eb38ed55 235//____________________________________________________________________
236TLinearFitter* AliTRDtrackerV1::GetTiltedRiemanFitterConstraint()
237{
41702fec 238 if(!fgTiltedRiemanConstrained) fgTiltedRiemanConstrained = new TLinearFitter(2, "hyp2");
239 return fgTiltedRiemanConstrained;
eb38ed55 240}
41702fec 241
eb38ed55 242//____________________________________________________________________
243AliRieman* AliTRDtrackerV1::GetRiemanFitter()
244{
fac58f00 245 if(!fgRieman) fgRieman = new AliRieman(AliTRDseedV1::kNtb * AliTRDgeometry::kNlayer);
41702fec 246 return fgRieman;
eb38ed55 247}
41702fec 248
0906e73e 249//_____________________________________________________________________________
250Int_t AliTRDtrackerV1::PropagateBack(AliESDEvent *event)
251{
2a3191bb 252// Propagation of ESD tracks from TPC to TOF detectors and building of the TRD track. For building
253// a TRD track an ESD track is used as seed. The informations obtained on the TRD track (measured points,
254// covariance, PID, etc.) are than used to update the corresponding ESD track.
255// Each track seed is first propagated to the geometrical limit of the TRD detector.
256// Its prolongation is searched in the TRD and if corresponding clusters are found tracklets are
257// constructed out of them (see AliTRDseedV1::AttachClusters()) and the track is updated.
258// Otherwise the ESD track is left unchanged.
259//
260// The following steps are performed:
261// 1. Selection of tracks based on the variance in the y-z plane.
262// 2. Propagation to the geometrical limit of the TRD volume. If track propagation fails the AliESDtrack::kTRDStop is set.
263// 3. Prolongation inside the fiducial volume (see AliTRDtrackerV1::FollowBackProlongation()) and marking
264// the following status bits:
265// - AliESDtrack::kTRDin - if the tracks enters the TRD fiducial volume
266// - AliESDtrack::kTRDStop - if the tracks fails propagation
267// - AliESDtrack::kTRDbackup - if the tracks fulfills chi2 conditions and qualify for refitting
268// 4. Writting to friends, PID, MC label, quality etc. Setting status bit AliESDtrack::kTRDout.
269// 5. Propagation to TOF. If track propagation fails the AliESDtrack::kTRDStop is set.
270//
271
272 AliTRDCalibraFillHisto *calibra = AliTRDCalibraFillHisto::Instance(); // Calibration monitor
41702fec 273 if (!calibra) AliInfo("Could not get Calibra instance\n");
828c6f80 274 if(!fgNTimeBins) CookNTimeBins();
275
eb2b4f91 276 // Define scalers
277 Int_t nFound = 0, // number of tracks found
278 nSeeds = 0, // total number of ESD seeds
279 nTRDseeds= 0, // number of seeds in the TRD acceptance
280 nTPCseeds= 0; // number of TPC seeds
41702fec 281 Float_t foundMin = 20.0;
282
4d6aee34 283 Float_t *quality = NULL;
284 Int_t *index = NULL;
eb2b4f91 285 nSeeds = event->GetNumberOfTracks();
286 // Sort tracks according to quality
287 // (covariance in the yz plane)
288 if(nSeeds){
289 quality = new Float_t[nSeeds];
290 index = new Int_t[nSeeds];
291 for (Int_t iSeed = nSeeds; iSeed--;) {
d611c74f 292 AliESDtrack *seed = event->GetTrack(iSeed);
293 Double_t covariance[15];
294 seed->GetExternalCovariance(covariance);
295 quality[iSeed] = covariance[0] + covariance[2];
296 }
eb2b4f91 297 TMath::Sort(nSeeds, quality, index,kFALSE);
41702fec 298 }
41702fec 299
eb2b4f91 300 // Propagate all seeds
41702fec 301 Int_t expectedClr;
302 AliTRDtrackV1 track;
eb2b4f91 303 for (Int_t iSeed = 0; iSeed < nSeeds; iSeed++) {
41702fec 304
305 // Get the seeds in sorted sequence
306 AliESDtrack *seed = event->GetTrack(index[iSeed]);
eb2b4f91 307 Float_t p4 = seed->GetC(seed->GetBz());
41702fec 308
309 // Check the seed status
310 ULong_t status = seed->GetStatus();
311 if ((status & AliESDtrack::kTPCout) == 0) continue;
312 if ((status & AliESDtrack::kTRDout) != 0) continue;
eb2b4f91 313
314 // Propagate to the entrance in the TRD mother volume
41702fec 315 new(&track) AliTRDtrackV1(*seed);
eb2b4f91 316 if(AliTRDgeometry::GetXtrdBeg() > (fgkMaxStep + track.GetX()) && !PropagateToX(track, AliTRDgeometry::GetXtrdBeg(), fgkMaxStep)){
317 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
318 continue;
319 }
320 if(!AdjustSector(&track)){
321 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
322 continue;
323 }
324 if(TMath::Abs(track.GetSnp()) > fgkMaxSnp) {
325 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
326 continue;
327 }
328
329 nTPCseeds++;
e4d4864b 330
eb2b4f91 331 // store track status at TRD entrance
76b60503 332 seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
ae3fbe1f 333
eb2b4f91 334 // prepare track and do propagation in the TRD
4d6aee34 335 track.SetReconstructor(fkReconstructor);
eb2b4f91 336 track.SetKink(Bool_t(seed->GetKinkIndex(0)));
337 expectedClr = FollowBackProlongation(track);
338 // check if track entered the TRD fiducial volume
339 if(track.GetTrackLow()){
340 seed->UpdateTrackParams(&track, AliESDtrack::kTRDin);
341 nTRDseeds++;
342 }
343 // check if track was stopped in the TRD
344 if (expectedClr<0){
345 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
346 continue;
347 }
ae3fbe1f 348
349 if(expectedClr){
eb2b4f91 350 nFound++;
41702fec 351 // computes PID for track
352 track.CookPID();
353 // update calibration references using this track
354 if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(&track);
355 // save calibration object
a2fbb6ec 356 if (fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 0){
224f357f 357 AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(track);
358 calibTrack->SetOwner();
359 seed->AddCalibObject(calibTrack);
360 }
361 //update ESD track
41702fec 362 if ((track.GetNumberOfClusters() > 15) && (track.GetNumberOfClusters() > 0.5*expectedClr)) {
2389e96f 363 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
41702fec 364 track.UpdateESDtrack(seed);
41702fec 365 }
366 }
367
27fbeba8 368 if ((TMath::Abs(track.GetC(track.GetBz()) - p4) / TMath::Abs(p4) < 0.2) ||(track.Pt() > 0.8)) {
e3cf3d02 369
41702fec 370 // Make backup for back propagation
41702fec 371 Int_t foundClr = track.GetNumberOfClusters();
372 if (foundClr >= foundMin) {
41702fec 373 track.CookLabel(1. - fgkLabelFraction);
f29f13a6 374 //if(track.GetBackupTrack()) UseClusters(track.GetBackupTrack());
41702fec 375
376 // Sign only gold tracks
377 if (track.GetChi2() / track.GetNumberOfClusters() < 4) {
f29f13a6 378 //if ((seed->GetKinkIndex(0) == 0) && (track.Pt() < 1.5)) UseClusters(&track);
41702fec 379 }
380 Bool_t isGold = kFALSE;
381
382 // Full gold track
383 if (track.GetChi2() / track.GetNumberOfClusters() < 5) {
384 if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
385
386 isGold = kTRUE;
387 }
388
389 // Almost gold track
390 if ((!isGold) && (track.GetNCross() == 0) && (track.GetChi2() / track.GetNumberOfClusters() < 7)) {
391 //seed->UpdateTrackParams(track, AliESDtrack::kTRDbackup);
392 if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
393
394 isGold = kTRUE;
395 }
396
397 if ((!isGold) && (track.GetBackupTrack())) {
398 if ((track.GetBackupTrack()->GetNumberOfClusters() > foundMin) && ((track.GetBackupTrack()->GetChi2()/(track.GetBackupTrack()->GetNumberOfClusters()+1)) < 7)) {
399 seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
400 isGold = kTRUE;
401 }
402 }
41702fec 403 }
404 }
405
eb2b4f91 406 // Propagation to the TOF
407 if(!(seed->GetStatus()&AliESDtrack::kTRDStop)) {
408 Int_t sm = track.GetSector();
409 // default value in case we have problems with the geometry.
410 Double_t xtof = 371.;
411 //Calculate radial position of the beginning of the TOF
412 //mother volume. In order to avoid mixing of the TRD
413 //and TOF modules some hard values are needed. This are:
414 //1. The path to the TOF module.
415 //2. The width of the TOF (29.05 cm)
416 //(with the help of Annalisa de Caro Mar-17-2009)
417 if(gGeoManager){
418 gGeoManager->cd(Form("/ALIC_1/B077_1/BSEGMO%d_1/BTOF%d_1", sm, sm));
4d6aee34 419 TGeoHMatrix *m = NULL;
eb2b4f91 420 Double_t loc[]={0., 0., -.5*29.05}, glob[3];
421
422 if((m=gGeoManager->GetCurrentMatrix())){
423 m->LocalToMaster(loc, glob);
424 xtof = TMath::Sqrt(glob[0]*glob[0]+glob[1]*glob[1]);
425 }
41702fec 426 }
672b48bd 427 if(xtof > (fgkMaxStep + track.GetX()) && !PropagateToX(track, xtof, fgkMaxStep)){
428 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
429 continue;
430 }
431 if(!AdjustSector(&track)){
432 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
433 continue;
434 }
435 if(TMath::Abs(track.GetSnp()) > fgkMaxSnp){
436 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
437 continue;
438 }
eb2b4f91 439 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
440 // TODO obsolete - delete
441 seed->SetTRDQuality(track.StatusForTOF());
41702fec 442 }
41702fec 443 seed->SetTRDBudget(track.GetBudget(0));
444 }
d611c74f 445 if(index) delete [] index;
446 if(quality) delete [] quality;
41702fec 447
d4cf71ca 448 AliInfo(Form("Number of seeds: TPCout[%d] TRDin[%d]", nTPCseeds, nTRDseeds));
449 AliInfo(Form("Number of tracks: TRDout[%d]", nFound));
450
d611c74f 451 // run stand alone tracking
4d6aee34 452 if (fkReconstructor->IsSeeding()) Clusters2Tracks(event);
41702fec 453
454 return 0;
0906e73e 455}
456
457
458//____________________________________________________________________
459Int_t AliTRDtrackerV1::RefitInward(AliESDEvent *event)
460{
41702fec 461 //
462 // Refits tracks within the TRD. The ESD event is expected to contain seeds
463 // at the outer part of the TRD.
464 // The tracks are propagated to the innermost time bin
465 // of the TRD and the ESD event is updated
466 // Origin: Thomas KUHR (Thomas.Kuhr@cern.ch)
467 //
468
469 Int_t nseed = 0; // contor for loaded seeds
470 Int_t found = 0; // contor for updated TRD tracks
471
472
473 AliTRDtrackV1 track;
474 for (Int_t itrack = 0; itrack < event->GetNumberOfTracks(); itrack++) {
475 AliESDtrack *seed = event->GetTrack(itrack);
476 new(&track) AliTRDtrackV1(*seed);
477
478 if (track.GetX() < 270.0) {
479 seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
480 continue;
481 }
482
e3cf3d02 483 // reject tracks which failed propagation in the TRD or
484 // are produced by the TRD stand alone tracker
41702fec 485 ULong_t status = seed->GetStatus();
e3cf3d02 486 if(!(status & AliESDtrack::kTRDout)) continue;
487 if(!(status & AliESDtrack::kTRDin)) continue;
41702fec 488 nseed++;
489
490 track.ResetCovariance(50.0);
491
492 // do the propagation and processing
493 Bool_t kUPDATE = kFALSE;
494 Double_t xTPC = 250.0;
495 if(FollowProlongation(track)){
496 // Prolongate to TPC
497 if (PropagateToX(track, xTPC, fgkMaxStep)) { // -with update
e3cf3d02 498 seed->UpdateTrackParams(&track, AliESDtrack::kTRDrefit);
499 found++;
500 kUPDATE = kTRUE;
501 }
502
503 // Update the friend track
a2fbb6ec 504 if (fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 0){
4d6aee34 505 TObject *o = NULL; Int_t ic = 0;
506 AliTRDtrackV1 *calibTrack = NULL;
e3cf3d02 507 while((o = seed->GetCalibObject(ic++))){
508 if(!(calibTrack = dynamic_cast<AliTRDtrackV1*>(o))) continue;
509 calibTrack->SetTrackHigh(track.GetTrackHigh());
510 }
41702fec 511 }
e3cf3d02 512 }
41702fec 513
514 // Prolongate to TPC without update
515 if(!kUPDATE) {
516 AliTRDtrackV1 tt(*seed);
e3cf3d02 517 if (PropagateToX(tt, xTPC, fgkMaxStep)) seed->UpdateTrackParams(&tt, AliESDtrack::kTRDbackup);
41702fec 518 }
519 }
980d5a2a 520 AliInfo(Form("Number of seeds: TRDout[%d]", nseed));
521 AliInfo(Form("Number of tracks: TRDrefit[%d]", found));
41702fec 522
523 return 0;
0906e73e 524}
525
0906e73e 526//____________________________________________________________________
527Int_t AliTRDtrackerV1::FollowProlongation(AliTRDtrackV1 &t)
528{
41702fec 529 // Extrapolates the TRD track in the TPC direction.
530 //
531 // Parameters
532 // t : the TRD track which has to be extrapolated
533 //
534 // Output
535 // number of clusters attached to the track
536 //
537 // Detailed description
538 //
539 // Starting from current radial position of track <t> this function
540 // extrapolates the track through the 6 TRD layers. The following steps
541 // are being performed for each plane:
542 // 1. prepare track:
543 // a. get plane limits in the local x direction
544 // b. check crossing sectors
545 // c. check track inclination
546 // 2. search tracklet in the tracker list (see GetTracklet() for details)
547 // 3. evaluate material budget using the geo manager
548 // 4. propagate and update track using the tracklet information.
549 //
550 // Debug level 2
551 //
552
e3cf3d02 553 Bool_t kStoreIn = kTRUE;
41702fec 554 Int_t nClustersExpected = 0;
e3cf3d02 555 for (Int_t iplane = kNPlanes; iplane--;) {
17896e82 556 Int_t index(-1);
41702fec 557 AliTRDseedV1 *tracklet = GetTracklet(&t, iplane, index);
17896e82 558 AliDebug(2, Form("Tracklet[%p] ly[%d] idx[%d]", (void*)tracklet, iplane, index));
41702fec 559 if(!tracklet) continue;
980d5a2a 560 if(!tracklet->IsOK()){
561 AliDebug(1, Form("Tracklet Det[%d] !OK", tracklet->GetDetector()));
562 continue;
563 }
e3cf3d02 564 Double_t x = tracklet->GetX();//GetX0();
41702fec 565 // reject tracklets which are not considered for inward refit
566 if(x > t.GetX()+fgkMaxStep) continue;
567
568 // append tracklet to track
569 t.SetTracklet(tracklet, index);
570
571 if (x < (t.GetX()-fgkMaxStep) && !PropagateToX(t, x+fgkMaxStep, fgkMaxStep)) break;
572 if (!AdjustSector(&t)) break;
573
574 // Start global position
575 Double_t xyz0[3];
576 t.GetXYZ(xyz0);
577
578 // End global position
579 Double_t alpha = t.GetAlpha(), y, z;
580 if (!t.GetProlongation(x,y,z)) break;
581 Double_t xyz1[3];
582 xyz1[0] = x * TMath::Cos(alpha) - y * TMath::Sin(alpha);
583 xyz1[1] = x * TMath::Sin(alpha) + y * TMath::Cos(alpha);
584 xyz1[2] = z;
585
51a23065 586 Double_t length = TMath::Sqrt(
587 (xyz0[0]-xyz1[0])*(xyz0[0]-xyz1[0]) +
588 (xyz0[1]-xyz1[1])*(xyz0[1]-xyz1[1]) +
589 (xyz0[2]-xyz1[2])*(xyz0[2]-xyz1[2])
590 );
591 if(length>0.){
592 // Get material budget
593 Double_t param[7];
594 if(AliTracker::MeanMaterialBudget(xyz0, xyz1, param)<=0.) break;
595 Double_t xrho= param[0]*param[4];
596 Double_t xx0 = param[1]; // Get mean propagation parameters
597
598 // Propagate and update
599 t.PropagateTo(x, xx0, xrho);
600 if (!AdjustSector(&t)) break;
601 }
e3cf3d02 602 if(kStoreIn){
603 t.SetTrackHigh();
604 kStoreIn = kFALSE;
605 }
606
b72f4eaf 607 Double_t cov[3]; tracklet->GetCovAt(x, cov);
608 Double_t p[2] = { tracklet->GetY(), tracklet->GetZ()};
609 Double_t chi2 = ((AliExternalTrackParam)t).GetPredictedChi2(p, cov);
610 if (chi2 < 1e+10 && t.Update(p, cov, chi2)){
41702fec 611 nClustersExpected += tracklet->GetN();
612 }
613 }
614
a2fbb6ec 615 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 1){
41702fec 616 Int_t index;
51a23065 617 for(int iplane=0; iplane<AliTRDgeometry::kNlayer; iplane++){
41702fec 618 AliTRDseedV1 *tracklet = GetTracklet(&t, iplane, index);
619 if(!tracklet) continue;
620 t.SetTracklet(tracklet, index);
621 }
622
a2fbb6ec 623 if(fkReconstructor->IsDebugStreaming()){
624 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
625 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
626 AliTRDtrackV1 track(t);
627 track.SetOwner();
628 cstreamer << "FollowProlongation"
629 << "EventNumber=" << eventNumber
630 << "ncl=" << nClustersExpected
631 << "track.=" << &track
632 << "\n";
633 }
41702fec 634 }
41702fec 635 return nClustersExpected;
0906e73e 636
637}
638
639//_____________________________________________________________________________
640Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
641{
afa21247 642// Extrapolates/Build the TRD track in the TOF direction.
643//
644// Parameters
645// t : the TRD track which has to be extrapolated
646//
647// Output
648// number of clusters attached to the track
649//
650// Starting from current radial position of track <t> this function
651// extrapolates the track through the 6 TRD layers. The following steps
652// are being performed for each plane:
653// 1. Propagate track to the entrance of the next chamber:
654// - get chamber limits in the radial direction
655// - check crossing sectors
656// - check track inclination
657// - check track prolongation against boundary conditions (see exclusion boundaries on AliTRDgeometry::IsOnBoundary())
658// 2. Build tracklet (see AliTRDseed::AttachClusters() for details) for this layer if needed. If only
659// Kalman filter is needed and tracklets are already linked to the track this step is skipped.
660// 3. Fit tracklet using the information from the Kalman filter.
661// 4. Propagate and update track at reference radial position of the tracklet.
662// 5. Register tracklet with the tracker and track; update pulls monitoring.
663//
664// Observation
2a3191bb 665// 1. During the propagation a bit map is filled detailing the status of the track in each TRD chamber. The following errors are being registered for each tracklet:
666// - AliTRDtrackV1::kProlongation : track prolongation failed
667// - AliTRDtrackV1::kPropagation : track prolongation failed
668// - AliTRDtrackV1::kAdjustSector : failed during sector crossing
669// - AliTRDtrackV1::kSnp : too large bending
670// - AliTRDtrackV1::kTrackletInit : fail to initialize tracklet
671// - AliTRDtrackV1::kUpdate : fail to attach clusters or fit the tracklet
672// - AliTRDtrackV1::kUnknown : anything which is not covered before
afa21247 673// 2. By default the status of the track before first TRD update is saved.
674//
675// Debug level 2
676//
677// Author
678// Alexandru Bercuci <A.Bercuci@gsi.de>
679//
41702fec 680
eb2b4f91 681 Int_t n = 0;
682 Double_t driftLength = .5*AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick();
4d6aee34 683 AliTRDtrackingChamber *chamber = NULL;
41702fec 684
7c3eecb8 685 Int_t debugLevel = fkReconstructor->IsDebugStreaming() ? fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) : 0;
686 TTreeSRedirector *cstreamer = fkReconstructor->IsDebugStreaming() ? fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker) : 0x0;
687
4d6aee34 688 AliTRDseedV1 tracklet, *ptrTracklet = NULL;
181d2c97 689 // in case of stand alone tracking we store all the pointers to the tracklets in a temporary array
690 AliTRDseedV1 *tracklets[kNPlanes];
691 memset(tracklets, 0, sizeof(AliTRDseedV1 *) * kNPlanes);
692 for(Int_t ip = 0; ip < kNPlanes; ip++){
693 tracklets[ip] = t.GetTracklet(ip);
694 t.UnsetTracklet(ip);
695 }
81a6494d 696 Bool_t kStoreIn = kTRUE, kPropagateIn = kTRUE;
e3cf3d02 697
053767a4 698 // Loop through the TRD layers
4d6aee34 699 TGeoHMatrix *matrix = NULL;
eb2b4f91 700 Double_t x, y, z;
701 for (Int_t ily=0, sm=-1, stk=-1, det=-1; ily < AliTRDgeometry::kNlayer; ily++) {
1bd708e3 702 AliDebug(2, Form("Propagate to x[%d] = %7.2f", ily, fR[ily]));
703
eb2b4f91 704 // rough estimate of the entry point
705 if (!t.GetProlongation(fR[ily], y, z)){
706 n=-1;
707 t.SetStatus(AliTRDtrackV1::kProlongation);
1bd708e3 708 AliDebug(4, Form("Failed Rough Prolongation to ly[%d] x[%7.2f] y[%7.2f] z[%7.2f]", ily, fR[ily], y, z));
eb2b4f91 709 break;
710 }
41702fec 711
eb2b4f91 712 // find sector / stack / detector
713 sm = t.GetSector();
714 // TODO cross check with y value !
715 stk = fGeom->GetStack(z, ily);
716 det = stk>=0 ? AliTRDgeometry::GetDetector(ily, stk, sm) : -1;
4d6aee34 717 matrix = det>=0 ? fGeom->GetClusterMatrix(det) : NULL;
1bd708e3 718 AliDebug(3, Form("Propagate to det[%3d]", det));
eb2b4f91 719
720 // check if supermodule/chamber is installed
721 if( !fGeom->GetSMstatus(sm) ||
722 stk<0. ||
723 fGeom->IsHole(ily, stk, sm) ||
724 !matrix ){
1bd708e3 725 AliDebug(4, Form("Missing Geometry ly[%d]. Guess radial position", ily));
eb2b4f91 726 // propagate to the default radial position
727 if(fR[ily] > (fgkMaxStep + t.GetX()) && !PropagateToX(t, fR[ily], fgkMaxStep)){
728 n=-1;
729 t.SetStatus(AliTRDtrackV1::kPropagation);
1bd708e3 730 AliDebug(4, "Failed Propagation [Missing Geometry]");
41702fec 731 break;
732 }
eb2b4f91 733 if(!AdjustSector(&t)){
734 n=-1;
735 t.SetStatus(AliTRDtrackV1::kAdjustSector);
1bd708e3 736 AliDebug(4, "Failed Adjust Sector [Missing Geometry]");
eb2b4f91 737 break;
738 }
739 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp){
740 n=-1;
741 t.SetStatus(AliTRDtrackV1::kSnp);
1bd708e3 742 AliDebug(4, "Failed Max Snp [Missing Geometry]");
eb2b4f91 743 break;
744 }
745 t.SetStatus(AliTRDtrackV1::kGeometry, ily);
41702fec 746 continue;
747 }
e3cf3d02 748
eb2b4f91 749 // retrieve rotation matrix for the current chamber
750 Double_t loc[] = {AliTRDgeometry::AnodePos()- driftLength, 0., 0.};
751 Double_t glb[] = {0., 0., 0.};
752 matrix->LocalToMaster(loc, glb);
753
754 // Propagate to the radial distance of the current layer
755 x = glb[0] - fgkMaxStep;
756 if(x > (fgkMaxStep + t.GetX()) && !PropagateToX(t, x, fgkMaxStep)){
757 n=-1;
758 t.SetStatus(AliTRDtrackV1::kPropagation);
1bd708e3 759 AliDebug(4, Form("Failed Initial Propagation to x[%7.2f]", x));
eb2b4f91 760 break;
761 }
762 if(!AdjustSector(&t)){
763 n=-1;
764 t.SetStatus(AliTRDtrackV1::kAdjustSector);
1bd708e3 765 AliDebug(4, "Failed Adjust Sector Start");
eb2b4f91 766 break;
767 }
768 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp) {
769 n=-1;
770 t.SetStatus(AliTRDtrackV1::kSnp);
1bd708e3 771 AliDebug(4, Form("Failed Max Snp[%f] MaxSnp[%f]", t.GetSnp(), fgkMaxSnp));
eb2b4f91 772 break;
773 }
4d6aee34 774 Bool_t doRecalculate = kFALSE;
eb2b4f91 775 if(sm != t.GetSector()){
776 sm = t.GetSector();
4d6aee34 777 doRecalculate = kTRUE;
eb2b4f91 778 }
779 if(stk != fGeom->GetStack(z, ily)){
780 stk = fGeom->GetStack(z, ily);
4d6aee34 781 doRecalculate = kTRUE;
eb2b4f91 782 }
4d6aee34 783 if(doRecalculate){
eb2b4f91 784 det = AliTRDgeometry::GetDetector(ily, stk, sm);
785 if(!(matrix = fGeom->GetClusterMatrix(det))){
786 t.SetStatus(AliTRDtrackV1::kGeometry, ily);
1bd708e3 787 AliDebug(4, Form("Failed Geometry Matrix ly[%d]", ily));
eb2b4f91 788 continue;
789 }
790 matrix->LocalToMaster(loc, glb);
791 x = glb[0] - fgkMaxStep;
792 }
793
794 // check if track is well inside fiducial volume
795 if (!t.GetProlongation(x+fgkMaxStep, y, z)) {
796 n=-1;
797 t.SetStatus(AliTRDtrackV1::kProlongation);
1bd708e3 798 AliDebug(4, Form("Failed Prolongation to x[%7.2f] y[%7.2f] z[%7.2f]", x+fgkMaxStep, y, z));
eb2b4f91 799 break;
800 }
801 if(fGeom->IsOnBoundary(det, y, z, .5)){
802 t.SetStatus(AliTRDtrackV1::kBoundary, ily);
1bd708e3 803 AliDebug(4, "Failed Track on Boundary");
eb2b4f91 804 continue;
805 }
806 // mark track as entering the FIDUCIAL volume of TRD
e3cf3d02 807 if(kStoreIn){
808 t.SetTrackLow();
809 kStoreIn = kFALSE;
810 }
b1957d3c 811
eb2b4f91 812 ptrTracklet = tracklets[ily];
813 if(!ptrTracklet){ // BUILD TRACKLET
1bd708e3 814 AliDebug(3, Form("Building tracklet det[%d]", det));
eb2b4f91 815 // check data in supermodule
816 if(!fTrSec[sm].GetNChambers()){
817 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
1bd708e3 818 AliDebug(4, "Failed NoClusters");
eb2b4f91 819 continue;
820 }
821 if(fTrSec[sm].GetX(ily) < 1.){
822 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
1bd708e3 823 AliDebug(4, "Failed NoX");
eb2b4f91 824 continue;
825 }
826
827 // check data in chamber
828 if(!(chamber = fTrSec[sm].GetChamber(stk, ily))){
829 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
1bd708e3 830 AliDebug(4, "Failed No Detector");
eb2b4f91 831 continue;
832 }
4d6aee34 833 if(chamber->GetNClusters() < fgNTimeBins*fkReconstructor->GetRecoParam() ->GetFindableClusters()){
eb2b4f91 834 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
1bd708e3 835 AliDebug(4, "Failed Not Enough Clusters in Detector");
eb2b4f91 836 continue;
837 }
838 // build tracklet
839 ptrTracklet = new(&tracklet) AliTRDseedV1(det);
4d6aee34 840 ptrTracklet->SetReconstructor(fkReconstructor);
eb2b4f91 841 ptrTracklet->SetKink(t.IsKink());
842 ptrTracklet->SetPadPlane(fGeom->GetPadPlane(ily, stk));
843 ptrTracklet->SetX0(glb[0]+driftLength);
844 if(!tracklet.Init(&t)){
845 n=-1;
846 t.SetStatus(AliTRDtrackV1::kTrackletInit);
1bd708e3 847 AliDebug(4, "Failed Tracklet Init");
eb2b4f91 848 break;
849 }
850 if(!tracklet.AttachClusters(chamber, kTRUE)){
851 t.SetStatus(AliTRDtrackV1::kNoAttach, ily);
7c3eecb8 852 if(debugLevel>3){
853 AliTRDseedV1 trackletCp(*ptrTracklet);
854 UChar_t status(t.GetStatusTRD(ily));
855 (*cstreamer) << "FollowBackProlongation2"
856 <<"status=" << status
857 <<"tracklet.=" << &trackletCp
858 << "\n";
859 }
1bd708e3 860 AliDebug(4, "Failed Attach Clusters");
eb2b4f91 861 continue;
862 }
1bd708e3 863 AliDebug(3, Form("Number of Clusters in Tracklet: %d", tracklet.GetN()));
4d6aee34 864 if(tracklet.GetN() < fgNTimeBins*fkReconstructor->GetRecoParam() ->GetFindableClusters()){
eb2b4f91 865 t.SetStatus(AliTRDtrackV1::kNoClustersTracklet, ily);
7c3eecb8 866 if(debugLevel>3){
867 AliTRDseedV1 trackletCp(*ptrTracklet);
868 UChar_t status(t.GetStatusTRD(ily));
869 (*cstreamer) << "FollowBackProlongation2"
870 <<"status=" << status
871 <<"tracklet.=" << &trackletCp
872 << "\n";
873 }
1bd708e3 874 AliDebug(4, "Failed N Clusters Attached");
eb2b4f91 875 continue;
876 }
877 ptrTracklet->UpdateUsed();
0217fcd0 878 } else AliDebug(2, Form("Use external tracklet ly[%d]", ily));
eb2b4f91 879 // propagate track to the radial position of the tracklet
880 ptrTracklet->UseClusters(); // TODO ? do we need this here ?
881 // fit tracklet no tilt correction
882 if(!ptrTracklet->Fit(kFALSE)){
883 t.SetStatus(AliTRDtrackV1::kNoFit, ily);
1bd708e3 884 AliDebug(4, "Failed Tracklet Fit");
eb2b4f91 885 continue;
886 }
887 x = ptrTracklet->GetX(); //GetX0();
888 if(x > (fgkMaxStep + t.GetX()) && !PropagateToX(t, x, fgkMaxStep)) {
889 n=-1;
890 t.SetStatus(AliTRDtrackV1::kPropagation);
1bd708e3 891 AliDebug(4, Form("Failed Propagation to Tracklet x[%7.2f]", x));
eb2b4f91 892 break;
893 }
894 if(!AdjustSector(&t)) {
895 n=-1;
896 t.SetStatus(AliTRDtrackV1::kAdjustSector);
1bd708e3 897 AliDebug(4, "Failed Adjust Sector");
eb2b4f91 898 break;
41702fec 899 }
eb2b4f91 900 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp) {
901 n=-1;
902 t.SetStatus(AliTRDtrackV1::kSnp);
1bd708e3 903 AliDebug(4, Form("Failed Max Snp[%f] MaxSnp[%f]", t.GetSnp(), fgkMaxSnp));
eb2b4f91 904 break;
905 }
81a6494d 906 if(kPropagateIn){
907 t.SetTrackLow();
908 kPropagateIn = kFALSE;
909 }
b72f4eaf 910 Double_t cov[3]; ptrTracklet->GetCovAt(x, cov);
911 Double_t p[2] = { ptrTracklet->GetY(), ptrTracklet->GetZ()};
912 Double_t chi2 = ((AliExternalTrackParam)t).GetPredictedChi2(p, cov);
6e39bde4 913 // update Kalman with the TRD measurement
b72f4eaf 914 if(chi2>1e+10){ // TODO
eb2b4f91 915 t.SetStatus(AliTRDtrackV1::kChi2, ily);
7c3eecb8 916 if(debugLevel > 2){
917 UChar_t status(t.GetStatusTRD());
918 AliTRDseedV1 trackletCp(*ptrTracklet);
919 AliTRDtrackV1 trackCp(t);
920 trackCp.SetOwner();
921 (*cstreamer) << "FollowBackProlongation1"
922 << "status=" << status
923 << "tracklet.=" << &trackletCp
924 << "track.=" << &trackCp
925 << "\n";
926 }
1bd708e3 927 AliDebug(4, Form("Failed Chi2[%f]", chi2));
eb2b4f91 928 continue;
929 }
b72f4eaf 930 if(!t.Update(p, cov, chi2)) {
eb2b4f91 931 n=-1;
932 t.SetStatus(AliTRDtrackV1::kUpdate);
7c3eecb8 933 if(debugLevel > 2){
934 UChar_t status(t.GetStatusTRD());
935 AliTRDseedV1 trackletCp(*ptrTracklet);
936 AliTRDtrackV1 trackCp(t);
937 trackCp.SetOwner();
938 (*cstreamer) << "FollowBackProlongation1"
939 << "status=" << status
940 << "tracklet.=" << &trackletCp
941 << "track.=" << &trackCp
942 << "\n";
943 }
1bd708e3 944 AliDebug(4, Form("Failed Track Update @ y[%7.2f] z[%7.2f] s2y[%f] s2z[%f] covyz[%f]", p[0], p[1], cov[0], cov[2], cov[1]));
eb2b4f91 945 break;
946 }
b72f4eaf 947 // fill residuals ?!
948 AliTracker::FillResiduals(&t, p, cov, ptrTracklet->GetVolumeId());
949
eb2b4f91 950
951 // load tracklet to the tracker
16cca13f 952 ptrTracklet->Update(&t);
eb2b4f91 953 ptrTracklet = SetTracklet(ptrTracklet);
17896e82 954 Int_t index(fTracklets->GetEntriesFast()-1);
955 t.SetTracklet(ptrTracklet, index);
eb2b4f91 956 n += ptrTracklet->GetN();
17896e82 957 AliDebug(2, Form("Setting Tracklet[%d] @ Idx[%d]", ily, index));
eb2b4f91 958
41702fec 959 // Reset material budget if 2 consecutive gold
eb2b4f91 960// if(ilayer>0 && t.GetTracklet(ilayer-1) && ptrTracklet->GetN() + t.GetTracklet(ilayer-1)->GetN() > 20) t.SetBudget(2, 0.);
41702fec 961
962 // Make backup of the track until is gold
963 // TO DO update quality check of the track.
964 // consider comparison with fTimeBinsRange
965 Float_t ratio0 = ptrTracklet->GetN() / Float_t(fgNTimeBins);
966 //Float_t ratio1 = Float_t(t.GetNumberOfClusters()+1) / Float_t(t.GetNExpected()+1);
41702fec 967
eb2b4f91 968 if( (chi2 < 18.0) &&
41702fec 969 (ratio0 > 0.8) &&
970 //(ratio1 > 0.6) &&
971 //(ratio0+ratio1 > 1.5) &&
972 (t.GetNCross() == 0) &&
973 (TMath::Abs(t.GetSnp()) < 0.85) &&
e3cf3d02 974 (t.GetNumberOfClusters() > 20)){
975 t.MakeBackupTrack();
976 }
053767a4 977 } // end layers loop
eb2b4f91 978 //printf("clusters[%d] chi2[%f] x[%f] status[%d ", n, t.GetChi2(), t.GetX(), t.GetStatusTRD());
979 //for(int i=0; i<6; i++) printf("%d ", t.GetStatusTRD(i)); printf("]\n");
41702fec 980
7c3eecb8 981 if(debugLevel > 1){
41702fec 982 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
eb2b4f91 983 AliTRDtrackV1 track(t);
984 track.SetOwner();
7c3eecb8 985 (*cstreamer) << "FollowBackProlongation0"
eb2b4f91 986 << "EventNumber=" << eventNumber
987 << "ncl=" << n
988 << "track.=" << &track
41702fec 989 << "\n";
990 }
991
eb2b4f91 992 return n;
0906e73e 993}
994
eb38ed55 995//_________________________________________________________________________
4d6aee34 996Float_t AliTRDtrackerV1::FitRieman(AliTRDseedV1 *tracklets, Double_t *chi2, Int_t *const planes){
41702fec 997 //
998 // Fits a Riemann-circle to the given points without tilting pad correction.
999 // The fit is performed using an instance of the class AliRieman (equations
1000 // and transformations see documentation of this class)
1001 // Afterwards all the tracklets are Updated
1002 //
1003 // Parameters: - Array of tracklets (AliTRDseedV1)
1004 // - Storage for the chi2 values (beginning with direction z)
1005 // - Seeding configuration
1006 // Output: - The curvature
1007 //
1008 AliRieman *fitter = AliTRDtrackerV1::GetRiemanFitter();
1009 fitter->Reset();
1010 Int_t allplanes[] = {0, 1, 2, 3, 4, 5};
1011 Int_t *ppl = &allplanes[0];
1012 Int_t maxLayers = 6;
1013 if(planes){
1014 maxLayers = 4;
1015 ppl = planes;
1016 }
1017 for(Int_t il = 0; il < maxLayers; il++){
1018 if(!tracklets[ppl[il]].IsOK()) continue;
e3cf3d02 1019 fitter->AddPoint(tracklets[ppl[il]].GetX0(), tracklets[ppl[il]].GetYfit(0), tracklets[ppl[il]].GetZfit(0),1,10);
41702fec 1020 }
1021 fitter->Update();
1022 // Set the reference position of the fit and calculate the chi2 values
1023 memset(chi2, 0, sizeof(Double_t) * 2);
1024 for(Int_t il = 0; il < maxLayers; il++){
1025 // Reference positions
1026 tracklets[ppl[il]].Init(fitter);
1027
1028 // chi2
1029 if((!tracklets[ppl[il]].IsOK()) && (!planes)) continue;
1030 chi2[0] += tracklets[ppl[il]].GetChi2Y();
1031 chi2[1] += tracklets[ppl[il]].GetChi2Z();
1032 }
1033 return fitter->GetC();
eb38ed55 1034}
1035
1036//_________________________________________________________________________
1037void AliTRDtrackerV1::FitRieman(AliTRDcluster **seedcl, Double_t chi2[2])
1038{
41702fec 1039 //
1040 // Performs a Riemann helix fit using the seedclusters as spacepoints
1041 // Afterwards the chi2 values are calculated and the seeds are updated
1042 //
1043 // Parameters: - The four seedclusters
1044 // - The tracklet array (AliTRDseedV1)
1045 // - The seeding configuration
1046 // - Chi2 array
1047 //
1048 // debug level 2
1049 //
1050 AliRieman *fitter = AliTRDtrackerV1::GetRiemanFitter();
1051 fitter->Reset();
a3743898 1052 for(Int_t i = 0; i < 4; i++){
a3743898 1053 fitter->AddPoint(seedcl[i]->GetX(), seedcl[i]->GetY(), seedcl[i]->GetZ(), 1., 10.);
1054 }
41702fec 1055 fitter->Update();
1056
1057
1058 // Update the seed and calculated the chi2 value
1059 chi2[0] = 0; chi2[1] = 0;
1060 for(Int_t ipl = 0; ipl < kNSeedPlanes; ipl++){
1061 // chi2
1062 chi2[0] += (seedcl[ipl]->GetZ() - fitter->GetZat(seedcl[ipl]->GetX())) * (seedcl[ipl]->GetZ() - fitter->GetZat(seedcl[ipl]->GetX()));
1063 chi2[1] += (seedcl[ipl]->GetY() - fitter->GetYat(seedcl[ipl]->GetX())) * (seedcl[ipl]->GetY() - fitter->GetYat(seedcl[ipl]->GetX()));
1064 }
eb38ed55 1065}
1066
1067
1068//_________________________________________________________________________
1069Float_t AliTRDtrackerV1::FitTiltedRiemanConstraint(AliTRDseedV1 *tracklets, Double_t zVertex)
1070{
41702fec 1071 //
1072 // Fits a helix to the clusters. Pad tilting is considered. As constraint it is
1073 // assumed that the vertex position is set to 0.
1074 // This method is very usefull for high-pt particles
1075 // Basis for the fit: (x - x0)^2 + (y - y0)^2 - R^2 = 0
1076 // x0, y0: Center of the circle
1077 // Measured y-position: ymeas = y - tan(phiT)(zc - zt)
1078 // zc: center of the pad row
1079 // Equation which has to be fitted (after transformation):
1080 // a + b * u + e * v + 2*(ymeas + tan(phiT)(z - zVertex))*t = 0
1081 // Transformation:
1082 // t = 1/(x^2 + y^2)
1083 // u = 2 * x * t
1084 // v = 2 * x * tan(phiT) * t
1085 // Parameters in the equation:
1086 // a = -1/y0, b = x0/y0, e = dz/dx
1087 //
1088 // The Curvature is calculated by the following equation:
1089 // - curv = a/Sqrt(b^2 + 1) = 1/R
1090 // Parameters: - the 6 tracklets
1091 // - the Vertex constraint
1092 // Output: - the Chi2 value of the track
1093 //
1094 // debug level 5
1095 //
1096
1097 TLinearFitter *fitter = GetTiltedRiemanFitterConstraint();
1098 fitter->StoreData(kTRUE);
1099 fitter->ClearPoints();
4d6aee34 1100 AliTRDcluster *cl = NULL;
41702fec 1101
1102 Float_t x, y, z, w, t, error, tilt;
1103 Double_t uvt[2];
1104 Int_t nPoints = 0;
053767a4 1105 for(Int_t ilr = 0; ilr < AliTRDgeometry::kNlayer; ilr++){
1106 if(!tracklets[ilr].IsOK()) continue;
8d2bec9e 1107 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
053767a4 1108 if(!tracklets[ilr].IsUsable(itb)) continue;
1109 cl = tracklets[ilr].GetClusters(itb);
1742f24c 1110 if(!cl->IsInChamber()) continue;
41702fec 1111 x = cl->GetX();
1112 y = cl->GetY();
1113 z = cl->GetZ();
053767a4 1114 tilt = tracklets[ilr].GetTilt();
41702fec 1115 // Transformation
1116 t = 1./(x * x + y * y);
1117 uvt[0] = 2. * x * t;
1118 uvt[1] = 2. * x * t * tilt ;
1119 w = 2. * (y + tilt * (z - zVertex)) * t;
78b7d24c 1120 error = 2. * TMath::Sqrt(cl->GetSigmaY2()+tilt*tilt*cl->GetSigmaZ2()) * t;
41702fec 1121 fitter->AddPoint(uvt, w, error);
1122 nPoints++;
1123 }
1124 }
e17f4785 1125 fitter->Eval();
41702fec 1126
1127 // Calculate curvature
1128 Double_t a = fitter->GetParameter(0);
1129 Double_t b = fitter->GetParameter(1);
1130 Double_t curvature = a/TMath::Sqrt(b*b + 1);
1131
1132 Float_t chi2track = fitter->GetChisquare()/Double_t(nPoints);
1133 for(Int_t ip = 0; ip < AliTRDtrackerV1::kNPlanes; ip++)
e3cf3d02 1134 tracklets[ip].SetC(curvature);
41702fec 1135
a2fbb6ec 1136/* if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker()) >= 5){
41702fec 1137 //Linear Model on z-direction
1138 Double_t xref = CalculateReferenceX(tracklets); // Relative to the middle of the stack
1139 Double_t slope = fitter->GetParameter(2);
1140 Double_t zref = slope * xref;
1141 Float_t chi2Z = CalculateChi2Z(tracklets, zref, slope, xref);
1142 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1143 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
4d6aee34 1144 TTreeSRedirector &treeStreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 1145 treeStreamer << "FitTiltedRiemanConstraint"
1146 << "EventNumber=" << eventNumber
1147 << "CandidateNumber=" << candidateNumber
1148 << "Curvature=" << curvature
1149 << "Chi2Track=" << chi2track
1150 << "Chi2Z=" << chi2Z
1151 << "zref=" << zref
1152 << "\n";
3a039a31 1153 }*/
41702fec 1154 return chi2track;
eb38ed55 1155}
1156
1157//_________________________________________________________________________
1158Float_t AliTRDtrackerV1::FitTiltedRieman(AliTRDseedV1 *tracklets, Bool_t sigError)
1159{
41702fec 1160 //
1161 // Performs a Riemann fit taking tilting pad correction into account
1162 // The equation of a Riemann circle, where the y position is substituted by the
1163 // measured y-position taking pad tilting into account, has to be transformed
1164 // into a 4-dimensional hyperplane equation
1165 // Riemann circle: (x-x0)^2 + (y-y0)^2 -R^2 = 0
1166 // Measured y-Position: ymeas = y - tan(phiT)(zc - zt)
1167 // zc: center of the pad row
1168 // zt: z-position of the track
1169 // The z-position of the track is assumed to be linear dependent on the x-position
1170 // Transformed equation: a + b * u + c * t + d * v + e * w - 2 * (ymeas + tan(phiT) * zc) * t = 0
1171 // Transformation: u = 2 * x * t
1172 // v = 2 * tan(phiT) * t
1173 // w = 2 * tan(phiT) * (x - xref) * t
1174 // t = 1 / (x^2 + ymeas^2)
1175 // Parameters: a = -1/y0
1176 // b = x0/y0
1177 // c = (R^2 -x0^2 - y0^2)/y0
1178 // d = offset
1179 // e = dz/dx
1180 // If the offset respectively the slope in z-position is impossible, the parameters are fixed using
1181 // results from the simple riemann fit. Afterwards the fit is redone.
1182 // The curvature is calculated according to the formula:
1183 // curv = a/(1 + b^2 + c*a) = 1/R
1184 //
1185 // Paramters: - Array of tracklets (connected to the track candidate)
1186 // - Flag selecting the error definition
1187 // Output: - Chi2 values of the track (in Parameter list)
1188 //
1189 TLinearFitter *fitter = GetTiltedRiemanFitter();
1190 fitter->StoreData(kTRUE);
1191 fitter->ClearPoints();
1192 AliTRDLeastSquare zfitter;
4d6aee34 1193 AliTRDcluster *cl = NULL;
41702fec 1194
1195 Double_t xref = CalculateReferenceX(tracklets);
6e39bde4 1196 Double_t x, y, z, t, tilt, dx, w, we, erry, errz;
1197 Double_t uvt[4], sumPolY[5], sumPolZ[3];
1198 memset(sumPolY, 0, sizeof(Double_t) * 5);
1199 memset(sumPolZ, 0, sizeof(Double_t) * 3);
41702fec 1200 Int_t nPoints = 0;
1201 // Containers for Least-square fitter
1202 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1203 if(!tracklets[ipl].IsOK()) continue;
ed15ef4f 1204 tilt = tracklets[ipl].GetTilt();
8d2bec9e 1205 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
41702fec 1206 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1742f24c 1207 if(!cl->IsInChamber()) continue;
41702fec 1208 if (!tracklets[ipl].IsUsable(itb)) continue;
1209 x = cl->GetX();
1210 y = cl->GetY();
1211 z = cl->GetZ();
41702fec 1212 dx = x - xref;
1213 // Transformation
1214 t = 1./(x*x + y*y);
1215 uvt[0] = 2. * x * t;
1216 uvt[1] = t;
1217 uvt[2] = 2. * tilt * t;
1218 uvt[3] = 2. * tilt * dx * t;
1219 w = 2. * (y + tilt*z) * t;
1220 // error definition changes for the different calls
1221 we = 2. * t;
78b7d24c 1222 we *= sigError ? TMath::Sqrt(cl->GetSigmaY2()+tilt*tilt*cl->GetSigmaZ2()) : 0.2;
41702fec 1223 fitter->AddPoint(uvt, w, we);
1224 zfitter.AddPoint(&x, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
6e39bde4 1225 // adding points for covariance matrix estimation
1226 erry = 1./(TMath::Sqrt(cl->GetSigmaY2()) + 0.1); // 0.1 is a systematic error (due to misalignment and miscalibration)
1227 erry *= erry;
1228 errz = 1./cl->GetSigmaZ2();
1229 for(Int_t ipol = 0; ipol < 5; ipol++){
1230 sumPolY[ipol] += erry;
1231 erry *= x;
1232 if(ipol < 3){
1233 sumPolZ[ipol] += errz;
1234 errz *= x;
1235 }
1236 }
41702fec 1237 nPoints++;
1238 }
1239 }
e17f4785 1240 fitter->Eval();
41702fec 1241 zfitter.Eval();
1242
1243 Double_t offset = fitter->GetParameter(3);
1244 Double_t slope = fitter->GetParameter(4);
1245
1246 // Linear fitter - not possible to make boundaries
1247 // Do not accept non possible z and dzdx combinations
1248 Bool_t acceptablez = kTRUE;
1249 Double_t zref = 0.0;
1250 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
1251 if(!tracklets[iLayer].IsOK()) continue;
1252 zref = offset + slope * (tracklets[iLayer].GetX0() - xref);
e3cf3d02 1253 if (TMath::Abs(tracklets[iLayer].GetZfit(0) - zref) > tracklets[iLayer].GetPadLength() * 0.5 + 1.0)
41702fec 1254 acceptablez = kFALSE;
1255 }
1256 if (!acceptablez) {
1257 Double_t dzmf = zfitter.GetFunctionParameter(1);
1258 Double_t zmf = zfitter.GetFunctionValue(&xref);
1259 fgTiltedRieman->FixParameter(3, zmf);
1260 fgTiltedRieman->FixParameter(4, dzmf);
e17f4785 1261 fitter->Eval();
41702fec 1262 fitter->ReleaseParameter(3);
1263 fitter->ReleaseParameter(4);
1264 offset = fitter->GetParameter(3);
1265 slope = fitter->GetParameter(4);
1266 }
1267
1268 // Calculate Curvarture
1269 Double_t a = fitter->GetParameter(0);
1270 Double_t b = fitter->GetParameter(1);
1271 Double_t c = fitter->GetParameter(2);
1272 Double_t curvature = 1.0 + b*b - c*a;
1273 if (curvature > 0.0)
1274 curvature = a / TMath::Sqrt(curvature);
1275
1276 Double_t chi2track = fitter->GetChisquare()/Double_t(nPoints);
1277
6e39bde4 1278 // Prepare error calculation
1279 TMatrixD covarPolY(3,3);
1280 covarPolY(0,0) = sumPolY[0]; covarPolY(1,1) = sumPolY[2]; covarPolY(2,2) = sumPolY[4];
1281 covarPolY(0,1) = covarPolY(1,0) = sumPolY[1];
1282 covarPolY(0,2) = covarPolY(2,0) = sumPolY[2];
1283 covarPolY(2,1) = covarPolY(1,2) = sumPolY[3];
1284 covarPolY.Invert();
1285 TMatrixD covarPolZ(2,2);
1286 covarPolZ(0,0) = sumPolZ[0]; covarPolZ(1,1) = sumPolZ[2];
1287 covarPolZ(1,0) = covarPolZ(0,1) = sumPolZ[1];
1288 covarPolZ.Invert();
1289
41702fec 1290 // Update the tracklets
6e39bde4 1291 Double_t x1, dy, dz;
1292 Double_t cov[15];
1293 memset(cov, 0, sizeof(Double_t) * 15);
41702fec 1294 for(Int_t iLayer = 0; iLayer < AliTRDtrackerV1::kNPlanes; iLayer++) {
1295
1296 x = tracklets[iLayer].GetX0();
6e39bde4 1297 x1 = x - xref;
41702fec 1298 y = 0;
1299 z = 0;
1300 dy = 0;
1301 dz = 0;
6e39bde4 1302 memset(cov, 0, sizeof(Double_t) * 3);
1303 TMatrixD transform(3,3);
1304 transform(0,0) = 1;
1305 transform(0,1) = x;
1306 transform(0,2) = x*x;
1307 transform(1,1) = 1;
1308 transform(1,2) = x;
1309 transform(2,2) = 1;
1310 TMatrixD covariance(transform, TMatrixD::kMult, covarPolY);
1311 covariance *= transform.T();
1312 TMatrixD transformZ(2,2);
1313 transformZ(0,0) = transformZ(1,1) = 1;
1314 transformZ(0,1) = x;
1315 TMatrixD covarZ(transformZ, TMatrixD::kMult, covarPolZ);
1316 covarZ *= transformZ.T();
41702fec 1317 // y: R^2 = (x - x0)^2 + (y - y0)^2
1318 // => y = y0 +/- Sqrt(R^2 - (x - x0)^2)
1319 // R = Sqrt() = 1/Curvature
1320 // => y = y0 +/- Sqrt(1/Curvature^2 - (x - x0)^2)
1321 Double_t res = (x * a + b); // = (x - x0)/y0
1322 res *= res;
1323 res = 1.0 - c * a + b * b - res; // = (R^2 - (x - x0)^2)/y0^2
1324 if (res >= 0) {
1325 res = TMath::Sqrt(res);
1326 y = (1.0 - res) / a;
1327 }
6e39bde4 1328 cov[0] = covariance(0,0);
1329 cov[2] = covarZ(0,0);
1330 cov[1] = 0.;
41702fec 1331
1332 // dy: R^2 = (x - x0)^2 + (y - y0)^2
1333 // => y = +/- Sqrt(R^2 - (x - x0)^2) + y0
1334 // => dy/dx = (x - x0)/Sqrt(R^2 - (x - x0)^2)
1335 // Curvature: cr = 1/R = a/Sqrt(1 + b^2 - c*a)
1336 // => dy/dx = (x - x0)/(1/(cr^2) - (x - x0)^2)
1337 Double_t x0 = -b / a;
1338 if (-c * a + b * b + 1 > 0) {
1339 if (1.0/(curvature * curvature) - (x - x0) * (x - x0) > 0.0) {
6e39bde4 1340 Double_t yderiv = (x - x0) / TMath::Sqrt(1.0/(curvature * curvature) - (x - x0) * (x - x0));
1341 if (a < 0) yderiv *= -1.0;
1342 dy = yderiv;
41702fec 1343 }
1344 }
1345 z = offset + slope * (x - xref);
1346 dz = slope;
1347 tracklets[iLayer].SetYref(0, y);
1348 tracklets[iLayer].SetYref(1, dy);
1349 tracklets[iLayer].SetZref(0, z);
1350 tracklets[iLayer].SetZref(1, dz);
1351 tracklets[iLayer].SetC(curvature);
6e39bde4 1352 tracklets[iLayer].SetCovRef(cov);
41702fec 1353 tracklets[iLayer].SetChi2(chi2track);
1354 }
1355
a2fbb6ec 1356/* if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) >=5){
1357 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
41702fec 1358 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1359 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
1360 Double_t chi2z = CalculateChi2Z(tracklets, offset, slope, xref);
1361 cstreamer << "FitTiltedRieman0"
1362 << "EventNumber=" << eventNumber
1363 << "CandidateNumber=" << candidateNumber
1364 << "xref=" << xref
1365 << "Chi2Z=" << chi2z
1366 << "\n";
3a039a31 1367 }*/
41702fec 1368 return chi2track;
eb38ed55 1369}
1370
3b57a3f7 1371
9e333711 1372//____________________________________________________________________
6e4d4425 1373Double_t AliTRDtrackerV1::FitLine(const AliTRDtrackV1 *track, AliTRDseedV1 *tracklets, Bool_t err, Int_t np, AliTrackPoint *points)
9e333711 1374{
4d6aee34 1375 //
1376 // Fit track with a staight line
1377 // Fills an AliTrackPoint array with np points
1378 // Function should be used to refit tracks when no magnetic field was on
1379 //
9e333711 1380 AliTRDLeastSquare yfitter, zfitter;
4d6aee34 1381 AliTRDcluster *cl = NULL;
9e333711 1382
4d6aee34 1383 AliTRDseedV1 work[kNPlanes], *tracklet = NULL;
9e333711 1384 if(!tracklets){
1385 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1386 if(!(tracklet = track->GetTracklet(ipl))) continue;
1387 if(!tracklet->IsOK()) continue;
1388 new(&work[ipl]) AliTRDseedV1(*tracklet);
1389 }
1390 tracklets = &work[0];
1391 }
1392
1393 Double_t xref = CalculateReferenceX(tracklets);
1394 Double_t x, y, z, dx, ye, yr, tilt;
1395 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1396 if(!tracklets[ipl].IsOK()) continue;
1397 for(Int_t itb = 0; itb < fgNTimeBins; itb++){
1398 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1399 if (!tracklets[ipl].IsUsable(itb)) continue;
1400 x = cl->GetX();
1401 z = cl->GetZ();
1402 dx = x - xref;
1403 zfitter.AddPoint(&dx, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
1404 }
1405 }
1406 zfitter.Eval();
1407 Double_t z0 = zfitter.GetFunctionParameter(0);
1408 Double_t dzdx = zfitter.GetFunctionParameter(1);
1409 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1410 if(!tracklets[ipl].IsOK()) continue;
1411 for(Int_t itb = 0; itb < fgNTimeBins; itb++){
1412 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1413 if (!tracklets[ipl].IsUsable(itb)) continue;
1414 x = cl->GetX();
1415 y = cl->GetY();
1416 z = cl->GetZ();
1417 tilt = tracklets[ipl].GetTilt();
1418 dx = x - xref;
1419 yr = y + tilt*(z - z0 - dzdx*dx);
1420 // error definition changes for the different calls
1421 ye = tilt*TMath::Sqrt(cl->GetSigmaZ2());
1422 ye += err ? tracklets[ipl].GetSigmaY() : 0.2;
1423 yfitter.AddPoint(&dx, yr, ye);
1424 }
1425 }
1426 yfitter.Eval();
1427 Double_t y0 = yfitter.GetFunctionParameter(0);
1428 Double_t dydx = yfitter.GetFunctionParameter(1);
1429 Double_t chi2 = 0.;//yfitter.GetChisquare()/Double_t(nPoints);
1430
1431 //update track points array
1432 if(np && points){
1433 Float_t xyz[3];
1434 for(int ip=0; ip<np; ip++){
1435 points[ip].GetXYZ(xyz);
1436 xyz[1] = y0 + dydx * (xyz[0] - xref);
1437 xyz[2] = z0 + dzdx * (xyz[0] - xref);
1438 points[ip].SetXYZ(xyz);
1439 }
1440 }
1441 return chi2;
1442}
1443
1444
3b57a3f7 1445//_________________________________________________________________________
6e4d4425 1446Double_t AliTRDtrackerV1::FitRiemanTilt(const AliTRDtrackV1 *track, AliTRDseedV1 *tracklets, Bool_t sigError, Int_t np, AliTrackPoint *points)
3b57a3f7 1447{
0fa1a8ee 1448//
1449// Performs a Riemann fit taking tilting pad correction into account
1450//
1451// Paramters: - Array of tracklets (connected to the track candidate)
1452// - Flag selecting the error definition
1453// Output: - Chi2 values of the track (in Parameter list)
1454//
1455// The equations which has to be solved simultaneously are:
1456// BEGIN_LATEX
1457// R^{2} = (x-x_{0})^{2} + (y^{*}-y_{0})^{2}
1458// y^{*} = y - tg(h)(z - z_{t})
1459// z_{t} = z_{0}+dzdx*(x-x_{r})
1460// END_LATEX
1461// with (x, y, z) the coordinate of the cluster, (x_0, y_0, z_0) the coordinate of the center of the Riemann circle,
1462// R its radius, x_r a constant refrence radial position in the middle of the TRD stack and dzdx the slope of the
1463// track in the x-z plane. Using the following transformations
1464// BEGIN_LATEX
1465// t = 1 / (x^{2} + y^{2})
1466// u = 2 * x * t
1467// v = 2 * tan(h) * t
1468// w = 2 * tan(h) * (x - x_{r}) * t
1469// END_LATEX
1470// One gets the following linear equation
1471// BEGIN_LATEX
1472// a + b * u + c * t + d * v + e * w = 2 * (y + tg(h) * z) * t
1473// END_LATEX
1474// where the coefficients have the following meaning
1475// BEGIN_LATEX
1476// a = -1/y_{0}
1477// b = x_{0}/y_{0}
1478// c = (R^{2} -x_{0}^{2} - y_{0}^{2})/y_{0}
1479// d = z_{0}
1480// e = dz/dx
1481// END_LATEX
1482// The error calculation for the free term is thus
1483// BEGIN_LATEX
1484// #sigma = 2 * #sqrt{#sigma^{2}_{y} + (tilt corr ...) + tg^{2}(h) * #sigma^{2}_{z}} * t
1485// END_LATEX
1486//
1487// From this simple model one can compute chi^2 estimates and a rough approximation of pt from the curvature according
1488// to the formula:
1489// BEGIN_LATEX
1490// C = 1/R = a/(1 + b^{2} + c*a)
1491// END_LATEX
1492//
1493// Authors
1494// M.Ivanov <M.Ivanov@gsi.de>
1495// A.Bercuci <A.Bercuci@gsi.de>
1496// M.Fasel <M.Fasel@gsi.de>
1497
41702fec 1498 TLinearFitter *fitter = GetTiltedRiemanFitter();
1499 fitter->StoreData(kTRUE);
1500 fitter->ClearPoints();
1501 AliTRDLeastSquare zfitter;
4d6aee34 1502 AliTRDcluster *cl = NULL;
3b57a3f7 1503
4d6aee34 1504 AliTRDseedV1 work[kNPlanes], *tracklet = NULL;
3b57a3f7 1505 if(!tracklets){
1506 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1507 if(!(tracklet = track->GetTracklet(ipl))) continue;
1508 if(!tracklet->IsOK()) continue;
1509 new(&work[ipl]) AliTRDseedV1(*tracklet);
1510 }
1511 tracklets = &work[0];
1512 }
1513
41702fec 1514 Double_t xref = CalculateReferenceX(tracklets);
1515 Double_t x, y, z, t, tilt, dx, w, we;
1516 Double_t uvt[4];
1517 Int_t nPoints = 0;
1518 // Containers for Least-square fitter
1519 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1520 if(!tracklets[ipl].IsOK()) continue;
8d2bec9e 1521 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
41702fec 1522 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1523 if (!tracklets[ipl].IsUsable(itb)) continue;
1524 x = cl->GetX();
1525 y = cl->GetY();
1526 z = cl->GetZ();
1527 tilt = tracklets[ipl].GetTilt();
1528 dx = x - xref;
1529 // Transformation
1530 t = 1./(x*x + y*y);
1531 uvt[0] = 2. * x * t;
1532 uvt[1] = t;
1533 uvt[2] = 2. * tilt * t;
1534 uvt[3] = 2. * tilt * dx * t;
1535 w = 2. * (y + tilt*z) * t;
1536 // error definition changes for the different calls
1537 we = 2. * t;
f29f13a6 1538 we *= sigError ? TMath::Sqrt(cl->GetSigmaY2()) : 0.2;
41702fec 1539 fitter->AddPoint(uvt, w, we);
1540 zfitter.AddPoint(&x, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
1541 nPoints++;
1542 }
1543 }
aec26713 1544 if(fitter->Eval()) return 1.E10;
1545
41702fec 1546 Double_t z0 = fitter->GetParameter(3);
1547 Double_t dzdx = fitter->GetParameter(4);
3b57a3f7 1548
1549
1550 // Linear fitter - not possible to make boundaries
1551 // Do not accept non possible z and dzdx combinations
1552 Bool_t accept = kTRUE;
1553 Double_t zref = 0.0;
1554 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
1555 if(!tracklets[iLayer].IsOK()) continue;
1556 zref = z0 + dzdx * (tracklets[iLayer].GetX0() - xref);
e3cf3d02 1557 if (TMath::Abs(tracklets[iLayer].GetZfit(0) - zref) > tracklets[iLayer].GetPadLength() * 0.5 + 1.0)
3b57a3f7 1558 accept = kFALSE;
1559 }
1560 if (!accept) {
41702fec 1561 zfitter.Eval();
3b57a3f7 1562 Double_t dzmf = zfitter.GetFunctionParameter(1);
1563 Double_t zmf = zfitter.GetFunctionValue(&xref);
1564 fitter->FixParameter(3, zmf);
1565 fitter->FixParameter(4, dzmf);
e17f4785 1566 fitter->Eval();
3b57a3f7 1567 fitter->ReleaseParameter(3);
1568 fitter->ReleaseParameter(4);
1569 z0 = fitter->GetParameter(3); // = zmf ?
1570 dzdx = fitter->GetParameter(4); // = dzmf ?
1571 }
1572
1573 // Calculate Curvature
1574 Double_t a = fitter->GetParameter(0);
1575 Double_t b = fitter->GetParameter(1);
1576 Double_t c = fitter->GetParameter(2);
1577 Double_t y0 = 1. / a;
1578 Double_t x0 = -b * y0;
a015e406 1579 Double_t tmp = y0*y0 + x0*x0 - c*y0;
1580 if(tmp<=0.) return 1.E10;
4d6aee34 1581 Double_t radius = TMath::Sqrt(tmp);
1582 Double_t curvature = 1.0 + b*b - c*a;
1583 if (curvature > 0.0) curvature = a / TMath::Sqrt(curvature);
3b57a3f7 1584
1585 // Calculate chi2 of the fit
1586 Double_t chi2 = fitter->GetChisquare()/Double_t(nPoints);
1587
1588 // Update the tracklets
1589 if(!track){
1590 for(Int_t ip = 0; ip < kNPlanes; ip++) {
1591 x = tracklets[ip].GetX0();
4d6aee34 1592 tmp = radius*radius-(x-x0)*(x-x0);
a015e406 1593 if(tmp <= 0.) continue;
1594 tmp = TMath::Sqrt(tmp);
3b57a3f7 1595
1596 // y: R^2 = (x - x0)^2 + (y - y0)^2
1597 // => y = y0 +/- Sqrt(R^2 - (x - x0)^2)
1598 tracklets[ip].SetYref(0, y0 - (y0>0.?1.:-1)*tmp);
1599 // => dy/dx = (x - x0)/Sqrt(R^2 - (x - x0)^2)
1600 tracklets[ip].SetYref(1, (x - x0) / tmp);
1601 tracklets[ip].SetZref(0, z0 + dzdx * (x - xref));
1602 tracklets[ip].SetZref(1, dzdx);
4d6aee34 1603 tracklets[ip].SetC(curvature);
3b57a3f7 1604 tracklets[ip].SetChi2(chi2);
1605 }
1606 }
3b57a3f7 1607 //update track points array
1608 if(np && points){
1609 Float_t xyz[3];
1610 for(int ip=0; ip<np; ip++){
1611 points[ip].GetXYZ(xyz);
4d6aee34 1612 xyz[1] = TMath::Abs(xyz[0] - x0) > radius ? 100. : y0 - (y0>0.?1.:-1.)*TMath::Sqrt((radius-(xyz[0]-x0))*(radius+(xyz[0]-x0)));
3b57a3f7 1613 xyz[2] = z0 + dzdx * (xyz[0] - xref);
1614 points[ip].SetXYZ(xyz);
1615 }
1616 }
1617
3b57a3f7 1618 return chi2;
1619}
1620
1621
1bf51039 1622//____________________________________________________________________
e17f4785 1623Double_t AliTRDtrackerV1::FitKalman(AliTRDtrackV1 *track, AliTRDseedV1 * const tracklets, Bool_t up, Int_t np, AliTrackPoint *points)
1bf51039 1624{
1625// Kalman filter implementation for the TRD.
1626// It returns the positions of the fit in the array "points"
1627//
1628// Author : A.Bercuci@gsi.de
1629
3cfaffa4 1630 // printf("Start track @ x[%f]\n", track->GetX());
1bf51039 1631
1632 //prepare marker points along the track
1633 Int_t ip = np ? 0 : 1;
1634 while(ip<np){
1635 if((up?-1:1) * (track->GetX() - points[ip].GetX()) > 0.) break;
1636 //printf("AliTRDtrackerV1::FitKalman() : Skip track marker x[%d] = %7.3f. Before track start ( %7.3f ).\n", ip, points[ip].GetX(), track->GetX());
1637 ip++;
1638 }
1639 //if(points) printf("First marker point @ x[%d] = %f\n", ip, points[ip].GetX());
1640
1641
4d6aee34 1642 AliTRDseedV1 tracklet, *ptrTracklet = NULL;
1bf51039 1643
1644 //Loop through the TRD planes
1645 for (Int_t jplane = 0; jplane < kNPlanes; jplane++) {
1646 // GET TRACKLET OR BUILT IT
1647 Int_t iplane = up ? jplane : kNPlanes - 1 - jplane;
1648 if(tracklets){
e17f4785 1649 if(!(ptrTracklet = &tracklets[iplane])) continue;
1bf51039 1650 }else{
1651 if(!(ptrTracklet = track->GetTracklet(iplane))){
4d6aee34 1652 /*AliTRDtrackerV1 *tracker = NULL;
a2fbb6ec 1653 if(!(tracker = dynamic_cast<AliTRDtrackerV1*>( AliTRDrecoParam:Tracker()))) continue;
1bf51039 1654 ptrTracklet = new(&tracklet) AliTRDseedV1(iplane);
1655 if(!tracker->MakeTracklet(ptrTracklet, track)) */
1656 continue;
1657 }
1658 }
1659 if(!ptrTracklet->IsOK()) continue;
1660
1661 Double_t x = ptrTracklet->GetX0();
1662
1663 while(ip < np){
1664 //don't do anything if next marker is after next update point.
1665 if((up?-1:1) * (points[ip].GetX() - x) - fgkMaxStep < 0) break;
1bf51039 1666 if(((up?-1:1) * (points[ip].GetX() - track->GetX()) < 0) && !PropagateToX(*track, points[ip].GetX(), fgkMaxStep)) return -1.;
1667
1668 Double_t xyz[3]; // should also get the covariance
3cfaffa4 1669 track->GetXYZ(xyz);
1670 track->Global2LocalPosition(xyz, track->GetAlpha());
1671 points[ip].SetXYZ(xyz[0], xyz[1], xyz[2]);
1bf51039 1672 ip++;
1673 }
3cfaffa4 1674 // printf("plane[%d] tracklet[%p] x[%f]\n", iplane, ptrTracklet, x);
1bf51039 1675
3cfaffa4 1676 // Propagate closer to the next update point
1bf51039 1677 if(((up?-1:1) * (x - track->GetX()) + fgkMaxStep < 0) && !PropagateToX(*track, x + (up?-1:1)*fgkMaxStep, fgkMaxStep)) return -1.;
1678
1679 if(!AdjustSector(track)) return -1;
1680 if(TMath::Abs(track->GetSnp()) > fgkMaxSnp) return -1;
1681
1682 //load tracklet to the tracker and the track
1683/* Int_t index;
1684 if((index = FindTracklet(ptrTracklet)) < 0){
1685 ptrTracklet = SetTracklet(&tracklet);
1686 index = fTracklets->GetEntriesFast()-1;
1687 }
1688 track->SetTracklet(ptrTracklet, index);*/
1689
1690
1691 // register tracklet to track with tracklet creation !!
1692 // PropagateBack : loaded tracklet to the tracker and update index
1693 // RefitInward : update index
1694 // MakeTrack : loaded tracklet to the tracker and update index
1695 if(!tracklets) track->SetTracklet(ptrTracklet, -1);
1696
1697
1698 //Calculate the mean material budget along the path inside the chamber
1699 Double_t xyz0[3]; track->GetXYZ(xyz0);
1700 Double_t alpha = track->GetAlpha();
1701 Double_t xyz1[3], y, z;
1702 if(!track->GetProlongation(x, y, z)) return -1;
1703 xyz1[0] = x * TMath::Cos(alpha) - y * TMath::Sin(alpha);
1704 xyz1[1] = +x * TMath::Sin(alpha) + y * TMath::Cos(alpha);
1705 xyz1[2] = z;
3cfaffa4 1706 if((xyz0[0] - xyz1[9] < 1e-3) && (xyz0[0] - xyz1[9] < 1e-3)) continue; // check wheter we are at the same global x position
1bf51039 1707 Double_t param[7];
3cfaffa4 1708 if(AliTracker::MeanMaterialBudget(xyz0, xyz1, param) <=0.) break;
1bf51039 1709 Double_t xrho = param[0]*param[4]; // density*length
1710 Double_t xx0 = param[1]; // radiation length
1711
1712 //Propagate the track
1713 track->PropagateTo(x, xx0, xrho);
1714 if (!AdjustSector(track)) break;
1715
1716 //Update track
b72f4eaf 1717 Double_t cov[3]; ptrTracklet->GetCovAt(x, cov);
1718 Double_t p[2] = { ptrTracklet->GetY(), ptrTracklet->GetZ()};
1719 Double_t chi2 = ((AliExternalTrackParam*)track)->GetPredictedChi2(p, cov);
1720 if(chi2<1e+10) track->Update(p, cov, chi2);
1bf51039 1721 if(!up) continue;
1722
1723 //Reset material budget if 2 consecutive gold
1724 if(iplane>0 && track->GetTracklet(iplane-1) && ptrTracklet->GetN() + track->GetTracklet(iplane-1)->GetN() > 20) track->SetBudget(2, 0.);
1725 } // end planes loop
1726
1727 // extrapolation
1728 while(ip < np){
1729 if(((up?-1:1) * (points[ip].GetX() - track->GetX()) < 0) && !PropagateToX(*track, points[ip].GetX(), fgkMaxStep)) return -1.;
1730
1731 Double_t xyz[3]; // should also get the covariance
3cfaffa4 1732 track->GetXYZ(xyz);
1733 track->Global2LocalPosition(xyz, track->GetAlpha());
1734 points[ip].SetXYZ(xyz[0], xyz[1], xyz[2]);
1bf51039 1735 ip++;
1736 }
1737
1738 return track->GetChi2();
1739}
3b57a3f7 1740
eb38ed55 1741//_________________________________________________________________________
bb56afff 1742Float_t AliTRDtrackerV1::CalculateChi2Z(AliTRDseedV1 *tracklets, Double_t offset, Double_t slope, Double_t xref)
eb38ed55 1743{
41702fec 1744 //
1745 // Calculates the chi2-value of the track in z-Direction including tilting pad correction.
1746 // A linear dependence on the x-value serves as a model.
1747 // The parameters are related to the tilted Riemann fit.
1748 // Parameters: - Array of tracklets (AliTRDseedV1) related to the track candidate
1749 // - the offset for the reference x
1750 // - the slope
1751 // - the reference x position
1752 // Output: - The Chi2 value of the track in z-Direction
1753 //
1754 Float_t chi2Z = 0, nLayers = 0;
053767a4 1755 for (Int_t iLayer = 0; iLayer < AliTRDgeometry::kNlayer; iLayer++) {
41702fec 1756 if(!tracklets[iLayer].IsOK()) continue;
1757 Double_t z = offset + slope * (tracklets[iLayer].GetX0() - xref);
e3cf3d02 1758 chi2Z += TMath::Abs(tracklets[iLayer].GetZfit(0) - z);
41702fec 1759 nLayers++;
1760 }
1761 chi2Z /= TMath::Max((nLayers - 3.0),1.0);
1762 return chi2Z;
eb38ed55 1763}
1764
bccda319 1765//_____________________________________________________________________________
1766Int_t AliTRDtrackerV1::PropagateToX(AliTRDtrackV1 &t, Double_t xToGo, Double_t maxStep)
1767{
41702fec 1768 //
1769 // Starting from current X-position of track <t> this function
1770 // extrapolates the track up to radial position <xToGo>.
1771 // Returns 1 if track reaches the plane, and 0 otherwise
1772 //
bccda319 1773
41702fec 1774 const Double_t kEpsilon = 0.00001;
bccda319 1775
41702fec 1776 // Current track X-position
1777 Double_t xpos = t.GetX();
bccda319 1778
41702fec 1779 // Direction: inward or outward
1780 Double_t dir = (xpos < xToGo) ? 1.0 : -1.0;
bccda319 1781
41702fec 1782 while (((xToGo - xpos) * dir) > kEpsilon) {
bccda319 1783
41702fec 1784 Double_t xyz0[3];
1785 Double_t xyz1[3];
1786 Double_t param[7];
1787 Double_t x;
1788 Double_t y;
1789 Double_t z;
bccda319 1790
41702fec 1791 // The next step size
1792 Double_t step = dir * TMath::Min(TMath::Abs(xToGo-xpos),maxStep);
bccda319 1793
41702fec 1794 // Get the global position of the starting point
1795 t.GetXYZ(xyz0);
bccda319 1796
41702fec 1797 // X-position after next step
1798 x = xpos + step;
bccda319 1799
41702fec 1800 // Get local Y and Z at the X-position of the next step
3352b455 1801 if(t.GetProlongation(x,y,z)<0) return 0; // No prolongation possible
bccda319 1802
41702fec 1803 // The global position of the end point of this prolongation step
1804 xyz1[0] = x * TMath::Cos(t.GetAlpha()) - y * TMath::Sin(t.GetAlpha());
1805 xyz1[1] = +x * TMath::Sin(t.GetAlpha()) + y * TMath::Cos(t.GetAlpha());
1806 xyz1[2] = z;
bccda319 1807
41702fec 1808 // Calculate the mean material budget between start and
1809 // end point of this prolongation step
83dea92e 1810 if(AliTracker::MeanMaterialBudget(xyz0, xyz1, param)<=0.) return 0;
bccda319 1811
41702fec 1812 // Propagate the track to the X-position after the next step
9c87a076 1813 if (!t.PropagateTo(x, param[1], param[0]*param[4])) return 0;
bccda319 1814
41702fec 1815 // Rotate the track if necessary
1816 AdjustSector(&t);
bccda319 1817
41702fec 1818 // New track X-position
1819 xpos = t.GetX();
bccda319 1820
41702fec 1821 }
bccda319 1822
41702fec 1823 return 1;
bccda319 1824
1825}
1826
eb38ed55 1827
1828//_____________________________________________________________________________
1829Int_t AliTRDtrackerV1::ReadClusters(TClonesArray* &array, TTree *clusterTree) const
1830{
41702fec 1831 //
1832 // Reads AliTRDclusters from the file.
1833 // The names of the cluster tree and branches
1834 // should match the ones used in AliTRDclusterizer::WriteClusters()
1835 //
1836
1837 Int_t nsize = Int_t(clusterTree->GetTotBytes() / (sizeof(AliTRDcluster)));
1838 TObjArray *clusterArray = new TObjArray(nsize+1000);
1839
1840 TBranch *branch = clusterTree->GetBranch("TRDcluster");
1841 if (!branch) {
1842 AliError("Can't get the branch !");
1843 return 1;
1844 }
1845 branch->SetAddress(&clusterArray);
1846
1847 if(!fClusters){
4d6aee34 1848 Float_t nclusters = fkReconstructor->GetRecoParam()->GetNClusters();
1849 if(fkReconstructor->IsHLT()) nclusters /= AliTRDgeometry::kNsector;
8ae98148 1850 array = new TClonesArray("AliTRDcluster", Int_t(nclusters));
41702fec 1851 array->SetOwner(kTRUE);
1852 }
1853
1854 // Loop through all entries in the tree
1855 Int_t nEntries = (Int_t) clusterTree->GetEntries();
1856 Int_t nbytes = 0;
1857 Int_t ncl = 0;
4d6aee34 1858 AliTRDcluster *c = NULL;
41702fec 1859 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
1860 // Import the tree
1861 nbytes += clusterTree->GetEvent(iEntry);
1862
1863 // Get the number of points in the detector
1864 Int_t nCluster = clusterArray->GetEntriesFast();
1865 for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) {
1866 if(!(c = (AliTRDcluster *) clusterArray->UncheckedAt(iCluster))) continue;
41702fec 1867 new((*fClusters)[ncl++]) AliTRDcluster(*c);
1868 delete (clusterArray->RemoveAt(iCluster));
1869 }
1870
1871 }
1872 delete clusterArray;
1873
1874 return 0;
eb38ed55 1875}
1876
1877//_____________________________________________________________________________
1878Int_t AliTRDtrackerV1::LoadClusters(TTree *cTree)
1879{
41702fec 1880 //
66f6bfd9 1881 // Fills clusters into TRD tracking sectors
41702fec 1882 //
41702fec 1883
4d6aee34 1884 if(!fkReconstructor->IsWritingClusters()){
48f8adf3 1885 fClusters = AliTRDReconstructor::GetClusters();
1886 } else {
66f6bfd9 1887 if (ReadClusters(fClusters, cTree)) {
1888 AliError("Problem with reading the clusters !");
1889 return 1;
1890 }
1891 }
1892 SetClustersOwner();
1893
48f8adf3 1894 if(!fClusters || !fClusters->GetEntriesFast()){
66f6bfd9 1895 AliInfo("No TRD clusters");
41702fec 1896 return 1;
1897 }
66f6bfd9 1898
1899 //Int_t nin =
1900 BuildTrackingContainers();
1901
1902 //Int_t ncl = fClusters->GetEntriesFast();
1903 //AliInfo(Form("Clusters %d [%6.2f %% in the active volume]", ncl, 100.*float(nin)/ncl));
1904
1905 return 0;
1906}
1907
1908//_____________________________________________________________________________
4d6aee34 1909Int_t AliTRDtrackerV1::LoadClusters(TClonesArray * const clusters)
66f6bfd9 1910{
1911 //
1912 // Fills clusters into TRD tracking sectors
1913 // Function for use in the HLT
1914
1915 if(!clusters || !clusters->GetEntriesFast()){
1916 AliInfo("No TRD clusters");
41702fec 1917 return 1;
1918 }
1919
66f6bfd9 1920 fClusters = clusters;
1921 SetClustersOwner();
1922
1923 //Int_t nin =
1924 BuildTrackingContainers();
1925
1926 //Int_t ncl = fClusters->GetEntriesFast();
1927 //AliInfo(Form("Clusters %d [%6.2f %% in the active volume]", ncl, 100.*float(nin)/ncl));
1928
1929 return 0;
1930}
1931
1932
1933//____________________________________________________________________
1934Int_t AliTRDtrackerV1::BuildTrackingContainers()
1935{
1936// Building tracking containers for clusters
1937
1938 Int_t nin =0, icl = fClusters->GetEntriesFast();
41702fec 1939 while (icl--) {
1940 AliTRDcluster *c = (AliTRDcluster *) fClusters->UncheckedAt(icl);
1941 if(c->IsInChamber()) nin++;
4226b195 1942 if(fkReconstructor->IsHLT()) c->SetRPhiMethod(AliTRDcluster::kCOG);
41702fec 1943 Int_t detector = c->GetDetector();
1944 Int_t sector = fGeom->GetSector(detector);
053767a4 1945 Int_t stack = fGeom->GetStack(detector);
1946 Int_t layer = fGeom->GetLayer(detector);
41702fec 1947
053767a4 1948 fTrSec[sector].GetChamber(stack, layer, kTRUE)->InsertCluster(c, icl);
41702fec 1949 }
b0a48c4d 1950
1951 const AliTRDCalDet *cal = AliTRDcalibDB::Instance()->GetT0Det();
053767a4 1952 for(int isector =0; isector<AliTRDgeometry::kNsector; isector++){
41702fec 1953 if(!fTrSec[isector].GetNChambers()) continue;
4d6aee34 1954 fTrSec[isector].Init(fkReconstructor, cal);
41702fec 1955 }
66f6bfd9 1956
1957 return nin;
eb38ed55 1958}
1959
1960
66f6bfd9 1961
0906e73e 1962//____________________________________________________________________
172b6f82 1963void AliTRDtrackerV1::UnloadClusters()
0906e73e 1964{
d4cf71ca 1965//
1966// Clears the arrays of clusters and tracks. Resets sectors and timebins
1967// If option "force" is also set the containers are also deleted. This is useful
1968// in case of HLT
1969
1970 if(fTracks){
1971 fTracks->Delete();
4d6aee34 1972 if(HasRemoveContainers()){delete fTracks; fTracks = NULL;}
d4cf71ca 1973 }
1974 if(fTracklets){
1975 fTracklets->Delete();
4d6aee34 1976 if(HasRemoveContainers()){delete fTracklets; fTracklets = NULL;}
d4cf71ca 1977 }
48f8adf3 1978 if(fClusters){
1979 if(IsClustersOwner()) fClusters->Delete();
1980
1981 // save clusters array in the reconstructor for further use.
4d6aee34 1982 if(!fkReconstructor->IsWritingClusters()){
48f8adf3 1983 AliTRDReconstructor::SetClusters(fClusters);
1984 SetClustersOwner(kFALSE);
4d6aee34 1985 } else AliTRDReconstructor::SetClusters(NULL);
48f8adf3 1986 }
0906e73e 1987
053767a4 1988 for (int i = 0; i < AliTRDgeometry::kNsector; i++) fTrSec[i].Clear();
0906e73e 1989
41702fec 1990 // Increment the Event Number
1991 AliTRDtrackerDebug::SetEventNumber(AliTRDtrackerDebug::GetEventNumber() + 1);
eb38ed55 1992}
0906e73e 1993
fac58f00 1994// //____________________________________________________________________
1995// void AliTRDtrackerV1::UseClusters(const AliKalmanTrack *t, Int_t) const
1996// {
1997// const AliTRDtrackV1 *track = dynamic_cast<const AliTRDtrackV1*>(t);
1998// if(!track) return;
1999//
4d6aee34 2000// AliTRDseedV1 *tracklet = NULL;
fac58f00 2001// for(Int_t ily=AliTRDgeometry::kNlayer; ily--;){
2002// if(!(tracklet = track->GetTracklet(ily))) continue;
4d6aee34 2003// AliTRDcluster *c = NULL;
8d2bec9e 2004// for(Int_t ic=AliTRDseed::kNclusters; ic--;){
fac58f00 2005// if(!(c=tracklet->GetClusters(ic))) continue;
2006// c->Use();
2007// }
2008// }
2009// }
2010//
b1957d3c 2011
eb38ed55 2012//_____________________________________________________________________________
4d6aee34 2013Bool_t AliTRDtrackerV1::AdjustSector(AliTRDtrackV1 *const track)
eb38ed55 2014{
41702fec 2015 //
2016 // Rotates the track when necessary
2017 //
2018
2019 Double_t alpha = AliTRDgeometry::GetAlpha();
2020 Double_t y = track->GetY();
2021 Double_t ymax = track->GetX()*TMath::Tan(0.5*alpha);
3cfaffa4 2022
41702fec 2023 if (y > ymax) {
2024 if (!track->Rotate( alpha)) {
2025 return kFALSE;
2026 }
2027 }
2028 else if (y < -ymax) {
2029 if (!track->Rotate(-alpha)) {
2030 return kFALSE;
2031 }
2032 }
2033
2034 return kTRUE;
0906e73e 2035
2036}
2037
eb38ed55 2038
0906e73e 2039//____________________________________________________________________
4d6aee34 2040AliTRDseedV1* AliTRDtrackerV1::GetTracklet(AliTRDtrackV1 *const track, Int_t p, Int_t &idx)
0906e73e 2041{
41702fec 2042 // Find tracklet for TRD track <track>
2043 // Parameters
2044 // - track
2045 // - sector
2046 // - plane
2047 // - index
2048 // Output
2049 // tracklet
2050 // index
2051 // Detailed description
2052 //
2053 idx = track->GetTrackletIndex(p);
17896e82 2054 AliTRDseedV1 *tracklet = (idx<0) ? NULL : (AliTRDseedV1*)fTracklets->UncheckedAt(idx);
41702fec 2055
2056 return tracklet;
0906e73e 2057}
2058
2059//____________________________________________________________________
4d6aee34 2060AliTRDseedV1* AliTRDtrackerV1::SetTracklet(const AliTRDseedV1 * const tracklet)
0906e73e 2061{
41702fec 2062 // Add this tracklet to the list of tracklets stored in the tracker
2063 //
2064 // Parameters
2065 // - tracklet : pointer to the tracklet to be added to the list
2066 //
2067 // Output
2068 // - the index of the new tracklet in the tracker tracklets list
2069 //
2070 // Detailed description
2071 // Build the tracklets list if it is not yet created (late initialization)
2072 // and adds the new tracklet to the list.
2073 //
2074 if(!fTracklets){
053767a4 2075 fTracklets = new TClonesArray("AliTRDseedV1", AliTRDgeometry::Nsector()*kMaxTracksStack);
41702fec 2076 fTracklets->SetOwner(kTRUE);
2077 }
2078 Int_t nentries = fTracklets->GetEntriesFast();
2079 return new ((*fTracklets)[nentries]) AliTRDseedV1(*tracklet);
972ef65e 2080}
2081
d20df6fc 2082//____________________________________________________________________
828c6f80 2083void AliTRDtrackerV1::CookNTimeBins()
2084{
2085 // Initialize number of time bins
2086
2087 if(fgNTimeBins){
2088 // first look if set by hand
2089 AliDebug(2, Form("NTimeBins [%d] (set by user)", fgNTimeBins));
2090 } else if(fkReconstructor && fkReconstructor->HasDigitsParam()) {
2091 // second look into digits param to avoid DB query
2092 fgNTimeBins = fkReconstructor->GetDigitsParam()->GetNTimeBins();
58e2ef2e 2093 if(fgNTimeBins>0) AliDebug(2, Form("NTimeBins [%d] (set from digits param)", fgNTimeBins));
2094 else AliWarning(Form("NTimeBins [%d] failed from digits param.", fgNTimeBins));
2095 }
2096
2097 if(!fgNTimeBins){ // third query DB
828c6f80 2098 AliTRDcalibDB *trd(NULL);
2099 if((trd = AliTRDcalibDB::Instance())) {
2100 if((fgNTimeBins = trd->GetNumberOfTimeBinsDCS()) <= 0){
2101 AliError("Corrupted DCS Object in OCDB");
2102 fgNTimeBins = 24;
58e2ef2e 2103 AliDebug(2, Form("NTimeBins [%d] (set to default 24)", fgNTimeBins));
828c6f80 2104 } else AliDebug(2, Form("NTimeBins [%d] (set from DB)", fgNTimeBins));
2105 } else AliFatal("Could not get DB.");
2106 }
58e2ef2e 2107
828c6f80 2108 if(fgNTimeBins<=0){
58e2ef2e 2109 AliError("NTimeBins failed all settings. Use default 24 !");
2110 fgNTimeBins = 24;
828c6f80 2111 }
2112}
2113
2114//____________________________________________________________________
4d6aee34 2115AliTRDtrackV1* AliTRDtrackerV1::SetTrack(const AliTRDtrackV1 * const track)
d20df6fc 2116{
2117 // Add this track to the list of tracks stored in the tracker
2118 //
2119 // Parameters
2120 // - track : pointer to the track to be added to the list
2121 //
2122 // Output
2123 // - the pointer added
2124 //
2125 // Detailed description
2126 // Build the tracks list if it is not yet created (late initialization)
2127 // and adds the new track to the list.
2128 //
2129 if(!fTracks){
053767a4 2130 fTracks = new TClonesArray("AliTRDtrackV1", AliTRDgeometry::Nsector()*kMaxTracksStack);
d20df6fc 2131 fTracks->SetOwner(kTRUE);
2132 }
2133 Int_t nentries = fTracks->GetEntriesFast();
2134 return new ((*fTracks)[nentries]) AliTRDtrackV1(*track);
2135}
2136
2137
0906e73e 2138
e4f2f73d 2139//____________________________________________________________________
eb38ed55 2140Int_t AliTRDtrackerV1::Clusters2TracksSM(Int_t sector, AliESDEvent *esd)
e4f2f73d 2141{
41702fec 2142 //
2143 // Steer tracking for one SM.
2144 //
2145 // Parameters :
2146 // sector : Array of (SM) propagation layers containing clusters
2147 // esd : The current ESD event. On output it contains the also
2148 // the ESD (TRD) tracks found in this SM.
2149 //
2150 // Output :
2151 // Number of tracks found in this TRD supermodule.
2152 //
2153 // Detailed description
2154 //
2155 // 1. Unpack AliTRDpropagationLayers objects for each stack.
2156 // 2. Launch stack tracking.
2157 // See AliTRDtrackerV1::Clusters2TracksStack() for details.
2158 // 3. Pack results in the ESD event.
2159 //
2160
41702fec 2161 Int_t nTracks = 0;
2162 Int_t nChambers = 0;
4d6aee34 2163 AliTRDtrackingChamber **stack = NULL, *chamber = NULL;
053767a4 2164 for(int istack = 0; istack<AliTRDgeometry::kNstack; istack++){
41702fec 2165 if(!(stack = fTrSec[sector].GetStack(istack))) continue;
2166 nChambers = 0;
053767a4 2167 for(int ilayer=0; ilayer<AliTRDgeometry::kNlayer; ilayer++){
2168 if(!(chamber = stack[ilayer])) continue;
4d6aee34 2169 if(chamber->GetNClusters() < fgNTimeBins * fkReconstructor->GetRecoParam() ->GetFindableClusters()) continue;
41702fec 2170 nChambers++;
053767a4 2171 //AliInfo(Form("sector %d stack %d layer %d clusters %d", sector, istack, ilayer, chamber->GetNClusters()));
41702fec 2172 }
2173 if(nChambers < 4) continue;
2174 //AliInfo(Form("Doing stack %d", istack));
b1135f96 2175 nTracks += Clusters2TracksStack(stack, fTracksESD);
41702fec 2176 }
2177 //AliInfo(Form("Found %d tracks in SM %d [%d]\n", nTracks, sector, esd->GetNumberOfTracks()));
2178
2179 for(int itrack=0; itrack<nTracks; itrack++)
b1135f96 2180 esd->AddTrack((AliESDtrack*)(fTracksESD->operator[](itrack)));
41702fec 2181
2182 // Reset Track and Candidate Number
2183 AliTRDtrackerDebug::SetCandidateNumber(0);
2184 AliTRDtrackerDebug::SetTrackNumber(0);
b1135f96 2185
2186 // delete ESD tracks in the array
2187 fTracksESD->Delete();
41702fec 2188 return nTracks;
e4f2f73d 2189}
2190
2191//____________________________________________________________________
4d6aee34 2192Int_t AliTRDtrackerV1::Clusters2TracksStack(AliTRDtrackingChamber **stack, TClonesArray * const esdTrackList)
e4f2f73d 2193{
41702fec 2194 //
2195 // Make tracks in one TRD stack.
2196 //
2197 // Parameters :
2198 // layer : Array of stack propagation layers containing clusters
2199 // esdTrackList : Array of ESD tracks found by the stand alone tracker.
2200 // On exit the tracks found in this stack are appended.
2201 //
2202 // Output :
2203 // Number of tracks found in this stack.
2204 //
2205 // Detailed description
2206 //
2207 // 1. Find the 3 most useful seeding chambers. See BuildSeedingConfigs() for details.
2208 // 2. Steer AliTRDtrackerV1::MakeSeeds() for 3 seeding layer configurations.
2209 // See AliTRDtrackerV1::MakeSeeds() for more details.
2210 // 3. Arrange track candidates in decreasing order of their quality
2211 // 4. Classify tracks in 5 categories according to:
2212 // a) number of layers crossed
2213 // b) track quality
2214 // 5. Sign clusters by tracks in decreasing order of track quality
2215 // 6. Build AliTRDtrack out of seeding tracklets
2216 // 7. Cook MC label
2217 // 8. Build ESD track and register it to the output list
2218 //
2219
b0a48c4d 2220 const AliTRDCalDet *cal = AliTRDcalibDB::Instance()->GetT0Det();
4d6aee34 2221 AliTRDtrackingChamber *chamber = NULL;
2222 AliTRDtrackingChamber **ci = NULL;
41702fec 2223 AliTRDseedV1 sseed[kMaxTracksStack*6]; // to be initialized
2224 Int_t pars[4]; // MakeSeeds parameters
2225
2226 //Double_t alpha = AliTRDgeometry::GetAlpha();
2227 //Double_t shift = .5 * alpha;
2228 Int_t configs[kNConfigs];
2229
fac58f00 2230 // Purge used clusters from the containers
2231 ci = &stack[0];
2232 for(Int_t ic = kNPlanes; ic--; ci++){
2233 if(!(*ci)) continue;
2234 (*ci)->Update();
2235 }
2236
41702fec 2237 // Build initial seeding configurations
2238 Double_t quality = BuildSeedingConfigs(stack, configs);
a2fbb6ec 2239 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 10){
41702fec 2240 AliInfo(Form("Plane config %d %d %d Quality %f"
2241 , configs[0], configs[1], configs[2], quality));
2242 }
d931f2aa 2243
41702fec 2244
2245 // Initialize contors
2246 Int_t ntracks, // number of TRD track candidates
2247 ntracks1, // number of registered TRD tracks/iter
2248 ntracks2 = 0; // number of all registered TRD tracks in stack
2249 fSieveSeeding = 0;
d931f2aa 2250
2251 // Get stack index
fac58f00 2252 Int_t ic = 0; ci = &stack[0];
2253 while(ic<kNPlanes && !(*ci)){ic++; ci++;}
2254 if(!(*ci)) return ntracks2;
2255 Int_t istack = fGeom->GetStack((*ci)->GetDetector());
d931f2aa 2256
41702fec 2257 do{
2258 // Loop over seeding configurations
2259 ntracks = 0; ntracks1 = 0;
2260 for (Int_t iconf = 0; iconf<3; iconf++) {
2261 pars[0] = configs[iconf];
2262 pars[1] = ntracks;
d931f2aa 2263 pars[2] = istack;
41702fec 2264 ntracks = MakeSeeds(stack, &sseed[6*ntracks], pars);
6e39bde4 2265 //AliInfo(Form("Number of Tracks after iteration step %d: %d\n", iconf, ntracks));
41702fec 2266 if(ntracks == kMaxTracksStack) break;
2267 }
980d5a2a 2268 AliDebug(2, Form("Candidate TRD tracks %d in iteration %d.", ntracks, fSieveSeeding));
41702fec 2269
2270 if(!ntracks) break;
2271
2272 // Sort the seeds according to their quality
2273 Int_t sort[kMaxTracksStack];
2274 TMath::Sort(ntracks, fTrackQuality, sort, kTRUE);
2275
2276 // Initialize number of tracks so far and logic switches
2277 Int_t ntracks0 = esdTrackList->GetEntriesFast();
2278 Bool_t signedTrack[kMaxTracksStack];
2279 Bool_t fakeTrack[kMaxTracksStack];
2280 for (Int_t i=0; i<ntracks; i++){
2281 signedTrack[i] = kFALSE;
2282 fakeTrack[i] = kFALSE;
2283 }
2284 //AliInfo("Selecting track candidates ...");
2285
2286 // Sieve clusters in decreasing order of track quality
2287 Double_t trackParams[7];
4d6aee34 2288 // AliTRDseedV1 *lseed = NULL;
41702fec 2289 Int_t jSieve = 0, candidates;
2290 do{
2291 //AliInfo(Form("\t\tITER = %i ", jSieve));
2292
2293 // Check track candidates
2294 candidates = 0;
2295 for (Int_t itrack = 0; itrack < ntracks; itrack++) {
804bb02e 2296 Int_t trackIndex = sort[itrack];
2297 if (signedTrack[trackIndex] || fakeTrack[trackIndex]) continue;
41702fec 2298
2299
804bb02e 2300 // Calculate track parameters from tracklets seeds
804bb02e 2301 Int_t ncl = 0;
2302 Int_t nused = 0;
2303 Int_t nlayers = 0;
2304 Int_t findable = 0;
2305 for (Int_t jLayer = 0; jLayer < kNPlanes; jLayer++) {
2306 Int_t jseed = kNPlanes*trackIndex+jLayer;
2307 if(!sseed[jseed].IsOK()) continue;
f29f13a6 2308 if (TMath::Abs(sseed[jseed].GetYref(0) / sseed[jseed].GetX0()) < 0.158) findable++;
eede7907 2309 // TODO here we get a sig fault which should never happen !
804bb02e 2310 sseed[jseed].UpdateUsed();
2311 ncl += sseed[jseed].GetN2();
2312 nused += sseed[jseed].GetNUsed();
2313 nlayers++;
804bb02e 2314 }
2315
f29f13a6 2316 // Filter duplicated tracks
2317 if (nused > 30){
2318 //printf("Skip %d nused %d\n", trackIndex, nused);
2319 fakeTrack[trackIndex] = kTRUE;
2320 continue;
2321 }
5ef88e00 2322 if (ncl>0 && Float_t(nused)/ncl >= .25){
f29f13a6 2323 //printf("Skip %d nused/ncl >= .25\n", trackIndex);
2324 fakeTrack[trackIndex] = kTRUE;
2325 continue;
2326 }
41702fec 2327
f29f13a6 2328 // Classify tracks
2329 Bool_t skip = kFALSE;
2330 switch(jSieve){
2331 case 0:
2332 if(nlayers < 6) {skip = kTRUE; break;}
2333 if(TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -5.){skip = kTRUE; break;}
2334 break;
2335
2336 case 1:
2337 if(nlayers < findable){skip = kTRUE; break;}
2338 if(TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -4.){skip = kTRUE; break;}
2339 break;
2340
2341 case 2:
2342 if ((nlayers == findable) || (nlayers == 6)) { skip = kTRUE; break;}
2343 if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -6.0){skip = kTRUE; break;}
2344 break;
2345
2346 case 3:
2347 if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -5.){skip = kTRUE; break;}
2348 break;
2349
2350 case 4:
2351 if (nlayers == 3){skip = kTRUE; break;}
2352 //if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) - nused/(nlayers-3.0) < -15.0){skip = kTRUE; break;}
2353 break;
2354 }
2355 if(skip){
2356 candidates++;
5ef88e00 2357 AliDebug(4, Form("REJECTED : %d idx[%d] quality[%e] tracklets[%d] usedClusters[%d]", itrack, trackIndex, fTrackQuality[trackIndex], nlayers, nused));
f29f13a6 2358 continue;
5ef88e00 2359 } else AliDebug(4, Form("ACCEPTED : %d idx[%d] quality[%e] tracklets[%d] usedClusters[%d]", itrack, trackIndex, fTrackQuality[trackIndex], nlayers, nused));
2360
f29f13a6 2361 signedTrack[trackIndex] = kTRUE;
2362
2363 // Build track parameters
2364 AliTRDseedV1 *lseed =&sseed[trackIndex*6];
2365 /* Int_t idx = 0;
2366 while(idx<3 && !lseed->IsOK()) {
2367 idx++;
2368 lseed++;
2369 }*/
2370 Double_t x = lseed->GetX0();// - 3.5;
2371 trackParams[0] = x; //NEW AB
2372 trackParams[1] = lseed->GetYref(0); // lseed->GetYat(x);
2373 trackParams[2] = lseed->GetZref(0); // lseed->GetZat(x);
2374 trackParams[3] = TMath::Sin(TMath::ATan(lseed->GetYref(1)));
2375 trackParams[4] = lseed->GetZref(1) / TMath::Sqrt(1. + lseed->GetYref(1) * lseed->GetYref(1));
2376 trackParams[5] = lseed->GetC();
2377 Int_t ich = 0; while(!(chamber = stack[ich])) ich++;
2378 trackParams[6] = fGeom->GetSector(chamber->GetDetector());/* *alpha+shift; // Supermodule*/
2379
a2fbb6ec 2380 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 1 && fkReconstructor->IsDebugStreaming()){
eb2b4f91 2381 //AliInfo(Form("Track %d [%d] nlayers %d trackQuality = %e nused %d, yref = %3.3f", itrack, trackIndex, nlayers, fTrackQuality[trackIndex], nused, trackParams[1]));
f29f13a6 2382
f29f13a6 2383 AliTRDseedV1 *dseed[6];
b82b4de1 2384 for(Int_t iseed = AliTRDgeometry::kNlayer; iseed--;) dseed[iseed] = new AliTRDseedV1(lseed[iseed]);
f29f13a6 2385
2386 //Int_t eventNrInFile = esd->GetEventNumberInFile();
f29f13a6 2387 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2388 Int_t trackNumber = AliTRDtrackerDebug::GetTrackNumber();
2389 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
a2fbb6ec 2390 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
f29f13a6 2391 cstreamer << "Clusters2TracksStack"
2392 << "EventNumber=" << eventNumber
2393 << "TrackNumber=" << trackNumber
2394 << "CandidateNumber=" << candidateNumber
2395 << "Iter=" << fSieveSeeding
2396 << "Like=" << fTrackQuality[trackIndex]
2397 << "S0.=" << dseed[0]
2398 << "S1.=" << dseed[1]
2399 << "S2.=" << dseed[2]
2400 << "S3.=" << dseed[3]
2401 << "S4.=" << dseed[4]
2402 << "S5.=" << dseed[5]
2403 << "p0=" << trackParams[0]
2404 << "p1=" << trackParams[1]
2405 << "p2=" << trackParams[2]
2406 << "p3=" << trackParams[3]
2407 << "p4=" << trackParams[4]
2408 << "p5=" << trackParams[5]
2409 << "p6=" << trackParams[6]
f29f13a6 2410 << "Ncl=" << ncl
2411 << "NLayers=" << nlayers
2412 << "Findable=" << findable
2413 << "NUsed=" << nused
2414 << "\n";
2415 }
d877f55f 2416
f29f13a6 2417 AliTRDtrackV1 *track = MakeTrack(&sseed[trackIndex*kNPlanes], trackParams);
2418 if(!track){
980d5a2a 2419 AliDebug(1, "Track building failed.");
f29f13a6 2420 continue;
d877f55f 2421 }
41702fec 2422
f29f13a6 2423 //AliInfo("End of MakeTrack()");
2424 AliESDtrack *esdTrack = new ((*esdTrackList)[ntracks0++]) AliESDtrack();
2425 esdTrack->UpdateTrackParams(track, AliESDtrack::kTRDout);
2426 esdTrack->SetLabel(track->GetLabel());
2427 track->UpdateESDtrack(esdTrack);
2428 // write ESD-friends if neccessary
a2fbb6ec 2429 if (fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 0){
f29f13a6 2430 AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(*track);
2431 calibTrack->SetOwner();
2432 esdTrack->AddCalibObject(calibTrack);
2433 }
2434 ntracks1++;
2435 AliTRDtrackerDebug::SetTrackNumber(AliTRDtrackerDebug::GetTrackNumber() + 1);
41702fec 2436 }
2437
2438 jSieve++;
2439 } while(jSieve<5 && candidates); // end track candidates sieve
2440 if(!ntracks1) break;
2441
2442 // increment counters
2443 ntracks2 += ntracks1;
4302c900 2444
4d6aee34 2445 if(fkReconstructor->IsHLT()) break;
41702fec 2446 fSieveSeeding++;
2447
2448 // Rebuild plane configurations and indices taking only unused clusters into account
2449 quality = BuildSeedingConfigs(stack, configs);
4d6aee34 2450 if(quality < 1.E-7) break; //fkReconstructor->GetRecoParam() ->GetPlaneQualityThreshold()) break;
41702fec 2451
2452 for(Int_t ip = 0; ip < kNPlanes; ip++){
2453 if(!(chamber = stack[ip])) continue;
b0a48c4d 2454 chamber->Build(fGeom, cal);//Indices(fSieveSeeding);
41702fec 2455 }
2456
a2fbb6ec 2457 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 10){
41702fec 2458 AliInfo(Form("Sieve level %d Plane config %d %d %d Quality %f", fSieveSeeding, configs[0], configs[1], configs[2], quality));
2459 }
2460 } while(fSieveSeeding<10); // end stack clusters sieve
2461
2462
2463
2464 //AliInfo(Form("Registered TRD tracks %d in stack %d.", ntracks2, pars[1]));
2465
2466 return ntracks2;
e4f2f73d 2467}
2468
2469//___________________________________________________________________
eb38ed55 2470Double_t AliTRDtrackerV1::BuildSeedingConfigs(AliTRDtrackingChamber **stack, Int_t *configs)
e4f2f73d 2471{
41702fec 2472 //
2473 // Assign probabilities to chambers according to their
2474 // capability of producing seeds.
2475 //
2476 // Parameters :
2477 //
2478 // layers : Array of stack propagation layers for all 6 chambers in one stack
2479 // configs : On exit array of configuration indexes (see GetSeedingConfig()
2480 // for details) in the decreasing order of their seeding probabilities.
2481 //
2482 // Output :
2483 //
2484 // Return top configuration quality
2485 //
2486 // Detailed description:
2487 //
2488 // To each chamber seeding configuration (see GetSeedingConfig() for
2489 // the list of all configurations) one defines 2 quality factors:
2490 // - an apriori topological quality (see GetSeedingConfig() for details) and
2491 // - a data quality based on the uniformity of the distribution of
2492 // clusters over the x range (time bins population). See CookChamberQA() for details.
2493 // The overall chamber quality is given by the product of this 2 contributions.
2494 //
2495
eb2b4f91 2496 Double_t chamberQ[kNPlanes];memset(chamberQ, 0, kNPlanes*sizeof(Double_t));
4d6aee34 2497 AliTRDtrackingChamber *chamber = NULL;
41702fec 2498 for(int iplane=0; iplane<kNPlanes; iplane++){
2499 if(!(chamber = stack[iplane])) continue;
2500 chamberQ[iplane] = (chamber = stack[iplane]) ? chamber->GetQuality() : 0.;
2501 }
2502
eb2b4f91 2503 Double_t tconfig[kNConfigs];memset(tconfig, 0, kNConfigs*sizeof(Double_t));
2504 Int_t planes[] = {0, 0, 0, 0};
41702fec 2505 for(int iconf=0; iconf<kNConfigs; iconf++){
2506 GetSeedingConfig(iconf, planes);
2507 tconfig[iconf] = fgTopologicQA[iconf];
2508 for(int iplane=0; iplane<4; iplane++) tconfig[iconf] *= chamberQ[planes[iplane]];
2509 }
2510
2511 TMath::Sort((Int_t)kNConfigs, tconfig, configs, kTRUE);
2512 // AliInfo(Form("q[%d] = %f", configs[0], tconfig[configs[0]]));
2513 // AliInfo(Form("q[%d] = %f", configs[1], tconfig[configs[1]]));
2514 // AliInfo(Form("q[%d] = %f", configs[2], tconfig[configs[2]]));
2515
2516 return tconfig[configs[0]];
e4f2f73d 2517}
2518
2519//____________________________________________________________________
e17f4785 2520Int_t AliTRDtrackerV1::MakeSeeds(AliTRDtrackingChamber **stack, AliTRDseedV1 * const sseed, const Int_t * const ipar)
e4f2f73d 2521{
afa21247 2522//
2a3191bb 2523// Seed tracklets and build candidate TRD tracks. The procedure is used during barrel tracking to account for tracks which are
2524// either missed by TPC prolongation or conversions inside the TRD volume.
2525// For stand alone tracking the procedure is used to estimate all tracks measured by TRD.
afa21247 2526//
2527// Parameters :
2528// layers : Array of stack propagation layers containing clusters
2529// sseed : Array of empty tracklet seeds. On exit they are filled.
2530// ipar : Control parameters:
2531// ipar[0] -> seeding chambers configuration
2532// ipar[1] -> stack index
2533// ipar[2] -> number of track candidates found so far
2534//
2535// Output :
2536// Number of tracks candidates found.
2537//
2538// The following steps are performed:
2539// 1. Build seeding layers by collapsing all time bins from each of the four seeding chambers along the
2540// radial coordinate. See AliTRDtrackingChamber::GetSeedingLayer() for details. The chambers selection for seeding
2541// is described in AliTRDtrackerV1::Clusters2TracksStack().
2542// 2. Using the seeding clusters from the seeding layer (step 1) build combinatorics using the following algorithm:
2543// - for each seeding cluster in the lower seeding layer find
2544// - all seeding clusters in the upper seeding layer inside a road defined by a given phi angle. The angle
2545// is calculated on the minimum pt of tracks from vertex accesible to the stand alone tracker.
2546// - for each pair of two extreme seeding clusters select middle upper cluster using roads defined externally by the
2547// reco params
2548// - select last seeding cluster as the nearest to the linear approximation of the track described by the first three
2549// seeding clusters.
2550// The implementation of road calculation and cluster selection can be found in the functions AliTRDchamberTimeBin::BuildCond()
2551// and AliTRDchamberTimeBin::GetClusters().
2552// 3. Helix fit of the seeding clusters set. (see AliTRDtrackerFitter::FitRieman(AliTRDcluster**)). No tilt correction is
2553// performed at this level
2554// 4. Initialize seeding tracklets in the seeding chambers.
2555// 5. *Filter 0* Chi2 cut on the Y and Z directions. The threshold is set externally by the reco params.
2556// 6. Attach (true) clusters to seeding tracklets (see AliTRDseedV1::AttachClusters()) and fit tracklet (see
2557// AliTRDseedV1::Fit()). The number of used clusters used by current seeds should not exceed ... (25).
2558// 7. *Filter 1* Check if all 4 seeding tracklets are correctly constructed.
2559// 8. Helix fit of the clusters from the seeding tracklets with tilt correction. Refit tracklets using the new
2560// approximation of the track.
2561// 9. *Filter 2* Calculate likelihood of the track. (See AliTRDtrackerV1::CookLikelihood()). The following quantities are
2562// checked against the Riemann fit:
2563// - position resolution in y
2564// - angular resolution in the bending plane
2565// - likelihood of the number of clusters attached to the tracklet
2566// 10. Extrapolation of the helix fit to the other 2 chambers *non seeding* chambers:
2567// - Initialization of extrapolation tracklets with the fit parameters
2568// - Attach clusters to extrapolated tracklets
2569// - Helix fit of tracklets
2570// 11. Improve seeding tracklets quality by reassigning clusters based on the last parameters of the track
2571// See AliTRDtrackerV1::ImproveSeedQuality() for details.
2572// 12. Helix fit of all 6 seeding tracklets and chi2 calculation
2573// 13. Hyperplane fit and track quality calculation. See AliTRDtrackerFitter::FitHyperplane() for details.
2574// 14. Cooking labels for tracklets. Should be done only for MC
2575// 15. Register seeds.
2576//
2577// Authors:
2578// Marian Ivanov <M.Ivanov@gsi.de>
2579// Alexandru Bercuci <A.Bercuci@gsi.de>
2580// Markus Fasel <M.Fasel@gsi.de>
41702fec 2581
4d6aee34 2582 AliTRDtrackingChamber *chamber = NULL;
2583 AliTRDcluster *c[kNSeedPlanes] = {NULL, NULL, NULL, NULL}; // initilize seeding clusters
e17f4785 2584 AliTRDseedV1 *cseed = &sseed[0]; // initialize tracklets for first track
41702fec 2585 Int_t ncl, mcl; // working variable for looping over clusters
2586 Int_t index[AliTRDchamberTimeBin::kMaxClustersLayer], jndex[AliTRDchamberTimeBin::kMaxClustersLayer];
2587 // chi2 storage
2588 // chi2[0] = tracklet chi2 on the Z direction
2589 // chi2[1] = tracklet chi2 on the R direction
2590 Double_t chi2[4];
2591
afa21247 2592 // this should be data member of AliTRDtrack TODO
41702fec 2593 Double_t seedQuality[kMaxTracksStack];
2594
2595 // unpack control parameters
2596 Int_t config = ipar[0];
2597 Int_t ntracks = ipar[1];
d931f2aa 2598 Int_t istack = ipar[2];
41702fec 2599 Int_t planes[kNSeedPlanes]; GetSeedingConfig(config, planes);
afa21247 2600 Int_t planesExt[kNPlanes-kNSeedPlanes]; GetExtrapolationConfig(config, planesExt);
be24510a 2601
2602
41702fec 2603 // Init chambers geometry
41702fec 2604 Double_t hL[kNPlanes]; // Tilting angle
2605 Float_t padlength[kNPlanes]; // pad lenghts
dd8059a8 2606 Float_t padwidth[kNPlanes]; // pad widths
4d6aee34 2607 AliTRDpadPlane *pp = NULL;
41702fec 2608 for(int iplane=0; iplane<kNPlanes; iplane++){
2609 pp = fGeom->GetPadPlane(iplane, istack);
bb79ccd5 2610 hL[iplane] = TMath::Tan(TMath::DegToRad()*pp->GetTiltingAngle());
41702fec 2611 padlength[iplane] = pp->GetLengthIPad();
dd8059a8 2612 padwidth[iplane] = pp->GetWidthIPad();
41702fec 2613 }
2614
eb2b4f91 2615 // Init anode wire position for chambers
e165b64b 2616 Double_t x0[kNPlanes], // anode wire position
eb2b4f91 2617 driftLength = .5*AliTRDgeometry::AmThick() - AliTRDgeometry::DrThick(); // drift length
4d6aee34 2618 TGeoHMatrix *matrix = NULL;
eb2b4f91 2619 Double_t loc[] = {AliTRDgeometry::AnodePos(), 0., 0.};
2620 Double_t glb[] = {0., 0., 0.};
2621 AliTRDtrackingChamber **cIter = &stack[0];
a3743898 2622 for(int iLayer=0; iLayer<kNPlanes; iLayer++,cIter++){
eb2b4f91 2623 if(!(*cIter)) continue;
e165b64b 2624 if(!(matrix = fGeom->GetClusterMatrix((*cIter)->GetDetector()))){
2625 continue;
2626 x0[iLayer] = fgkX0[iLayer];
2627 }
eb2b4f91 2628 matrix->LocalToMaster(loc, glb);
2629 x0[iLayer] = glb[0];
2630 }
2631
980d5a2a 2632 AliDebug(2, Form("Making seeds Stack[%d] Config[%d] Tracks[%d]...", istack, config, ntracks));
41702fec 2633
d931f2aa 2634 // Build seeding layers
d611c74f 2635 ResetSeedTB();
41702fec 2636 Int_t nlayers = 0;
41702fec 2637 for(int isl=0; isl<kNSeedPlanes; isl++){
2638 if(!(chamber = stack[planes[isl]])) continue;
4d6aee34 2639 if(!chamber->GetSeedingLayer(fSeedTB[isl], fGeom, fkReconstructor)) continue;
41702fec 2640 nlayers++;
41702fec 2641 }
eb2b4f91 2642 if(nlayers < kNSeedPlanes) return ntracks;
41702fec 2643
2644
2645 // Start finding seeds
2646 Double_t cond0[4], cond1[4], cond2[4];
2647 Int_t icl = 0;
d611c74f 2648 while((c[3] = (*fSeedTB[3])[icl++])){
41702fec 2649 if(!c[3]) continue;
d611c74f 2650 fSeedTB[0]->BuildCond(c[3], cond0, 0);
2651 fSeedTB[0]->GetClusters(cond0, index, ncl);
41702fec 2652 //printf("Found c[3] candidates 0 %d\n", ncl);
2653 Int_t jcl = 0;
2654 while(jcl<ncl) {
d611c74f 2655 c[0] = (*fSeedTB[0])[index[jcl++]];
41702fec 2656 if(!c[0]) continue;
2657 Double_t dx = c[3]->GetX() - c[0]->GetX();
afa21247 2658 Double_t dzdx = (c[3]->GetZ() - c[0]->GetZ())/dx;
2659 Double_t dydx = (c[3]->GetY() - c[0]->GetY())/dx;
2660 fSeedTB[1]->BuildCond(c[0], cond1, 1, dzdx, dydx);
d611c74f 2661 fSeedTB[1]->GetClusters(cond1, jndex, mcl);
41702fec 2662 //printf("Found c[0] candidates 1 %d\n", mcl);
2663
2664 Int_t kcl = 0;
2665 while(kcl<mcl) {
d611c74f 2666 c[1] = (*fSeedTB[1])[jndex[kcl++]];
2667 if(!c[1]) continue;
afa21247 2668 fSeedTB[2]->BuildCond(c[1], cond2, 2, dzdx, dydx);
d611c74f 2669 c[2] = fSeedTB[2]->GetNearestCluster(cond2);
2670 //printf("Found c[1] candidate 2 %p\n", c[2]);
2671 if(!c[2]) continue;
980d5a2a 2672
2673 AliDebug(3, Form("Seeding clusters\n 0[%6.3f %6.3f %6.3f]\n 1[%6.3f %6.3f %6.3f]\n 2[%6.3f %6.3f %6.3f]\n 3[%6.3f %6.3f %6.3f].",
2674 c[0]->GetX(), c[0]->GetY(), c[0]->GetZ(),
2675 c[1]->GetX(), c[1]->GetY(), c[1]->GetZ(),
2676 c[2]->GetX(), c[2]->GetY(), c[2]->GetZ(),
2677 c[3]->GetX(), c[3]->GetY(), c[3]->GetZ()));
d611c74f 2678
804bb02e 2679 for (Int_t il = 0; il < kNPlanes; il++) cseed[il].Reset();
41702fec 2680
d611c74f 2681 FitRieman(c, chi2);
2682
d931f2aa 2683 AliTRDseedV1 *tseed = &cseed[0];
bb2db46c 2684 cIter = &stack[0];
2685 for(int iLayer=0; iLayer<kNPlanes; iLayer++, tseed++, cIter++){
2686 Int_t det = (*cIter) ? (*cIter)->GetDetector() : -1;
eb2b4f91 2687 tseed->SetDetector(det);
43d6ad34 2688 tseed->SetTilt(hL[iLayer]);
2689 tseed->SetPadLength(padlength[iLayer]);
dd8059a8 2690 tseed->SetPadWidth(padwidth[iLayer]);
4d6aee34 2691 tseed->SetReconstructor(fkReconstructor);
eb2b4f91 2692 tseed->SetX0(det<0 ? fR[iLayer]+driftLength : x0[iLayer]);
d611c74f 2693 tseed->Init(GetRiemanFitter());
f29f13a6 2694 tseed->SetStandAlone(kTRUE);
d611c74f 2695 }
2696
2697 Bool_t isFake = kFALSE;
a2fbb6ec 2698 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) >= 2 && fkReconstructor->IsDebugStreaming()){
d611c74f 2699 if (c[0]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
2700 if (c[1]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
2701 if (c[2]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
2702
2703 Double_t xpos[4];
2704 for(Int_t l = 0; l < kNSeedPlanes; l++) xpos[l] = fSeedTB[l]->GetX();
2705 Float_t yref[4];
2706 for(int il=0; il<4; il++) yref[il] = cseed[planes[il]].GetYref(0);
2707 Int_t ll = c[3]->GetLabel(0);
2708 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2709 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2710 AliRieman *rim = GetRiemanFitter();
a2fbb6ec 2711 TTreeSRedirector &cs0 = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
d611c74f 2712 cs0 << "MakeSeeds0"
2713 <<"EventNumber=" << eventNumber
2714 <<"CandidateNumber=" << candidateNumber
2715 <<"isFake=" << isFake
2716 <<"config=" << config
2717 <<"label=" << ll
2718 <<"chi2z=" << chi2[0]
2719 <<"chi2y=" << chi2[1]
2720 <<"Y2exp=" << cond2[0]
2721 <<"Z2exp=" << cond2[1]
2722 <<"X0=" << xpos[0] //layer[sLayer]->GetX()
2723 <<"X1=" << xpos[1] //layer[sLayer + 1]->GetX()
2724 <<"X2=" << xpos[2] //layer[sLayer + 2]->GetX()
2725 <<"X3=" << xpos[3] //layer[sLayer + 3]->GetX()
2726 <<"yref0=" << yref[0]
2727 <<"yref1=" << yref[1]
2728 <<"yref2=" << yref[2]
2729 <<"yref3=" << yref[3]
2730 <<"c0.=" << c[0]
2731 <<"c1.=" << c[1]
2732 <<"c2.=" << c[2]
2733 <<"c3.=" << c[3]
2734 <<"Seed0.=" << &cseed[planes[0]]
2735 <<"Seed1.=" << &cseed[planes[1]]
2736 <<"Seed2.=" << &cseed[planes[2]]
2737 <<"Seed3.=" << &cseed[planes[3]]
2738 <<"RiemanFitter.=" << rim
2739 <<"\n";
2740 }
4d6aee34 2741 if(chi2[0] > fkReconstructor->GetRecoParam() ->GetChi2Z()/*7./(3. - sLayer)*//*iter*/){
980d5a2a 2742 AliDebug(3, Form("Filter on chi2Z [%f].", chi2[0]));
d611c74f 2743 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2744 continue;
2745 }
4d6aee34 2746 if(chi2[1] > fkReconstructor->GetRecoParam() ->GetChi2Y()/*1./(3. - sLayer)*//*iter*/){
980d5a2a 2747 AliDebug(3, Form("Filter on chi2Y [%f].", chi2[1]));
d611c74f 2748 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2749 continue;
2750 }
2751 //AliInfo("Passed chi2 filter.");
2752
2753 // try attaching clusters to tracklets
6e39bde4 2754 Int_t mlayers = 0;
980d5a2a 2755 AliTRDcluster *cl = NULL;
be24510a 2756 for(int iLayer=0; iLayer<kNSeedPlanes; iLayer++){
d611c74f 2757 Int_t jLayer = planes[iLayer];
980d5a2a 2758 Int_t nNotInChamber = 0;
f29f13a6 2759 if(!cseed[jLayer].AttachClusters(stack[jLayer], kTRUE)) continue;
980d5a2a 2760 if(/*fkReconstructor->IsHLT()*/kFALSE){
2761 cseed[jLayer].UpdateUsed();
2762 if(!cseed[jLayer].IsOK()) continue;
2763 }else{
2764 cseed[jLayer].Fit();
2765 cseed[jLayer].UpdateUsed();
2766 cseed[jLayer].ResetClusterIter();
2767 while((cl = cseed[jLayer].NextCluster())){
2768 if(!cl->IsInChamber()) nNotInChamber++;
2769 }
2770 //printf("clusters[%d], used[%d], not in chamber[%d]\n", cseed[jLayer].GetN(), cseed[jLayer].GetNUsed(), nNotInChamber);
2771 if(cseed[jLayer].GetN() - (cseed[jLayer].GetNUsed() + nNotInChamber) < 5) continue; // checking for Cluster which are not in chamber is a much stronger restriction on real data
2772 }
d611c74f 2773 mlayers++;
2774 }
be24510a 2775
2776 if(mlayers < kNSeedPlanes){
980d5a2a 2777 AliDebug(2, Form("Found only %d tracklets out of %d. Skip.", mlayers, kNSeedPlanes));
be24510a 2778 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2779 continue;
2780 }
2781
2782 // temporary exit door for the HLT
4d6aee34 2783 if(fkReconstructor->IsHLT()){
be24510a 2784 // attach clusters to extrapolation chambers
2785 for(int iLayer=0; iLayer<kNPlanes-kNSeedPlanes; iLayer++){
2786 Int_t jLayer = planesExt[iLayer];
2787 if(!(chamber = stack[jLayer])) continue;
980d5a2a 2788 if(!cseed[jLayer].AttachClusters(chamber, kTRUE)) continue;
5f1ae1e7 2789 cseed[jLayer].Fit();
be24510a 2790 }
4302c900 2791 fTrackQuality[ntracks] = 1.; // dummy value
2792 ntracks++;
218ba867 2793 if(ntracks == kMaxTracksStack) return ntracks;
4302c900 2794 cseed += 6;
2795 continue;
2796 }
2797
be24510a 2798
f29f13a6 2799 // Update Seeds and calculate Likelihood
d611c74f 2800 // fit tracklets and cook likelihood
f29f13a6 2801 FitTiltedRieman(&cseed[0], kTRUE);
2802 for(int iLayer=0; iLayer<kNSeedPlanes; iLayer++){
2803 Int_t jLayer = planes[iLayer];
2804 cseed[jLayer].Fit(kTRUE);
2805 }
91834b8d 2806 Double_t like = CookLikelihood(&cseed[0], planes); // to be checked
d611c74f 2807
4d6aee34 2808 if (TMath::Log(1.E-9 + like) < fkReconstructor->GetRecoParam() ->GetTrackLikelihood()){
980d5a2a 2809 AliDebug(3, Form("Filter on likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
d611c74f 2810 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2811 continue;
2812 }
2813 //AliInfo(Form("Passed likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
2814
2815 // book preliminary results
2816 seedQuality[ntracks] = like;
2817 fSeedLayer[ntracks] = config;/*sLayer;*/
2818
2819 // attach clusters to the extrapolation seeds
980d5a2a 2820 Int_t elayers(0);
be24510a 2821 for(int iLayer=0; iLayer<kNPlanes-kNSeedPlanes; iLayer++){
2822 Int_t jLayer = planesExt[iLayer];
d611c74f 2823 if(!(chamber = stack[jLayer])) continue;
d611c74f 2824
2825 // fit extrapolated seed
2826 if ((jLayer == 0) && !(cseed[1].IsOK())) continue;
2827 if ((jLayer == 5) && !(cseed[4].IsOK())) continue;
2828 AliTRDseedV1 pseed = cseed[jLayer];
f29f13a6 2829 if(!pseed.AttachClusters(chamber, kTRUE)) continue;
2830 pseed.Fit(kTRUE);
d611c74f 2831 cseed[jLayer] = pseed;
d611c74f 2832 FitTiltedRieman(cseed, kTRUE);
f29f13a6 2833 cseed[jLayer].Fit(kTRUE);
980d5a2a 2834 elayers++;
d611c74f 2835 }
2836
2837 // AliInfo("Extrapolation done.");
2838 // Debug Stream containing all the 6 tracklets
a2fbb6ec 2839 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) >= 2 && fkReconstructor->IsDebugStreaming()){
2840 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
d611c74f 2841 TLinearFitter *tiltedRieman = GetTiltedRiemanFitter();
2842 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2843 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2844 cstreamer << "MakeSeeds1"
2845 << "EventNumber=" << eventNumber
2846 << "CandidateNumber=" << candidateNumber
2847 << "S0.=" << &cseed[0]
2848 << "S1.=" << &cseed[1]
2849 << "S2.=" << &cseed[2]
2850 << "S3.=" << &cseed[3]
2851 << "S4.=" << &cseed[4]
2852 << "S5.=" << &cseed[5]
2853 << "FitterT.=" << tiltedRieman
2854 << "\n";
2855 }
2856
980d5a2a 2857 if(fkReconstructor->GetRecoParam()->HasImproveTracklets()){
2858 AliTRDseedV1 bseed[AliTRDgeometry::kNlayer];
2859 for(Int_t jLayer=AliTRDgeometry::kNlayer; jLayer--;) bseed[jLayer] = cseed[jLayer];
2860
2861 if(ImproveSeedQuality(stack, cseed) < mlayers+elayers){
2862 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2863 AliDebug(3, "Filter on improve seeds.");
2864 } else {
2865 // store results
2866 for(Int_t jLayer=AliTRDgeometry::kNlayer; jLayer--;) cseed[jLayer] = bseed[jLayer];
2867 }
d611c74f 2868 }
2869 //AliInfo("Improve seed quality done.");
2870
2871 // fit full track and cook likelihoods
2872 // Double_t curv = FitRieman(&cseed[0], chi2);
2873 // Double_t chi2ZF = chi2[0] / TMath::Max((mlayers - 3.), 1.);
2874 // Double_t chi2RF = chi2[1] / TMath::Max((mlayers - 3.), 1.);
2875
2876 // do the final track fitting (Once with vertex constraint and once without vertex constraint)
2877 Double_t chi2Vals[3];
6e39bde4 2878 chi2Vals[0] = FitTiltedRieman(&cseed[0], kTRUE);
a2fbb6ec 2879 if(fkReconstructor->GetRecoParam()->IsVertexConstrained())
d611c74f 2880 chi2Vals[1] = FitTiltedRiemanConstraint(&cseed[0], GetZ()); // Do Vertex Constrained fit if desired
2881 else
2882 chi2Vals[1] = 1.;
2883 chi2Vals[2] = GetChi2Z(&cseed[0]) / TMath::Max((mlayers - 3.), 1.);
2884 // Chi2 definitions in testing stage
2885 //chi2Vals[2] = GetChi2ZTest(&cseed[0]);
2886 fTrackQuality[ntracks] = CalculateTrackLikelihood(&cseed[0], &chi2Vals[0]);
2887 //AliInfo("Hyperplane fit done\n");
fac58f00 2888
a2fbb6ec 2889 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) >= 2 && fkReconstructor->IsDebugStreaming()){
2890 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
d611c74f 2891 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2892 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2893 TLinearFitter *fitterTC = GetTiltedRiemanFitterConstraint();
2894 TLinearFitter *fitterT = GetTiltedRiemanFitter();
91834b8d 2895 Int_t ncls = 0;
2896 for(Int_t iseed = 0; iseed < kNPlanes; iseed++){
2897 ncls += cseed[iseed].IsOK() ? cseed[iseed].GetN2() : 0;
2898 }
d611c74f 2899 cstreamer << "MakeSeeds2"
2900 << "EventNumber=" << eventNumber
2901 << "CandidateNumber=" << candidateNumber
2902 << "Chi2TR=" << chi2Vals[0]
2903 << "Chi2TC=" << chi2Vals[1]
2904 << "Nlayers=" << mlayers
91834b8d 2905 << "NClusters=" << ncls
d611c74f 2906 << "Like=" << like
2907 << "S0.=" << &cseed[0]
2908 << "S1.=" << &cseed[1]
2909 << "S2.=" << &cseed[2]
2910 << "S3.=" << &cseed[3]
2911 << "S4.=" << &cseed[4]
2912 << "S5.=" << &cseed[5]
d611c74f 2913 << "FitterT.=" << fitterT
2914 << "FitterTC.=" << fitterTC
2915 << "\n";
2916 }
2917
2918 ntracks++;
2919 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2920 if(ntracks == kMaxTracksStack){
2921 AliWarning(Form("Number of seeds reached maximum allowed (%d) in stack.", kMaxTracksStack));
2922 return ntracks;
2923 }
2924 cseed += 6;
41702fec 2925 }
2926 }
2927 }
41702fec 2928
2929 return ntracks;
e4f2f73d 2930}
2931
2932//_____________________________________________________________________________
e17f4785 2933AliTRDtrackV1* AliTRDtrackerV1::MakeTrack(AliTRDseedV1 * const seeds, Double_t *params)
e4f2f73d 2934{
afa21247 2935//
2936// Build a TRD track out of tracklet candidates
2937//
2938// Parameters :
2939// seeds : array of tracklets
2940// params : array of track parameters as they are estimated by stand alone tracker. 7 elements.
2941// [0] - radial position of the track at reference point
2942// [1] - y position of the fit at [0]
2943// [2] - z position of the fit at [0]
2944// [3] - snp of the first tracklet
2945// [4] - tgl of the first tracklet
2946// [5] - curvature of the Riemann fit - 1/pt
2947// [6] - sector rotation angle
2948//
2949// Output :
2950// The TRD track.
2951//
2952// Initialize the TRD track based on the parameters of the fit and a parametric covariance matrix
2953// (diagonal with constant variance terms TODO - correct parameterization)
2954//
2955// In case of HLT just register the tracklets in the tracker and return values of the Riemann fit. For the
2956// offline case perform a full Kalman filter on the already found tracklets (see AliTRDtrackerV1::FollowBackProlongation()
2957// for details). Do also MC label calculation and PID if propagation successfully.
41702fec 2958
afa21247 2959
41702fec 2960 Double_t alpha = AliTRDgeometry::GetAlpha();
2961 Double_t shift = AliTRDgeometry::GetAlpha()/2.0;
2962 Double_t c[15];
2963
afa21247 2964 c[ 0] = 0.2; // s^2_y
2965 c[ 1] = 0.0; c[ 2] = 2.0; // s^2_z
2966 c[ 3] = 0.0; c[ 4] = 0.0; c[ 5] = 0.02; // s^2_snp
2967 c[ 6] = 0.0; c[ 7] = 0.0; c[ 8] = 0.0; c[ 9] = 0.1; // s^2_tgl
2968 c[10] = 0.0; c[11] = 0.0; c[12] = 0.0; c[13] = 0.0; c[14] = params[5]*params[5]*0.01; // s^2_1/pt
41702fec 2969
d20df6fc 2970 AliTRDtrackV1 track(seeds, &params[1], c, params[0], params[6]*alpha+shift);
2971 track.PropagateTo(params[0]-5.0);
4d6aee34 2972 AliTRDseedV1 *ptrTracklet = NULL;
9887cc9f 2973
2974 // skip Kalman filter for HLT
5f1ae1e7 2975 if(/*fkReconstructor->IsHLT()*/kFALSE){
9887cc9f 2976 for (Int_t jLayer = 0; jLayer < AliTRDgeometry::kNlayer; jLayer++) {
2977 track.UnsetTracklet(jLayer);
e17f4785 2978 ptrTracklet = &seeds[jLayer];
9887cc9f 2979 if(!ptrTracklet->IsOK()) continue;
5f1ae1e7 2980 if(TMath::Abs(ptrTracklet->GetYref(1) - ptrTracklet->GetYfit(1)) >= .2) continue; // check this condition with Marian
9887cc9f 2981 ptrTracklet = SetTracklet(ptrTracklet);
f29f13a6 2982 ptrTracklet->UseClusters();
91834b8d 2983 track.SetTracklet(ptrTracklet, fTracklets->GetEntriesFast()-1);
2984 }
d78d7df0 2985 AliTRDtrackV1 *ptrTrack = SetTrack(&track);
799e38d7 2986 ptrTrack->CookPID();
5f1ae1e7 2987 ptrTrack->CookLabel(.9);
4d6aee34 2988 ptrTrack->SetReconstructor(fkReconstructor);
d78d7df0 2989 return ptrTrack;
91834b8d 2990 }
393fda1c 2991
b1135f96 2992 // prevent the error message in AliTracker::MeanMaterialBudget: "start point out of geometry"
2993 if(TMath::Abs(track.GetX()) + TMath::Abs(track.GetY()) + TMath::Abs(track.GetZ()) > 10000)
17e0e535 2994 return NULL;
2995
d20df6fc 2996 track.ResetCovariance(1);
e79f8eb0 2997 Int_t nc = TMath::Abs(FollowBackProlongation(track));
a2fbb6ec 2998 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) > 5 && fkReconstructor->IsDebugStreaming()){
393fda1c 2999 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
3000 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
3001 Double_t p[5]; // Track Params for the Debug Stream
3002 track.GetExternalParameters(params[0], p);
a2fbb6ec 3003 TTreeSRedirector &cs = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
393fda1c 3004 cs << "MakeTrack"
3005 << "EventNumber=" << eventNumber
3006 << "CandidateNumber=" << candidateNumber
3007 << "nc=" << nc
3008 << "X=" << params[0]
3009 << "Y=" << p[0]
3010 << "Z=" << p[1]
3011 << "snp=" << p[2]
3012 << "tnd=" << p[3]
3013 << "crv=" << p[4]
3014 << "Yin=" << params[1]
3015 << "Zin=" << params[2]
3016 << "snpin=" << params[3]
3017 << "tndin=" << params[4]
3018 << "crvin=" << params[5]
3019 << "track.=" << &track
3020 << "\n";
3021 }
4d6aee34 3022 if (nc < 30) return NULL;
d20df6fc 3023
3024 AliTRDtrackV1 *ptrTrack = SetTrack(&track);
4d6aee34 3025 ptrTrack->SetReconstructor(fkReconstructor);
48f8adf3 3026 ptrTrack->CookLabel(.9);
24253b0a 3027
d20df6fc 3028 // computes PID for track
3029 ptrTrack->CookPID();
3030 // update calibration references using this track
48f8adf3 3031 AliTRDCalibraFillHisto *calibra = AliTRDCalibraFillHisto::Instance();
3032 if (!calibra){
3033 AliInfo("Could not get Calibra instance\n");
3034 if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(ptrTrack);
3035 }
d20df6fc 3036 return ptrTrack;
e4f2f73d 3037}
3038
0906e73e 3039
3040//____________________________________________________________________
eb38ed55 3041Int_t AliTRDtrackerV1::ImproveSeedQuality(AliTRDtrackingChamber **stack, AliTRDseedV1 *cseed)
e4f2f73d 3042{
41702fec 3043 //
3044 // Sort tracklets according to "quality" and try to "improve" the first 4 worst
3045 //
3046 // Parameters :
3047 // layers : Array of propagation layers for a stack/supermodule
3048 // cseed : Array of 6 seeding tracklets which has to be improved
3049 //
f29f13a6 3050 // Output :
41702fec 3051 // cssed : Improved seeds
3052 //
3053 // Detailed description
3054 //
3055 // Iterative procedure in which new clusters are searched for each
3056 // tracklet seed such that the seed quality (see AliTRDseed::GetQuality())
3057 // can be maximized. If some optimization is found the old seeds are replaced.
3058 //
3059 // debug level: 7
3060 //
3061
3062 // make a local working copy
4d6aee34 3063 AliTRDtrackingChamber *chamber = NULL;
980d5a2a 3064 AliTRDseedV1 bseed[AliTRDgeometry::kNlayer];
3065
3066 Float_t quality(0.),
3067 lQuality[] = {1.e3, 1.e3, 1.e3, 1.e3, 1.e3, 1.e3};
3068 Int_t rLayers(0);
3069 for(Int_t jLayer=AliTRDgeometry::kNlayer; jLayer--;){
3070 bseed[jLayer] = cseed[jLayer];
3071 if(!bseed[jLayer].IsOK()) continue;
3072 rLayers++;
3073 lQuality[jLayer] = bseed[jLayer].GetQuality(kTRUE);
3074 quality += lQuality[jLayer];
3075 }
3076 Float_t chi2 = FitTiltedRieman(bseed, kTRUE);
41702fec 3077
3078 for (Int_t iter = 0; iter < 4; iter++) {
560e5c05 3079 AliDebug(2, Form("Iter[%d] Q[%f] chi2[%f]", iter, quality, chi2));
3080
980d5a2a 3081 // Try better cluster set
3082 Int_t nLayers(0); Float_t qualitynew(0.);
3083 Int_t indexes[6];
3084 TMath::Sort(Int_t(AliTRDgeometry::kNlayer), lQuality, indexes, kFALSE);
3085 for(Int_t jLayer=AliTRDgeometry::kNlayer; jLayer--;) {
3086 Int_t bLayer = indexes[jLayer];
3087 bseed[bLayer].Reset("c");
41702fec 3088 if(!(chamber = stack[bLayer])) continue;
980d5a2a 3089 if(!bseed[bLayer].AttachClusters(chamber, kTRUE)) continue;
f29f13a6 3090 bseed[bLayer].Fit(kTRUE);
980d5a2a 3091 if(!bseed[bLayer].IsOK()) continue;
3092 nLayers++;
3093 lQuality[jLayer] = bseed[jLayer].GetQuality(kTRUE);
3094 qualitynew += lQuality[jLayer];
41702fec 3095 }
980d5a2a 3096 if(rLayers > nLayers){
3097 AliDebug(1, Form("Lost %d tracklets while improving.", rLayers-nLayers));
3098 break;
3099 } else rLayers=nLayers;
3100
3101 if(qualitynew >= quality){
3102 AliDebug(4, Form("Quality worsen in iter[%d].", iter));
3103 break;
3104 } else quality = qualitynew;
3105
3106 // try improve track parameters
3107 AliTRDseedV1 tseed[AliTRDgeometry::kNlayer];
3108 for(Int_t jLayer=AliTRDgeometry::kNlayer; jLayer--;) tseed[jLayer]=bseed[jLayer];
3109 Float_t chi2new = FitTiltedRieman(tseed, kTRUE);
3110 if(chi2new > chi2){
3111 AliDebug(4, Form("Chi2 worsen in iter[%d].", iter));
3112 break;
3113 } else chi2 = chi2new;
3114
3115 // store better tracklets
3116 for(Int_t jLayer=AliTRDgeometry::kNlayer; jLayer--;) bseed[jLayer]=tseed[jLayer];
3117
41702fec 3118
a2fbb6ec 3119 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kTracker) >= 7 && fkReconstructor->IsDebugStreaming()){
41702fec 3120 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
3121 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
3122 TLinearFitter *tiltedRieman = GetTiltedRiemanFitter();
a2fbb6ec 3123 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
41702fec 3124 cstreamer << "ImproveSeedQuality"
980d5a2a 3125 << "EventNumber=" << eventNumber
3126 << "CandidateNumber=" << candidateNumber
3127 << "Iteration=" << iter
3128 << "S0.=" << &bseed[0]
3129 << "S1.=" << &bseed[1]
3130 << "S2.=" << &bseed[2]
3131 << "S3.=" << &bseed[3]
3132 << "S4.=" << &bseed[4]
3133 << "S5.=" << &bseed[5]
3134 << "FitterT.=" << tiltedRieman
3135 << "\n";
41702fec 3136 }
3137 } // Loop: iter
41702fec 3138 // we are sure that at least 2 tracklets are OK !
980d5a2a 3139 return rLayers;
e4f2f73d 3140}
3141
eb38ed55 3142//_________________________________________________________________________
4d6aee34 3143Double_t AliTRDtrackerV1::CalculateTrackLikelihood(const AliTRDseedV1 *const tracklets, Double_t *chi2){
41702fec 3144 //
3145 // Calculates the Track Likelihood value. This parameter serves as main quality criterion for
3146 // the track selection
3147 // The likelihood value containes:
3148 // - The chi2 values from the both fitters and the chi2 values in z-direction from a linear fit
3149 // - The Sum of the Parameter |slope_ref - slope_fit|/Sigma of the tracklets
3150 // For all Parameters an exponential dependency is used
3151 //
3152 // Parameters: - Array of tracklets (AliTRDseedV1) related to the track candidate
3153 // - Array of chi2 values:
3154 // * Non-Constrained Tilted Riemann fit
3155 // * Vertex-Constrained Tilted Riemann fit
3156 // * z-Direction from Linear fit
3157 // Output: - The calculated track likelihood
3158 //
3159 // debug level 2
3160 //
3161
f29f13a6 3162 Double_t chi2phi = 0, nLayers = 0;
41702fec 3163 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
3164 if(!tracklets[iLayer].IsOK()) continue;
f29f13a6 3165 chi2phi += tracklets[iLayer].GetChi2Phi();
41702fec 3166 nLayers++;
3167 }
f29f13a6 3168 chi2phi /= Float_t (nLayers - 2.0);
41702fec 3169
3170 Double_t likeChi2Z = TMath::Exp(-chi2[2] * 0.14); // Chi2Z
a2fbb6ec 3171 Double_t likeChi2TC = (fkReconstructor->GetRecoParam()->IsVertexConstrained()) ?
d20df6fc 3172 TMath::Exp(-chi2[1] * 0.677) : 1; // Constrained Tilted Riemann
6e39bde4 3173 Double_t likeChi2TR = TMath::Exp(-chi2[0] * 0.0078); // Non-constrained Tilted Riemann
3174 Double_t likeChi2Phi= TMath::Exp(-chi2phi * 3.23);//3.23
f29f13a6 3175 Double_t trackLikelihood = likeChi2Z * likeChi2TR * likeChi2Phi;
41702fec 3176
a2fbb6ec 3177 if(fkReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::