Jpp  17.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRootFit.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <limits>
5 
6 #include "TROOT.h"
7 #include "TF1.h"
8 #include "TH1D.h"
9 #include "TFitResult.h"
10 
11 #include "JROOT/JRootToolkit.hh"
12 
13 #include "Jeep/JPrint.hh"
14 #include "Jeep/JParser.hh"
15 #include "Jeep/JMessage.hh"
16 
17 
18 /**
19  * \file
20  *
21  * Program to test ROOT fit.
22  * \author mdejong
23  */
24 int main(int argc, char **argv)
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  ASSERT(result.Get() != NULL, "TFitResultPtr");
89 
90  ERROR("Result: "
91  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
92  << FIXED(7,3) << f1.GetParError (0) << " "
93  << "[" << FIXED(7,3) << ymin << "," << FIXED(7,3) << ymax << "]" << " "
94  << (result->IsParameterBound(0) ? " *** bound *** " : "")
95  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
96  << endl);
97  }
98  {
99  NOTICE("Normal fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
100 
101  TF1 f1("f1", "[0]");
102 
103  ASSERT(setParameter(f1, JFitParameter_t(0, y)) == true);
104  ASSERT(setParameter(f1, JFitParameter_t(1, y)) == false);
105 
106  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
107 
108  ASSERT(result.Get() != NULL, "TFitResultPtr");
109 
110  DEBUG("Result: "
111  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
112  << FIXED(7,3) << f1.GetParError (0) << " "
113  << (result->IsParameterBound(0) ? " *** bound *** " : "")
114  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
115  << endl);
116 
117  ASSERT(result->IsValid());
118  ASSERT(!result->IsParameterBound(0));
119  ASSERT(!result->IsParameterFixed(0));
120  ASSERT(fabs(f1.GetParameter(0) - y) < f1.GetParError(0));
121  }
122  {
123  NOTICE("Fixed parameters fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
124 
125  TF1 f1("f1", "[0]");
126 
127  ASSERT(fixParameter(f1, JFitParameter_t(0, y)) == true);
128  ASSERT(isParameterFixed(f1, 0) == true);
129  ASSERT(isParameterFixed(f1, 1) == false);
130 
131  const JFitParameter_t parameter(0, 0.0);
132 
133  ASSERT(fixParameter(f1, parameter));
134  ASSERT(fixParameter(f1, parameter));
135 
136  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
137 
138  ASSERT(result.Get() != NULL, "TFitResultPtr");
139 
140  DEBUG("Result: "
141  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
142  << FIXED(7,3) << f1.GetParError (0) << " "
143  << (result->IsParameterBound(0) ? " *** bound *** " : "")
144  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
145  << endl);
146 
147  ASSERT(result->IsValid());
148  ASSERT(!result->IsParameterBound(0));
149  ASSERT(result->IsParameterFixed(0));
150  ASSERT(fabs(f1.GetParameter(0) - parameter.value) < precision);
151  ASSERT(fabs(f1.GetParError (0)) < precision);
152  }
153  {
154  NOTICE("Fixed parameters fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
155 
156  TF1 f1("f1", "[0]");
157 
158  ASSERT(fixParameter(f1, JFitParameter_t(0, y)) == true);
159  ASSERT(isParameterFixed(f1, 0) == true);
160  ASSERT(isParameterFixed(f1, 1) == false);
161 
162  const JFitParameter_t parameter(0, 0.0);
163 
164  ASSERT(fixParameter(f1, parameter));
165  ASSERT(fixParameter(f1, parameter));
166 
167  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
168 
169  ASSERT(result.Get() != NULL, "TFitResultPtr");
170 
171  DEBUG("Result: "
172  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
173  << FIXED(7,3) << f1.GetParError (0) << " "
174  << (result->IsParameterBound(0) ? " *** bound *** " : "")
175  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
176  << endl);
177 
178  ASSERT(result->IsValid());
179  ASSERT(!result->IsParameterBound(0));
180  ASSERT(result->IsParameterFixed(0));
181  ASSERT(fabs(f1.GetParameter(0) - parameter.value) < precision);
182  ASSERT(fabs(f1.GetParError (0)) < precision);
183  }
184  {
185  NOTICE("Released parameters fit " << FIXED(7,3) << y << " +/- " << FIXED(7,3) << z << endl);
186 
187  TF1 f1("f1", "[0]");
188 
189  ASSERT(fixParameter(f1, JFitParameter_t(0, y)) == true);
190  ASSERT(isParameterFixed(f1, 0) == true);
191 
192  ASSERT(releaseParameter(f1, 0) == true);
193  ASSERT(releaseParameter(f1, 1) == false);
194 
195  ASSERT(!isParameterFixed(f1, 0));
196 
197  const TFitResultPtr result = h1.Fit(&f1, option.c_str());
198 
199  ASSERT(result.Get() != NULL, "TFitResultPtr");
200 
201  DEBUG("Result: "
202  << FIXED(7,3) << f1.GetParameter(0) << " +/- "
203  << FIXED(7,3) << f1.GetParError (0) << " "
204  << (result->IsParameterBound(0) ? " *** bound *** " : "")
205  << (result->IsParameterFixed(0) ? " *** fixed *** " : "")
206  << endl);
207 
208  ASSERT(result->IsValid());
209  ASSERT(!result->IsParameterBound(0));
210  ASSERT(!result->IsParameterFixed(0));
211  ASSERT(fabs(f1.GetParameter(0) - y) < f1.GetParError(0));
212  }
213  }
214 
215  return 0;
216 }
Utility class to parse command line options.
Definition: JParser.hh:1517
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
bool releaseParameter(TF1 &f1, const Int_t index)
Release fit parameter.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
bool isParameterFixed(const TF1 &f1, const Int_t index)
Check if fit parameter is fixed.
const JPolynome f1(1.0, 2.0, 3.0)
Function.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
bool setParameter(TF1 &f1, const JFitParameter_t &parameter)
Set fit parameter.
return result
Definition: JPolint.hh:764
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
bool fixParameter(TF1 &f1, const JFitParameter_t &parameter)
Fix fit parameter.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62