CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PtYDistributor.cc
Go to the documentation of this file.
1 
3 
6 #include "TFile.h"
7 #include "TGraph.h"
8 
9 #include "CLHEP/Random/RandGeneral.h"
10 
11 #include <iostream>
12 #include <string>
13 
14 using namespace gen;
15 
17  double ptmax = 100, double ptmin = 0,
18  double ymax = 10, double ymin = -10,
19  int ptbins = 1000, int ybins = 50)
20  : ptmax_(ptmax),ptmin_(ptmin),ymax_(ymax),ymin_(ymin), ptbins_(ptbins), ybins_(ybins)
21 {
22  // edm::FileInPath f1(input);
23  std::string fDataFile = fip.fullPath();
24 
25  std::cout<<" File from "<<fDataFile <<std::endl;
26  TFile f(fDataFile.c_str(),"READ");
27  TGraph* yfunc = (TGraph*)f.Get("rapidity");
28  TGraph* ptfunc = (TGraph*)f.Get("pt");
29 
30  if( !yfunc )
31  throw edm::Exception(edm::errors::NullPointerError,"PtYDistributor")
32  <<"Rapidity distribution could not be found in file "<<fDataFile;
33 
34  if( !ptfunc )
35  throw edm::Exception(edm::errors::NullPointerError,"PtYDistributor")
36  <<"Pt distribution could not be found in file "<<fDataFile;
37 
38  if(ptbins_ > 100000){
39  ptbins_ = 100000;
40  }
41 
42  if(ybins_ > 100000){
43  ybins_ = 100000;
44  }
45 
46  double aProbFunc1[100000];
47  double aProbFunc2[100000];
48 
49  for(int i = 0; i < ybins_; ++i){
50  double xy = ymin_+i*(ymax_-ymin_)/ybins_;
51  double yy = yfunc->Eval(xy);
52  aProbFunc1[i] = yy;
53  }
54 
55  for(int ip = 0; ip < ptbins_; ++ip){
56  double xpt = ptmin_+ip*(ptmax_-ptmin_)/ptbins_;
57  double ypt = ptfunc->Eval(xpt);
58  aProbFunc2[ip] = ypt;
59  }
60 
61  fYGenerator = new CLHEP::RandGeneral(nullptr,aProbFunc1,ybins_);
62  fPtGenerator = new CLHEP::RandGeneral(nullptr,aProbFunc2,ptbins_);
63 
64  f.Close();
65 
66 } // from file
67 
68 double
69 PtYDistributor::fireY(CLHEP::HepRandomEngine* engine){
70  return fireY(ymin_,ymax_, engine);
71 }
72 
73 double
74 PtYDistributor::firePt(CLHEP::HepRandomEngine* engine){
75  return firePt(ptmin_,ptmax_, engine);
76 }
77 
78 double
79 PtYDistributor::fireY(double ymin, double ymax, CLHEP::HepRandomEngine* engine){
80 
81  double y = -999;
82 
83  if(fYGenerator){
84  while(y < ymin || y > ymax)
85  y = ymin_+(ymax_-ymin_)*fYGenerator->shoot(engine);
86  }else{
87  throw edm::Exception(edm::errors::NullPointerError,"PtYDistributor")
88  <<"Random y requested but Random Number Generator for y not Initialized!";
89  }
90  return y;
91 }
92 
93 double
94 PtYDistributor::firePt(double ptmin, double ptmax, CLHEP::HepRandomEngine* engine){
95 
96  double pt = -999;
97 
98  if(fPtGenerator){
99  while(pt < ptmin || pt > ptmax)
100  pt = ptmin_+(ptmax_-ptmin_)*fPtGenerator->shoot(engine);
101  }else{
102  throw edm::Exception(edm::errors::NullPointerError,"PtYDistributor")
103  <<"Random pt requested but Random Number Generator for pt not Initialized!";
104  }
105  return pt;
106 }
int i
Definition: DBlmapReader.cc:9
double firePt(CLHEP::HepRandomEngine *)
double fireY(CLHEP::HepRandomEngine *)
double f[11][100]
CLHEP::RandGeneral * fYGenerator
CLHEP::RandGeneral * fPtGenerator
double ptmin
Definition: HydjetWrapper.h:90
tuple cout
Definition: gather_cfg.py:145
std::string fullPath() const
Definition: FileInPath.cc:184