#include #include #include #include "TROOT.h" #include "TFile.h" #include "TH1D.h" #include "TMath.h" #include "TRandom.h" #include "JTools/JHistogram1D.hh" #include "JTools/JCollection.hh" #include "JTools/JGrid.hh" #include "JTools/JSet.hh" #include "JTools/JQuadrature.hh" #include "JTools/JFunction1D_t.hh" #include "JTools/JToolsToolkit.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { /** * Function g1. * * \param x x value * \return function value */ inline Double_t g1(const Double_t x) { return TMath::Gaus(x, 0.0, 1.0, kTRUE); } /** * Integral of g1. * * \param x x value * \return integral value */ inline Double_t G1(const Double_t x) { return 0.5 * (1.0 + TMath::Erf(sqrt(0.5)*x)); } } /** * \file * * Example program to test JTOOLS::JHistogram1D. * \author mdejong */ int main(int argc, char **argv) { using namespace std; string outputFile; int numberOfEvents; int numberOfBins; bool quadrature; bool subtract; int debug; try { JParser<> zap("Example program to test 1D histogram."); zap['o'] = make_field(outputFile) = "histogram.root"; zap['n'] = make_field(numberOfEvents); zap['N'] = make_field(numberOfBins); zap['Q'] = make_field(quadrature); zap['D'] = make_field(subtract); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } if (numberOfEvents <= 0) { FATAL("No events." << endl); } using namespace JPP; const Int_t nx = 1000; const Double_t xmin = -5.0; const Double_t xmax = +5.0; typedef JElement2D JElement_t; JHistogram1D histogram; // abscissa if (!quadrature) histogram.configure(make_grid(numberOfBins, xmin, xmax)); else histogram.configure(JGaussHermite(numberOfBins)); // fill for (int i = 0; i != numberOfEvents; ++i) { histogram.fill(gRandom->Gaus(0.0, 1.0), 1.0); } histogram.div((double) numberOfEvents); // interpolation typedef JSplineFunction1S_t JFunction1D_t; //typedef JPolintFunction1D<3, JElement_t, JCollection, JResultHesse > JFunction1D_t; JFunction1D_t F1; integrate(histogram, F1); F1.compile(); JFunction1D_t f1; makePDF(histogram, f1); f1.compile(); f1.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero)); F1.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero)); TFile out(outputFile.c_str(), "recreate"); TH1D h0("h0", NULL, nx, xmin, xmax); TH1D h1("h1", NULL, nx, xmin, xmax); TH1D h2("h2", NULL, nx, xmin, xmax); TH1D i0("i0", NULL, nx, xmin, xmax); TH1D i1("i1", NULL, nx, xmin, xmax); for (int i = 1; i <= h0.GetNbinsX(); ++i) { const Double_t x = h0.GetBinCenter(i); h0.SetBinContent(i, g1(x)); i0.SetBinContent(i, G1(x)); try { h1.SetBinContent(i, get_value(F1(x).fp)); h2.SetBinContent(i, get_value(f1(x))); i1.SetBinContent(i, get_value(F1(x))); } catch(const exception& error) { ERROR(error.what() << endl); } } if (subtract) { h1.Add(&h0, -1.0); h2.Add(&h0, -1.0); i1.Add(&i0, -1.0); } out.Write(); out.Close(); }