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