Jpp
Functions
JRootFit.cc File Reference
#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 "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 24 of file JRootFit.cc.

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