test_mc_rejection_method_log.C
Go to the documentation of this file.
1 //
2 // MC rejection method: Sanity check
3 //
4 // Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
5 // University of Liverpool & STFC Rutherford Appleton Lab
6 //
7 
8 #include <iostream>
9 
10 #include <TRandom.h>
11 #include <TF1.h>
12 #include <TH1D.h>
13 
15 {
16  const int N = 100000;
17  const int nbins = 200;
18  const double ymax = 1.1;
19  const double xmin = 1E-2;
20  const double xmax = 10;
21  const double logxmin = TMath::Log(xmin);
22  const double logxmax = TMath::Log(xmax);
23  const double dlogx = logxmax-logxmin;
24 
25  TRandom rg;
26 
27  TF1 * func = new TF1 ("func","1/(x+1)",0,10);
28  TH1D * hgen = new TH1D ("hgen","generated",nbins,xmin,xmax);
29 
30  for(int i=0; i<N; i++) {
31  cout << "..................." << i << endl;
32  bool selected=false;
33  while(1) {
34  double xg = TMath::Exp(logxmin + rg.Uniform() * dlogx);
35  double yg = ymax * rg.Uniform();
36  double yc = xg * func->Eval(xg);
37  selected = (yg<yc);
38  if(selected) {
39  hgen->Fill(xg);
40  break;
41  }
42  }
43  }
44 
45  double IF = func->Integral(xmin,xmax);
46  double IH = hgen->Integral("width");
47  double sc = IF/IH;
48  hgen->Scale(sc);
49 
50  hgen->Draw();
51  func->Draw("same");
52 }
std::map< std::string, double > xmax
Double_t ymax
Definition: plot.C:25
const int nbins
Definition: cellShifts.C:15
Float_t E
Definition: plot.C:20
void test_mc_rejection_method_log()
double func(double x, double y)
OStream cout
Definition: OStream.cxx:6