Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JTools/JHistogram1D.cc
Go to the documentation of this file.
1
2#include <string>
3#include <iostream>
4#include <iomanip>
5
6#include "TROOT.h"
7#include "TFile.h"
8#include "TH1D.h"
9#include "TMath.h"
10#include "TRandom.h"
11
13#include "JTools/JCollection.hh"
14#include "JTools/JGrid.hh"
15#include "JTools/JSet.hh"
16#include "JTools/JQuadrature.hh"
19
20#include "Jeep/JParser.hh"
21#include "Jeep/JMessage.hh"
22
23
24namespace {
25
26 /**
27 * Function g1.
28 *
29 * \param x x value
30 * \return function value
31 */
32 inline Double_t g1(const Double_t x)
33 {
34 return TMath::Gaus(x, 0.0, 1.0, kTRUE);
35 }
36
37
38 /**
39 * Integral of g1.
40 *
41 * \param x x value
42 * \return integral value
43 */
44 inline Double_t G1(const Double_t x)
45 {
46 return 0.5 * (1.0 + TMath::Erf(sqrt(0.5)*x));
47 }
48}
49
50
51/**
52 * \file
53 *
54 * Example program to test JTOOLS::JHistogram1D.
55 * \author mdejong
56 */
57int main(int argc, char **argv)
58{
59 using namespace std;
60
61 string outputFile;
62 int numberOfEvents;
63 int numberOfBins;
64 bool quadrature;
65 bool subtract;
66 int debug;
67
68 try {
69
70 JParser<> zap("Example program to test 1D histogram.");
71
72 zap['o'] = make_field(outputFile) = "histogram.root";
73 zap['n'] = make_field(numberOfEvents);
74 zap['N'] = make_field(numberOfBins);
75 zap['Q'] = make_field(quadrature);
76 zap['D'] = make_field(subtract);
77 zap['d'] = make_field(debug) = 3;
78
79 zap(argc, argv);
80 }
81 catch(const exception &error) {
82 FATAL(error.what() << endl);
83 }
84
85
86 if (numberOfEvents <= 0) {
87 FATAL("No events." << endl);
88 }
89
90 using namespace JPP;
91
92
93 const Int_t nx = 1000;
94 const Double_t xmin = -5.0;
95 const Double_t xmax = +5.0;
96
97
98 typedef JElement2D<double, double> JElement_t;
99
101
102
103 // abscissa
104
105 if (!quadrature)
106 histogram.configure(make_grid(numberOfBins, xmin, xmax));
107 else
108 histogram.configure(JGaussHermite(numberOfBins));
109
110
111 // fill
112
113 for (int i = 0; i != numberOfEvents; ++i) {
114 histogram.fill(gRandom->Gaus(0.0, 1.0), 1.0);
115 }
116
117 histogram.div((double) numberOfEvents);
118
119
120 // interpolation
121
122 typedef JSplineFunction1S_t JFunction1D_t;
123 //typedef JPolintFunction1D<3, JElement_t, JCollection, JResultHesse<double> > JFunction1D_t;
124
125
126 JFunction1D_t F1;
127
128 integrate(histogram, F1);
129
130 F1.compile();
131
132
133 JFunction1D_t f1;
134
135 makePDF(histogram, f1);
136
137 f1.compile();
138
139 f1.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
140 F1.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
141
142
143 TFile out(outputFile.c_str(), "recreate");
144
145 TH1D h0("h0", NULL, nx, xmin, xmax);
146 TH1D h1("h1", NULL, nx, xmin, xmax);
147 TH1D h2("h2", NULL, nx, xmin, xmax);
148
149 TH1D i0("i0", NULL, nx, xmin, xmax);
150 TH1D i1("i1", NULL, nx, xmin, xmax);
151
152 for (int i = 1; i <= h0.GetNbinsX(); ++i) {
153
154 const Double_t x = h0.GetBinCenter(i);
155
156 h0.SetBinContent(i, g1(x));
157 i0.SetBinContent(i, G1(x));
158
159 try {
160 h1.SetBinContent(i, get_value(F1(x).fp));
161 h2.SetBinContent(i, get_value(f1(x)));
162 i1.SetBinContent(i, get_value(F1(x)));
163 }
164 catch(const exception& error) {
165 ERROR(error.what() << endl);
166 }
167 }
168
169 if (subtract) {
170 h1.Add(&h0, -1.0);
171 h2.Add(&h0, -1.0);
172 i1.Add(&i0, -1.0);
173 }
174
175 out.Write();
176 out.Close();
177}
178
General purpose class for a collection of sorted elements.
string outputFile
General purpose messaging.
#define ERROR(A)
Definition JMessage.hh:66
#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
Auxiliary classes for numerical integration.
Double_t g1(const Double_t x)
Function.
Definition JQuantiles.cc:25
Double_t G1(const Double_t x)
Integral of method g1.
Definition JQuantiles.cc:37
bool quadrature
Definition JResultPDF.cc:23
int numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:73
This include file contains various recursive methods to operate on multi-dimensional collections.
int main(int argc, char **argv)
Utility class to parse command line options.
Definition JParser.hh:1698
Numerical integrator for .
Histogram in 1D.
void fill(typename JClass< abscissa_type >::argument_type x, typename JClass< contents_type >::argument_type w)
Fill histogram.
JHistogram1D & div(const double value)
Scale contents.
static const JZero zero
Function object to assign zero value.
Definition JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
2D Element.
Definition JPolint.hh:1131
Type definition of a spline interpolation method based on a JCollection with JResultPDF result type.