#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TF1.h" #include "TH1D.h" #include "TMath.h" #include "JFit/JGandalf.hh" #include "JTools/JElement.hh" #include "JTools/JQuantile.hh" #include "JROOT/JRootToolkit.hh" #include "JMath/JGauss.hh" #include "JMath/JConstants.hh" #include "Jeep/JTimer.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { using namespace JPP; using TMath::PoissonI; typedef JElement2D JElement_t; typedef JGandalf::result_type result_type; /** * Fit function. * * \param gauss gauss * \param point point * \return chi2 */ inline result_type g1(const JGauss& gauss, const JElement_t& point) { result_type result; const double u = (point.getX() - gauss.mean) / gauss.sigma; const double fs = gauss.signal * exp(-0.5*u*u) / (sqrt(2.0*PI) * gauss.sigma); const double fb = gauss.background; const double f1 = fs + fb; const double p = PoissonI(point.getY(), f1); result.chi2 = -log(p); result.gradient.mean = fs * u / gauss.sigma; // d(f)/d(mean) result.gradient.sigma = fs * u*u / gauss.sigma - fs / gauss.sigma; // d(f)/d(sigma) result.gradient.signal = fs / gauss.signal; // d(f)/d(signal) result.gradient.background = 1.0; // d(f)/d(background) result.gradient *= 1.0 - point.getY()/f1; // d(chi2)/d(f) return result; } } /** * \file * * Program to test JFIT::JGandalf algorithm. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; string outputFile; int numberOfEvents; JGauss gauss; JGauss precision; int debug; try { JParser<> zap("Program to test JGandalf algorithm."); zap['o'] = make_field(outputFile) = ""; zap['n'] = make_field(numberOfEvents) = 100; zap['@'] = make_field(gauss) = JGauss(0.0, 1.0, 1000.0, 100.0); zap['e'] = make_field(precision) = JGauss(0.05, 0.05, 25.0, 25.0); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception& error) { FATAL(error.what() << endl); } using namespace JFIT; ASSERT(numberOfEvents > 0); JGandalf::debug = debug; TF1 fs("fs", "exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1]))"); TF1 fb("fb", "1.0"); fs.FixParameter(0, gauss.mean); fs.FixParameter(1, gauss.sigma); const Int_t nx = 21; const Double_t xmin = -5.0; const Double_t xmax = +5.0; JQuantile Q[] = { JQuantile("mean "), JQuantile("sigma "), JQuantile("signal "), JQuantile("background") }; TH1D H[] = { TH1D("ha", "", 101, -0.1, +0.1), TH1D("hb", "", 101, -0.1, +0.1), TH1D("hc", "", 101, -100.0, +100.0), TH1D("hd", "", 101, -100.0, +100.0) }; JTimer timer; for (int i = 0; i != numberOfEvents; ++i) { STATUS("event: " << setw(10) << i << '\r'); DEBUG(endl); TH1D h0("h0", NULL, nx, xmin, xmax); h0.Sumw2(); h0.FillRandom("fs", (Int_t) gauss.signal); h0.FillRandom("fb", (Int_t) gauss.background); vector data; for (Int_t i = 1; i <= h0.GetNbinsX(); ++i) { data.push_back(JElement_t(h0.GetBinCenter (i), h0.GetBinContent(i))); } JGandalf fit; JGandalf::EPSILON = 1.0e-4; fit.parameters.resize(4); fit.parameters[0] = &JGauss::mean; fit.parameters[1] = &JGauss::sigma; fit.parameters[2] = &JGauss::signal; fit.parameters[3] = &JGauss::background; fit.value = JGauss(h0.GetMean(), h0.GetRMS(), h0.GetEntries() * (xmax - xmin) / nx - h0.GetMinimum(), h0.GetMinimum()); DEBUG("Start value " << fit.value << endl); timer.start(); const double chi2 = fit(g1, data.begin(), data.end()); timer.stop(); DEBUG("Final value " << fit.value << endl); DEBUG("Chi2 " << chi2 << endl); const double Y[] = { fit.value.mean - gauss.mean, fit.value.sigma - gauss.sigma, fit.value.signal * nx / (xmax - xmin) - gauss.signal, fit.value.background * nx - gauss.background }; for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) { Q[i].put (Y[i]); H[i].Fill(Y[i]); } } for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) { NOTICE((i == 0 ? longprint : shortprint) << Q[i]); } if (debug >= notice_t) { timer.print(cout, true, micro_t); } if (outputFile != "") { TFile out(outputFile.c_str(), "recreate"); for (int i = 0; i != sizeof(H)/sizeof(H[0]); ++i) { out << H[i]; } out.Write(); out.Close(); } for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) { ASSERT(fabs(Q[i].getMean()) < Q[i].getSTDev()); } ASSERT(Q[0].getSTDev() < precision.mean); ASSERT(Q[1].getSTDev() < precision.sigma); ASSERT(Q[2].getSTDev() < precision.signal); ASSERT(Q[3].getSTDev() < precision.background); return 0; }