Jpp  19.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
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 "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

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file JRootfitToGauss.cc.

31 {
32  using namespace std;
33  using namespace JPP;
34 
35  const char* const count_t = "count";
36  const char* const value_t = "value";
37 
38  struct {
39  double signal = 25;
40  double background = 5;
41  double center = 0.0;
42  double sigma = 1.0;
43  } parameters;
44 
45  string inputFile;
46  string outputFile;
47  range_type X;
48  size_t N;
49  string option;
50  bool writeFits;
51  UInt_t seed;
52  int debug;
53 
54  try {
55 
56  JProperties properties;
57 
58  properties.insert(gmake_property(parameters.signal));
59  properties.insert(gmake_property(parameters.background));
60  properties.insert(gmake_property(parameters.center));
61  properties.insert(gmake_property(parameters.sigma));
62 
63  JParser<> zap("Program to test JRootfit algorithm.");
64 
65  zap['f'] = make_field(inputFile) = "";
66  zap['o'] = make_field(outputFile);
67  zap['@'] = make_field(properties) = JPARSER::initialised();
68  zap['x'] = make_field(X) = range_type();
69  zap['n'] = make_field(N) = 0;
70  zap['O'] = make_field(option) = count_t, value_t;
71  zap['w'] = make_field(writeFits);
72  zap['S'] = make_field(seed) = 0;
73  zap['d'] = make_field(debug) = 1;
74 
75  zap(argc, argv);
76  }
77  catch(const exception& error) {
78  FATAL(error.what() << endl);
79  }
80 
81 
82  gRandom->SetSeed(seed);
83 
84  const size_t nx = 20;
85  const double xmin = parameters.center - 5.0 * parameters.sigma;
86  const double xmax = parameters.center + 5.0 * parameters.sigma;
87 
88  TH1D* h1 = NULL;
89 
90  if (inputFile == "") {
91 
92  h1 = new TH1D("h1", NULL, nx, xmin, xmax);
93 
94  h1->Sumw2();
95 
96  auto f0 = JP0<1>(parameters.signal) * JGauss<1>(parameters.center, parameters.sigma) + JP0<2>(parameters.background);
97 
98  if (N == 0)
99  FillRandom(h1, f0);
100  else
101  FillRandom(h1, f0, N);
102 
103  } else {
104 
105  TFile* in = TFile::Open(inputFile.c_str(), "exist");
106 
107  in->GetObject("h1", h1);
108  }
109 
110 
111  // determine start values from data
112 
113  const double center = h1->GetMean();
114  const double sigma = h1->GetStdDev() * 0.66;
115  const double signal = h1->GetMaximum();
116  const double background = h1->GetMinimum() + 0.10;
117 
118 
119  // fit
120 
121  auto f1 = JP0<1>(signal) * JGauss<1>(center, sigma) + Exp(JP0<2>(log(background)));
122 
123  typedef decltype(f1) function_type;
124 
125  JRootfit<function_type>::debug = debug;
126 
127  for (size_t i = 0; i != getNumberOfParameters<function_type>(); ++i) {
128  cout << setw(2) << i << ' '
129  << FIXED(15,9) << getParameter(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 
152 
153  TFile out(outputFile.c_str(), "recreate");
154 
155  out << *h1;
156 
157  out.Write();
158  out.Close();
159 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1711
int getParameter(const std::string &text)
Get parameter number from text string.
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
Utility class to parse parameter values.
Definition: JProperties.hh:497
then usage $script< input file >[option] nPossible options count
Definition: JVolume1D.sh:31
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84
JExp< JF1_t > Exp(const JF1_t &f1)
Exponent of function.
Definition: JMathlib.hh:2545
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
JRange< int > range_type
const double sigma[]
Definition: JQuadrature.cc:74
const JPolynome f1(1.0, 2.0, 3.0)
Function.
void FillRandom(TH1 *h1, const T &f1)
Fill 1D histogram according Poisson statistics with expectation values from given 1D function...
Definition: JRootTestkit.hh:33
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
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:1247
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
then fatal The output file must have the wildcard in the e g root fi eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
no fit printf nominal n $STRING awk v X
int debug
debug level
do echo n Creating graphics for string $STRING for((FLOOR=$FIRST_FLOOR;$FLOOR<=$LAST_FLOOR;FLOOR+=1))
size_t getNumberOfParameters()
Get number of parameters.
Definition: JMathlib.hh:155