AttenuationFit.h
Go to the documentation of this file.
1 // \author Darko Veberic
2 // \date 2014
3 
4 #ifndef _cic_AttenuationFit_h_
5 #define _cic_AttenuationFit_h_
6 
7 #include <Minuit2/FCNBase.h>
8 #include <Minuit2/MnMinimize.h>
9 #include <Minuit2/MnHesse.h>
10 #include <Minuit2/FunctionMinimum.h>
11 #include <Minuit2/MnUserParameters.h>
12 
13 
14 namespace cic {
15 
16  namespace AttenuationFit {
17 
18  typedef std::function<double(const double, const std::vector<double>&)> Attenuation;
19 
20  typedef std::function<double(double, double)> SFunction;
21 
22 
23  template<class CIC>
24  class CICFitter : public ROOT::Minuit2::FCNBase {
25  public:
26  explicit
27  CICFitter(CIC& cic, const Attenuation& att, unsigned int ndof = 1)
28  : fCIC(&cic), fAttenuation(att), fNdof(ndof) { }
29 
30  std::vector<double>
31  Minimize(const std::vector<std::string>& name,
32  const std::vector<double>& init,
33  const std::vector<double>& sigma)
34  {
35  if (name.size() != init.size() ||
36  name.size() != sigma.size())
37  throw std::runtime_error("size mismatch!");
38 
39  ROOT::Minuit2::MnUserParameters pars;
40  for (unsigned int i = 0, n = name.size(); i < n; ++i) {
41  pars.Add(name[i], init[i], sigma[i]);
42  if (!sigma[i])
43  pars.Fix(name[i]);
44  }
45 
46  ROOT::Minuit2::MnMinimize m(*this, pars, 0);
47  ROOT::Minuit2::FunctionMinimum fmin = m();
48  ROOT::Minuit2::MnHesse hesse;
49  hesse(m.Fcnbase(), fmin);
50 
51  if (!fmin.HasValidParameters())
52  return std::vector<double>();
53  std::vector<double> result = fmin.UserParameters().Params();
54  result.push_back(fmin.Fval());
55  result.push_back(fNdof);
56  return result;
57  }
58 
59  double Up() const override { return 1; }
60 
61  virtual double operator()(const std::vector<double>& pars) const override = 0;
62 
63  double GetAttenuation(const double sin2theta, const std::vector<double>& pars) const
64  { return fAttenuation(sin2theta, pars); }
65 
66  private:
67  CICFitter(const CICFitter&) = delete;
68  CICFitter& operator=(const CICFitter&) = delete;
69 
70  protected:
71  CIC* fCIC = nullptr;
73  unsigned int fNdof = 1;
74  };
75 
76 
77  template<class CIC>
78  class KS : public CICFitter<CIC> {
79  public:
80  explicit
81  KS(CIC& cic, const Attenuation& att, const double lnSCut)
82  : CICFitter<CIC>(cic, att), fLnSCut(lnSCut) { }
83 
84  virtual
85  double
86  operator()(const std::vector<double>& pars)
87  const
88  override
89  {
90  const auto att = [this, &pars](const double sin2theta) {
91  return this->GetAttenuation(sin2theta, pars);
92  };
93  return this->fCIC->GetKS(att, fLnSCut);
94  }
95 
96  private:
97  double fLnSCut = 0;
98  };
99 
100 
101  template<class CIC>
102  class AndersonDarling : public CICFitter<CIC> {
103  public:
104  explicit
105  AndersonDarling(CIC& cic, const Attenuation& att, const double lnSCut)
106  : CICFitter<CIC>(cic, att), fLnSCut(lnSCut) { }
107 
108  virtual
109  double
110  operator()(const std::vector<double>& pars)
111  const
112  override
113  {
114  const auto att = [this, &pars](const double sin2theta) {
115  return this->GetAttenuation(sin2theta, pars);
116  };
117  return this->fCIC->GetAndersonDarling(att, fLnSCut);
118  }
119 
120  private:
121  double fLnSCut = 0;
122  };
123 
124 
125  template<class CIC>
126  class Chi2 : public CICFitter<CIC> {
127  public:
128  explicit
129  Chi2(CIC& cic, const Attenuation& att, const double lnSCut)
130  : CICFitter<CIC>(cic, att), fLnSCut(lnSCut) { }
131 
132  virtual
133  double
134  operator()(const std::vector<double>& pars)
135  const
136  override
137  {
138  const auto att = [this, &pars](const double sin2theta) {
139  return this->GetAttenuation(sin2theta, pars);
140  };
141  return this->fCIC->GetChi2Ndof(att, fLnSCut);
142  }
143 
144  private:
145  double fLnSCut = 0;
146  };
147 
148 
149  template<class CIC>
150  class PoissonChi2 : public CICFitter<CIC> {
151  public:
152  explicit
153  PoissonChi2(CIC& cic, const Attenuation& att, const double lnSCut)
154  : CICFitter<CIC>(cic, att, cic.GetNBins() - 1), fLnSCut(lnSCut) { }
155 
156  virtual
157  double
158  operator()(const std::vector<double>& pars)
159  const
160  override
161  {
162  const auto att = [this, &pars](const double sin2theta) {
163  return this->GetAttenuation(sin2theta, pars);
164  };
165  return this->fCIC->GetPoissonChi2(att, fLnSCut);
166  }
167 
168  private:
169  double fLnSCut = 0;
170  };
171 
172 
173  template<class CIC, class Binning>
174  class PoissonChi22D : public CICFitter<CIC> {
175  public:
176  explicit
177  PoissonChi22D(CIC& cic, const Attenuation& att, const Binning& lnSBinning) :
178  CICFitter<CIC>(cic, att, (cic.GetNBins() - 1)*lnSBinning.GetNBins()),
179  fLnSBinning(lnSBinning)
180  { }
181 
182  virtual
183  double
184  operator()(const std::vector<double>& pars)
185  const
186  override
187  {
188  const auto att = [this, &pars](const double sin2theta) {
189  return this->GetAttenuation(sin2theta, pars);
190  };
191  return this->fCIC->GetPoissonChi22D(att, fLnSBinning);
192  }
193 
194  private:
195  Binning fLnSBinning;
196  };
197 
198 
199  template<class CIC, class Binning>
200  class AndersonDarling2D : public CICFitter<CIC> {
201  public:
202  explicit
203  AndersonDarling2D(CIC& cic, const Attenuation& att, const Binning& lnSBinning) :
204  CICFitter<CIC>(cic, att, lnSBinning.GetNBins()),
205  fLnSBinning(lnSBinning)
206  { }
207 
208  virtual
209  double
210  operator()(const std::vector<double>& pars)
211  const
212  override
213  {
214  const auto att = [this, &pars](const double sin2theta) {
215  return this->GetAttenuation(sin2theta, pars);
216  };
217  return this->fCIC->GetAndersonDarling2D(att, fLnSBinning);
218  }
219 
220  private:
221  Binning fLnSBinning;
222  };
223 
224  }
225 
226 }
227 
228 #endif
double GetAttenuation(const double sin2theta, const std::vector< double > &pars) const
double Up() const override
virtual double operator()(const std::vector< double > &pars) const override
virtual double operator()(const std::vector< double > &pars) const override
AndersonDarling2D(CIC &cic, const Attenuation &att, const Binning &lnSBinning)
virtual double operator()(const std::vector< double > &pars) const override
virtual double operator()(const std::vector< double > &pars) const override=0
const Data result[]
std::function< double(double, double)> SFunction
virtual double operator()(const std::vector< double > &pars) const override
CICFitter & operator=(const CICFitter &)=delete
KS(CIC &cic, const Attenuation &att, const double lnSCut)
Chi2(CIC &cic, const Attenuation &att, const double lnSCut)
PoissonChi2(CIC &cic, const Attenuation &att, const double lnSCut)
std::vector< double > Minimize(const std::vector< std::string > &name, const std::vector< double > &init, const std::vector< double > &sigma)
PoissonChi22D(CIC &cic, const Attenuation &att, const Binning &lnSBinning)
virtual double operator()(const std::vector< double > &pars) const override
constexpr double m
Definition: AugerUnits.h:121
CICFitter(CIC &cic, const Attenuation &att, unsigned int ndof=1)
virtual double operator()(const std::vector< double > &pars) const override
AndersonDarling(CIC &cic, const Attenuation &att, const double lnSCut)
std::function< double(const double, const std::vector< double > &)> Attenuation

, generated on Tue Sep 26 2023.