First version of the PMD online DA. Pedestal calibration is contained in AliPMDCalibP...
[u/mrichter/AliRoot.git] / STEER / AliVertexerTracks.cxx
CommitLineData
2d57349e 1/**************************************************************************
2 * Copyright(c) 1998-2003, 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 **************************************************************************/
15
16//-----------------------------------------------------------------
17// Implementation of the vertexer from ESD tracks
18//
19// Origin: AliITSVertexerTracks
20// A.Dainese, Padova,
21// andrea.dainese@pd.infn.it
22// M.Masera, Torino,
23// massimo.masera@to.infn.it
24// Moved to STEER and adapted to ESD tracks:
25// F.Prino, Torino, prino@to.infn.it
26//-----------------------------------------------------------------
27
28//---- Root headers --------
29#include <TTree.h>
ec1be5d5 30#include <TMatrixD.h>
2d57349e 31//---- AliRoot headers -----
32#include "AliStrLine.h"
33#include "AliVertexerTracks.h"
c5e3e5d1 34#include "AliESD.h"
2d57349e 35#include "AliESDtrack.h"
36
37ClassImp(AliVertexerTracks)
38
39
40//----------------------------------------------------------------------------
41AliVertexerTracks::AliVertexerTracks():
07680cae 42TObject(),
43fVert(),
44fCurrentVertex(0),
dc3719f3 45fFieldkG(-999.),
fee9ef0d 46fConstraint(kFALSE),
47fOnlyFitter(kFALSE),
0c69be49 48fMinTracks(1),
fe12e09c 49fMinITSClusters(5),
07680cae 50fTrkArray(),
51fTrksToSkip(0),
52fNTrksToSkip(0),
53fDCAcut(0),
54fAlgo(1),
fe12e09c 55fNSigma(3),
fee9ef0d 56fMaxd0z0(0.5),
50ff0bcd 57fITSin(kTRUE),
58fITSrefit(kTRUE),
07680cae 59fDebug(0)
2d57349e 60{
61//
62// Default constructor
63//
64 SetVtxStart();
07680cae 65 SetVtxStartSigma();
2d57349e 66 SetMinTracks();
07680cae 67 SetMinITSClusters();
68 SetNSigmad0();
fee9ef0d 69 SetMaxd0z0();
2d57349e 70}
dc3719f3 71//----------------------------------------------------------------------------
72AliVertexerTracks::AliVertexerTracks(Double_t fieldkG):
07680cae 73TObject(),
74fVert(),
75fCurrentVertex(0),
dc3719f3 76fFieldkG(-999.),
fee9ef0d 77fConstraint(kFALSE),
78fOnlyFitter(kFALSE),
0c69be49 79fMinTracks(1),
fe12e09c 80fMinITSClusters(5),
07680cae 81fTrkArray(),
82fTrksToSkip(0),
83fNTrksToSkip(0),
84fDCAcut(0),
85fAlgo(1),
fe12e09c 86fNSigma(3),
fee9ef0d 87fMaxd0z0(0.5),
50ff0bcd 88fITSin(kTRUE),
89fITSrefit(kTRUE),
07680cae 90fDebug(0)
2d57349e 91{
92//
93// Standard constructor
94//
dc3719f3 95 SetVtxStart();
07680cae 96 SetVtxStartSigma();
2d57349e 97 SetMinTracks();
07680cae 98 SetMinITSClusters();
99 SetNSigmad0();
dc3719f3 100 SetMaxd0z0();
101 SetFieldkG(fieldkG);
2d57349e 102}
103//-----------------------------------------------------------------------------
fee9ef0d 104AliVertexerTracks::~AliVertexerTracks()
105{
2d57349e 106 // Default Destructor
eae1677e 107 // The objects pointed by the following pointer are not owned
2d57349e 108 // by this class and are not deleted
ec1be5d5 109 fCurrentVertex = 0;
146c29df 110 if(fTrksToSkip) { delete [] fTrksToSkip; fTrksToSkip=NULL; }
ec1be5d5 111}
c5e3e5d1 112//----------------------------------------------------------------------------
ec1be5d5 113AliESDVertex* AliVertexerTracks::FindPrimaryVertex(const AliESD *esdEvent)
114{
c5e3e5d1 115//
ec1be5d5 116// Primary vertex for current ESD event
eae1677e 117// (Two iterations:
fee9ef0d 118// 1st with 5*fNSigma*sigma cut w.r.t. to initial vertex
119// + cut on sqrt(d0d0+z0z0) if fConstraint=kFALSE
120// 2nd with fNSigma*sigma cut w.r.t. to vertex found in 1st iteration)
318aecb9 121// All ESD tracks with inside the beam pipe are then propagated to found vertex
07680cae 122//
123 fCurrentVertex = 0;
124
dc3719f3 125 // accept 1-track case only if constraint is available
126 if(!fConstraint && fMinTracks==1) fMinTracks=2;
127
fee9ef0d 128 // read tracks from ESD
50ff0bcd 129 Int_t nTrksTot = (Int_t)esdEvent->GetNumberOfTracks();
146c29df 130 if(nTrksTot<=0) {
80d36431 131 if(fDebug) printf("TooFewTracks\n");
132 TooFewTracks(esdEvent);
133 return fCurrentVertex;
134 }
713e5fbf 135
07680cae 136 TTree *trkTree = new TTree("TreeT","tracks");
137 AliESDtrack *esdTrack = 0;
138 trkTree->Branch("tracks","AliESDtrack",&esdTrack);
139
eae1677e 140 Bool_t skipThis;
50ff0bcd 141 for(Int_t i=0; i<nTrksTot; i++) {
fee9ef0d 142 AliESDtrack *et = esdEvent->GetTrack(i);
143 esdTrack = new AliESDtrack(*et);
eae1677e 144 // check tracks to skip
145 skipThis = kFALSE;
146 for(Int_t j=0; j<fNTrksToSkip; j++) {
fee9ef0d 147 if(et->GetID()==fTrksToSkip[j]) {
eae1677e 148 if(fDebug) printf("skipping track: %d\n",i);
149 skipThis = kTRUE;
150 }
151 }
50ff0bcd 152 if(skipThis) {delete esdTrack;continue;}
153 if(fITSin) {
154 if(!(esdTrack->GetStatus()&AliESDtrack::kITSin)) {delete esdTrack;continue;}
155 if(fITSrefit && !(esdTrack->GetStatus()&AliESDtrack::kITSrefit)) {delete esdTrack;continue;}
156 Int_t nclus=esdTrack->GetNcls(0); // check number of clusters in ITS
157 if(nclus<fMinITSClusters) {delete esdTrack;continue;}
158 }
07680cae 159 trkTree->Fill();
5834e9a1 160 delete esdTrack;
07680cae 161 }
fee9ef0d 162
163 // If fConstraint=kFALSE
164 // run VertexFinder(1) to get rough estimate of initVertex (x,y)
165 if(!fConstraint) {
166 // fill fTrkArray, for VertexFinder()
167 if(!fTrkArray.IsEmpty()) fTrkArray.Delete();
168 PrepareTracks(*trkTree,0);
169 Double_t cutsave = fDCAcut; fDCAcut = 0.1; // 1 mm
170 VertexFinder(1); // using weights, cutting dca < fDCAcut
171 fDCAcut = cutsave;
5834e9a1 172 fTrkArray.Delete();
fee9ef0d 173 if(fVert.GetNContributors()>0) {
174 fVert.GetXYZ(fNominalPos);
175 fNominalPos[0] = fVert.GetXv();
176 fNominalPos[1] = fVert.GetYv();
177 fNominalPos[2] = fVert.GetZv();
178 if(fDebug) printf("No mean vertex: VertexFinder gives (%f, %f, %f)\n",fNominalPos[0],fNominalPos[1],fNominalPos[2]);
179 } else {
180 fNominalPos[0] = 0.;
181 fNominalPos[1] = 0.;
182 fNominalPos[2] = 0.;
183 if(fDebug) printf("No mean vertex and VertexFinder failed\n");
0c69be49 184 }
07680cae 185 }
fee9ef0d 186
187 // TWO ITERATIONS:
188 //
189 // ITERATION 1
190 // propagate tracks to fNominalPos vertex
191 // preselect them:
192 // if(constraint) reject for |d0|>5*fNSigma*sigma w.r.t. fNominal... vertex
193 // else reject for |d0|\oplus|z0| > 5 mm w.r.t. fNominal... vertex
07680cae 194 // ITERATION 2
195 // propagate tracks to best between initVertex and fCurrentVertex
196 // preselect tracks (reject for |d0|>fNSigma*sigma w.r.t. best
197 // between initVertex and fCurrentVertex)
fee9ef0d 198 for(Int_t iter=0; iter<2; iter++) {
199 if(fOnlyFitter && iter==0) continue;
200 Int_t nTrksPrep = PrepareTracks(*trkTree,iter+1);
201 if(fDebug) printf(" tracks prepared - iteration %d: %d\n",iter+1,nTrksPrep);
202 if(nTrksPrep < fMinTracks) {
203 if(fDebug) printf("TooFewTracks\n");
204 TooFewTracks(esdEvent);
205 if(fDebug) fCurrentVertex->PrintStatus();
206 fTrkArray.Delete();
207 delete trkTree;
208 return fCurrentVertex;
209 }
07680cae 210
fee9ef0d 211 // vertex finder
212 if(!fOnlyFitter) {
213 if(nTrksPrep==1){
214 if(fDebug) printf("Just one track\n");
215 OneTrackVertFinder();
216 }else{
217 switch (fAlgo) {
218 case 1: StrLinVertexFinderMinDist(1); break;
219 case 2: StrLinVertexFinderMinDist(0); break;
220 case 3: HelixVertexFinder(); break;
221 case 4: VertexFinder(1); break;
222 case 5: VertexFinder(0); break;
223 default: printf("Wrong algorithm\n"); break;
224 }
225 }
226 if(fDebug) printf(" Vertex finding completed\n");
0c69be49 227 }
07680cae 228
fee9ef0d 229 // vertex fitter
230 VertexFitter(fConstraint);
231 if(fDebug) printf(" Vertex fit completed\n");
232 if(iter==0) fTrkArray.Delete();
233 } // end loop on the two iterations
07680cae 234
eae1677e 235
fee9ef0d 236 // take true pos from SPD vertex in ESD and write it in tracks' vertex
07680cae 237 Double_t tp[3];
238 esdEvent->GetVertex()->GetTruePos(tp);
239 fCurrentVertex->SetTruePos(tp);
eae1677e 240
fee9ef0d 241 if(fConstraint) {
242 if(fOnlyFitter) {
243 fCurrentVertex->SetTitle("VertexerTracksWithConstraintOnlyFitter");
244 } else {
245 fCurrentVertex->SetTitle("VertexerTracksWithConstraint");
318aecb9 246 }
247 } else {
fee9ef0d 248 fCurrentVertex->SetTitle("VertexerTracksNoConstraint");
318aecb9 249 }
50ff0bcd 250
fee9ef0d 251
50ff0bcd 252 // set indices of used tracks
253 UShort_t *indices = 0;
254 AliESDtrack *ett = 0;
255 if(fCurrentVertex->GetNContributors()>0) {
256 indices = new UShort_t[fCurrentVertex->GetNContributors()];
257 for(Int_t jj=0;jj<(Int_t)fTrkArray.GetEntriesFast();jj++) {
258 ett = (AliESDtrack*)fTrkArray.At(jj);
259 indices[jj] = (UShort_t)ett->GetID();
260 }
261 fCurrentVertex->SetIndices(fCurrentVertex->GetNContributors(),indices);
262 }
263 delete [] indices;
fee9ef0d 264
265 delete trkTree;
266
50ff0bcd 267 fTrkArray.Delete();
268
146c29df 269 if(fTrksToSkip) { delete [] fTrksToSkip; fTrksToSkip=NULL; }
50ff0bcd 270
271
272 if(fDebug) fCurrentVertex->PrintStatus();
273 if(fDebug) fCurrentVertex->PrintIndices();
fee9ef0d 274
275
07680cae 276 return fCurrentVertex;
277}
50ff0bcd 278//------------------------------------------------------------------------
fee9ef0d 279Double_t AliVertexerTracks::GetDeterminant3X3(Double_t matr[][3])
280{
50ff0bcd 281 //
282 Double_t det=matr[0][0]*matr[1][1]*matr[2][2]-matr[0][0]*matr[1][2]*matr[2][1]-matr[0][1]*matr[1][0]*matr[2][2]+matr[0][1]*matr[1][2]*matr[2][0]+matr[0][2]*matr[1][0]*matr[2][1]-matr[0][2]*matr[1][1]*matr[2][0];
283 return det;
284}
285//-------------------------------------------------------------------------
fee9ef0d 286void AliVertexerTracks::GetStrLinDerivMatrix(Double_t *p0,Double_t *p1,Double_t (*m)[3],Double_t *d)
287{
50ff0bcd 288 //
289 Double_t x12=p0[0]-p1[0];
290 Double_t y12=p0[1]-p1[1];
291 Double_t z12=p0[2]-p1[2];
292 Double_t kk=x12*x12+y12*y12+z12*z12;
293 m[0][0]=2-2/kk*x12*x12;
294 m[0][1]=-2/kk*x12*y12;
295 m[0][2]=-2/kk*x12*z12;
296 m[1][0]=-2/kk*x12*y12;
297 m[1][1]=2-2/kk*y12*y12;
298 m[1][2]=-2/kk*y12*z12;
299 m[2][0]=-2/kk*x12*z12;
300 m[2][1]=-2*y12*z12;
301 m[2][2]=2-2/kk*z12*z12;
302 d[0]=2*p0[0]-2/kk*p0[0]*x12*x12-2/kk*p0[2]*x12*z12-2/kk*p0[1]*x12*y12;
303 d[1]=2*p0[1]-2/kk*p0[1]*y12*y12-2/kk*p0[0]*x12*y12-2/kk*p0[2]*z12*y12;
304 d[2]=2*p0[2]-2/kk*p0[2]*z12*z12-2/kk*p0[0]*x12*z12-2/kk*p0[1]*z12*y12;
c5e3e5d1 305
50ff0bcd 306}
307//--------------------------------------------------------------------------
fee9ef0d 308void AliVertexerTracks::GetStrLinDerivMatrix(Double_t *p0,Double_t *p1,Double_t *sigmasq,Double_t (*m)[3],Double_t *d)
309{
50ff0bcd 310 //
311 Double_t x12=p1[0]-p0[0];
312 Double_t y12=p1[1]-p0[1];
313 Double_t z12=p1[2]-p0[2];
c5e3e5d1 314
50ff0bcd 315 Double_t den= x12*x12*sigmasq[1]*sigmasq[2]+y12*y12*sigmasq[0]*sigmasq[2]+z12*z12*sigmasq[0]*sigmasq[1];
c5e3e5d1 316
50ff0bcd 317 Double_t kk= 2*(x12*x12/sigmasq[0]+y12*y12/sigmasq[1]+z12*z12/sigmasq[2]);
c5e3e5d1 318
50ff0bcd 319 Double_t cc[3];
320 cc[0]=-x12/sigmasq[0];
321 cc[1]=-y12/sigmasq[1];
322 cc[2]=-z12/sigmasq[2];
ec1be5d5 323
50ff0bcd 324 Double_t ww=(-p0[0]*x12*sigmasq[1]*sigmasq[2]-p0[1]*y12*sigmasq[0]*sigmasq[2]-p0[2]*z12*sigmasq[0]*sigmasq[1])/den;
ec1be5d5 325
50ff0bcd 326 Double_t ss= -p0[0]*cc[0]-p0[1]*cc[1]-p0[2]*cc[2];
2d57349e 327
50ff0bcd 328 Double_t aa[3];
329 aa[0]=x12*sigmasq[1]*sigmasq[2]/den;
330 aa[1]=y12*sigmasq[0]*sigmasq[2]/den;
331 aa[2]=z12*sigmasq[0]*sigmasq[1]/den;
2d57349e 332
50ff0bcd 333 m[0][0]=aa[0]*(aa[0]*kk+2*cc[0])+2*cc[0]*aa[0]+2/sigmasq[0];
334 m[0][1]=aa[1]*(aa[0]*kk+2*cc[0])+2*cc[1]*aa[0];
335 m[0][2]=aa[2]*(aa[0]*kk+2*cc[0])+2*cc[2]*aa[0];
2d57349e 336
50ff0bcd 337 m[1][0]=aa[0]*(aa[1]*kk+2*cc[1])+2*cc[0]*aa[1];
338 m[1][1]=aa[1]*(aa[1]*kk+2*cc[1])+2*cc[1]*aa[1]+2/sigmasq[1];
339 m[1][2]=aa[2]*(aa[1]*kk+2*cc[1])+2*cc[2]*aa[1];
2d57349e 340
50ff0bcd 341 m[2][0]=aa[0]*(aa[2]*kk+2*cc[2])+2*cc[0]*aa[2];
342 m[2][1]=aa[1]*(aa[2]*kk+2*cc[2])+2*cc[1]*aa[2];
343 m[2][2]=aa[2]*(aa[2]*kk+2*cc[2])+2*cc[2]*aa[2]+2/sigmasq[2];
08df6187 344
50ff0bcd 345 d[0]=-ww*(aa[0]*kk+2*cc[0])-2*ss*aa[0]+2*p0[0]/sigmasq[0];
346 d[1]=-ww*(aa[1]*kk+2*cc[1])-2*ss*aa[1]+2*p0[1]/sigmasq[1];
347 d[2]=-ww*(aa[2]*kk+2*cc[2])-2*ss*aa[2]+2*p0[2]/sigmasq[2];
2d57349e 348
2d57349e 349 }
50ff0bcd 350//--------------------------------------------------------------------------
fee9ef0d 351Double_t AliVertexerTracks::GetStrLinMinDist(Double_t *p0,Double_t *p1,Double_t *x0)
352{
50ff0bcd 353 //
354 Double_t x12=p0[0]-p1[0];
355 Double_t y12=p0[1]-p1[1];
356 Double_t z12=p0[2]-p1[2];
357 Double_t x10=p0[0]-x0[0];
358 Double_t y10=p0[1]-x0[1];
359 Double_t z10=p0[2]-x0[2];
360 return ((x10*x10+y10*y10+z10*z10)*(x12*x12+y12*y12+z12*z12)-(x10*x12+y10*y12+z10*z12)*(x10*x12+y10*y12+z10*z12))/(x12*x12+y12*y12+z12*z12);
2d57349e 361}
362//---------------------------------------------------------------------------
fee9ef0d 363void AliVertexerTracks::OneTrackVertFinder()
364{
0c69be49 365 // find vertex for events with 1 track, using DCA to nominal beam axis
366 if(fDebug) printf("Number of prepared tracks =%d - Call OneTrackVertFinder",fTrkArray.GetEntries());
367 AliESDtrack *track1;
368 track1 = (AliESDtrack*)fTrkArray.At(0);
dc3719f3 369 Double_t field=GetFieldkG();
0c69be49 370 Double_t alpha=track1->GetAlpha();
371 Double_t mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
372 Double_t pos[3],dir[3];
373 track1->GetXYZAt(mindist,field,pos);
374 track1->GetPxPyPzAt(mindist,field,dir);
375 AliStrLine *line1 = new AliStrLine(pos,dir);
376 Double_t p1[3]={fNominalPos[0],fNominalPos[1],0.};
377 Double_t p2[3]={fNominalPos[0],fNominalPos[1],10.};
378 AliStrLine *zeta=new AliStrLine(p1,p2,kTRUE);
379 Double_t crosspoint[3]={0.,0.,0.};
380 Double_t sigma=999.;
381 Int_t nContrib=-1;
382 Int_t retcode = zeta->Cross(line1,crosspoint);
383 if(retcode>=0){
384 sigma=line1->GetDistFromPoint(crosspoint);
385 nContrib=1;
386 }
387 delete zeta;
388 delete line1;
389 fVert.SetXYZ(crosspoint);
390 fVert.SetDispersion(sigma);
391 fVert.SetNContributors(nContrib);
392}
393//---------------------------------------------------------------------------
fee9ef0d 394void AliVertexerTracks::HelixVertexFinder()
395{
2d57349e 396 // Get estimate of vertex position in (x,y) from tracks DCA
397
398
399 Double_t initPos[3];
400 initPos[2] = 0.;
401 for(Int_t i=0;i<2;i++)initPos[i]=fNominalPos[i];
dc3719f3 402 Double_t field=GetFieldkG();
2d57349e 403
404 Int_t nacc = (Int_t)fTrkArray.GetEntriesFast();
405
406 Double_t aver[3]={0.,0.,0.};
407 Double_t averquad[3]={0.,0.,0.};
408 Double_t sigmaquad[3]={0.,0.,0.};
409 Double_t sigma=0;
410 Int_t ncombi = 0;
411 AliESDtrack *track1;
412 AliESDtrack *track2;
413 Double_t distCA;
414 Double_t x, par[5];
415 Double_t alpha, cs, sn;
416 Double_t crosspoint[3];
417 for(Int_t i=0; i<nacc; i++){
418 track1 = (AliESDtrack*)fTrkArray.At(i);
419
420
421 for(Int_t j=i+1; j<nacc; j++){
422 track2 = (AliESDtrack*)fTrkArray.At(j);
423
424 distCA=track2->PropagateToDCA(track1,field);
2d57349e 425 if(fDCAcut<=0 ||(fDCAcut>0&&distCA<fDCAcut)){
426 track1->GetExternalParameters(x,par);
427 alpha=track1->GetAlpha();
428 cs=TMath::Cos(alpha); sn=TMath::Sin(alpha);
429 Double_t x1=x*cs - par[0]*sn;
430 Double_t y1=x*sn + par[0]*cs;
431 Double_t z1=par[1];
432 Double_t sx1=sn*sn*track1->GetSigmaY2(), sy1=cs*cs*track1->GetSigmaY2();
433 track2->GetExternalParameters(x,par);
434 alpha=track2->GetAlpha();
435 cs=TMath::Cos(alpha); sn=TMath::Sin(alpha);
436 Double_t x2=x*cs - par[0]*sn;
437 Double_t y2=x*sn + par[0]*cs;
438 Double_t z2=par[1];
439 Double_t sx2=sn*sn*track2->GetSigmaY2(), sy2=cs*cs*track2->GetSigmaY2();
440 Double_t sz1=track1->GetSigmaZ2(), sz2=track2->GetSigmaZ2();
441 Double_t wx1=sx2/(sx1+sx2), wx2=1.- wx1;
442 Double_t wy1=sy2/(sy1+sy2), wy2=1.- wy1;
443 Double_t wz1=sz2/(sz1+sz2), wz2=1.- wz1;
444 crosspoint[0]=wx1*x1 + wx2*x2;
445 crosspoint[1]=wy1*y1 + wy2*y2;
446 crosspoint[2]=wz1*z1 + wz2*z2;
447
448 ncombi++;
449 for(Int_t jj=0;jj<3;jj++)aver[jj]+=crosspoint[jj];
450 for(Int_t jj=0;jj<3;jj++)averquad[jj]+=(crosspoint[jj]*crosspoint[jj]);
451 }
452 }
453
454 }
455 if(ncombi>0){
456 for(Int_t jj=0;jj<3;jj++){
457 initPos[jj] = aver[jj]/ncombi;
458 averquad[jj]/=ncombi;
459 sigmaquad[jj]=averquad[jj]-initPos[jj]*initPos[jj];
460 sigma+=sigmaquad[jj];
461 }
462 sigma=TMath::Sqrt(TMath::Abs(sigma));
463 }
464 else {
465 Warning("HelixVertexFinder","Finder did not succed");
466 sigma=999;
467 }
468 fVert.SetXYZ(initPos);
469 fVert.SetDispersion(sigma);
470 fVert.SetNContributors(ncombi);
471}
50ff0bcd 472//----------------------------------------------------------------------------
fee9ef0d 473Int_t AliVertexerTracks::PrepareTracks(TTree &trkTree,Int_t optImpParCut)
474{
50ff0bcd 475//
476// Propagate tracks to initial vertex position and store them in a TObjArray
477//
fee9ef0d 478 Double_t maxd0rphi;
479 Double_t maxd0z0 = fMaxd0z0; // default is 5 mm
50ff0bcd 480 Int_t nTrks = 0;
481 Double_t sigmaCurr[3];
fee9ef0d 482 Double_t normdistx,normdisty;
50ff0bcd 483 Float_t d0z0[2],covd0z0[3];
484 Double_t sigma;
dc3719f3 485 Double_t field=GetFieldkG();
50ff0bcd 486
487 AliESDVertex *initVertex = new AliESDVertex(fNominalPos,fNominalCov,1,1);
488
489 Int_t nEntries = (Int_t)trkTree.GetEntries();
490 if(!fTrkArray.IsEmpty()) fTrkArray.Delete();
491
492 if(fDebug) {
493 printf(" PrepareTracks()\n");
494 }
495
496 for(Int_t i=0; i<nEntries; i++) {
497 AliESDtrack *track = new AliESDtrack;
498 trkTree.SetBranchAddress("tracks",&track);
499 trkTree.GetEvent(i);
500
501 // propagate track to vertex
fee9ef0d 502 if(optImpParCut<=1 || fOnlyFitter) { // optImpParCut==1 or 0
50ff0bcd 503 track->RelateToVertex(initVertex,field,100.);
fee9ef0d 504 } else { // optImpParCut==2
50ff0bcd 505 fCurrentVertex->GetSigmaXYZ(sigmaCurr);
fee9ef0d 506 normdistx = TMath::Abs(fCurrentVertex->GetXv()-fNominalPos[0])/TMath::Sqrt(sigmaCurr[0]*sigmaCurr[0]+fNominalCov[0]);
80d36431 507 normdisty = TMath::Abs(fCurrentVertex->GetYv()-fNominalPos[1])/TMath::Sqrt(sigmaCurr[1]*sigmaCurr[1]+fNominalCov[2]);
fee9ef0d 508 if(normdistx < 3. && normdisty < 3. &&
80d36431 509 (sigmaCurr[0]+sigmaCurr[1])<(TMath::Sqrt(fNominalCov[0])+TMath::Sqrt(fNominalCov[2]))) {
50ff0bcd 510 track->RelateToVertex(fCurrentVertex,field,100.);
511 } else {
512 track->RelateToVertex(initVertex,field,100.);
513 }
514 }
515
50ff0bcd 516 track->GetImpactParameters(d0z0,covd0z0);
517 sigma = TMath::Sqrt(covd0z0[0]);
518 maxd0rphi = fNSigma*sigma;
fee9ef0d 519 if(optImpParCut==1) maxd0rphi *= 5.;
520
521
50ff0bcd 522
fee9ef0d 523 if(fDebug) printf("trk %d; lab %d; |d0| = %f; d0 cut = %f; |z0| = %f; |d0|oplus|z0| = %f; d0z0 cut = %f\n",i,track->GetLabel(),TMath::Abs(d0z0[0]),maxd0rphi,TMath::Abs(d0z0[1]),TMath::Sqrt(d0z0[0]*d0z0[0]+d0z0[1]*d0z0[1]),maxd0z0);
524
80d36431 525 // during iterations 1 and 2, if fConstraint=kFALSE,
fee9ef0d 526 // select tracks with d0oplusz0 < maxd0z0
80d36431 527 if(optImpParCut>=1 && !fConstraint && nEntries>=3 &&
fee9ef0d 528 fVert.GetNContributors()>0) {
529 if(TMath::Sqrt(d0z0[0]*d0z0[0]+d0z0[1]*d0z0[1]) > maxd0z0) {
530 if(fDebug) printf(" rejected\n");
531 delete track; continue;
532 }
533 }
534
535 // select tracks with d0rphi < maxd0rphi
536 if(optImpParCut>0 && TMath::Abs(d0z0[0]) > maxd0rphi) {
50ff0bcd 537 if(fDebug) printf(" rejected\n");
538 delete track; continue;
539 }
540
541 fTrkArray.AddLast(track);
542 nTrks++;
543 }
544
545 delete initVertex;
546
547 return nTrks;
548}
549//---------------------------------------------------------------------------
fee9ef0d 550AliESDVertex* AliVertexerTracks::RemoveTracksFromVertex(AliESDVertex *inVtx,
551 TTree *trksTree,
552 Float_t *diamondxy)
553{
50ff0bcd 554//
fee9ef0d 555// Removes tracks in trksTree from fit of inVtx
50ff0bcd 556//
fee9ef0d 557
146c29df 558 if(!strstr(inVtx->GetTitle(),"VertexerTracksWithConstraint")) {
559 printf("ERROR: primary vertex has no constraint: cannot remove tracks\n");
560 return 0x0;
561 }
fee9ef0d 562 if(!strstr(inVtx->GetTitle(),"VertexerTracksWithConstraintOnlyFitter"))
563 printf("WARNING: result of tracks' removal will be only approximately correct\n");
564
565 TMatrixD rv(3,1);
566 rv(0,0) = inVtx->GetXv();
567 rv(1,0) = inVtx->GetYv();
568 rv(2,0) = inVtx->GetZv();
569 TMatrixD vV(3,3);
570 Double_t cov[6];
571 inVtx->GetCovMatrix(cov);
572 vV(0,0) = cov[0];
573 vV(0,1) = cov[1]; vV(1,0) = cov[1];
574 vV(1,1) = cov[2];
575 vV(0,2) = cov[3]; vV(2,0) = cov[3];
576 vV(1,2) = cov[4]; vV(2,1) = cov[4];
577 vV(2,2) = cov[5];
578
579 TMatrixD sumWi(TMatrixD::kInverted,vV);
580 TMatrixD sumWiri(sumWi,TMatrixD::kMult,rv);
581
582 Int_t nUsedTrks = inVtx->GetNContributors();
583 Double_t chi2 = inVtx->GetChi2();
584
585 AliESDtrack *track = 0;
586 trksTree->SetBranchAddress("tracks",&track);
587 Int_t ntrks = trksTree->GetEntries();
588 for(Int_t i=0;i<ntrks;i++) {
589 trksTree->GetEvent(i);
590 if(!inVtx->UsesTrack(track->GetID())) {
591 printf("track %d was not used in vertex fit\n",track->GetID());
592 continue;
593 }
594 Double_t alpha = track->GetAlpha();
595 Double_t xl = diamondxy[0]*TMath::Cos(alpha)+diamondxy[1]*TMath::Sin(alpha);
dc3719f3 596 track->AliExternalTrackParam::PropagateTo(xl,GetFieldkG());
fee9ef0d 597 // vector of track global coordinates
598 TMatrixD ri(3,1);
599 // covariance matrix of ri
600 TMatrixD wWi(3,3);
601
602 // get space point from track
603 if(!TrackToPoint(track,ri,wWi)) continue;
604
605 TMatrixD wWiri(wWi,TMatrixD::kMult,ri);
606
607 sumWi -= wWi;
608 sumWiri -= wWiri;
609
610 // track chi2
611 TMatrixD deltar = rv; deltar -= ri;
612 TMatrixD wWideltar(wWi,TMatrixD::kMult,deltar);
613 Double_t chi2i = deltar(0,0)*wWideltar(0,0)+
614 deltar(1,0)*wWideltar(1,0)+
615 deltar(2,0)*wWideltar(2,0);
616 // remove from total chi2
617 chi2 -= chi2i;
618
619 nUsedTrks--;
620 if(nUsedTrks<2) {
621 printf("Trying to remove too many tracks!\n");
622 return 0x0;
623 }
624 }
625
626 TMatrixD rvnew(3,1);
627 TMatrixD vVnew(3,3);
628
629 // new inverted of weights matrix
630 TMatrixD invsumWi(TMatrixD::kInverted,sumWi);
631 vVnew = invsumWi;
632 // new position of primary vertex
633 rvnew.Mult(vVnew,sumWiri);
634
635 Double_t position[3];
636 position[0] = rvnew(0,0);
637 position[1] = rvnew(1,0);
638 position[2] = rvnew(2,0);
639 cov[0] = vVnew(0,0);
640 cov[1] = vVnew(0,1);
641 cov[2] = vVnew(1,1);
642 cov[3] = vVnew(0,2);
643 cov[4] = vVnew(1,2);
644 cov[5] = vVnew(2,2);
645
646 // store data in the vertex object
647 AliESDVertex *outVtx = new AliESDVertex(position,cov,chi2,nUsedTrks);
648 outVtx->SetTitle(inVtx->GetTitle());
649 Double_t tp[3];
650 inVtx->GetTruePos(tp);
651 outVtx->SetTruePos(tp);
652 UShort_t *inindices = inVtx->GetIndices();
653 UShort_t *outindices = new UShort_t[outVtx->GetNContributors()];
654 Int_t j=0;
655 Bool_t copyindex;
656 for(Int_t k=0; k<inVtx->GetNIndices(); k++) {
657 copyindex=kTRUE;
658 for(Int_t l=0; l<ntrks; l++) {
659 trksTree->GetEvent(l);
660 if(inindices[k]==track->GetID()) copyindex=kFALSE;
661 }
662 if(copyindex) {
663 outindices[j] = inindices[k]; j++;
664 }
665 }
666 outVtx->SetIndices(outVtx->GetNContributors(),outindices);
667 delete [] outindices;
668
669 if(fDebug) {
670 printf("Vertex before removing tracks:\n");
671 inVtx->PrintStatus();
672 inVtx->PrintIndices();
673 printf("Vertex after removing tracks:\n");
674 outVtx->PrintStatus();
675 outVtx->PrintIndices();
676 }
677
678 return outVtx;
679}
680//---------------------------------------------------------------------------
681void AliVertexerTracks::SetSkipTracks(Int_t n,Int_t *skipped)
682{
683//
684// Mark the tracks not to be used in the vertex reconstruction.
685// Tracks are identified by AliESDtrack::GetID()
686//
687 fNTrksToSkip = n; fTrksToSkip = new Int_t[n];
50ff0bcd 688 for(Int_t i=0;i<n;i++) fTrksToSkip[i] = skipped[i];
689 return;
690}
691//---------------------------------------------------------------------------
fee9ef0d 692void AliVertexerTracks::SetVtxStart(AliESDVertex *vtx)
693{
50ff0bcd 694//
695// Set initial vertex knowledge
696//
697 vtx->GetXYZ(fNominalPos);
698 vtx->GetCovMatrix(fNominalCov);
fee9ef0d 699 SetConstraintOn();
50ff0bcd 700 return;
701}
2d57349e 702//---------------------------------------------------------------------------
fee9ef0d 703void AliVertexerTracks::StrLinVertexFinderMinDist(Int_t optUseWeights)
dc3719f3 704{
705 AliESDtrack *track1;
706 Double_t field=GetFieldkG();
707 const Int_t knacc = (Int_t)fTrkArray.GetEntriesFast();
708 TClonesArray *linarray = new TClonesArray("AliStrLine",1000);
709 TClonesArray &lines = *linarray;
710 for(Int_t i=0; i<knacc; i++){
711 track1 = (AliESDtrack*)fTrkArray.At(i);
712 Double_t alpha=track1->GetAlpha();
713 Double_t mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
714 Double_t pos[3],dir[3],sigmasq[3];
715 track1->GetXYZAt(mindist,field,pos);
716 track1->GetPxPyPzAt(mindist,field,dir);
717 sigmasq[0]=TMath::Sin(alpha)*TMath::Sin(alpha)*track1->GetSigmaY2();
718 sigmasq[1]=TMath::Cos(alpha)*TMath::Cos(alpha)*track1->GetSigmaY2();
719 sigmasq[2]=track1->GetSigmaZ2();
146c29df 720 TMatrixD ri(3,1);
721 TMatrixD wWi(3,3);
722 if(!TrackToPoint(track1,ri,wWi)) continue;
723 Double_t wmat[9];
724 Int_t iel=0;
725 for(Int_t ia=0;ia<3;ia++){
726 for(Int_t ib=0;ib<3;ib++){
727 wmat[iel]=wWi(ia,ib);
728 iel++;
729 }
730 }
731 new(lines[i]) AliStrLine(pos,sigmasq,wmat,dir);
dc3719f3 732 }
733 fVert=TrackletVertexFinder(linarray,optUseWeights);
734 linarray->Delete();
735 delete linarray;
736}
737//---------------------------------------------------------------------------
146c29df 738AliESDVertex AliVertexerTracks::TrackletVertexFinder(TClonesArray *lines, Int_t optUseWeights)
fee9ef0d 739{
2d57349e 740 // Calculate the point at minimum distance to prepared tracks
741
dc3719f3 742 const Int_t knacc = (Int_t)lines->GetEntriesFast();
743 Double_t initPos[3]={0.,0.,0.};
2d57349e 744
2d57349e 745 Double_t (*vectP0)[3]=new Double_t [knacc][3];
746 Double_t (*vectP1)[3]=new Double_t [knacc][3];
747
748 Double_t sum[3][3];
749 Double_t dsum[3]={0,0,0};
146c29df 750 TMatrixD sumWi(3,3);
751 for(Int_t i=0;i<3;i++){
752 for(Int_t j=0;j<3;j++){
753 sum[i][j]=0;
754 sumWi(i,j)=0.;
755 }
756 }
757
2d57349e 758 for(Int_t i=0; i<knacc; i++){
dc3719f3 759 AliStrLine* line1 = (AliStrLine*)lines->At(i);
760 Double_t p0[3],cd[3],sigmasq[3];
146c29df 761 Double_t wmat[9];
2d57349e 762 line1->GetP0(p0);
763 line1->GetCd(cd);
dc3719f3 764 line1->GetSigma2P0(sigmasq);
146c29df 765 line1->GetWMatrix(wmat);
766 TMatrixD wWi(3,3);
767 Int_t iel=0;
768 for(Int_t ia=0;ia<3;ia++){
769 for(Int_t ib=0;ib<3;ib++){
770 wWi(ia,ib)=wmat[iel];
771 iel++;
772 }
773 }
774
775 sumWi+=wWi;
776
2d57349e 777 Double_t p1[3]={p0[0]+cd[0],p0[1]+cd[1],p0[2]+cd[2]};
778 vectP0[i][0]=p0[0];
779 vectP0[i][1]=p0[1];
780 vectP0[i][2]=p0[2];
781 vectP1[i][0]=p1[0];
782 vectP1[i][1]=p1[1];
783 vectP1[i][2]=p1[2];
784
785 Double_t matr[3][3];
786 Double_t dknow[3];
787 if(optUseWeights==0)GetStrLinDerivMatrix(p0,p1,matr,dknow);
dc3719f3 788 else GetStrLinDerivMatrix(p0,p1,sigmasq,matr,dknow);
789
2d57349e 790
791 for(Int_t iii=0;iii<3;iii++){
792 dsum[iii]+=dknow[iii];
793 for(Int_t lj=0;lj<3;lj++) sum[iii][lj]+=matr[iii][lj];
794 }
2d57349e 795 }
146c29df 796
797 TMatrixD invsumWi(TMatrixD::kInverted,sumWi);
798 Double_t covmatrix[6];
799 covmatrix[0] = invsumWi(0,0);
800 covmatrix[1] = invsumWi(0,1);
801 covmatrix[2] = invsumWi(1,1);
802 covmatrix[3] = invsumWi(0,2);
803 covmatrix[4] = invsumWi(1,2);
804 covmatrix[5] = invsumWi(2,2);
805
2d57349e 806 Double_t vett[3][3];
807 Double_t det=GetDeterminant3X3(sum);
dc3719f3 808 Double_t sigma=0;
2d57349e 809
fee9ef0d 810 if(det!=0){
811 for(Int_t zz=0;zz<3;zz++){
812 for(Int_t ww=0;ww<3;ww++){
813 for(Int_t kk=0;kk<3;kk++) vett[ww][kk]=sum[ww][kk];
814 }
815 for(Int_t kk=0;kk<3;kk++) vett[kk][zz]=dsum[kk];
816 initPos[zz]=GetDeterminant3X3(vett)/det;
817 }
818
819
820 for(Int_t i=0; i<knacc; i++){
821 Double_t p0[3]={0,0,0},p1[3]={0,0,0};
822 for(Int_t ii=0;ii<3;ii++){
823 p0[ii]=vectP0[i][ii];
824 p1[ii]=vectP1[i][ii];
825 }
826 sigma+=GetStrLinMinDist(p0,p1,initPos);
827 }
828
829 sigma=TMath::Sqrt(sigma);
830 }else{
50ff0bcd 831 sigma=999;
832 }
146c29df 833 AliESDVertex theVert(initPos,covmatrix,99999.,knacc);
834 theVert.SetDispersion(sigma);
50ff0bcd 835 delete vectP0;
836 delete vectP1;
dc3719f3 837 return theVert;
50ff0bcd 838}
839//---------------------------------------------------------------------------
fee9ef0d 840Bool_t AliVertexerTracks::TrackToPoint(AliESDtrack *t,
841 TMatrixD &ri,TMatrixD &wWi) const
842{
843//
844// Extract from the AliESDtrack the global coordinates ri and covariance matrix
845// wWi of the space point that it represents (to be used in VertexFitter())
846//
847
848
849 Double_t rotAngle = t->GetAlpha();
850 if(rotAngle<0.) rotAngle += 2.*TMath::Pi();
851 Double_t cosRot = TMath::Cos(rotAngle);
852 Double_t sinRot = TMath::Sin(rotAngle);
853
854 ri(0,0) = t->GetX()*cosRot-t->GetY()*sinRot;
855 ri(1,0) = t->GetX()*sinRot+t->GetY()*cosRot;
856 ri(2,0) = t->GetZ();
857
858 // matrix to go from global (x,y,z) to local (y,z);
859 TMatrixD qQi(2,3);
860 qQi(0,0) = -sinRot;
861 qQi(0,1) = cosRot;
862 qQi(0,2) = 0.;
863 qQi(1,0) = 0.;
864 qQi(1,1) = 0.;
865 qQi(1,2) = 1.;
866
867 // covariance matrix of local (y,z) - inverted
868 TMatrixD uUi(2,2);
869 Double_t cc[15];
870 t->GetExternalCovariance(cc);
871 uUi(0,0) = cc[0];
146c29df 872
fee9ef0d 873 uUi(0,1) = cc[1];
874 uUi(1,0) = cc[1];
875 uUi(1,1) = cc[2];
876 if(uUi.Determinant() <= 0.) return kFALSE;
877 TMatrixD uUiInv(TMatrixD::kInverted,uUi);
878
879 // weights matrix: wWi = qQiT * uUiInv * qQi
880 TMatrixD uUiInvQi(uUiInv,TMatrixD::kMult,qQi);
881 TMatrixD m(qQi,TMatrixD::kTransposeMult,uUiInvQi);
882 wWi = m;
883
884 return kTRUE;
885}
886//---------------------------------------------------------------------------
887void AliVertexerTracks::TooFewTracks(const AliESD* esdEvent)
888{
50ff0bcd 889//
890// When the number of tracks is < fMinTracks
891//
2d57349e 892
50ff0bcd 893 // deal with vertices not found
894 Double_t pos[3],err[3];
895 Int_t ncontr=0;
896 pos[0] = fNominalPos[0];
897 err[0] = TMath::Sqrt(fNominalCov[0]);
898 pos[1] = fNominalPos[1];
899 err[1] = TMath::Sqrt(fNominalCov[2]);
900 pos[2] = esdEvent->GetVertex()->GetZv();
901 err[2] = esdEvent->GetVertex()->GetZRes();
902 if(err[0]>1. && esdEvent->GetVertex()->GetNContributors()<=0)
903 ncontr = -1; // (x,y,z) = (0,0,0)
904 if(err[0]>1. && esdEvent->GetVertex()->GetNContributors()>0)
905 ncontr = -2; // (x,y,z) = (0,0,z_fromSPD)
906 if(err[0]<1. && esdEvent->GetVertex()->GetNContributors()<=0)
907 ncontr = -3; // (x,y,z) = (x_mean,y_mean,0)
908 if(err[0]<1. && esdEvent->GetVertex()->GetNContributors()>0)
909 ncontr = -4; // (x,y,z) = (x_mean,y_mean,z_fromSPD)
910 fCurrentVertex = 0;
911 fCurrentVertex = new AliESDVertex(pos,err);
912 fCurrentVertex->SetNContributors(ncontr);
2d57349e 913
50ff0bcd 914 Double_t tp[3];
915 esdEvent->GetVertex()->GetTruePos(tp);
916 fCurrentVertex->SetTruePos(tp);
fee9ef0d 917 if(fConstraint) {
918 fCurrentVertex->SetTitle("VertexerTracksWithConstraint");
919 } else {
50ff0bcd 920 fCurrentVertex->SetTitle("VertexerTracksNoConstraint");
fee9ef0d 921 }
2d57349e 922
50ff0bcd 923 return;
924}
925//---------------------------------------------------------------------------
fee9ef0d 926void AliVertexerTracks::VertexFinder(Int_t optUseWeights)
927{
2d57349e 928
50ff0bcd 929 // Get estimate of vertex position in (x,y) from tracks DCA
930
931 Double_t initPos[3];
932 initPos[2] = 0.;
933 for(Int_t i=0;i<2;i++)initPos[i]=fNominalPos[i];
934 Int_t nacc = (Int_t)fTrkArray.GetEntriesFast();
935 Double_t aver[3]={0.,0.,0.};
936 Double_t aversq[3]={0.,0.,0.};
937 Double_t sigmasq[3]={0.,0.,0.};
938 Double_t sigma=0;
939 Int_t ncombi = 0;
940 AliESDtrack *track1;
941 AliESDtrack *track2;
942 Double_t pos[3],dir[3];
943 Double_t alpha,mindist;
dc3719f3 944 Double_t field=GetFieldkG();
2d57349e 945
50ff0bcd 946 for(Int_t i=0; i<nacc; i++){
947 track1 = (AliESDtrack*)fTrkArray.At(i);
948 alpha=track1->GetAlpha();
949 mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
950 track1->GetXYZAt(mindist,field,pos);
951 track1->GetPxPyPzAt(mindist,field,dir);
952 AliStrLine *line1 = new AliStrLine(pos,dir);
2d57349e 953
50ff0bcd 954 // AliStrLine *line1 = new AliStrLine();
955 // track1->ApproximateHelixWithLine(mindist,field,line1);
956
957 for(Int_t j=i+1; j<nacc; j++){
958 track2 = (AliESDtrack*)fTrkArray.At(j);
959 alpha=track2->GetAlpha();
960 mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
961 track2->GetXYZAt(mindist,field,pos);
962 track2->GetPxPyPzAt(mindist,field,dir);
963 AliStrLine *line2 = new AliStrLine(pos,dir);
964 // AliStrLine *line2 = new AliStrLine();
965 // track2->ApproximateHelixWithLine(mindist,field,line2);
966 Double_t distCA=line2->GetDCA(line1);
fee9ef0d 967 //printf("%d %d %f\n",i,j,distCA);
968 if(fDCAcut<=0 || (fDCAcut>0&&distCA<fDCAcut)){
50ff0bcd 969 Double_t pnt1[3],pnt2[3],crosspoint[3];
2d57349e 970
50ff0bcd 971 if(optUseWeights<=0){
972 Int_t retcode = line2->Cross(line1,crosspoint);
973 if(retcode>=0){
974 ncombi++;
975 for(Int_t jj=0;jj<3;jj++)aver[jj]+=crosspoint[jj];
976 for(Int_t jj=0;jj<3;jj++)aversq[jj]+=(crosspoint[jj]*crosspoint[jj]);
977 }
978 }
979 if(optUseWeights>0){
980 Int_t retcode = line1->CrossPoints(line2,pnt1,pnt2);
981 if(retcode>=0){
5c03260f 982 Double_t cs, sn;
50ff0bcd 983 alpha=track1->GetAlpha();
984 cs=TMath::Cos(alpha); sn=TMath::Sin(alpha);
985 Double_t sx1=sn*sn*track1->GetSigmaY2(), sy1=cs*cs*track1->GetSigmaY2();
986 alpha=track2->GetAlpha();
987 cs=TMath::Cos(alpha); sn=TMath::Sin(alpha);
988 Double_t sx2=sn*sn*track2->GetSigmaY2(), sy2=cs*cs*track2->GetSigmaY2();
989 Double_t sz1=track1->GetSigmaZ2(), sz2=track2->GetSigmaZ2();
990 Double_t wx1=sx2/(sx1+sx2), wx2=1.- wx1;
991 Double_t wy1=sy2/(sy1+sy2), wy2=1.- wy1;
992 Double_t wz1=sz2/(sz1+sz2), wz2=1.- wz1;
993 crosspoint[0]=wx1*pnt1[0] + wx2*pnt2[0];
994 crosspoint[1]=wy1*pnt1[1] + wy2*pnt2[1];
995 crosspoint[2]=wz1*pnt1[2] + wz2*pnt2[2];
996
997 ncombi++;
998 for(Int_t jj=0;jj<3;jj++)aver[jj]+=crosspoint[jj];
999 for(Int_t jj=0;jj<3;jj++)aversq[jj]+=(crosspoint[jj]*crosspoint[jj]);
1000 }
1001 }
1002 }
1003 delete line2;
1004 }
1005 delete line1;
71d84967 1006 }
50ff0bcd 1007 if(ncombi>0){
1008 for(Int_t jj=0;jj<3;jj++){
1009 initPos[jj] = aver[jj]/ncombi;
fee9ef0d 1010 //printf("%f\n",initPos[jj]);
50ff0bcd 1011 aversq[jj]/=ncombi;
1012 sigmasq[jj]=aversq[jj]-initPos[jj]*initPos[jj];
1013 sigma+=sigmasq[jj];
1014 }
1015 sigma=TMath::Sqrt(TMath::Abs(sigma));
1016 }
1017 else {
1018 Warning("VertexFinder","Finder did not succed");
1019 sigma=999;
1020 }
1021 fVert.SetXYZ(initPos);
1022 fVert.SetDispersion(sigma);
1023 fVert.SetNContributors(ncombi);
2d57349e 1024}
ec1be5d5 1025//---------------------------------------------------------------------------
fee9ef0d 1026void AliVertexerTracks::VertexFitter(Bool_t useConstraint)
1027{
ec1be5d5 1028//
1029// The optimal estimate of the vertex position is given by a "weighted
1030// average of tracks positions"
fee9ef0d 1031// Original method: V. Karimaki, CMS Note 97/0051
ec1be5d5 1032//
1033 Double_t initPos[3];
1034 fVert.GetXYZ(initPos);
fee9ef0d 1035 Int_t arrEntries = (Int_t)fTrkArray.GetEntries();
1036 if(arrEntries==1) useConstraint=kTRUE;
ec1be5d5 1037 if(fDebug) {
1038 printf(" VertexFitter(): start\n");
1039 printf(" Number of tracks in array: %d\n",(Int_t)fTrkArray.GetEntriesFast());
1040 printf(" Minimum # tracks required in fit: %d\n",fMinTracks);
1041 printf("Vertex position after finder: %f,%f,%f\n",initPos[0],initPos[1],initPos[2]);
fee9ef0d 1042 if(useConstraint) printf(" This vertex will be used in fit: (%f+-%f,%f+-%f)\n",fNominalPos[0],TMath::Sqrt(fNominalCov[0]),fNominalPos[1],TMath::Sqrt(fNominalCov[2]));
ec1be5d5 1043 }
1044
1045 Int_t i,j,k,step=0;
1046 TMatrixD rv(3,1);
1047 TMatrixD vV(3,3);
1048 rv(0,0) = initPos[0];
1049 rv(1,0) = initPos[1];
1050 rv(2,0) = 0.;
1051 Double_t xlStart,alpha;
ec1be5d5 1052 Int_t nUsedTrks;
fee9ef0d 1053 Double_t chi2,chi2i,chi2b;
ec1be5d5 1054 AliESDtrack *t = 0;
1055 Int_t failed = 0;
1056
50ff0bcd 1057 // initial vertex covariance matrix
1058 TMatrixD vVb(3,3);
1059 vVb(0,0) = fNominalCov[0];
1060 vVb(0,1) = fNominalCov[1];
1061 vVb(0,2) = 0.;
1062 vVb(1,0) = fNominalCov[1];
1063 vVb(1,1) = fNominalCov[2];
1064 vVb(1,2) = 0.;
1065 vVb(2,0) = 0.;
1066 vVb(2,1) = 0.;
1067 vVb(2,2) = fNominalCov[5];
1068 TMatrixD vVbInv(TMatrixD::kInverted,vVb);
1069 TMatrixD rb(3,1);
1070 rb(0,0) = fNominalPos[0];
1071 rb(1,0) = fNominalPos[1];
1072 rb(2,0) = fNominalPos[2];
1073 TMatrixD vVbInvrb(vVbInv,TMatrixD::kMult,rb);
1074
1075
eae1677e 1076 // 2 steps:
1077 // 1st - estimate of vtx using all tracks
1078 // 2nd - estimate of global chi2
1079 for(step=0; step<2; step++) {
ec1be5d5 1080 if(fDebug) printf(" step = %d\n",step);
1081 chi2 = 0.;
1082 nUsedTrks = 0;
1083
1084 TMatrixD sumWiri(3,1);
1085 TMatrixD sumWi(3,3);
1086 for(i=0; i<3; i++) {
1087 sumWiri(i,0) = 0.;
1088 for(j=0; j<3; j++) sumWi(j,i) = 0.;
1089 }
1090
fee9ef0d 1091 // mean vertex constraint
1092 if(useConstraint) {
50ff0bcd 1093 for(i=0;i<3;i++) {
1094 sumWiri(i,0) += vVbInvrb(i,0);
1095 for(k=0;k<3;k++) sumWi(i,k) += vVbInv(i,k);
07680cae 1096 }
fee9ef0d 1097 // chi2
1098 TMatrixD deltar = rv; deltar -= rb;
1099 TMatrixD vVbInvdeltar(vVbInv,TMatrixD::kMult,deltar);
1100 chi2b = deltar(0,0)*vVbInvdeltar(0,0)+
1101 deltar(1,0)*vVbInvdeltar(1,0)+
1102 deltar(2,0)*vVbInvdeltar(2,0);
1103 chi2 += chi2b;
07680cae 1104 }
1105
1106
ec1be5d5 1107 // loop on tracks
1108 for(k=0; k<arrEntries; k++) {
ec1be5d5 1109 // get track from track array
1110 t = (AliESDtrack*)fTrkArray.At(k);
1111 alpha = t->GetAlpha();
1112 xlStart = initPos[0]*TMath::Cos(alpha)+initPos[1]*TMath::Sin(alpha);
fee9ef0d 1113 // to vtxSeed (from finder)
dc3719f3 1114 t->AliExternalTrackParam::PropagateTo(xlStart,GetFieldkG());
fee9ef0d 1115
1116
ec1be5d5 1117 // vector of track global coordinates
1118 TMatrixD ri(3,1);
fee9ef0d 1119 // covariance matrix of ri
1120 TMatrixD wWi(3,3);
1121
1122 // get space point from track
1123 if(!TrackToPoint(t,ri,wWi)) continue;
fee9ef0d 1124 TMatrixD wWiri(wWi,TMatrixD::kMult,ri);
ec1be5d5 1125
1126 // track chi2
1127 TMatrixD deltar = rv; deltar -= ri;
1128 TMatrixD wWideltar(wWi,TMatrixD::kMult,deltar);
1129 chi2i = deltar(0,0)*wWideltar(0,0)+
1130 deltar(1,0)*wWideltar(1,0)+
1131 deltar(2,0)*wWideltar(2,0);
1132
ec1be5d5 1133 // add to total chi2
1134 chi2 += chi2i;
1135
ec1be5d5 1136 sumWiri += wWiri;
1137 sumWi += wWi;
1138
1139 nUsedTrks++;
1140 } // end loop on tracks
1141
1142 if(nUsedTrks < fMinTracks) {
1143 failed=1;
1144 continue;
1145 }
1146
1147 Double_t determinant = sumWi.Determinant();
1148 //cerr<<" determinant: "<<determinant<<endl;
1149 if(determinant < 100.) {
1150 printf("det(V) = 0\n");
1151 failed=1;
1152 continue;
1153 }
1154
1155 // inverted of weights matrix
1156 TMatrixD invsumWi(TMatrixD::kInverted,sumWi);
1157 vV = invsumWi;
1158
1159 // position of primary vertex
1160 rv.Mult(vV,sumWiri);
1161
eae1677e 1162 } // end loop on the 2 steps
ec1be5d5 1163
5834e9a1 1164 // delete t;
ec1be5d5 1165
1166 if(failed) {
50ff0bcd 1167 if(fDebug) printf("TooFewTracks\n");
ec1be5d5 1168 fCurrentVertex = new AliESDVertex(0.,0.,-1);
1169 return;
1170 }
1171
1172 Double_t position[3];
1173 position[0] = rv(0,0);
1174 position[1] = rv(1,0);
1175 position[2] = rv(2,0);
1176 Double_t covmatrix[6];
1177 covmatrix[0] = vV(0,0);
1178 covmatrix[1] = vV(0,1);
1179 covmatrix[2] = vV(1,1);
1180 covmatrix[3] = vV(0,2);
1181 covmatrix[4] = vV(1,2);
1182 covmatrix[5] = vV(2,2);
1183
dc3719f3 1184 // for correct chi2/ndf, count constraint as additional "track"
1185 if(fConstraint) nUsedTrks++;
ec1be5d5 1186 // store data in the vertex object
1187 fCurrentVertex = new AliESDVertex(position,covmatrix,chi2,nUsedTrks);
1188
1189 if(fDebug) {
1190 printf(" VertexFitter(): finish\n");
1191 printf(" rv = ( %f , %f , %f )\n\n",rv(0,0),rv(1,0),rv(2,0));
1192 fCurrentVertex->PrintStatus();
1193 }
1194
1195 return;
1196}
50ff0bcd 1197//----------------------------------------------------------------------------
fee9ef0d 1198AliESDVertex* AliVertexerTracks::VertexForSelectedTracks(TTree *trkTree,Bool_t optUseFitter,Bool_t optPropagate)
1199{
eae1677e 1200//
50ff0bcd 1201// Return vertex from tracks in trkTree
eae1677e 1202//
1203
fee9ef0d 1204 SetConstraintOff();
1205
50ff0bcd 1206 // get tracks and propagate them to initial vertex position
fee9ef0d 1207 Int_t nTrksPrep = PrepareTracks(*trkTree,0);
0c69be49 1208 if(nTrksPrep < TMath::Max(2,fMinTracks) ) {
50ff0bcd 1209 if(fDebug) printf("TooFewTracks\n");
fee9ef0d 1210 fCurrentVertex = new AliESDVertex(0.,0.,-1);
1211 return fCurrentVertex;
50ff0bcd 1212 }
1213
fee9ef0d 1214 switch (fAlgo) {
1215 case 1: StrLinVertexFinderMinDist(1); break;
1216 case 2: StrLinVertexFinderMinDist(0); break;
1217 case 3: HelixVertexFinder(); break;
1218 case 4: VertexFinder(1); break;
1219 case 5: VertexFinder(0); break;
1220 default: printf("Wrong algorithm\n"); break;
1221 }
50ff0bcd 1222
fee9ef0d 1223 if(fDebug) printf(" Vertex finding completed\n");
1224
1225 // vertex fitter
1226 if(optUseFitter){
1227 VertexFitter(fConstraint);
1228 if(fDebug) printf(" Vertex fit completed\n");
1229 }else{
1230 Double_t position[3]={fVert.GetXv(),fVert.GetYv(),fVert.GetZv()};
146c29df 1231 Double_t covmatrix[6];
1232 fVert.GetCovMatrix(covmatrix);
fee9ef0d 1233 Double_t chi2=99999.;
1234 Int_t nUsedTrks=fVert.GetNContributors();
1235 fCurrentVertex = new AliESDVertex(position,covmatrix,chi2,nUsedTrks);
1236 }
1237 fCurrentVertex->SetDispersion(fVert.GetDispersion());
1238
1239
1240 // set indices of used tracks and propagate track to found vertex
50ff0bcd 1241 UShort_t *indices = 0;
1242 AliESDtrack *eta = 0;
fee9ef0d 1243 if(fCurrentVertex->GetNContributors()>0) {
1244 indices = new UShort_t[fCurrentVertex->GetNContributors()];
50ff0bcd 1245 for(Int_t jj=0;jj<(Int_t)fTrkArray.GetEntriesFast();jj++) {
1246 eta = (AliESDtrack*)fTrkArray.At(jj);
1247 indices[jj] = (UShort_t)eta->GetID();
fee9ef0d 1248 if(optPropagate&&optUseFitter){
1249 if(TMath::Sqrt(fCurrentVertex->GetXv()*fCurrentVertex->GetXv()+fCurrentVertex->GetYv()*fCurrentVertex->GetYv())<3.) {
dc3719f3 1250 eta->RelateToVertex(fCurrentVertex,GetFieldkG(),100.);
fee9ef0d 1251 if(fDebug) printf("Track %d propagated to found vertex\n",jj);
1252 }else{
1253 AliWarning("Found vertex outside beam pipe!");
1254 }
1255 }
50ff0bcd 1256 }
fee9ef0d 1257 fCurrentVertex->SetIndices(fCurrentVertex->GetNContributors(),indices);
50ff0bcd 1258 }
1259 delete [] indices;
1260 fTrkArray.Delete();
1261
fee9ef0d 1262 return fCurrentVertex;
eae1677e 1263}
50ff0bcd 1264//----------------------------------------------------------------------------
fee9ef0d 1265AliESDVertex* AliVertexerTracks::VertexForSelectedTracks(TObjArray *trkArray,Bool_t optUseFitter, Bool_t optPropagate)
1266{
50ff0bcd 1267//
1268// Return vertex from array of tracks
1269//
5834e9a1 1270
50ff0bcd 1271 // get tracks and propagate them to initial vertex position
1272 Int_t nTrks = trkArray->GetEntriesFast();
0c69be49 1273 if(nTrks < TMath::Max(2,fMinTracks) ) {
50ff0bcd 1274 if(fDebug) printf("TooFewTracks\n");
fee9ef0d 1275 fCurrentVertex = new AliESDVertex(0.,0.,-1);
1276 return fCurrentVertex;
50ff0bcd 1277 }
1278 TTree *trkTree = new TTree("TreeT","tracks");
1279 AliESDtrack *esdTrack = 0;
1280 trkTree->Branch("tracks","AliESDtrack",&esdTrack);
1281 for(Int_t i=0; i<nTrks; i++){
1282 esdTrack = (AliESDtrack*)trkArray->At(i);
1283 trkTree->Fill();
1284 }
1285
fee9ef0d 1286 AliESDVertex *vtx = VertexForSelectedTracks(trkTree,optUseFitter,optPropagate);
50ff0bcd 1287 delete trkTree;
1288 return vtx;
1289}
1290//--------------------------------------------------------------------------