TrdMCClusterR Fit Classifier
Line.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <utility>
5 #include <cmath>
6 
7 #include <TF3.h>
8 
9 #include "LinearAlgebra.h"
10 
11 
12 namespace Line
13 {
14  //____________________________________________________________________________
15  struct FitLine2D
16  {
17  double m; // slope
18  double c; // y-intercept
19  double err_m; // uncertainty in slope
20  double err_c; // uncertainty in y-intercept
21  double fit_chi2ndf; // goodness of linear fit
22  int trkID; // AMS trkID
23  int g3PID; // Geant3 PID of track
24 
25  FitLine2D(double m, double c, double err_m, double err_c, double fit_chi2ndf, int trkID, int g3PID)
27  };
28 
29 
30  //____________________________________________________________________________
32  {
33  int trkID;
34  int g3PID;
35 
36  bool fitSuccess; // Flag to indicate if the fit was successful
37  std::vector<double> fitParameters; // Fit parameters
38  std::vector<double> fitErrors; // Errors on fit parameters
39 
40  double chi2; // Chi-square of the fit
41  int ndf; // Degrees of freedom
42 
43  FitLine3DMinuit() : trkID(-99999999), g3PID(-99999999), fitSuccess(false), chi2(0.0), ndf(0) {} // default constructor
44  };
45 
46 
47  //__________________________________________________________________________
48  struct FitLine3D
49  {
50  TFitResultPtr fit; // Store TFitResultPtr instead of TF3
51  int trkID;
52  int g3PID;
53 
54  FitLine3D() : trkID(-99999999), g3PID(-99999999) {} // default constructor
55  };
56 
57 
58  //___________________________________________________________________________
60  {
61  int trkID;
62  int g3PID;
63  std::vector<double> point;
64  std::vector<double> direction;
65 
67  Line3DParametricForm() : trkID(-99999999), g3PID(-99999999), point(), direction() {}
68 
70  Line3DParametricForm(int trkID, int g3PID, const std::vector<double>& pt, const std::vector<double>& dir)
71  : trkID(trkID), g3PID(g3PID), point(pt), direction(dir) {}
72  };
73 
74 
75  //___________________________________________________________________________
76  Line3DParametricForm Getline3DParametricForm(const double& m1, const double& b1,
77  const double& m2, const double& b2,
78  const double& m3, const double& b3,
79  const int& trkID,
80  const int& g3PID,
81  bool debugMode)
82  {
85 
87  // const std::vector<double> vX0Y0Z0 = LA::SolveSystemOfThreeEquations(m1, b1, m2, b2, m3, b3, debugMode);
88 
90  const std::vector<double> vX0Y0Z0 = LA::ROOTEquationSolver(m1, b1, m2, b2, m3, b3, debugMode);
91 
92  if (vX0Y0Z0.size() != 3)
93  return Line3DParametricForm();
94 
96  double x0 = vX0Y0Z0.at(0);
97  double y0 = vX0Y0Z0.at(1);
98  double z0 = vX0Y0Z0.at(2);
99 
101  double a = 1; // x does not change if parallel to x-axis
102  double b = m3; // y does not change if parallel to y-axis
103  double c = m1; // Use m2 if line is parallel to x or y-axis
104 
105  if (debugMode)
106  {
108  std::cout << "\nThe parametric form of the 3D line is:" << std::endl;
109  std::cout << "x = " << x0 << " + " << a << ".t" << std::endl;
110  std::cout << "y = " << y0 << " + " << b << ".t" << std::endl;
111  std::cout << "z = " << z0 << " + " << c << ".t" << std::endl;
112  }
113 
114  const std::vector<double> vDir = {a, b, c};
115  return Line3DParametricForm(trkID, g3PID, vX0Y0Z0, vDir);
116  }
117 
118 
119 
120 
121  //__________________________________________________________________________
122  double distanceFromLine3D(double *coords, double *params)
123  {
144  //______________________________________
146 
148  double x = coords[0];
149  double y = coords[1];
150  double z = coords[2];
151 
153  double px = params[0];
154  double py = params[1];
155  double pz = params[2];
156  double vx = params[3];
157  double vy = params[4];
158  double vz = params[5];
159 
160  //______________________________________
162 
171  double u = ((x - px) * vx + (y - py) * vy + (z - pz) * vz) / (vx * vx + vy * vy + vz * vz);
172 
173 
179  double dx = px + u * vx - x;
180  double dy = py + u * vy - y;
181  double dz = pz + u * vz - z;
182 
183 
190  return std::sqrt(dx*dx + dy*dy + dz*dz);
191  }
192 }
std::vector< double > ROOTEquationSolver(const double &m1, const double &b1, const double &m2, const double &b2, const double &m3, const double &b3, bool debugMode)
Definition: LinearAlgebra.h:19
Definition: Line.h:13
Line3DParametricForm Getline3DParametricForm(const double &m1, const double &b1, const double &m2, const double &b2, const double &m3, const double &b3, const int &trkID, const int &g3PID, bool debugMode)
Definition: Line.h:76
double distanceFromLine3D(double *coords, double *params)
Definition: Line.h:122
Definition: Line.h:16
double fit_chi2ndf
Definition: Line.h:21
double c
Definition: Line.h:18
double err_m
Definition: Line.h:19
double m
Definition: Line.h:17
FitLine2D(double m, double c, double err_m, double err_c, double fit_chi2ndf, int trkID, int g3PID)
Definition: Line.h:25
int g3PID
Definition: Line.h:23
int trkID
Definition: Line.h:22
double err_c
Definition: Line.h:20
Definition: Line.h:32
int ndf
Definition: Line.h:41
FitLine3DMinuit()
Definition: Line.h:43
bool fitSuccess
Definition: Line.h:36
int trkID
Definition: Line.h:33
int g3PID
Definition: Line.h:34
double chi2
Definition: Line.h:40
std::vector< double > fitErrors
Definition: Line.h:38
std::vector< double > fitParameters
Definition: Line.h:37
Definition: Line.h:49
FitLine3D()
Definition: Line.h:54
int g3PID
Definition: Line.h:52
int trkID
Definition: Line.h:51
TFitResultPtr fit
Definition: Line.h:50
Definition: Line.h:60
std::vector< double > point
Definition: Line.h:63
Line3DParametricForm()
Vector containing the direction ratios (a, b, c)
Definition: Line.h:67
std::vector< double > direction
Vector containing the point on the line (x0, y0, z0)
Definition: Line.h:64
Line3DParametricForm(int trkID, int g3PID, const std::vector< double > &pt, const std::vector< double > &dir)
Initialize with empty vectors.
Definition: Line.h:70
int g3PID
Definition: Line.h:62
int trkID
Definition: Line.h:61