Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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#include "JROOT/JMinimizer.hh"
13
14#include "Jeep/JPrint.hh"
15#include "Jeep/JParser.hh"
16#include "Jeep/JMessage.hh"
17
18
19/**
20 * \file
21 *
22 * Program to test ROOT fit.
23 * \author mdejong
24 */
25int main(int argc, char **argv)
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}
General purpose messaging.
#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:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
I/O formatting auxiliaries.
int main(int argc, char **argv)
Definition JRootFit.cc:25
Utility class to parse command line options.
Definition JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Auxiliary data structure for a parameter index and its value.