Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JRootfitToGauss2D.cc File Reference

Program to test JRootfit algorithm. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <type_traits>
#include <chrono>
#include "TROOT.h"
#include "TFile.h"
#include "TH2D.h"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JRootTestkit.hh"
#include "JROOT/JRootfit.hh"
#include "JROOT/JGraph2DErrors.hh"
#include "JMath/JMathlib2D.hh"
#include "JLang/JManip.hh"
#include "Jeep/JProperties.hh"
#include "Jeep/JParser.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to test JRootfit algorithm.

Author
mdejong

Definition in file JRootfitToGauss2D.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 31 of file JRootfitToGauss2D.cc.

32{
33 using namespace std;
34 using namespace JPP;
35
36 const char* const count_t = "count";
37 const char* const value_t = "value";
38
39 struct {
40 double signal = 100;
41 double background = 5;
42 double center = 0.0;
43 double sigma = 1.0;
44 } parameters;
45
46 string inputFile;
47 string outputFile;
48 range_type X;
49 range_type Y;
50 size_t N;
51 string option;
52 bool writeFits;
53 ULong_t seed;
54 int debug;
55
56 try {
57
58 JProperties properties;
59
60 properties.insert(gmake_property(parameters.signal));
61 properties.insert(gmake_property(parameters.background));
62 properties.insert(gmake_property(parameters.center));
63 properties.insert(gmake_property(parameters.sigma));
64
65 JParser<> zap("Program to test JRootfit algorithm.");
66
67 zap['f'] = make_field(inputFile) = "";
68 zap['o'] = make_field(outputFile);
69 zap['@'] = make_field(properties) = JPARSER::initialised();
70 zap['x'] = make_field(X) = range_type();
71 zap['y'] = make_field(Y) = range_type();
72 zap['n'] = make_field(N) = 0;
73 zap['O'] = make_field(option) = count_t, value_t;
74 zap['w'] = make_field(writeFits);
75 zap['S'] = make_field(seed) = 0;
76 zap['d'] = make_field(debug) = 1;
77
78 zap(argc, argv);
79 }
80 catch(const exception& error) {
81 FATAL(error.what() << endl);
82 }
83
84
85 gRandom->SetSeed(seed);
86
87 const size_t ls = 2;
88
89 const size_t nx = 20 * ls;
90 const double xmin = -5.0 * ls;
91 const double xmax = +5.0 * ls;
92
93 const size_t ny = 20 * ls;
94 const double ymin = -5.0 * ls;
95 const double ymax = +5.0 * ls;
96
97 TH2D* h2 = NULL;
98
99 if (inputFile == "") {
100
101 h2 = new TH2D("h2", NULL, nx, xmin, xmax, ny, ymin, ymax);
102
103 h2->Sumw2();
104
105 auto f2 = (JGauss2X<1>(parameters.center, parameters.sigma) *
106 JGauss2Y<2>(parameters.center, parameters.sigma) *
107 JP0<1>(parameters.signal) +
108 JP0<2>(parameters.background));
109
110 if (N == 0)
111 FillRandom(h2, f2);
112 else
113 FillRandom(h2, f2, N);
114
115 } else {
116
117 TFile* in = TFile::Open(inputFile.c_str(), "exist");
118
119 in->GetObject("h2", h2);
120 }
121
122
123 // determine start values from data
124
125 const double x0 = h2->GetMean(1);
126 const double y0 = h2->GetMean(2);
127 const double xs = h2->GetStdDev(1) * 0.33;
128 const double ys = h2->GetStdDev(2) * 0.33;
129 const double signal = h2->GetMaximum();
130 const double background = h2->GetMinimum() + 0.10;
131
132 // fit
133
134 auto f2 = (JGauss2X<1>(x0, xs) *
135 JGauss2Y<2>(y0, ys) *
136 JP0<1>(signal)) + Exp(JP0<2>(log(background)));
137
138 //auto f2 = JGauss2D<1>(sqrt(x0*y0), sqrt(xs*ys)) * JP0<1>(signal) + JP0<2>(background);
139
140 typedef decltype(f2) function_type;
141
143
144 for (size_t i = 0; i != getNumberOfParameters<function_type>(); ++i) {
145 cout << setw(2) << i << ' ' << FIXED(15,9) << f2[i] << endl;
146 }
147
148 {
149 const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
150
151 const auto result = (option == count_t ?
152 (writeFits ? Fit<m_count>(h2, f2, {}, X, Y) : Fit<m_count>(*h2, f2, {}, X, Y)) :
153 (writeFits ? Fit<m_value>(h2, f2, {}, X, Y) : Fit<m_value>(*h2, f2, {}, X, Y)));
154
155 const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
156
157 cout << "chi2/NDF " << FIXED(7,3) << result.getChi2() << "/" << result.getNDF() << endl;
158 cout << "Number of iterations " << result.numberOfIterations << endl;
159 cout << "Elapsed time [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).count() << endl;
160
161 for (size_t i = 0; i != result.getNumberOfParameters(); ++i) {
162 cout << setw(2) << i << ' '
163 << FIXED(15,9) << result.getValue(i) << " +/- "
164 << FIXED(15,9) << result.getError(i) << endl;
165 }
166 }
167 {
169
170 for (Int_t ix = 1; ix <= h2->GetXaxis()->GetNbins(); ++ix) {
171 for (Int_t iy = 1; iy <= h2->GetYaxis()->GetNbins(); ++iy) {
172
173 const double x = h2->GetXaxis()->GetBinCenter(ix);
174 const double y = h2->GetYaxis()->GetBinCenter(iy);
175 const double value = h2->GetBinContent(ix, iy);
176 const double error = h2->GetBinError (ix, iy);
177
178 gs.put(x, y, value, 0.0, 0.0, error);
179 }
180 }
181
182 JGraph2DErrors g2(gs, "");
183
184 const auto result = Fit(g2, f2, {}, X, Y);
185
186 cout << "chi2/NDF " << FIXED(7,3) << result.getChi2() << "/" << result.getNDF() << endl;
187 cout << "Number of iterations " << result.numberOfIterations << endl;
188
189 for (size_t i = 0; i != result.getNumberOfParameters(); ++i) {
190 cout << setw(2) << i << ' '
191 << FIXED(15,9) << result.getValue(i) << " +/- "
192 << FIXED(15,9) << result.getError(i) << endl;
193 }
194 }
195
196 TFile out(outputFile.c_str(), "recreate");
197
198 out << *h2;
199
200 out.Write();
201 out.Close();
202}
string outputFile
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2140
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
Utility class to parse parameter values.
Utility class to parse command line options.
Definition JParser.hh:1697
JExp< JF1_t > Exp(const JF1_t &f1)
Exponent of function.
Definition JMathlib.hh:2678
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void FillRandom(TH1 *h1, const T &f1)
Fill 1D histogram according Poisson statistics with expectation values from given 1D function.
JRootfit_t< JF1_t > Fit(const TH1 &h1, const JF1_t &f1, const index_list &ls=index_list(), const range_type &X=range_type())
Global fit fuction.
Definition JRootfit.hh:1560
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Make 2D function of x from 1D function.
Definition JMathlib2D.hh:25
Make 2D function of y from 1D function.
Definition JMathlib2D.hh:95
Termination class for polynomial function.
Definition JMathlib.hh:1829
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:67
Data structure for graph data.
void put(const Double_t x, const Double_t y, const Double_t z, const Double_t ex, const Double_t ey, const Double_t ez)
Put data.
Auxiliary data structure to build TGraph2DErrors.
Data point for counting measurement.
Definition JRootfit.hh:126
Data point for value with error.
Definition JRootfit.hh:189
Auxiliary data structure to list files in directory.