#include #include #include #include "TROOT.h" #include "TFile.h" #include "TH2D.h" #include "TMath.h" #include "TRandom.h" #include "JTools/JHistogram1D_t.hh" #include "JTools/JHistogramMap_t.hh" #include "JTools/JMultiHistogram.hh" #include "JTools/JCollection.hh" #include "JTools/JMultiGrid.hh" #include "JTools/JMultiSet.hh" #include "JTools/JQuadrature.hh" #include "JTools/JFunction1D_t.hh" #include "JTools/JFunctionalMap_t.hh" #include "JTools/JToolsToolkit.hh" #include "JTools/JMultiPDF.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)); } /** * Function g2. * * \param x x value * \param y y value * \return function value */ inline Double_t g2(const Double_t x, const Double_t y) { return g1(x) * g1(y); } /** * Integral of g2. * * \param x x value * \param y y value * \return integral value */ inline Double_t G2(const Double_t x, const Double_t y) { return G1(x) * G1(y); } } /** * \file * * Example program to test JTOOLS::JMultiHistogram. * \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 2D 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 = 100; const Double_t xmin = -3.0; const Double_t xmax = +3.0; typedef JMultiHistogram > JHistogram2D_t; JHistogram2D_t histogram; // abscissa if (!quadrature) configure(histogram, make_grid(numberOfBins + 1, xmin, xmax)); else configure(histogram, make_set(JGaussHermite(numberOfBins + 1))); // fill for (int i = 0; i != numberOfEvents; ++i) { const double x = gRandom->Gaus(0.0, 1.0); const double y = gRandom->Gaus(0.0, 1.0); histogram.fill(x, y, 1.0); } histogram.div((double) numberOfEvents); // interpolation based on function values JMultiPDF > f2(histogram); // interpolation based on integral values accumulate(histogram); JMultiFunction > F2; copy(histogram, F2); F2.compile(); TFile out(outputFile.c_str(), "recreate"); TH2D h0("h0", NULL, nx, xmin, xmax, nx, xmin, xmax); TH2D h1("h1", NULL, nx, xmin, xmax, nx, xmin, xmax); TH2D h2("h2", NULL, nx, xmin, xmax, nx, xmin, xmax); for (int i = 1; i <= h0.GetXaxis()->GetNbins(); ++i) { for (int j = 1; j <= h0.GetYaxis()->GetNbins(); ++j) { const Double_t x = h0.GetXaxis()->GetBinCenter(i); const Double_t y = h0.GetYaxis()->GetBinCenter(j); h0.SetBinContent(i, j, g2(x,y)); try { h1.SetBinContent(i, j, F2(x,y).fp.fp); h2.SetBinContent(i, j, f2(x,y)); } catch(const exception& error) { //ERROR(error.what() << endl); } } } if (subtract) { h1.Add(&h0, -1.0); h2.Add(&h0, -1.0); } out.Write(); out.Close(); }