1 #ifndef PhysicsTools_Utilities_interface_Lumi3DReWeighting_cc
2 #define PhysicsTools_Utilities_interface_Lumi3DReWeighting_cc
23 #include "TStopwatch.h"
29 #include <boost/shared_ptr.hpp>
41 std::string GenHistName =
"pileup",
42 std::string DataHistName =
"pileup",
43 std::string WeightOutputFile =
"") :
44 generatedFileName_( generatedFile),
45 dataFileName_ ( dataFile ),
46 GenHistName_ ( GenHistName ),
47 DataHistName_ ( DataHistName ),
48 weightFileName_ (WeightOutputFile)
53 boost::shared_ptr<TH1> Data_temp = boost::shared_ptr<TH1>( (
static_cast<TH1*
>(
dataFile_->Get(
DataHistName_.c_str() )->Clone() )) );
55 boost::shared_ptr<TH1> MC_temp = boost::shared_ptr<TH1>( (
static_cast<TH1*
>(
generatedFile_->Get(
GenHistName_.c_str() )->Clone() )) );
65 Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
66 MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
71 std::string WeightOutputFile =
"") {
79 Int_t NMCBins = MC_distr.size();
81 MC_distr_ = boost::shared_ptr<TH1> (
new TH1F(
"MC_distr",
"MC dist",NMCBins,0.,
float(NMCBins)) );
83 Int_t NDBins = Lumi_distr.size();
85 Data_distr_ = boost::shared_ptr<TH1> (
new TH1F(
"Data_distr",
"Data dist",NDBins,0.,
float(NDBins)) );
87 for(
int ibin = 1; ibin<NMCBins+1; ++ibin ) {
88 MC_distr_->SetBinContent(ibin,MC_distr[ibin-1]);
91 for(
int ibin = 1; ibin<NDBins+1; ++ibin ) {
92 Data_distr_->SetBinContent(ibin, Lumi_distr[ibin-1]);
98 if(fabs(1.0 - deltaH) > 0.001 ) {
102 if(fabs(1.0 - deltaMC) > 0.001 ) {
113 int npm1 =
min(pv1,49);
114 int np0 =
min(pv2,49);
115 int npp1 =
min(pv3,49);
132 std::vector<PileupSummaryInfo>::const_iterator PVI;
138 for(PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
140 int BX = PVI->getBunchCrossing();
143 npm1 = PVI->getPU_NumInteractions();
146 np0 = PVI->getPU_NumInteractions();
149 npp1 = PVI->getPU_NumInteractions();
164 void Lumi3DReWeighting::weight3D_set( std::string generatedFile, std::string dataFile, std::string GenHistName, std::string DataHistName, std::string WeightOutputFile )
178 boost::shared_ptr<TH1> Data_temp = boost::shared_ptr<TH1>( (
static_cast<TH1*
>(
dataFile_->Get(
DataHistName_.c_str() )->Clone() )) );
180 boost::shared_ptr<TH1> MC_temp = boost::shared_ptr<TH1>( (
static_cast<TH1*
>(
generatedFile_->Get(
GenHistName_.c_str() )->Clone() )) );
190 Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
191 MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
200 TH3D* WHist =
new TH3D(
"WHist",
"3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
201 TH3D* DHist =
new TH3D(
"DHist",
"3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
202 TH3D* MHist =
new TH3D(
"MHist",
"3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
207 std::cout <<
" MC and Data distributions are not initialized! You must call the Lumi3DReWeighting constructor. " << std::endl;
213 double MC_ints[50][50][50];
214 double Data_ints[50][50][50];
216 for (
int i=0;
i<50;
i++) {
217 for(
int j=0;
j<50;
j++) {
218 for(
int k=0;
k<50;
k++) {
219 MC_ints[
i][
j][
k] = 0.;
220 Data_ints[
i][
j][
k] = 0.;
232 for (
int i = 1;
i<50; ++
i) {
233 base = base*float(
i);
240 double probi, probj, probk;
248 for (
int jbin=1;jbin<NMCbin+1;jbin++) {
250 xweight =
MC_distr_->GetBinContent(jbin);
260 throw cms::Exception(
"BadInputValue") <<
" Your histogram generates MC luminosity values less than zero!"
261 <<
" Please Check. Terminating." << std::endl;
269 Expval =
exp(-1.*mean);
274 for (
int i = 1;
i<50; ++
i) {
281 for (
int i=0;
i<50;
i++) {
282 probi = PowerSer[
i]/factorial[
i]*Expval;
283 for(
int j=0;
j<50;
j++) {
284 probj = PowerSer[
j]/factorial[
j]*Expval;
285 for(
int k=0;
k<50;
k++) {
286 probk = PowerSer[
k]/factorial[
k]*Expval;
288 MC_ints[
i][
j][
k] = MC_ints[
i][
j][
k]+probi*probj*probk*xweight;
297 for (
int jbin=1;jbin<NDatabin+1;jbin++) {
298 mean = (
Data_distr_->GetBinCenter(jbin))*ScaleFactor;
304 throw cms::Exception(
"BadInputValue") <<
" Your histogram generates Data luminosity values less than zero!"
305 <<
" Please Check. Terminating." << std::endl;
312 Expval =
exp(-1.*mean);
317 for (
int i = 1;
i<50; ++
i) {
324 for (
int i=0;
i<50;
i++) {
325 probi = PowerSer[
i]/factorial[
i]*Expval;
326 for(
int j=0;
j<50;
j++) {
327 probj = PowerSer[
j]/factorial[
j]*Expval;
328 for(
int k=0;
k<50;
k++) {
329 probk = PowerSer[
k]/factorial[
k]*Expval;
331 Data_ints[
i][
j][
k] = Data_ints[
i][
j][
k]+probi*probj*probk*xweight;
339 for (
int i=0;
i<50;
i++) {
341 for(
int j=0;
j<50;
j++) {
342 for(
int k=0;
k<50;
k++) {
343 if( (MC_ints[
i][
j][
k])>0.) {
350 DHist->SetBinContent(
i+1,
j+1,k+1,Data_ints[
i][
j][k] );
351 MHist->SetBinContent(
i+1,
j+1,k+1,MC_ints[
i][
j][k] );
359 std::cout <<
" 3D Weight Matrix initialized! " << std::endl;
380 TFile *
infile =
new TFile(WeightFileName.c_str());
381 TH1F *WHist = (TH1F*)infile->Get(
"WHist");
385 throw cms::Exception(
"HistogramNotFound") <<
" Could not find the histogram WHist in the file "
386 <<
"in the file " << WeightFileName <<
"." << std::endl;
389 for (
int i=0;
i<50;
i++) {
391 for(
int j=0;
j<50;
j++) {
392 for(
int k=0;
k<50;
k++) {
401 std::cout <<
" 3D Weight Matrix initialized! " << std::endl;
410 TFile *infileMC =
new TFile(MCWeightFileName.c_str());
411 TH3D *MHist = (TH3D*)infileMC->Get(
"MHist");
415 throw cms::Exception(
"HistogramNotFound") <<
" Could not find the histogram MHist in the file "
416 <<
"in the file " << MCWeightFileName <<
"." << std::endl;
419 TFile *infileD =
new TFile(DataWeightFileName.c_str());
420 TH3D *DHist = (TH3D*)infileD->Get(
"DHist");
424 throw cms::Exception(
"HistogramNotFound") <<
" Could not find the histogram DHist in the file "
425 <<
"in the file " << DataWeightFileName <<
"." << std::endl;
428 for (
int i=0;
i<50;
i++) {
429 for(
int j=0;
j<50;
j++) {
430 for(
int k=0;
k<50;
k++) {
431 Weight3D_[
i][
j][
k] = DHist->GetBinContent(
i+1,
j+1,
k+1)/MHist->GetBinContent(
i+1,
j+1,
k+1);
436 std::cout <<
" 3D Weight Matrix initialized! " << std::endl;
double Weight3D_[50][50][50]
std::string dataFileName_
std::string DataHistName_
EventID const & min(EventID const &lh, EventID const &rh)
double weight3D(const edm::EventBase &e)
void weight3D_set(std::string generatedFile, std::string dataFile, std::string GenHistName, std::string DataHistName, std::string WeightOutputFile)
void weight3D_init(float Scale)
boost::shared_ptr< TH1 > MC_distr_
std::string generatedFileName_
boost::shared_ptr< TFile > dataFile_
bool getByLabel(InputTag const &, Handle< T > &) const
int factorial(int n)
factorial function
boost::shared_ptr< TFile > generatedFile_
boost::shared_ptr< TH1 > Data_distr_
std::string weightFileName_