]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TRD/AliTRDtrackerV1.cxx
Coding rules
[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"
e4f2f73d 52#include "AliTRDseedV1.h"
0906e73e 53#include "AliTRDtrackV1.h"
bb56afff 54#include "AliTRDtrackerV1.h"
55#include "AliTRDtrackerDebug.h"
56#include "AliTRDtrackingChamber.h"
57#include "AliTRDchamberTimeBin.h"
58
e4f2f73d 59ClassImp(AliTRDtrackerV1)
eb38ed55 60
eb38ed55 61const Float_t AliTRDtrackerV1::fgkMinClustersInTrack = 0.5; //
62const Float_t AliTRDtrackerV1::fgkLabelFraction = 0.8; //
63const Double_t AliTRDtrackerV1::fgkMaxChi2 = 12.0; //
64const Double_t AliTRDtrackerV1::fgkMaxSnp = 0.95; // Maximum local sine of the azimuthal angle
65const Double_t AliTRDtrackerV1::fgkMaxStep = 2.0; // Maximal step size in propagation
d76231c8 66Double_t AliTRDtrackerV1::fgTopologicQA[kNConfigs] = {
6e39bde4 67 0.5112, 0.5112, 0.5112, 0.0786, 0.0786,
41702fec 68 0.0786, 0.0786, 0.0579, 0.0579, 0.0474,
69 0.0474, 0.0408, 0.0335, 0.0335, 0.0335
e165b64b 70};
71const Double_t AliTRDtrackerV1::fgkX0[kNPlanes] = {
72 300.2, 312.8, 325.4, 338.0, 350.6, 363.2};
2985ffcb 73Int_t AliTRDtrackerV1::fgNTimeBins = 0;
4d6aee34 74AliRieman* AliTRDtrackerV1::fgRieman = NULL;
75TLinearFitter* AliTRDtrackerV1::fgTiltedRieman = NULL;
76TLinearFitter* AliTRDtrackerV1::fgTiltedRiemanConstrained = NULL;
e4f2f73d 77
78//____________________________________________________________________
3a039a31 79AliTRDtrackerV1::AliTRDtrackerV1(AliTRDReconstructor *rec)
41702fec 80 :AliTracker()
4d6aee34 81 ,fkReconstructor(NULL)
82 ,fGeom(NULL)
83 ,fClusters(NULL)
84 ,fTracklets(NULL)
85 ,fTracks(NULL)
41702fec 86 ,fSieveSeeding(0)
e4f2f73d 87{
41702fec 88 //
89 // Default constructor.
90 //
eb2b4f91 91
92 SetReconstructor(rec); // initialize reconstructor
93
94 // initialize geometry
95 if(!AliGeomManager::GetGeometry()){
96 AliFatal("Could not get geometry.");
97 }
98 fGeom = new AliTRDgeometry();
99 fGeom->CreateClusterMatrixArray();
4d6aee34 100 TGeoHMatrix *matrix = NULL;
eb2b4f91 101 Double_t loc[] = {0., 0., 0.};
102 Double_t glb[] = {0., 0., 0.};
103 for(Int_t ily=kNPlanes; ily--;){
e165b64b 104 Int_t ism = 0;
105 while(!(matrix = fGeom->GetClusterMatrix(AliTRDgeometry::GetDetector(ily, 2, ism)))) ism++;
106 if(!matrix){
107 AliError(Form("Could not get transformation matrix for layer %d. Use default.", ily));
108 fR[ily] = fgkX0[ily];
eb2b4f91 109 continue;
110 }
111 matrix->LocalToMaster(loc, glb);
112 fR[ily] = glb[0]+ AliTRDgeometry::AnodePos()-.5*AliTRDgeometry::AmThick() - AliTRDgeometry::DrThick();
113 }
114
115 // initialize calibration values
4d6aee34 116 AliTRDcalibDB *trd = NULL;
a7ac01d2 117 if (!(trd = AliTRDcalibDB::Instance())) {
eb2b4f91 118 AliFatal("Could not get calibration.");
41702fec 119 }
a7ac01d2 120 if(!fgNTimeBins) fgNTimeBins = trd->GetNumberOfTimeBins();
41702fec 121
eb2b4f91 122 // initialize cluster containers
053767a4 123 for (Int_t isector = 0; isector < AliTRDgeometry::kNsector; isector++) new(&fTrSec[isector]) AliTRDtrackingSector(fGeom, isector);
3a039a31 124
eb2b4f91 125 // initialize arrays
126 memset(fTrackQuality, 0, kMaxTracksStack*sizeof(Double_t));
127 memset(fSeedLayer, 0, kMaxTracksStack*sizeof(Int_t));
128 memset(fSeedTB, 0, kNSeedPlanes*sizeof(AliTRDchamberTimeBin*));
eb38ed55 129}
130
e4f2f73d 131//____________________________________________________________________
132AliTRDtrackerV1::~AliTRDtrackerV1()
133{
41702fec 134 //
135 // Destructor
136 //
137
4d6aee34 138 if(fgRieman) delete fgRieman; fgRieman = NULL;
139 if(fgTiltedRieman) delete fgTiltedRieman; fgTiltedRieman = NULL;
140 if(fgTiltedRiemanConstrained) delete fgTiltedRiemanConstrained; fgTiltedRiemanConstrained = NULL;
d611c74f 141 for(Int_t isl =0; isl<kNSeedPlanes; isl++) if(fSeedTB[isl]) delete fSeedTB[isl];
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 }
180 AliInfo(Form("Number of found tracks : %d", ntracks));
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");
274
eb2b4f91 275 // Define scalers
276 Int_t nFound = 0, // number of tracks found
277 nSeeds = 0, // total number of ESD seeds
278 nTRDseeds= 0, // number of seeds in the TRD acceptance
279 nTPCseeds= 0; // number of TPC seeds
41702fec 280 Float_t foundMin = 20.0;
281
4d6aee34 282 Float_t *quality = NULL;
283 Int_t *index = NULL;
eb2b4f91 284 nSeeds = event->GetNumberOfTracks();
285 // Sort tracks according to quality
286 // (covariance in the yz plane)
287 if(nSeeds){
288 quality = new Float_t[nSeeds];
289 index = new Int_t[nSeeds];
290 for (Int_t iSeed = nSeeds; iSeed--;) {
d611c74f 291 AliESDtrack *seed = event->GetTrack(iSeed);
292 Double_t covariance[15];
293 seed->GetExternalCovariance(covariance);
294 quality[iSeed] = covariance[0] + covariance[2];
295 }
eb2b4f91 296 TMath::Sort(nSeeds, quality, index,kFALSE);
41702fec 297 }
41702fec 298
eb2b4f91 299 // Propagate all seeds
41702fec 300 Int_t expectedClr;
301 AliTRDtrackV1 track;
eb2b4f91 302 for (Int_t iSeed = 0; iSeed < nSeeds; iSeed++) {
41702fec 303
304 // Get the seeds in sorted sequence
305 AliESDtrack *seed = event->GetTrack(index[iSeed]);
eb2b4f91 306 Float_t p4 = seed->GetC(seed->GetBz());
41702fec 307
308 // Check the seed status
309 ULong_t status = seed->GetStatus();
310 if ((status & AliESDtrack::kTPCout) == 0) continue;
311 if ((status & AliESDtrack::kTRDout) != 0) continue;
eb2b4f91 312
313 // Propagate to the entrance in the TRD mother volume
41702fec 314 new(&track) AliTRDtrackV1(*seed);
eb2b4f91 315 if(AliTRDgeometry::GetXtrdBeg() > (fgkMaxStep + track.GetX()) && !PropagateToX(track, AliTRDgeometry::GetXtrdBeg(), fgkMaxStep)){
316 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
317 continue;
318 }
319 if(!AdjustSector(&track)){
320 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
321 continue;
322 }
323 if(TMath::Abs(track.GetSnp()) > fgkMaxSnp) {
324 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
325 continue;
326 }
327
328 nTPCseeds++;
e4d4864b 329
eb2b4f91 330 // store track status at TRD entrance
76b60503 331 seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
ae3fbe1f 332
eb2b4f91 333 // prepare track and do propagation in the TRD
4d6aee34 334 track.SetReconstructor(fkReconstructor);
eb2b4f91 335 track.SetKink(Bool_t(seed->GetKinkIndex(0)));
336 expectedClr = FollowBackProlongation(track);
337 // check if track entered the TRD fiducial volume
338 if(track.GetTrackLow()){
339 seed->UpdateTrackParams(&track, AliESDtrack::kTRDin);
340 nTRDseeds++;
341 }
342 // check if track was stopped in the TRD
343 if (expectedClr<0){
344 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
345 continue;
346 }
ae3fbe1f 347
348 if(expectedClr){
eb2b4f91 349 nFound++;
41702fec 350 // computes PID for track
351 track.CookPID();
352 // update calibration references using this track
353 if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(&track);
354 // save calibration object
4d6aee34 355 if (fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 0){
224f357f 356 AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(track);
357 calibTrack->SetOwner();
358 seed->AddCalibObject(calibTrack);
359 }
360 //update ESD track
41702fec 361 if ((track.GetNumberOfClusters() > 15) && (track.GetNumberOfClusters() > 0.5*expectedClr)) {
2389e96f 362 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
41702fec 363 track.UpdateESDtrack(seed);
41702fec 364 }
365 }
366
27fbeba8 367 if ((TMath::Abs(track.GetC(track.GetBz()) - p4) / TMath::Abs(p4) < 0.2) ||(track.Pt() > 0.8)) {
e3cf3d02 368
41702fec 369 // Make backup for back propagation
41702fec 370 Int_t foundClr = track.GetNumberOfClusters();
371 if (foundClr >= foundMin) {
41702fec 372 track.CookLabel(1. - fgkLabelFraction);
f29f13a6 373 //if(track.GetBackupTrack()) UseClusters(track.GetBackupTrack());
41702fec 374
375 // Sign only gold tracks
376 if (track.GetChi2() / track.GetNumberOfClusters() < 4) {
f29f13a6 377 //if ((seed->GetKinkIndex(0) == 0) && (track.Pt() < 1.5)) UseClusters(&track);
41702fec 378 }
379 Bool_t isGold = kFALSE;
380
381 // Full gold track
382 if (track.GetChi2() / track.GetNumberOfClusters() < 5) {
383 if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
384
385 isGold = kTRUE;
386 }
387
388 // Almost gold track
389 if ((!isGold) && (track.GetNCross() == 0) && (track.GetChi2() / track.GetNumberOfClusters() < 7)) {
390 //seed->UpdateTrackParams(track, AliESDtrack::kTRDbackup);
391 if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
392
393 isGold = kTRUE;
394 }
395
396 if ((!isGold) && (track.GetBackupTrack())) {
397 if ((track.GetBackupTrack()->GetNumberOfClusters() > foundMin) && ((track.GetBackupTrack()->GetChi2()/(track.GetBackupTrack()->GetNumberOfClusters()+1)) < 7)) {
398 seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
399 isGold = kTRUE;
400 }
401 }
41702fec 402 }
403 }
404
eb2b4f91 405 // Propagation to the TOF
406 if(!(seed->GetStatus()&AliESDtrack::kTRDStop)) {
407 Int_t sm = track.GetSector();
408 // default value in case we have problems with the geometry.
409 Double_t xtof = 371.;
410 //Calculate radial position of the beginning of the TOF
411 //mother volume. In order to avoid mixing of the TRD
412 //and TOF modules some hard values are needed. This are:
413 //1. The path to the TOF module.
414 //2. The width of the TOF (29.05 cm)
415 //(with the help of Annalisa de Caro Mar-17-2009)
416 if(gGeoManager){
417 gGeoManager->cd(Form("/ALIC_1/B077_1/BSEGMO%d_1/BTOF%d_1", sm, sm));
4d6aee34 418 TGeoHMatrix *m = NULL;
eb2b4f91 419 Double_t loc[]={0., 0., -.5*29.05}, glob[3];
420
421 if((m=gGeoManager->GetCurrentMatrix())){
422 m->LocalToMaster(loc, glob);
423 xtof = TMath::Sqrt(glob[0]*glob[0]+glob[1]*glob[1]);
424 }
41702fec 425 }
672b48bd 426 if(xtof > (fgkMaxStep + track.GetX()) && !PropagateToX(track, xtof, fgkMaxStep)){
427 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
428 continue;
429 }
430 if(!AdjustSector(&track)){
431 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
432 continue;
433 }
434 if(TMath::Abs(track.GetSnp()) > fgkMaxSnp){
435 seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
436 continue;
437 }
eb2b4f91 438 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
439 // TODO obsolete - delete
440 seed->SetTRDQuality(track.StatusForTOF());
41702fec 441 }
41702fec 442 seed->SetTRDBudget(track.GetBudget(0));
443 }
d611c74f 444 if(index) delete [] index;
445 if(quality) delete [] quality;
41702fec 446
d4cf71ca 447 AliInfo(Form("Number of seeds: TPCout[%d] TRDin[%d]", nTPCseeds, nTRDseeds));
448 AliInfo(Form("Number of tracks: TRDout[%d]", nFound));
449
d611c74f 450 // run stand alone tracking
4d6aee34 451 if (fkReconstructor->IsSeeding()) Clusters2Tracks(event);
41702fec 452
453 return 0;
0906e73e 454}
455
456
457//____________________________________________________________________
458Int_t AliTRDtrackerV1::RefitInward(AliESDEvent *event)
459{
41702fec 460 //
461 // Refits tracks within the TRD. The ESD event is expected to contain seeds
462 // at the outer part of the TRD.
463 // The tracks are propagated to the innermost time bin
464 // of the TRD and the ESD event is updated
465 // Origin: Thomas KUHR (Thomas.Kuhr@cern.ch)
466 //
467
468 Int_t nseed = 0; // contor for loaded seeds
469 Int_t found = 0; // contor for updated TRD tracks
470
471
472 AliTRDtrackV1 track;
473 for (Int_t itrack = 0; itrack < event->GetNumberOfTracks(); itrack++) {
474 AliESDtrack *seed = event->GetTrack(itrack);
475 new(&track) AliTRDtrackV1(*seed);
476
477 if (track.GetX() < 270.0) {
478 seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
479 continue;
480 }
481
e3cf3d02 482 // reject tracks which failed propagation in the TRD or
483 // are produced by the TRD stand alone tracker
41702fec 484 ULong_t status = seed->GetStatus();
e3cf3d02 485 if(!(status & AliESDtrack::kTRDout)) continue;
486 if(!(status & AliESDtrack::kTRDin)) continue;
41702fec 487 nseed++;
488
489 track.ResetCovariance(50.0);
490
491 // do the propagation and processing
492 Bool_t kUPDATE = kFALSE;
493 Double_t xTPC = 250.0;
494 if(FollowProlongation(track)){
495 // Prolongate to TPC
496 if (PropagateToX(track, xTPC, fgkMaxStep)) { // -with update
e3cf3d02 497 seed->UpdateTrackParams(&track, AliESDtrack::kTRDrefit);
498 found++;
499 kUPDATE = kTRUE;
500 }
501
502 // Update the friend track
4d6aee34 503 if (fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 0){
504 TObject *o = NULL; Int_t ic = 0;
505 AliTRDtrackV1 *calibTrack = NULL;
e3cf3d02 506 while((o = seed->GetCalibObject(ic++))){
507 if(!(calibTrack = dynamic_cast<AliTRDtrackV1*>(o))) continue;
508 calibTrack->SetTrackHigh(track.GetTrackHigh());
509 }
41702fec 510 }
e3cf3d02 511 }
41702fec 512
513 // Prolongate to TPC without update
514 if(!kUPDATE) {
515 AliTRDtrackV1 tt(*seed);
e3cf3d02 516 if (PropagateToX(tt, xTPC, fgkMaxStep)) seed->UpdateTrackParams(&tt, AliESDtrack::kTRDbackup);
41702fec 517 }
518 }
519 AliInfo(Form("Number of loaded seeds: %d",nseed));
520 AliInfo(Form("Number of found tracks from loaded seeds: %d",found));
521
522 return 0;
0906e73e 523}
524
0906e73e 525//____________________________________________________________________
526Int_t AliTRDtrackerV1::FollowProlongation(AliTRDtrackV1 &t)
527{
41702fec 528 // Extrapolates the TRD track in the TPC direction.
529 //
530 // Parameters
531 // t : the TRD track which has to be extrapolated
532 //
533 // Output
534 // number of clusters attached to the track
535 //
536 // Detailed description
537 //
538 // Starting from current radial position of track <t> this function
539 // extrapolates the track through the 6 TRD layers. The following steps
540 // are being performed for each plane:
541 // 1. prepare track:
542 // a. get plane limits in the local x direction
543 // b. check crossing sectors
544 // c. check track inclination
545 // 2. search tracklet in the tracker list (see GetTracklet() for details)
546 // 3. evaluate material budget using the geo manager
547 // 4. propagate and update track using the tracklet information.
548 //
549 // Debug level 2
550 //
551
e3cf3d02 552 Bool_t kStoreIn = kTRUE;
41702fec 553 Int_t nClustersExpected = 0;
e3cf3d02 554 for (Int_t iplane = kNPlanes; iplane--;) {
41702fec 555 Int_t index = 0;
556 AliTRDseedV1 *tracklet = GetTracklet(&t, iplane, index);
557 if(!tracklet) continue;
558 if(!tracklet->IsOK()) AliWarning("tracklet not OK");
559
e3cf3d02 560 Double_t x = tracklet->GetX();//GetX0();
41702fec 561 // reject tracklets which are not considered for inward refit
562 if(x > t.GetX()+fgkMaxStep) continue;
563
564 // append tracklet to track
565 t.SetTracklet(tracklet, index);
566
567 if (x < (t.GetX()-fgkMaxStep) && !PropagateToX(t, x+fgkMaxStep, fgkMaxStep)) break;
568 if (!AdjustSector(&t)) break;
569
570 // Start global position
571 Double_t xyz0[3];
572 t.GetXYZ(xyz0);
573
574 // End global position
575 Double_t alpha = t.GetAlpha(), y, z;
576 if (!t.GetProlongation(x,y,z)) break;
577 Double_t xyz1[3];
578 xyz1[0] = x * TMath::Cos(alpha) - y * TMath::Sin(alpha);
579 xyz1[1] = x * TMath::Sin(alpha) + y * TMath::Cos(alpha);
580 xyz1[2] = z;
581
51a23065 582 Double_t length = TMath::Sqrt(
583 (xyz0[0]-xyz1[0])*(xyz0[0]-xyz1[0]) +
584 (xyz0[1]-xyz1[1])*(xyz0[1]-xyz1[1]) +
585 (xyz0[2]-xyz1[2])*(xyz0[2]-xyz1[2])
586 );
587 if(length>0.){
588 // Get material budget
589 Double_t param[7];
590 if(AliTracker::MeanMaterialBudget(xyz0, xyz1, param)<=0.) break;
591 Double_t xrho= param[0]*param[4];
592 Double_t xx0 = param[1]; // Get mean propagation parameters
593
594 // Propagate and update
595 t.PropagateTo(x, xx0, xrho);
596 if (!AdjustSector(&t)) break;
597 }
e3cf3d02 598 if(kStoreIn){
599 t.SetTrackHigh();
600 kStoreIn = kFALSE;
601 }
602
b72f4eaf 603 Double_t cov[3]; tracklet->GetCovAt(x, cov);
604 Double_t p[2] = { tracklet->GetY(), tracklet->GetZ()};
605 Double_t chi2 = ((AliExternalTrackParam)t).GetPredictedChi2(p, cov);
606 if (chi2 < 1e+10 && t.Update(p, cov, chi2)){
41702fec 607 nClustersExpected += tracklet->GetN();
608 }
609 }
610
4d6aee34 611 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 1){
41702fec 612 Int_t index;
51a23065 613 for(int iplane=0; iplane<AliTRDgeometry::kNlayer; iplane++){
41702fec 614 AliTRDseedV1 *tracklet = GetTracklet(&t, iplane, index);
615 if(!tracklet) continue;
616 t.SetTracklet(tracklet, index);
617 }
618
619 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
4d6aee34 620 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
eb2b4f91 621 AliTRDtrackV1 track(t);
622 track.SetOwner();
41702fec 623 cstreamer << "FollowProlongation"
624 << "EventNumber=" << eventNumber
625 << "ncl=" << nClustersExpected
eb2b4f91 626 << "track.=" << &track
41702fec 627 << "\n";
628 }
629
630 return nClustersExpected;
0906e73e 631
632}
633
634//_____________________________________________________________________________
635Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
636{
afa21247 637// Extrapolates/Build the TRD track in the TOF direction.
638//
639// Parameters
640// t : the TRD track which has to be extrapolated
641//
642// Output
643// number of clusters attached to the track
644//
645// Starting from current radial position of track <t> this function
646// extrapolates the track through the 6 TRD layers. The following steps
647// are being performed for each plane:
648// 1. Propagate track to the entrance of the next chamber:
649// - get chamber limits in the radial direction
650// - check crossing sectors
651// - check track inclination
652// - check track prolongation against boundary conditions (see exclusion boundaries on AliTRDgeometry::IsOnBoundary())
653// 2. Build tracklet (see AliTRDseed::AttachClusters() for details) for this layer if needed. If only
654// Kalman filter is needed and tracklets are already linked to the track this step is skipped.
655// 3. Fit tracklet using the information from the Kalman filter.
656// 4. Propagate and update track at reference radial position of the tracklet.
657// 5. Register tracklet with the tracker and track; update pulls monitoring.
658//
659// Observation
2a3191bb 660// 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:
661// - AliTRDtrackV1::kProlongation : track prolongation failed
662// - AliTRDtrackV1::kPropagation : track prolongation failed
663// - AliTRDtrackV1::kAdjustSector : failed during sector crossing
664// - AliTRDtrackV1::kSnp : too large bending
665// - AliTRDtrackV1::kTrackletInit : fail to initialize tracklet
666// - AliTRDtrackV1::kUpdate : fail to attach clusters or fit the tracklet
667// - AliTRDtrackV1::kUnknown : anything which is not covered before
afa21247 668// 2. By default the status of the track before first TRD update is saved.
669//
670// Debug level 2
671//
672// Author
673// Alexandru Bercuci <A.Bercuci@gsi.de>
674//
41702fec 675
eb2b4f91 676 Int_t n = 0;
677 Double_t driftLength = .5*AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick();
4d6aee34 678 AliTRDtrackingChamber *chamber = NULL;
41702fec 679
4d6aee34 680 AliTRDseedV1 tracklet, *ptrTracklet = NULL;
181d2c97 681 // in case of stand alone tracking we store all the pointers to the tracklets in a temporary array
682 AliTRDseedV1 *tracklets[kNPlanes];
683 memset(tracklets, 0, sizeof(AliTRDseedV1 *) * kNPlanes);
684 for(Int_t ip = 0; ip < kNPlanes; ip++){
685 tracklets[ip] = t.GetTracklet(ip);
686 t.UnsetTracklet(ip);
687 }
81a6494d 688 Bool_t kStoreIn = kTRUE, kPropagateIn = kTRUE;
e3cf3d02 689
053767a4 690 // Loop through the TRD layers
4d6aee34 691 TGeoHMatrix *matrix = NULL;
eb2b4f91 692 Double_t x, y, z;
693 for (Int_t ily=0, sm=-1, stk=-1, det=-1; ily < AliTRDgeometry::kNlayer; ily++) {
694 // rough estimate of the entry point
695 if (!t.GetProlongation(fR[ily], y, z)){
696 n=-1;
697 t.SetStatus(AliTRDtrackV1::kProlongation);
698 break;
699 }
41702fec 700
eb2b4f91 701 // find sector / stack / detector
702 sm = t.GetSector();
703 // TODO cross check with y value !
704 stk = fGeom->GetStack(z, ily);
705 det = stk>=0 ? AliTRDgeometry::GetDetector(ily, stk, sm) : -1;
4d6aee34 706 matrix = det>=0 ? fGeom->GetClusterMatrix(det) : NULL;
eb2b4f91 707
708 // check if supermodule/chamber is installed
709 if( !fGeom->GetSMstatus(sm) ||
710 stk<0. ||
711 fGeom->IsHole(ily, stk, sm) ||
712 !matrix ){
713 // propagate to the default radial position
714 if(fR[ily] > (fgkMaxStep + t.GetX()) && !PropagateToX(t, fR[ily], fgkMaxStep)){
715 n=-1;
716 t.SetStatus(AliTRDtrackV1::kPropagation);
41702fec 717 break;
718 }
eb2b4f91 719 if(!AdjustSector(&t)){
720 n=-1;
721 t.SetStatus(AliTRDtrackV1::kAdjustSector);
722 break;
723 }
724 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp){
725 n=-1;
726 t.SetStatus(AliTRDtrackV1::kSnp);
727 break;
728 }
729 t.SetStatus(AliTRDtrackV1::kGeometry, ily);
41702fec 730 continue;
731 }
e3cf3d02 732
eb2b4f91 733 // retrieve rotation matrix for the current chamber
734 Double_t loc[] = {AliTRDgeometry::AnodePos()- driftLength, 0., 0.};
735 Double_t glb[] = {0., 0., 0.};
736 matrix->LocalToMaster(loc, glb);
737
738 // Propagate to the radial distance of the current layer
739 x = glb[0] - fgkMaxStep;
740 if(x > (fgkMaxStep + t.GetX()) && !PropagateToX(t, x, fgkMaxStep)){
741 n=-1;
742 t.SetStatus(AliTRDtrackV1::kPropagation);
743 break;
744 }
745 if(!AdjustSector(&t)){
746 n=-1;
747 t.SetStatus(AliTRDtrackV1::kAdjustSector);
748 break;
749 }
750 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp) {
751 n=-1;
752 t.SetStatus(AliTRDtrackV1::kSnp);
753 break;
754 }
4d6aee34 755 Bool_t doRecalculate = kFALSE;
eb2b4f91 756 if(sm != t.GetSector()){
757 sm = t.GetSector();
4d6aee34 758 doRecalculate = kTRUE;
eb2b4f91 759 }
760 if(stk != fGeom->GetStack(z, ily)){
761 stk = fGeom->GetStack(z, ily);
4d6aee34 762 doRecalculate = kTRUE;
eb2b4f91 763 }
4d6aee34 764 if(doRecalculate){
eb2b4f91 765 det = AliTRDgeometry::GetDetector(ily, stk, sm);
766 if(!(matrix = fGeom->GetClusterMatrix(det))){
767 t.SetStatus(AliTRDtrackV1::kGeometry, ily);
768 continue;
769 }
770 matrix->LocalToMaster(loc, glb);
771 x = glb[0] - fgkMaxStep;
772 }
773
774 // check if track is well inside fiducial volume
775 if (!t.GetProlongation(x+fgkMaxStep, y, z)) {
776 n=-1;
777 t.SetStatus(AliTRDtrackV1::kProlongation);
778 break;
779 }
780 if(fGeom->IsOnBoundary(det, y, z, .5)){
781 t.SetStatus(AliTRDtrackV1::kBoundary, ily);
782 continue;
783 }
784 // mark track as entering the FIDUCIAL volume of TRD
e3cf3d02 785 if(kStoreIn){
786 t.SetTrackLow();
787 kStoreIn = kFALSE;
788 }
b1957d3c 789
eb2b4f91 790 ptrTracklet = tracklets[ily];
791 if(!ptrTracklet){ // BUILD TRACKLET
792 // check data in supermodule
793 if(!fTrSec[sm].GetNChambers()){
794 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
795 continue;
796 }
797 if(fTrSec[sm].GetX(ily) < 1.){
798 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
799 continue;
800 }
801
802 // check data in chamber
803 if(!(chamber = fTrSec[sm].GetChamber(stk, ily))){
804 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
805 continue;
806 }
4d6aee34 807 if(chamber->GetNClusters() < fgNTimeBins*fkReconstructor->GetRecoParam() ->GetFindableClusters()){
eb2b4f91 808 t.SetStatus(AliTRDtrackV1::kNoClusters, ily);
809 continue;
810 }
811 // build tracklet
812 ptrTracklet = new(&tracklet) AliTRDseedV1(det);
4d6aee34 813 ptrTracklet->SetReconstructor(fkReconstructor);
eb2b4f91 814 ptrTracklet->SetKink(t.IsKink());
815 ptrTracklet->SetPadPlane(fGeom->GetPadPlane(ily, stk));
816 ptrTracklet->SetX0(glb[0]+driftLength);
817 if(!tracklet.Init(&t)){
818 n=-1;
819 t.SetStatus(AliTRDtrackV1::kTrackletInit);
820 break;
821 }
822 if(!tracklet.AttachClusters(chamber, kTRUE)){
823 t.SetStatus(AliTRDtrackV1::kNoAttach, ily);
824 continue;
825 }
4d6aee34 826 if(tracklet.GetN() < fgNTimeBins*fkReconstructor->GetRecoParam() ->GetFindableClusters()){
eb2b4f91 827 t.SetStatus(AliTRDtrackV1::kNoClustersTracklet, ily);
828 continue;
829 }
830 ptrTracklet->UpdateUsed();
831 }
eb2b4f91 832 // propagate track to the radial position of the tracklet
833 ptrTracklet->UseClusters(); // TODO ? do we need this here ?
834 // fit tracklet no tilt correction
835 if(!ptrTracklet->Fit(kFALSE)){
836 t.SetStatus(AliTRDtrackV1::kNoFit, ily);
837 continue;
838 }
839 x = ptrTracklet->GetX(); //GetX0();
840 if(x > (fgkMaxStep + t.GetX()) && !PropagateToX(t, x, fgkMaxStep)) {
841 n=-1;
842 t.SetStatus(AliTRDtrackV1::kPropagation);
843 break;
844 }
845 if(!AdjustSector(&t)) {
846 n=-1;
847 t.SetStatus(AliTRDtrackV1::kAdjustSector);
848 break;
41702fec 849 }
eb2b4f91 850 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp) {
851 n=-1;
852 t.SetStatus(AliTRDtrackV1::kSnp);
853 break;
854 }
81a6494d 855 if(kPropagateIn){
856 t.SetTrackLow();
857 kPropagateIn = kFALSE;
858 }
b72f4eaf 859 Double_t cov[3]; ptrTracklet->GetCovAt(x, cov);
860 Double_t p[2] = { ptrTracklet->GetY(), ptrTracklet->GetZ()};
861 Double_t chi2 = ((AliExternalTrackParam)t).GetPredictedChi2(p, cov);
4d6aee34 862 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 2){
6e39bde4 863 Double_t ytrack = ptrTracklet->GetYref(0);
864 Double_t ztrack = ptrTracklet->GetZref(0);
865 Double_t ytracklet = ptrTracklet->GetYfit(0);
866 Double_t ztracklet = ptrTracklet->GetZfit(0);
867 Double_t phitrack = ptrTracklet->GetYref(1);
868 Double_t phitracklet = ptrTracklet->GetYfit(1);
869 Double_t thetatrack = ptrTracklet->GetZref(1);
870 Double_t thetatracklet = ptrTracklet->GetZfit(1);
871
4d6aee34 872 TTreeSRedirector &mystreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
6e39bde4 873 mystreamer << "FollowBackProlongation1"
874 << "il=" << ily
875 << "x=" << x
876 << "ytrack=" << ytrack
877 << "ztrack=" << ztrack
878 << "ytracklet=" << ytracklet
879 << "ztracklet=" << ztracklet
880 << "phitrack=" << phitrack
881 << "thetatrack=" << thetatrack
882 << "phitracklet=" << phitracklet
883 << "thetatracklet=" << thetatracklet
884 << "chi2=" << chi2
885 << "\n";
886 }
887 // update Kalman with the TRD measurement
b72f4eaf 888 if(chi2>1e+10){ // TODO
eb2b4f91 889 t.SetStatus(AliTRDtrackV1::kChi2, ily);
890 continue;
891 }
b72f4eaf 892 if(!t.Update(p, cov, chi2)) {
eb2b4f91 893 n=-1;
894 t.SetStatus(AliTRDtrackV1::kUpdate);
895 break;
896 }
b72f4eaf 897 // fill residuals ?!
898 AliTracker::FillResiduals(&t, p, cov, ptrTracklet->GetVolumeId());
899
eb2b4f91 900
901 // load tracklet to the tracker
16cca13f 902 ptrTracklet->Update(&t);
eb2b4f91 903 ptrTracklet = SetTracklet(ptrTracklet);
904 t.SetTracklet(ptrTracklet, fTracklets->GetEntriesFast()-1);
905 n += ptrTracklet->GetN();
906
41702fec 907 // Reset material budget if 2 consecutive gold
eb2b4f91 908// if(ilayer>0 && t.GetTracklet(ilayer-1) && ptrTracklet->GetN() + t.GetTracklet(ilayer-1)->GetN() > 20) t.SetBudget(2, 0.);
41702fec 909
910 // Make backup of the track until is gold
911 // TO DO update quality check of the track.
912 // consider comparison with fTimeBinsRange
913 Float_t ratio0 = ptrTracklet->GetN() / Float_t(fgNTimeBins);
914 //Float_t ratio1 = Float_t(t.GetNumberOfClusters()+1) / Float_t(t.GetNExpected()+1);
41702fec 915
eb2b4f91 916 if( (chi2 < 18.0) &&
41702fec 917 (ratio0 > 0.8) &&
918 //(ratio1 > 0.6) &&
919 //(ratio0+ratio1 > 1.5) &&
920 (t.GetNCross() == 0) &&
921 (TMath::Abs(t.GetSnp()) < 0.85) &&
e3cf3d02 922 (t.GetNumberOfClusters() > 20)){
923 t.MakeBackupTrack();
924 }
053767a4 925 } // end layers loop
eb2b4f91 926 //printf("clusters[%d] chi2[%f] x[%f] status[%d ", n, t.GetChi2(), t.GetX(), t.GetStatusTRD());
927 //for(int i=0; i<6; i++) printf("%d ", t.GetStatusTRD(i)); printf("]\n");
41702fec 928
4d6aee34 929 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 1){
930 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 931 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
eb2b4f91 932 AliTRDtrackV1 track(t);
933 track.SetOwner();
41702fec 934 cstreamer << "FollowBackProlongation"
eb2b4f91 935 << "EventNumber=" << eventNumber
936 << "ncl=" << n
937 << "track.=" << &track
41702fec 938 << "\n";
939 }
940
eb2b4f91 941 return n;
0906e73e 942}
943
eb38ed55 944//_________________________________________________________________________
4d6aee34 945Float_t AliTRDtrackerV1::FitRieman(AliTRDseedV1 *tracklets, Double_t *chi2, Int_t *const planes){
41702fec 946 //
947 // Fits a Riemann-circle to the given points without tilting pad correction.
948 // The fit is performed using an instance of the class AliRieman (equations
949 // and transformations see documentation of this class)
950 // Afterwards all the tracklets are Updated
951 //
952 // Parameters: - Array of tracklets (AliTRDseedV1)
953 // - Storage for the chi2 values (beginning with direction z)
954 // - Seeding configuration
955 // Output: - The curvature
956 //
957 AliRieman *fitter = AliTRDtrackerV1::GetRiemanFitter();
958 fitter->Reset();
959 Int_t allplanes[] = {0, 1, 2, 3, 4, 5};
960 Int_t *ppl = &allplanes[0];
961 Int_t maxLayers = 6;
962 if(planes){
963 maxLayers = 4;
964 ppl = planes;
965 }
966 for(Int_t il = 0; il < maxLayers; il++){
967 if(!tracklets[ppl[il]].IsOK()) continue;
e3cf3d02 968 fitter->AddPoint(tracklets[ppl[il]].GetX0(), tracklets[ppl[il]].GetYfit(0), tracklets[ppl[il]].GetZfit(0),1,10);
41702fec 969 }
970 fitter->Update();
971 // Set the reference position of the fit and calculate the chi2 values
972 memset(chi2, 0, sizeof(Double_t) * 2);
973 for(Int_t il = 0; il < maxLayers; il++){
974 // Reference positions
975 tracklets[ppl[il]].Init(fitter);
976
977 // chi2
978 if((!tracklets[ppl[il]].IsOK()) && (!planes)) continue;
979 chi2[0] += tracklets[ppl[il]].GetChi2Y();
980 chi2[1] += tracklets[ppl[il]].GetChi2Z();
981 }
982 return fitter->GetC();
eb38ed55 983}
984
985//_________________________________________________________________________
986void AliTRDtrackerV1::FitRieman(AliTRDcluster **seedcl, Double_t chi2[2])
987{
41702fec 988 //
989 // Performs a Riemann helix fit using the seedclusters as spacepoints
990 // Afterwards the chi2 values are calculated and the seeds are updated
991 //
992 // Parameters: - The four seedclusters
993 // - The tracklet array (AliTRDseedV1)
994 // - The seeding configuration
995 // - Chi2 array
996 //
997 // debug level 2
998 //
999 AliRieman *fitter = AliTRDtrackerV1::GetRiemanFitter();
1000 fitter->Reset();
a3743898 1001 for(Int_t i = 0; i < 4; i++){
a3743898 1002 fitter->AddPoint(seedcl[i]->GetX(), seedcl[i]->GetY(), seedcl[i]->GetZ(), 1., 10.);
1003 }
41702fec 1004 fitter->Update();
1005
1006
1007 // Update the seed and calculated the chi2 value
1008 chi2[0] = 0; chi2[1] = 0;
1009 for(Int_t ipl = 0; ipl < kNSeedPlanes; ipl++){
1010 // chi2
1011 chi2[0] += (seedcl[ipl]->GetZ() - fitter->GetZat(seedcl[ipl]->GetX())) * (seedcl[ipl]->GetZ() - fitter->GetZat(seedcl[ipl]->GetX()));
1012 chi2[1] += (seedcl[ipl]->GetY() - fitter->GetYat(seedcl[ipl]->GetX())) * (seedcl[ipl]->GetY() - fitter->GetYat(seedcl[ipl]->GetX()));
1013 }
eb38ed55 1014}
1015
1016
1017//_________________________________________________________________________
1018Float_t AliTRDtrackerV1::FitTiltedRiemanConstraint(AliTRDseedV1 *tracklets, Double_t zVertex)
1019{
41702fec 1020 //
1021 // Fits a helix to the clusters. Pad tilting is considered. As constraint it is
1022 // assumed that the vertex position is set to 0.
1023 // This method is very usefull for high-pt particles
1024 // Basis for the fit: (x - x0)^2 + (y - y0)^2 - R^2 = 0
1025 // x0, y0: Center of the circle
1026 // Measured y-position: ymeas = y - tan(phiT)(zc - zt)
1027 // zc: center of the pad row
1028 // Equation which has to be fitted (after transformation):
1029 // a + b * u + e * v + 2*(ymeas + tan(phiT)(z - zVertex))*t = 0
1030 // Transformation:
1031 // t = 1/(x^2 + y^2)
1032 // u = 2 * x * t
1033 // v = 2 * x * tan(phiT) * t
1034 // Parameters in the equation:
1035 // a = -1/y0, b = x0/y0, e = dz/dx
1036 //
1037 // The Curvature is calculated by the following equation:
1038 // - curv = a/Sqrt(b^2 + 1) = 1/R
1039 // Parameters: - the 6 tracklets
1040 // - the Vertex constraint
1041 // Output: - the Chi2 value of the track
1042 //
1043 // debug level 5
1044 //
1045
1046 TLinearFitter *fitter = GetTiltedRiemanFitterConstraint();
1047 fitter->StoreData(kTRUE);
1048 fitter->ClearPoints();
4d6aee34 1049 AliTRDcluster *cl = NULL;
41702fec 1050
1051 Float_t x, y, z, w, t, error, tilt;
1052 Double_t uvt[2];
1053 Int_t nPoints = 0;
053767a4 1054 for(Int_t ilr = 0; ilr < AliTRDgeometry::kNlayer; ilr++){
1055 if(!tracklets[ilr].IsOK()) continue;
8d2bec9e 1056 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
053767a4 1057 if(!tracklets[ilr].IsUsable(itb)) continue;
1058 cl = tracklets[ilr].GetClusters(itb);
1742f24c 1059 if(!cl->IsInChamber()) continue;
41702fec 1060 x = cl->GetX();
1061 y = cl->GetY();
1062 z = cl->GetZ();
053767a4 1063 tilt = tracklets[ilr].GetTilt();
41702fec 1064 // Transformation
1065 t = 1./(x * x + y * y);
1066 uvt[0] = 2. * x * t;
1067 uvt[1] = 2. * x * t * tilt ;
1068 w = 2. * (y + tilt * (z - zVertex)) * t;
78b7d24c 1069 error = 2. * TMath::Sqrt(cl->GetSigmaY2()+tilt*tilt*cl->GetSigmaZ2()) * t;
41702fec 1070 fitter->AddPoint(uvt, w, error);
1071 nPoints++;
1072 }
1073 }
1074 fitter->Eval();
1075
1076 // Calculate curvature
1077 Double_t a = fitter->GetParameter(0);
1078 Double_t b = fitter->GetParameter(1);
1079 Double_t curvature = a/TMath::Sqrt(b*b + 1);
1080
1081 Float_t chi2track = fitter->GetChisquare()/Double_t(nPoints);
1082 for(Int_t ip = 0; ip < AliTRDtrackerV1::kNPlanes; ip++)
e3cf3d02 1083 tracklets[ip].SetC(curvature);
41702fec 1084
4d6aee34 1085/* if(fkReconstructor->GetStreamLevel() >= 5){
41702fec 1086 //Linear Model on z-direction
1087 Double_t xref = CalculateReferenceX(tracklets); // Relative to the middle of the stack
1088 Double_t slope = fitter->GetParameter(2);
1089 Double_t zref = slope * xref;
1090 Float_t chi2Z = CalculateChi2Z(tracklets, zref, slope, xref);
1091 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1092 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
4d6aee34 1093 TTreeSRedirector &treeStreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 1094 treeStreamer << "FitTiltedRiemanConstraint"
1095 << "EventNumber=" << eventNumber
1096 << "CandidateNumber=" << candidateNumber
1097 << "Curvature=" << curvature
1098 << "Chi2Track=" << chi2track
1099 << "Chi2Z=" << chi2Z
1100 << "zref=" << zref
1101 << "\n";
3a039a31 1102 }*/
41702fec 1103 return chi2track;
eb38ed55 1104}
1105
1106//_________________________________________________________________________
1107Float_t AliTRDtrackerV1::FitTiltedRieman(AliTRDseedV1 *tracklets, Bool_t sigError)
1108{
41702fec 1109 //
1110 // Performs a Riemann fit taking tilting pad correction into account
1111 // The equation of a Riemann circle, where the y position is substituted by the
1112 // measured y-position taking pad tilting into account, has to be transformed
1113 // into a 4-dimensional hyperplane equation
1114 // Riemann circle: (x-x0)^2 + (y-y0)^2 -R^2 = 0
1115 // Measured y-Position: ymeas = y - tan(phiT)(zc - zt)
1116 // zc: center of the pad row
1117 // zt: z-position of the track
1118 // The z-position of the track is assumed to be linear dependent on the x-position
1119 // Transformed equation: a + b * u + c * t + d * v + e * w - 2 * (ymeas + tan(phiT) * zc) * t = 0
1120 // Transformation: u = 2 * x * t
1121 // v = 2 * tan(phiT) * t
1122 // w = 2 * tan(phiT) * (x - xref) * t
1123 // t = 1 / (x^2 + ymeas^2)
1124 // Parameters: a = -1/y0
1125 // b = x0/y0
1126 // c = (R^2 -x0^2 - y0^2)/y0
1127 // d = offset
1128 // e = dz/dx
1129 // If the offset respectively the slope in z-position is impossible, the parameters are fixed using
1130 // results from the simple riemann fit. Afterwards the fit is redone.
1131 // The curvature is calculated according to the formula:
1132 // curv = a/(1 + b^2 + c*a) = 1/R
1133 //
1134 // Paramters: - Array of tracklets (connected to the track candidate)
1135 // - Flag selecting the error definition
1136 // Output: - Chi2 values of the track (in Parameter list)
1137 //
1138 TLinearFitter *fitter = GetTiltedRiemanFitter();
1139 fitter->StoreData(kTRUE);
1140 fitter->ClearPoints();
1141 AliTRDLeastSquare zfitter;
4d6aee34 1142 AliTRDcluster *cl = NULL;
41702fec 1143
1144 Double_t xref = CalculateReferenceX(tracklets);
6e39bde4 1145 Double_t x, y, z, t, tilt, dx, w, we, erry, errz;
1146 Double_t uvt[4], sumPolY[5], sumPolZ[3];
1147 memset(sumPolY, 0, sizeof(Double_t) * 5);
1148 memset(sumPolZ, 0, sizeof(Double_t) * 3);
41702fec 1149 Int_t nPoints = 0;
1150 // Containers for Least-square fitter
1151 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1152 if(!tracklets[ipl].IsOK()) continue;
ed15ef4f 1153 tilt = tracklets[ipl].GetTilt();
8d2bec9e 1154 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
41702fec 1155 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1742f24c 1156 if(!cl->IsInChamber()) continue;
41702fec 1157 if (!tracklets[ipl].IsUsable(itb)) continue;
1158 x = cl->GetX();
1159 y = cl->GetY();
1160 z = cl->GetZ();
41702fec 1161 dx = x - xref;
1162 // Transformation
1163 t = 1./(x*x + y*y);
1164 uvt[0] = 2. * x * t;
1165 uvt[1] = t;
1166 uvt[2] = 2. * tilt * t;
1167 uvt[3] = 2. * tilt * dx * t;
1168 w = 2. * (y + tilt*z) * t;
1169 // error definition changes for the different calls
1170 we = 2. * t;
78b7d24c 1171 we *= sigError ? TMath::Sqrt(cl->GetSigmaY2()+tilt*tilt*cl->GetSigmaZ2()) : 0.2;
41702fec 1172 fitter->AddPoint(uvt, w, we);
1173 zfitter.AddPoint(&x, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
6e39bde4 1174 // adding points for covariance matrix estimation
1175 erry = 1./(TMath::Sqrt(cl->GetSigmaY2()) + 0.1); // 0.1 is a systematic error (due to misalignment and miscalibration)
1176 erry *= erry;
1177 errz = 1./cl->GetSigmaZ2();
1178 for(Int_t ipol = 0; ipol < 5; ipol++){
1179 sumPolY[ipol] += erry;
1180 erry *= x;
1181 if(ipol < 3){
1182 sumPolZ[ipol] += errz;
1183 errz *= x;
1184 }
1185 }
41702fec 1186 nPoints++;
1187 }
1188 }
1189 fitter->Eval();
1190 zfitter.Eval();
1191
1192 Double_t offset = fitter->GetParameter(3);
1193 Double_t slope = fitter->GetParameter(4);
1194
1195 // Linear fitter - not possible to make boundaries
1196 // Do not accept non possible z and dzdx combinations
1197 Bool_t acceptablez = kTRUE;
1198 Double_t zref = 0.0;
1199 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
1200 if(!tracklets[iLayer].IsOK()) continue;
1201 zref = offset + slope * (tracklets[iLayer].GetX0() - xref);
e3cf3d02 1202 if (TMath::Abs(tracklets[iLayer].GetZfit(0) - zref) > tracklets[iLayer].GetPadLength() * 0.5 + 1.0)
41702fec 1203 acceptablez = kFALSE;
1204 }
1205 if (!acceptablez) {
1206 Double_t dzmf = zfitter.GetFunctionParameter(1);
1207 Double_t zmf = zfitter.GetFunctionValue(&xref);
1208 fgTiltedRieman->FixParameter(3, zmf);
1209 fgTiltedRieman->FixParameter(4, dzmf);
1210 fitter->Eval();
1211 fitter->ReleaseParameter(3);
1212 fitter->ReleaseParameter(4);
1213 offset = fitter->GetParameter(3);
1214 slope = fitter->GetParameter(4);
1215 }
1216
1217 // Calculate Curvarture
1218 Double_t a = fitter->GetParameter(0);
1219 Double_t b = fitter->GetParameter(1);
1220 Double_t c = fitter->GetParameter(2);
1221 Double_t curvature = 1.0 + b*b - c*a;
1222 if (curvature > 0.0)
1223 curvature = a / TMath::Sqrt(curvature);
1224
1225 Double_t chi2track = fitter->GetChisquare()/Double_t(nPoints);
1226
6e39bde4 1227 // Prepare error calculation
1228 TMatrixD covarPolY(3,3);
1229 covarPolY(0,0) = sumPolY[0]; covarPolY(1,1) = sumPolY[2]; covarPolY(2,2) = sumPolY[4];
1230 covarPolY(0,1) = covarPolY(1,0) = sumPolY[1];
1231 covarPolY(0,2) = covarPolY(2,0) = sumPolY[2];
1232 covarPolY(2,1) = covarPolY(1,2) = sumPolY[3];
1233 covarPolY.Invert();
1234 TMatrixD covarPolZ(2,2);
1235 covarPolZ(0,0) = sumPolZ[0]; covarPolZ(1,1) = sumPolZ[2];
1236 covarPolZ(1,0) = covarPolZ(0,1) = sumPolZ[1];
1237 covarPolZ.Invert();
1238
41702fec 1239 // Update the tracklets
6e39bde4 1240 Double_t x1, dy, dz;
1241 Double_t cov[15];
1242 memset(cov, 0, sizeof(Double_t) * 15);
41702fec 1243 for(Int_t iLayer = 0; iLayer < AliTRDtrackerV1::kNPlanes; iLayer++) {
1244
1245 x = tracklets[iLayer].GetX0();
6e39bde4 1246 x1 = x - xref;
41702fec 1247 y = 0;
1248 z = 0;
1249 dy = 0;
1250 dz = 0;
6e39bde4 1251 memset(cov, 0, sizeof(Double_t) * 3);
1252 TMatrixD transform(3,3);
1253 transform(0,0) = 1;
1254 transform(0,1) = x;
1255 transform(0,2) = x*x;
1256 transform(1,1) = 1;
1257 transform(1,2) = x;
1258 transform(2,2) = 1;
1259 TMatrixD covariance(transform, TMatrixD::kMult, covarPolY);
1260 covariance *= transform.T();
1261 TMatrixD transformZ(2,2);
1262 transformZ(0,0) = transformZ(1,1) = 1;
1263 transformZ(0,1) = x;
1264 TMatrixD covarZ(transformZ, TMatrixD::kMult, covarPolZ);
1265 covarZ *= transformZ.T();
41702fec 1266 // y: R^2 = (x - x0)^2 + (y - y0)^2
1267 // => y = y0 +/- Sqrt(R^2 - (x - x0)^2)
1268 // R = Sqrt() = 1/Curvature
1269 // => y = y0 +/- Sqrt(1/Curvature^2 - (x - x0)^2)
1270 Double_t res = (x * a + b); // = (x - x0)/y0
1271 res *= res;
1272 res = 1.0 - c * a + b * b - res; // = (R^2 - (x - x0)^2)/y0^2
1273 if (res >= 0) {
1274 res = TMath::Sqrt(res);
1275 y = (1.0 - res) / a;
1276 }
6e39bde4 1277 cov[0] = covariance(0,0);
1278 cov[2] = covarZ(0,0);
1279 cov[1] = 0.;
41702fec 1280
1281 // dy: R^2 = (x - x0)^2 + (y - y0)^2
1282 // => y = +/- Sqrt(R^2 - (x - x0)^2) + y0
1283 // => dy/dx = (x - x0)/Sqrt(R^2 - (x - x0)^2)
1284 // Curvature: cr = 1/R = a/Sqrt(1 + b^2 - c*a)
1285 // => dy/dx = (x - x0)/(1/(cr^2) - (x - x0)^2)
1286 Double_t x0 = -b / a;
1287 if (-c * a + b * b + 1 > 0) {
1288 if (1.0/(curvature * curvature) - (x - x0) * (x - x0) > 0.0) {
6e39bde4 1289 Double_t yderiv = (x - x0) / TMath::Sqrt(1.0/(curvature * curvature) - (x - x0) * (x - x0));
1290 if (a < 0) yderiv *= -1.0;
1291 dy = yderiv;
41702fec 1292 }
1293 }
1294 z = offset + slope * (x - xref);
1295 dz = slope;
1296 tracklets[iLayer].SetYref(0, y);
1297 tracklets[iLayer].SetYref(1, dy);
1298 tracklets[iLayer].SetZref(0, z);
1299 tracklets[iLayer].SetZref(1, dz);
1300 tracklets[iLayer].SetC(curvature);
6e39bde4 1301 tracklets[iLayer].SetCovRef(cov);
41702fec 1302 tracklets[iLayer].SetChi2(chi2track);
1303 }
1304
4d6aee34 1305/* if(fkReconstructor->GetStreamLevel() >=5){
1306 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 1307 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1308 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
1309 Double_t chi2z = CalculateChi2Z(tracklets, offset, slope, xref);
1310 cstreamer << "FitTiltedRieman0"
1311 << "EventNumber=" << eventNumber
1312 << "CandidateNumber=" << candidateNumber
1313 << "xref=" << xref
1314 << "Chi2Z=" << chi2z
1315 << "\n";
3a039a31 1316 }*/
41702fec 1317 return chi2track;
eb38ed55 1318}
1319
3b57a3f7 1320
9e333711 1321//____________________________________________________________________
6e4d4425 1322Double_t AliTRDtrackerV1::FitLine(const AliTRDtrackV1 *track, AliTRDseedV1 *tracklets, Bool_t err, Int_t np, AliTrackPoint *points)
9e333711 1323{
4d6aee34 1324 //
1325 // Fit track with a staight line
1326 // Fills an AliTrackPoint array with np points
1327 // Function should be used to refit tracks when no magnetic field was on
1328 //
9e333711 1329 AliTRDLeastSquare yfitter, zfitter;
4d6aee34 1330 AliTRDcluster *cl = NULL;
9e333711 1331
4d6aee34 1332 AliTRDseedV1 work[kNPlanes], *tracklet = NULL;
9e333711 1333 if(!tracklets){
1334 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1335 if(!(tracklet = track->GetTracklet(ipl))) continue;
1336 if(!tracklet->IsOK()) continue;
1337 new(&work[ipl]) AliTRDseedV1(*tracklet);
1338 }
1339 tracklets = &work[0];
1340 }
1341
1342 Double_t xref = CalculateReferenceX(tracklets);
1343 Double_t x, y, z, dx, ye, yr, tilt;
1344 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1345 if(!tracklets[ipl].IsOK()) continue;
1346 for(Int_t itb = 0; itb < fgNTimeBins; itb++){
1347 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1348 if (!tracklets[ipl].IsUsable(itb)) continue;
1349 x = cl->GetX();
1350 z = cl->GetZ();
1351 dx = x - xref;
1352 zfitter.AddPoint(&dx, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
1353 }
1354 }
1355 zfitter.Eval();
1356 Double_t z0 = zfitter.GetFunctionParameter(0);
1357 Double_t dzdx = zfitter.GetFunctionParameter(1);
1358 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1359 if(!tracklets[ipl].IsOK()) continue;
1360 for(Int_t itb = 0; itb < fgNTimeBins; itb++){
1361 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1362 if (!tracklets[ipl].IsUsable(itb)) continue;
1363 x = cl->GetX();
1364 y = cl->GetY();
1365 z = cl->GetZ();
1366 tilt = tracklets[ipl].GetTilt();
1367 dx = x - xref;
1368 yr = y + tilt*(z - z0 - dzdx*dx);
1369 // error definition changes for the different calls
1370 ye = tilt*TMath::Sqrt(cl->GetSigmaZ2());
1371 ye += err ? tracklets[ipl].GetSigmaY() : 0.2;
1372 yfitter.AddPoint(&dx, yr, ye);
1373 }
1374 }
1375 yfitter.Eval();
1376 Double_t y0 = yfitter.GetFunctionParameter(0);
1377 Double_t dydx = yfitter.GetFunctionParameter(1);
1378 Double_t chi2 = 0.;//yfitter.GetChisquare()/Double_t(nPoints);
1379
1380 //update track points array
1381 if(np && points){
1382 Float_t xyz[3];
1383 for(int ip=0; ip<np; ip++){
1384 points[ip].GetXYZ(xyz);
1385 xyz[1] = y0 + dydx * (xyz[0] - xref);
1386 xyz[2] = z0 + dzdx * (xyz[0] - xref);
1387 points[ip].SetXYZ(xyz);
1388 }
1389 }
1390 return chi2;
1391}
1392
1393
3b57a3f7 1394//_________________________________________________________________________
6e4d4425 1395Double_t AliTRDtrackerV1::FitRiemanTilt(const AliTRDtrackV1 *track, AliTRDseedV1 *tracklets, Bool_t sigError, Int_t np, AliTrackPoint *points)
3b57a3f7 1396{
0fa1a8ee 1397//
1398// Performs a Riemann fit taking tilting pad correction into account
1399//
1400// Paramters: - Array of tracklets (connected to the track candidate)
1401// - Flag selecting the error definition
1402// Output: - Chi2 values of the track (in Parameter list)
1403//
1404// The equations which has to be solved simultaneously are:
1405// BEGIN_LATEX
1406// R^{2} = (x-x_{0})^{2} + (y^{*}-y_{0})^{2}
1407// y^{*} = y - tg(h)(z - z_{t})
1408// z_{t} = z_{0}+dzdx*(x-x_{r})
1409// END_LATEX
1410// with (x, y, z) the coordinate of the cluster, (x_0, y_0, z_0) the coordinate of the center of the Riemann circle,
1411// R its radius, x_r a constant refrence radial position in the middle of the TRD stack and dzdx the slope of the
1412// track in the x-z plane. Using the following transformations
1413// BEGIN_LATEX
1414// t = 1 / (x^{2} + y^{2})
1415// u = 2 * x * t
1416// v = 2 * tan(h) * t
1417// w = 2 * tan(h) * (x - x_{r}) * t
1418// END_LATEX
1419// One gets the following linear equation
1420// BEGIN_LATEX
1421// a + b * u + c * t + d * v + e * w = 2 * (y + tg(h) * z) * t
1422// END_LATEX
1423// where the coefficients have the following meaning
1424// BEGIN_LATEX
1425// a = -1/y_{0}
1426// b = x_{0}/y_{0}
1427// c = (R^{2} -x_{0}^{2} - y_{0}^{2})/y_{0}
1428// d = z_{0}
1429// e = dz/dx
1430// END_LATEX
1431// The error calculation for the free term is thus
1432// BEGIN_LATEX
1433// #sigma = 2 * #sqrt{#sigma^{2}_{y} + (tilt corr ...) + tg^{2}(h) * #sigma^{2}_{z}} * t
1434// END_LATEX
1435//
1436// From this simple model one can compute chi^2 estimates and a rough approximation of pt from the curvature according
1437// to the formula:
1438// BEGIN_LATEX
1439// C = 1/R = a/(1 + b^{2} + c*a)
1440// END_LATEX
1441//
1442// Authors
1443// M.Ivanov <M.Ivanov@gsi.de>
1444// A.Bercuci <A.Bercuci@gsi.de>
1445// M.Fasel <M.Fasel@gsi.de>
1446
41702fec 1447 TLinearFitter *fitter = GetTiltedRiemanFitter();
1448 fitter->StoreData(kTRUE);
1449 fitter->ClearPoints();
1450 AliTRDLeastSquare zfitter;
4d6aee34 1451 AliTRDcluster *cl = NULL;
3b57a3f7 1452
4d6aee34 1453 AliTRDseedV1 work[kNPlanes], *tracklet = NULL;
3b57a3f7 1454 if(!tracklets){
1455 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1456 if(!(tracklet = track->GetTracklet(ipl))) continue;
1457 if(!tracklet->IsOK()) continue;
1458 new(&work[ipl]) AliTRDseedV1(*tracklet);
1459 }
1460 tracklets = &work[0];
1461 }
1462
41702fec 1463 Double_t xref = CalculateReferenceX(tracklets);
1464 Double_t x, y, z, t, tilt, dx, w, we;
1465 Double_t uvt[4];
1466 Int_t nPoints = 0;
1467 // Containers for Least-square fitter
1468 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
1469 if(!tracklets[ipl].IsOK()) continue;
8d2bec9e 1470 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
41702fec 1471 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
1472 if (!tracklets[ipl].IsUsable(itb)) continue;
1473 x = cl->GetX();
1474 y = cl->GetY();
1475 z = cl->GetZ();
1476 tilt = tracklets[ipl].GetTilt();
1477 dx = x - xref;
1478 // Transformation
1479 t = 1./(x*x + y*y);
1480 uvt[0] = 2. * x * t;
1481 uvt[1] = t;
1482 uvt[2] = 2. * tilt * t;
1483 uvt[3] = 2. * tilt * dx * t;
1484 w = 2. * (y + tilt*z) * t;
1485 // error definition changes for the different calls
1486 we = 2. * t;
f29f13a6 1487 we *= sigError ? TMath::Sqrt(cl->GetSigmaY2()) : 0.2;
41702fec 1488 fitter->AddPoint(uvt, w, we);
1489 zfitter.AddPoint(&x, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
1490 nPoints++;
1491 }
1492 }
aec26713 1493 if(fitter->Eval()) return 1.E10;
1494
41702fec 1495 Double_t z0 = fitter->GetParameter(3);
1496 Double_t dzdx = fitter->GetParameter(4);
3b57a3f7 1497
1498
1499 // Linear fitter - not possible to make boundaries
1500 // Do not accept non possible z and dzdx combinations
1501 Bool_t accept = kTRUE;
1502 Double_t zref = 0.0;
1503 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
1504 if(!tracklets[iLayer].IsOK()) continue;
1505 zref = z0 + dzdx * (tracklets[iLayer].GetX0() - xref);
e3cf3d02 1506 if (TMath::Abs(tracklets[iLayer].GetZfit(0) - zref) > tracklets[iLayer].GetPadLength() * 0.5 + 1.0)
3b57a3f7 1507 accept = kFALSE;
1508 }
1509 if (!accept) {
41702fec 1510 zfitter.Eval();
3b57a3f7 1511 Double_t dzmf = zfitter.GetFunctionParameter(1);
1512 Double_t zmf = zfitter.GetFunctionValue(&xref);
1513 fitter->FixParameter(3, zmf);
1514 fitter->FixParameter(4, dzmf);
1515 fitter->Eval();
1516 fitter->ReleaseParameter(3);
1517 fitter->ReleaseParameter(4);
1518 z0 = fitter->GetParameter(3); // = zmf ?
1519 dzdx = fitter->GetParameter(4); // = dzmf ?
1520 }
1521
1522 // Calculate Curvature
1523 Double_t a = fitter->GetParameter(0);
1524 Double_t b = fitter->GetParameter(1);
1525 Double_t c = fitter->GetParameter(2);
1526 Double_t y0 = 1. / a;
1527 Double_t x0 = -b * y0;
a015e406 1528 Double_t tmp = y0*y0 + x0*x0 - c*y0;
1529 if(tmp<=0.) return 1.E10;
4d6aee34 1530 Double_t radius = TMath::Sqrt(tmp);
1531 Double_t curvature = 1.0 + b*b - c*a;
1532 if (curvature > 0.0) curvature = a / TMath::Sqrt(curvature);
3b57a3f7 1533
1534 // Calculate chi2 of the fit
1535 Double_t chi2 = fitter->GetChisquare()/Double_t(nPoints);
1536
1537 // Update the tracklets
1538 if(!track){
1539 for(Int_t ip = 0; ip < kNPlanes; ip++) {
1540 x = tracklets[ip].GetX0();
4d6aee34 1541 tmp = radius*radius-(x-x0)*(x-x0);
a015e406 1542 if(tmp <= 0.) continue;
1543 tmp = TMath::Sqrt(tmp);
3b57a3f7 1544
1545 // y: R^2 = (x - x0)^2 + (y - y0)^2
1546 // => y = y0 +/- Sqrt(R^2 - (x - x0)^2)
1547 tracklets[ip].SetYref(0, y0 - (y0>0.?1.:-1)*tmp);
1548 // => dy/dx = (x - x0)/Sqrt(R^2 - (x - x0)^2)
1549 tracklets[ip].SetYref(1, (x - x0) / tmp);
1550 tracklets[ip].SetZref(0, z0 + dzdx * (x - xref));
1551 tracklets[ip].SetZref(1, dzdx);
4d6aee34 1552 tracklets[ip].SetC(curvature);
3b57a3f7 1553 tracklets[ip].SetChi2(chi2);
1554 }
1555 }
3b57a3f7 1556 //update track points array
1557 if(np && points){
1558 Float_t xyz[3];
1559 for(int ip=0; ip<np; ip++){
1560 points[ip].GetXYZ(xyz);
4d6aee34 1561 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 1562 xyz[2] = z0 + dzdx * (xyz[0] - xref);
1563 points[ip].SetXYZ(xyz);
1564 }
1565 }
1566
3b57a3f7 1567 return chi2;
1568}
1569
1570
1bf51039 1571//____________________________________________________________________
4d6aee34 1572Double_t AliTRDtrackerV1::FitKalman(AliTRDtrackV1 *track, const AliTRDseedV1 * const tracklets, Bool_t up, Int_t np, AliTrackPoint *points)
1bf51039 1573{
1574// Kalman filter implementation for the TRD.
1575// It returns the positions of the fit in the array "points"
1576//
1577// Author : A.Bercuci@gsi.de
1578
3cfaffa4 1579 // printf("Start track @ x[%f]\n", track->GetX());
1bf51039 1580
1581 //prepare marker points along the track
1582 Int_t ip = np ? 0 : 1;
1583 while(ip<np){
1584 if((up?-1:1) * (track->GetX() - points[ip].GetX()) > 0.) break;
1585 //printf("AliTRDtrackerV1::FitKalman() : Skip track marker x[%d] = %7.3f. Before track start ( %7.3f ).\n", ip, points[ip].GetX(), track->GetX());
1586 ip++;
1587 }
1588 //if(points) printf("First marker point @ x[%d] = %f\n", ip, points[ip].GetX());
1589
1590
4d6aee34 1591 AliTRDseedV1 tracklet, *ptrTracklet = NULL;
1bf51039 1592
1593 //Loop through the TRD planes
1594 for (Int_t jplane = 0; jplane < kNPlanes; jplane++) {
1595 // GET TRACKLET OR BUILT IT
1596 Int_t iplane = up ? jplane : kNPlanes - 1 - jplane;
1597 if(tracklets){
4d6aee34 1598 if(!(ptrTracklet = const_cast<AliTRDseedV1 *>(&tracklets[iplane]))) continue;
1bf51039 1599 }else{
1600 if(!(ptrTracklet = track->GetTracklet(iplane))){
4d6aee34 1601 /*AliTRDtrackerV1 *tracker = NULL;
1bf51039 1602 if(!(tracker = dynamic_cast<AliTRDtrackerV1*>( AliTRDReconstructor::Tracker()))) continue;
1603 ptrTracklet = new(&tracklet) AliTRDseedV1(iplane);
1604 if(!tracker->MakeTracklet(ptrTracklet, track)) */
1605 continue;
1606 }
1607 }
1608 if(!ptrTracklet->IsOK()) continue;
1609
1610 Double_t x = ptrTracklet->GetX0();
1611
1612 while(ip < np){
1613 //don't do anything if next marker is after next update point.
1614 if((up?-1:1) * (points[ip].GetX() - x) - fgkMaxStep < 0) break;
1bf51039 1615 if(((up?-1:1) * (points[ip].GetX() - track->GetX()) < 0) && !PropagateToX(*track, points[ip].GetX(), fgkMaxStep)) return -1.;
1616
1617 Double_t xyz[3]; // should also get the covariance
3cfaffa4 1618 track->GetXYZ(xyz);
1619 track->Global2LocalPosition(xyz, track->GetAlpha());
1620 points[ip].SetXYZ(xyz[0], xyz[1], xyz[2]);
1bf51039 1621 ip++;
1622 }
3cfaffa4 1623 // printf("plane[%d] tracklet[%p] x[%f]\n", iplane, ptrTracklet, x);
1bf51039 1624
3cfaffa4 1625 // Propagate closer to the next update point
1bf51039 1626 if(((up?-1:1) * (x - track->GetX()) + fgkMaxStep < 0) && !PropagateToX(*track, x + (up?-1:1)*fgkMaxStep, fgkMaxStep)) return -1.;
1627
1628 if(!AdjustSector(track)) return -1;
1629 if(TMath::Abs(track->GetSnp()) > fgkMaxSnp) return -1;
1630
1631 //load tracklet to the tracker and the track
1632/* Int_t index;
1633 if((index = FindTracklet(ptrTracklet)) < 0){
1634 ptrTracklet = SetTracklet(&tracklet);
1635 index = fTracklets->GetEntriesFast()-1;
1636 }
1637 track->SetTracklet(ptrTracklet, index);*/
1638
1639
1640 // register tracklet to track with tracklet creation !!
1641 // PropagateBack : loaded tracklet to the tracker and update index
1642 // RefitInward : update index
1643 // MakeTrack : loaded tracklet to the tracker and update index
1644 if(!tracklets) track->SetTracklet(ptrTracklet, -1);
1645
1646
1647 //Calculate the mean material budget along the path inside the chamber
1648 Double_t xyz0[3]; track->GetXYZ(xyz0);
1649 Double_t alpha = track->GetAlpha();
1650 Double_t xyz1[3], y, z;
1651 if(!track->GetProlongation(x, y, z)) return -1;
1652 xyz1[0] = x * TMath::Cos(alpha) - y * TMath::Sin(alpha);
1653 xyz1[1] = +x * TMath::Sin(alpha) + y * TMath::Cos(alpha);
1654 xyz1[2] = z;
3cfaffa4 1655 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 1656 Double_t param[7];
3cfaffa4 1657 if(AliTracker::MeanMaterialBudget(xyz0, xyz1, param) <=0.) break;
1bf51039 1658 Double_t xrho = param[0]*param[4]; // density*length
1659 Double_t xx0 = param[1]; // radiation length
1660
1661 //Propagate the track
1662 track->PropagateTo(x, xx0, xrho);
1663 if (!AdjustSector(track)) break;
1664
1665 //Update track
b72f4eaf 1666 Double_t cov[3]; ptrTracklet->GetCovAt(x, cov);
1667 Double_t p[2] = { ptrTracklet->GetY(), ptrTracklet->GetZ()};
1668 Double_t chi2 = ((AliExternalTrackParam*)track)->GetPredictedChi2(p, cov);
1669 if(chi2<1e+10) track->Update(p, cov, chi2);
1bf51039 1670 if(!up) continue;
1671
1672 //Reset material budget if 2 consecutive gold
1673 if(iplane>0 && track->GetTracklet(iplane-1) && ptrTracklet->GetN() + track->GetTracklet(iplane-1)->GetN() > 20) track->SetBudget(2, 0.);
1674 } // end planes loop
1675
1676 // extrapolation
1677 while(ip < np){
1678 if(((up?-1:1) * (points[ip].GetX() - track->GetX()) < 0) && !PropagateToX(*track, points[ip].GetX(), fgkMaxStep)) return -1.;
1679
1680 Double_t xyz[3]; // should also get the covariance
3cfaffa4 1681 track->GetXYZ(xyz);
1682 track->Global2LocalPosition(xyz, track->GetAlpha());
1683 points[ip].SetXYZ(xyz[0], xyz[1], xyz[2]);
1bf51039 1684 ip++;
1685 }
1686
1687 return track->GetChi2();
1688}
3b57a3f7 1689
eb38ed55 1690//_________________________________________________________________________
bb56afff 1691Float_t AliTRDtrackerV1::CalculateChi2Z(AliTRDseedV1 *tracklets, Double_t offset, Double_t slope, Double_t xref)
eb38ed55 1692{
41702fec 1693 //
1694 // Calculates the chi2-value of the track in z-Direction including tilting pad correction.
1695 // A linear dependence on the x-value serves as a model.
1696 // The parameters are related to the tilted Riemann fit.
1697 // Parameters: - Array of tracklets (AliTRDseedV1) related to the track candidate
1698 // - the offset for the reference x
1699 // - the slope
1700 // - the reference x position
1701 // Output: - The Chi2 value of the track in z-Direction
1702 //
1703 Float_t chi2Z = 0, nLayers = 0;
053767a4 1704 for (Int_t iLayer = 0; iLayer < AliTRDgeometry::kNlayer; iLayer++) {
41702fec 1705 if(!tracklets[iLayer].IsOK()) continue;
1706 Double_t z = offset + slope * (tracklets[iLayer].GetX0() - xref);
e3cf3d02 1707 chi2Z += TMath::Abs(tracklets[iLayer].GetZfit(0) - z);
41702fec 1708 nLayers++;
1709 }
1710 chi2Z /= TMath::Max((nLayers - 3.0),1.0);
1711 return chi2Z;
eb38ed55 1712}
1713
bccda319 1714//_____________________________________________________________________________
1715Int_t AliTRDtrackerV1::PropagateToX(AliTRDtrackV1 &t, Double_t xToGo, Double_t maxStep)
1716{
41702fec 1717 //
1718 // Starting from current X-position of track <t> this function
1719 // extrapolates the track up to radial position <xToGo>.
1720 // Returns 1 if track reaches the plane, and 0 otherwise
1721 //
bccda319 1722
41702fec 1723 const Double_t kEpsilon = 0.00001;
bccda319 1724
41702fec 1725 // Current track X-position
1726 Double_t xpos = t.GetX();
bccda319 1727
41702fec 1728 // Direction: inward or outward
1729 Double_t dir = (xpos < xToGo) ? 1.0 : -1.0;
bccda319 1730
41702fec 1731 while (((xToGo - xpos) * dir) > kEpsilon) {
bccda319 1732
41702fec 1733 Double_t xyz0[3];
1734 Double_t xyz1[3];
1735 Double_t param[7];
1736 Double_t x;
1737 Double_t y;
1738 Double_t z;
bccda319 1739
41702fec 1740 // The next step size
1741 Double_t step = dir * TMath::Min(TMath::Abs(xToGo-xpos),maxStep);
bccda319 1742
41702fec 1743 // Get the global position of the starting point
1744 t.GetXYZ(xyz0);
bccda319 1745
41702fec 1746 // X-position after next step
1747 x = xpos + step;
bccda319 1748
41702fec 1749 // Get local Y and Z at the X-position of the next step
3352b455 1750 if(t.GetProlongation(x,y,z)<0) return 0; // No prolongation possible
bccda319 1751
41702fec 1752 // The global position of the end point of this prolongation step
1753 xyz1[0] = x * TMath::Cos(t.GetAlpha()) - y * TMath::Sin(t.GetAlpha());
1754 xyz1[1] = +x * TMath::Sin(t.GetAlpha()) + y * TMath::Cos(t.GetAlpha());
1755 xyz1[2] = z;
bccda319 1756
41702fec 1757 // Calculate the mean material budget between start and
1758 // end point of this prolongation step
83dea92e 1759 if(AliTracker::MeanMaterialBudget(xyz0, xyz1, param)<=0.) return 0;
bccda319 1760
41702fec 1761 // Propagate the track to the X-position after the next step
9c87a076 1762 if (!t.PropagateTo(x, param[1], param[0]*param[4])) return 0;
bccda319 1763
41702fec 1764 // Rotate the track if necessary
1765 AdjustSector(&t);
bccda319 1766
41702fec 1767 // New track X-position
1768 xpos = t.GetX();
bccda319 1769
41702fec 1770 }
bccda319 1771
41702fec 1772 return 1;
bccda319 1773
1774}
1775
eb38ed55 1776
1777//_____________________________________________________________________________
1778Int_t AliTRDtrackerV1::ReadClusters(TClonesArray* &array, TTree *clusterTree) const
1779{
41702fec 1780 //
1781 // Reads AliTRDclusters from the file.
1782 // The names of the cluster tree and branches
1783 // should match the ones used in AliTRDclusterizer::WriteClusters()
1784 //
1785
1786 Int_t nsize = Int_t(clusterTree->GetTotBytes() / (sizeof(AliTRDcluster)));
1787 TObjArray *clusterArray = new TObjArray(nsize+1000);
1788
1789 TBranch *branch = clusterTree->GetBranch("TRDcluster");
1790 if (!branch) {
1791 AliError("Can't get the branch !");
1792 return 1;
1793 }
1794 branch->SetAddress(&clusterArray);
1795
1796 if(!fClusters){
4d6aee34 1797 Float_t nclusters = fkReconstructor->GetRecoParam()->GetNClusters();
1798 if(fkReconstructor->IsHLT()) nclusters /= AliTRDgeometry::kNsector;
8ae98148 1799 array = new TClonesArray("AliTRDcluster", Int_t(nclusters));
41702fec 1800 array->SetOwner(kTRUE);
1801 }
1802
1803 // Loop through all entries in the tree
1804 Int_t nEntries = (Int_t) clusterTree->GetEntries();
1805 Int_t nbytes = 0;
1806 Int_t ncl = 0;
4d6aee34 1807 AliTRDcluster *c = NULL;
41702fec 1808 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
1809 // Import the tree
1810 nbytes += clusterTree->GetEvent(iEntry);
1811
1812 // Get the number of points in the detector
1813 Int_t nCluster = clusterArray->GetEntriesFast();
1814 for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) {
1815 if(!(c = (AliTRDcluster *) clusterArray->UncheckedAt(iCluster))) continue;
41702fec 1816 new((*fClusters)[ncl++]) AliTRDcluster(*c);
1817 delete (clusterArray->RemoveAt(iCluster));
1818 }
1819
1820 }
1821 delete clusterArray;
1822
1823 return 0;
eb38ed55 1824}
1825
1826//_____________________________________________________________________________
1827Int_t AliTRDtrackerV1::LoadClusters(TTree *cTree)
1828{
41702fec 1829 //
66f6bfd9 1830 // Fills clusters into TRD tracking sectors
41702fec 1831 //
41702fec 1832
4d6aee34 1833 if(!fkReconstructor->IsWritingClusters()){
48f8adf3 1834 fClusters = AliTRDReconstructor::GetClusters();
1835 } else {
66f6bfd9 1836 if (ReadClusters(fClusters, cTree)) {
1837 AliError("Problem with reading the clusters !");
1838 return 1;
1839 }
1840 }
1841 SetClustersOwner();
1842
48f8adf3 1843 if(!fClusters || !fClusters->GetEntriesFast()){
66f6bfd9 1844 AliInfo("No TRD clusters");
41702fec 1845 return 1;
1846 }
66f6bfd9 1847
1848 //Int_t nin =
1849 BuildTrackingContainers();
1850
1851 //Int_t ncl = fClusters->GetEntriesFast();
1852 //AliInfo(Form("Clusters %d [%6.2f %% in the active volume]", ncl, 100.*float(nin)/ncl));
1853
1854 return 0;
1855}
1856
1857//_____________________________________________________________________________
4d6aee34 1858Int_t AliTRDtrackerV1::LoadClusters(TClonesArray * const clusters)
66f6bfd9 1859{
1860 //
1861 // Fills clusters into TRD tracking sectors
1862 // Function for use in the HLT
1863
1864 if(!clusters || !clusters->GetEntriesFast()){
1865 AliInfo("No TRD clusters");
41702fec 1866 return 1;
1867 }
1868
66f6bfd9 1869 fClusters = clusters;
1870 SetClustersOwner();
1871
1872 //Int_t nin =
1873 BuildTrackingContainers();
1874
1875 //Int_t ncl = fClusters->GetEntriesFast();
1876 //AliInfo(Form("Clusters %d [%6.2f %% in the active volume]", ncl, 100.*float(nin)/ncl));
1877
1878 return 0;
1879}
1880
1881
1882//____________________________________________________________________
1883Int_t AliTRDtrackerV1::BuildTrackingContainers()
1884{
1885// Building tracking containers for clusters
1886
1887 Int_t nin =0, icl = fClusters->GetEntriesFast();
41702fec 1888 while (icl--) {
1889 AliTRDcluster *c = (AliTRDcluster *) fClusters->UncheckedAt(icl);
1890 if(c->IsInChamber()) nin++;
1891 Int_t detector = c->GetDetector();
1892 Int_t sector = fGeom->GetSector(detector);
053767a4 1893 Int_t stack = fGeom->GetStack(detector);
1894 Int_t layer = fGeom->GetLayer(detector);
41702fec 1895
053767a4 1896 fTrSec[sector].GetChamber(stack, layer, kTRUE)->InsertCluster(c, icl);
41702fec 1897 }
b0a48c4d 1898
1899 const AliTRDCalDet *cal = AliTRDcalibDB::Instance()->GetT0Det();
053767a4 1900 for(int isector =0; isector<AliTRDgeometry::kNsector; isector++){
41702fec 1901 if(!fTrSec[isector].GetNChambers()) continue;
4d6aee34 1902 fTrSec[isector].Init(fkReconstructor, cal);
41702fec 1903 }
66f6bfd9 1904
1905 return nin;
eb38ed55 1906}
1907
1908
66f6bfd9 1909
0906e73e 1910//____________________________________________________________________
172b6f82 1911void AliTRDtrackerV1::UnloadClusters()
0906e73e 1912{
d4cf71ca 1913//
1914// Clears the arrays of clusters and tracks. Resets sectors and timebins
1915// If option "force" is also set the containers are also deleted. This is useful
1916// in case of HLT
1917
1918 if(fTracks){
1919 fTracks->Delete();
4d6aee34 1920 if(HasRemoveContainers()){delete fTracks; fTracks = NULL;}
d4cf71ca 1921 }
1922 if(fTracklets){
1923 fTracklets->Delete();
4d6aee34 1924 if(HasRemoveContainers()){delete fTracklets; fTracklets = NULL;}
d4cf71ca 1925 }
48f8adf3 1926 if(fClusters){
1927 if(IsClustersOwner()) fClusters->Delete();
1928
1929 // save clusters array in the reconstructor for further use.
4d6aee34 1930 if(!fkReconstructor->IsWritingClusters()){
48f8adf3 1931 AliTRDReconstructor::SetClusters(fClusters);
1932 SetClustersOwner(kFALSE);
4d6aee34 1933 } else AliTRDReconstructor::SetClusters(NULL);
48f8adf3 1934 }
0906e73e 1935
053767a4 1936 for (int i = 0; i < AliTRDgeometry::kNsector; i++) fTrSec[i].Clear();
0906e73e 1937
41702fec 1938 // Increment the Event Number
1939 AliTRDtrackerDebug::SetEventNumber(AliTRDtrackerDebug::GetEventNumber() + 1);
eb38ed55 1940}
0906e73e 1941
fac58f00 1942// //____________________________________________________________________
1943// void AliTRDtrackerV1::UseClusters(const AliKalmanTrack *t, Int_t) const
1944// {
1945// const AliTRDtrackV1 *track = dynamic_cast<const AliTRDtrackV1*>(t);
1946// if(!track) return;
1947//
4d6aee34 1948// AliTRDseedV1 *tracklet = NULL;
fac58f00 1949// for(Int_t ily=AliTRDgeometry::kNlayer; ily--;){
1950// if(!(tracklet = track->GetTracklet(ily))) continue;
4d6aee34 1951// AliTRDcluster *c = NULL;
8d2bec9e 1952// for(Int_t ic=AliTRDseed::kNclusters; ic--;){
fac58f00 1953// if(!(c=tracklet->GetClusters(ic))) continue;
1954// c->Use();
1955// }
1956// }
1957// }
1958//
b1957d3c 1959
eb38ed55 1960//_____________________________________________________________________________
4d6aee34 1961Bool_t AliTRDtrackerV1::AdjustSector(AliTRDtrackV1 *const track)
eb38ed55 1962{
41702fec 1963 //
1964 // Rotates the track when necessary
1965 //
1966
1967 Double_t alpha = AliTRDgeometry::GetAlpha();
1968 Double_t y = track->GetY();
1969 Double_t ymax = track->GetX()*TMath::Tan(0.5*alpha);
3cfaffa4 1970
41702fec 1971 if (y > ymax) {
1972 if (!track->Rotate( alpha)) {
1973 return kFALSE;
1974 }
1975 }
1976 else if (y < -ymax) {
1977 if (!track->Rotate(-alpha)) {
1978 return kFALSE;
1979 }
1980 }
1981
1982 return kTRUE;
0906e73e 1983
1984}
1985
eb38ed55 1986
0906e73e 1987//____________________________________________________________________
4d6aee34 1988AliTRDseedV1* AliTRDtrackerV1::GetTracklet(AliTRDtrackV1 *const track, Int_t p, Int_t &idx)
0906e73e 1989{
41702fec 1990 // Find tracklet for TRD track <track>
1991 // Parameters
1992 // - track
1993 // - sector
1994 // - plane
1995 // - index
1996 // Output
1997 // tracklet
1998 // index
1999 // Detailed description
2000 //
2001 idx = track->GetTrackletIndex(p);
4d6aee34 2002 AliTRDseedV1 *tracklet = (idx==0xffff) ? NULL : (AliTRDseedV1*)fTracklets->UncheckedAt(idx);
41702fec 2003
2004 return tracklet;
0906e73e 2005}
2006
2007//____________________________________________________________________
4d6aee34 2008AliTRDseedV1* AliTRDtrackerV1::SetTracklet(const AliTRDseedV1 * const tracklet)
0906e73e 2009{
41702fec 2010 // Add this tracklet to the list of tracklets stored in the tracker
2011 //
2012 // Parameters
2013 // - tracklet : pointer to the tracklet to be added to the list
2014 //
2015 // Output
2016 // - the index of the new tracklet in the tracker tracklets list
2017 //
2018 // Detailed description
2019 // Build the tracklets list if it is not yet created (late initialization)
2020 // and adds the new tracklet to the list.
2021 //
2022 if(!fTracklets){
053767a4 2023 fTracklets = new TClonesArray("AliTRDseedV1", AliTRDgeometry::Nsector()*kMaxTracksStack);
41702fec 2024 fTracklets->SetOwner(kTRUE);
2025 }
2026 Int_t nentries = fTracklets->GetEntriesFast();
2027 return new ((*fTracklets)[nentries]) AliTRDseedV1(*tracklet);
972ef65e 2028}
2029
d20df6fc 2030//____________________________________________________________________
4d6aee34 2031AliTRDtrackV1* AliTRDtrackerV1::SetTrack(const AliTRDtrackV1 * const track)
d20df6fc 2032{
2033 // Add this track to the list of tracks stored in the tracker
2034 //
2035 // Parameters
2036 // - track : pointer to the track to be added to the list
2037 //
2038 // Output
2039 // - the pointer added
2040 //
2041 // Detailed description
2042 // Build the tracks list if it is not yet created (late initialization)
2043 // and adds the new track to the list.
2044 //
2045 if(!fTracks){
053767a4 2046 fTracks = new TClonesArray("AliTRDtrackV1", AliTRDgeometry::Nsector()*kMaxTracksStack);
d20df6fc 2047 fTracks->SetOwner(kTRUE);
2048 }
2049 Int_t nentries = fTracks->GetEntriesFast();
2050 return new ((*fTracks)[nentries]) AliTRDtrackV1(*track);
2051}
2052
2053
0906e73e 2054
e4f2f73d 2055//____________________________________________________________________
eb38ed55 2056Int_t AliTRDtrackerV1::Clusters2TracksSM(Int_t sector, AliESDEvent *esd)
e4f2f73d 2057{
41702fec 2058 //
2059 // Steer tracking for one SM.
2060 //
2061 // Parameters :
2062 // sector : Array of (SM) propagation layers containing clusters
2063 // esd : The current ESD event. On output it contains the also
2064 // the ESD (TRD) tracks found in this SM.
2065 //
2066 // Output :
2067 // Number of tracks found in this TRD supermodule.
2068 //
2069 // Detailed description
2070 //
2071 // 1. Unpack AliTRDpropagationLayers objects for each stack.
2072 // 2. Launch stack tracking.
2073 // See AliTRDtrackerV1::Clusters2TracksStack() for details.
2074 // 3. Pack results in the ESD event.
2075 //
2076
2077 // allocate space for esd tracks in this SM
2078 TClonesArray esdTrackList("AliESDtrack", 2*kMaxTracksStack);
2079 esdTrackList.SetOwner();
2080
2081 Int_t nTracks = 0;
2082 Int_t nChambers = 0;
4d6aee34 2083 AliTRDtrackingChamber **stack = NULL, *chamber = NULL;
053767a4 2084 for(int istack = 0; istack<AliTRDgeometry::kNstack; istack++){
41702fec 2085 if(!(stack = fTrSec[sector].GetStack(istack))) continue;
2086 nChambers = 0;
053767a4 2087 for(int ilayer=0; ilayer<AliTRDgeometry::kNlayer; ilayer++){
2088 if(!(chamber = stack[ilayer])) continue;
4d6aee34 2089 if(chamber->GetNClusters() < fgNTimeBins * fkReconstructor->GetRecoParam() ->GetFindableClusters()) continue;
41702fec 2090 nChambers++;
053767a4 2091 //AliInfo(Form("sector %d stack %d layer %d clusters %d", sector, istack, ilayer, chamber->GetNClusters()));
41702fec 2092 }
2093 if(nChambers < 4) continue;
2094 //AliInfo(Form("Doing stack %d", istack));
2095 nTracks += Clusters2TracksStack(stack, &esdTrackList);
2096 }
2097 //AliInfo(Form("Found %d tracks in SM %d [%d]\n", nTracks, sector, esd->GetNumberOfTracks()));
2098
2099 for(int itrack=0; itrack<nTracks; itrack++)
2100 esd->AddTrack((AliESDtrack*)esdTrackList[itrack]);
2101
2102 // Reset Track and Candidate Number
2103 AliTRDtrackerDebug::SetCandidateNumber(0);
2104 AliTRDtrackerDebug::SetTrackNumber(0);
2105 return nTracks;
e4f2f73d 2106}
2107
2108//____________________________________________________________________
4d6aee34 2109Int_t AliTRDtrackerV1::Clusters2TracksStack(AliTRDtrackingChamber **stack, TClonesArray * const esdTrackList)
e4f2f73d 2110{
41702fec 2111 //
2112 // Make tracks in one TRD stack.
2113 //
2114 // Parameters :
2115 // layer : Array of stack propagation layers containing clusters
2116 // esdTrackList : Array of ESD tracks found by the stand alone tracker.
2117 // On exit the tracks found in this stack are appended.
2118 //
2119 // Output :
2120 // Number of tracks found in this stack.
2121 //
2122 // Detailed description
2123 //
2124 // 1. Find the 3 most useful seeding chambers. See BuildSeedingConfigs() for details.
2125 // 2. Steer AliTRDtrackerV1::MakeSeeds() for 3 seeding layer configurations.
2126 // See AliTRDtrackerV1::MakeSeeds() for more details.
2127 // 3. Arrange track candidates in decreasing order of their quality
2128 // 4. Classify tracks in 5 categories according to:
2129 // a) number of layers crossed
2130 // b) track quality
2131 // 5. Sign clusters by tracks in decreasing order of track quality
2132 // 6. Build AliTRDtrack out of seeding tracklets
2133 // 7. Cook MC label
2134 // 8. Build ESD track and register it to the output list
2135 //
2136
b0a48c4d 2137 const AliTRDCalDet *cal = AliTRDcalibDB::Instance()->GetT0Det();
4d6aee34 2138 AliTRDtrackingChamber *chamber = NULL;
2139 AliTRDtrackingChamber **ci = NULL;
41702fec 2140 AliTRDseedV1 sseed[kMaxTracksStack*6]; // to be initialized
2141 Int_t pars[4]; // MakeSeeds parameters
2142
2143 //Double_t alpha = AliTRDgeometry::GetAlpha();
2144 //Double_t shift = .5 * alpha;
2145 Int_t configs[kNConfigs];
2146
fac58f00 2147 // Purge used clusters from the containers
2148 ci = &stack[0];
2149 for(Int_t ic = kNPlanes; ic--; ci++){
2150 if(!(*ci)) continue;
2151 (*ci)->Update();
2152 }
2153
41702fec 2154 // Build initial seeding configurations
2155 Double_t quality = BuildSeedingConfigs(stack, configs);
4d6aee34 2156 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 10){
41702fec 2157 AliInfo(Form("Plane config %d %d %d Quality %f"
2158 , configs[0], configs[1], configs[2], quality));
2159 }
d931f2aa 2160
41702fec 2161
2162 // Initialize contors
2163 Int_t ntracks, // number of TRD track candidates
2164 ntracks1, // number of registered TRD tracks/iter
2165 ntracks2 = 0; // number of all registered TRD tracks in stack
2166 fSieveSeeding = 0;
d931f2aa 2167
2168 // Get stack index
fac58f00 2169 Int_t ic = 0; ci = &stack[0];
2170 while(ic<kNPlanes && !(*ci)){ic++; ci++;}
2171 if(!(*ci)) return ntracks2;
2172 Int_t istack = fGeom->GetStack((*ci)->GetDetector());
d931f2aa 2173
41702fec 2174 do{
2175 // Loop over seeding configurations
2176 ntracks = 0; ntracks1 = 0;
2177 for (Int_t iconf = 0; iconf<3; iconf++) {
2178 pars[0] = configs[iconf];
2179 pars[1] = ntracks;
d931f2aa 2180 pars[2] = istack;
41702fec 2181 ntracks = MakeSeeds(stack, &sseed[6*ntracks], pars);
6e39bde4 2182 //AliInfo(Form("Number of Tracks after iteration step %d: %d\n", iconf, ntracks));
41702fec 2183 if(ntracks == kMaxTracksStack) break;
2184 }
4d6aee34 2185 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 1) AliInfo(Form("Candidate TRD tracks %d in iteration %d.", ntracks, fSieveSeeding));
41702fec 2186
2187 if(!ntracks) break;
2188
2189 // Sort the seeds according to their quality
2190 Int_t sort[kMaxTracksStack];
2191 TMath::Sort(ntracks, fTrackQuality, sort, kTRUE);
2192
2193 // Initialize number of tracks so far and logic switches
2194 Int_t ntracks0 = esdTrackList->GetEntriesFast();
2195 Bool_t signedTrack[kMaxTracksStack];
2196 Bool_t fakeTrack[kMaxTracksStack];
2197 for (Int_t i=0; i<ntracks; i++){
2198 signedTrack[i] = kFALSE;
2199 fakeTrack[i] = kFALSE;
2200 }
2201 //AliInfo("Selecting track candidates ...");
2202
2203 // Sieve clusters in decreasing order of track quality
2204 Double_t trackParams[7];
4d6aee34 2205 // AliTRDseedV1 *lseed = NULL;
41702fec 2206 Int_t jSieve = 0, candidates;
2207 do{
2208 //AliInfo(Form("\t\tITER = %i ", jSieve));
2209
2210 // Check track candidates
2211 candidates = 0;
2212 for (Int_t itrack = 0; itrack < ntracks; itrack++) {
804bb02e 2213 Int_t trackIndex = sort[itrack];
2214 if (signedTrack[trackIndex] || fakeTrack[trackIndex]) continue;
41702fec 2215
2216
804bb02e 2217 // Calculate track parameters from tracklets seeds
804bb02e 2218 Int_t ncl = 0;
2219 Int_t nused = 0;
2220 Int_t nlayers = 0;
2221 Int_t findable = 0;
2222 for (Int_t jLayer = 0; jLayer < kNPlanes; jLayer++) {
2223 Int_t jseed = kNPlanes*trackIndex+jLayer;
2224 if(!sseed[jseed].IsOK()) continue;
f29f13a6 2225 if (TMath::Abs(sseed[jseed].GetYref(0) / sseed[jseed].GetX0()) < 0.158) findable++;
eede7907 2226 // TODO here we get a sig fault which should never happen !
804bb02e 2227 sseed[jseed].UpdateUsed();
2228 ncl += sseed[jseed].GetN2();
2229 nused += sseed[jseed].GetNUsed();
2230 nlayers++;
804bb02e 2231 }
2232
f29f13a6 2233 // Filter duplicated tracks
2234 if (nused > 30){
2235 //printf("Skip %d nused %d\n", trackIndex, nused);
2236 fakeTrack[trackIndex] = kTRUE;
2237 continue;
2238 }
2239 if (Float_t(nused)/ncl >= .25){
2240 //printf("Skip %d nused/ncl >= .25\n", trackIndex);
2241 fakeTrack[trackIndex] = kTRUE;
2242 continue;
2243 }
41702fec 2244
f29f13a6 2245 // Classify tracks
2246 Bool_t skip = kFALSE;
2247 switch(jSieve){
2248 case 0:
2249 if(nlayers < 6) {skip = kTRUE; break;}
2250 if(TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -5.){skip = kTRUE; break;}
2251 break;
2252
2253 case 1:
2254 if(nlayers < findable){skip = kTRUE; break;}
2255 if(TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -4.){skip = kTRUE; break;}
2256 break;
2257
2258 case 2:
2259 if ((nlayers == findable) || (nlayers == 6)) { skip = kTRUE; break;}
2260 if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -6.0){skip = kTRUE; break;}
2261 break;
2262
2263 case 3:
2264 if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -5.){skip = kTRUE; break;}
2265 break;
2266
2267 case 4:
2268 if (nlayers == 3){skip = kTRUE; break;}
2269 //if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) - nused/(nlayers-3.0) < -15.0){skip = kTRUE; break;}
2270 break;
2271 }
2272 if(skip){
2273 candidates++;
2274 //printf("REJECTED : %d [%d] nlayers %d trackQuality = %e nused %d\n", itrack, trackIndex, nlayers, fTrackQuality[trackIndex], nused);
2275 continue;
2276 }
2277 signedTrack[trackIndex] = kTRUE;
2278
2279 // Build track parameters
2280 AliTRDseedV1 *lseed =&sseed[trackIndex*6];
2281 /* Int_t idx = 0;
2282 while(idx<3 && !lseed->IsOK()) {
2283 idx++;
2284 lseed++;
2285 }*/
2286 Double_t x = lseed->GetX0();// - 3.5;
2287 trackParams[0] = x; //NEW AB
2288 trackParams[1] = lseed->GetYref(0); // lseed->GetYat(x);
2289 trackParams[2] = lseed->GetZref(0); // lseed->GetZat(x);
2290 trackParams[3] = TMath::Sin(TMath::ATan(lseed->GetYref(1)));
2291 trackParams[4] = lseed->GetZref(1) / TMath::Sqrt(1. + lseed->GetYref(1) * lseed->GetYref(1));
2292 trackParams[5] = lseed->GetC();
2293 Int_t ich = 0; while(!(chamber = stack[ich])) ich++;
2294 trackParams[6] = fGeom->GetSector(chamber->GetDetector());/* *alpha+shift; // Supermodule*/
2295
4d6aee34 2296 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 1){
eb2b4f91 2297 //AliInfo(Form("Track %d [%d] nlayers %d trackQuality = %e nused %d, yref = %3.3f", itrack, trackIndex, nlayers, fTrackQuality[trackIndex], nused, trackParams[1]));
f29f13a6 2298
f29f13a6 2299 AliTRDseedV1 *dseed[6];
b82b4de1 2300 for(Int_t iseed = AliTRDgeometry::kNlayer; iseed--;) dseed[iseed] = new AliTRDseedV1(lseed[iseed]);
f29f13a6 2301
2302 //Int_t eventNrInFile = esd->GetEventNumberInFile();
f29f13a6 2303 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2304 Int_t trackNumber = AliTRDtrackerDebug::GetTrackNumber();
2305 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
4d6aee34 2306 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
f29f13a6 2307 cstreamer << "Clusters2TracksStack"
2308 << "EventNumber=" << eventNumber
2309 << "TrackNumber=" << trackNumber
2310 << "CandidateNumber=" << candidateNumber
2311 << "Iter=" << fSieveSeeding
2312 << "Like=" << fTrackQuality[trackIndex]
2313 << "S0.=" << dseed[0]
2314 << "S1.=" << dseed[1]
2315 << "S2.=" << dseed[2]
2316 << "S3.=" << dseed[3]
2317 << "S4.=" << dseed[4]
2318 << "S5.=" << dseed[5]
2319 << "p0=" << trackParams[0]
2320 << "p1=" << trackParams[1]
2321 << "p2=" << trackParams[2]
2322 << "p3=" << trackParams[3]
2323 << "p4=" << trackParams[4]
2324 << "p5=" << trackParams[5]
2325 << "p6=" << trackParams[6]
f29f13a6 2326 << "Ncl=" << ncl
2327 << "NLayers=" << nlayers
2328 << "Findable=" << findable
2329 << "NUsed=" << nused
2330 << "\n";
2331 }
d877f55f 2332
f29f13a6 2333 AliTRDtrackV1 *track = MakeTrack(&sseed[trackIndex*kNPlanes], trackParams);
2334 if(!track){
2335 AliWarning("Fail to build a TRD Track.");
2336 continue;
d877f55f 2337 }
41702fec 2338
f29f13a6 2339 //AliInfo("End of MakeTrack()");
2340 AliESDtrack *esdTrack = new ((*esdTrackList)[ntracks0++]) AliESDtrack();
2341 esdTrack->UpdateTrackParams(track, AliESDtrack::kTRDout);
2342 esdTrack->SetLabel(track->GetLabel());
2343 track->UpdateESDtrack(esdTrack);
2344 // write ESD-friends if neccessary
4d6aee34 2345 if (fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 0){
f29f13a6 2346 AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(*track);
2347 calibTrack->SetOwner();
2348 esdTrack->AddCalibObject(calibTrack);
2349 }
2350 ntracks1++;
2351 AliTRDtrackerDebug::SetTrackNumber(AliTRDtrackerDebug::GetTrackNumber() + 1);
41702fec 2352 }
2353
2354 jSieve++;
2355 } while(jSieve<5 && candidates); // end track candidates sieve
2356 if(!ntracks1) break;
2357
2358 // increment counters
2359 ntracks2 += ntracks1;
4302c900 2360
4d6aee34 2361 if(fkReconstructor->IsHLT()) break;
41702fec 2362 fSieveSeeding++;
2363
2364 // Rebuild plane configurations and indices taking only unused clusters into account
2365 quality = BuildSeedingConfigs(stack, configs);
4d6aee34 2366 if(quality < 1.E-7) break; //fkReconstructor->GetRecoParam() ->GetPlaneQualityThreshold()) break;
41702fec 2367
2368 for(Int_t ip = 0; ip < kNPlanes; ip++){
2369 if(!(chamber = stack[ip])) continue;
b0a48c4d 2370 chamber->Build(fGeom, cal);//Indices(fSieveSeeding);
41702fec 2371 }
2372
4d6aee34 2373 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 10){
41702fec 2374 AliInfo(Form("Sieve level %d Plane config %d %d %d Quality %f", fSieveSeeding, configs[0], configs[1], configs[2], quality));
2375 }
2376 } while(fSieveSeeding<10); // end stack clusters sieve
2377
2378
2379
2380 //AliInfo(Form("Registered TRD tracks %d in stack %d.", ntracks2, pars[1]));
2381
2382 return ntracks2;
e4f2f73d 2383}
2384
2385//___________________________________________________________________
eb38ed55 2386Double_t AliTRDtrackerV1::BuildSeedingConfigs(AliTRDtrackingChamber **stack, Int_t *configs)
e4f2f73d 2387{
41702fec 2388 //
2389 // Assign probabilities to chambers according to their
2390 // capability of producing seeds.
2391 //
2392 // Parameters :
2393 //
2394 // layers : Array of stack propagation layers for all 6 chambers in one stack
2395 // configs : On exit array of configuration indexes (see GetSeedingConfig()
2396 // for details) in the decreasing order of their seeding probabilities.
2397 //
2398 // Output :
2399 //
2400 // Return top configuration quality
2401 //
2402 // Detailed description:
2403 //
2404 // To each chamber seeding configuration (see GetSeedingConfig() for
2405 // the list of all configurations) one defines 2 quality factors:
2406 // - an apriori topological quality (see GetSeedingConfig() for details) and
2407 // - a data quality based on the uniformity of the distribution of
2408 // clusters over the x range (time bins population). See CookChamberQA() for details.
2409 // The overall chamber quality is given by the product of this 2 contributions.
2410 //
2411
eb2b4f91 2412 Double_t chamberQ[kNPlanes];memset(chamberQ, 0, kNPlanes*sizeof(Double_t));
4d6aee34 2413 AliTRDtrackingChamber *chamber = NULL;
41702fec 2414 for(int iplane=0; iplane<kNPlanes; iplane++){
2415 if(!(chamber = stack[iplane])) continue;
2416 chamberQ[iplane] = (chamber = stack[iplane]) ? chamber->GetQuality() : 0.;
2417 }
2418
eb2b4f91 2419 Double_t tconfig[kNConfigs];memset(tconfig, 0, kNConfigs*sizeof(Double_t));
2420 Int_t planes[] = {0, 0, 0, 0};
41702fec 2421 for(int iconf=0; iconf<kNConfigs; iconf++){
2422 GetSeedingConfig(iconf, planes);
2423 tconfig[iconf] = fgTopologicQA[iconf];
2424 for(int iplane=0; iplane<4; iplane++) tconfig[iconf] *= chamberQ[planes[iplane]];
2425 }
2426
2427 TMath::Sort((Int_t)kNConfigs, tconfig, configs, kTRUE);
2428 // AliInfo(Form("q[%d] = %f", configs[0], tconfig[configs[0]]));
2429 // AliInfo(Form("q[%d] = %f", configs[1], tconfig[configs[1]]));
2430 // AliInfo(Form("q[%d] = %f", configs[2], tconfig[configs[2]]));
2431
2432 return tconfig[configs[0]];
e4f2f73d 2433}
2434
2435//____________________________________________________________________
4d6aee34 2436Int_t AliTRDtrackerV1::MakeSeeds(AliTRDtrackingChamber **stack, const AliTRDseedV1 * const sseed, const Int_t * const ipar)
e4f2f73d 2437{
afa21247 2438//
2a3191bb 2439// Seed tracklets and build candidate TRD tracks. The procedure is used during barrel tracking to account for tracks which are
2440// either missed by TPC prolongation or conversions inside the TRD volume.
2441// For stand alone tracking the procedure is used to estimate all tracks measured by TRD.
afa21247 2442//
2443// Parameters :
2444// layers : Array of stack propagation layers containing clusters
2445// sseed : Array of empty tracklet seeds. On exit they are filled.
2446// ipar : Control parameters:
2447// ipar[0] -> seeding chambers configuration
2448// ipar[1] -> stack index
2449// ipar[2] -> number of track candidates found so far
2450//
2451// Output :
2452// Number of tracks candidates found.
2453//
2454// The following steps are performed:
2455// 1. Build seeding layers by collapsing all time bins from each of the four seeding chambers along the
2456// radial coordinate. See AliTRDtrackingChamber::GetSeedingLayer() for details. The chambers selection for seeding
2457// is described in AliTRDtrackerV1::Clusters2TracksStack().
2458// 2. Using the seeding clusters from the seeding layer (step 1) build combinatorics using the following algorithm:
2459// - for each seeding cluster in the lower seeding layer find
2460// - all seeding clusters in the upper seeding layer inside a road defined by a given phi angle. The angle
2461// is calculated on the minimum pt of tracks from vertex accesible to the stand alone tracker.
2462// - for each pair of two extreme seeding clusters select middle upper cluster using roads defined externally by the
2463// reco params
2464// - select last seeding cluster as the nearest to the linear approximation of the track described by the first three
2465// seeding clusters.
2466// The implementation of road calculation and cluster selection can be found in the functions AliTRDchamberTimeBin::BuildCond()
2467// and AliTRDchamberTimeBin::GetClusters().
2468// 3. Helix fit of the seeding clusters set. (see AliTRDtrackerFitter::FitRieman(AliTRDcluster**)). No tilt correction is
2469// performed at this level
2470// 4. Initialize seeding tracklets in the seeding chambers.
2471// 5. *Filter 0* Chi2 cut on the Y and Z directions. The threshold is set externally by the reco params.
2472// 6. Attach (true) clusters to seeding tracklets (see AliTRDseedV1::AttachClusters()) and fit tracklet (see
2473// AliTRDseedV1::Fit()). The number of used clusters used by current seeds should not exceed ... (25).
2474// 7. *Filter 1* Check if all 4 seeding tracklets are correctly constructed.
2475// 8. Helix fit of the clusters from the seeding tracklets with tilt correction. Refit tracklets using the new
2476// approximation of the track.
2477// 9. *Filter 2* Calculate likelihood of the track. (See AliTRDtrackerV1::CookLikelihood()). The following quantities are
2478// checked against the Riemann fit:
2479// - position resolution in y
2480// - angular resolution in the bending plane
2481// - likelihood of the number of clusters attached to the tracklet
2482// 10. Extrapolation of the helix fit to the other 2 chambers *non seeding* chambers:
2483// - Initialization of extrapolation tracklets with the fit parameters
2484// - Attach clusters to extrapolated tracklets
2485// - Helix fit of tracklets
2486// 11. Improve seeding tracklets quality by reassigning clusters based on the last parameters of the track
2487// See AliTRDtrackerV1::ImproveSeedQuality() for details.
2488// 12. Helix fit of all 6 seeding tracklets and chi2 calculation
2489// 13. Hyperplane fit and track quality calculation. See AliTRDtrackerFitter::FitHyperplane() for details.
2490// 14. Cooking labels for tracklets. Should be done only for MC
2491// 15. Register seeds.
2492//
2493// Authors:
2494// Marian Ivanov <M.Ivanov@gsi.de>
2495// Alexandru Bercuci <A.Bercuci@gsi.de>
2496// Markus Fasel <M.Fasel@gsi.de>
41702fec 2497
4d6aee34 2498 AliTRDtrackingChamber *chamber = NULL;
2499 AliTRDcluster *c[kNSeedPlanes] = {NULL, NULL, NULL, NULL}; // initilize seeding clusters
2500 AliTRDseedV1 *cseed = const_cast<AliTRDseedV1 *>(&sseed[0]); // initialize tracklets for first track
41702fec 2501 Int_t ncl, mcl; // working variable for looping over clusters
2502 Int_t index[AliTRDchamberTimeBin::kMaxClustersLayer], jndex[AliTRDchamberTimeBin::kMaxClustersLayer];
2503 // chi2 storage
2504 // chi2[0] = tracklet chi2 on the Z direction
2505 // chi2[1] = tracklet chi2 on the R direction
2506 Double_t chi2[4];
2507
afa21247 2508 // this should be data member of AliTRDtrack TODO
41702fec 2509 Double_t seedQuality[kMaxTracksStack];
2510
2511 // unpack control parameters
2512 Int_t config = ipar[0];
2513 Int_t ntracks = ipar[1];
d931f2aa 2514 Int_t istack = ipar[2];
41702fec 2515 Int_t planes[kNSeedPlanes]; GetSeedingConfig(config, planes);
afa21247 2516 Int_t planesExt[kNPlanes-kNSeedPlanes]; GetExtrapolationConfig(config, planesExt);
be24510a 2517
2518
41702fec 2519 // Init chambers geometry
41702fec 2520 Double_t hL[kNPlanes]; // Tilting angle
2521 Float_t padlength[kNPlanes]; // pad lenghts
dd8059a8 2522 Float_t padwidth[kNPlanes]; // pad widths
4d6aee34 2523 AliTRDpadPlane *pp = NULL;
41702fec 2524 for(int iplane=0; iplane<kNPlanes; iplane++){
2525 pp = fGeom->GetPadPlane(iplane, istack);
bb79ccd5 2526 hL[iplane] = TMath::Tan(TMath::DegToRad()*pp->GetTiltingAngle());
41702fec 2527 padlength[iplane] = pp->GetLengthIPad();
dd8059a8 2528 padwidth[iplane] = pp->GetWidthIPad();
41702fec 2529 }
2530
eb2b4f91 2531 // Init anode wire position for chambers
e165b64b 2532 Double_t x0[kNPlanes], // anode wire position
eb2b4f91 2533 driftLength = .5*AliTRDgeometry::AmThick() - AliTRDgeometry::DrThick(); // drift length
4d6aee34 2534 TGeoHMatrix *matrix = NULL;
eb2b4f91 2535 Double_t loc[] = {AliTRDgeometry::AnodePos(), 0., 0.};
2536 Double_t glb[] = {0., 0., 0.};
2537 AliTRDtrackingChamber **cIter = &stack[0];
a3743898 2538 for(int iLayer=0; iLayer<kNPlanes; iLayer++,cIter++){
eb2b4f91 2539 if(!(*cIter)) continue;
e165b64b 2540 if(!(matrix = fGeom->GetClusterMatrix((*cIter)->GetDetector()))){
2541 continue;
2542 x0[iLayer] = fgkX0[iLayer];
2543 }
eb2b4f91 2544 matrix->LocalToMaster(loc, glb);
2545 x0[iLayer] = glb[0];
2546 }
2547
4d6aee34 2548 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 2){
41702fec 2549 AliInfo(Form("Making seeds Stack[%d] Config[%d] Tracks[%d]...", istack, config, ntracks));
2550 }
2551
d931f2aa 2552 // Build seeding layers
d611c74f 2553 ResetSeedTB();
41702fec 2554 Int_t nlayers = 0;
41702fec 2555 for(int isl=0; isl<kNSeedPlanes; isl++){
2556 if(!(chamber = stack[planes[isl]])) continue;
4d6aee34 2557 if(!chamber->GetSeedingLayer(fSeedTB[isl], fGeom, fkReconstructor)) continue;
41702fec 2558 nlayers++;
41702fec 2559 }
eb2b4f91 2560 if(nlayers < kNSeedPlanes) return ntracks;
41702fec 2561
2562
2563 // Start finding seeds
2564 Double_t cond0[4], cond1[4], cond2[4];
2565 Int_t icl = 0;
d611c74f 2566 while((c[3] = (*fSeedTB[3])[icl++])){
41702fec 2567 if(!c[3]) continue;
d611c74f 2568 fSeedTB[0]->BuildCond(c[3], cond0, 0);
2569 fSeedTB[0]->GetClusters(cond0, index, ncl);
41702fec 2570 //printf("Found c[3] candidates 0 %d\n", ncl);
2571 Int_t jcl = 0;
2572 while(jcl<ncl) {
d611c74f 2573 c[0] = (*fSeedTB[0])[index[jcl++]];
41702fec 2574 if(!c[0]) continue;
2575 Double_t dx = c[3]->GetX() - c[0]->GetX();
afa21247 2576 Double_t dzdx = (c[3]->GetZ() - c[0]->GetZ())/dx;
2577 Double_t dydx = (c[3]->GetY() - c[0]->GetY())/dx;
2578 fSeedTB[1]->BuildCond(c[0], cond1, 1, dzdx, dydx);
d611c74f 2579 fSeedTB[1]->GetClusters(cond1, jndex, mcl);
41702fec 2580 //printf("Found c[0] candidates 1 %d\n", mcl);
2581
2582 Int_t kcl = 0;
2583 while(kcl<mcl) {
d611c74f 2584 c[1] = (*fSeedTB[1])[jndex[kcl++]];
2585 if(!c[1]) continue;
afa21247 2586 fSeedTB[2]->BuildCond(c[1], cond2, 2, dzdx, dydx);
d611c74f 2587 c[2] = fSeedTB[2]->GetNearestCluster(cond2);
2588 //printf("Found c[1] candidate 2 %p\n", c[2]);
2589 if(!c[2]) continue;
2590
6e39bde4 2591 //AliInfo("Seeding clusters found. Building seeds ...");
2592 //for(Int_t i = 0; i < kNSeedPlanes; i++) printf("%i. coordinates: x = %6.3f, y = %6.3f, z = %6.3f\n", i, c[i]->GetX(), c[i]->GetY(), c[i]->GetZ());
d611c74f 2593
804bb02e 2594 for (Int_t il = 0; il < kNPlanes; il++) cseed[il].Reset();
41702fec 2595
d611c74f 2596 FitRieman(c, chi2);
2597
d931f2aa 2598 AliTRDseedV1 *tseed = &cseed[0];
bb2db46c 2599 cIter = &stack[0];
2600 for(int iLayer=0; iLayer<kNPlanes; iLayer++, tseed++, cIter++){
2601 Int_t det = (*cIter) ? (*cIter)->GetDetector() : -1;
eb2b4f91 2602 tseed->SetDetector(det);
43d6ad34 2603 tseed->SetTilt(hL[iLayer]);
2604 tseed->SetPadLength(padlength[iLayer]);
dd8059a8 2605 tseed->SetPadWidth(padwidth[iLayer]);
4d6aee34 2606 tseed->SetReconstructor(fkReconstructor);
eb2b4f91 2607 tseed->SetX0(det<0 ? fR[iLayer]+driftLength : x0[iLayer]);
d611c74f 2608 tseed->Init(GetRiemanFitter());
f29f13a6 2609 tseed->SetStandAlone(kTRUE);
d611c74f 2610 }
2611
2612 Bool_t isFake = kFALSE;
4d6aee34 2613 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 2){
d611c74f 2614 if (c[0]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
2615 if (c[1]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
2616 if (c[2]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
2617
2618 Double_t xpos[4];
2619 for(Int_t l = 0; l < kNSeedPlanes; l++) xpos[l] = fSeedTB[l]->GetX();
2620 Float_t yref[4];
2621 for(int il=0; il<4; il++) yref[il] = cseed[planes[il]].GetYref(0);
2622 Int_t ll = c[3]->GetLabel(0);
2623 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2624 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2625 AliRieman *rim = GetRiemanFitter();
4d6aee34 2626 TTreeSRedirector &cs0 = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
d611c74f 2627 cs0 << "MakeSeeds0"
2628 <<"EventNumber=" << eventNumber
2629 <<"CandidateNumber=" << candidateNumber
2630 <<"isFake=" << isFake
2631 <<"config=" << config
2632 <<"label=" << ll
2633 <<"chi2z=" << chi2[0]
2634 <<"chi2y=" << chi2[1]
2635 <<"Y2exp=" << cond2[0]
2636 <<"Z2exp=" << cond2[1]
2637 <<"X0=" << xpos[0] //layer[sLayer]->GetX()
2638 <<"X1=" << xpos[1] //layer[sLayer + 1]->GetX()
2639 <<"X2=" << xpos[2] //layer[sLayer + 2]->GetX()
2640 <<"X3=" << xpos[3] //layer[sLayer + 3]->GetX()
2641 <<"yref0=" << yref[0]
2642 <<"yref1=" << yref[1]
2643 <<"yref2=" << yref[2]
2644 <<"yref3=" << yref[3]
2645 <<"c0.=" << c[0]
2646 <<"c1.=" << c[1]
2647 <<"c2.=" << c[2]
2648 <<"c3.=" << c[3]
2649 <<"Seed0.=" << &cseed[planes[0]]
2650 <<"Seed1.=" << &cseed[planes[1]]
2651 <<"Seed2.=" << &cseed[planes[2]]
2652 <<"Seed3.=" << &cseed[planes[3]]
2653 <<"RiemanFitter.=" << rim
2654 <<"\n";
2655 }
4d6aee34 2656 if(chi2[0] > fkReconstructor->GetRecoParam() ->GetChi2Z()/*7./(3. - sLayer)*//*iter*/){
6e39bde4 2657 //AliInfo(Form("Failed chi2 filter on chi2Z [%f].", chi2[0]));
d611c74f 2658 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2659 continue;
2660 }
4d6aee34 2661 if(chi2[1] > fkReconstructor->GetRecoParam() ->GetChi2Y()/*1./(3. - sLayer)*//*iter*/){
6e39bde4 2662 //AliInfo(Form("Failed chi2 filter on chi2Y [%f].", chi2[1]));
d611c74f 2663 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2664 continue;
2665 }
2666 //AliInfo("Passed chi2 filter.");
2667
2668 // try attaching clusters to tracklets
6e39bde4 2669 Int_t mlayers = 0;
2670 AliTRDcluster *cl = NULL;
be24510a 2671 for(int iLayer=0; iLayer<kNSeedPlanes; iLayer++){
d611c74f 2672 Int_t jLayer = planes[iLayer];
6e39bde4 2673 Int_t nNotInChamber = 0;
f29f13a6 2674 if(!cseed[jLayer].AttachClusters(stack[jLayer], kTRUE)) continue;
6e39bde4 2675 cseed[jLayer].Fit();
f29f13a6 2676 cseed[jLayer].UpdateUsed();
6e39bde4 2677 cseed[jLayer].ResetClusterIter();
2678 while((cl = cseed[jLayer].NextCluster())){
2679 if(!cl->IsInChamber()) nNotInChamber++;
2680 }
2681 //printf("clusters[%d], used[%d], not in chamber[%d]\n", cseed[jLayer].GetN(), cseed[jLayer].GetNUsed(), nNotInChamber);
2682 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
d611c74f 2683 mlayers++;
2684 }
be24510a 2685
2686 if(mlayers < kNSeedPlanes){
2687 //AliInfo(Form("Failed updating all seeds %d [%d].", mlayers, kNSeedPlanes));
2688 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2689 continue;
2690 }
2691
2692 // temporary exit door for the HLT
4d6aee34 2693 if(fkReconstructor->IsHLT()){
be24510a 2694 // attach clusters to extrapolation chambers
2695 for(int iLayer=0; iLayer<kNPlanes-kNSeedPlanes; iLayer++){
2696 Int_t jLayer = planesExt[iLayer];
2697 if(!(chamber = stack[jLayer])) continue;
f29f13a6 2698 cseed[jLayer].AttachClusters(chamber, kTRUE);
6e39bde4 2699 cseed[jLayer].Fit();
be24510a 2700 }
4302c900 2701 fTrackQuality[ntracks] = 1.; // dummy value
2702 ntracks++;
218ba867 2703 if(ntracks == kMaxTracksStack) return ntracks;
4302c900 2704 cseed += 6;
2705 continue;
2706 }
2707
be24510a 2708
f29f13a6 2709 // Update Seeds and calculate Likelihood
d611c74f 2710 // fit tracklets and cook likelihood
f29f13a6 2711 FitTiltedRieman(&cseed[0], kTRUE);
2712 for(int iLayer=0; iLayer<kNSeedPlanes; iLayer++){
2713 Int_t jLayer = planes[iLayer];
2714 cseed[jLayer].Fit(kTRUE);
2715 }
91834b8d 2716 Double_t like = CookLikelihood(&cseed[0], planes); // to be checked
d611c74f 2717
4d6aee34 2718 if (TMath::Log(1.E-9 + like) < fkReconstructor->GetRecoParam() ->GetTrackLikelihood()){
d611c74f 2719 //AliInfo(Form("Failed likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
2720 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2721 continue;
2722 }
2723 //AliInfo(Form("Passed likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
2724
2725 // book preliminary results
2726 seedQuality[ntracks] = like;
2727 fSeedLayer[ntracks] = config;/*sLayer;*/
2728
2729 // attach clusters to the extrapolation seeds
be24510a 2730 for(int iLayer=0; iLayer<kNPlanes-kNSeedPlanes; iLayer++){
2731 Int_t jLayer = planesExt[iLayer];
d611c74f 2732 if(!(chamber = stack[jLayer])) continue;
d611c74f 2733
2734 // fit extrapolated seed
2735 if ((jLayer == 0) && !(cseed[1].IsOK())) continue;
2736 if ((jLayer == 5) && !(cseed[4].IsOK())) continue;
2737 AliTRDseedV1 pseed = cseed[jLayer];
f29f13a6 2738 if(!pseed.AttachClusters(chamber, kTRUE)) continue;
2739 pseed.Fit(kTRUE);
d611c74f 2740 cseed[jLayer] = pseed;
d611c74f 2741 FitTiltedRieman(cseed, kTRUE);
f29f13a6 2742 cseed[jLayer].Fit(kTRUE);
d611c74f 2743 }
2744
2745 // AliInfo("Extrapolation done.");
2746 // Debug Stream containing all the 6 tracklets
4d6aee34 2747 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 2){
2748 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
d611c74f 2749 TLinearFitter *tiltedRieman = GetTiltedRiemanFitter();
2750 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2751 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2752 cstreamer << "MakeSeeds1"
2753 << "EventNumber=" << eventNumber
2754 << "CandidateNumber=" << candidateNumber
2755 << "S0.=" << &cseed[0]
2756 << "S1.=" << &cseed[1]
2757 << "S2.=" << &cseed[2]
2758 << "S3.=" << &cseed[3]
2759 << "S4.=" << &cseed[4]
2760 << "S5.=" << &cseed[5]
2761 << "FitterT.=" << tiltedRieman
2762 << "\n";
2763 }
2764
4d6aee34 2765 if(fkReconstructor->HasImproveTracklets() && ImproveSeedQuality(stack, cseed) < 4){
d611c74f 2766 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2767 continue;
2768 }
2769 //AliInfo("Improve seed quality done.");
2770
2771 // fit full track and cook likelihoods
2772 // Double_t curv = FitRieman(&cseed[0], chi2);
2773 // Double_t chi2ZF = chi2[0] / TMath::Max((mlayers - 3.), 1.);
2774 // Double_t chi2RF = chi2[1] / TMath::Max((mlayers - 3.), 1.);
2775
2776 // do the final track fitting (Once with vertex constraint and once without vertex constraint)
2777 Double_t chi2Vals[3];
6e39bde4 2778 chi2Vals[0] = FitTiltedRieman(&cseed[0], kTRUE);
4d6aee34 2779 if(fkReconstructor->HasVertexConstrained())
d611c74f 2780 chi2Vals[1] = FitTiltedRiemanConstraint(&cseed[0], GetZ()); // Do Vertex Constrained fit if desired
2781 else
2782 chi2Vals[1] = 1.;
2783 chi2Vals[2] = GetChi2Z(&cseed[0]) / TMath::Max((mlayers - 3.), 1.);
2784 // Chi2 definitions in testing stage
2785 //chi2Vals[2] = GetChi2ZTest(&cseed[0]);
2786 fTrackQuality[ntracks] = CalculateTrackLikelihood(&cseed[0], &chi2Vals[0]);
2787 //AliInfo("Hyperplane fit done\n");
fac58f00 2788
4d6aee34 2789 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 2){
2790 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
d611c74f 2791 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2792 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2793 TLinearFitter *fitterTC = GetTiltedRiemanFitterConstraint();
2794 TLinearFitter *fitterT = GetTiltedRiemanFitter();
91834b8d 2795 Int_t ncls = 0;
2796 for(Int_t iseed = 0; iseed < kNPlanes; iseed++){
2797 ncls += cseed[iseed].IsOK() ? cseed[iseed].GetN2() : 0;
2798 }
d611c74f 2799 cstreamer << "MakeSeeds2"
2800 << "EventNumber=" << eventNumber
2801 << "CandidateNumber=" << candidateNumber
2802 << "Chi2TR=" << chi2Vals[0]
2803 << "Chi2TC=" << chi2Vals[1]
2804 << "Nlayers=" << mlayers
91834b8d 2805 << "NClusters=" << ncls
d611c74f 2806 << "Like=" << like
2807 << "S0.=" << &cseed[0]
2808 << "S1.=" << &cseed[1]
2809 << "S2.=" << &cseed[2]
2810 << "S3.=" << &cseed[3]
2811 << "S4.=" << &cseed[4]
2812 << "S5.=" << &cseed[5]
d611c74f 2813 << "FitterT.=" << fitterT
2814 << "FitterTC.=" << fitterTC
2815 << "\n";
2816 }
2817
2818 ntracks++;
2819 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2820 if(ntracks == kMaxTracksStack){
2821 AliWarning(Form("Number of seeds reached maximum allowed (%d) in stack.", kMaxTracksStack));
2822 return ntracks;
2823 }
2824 cseed += 6;
41702fec 2825 }
2826 }
2827 }
41702fec 2828
2829 return ntracks;
e4f2f73d 2830}
2831
2832//_____________________________________________________________________________
4d6aee34 2833AliTRDtrackV1* AliTRDtrackerV1::MakeTrack(const AliTRDseedV1 * const seeds, Double_t *params)
e4f2f73d 2834{
afa21247 2835//
2836// Build a TRD track out of tracklet candidates
2837//
2838// Parameters :
2839// seeds : array of tracklets
2840// params : array of track parameters as they are estimated by stand alone tracker. 7 elements.
2841// [0] - radial position of the track at reference point
2842// [1] - y position of the fit at [0]
2843// [2] - z position of the fit at [0]
2844// [3] - snp of the first tracklet
2845// [4] - tgl of the first tracklet
2846// [5] - curvature of the Riemann fit - 1/pt
2847// [6] - sector rotation angle
2848//
2849// Output :
2850// The TRD track.
2851//
2852// Initialize the TRD track based on the parameters of the fit and a parametric covariance matrix
2853// (diagonal with constant variance terms TODO - correct parameterization)
2854//
2855// In case of HLT just register the tracklets in the tracker and return values of the Riemann fit. For the
2856// offline case perform a full Kalman filter on the already found tracklets (see AliTRDtrackerV1::FollowBackProlongation()
2857// for details). Do also MC label calculation and PID if propagation successfully.
41702fec 2858
afa21247 2859
41702fec 2860 Double_t alpha = AliTRDgeometry::GetAlpha();
2861 Double_t shift = AliTRDgeometry::GetAlpha()/2.0;
2862 Double_t c[15];
2863
afa21247 2864 c[ 0] = 0.2; // s^2_y
2865 c[ 1] = 0.0; c[ 2] = 2.0; // s^2_z
2866 c[ 3] = 0.0; c[ 4] = 0.0; c[ 5] = 0.02; // s^2_snp
2867 c[ 6] = 0.0; c[ 7] = 0.0; c[ 8] = 0.0; c[ 9] = 0.1; // s^2_tgl
2868 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 2869
d20df6fc 2870 AliTRDtrackV1 track(seeds, &params[1], c, params[0], params[6]*alpha+shift);
2871 track.PropagateTo(params[0]-5.0);
4d6aee34 2872 AliTRDseedV1 *ptrTracklet = NULL;
9887cc9f 2873
2874 // skip Kalman filter for HLT
4d6aee34 2875 if(fkReconstructor->IsHLT()){
9887cc9f 2876 for (Int_t jLayer = 0; jLayer < AliTRDgeometry::kNlayer; jLayer++) {
2877 track.UnsetTracklet(jLayer);
4d6aee34 2878 ptrTracklet = const_cast<AliTRDseedV1 *>(&seeds[jLayer]);
9887cc9f 2879 if(!ptrTracklet->IsOK()) continue;
6e39bde4 2880 if(TMath::Abs(ptrTracklet->GetYref(1) - ptrTracklet->GetYfit(1)) >= .2) continue; // check this condition with Marian
9887cc9f 2881 ptrTracklet = SetTracklet(ptrTracklet);
f29f13a6 2882 ptrTracklet->UseClusters();
91834b8d 2883 track.SetTracklet(ptrTracklet, fTracklets->GetEntriesFast()-1);
2884 }
d78d7df0 2885 AliTRDtrackV1 *ptrTrack = SetTrack(&track);
799e38d7 2886 ptrTrack->CookPID();
4d6aee34 2887 ptrTrack->SetReconstructor(fkReconstructor);
d78d7df0 2888 return ptrTrack;
91834b8d 2889 }
393fda1c 2890
d20df6fc 2891 track.ResetCovariance(1);
e79f8eb0 2892 Int_t nc = TMath::Abs(FollowBackProlongation(track));
4d6aee34 2893 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) > 5){
393fda1c 2894 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2895 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2896 Double_t p[5]; // Track Params for the Debug Stream
2897 track.GetExternalParameters(params[0], p);
4d6aee34 2898 TTreeSRedirector &cs = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
393fda1c 2899 cs << "MakeTrack"
2900 << "EventNumber=" << eventNumber
2901 << "CandidateNumber=" << candidateNumber
2902 << "nc=" << nc
2903 << "X=" << params[0]
2904 << "Y=" << p[0]
2905 << "Z=" << p[1]
2906 << "snp=" << p[2]
2907 << "tnd=" << p[3]
2908 << "crv=" << p[4]
2909 << "Yin=" << params[1]
2910 << "Zin=" << params[2]
2911 << "snpin=" << params[3]
2912 << "tndin=" << params[4]
2913 << "crvin=" << params[5]
2914 << "track.=" << &track
2915 << "\n";
2916 }
4d6aee34 2917 if (nc < 30) return NULL;
d20df6fc 2918
2919 AliTRDtrackV1 *ptrTrack = SetTrack(&track);
4d6aee34 2920 ptrTrack->SetReconstructor(fkReconstructor);
48f8adf3 2921 ptrTrack->CookLabel(.9);
24253b0a 2922
d20df6fc 2923 // computes PID for track
2924 ptrTrack->CookPID();
2925 // update calibration references using this track
48f8adf3 2926 AliTRDCalibraFillHisto *calibra = AliTRDCalibraFillHisto::Instance();
2927 if (!calibra){
2928 AliInfo("Could not get Calibra instance\n");
2929 if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(ptrTrack);
2930 }
d20df6fc 2931 return ptrTrack;
e4f2f73d 2932}
2933
0906e73e 2934
e4f2f73d 2935//____________________________________________________________________
eb38ed55 2936Int_t AliTRDtrackerV1::ImproveSeedQuality(AliTRDtrackingChamber **stack, AliTRDseedV1 *cseed)
e4f2f73d 2937{
41702fec 2938 //
2939 // Sort tracklets according to "quality" and try to "improve" the first 4 worst
2940 //
2941 // Parameters :
2942 // layers : Array of propagation layers for a stack/supermodule
2943 // cseed : Array of 6 seeding tracklets which has to be improved
2944 //
f29f13a6 2945 // Output :
41702fec 2946 // cssed : Improved seeds
2947 //
2948 // Detailed description
2949 //
2950 // Iterative procedure in which new clusters are searched for each
2951 // tracklet seed such that the seed quality (see AliTRDseed::GetQuality())
2952 // can be maximized. If some optimization is found the old seeds are replaced.
2953 //
2954 // debug level: 7
2955 //
2956
2957 // make a local working copy
4d6aee34 2958 AliTRDtrackingChamber *chamber = NULL;
41702fec 2959 AliTRDseedV1 bseed[6];
2960 Int_t nLayers = 0;
2961 for (Int_t jLayer = 0; jLayer < 6; jLayer++) bseed[jLayer] = cseed[jLayer];
2962
2963 Float_t lastquality = 10000.0;
2964 Float_t lastchi2 = 10000.0;
2965 Float_t chi2 = 1000.0;
2966
2967 for (Int_t iter = 0; iter < 4; iter++) {
2968 Float_t sumquality = 0.0;
2969 Float_t squality[6];
2970 Int_t sortindexes[6];
2971
2972 for (Int_t jLayer = 0; jLayer < 6; jLayer++) {
3a039a31 2973 squality[jLayer] = bseed[jLayer].IsOK() ? bseed[jLayer].GetQuality(kTRUE) : 1000.;
41702fec 2974 sumquality += squality[jLayer];
2975 }
2976 if ((sumquality >= lastquality) || (chi2 > lastchi2)) break;
2977
2978 nLayers = 0;
2979 lastquality = sumquality;
2980 lastchi2 = chi2;
2981 if (iter > 0) for (Int_t jLayer = 0; jLayer < 6; jLayer++) cseed[jLayer] = bseed[jLayer];
2982
2983 TMath::Sort(6, squality, sortindexes, kFALSE);
2984 for (Int_t jLayer = 5; jLayer > 1; jLayer--) {
2985 Int_t bLayer = sortindexes[jLayer];
2986 if(!(chamber = stack[bLayer])) continue;
f29f13a6 2987 bseed[bLayer].AttachClusters(chamber, kTRUE);
2988 bseed[bLayer].Fit(kTRUE);
41702fec 2989 if(bseed[bLayer].IsOK()) nLayers++;
2990 }
2991
2992 chi2 = FitTiltedRieman(bseed, kTRUE);
4d6aee34 2993 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 7){
41702fec 2994 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2995 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2996 TLinearFitter *tiltedRieman = GetTiltedRiemanFitter();
4d6aee34 2997 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 2998 cstreamer << "ImproveSeedQuality"
2999 << "EventNumber=" << eventNumber
3000 << "CandidateNumber=" << candidateNumber
3001 << "Iteration=" << iter
3002 << "S0.=" << &bseed[0]
3003 << "S1.=" << &bseed[1]
3004 << "S2.=" << &bseed[2]
3005 << "S3.=" << &bseed[3]
3006 << "S4.=" << &bseed[4]
3007 << "S5.=" << &bseed[5]
3008 << "FitterT.=" << tiltedRieman
3009 << "\n";
3010 }
3011 } // Loop: iter
41702fec 3012 // we are sure that at least 2 tracklets are OK !
3013 return nLayers+2;
e4f2f73d 3014}
3015
eb38ed55 3016//_________________________________________________________________________
4d6aee34 3017Double_t AliTRDtrackerV1::CalculateTrackLikelihood(const AliTRDseedV1 *const tracklets, Double_t *chi2){
41702fec 3018 //
3019 // Calculates the Track Likelihood value. This parameter serves as main quality criterion for
3020 // the track selection
3021 // The likelihood value containes:
3022 // - The chi2 values from the both fitters and the chi2 values in z-direction from a linear fit
3023 // - The Sum of the Parameter |slope_ref - slope_fit|/Sigma of the tracklets
3024 // For all Parameters an exponential dependency is used
3025 //
3026 // Parameters: - Array of tracklets (AliTRDseedV1) related to the track candidate
3027 // - Array of chi2 values:
3028 // * Non-Constrained Tilted Riemann fit
3029 // * Vertex-Constrained Tilted Riemann fit
3030 // * z-Direction from Linear fit
3031 // Output: - The calculated track likelihood
3032 //
3033 // debug level 2
3034 //
3035
f29f13a6 3036 Double_t chi2phi = 0, nLayers = 0;
41702fec 3037 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
3038 if(!tracklets[iLayer].IsOK()) continue;
f29f13a6 3039 chi2phi += tracklets[iLayer].GetChi2Phi();
41702fec 3040 nLayers++;
3041 }
f29f13a6 3042 chi2phi /= Float_t (nLayers - 2.0);
41702fec 3043
3044 Double_t likeChi2Z = TMath::Exp(-chi2[2] * 0.14); // Chi2Z
4d6aee34 3045 Double_t likeChi2TC = (fkReconstructor->HasVertexConstrained()) ?
d20df6fc 3046 TMath::Exp(-chi2[1] * 0.677) : 1; // Constrained Tilted Riemann
6e39bde4 3047 Double_t likeChi2TR = TMath::Exp(-chi2[0] * 0.0078); // Non-constrained Tilted Riemann
3048 Double_t likeChi2Phi= TMath::Exp(-chi2phi * 3.23);//3.23
f29f13a6 3049 Double_t trackLikelihood = likeChi2Z * likeChi2TR * likeChi2Phi;
41702fec 3050
4d6aee34 3051 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 2){
41702fec 3052 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
3053 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
4d6aee34 3054 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 3055 cstreamer << "CalculateTrackLikelihood0"
3056 << "EventNumber=" << eventNumber
3057 << "CandidateNumber=" << candidateNumber
3058 << "LikeChi2Z=" << likeChi2Z
3059 << "LikeChi2TR=" << likeChi2TR
3060 << "LikeChi2TC=" << likeChi2TC
f29f13a6 3061 << "LikeChi2Phi=" << likeChi2Phi
41702fec 3062 << "TrackLikelihood=" << trackLikelihood
3063 << "\n";
3064 }
6e39bde4 3065
41702fec 3066 return trackLikelihood;
e4f2f73d 3067}
3068
3069//____________________________________________________________________
91834b8d 3070Double_t AliTRDtrackerV1::CookLikelihood(AliTRDseedV1 *cseed, Int_t planes[4])
e4f2f73d 3071{
41702fec 3072 //
3073 // Calculate the probability of this track candidate.
3074 //
3075 // Parameters :
3076 // cseeds : array of candidate tracklets
3077 // planes : array of seeding planes (see seeding configuration)
3078 // chi2 : chi2 values (on the Z and Y direction) from the rieman fit of the track.
3079 //
3080 // Output :
3081 // likelihood value
3082 //
3083 // Detailed description
3084 //
3085 // The track quality is estimated based on the following 4 criteria:
3086 // 1. precision of the rieman fit on the Y direction (likea)
3087 // 2. chi2 on the Y direction (likechi2y)
3088 // 3. chi2 on the Z direction (likechi2z)
3089 // 4. number of attached clusters compared to a reference value
3090 // (see AliTRDrecoParam::fkFindable) (likeN)
3091 //
3092 // The distributions for each type of probabilities are given below as of
3093 // (date). They have to be checked to assure consistency of estimation.
3094 //
3095
3096 // ratio of the total number of clusters/track which are expected to be found by the tracker.
4d6aee34 3097 const AliTRDrecoParam *fRecoPars = fkReconstructor->GetRecoParam();
41702fec 3098
91834b8d 3099 Double_t chi2y = GetChi2Y(&cseed[0]);
3100 Double_t chi2z = GetChi2Z(&cseed[0]);
3101
8ae98148 3102 Float_t nclusters = 0.;
41702fec 3103 Double_t sumda = 0.;
3104 for(UChar_t ilayer = 0; ilayer < 4; ilayer++){
3105 Int_t jlayer = planes[ilayer];
3106 nclusters += cseed[jlayer].GetN2();
e3cf3d02 3107 sumda += TMath::Abs(cseed[jlayer].GetYfit(1) - cseed[jlayer].GetYref(1));
41702fec 3108 }
8ae98148 3109 nclusters *= .25;
3110
5a2e200c 3111 Double_t likea = TMath::Exp(-sumda * fRecoPars->GetPhiSlope());
41702fec 3112 Double_t likechi2y = 0.0000000001;
4d6aee34 3113 if (fkReconstructor->IsCosmic() || chi2y < fRecoPars->GetChi2YCut()) likechi2y += TMath::Exp(-TMath::Sqrt(chi2y) * fRecoPars->GetChi2YSlope());
5a2e200c 3114 Double_t likechi2z = TMath::Exp(-chi2z * fRecoPars->GetChi2ZSlope());
8ae98148 3115 Double_t likeN = TMath::Exp(-(fRecoPars->GetNMeanClusters() - nclusters) / fRecoPars->GetNSigmaClusters());
41702fec 3116 Double_t like = likea * likechi2y * likechi2z * likeN;
3117
4d6aee34 3118 if(fkReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 2){
41702fec 3119 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
3120 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
4d6aee34 3121 Int_t nTracklets = 0; Float_t meanNcls = 0;
91834b8d 3122 for(Int_t iseed=0; iseed < kNPlanes; iseed++){
3123 if(!cseed[iseed].IsOK()) continue;
3124 nTracklets++;
4d6aee34 3125 meanNcls += cseed[iseed].GetN2();
91834b8d 3126 }
4d6aee34 3127 if(nTracklets) meanNcls /= nTracklets;
41702fec 3128 // The Debug Stream contains the seed
4d6aee34 3129 TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
41702fec 3130 cstreamer << "CookLikelihood"
3131 << "EventNumber=" << eventNumber
3132 << "CandidateNumber=" << candidateNumber
3133 << "tracklet0.=" << &cseed[0]
3134 << "tracklet1.=" << &cseed[1]
3135 << "tracklet2.=" << &cseed[2]
3136 << "tracklet3.=" << &cseed[3]
3137 << "tracklet4.=" << &cseed[4]
3138 << "tracklet5.=" << &cseed[5]
3139 << "sumda=" << sumda
91834b8d 3140 << "chi2y=" << chi2y
3141 << "chi2z=" << chi2z
41702fec 3142 << "likea=" << likea
3143 << "likechi2y=" << likechi2y
3144 << "likechi2z=" << likechi2z
3145 << "nclusters=" << nclusters
3146 << "likeN=" << likeN
3147 << "like=" << like
4d6aee34 3148 << "meanncls=" << meanNcls
41702fec 3149 << "\n";
3150 }
3151
3152 return like;
e4f2f73d 3153}
3154
e4f2f73d 3155//____________________________________________________________________
0906e73e 3156void AliTRDtrackerV1::GetSeedingConfig(Int_t iconfig, Int_t planes[4])
e4f2f73d 3157{
41702fec 3158 //
3159 // Map seeding configurations to detector planes.
3160 //
3161 // Parameters :
3162 // iconfig : configuration index
3163 // planes : member planes of this configuration. On input empty.
3164 //
3165 // Output :
3166 // planes : contains the planes which are defining the configuration
3167 //
3168 // Detailed description
3169 //
3170 // Here is the list of seeding planes configurations together with
3171 // their topological classification:
3172 //
3173 // 0 - 5432 TQ 0
3174 // 1 - 4321 TQ 0
3175 // 2 - 3210 TQ 0
3176 // 3 - 5321 TQ 1
3177 // 4 - 4210 TQ 1
3178 // 5 - 5431 TQ 1
3179 // 6 - 4320 TQ 1
3180 // 7 - 5430 TQ 2
3181 // 8 - 5210 TQ 2
3182 // 9 - 5421 TQ 3
3183 // 10 - 4310 TQ 3
3184 // 11 - 5410 TQ 4
3185 // 12 - 5420 TQ 5
3186 // 13 - 5320 TQ 5
3187 // 14 - 5310 TQ 5
3188 //
3189 // The topologic quality is modeled as follows:
3190 // 1. The general model is define by the equation:
3191 // p(conf) = exp(-conf/2)
3192 // 2. According to the topologic classification, configurations from the same
3193 // class are assigned the agerage value over the model values.
3194 // 3. Quality values are normalized.
3195 //
3196 // The topologic quality distribution as function of configuration is given below:
3197 //Begin_Html
3198 // <img src="gif/topologicQA.gif">
3199 //End_Html
3200 //
3201
3202 switch(iconfig){
3203 case 0: // 5432 TQ 0
3204 planes[0] = 2;
3205 planes[1] = 3;
3206 planes[2] = 4;
3207 planes[3] = 5;
3208 break;
3209 case 1: // 4321 TQ 0
3210 planes[0] = 1;
3211 planes[1] = 2;
3212 planes[2] = 3;
3213 planes[3] = 4;
3214 break;
3215 case 2: // 3210 TQ 0
3216 planes[0] = 0;
3217 planes[1] = 1;
3218 planes[2] = 2;
3219 planes[3] = 3;
3220 break;
3221 case 3: // 5321 TQ 1
3222 planes[0] = 1;
3223 planes[1] = 2;
3224 planes[2] = 3;
3225 planes[3] = 5;
3226 break;
3227 case 4: // 4210 TQ 1
3228 planes[0] = 0;
3229 planes[1] = 1;
3230 planes[2] = 2;
3231 planes[3] = 4;
3232 break;
3233 case 5: // 5431 TQ 1
3234 planes[0] = 1;
3235 planes[1] = 3;
3236 planes[2] = 4;
3237 planes[3] = 5;
3238 break;
3239 case 6: // 4320 TQ 1
3240 planes[0] = 0;
3241 planes[1] = 2;
3242 planes[2] = 3;
3243 planes[3] = 4;
3244 break;
3245 case 7: // 5430 TQ 2
3246 planes[0] = 0;
3247 planes[1] = 3;
3248 planes[2] = 4;
3249 planes[3] = 5;
3250 break;
3251 case 8: // 5210 TQ 2
3252 planes[0] = 0;
3253 planes[1] = 1;
3254 planes[2] = 2;
3255 planes[3] = 5;
3256 break;
3257 case 9: // 5421 TQ 3
3258 planes[0] = 1;
3259 planes[1] = 2;
3260 planes[2] = 4;
3261 planes[3] = 5;
3262 break;
3263 case 10: // 4310 TQ 3
3264 planes[0] = 0;
3265 planes[1] = 1;
3266 planes[2] = 3;
3267 planes[3] = 4;
3268 break;
3269 case 11: // 5410 TQ 4
3270 planes[0] = 0;
3271 planes[1] = 1;
3272 planes[2] = 4;
3273 planes[3] = 5;
3274 break;
3275 case 12: // 5420 TQ 5
3276 planes[0] = 0;
3277 planes[1] = 2;
3278 planes[2] = 4;
3279 planes[3] = 5;
3280 break;
3281 case 13: // 5320 TQ 5
3282 planes[0] = 0;
3283 planes[1] = 2;
3284 planes[2] = 3;
3285 planes[3] = 5;
3286 break;
3287 case 14: // 5310 TQ 5
3288 planes[0] = 0;
3289 planes[1] = 1;
3290 planes[2] = 3;
3291 planes[3] = 5;
3292 break;
3293 }
e4f2f73d 3294}
3295
3296//____________________________________________________________________
0906e73e 3297void AliTRDtrackerV1::GetExtrapolationConfig(Int_t iconfig, Int_t planes[2])
e4f2f73d 3298{
41702fec 3299 //
3300 // Returns the extrapolation planes for a seeding configuration.
3301 //
3302 // Parameters :
3303 // iconfig : configuration index
3304 // planes : planes which are not in this configuration. On input empty.
3305 //
3306 // Output :
3307 // planes : contains the planes which are not in the configuration
3308 //
3309 // Detailed description
3310 //
3311
3312 switch(iconfig){
3313 case 0: // 5432 TQ 0
3314 planes[0] = 1;
3315 planes[1] = 0;
3316 break;
3317 case 1: // 4321 TQ 0
3318 planes[0] = 5;
3319 planes[1] = 0;
3320 break;
3321 case 2: // 3210 TQ 0
3322 planes[0] = 4;
3323 planes[1] = 5;
3324 break;
3325 case 3: // 5321 TQ 1
3326 planes[0] = 4;
3327 planes[1] = 0;
3328 break;
3329 case 4: // 4210 TQ 1
3330 planes[0] = 5;
3331 planes[1] = 3;
3332 break;
3333 case 5: // 5431 TQ 1
3334 planes[0] = 2;
3335 planes[1] = 0;
3336 break;
3337 case 6: // 4320 TQ 1
3338 planes[0] = 5;
3339 planes[1] = 1;
3340 break;
3341 case 7: // 5430 TQ 2
3342 planes[0] = 2;
3343 planes[1] = 1;
3344 break;
3345 case 8: // 5210 TQ 2
3346 planes[0] = 4;
3347 planes[1] = 3;
3348 break;
3349 case 9: // 5421 TQ 3
3350 planes[0] = 3;
3351 planes[1] = 0;
3352 break;
3353 case 10: // 4310 TQ 3
3354 planes[0] = 5;
3355 planes[1] = 2;
3356 break;
3357 case 11: // 5410 TQ 4
3358 planes[0] = 3;
3359 planes[1] = 2;
3360 break;
3361 case 12: // 5420 TQ 5
3362 planes[0] = 3;
3363 planes[1] = 1;
3364 break;
3365 case 13: // 5320 TQ 5
3366 planes[0] = 4;
3367 planes[1] = 1;
3368 break;
3369 case 14: // 5310 TQ 5
3370 planes[0] = 4;
3371 planes[1] = 2;
3372 break;
3373 }
e4f2f73d 3374}
eb38ed55 3375
3376//____________________________________________________________________
3377AliCluster* AliTRDtrackerV1::GetCluster(Int_t idx) const
3378{
41702fec 3379 Int_t ncls = fClusters->GetEntriesFast();
4d6aee34 3380 return idx >= 0 && idx < ncls ? (AliCluster*)fClusters->UncheckedAt(idx) : NULL;
eb38ed55 3381}
3382
3b57a3f7 3383//____________________________________________________________________
3384AliTRDseedV1* AliTRDtrackerV1::GetTracklet(Int_t idx) const
3385{
41702fec 3386 Int_t ntrklt = fTracklets->GetEntriesFast();
4d6aee34 3387 return idx >= 0 && idx < ntrklt ? (AliTRDseedV1*)fTracklets->UncheckedAt(idx) : NULL;
3b57a3f7 3388}
3389
3390//____________________________________________________________________
3391AliKalmanTrack* AliTRDtrackerV1::GetTrack(Int_t idx) const
3392{
41702fec 3393 Int_t ntrk = fTracks->GetEntriesFast();
4d6aee34 3394 return idx >= 0 && idx < ntrk ? (AliKalmanTrack*)fTracks->UncheckedAt(idx) : NULL;
3b57a3f7 3395}
3396
6e39bde4 3397
eb38ed55 3398
fac58f00 3399// //_____________________________________________________________________________
3400// Int_t AliTRDtrackerV1::Freq(Int_t n, const Int_t *inlist
3401// , Int_t *outlist, Bool_t down)
3402// {
3403// //
3404// // Sort eleements according occurancy
3405// // The size of output array has is 2*n
3406// //
3407//
3408// if (n <= 0) {
3409// return 0;
3410// }
3411//
3412// Int_t *sindexS = new Int_t[n]; // Temporary array for sorting
3413// Int_t *sindexF = new Int_t[2*n];
3414// for (Int_t i = 0; i < n; i++) {
3415// sindexF[i] = 0;
3416// }
3417//
3418// TMath::Sort(n,inlist,sindexS,down);
3419//
3420// Int_t last = inlist[sindexS[0]];
3421// Int_t val = last;
3422// sindexF[0] = 1;
3423// sindexF[0+n] = last;
3424// Int_t countPos = 0;
3425//
3426// // Find frequency
3427// for (Int_t i = 1; i < n; i++) {
3428// val = inlist[sindexS[i]];
3429// if (last == val) {
3430// sindexF[countPos]++;
3431// }
3432// else {
3433// countPos++;
3434// sindexF[countPos+n] = val;
3435// sindexF[countPos]++;
3436// last = val;
3437// }
3438// }
3439// if (last == val) {
3440// countPos++;
3441// }
3442//
3443// // Sort according frequency
3444// TMath::Sort(countPos,sindexF,sindexS,kTRUE);
3445//
3446// for (Int_t i = 0; i < countPos; i++) {
3447// outlist[2*i ] = sindexF[sindexS[i]+n];
3448// outlist[2*i+1] = sindexF[sindexS[i]];
3449// }
3450//
3451// delete [] sindexS;
3452// delete [] sindexF;
3453//
3454// return countPos;
3455//
3456// }
bb56afff 3457
06b32d95 3458
d611c74f 3459//____________________________________________________________________
3460void AliTRDtrackerV1::ResetSeedTB()
3461{
3462// reset buffer for seeding time bin layers. If the time bin
3463// layers are not allocated this function allocates them
3464
3465 for(Int_t isl=0; isl<kNSeedPlanes; isl++){
3466 if(!fSeedTB[isl]) fSeedTB[isl] = new AliTRDchamberTimeBin();
3467 else fSeedTB[isl]->Clear();
3468 }
3469}
3470
e3cf3d02 3471
3472//_____________________________________________________________________________
4d6aee34 3473Float_t AliTRDtrackerV1::GetChi2Y(const AliTRDseedV1 * const tracklets) const
e3cf3d02 3474{
3475 // Calculates normalized chi2 in y-direction
3476 // chi2 = Sum chi2 / n_tracklets
3477
3478 Double_t chi2 = 0.; Int_t n = 0;
3479 for(Int_t ipl = kNPlanes; ipl--;){
3480 if(!tracklets[ipl].IsOK()) continue;
3481 chi2 += tracklets[ipl].GetChi2Y();
3482 n++;
3483 }
3484 return n ? chi2/n : 0.;
3485}
3486
bb56afff 3487//_____________________________________________________________________________
4d6aee34 3488Float_t AliTRDtrackerV1::GetChi2Z(const AliTRDseedV1 *const tracklets) const
bb56afff 3489{
91834b8d 3490 // Calculates normalized chi2 in z-direction
e3cf3d02 3491 // chi2 = Sum chi2 / n_tracklets
41702fec 3492
e3cf3d02 3493 Double_t chi2 = 0; Int_t n = 0;
3494 for(Int_t ipl = kNPlanes; ipl--;){
41702fec 3495 if(!tracklets[ipl].IsOK()) continue;
e3cf3d02 3496 chi2 += tracklets[ipl].GetChi2Z();
3497 n++;
41702fec 3498 }
e3cf3d02 3499 return n ? chi2/n : 0.;
bb56afff 3500}
8acca6a3 3501
6e39bde4 3502//____________________________________________________________________
4d6aee34 3503Float_t AliTRDtrackerV1::CalculateReferenceX(const AliTRDseedV1 *const tracklets){
6e39bde4 3504 //
3505 // Calculates the reference x-position for the tilted Rieman fit defined as middle
3506 // of the stack (middle between layers 2 and 3). For the calculation all the tracklets
3507 // are taken into account
3508 //
3509 // Parameters: - Array of tracklets(AliTRDseedV1)
3510 //
3511 // Output: - The reference x-position(Float_t)
3512 // Only kept for compatibility with the old code
3513 //
3514 Int_t nDistances = 0;
3515 Float_t meanDistance = 0.;
3516 Int_t startIndex = 5;
3517 for(Int_t il =5; il > 0; il--){
3518 if(tracklets[il].IsOK() && tracklets[il -1].IsOK()){
3519 Float_t xdiff = tracklets[il].GetX0() - tracklets[il -1].GetX0();
3520 meanDistance += xdiff;
3521 nDistances++;
3522 }
3523 if(tracklets[il].IsOK()) startIndex = il;
3524 }
3525 if(tracklets[0].IsOK()) startIndex = 0;
3526 if(!nDistances){
3527 // We should normally never get here
3528 Float_t xpos[2]; memset(xpos, 0, sizeof(Float_t) * 2);
3529 Int_t iok = 0, idiff = 0;
3530 // This attempt is worse and should be avoided:
3531 // check for two chambers which are OK and repeat this without taking the mean value
3532 // Strategy avoids a division by 0;
3533 for(Int_t il = 5; il >= 0; il--){
3534 if(tracklets[il].IsOK()){
3535 xpos[iok] = tracklets[il].GetX0();
3536 iok++;
3537 startIndex = il;
3538 }
3539 if(iok) idiff++; // to get the right difference;
3540 if(iok > 1) break;
3541 }
3542 if(iok > 1){
3543 meanDistance = (xpos[0] - xpos[1])/idiff;
3544 }
3545 else{
3546 // we have do not even have 2 layers which are OK? The we do not need to fit at all
3547 return 331.;
3548 }
3549 }
3550 else{
3551 meanDistance /= nDistances;
3552 }
3553 return tracklets[startIndex].GetX0() + (2.5 - startIndex) * meanDistance - 0.5 * (AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
3554}
3555
3556//_____________________________________________________________________________
4d6aee34 3557Double_t AliTRDtrackerV1::FitTiltedRiemanV1(AliTRDseedV1 *const tracklets){
6e39bde4 3558 //
3559 // Track Fitter Function using the new class implementation of
3560 // the Rieman fit
3561 //
3562 AliTRDtrackFitterRieman fitter;
3563 fitter.SetRiemanFitter(GetTiltedRiemanFitter());
3564 fitter.Reset();
3565 for(Int_t il = 0; il < AliTRDgeometry::kNlayer; il++) fitter.SetTracklet(il, &tracklets[il]);
3566 Double_t chi2 = fitter.Eval();
3567 // Update the tracklets
3568 Double_t cov[15]; Double_t x0;
3569 memset(cov, 0, sizeof(Double_t) * 15);
3570 for(Int_t il = 0; il < AliTRDgeometry::kNlayer; il++){
3571 x0 = tracklets[il].GetX0();
3572 tracklets[il].SetYref(0, fitter.GetYat(x0));
3573 tracklets[il].SetZref(0, fitter.GetZat(x0));
3574 tracklets[il].SetYref(1, fitter.GetDyDxAt(x0));
3575 tracklets[il].SetZref(1, fitter.GetDzDx());
3576 tracklets[il].SetC(fitter.GetCurvature());
3577 fitter.GetCovAt(x0, cov);
3578 tracklets[il].SetCovRef(cov);
3579 tracklets[il].SetChi2(chi2);
3580 }
3581 return chi2;
3582}
3583
8acca6a3 3584///////////////////////////////////////////////////////
3585// //
3586// Resources of class AliTRDLeastSquare //
3587// //
3588///////////////////////////////////////////////////////
3589
3590//_____________________________________________________________________________
3591AliTRDtrackerV1::AliTRDLeastSquare::AliTRDLeastSquare(){
41702fec 3592 //
3593 // Constructor of the nested class AliTRDtrackFitterLeastSquare
3594 //
3595 memset(fParams, 0, sizeof(Double_t) * 2);
3596 memset(fSums, 0, sizeof(Double_t) * 5);
3597 memset(fCovarianceMatrix, 0, sizeof(Double_t) * 3);
8acca6a3 3598
3599}
3600
3601//_____________________________________________________________________________
4d6aee34 3602void AliTRDtrackerV1::AliTRDLeastSquare::AddPoint(const Double_t *const x, Double_t y, Double_t sigmaY){
41702fec 3603 //
3604 // Adding Point to the fitter
3605 //
2ed81ab2 3606 Double_t weight = 1/(sigmaY > 1e-9 ? sigmaY : 1e-9);
3607 weight *= weight;
4d6aee34 3608 const Double_t &xpt = *x;
41702fec 3609 // printf("Adding point x = %f, y = %f, sigma = %f\n", xpt, y, sigmaY);
3610 fSums[0] += weight;
3611 fSums[1] += weight * xpt;
3612 fSums[2] += weight * y;
3613 fSums[3] += weight * xpt * y;
3614 fSums[4] += weight * xpt * xpt;
3615 fSums[5] += weight * y * y;
8acca6a3 3616}
3617
3618//_____________________________________________________________________________
4d6aee34 3619void AliTRDtrackerV1::AliTRDLeastSquare::RemovePoint(const Double_t *const x, Double_t y, Double_t sigmaY){
41702fec 3620 //
3621 // Remove Point from the sample
3622 //
2ed81ab2 3623
3624 Double_t weight = 1/(sigmaY > 1e-9 ? sigmaY : 1e-9);
3625 weight *= weight;
4d6aee34 3626 const Double_t &xpt = *x;
41702fec 3627 fSums[0] -= weight;
3628 fSums[1] -= weight * xpt;
3629 fSums[2] -= weight * y;
3630 fSums[3] -= weight * xpt * y;
3631 fSums[4] -= weight * xpt * xpt;
3632 fSums[5] -= weight * y * y;
8acca6a3 3633}
3634
3635//_____________________________________________________________________________
3636void AliTRDtrackerV1::AliTRDLeastSquare::Eval(){
41702fec 3637 //
3638 // Evaluation of the fit:
3639 // Calculation of the parameters
3640 // Calculation of the covariance matrix
3641 //
3642
3643 Double_t denominator = fSums[0] * fSums[4] - fSums[1] *fSums[1];
aec26713 3644 if(denominator==0) return;
3645
41702fec 3646 // for(Int_t isum = 0; isum < 5; isum++)
3647 // printf("fSums[%d] = %f\n", isum, fSums[isum]);
3648 // printf("denominator = %f\n", denominator);
3649 fParams[0] = (fSums[2] * fSums[4] - fSums[1] * fSums[3])/ denominator;
3650 fParams[1] = (fSums[0] * fSums[3] - fSums[1] * fSums[2]) / denominator;
3651 // printf("fParams[0] = %f, fParams[1] = %f\n", fParams[0], fParams[1]);
3652
3653 // Covariance matrix
2ed81ab2 3654 fCovarianceMatrix[0] = fSums[4] / fSums[0] - fSums[1] * fSums[1] / (fSums[0] * fSums[0]);
3655 fCovarianceMatrix[1] = fSums[5] / fSums[0] - fSums[2] * fSums[2] / (fSums[0] * fSums[0]);
3656 fCovarianceMatrix[2] = fSums[3] / fSums[0] - fSums[1] * fSums[2] / (fSums[0] * fSums[0]);
8acca6a3 3657}
3658
46b6abd7 3659//_____________________________________________________________________________
4d6aee34 3660Double_t AliTRDtrackerV1::AliTRDLeastSquare::GetFunctionValue(const Double_t *const xpos) const {
41702fec 3661 //
3662 // Returns the Function value of the fitted function at a given x-position
3663 //
3664 return fParams[0] + fParams[1] * (*xpos);
46b6abd7 3665}
3666
3667//_____________________________________________________________________________
3668void AliTRDtrackerV1::AliTRDLeastSquare::GetCovarianceMatrix(Double_t *storage) const {
41702fec 3669 //
3670 // Copies the values of the covariance matrix into the storage
3671 //
3672 memcpy(storage, fCovarianceMatrix, sizeof(Double_t) * 3);
46b6abd7 3673}
3674
6e39bde4 3675//_____________________________________________________________________________
3676void AliTRDtrackerV1::AliTRDLeastSquare::Reset(){
3677 //
3678 // Reset the fitter
3679 //
3680 memset(fParams, 0, sizeof(Double_t) * 2);
3681 memset(fCovarianceMatrix, 0, sizeof(Double_t) * 3);
3682 memset(fSums, 0, sizeof(Double_t) * 6);
3683}
3684
3685///////////////////////////////////////////////////////
3686// //
3687// Resources of class AliTRDtrackFitterRieman //
3688// //
3689///////////////////////////////////////////////////////
3690
3691//_____________________________________________________________________________
3692AliTRDtrackerV1::AliTRDtrackFitterRieman::AliTRDtrackFitterRieman():
3693 fTrackFitter(NULL),
3694 fZfitter(NULL),
3695 fCovarPolY(NULL),
3696 fCovarPolZ(NULL),
3697 fXref(0.),
3698 fSysClusterError(0.)
3699{
3700 //
3701 // Default constructor
3702 //
3703 fZfitter = new AliTRDLeastSquare;
3704 fCovarPolY = new TMatrixD(3,3);
3705 fCovarPolZ = new TMatrixD(2,2);
3706 memset(fTracklets, 0, sizeof(AliTRDseedV1 *) * 6);
3707 memset(fParameters, 0, sizeof(Double_t) * 5);
3708 memset(fSumPolY, 0, sizeof(Double_t) * 5);
3709 memset(fSumPolZ, 0, sizeof(Double_t) * 2);
3710}
3711
3712//_____________________________________________________________________________
3713AliTRDtrackerV1::AliTRDtrackFitterRieman::~AliTRDtrackFitterRieman(){
3714 //
3715 // Destructor
3716 //
3717 if(fZfitter) delete fZfitter;
3718 if(fCovarPolY) delete fCovarPolY;
3719 if(fCovarPolZ) delete fCovarPolZ;
3720}
3721
3722//_____________________________________________________________________________
3723void AliTRDtrackerV1::AliTRDtrackFitterRieman::Reset(){
3724 //
3725 // Reset the Fitter
3726 //
3727 if(fTrackFitter){
3728 fTrackFitter->StoreData(kTRUE);
3729 fTrackFitter->ClearPoints();
3730 }
3731 if(fZfitter){
3732 fZfitter->Reset();
3733 }
3734 fXref = 0.;
3735 memset(fTracklets, 0, sizeof(AliTRDseedV1 *) * AliTRDgeometry::kNlayer);
3736 memset(fParameters, 0, sizeof(Double_t) * 5);
3737 memset(fSumPolY, 0, sizeof(Double_t) * 5);
3738 memset(fSumPolZ, 0, sizeof(Double_t) * 2);
3739 for(Int_t irow = 0; irow < fCovarPolY->GetNrows(); irow++)
3740 for(Int_t icol = 0; icol < fCovarPolY->GetNcols(); icol++){
3741 (*fCovarPolY)(irow, icol) = 0.;
3742 if(irow < 2 && icol < 2)
3743 (*fCovarPolZ)(irow, icol) = 0.;
3744 }
3745}
3746
3747//_____________________________________________________________________________
3748void AliTRDtrackerV1::AliTRDtrackFitterRieman::SetTracklet(Int_t itr, AliTRDseedV1 *tracklet){
3749 //
3750 // Add tracklet into the fitter
3751 //
3752 if(itr >= AliTRDgeometry::kNlayer) return;
3753 fTracklets[itr] = tracklet;
3754}
3755
3756//_____________________________________________________________________________
3757Double_t AliTRDtrackerV1::AliTRDtrackFitterRieman::Eval(){
3758 //
3759 // Perform the fit
3760 // 1. Apply linear transformation and store points in the fitter
3761 // 2. Evaluate the fit
3762 // 3. Check if the result of the fit in z-direction is reasonable
3763 // if not
3764 // 3a. Fix the parameters 3 and 4 with the results of a simple least
3765 // square fit
3766 // 3b. Redo the fit with the fixed parameters
3767 // 4. Store fit results (parameters and errors)
3768 //
3769 if(!fTrackFitter){
3770 return 1e10;
3771 }
3772 fXref = CalculateReferenceX();
3773 for(Int_t il = 0; il < AliTRDgeometry::kNlayer; il++) UpdateFitters(fTracklets[il]);
3774 if(!fTrackFitter->GetNpoints()) return 1e10;
3775 // perform the fit
3776 fTrackFitter->Eval();
3777 fZfitter->Eval();
3778 fParameters[3] = fTrackFitter->GetParameter(3);
3779 fParameters[4] = fTrackFitter->GetParameter(4);
3780 if(!CheckAcceptable(fParameters[3], fParameters[4])) {
3781 fTrackFitter->FixParameter(3, fZfitter->GetFunctionValue(&fXref));
3782 fTrackFitter->FixParameter(4, fZfitter->GetFunctionParameter(1));
3783 fTrackFitter->Eval();
3784 fTrackFitter->ReleaseParameter(3);
3785 fTrackFitter->ReleaseParameter(4);
3786 fParameters[3] = fTrackFitter->GetParameter(3);
3787 fParameters[4] = fTrackFitter->GetParameter(4);
3788 }
3789 // Update the Fit Parameters and the errors
3790 fParameters[0] = fTrackFitter->GetParameter(0);
3791 fParameters[1] = fTrackFitter->GetParameter(1);
3792 fParameters[2] = fTrackFitter->GetParameter(2);
3793
3794 // Prepare Covariance estimation
3795 (*fCovarPolY)(0,0) = fSumPolY[0]; (*fCovarPolY)(1,1) = fSumPolY[2]; (*fCovarPolY)(2,2) = fSumPolY[4];
3796 (*fCovarPolY)(1,0) = (*fCovarPolY)(0,1) = fSumPolY[1];
3797 (*fCovarPolY)(2,0) = (*fCovarPolY)(0,2) = fSumPolY[2];
3798 (*fCovarPolY)(2,1) = (*fCovarPolY)(1,2) = fSumPolY[3];
3799 fCovarPolY->Invert();
3800 (*fCovarPolZ)(0,0) = fSumPolZ[0]; (*fCovarPolZ)(1,1) = fSumPolZ[2];
3801 (*fCovarPolZ)(1,0) = (*fCovarPolZ)(0,1) = fSumPolZ[1];
3802 fCovarPolZ->Invert();
3803 return fTrackFitter->GetChisquare() / fTrackFitter->GetNpoints();
3804}
3805
3806//_____________________________________________________________________________
4d6aee34 3807void AliTRDtrackerV1::AliTRDtrackFitterRieman::UpdateFitters(AliTRDseedV1 * const tracklet){
6e39bde4 3808 //
3809 // Does the transformations and updates the fitters
3810 // The following transformation is applied
3811 //
3812 AliTRDcluster *cl = NULL;
3813 Double_t x, y, z, dx, t, w, we, yerr, zerr;
3814 Double_t uvt[4];
3815 if(!tracklet || !tracklet->IsOK()) return;
3816 Double_t tilt = tracklet->GetTilt();
3817 for(Int_t itb = 0; itb < AliTRDseedV1::kNclusters; itb++){
3818 if(!(cl = tracklet->GetClusters(itb))) continue;
3819 if(!cl->IsInChamber()) continue;
3820 if (!tracklet->IsUsable(itb)) continue;
3821 x = cl->GetX();
3822 y = cl->GetY();
3823 z = cl->GetZ();
3824 dx = x - fXref;
3825 // Transformation
3826 t = 1./(x*x + y*y);
3827 uvt[0] = 2. * x * t;
3828 uvt[1] = t;
3829 uvt[2] = 2. * tilt * t;
3830 uvt[3] = 2. * tilt * dx * t;
3831 w = 2. * (y + tilt*z) * t;
3832 // error definition changes for the different calls
3833 we = 2. * t;
3834 we *= TMath::Sqrt(cl->GetSigmaY2()+tilt*tilt*cl->GetSigmaZ2());
3835 // Update sums for error calculation
3836 yerr = 1./(TMath::Sqrt(cl->GetSigmaY2()) + fSysClusterError);
3837 yerr *= yerr;
3838 zerr = 1./cl->GetSigmaZ2();
3839 for(Int_t ipol = 0; ipol < 5; ipol++){
3840 fSumPolY[ipol] += yerr;
3841 yerr *= x;
3842 if(ipol < 3){
3843 fSumPolZ[ipol] += zerr;
3844 zerr *= x;
3845 }
3846 }
3847 fTrackFitter->AddPoint(uvt, w, we);
3848 fZfitter->AddPoint(&x, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
3849 }
3850}
3851
3852//_____________________________________________________________________________
3853Bool_t AliTRDtrackerV1::AliTRDtrackFitterRieman::CheckAcceptable(Double_t offset, Double_t slope){
3854 //
3855 // Check whether z-results are acceptable
3856 // Definition: Distance between tracklet fit and track fit has to be
3857 // less then half a padlength
3858 // Point of comparision is at the anode wire
3859 //
3860 Bool_t acceptablez = kTRUE;
3861 Double_t zref = 0.0;
3862 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
3863 if(!fTracklets[iLayer]->IsOK()) continue;
3864 zref = offset + slope * (fTracklets[iLayer]->GetX0() - fXref);
3865 if (TMath::Abs(fTracklets[iLayer]->GetZfit(0) - zref) > fTracklets[iLayer]->GetPadLength() * 0.5 + 1.0)
3866 acceptablez = kFALSE;
3867 }
3868 return acceptablez;
3869}
3870
3871//_____________________________________________________________________________
3872Double_t AliTRDtrackerV1::AliTRDtrackFitterRieman::GetYat(Double_t x) const {
3873 //
3874 // Calculate y position out of the track parameters
3875 // y: R^2 = (x - x0)^2 + (y - y0)^2
3876 // => y = y0 +/- Sqrt(R^2 - (x - x0)^2)
3877 // R = Sqrt() = 1/Curvature
3878 // => y = y0 +/- Sqrt(1/Curvature^2 - (x - x0)^2)
3879 //
3880 Double_t y = 0;
3881 Double_t disc = (x * fParameters[0] + fParameters[1]);
3882 disc = 1 - fParameters[0]*fParameters[2] + fParameters[1]*fParameters[1] - disc*disc;
3883 if (disc >= 0) {
3884 disc = TMath::Sqrt(disc);
3885 y = (1.0 - disc) / fParameters[0];
3886 }
3887 return y;
3888}
3889
3890//_____________________________________________________________________________
3891Double_t AliTRDtrackerV1::AliTRDtrackFitterRieman::GetZat(Double_t x) const {
3892 //
3893 // Return z position for a given x position
3894 // Simple linear function
3895 //
3896 return fParameters[3] + fParameters[4] * (x - fXref);
3897}
3898
3899//_____________________________________________________________________________
3900Double_t AliTRDtrackerV1::AliTRDtrackFitterRieman::GetDyDxAt(Double_t x) const {
3901 //
3902 // Calculate dydx at a given radial position out of the track parameters
3903 // dy: R^2 = (x - x0)^2 + (y - y0)^2
3904 // => y = +/- Sqrt(R^2 - (x - x0)^2) + y0
3905 // => dy/dx = (x - x0)/Sqrt(R^2 - (x - x0)^2)
3906 // Curvature: cr = 1/R = a/Sqrt(1 + b^2 - c*a)
3907 // => dy/dx = (x - x0)/(1/(cr^2) - (x - x0)^2)
3908 //
3909 Double_t x0 = -fParameters[1] / fParameters[0];
3910 Double_t curvature = GetCurvature();
3911 Double_t dy = 0;
3912 if (-fParameters[2] * fParameters[0] + fParameters[1] * fParameters[1] + 1 > 0) {
3913 if (1.0/(curvature * curvature) - (x - x0) * (x - x0) > 0.0) {
3914 Double_t yderiv = (x - x0) / TMath::Sqrt(1.0/(curvature * curvature) - (x - x0) * (x - x0));
3915 if (fParameters[0] < 0) yderiv *= -1.0;
3916 dy = yderiv;
3917 }
3918 }
3919 return dy;
3920}
3921
3922//_____________________________________________________________________________
3923Double_t AliTRDtrackerV1::AliTRDtrackFitterRieman::GetCurvature() const {
3924 //
3925 // Calculate track curvature
3926 //
3927 //
3928 Double_t curvature = 1.0 + fParameters[1]*fParameters[1] - fParameters[2]*fParameters[0];
3929 if (curvature > 0.0)
3930 curvature = fParameters[0] / TMath::Sqrt(curvature);
3931 return curvature;
3932}
3933
3934//_____________________________________________________________________________
3935void AliTRDtrackerV1::AliTRDtrackFitterRieman::GetCovAt(Double_t x, Double_t *cov) const {
3936 //
3937 // Error Definition according to gauss error propagation
3938 //
3939 TMatrixD transform(3,3);
3940 transform(0,0) = transform(1,1) = transform(2,2) = 1;
3941 transform(0,1) = transform(1,2) = x;
3942 transform(0,2) = x*x;
3943 TMatrixD covariance(transform, TMatrixD::kMult, *fCovarPolY);
3944 covariance *= transform.T();
3945 cov[0] = covariance(0,0);
3946 TMatrixD transformZ(2,2);
3947 transformZ(0,0) = transformZ(1,1) = 1;
3948 transformZ(0,1) = x;
3949 TMatrixD covarZ(transformZ, TMatrixD::kMult, *fCovarPolZ);
3950 covarZ *= transformZ.T();
3951 cov[1] = covarZ(0,0);
3952 cov[2] = 0;
3953}
3954
3955//____________________________________________________________________
3956Double_t AliTRDtrackerV1::AliTRDtrackFitterRieman::CalculateReferenceX(){
3957 //
3958 // Calculates the reference x-position for the tilted Rieman fit defined as middle
3959 // of the stack (middle between layers 2 and 3). For the calculation all the tracklets
3960 // are taken into account
3961 //
3962 // Parameters: - Array of tracklets(AliTRDseedV1)
3963 //
3964 // Output: - The reference x-position(Float_t)
3965 //
3966 Int_t nDistances = 0;
3967 Float_t meanDistance = 0.;
3968 Int_t startIndex = 5;
3969 for(Int_t il =5; il > 0; il--){
3970 if(fTracklets[il]->IsOK() && fTracklets[il -1]->IsOK()){
3971 Float_t xdiff = fTracklets[il]->GetX0() - fTracklets[il -1]->GetX0();
3972 meanDistance += xdiff;
3973 nDistances++;
3974 }
3975 if(fTracklets[il]->IsOK()) startIndex = il;
3976 }
3977 if(fTracklets[0]->IsOK()) startIndex = 0;
3978 if(!nDistances){
3979 // We should normally never get here
3980 Float_t xpos[2]; memset(xpos, 0, sizeof(Float_t) * 2);
3981 Int_t iok = 0, idiff = 0;
3982 // This attempt is worse and should be avoided:
3983 // check for two chambers which are OK and repeat this without taking the mean value
3984 // Strategy avoids a division by 0;
3985 for(Int_t il = 5; il >= 0; il--){
3986 if(fTracklets[il]->IsOK()){
3987 xpos[iok] = fTracklets[il]->GetX0();
3988 iok++;
3989 startIndex = il;
3990 }
3991 if(iok) idiff++; // to get the right difference;
3992 if(iok > 1) break;
3993 }
3994 if(iok > 1){
3995 meanDistance = (xpos[0] - xpos[1])/idiff;
3996 }
3997 else{
3998 // we have do not even have 2 layers which are OK? The we do not need to fit at all
3999 return 331.;
4000 }
4001 }
4002 else{
4003 meanDistance /= nDistances;
4004 }
4005 return fTracklets[startIndex]->GetX0() + (2.5 - startIndex) * meanDistance - 0.5 * (AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
4006}