]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveBase/AliEveTrackFitter.cxx
Merge changes from branches/dev/EVE. This branch was following development in ROOT...
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveTrackFitter.cxx
1 // $Id$
2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4 /**************************************************************************
5  * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
7  * full copyright notice.                                                 *
8  **************************************************************************/
9
10 #include "AliEveTrackFitter.h"
11
12 #include "TCanvas.h"
13 #include "TGraph.h"
14 #include "TGraphErrors.h"
15 #include "TQObject.h"
16
17 #include "AliRieman.h"
18 #include "AliExternalTrackParam.h"
19
20 #include <TEveTrack.h>
21 #include <TEveTrackPropagator.h>
22 #include <TEveVSDStructs.h>
23 #include <TEveManager.h>
24
25
26 //==============================================================================
27 //==============================================================================
28 // AliEveTrackFitter
29 //==============================================================================
30
31 //______________________________________________________________________________
32 //
33 // AliEveTrackFitter is an interface to TEvePointSet allowing AliRieman fit.
34 // It builds a list of points by listening to selection signal of any
35 // object of type TEvePointSet. After selection the list is feeded to
36 // AliRieman fitter, which returns helix parameters visualized with
37 // TEveTrack.
38 //
39
40 ClassImp(AliEveTrackFitter)
41
42 AliEveTrackFitter::AliEveTrackFitter(const Text_t* name, Int_t nPoints) :
43     TEvePointSet   (name, nPoints),
44
45     fAlpha         (0),
46     fRieman        (0),
47
48     fConnected     (kFALSE),
49     fSPMap         (),
50     fTrackList     (0),
51
52     fGraphPicked   (0),
53     fGraphHelix    (0)
54 {
55   // Constructor.
56
57   SetMarkerColor(3);
58   SetOwnIds(kFALSE);
59
60   fTrackList = new TEveTrackList("Tracks");
61   fTrackList->IncDenyDestroy();
62   fTrackList->SetLineWidth(2);
63   fTrackList->SetLineColor(8);
64   fTrackList->GetPropagator()->SetEditPathMarks(kTRUE);
65   AddElement(fTrackList);
66   UpdateItems();
67
68   fGraphPicked = new TGraph();
69   fGraphPicked->SetName("Selected points");
70   fGraphPicked->SetMarkerColor(4);
71   fGraphPicked->SetMarkerStyle(4);
72   fGraphPicked->SetMarkerSize(2);
73
74   fGraphHelix = new TGraphErrors();
75   fGraphHelix->SetName("Fitted points");
76   fGraphHelix->SetMarkerColor(2);
77 }
78
79 AliEveTrackFitter::~AliEveTrackFitter()
80 {
81   // Destructor.
82
83   if (fRieman) delete fRieman;
84
85   fTrackList->DecDenyDestroy();
86 }
87
88 /******************************************************************************/
89
90 void AliEveTrackFitter::DestroyElements()
91 {
92   // Virtual method of base class TEveElement.
93   // Preserves TEveTrackList object for fitted helices.
94
95   TEveElement::DestroyElements();
96
97   // fTrackList is destroyed because DenyDestroy is set.
98   gEve->AddElement(fTrackList, this);
99   fTrackList->DestroyElements();
100
101   UpdateItems();
102 }
103
104 /******************************************************************************/
105
106 void AliEveTrackFitter::Start()
107 {
108   // Clear existing point selection and maintain connection to the
109   // TEvePointSet signal.
110
111   Reset();
112   if (fConnected == kFALSE)
113   {
114     TQObject::Connect("TEvePointSet", "PointSelected(Int_t)",
115                       "AliEveTrackFitter", this, "AddFitPoint(Int_t)");
116     fConnected = kTRUE;
117   }
118 }
119
120 void AliEveTrackFitter::Stop()
121 {
122   // Stop adding points for the fit.
123
124   if (fConnected)
125   {
126     TQObject::Disconnect("TEvePointSet", "AddFitPoint(Int_t)");
127     fConnected = kFALSE;
128   }
129 }
130
131 void AliEveTrackFitter::Reset(Int_t nPoints, Int_t nIntIds)
132 {
133   // Reset selection.
134
135   if (fRieman) fRieman->Reset();
136   TEvePointSet::Reset(nPoints, nIntIds);
137   fSPMap.clear();
138 }
139
140 /******************************************************************************/
141
142 void AliEveTrackFitter::AddFitPoint(Int_t pointId)
143 {
144   // Add or remove given point depending if exists in the map.
145
146   Float_t x, y, z;
147
148   TEvePointSet* ps = dynamic_cast<TEvePointSet*>((TQObject*) gTQSender);
149
150   PointMap_t::iterator g = fSPMap.find(Point_t(ps, pointId));
151   if (g != fSPMap.end())
152   {
153     Int_t idx = g->second;
154     if (idx != fLastPoint)
155     {
156       GetPoint(fLastPoint, x, y, z);
157       SetPoint(idx, x, y, z);
158     }
159     fSPMap.erase(g);
160     --fLastPoint;
161   }
162   else
163   {
164     fSPMap[Point_t(ps, pointId)] = Size();
165     ps->GetPoint(pointId, x, y, z);
166     SetNextPoint(x, y, z);
167   }
168
169   ResetBBox();
170   ElementChanged(kTRUE, kTRUE);
171 }
172
173 /******************************************************************************/
174
175 void AliEveTrackFitter::FitTrack()
176 {
177   // Fit selected points with AliRieman fitter.
178
179   using namespace TMath;
180
181   if (fRieman) delete fRieman;
182   fRieman = new AliRieman(Size());
183
184   Float_t x, y, z;
185   Int_t alphaIdx = 0;
186   GetPoint(alphaIdx, x, y, z);
187   Float_t minR2=x*x + y*y;
188   for (Int_t i=1; i<=fLastPoint; i++)
189   {
190     GetPoint(i, x, y, z);
191     Float_t cR2 = x*x + y*y;
192     if ( minR2 > cR2 )
193     {
194       minR2 = cR2;
195       alphaIdx = i;
196     }
197   }
198   GetPoint(alphaIdx, x, y, z);
199   fAlpha = ATan2(y, x);
200   Float_t sin = Sin(-fAlpha);
201   Float_t cos = Cos(-fAlpha);
202   for (Int_t i=0; i<=fLastPoint; i++) {
203     GetPoint(i, x, y, z);
204     fRieman->AddPoint(cos*x - sin*y, cos*y + sin*x, z, 1, 1);
205   }
206   fRieman->Update();
207
208   Double_t r = Sqrt(minR2);
209   Double_t param[5];
210   Double_t cov[15];
211   fRieman->GetExternalParameters(r, param, cov);
212   // curvature to pt
213   param[4] /= TEveTrackPropagator::fgDefMagField*TEveTrackPropagator::fgkB2C;
214   // sign in tang
215   if (param[4] < 0) param[3] *= -1;
216   AliExternalTrackParam trackParam(r, fAlpha, param, cov);
217   trackParam.Print();
218
219   // make track
220   Double_t v0[3];
221   trackParam.GetXYZAt(r, TEveTrackPropagator::fgDefMagField, v0);
222   Double_t p0[3];
223   trackParam.GetPxPyPzAt(r, TEveTrackPropagator::fgDefMagField, p0);
224   TEveRecTrack rc;
225   rc.fV.Set(v0);
226   rc.fP.Set(p0);
227   rc.fSign = trackParam.Charge();
228
229   TEveTrack* track = new TEveTrack(&rc, fTrackList->GetPropagator());
230   track->SetName(Form("track %f", fAlpha));
231   for(Int_t i=0; i<=fLastPoint; ++i)
232   {
233     TEvePathMark pm(TEvePathMark::kDaughter);
234     GetPoint(i, x, y, z);
235     pm.fV.Set(x, y, z);
236     pm.fP.Set(p0);
237     track->AddPathMark(pm);
238   }
239   track->MakeTrack();
240   track->SetAttLineAttMarker(fTrackList);
241   fTrackList->AddElement(track);
242 }
243
244
245 /******************************************************************************/
246
247 void AliEveTrackFitter::DrawDebugGraph()
248 {
249   // Draw graph of picked points and helix points.
250
251   static const TEveException kEH("AliEveTrackFitter::DrawRiemanGraph ");
252
253   if (fRieman == 0)
254     throw(kEH + "fitter not set.");
255
256   Int_t nR = fRieman->GetN();
257   fGraphPicked->Set(nR);
258   fGraphHelix->Set(nR);
259
260   Double_t* x  =  fRieman->GetX();
261   Double_t* y  =  fRieman->GetY();
262   Double_t* sy =  fRieman->GetSy();
263   for (Int_t i=0; i<nR; i++)
264   {
265     fGraphPicked->SetPoint(i, x[i], y[i]);
266     fGraphHelix->SetPoint (i, x[i], fRieman->GetYat(x[i]));
267     fGraphHelix->SetPointError(i, 0.1, sy[i]); // now faked
268   }
269
270   if (gPad)
271     gPad->Clear();
272
273   fGraphPicked->Draw("AP");
274   fGraphHelix->Draw("SAME P");
275   gPad->GetCanvas()->SetTitle(Form("AliRieman alpha: %f", fAlpha));
276   gPad->Modified();
277   gPad->Update();
278 }