Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
JSchwarzenegger.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 "TH2D.h"
12#include "TMath.h"
13#include "TRandom3.h"
14
15#include "JFit/JGandalf.hh"
16#include "JTools/JElement.hh"
17#include "JTools/JStats.hh"
18#include "JROOT/JRootToolkit.hh"
19#include "JMath/JGauss.hh"
20#include "JMath/JConstants.hh"
21
22#include "Jeep/JTimer.hh"
23#include "Jeep/JPrint.hh"
24#include "Jeep/JParser.hh"
25#include "Jeep/JMessage.hh"
26
27
28namespace {
29
30 using namespace JPP;
31 using TMath::PoissonI;
32
33 typedef JElement2D<double, double> JElement_t;
34 typedef JGandalf<JGauss>::result_type result_type;
35
36 /**
37 * Fit function.
38 *
39 * \param gauss gauss
40 * \param point point
41 * \return chi2
42 */
43 inline result_type g1(const JGauss& gauss, const JElement_t& point)
44 {
45 result_type result;
46
47 const double u = (point.getX() - gauss.mean) / gauss.sigma;
48 const double fs = gauss.signal * exp(-0.5*u*u) / (sqrt(2.0*PI) * gauss.sigma);
49 const double fb = gauss.background;
50 const double f1 = fs + fb;
51
52 const double p = PoissonI(point.getY(), f1);
53
54 result.chi2 = -log(p);
55
56 result.gradient.mean = fs * u / gauss.sigma; // d(f)/d(mean)
57 result.gradient.sigma = fs * u*u / gauss.sigma - fs / gauss.sigma; // d(f)/d(sigma)
58 result.gradient.signal = fs / gauss.signal; // d(f)/d(signal)
59 result.gradient.background = 1.0; // d(f)/d(background)
60
61 result.gradient *= 1.0 - point.getY()/f1; // d(chi2)/d(f)
62
63 return result;
64 }
65}
66
67
68/**
69 * \file
70 *
71 * Program to test JFIT::JGandalf algorithm.
72 * \author mdejong
73 */
74int main(int argc, char **argv)
75{
76 using namespace std;
77 using namespace JPP;
78
79 string outputFile;
81 Int_t seed;
82 int debug;
83
84 try {
85
86 JParser<> zap("Program to test JGandalf algorithm.");
87
88 zap['o'] = make_field(outputFile) = "";
89 zap['@'] = make_field(gauss) = JGauss(0.0, 1.0, 1000.0, 100.0);
90 zap['S'] = make_field(seed) = 0;
91 zap['d'] = make_field(debug) = 3;
92
93 zap(argc, argv);
94 }
95 catch(const exception& error) {
96 FATAL(error.what() << endl);
97 }
98
99
100 gRandom->SetSeed(seed);
101
103
104 TF1 fs("fs", "exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1]))");
105 TF1 fb("fb", "1.0");
106
107 fs.FixParameter(0, gauss.mean);
108 fs.FixParameter(1, gauss.sigma);
109
110 const Int_t nx = 21;
111 const Double_t xmin = -5.25;
112 const Double_t xmax = +5.25;
113
114
115 TFile out(outputFile.c_str(), "recreate");
116
117 TH1D h0("h0", NULL, nx, xmin, xmax);
118 TH1D h1("h1", NULL, nx*100, xmin, xmax);
119
120 h0.Sumw2();
121 h1.Sumw2();
122 /*
123 for (size_t i = 0; i != (size_t) (gauss.signal); ++i) { h0.Fill(fs.GetRandom(gRandom)); }
124 for (size_t i = 0; i != (size_t) (gauss.background * nx); ++i) { h0.Fill(fb.GetRandom(gRandom)); }
125 */
126 h0.FillRandom("fs", (Int_t) gauss.signal);
127 h0.FillRandom("fb", (Int_t) gauss.background * nx);
128
129 const Int_t ny = 5000;
130 const Double_t ymin = 0.0;
131 const Double_t ymax = gauss.signal + 3.0 * sqrt(gauss.signal) * (xmax - xmin) / nx + gauss.background;
132
133 TH2D h2("h2", NULL,
134 nx, xmin, xmax,
135 ny, ymin, ymax);
136
137
139
140 for (Int_t i = 1; i <= h0.GetNbinsX(); ++i) {
141 data.push_back(JElement_t(h0.GetBinCenter (i),
142 h0.GetBinContent(i)));
143 }
144
146
148
149 fit.parameters.resize(4);
150
151 fit.parameters[0] = &JGauss::mean;
152 fit.parameters[1] = &JGauss::sigma;
153 fit.parameters[2] = &JGauss::signal;
154 fit.parameters[3] = &JGauss::background;
155
156 fit.value = JGauss(h0.GetMean(),
157 h0.GetRMS(),
158 h0.GetEntries() * (xmax - xmin) / nx - h0.GetMinimum(),
159 h0.GetMinimum());
160
161 const double chi2 = fit(g1, data.begin(), data.end());
162
163 DEBUG("Final value " << fit.value << endl);
164 DEBUG("Chi2 " << chi2 << endl);
165
166 for (Int_t ix = 1; ix <= h1.GetXaxis()->GetNbins(); ++ix) {
167
168 const double x = h1.GetXaxis()->GetBinCenter(ix);
169 const double y = fit.value(x);
170
171 h1.SetBinContent(ix, y);
172 }
173
174 for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
175 for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
176
177 const double x = h2.GetXaxis()->GetBinCenter(ix);
178 const double y = h2.GetYaxis()->GetBinCenter(iy);
179 const double z = TMath::Poisson(y, fit.value(x));
180
181 h2.SetBinContent(ix, iy, z);
182 }
183 }
184
185 out.Write();
186 out.Close();
187}
string outputFile
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Mathematical constants.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
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
int main(int argc, char **argv)
Place holder for custom implementation.
Utility class to parse command line options.
Definition JParser.hh:1698
double gauss(const double x, const double sigma)
Gauss function (normalised to 1 at x = 0).
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
Gauss function object.
Definition JMathlib.hh:1589
2D Element.
Definition JPolint.hh:1131