]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveBase/AliEveTrackFitter.cxx
MUON DAs
[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 #include "AliEveTrack.h"
12
13 #include "TCanvas.h"
14 #include "TGraph.h"
15 #include "TGraphErrors.h"
16 #include "TQObject.h"
17
18 #include "AliRieman.h"
19 #include "AliExternalTrackParam.h"
20
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 // AliEveTrack.
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
67   fGraphPicked = new TGraph();
68   fGraphPicked->SetName("Selected points");
69   fGraphPicked->SetMarkerColor(4);
70   fGraphPicked->SetMarkerStyle(4);
71   fGraphPicked->SetMarkerSize(2);
72
73   fGraphHelix = new TGraphErrors();
74   fGraphHelix->SetName("Fitted points");
75   fGraphHelix->SetMarkerColor(2);
76 }
77
78 AliEveTrackFitter::~AliEveTrackFitter()
79 {
80   // Destructor.
81
82   if (fRieman) delete fRieman;
83
84   fTrackList->DecDenyDestroy();
85 }
86
87 /******************************************************************************/
88
89 void AliEveTrackFitter::DestroyElements()
90 {
91   // Virtual method of base class TEveElement.
92   // Preserves TEveTrackList object for fitted helices.
93
94   TEveElement::DestroyElements();
95
96   // fTrackList is destroyed because DenyDestroy is set.
97   gEve->AddElement(fTrackList, this);
98   fTrackList->DestroyElements();
99 }
100
101 /******************************************************************************/
102
103 void AliEveTrackFitter::Start()
104 {
105   // Clear existing point selection and maintain connection to the
106   // TEvePointSet signal.
107
108   Reset();
109   if (fConnected == kFALSE)
110   {
111     TQObject::Connect("TEvePointSet", "PointSelected(Int_t)",
112                       "AliEveTrackFitter", this, "AddFitPoint(Int_t)");
113     fConnected = kTRUE;
114   }
115 }
116
117 void AliEveTrackFitter::Stop()
118 {
119   // Stop adding points for the fit.
120
121   if (fConnected)
122   {
123     TQObject::Disconnect("TEvePointSet", "AddFitPoint(Int_t)");
124     fConnected = kFALSE;
125   }
126 }
127
128 void AliEveTrackFitter::Reset(Int_t nPoints, Int_t nIntIds)
129 {
130   // Reset selection.
131
132   if (fRieman) fRieman->Reset();
133   TEvePointSet::Reset(nPoints, nIntIds);
134   fSPMap.clear();
135 }
136
137 /******************************************************************************/
138
139 void AliEveTrackFitter::AddFitPoint(Int_t pointId)
140 {
141   // Add or remove given point depending if exists in the map.
142
143   Float_t x, y, z;
144
145   TEvePointSet* ps = static_cast<TEvePointSet*>((TQObject*) gTQSender);
146
147   PointMap_t::iterator g = fSPMap.find(Point_t(ps, pointId));
148   if (g != fSPMap.end())
149   {
150     Int_t idx = g->second;
151     if (idx != fLastPoint)
152     {
153       GetPoint(fLastPoint, x, y, z);
154       SetPoint(idx, x, y, z);
155     }
156     fSPMap.erase(g);
157     --fLastPoint;
158   }
159   else
160   {
161     fSPMap[Point_t(ps, pointId)] = Size();
162     ps->GetPoint(pointId, x, y, z);
163     SetNextPoint(x, y, z);
164   }
165
166   ResetBBox();
167   ElementChanged(kTRUE, kTRUE);
168 }
169
170 /******************************************************************************/
171
172 void AliEveTrackFitter::FitTrack()
173 {
174   // Fit selected points with AliRieman fitter.
175
176   using namespace TMath;
177
178   if (fRieman) delete fRieman;
179   fRieman = new AliRieman(Size());
180
181   Float_t x, y, z;
182   Int_t alphaIdx = 0;
183   GetPoint(alphaIdx, x, y, z);
184   Float_t minR2=x*x + y*y;
185   for (Int_t i=0; i<=fLastPoint; i++)
186   {
187     GetPoint(i, x, y, z);
188     Float_t cR2 = x*x + y*y;
189     if (minR2 > cR2)
190     {
191       minR2 = cR2;
192       alphaIdx = i;
193     }
194   }
195   GetPoint(alphaIdx, x, y, z);
196   fAlpha = ATan2(y, x);
197   Float_t sin = Sin(-fAlpha);
198   Float_t cos = Cos(-fAlpha);
199   for (Int_t i = 0; i <= fLastPoint; ++i)
200   {
201     GetPoint(i, x, y, z);
202     fRieman->AddPoint(cos*x - sin*y, cos*y + sin*x, z, 1, 1);
203   }
204   fRieman->Update();
205
206   Double_t r = Sqrt(minR2);
207   Double_t param[5];
208   Double_t cov[15];
209   fRieman->GetExternalParameters(r, param, cov);
210   // curvature to pt
211   param[4] /= TEveTrackPropagator::fgDefMagField*TEveTrackPropagator::fgkB2C;
212   // sign in tang
213   if (param[4] < 0) param[3] = -param[3];
214   AliExternalTrackParam trackParam(r, fAlpha, param, cov);
215   trackParam.Print();
216
217   // make track
218   Double_t v0[3];
219   trackParam.GetXYZAt(r, TEveTrackPropagator::fgDefMagField, v0);
220   Double_t p0[3];
221   trackParam.GetPxPyPzAt(r, TEveTrackPropagator::fgDefMagField, p0);
222   TEveRecTrack rc;
223   rc.fV.Set(v0);
224   rc.fP.Set(p0);
225   rc.fSign = trackParam.Charge();
226
227   AliEveTrack* track = new AliEveTrack(&rc, fTrackList->GetPropagator());
228   track->SetName(Form("track %f", fAlpha));
229
230   track->MakeTrack();
231   track->SetAttLineAttMarker(fTrackList);
232   fTrackList->AddElement(track);
233 }
234
235
236 /******************************************************************************/
237
238 void AliEveTrackFitter::DrawDebugGraph()
239 {
240   // Draw graph of picked points and helix points.
241
242   static const TEveException kEH("AliEveTrackFitter::DrawRiemanGraph ");
243
244   if (fRieman == 0)
245     throw(kEH + "fitter not set.");
246
247   Int_t nR = fRieman->GetN();
248   fGraphPicked->Set(nR);
249   fGraphHelix->Set(nR);
250
251   Double_t* x  =  fRieman->GetX();
252   Double_t* y  =  fRieman->GetY();
253   Double_t* sy =  fRieman->GetSy();
254   for (Int_t i=0; i<nR; i++)
255   {
256     fGraphPicked->SetPoint(i, x[i], y[i]);
257     fGraphHelix->SetPoint (i, x[i], fRieman->GetYat(x[i]));
258     fGraphHelix->SetPointError(i, 0.1, sy[i]); // now faked
259   }
260
261   if (gPad)
262     gPad->Clear();
263
264   fGraphPicked->Draw("AP");
265   fGraphHelix->Draw("SAME P");
266   gPad->GetCanvas()->SetTitle(Form("AliRieman alpha: %f", fAlpha));
267   gPad->Modified();
268   gPad->Update();
269 }