Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JRootfitToGauss.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 "TH1D.h"
#include "JMath/JMathlib.hh"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JRootTestkit.hh"
#include "JROOT/JRootfit.hh"
#include "JROOT/JGraphErrors.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 JRootfitToGauss.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 31 of file JRootfitToGauss.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 = 25;
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 size_t N;
50 string option;
51 bool writeFits;
52 ULong_t seed;
53 int debug;
54
55 try {
56
57 JProperties properties;
58
59 properties.insert(gmake_property(parameters.signal));
60 properties.insert(gmake_property(parameters.background));
61 properties.insert(gmake_property(parameters.center));
62 properties.insert(gmake_property(parameters.sigma));
63
64 JParser<> zap("Program to test JRootfit algorithm.");
65
66 zap['f'] = make_field(inputFile) = "";
67 zap['o'] = make_field(outputFile);
68 zap['@'] = make_field(properties) = JPARSER::initialised();
69 zap['x'] = make_field(X) = range_type();
70 zap['n'] = make_field(N) = 0;
71 zap['O'] = make_field(option) = count_t, value_t;
72 zap['w'] = make_field(writeFits);
73 zap['S'] = make_field(seed) = 0;
74 zap['d'] = make_field(debug) = 1;
75
76 zap(argc, argv);
77 }
78 catch(const exception& error) {
79 FATAL(error.what() << endl);
80 }
81
82
83 gRandom->SetSeed(seed);
84
85 const size_t nx = 20;
86 const double xmin = parameters.center - 5.0 * parameters.sigma;
87 const double xmax = parameters.center + 5.0 * parameters.sigma;
88
89 TH1D* h1 = NULL;
90
91 if (inputFile == "") {
92
93 h1 = new TH1D("h1", NULL, nx, xmin, xmax);
94
95 h1->Sumw2();
96
97 auto f0 = JP0<1>(parameters.signal) * JGauss<1>(parameters.center, parameters.sigma) + JP0<2>(parameters.background);
98
99 if (N == 0)
100 FillRandom(h1, f0);
101 else
102 FillRandom(h1, f0, N);
103
104 } else {
105
106 TFile* in = TFile::Open(inputFile.c_str(), "exist");
107
108 in->GetObject("h1", h1);
109 }
110
111
112 // determine start values from data
113
114 const double center = h1->GetMean();
115 const double sigma = h1->GetStdDev() * 0.66;
116 const double signal = h1->GetMaximum();
117 const double background = h1->GetMinimum() + 0.1;
118
119
120 // fit
121
122 auto f1 = JP0<1>(signal) * JGauss<1>(center, sigma) + Exp(JP0<2>(log(background)));
123
124 typedef decltype(f1) function_type;
125
127
128 for (size_t i = 0; i != getNumberOfParameters<function_type>(); ++i) {
129 cout << setw(2) << i << ' ' << FIXED(15,9) << f1[i] << endl;
130 }
131
132 {
133 const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
134
135 const auto result = (option == count_t ?
136 (writeFits ? Fit<m_count>(h1, f1, {}, X) : Fit<m_count>(*h1, f1, {}, X)) :
137 (writeFits ? Fit<m_value>(h1, f1, {}, X) : Fit<m_value>(*h1, f1, {}, X)));
138
139 const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
140
141 cout << "chi2/NDF " << FIXED(7,3) << result.getChi2() << "/" << result.getNDF() << endl;
142 cout << "Number of iterations " << result.numberOfIterations << endl;
143 cout << "Elapsed time [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).count() << endl;
144
145 for (size_t i = 0; i != result.getNumberOfParameters(); ++i) {
146 cout << setw(2) << i << ' '
147 << FIXED(15,9) << result.getValue(i) << " +/- "
148 << FIXED(15,9) << result.getError(i) << endl;
149 }
150 }
151 {
153
154 for (Int_t ix = 1; ix <= h1->GetXaxis()->GetNbins(); ++ix) {
155
156 const double x = h1->GetXaxis()->GetBinCenter(ix);
157 const double value = h1->GetBinContent(ix);
158 const double error = h1->GetBinError (ix);
159
160 gs.put(x, value, 0.0, error);
161 }
162
163 JGraphErrors g1(gs, "");
164
165 const auto result = Fit(g1, f1, {}, X);
166
167 cout << "chi2/NDF " << FIXED(7,3) << result.getChi2() << "/" << result.getNDF() << endl;
168 cout << "Number of iterations " << result.numberOfIterations << endl;
169
170 for (size_t i = 0; i != result.getNumberOfParameters(); ++i) {
171 cout << setw(2) << i << ' '
172 << FIXED(15,9) << result.getValue(i) << " +/- "
173 << FIXED(15,9) << result.getError(i) << endl;
174 }
175 }
176
177 TFile out(outputFile.c_str(), "recreate");
178
179 out << *h1;
180
181 out.Write();
182 out.Close();
183}
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
Double_t g1(const Double_t x)
Function.
Definition JQuantiles.cc:25
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
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 ex, const Double_t ey)
Put data.
Auxiliary data structure to build TGraphErrors.
Data point for counting measurement.
Definition JRootfit.hh:126
Data point for value with error.
Definition JRootfit.hh:189