CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/PhysicsTools/Utilities/interface/LumiReweightingStandAlone.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_Utilities_interface_LumiReWeighting_h
00002 #define PhysicsTools_Utilities_interface_LumiReWeighting_h
00003 
00004 
00020 #include "TH1.h"
00021 #include "TH3.h"
00022 #include "TFile.h"
00023 #include "TRandom1.h"
00024 #include "TRandom2.h"
00025 #include "TRandom3.h"
00026 #include "TStopwatch.h"
00027 #include <string>
00028 #include <vector>
00029 #include <cmath>
00030 #include <algorithm>
00031 
00032 namespace reweight {
00033 
00034 
00035   // add a class to shift the mean of a poisson-like luminosity distribution by an arbitrary amount. 
00036   // Only valid for small (<1.5) shifts about the 2011 lumi distribution for now, because shifts are non-linear
00037   // Each PoissonMeanShifter does one shift, so defining multiples can give you an arbitrary collection
00038 
00039   class PoissonMeanShifter {
00040 
00041   public:
00042 
00043     PoissonMeanShifter() { };
00044 
00045     PoissonMeanShifter( float Shift ){
00046 
00047       // these are the polynomial or exponential coefficients for each bin of a 25-bin sequence that
00048       // convert the Distribution of the 2011 luminosity to something with a lower or higher peak luminosity.
00049       // The distributions aren't quite poisson because they model luminosity decreasing during a fill. This implies that
00050       // they do get wider as the mean increases, so the weights are not linear with increasing mean.
00051 
00052       static double p0_minus[20] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
00053       static double p1_minus[20] = {
00054         -0.677786,
00055         -0.619614,
00056         -0.49465,
00057         -0.357963,
00058         -0.238359,
00059         -0.110002,
00060         0.0348629,
00061         0.191263,
00062         0.347648,
00063         0.516615,
00064         0.679646,
00065         0.836673,
00066         0.97764,
00067         1.135,
00068         1.29922,
00069         1.42467,
00070         1.55901,
00071         1.61762,
00072         1.67275,
00073         1.96008
00074       };
00075       static double p2_minus[20] = {
00076         0.526164,
00077         0.251816,
00078         0.11049,
00079         0.026917,
00080         -0.0464692,
00081         -0.087022,
00082         -0.0931581,
00083         -0.0714295,
00084         -0.0331772,
00085         0.0347473,
00086         0.108658,
00087         0.193048,
00088         0.272314,
00089         0.376357,
00090         0.4964,
00091         0.58854,
00092         0.684959,
00093         0.731063,
00094         0.760044,
00095         1.02386
00096       };
00097 
00098       static double p1_expoM[5] = {
00099         1.63363e-03,
00100         6.79290e-04,
00101         3.69900e-04,
00102         2.24349e-04,
00103         9.87156e-06
00104       };
00105 
00106       static double p2_expoM[5] = {
00107         2.64692,
00108         3.26585,
00109         3.53229,
00110         4.18035,
00111         5.64027
00112       };
00113 
00114 
00115       static double p0_plus[20] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
00116       static double p1_plus[20] = {
00117         -0.739059,
00118         -0.594445,
00119         -0.477276,
00120         -0.359707,
00121         -0.233573,
00122         -0.103458,
00123         0.0373401,
00124         0.176571,
00125         0.337617,
00126         0.499074,
00127         0.675126,
00128         0.840522,
00129         1.00917,
00130         1.15847,
00131         1.23816,
00132         1.44271,
00133         1.52982,
00134         1.46385,
00135         1.5802,
00136         0.988689
00137       };
00138       static double p2_plus[20] = {
00139         0.208068,
00140         0.130033,
00141         0.0850356,
00142         0.0448344,
00143         0.000749832,
00144         -0.0331347,
00145         -0.0653281,
00146         -0.0746009,
00147         -0.0800667,
00148         -0.0527636,
00149         -0.00402649,
00150         0.103338,
00151         0.261261,
00152         0.491084,
00153         0.857966,
00154         1.19495,
00155         1.75071,
00156         2.65559,
00157         3.35433,
00158         5.48835
00159       };
00160 
00161       static double p1_expoP[5] = {
00162         1.42463e-01,
00163         4.18966e-02,
00164         1.12697e-01,
00165         1.66197e-01,
00166         1.50768e-01
00167       };
00168 
00169       static double p2_expoP[5] = {
00170         1.98758,
00171         2.27217,
00172         2.26799,
00173         2.38455,
00174         2.52428
00175       };
00176 
00177       // initialize weights based on desired Shift
00178 
00179 
00180 
00181       for (int ibin=0;ibin<20;ibin++) {
00182 
00183         if( Shift < .0) {
00184           Pweight_[ibin] = p0_minus[ibin] + p1_minus[ibin]*Shift + p2_minus[ibin]*Shift*Shift;
00185         }
00186         else {
00187           Pweight_[ibin] = p0_plus[ibin] + p1_plus[ibin]*Shift + p2_plus[ibin]*Shift*Shift;
00188         }
00189       }
00190 
00191       // last few bins fit better to an exponential...
00192 
00193       for (int ibin=20;ibin<25;ibin++) {
00194         if( Shift < 0.) {
00195           Pweight_[ibin] = p1_expoM[ibin-20]*exp(p2_expoM[ibin-20]*Shift);
00196         }
00197         else {
00198           Pweight_[ibin] = p1_expoP[ibin-20]*exp(p2_expoP[ibin-20]*Shift);
00199         }
00200       } 
00201 
00202     };
00203 
00204     double ShiftWeight( int ibin ) {
00205 
00206       if(ibin<25 && ibin>=0) { return Pweight_[ibin]; }
00207       else { return 0;}
00208 
00209     };
00210 
00211     double ShiftWeight( float pvnum ) {
00212 
00213       int ibin = int(pvnum);
00214 
00215       if(ibin<25 && ibin>=0) { return Pweight_[ibin]; }
00216       else { return 0;}
00217 
00218     };
00219 
00220   private:
00221 
00222     double Pweight_[25];
00223 
00224   };
00225 
00226 
00227   class LumiReWeighting {
00228   public:
00229 
00230     LumiReWeighting ( ) { } ;
00231 
00232     LumiReWeighting( std::string generatedFile,
00233                      std::string dataFile,
00234                      std::string GenHistName,
00235                      std::string DataHistName) :
00236       generatedFileName_( generatedFile), 
00237       dataFileName_     ( dataFile ), 
00238       GenHistName_      ( GenHistName ), 
00239       DataHistName_     ( DataHistName )
00240         {
00241           generatedFile_ = new TFile( generatedFileName_.c_str() ) ; //MC distribution
00242           dataFile_      = new TFile( dataFileName_.c_str() );       //Data distribution
00243 
00244           Data_distr_ = new TH1(  *(static_cast<TH1*>(dataFile_->Get( DataHistName_.c_str() )->Clone() )) );
00245           MC_distr_ = new TH1(  *(static_cast<TH1*>(generatedFile_->Get( GenHistName_.c_str() )->Clone() )) );
00246 
00247           // normalize both histograms first                                                                            
00248 
00249           Data_distr_->Scale( 1.0/ Data_distr_->Integral() );
00250           MC_distr_->Scale( 1.0/ MC_distr_->Integral() );
00251 
00252           weights_ = new TH1( *(Data_distr_)) ;
00253 
00254           // MC * data/MC = data, so the weights are data/MC:
00255 
00256           weights_->SetName("lumiWeights");
00257 
00258           TH1* den = new TH1(*(MC_distr_));
00259 
00260           weights_->Divide( den );  // so now the average weight should be 1.0
00261 
00262           std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
00263 
00264           int NBins = weights_->GetNbinsX();
00265 
00266           for(int ibin = 1; ibin<NBins+1; ++ibin){
00267             std::cout << "   " << ibin-1 << " " << weights_->GetBinContent(ibin) << std::endl;
00268           }
00269 
00270           weightOOT_init();
00271 
00272           FirstWarning_ = true;
00273 
00274         }
00275 
00276     
00277       LumiReWeighting( std::vector< float > MC_distr, std::vector< float > Lumi_distr){
00278         // no histograms for input: use vectors
00279   
00280         // now, make histograms out of them:
00281 
00282         // first, check they are the same size...
00283 
00284         if( MC_distr.size() != Lumi_distr.size() ){   
00285 
00286           std::cerr <<"ERROR: LumiReWeighting: input vectors have different sizes. Quitting... \n";
00287           return;
00288 
00289         }
00290 
00291         Int_t NBins = MC_distr.size();
00292 
00293         MC_distr_ = new TH1F("MC_distr","MC dist",NBins,-0.5, float(NBins)-0.5);
00294         Data_distr_ = new TH1F("Data_distr","Data dist",NBins,-0.5, float(NBins)-0.5);
00295 
00296         weights_ = new TH1F("luminumer","luminumer",NBins,-0.5, float(NBins)-0.5);
00297         TH1* den = new TH1F("lumidenom","lumidenom",NBins,-0.5, float(NBins)-0.5);
00298 
00299         for(int ibin = 1; ibin<NBins+1; ++ibin ) {
00300           weights_->SetBinContent(ibin, Lumi_distr[ibin-1]);
00301           Data_distr_->SetBinContent(ibin, Lumi_distr[ibin-1]);
00302           den->SetBinContent(ibin,MC_distr[ibin-1]);
00303           MC_distr_->SetBinContent(ibin,MC_distr[ibin-1]);
00304         }
00305 
00306         // check integrals, make sure things are normalized
00307 
00308         float deltaH = weights_->Integral();
00309         if(fabs(1.0 - deltaH) > 0.02 ) { //*OOPS*...
00310           weights_->Scale( 1.0/ deltaH );
00311           Data_distr_->Scale( 1.0/ deltaH );
00312         }
00313         float deltaMC = den->Integral();
00314         if(fabs(1.0 - deltaMC) > 0.02 ) {
00315           den->Scale(1.0/ deltaMC );
00316           MC_distr_->Scale(1.0/ deltaMC );
00317         }
00318 
00319         weights_->Divide( den );  // so now the average weight should be 1.0    
00320 
00321         std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
00322 
00323         for(int ibin = 1; ibin<NBins+1; ++ibin){
00324           std::cout << "   " << ibin-1 << " " << weights_->GetBinContent(ibin) << std::endl;
00325         }
00326 
00327         weightOOT_init();
00328 
00329         FirstWarning_ = true;
00330 
00331       }
00332 
00333       void weight3D_init() { 
00334 
00335         //create histogram to write output weights, save pain of generating them again...
00336 
00337         TH3D* WHist = new TH3D("WHist","3D weights",35,-.5,34.5,35,-.5,34.5,35,-.5,34.5 );
00338 
00339 
00340         using std::min;
00341 
00342         int Npoints = 100000000;
00343 
00344         if( MC_distr_->GetEntries() == 0 ) {
00345           std::cout << " MC and Data distributions are not initialized! You must call the LumiReWeighting constructor. " << std::endl;
00346         }
00347 
00348         std::cout << " Generating 100M 3-D Weights.  This will take a while..." << std::endl;
00349 
00350         // arrays for storing number of interactions
00351 
00352         double MC_ints[35][35][35];
00353         double Data_ints[35][35][35];
00354 
00355         for (int i=0; i<35; i++) {
00356           for(int j=0; j<35; j++) {
00357             for(int k=0; k<35; k++) {
00358               MC_ints[i][j][k] = 0.;
00359               Data_ints[i][j][k] = 0.;
00360             }
00361           }
00362         }
00363 
00364         // initialize random numbers
00365 
00366         TRandom *r1 = new TRandom1();
00367 
00368         double x,y;
00369         double xint, yint;
00370         int xi;
00371 
00372         // Get entries randomly for Data, MC, fill arrays:
00373 
00374         for (int j=0;j<Npoints;j++) {       
00375 
00376           if(j%1000000==0) std::cout << " ." << std::endl;
00377 
00378           x =  MC_distr_->GetRandom();
00379 
00380           //for Summer 11, we have this int feature that generates the spike at zero int.
00381           xi = int(x);
00382           xint = r1->Poisson(xi);
00383 
00384           int x0 = min(int(xint),34);
00385           xint = r1->Poisson(xi);
00386           int x1 = min(int(xint),34);
00387           xint = r1->Poisson(xi);
00388           int x2 = min(int(xint),34);
00389      
00390           // cout << x0 << "  " << x1 << " " << x2 << endl;
00391 
00392           MC_ints[x0][x1][x2] =  MC_ints[x0][x1][x2]+1.0;
00393 
00394           y =  Data_distr_->GetRandom();
00395 
00396           yint = r1->Poisson(y);
00397 
00398           int y0 = min(int(yint),34);
00399           yint = r1->Poisson(y);
00400           int y1 = min(int(yint),34);
00401           yint = r1->Poisson(y);
00402           int y2 = min(int(yint),34);
00403 
00404           Data_ints[y0][y1][y2] = Data_ints[y0][y1][y2]+1.0;
00405         }
00406 
00407         for (int i=0; i<35; i++) {  
00408           for(int j=0; j<35; j++) {
00409             for(int k=0; k<35; k++) {
00410               if( (MC_ints[i][j][k])>0.) {
00411                 Weight3D_[i][j][k]  =  Data_ints[i][j][k]/MC_ints[i][j][k];
00412               }
00413               else {
00414                 Weight3D_[i][j][k]  = 0.;
00415               }
00416               WHist->SetBinContent( i,j,k,Weight3D_[i][j][k] );
00417             }
00418           }
00419         }
00420 
00421         std::cout << " 3D Weight Matrix initialized! " << std::endl;
00422         std::cout << " Writing weights to file Weight3D.root for re-use...  " << std::endl;
00423 
00424         TFile * outfile = new TFile("Weight3D.root","RECREATE");
00425         WHist->Write();
00426         outfile->Write();
00427         outfile->Close();
00428         outfile->Delete();              
00429 
00430 
00431         return;
00432 
00433 
00434       }
00435 
00436       void weight3D_init( std::string WeightFileName ) { 
00437 
00438         TFile *infile = new TFile(WeightFileName.c_str());
00439         TH1F *WHist = (TH1F*)infile->Get("WHist");
00440 
00441         // Check if the histogram exists           
00442         if (!WHist) {
00443           std::cout << " Could not find the histogram WHist in the file "
00444                                                     << "in the file " << WeightFileName << "." << std::endl;
00445           return;
00446         }
00447 
00448         for (int i=0; i<35; i++) {  
00449           for(int j=0; j<35; j++) {
00450             for(int k=0; k<35; k++) {
00451               Weight3D_[i][j][k] = WHist->GetBinContent(i,j,k);
00452             }
00453           }
00454         }
00455 
00456         std::cout << " 3D Weight Matrix initialized! " << std::endl;
00457 
00458         return;
00459 
00460 
00461       }
00462 
00463 
00464 
00465       void weightOOT_init() {
00466 
00467         // The following are poisson distributions with different means, where the maximum
00468         // of the function has been normalized to weight 1.0
00469         // These are used to reweight the out-of-time pileup to match the in-time distribution.
00470         // The total event weight is the product of the in-time weight, the out-of-time weight,
00471         // and a residual correction to fix the distortions caused by the fact that the out-of-time
00472         // distribution is not flat.
00473 
00474         static double weight_24[25] = {
00475           0,
00476           0,
00477           0,
00478           0,
00479           2.46277e-06,
00480           2.95532e-05,
00481           0.000104668,
00482           0.000401431,
00483           0.00130034,
00484           0.00342202,
00485           0.00818132,
00486           0.0175534,
00487           0.035784,
00488           0.0650836,
00489           0.112232,
00490           0.178699,
00491           0.268934,
00492           0.380868,
00493           0.507505,
00494           0.640922,
00495           0.768551,
00496           0.877829,
00497           0.958624,
00498           0.99939,
00499           1
00500         };
00501 
00502         static double weight_23[25] = {
00503           0,
00504           1.20628e-06,
00505           1.20628e-06,
00506           2.41255e-06,
00507           1.20628e-05,
00508           6.39326e-05,
00509           0.000252112,
00510           0.000862487,
00511           0.00244995,
00512           0.00616527,
00513           0.0140821,
00514           0.0293342,
00515           0.0564501,
00516           0.100602,
00517           0.164479,
00518           0.252659,
00519           0.36268,
00520           0.491427,
00521           0.627979,
00522           0.75918,
00523           0.873185,
00524           0.957934,
00525           0.999381,
00526           1,
00527           0.957738
00528         };
00529 
00530         static double weight_22[25] = {
00531           0,
00532           0,
00533           0,
00534           5.88636e-06,
00535           3.0609e-05,
00536           0.000143627,
00537           0.000561558,
00538           0.00173059,
00539           0.00460078,
00540           0.0110616,
00541           0.0238974,
00542           0.0475406,
00543           0.0875077,
00544           0.148682,
00545           0.235752,
00546           0.343591,
00547           0.473146,
00548           0.611897,
00549           0.748345,
00550           0.865978,
00551           0.953199,
00552           0.997848,
00553           1,
00554           0.954245,
00555           0.873688
00556         };
00557 
00558         static double weight_21[25] = {
00559           0,
00560           0,
00561           1.15381e-06,
00562           8.07665e-06,
00563           7.1536e-05,
00564           0.000280375,
00565           0.00107189,
00566           0.00327104,
00567           0.00809396,
00568           0.0190978,
00569           0.0401894,
00570           0.0761028,
00571           0.13472,
00572           0.216315,
00573           0.324649,
00574           0.455125,
00575           0.598241,
00576           0.739215,
00577           0.861866,
00578           0.953911,
00579           0.998918,
00580           1,
00581           0.956683,
00582           0.872272,
00583           0.76399
00584         };
00585  
00586  
00587         static double weight_20[25] = {
00588           0,
00589           0,
00590           1.12532e-06,
00591           2.58822e-05,
00592           0.000145166,
00593           0.000633552,
00594           0.00215048,
00595           0.00592816,
00596           0.0145605,
00597           0.0328367,
00598           0.0652649,
00599           0.11893,
00600           0.19803,
00601           0.305525,
00602           0.436588,
00603           0.581566,
00604           0.727048,
00605           0.8534,
00606           0.949419,
00607           0.999785,
00608           1,
00609           0.953008,
00610           0.865689,
00611           0.753288,
00612           0.62765
00613         }; 
00614         static double weight_19[25] = {
00615           0,
00616           0,
00617           1.20714e-05,
00618           5.92596e-05,
00619           0.000364337,
00620           0.00124994,
00621           0.00403953,
00622           0.0108149,
00623           0.025824,
00624           0.0544969,
00625           0.103567,
00626           0.17936,
00627           0.283532,
00628           0.416091,
00629           0.562078,
00630           0.714714,
00631           0.846523,
00632           0.947875,
00633           1,
00634           0.999448,
00635           0.951404,
00636           0.859717,
00637           0.742319,
00638           0.613601,
00639           0.48552
00640         };
00641 
00642         static double weight_18[25] = {
00643           0,
00644           3.20101e-06,
00645           2.88091e-05,
00646           0.000164319,
00647           0.000719161,
00648           0.00250106,
00649           0.00773685,
00650           0.0197513,
00651           0.0443693,
00652           0.0885998,
00653           0.159891,
00654           0.262607,
00655           0.392327,
00656           0.543125,
00657           0.69924,
00658           0.837474,
00659           0.943486,
00660           0.998029,
00661           1,
00662           0.945937,
00663           0.851807,
00664           0.729309,
00665           0.596332,
00666           0.467818,
00667           0.350434
00668         };
00669 
00670  
00671         static double weight_17[25] = {
00672           1.03634e-06,
00673           7.25437e-06,
00674           4.97443e-05,
00675           0.000340956,
00676           0.00148715,
00677           0.00501485,
00678           0.0143067,
00679           0.034679,
00680           0.0742009,
00681           0.140287,
00682           0.238288,
00683           0.369416,
00684           0.521637,
00685           0.682368,
00686           0.828634,
00687           0.939655,
00688           1,
00689           0.996829,
00690           0.94062,
00691           0.841575,
00692           0.716664,
00693           0.582053,
00694           0.449595,
00695           0.331336,
00696           0.234332
00697         };
00698 
00699  
00700         static double weight_16[25] = {
00701           4.03159e-06,
00702           2.41895e-05,
00703           0.000141106,
00704           0.00081942,
00705           0.00314565,
00706           0.00990662,
00707           0.026293,
00708           0.0603881,
00709           0.120973,
00710           0.214532,
00711           0.343708,
00712           0.501141,
00713           0.665978,
00714           0.820107,
00715           0.938149,
00716           1,
00717           0.99941,
00718           0.940768,
00719           0.837813,
00720           0.703086,
00721           0.564023,
00722           0.42928,
00723           0.312515,
00724           0.216251,
00725           0.14561
00726         };
00727  
00728  
00729         static double weight_15[25] = {
00730           9.76084e-07,
00731           5.07564e-05,
00732           0.000303562,
00733           0.00174036,
00734           0.00617959,
00735           0.0188579,
00736           0.047465,
00737           0.101656,
00738           0.189492,
00739           0.315673,
00740           0.474383,
00741           0.646828,
00742           0.809462,
00743           0.934107,
00744           0.998874,
00745           1,
00746           0.936163,
00747           0.827473,
00748           0.689675,
00749           0.544384,
00750           0.40907,
00751           0.290648,
00752           0.198861,
00753           0.12951,
00754           0.0808051
00755         };
00756  
00757  
00758         static double weight_14[25] = {
00759           1.13288e-05,
00760           0.000124617,
00761           0.000753365,
00762           0.00345056,
00763           0.0123909,
00764           0.0352712,
00765           0.0825463,
00766           0.16413,
00767           0.287213,
00768           0.44615,
00769           0.625826,
00770           0.796365,
00771           0.930624,
00772           0.999958,
00773           1,
00774           0.934414,
00775           0.816456,
00776           0.672939,
00777           0.523033,
00778           0.386068,
00779           0.269824,
00780           0.180342,
00781           0.114669,
00782           0.0698288,
00783           0.0406496
00784         };
00785 
00786  
00787         static double weight_13[25] = {
00788           2.54296e-05,
00789           0.000261561,
00790           0.00167018,
00791           0.00748083,
00792           0.0241308,
00793           0.0636801,
00794           0.138222,
00795           0.255814,
00796           0.414275,
00797           0.600244,
00798           0.779958,
00799           0.92256,
00800           0.999155,
00801           1,
00802           0.927126,
00803           0.804504,
00804           0.651803,
00805           0.497534,
00806           0.35976,
00807           0.245834,
00808           0.160904,
00809           0.0991589,
00810           0.0585434,
00811           0.0332437,
00812           0.0180159
00813         };
00814 
00815         static double weight_12[25] = {
00816           5.85742e-05,
00817           0.000627706,
00818           0.00386677,
00819           0.0154068,
00820           0.0465892,
00821           0.111683,
00822           0.222487,
00823           0.381677,
00824           0.5719,
00825           0.765001,
00826           0.915916,
00827           1,
00828           0.999717,
00829           0.921443,
00830           0.791958,
00831           0.632344,
00832           0.475195,
00833           0.334982,
00834           0.223666,
00835           0.141781,
00836           0.0851538,
00837           0.048433,
00838           0.0263287,
00839           0.0133969,
00840           0.00696683
00841         };
00842 
00843  
00844         static double weight_11[25] = {
00845           0.00015238,
00846           0.00156064,
00847           0.00846044,
00848           0.0310939,
00849           0.0856225,
00850           0.187589,
00851           0.343579,
00852           0.541892,
00853           0.74224,
00854           0.909269,
00855           0.998711,
00856           1,
00857           0.916889,
00858           0.77485,
00859           0.608819,
00860           0.447016,
00861           0.307375,
00862           0.198444,
00863           0.121208,
00864           0.070222,
00865           0.0386492,
00866           0.0201108,
00867           0.0100922,
00868           0.00484937,
00869           0.00222458
00870         };
00871 
00872         static double weight_10[25] = {
00873           0.000393044,
00874           0.00367001,
00875           0.0179474,
00876           0.060389,
00877           0.151477,
00878           0.302077,
00879           0.503113,
00880           0.720373,
00881           0.899568,
00882           1,
00883           0.997739,
00884           0.909409,
00885           0.75728,
00886           0.582031,
00887           0.415322,
00888           0.277663,
00889           0.174147,
00890           0.102154,
00891           0.0566719,
00892           0.0298642,
00893           0.0147751,
00894           0.00710995,
00895           0.00319628,
00896           0.00140601,
00897           0.000568796
00898         };
00899 
00900  
00901         static double weight_9[25] = {
00902           0.00093396,
00903           0.00854448,
00904           0.0380306,
00905           0.113181,
00906           0.256614,
00907           0.460894,
00908           0.690242,
00909           0.888781,
00910           1,
00911           0.998756,
00912           0.899872,
00913           0.735642,
00914           0.552532,
00915           0.382726,
00916           0.246114,
00917           0.147497,
00918           0.0825541,
00919           0.0441199,
00920           0.0218157,
00921           0.0103578,
00922           0.00462959,
00923           0.0019142,
00924           0.000771598,
00925           0.000295893,
00926           0.000111529
00927         };
00928 
00929  
00930         static double weight_8[25] = {
00931           0.00240233,
00932           0.0192688,
00933           0.0768653,
00934           0.205008,
00935           0.410958,
00936           0.65758,
00937           0.875657,
00938           0.999886,
00939           1,
00940           0.889476,
00941           0.711446,
00942           0.517781,
00943           0.345774,
00944           0.212028,
00945           0.121208,
00946           0.0644629,
00947           0.0324928,
00948           0.0152492,
00949           0.00673527,
00950           0.0028547,
00951           0.00117213,
00952           0.000440177,
00953           0.000168471,
00954           5.80689e-05,
00955           1.93563e-05
00956         };
00957 
00958         static double weight_7[25] = {
00959           0.00617233,
00960           0.0428714,
00961           0.150018,
00962           0.350317,
00963           0.612535,
00964           0.856525,
00965           0.999923,
00966           1,
00967           0.87544,
00968           0.679383,
00969           0.478345,
00970           0.303378,
00971           0.176923,
00972           0.0950103,
00973           0.0476253,
00974           0.0222211,
00975           0.00972738,
00976           0.00392962,
00977           0.0015258,
00978           0.000559168,
00979           0.000183928,
00980           6.77983e-05,
00981           1.67818e-05,
00982           7.38398e-06,
00983           6.71271e-07
00984         };
00985  
00986         static double weight_6[25] = {
00987           0.0154465,
00988           0.0923472,
00989           0.277322,
00990           0.55552,
00991           0.833099,
00992           0.999035,
00993           1,
00994           0.855183,
00995           0.641976,
00996           0.428277,
00997           0.256804,
00998           0.139798,
00999           0.0700072,
01000           0.0321586,
01001           0.0137971,
01002           0.00544756,
01003           0.00202316,
01004           0.000766228,
01005           0.000259348,
01006           8.45836e-05,
01007           1.80362e-05,
01008           8.70713e-06,
01009           3.73163e-06,
01010           6.21938e-07,
01011           0
01012         };
01013  
01014  
01015         static double weight_5[25] = {
01016           0.0382845,
01017           0.191122,
01018           0.478782,
01019           0.797314,
01020           1,
01021           0.997148,
01022           0.831144,
01023           0.59461,
01024           0.371293,
01025           0.205903,
01026           0.103102,
01027           0.0471424,
01028           0.0194997,
01029           0.00749415,
01030           0.00273709,
01031           0.000879189,
01032           0.000286049,
01033           0.000102364,
01034           1.70606e-05,
01035           3.98081e-06,
01036           2.27475e-06,
01037           0,
01038           0,
01039           0,
01040           0
01041         };
01042  
01043  
01044         static double weight_4[25] = {
01045           0.0941305,
01046           0.373824,
01047           0.750094,
01048           1,
01049           0.997698,
01050           0.800956,
01051           0.532306,
01052           0.304597,
01053           0.152207,
01054           0.0676275,
01055           0.0270646,
01056           0.00975365,
01057           0.00326077,
01058           0.00101071,
01059           0.000301781,
01060           7.41664e-05,
01061           1.58563e-05,
01062           3.58045e-06,
01063           1.02299e-06,
01064           0,
01065           5.11493e-07,
01066           0,
01067           0,
01068           0,
01069           0
01070         };
01071  
01072  
01073         static double weight_3[25] = {
01074           0.222714,
01075           0.667015,
01076           1,
01077           0.999208,
01078           0.750609,
01079           0.449854,
01080           0.224968,
01081           0.0965185,
01082           0.0361225,
01083           0.012084,
01084           0.00359618,
01085           0.000977166,
01086           0.000239269,
01087           6.29422e-05,
01088           1.16064e-05,
01089           1.78559e-06,
01090           0,
01091           4.46398e-07,
01092           0,
01093           0,
01094           0,
01095           0,
01096           0,
01097           0,
01098           0
01099         };
01100  
01101         static double weight_2[25] = {
01102           0.499541,
01103           0.999607,
01104           1,
01105           0.666607,
01106           0.333301,
01107           0.13279,
01108           0.0441871,
01109           0.0127455,
01110           0.00318434,
01111           0.00071752,
01112           0.000132204,
01113           2.69578e-05,
01114           5.16999e-06,
01115           2.21571e-06,
01116           0,
01117           0,
01118           0,
01119           0,
01120           0,
01121           0,
01122           0,
01123           0,
01124           0,
01125           0,
01126           0
01127         };
01128  
01129         static double weight_1[25] = {
01130           0.999165,
01131           1,
01132           0.499996,
01133           0.166868,
01134           0.0414266,
01135           0.00831053,
01136           0.00137472,
01137           0.000198911,
01138           2.66302e-05,
01139           2.44563e-06,
01140           2.71737e-07,
01141           2.71737e-07,
01142           0,
01143           0,
01144           0,
01145           0,
01146           0,
01147           0,
01148           0,
01149           0,
01150           0,
01151           0,
01152           0,
01153           0,
01154           0
01155         };
01156  
01157         static double weight_0[25] = {
01158           1,
01159           0,
01160           0,
01161           0,
01162           0,
01163           0,
01164           0,
01165           0,
01166           0,
01167           0,
01168           0,
01169           0,
01170           0,
01171           0,
01172           0,
01173           0,
01174           0,
01175           0,
01176           0,
01177           0,
01178           0,
01179           0,
01180           0,
01181           0,
01182           0
01183         };
01184 
01185         //WeightOOTPU_ = {0};
01186 
01187         double* WeightPtr = 0;
01188 
01189         for(int iint = 0; iint<25; ++iint){
01190           if(iint ==0) WeightPtr = weight_0;
01191           if(iint ==1) WeightPtr = weight_1;
01192           if(iint ==2) WeightPtr = weight_2;
01193           if(iint ==3) WeightPtr = weight_3;
01194           if(iint ==4) WeightPtr = weight_4;
01195           if(iint ==5) WeightPtr = weight_5;
01196           if(iint ==6) WeightPtr = weight_6;
01197           if(iint ==7) WeightPtr = weight_7;
01198           if(iint ==8) WeightPtr = weight_8;
01199           if(iint ==9) WeightPtr = weight_9;
01200           if(iint ==10) WeightPtr = weight_10;
01201           if(iint ==11) WeightPtr = weight_11;
01202           if(iint ==12) WeightPtr = weight_12;
01203           if(iint ==13) WeightPtr = weight_13;
01204           if(iint ==14) WeightPtr = weight_14;
01205           if(iint ==15) WeightPtr = weight_15;
01206           if(iint ==16) WeightPtr = weight_16;
01207           if(iint ==17) WeightPtr = weight_17;
01208           if(iint ==18) WeightPtr = weight_18;
01209           if(iint ==19) WeightPtr = weight_19;
01210           if(iint ==20) WeightPtr = weight_20;
01211           if(iint ==21) WeightPtr = weight_21;
01212           if(iint ==22) WeightPtr = weight_22;
01213           if(iint ==23) WeightPtr = weight_23;
01214           if(iint ==24) WeightPtr = weight_24;
01215 
01216           for(int ibin = 0; ibin<25; ++ibin){
01217             WeightOOTPU_[iint][ibin] = *(WeightPtr+ibin);
01218           }
01219         }
01220 
01221       }
01222 
01223 
01224       double ITweight( int npv ){
01225         int bin = weights_->GetXaxis()->FindBin( npv );
01226         return weights_->GetBinContent( bin );
01227       }
01228 
01229       double ITweight3BX( float ave_int ){
01230         int bin = weights_->GetXaxis()->FindBin( ave_int );
01231         return weights_->GetBinContent( bin );
01232       }
01233 
01234       double weight3D( int pv1, int pv2, int pv3 ) {
01235 
01236         using std::min;
01237 
01238         int npm1 = min(pv1,34);
01239         int np0 = min(pv2,34);
01240         int npp1 = min(pv3,34);
01241 
01242         return Weight3D_[npm1][np0][npp1];
01243 
01244       }
01245 
01246 
01247 
01248       double weightOOT( int npv_in_time, int npv_m50nsBX ){
01249 
01250         static double Correct_Weights2011[25] = { // residual correction to match lumi spectrum
01251           5.30031,
01252           2.07903,
01253           1.40729,
01254           1.27687,
01255           1.0702,
01256           0.902094,
01257           0.902345,
01258           0.931449,
01259           0.78202,
01260           0.824686,
01261           0.837735,
01262           0.910261,
01263           1.01394,
01264           1.1599,
01265           1.12778,
01266           1.58423,
01267           1.78868,
01268           1.58296,
01269           2.3291,
01270           3.86641,
01271           0,
01272           0,
01273           0,
01274           0,
01275           0
01276         };                        
01277 
01278 
01279         if(FirstWarning_) {
01280 
01281           std::cout << " **** Warning: Out-of-time pileup reweighting appropriate only for PU_S3 **** " << std::endl;
01282           std::cout << " ****                          will be applied                           **** " << std::endl;
01283 
01284           FirstWarning_ = false;
01285 
01286         }
01287 
01288 
01289         // Note: for the "uncorrelated" out-of-time pileup, reweighting is only done on the 50ns
01290         // "late" bunch (BX=+1), since that is basically the only one that matters in terms of 
01291         // energy deposition.  
01292 
01293         if(npv_in_time < 0) {
01294           std::cerr << " no in-time beam crossing found\n! " ;
01295           std::cerr << " Returning event weight=0\n! ";
01296           return 0.;
01297         }
01298         if(npv_m50nsBX < 0) {
01299           std::cerr << " no out-of-time beam crossing found\n! " ;
01300           std::cerr << " Returning event weight=0\n! ";
01301           return 0.;
01302         }
01303 
01304         int bin = weights_->GetXaxis()->FindBin( npv_in_time );
01305 
01306         double inTimeWeight = weights_->GetBinContent( bin );
01307 
01308         double TotalWeight = 1.0;
01309 
01310 
01311         TotalWeight = inTimeWeight * WeightOOTPU_[bin-1][npv_m50nsBX] * Correct_Weights2011[bin-1];
01312 
01313 
01314         return TotalWeight;
01315  
01316       }
01317 
01318   protected:
01319 
01320       std::string generatedFileName_;
01321       std::string dataFileName_;
01322       std::string GenHistName_;
01323       std::string DataHistName_;
01324       TFile *generatedFile_;
01325       TFile *dataFile_;
01326       TH1  *weights_;
01327 
01328       //keep copies of normalized distributions:                                                                                  
01329       TH1*      MC_distr_;
01330       TH1*      Data_distr_;
01331 
01332       double WeightOOTPU_[25][25];
01333       double Weight3D_[35][35][35];
01334 
01335 
01336       bool FirstWarning_;
01337 
01338 
01339   };
01340 }
01341 
01342 
01343 
01344 #endif