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 <TObjArray.h>
31 #include <TRandom.h> // TEMPORARY !!!!!!!
32 //---- AliRoot headers -----
34 #include "AliKalmanTrack.h"
35 #include "AliITSStrLine.h"
36 #include "AliITStrackV2.h"
37 #include "AliITSVertex.h"
38 #include "AliITSVertexerTracks.h"
41 ClassImp(AliITSVertexerTracks)
44 //----------------------------------------------------------------------------
45 AliITSVertexerTracks::AliITSVertexerTracks():AliITSVertexer() {
47 // Default constructor
56 for(Int_t i=0; i<3; i++)fInitPos[i] = 0.;
59 //----------------------------------------------------------------------------
60 AliITSVertexerTracks::AliITSVertexerTracks(Double_t field, TString fn,
61 Double_t xStart,Double_t yStart,
65 // Standard constructor
69 SetVtxStart(xStart,yStart);
71 SetUseThrustFrame(useThFr);
75 for(Int_t i=0; i<3; i++)fInitPos[i] = 0.;
77 //----------------------------------------------------------------------------
78 Bool_t AliITSVertexerTracks::CheckField() const {
80 // Check if the conv. const. has been set
83 Double_t cc = t.GetConvConst();
84 Double_t field = 100./0.299792458/cc;
86 if(field<0.1 || field>0.6) {
87 printf("AliITSVertexerTracks::CheckField():\n ERROR: AliKalmanTrack::fConvConst not set\n Use AliKalmanTrack::SetConvConst() or AliITSVertexerTracks::SetField()\n");
90 printf("AliITSVertexerTracks::CheckField(): Using B = %3.1f T\n",field);
93 //---------------------------------------------------------------------------
94 void AliITSVertexerTracks::ComputeMaxChi2PerTrack(Int_t nTracks) {
96 // Max. contr. to the chi2 has been tuned as a function of multiplicity
98 if(nTracks < 7) { fMaxChi2PerTrack = 1.e6;
99 } else { fMaxChi2PerTrack = 100.; }
103 //---------------------------------------------------------------------------
104 void AliITSVertexerTracks::FindVertices() {
106 // Vertices for all events from fFirstEvent to fLastEvent
109 // Check if the conv. const. has been set
110 if(!CheckField()) return;
114 for(Int_t ev=fFirstEvent; ev<=fLastEvent; ev++) {
115 if(ev % 100 == 0 || fDebug) printf("--- Processing event %d of %d ---\n",ev,fLastEvent);
117 FindVertexForCurrentEvent(ev);
119 if(!fCurrentVertex) {
120 printf("AliITSVertexerTracks::FindVertixes(): no tracks tree for event %d\n",ev);
124 if(fDebug) fCurrentVertex->PrintStatus();
125 TString vtxName = "Vertex_";
127 // fCurrentVertex->SetName(vtxName.Data());
128 fCurrentVertex->SetTitle("VertexerTracks");
129 WriteCurrentVertex();
130 } // loop over events
134 //----------------------------------------------------------------------------
135 Int_t AliITSVertexerTracks::PrepareTracks(TTree &trkTree) {
137 // Propagate tracks to initial vertex position and store them in a TObjArray
139 Double_t maxd0rphi = 3.;
140 Double_t alpha,xlStart,d0rphi;
144 Int_t nEntries = (Int_t)trkTree.GetEntries();
146 if(!fTrkArray.IsEmpty()) fTrkArray.Clear();
147 fTrkArray.Expand(nEntries);
150 printf(" PrepareTracks()\n");
154 for(Int_t i=0; i<nEntries; i++) {
155 // check tracks to skip
157 for(Int_t j=0; j<fNTrksToSkip; j++) {
158 if(i==fTrksToSkip[j]) {
159 if(fDebug) printf("skipping track: %d\n",i);
163 if(skipThis) continue;
165 AliITStrackV2 *itstrack = new AliITStrackV2;
166 trkTree.SetBranchAddress("tracks",&itstrack);
170 // propagate track to vtxSeed
171 alpha = itstrack->GetAlpha();
172 xlStart = fNominalPos[0]*TMath::Cos(alpha)+fNominalPos[1]*TMath::Sin(alpha);
173 itstrack->PropagateTo(3.,0.0023,65.19); // to beam pipe (0.8 mm of Be)
174 itstrack->PropagateTo(xlStart,0.,0.); // to vtxSeed
176 // select tracks with d0rphi < maxd0rphi
177 d0rphi = TMath::Abs(itstrack->GetD(fNominalPos[0],fNominalPos[1]));
178 if(d0rphi > maxd0rphi) { delete itstrack; continue; }
180 fTrkArray.AddLast(itstrack);
185 if(fTrksToSkip) delete [] fTrksToSkip;
189 //----------------------------------------------------------------------------
190 void AliITSVertexerTracks::PrintStatus() const {
194 printf(" Initial position (%f,%f)\n",fNominalPos[0],fNominalPos[1]);
195 printf(" Vertex position after vertex finder (%f, %f, %f)\n",fInitPos[0],fInitPos[1],fInitPos[2]);
196 printf(" Number of tracks in array: %d\n",(Int_t)fTrkArray.GetEntriesFast());
197 printf(" Minimum # tracks required in fit: %d\n",fMinTracks);
198 printf(" Using Thrust Frame: %d fPhiThrust = %f\n",fUseThrustFrame,fPhiThrust);
202 //----------------------------------------------------------------------------
203 AliITSVertex* AliITSVertexerTracks::FindVertexForCurrentEvent(Int_t evnumb) {
205 // Vertex for current event
209 // get tree with tracks from input file
210 TString treeName = "TreeT_ITS_";
212 // TTree *trkTree=(TTree*)fInFile->Get(treeName.Data()); masera
214 if(!trkTree) return fCurrentVertex;
217 // get tracks and propagate them to initial vertex position
218 Int_t nTrks = PrepareTracks(*trkTree);
220 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
221 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
227 ComputeMaxChi2PerTrack(nTrks);
228 if(fUseThrustFrame) ThrustFinderXY();
229 if(fDebug) printf(" thrust found: phi = %f\n",fPhiThrust);
231 if(fDebug) printf(" vertex fit completed\n");
236 // fCurrentVertex->SetName(vtxName.Data());
237 return fCurrentVertex;
239 //---------------------------------------------------------------------------
240 void AliITSVertexerTracks::SetSkipTracks(Int_t n,Int_t *skipped) {
242 // Mark the tracks not ot be used in the vertex finding
245 fTrksToSkip = new Int_t[n];
246 for(Int_t i=0;i<n;i++) fTrksToSkip[i] = skipped[i];
249 //----------------------------------------------------------------------------
250 Double_t AliITSVertexerTracks::SumPl(TTree &momTree,Double_t phi) const {
252 // Function to be maximized for thrust determination
262 momTree.SetBranchAddress("momenta",&mom);
263 Int_t entries = (Int_t)momTree.GetEntries();
265 for(Int_t i=0; i<entries; i++) {
275 //---------------------------------------------------------------------------
276 void AliITSVertexerTracks::ThrustFinderXY() {
278 // This function looks for the thrust direction, \vec{u}, in the (x,y) plane.
279 // The following function is maximized:
280 // \Sum_{\vec{p}\cdot\vec{u}} \vec{p}\cdot\vec{u} / \Sum |\vec{p}|
281 // where \vec{p} = (p_x,p_y)
283 Double_t pt,alpha,phi;
286 // tree for thrust determination
287 TVector3 *ioMom = new TVector3;
288 TTree *t = new TTree("Tree_Momenta","Tree with momenta");
289 t->Branch("momenta","TVector3",&ioMom);
292 AliITStrackV2 *itstrack = 0;
293 Int_t arrEntries = (Int_t)fTrkArray.GetEntries();
296 for(Int_t i=0; i<arrEntries; i++) {
297 itstrack = (AliITStrackV2*)fTrkArray.At(i);
298 // momentum of the track at the vertex
299 pt = 1./TMath::Abs(itstrack->Get1Pt());
300 alpha = itstrack->GetAlpha();
301 phi = alpha+TMath::ASin(itstrack->GetSnp());
302 ioMom->SetX(pt*TMath::Cos(phi));
303 ioMom->SetY(pt*TMath::Sin(phi));
306 totPt += ioMom->Pt();
308 } // end loop on tracks
310 Double_t tValue=0.,tPhi=0.;
311 Double_t maxSumPl = 0.;
318 dPhi = 2.*TMath::Pi()/(Double_t)nSteps;
320 for(iStep=0; iStep<nSteps; iStep++) {
322 thisSumPl = SumPl(*t,phi);
323 if(thisSumPl > maxSumPl) {
324 maxSumPl = thisSumPl;
333 for(iStep=0; iStep<nSteps; iStep++) {
335 thisSumPl = SumPl(*t,phi);
336 if(thisSumPl > maxSumPl) {
337 maxSumPl = thisSumPl;
342 tValue = 2.*maxSumPl/totPt;
343 if(tPhi<0.) tPhi += 2.*TMath::Pi();
344 if(tPhi>2.*TMath::Pi()) tPhi -= 2.*TMath::Pi();
353 //---------------------------------------------------------------------------
354 void AliITSVertexerTracks::TooFewTracks() {
356 // When the number of tracks is < fMinTracks the vertex is set to (0,0,0)
357 // and the number of tracks to -1
359 fCurrentVertex = new AliITSVertex(0.,0.,-1);
362 //---------------------------------------------------------------------------
363 void AliITSVertexerTracks::VertexFinder() {
365 // Get estimate of vertex position in (x,y) from tracks DCA
366 // Then this estimate is stored to the data member fInitPos
367 // (previous values are overwritten)
372 ******* TEMPORARY!!! FOR TEST ONLY!!! **********************************
374 fInitPos[0] = fNominalPos[0]+gRandom->Gaus(0.,0.0100); // 100 micron gaussian smearing
375 fInitPos[1] = fNominalPos[1]+gRandom->Gaus(0.,0.0100); // 100 micron gaussian smearing
379 for(Int_t i=0;i<2;i++)fInitPos[i]=fNominalPos[i];
381 Int_t nacc = (Int_t)fTrkArray.GetEntriesFast();
383 Double_t aver[3]={0.,0.,0.};
385 AliITStrackV2 *track1;
386 AliITStrackV2 *track2;
387 for(Int_t i=0; i<nacc; i++){
388 track1 = (AliITStrackV2*)fTrkArray.At(i);
391 track1->GetExternalParameters(xv,par);
392 cout<<"Track in position "<<i<<" xr= "<<xv<<endl;
393 for(Int_t ii=0;ii<5;ii++)cout<<par[ii]<<" ";
397 Double_t alpha = track1->GetAlpha();
398 Double_t azim = TMath::ASin(track1->GetSnp())+alpha;
399 Double_t theta = TMath::Pi()/2. - TMath::ATan(track1->GetTgl());
400 mom1[0] = TMath::Sin(theta)*TMath::Cos(azim);
401 mom1[1] = TMath::Sin(theta)*TMath::Sin(azim);
402 mom1[2] = TMath::Cos(theta);
405 Double_t mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
406 track1->GetGlobalXYZat(mindist,pos1[0],pos1[1],pos1[2]);
407 AliITSStrLine *line1 = new AliITSStrLine(pos1,mom1);
408 for(Int_t j=i+1; j<nacc; j++){
409 track2 = (AliITStrackV2*)fTrkArray.At(j);
411 alpha = track2->GetAlpha();
412 azim = TMath::ASin(track2->GetSnp())+alpha;
413 theta = TMath::Pi()/2. - TMath::ATan(track2->GetTgl());
414 mom2[0] = TMath::Sin(theta)*TMath::Cos(azim);
415 mom2[1] = TMath::Sin(theta)*TMath::Sin(azim);
416 mom2[2] = TMath::Cos(theta);
418 mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
419 track2->GetGlobalXYZat(mindist,pos2[0],pos2[1],pos2[2]);
420 AliITSStrLine *line2 = new AliITSStrLine(pos2,mom2);
421 Double_t crosspoint[3];
422 Int_t retcode = line2->Cross(line1,crosspoint);
424 if(fDebug>10)cout<<" i= "<<i<<", j= "<<j<<endl;
425 if(fDebug>10)cout<<"bad intersection\n";
426 line1->PrintStatus();
427 line2->PrintStatus();
431 for(Int_t jj=0;jj<3;jj++)aver[jj]+=crosspoint[jj];
432 if(fDebug>10)cout<<" i= "<<i<<", j= "<<j<<endl;
433 if(fDebug>10)cout<<"\n Cross point: ";
434 if(fDebug>10)cout<<crosspoint[0]<<" "<<crosspoint[1]<<" "<<crosspoint[2]<<endl;
441 for(Int_t jj=0;jj<3;jj++)fInitPos[jj] = aver[jj]/ncombi;
444 Warning("VertexFinder","Finder did not succed");
448 //************************************************************************
453 //---------------------------------------------------------------------------
454 void AliITSVertexerTracks::VertexFitter() {
456 // The optimal estimate of the vertex position is given by a "weighted
457 // average of tracks positions"
458 // Original method: CMS Note 97/0051
461 printf(" VertexFitter(): start\n");
469 rv(0,0) = fInitPos[0];
470 rv(1,0) = fInitPos[1];
472 Double_t xlStart,alpha;
474 Double_t cosRot,sinRot;
478 Int_t arrEntries = (Int_t)fTrkArray.GetEntries();
479 AliITStrackV2 *t = 0;
482 Int_t *skipTrack = new Int_t[arrEntries];
483 for(i=0; i<arrEntries; i++) skipTrack[i]=0;
486 // 1st - first estimate of vtx using all tracks
487 // 2nd - apply cut on chi2 max per track
488 // 3rd - estimate of global chi2
489 for(step=0; step<3; step++) {
490 if(fDebug) printf(" step = %d\n",step);
494 TMatrixD SumWiri(3,1);
498 for(j=0; j<3; j++) SumWi(j,i) = 0.;
502 for(k=0; k<arrEntries; k++) {
503 if(skipTrack[k]) continue;
504 // get track from track array
505 t = (AliITStrackV2*)fTrkArray.At(k);
506 alpha = t->GetAlpha();
507 xlStart = fInitPos[0]*TMath::Cos(alpha)+fInitPos[1]*TMath::Sin(alpha);
508 t->PropagateTo(xlStart,0.,0.); // to vtxSeed
509 rotAngle = alpha-fPhiThrust;
510 if(alpha<0.) rotAngle += 2.*TMath::Pi();
511 cosRot = TMath::Cos(rotAngle);
512 sinRot = TMath::Sin(rotAngle);
514 // vector of track global coordinates
516 ri(0,0) = t->GetX()*cosRot-t->GetY()*sinRot;
517 ri(1,0) = t->GetX()*sinRot+t->GetY()*cosRot;
520 // matrix to go from global (x,y,z) to local (y,z);
529 // covariance matrix of local (y,z) - inverted
531 t->GetExternalCovariance(cc);
537 // weights matrix: Wi = QiT * UiInv * Qi
538 if(Ui.Determinant() <= 0.) continue;
539 TMatrixD UiInv(TMatrixD::kInverted,Ui);
540 TMatrixD UiInvQi(UiInv,TMatrixD::kMult,Qi);
541 TMatrixD Wi(Qi,TMatrixD::kTransposeMult,UiInvQi);
544 TMatrixD deltar = rv; deltar -= ri;
545 TMatrixD Wideltar(Wi,TMatrixD::kMult,deltar);
546 chi2i = deltar(0,0)*Wideltar(0,0)+
547 deltar(1,0)*Wideltar(1,0)+
548 deltar(2,0)*Wideltar(2,0);
551 if(step==1 && chi2i > fMaxChi2PerTrack) {
559 TMatrixD Wiri(Wi,TMatrixD::kMult,ri);
565 } // end loop on tracks
567 if(nUsedTrks < fMinTracks) {
572 Double_t determinant = SumWi.Determinant();
573 //cerr<<" determinant: "<<determinant<<endl;
574 if(determinant < 100.) {
575 printf("det(V) = 0\n");
580 // inverted of weights matrix
581 TMatrixD InvSumWi(TMatrixD::kInverted,SumWi);
584 // position of primary vertex
587 } // end loop on the 3 steps
597 Double_t position[3];
598 position[0] = rv(0,0);
599 position[1] = rv(1,0);
600 position[2] = rv(2,0);
601 Double_t covmatrix[6];
602 covmatrix[0] = V(0,0);
603 covmatrix[1] = V(0,1);
604 covmatrix[2] = V(1,1);
605 covmatrix[3] = V(0,2);
606 covmatrix[4] = V(1,2);
607 covmatrix[5] = V(2,2);
609 // store data in the vertex object
610 fCurrentVertex = new AliITSVertex(fPhiThrust,position,covmatrix,chi2,nUsedTrks);
613 printf(" VertexFitter(): finish\n");
614 printf(" rv = ( %f , %f , %f )\n\n",rv(0,0),rv(1,0),rv(2,0));
615 fCurrentVertex->PrintStatus();
620 //----------------------------------------------------------------------------
621 AliITSVertex *AliITSVertexerTracks::VertexOnTheFly(TTree &trkTree) {
623 // Return vertex from tracks in trkTree
625 if(fCurrentVertex) fCurrentVertex = 0;
627 // get tracks and propagate them to initial vertex position
628 Int_t nTrks = PrepareTracks(*(&trkTree));
629 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
630 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
636 ComputeMaxChi2PerTrack(nTrks);
637 if(fUseThrustFrame) ThrustFinderXY();
638 if(fDebug) printf(" thrust found: phi = %f\n",fPhiThrust);
640 if(fDebug) printf(" vertex fit completed\n");
642 return fCurrentVertex;
644 //----------------------------------------------------------------------------