CbmRoot
RapidityFit.cxx
Go to the documentation of this file.
1 #include "RapidityFit.h"
2 
3 #include "Minuit2/FCNBase.h"
4 #include "Minuit2/FunctionMinimum.h"
5 #include "Minuit2/MnHesse.h"
6 #include "Minuit2/MnMigrad.h"
7 #include "Minuit2/MnMinos.h"
8 #include "Minuit2/MnPrint.h"
9 #include "Minuit2/MnUserParameterState.h"
10 #include "Minuit2/SimplexMinimizer.h"
11 #include "TMath.h"
12 
13 #include <fstream>
14 
15 
16 using namespace ROOT::Minuit2;
17 
18 
20  double TwoGauss(double y, double A, double sig, double y0) {
21  return A / sqrt(2. * TMath::Pi()) / sig / 2.
22  * (TMath::Exp(-(y - y0) * (y - y0) / 2. / sig / sig)
23  + TMath::Exp(-(y + y0) * (y + y0) / 2. / sig / sig));
24  }
25 
26  class DoubleGaussFCN : public FCNBase {
27 
28  public:
29  DoubleGaussFCN(const std::vector<double>& y_,
30  const std::vector<double>& dndy_,
31  const std::vector<double>& err_)
32  : fPositions(y_)
33  , fMeasurements(dndy_)
34  , fErrors(err_)
35  // fPositions(y_), fMeasurements(dndy_), fErrors(err_), iter(0)
36  {}
37 
39 
40  double operator()(const std::vector<double>& par) const {
41  double chi2 = 0.;
42  for (unsigned int i = 0; i < fPositions.size(); ++i) {
43  double val = TwoGauss(fPositions[i], par[0], par[1], par[2]);
44  chi2 += (fMeasurements[i] - val) * (fMeasurements[i] - val) / fErrors[i]
45  / fErrors[i];
46  }
47  return chi2;
48  }
49 
50  double Up() const { return 1.; }
51 
52  private:
53  std::vector<double> fPositions;
54  std::vector<double> fMeasurements;
55  std::vector<double> fErrors;
56  // int iter;
57  };
58 } // namespace RapidityFitNamespace
59 
60 using namespace RapidityFitNamespace;
61 
63 
65  DoubleGaussFCN mfunc(y, dndy, err);
66  std::vector<double> params(3, 0.);
67  params[0] = Parameters.A.value;
68  params[1] = Parameters.sig.value;
69  params[2] = Parameters.yav.value;
70  MnUserParameters upar;
71  upar.Add("A",
72  Parameters.A.value,
73  Parameters.A.error,
74  Parameters.A.xmin,
75  Parameters.A.xmax);
76  upar.Add("sig",
77  Parameters.sig.value,
78  Parameters.sig.error,
79  Parameters.sig.xmin,
80  Parameters.sig.xmax);
81  upar.Add("yav",
82  Parameters.yav.value,
83  Parameters.yav.error,
84  Parameters.yav.xmin,
85  Parameters.yav.xmax);
86  int nparams = 3;
87 
88  MnMigrad migrad(mfunc, upar);
89  FunctionMinimum min = migrad();
90  MnHesse hess;
91  hess(mfunc, min);
92  RapidityFitParameters ret = Parameters;
93  ret.A.value = (min.UserParameters()).Params()[0];
94  ret.A.error = (min.UserParameters()).Errors()[0];
95  ret.sig.value = (min.UserParameters()).Params()[1];
96  ret.sig.error = (min.UserParameters()).Errors()[1];
97  ret.yav.value = (min.UserParameters()).Params()[2];
98  ret.yav.error = (min.UserParameters()).Errors()[2];
99 
100  params[0] = ret.A.value;
101  params[1] = ret.sig.value;
102  params[2] = ret.yav.value;
103  std::cout << "A = " << params[0] << " "
104  << " sig = " << params[1] << " "
105  << " y0 = " << params[2] << " "
106  << " chi2/ndf = " << mfunc(params) / (y.size() - nparams)
107  << std::endl;
108 
109  ret.chi2ndf = mfunc(params) / (y.size() - nparams);
110 
111  return ret;
112 }
RapidityFitParameter::error
double error
Definition: RapidityFit.h:11
RapidityFitParameter::value
double value
Definition: RapidityFit.h:10
RapidityFitParameters
Definition: RapidityFit.h:28
RapidityFitNamespace::DoubleGaussFCN::DoubleGaussFCN
DoubleGaussFCN(const std::vector< double > &y_, const std::vector< double > &dndy_, const std::vector< double > &err_)
Definition: RapidityFit.cxx:29
RapidityFitNamespace::DoubleGaussFCN
Definition: RapidityFit.cxx:26
RapidityFitParameters::sig
RapidityFitParameter sig
Definition: RapidityFit.h:29
sqrt
friend F32vec4 sqrt(const F32vec4 &a)
Definition: L1/vectors/P4_F32vec4.h:41
RapidityFitParameters::A
RapidityFitParameter A
Definition: RapidityFit.h:29
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
RapidityFitNamespace::DoubleGaussFCN::fMeasurements
std::vector< double > fMeasurements
Definition: RapidityFit.cxx:54
RapidityFitNamespace::DoubleGaussFCN::operator()
double operator()(const std::vector< double > &par) const
Definition: RapidityFit.cxx:40
RapidityFit.h
RapidityFitNamespace
Definition: RapidityFit.cxx:19
RapidityFit::PerformFit
RapidityFitParameters PerformFit()
Definition: RapidityFit.cxx:64
min
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:33
RapidityFitParameters::chi2ndf
double chi2ndf
Definition: RapidityFit.h:30
RapidityFitBlastWaveNamespace::dndy
double dndy(double y, double A, double eta, double T, double mass)
Definition: RapidityFitBlastWave.cxx:20
RapidityFitNamespace::DoubleGaussFCN::fErrors
std::vector< double > fErrors
Definition: RapidityFit.cxx:55
RapidityFitParameters::yav
RapidityFitParameter yav
Definition: RapidityFit.h:29
xMath::Pi
double Pi()
Definition: xMath.h:5
RapidityFitNamespace::TwoGauss
double TwoGauss(double y, double A, double sig, double y0)
Definition: RapidityFit.cxx:20
RapidityFitNamespace::DoubleGaussFCN::fPositions
std::vector< double > fPositions
Definition: RapidityFit.cxx:53
RapidityFitNamespace::DoubleGaussFCN::~DoubleGaussFCN
~DoubleGaussFCN()
Definition: RapidityFit.cxx:38
y
Double_t y
Definition: CbmMvdSensorDigiToHitTask.cxx:68
RapidityFitNamespace::DoubleGaussFCN::Up
double Up() const
Definition: RapidityFit.cxx:50
RapidityFit::~RapidityFit
~RapidityFit(void)
Definition: RapidityFit.cxx:62