CMS 3D CMS Logo

List of all members | Public Member Functions | Protected Attributes
edm::Lumi3DReWeighting Class Reference

#include <Lumi3DReWeighting.h>

Public Member Functions

 Lumi3DReWeighting (std::string generatedFile, std::string dataFile, std::string GenHistName, std::string DataHistName, std::string WeightOutputFile)
 
 Lumi3DReWeighting (const std::vector< float > &MC_distr, const std::vector< float > &Lumi_distr, std::string WeightOutputFile)
 
 Lumi3DReWeighting ()
 
double weight3D (const edm::EventBase &e)
 
double weight3D (int, int, int)
 
void weight3D_init (float Scale)
 
void weight3D_init (std::string WeightFileName)
 
void weight3D_init (std::string MCFileName, std::string DataFileName)
 
void weight3D_set (std::string generatedFile, std::string dataFile, std::string GenHistName, std::string DataHistName, std::string WeightOutputFile)
 

Protected Attributes

boost::shared_ptr< TH1 > Data_distr_
 
boost::shared_ptr< TFile > dataFile_
 
std::string dataFileName_
 
std::string DataHistName_
 
boost::shared_ptr< TFile > generatedFile_
 
std::string generatedFileName_
 
std::string GenHistName_
 
boost::shared_ptr< TH1 > MC_distr_
 
double Weight3D_ [50][50][50]
 
std::string weightFileName_
 
boost::shared_ptr< TH1 > weights_
 

Detailed Description

Definition at line 29 of file Lumi3DReWeighting.h.

Constructor & Destructor Documentation

Lumi3DReWeighting::Lumi3DReWeighting ( std::string  generatedFile,
std::string  dataFile,
std::string  GenHistName = "pileup",
std::string  DataHistName = "pileup",
std::string  WeightOutputFile = "" 
)

Definition at line 40 of file Lumi3DReWeighting.cc.

References Data_distr_, dataFile_, dataFileName_, DataHistName_, generatedFile_, generatedFileName_, GenHistName_, and MC_distr_.

44  :
45  generatedFileName_( generatedFile),
46  dataFileName_ ( dataFile ),
47  GenHistName_ ( GenHistName ),
48  DataHistName_ ( DataHistName ),
49  weightFileName_ (WeightOutputFile)
50  {
51  generatedFile_ = boost::shared_ptr<TFile>( new TFile(generatedFileName_.c_str()) ); //MC distribution
52  dataFile_ = boost::shared_ptr<TFile>( new TFile(dataFileName_.c_str()) ); //Data distribution
53 
54  boost::shared_ptr<TH1> Data_temp = boost::shared_ptr<TH1>( (static_cast<TH1*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
55 
56  boost::shared_ptr<TH1> MC_temp = boost::shared_ptr<TH1>( (static_cast<TH1*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
57 
58 
59  MC_distr_ = boost::shared_ptr<TH1>( (static_cast<TH1*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
60  Data_distr_ = boost::shared_ptr<TH1>( (static_cast<TH1*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
61 
62  // MC * data/MC = data, so the weights are data/MC:
63 
64  // normalize both histograms first
65 
66  Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
67  MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
68 
69 }
boost::shared_ptr< TH1 > MC_distr_
boost::shared_ptr< TFile > dataFile_
boost::shared_ptr< TFile > generatedFile_
boost::shared_ptr< TH1 > Data_distr_
Lumi3DReWeighting::Lumi3DReWeighting ( const std::vector< float > &  MC_distr,
const std::vector< float > &  Lumi_distr,
std::string  WeightOutputFile = "" 
)

Definition at line 71 of file Lumi3DReWeighting.cc.

References Data_distr_, MC_distr_, and weightFileName_.

72  {
73 
74  weightFileName_ = WeightOutputFile;
75 
76  // no histograms for input: use vectors
77 
78  // now, make histograms out of them:
79 
80  Int_t NMCBins = MC_distr.size();
81 
82  MC_distr_ = boost::shared_ptr<TH1> ( new TH1F("MC_distr","MC dist",NMCBins,0., float(NMCBins)) );
83 
84  Int_t NDBins = Lumi_distr.size();
85 
86  Data_distr_ = boost::shared_ptr<TH1> ( new TH1F("Data_distr","Data dist",NDBins,0., float(NDBins)) );
87 
88  for(int ibin = 1; ibin<NMCBins+1; ++ibin ) {
89  MC_distr_->SetBinContent(ibin,MC_distr[ibin-1]);
90  }
91 
92  for(int ibin = 1; ibin<NDBins+1; ++ibin ) {
93  Data_distr_->SetBinContent(ibin, Lumi_distr[ibin-1]);
94  }
95 
96  // check integrals, make sure things are normalized
97 
98  float deltaH = Data_distr_->Integral();
99  if(fabs(1.0 - deltaH) > 0.001 ) { //*OOPS*...
100  Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
101  }
102  float deltaMC = MC_distr_->Integral();
103  if(fabs(1.0 - deltaMC) > 0.001 ) {
104  MC_distr_->Scale(1.0/ MC_distr_->Integral());
105  }
106 
107 }
boost::shared_ptr< TH1 > MC_distr_
boost::shared_ptr< TH1 > Data_distr_
edm::Lumi3DReWeighting::Lumi3DReWeighting ( )
inline

Member Function Documentation

double Lumi3DReWeighting::weight3D ( const edm::EventBase e)

Definition at line 124 of file Lumi3DReWeighting.cc.

References rpcdqm::BX, edm::EventBase::getByLabel(), min(), edm::min(), and Weight3D_.

Referenced by Lumi3DReWeighting().

124  {
125 
126  using std::min;
127 
128  // get pileup summary information
129 
131  e.getByLabel(edm::InputTag("addPileupInfo"), PupInfo);
132 
133  std::vector<PileupSummaryInfo>::const_iterator PVI;
134 
135  int npm1=-1;
136  int np0=-1;
137  int npp1=-1;
138 
139  for(PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
140 
141  int BX = PVI->getBunchCrossing();
142 
143  if(BX == -1) {
144  npm1 = PVI->getPU_NumInteractions();
145  }
146  if(BX == 0) {
147  np0 = PVI->getPU_NumInteractions();
148  }
149  if(BX == 1) {
150  npp1 = PVI->getPU_NumInteractions();
151  }
152 
153  }
154 
155  npm1 = min(npm1,49);
156  np0 = min(np0,49);
157  npp1 = min(npp1,49);
158 
159  // std::cout << " vertices " << npm1 << " " << np0 << " " << npp1 << std::endl;
160 
161  return Weight3D_[npm1][np0][npp1];
162 
163 }
double Weight3D_[50][50][50]
EventID const & min(EventID const &lh, EventID const &rh)
Definition: EventID.h:137
T min(T a, T b)
Definition: MathUtil.h:58
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:92
double Lumi3DReWeighting::weight3D ( int  pv1,
int  pv2,
int  pv3 
)

Definition at line 110 of file Lumi3DReWeighting.cc.

References min(), edm::min(), and Weight3D_.

110  {
111 
112  using std::min;
113 
114  int npm1 = min(pv1,49);
115  int np0 = min(pv2,49);
116  int npp1 = min(pv3,49);
117 
118  return Weight3D_[npm1][np0][npp1];
119 
120 }
double Weight3D_[50][50][50]
EventID const & min(EventID const &lh, EventID const &rh)
Definition: EventID.h:137
T min(T a, T b)
Definition: MathUtil.h:58
void Lumi3DReWeighting::weight3D_init ( float  Scale)

Definition at line 195 of file Lumi3DReWeighting.cc.

References runEdmFileComparison::base, gather_cfg::cout, Data_distr_, Exception, JetChargeProducer_cfi::exp, factorial(), objects.autophobj::float, mps_fire::i, createfilelist::int, gen::k, MC_distr_, SiStripPI::mean, min(), lumiCalc2::outfile, Weight3D_, weightFileName_, and protons_cff::xi.

Referenced by Lumi3DReWeighting().

195  {
196 
197  // Scale factor is used to shift target distribution (i.e. luminosity scale) 1. = no shift
198 
199  //create histogram to write output weights, save pain of generating them again...
200 
201  TH3D* WHist = new TH3D("WHist","3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
202  TH3D* DHist = new TH3D("DHist","3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
203  TH3D* MHist = new TH3D("MHist","3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
204 
205  using std::min;
206 
207  if( MC_distr_->GetEntries() == 0 ) {
208  std::cout << " MC and Data distributions are not initialized! You must call the Lumi3DReWeighting constructor. " << std::endl;
209  }
210 
211 
212  // arrays for storing number of interactions
213 
214  double MC_ints[50][50][50];
215  double Data_ints[50][50][50];
216 
217  for (int i=0; i<50; i++) {
218  for(int j=0; j<50; j++) {
219  for(int k=0; k<50; k++) {
220  MC_ints[i][j][k] = 0.;
221  Data_ints[i][j][k] = 0.;
222  }
223  }
224  }
225 
226  double factorial[50];
227  double PowerSer[50];
228  double base = 1.;
229 
230  factorial[0] = 1.;
231  PowerSer[0]=1.;
232 
233  for (int i = 1; i<50; ++i) {
234  base = base*float(i);
235  factorial[i] = base;
236  }
237 
238 
239  double x;
240  double xweight;
241  double probi, probj, probk;
242  double Expval, mean;
243  int xi;
244 
245  // Get entries for Data, MC, fill arrays:
246 
247  int NMCbin = MC_distr_->GetNbinsX();
248 
249  for (int jbin=1;jbin<NMCbin+1;jbin++) {
250  x = MC_distr_->GetBinCenter(jbin);
251  xweight = MC_distr_->GetBinContent(jbin); //use as weight for matrix
252 
253  //for Summer 11, we have this int feature:
254  xi = int(x);
255 
256  // Generate Poisson distribution for each value of the mean
257 
258  mean = double(xi);
259 
260  if(mean<0.) {
261  throw cms::Exception("BadInputValue") << " Your histogram generates MC luminosity values less than zero!"
262  << " Please Check. Terminating." << std::endl;
263  }
264 
265 
266  if(mean==0.){
267  Expval = 1.;
268  }
269  else {
270  Expval = exp(-1.*mean);
271  }
272 
273  base = 1.;
274 
275  for (int i = 1; i<50; ++i) {
276  base = base*mean;
277  PowerSer[i] = base; // PowerSer is mean^i
278  }
279 
280  // compute poisson probability for each Nvtx in weight matrix
281 
282  for (int i=0; i<50; i++) {
283  probi = PowerSer[i]/factorial[i]*Expval;
284  for(int j=0; j<50; j++) {
285  probj = PowerSer[j]/factorial[j]*Expval;
286  for(int k=0; k<50; k++) {
287  probk = PowerSer[k]/factorial[k]*Expval;
288  // joint probability is product of event weights multiplied by weight of input distribution bin
289  MC_ints[i][j][k] = MC_ints[i][j][k]+probi*probj*probk*xweight;
290  }
291  }
292  }
293 
294  }
295 
296  int NDatabin = Data_distr_->GetNbinsX();
297 
298  for (int jbin=1;jbin<NDatabin+1;jbin++) {
299  mean = (Data_distr_->GetBinCenter(jbin))*ScaleFactor;
300  xweight = Data_distr_->GetBinContent(jbin);
301 
302  // Generate poisson distribution for each value of the mean
303 
304  if(mean<0.) {
305  throw cms::Exception("BadInputValue") << " Your histogram generates Data luminosity values less than zero!"
306  << " Please Check. Terminating." << std::endl;
307  }
308 
309  if(mean==0.){
310  Expval = 1.;
311  }
312  else {
313  Expval = exp(-1.*mean);
314  }
315 
316  base = 1.;
317 
318  for (int i = 1; i<50; ++i) {
319  base = base*mean;
320  PowerSer[i] = base;
321  }
322 
323  // compute poisson probability for each Nvtx in weight matrix
324 
325  for (int i=0; i<50; i++) {
326  probi = PowerSer[i]/factorial[i]*Expval;
327  for(int j=0; j<50; j++) {
328  probj = PowerSer[j]/factorial[j]*Expval;
329  for(int k=0; k<50; k++) {
330  probk = PowerSer[k]/factorial[k]*Expval;
331  // joint probability is product of event weights multiplied by weight of input distribution bin
332  Data_ints[i][j][k] = Data_ints[i][j][k]+probi*probj*probk*xweight;
333  }
334  }
335  }
336 
337  }
338 
339 
340  for (int i=0; i<50; i++) {
341  //if(i<5) std::cout << "i = " << i << std::endl;
342  for(int j=0; j<50; j++) {
343  for(int k=0; k<50; k++) {
344  if( (MC_ints[i][j][k])>0.) {
345  Weight3D_[i][j][k] = Data_ints[i][j][k]/MC_ints[i][j][k];
346  }
347  else {
348  Weight3D_[i][j][k] = 0.;
349  }
350  WHist->SetBinContent( i+1,j+1,k+1,Weight3D_[i][j][k] );
351  DHist->SetBinContent( i+1,j+1,k+1,Data_ints[i][j][k] );
352  MHist->SetBinContent( i+1,j+1,k+1,MC_ints[i][j][k] );
353  // if(i<5 && j<5 && k<5) std::cout << Weight3D_[i][j][k] << " " ;
354  }
355  // if(i<5 && j<5) std::cout << std::endl;
356  }
357  }
358 
359  if(! weightFileName_.empty() ) {
360  std::cout << " 3D Weight Matrix initialized! " << std::endl;
361  std::cout << " Writing weights to file " << weightFileName_ << " for re-use... " << std::endl;
362 
363 
364  TFile * outfile = new TFile(weightFileName_.c_str(),"RECREATE");
365  WHist->Write();
366  MHist->Write();
367  DHist->Write();
368  outfile->Write();
369  outfile->Close();
370  outfile->Delete();
371  }
372 
373  return;
374 
375 
376 }
double Weight3D_[50][50][50]
T min(T a, T b)
Definition: MathUtil.h:58
base
Make Sure CMSSW is Setup ##.
int k[5][pyjets_maxn]
boost::shared_ptr< TH1 > MC_distr_
int factorial(int n)
factorial function
boost::shared_ptr< TH1 > Data_distr_
void Lumi3DReWeighting::weight3D_init ( std::string  WeightFileName)

Definition at line 379 of file Lumi3DReWeighting.cc.

References gather_cfg::cout, Exception, mps_fire::i, gen::k, and Weight3D_.

379  {
380 
381  TFile *infile = new TFile(WeightFileName.c_str());
382  TH1F *WHist = (TH1F*)infile->Get("WHist");
383 
384  // Check if the histogram exists
385  if (!WHist) {
386  throw cms::Exception("HistogramNotFound") << " Could not find the histogram WHist in the file "
387  << "in the file " << WeightFileName << "." << std::endl;
388  }
389 
390  for (int i=0; i<50; i++) {
391  // if(i<5) std::cout << "i = " << i << std::endl;
392  for(int j=0; j<50; j++) {
393  for(int k=0; k<50; k++) {
394  Weight3D_[i][j][k] = WHist->GetBinContent(i+1,j+1,k+1);
395  // if(i<5 && j<5 && k<5) std::cout << Weight3D_[i][j][k] << " ";
396  }
397  // if(i<5 && j<5) std::cout << std::endl;
398 
399  }
400  }
401 
402  std::cout << " 3D Weight Matrix initialized! " << std::endl;
403 
404  return;
405 
406 
407 }
double Weight3D_[50][50][50]
int k[5][pyjets_maxn]
void Lumi3DReWeighting::weight3D_init ( std::string  MCFileName,
std::string  DataFileName 
)

Definition at line 409 of file Lumi3DReWeighting.cc.

References gather_cfg::cout, Exception, mps_fire::i, gen::k, and Weight3D_.

409  {
410 
411  TFile *infileMC = new TFile(MCWeightFileName.c_str());
412  TH3D *MHist = (TH3D*)infileMC->Get("MHist");
413 
414  // Check if the histogram exists
415  if (!MHist) {
416  throw cms::Exception("HistogramNotFound") << " Could not find the histogram MHist in the file "
417  << "in the file " << MCWeightFileName << "." << std::endl;
418  }
419 
420  TFile *infileD = new TFile(DataWeightFileName.c_str());
421  TH3D *DHist = (TH3D*)infileD->Get("DHist");
422 
423  // Check if the histogram exists
424  if (!DHist) {
425  throw cms::Exception("HistogramNotFound") << " Could not find the histogram DHist in the file "
426  << "in the file " << DataWeightFileName << "." << std::endl;
427  }
428 
429  for (int i=0; i<50; i++) {
430  for(int j=0; j<50; j++) {
431  for(int k=0; k<50; k++) {
432  Weight3D_[i][j][k] = DHist->GetBinContent(i+1,j+1,k+1)/MHist->GetBinContent(i+1,j+1,k+1);
433  }
434  }
435  }
436 
437  std::cout << " 3D Weight Matrix initialized! " << std::endl;
438 
439  return;
440 
441 
442 }
double Weight3D_[50][50][50]
int k[5][pyjets_maxn]
void Lumi3DReWeighting::weight3D_set ( std::string  generatedFile,
std::string  dataFile,
std::string  GenHistName,
std::string  DataHistName,
std::string  WeightOutputFile 
)

Definition at line 165 of file Lumi3DReWeighting.cc.

References gather_cfg::cout, Data_distr_, dataFile_, dataFileName_, DataHistName_, generatedFile_, generatedFileName_, GenHistName_, MC_distr_, and weightFileName_.

Referenced by Lumi3DReWeighting().

166 {
167 
168  generatedFileName_ = generatedFile;
169  dataFileName_ = dataFile ;
170  GenHistName_ = GenHistName ;
171  DataHistName_= DataHistName ;
172  weightFileName_ = WeightOutputFile;
173 
174  std::cout<< " seting values: " << generatedFileName_ << " " << dataFileName_ << " " << GenHistName_ << " " << DataHistName_ << std::endl;
175 
176  generatedFile_ = boost::shared_ptr<TFile>( new TFile(generatedFileName_.c_str()) ); //MC distribution
177  dataFile_ = boost::shared_ptr<TFile>( new TFile(dataFileName_.c_str()) ); //Data distribution
178 
179  boost::shared_ptr<TH1> Data_temp = boost::shared_ptr<TH1>( (static_cast<TH1*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
180 
181  boost::shared_ptr<TH1> MC_temp = boost::shared_ptr<TH1>( (static_cast<TH1*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
182 
183 
184  MC_distr_ = boost::shared_ptr<TH1>( (static_cast<TH1*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
185  Data_distr_ = boost::shared_ptr<TH1>( (static_cast<TH1*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
186 
187  // MC * data/MC = data, so the weights are data/MC:
188 
189  // normalize both histograms first
190 
191  Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
192  MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
193 }
boost::shared_ptr< TH1 > MC_distr_
boost::shared_ptr< TFile > dataFile_
boost::shared_ptr< TFile > generatedFile_
boost::shared_ptr< TH1 > Data_distr_

Member Data Documentation

boost::shared_ptr<TH1> edm::Lumi3DReWeighting::Data_distr_
protected

Definition at line 69 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), weight3D_init(), and weight3D_set().

boost::shared_ptr<TFile> edm::Lumi3DReWeighting::dataFile_
protected

Definition at line 63 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), and weight3D_set().

std::string edm::Lumi3DReWeighting::dataFileName_
protected

Definition at line 58 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), and weight3D_set().

std::string edm::Lumi3DReWeighting::DataHistName_
protected

Definition at line 60 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), and weight3D_set().

boost::shared_ptr<TFile> edm::Lumi3DReWeighting::generatedFile_
protected

Definition at line 62 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), and weight3D_set().

std::string edm::Lumi3DReWeighting::generatedFileName_
protected

Definition at line 57 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), and weight3D_set().

std::string edm::Lumi3DReWeighting::GenHistName_
protected

Definition at line 59 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), and weight3D_set().

boost::shared_ptr<TH1> edm::Lumi3DReWeighting::MC_distr_
protected

Definition at line 68 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), weight3D_init(), and weight3D_set().

double edm::Lumi3DReWeighting::Weight3D_[50][50][50]
protected

Definition at line 72 of file Lumi3DReWeighting.h.

Referenced by weight3D(), and weight3D_init().

std::string edm::Lumi3DReWeighting::weightFileName_
protected

Definition at line 61 of file Lumi3DReWeighting.h.

Referenced by Lumi3DReWeighting(), weight3D_init(), and weight3D_set().

boost::shared_ptr<TH1> edm::Lumi3DReWeighting::weights_
protected

Definition at line 64 of file Lumi3DReWeighting.h.