1 /**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 //-----------------------------------------------------------------
17 // Implementation of the vertexer from tracks
19 // Origin: A.Dainese, Padova, andrea.dainese@pd.infn.it
20 // M.Masera, Torino, massimo.masera@to.infn.it
21 //-----------------------------------------------------------------
23 //---- standard headers ----
24 #include <Riostream.h>
25 //---- Root headers --------
30 #include <TRandom.h> // TEMPORARY !!!!!!!
31 //---- AliRoot headers -----
33 #include "AliKalmanTrack.h"
34 #include "AliITSStrLine.h"
35 #include "AliITStrackV2.h"
36 #include "AliITSVertex.h"
37 #include "AliITSVertexerTracks.h"
40 ClassImp(AliITSVertexerTracks)
43 //----------------------------------------------------------------------------
44 AliITSVertexerTracks::AliITSVertexerTracks():AliITSVertexer() {
46 // Default constructor
55 for(Int_t i=0; i<3; i++)fInitPos[i] = 0.;
58 //----------------------------------------------------------------------------
59 AliITSVertexerTracks::AliITSVertexerTracks(Double_t field, TString fn,
60 Double_t xStart,Double_t yStart,
64 // Standard constructor
68 SetVtxStart(xStart,yStart);
70 SetUseThrustFrame(useThFr);
74 for(Int_t i=0; i<3; i++)fInitPos[i] = 0.;
76 //----------------------------------------------------------------------------
77 Bool_t AliITSVertexerTracks::CheckField() const {
79 // Check if the conv. const. has been set
82 Double_t cc = t.GetConvConst();
83 Double_t field = 100./0.299792458/cc;
85 if(field<0.1 || field>0.6) {
86 printf("AliITSVertexerTracks::CheckField():\n ERROR: AliKalmanTrack::fConvConst not set\n Use AliKalmanTrack::SetConvConst() or AliITSVertexerTracks::SetField()\n");
89 printf("AliITSVertexerTracks::CheckField(): Using B = %3.1f T\n",field);
92 //---------------------------------------------------------------------------
93 void AliITSVertexerTracks::ComputeMaxChi2PerTrack(Int_t nTracks) {
95 // Max. contr. to the chi2 has been tuned as a function of multiplicity
97 if(nTracks < 7) { fMaxChi2PerTrack = 1.e6;
98 } else { fMaxChi2PerTrack = 100.; }
102 //---------------------------------------------------------------------------
103 void AliITSVertexerTracks::FindVertices() {
105 // Vertices for all events from fFirstEvent to fLastEvent
108 // Check if the conv. const. has been set
109 if(!CheckField()) return;
113 for(Int_t ev=fFirstEvent; ev<=fLastEvent; ev++) {
114 if(ev % 100 == 0 || fDebug) printf("--- Processing event %d of %d ---\n",ev,fLastEvent);
116 FindVertexForCurrentEvent(ev);
118 if(!fCurrentVertex) {
119 printf("AliITSVertexerTracks::FindVertixes(): no tracks tree for event %d\n",ev);
123 if(fDebug) fCurrentVertex->PrintStatus();
124 TString vtxName = "Vertex_";
126 // fCurrentVertex->SetName(vtxName.Data());
127 fCurrentVertex->SetTitle("VertexerTracks");
128 WriteCurrentVertex();
129 } // loop over events
133 //----------------------------------------------------------------------------
134 Int_t AliITSVertexerTracks::PrepareTracks(TTree &trkTree) {
136 // Propagate tracks to initial vertex position and store them in a TObjArray
138 Double_t maxd0rphi = 3.;
139 Double_t alpha,xlStart,d0rphi;
143 Int_t nEntries = (Int_t)trkTree.GetEntries();
145 if(!fTrkArray.IsEmpty()) fTrkArray.Clear();
146 fTrkArray.Expand(nEntries);
149 printf(" PrepareTracks()\n");
153 for(Int_t i=0; i<nEntries; i++) {
154 // check tracks to skip
156 for(Int_t j=0; j<fNTrksToSkip; j++) {
157 if(i==fTrksToSkip[j]) {
158 if(fDebug) printf("skipping track: %d\n",i);
162 if(skipThis) continue;
164 AliITStrackV2 *itstrack = new AliITStrackV2;
165 trkTree.SetBranchAddress("tracks",&itstrack);
169 // propagate track to vtxSeed
170 alpha = itstrack->GetAlpha();
171 xlStart = fNominalPos[0]*TMath::Cos(alpha)+fNominalPos[1]*TMath::Sin(alpha);
172 itstrack->PropagateTo(3.,0.0023,65.19); // to beam pipe (0.8 mm of Be)
173 itstrack->PropagateTo(xlStart,0.,0.); // to vtxSeed
175 // select tracks with d0rphi < maxd0rphi
176 d0rphi = TMath::Abs(itstrack->GetD(fNominalPos[0],fNominalPos[1]));
177 if(d0rphi > maxd0rphi) { delete itstrack; continue; }
179 fTrkArray.AddLast(itstrack);
184 if(fTrksToSkip) delete [] fTrksToSkip;
188 //----------------------------------------------------------------------------
189 void AliITSVertexerTracks::PrintStatus() const {
193 printf(" Initial position (%f,%f)\n",fNominalPos[0],fNominalPos[1]);
194 printf(" Vertex position after vertex finder (%f, %f, %f)\n",fInitPos[0],fInitPos[1],fInitPos[2]);
195 printf(" Number of tracks in array: %d\n",(Int_t)fTrkArray.GetEntriesFast());
196 printf(" Minimum # tracks required in fit: %d\n",fMinTracks);
197 printf(" Using Thrust Frame: %d fPhiThrust = %f\n",fUseThrustFrame,fPhiThrust);
201 //----------------------------------------------------------------------------
202 AliITSVertex* AliITSVertexerTracks::FindVertexForCurrentEvent(Int_t evnumb) {
204 // Vertex for current event
208 // get tree with tracks from input file
209 TString treeName = "TreeT_ITS_";
211 // TTree *trkTree=(TTree*)fInFile->Get(treeName.Data()); masera
213 if(!trkTree) return fCurrentVertex;
216 // get tracks and propagate them to initial vertex position
217 Int_t nTrks = PrepareTracks(*trkTree);
219 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
220 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
226 ComputeMaxChi2PerTrack(nTrks);
227 if(fUseThrustFrame) ThrustFinderXY();
228 if(fDebug) printf(" thrust found: phi = %f\n",fPhiThrust);
230 if(fDebug) printf(" vertex fit completed\n");
235 // fCurrentVertex->SetName(vtxName.Data());
236 return fCurrentVertex;
238 //---------------------------------------------------------------------------
239 void AliITSVertexerTracks::SetSkipTracks(Int_t n,Int_t *skipped) {
241 // Mark the tracks not ot be used in the vertex finding
244 fTrksToSkip = new Int_t[n];
245 for(Int_t i=0;i<n;i++) fTrksToSkip[i] = skipped[i];
248 //----------------------------------------------------------------------------
249 Double_t AliITSVertexerTracks::SumPl(TTree &momTree,Double_t phi) const {
251 // Function to be maximized for thrust determination
261 momTree.SetBranchAddress("momenta",&mom);
262 Int_t entries = (Int_t)momTree.GetEntries();
264 for(Int_t i=0; i<entries; i++) {
274 //---------------------------------------------------------------------------
275 void AliITSVertexerTracks::ThrustFinderXY() {
277 // This function looks for the thrust direction, \vec{u}, in the (x,y) plane.
278 // The following function is maximized:
279 // \Sum_{\vec{p}\cdot\vec{u}} \vec{p}\cdot\vec{u} / \Sum |\vec{p}|
280 // where \vec{p} = (p_x,p_y)
282 Double_t pt,alpha,phi;
285 // tree for thrust determination
286 TVector3 *ioMom = new TVector3;
287 TTree *t = new TTree("Tree_Momenta","Tree with momenta");
288 t->Branch("momenta","TVector3",&ioMom);
291 AliITStrackV2 *itstrack = 0;
292 Int_t arrEntries = (Int_t)fTrkArray.GetEntries();
295 for(Int_t i=0; i<arrEntries; i++) {
296 itstrack = (AliITStrackV2*)fTrkArray.At(i);
297 // momentum of the track at the vertex
298 pt = 1./TMath::Abs(itstrack->Get1Pt());
299 alpha = itstrack->GetAlpha();
300 phi = alpha+TMath::ASin(itstrack->GetSnp());
301 ioMom->SetX(pt*TMath::Cos(phi));
302 ioMom->SetY(pt*TMath::Sin(phi));
305 totPt += ioMom->Pt();
307 } // end loop on tracks
309 Double_t tValue=0.,tPhi=0.;
310 Double_t maxSumPl = 0.;
317 dPhi = 2.*TMath::Pi()/(Double_t)nSteps;
319 for(iStep=0; iStep<nSteps; iStep++) {
321 thisSumPl = SumPl(*t,phi);
322 if(thisSumPl > maxSumPl) {
323 maxSumPl = thisSumPl;
332 for(iStep=0; iStep<nSteps; iStep++) {
334 thisSumPl = SumPl(*t,phi);
335 if(thisSumPl > maxSumPl) {
336 maxSumPl = thisSumPl;
341 tValue = 2.*maxSumPl/totPt;
342 if(tPhi<0.) tPhi += 2.*TMath::Pi();
343 if(tPhi>2.*TMath::Pi()) tPhi -= 2.*TMath::Pi();
352 //---------------------------------------------------------------------------
353 void AliITSVertexerTracks::TooFewTracks() {
355 // When the number of tracks is < fMinTracks the vertex is set to (0,0,0)
356 // and the number of tracks to -1
358 fCurrentVertex = new AliITSVertex(0.,0.,-1);
361 //---------------------------------------------------------------------------
362 void AliITSVertexerTracks::VertexFinder() {
364 // Get estimate of vertex position in (x,y) from tracks DCA
365 // Then this estimate is stored to the data member fInitPos
366 // (previous values are overwritten)
371 ******* TEMPORARY!!! FOR TEST ONLY!!! **********************************
373 fInitPos[0] = fNominalPos[0]+gRandom->Gaus(0.,0.0100); // 100 micron gaussian smearing
374 fInitPos[1] = fNominalPos[1]+gRandom->Gaus(0.,0.0100); // 100 micron gaussian smearing
378 for(Int_t i=0;i<2;i++)fInitPos[i]=fNominalPos[i];
380 Int_t nacc = (Int_t)fTrkArray.GetEntriesFast();
382 Double_t aver[3]={0.,0.,0.};
384 AliITStrackV2 *track1;
385 AliITStrackV2 *track2;
386 for(Int_t i=0; i<nacc; i++){
387 track1 = (AliITStrackV2*)fTrkArray.At(i);
390 track1->GetExternalParameters(xv,par);
391 cout<<"Track in position "<<i<<" xr= "<<xv<<endl;
392 for(Int_t ii=0;ii<5;ii++)cout<<par[ii]<<" ";
396 Double_t alpha = track1->GetAlpha();
397 Double_t azim = TMath::ASin(track1->GetSnp())+alpha;
398 Double_t theta = TMath::Pi()/2. - TMath::ATan(track1->GetTgl());
399 mom1[0] = TMath::Sin(theta)*TMath::Cos(azim);
400 mom1[1] = TMath::Sin(theta)*TMath::Sin(azim);
401 mom1[2] = TMath::Cos(theta);
404 Double_t mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
405 track1->GetGlobalXYZat(mindist,pos1[0],pos1[1],pos1[2]);
406 AliITSStrLine *line1 = new AliITSStrLine(pos1,mom1);
407 for(Int_t j=i+1; j<nacc; j++){
408 track2 = (AliITStrackV2*)fTrkArray.At(j);
410 alpha = track2->GetAlpha();
411 azim = TMath::ASin(track2->GetSnp())+alpha;
412 theta = TMath::Pi()/2. - TMath::ATan(track2->GetTgl());
413 mom2[0] = TMath::Sin(theta)*TMath::Cos(azim);
414 mom2[1] = TMath::Sin(theta)*TMath::Sin(azim);
415 mom2[2] = TMath::Cos(theta);
417 mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
418 track2->GetGlobalXYZat(mindist,pos2[0],pos2[1],pos2[2]);
419 AliITSStrLine *line2 = new AliITSStrLine(pos2,mom2);
420 Double_t crosspoint[3];
421 Int_t retcode = line2->Cross(line1,crosspoint);
423 if(fDebug>10)cout<<" i= "<<i<<", j= "<<j<<endl;
424 if(fDebug>10)cout<<"bad intersection\n";
425 line1->PrintStatus();
426 line2->PrintStatus();
430 for(Int_t jj=0;jj<3;jj++)aver[jj]+=crosspoint[jj];
431 if(fDebug>10)cout<<" i= "<<i<<", j= "<<j<<endl;
432 if(fDebug>10)cout<<"\n Cross point: ";
433 if(fDebug>10)cout<<crosspoint[0]<<" "<<crosspoint[1]<<" "<<crosspoint[2]<<endl;
440 for(Int_t jj=0;jj<3;jj++)fInitPos[jj] = aver[jj]/ncombi;
443 Warning("VertexFinder","Finder did not succed");
447 //************************************************************************
452 //---------------------------------------------------------------------------
453 void AliITSVertexerTracks::VertexFitter() {
455 // The optimal estimate of the vertex position is given by a "weighted
456 // average of tracks positions"
457 // Original method: CMS Note 97/0051
460 printf(" VertexFitter(): start\n");
468 rv(0,0) = fInitPos[0];
469 rv(1,0) = fInitPos[1];
471 Double_t xlStart,alpha;
473 Double_t cosRot,sinRot;
477 Int_t arrEntries = (Int_t)fTrkArray.GetEntries();
478 AliITStrackV2 *t = 0;
481 Int_t *skipTrack = new Int_t[arrEntries];
482 for(i=0; i<arrEntries; i++) skipTrack[i]=0;
485 // 1st - first estimate of vtx using all tracks
486 // 2nd - apply cut on chi2 max per track
487 // 3rd - estimate of global chi2
488 for(step=0; step<3; step++) {
489 if(fDebug) printf(" step = %d\n",step);
493 TMatrixD SumWiri(3,1);
497 for(j=0; j<3; j++) SumWi(j,i) = 0.;
501 for(k=0; k<arrEntries; k++) {
502 if(skipTrack[k]) continue;
503 // get track from track array
504 t = (AliITStrackV2*)fTrkArray.At(k);
505 alpha = t->GetAlpha();
506 xlStart = fInitPos[0]*TMath::Cos(alpha)+fInitPos[1]*TMath::Sin(alpha);
507 t->PropagateTo(xlStart,0.,0.); // to vtxSeed
508 rotAngle = alpha-fPhiThrust;
509 if(alpha<0.) rotAngle += 2.*TMath::Pi();
510 cosRot = TMath::Cos(rotAngle);
511 sinRot = TMath::Sin(rotAngle);
513 // vector of track global coordinates
515 ri(0,0) = t->GetX()*cosRot-t->GetY()*sinRot;
516 ri(1,0) = t->GetX()*sinRot+t->GetY()*cosRot;
519 // matrix to go from global (x,y,z) to local (y,z);
528 // covariance matrix of local (y,z) - inverted
530 t->GetExternalCovariance(cc);
536 // weights matrix: Wi = QiT * UiInv * Qi
537 if(Ui.Determinant() <= 0.) continue;
538 TMatrixD UiInv(TMatrixD::kInverted,Ui);
539 TMatrixD UiInvQi(UiInv,TMatrixD::kMult,Qi);
540 TMatrixD Wi(Qi,TMatrixD::kTransposeMult,UiInvQi);
543 TMatrixD deltar = rv; deltar -= ri;
544 TMatrixD Wideltar(Wi,TMatrixD::kMult,deltar);
545 chi2i = deltar(0,0)*Wideltar(0,0)+
546 deltar(1,0)*Wideltar(1,0)+
547 deltar(2,0)*Wideltar(2,0);
550 if(step==1 && chi2i > fMaxChi2PerTrack) {
558 TMatrixD Wiri(Wi,TMatrixD::kMult,ri);
564 } // end loop on tracks
566 if(nUsedTrks < fMinTracks) {
571 Double_t determinant = SumWi.Determinant();
572 //cerr<<" determinant: "<<determinant<<endl;
573 if(determinant < 100.) {
574 printf("det(V) = 0\n");
579 // inverted of weights matrix
580 TMatrixD InvSumWi(TMatrixD::kInverted,SumWi);
583 // position of primary vertex
586 } // end loop on the 3 steps
596 Double_t position[3];
597 position[0] = rv(0,0);
598 position[1] = rv(1,0);
599 position[2] = rv(2,0);
600 Double_t covmatrix[6];
601 covmatrix[0] = V(0,0);
602 covmatrix[1] = V(0,1);
603 covmatrix[2] = V(1,1);
604 covmatrix[3] = V(0,2);
605 covmatrix[4] = V(1,2);
606 covmatrix[5] = V(2,2);
608 // store data in the vertex object
609 fCurrentVertex = new AliITSVertex(fPhiThrust,position,covmatrix,chi2,nUsedTrks);
612 printf(" VertexFitter(): finish\n");
613 printf(" rv = ( %f , %f , %f )\n\n",rv(0,0),rv(1,0),rv(2,0));
614 fCurrentVertex->PrintStatus();
619 //----------------------------------------------------------------------------
620 AliITSVertex *AliITSVertexerTracks::VertexOnTheFly(TTree &trkTree) {
622 // Return vertex from tracks in trkTree
624 if(fCurrentVertex) fCurrentVertex = 0;
626 // get tracks and propagate them to initial vertex position
627 Int_t nTrks = PrepareTracks(*(&trkTree));
628 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
629 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
635 ComputeMaxChi2PerTrack(nTrks);
636 if(fUseThrustFrame) ThrustFinderXY();
637 if(fDebug) printf(" thrust found: phi = %f\n",fPhiThrust);
639 if(fDebug) printf(" vertex fit completed\n");
641 return fCurrentVertex;
643 //----------------------------------------------------------------------------