test_mc_rejection_method_lin.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 = 500000;
17  const int nbins = 300;
18  const double ymax = 1.1;
19  const double xmin = 0;
20  const double xmax = 10;
21  const double dx = xmax-xmin;
22 
23  TRandom rg;
24 
25  TF1 * func = new TF1 ("func","1/(x+1)",0,10);
26  TH1D * hgen = new TH1D ("hgen","generated",nbins,xmin,xmax);
27 
28  for(int i=0; i<N; i++) {
29  cout << "..................." << i << endl;
30  bool selected=false;
31  while(1) {
32  double xg = xmin + rg.Uniform() * dx;
33  double yg = ymax * rg.Uniform();
34  double yc = func->Eval(xg);
35  selected = (yg<yc);
36  if(selected) {
37  hgen->Fill(xg);
38  break;
39  }
40  }
41  }
42 
43  double IF = func->Integral(xmin,xmax);
44  double IH = hgen->Integral("width");
45  double sc = IF/IH;
46  hgen->Scale(sc);
47 
48  hgen->Draw();
49  func->Draw("same");
50 }
std::map< std::string, double > xmax
Double_t ymax
Definition: plot.C:25
const int nbins
Definition: cellShifts.C:15
double dx[NP][NC]
double func(double x, double y)
OStream cout
Definition: OStream.cxx:6
void test_mc_rejection_method_lin()