TrdMCClusterR Fit Classifier
ClusterFitter3DMinuit.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <TH3D.h>
4 #include <TFile.h>
5 #include <TMinuit.h>
6 #include <TFitResult.h>
7 
8 #include "Line.h"
9 #include "AMSTrdMCTrack.h"
10 
11 #include <vector>
12 #include <string>
13 
14 //
15 #include <Math/Functor.h>
16 #include <TPolyLine3D.h>
17 #include <Math/Vector3D.h>
18 #include <Fit/Fitter.h>
19 
20 using namespace ROOT::Math;
21 
22 using std::cerr;
23 using std::endl;
24 
25 
26 // Global variables to store data points for Minuit function
27 TH3D* gHistogram = nullptr;
28 
29 
31 {
32 
33  // Objective function to be minimized
34  void objectiveFunction(int& nDim, double* gout, double& result, double params[], int flag)
35  {
36  result = 0.0;
37  for (int ix = 1; ix <= gHistogram->GetNbinsX(); ++ix) {
38  for (int iy = 1; iy <= gHistogram->GetNbinsY(); ++iy) {
39  for (int iz = 1; iz <= gHistogram->GetNbinsZ(); ++iz) {
40  if (gHistogram->GetBinContent(ix, iy, iz) > 0)
41  {
42  double coords[3];
43  coords[0] = gHistogram->GetXaxis()->GetBinCenter(ix);
44  coords[1] = gHistogram->GetYaxis()->GetBinCenter(iy);
45  coords[2] = gHistogram->GetZaxis()->GetBinCenter(iz);
46  result += Line::distanceFromLine3D(coords, params);
47  }
48  }
49  }
50  }
51  }
52 
53 
54  Line::FitLine3DMinuit fitClustersToLine3D(const AMSTrdMCTrack::Track &track, const int& eventID, TFile * file)
55  {
56  Line::FitLine3DMinuit result;
57  result.trkID = track.trkID;
58  result.g3PID = track.g3PID;
59 
60  TH3D * histogram = AMSTrdMCTrack::setClustersInTH3D(track);
61  gHistogram = histogram;
62  TMinuit minuit(6);
63  minuit.SetFCN(objectiveFunction);
64 
65  // Initialize parameters
66  // (Int_t parNo, const char *name, Double_t initVal, Double_t initErr, Double_t lowerLimit, Double_t upperLimit)
67  minuit.DefineParameter(0, "px", 0, 0.1, -100, 100); // positions
68  minuit.DefineParameter(1, "py", 0, 0.1, -100, 100);
69  minuit.DefineParameter(2, "pz", 100, 0.1, 90, 140);
70  minuit.DefineParameter(3, "vx", 1, 0.1, -1000, 1000); // directions
71  minuit.DefineParameter(4, "vy", 0, 0.1, -1000, 1000);
72  minuit.DefineParameter(5, "vz", 0, 0.1, -1000, 1000);
73 
74  // Minimize
75  minuit.Migrad();
76 
77  // Extract parameters and errors
78  for (int i = 0; i < 6; i++) {
79  double paramValue, paramError;
80  minuit.GetParameter(i, paramValue, paramError);
81  result.fitParameters.push_back(paramValue);
82  result.fitErrors.push_back(paramError);
83  }
84 
85  // Optionally, get the chi-square and degrees of freedom
86  // result.chi2 = minuit.GetChisquare();
87  // result.ndf = minuit.GetNDF();
88 
89  // Check if the fit was successful
90  result.fitSuccess = (minuit.GetStatus() == 0); // Assuming status 0 indicates a successful fit
91 
92  printf("\n\nFor track.trkID = %d and g3PID: %d: minuit.GetStatus() = %d\n\n\n", track.trkID, track.g3PID, minuit.GetStatus());
93 
94  // if (file && result.fitResult)
95  {
96  file->cd();
97  histogram->Write(Form("graph2D_%d_%d_%d_%d", eventID, track.trkID, track.g3PID, track.clusters.size()));
98  }
99 
100  delete histogram;
101  return result;
102  }
103 }
TH3D * gHistogram
Definition: ClusterFitter3DMinuit.h:27
Definition: ClusterFitter3DMinuit.h:31
Line::FitLine3DMinuit fitClustersToLine3D(const AMSTrdMCTrack::Track &track, const int &eventID, TFile *file)
Definition: ClusterFitter3DMinuit.h:54
void objectiveFunction(int &nDim, double *gout, double &result, double params[], int flag)
Definition: ClusterFitter3DMinuit.h:34
double distanceFromLine3D(double *coords, double *params)
Definition: Line.h:122
Definition: AMSTrdMCTrack.h:49
int trkID
Definition: AMSTrdMCTrack.h:50
std::vector< Cluster > clusters
Definition: AMSTrdMCTrack.h:52
int g3PID
Definition: AMSTrdMCTrack.h:51
Definition: Line.h:32
bool fitSuccess
Definition: Line.h:36
int trkID
Definition: Line.h:33
int g3PID
Definition: Line.h:34
std::vector< double > fitErrors
Definition: Line.h:38
std::vector< double > fitParameters
Definition: Line.h:37