]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Alieve/TrackFitter.cxx
dab5de7143d9f488f87f58f5c4884b8749916022
[u/mrichter/AliRoot.git] / EVE / Alieve / TrackFitter.cxx
1 // $Header$
2
3 #include "TrackFitter.h"
4
5 //#include "TClass.h"
6 #include "TCanvas.h"
7 #include "TGraph.h"
8 #include "TGraphErrors.h"
9
10 #include "AliCluster.h"
11 #include "AliRieman.h"
12 #include "AliExternalTrackParam.h"
13
14 #include <Reve/Track.h>
15 #include <Reve/PODs.h>
16 #include <Reve/ReveManager.h>
17
18 using namespace Reve;
19 using namespace Alieve;
20
21 //______________________________________________________________________
22 // TrackFitter
23 //
24 //  TrackFitter is an interface to helix fit. It creates a set of points, listening to signal 
25 //  PointCtrlClicked() of any Reve::PointSet. Via editor it fits selected points and creates a 
26 //  reconstructed track.
27 // 
28
29 ClassImp(TrackFitter)
30
31 TrackFitter::TrackFitter(const Text_t* name, Int_t n_points, TreeVarType_e tv_type) :
32     Reve::PointSet(name, n_points, tv_type),
33
34     fGraphSelected(0),
35     fGraphFitted(0),
36
37     fRieman(0),
38     fConnected(kFALSE),
39     fTrackList(0)
40 {
41   // Constructor.
42
43   SetMarkerColor(3);
44   SetOwnIds(kFALSE);
45
46   fGraphSelected = new TGraph();
47   fGraphSelected->SetName("Selected points");
48   fGraphSelected->SetMarkerColor(4);
49   fGraphSelected->SetMarkerStyle(4);  
50   fGraphSelected->SetMarkerSize(2);
51
52   fGraphFitted = new TGraphErrors();
53   fGraphFitted->SetName("Fitted points");
54   fGraphFitted->SetMarkerColor(2);
55
56   fTrackList = new TrackList("Tracks");
57   fTrackList->SetLineWidth(2);
58   fTrackList->SetLineColor(8);
59   fTrackList->IncDenyDestroy();
60   fTrackList->GetRnrStyle()->SetEditPathMarks(kTRUE);
61   gReve->AddRenderElement(fTrackList, this);
62   UpdateItems();
63 }
64
65 TrackFitter::~TrackFitter()
66 {
67   // Destructor.
68
69   if(fRieman) delete fRieman;
70   fTrackList->DecDenyDestroy();
71   delete fTrackList;
72 }
73
74 /**************************************************************************/
75 void TrackFitter::DestroyElements()
76 {
77   // Virtual method of base class Reve::RenderElement.
78   // It preserves track list to have coomon track propagator attributes.
79
80   RenderElement::DestroyElements();
81   gReve->AddRenderElement(fTrackList, this);
82   fTrackList->DestroyElements();
83   UpdateItems();
84 }
85
86 /**************************************************************************/
87 void TrackFitter::Start()
88 {
89   // Start selection of points.
90
91   Reset();
92   if(fConnected == kFALSE)
93   {
94     TQObject::Connect("Reve::PointSet", "PointCtrlClicked(Reve::PointSet*,Int_t)",
95                       "Alieve::TrackFitter", this, "AddFitPoint(Reve::PointSet*,Int_t)");
96
97     fConnected = kTRUE;
98   }
99 }
100
101 void TrackFitter::Stop()
102 {
103   // Stop selection of points.
104
105   if(fConnected)
106   {
107     TQObject::Disconnect("Reve::PointSet", "AddFitPoint(Reve::PointSet*,Int_t)");
108     fConnected = kFALSE;
109   }
110 }
111
112 /**************************************************************************/
113
114 void TrackFitter::AddFitPoint(Reve::PointSet* ps, Int_t n)
115
116   // Add/remove given point depending if exists in the fMapPS.
117  
118   Float_t x, y, z;
119
120   std::map<Point_t, Int_t>::iterator g = fMapPS.find(Point_t(ps, n));
121   if(g != fMapPS.end())
122   {
123     Int_t idx = g->second;
124     if(idx != fLastPoint)
125     {
126       GetPoint(fLastPoint, x, y, z);
127       SetPoint(idx, x, y, z);
128     }
129     fMapPS.erase(g);
130     fLastPoint--;
131   }
132   else 
133   {
134     fMapPS[Point_t(ps, n)] = Size();
135     ps->GetPoint(n, x, y, z);
136     SetNextPoint(x, y, z); 
137     SetPointId(ps->GetPointId(n));
138   }
139   ResetBBox();
140   ElementChanged(kTRUE, kTRUE);
141 }
142
143 /**************************************************************************/
144
145 void TrackFitter::FitTrack()
146 {
147   // Fit selected points with AliRieman fitter.
148
149   using namespace TMath;
150
151   if(fRieman) delete fRieman;
152   fRieman = new AliRieman(Size());
153
154   Float_t x, y, z;
155   Int_t alphaIdx = 0;
156   GetPoint(alphaIdx, x, y, z);
157   Float_t minR2=x*x + y*y;
158   for (Int_t i=1; i<=fLastPoint; i++)
159   {
160     GetPoint(i, x, y, z);
161     Float_t cR2 = x*x + y*y;
162     if ( minR2 > cR2 )
163     {
164       minR2 = cR2;
165       alphaIdx = i;
166     }
167   }
168   GetPoint(alphaIdx, x, y, z);
169   fAlpha = ATan2(y, x);
170   Float_t sin = Sin(-fAlpha);
171   Float_t cos = Cos(-fAlpha);  
172   for (Int_t i=0; i<=fLastPoint; i++) { 
173     GetPoint(i, x, y, z);
174     fRieman->AddPoint(cos*x - sin*y, cos*y + sin*x, z, 1, 1);
175   }
176   fRieman->Update();
177
178   Double_t r = Sqrt(minR2);
179   Double_t param[5];
180   Double_t cov[15];
181   fRieman->GetExternalParameters(r, param, cov);
182   // curvature to pt
183   param[4] /= TrackRnrStyle::fgDefMagField*TrackRnrStyle::fgkB2C;
184   // sign in tang
185   if(param[4] < 0) param[3] *= -1;
186   AliExternalTrackParam trackParam(r, fAlpha, param, cov);
187   trackParam.Print();
188
189   // make track
190   Double_t V0[3];
191   trackParam.GetXYZAt(r, TrackRnrStyle::fgDefMagField, V0);
192   Double_t P0[3];
193   trackParam.GetPxPyPzAt(r, TrackRnrStyle::fgDefMagField, P0);
194   RecTrack rc;
195   rc.V.Set(V0); 
196   rc.P.Set(P0);
197   rc.sign = trackParam.Charge();
198
199   Track* track = new Track(&rc, fTrackList->GetRnrStyle());
200   track->SetName(Form("track %f", fAlpha));
201   PathMark* pm = new PathMark(PathMark::Daughter);
202   for(Int_t i=0; i==fLastPoint; i++)
203   {
204     GetPoint(i, x, y, z);
205     pm->V.Set(x, y, z);
206     pm->P.Set(P0); 
207     track->AddPathMark(pm);
208   }
209   track->MakeTrack();
210   track->SetAttLineAttMarker(fTrackList); 
211   gReve->AddRenderElement(track, fTrackList);
212 }
213
214
215 void TrackFitter::Reset(Int_t n, Int_t ids)
216 {
217   // Reset selection.
218
219   if(fRieman) fRieman->Reset();
220   PointSet::Reset(n, ids);
221   fMapPS.clear();
222 }
223
224 /**************************************************************************/
225 void TrackFitter::DrawRiemanGraph()
226 {
227   // Draw graph of rieman fit.
228
229    static const Exc_t eH("TrackFitter::DrawRiemanGraph ");
230
231   if(fRieman == 0)
232     throw(eH + "fitter not set.");
233
234   Int_t nR = fRieman->GetN();
235   fGraphSelected->Set(nR);
236   fGraphFitted->Set(nR);
237
238   Double_t* x =  fRieman->GetX();  
239   Double_t* y =  fRieman->GetY();
240   Double_t* sy =  fRieman->GetSy();
241   for (Int_t i=0; i<nR; i++)
242   {
243     fGraphSelected->SetPoint(i, x[i], y[i]);
244     fGraphFitted->SetPoint(i, x[i], fRieman->GetYat(x[i]));
245     fGraphFitted->SetPointError(i, 0.1, sy[i]);
246   }
247   
248   if (gPad) gPad->Clear();
249   fGraphSelected->Draw("AP");
250   fGraphFitted->Draw("SAME P");
251   gPad->GetCanvas()->SetTitle(Form("AliRieman alpha: %f", fAlpha));
252   gPad->Modified();
253   gPad->Update();
254 }