CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/PhysicsTools/Utilities/src/LumiReWeighting.cc

Go to the documentation of this file.
00001 #ifndef PhysicsTools_Utilities_interface_LumiReWeighting_cc
00002 #define PhysicsTools_Utilities_interface_LumiReWeighting_cc
00003 
00004 
00020 #include "TRandom1.h"
00021 #include "TRandom2.h"
00022 #include "TRandom3.h"
00023 #include "TStopwatch.h"
00024 #include "TH1.h"
00025 #include "TFile.h"
00026 #include <string>
00027 #include <algorithm>
00028 #include <boost/shared_ptr.hpp>
00029 #include "SimDataFormats/PileupSummaryInfo/interface/PileupSummaryInfo.h" 
00030 #include "PhysicsTools/Utilities/interface/LumiReWeighting.h"
00031 #include "DataFormats/Common/interface/Handle.h"
00032 #include "DataFormats/Provenance/interface/ProcessHistory.h"
00033 #include "DataFormats/Provenance/interface/Provenance.h"
00034 #include "FWCore/Common/interface/EventBase.h"
00035 
00036 using namespace edm;
00037 
00038 LumiReWeighting::LumiReWeighting( std::string generatedFile,
00039                    std::string dataFile,
00040                    std::string GenHistName = "pileup",
00041                    std::string DataHistName = "pileup" ) :
00042       generatedFileName_( generatedFile), 
00043       dataFileName_     ( dataFile ), 
00044       GenHistName_        ( GenHistName ), 
00045       DataHistName_        ( DataHistName )
00046       {
00047         generatedFile_ = boost::shared_ptr<TFile>( new TFile(generatedFileName_.c_str()) ); //MC distribution
00048         dataFile_      = boost::shared_ptr<TFile>( new TFile(dataFileName_.c_str()) );      //Data distribution
00049 
00050         Data_distr_ = boost::shared_ptr<TH1>(  (static_cast<TH1*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
00051         MC_distr_ = boost::shared_ptr<TH1>(  (static_cast<TH1*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
00052 
00053         // MC * data/MC = data, so the weights are data/MC:
00054 
00055         // normalize both histograms first
00056 
00057         Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
00058         MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
00059 
00060         weights_ = boost::shared_ptr<TH1>( static_cast<TH1*>(Data_distr_->Clone()) );
00061 
00062         weights_->SetName("lumiWeights");
00063 
00064         TH1* den = dynamic_cast<TH1*>(MC_distr_->Clone());
00065 
00066         //den->Scale(1.0/ den->Integral());
00067 
00068         weights_->Divide( den );  // so now the average weight should be 1.0
00069 
00070         std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
00071 
00072         int NBins = weights_->GetNbinsX();
00073 
00074         for(int ibin = 1; ibin<NBins+1; ++ibin){
00075           std::cout << "   " << ibin-1 << " " << weights_->GetBinContent(ibin) << std::endl;
00076         }
00077 
00078 
00079         weightOOT_init();
00080 
00081         FirstWarning_ = true;
00082         OldLumiSection_ = -1;
00083 }
00084 
00085 LumiReWeighting::LumiReWeighting( std::vector< float > MC_distr, std::vector< float > Lumi_distr) {
00086   // no histograms for input: use vectors
00087   
00088   // now, make histograms out of them:
00089 
00090   // first, check they are the same size...
00091 
00092   if( MC_distr.size() != Lumi_distr.size() ){   
00093 
00094     std::cerr <<"ERROR: LumiReWeighting: input vectors have different sizes. Quitting... \n";
00095     return;
00096 
00097   }
00098 
00099   Int_t NBins = MC_distr.size();
00100 
00101   MC_distr_ = boost::shared_ptr<TH1> ( new TH1F("MC_distr","MC dist",NBins,-0.5, float(NBins)-0.5) );
00102   Data_distr_ = boost::shared_ptr<TH1> ( new TH1F("Data_distr","Data dist",NBins,-0.5, float(NBins)-0.5) );
00103 
00104   weights_ = boost::shared_ptr<TH1> ( new TH1F("luminumer","luminumer",NBins,-0.5, float(NBins)-0.5) );
00105   TH1* den = new TH1F("lumidenom","lumidenom",NBins,-0.5, float(NBins)-0.5) ;
00106 
00107   for(int ibin = 1; ibin<NBins+1; ++ibin ) {
00108     weights_->SetBinContent(ibin, Lumi_distr[ibin-1]);
00109     Data_distr_->SetBinContent(ibin, Lumi_distr[ibin-1]);
00110     den->SetBinContent(ibin,MC_distr[ibin-1]);
00111     MC_distr_->SetBinContent(ibin,MC_distr[ibin-1]);
00112   }
00113 
00114   // check integrals, make sure things are normalized
00115 
00116   float deltaH = weights_->Integral();
00117   if(fabs(1.0 - deltaH) > 0.02 ) { //*OOPS*...
00118     weights_->Scale( 1.0/ weights_->Integral() );
00119     Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
00120   }
00121   float deltaMC = den->Integral();
00122   if(fabs(1.0 - deltaMC) > 0.02 ) {
00123     den->Scale(1.0/ den->Integral());
00124     MC_distr_->Scale(1.0/ MC_distr_->Integral());
00125   }
00126 
00127   weights_->Divide( den );  // so now the average weight should be 1.0    
00128 
00129   std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
00130 
00131   for(int ibin = 1; ibin<NBins+1; ++ibin){
00132     std::cout << "   " << ibin-1 << " " << weights_->GetBinContent(ibin) << std::endl;
00133   }
00134 
00135   weightOOT_init();
00136 
00137   FirstWarning_ = true;
00138   OldLumiSection_ = -1;
00139 }
00140 
00141 double LumiReWeighting::weight( int npv ) {
00142   int bin = weights_->GetXaxis()->FindBin( npv );
00143   return weights_->GetBinContent( bin );
00144 }
00145 
00146 double LumiReWeighting::weight( float npv ) {
00147   int bin = weights_->GetXaxis()->FindBin( npv );
00148   return weights_->GetBinContent( bin );
00149 }
00150 
00151 double LumiReWeighting::weight3BX( float ave_npv ) {
00152   int bin = weights_->GetXaxis()->FindBin( ave_npv );
00153   return weights_->GetBinContent( bin );
00154 }
00155 
00156 
00157 // This version of weight does all of the work for you, assuming you want to re-weight
00158 // using the true number of interactions in the in-time beam crossing.
00159 
00160 
00161 double LumiReWeighting::weight( const edm::EventBase &e ) {
00162 
00163   // find provenance of event objects, just to check at the job beginning if there might be an issue  
00164 
00165   if(FirstWarning_) {
00166 
00167     edm::ReleaseVersion TargetRelease = edm::ReleaseVersion("\"CMSSW_4_2_2_patch2\"");
00168     edm::ProcessHistory PHist = e.processHistory();
00169     edm::ProcessHistory::const_iterator PHist_iter = PHist.begin();
00170 
00171     for(; PHist_iter<PHist.end() ;++PHist_iter) {
00172       edm::ProcessConfiguration PConf = *(PHist_iter);
00173       edm::ReleaseVersion Release =  PConf.releaseVersion() ;
00174       const std::string Process =  PConf.processName();
00175 
00176       if((Process=="HLT") && (Release==TargetRelease)) {
00177         std::cout << " **** Warning! You are using in-time-only pileup reweighting for Release " << Release << " **** " << std::endl;
00178         std::cout << " **** There is a weightOOT() function available if needed ****  " << std::endl;
00179       }
00180     }
00181     //    SetFirstFalse();
00182     FirstWarning_ = false;
00183   }
00184 
00185   // get pileup summary information
00186 
00187   Handle<std::vector< PileupSummaryInfo > >  PupInfo;
00188   e.getByLabel(edm::InputTag("addPileupInfo"), PupInfo);
00189 
00190   std::vector<PileupSummaryInfo>::const_iterator PVI;
00191 
00192   int npv = -1;
00193   for(PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
00194 
00195     int BX = PVI->getBunchCrossing();
00196 
00197     if(BX == 0) { 
00198       npv = PVI->getPU_NumInteractions();
00199       continue;
00200     }
00201 
00202   }
00203 
00204   if(npv < 0) std::cerr << " no in-time beam crossing found\n! " ;
00205 
00206   int bin = weights_->GetXaxis()->FindBin( npv );
00207 
00208   return weights_->GetBinContent( bin );
00209  
00210 }
00211 
00212 double LumiReWeighting::weight3BX( const edm::EventBase &e ) {
00213 
00214 
00215   // get pileup summary information
00216 
00217   Handle<std::vector< PileupSummaryInfo > >  PupInfo;
00218   e.getByLabel(edm::InputTag("addPileupInfo"), PupInfo);
00219 
00220   std::vector<PileupSummaryInfo>::const_iterator PVI;
00221 
00222   int sum_npv = 0;
00223 
00224   for(PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
00225 
00226     sum_npv += PVI->getPU_NumInteractions();
00227 
00228   }
00229 
00230   float ave_npv = float(sum_npv)/3.;
00231 
00232 
00233   int bin = weights_->GetXaxis()->FindBin( ave_npv );
00234 
00235   return weights_->GetBinContent( bin );
00236  
00237 }
00238 
00239 void LumiReWeighting::weightOOT_init() {
00240 
00241   // The following are poisson distributions with different means, where the maximum
00242   // of the function has been normalized to weight 1.0
00243   // These are used to reweight the out-of-time pileup to match the in-time distribution.
00244   // The total event weight is the product of the in-time weight, the out-of-time weight,
00245   // and a residual correction to fix the distortions caused by the fact that the out-of-time
00246   // distribution is not flat.
00247 
00248   static double weight_24[25] = {
00249     0,
00250     0,
00251     0,
00252     0,
00253     2.46277e-06,
00254     2.95532e-05,
00255     0.000104668,
00256     0.000401431,
00257     0.00130034,
00258     0.00342202,
00259     0.00818132,
00260     0.0175534,
00261     0.035784,
00262     0.0650836,
00263     0.112232,
00264     0.178699,
00265     0.268934,
00266     0.380868,
00267     0.507505,
00268     0.640922,
00269     0.768551,
00270     0.877829,
00271     0.958624,
00272     0.99939,
00273     1
00274   };
00275 
00276   static double weight_23[25] = {
00277     0,
00278     1.20628e-06,
00279     1.20628e-06,
00280     2.41255e-06,
00281     1.20628e-05,
00282     6.39326e-05,
00283     0.000252112,
00284     0.000862487,
00285     0.00244995,
00286     0.00616527,
00287     0.0140821,
00288     0.0293342,
00289     0.0564501,
00290     0.100602,
00291     0.164479,
00292     0.252659,
00293     0.36268,
00294     0.491427,
00295     0.627979,
00296     0.75918,
00297     0.873185,
00298     0.957934,
00299     0.999381,
00300     1,
00301     0.957738
00302   };
00303 
00304   static double weight_22[25] = {
00305     0,
00306     0,
00307     0,
00308     5.88636e-06,
00309     3.0609e-05,
00310     0.000143627,
00311     0.000561558,
00312     0.00173059,
00313     0.00460078,
00314     0.0110616,
00315     0.0238974,
00316     0.0475406,
00317     0.0875077,
00318     0.148682,
00319     0.235752,
00320     0.343591,
00321     0.473146,
00322     0.611897,
00323     0.748345,
00324     0.865978,
00325     0.953199,
00326     0.997848,
00327     1,
00328     0.954245,
00329     0.873688
00330   };
00331 
00332   static double weight_21[25] = {
00333     0,
00334     0,
00335     1.15381e-06,
00336     8.07665e-06,
00337     7.1536e-05,
00338     0.000280375,
00339     0.00107189,
00340     0.00327104,
00341     0.00809396,
00342     0.0190978,
00343     0.0401894,
00344     0.0761028,
00345     0.13472,
00346     0.216315,
00347     0.324649,
00348     0.455125,
00349     0.598241,
00350     0.739215,
00351     0.861866,
00352     0.953911,
00353     0.998918,
00354     1,
00355     0.956683,
00356     0.872272,
00357     0.76399
00358   };
00359  
00360  
00361   static double weight_20[25] = {
00362     0,
00363     0,
00364     1.12532e-06,
00365     2.58822e-05,
00366     0.000145166,
00367     0.000633552,
00368     0.00215048,
00369     0.00592816,
00370     0.0145605,
00371     0.0328367,
00372     0.0652649,
00373     0.11893,
00374     0.19803,
00375     0.305525,
00376     0.436588,
00377     0.581566,
00378     0.727048,
00379     0.8534,
00380     0.949419,
00381     0.999785,
00382     1,
00383     0.953008,
00384     0.865689,
00385     0.753288,
00386     0.62765
00387   }; 
00388   static double weight_19[25] = {
00389     0,
00390     0,
00391     1.20714e-05,
00392     5.92596e-05,
00393     0.000364337,
00394     0.00124994,
00395     0.00403953,
00396     0.0108149,
00397     0.025824,
00398     0.0544969,
00399     0.103567,
00400     0.17936,
00401     0.283532,
00402     0.416091,
00403     0.562078,
00404     0.714714,
00405     0.846523,
00406     0.947875,
00407     1,
00408     0.999448,
00409     0.951404,
00410     0.859717,
00411     0.742319,
00412     0.613601,
00413     0.48552
00414   };
00415 
00416   static double weight_18[25] = {
00417     0,
00418     3.20101e-06,
00419     2.88091e-05,
00420     0.000164319,
00421     0.000719161,
00422     0.00250106,
00423     0.00773685,
00424     0.0197513,
00425     0.0443693,
00426     0.0885998,
00427     0.159891,
00428     0.262607,
00429     0.392327,
00430     0.543125,
00431     0.69924,
00432     0.837474,
00433     0.943486,
00434     0.998029,
00435     1,
00436     0.945937,
00437     0.851807,
00438     0.729309,
00439     0.596332,
00440     0.467818,
00441     0.350434
00442   };
00443 
00444  
00445   static double weight_17[25] = {
00446     1.03634e-06,
00447     7.25437e-06,
00448     4.97443e-05,
00449     0.000340956,
00450     0.00148715,
00451     0.00501485,
00452     0.0143067,
00453     0.034679,
00454     0.0742009,
00455     0.140287,
00456     0.238288,
00457     0.369416,
00458     0.521637,
00459     0.682368,
00460     0.828634,
00461     0.939655,
00462     1,
00463     0.996829,
00464     0.94062,
00465     0.841575,
00466     0.716664,
00467     0.582053,
00468     0.449595,
00469     0.331336,
00470     0.234332
00471   };
00472 
00473  
00474   static double weight_16[25] = {
00475     4.03159e-06,
00476     2.41895e-05,
00477     0.000141106,
00478     0.00081942,
00479     0.00314565,
00480     0.00990662,
00481     0.026293,
00482     0.0603881,
00483     0.120973,
00484     0.214532,
00485     0.343708,
00486     0.501141,
00487     0.665978,
00488     0.820107,
00489     0.938149,
00490     1,
00491     0.99941,
00492     0.940768,
00493     0.837813,
00494     0.703086,
00495     0.564023,
00496     0.42928,
00497     0.312515,
00498     0.216251,
00499     0.14561
00500   };
00501  
00502  
00503   static double weight_15[25] = {
00504     9.76084e-07,
00505     5.07564e-05,
00506     0.000303562,
00507     0.00174036,
00508     0.00617959,
00509     0.0188579,
00510     0.047465,
00511     0.101656,
00512     0.189492,
00513     0.315673,
00514     0.474383,
00515     0.646828,
00516     0.809462,
00517     0.934107,
00518     0.998874,
00519     1,
00520     0.936163,
00521     0.827473,
00522     0.689675,
00523     0.544384,
00524     0.40907,
00525     0.290648,
00526     0.198861,
00527     0.12951,
00528     0.0808051
00529   };
00530  
00531  
00532   static double weight_14[25] = {
00533     1.13288e-05,
00534     0.000124617,
00535     0.000753365,
00536     0.00345056,
00537     0.0123909,
00538     0.0352712,
00539     0.0825463,
00540     0.16413,
00541     0.287213,
00542     0.44615,
00543     0.625826,
00544     0.796365,
00545     0.930624,
00546     0.999958,
00547     1,
00548     0.934414,
00549     0.816456,
00550     0.672939,
00551     0.523033,
00552     0.386068,
00553     0.269824,
00554     0.180342,
00555     0.114669,
00556     0.0698288,
00557     0.0406496
00558   };
00559 
00560  
00561   static double weight_13[25] = {
00562     2.54296e-05,
00563     0.000261561,
00564     0.00167018,
00565     0.00748083,
00566     0.0241308,
00567     0.0636801,
00568     0.138222,
00569     0.255814,
00570     0.414275,
00571     0.600244,
00572     0.779958,
00573     0.92256,
00574     0.999155,
00575     1,
00576     0.927126,
00577     0.804504,
00578     0.651803,
00579     0.497534,
00580     0.35976,
00581     0.245834,
00582     0.160904,
00583     0.0991589,
00584     0.0585434,
00585     0.0332437,
00586     0.0180159
00587   };
00588 
00589   static double weight_12[25] = {
00590     5.85742e-05,
00591     0.000627706,
00592     0.00386677,
00593     0.0154068,
00594     0.0465892,
00595     0.111683,
00596     0.222487,
00597     0.381677,
00598     0.5719,
00599     0.765001,
00600     0.915916,
00601     1,
00602     0.999717,
00603     0.921443,
00604     0.791958,
00605     0.632344,
00606     0.475195,
00607     0.334982,
00608     0.223666,
00609     0.141781,
00610     0.0851538,
00611     0.048433,
00612     0.0263287,
00613     0.0133969,
00614     0.00696683
00615   };
00616 
00617  
00618   static double weight_11[25] = {
00619     0.00015238,
00620     0.00156064,
00621     0.00846044,
00622     0.0310939,
00623     0.0856225,
00624     0.187589,
00625     0.343579,
00626     0.541892,
00627     0.74224,
00628     0.909269,
00629     0.998711,
00630     1,
00631     0.916889,
00632     0.77485,
00633     0.608819,
00634     0.447016,
00635     0.307375,
00636     0.198444,
00637     0.121208,
00638     0.070222,
00639     0.0386492,
00640     0.0201108,
00641     0.0100922,
00642     0.00484937,
00643     0.00222458
00644   };
00645 
00646   static double weight_10[25] = {
00647     0.000393044,
00648     0.00367001,
00649     0.0179474,
00650     0.060389,
00651     0.151477,
00652     0.302077,
00653     0.503113,
00654     0.720373,
00655     0.899568,
00656     1,
00657     0.997739,
00658     0.909409,
00659     0.75728,
00660     0.582031,
00661     0.415322,
00662     0.277663,
00663     0.174147,
00664     0.102154,
00665     0.0566719,
00666     0.0298642,
00667     0.0147751,
00668     0.00710995,
00669     0.00319628,
00670     0.00140601,
00671     0.000568796
00672   };
00673 
00674  
00675   static double weight_9[25] = {
00676     0.00093396,
00677     0.00854448,
00678     0.0380306,
00679     0.113181,
00680     0.256614,
00681     0.460894,
00682     0.690242,
00683     0.888781,
00684     1,
00685     0.998756,
00686     0.899872,
00687     0.735642,
00688     0.552532,
00689     0.382726,
00690     0.246114,
00691     0.147497,
00692     0.0825541,
00693     0.0441199,
00694     0.0218157,
00695     0.0103578,
00696     0.00462959,
00697     0.0019142,
00698     0.000771598,
00699     0.000295893,
00700     0.000111529
00701   };
00702 
00703  
00704   static double weight_8[25] = {
00705     0.00240233,
00706     0.0192688,
00707     0.0768653,
00708     0.205008,
00709     0.410958,
00710     0.65758,
00711     0.875657,
00712     0.999886,
00713     1,
00714     0.889476,
00715     0.711446,
00716     0.517781,
00717     0.345774,
00718     0.212028,
00719     0.121208,
00720     0.0644629,
00721     0.0324928,
00722     0.0152492,
00723     0.00673527,
00724     0.0028547,
00725     0.00117213,
00726     0.000440177,
00727     0.000168471,
00728     5.80689e-05,
00729     1.93563e-05
00730   };
00731 
00732   static double weight_7[25] = {
00733     0.00617233,
00734     0.0428714,
00735     0.150018,
00736     0.350317,
00737     0.612535,
00738     0.856525,
00739     0.999923,
00740     1,
00741     0.87544,
00742     0.679383,
00743     0.478345,
00744     0.303378,
00745     0.176923,
00746     0.0950103,
00747     0.0476253,
00748     0.0222211,
00749     0.00972738,
00750     0.00392962,
00751     0.0015258,
00752     0.000559168,
00753     0.000183928,
00754     6.77983e-05,
00755     1.67818e-05,
00756     7.38398e-06,
00757     6.71271e-07
00758   };
00759  
00760   static double weight_6[25] = {
00761     0.0154465,
00762     0.0923472,
00763     0.277322,
00764     0.55552,
00765     0.833099,
00766     0.999035,
00767     1,
00768     0.855183,
00769     0.641976,
00770     0.428277,
00771     0.256804,
00772     0.139798,
00773     0.0700072,
00774     0.0321586,
00775     0.0137971,
00776     0.00544756,
00777     0.00202316,
00778     0.000766228,
00779     0.000259348,
00780     8.45836e-05,
00781     1.80362e-05,
00782     8.70713e-06,
00783     3.73163e-06,
00784     6.21938e-07,
00785     0
00786   };
00787  
00788  
00789   static double weight_5[25] = {
00790     0.0382845,
00791     0.191122,
00792     0.478782,
00793     0.797314,
00794     1,
00795     0.997148,
00796     0.831144,
00797     0.59461,
00798     0.371293,
00799     0.205903,
00800     0.103102,
00801     0.0471424,
00802     0.0194997,
00803     0.00749415,
00804     0.00273709,
00805     0.000879189,
00806     0.000286049,
00807     0.000102364,
00808     1.70606e-05,
00809     3.98081e-06,
00810     2.27475e-06,
00811     0,
00812     0,
00813     0,
00814     0
00815   };
00816  
00817  
00818   static double weight_4[25] = {
00819     0.0941305,
00820     0.373824,
00821     0.750094,
00822     1,
00823     0.997698,
00824     0.800956,
00825     0.532306,
00826     0.304597,
00827     0.152207,
00828     0.0676275,
00829     0.0270646,
00830     0.00975365,
00831     0.00326077,
00832     0.00101071,
00833     0.000301781,
00834     7.41664e-05,
00835     1.58563e-05,
00836     3.58045e-06,
00837     1.02299e-06,
00838     0,
00839     5.11493e-07,
00840     0,
00841     0,
00842     0,
00843     0
00844   };
00845  
00846  
00847   static double weight_3[25] = {
00848     0.222714,
00849     0.667015,
00850     1,
00851     0.999208,
00852     0.750609,
00853     0.449854,
00854     0.224968,
00855     0.0965185,
00856     0.0361225,
00857     0.012084,
00858     0.00359618,
00859     0.000977166,
00860     0.000239269,
00861     6.29422e-05,
00862     1.16064e-05,
00863     1.78559e-06,
00864     0,
00865     4.46398e-07,
00866     0,
00867     0,
00868     0,
00869     0,
00870     0,
00871     0,
00872     0
00873   };
00874  
00875   static double weight_2[25] = {
00876     0.499541,
00877     0.999607,
00878     1,
00879     0.666607,
00880     0.333301,
00881     0.13279,
00882     0.0441871,
00883     0.0127455,
00884     0.00318434,
00885     0.00071752,
00886     0.000132204,
00887     2.69578e-05,
00888     5.16999e-06,
00889     2.21571e-06,
00890     0,
00891     0,
00892     0,
00893     0,
00894     0,
00895     0,
00896     0,
00897     0,
00898     0,
00899     0,
00900     0
00901   };
00902  
00903   static double weight_1[25] = {
00904     0.999165,
00905     1,
00906     0.499996,
00907     0.166868,
00908     0.0414266,
00909     0.00831053,
00910     0.00137472,
00911     0.000198911,
00912     2.66302e-05,
00913     2.44563e-06,
00914     2.71737e-07,
00915     2.71737e-07,
00916     0,
00917     0,
00918     0,
00919     0,
00920     0,
00921     0,
00922     0,
00923     0,
00924     0,
00925     0,
00926     0,
00927     0,
00928     0
00929   };
00930  
00931   static double weight_0[25] = {
00932     1,
00933     0,
00934     0,
00935     0,
00936     0,
00937     0,
00938     0,
00939     0,
00940     0,
00941     0,
00942     0,
00943     0,
00944     0,
00945     0,
00946     0,
00947     0,
00948     0,
00949     0,
00950     0,
00951     0,
00952     0,
00953     0,
00954     0,
00955     0,
00956     0
00957   };
00958 
00959   //WeightOOTPU_ = {0};
00960 
00961   double* WeightPtr = 0;
00962 
00963   for(int iint = 0; iint<25; ++iint){
00964     if(iint ==0) WeightPtr = weight_0;
00965     if(iint ==1) WeightPtr = weight_1;
00966     if(iint ==2) WeightPtr = weight_2;
00967     if(iint ==3) WeightPtr = weight_3;
00968     if(iint ==4) WeightPtr = weight_4;
00969     if(iint ==5) WeightPtr = weight_5;
00970     if(iint ==6) WeightPtr = weight_6;
00971     if(iint ==7) WeightPtr = weight_7;
00972     if(iint ==8) WeightPtr = weight_8;
00973     if(iint ==9) WeightPtr = weight_9;
00974     if(iint ==10) WeightPtr = weight_10;
00975     if(iint ==11) WeightPtr = weight_11;
00976     if(iint ==12) WeightPtr = weight_12;
00977     if(iint ==13) WeightPtr = weight_13;
00978     if(iint ==14) WeightPtr = weight_14;
00979     if(iint ==15) WeightPtr = weight_15;
00980     if(iint ==16) WeightPtr = weight_16;
00981     if(iint ==17) WeightPtr = weight_17;
00982     if(iint ==18) WeightPtr = weight_18;
00983     if(iint ==19) WeightPtr = weight_19;
00984     if(iint ==20) WeightPtr = weight_20;
00985     if(iint ==21) WeightPtr = weight_21;
00986     if(iint ==22) WeightPtr = weight_22;
00987     if(iint ==23) WeightPtr = weight_23;
00988     if(iint ==24) WeightPtr = weight_24;
00989 
00990     for(int ibin = 0; ibin<25; ++ibin){
00991       WeightOOTPU_[iint][ibin] = *(WeightPtr+ibin);
00992     }
00993   }
00994 
00995 }
00996 
00997 // Use this routine to re-weight out-of-time pileup to match the in-time distribution
00998 // As of May 2011, CMS is only sensitive to a bunch that is 50ns "late", which corresponds to
00999 // BunchCrossing +1.  So, we use that here for re-weighting.
01000 
01001 double LumiReWeighting::weightOOT( const edm::EventBase &e ) {
01002 
01003 
01004   static double Correct_Weights2011[25] = { // residual correction to match lumi spectrum
01005     5.30031,
01006     2.07903,
01007     1.40729,
01008     1.27687,
01009     1.0702,
01010     0.902094,
01011     0.902345,
01012     0.931449,
01013     0.78202,
01014     0.824686,
01015     0.837735,
01016     0.910261,
01017     1.01394,
01018     1.1599,
01019     1.12778,
01020     1.58423,
01021     1.78868,
01022     1.58296,
01023     2.3291,
01024     3.86641,
01025     0,
01026     0,
01027     0,
01028     0,
01029     0
01030   };                        
01031 
01032   //int Run = e.run();
01033   int LumiSection = e.luminosityBlock();
01034   
01035   // do some caching here, attempt to catch file boundaries
01036 
01037   if(LumiSection != OldLumiSection_) {
01038 
01039     Reweight_4_2_2p2_ = false;
01040 
01041     static edm::ReleaseVersion TargetRelease = edm::ReleaseVersion("\"CMSSW_4_2_2_patch2\"");
01042     edm::ProcessHistory PHist = e.processHistory();
01043     edm::ProcessHistory::const_iterator PHist_iter = PHist.begin();
01044 
01045     // check to see if we need to do the special out-of-time poisson weighting for CMSSW_4_2_2_patch2 MC:
01046 
01047     for(; PHist_iter<PHist.end() ;++PHist_iter) {
01048       edm::ProcessConfiguration PConf = *(PHist_iter);
01049       edm::ReleaseVersion Release =  PConf.releaseVersion() ;
01050       const std::string Process =  PConf.processName();
01051 
01052       if((Process=="HLT") && (Release==TargetRelease)) {
01053 
01054         Reweight_4_2_2p2_ = true;
01055 
01056         if(FirstWarning_) {
01057 
01058           std::cout << " **** Warning: Out-of-time pileup reweighting appropriate for Release " << Release << " **** " << std::endl;
01059           std::cout << " **** will be applied  ****  " << std::endl;
01060 
01061           FirstWarning_ = false;
01062         }
01063       }
01064     }
01065     OldLumiSection_ = LumiSection;
01066   }
01067 
01068   // find the pileup summary information
01069 
01070   Handle<std::vector< PileupSummaryInfo > >  PupInfo;
01071   e.getByLabel(edm::InputTag("addPileupInfo"), PupInfo);
01072 
01073   std::vector<PileupSummaryInfo>::const_iterator PVI;
01074 
01075   int npv = -1;
01076   int npv50ns = -1;
01077 
01078   for(PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
01079 
01080     int BX = PVI->getBunchCrossing();
01081 
01082     if(BX == 0) { 
01083       npv = PVI->getPU_NumInteractions();
01084     }
01085 
01086     if(BX == 1) { 
01087       npv50ns = PVI->getPU_NumInteractions();
01088     }
01089 
01090   }
01091 
01092   // Note: for the "uncorrelated" out-of-time pileup, reweighting is only done on the 50ns
01093   // "late" bunch (BX=+1), since that is basically the only one that matters in terms of 
01094   // energy deposition.  
01095 
01096   if(npv < 0) {
01097     std::cerr << " no in-time beam crossing found\n! " ;
01098     std::cerr << " Returning event weight=0\n! ";
01099     return 0.;
01100   }
01101   if(npv50ns < 0) {
01102     std::cerr << " no out-of-time beam crossing found\n! " ;
01103     std::cerr << " Returning event weight=0\n! ";
01104     return 0.;
01105   }
01106 
01107   int bin = weights_->GetXaxis()->FindBin( npv );
01108 
01109   double inTimeWeight = weights_->GetBinContent( bin );
01110 
01111   double TotalWeight = 1.0;
01112 
01113   if(Reweight_4_2_2p2_) {
01114     TotalWeight = inTimeWeight * WeightOOTPU_[bin-1][npv50ns] * Correct_Weights2011[bin-1];
01115   }
01116   else {
01117     TotalWeight = inTimeWeight;
01118   }
01119 
01120   return TotalWeight;
01121  
01122 }
01123 
01124 #endif