1 #ifndef PhysicsTools_Utilities_interface_LumiReWeighting_h 2 #define PhysicsTools_Utilities_interface_LumiReWeighting_h 26 #include "TStopwatch.h" 40 class PoissonMeanShifter {
53 static const double p0_minus[20] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
54 static const double p1_minus[20] = {
76 static const double p2_minus[20] = {
99 static const double p1_expoM[5] = {
107 static const double p2_expoM[5] = {
116 static const double p0_plus[20] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
117 static const double p1_plus[20] = {
139 static const double p2_plus[20] = {
162 static const double p1_expoP[5] = {
170 static const double p2_expoP[5] = {
182 for (
int ibin=0;ibin<20;ibin++) {
185 Pweight_[ibin] = p0_minus[ibin] + p1_minus[ibin]*Shift + p2_minus[ibin]*Shift*Shift;
188 Pweight_[ibin] = p0_plus[ibin] + p1_plus[ibin]*Shift + p2_plus[ibin]*Shift*Shift;
194 for (
int ibin=20;ibin<25;ibin++) {
196 Pweight_[ibin] = p1_expoM[ibin-20]*
exp(p2_expoM[ibin-20]*Shift);
199 Pweight_[ibin] = p1_expoP[ibin-20]*
exp(p2_expoP[ibin-20]*Shift);
207 if(ibin<25 && ibin>=0) {
return Pweight_[ibin]; }
214 int ibin =
int(pvnum);
216 if(ibin<25 && ibin>=0) {
return Pweight_[ibin]; }
237 generatedFileName_( generatedFile),
238 dataFileName_ ( dataFile ),
239 GenHistName_ ( GenHistName ),
240 DataHistName_ ( DataHistName )
242 generatedFile_ =
new TFile( generatedFileName_.c_str() ) ;
243 dataFile_ =
new TFile( dataFileName_.c_str() );
245 Data_distr_ =
new TH1F( *(static_cast<TH1F*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
246 MC_distr_ =
new TH1F( *(static_cast<TH1F*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
250 Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
251 MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
253 weights_ =
new TH1F( *(Data_distr_)) ;
257 weights_->SetName(
"lumiWeights");
259 TH1F* den =
new TH1F(*(MC_distr_));
261 weights_->Divide( den );
263 std::cout <<
" Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
265 int NBins = weights_->GetNbinsX();
267 for(
int ibin = 1; ibin<NBins+1; ++ibin){
268 std::cout <<
" " << ibin-1 <<
" " << weights_->GetBinContent(ibin) << std::endl;
273 FirstWarning_ =
true;
278 LumiReWeighting(
const std::vector< float >& MC_distr,
const std::vector< float >& Lumi_distr){
285 if( MC_distr.size() != Lumi_distr.size() ){
287 std::cerr <<
"ERROR: LumiReWeighting: input vectors have different sizes. Quitting... \n";
292 Int_t NBins = MC_distr.size();
294 MC_distr_ =
new TH1F(
"MC_distr",
"MC dist",NBins,-0.5,
float(NBins)-0.5);
295 Data_distr_ =
new TH1F(
"Data_distr",
"Data dist",NBins,-0.5,
float(NBins)-0.5);
297 weights_ =
new TH1F(
"luminumer",
"luminumer",NBins,-0.5,
float(NBins)-0.5);
298 TH1F* den =
new TH1F(
"lumidenom",
"lumidenom",NBins,-0.5,
float(NBins)-0.5);
300 for(
int ibin = 1; ibin<NBins+1; ++ibin ) {
301 weights_->SetBinContent(ibin, Lumi_distr[ibin-1]);
302 Data_distr_->SetBinContent(ibin, Lumi_distr[ibin-1]);
303 den->SetBinContent(ibin,MC_distr[ibin-1]);
304 MC_distr_->SetBinContent(ibin,MC_distr[ibin-1]);
309 float deltaH = weights_->Integral();
310 if(fabs(1.0 - deltaH) > 0.02 ) {
311 weights_->Scale( 1.0/ deltaH );
312 Data_distr_->Scale( 1.0/ deltaH );
314 float deltaMC = den->Integral();
315 if(fabs(1.0 - deltaMC) > 0.02 ) {
316 den->Scale(1.0/ deltaMC );
317 MC_distr_->Scale(1.0/ deltaMC );
320 weights_->Divide( den );
322 std::cout <<
" Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
324 for(
int ibin = 1; ibin<NBins+1; ++ibin){
325 std::cout <<
" " << ibin-1 <<
" " << weights_->GetBinContent(ibin) << std::endl;
330 FirstWarning_ =
true;
338 TH3D* WHist =
new TH3D(
"WHist",
"3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
339 TH3D* DHist =
new TH3D(
"DHist",
"3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
340 TH3D* MHist =
new TH3D(
"MHist",
"3D weights",50,0.,50.,50,0.,50.,50,0.,50. );
345 if( MC_distr_->GetEntries() == 0 ) {
346 std::cout <<
" MC and Data distributions are not initialized! You must call the LumiReWeighting constructor. " << std::endl;
351 double MC_ints[50][50][50];
352 double Data_ints[50][50][50];
354 for (
int i=0;
i<50;
i++) {
355 for(
int j=0; j<50; j++) {
356 for(
int k=0;
k<50;
k++) {
357 MC_ints[
i][j][
k] = 0.;
358 Data_ints[
i][j][
k] = 0.;
370 for (
int i = 1;
i<50; ++
i) {
378 double probi, probj, probk;
383 int NMCbin = MC_distr_->GetNbinsX();
385 for (
int jbin=1;jbin<NMCbin+1;jbin++) {
386 x = MC_distr_->GetBinCenter(jbin);
387 xweight = MC_distr_->GetBinContent(jbin);
396 std::cout <<
"LumiReweighting:BadInputValue" <<
" Your histogram generates MC luminosity values less than zero!" 397 <<
" Please Check. Terminating." << std::endl;
405 Expval =
exp(-1.*mean);
410 for (
int i = 1;
i<50; ++
i) {
416 for (
int i=0;
i<50;
i++) {
417 probi = PowerSer[
i]/factorial[
i]*Expval;
418 for(
int j=0; j<50; j++) {
419 probj = PowerSer[j]/factorial[j]*Expval;
420 for(
int k=0;
k<50;
k++) {
421 probk = PowerSer[
k]/factorial[
k]*Expval;
423 MC_ints[
i][j][
k] = MC_ints[
i][j][
k]+probi*probj*probk*xweight;
430 int NDatabin = Data_distr_->GetNbinsX();
432 for (
int jbin=1;jbin<NDatabin+1;jbin++) {
433 mean = (Data_distr_->GetBinCenter(jbin))*ScaleFactor;
434 xweight = Data_distr_->GetBinContent(jbin);
438 std::cout <<
"LumiReweighting:BadInputValue" <<
" Your histogram generates MC luminosity values less than zero!" 439 <<
" Please Check. Terminating." << std::endl;
446 Expval =
exp(-1.*mean);
451 for (
int i = 1;
i<50; ++
i) {
458 for (
int i=0;
i<50;
i++) {
459 probi = PowerSer[
i]/factorial[
i]*Expval;
460 for(
int j=0; j<50; j++) {
461 probj = PowerSer[j]/factorial[j]*Expval;
462 for(
int k=0;
k<50;
k++) {
463 probk = PowerSer[
k]/factorial[
k]*Expval;
465 Data_ints[
i][j][
k] = Data_ints[
i][j][
k]+probi*probj*probk*xweight;
473 for (
int i=0;
i<50;
i++) {
475 for(
int j=0; j<50; j++) {
476 for(
int k=0;
k<50;
k++) {
477 if( (MC_ints[
i][j][
k])>0.) {
478 Weight3D_[
i][j][
k] = Data_ints[
i][j][
k]/MC_ints[
i][j][
k];
481 Weight3D_[
i][j][
k] = 0.;
483 WHist->SetBinContent(
i+1,j+1,k+1,Weight3D_[
i][j][k] );
484 DHist->SetBinContent(
i+1,j+1,k+1,Data_ints[
i][j][k] );
485 MHist->SetBinContent(
i+1,j+1,k+1,MC_ints[
i][j][k] );
492 if(! WeightOutputFile.empty() ) {
493 std::cout <<
" 3D Weight Matrix initialized! " << std::endl;
494 std::cout <<
" Writing weights to file " << WeightOutputFile <<
" for re-use... " << std::endl;
497 TFile *
outfile =
new TFile(WeightOutputFile.c_str(),
"RECREATE");
512 TFile *infile =
new TFile(WeightFileName.c_str());
513 TH1F *WHist = (TH1F*)infile->Get(
"WHist");
517 std::cout <<
" Could not find the histogram WHist in the file " 518 <<
"in the file " << WeightFileName <<
"." << std::endl;
522 for (
int i=0;
i<50;
i++) {
523 for(
int j=0; j<50; j++) {
524 for(
int k=0;
k<50;
k++) {
525 Weight3D_[
i][j][
k] = WHist->GetBinContent(
i,j,
k);
530 std::cout <<
" 3D Weight Matrix initialized! " << std::endl;
548 static const double weight_24[25] = {
576 static const double weight_23[25] = {
604 static const double weight_22[25] = {
632 static const double weight_21[25] = {
661 static const double weight_20[25] = {
688 static const double weight_19[25] = {
716 static const double weight_18[25] = {
745 static const double weight_17[25] = {
774 static const double weight_16[25] = {
803 static const double weight_15[25] = {
832 static const double weight_14[25] = {
861 static const double weight_13[25] = {
889 static const double weight_12[25] = {
918 static const double weight_11[25] = {
946 static const double weight_10[25] = {
975 static const double weight_9[25] = {
1004 static const double weight_8[25] = {
1032 static const double weight_7[25] = {
1060 static const double weight_6[25] = {
1089 static const double weight_5[25] = {
1118 static const double weight_4[25] = {
1147 static const double weight_3[25] = {
1175 static const double weight_2[25] = {
1203 static const double weight_1[25] = {
1231 static const double weight_0[25] = {
1261 const double* WeightPtr = 0;
1264 if(
iint ==0) WeightPtr = weight_0;
1265 if(
iint ==1) WeightPtr = weight_1;
1266 if(
iint ==2) WeightPtr = weight_2;
1267 if(
iint ==3) WeightPtr = weight_3;
1268 if(
iint ==4) WeightPtr = weight_4;
1269 if(
iint ==5) WeightPtr = weight_5;
1270 if(
iint ==6) WeightPtr = weight_6;
1271 if(
iint ==7) WeightPtr = weight_7;
1272 if(
iint ==8) WeightPtr = weight_8;
1273 if(
iint ==9) WeightPtr = weight_9;
1274 if(
iint ==10) WeightPtr = weight_10;
1275 if(
iint ==11) WeightPtr = weight_11;
1276 if(
iint ==12) WeightPtr = weight_12;
1277 if(
iint ==13) WeightPtr = weight_13;
1278 if(
iint ==14) WeightPtr = weight_14;
1279 if(
iint ==15) WeightPtr = weight_15;
1280 if(
iint ==16) WeightPtr = weight_16;
1281 if(
iint ==17) WeightPtr = weight_17;
1282 if(
iint ==18) WeightPtr = weight_18;
1283 if(
iint ==19) WeightPtr = weight_19;
1284 if(
iint ==20) WeightPtr = weight_20;
1285 if(
iint ==21) WeightPtr = weight_21;
1286 if(
iint ==22) WeightPtr = weight_22;
1287 if(
iint ==23) WeightPtr = weight_23;
1288 if(
iint ==24) WeightPtr = weight_24;
1290 for(
int ibin = 0; ibin<25; ++ibin){
1291 WeightOOTPU_[
iint][ibin] = *(WeightPtr+ibin);
1299 int bin = weights_->GetXaxis()->FindBin( npv );
1300 return weights_->GetBinContent( bin );
1304 int bin = weights_->GetXaxis()->FindBin( ave_int );
1305 return weights_->GetBinContent( bin );
1309 int bin = weights_->GetXaxis()->FindBin( n_int );
1310 return weights_->GetBinContent( bin );
1318 int npm1 =
min(pv1,34);
1319 int np0 =
min(pv2,34);
1320 int npp1 =
min(pv3,34);
1322 return Weight3D_[npm1][np0][npp1];
1330 static const double Correct_Weights2011[25] = {
1361 std::cout <<
" **** Warning: Out-of-time pileup reweighting appropriate only for PU_S3 **** " << std::endl;
1362 std::cout <<
" **** will be applied **** " << std::endl;
1364 FirstWarning_ =
false;
1373 if(npv_in_time < 0) {
1374 std::cerr <<
" no in-time beam crossing found\n! " ;
1375 std::cerr <<
" Returning event weight=0\n! ";
1378 if(npv_m50nsBX < 0) {
1379 std::cerr <<
" no out-of-time beam crossing found\n! " ;
1380 std::cerr <<
" Returning event weight=0\n! ";
1384 int bin = weights_->GetXaxis()->FindBin( npv_in_time );
1386 double inTimeWeight = weights_->GetBinContent( bin );
1388 double TotalWeight = 1.0;
1391 TotalWeight = inTimeWeight * WeightOOTPU_[bin-1][npv_m50nsBX] * Correct_Weights2011[bin-1];
1412 double WeightOOTPU_[25][25];
1413 double Weight3D_[50][50][50];
LumiReWeighting(std::string generatedFile, std::string dataFile, std::string GenHistName, std::string DataHistName)
void weight3D_init(float ScaleFactor, std::string WeightOutputFile="")
double weight(float n_int)
T x() const
Cartesian x coordinate.
double ShiftWeight(int ibin)
void weight3D_set(std::string WeightFileName)
LumiReWeighting(const std::vector< float > &MC_distr, const std::vector< float > &Lumi_distr)
std::string DataHistName_
double ShiftWeight(float pvnum)
base
Make Sure CMSSW is Setup ##.
double weightOOT(int npv_in_time, int npv_m50nsBX)
bin
set the eta bin as selection string.
std::string dataFileName_
double ITweight3BX(float ave_int)
double weight3D(int pv1, int pv2, int pv3)
PoissonMeanShifter(float Shift)
int factorial(int n)
factorial function
std::string generatedFileName_