Jpp  19.1.0
the software that should make you happy
Functions
JRootFit.cc File Reference

Program to test ROOT fit. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <limits>
#include "TROOT.h"
#include "TF1.h"
#include "TH1D.h"
#include "TFitResult.h"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JMinimizer.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to test ROOT fit.

Author
mdejong

Definition in file JRootFit.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 25 of file JRootFit.cc.

26 {
27  using namespace std;
28  using namespace JPP;
29 
30  double precision;
31  int debug;
32 
33  try {
34 
35  JParser<> zap("Program to test ROOT fit.");
36 
37  zap['e'] = make_field(precision) = numeric_limits<double>::min();
38  zap['d'] = make_field(debug) = 3;
39 
40  zap(argc, argv);
41  }
42  catch(const exception& error) {
43  FATAL(error.what() << endl);
44  }
45 
46  string option = "";
47 
48  if (option.find('S') == string::npos) {
49  option += 'S';
50  }
51 
52  if (debug < debug_t && option.find('Q') == string::npos) {
53  option += "Q";
54  }
55 
56 
57  TH1D h1("h1", NULL, 1, -0.5, +0.5);
58 
59 
60  struct {
61  double y;
62  double z;
63  } data[] = {
64  { 100.0, 10.0 },
65  { 0.0, 10.0 }
66  };
67 
68 
69  for (int i = 0; i != sizeof(data)/sizeof(data[0]); ++i) {
70 
71  const double y = data[i].y;
72  const double z = data[i].z;
73 
74  h1.SetBinContent(1, y);
75  h1.SetBinError (1, z);
76 
77  {
78  NOTICE("ROOT fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
79 
80  TF1 f1("f1", "[0]");
81 
82  double ymin, ymax;
83 
84  f1.SetParLimits(0, 0.0, 0.0);
85  f1.GetParLimits(0, ymin, ymax);
86 
87  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
88 
89  ASSERT(result.Get() != NULL, "TFitResultPtr");
90 
91  ERROR("Result: "
92  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
93  << FIXED(7,3) << f1.GetParError (0) << " "
94  << "[" << FIXED(7,3) << ymin << "," << FIXED(7,3) << ymax << "]" << " "
95  << (result->IsParameterBound(0) ? " *** bound *** " : "")
96  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
97  << endl);
98  }
99  {
100  NOTICE("Normal fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
101 
102  TF1 f1("f1", "[0]");
103 
104  ASSERT(setParameter(f1, JFitParameter_t(0, y)) == true);
105  ASSERT(setParameter(f1, JFitParameter_t(1, y)) == false);
106 
107  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
108 
109  ASSERT(result.Get() != NULL, "TFitResultPtr");
110 
111  DEBUG("Result: "
112  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
113  << FIXED(7,3) << f1.GetParError (0) << " "
114  << (result->IsParameterBound(0) ? " *** bound *** " : "")
115  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
116  << endl);
117 
118  ASSERT(result->IsValid());
119  ASSERT(!result->IsParameterBound(0));
120  ASSERT(!result->IsParameterFixed(0));
121  ASSERT(fabs(f1.GetParameter(0) - y) < f1.GetParError(0));
122  }
123  {
124  NOTICE("Fixed parameters fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
125 
126  TF1 f1("f1", "[0]");
127 
128  ASSERT(fixParameter(f1, JFitParameter_t(0, y)) == true);
129  ASSERT(isParameterFixed(f1, 0) == true);
130  ASSERT(isParameterFixed(f1, 1) == false);
131 
132  const JFitParameter_t parameter(0, 0.0);
133 
134  ASSERT(fixParameter(f1, parameter));
135  ASSERT(fixParameter(f1, parameter));
136 
137  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
138 
139  ASSERT(result.Get() != NULL, "TFitResultPtr");
140 
141  DEBUG("Result: "
142  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
143  << FIXED(7,3) << f1.GetParError (0) << " "
144  << (result->IsParameterBound(0) ? " *** bound *** " : "")
145  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
146  << endl);
147 
148  ASSERT(result->IsValid());
149  ASSERT(!result->IsParameterBound(0));
150  ASSERT(result->IsParameterFixed(0));
151  ASSERT(fabs(f1.GetParameter(0) - parameter.value) < precision);
152  ASSERT(fabs(f1.GetParError (0)) < precision);
153  }
154  {
155  NOTICE("Fixed parameters fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
156 
157  TF1 f1("f1", "[0]");
158 
159  ASSERT(fixParameter(f1, JFitParameter_t(0, y)) == true);
160  ASSERT(isParameterFixed(f1, 0) == true);
161  ASSERT(isParameterFixed(f1, 1) == false);
162 
163  const JFitParameter_t parameter(0, 0.0);
164 
165  ASSERT(fixParameter(f1, parameter));
166  ASSERT(fixParameter(f1, parameter));
167 
168  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
169 
170  ASSERT(result.Get() != NULL, "TFitResultPtr");
171 
172  DEBUG("Result: "
173  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
174  << FIXED(7,3) << f1.GetParError (0) << " "
175  << (result->IsParameterBound(0) ? " *** bound *** " : "")
176  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
177  << endl);
178 
179  ASSERT(result->IsValid());
180  ASSERT(!result->IsParameterBound(0));
181  ASSERT(result->IsParameterFixed(0));
182  ASSERT(fabs(f1.GetParameter(0) - parameter.value) < precision);
183  ASSERT(fabs(f1.GetParError (0)) < precision);
184  }
185  {
186  NOTICE("Released parameters fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
187 
188  TF1 f1("f1", "[0]");
189 
190  ASSERT(fixParameter(f1, JFitParameter_t(0, y)) == true);
191  ASSERT(isParameterFixed(f1, 0) == true);
192 
193  ASSERT(releaseParameter(f1, 0) == true);
194  ASSERT(releaseParameter(f1, 1) == false);
195 
196  ASSERT(!isParameterFixed(f1, 0));
197 
198  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
199 
200  ASSERT(result.Get() != NULL, "TFitResultPtr");
201 
202  DEBUG("Result: "
203  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
204  << FIXED(7,3) << f1.GetParError (0) << " "
205  << (result->IsParameterBound(0) ? " *** bound *** " : "")
206  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
207  << endl);
208 
209  ASSERT(result->IsValid());
210  ASSERT(!result->IsParameterBound(0));
211  ASSERT(!result->IsParameterFixed(0));
212  ASSERT(fabs(f1.GetParameter(0) - y) < f1.GetParError(0));
213  }
214  }
215 
216  return 0;
217 }
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
Utility class to parse command line options.
Definition: JParser.hh:1698
const JPolynome f1(1.0, 2.0, 3.0)
Function.
@ debug_t
debug
Definition: JMessage.hh:29
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool fixParameter(TF1 &f1, const JFitParameter_t &parameter)
Fix fit parameter.
bool setParameter(TF1 &f1, const JFitParameter_t &parameter)
Set fit parameter.
bool isParameterFixed(const TF1 &f1, const Int_t index)
Check if fit parameter is fixed.
bool releaseParameter(TF1 &f1, const Int_t index)
Release fit parameter.
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448
Auxiliary data structure for a parameter index and its value.