DAS  3.0
Das Analysis System
AbstractFit

Description

Both response and resolution fits needs to test different fit ranges and parameter configurations.

#include <fit.h>

+ Inheritance diagram for AbstractFit:
+ Collaboration diagram for AbstractFit:

Public Member Functions

virtual bool good () const
 

Public Attributes

const std::unique_ptr< TH1 > & h
 
std::uint32_t status
 
std::ostream & cout
 
double * p
 
double * e
 
std::unique_ptr< TF1 > f
 
std::optional< double > chi2ndf
 
std::optional< double > chi2ndfErr
 
std::pair< float, float > interval
 

Protected Member Functions

 AbstractFit (const unique_ptr< TH1 > &h, ostream &cout, int npars)
 
virtual ~AbstractFit ()
 
void fit (std::pair< float, float >, const char *)
 
virtual void Write (const char *name)
 

Constructor & Destructor Documentation

◆ AbstractFit()

AbstractFit ( const unique_ptr< TH1 > &  h,
ostream &  cout,
int  npars 
)
inlineprotected
38  :
39  h(h), status(0), cout(cout),
40  p(new double[npars]), e(new double[npars])
41  { }

◆ ~AbstractFit()

virtual ~AbstractFit ( )
inlineprotectedvirtual
44  {
45  for (int i = 0; i < f->GetNpar(); ++i) {
46  if (isfinite(p[i])) continue;
47  const char * what = Form("Fit parameter %d is not finite", i);
48  BOOST_THROW_EXCEPTION( std::runtime_error(what) );
49  }
50  delete p;
51  delete e;
52  }

Member Function Documentation

◆ fit()

void fit ( std::pair< float, float >  range,
const char *  fitName 
)
protected

Perform fit in given range. Starting parameters are always taken from the members, which are expected to provide the so-far best parameter estimation. The parameters and the range are overriden in case of improvement of fit performance.

Link to ROOT TH1::Fit() doc

Todo:
https://stackoverflow.com/questions/37385560/c-redirect-or-disable-stdio-temporarily to avoid this dumb arrow
Todo:
E: use Minos for error estimations
100 {
101  using namespace std;
102 
103  // reinitialise the params to the best known fit
104  f->SetParameters(p);
105  f->SetParErrors (e);
106  f->SetRange(range.first, range.second);
107  for (int i = 0; i < f->GetNpar(); ++i) {
108  double m, M;
109  f->GetParLimits(i,m,M);
110  if (p[i] >= m && p[i] <= M) continue;
111  cerr << orange << setw(2) << i
112  << setw(10) << m
113  << setw(10) << M
114  << setw(10) << p[i]
115  << def << '\n';
116  }
117 
118  // run the fit
119  TFitResultPtr result = h->Fit(f.get(), "QS0NRE");
121  // Q: quiet
122  // S: return smart pointer to fit restult (automatic conversion to integer provides fit status)
123  // 0: do not draw
124  // N: do not store graphics
125  // R: use fit range
127 
128  // get result
129  double currentChi2ndf = f->GetChisquare()/f->GetNDF();
130 
131  bool success = result >= 0 && result % 10 == 0 && currentChi2ndf < 10000;
132  // status = migradStatus + 10*minosStatus + 100*hesseStatus + 1000*improveStatus.
133  // TMinuit returns 0 (for migrad, minos, hesse or improve) in case of success and 4 in case of error (see the documentation of TMinuit::mnexcm).
134  // For example, for an error only in Minos but not in Migrad a fitStatus of 40 will be returned.
135  // Minuit2 returns 0 in case of success and different values in migrad,minos or hesse depending on the error.
136  // See in this case the documentation of Minuit2Minimizer::Minimize for the migrad return status,
137  // Minuit2Minimizer::GetMinosError for the minos return status,
138  // and Minuit2Minimizer::Hesse for the hesse return status.
139 
140  bool improvement = !chi2ndf || abs(currentChi2ndf-1) <= abs(*chi2ndf-1);
141 
142  cout << h->GetTitle() << " with " << bold << fitName << normal << " in ["
143  << fixed << setprecision(2) << range.first << "," << range.second << "]: " << defaultfloat
144  << (success ? (improvement ? green : orange) : red) << *this
145  << fixed << setprecision(3) << setw(15) << currentChi2ndf << defaultfloat
146  << "\t(status = " << (int) result << ")" << def << endl;
147 
148  // skip if the new attempt isn't improving the performance
149  if (!success || !improvement) return;
150 
151  // at this stage, we can consider that the fit has improved,
152  // therefore we save the current parameter estimates
153  chi2ndf = currentChi2ndf;
154  chi2ndfErr = sqrt(2./f->GetNDF());
155  for (int i = 0; i < f->GetNpar(); ++i) {
156  p[i] = f->GetParameter(i);
157  e[i] = f->GetParError (i);
158  }
159  interval = range;
160 }

◆ good()

virtual bool good ( ) const
inlinevirtual

Reimplemented in ResponseFit.

34 { return chi2ndf && status; }

◆ Write()

virtual void Write ( const char *  name)
inlineprotectedvirtual

Write the function to the current TDirectory.

Reimplemented in ResponseFit, and ResolutionFit.

66  {
67  if (good()) // successful fit
68  f->SetTitle(Form("%.1f", *chi2ndf));
69  else {
70  f->SetLineStyle(kDashed);
71  f->SetTitle("fail");
72  }
73 
74  f->SetParameters(p);
75  f->SetParErrors (e);
76  f->SetRange(interval.first, interval.second);
77  f->Write(name);
78  }

Member Data Documentation

◆ chi2ndf

std::optional<double> chi2ndf

◆ chi2ndfErr

std::optional<double> chi2ndfErr

best chi2/ndf

◆ cout

std::ostream& cout

output stream, can be overwritten to reduce I/O

◆ e

double * e

uncertainties on best parameter estimates

◆ f

std::unique_ptr<TF1> f

Starting by passing Gaussian range.

◆ h

const std::unique_ptr<TH1>& h

histogram ready to be fitted

◆ interval

std::pair<float, float> interval

best fit range

◆ p

double* p

best parameter estimation

◆ status

std::uint32_t status

a bit-field to be used in daughter classes


The documentation for this struct was generated from the following file:
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:93
Step::def
static const char * def
Definition: Step.h:36
Step::red
static const char * red
Definition: Step.h:34
orange
static const char * orange
Definition: colours.h:6
DAS::JetEnergy::AbstractFit::interval
std::pair< float, float > interval
best fit range
Definition: fit.h:32
DAS::JetEnergy::AbstractFit::cout
std::ostream & cout
output stream, can be overwritten to reduce I/O
Definition: fit.h:25
normal
static const char * normal
Definition: colours.h:8
Step::green
static const char * green
Definition: Step.h:33
DAS::JetEnergy::AbstractFit::chi2ndfErr
std::optional< double > chi2ndfErr
best chi2/ndf
Definition: fit.h:31
DAS::JetEnergy::AbstractFit::good
virtual bool good() const
Definition: fit.h:34
DAS::JetEnergy::AbstractFit::chi2ndf
std::optional< double > chi2ndf
Definition: fit.h:31
DAS::JetEnergy::AbstractFit::e
double * e
uncertainties on best parameter estimates
Definition: fit.h:28
DAS::JetEnergy::AbstractFit::status
std::uint32_t status
a bit-field to be used in daughter classes
Definition: fit.h:24
DAS::JetEnergy::AbstractFit::h
const std::unique_ptr< TH1 > & h
histogram ready to be fitted
Definition: fit.h:23
metPhiCorrectionExample.range
range
Definition: metPhiCorrectionExample.py:100
DAS::JetEnergy::AbstractFit::p
double * p
best parameter estimation
Definition: fit.h:27
DAS::JetEnergy::AbstractFit::f
std::unique_ptr< TF1 > f
Starting by passing Gaussian range.
Definition: fit.h:30
Step::bold
static const char * bold
Definition: Step.h:35