Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JGandalfFitAlaMinuitToGauss.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4#include <vector>
5#include <cmath>
6
7#include "TROOT.h"
8#include "TFile.h"
9#include "TF1.h"
10#include "TH1D.h"
11#include "TMath.h"
12
13#include "JMath/JModel.hh"
14#include "JMath/JConstants.hh"
15#include "JFit/JGandalf.hh"
16#include "JTools/JElement.hh"
17#include "JTools/JQuantile.hh"
18#include "JROOT/JRootToolkit.hh"
19
20#include "Jeep/JTimer.hh"
21#include "Jeep/JPrint.hh"
22#include "Jeep/JParser.hh"
23#include "Jeep/JMessage.hh"
24
25
26namespace {
27
28 using namespace JPP;
29 using TMath::PoissonI;
30
31 typedef JElement2D<double, double> JElement_t;
32
33
34 /**
35 * Gauss.
36 */
37 struct JGauss :
38 public JMATH::JModel_t
39 {
40 /**
41 * Default constructor.
42 */
43 JGauss()
44 {}
45
46
47 /**
48 * Constructor.
49 *
50 * \param mean mean
51 * \param sigma sigma
52 * \param signal signal
53 * \param background background
54 */
55 JGauss(const double mean,
56 const double sigma,
57 const double signal,
58 const double background)
59 {
60 this->push_back(mean);
61 this->push_back(sigma);
62 this->push_back(signal);
63 this->push_back(background);
64 }
65 };
66
67
68 typedef JGandalf<JGauss>::result_type result_type;
69
70
71 /**
72 * Fit function.
73 *
74 * \param gauss gauss
75 * \param point point
76 * \return chi2
77 */
78 inline result_type g1(const JGauss& gauss, const JElement_t& point)
79 {
80 result_type result;
81
82 const double u = (point.getX() - gauss[0]) / gauss[1];
83 const double fs = gauss[2] * exp(-0.5*u*u) / (sqrt(2.0*PI) * gauss[1]);
84 const double fb = gauss[3];
85 const double f1 = fs + fb;
86
87 const double p = PoissonI(point.getY(), f1);
88
89 result.chi2 = -log(p);
90
91 result.gradient[0] = fs * u / gauss[1]; // d(f)/d(mean)
92 result.gradient[1] = fs * u*u / gauss[1] - fs / gauss[1]; // d(f)/d(sigma)
93 result.gradient[2] = fs / gauss[2]; // d(f)/d(signal)
94 result.gradient[3] = 1.0; // d(f)/d(background)
95
96 result.gradient *= 1.0 - point.getY()/f1; // d(chi2)/d(f)
97
98 return result;
99 }
100}
101
102
103/**
104 * \file
105 *
106 * Program to test JFIT::JGandalf algorithm.
107 * \author mdejong
108 */
109int main(int argc, char **argv)
110{
111 using namespace std;
112 using namespace JPP;
113
114 string outputFile;
115 int numberOfEvents;
117 JGauss precision;
118 int debug;
119
120 try {
121
122 JParser<> zap("Program to test JGandalf algorithm.");
123
124 zap['o'] = make_field(outputFile) = "";
125 zap['n'] = make_field(numberOfEvents) = 100;
126 zap['@'] = make_field(gauss) = JGauss(0.0, 1.0, 1000.0, 100.0);
127 zap['e'] = make_field(precision) = JGauss(0.05, 0.05, 25.0, 25.0);
128 zap['d'] = make_field(debug) = 3;
129
130 zap(argc, argv);
131 }
132 catch(const exception& error) {
133 FATAL(error.what() << endl);
134 }
135
136 using namespace JFIT;
137
138 ASSERT(numberOfEvents > 0);
139
141
142 TF1 fs("fs", "exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1]))");
143 TF1 fb("fb", "1.0");
144
145 fs.FixParameter(0, gauss[0]);
146 fs.FixParameter(1, gauss[1]);
147
148 const Int_t nx = 21;
149 const Double_t xmin = -5.0;
150 const Double_t xmax = +5.0;
151
152 JQuantile Q[] = { JQuantile("mean "),
153 JQuantile("sigma "),
154 JQuantile("signal "),
155 JQuantile("background") };
156
157 TH1D H[] = { TH1D("ha", "", 101, -0.1, +0.1),
158 TH1D("hb", "", 101, -0.1, +0.1),
159 TH1D("hc", "", 101, -100.0, +100.0),
160 TH1D("hd", "", 101, -100.0, +100.0) };
161
162 JTimer timer;
163
164 for (int i = 0; i != numberOfEvents; ++i) {
165
166 STATUS("event: " << setw(10) << i << '\r'); DEBUG(endl);
167
168 TH1D h0("h0", NULL, nx, xmin, xmax);
169
170 h0.Sumw2();
171
172 h0.FillRandom("fs", (Int_t) gauss[2]);
173 h0.FillRandom("fb", (Int_t) gauss[3]);
174
176
177 for (Int_t i = 1; i <= h0.GetNbinsX(); ++i) {
178 data.push_back(JElement_t(h0.GetBinCenter (i),
179 h0.GetBinContent(i)));
180 }
181
183
184 fit.parameters.resize(4);
185
186 for (size_t i = 0; i != 4; ++i) {
187 fit.parameters[i] = i;
188 }
189
190 fit.value = JGauss(h0.GetMean(),
191 h0.GetRMS(),
192 h0.GetEntries() * (xmax - xmin) / nx - h0.GetMinimum(),
193 h0.GetMinimum());
194
195 DEBUG("Start value " << fit.value << endl);
196
197 timer.start();
198
199 const double chi2 = fit(g1, data.begin(), data.end());
200
201 timer.stop();
202
203 DEBUG("Final value " << fit.value << endl);
204 DEBUG("Chi2 " << chi2 << endl);
205
206 const double Y[] = { fit.value[0] - gauss[0],
207 fit.value[1] - gauss[1],
208 fit.value[2] * nx / (xmax - xmin) - gauss[2],
209 fit.value[3] * nx - gauss[3] };
210
211 for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
212 Q[i].put (Y[i]);
213 H[i].Fill(Y[i]);
214 }
215 }
216
217 for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
218 NOTICE((i == 0 ? longprint : shortprint) << Q[i]);
219 }
220
221 if (debug >= notice_t) {
222 timer.print(cout, true, micro_t);
223 }
224
225 if (outputFile != "") {
226
227 TFile out(outputFile.c_str(), "recreate");
228
229 for (int i = 0; i != sizeof(H)/sizeof(H[0]); ++i) {
230 out << H[i];
231 }
232
233 out.Write();
234 out.Close();
235 }
236
237 for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
238 ASSERT(fabs(Q[i].getMean()) < Q[i].getSTDev());
239 }
240
241 ASSERT(Q[0].getSTDev() < precision[0]);
242 ASSERT(Q[1].getSTDev() < precision[1]);
243 ASSERT(Q[2].getSTDev() < precision[2]);
244 ASSERT(Q[3].getSTDev() < precision[3]);
245
246 return 0;
247}
double getMean(vector< double > &v)
get mean of vector content
string outputFile
The elements in a collection are sorted according to their abscissa values and a given distance opera...
int main(int argc, char **argv)
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition JManip.hh:172
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition JManip.hh:144
Mathematical constants.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define STATUS(A)
Definition JMessage.hh:63
#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.
Double_t g1(const Double_t x)
Function.
Definition JQuantiles.cc:25
Place holder for custom implementation.
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition JTimer.hh:172
void stop()
Stop timer.
Definition JTimer.hh:127
void start()
Start timer.
Definition JTimer.hh:106
Utility class to parse command line options.
Definition JParser.hh:1698
const JPolynome f1(1.0, 2.0, 3.0)
Function.
@ micro_t
micro
Definition JScale.hh:29
@ notice_t
notice
Definition JMessage.hh:32
Auxiliary classes and methods for linear and iterative data regression.
Definition JEnergy.hh:15
double gauss(const double x, const double sigma)
Gauss function (normalised to 1 at x = 0).
static const double H
Planck constant [eV s].
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
return result
Definition JPolint.hh:862
Data structure for return value of fit function.
Definition JGandalf.hh:102
double background
Definition JGauss.hh:164
double signal
Definition JGauss.hh:163
Gauss function object.
Definition JMathlib.hh:1589
double sigma
sigma
Definition JMathlib.hh:1665
JGauss()
Default constructor.
Definition JGauss.hh:185
2D Element.
Definition JPolint.hh:1131
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133