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
18 // It accepts V2 and ESD tracks
20 // Origin: A.Dainese, Padova,
21 // andrea.dainese@pd.infn.it
23 // massimo.masera@to.infn.it
24 //-----------------------------------------------------------------
26 //---- standard headers ----
27 #include <Riostream.h>
28 //---- Root headers --------
33 //---- AliRoot headers -----
34 #include "AliITSStrLine.h"
35 #include "AliITStrackV2.h"
36 #include "AliESDVertex.h"
37 #include "AliITSVertexerTracks.h"
39 #include "AliESDtrack.h"
42 ClassImp(AliITSVertexerTracks)
45 //----------------------------------------------------------------------------
46 AliITSVertexerTracks::AliITSVertexerTracks():AliITSVertexer() {
48 // Default constructor
56 for(Int_t i=0; i<3; i++)fInitPos[i] = 0.;
58 //----------------------------------------------------------------------------
59 AliITSVertexerTracks::AliITSVertexerTracks(TFile *inFile,TFile *outFile,
62 Double_t xStart,Double_t yStart) {
64 // Standard constructor
72 SetVtxStart(xStart,yStart);
76 for(Int_t i=0; i<3; i++) fInitPos[i] = 0.;
79 //----------------------------------------------------------------------------
80 AliITSVertexerTracks::AliITSVertexerTracks(Double_t field, TString fn,
81 Double_t xStart,Double_t yStart)
84 // Alternative constructor
89 SetVtxStart(xStart,yStart);
93 for(Int_t i=0; i<3; i++) fInitPos[i] = 0.;
95 //______________________________________________________________________
96 AliITSVertexerTracks::AliITSVertexerTracks(const AliITSVertexerTracks &vtxr) : AliITSVertexer(vtxr) {
98 // Copies are not allowed. The method is protected to avoid misuse.
99 Error("AliITSVertexerTracks","Copy constructor not allowed\n");
102 //______________________________________________________________________
103 AliITSVertexerTracks& AliITSVertexerTracks::operator=(const AliITSVertexerTracks& /* vtxr */){
104 // Assignment operator
105 // Assignment is not allowed. The method is protected to avoid misuse.
106 Error("= operator","Assignment operator not allowed\n");
110 //-----------------------------------------------------------------------------
111 AliITSVertexerTracks::~AliITSVertexerTracks() {
112 // Default Destructor
113 // The objects poited by the following pointers are not owned
114 // by this class and are not deleted
120 //-----------------------------------------------------------------------------
121 Bool_t AliITSVertexerTracks::CheckField() const {
123 // Check if the conv. const. has been set
126 Double_t cc = t.GetConvConst();
127 Double_t field = 100./0.299792458/cc;
129 if(field<0.1 || field>0.6) {
130 printf("AliITSVertexerTracks::CheckField():\n ERROR: AliKalmanTrack::fConvConst not set\n Use AliKalmanTrack::SetConvConst() or AliITSVertexerTracks::SetField()\n");
133 printf("AliITSVertexerTracks::CheckField(): Using B = %3.1f T\n",field);
136 //---------------------------------------------------------------------------
137 void AliITSVertexerTracks::ComputeMaxChi2PerTrack(Int_t nTracks) {
139 // Max. contr. to the chi2 has been tuned as a function of multiplicity
141 if(nTracks < 7) { fMaxChi2PerTrack = 1.e6;
142 } else { fMaxChi2PerTrack = 100.; }
146 //---------------------------------------------------------------------------
147 void AliITSVertexerTracks::FindVertices() {
149 // Vertices for all events from fFirstEvent to fLastEvent
152 // Check if the conv. const. has been set
153 if(!CheckField()) return;
155 TDirectory *curdir = 0;
158 for(Int_t ev=fFirstEvent; ev<=fLastEvent; ev++) {
159 if(ev % 100 == 0 || fDebug) printf("--- Processing event %d of %d ---\n",ev,fLastEvent);
161 FindVertexForCurrentEvent(ev);
163 if(!fCurrentVertex) {
164 printf("AliITSVertexerTracks::FindVertixes(): no tracks tree for event %d\n",ev);
168 if(fDebug) fCurrentVertex->PrintStatus();
170 // write vertex to file
171 TString vtxName = "Vertex_";
173 fCurrentVertex->SetName(vtxName.Data());
174 fCurrentVertex->SetTitle("VertexerTracks");
175 //WriteCurrentVertex();
178 fCurrentVertex->Write();
181 } // loop over events
185 //---------------------------------------------------------------------------
186 void AliITSVertexerTracks::FindVerticesESD() {
188 // Vertices for all events from fFirstEvent to fLastEvent
191 // Check if the conv. const. has been set
192 if(!CheckField()) return;
194 TDirectory *curdir = 0;
198 TIter next(fInFile->GetListOfKeys());
199 // loop on events in file
200 while ((key=(TKey*)next())!=0) {
201 AliESD *esdEvent=(AliESD*)key->ReadObj();
203 printf("AliITSVertexerTracks::FindVerticesESD(): not an ESD!\n");
206 Int_t ev = (Int_t)esdEvent->GetEventNumber();
207 if(ev<fFirstEvent || ev>fLastEvent) { delete esdEvent; continue; }
208 if(ev % 100 == 0 || fDebug)
209 printf("--- Processing event %d of %d ---\n",ev,fLastEvent-fFirstEvent);
211 FindVertexForCurrentEvent(esdEvent);
213 if(!fCurrentVertex) {
214 printf("AliITSVertexerTracks::FindVertixesESD():\n no vertex for event %d\n",ev);
218 cout<<"VERTICE TROVATO\n";
219 if(fDebug) fCurrentVertex->PrintStatus();
221 // write the ESD to file
224 sprintf(ename,"%d",ev);
227 esdEvent->Write(ename,TObject::kOverwrite);
230 } // loop over events
234 //----------------------------------------------------------------------------
235 Int_t AliITSVertexerTracks::PrepareTracks(TTree &trkTree) {
237 // Propagate tracks to initial vertex position and store them in a TObjArray
239 Double_t maxd0rphi = 3.;
240 Double_t alpha,xlStart,d0rphi;
244 Int_t nEntries = (Int_t)trkTree.GetEntries();
246 if(!fTrkArray.IsEmpty()) fTrkArray.Clear();
247 fTrkArray.Expand(nEntries);
250 printf(" PrepareTracks()\n");
254 for(Int_t i=0; i<nEntries; i++) {
255 // check tracks to skip
257 for(Int_t j=0; j<fNTrksToSkip; j++) {
258 if(i==fTrksToSkip[j]) {
259 if(fDebug) printf("skipping track: %d\n",i);
263 if(skipThis) continue;
265 AliITStrackV2 *itstrack = new AliITStrackV2;
266 trkTree.SetBranchAddress("tracks",&itstrack);
270 // propagate track to vtxSeed
271 alpha = itstrack->GetAlpha();
272 xlStart = fNominalPos[0]*TMath::Cos(alpha)+fNominalPos[1]*TMath::Sin(alpha);
273 itstrack->PropagateTo(3.,0.0023,65.19); // to beam pipe (0.8 mm of Be)
274 itstrack->PropagateTo(xlStart,0.,0.); // to vtxSeed
276 // select tracks with d0rphi < maxd0rphi
277 d0rphi = TMath::Abs(itstrack->GetD(fNominalPos[0],fNominalPos[1]));
278 if(d0rphi > maxd0rphi) { delete itstrack; continue; }
280 fTrkArray.AddLast(itstrack);
285 if(fTrksToSkip) delete [] fTrksToSkip;
289 //----------------------------------------------------------------------------
290 void AliITSVertexerTracks::PrintStatus() const {
294 printf(" Initial position (%f,%f)\n",fNominalPos[0],fNominalPos[1]);
295 printf(" Vertex position after vertex finder (%f, %f, %f)\n",fInitPos[0],fInitPos[1],fInitPos[2]);
296 printf(" Number of tracks in array: %d\n",(Int_t)fTrkArray.GetEntriesFast());
297 printf(" Minimum # tracks required in fit: %d\n",fMinTracks);
301 //----------------------------------------------------------------------------
302 AliESDVertex* AliITSVertexerTracks::FindVertexForCurrentEvent(Int_t evnumb) {
304 // Vertex for current event
308 // get tree with tracks from input file
309 TString treeName = "TreeT_ITS_";
311 TTree *trkTree=(TTree*)fInFile->Get(treeName.Data());
312 if(!trkTree) return fCurrentVertex;
315 // get tracks and propagate them to initial vertex position
316 Int_t nTrks = PrepareTracks(*trkTree);
318 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
319 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
325 ComputeMaxChi2PerTrack(nTrks);
327 if(fDebug) printf(" vertex fit completed\n");
329 return fCurrentVertex;
331 //----------------------------------------------------------------------------
332 AliESDVertex* AliITSVertexerTracks::FindVertexForCurrentEvent(AliESD *esdEvent)
335 // Vertex for current ESD event
338 Double_t vtx[3],cvtx[6];
340 // put tracks reco in ITS in a tree
341 Int_t entr = (Int_t)esdEvent->GetNumberOfTracks();
342 TTree *trkTree = new TTree("TreeT_ITS","its tracks");
343 AliITStrackV2 *itstrack = 0;
344 trkTree->Branch("tracks","AliITStrackV2",&itstrack,entr,0);
346 for(Int_t i=0; i<entr; i++) {
347 AliESDtrack *esdTrack = (AliESDtrack*)esdEvent->GetTrack(i);
348 if(!esdTrack->GetStatus()&AliESDtrack::kITSin)
349 { delete esdTrack; continue; }
351 itstrack = new AliITStrackV2(*esdTrack);
353 catch (const Char_t *msg) {
354 Warning("FindVertexForCurrentEvent",msg);
365 // preselect tracks and propagate them to initial vertex position
366 Int_t nTrks = PrepareTracks(*trkTree);
368 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
369 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
371 // Set initial vertex position from ESD
372 esdEvent->GetVertex()->GetXYZ(vtx);
373 SetVtxStart(vtx[0],vtx[1]);
379 ComputeMaxChi2PerTrack(nTrks);
381 if(fDebug) printf(" vertex fit completed\n");
383 // store vertex information in ESD
384 fCurrentVertex->GetXYZ(vtx);
385 fCurrentVertex->GetCovMatrix(cvtx);
388 esdEvent->GetVertex()->GetTruePos(tp);
389 fCurrentVertex->SetTruePos(tp);
391 esdEvent->SetVertex(fCurrentVertex);
393 cout<<"Vertex: "<<vtx[0]<<", "<<vtx[1]<<", "<<vtx[2]<<endl;
394 return fCurrentVertex;
396 //---------------------------------------------------------------------------
397 void AliITSVertexerTracks::SetSkipTracks(Int_t n,Int_t *skipped) {
399 // Mark the tracks not ot be used in the vertex finding
402 fTrksToSkip = new Int_t[n];
403 for(Int_t i=0;i<n;i++) fTrksToSkip[i] = skipped[i];
406 //---------------------------------------------------------------------------
407 void AliITSVertexerTracks::TooFewTracks() {
409 // When the number of tracks is < fMinTracks the vertex is set to (0,0,0)
410 // and the number of tracks to -1
412 fCurrentVertex = new AliESDVertex(0.,0.,-1);
415 //---------------------------------------------------------------------------
416 void AliITSVertexerTracks::VertexFinder() {
418 // Get estimate of vertex position in (x,y) from tracks DCA
419 // Then this estimate is stored to the data member fInitPos
420 // (previous values are overwritten)
425 ******* TEMPORARY!!! FOR TEST ONLY!!! **********************************
427 fInitPos[0] = fNominalPos[0]+gRandom->Gaus(0.,0.0100); // 100 micron gaussian smearing
428 fInitPos[1] = fNominalPos[1]+gRandom->Gaus(0.,0.0100); // 100 micron gaussian smearing
432 for(Int_t i=0;i<2;i++)fInitPos[i]=fNominalPos[i];
434 Int_t nacc = (Int_t)fTrkArray.GetEntriesFast();
436 Double_t aver[3]={0.,0.,0.};
438 AliITStrackV2 *track1;
439 AliITStrackV2 *track2;
442 for(Int_t i=0; i<nacc; i++){
443 track1 = (AliITStrackV2*)fTrkArray.At(i);
446 track1->GetExternalParameters(xv,par);
447 cout<<"Track in position "<<i<<" xr= "<<xv<<endl;
448 for(Int_t ii=0;ii<5;ii++)cout<<par[ii]<<" ";
452 Double_t alpha = track1->GetAlpha();
453 Double_t azim = TMath::ASin(track1->GetSnp())+alpha;
454 Double_t theta = TMath::Pi()/2. - TMath::ATan(track1->GetTgl());
455 mom1[0] = TMath::Sin(theta)*TMath::Cos(azim);
456 mom1[1] = TMath::Sin(theta)*TMath::Sin(azim);
457 mom1[2] = TMath::Cos(theta);
460 Double_t mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
461 track1->GetGlobalXYZat(mindist,pos1[0],pos1[1],pos1[2]);
464 for(Int_t j=i+1; j<nacc; j++){
465 track2 = (AliITStrackV2*)fTrkArray.At(j);
467 alpha = track2->GetAlpha();
468 azim = TMath::ASin(track2->GetSnp())+alpha;
469 theta = TMath::Pi()/2. - TMath::ATan(track2->GetTgl());
470 mom2[0] = TMath::Sin(theta)*TMath::Cos(azim);
471 mom2[1] = TMath::Sin(theta)*TMath::Sin(azim);
472 mom2[2] = TMath::Cos(theta);
474 mindist = TMath::Cos(alpha)*fNominalPos[0]+TMath::Sin(alpha)*fNominalPos[1];
475 track2->GetGlobalXYZat(mindist,pos2[0],pos2[1],pos2[2]);
478 Double_t crosspoint[3];
479 Int_t retcode = line2.Cross(&line1,crosspoint);
481 if(fDebug>10)cout<<" i= "<<i<<", j= "<<j<<endl;
482 if(fDebug>10)cout<<"bad intersection\n";
488 for(Int_t jj=0;jj<3;jj++)aver[jj]+=crosspoint[jj];
489 if(fDebug>10)cout<<" i= "<<i<<", j= "<<j<<endl;
490 if(fDebug>10)cout<<"\n Cross point: ";
491 if(fDebug>10)cout<<crosspoint[0]<<" "<<crosspoint[1]<<" "<<crosspoint[2]<<endl;
496 for(Int_t jj=0;jj<3;jj++)fInitPos[jj] = aver[jj]/ncombi;
499 Warning("VertexFinder","Finder did not succed");
503 //************************************************************************
507 //---------------------------------------------------------------------------
508 void AliITSVertexerTracks::VertexFitter() {
510 // The optimal estimate of the vertex position is given by a "weighted
511 // average of tracks positions"
512 // Original method: CMS Note 97/0051
515 printf(" VertexFitter(): start\n");
523 rv(0,0) = fInitPos[0];
524 rv(1,0) = fInitPos[1];
526 Double_t xlStart,alpha;
528 Double_t cosRot,sinRot;
532 Int_t arrEntries = (Int_t)fTrkArray.GetEntries();
533 AliITStrackV2 *t = 0;
536 Int_t *skipTrack = new Int_t[arrEntries];
537 for(i=0; i<arrEntries; i++) skipTrack[i]=0;
540 // 1st - first estimate of vtx using all tracks
541 // 2nd - apply cut on chi2 max per track
542 // 3rd - estimate of global chi2
543 for(step=0; step<3; step++) {
544 if(fDebug) printf(" step = %d\n",step);
548 TMatrixD sumWiri(3,1);
552 for(j=0; j<3; j++) sumWi(j,i) = 0.;
556 for(k=0; k<arrEntries; k++) {
557 if(skipTrack[k]) continue;
558 // get track from track array
559 t = (AliITStrackV2*)fTrkArray.At(k);
560 alpha = t->GetAlpha();
561 xlStart = fInitPos[0]*TMath::Cos(alpha)+fInitPos[1]*TMath::Sin(alpha);
562 t->PropagateTo(xlStart,0.,0.); // to vtxSeed
564 if(alpha<0.) rotAngle += 2.*TMath::Pi();
565 cosRot = TMath::Cos(rotAngle);
566 sinRot = TMath::Sin(rotAngle);
568 // vector of track global coordinates
570 ri(0,0) = t->GetX()*cosRot-t->GetY()*sinRot;
571 ri(1,0) = t->GetX()*sinRot+t->GetY()*cosRot;
574 // matrix to go from global (x,y,z) to local (y,z);
583 // covariance matrix of local (y,z) - inverted
585 t->GetExternalCovariance(cc);
591 // weights matrix: wWi = qQiT * uUiInv * qQi
592 if(uUi.Determinant() <= 0.) continue;
593 TMatrixD uUiInv(TMatrixD::kInverted,uUi);
594 TMatrixD uUiInvQi(uUiInv,TMatrixD::kMult,qQi);
595 TMatrixD wWi(qQi,TMatrixD::kTransposeMult,uUiInvQi);
598 TMatrixD deltar = rv; deltar -= ri;
599 TMatrixD wWideltar(wWi,TMatrixD::kMult,deltar);
600 chi2i = deltar(0,0)*wWideltar(0,0)+
601 deltar(1,0)*wWideltar(1,0)+
602 deltar(2,0)*wWideltar(2,0);
605 if(step==1 && chi2i > fMaxChi2PerTrack) {
613 TMatrixD wWiri(wWi,TMatrixD::kMult,ri);
619 } // end loop on tracks
621 if(nUsedTrks < fMinTracks) {
626 Double_t determinant = sumWi.Determinant();
627 //cerr<<" determinant: "<<determinant<<endl;
628 if(determinant < 100.) {
629 printf("det(V) = 0\n");
634 // inverted of weights matrix
635 TMatrixD invsumWi(TMatrixD::kInverted,sumWi);
638 // position of primary vertex
641 } // end loop on the 3 steps
651 Double_t position[3];
652 position[0] = rv(0,0);
653 position[1] = rv(1,0);
654 position[2] = rv(2,0);
655 Double_t covmatrix[6];
656 covmatrix[0] = vV(0,0);
657 covmatrix[1] = vV(0,1);
658 covmatrix[2] = vV(1,1);
659 covmatrix[3] = vV(0,2);
660 covmatrix[4] = vV(1,2);
661 covmatrix[5] = vV(2,2);
663 // store data in the vertex object
664 fCurrentVertex = new AliESDVertex(position,covmatrix,chi2,nUsedTrks);
667 printf(" VertexFitter(): finish\n");
668 printf(" rv = ( %f , %f , %f )\n\n",rv(0,0),rv(1,0),rv(2,0));
669 fCurrentVertex->PrintStatus();
674 //----------------------------------------------------------------------------
675 AliESDVertex *AliITSVertexerTracks::VertexOnTheFly(TTree &trkTree) {
677 // Return vertex from tracks in trkTree
679 if(fCurrentVertex) fCurrentVertex = 0;
681 // get tracks and propagate them to initial vertex position
682 Int_t nTrks = PrepareTracks(*(&trkTree));
683 if(fDebug) printf(" tracks prepared: %d\n",nTrks);
684 if(nTrks < fMinTracks) { TooFewTracks(); return fCurrentVertex; }
690 ComputeMaxChi2PerTrack(nTrks);
692 if(fDebug) printf(" vertex fit completed\n");
694 return fCurrentVertex;
696 //----------------------------------------------------------------------------