Changed fitting algorithm
authordecaro <decaro@f7af4fe6-9843-0410-8265-dc069ae4e863>
Fri, 11 Apr 2008 09:21:21 +0000 (09:21 +0000)
committerdecaro <decaro@f7af4fe6-9843-0410-8265-dc069ae4e863>
Fri, 11 Apr 2008 09:21:21 +0000 (09:21 +0000)
EVE/EveBase/AliEveCosmicRayFitter.cxx
EVE/EveBase/AliEveCosmicRayFitter.h

index 8bad243..b5953d5 100644 (file)
 #include "TGraph2D.h"
 #include "TGraph2DErrors.h"
 
+#include "TEveElement.h"
+
 #include "TVirtualFitter.h"
+#include "TLinearFitter.h"
 
 #include "TEveLine.h"
 
@@ -51,12 +54,17 @@ void SumDistance3D(Int_t &, Double_t *, Double_t & sum, Double_t * par, Int_t );
 ClassImp(AliEveCosmicRayFitter)
 
 AliEveCosmicRayFitter::AliEveCosmicRayFitter(const Text_t* name, Int_t n_points) :
-    TEvePointSet   (name, n_points),
+    TEvePointSet    (name, n_points),
+
+    fLine3DFitter   (0),
+    fLineFitter1    (0),
+    fLineFitter2    (0),
 
-    fLine3DFitter  (0),
+    fConnected      (kFALSE),
+    fSPMap          (),
+    fTrackList      (0),
 
-    fConnected     (kFALSE),
-    fSPMap         (),
+    fStartingPointX (390.),
 
     fGraphPicked1   (0),
     fGraphLinear1   (0),
@@ -69,11 +77,19 @@ AliEveCosmicRayFitter::AliEveCosmicRayFitter(const Text_t* name, Int_t n_points)
   // Constructor.
 
   fLine3DFitter = TVirtualFitter::Fitter(0, 4);
+  fLineFitter1 = new TLinearFitter(1, "hyp1");
+  fLineFitter2 = new TLinearFitter(1, "hyp1");
 
   SetMarkerColor(3);
   SetOwnIds(kFALSE);
 
-  fGraphPicked1 = new TGraph("Y vs X");
+  fTrackList = new TEveElementList("Cosmic ray");
+  fTrackList->SetTitle("muons");
+  fTrackList->SetMainColor((Color_t)8);
+  gEve->AddElement(fTrackList);
+  UpdateItems();
+
+  fGraphPicked1 = new TGraph();
   fGraphPicked1->SetName("Selected points");
   fGraphPicked1->SetMarkerColor(4);
   fGraphPicked1->SetMarkerStyle(4);  
@@ -83,7 +99,7 @@ AliEveCosmicRayFitter::AliEveCosmicRayFitter(const Text_t* name, Int_t n_points)
   fGraphLinear1->SetName("Fitted points");
   fGraphLinear1->SetMarkerColor(2);
 
-  fGraphPicked2 = new TGraph("Z vs X");
+  fGraphPicked2 = new TGraph();
   fGraphPicked2->SetName("Selected points");
   fGraphPicked2->SetMarkerColor(4);
   fGraphPicked2->SetMarkerStyle(4);  
@@ -93,7 +109,7 @@ AliEveCosmicRayFitter::AliEveCosmicRayFitter(const Text_t* name, Int_t n_points)
   fGraphLinear2->SetName("Fitted points");
   fGraphLinear2->SetMarkerColor(2);
 
-  fGraphPicked3 = new TGraph2D("Z vs Y vs X");
+  fGraphPicked3 = new TGraph2D();
   fGraphPicked3->SetName("Selected points");
   fGraphPicked3->SetMarkerColor(4);
   fGraphPicked3->SetMarkerStyle(4);  
@@ -110,6 +126,11 @@ AliEveCosmicRayFitter::~AliEveCosmicRayFitter()
   // Destructor.
 
   if(fLine3DFitter) delete fLine3DFitter;
+  if(fLineFitter1) delete fLineFitter1;
+  if(fLineFitter2) delete fLineFitter2;
+
+  fTrackList->DecDenyDestroy();
+  delete fTrackList;
 
 }
 
@@ -146,13 +167,17 @@ void AliEveCosmicRayFitter::Reset(Int_t n, Int_t ids)
 {
   // Reset selection.
 
+  if(fGraphPicked3) fGraphPicked3->Clear();
   if(fLine3DFitter) fLine3DFitter->Clear();
+  if(fLineFitter1) fLineFitter1->Clear();
+  if(fLineFitter2) fLineFitter2->Clear();
   TEvePointSet::Reset(n, ids);
   fSPMap.clear();
 }
 
 /**************************************************************************/
 
+//void AliEveCosmicRayFitter::AddFitPoint(TEvePointSet* ps, Int_t n)
 void AliEveCosmicRayFitter::AddFitPoint(Int_t n)
 { 
   // Add/remove given point depending if exists in the fSPMap.
@@ -160,7 +185,6 @@ void AliEveCosmicRayFitter::AddFitPoint(Int_t n)
   Float_t x, y, z;
   TEvePointSet* ps = dynamic_cast<TEvePointSet*>((TQObject*) gTQSender);
 
-
   std::map<Point_t, Int_t>::iterator g = fSPMap.find(Point_t(ps, n));
   if(g != fSPMap.end())
   {
@@ -178,7 +202,7 @@ void AliEveCosmicRayFitter::AddFitPoint(Int_t n)
     fSPMap[Point_t(ps, n)] = Size();
     ps->GetPoint(n, x, y, z);
     SetNextPoint(x, y, z); 
-    //  SetPointId(ps->GetPointId(n));
+    SetPointId(ps->GetPointId(n));
   }
   ResetBBox();
   ElementChanged(kTRUE, kTRUE);
@@ -193,17 +217,19 @@ void AliEveCosmicRayFitter::FitTrack()
   using namespace TMath;
 
   if(fLine3DFitter) delete fLine3DFitter;
+  if(fLineFitter1) delete fLineFitter1;
+  if(fLineFitter2) delete fLineFitter2;
   fLine3DFitter = TVirtualFitter::Fitter(0, 4);
+  fLineFitter1 = new TLinearFitter(1, "hyp1");
+  fLineFitter2 = new TLinearFitter(1, "hyp1");
 
   AliDebug(2, Form(" Selected point number %3i   %3i", fLastPoint+1, Size()));
 
   Double_t *posXpoint = new Double_t[fLastPoint+1];
   Double_t *posYpoint = new Double_t[fLastPoint+1];
   Double_t *posZpoint = new Double_t[fLastPoint+1];
-
-  //Double_t *errXpoint = new Double_t[fLastPoint+1];
-  //Double_t *errYpoint = new Double_t[fLastPoint+1];
-  //Double_t *errZpoint = new Double_t[fLastPoint+1];
+  Double_t *errYpoint = new Double_t[fLastPoint+1];
+  Double_t *errZpoint = new Double_t[fLastPoint+1];
 
   Double_t x, y, z;
 
@@ -213,17 +239,16 @@ void AliEveCosmicRayFitter::FitTrack()
     posXpoint[i] = x;
     posYpoint[i] = y;
     posZpoint[i] = z;
-    //errXpoint[i] = 0.001*posXpoint[i];
-    //errYpoint[i] = 0.05*posYpoint[i];
-    //errZpoint[i] = 0.05*posZpoint[i];
+    errYpoint[i] = 10.;//0.05*posYpoint[i];//arbitrary
+    errZpoint[i] = 10.;//0.05*posZpoint[i];//arbitrary
 
     fGraphPicked3->SetPoint(i,x,y,z);
-    //fGraphPicked3->SetPointError(i,errXpoint[i],errYpoint[i],errZpoint[i]);
+    //fGraphPicked3->SetPointError(i,errXpoint,errYpoint[i],errZpoint[i]);
 
     AliDebug(2, Form("  %f    %f    %f",
                     posXpoint[i], posYpoint[i], posZpoint[i]));
   }
-
+  /*
   fLine3DFitter->SetObjectFit(fGraphPicked3);
   fLine3DFitter->SetFCN(SumDistance3D);
   Double_t arglist[10];
@@ -245,16 +270,29 @@ void AliEveCosmicRayFitter::FitTrack()
   Double_t amin,edm, errdef;
   fLine3DFitter->GetStats(amin,edm,errdef,nvpar,nparx);
   fLine3DFitter->PrintResults(1,amin);
-
+  */
   TEveLine* line3D = new TEveLine();
-  line3D->SetLineColor(2);
-  line3D->SetLineWidth(2);
+  line3D->SetLineColor(4);
+  line3D->SetLineWidth(3);
   line3D->SetLineStyle(2);
 
+  AliDebug(2,Form(" fStartingPointX = %f\n",fStartingPointX));
+
   Double_t parFit3D[4];
-  for (int i = 0; i <4; ++i) 
+  for (int i = 0; i <4; ++i)
     parFit3D[i] = fLine3DFitter->GetParameter(i);
-  Float_t xCoor = 16000.;
+
+  Float_t xCoor = 0.;
+  xCoor = fStartingPointX;
+  /*
+  Float_t radicando = parFit3D[1]*parFit3D[1]*parFit3D[0]*parFit3D[0] -
+    (parFit3D[1]*parFit3D[1] + 1.)*(parFit3D[0]*parFit3D[0] -
+                                   fStartingPointX*fStartingPointX);
+  if (radicando>=0.) xCoor = (- parFit3D[1]*parFit3D[0] + TMath::Sqrt(radicando))/
+                      (parFit3D[1]*parFit3D[1] + 1.);
+                      else xCoor = fStartingPointX;
+  */
+
   Float_t yCoorP = parFit3D[0] + xCoor*parFit3D[1];
   Float_t zCoorP = parFit3D[2] + xCoor*parFit3D[3];
   Float_t yCoorM = parFit3D[0] - xCoor*parFit3D[1];
@@ -263,14 +301,45 @@ void AliEveCosmicRayFitter::FitTrack()
   line3D->SetNextPoint( xCoor, yCoorP, zCoorP);
   line3D->SetNextPoint(-xCoor, yCoorM, zCoorM);
 
-  gEve->AddElement(line3D);
+  //gEve->AddElement(line3D, fTrackList);
 
-  delete posXpoint;
-  delete posYpoint;
-  delete posZpoint;
-  //delete errXpoint;
-  //delete errYpoint;
-  //delete errZpoint;
+  fLineFitter1->AssignData(fLastPoint+1, 1, posXpoint, posYpoint, errYpoint);
+  fLineFitter2->AssignData(fLastPoint+1, 1, posXpoint, posZpoint, errZpoint);
+
+  Int_t fitResult1 = fLineFitter1->Eval();
+  Int_t fitResult2 = fLineFitter2->Eval();
+  if (fitResult1 || fitResult2) {
+    AliInfo(Form(" linear fit result is not ok (%1i, %1i)", fitResult1, fitResult2));
+    return;
+  }
+
+  // make track
+  TVectorD params1;
+  TVectorD params2;
+  fLineFitter1->GetParameters(params1);
+  fLineFitter2->GetParameters(params2);
+
+  TEveLine* line3Dbis = new TEveLine();
+  line3Dbis->SetLineColor(4);
+  line3Dbis->SetLineWidth(5);
+  line3Dbis->SetLineStyle(2);
+
+  xCoor = fStartingPointX;
+  yCoorP = params1(0) + xCoor*params1(1);
+  zCoorP = params2(0) + xCoor*params2(1);
+  yCoorM = params1(0) - xCoor*params1(1);
+  zCoorM = params2(0) - xCoor*params2(1);
+
+  line3Dbis->SetNextPoint( xCoor, yCoorP, zCoorP);
+  line3Dbis->SetNextPoint(-xCoor, yCoorM, zCoorM);
+
+  gEve->AddElement(line3Dbis, fTrackList);
+
+  delete [] posXpoint;
+  delete [] posYpoint;
+  delete [] posZpoint;
+  delete [] errYpoint;
+  delete [] errZpoint;
 
 }
 
@@ -281,6 +350,8 @@ void AliEveCosmicRayFitter::DestroyElements()
   // It preserves track list to have coomon track propagator attributes.
 
   TEveElement::DestroyElements();
+  gEve->AddElement(fTrackList, this);
+  fTrackList->DestroyElements();
   UpdateItems();
 }
 
@@ -293,7 +364,7 @@ void AliEveCosmicRayFitter::DrawDebugGraph()
 
   static const TEveException eH("CosmicRayFitter::DrawDebugGraph ");
 
-  if(fLine3DFitter == 0)
+  if(fLineFitter1 == 0 || fLineFitter2 == 0)
     throw(eH + "fitter not set.");
 
   AliDebug(2,Form("           %3i   %3i\n", fLastPoint+1, Size()));
@@ -303,8 +374,17 @@ void AliEveCosmicRayFitter::DrawDebugGraph()
   fGraphLinear1->Set(nR);
   fGraphPicked2->Set(nR);
   fGraphLinear2->Set(nR);
-  fGraphLinear3->Set(nR);
 
+  TVectorD params1;
+  TVectorD params2;
+  fLineFitter1->GetParameters(params1);
+  fLineFitter2->GetParameters(params2);
+  /*
+  TVectorD sy;
+  fLineFitter1->GetErrors(sy);
+  TVectorD sz;
+  fLineFitter2->GetErrors(sz);
+  */
   Double_t *x = new Double_t[nR];
   Double_t *y = new Double_t[nR];
   Double_t *z = new Double_t[nR];
@@ -317,6 +397,7 @@ void AliEveCosmicRayFitter::DrawDebugGraph()
     AliDebug(2,Form("  %f      %f      %f\n", x[i], y[i], z[i]));
   }
 
+  fGraphLinear3->Set(nR);
   Double_t parFit3D[4];
    for (int i = 0; i <4; ++i) 
       parFit3D[i] = fLine3DFitter->GetParameter(i);
@@ -325,20 +406,24 @@ void AliEveCosmicRayFitter::DrawDebugGraph()
   Double_t zCoor = 0.;
   for (Int_t i=0; i<nR; i++)
   {
-    yCoor = parFit3D[0] + x[i]*parFit3D[1];
-    zCoor = parFit3D[2] + x[i]*parFit3D[3];
+    yCoor = params1(0) + x[i]*params1(1);
+    zCoor = params2(0) + x[i]*params2(1);
     fGraphPicked1->SetPoint(i, x[i], y[i]);
     fGraphLinear1->SetPoint(i, x[i], yCoor);
-    //fGraphLinear1->SetPointError(i, TMath::Abs(x[i])*0.001, TMath::Abs(yCoor)*0.05);
+    fGraphLinear1->SetPointError(i, TMath::Abs(x[i])*0.001, TMath::Abs(yCoor)*0.05);
     fGraphPicked2->SetPoint(i, x[i], z[i]);
     fGraphLinear2->SetPoint(i, x[i], zCoor);
-    //fGraphLinear2->SetPointError(i, TMath::Abs(x[i])*0.001, TMath::Abs(zCoor)*0.05);
-
-    fGraphLinear3->SetPoint(i, x[i], yCoor, zCoor);
-    //fGraphLinear3->SetPointError(i, TMath::Abs(x[i])*0.001, TMath::Abs(yCoor)*0.05, TMath::Abs(zCoor)*0.05);
+    fGraphLinear2->SetPointError(i, TMath::Abs(x[i])*0.001, TMath::Abs(zCoor)*0.05);
+
+    fGraphLinear3->SetPoint(i, x[i],
+                           parFit3D[0]+x[i]*parFit3D[1],
+                           parFit3D[2]+x[i]*parFit3D[3]);
+    fGraphLinear3->SetPointError(i, TMath::Abs(x[i])*0.001,
+                                TMath::Abs(parFit3D[0]+x[i]*parFit3D[1])*0.05,
+                                TMath::Abs(parFit3D[2]+x[i]*parFit3D[3])*0.05);
   }
   
-  TCanvas * canvas=0;
+  TCanvas * canvas = 0;
   if (gPad) gPad->Clear();
   else if (gPad==0 || gPad->GetCanvas()->IsEditable() == kFALSE) {
     canvas = new TCanvas("canvas", "CosmicRayFitter", 800, 800);
@@ -415,15 +500,15 @@ void SumDistance3D(Int_t &, Double_t *, Double_t & sum, Double_t * par, Int_t )
   sum = 0;
   Double_t d = 0;
   for (Int_t i  = 0; i < npoints; ++i) { 
-    d = Distance3D(x[i],y[i],z[i],par); 
+    d = Distance3D(x[i],y[i],z[i],par);
     sum += d;
 #ifdef DEBUG
-    std::cout << " point " << i << "\t" 
+    std::cout << " point " << i << "\t"
              << x[i] << "\t" 
              << y[i] << "\t" 
              << z[i] << "\t" 
              << std::sqrt(d) << std::endl; 
-    std::cout << " Total sum2 = " << sum << std::endl;
+    std::cout << " Total sum3D = " << sum << std::endl;
 #endif
 
   }
@@ -434,7 +519,7 @@ void SumDistance3D(Int_t &, Double_t *, Double_t & sum, Double_t * par, Int_t )
 Double_t Distance3D(Double_t x, Double_t y, Double_t z, Double_t *p)
 {
   //
-  // distance line point is D = | (xp-x0) cross  ux | 
+  // distance line point is D = | (xp-x0) cross  ux |
   // where ux is direction of line and x0 is a point in the line (like t = 0) 
   //
 
index b122892..01934a7 100644 (file)
@@ -20,13 +20,15 @@ class TGraph2D;
 class TLinearFitter;
 class TVirtualFitter;
 
+class TEveElement;
+
 class AliEveCosmicRayFitter : public TEvePointSet
 {
 public:
   AliEveCosmicRayFitter(const Text_t* name = "CosmicRayFitter", Int_t n_points=0);
   virtual ~AliEveCosmicRayFitter();
   
-  void AddFitPoint(Int_t); // slot for PointCtrlClicked() signal
+  void AddFitPoint(Int_t); // slot for PointSelected() signal
 
   virtual void       Start();
   virtual void       Stop();
@@ -34,6 +36,15 @@ public:
   virtual void  Reset(Int_t n_points=0, Int_t n_int_ids=0);
 
   Bool_t       GetConnected() { return fConnected; }
+
+  void          DrawDebugGraph();
+
+  virtual void  DestroyElements(); // *MENU*
+
+private:
+  AliEveCosmicRayFitter(const AliEveCosmicRayFitter&);            // Not implemented
+  AliEveCosmicRayFitter& operator=(const AliEveCosmicRayFitter&); // Not implemented
+
   TVirtualFitter* GetLine3DFitter(){ return fLine3DFitter; }
 
   TGraph*         GetGraphSelected1() { return fGraphPicked1; }
@@ -42,9 +53,6 @@ public:
   TGraphErrors*   GetGraphFitted1() { return fGraphLinear1; }
   TGraphErrors*   GetGraphFitted2() { return fGraphLinear2; }
   TGraph2DErrors* GetGraphFitted3() { return fGraphLinear3; }
-  void          DrawDebugGraph();
-
-  virtual void  DestroyElements(); // *MENU*
 
 protected:
   struct Point_t
@@ -69,21 +77,24 @@ protected:
   typedef std::map<Point_t, Int_t>          PointMap_t; // Map of registered points.
 
   TVirtualFitter* fLine3DFitter; // 3D straight line fitter
+  TLinearFitter*  fLineFitter1;  // 2D straight line fitter
+  TLinearFitter*  fLineFitter2;  // 2D straight line fitter
 
-  Bool_t     fConnected;         // object connected to pointset Ctrl-shift signal 
+  Bool_t          fConnected;    // object connected to pointset Ctrl-shift signal
   
   PointMap_t fSPMap;             // map of selected points from different PointSet
 
-  TGraph            *fGraphPicked1; // graph of selected points debug info
-  TGraphErrors      *fGraphLinear1; // graph of fitted points for debug info
-  TGraph            *fGraphPicked2; // graph of selected points debug info
-  TGraphErrors      *fGraphLinear2; // graph of fitted points for debug info
-  TGraph2D          *fGraphPicked3; // graph of selected points debug info
-  TGraph2DErrors    *fGraphLinear3; // graph of fitted points for debug info
+  TEveElementList*   fTrackList; // list of tracks removed in the destructor
 
-private:
-  AliEveCosmicRayFitter(const AliEveCosmicRayFitter&);            // Not implemented
-  AliEveCosmicRayFitter& operator=(const AliEveCosmicRayFitter&); // Not implemented
+  Float_t       fStartingPointX; // starting point (in X direction)
+                                // to plot the fitted line
+
+  TGraph         *fGraphPicked1; // graph of selected points debug info
+  TGraphErrors   *fGraphLinear1; // graph of fitted points for debug info
+  TGraph         *fGraphPicked2; // graph of selected points debug info
+  TGraphErrors   *fGraphLinear2; // graph of fitted points for debug info
+  TGraph2D       *fGraphPicked3; // graph of selected points debug info
+  TGraph2DErrors *fGraphLinear3; // graph of fitted points for debug info
 
   ClassDef(AliEveCosmicRayFitter, 0); // Interface to TEvePointSet allowing 3D straight linear fit.
 };