Fix enums as reported by coverity
[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"
0e33c639 11#include "AliEveTrack.h"
5a1436d6 12
5a1436d6 13#include "TCanvas.h"
14#include "TGraph.h"
15#include "TGraphErrors.h"
a15e6d7d 16#include "TQObject.h"
5a1436d6 17
5a1436d6 18#include "AliRieman.h"
19#include "AliExternalTrackParam.h"
20
84aff7a4 21#include <TEveTrackPropagator.h>
22#include <TEveVSDStructs.h>
23#include <TEveManager.h>
5a1436d6 24
5a1436d6 25
a15e6d7d 26//==============================================================================
27//==============================================================================
d810d0de 28// AliEveTrackFitter
a15e6d7d 29//==============================================================================
30
31//______________________________________________________________________________
5a1436d6 32//
40790e5b 33// AliEveTrackFitter is an interface to TEvePointSet allowing AliRieman fit.
a15e6d7d 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
0e33c639 37// AliEveTrack.
51346b82 38//
5a1436d6 39
d810d0de 40ClassImp(AliEveTrackFitter)
5a1436d6 41
a15e6d7d 42AliEveTrackFitter::AliEveTrackFitter(const Text_t* name, Int_t nPoints) :
43 TEvePointSet (name, nPoints),
fd31e9de 44
fd31e9de 45 fAlpha (0),
46 fRieman (0),
40790e5b 47
fd31e9de 48 fConnected (kFALSE),
40790e5b 49 fSPMap (),
fd31e9de 50 fTrackList (0),
40790e5b 51
52 fGraphPicked (0),
53 fGraphHelix (0)
5a1436d6 54{
55 // Constructor.
56
57 SetMarkerColor(3);
58 SetOwnIds(kFALSE);
59
84aff7a4 60 fTrackList = new TEveTrackList("Tracks");
a15e6d7d 61 fTrackList->IncDenyDestroy();
5a1436d6 62 fTrackList->SetLineWidth(2);
63 fTrackList->SetLineColor(8);
84aff7a4 64 fTrackList->GetPropagator()->SetEditPathMarks(kTRUE);
a15e6d7d 65 AddElement(fTrackList);
40790e5b 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);
5a1436d6 76}
77
d810d0de 78AliEveTrackFitter::~AliEveTrackFitter()
5a1436d6 79{
80 // Destructor.
81
a15e6d7d 82 if (fRieman) delete fRieman;
40790e5b 83
5a1436d6 84 fTrackList->DecDenyDestroy();
5a1436d6 85}
86
57ffa5fb 87/******************************************************************************/
a15e6d7d 88
89void 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();
a15e6d7d 99}
100
101/******************************************************************************/
102
d810d0de 103void AliEveTrackFitter::Start()
5a1436d6 104{
40790e5b 105 // Clear existing point selection and maintain connection to the
106 // TEvePointSet signal.
5a1436d6 107
108 Reset();
a15e6d7d 109 if (fConnected == kFALSE)
5a1436d6 110 {
a15e6d7d 111 TQObject::Connect("TEvePointSet", "PointSelected(Int_t)",
112 "AliEveTrackFitter", this, "AddFitPoint(Int_t)");
5a1436d6 113 fConnected = kTRUE;
114 }
115}
116
d810d0de 117void AliEveTrackFitter::Stop()
5a1436d6 118{
40790e5b 119 // Stop adding points for the fit.
5a1436d6 120
a15e6d7d 121 if (fConnected)
5a1436d6 122 {
a15e6d7d 123 TQObject::Disconnect("TEvePointSet", "AddFitPoint(Int_t)");
5a1436d6 124 fConnected = kFALSE;
125 }
126}
127
a15e6d7d 128void AliEveTrackFitter::Reset(Int_t nPoints, Int_t nIntIds)
40790e5b 129{
130 // Reset selection.
131
a15e6d7d 132 if (fRieman) fRieman->Reset();
133 TEvePointSet::Reset(nPoints, nIntIds);
40790e5b 134 fSPMap.clear();
135}
136
57ffa5fb 137/******************************************************************************/
5a1436d6 138
a15e6d7d 139void AliEveTrackFitter::AddFitPoint(Int_t pointId)
51346b82 140{
40790e5b 141 // Add or remove given point depending if exists in the map.
51346b82 142
5a1436d6 143 Float_t x, y, z;
144
469ca156 145 TEvePointSet* ps = static_cast<TEvePointSet*>((TQObject*) gTQSender);
a15e6d7d 146
147 PointMap_t::iterator g = fSPMap.find(Point_t(ps, pointId));
148 if (g != fSPMap.end())
5a1436d6 149 {
150 Int_t idx = g->second;
a15e6d7d 151 if (idx != fLastPoint)
5a1436d6 152 {
153 GetPoint(fLastPoint, x, y, z);
154 SetPoint(idx, x, y, z);
155 }
40790e5b 156 fSPMap.erase(g);
a15e6d7d 157 --fLastPoint;
5a1436d6 158 }
51346b82 159 else
5a1436d6 160 {
a15e6d7d 161 fSPMap[Point_t(ps, pointId)] = Size();
162 ps->GetPoint(pointId, x, y, z);
51346b82 163 SetNextPoint(x, y, z);
5a1436d6 164 }
40790e5b 165
5a1436d6 166 ResetBBox();
167 ElementChanged(kTRUE, kTRUE);
168}
169
57ffa5fb 170/******************************************************************************/
5a1436d6 171
d810d0de 172void AliEveTrackFitter::FitTrack()
5a1436d6 173{
174 // Fit selected points with AliRieman fitter.
175
176 using namespace TMath;
177
a15e6d7d 178 if (fRieman) delete fRieman;
5a1436d6 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;
4b456ebb 185 for (Int_t i=0; i<=fLastPoint; i++)
5a1436d6 186 {
187 GetPoint(i, x, y, z);
188 Float_t cR2 = x*x + y*y;
4b456ebb 189 if (minR2 > cR2)
5a1436d6 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);
51346b82 198 Float_t cos = Cos(-fAlpha);
4b456ebb 199 for (Int_t i = 0; i <= fLastPoint; ++i)
200 {
5a1436d6 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
84aff7a4 211 param[4] /= TEveTrackPropagator::fgDefMagField*TEveTrackPropagator::fgkB2C;
5a1436d6 212 // sign in tang
4b456ebb 213 if (param[4] < 0) param[3] = -param[3];
5a1436d6 214 AliExternalTrackParam trackParam(r, fAlpha, param, cov);
215 trackParam.Print();
216
217 // make track
a15e6d7d 218 Double_t v0[3];
219 trackParam.GetXYZAt(r, TEveTrackPropagator::fgDefMagField, v0);
220 Double_t p0[3];
221 trackParam.GetPxPyPzAt(r, TEveTrackPropagator::fgDefMagField, p0);
84aff7a4 222 TEveRecTrack rc;
a15e6d7d 223 rc.fV.Set(v0);
224 rc.fP.Set(p0);
84aff7a4 225 rc.fSign = trackParam.Charge();
5a1436d6 226
0e33c639 227 AliEveTrack* track = new AliEveTrack(&rc, fTrackList->GetPropagator());
5a1436d6 228 track->SetName(Form("track %f", fAlpha));
4b456ebb 229
5a1436d6 230 track->MakeTrack();
51346b82 231 track->SetAttLineAttMarker(fTrackList);
a15e6d7d 232 fTrackList->AddElement(track);
5a1436d6 233}
234
235
40790e5b 236/******************************************************************************/
5a1436d6 237
40790e5b 238void AliEveTrackFitter::DrawDebugGraph()
5a1436d6 239{
40790e5b 240 // Draw graph of picked points and helix points.
5a1436d6 241
a15e6d7d 242 static const TEveException kEH("AliEveTrackFitter::DrawRiemanGraph ");
5a1436d6 243
a15e6d7d 244 if (fRieman == 0)
245 throw(kEH + "fitter not set.");
5a1436d6 246
247 Int_t nR = fRieman->GetN();
40790e5b 248 fGraphPicked->Set(nR);
249 fGraphHelix->Set(nR);
5a1436d6 250
40790e5b 251 Double_t* x = fRieman->GetX();
252 Double_t* y = fRieman->GetY();
5a1436d6 253 Double_t* sy = fRieman->GetSy();
254 for (Int_t i=0; i<nR; i++)
255 {
40790e5b 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
5a1436d6 259 }
51346b82 260
40790e5b 261 if (gPad)
262 gPad->Clear();
263
264 fGraphPicked->Draw("AP");
265 fGraphHelix->Draw("SAME P");
5a1436d6 266 gPad->GetCanvas()->SetTitle(Form("AliRieman alpha: %f", fAlpha));
267 gPad->Modified();
268 gPad->Update();
269}