Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
12 #include "JTools/JHistogram1D.hh"
13 #include "JTools/JCollection.hh"
14 #include "JTools/JGrid.hh"
15 #include "JTools/JSet.hh"
16 #include "JTools/JQuadrature.hh"
17 #include "JTools/JFunction1D_t.hh"
18 #include "JTools/JToolsToolkit.hh"
19 
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 
24 namespace {
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  */
57 int 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 
100  JHistogram1D<JElement_t, JCollection> histogram;
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 
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
string outputFile
bool quadrature
Definition: JResultPDF.cc:23
Double_t G1(const Double_t x)
Integral of method g1.
Definition: JQuantiles.cc:37
void makePDF(const JHistogram1D< JElement_t, JContainer_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of histogram to probability density function (PDF).
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
This include file contains various recursive methods to operate on multi-dimensional collections...
#define ERROR(A)
Definition: JMessage.hh:66
General purpose class for a collection of sorted elements.
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Auxiliary classes for numerical integration.
Utility class to parse command line options.
JGrid< JAbscissa_t > make_grid(const int nx, const JAbscissa_t Xmin, const JAbscissa_t Xmax)
Helper method for JGrid.
Definition: JGrid.hh:177
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:998
int numberOfBins
number of bins for average CDF integral of optical module
Definition: JSirene.cc:64
JElement_t::ordinate_type integrate(const JCollection< JElement_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of data points to integral values.
Definition: JCollection.hh:813
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25