TrdMCClusterR Fit Classifier
ClusterFitter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include<TGraph.h>
4 #include<TF1.h>
5 #include<TFitResult.h>
6 
7 #include<vector>
8 
9 #include "TRDConstants.h"
10 #include "Line.h"
11 
12 using std::cerr;
13 using std::endl;
14 
15 
16 namespace ClusterFitter
17 {
18  //__________________________________________________________________________________________________________________________
19  std::vector<Line::FitLine2D> GetClusterProjectionLinearFits(const std::vector<double>& TrkClusters_x,
20  const std::vector<double>& TrkClusters_y,
21  const std::vector<double>& TrkClusters_z,
22  const int& trkID,
23  const int& g3PID,
24  const bool& debug = false,
25  const double threshold = 1000
26  )
27  {
32  if (debug == true)
33  {
34  printf("\nClusterFitter:");
35  printf("\nX\n"); VectorUtilities::printVector(TrkClusters_x);
36  printf("\nY\n"); VectorUtilities::printVector(TrkClusters_y);
37  printf("\nZ\n"); VectorUtilities::printVector(TrkClusters_z);
38  }
39 
40 
42  if (TrkClusters_x.size() != TrkClusters_y.size() || TrkClusters_x.size() != TrkClusters_z.size())
43  {
45  cerr<<"\n\nERROR! Argument cluster vector lengths are not equal.\n";
46  cerr<<"SizeX = "<<TrkClusters_x.size()<<", SizeY = "<<TrkClusters_y.size()<<", SizeZ = "<<TrkClusters_z.size()<<endl;
47  cerr<<"Returning empty vector\n\n";
48  return {};
49  }
50 
52  if (debug == true)
53  cerr<<"\nClusterFitter: number of clusters to be fitted for this tracks = "<<TrkClusters_x.size()<<endl;
54  if (TrkClusters_x.size() < 2)
55  return {};
56 
57 
58  TGraph * graph_clusters_XZ = new TGraph(TrkClusters_x.size());
59  TGraph * graph_clusters_YZ = new TGraph(TrkClusters_x.size());
60  TGraph * graph_clusters_XY = new TGraph(TrkClusters_x.size());
61  for(unsigned int iclust = 0; iclust < TrkClusters_x.size(); iclust++)
62  {
63  graph_clusters_XZ->SetPoint((int)iclust, TrkClusters_x[iclust], TrkClusters_z[iclust]);
64  graph_clusters_YZ->SetPoint((int)iclust, TrkClusters_y[iclust], TrkClusters_z[iclust]);
65  graph_clusters_XY->SetPoint((int)iclust, TrkClusters_x[iclust], TrkClusters_y[iclust]);
66  }
67  TF1 * lin_fit_XZ = new TF1("track_XZ", "[0]*x+[1]", TRD_XYMIN, TRD_XYMAX);
68  TF1 * lin_fit_YZ = new TF1("track_YZ", "[0]*x+[1]", TRD_XYMIN, TRD_XYMAX);
69  TF1 * lin_fit_XY = new TF1("track_XY", "[0]*x+[1]", TRD_XYMIN, TRD_XYMAX);
70 
71  graph_clusters_XZ->Fit(lin_fit_XZ, "SQ"); //"V" for verbose fitting
72  graph_clusters_YZ->Fit(lin_fit_YZ, "SQ");
73  graph_clusters_XY->Fit(lin_fit_XY, "SQ");
74  TFitResultPtr r1 = graph_clusters_XZ->Fit(lin_fit_XZ, "SQ");
75  TFitResultPtr r2 = graph_clusters_YZ->Fit(lin_fit_YZ, "SQ");
76  TFitResultPtr r3 = graph_clusters_XY->Fit(lin_fit_XY, "SQ");
77 
78  double fit_xz_chi2ndf = r1->Chi2()/r1->Ndf();
79  double fit_yz_chi2ndf = r2->Chi2()/r2->Ndf();
80  double fit_xy_chi2ndf = r3->Chi2()/r3->Ndf();
81 
82  delete graph_clusters_XZ;
83  delete graph_clusters_YZ;
84  delete graph_clusters_XY;
85  delete lin_fit_XZ;
86  delete lin_fit_YZ;
87  delete lin_fit_XY;
88 
89  double mXZ = r1->Parameter(0);
90  double mPerErrXZ = 100*fabs(r1->ParError(0)/mXZ);
91  double sXZ = r1->Parameter(1);
92  double sPerErrXZ = 100*fabs(r1->ParError(1)/sXZ);
93 
94  double mYZ = r2->Parameter(0);
95  double mPerErrYZ = 100*fabs(r2->ParError(0)/mYZ);
96  double sYZ = r2->Parameter(1);
97  double sPerErrYZ = 100*fabs(r2->ParError(1)/sYZ);
98 
99  double mXY = r3->Parameter(0);
100  double mPerErrXY = 100*fabs(r3->ParError(0)/mXY);
101  double sXY = r3->Parameter(1);
102  double sPerErrXY = 100*fabs(r3->ParError(1)/sXY);
103 
104  if (debug == true)
105  {
106  printf("Fit XZ: M = %14.7f, %%errM = %14.7f, C = %14.7f, %%errC = %14.7f, Chi-sq/ndf = %14.7f\n", mXZ, mPerErrXZ, sXZ, sPerErrXZ, fit_xz_chi2ndf);
107  printf("Fit YZ: M = %14.7f, %%errM = %14.7f, C = %14.7f, %%errC = %14.7f, Chi-sq/ndf = %14.7f\n", mYZ, mPerErrYZ, sYZ, sPerErrYZ, fit_yz_chi2ndf);
108  printf("Fit XY: M = %14.7f, %%errM = %14.7f, C = %14.7f, %%errC = %14.7f, Chi-sq/ndf = %14.7f\n", mXY, mPerErrXY, sXY, sPerErrXY, fit_xy_chi2ndf);
109  }
110 
111  if (fit_xz_chi2ndf > threshold || fit_yz_chi2ndf > threshold || fit_xy_chi2ndf > threshold)
112  {
113  if (debug == true)
114  cerr<<"\n\nINFO: Fit is poorer than provided threshold. Returning empty vector.\n\n";
115  return {};
116  }
117 
118  Line::FitLine2D lineXZ (mXZ, sXZ, r1->ParError(0), r1->ParError(1), fit_xz_chi2ndf, trkID, g3PID);
119  Line::FitLine2D lineYZ (mYZ, sYZ, r2->ParError(0), r2->ParError(1), fit_yz_chi2ndf, trkID, g3PID);
120  Line::FitLine2D lineXY (mXY, sXY, r3->ParError(0), r3->ParError(1), fit_xy_chi2ndf, trkID, g3PID);
121  return {lineXZ, lineYZ, lineXY};
122  }
123 }
const int TRD_XYMAX
Definition: TRDConstants.h:8
const int TRD_XYMIN
cm
Definition: TRDConstants.h:9
Definition: ClusterFitter.h:17
std::vector< Line::FitLine2D > GetClusterProjectionLinearFits(const std::vector< double > &TrkClusters_x, const std::vector< double > &TrkClusters_y, const std::vector< double > &TrkClusters_z, const int &trkID, const int &g3PID, const bool &debug=false, const double threshold=1000)
Definition: ClusterFitter.h:19
void printVector(const std::vector< T > &v)
Definition: Utilities.h:58
Definition: Line.h:16