Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:37:25

0001 #ifndef PhysicsTools_Utilities_interface_LumiReWeighting_h
0002 #define PhysicsTools_Utilities_interface_LumiReWeighting_h
0003 
0004 /**
0005   \class    LumiReWeighting LumiReWeighting.h "PhysicsTools/Utilities/interface/LumiReWeighting.h"
0006   \brief    Class to provide lumi weighting for analyzers to weight "flat-to-N" MC samples to data
0007 
0008   This class will trivially take two histograms:
0009   1. The generated "flat-to-N" distributions from a given processing
0010   2. A histogram generated from the "estimatePileup" macro here:
0011 
0012   https://twiki.cern.ch/twiki/bin/view/CMS/LumiCalc#How_to_use_script_estimatePileup
0013 
0014   \author Salvatore Rappoccio
0015 */
0016 
0017 #include "TH1.h"
0018 #include "TFile.h"
0019 #include <cmath>
0020 #include <string>
0021 
0022 #include <vector>
0023 #include "FWCore/Utilities/interface/InputTag.h"
0024 
0025 namespace reweight {
0026 
0027   // add a class to shift the mean of a poisson-like luminosity distribution by an arbitrary amount.
0028   // Only valid for small (<1.5) shifts about the 2011 lumi distribution for now, because shifts are non-linear
0029   // Each PoissonMeanShifter does one shift, so defining multiples can give you an arbitrary collection
0030 
0031   class PoissonMeanShifter {
0032   public:
0033     PoissonMeanShifter() {}
0034 
0035     PoissonMeanShifter(float Shift) {
0036       // these are the polynomial or exponential coefficients for each bin of a 25-bin sequence that
0037       // convert the Distribution of the 2011 luminosity to something with a lower or higher peak luminosity.
0038       // The distributions aren't quite poisson because they model luminosity decreasing during a fill. This implies that
0039       // they do get wider as the mean increases, so the weights are not linear with increasing mean.
0040 
0041       constexpr double p0_minus[20] = {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.};
0042       constexpr double p1_minus[20] = {-0.677786, -0.619614, -0.49465, -0.357963, -0.238359, -0.110002, 0.0348629,
0043                                        0.191263,  0.347648,  0.516615, 0.679646,  0.836673,  0.97764,   1.135,
0044                                        1.29922,   1.42467,   1.55901,  1.61762,   1.67275,   1.96008};
0045       constexpr double p2_minus[20] = {0.526164,   0.251816,   0.11049,   0.026917, -0.0464692, -0.087022, -0.0931581,
0046                                        -0.0714295, -0.0331772, 0.0347473, 0.108658, 0.193048,   0.272314,  0.376357,
0047                                        0.4964,     0.58854,    0.684959,  0.731063, 0.760044,   1.02386};
0048 
0049       constexpr double p1_expoM[5] = {1.63363e-03, 6.79290e-04, 3.69900e-04, 2.24349e-04, 9.87156e-06};
0050 
0051       constexpr double p2_expoM[5] = {2.64692, 3.26585, 3.53229, 4.18035, 5.64027};
0052 
0053       constexpr double p0_plus[20] = {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.};
0054       constexpr double p1_plus[20] = {-0.739059, -0.594445, -0.477276, -0.359707, -0.233573, -0.103458, 0.0373401,
0055                                       0.176571,  0.337617,  0.499074,  0.675126,  0.840522,  1.00917,   1.15847,
0056                                       1.23816,   1.44271,   1.52982,   1.46385,   1.5802,    0.988689};
0057       constexpr double p2_plus[20] = {0.208068,    0.130033,   0.0850356,  0.0448344,  0.000749832,
0058                                       -0.0331347,  -0.0653281, -0.0746009, -0.0800667, -0.0527636,
0059                                       -0.00402649, 0.103338,   0.261261,   0.491084,   0.857966,
0060                                       1.19495,     1.75071,    2.65559,    3.35433,    5.48835};
0061 
0062       constexpr double p1_expoP[5] = {1.42463e-01, 4.18966e-02, 1.12697e-01, 1.66197e-01, 1.50768e-01};
0063 
0064       constexpr double p2_expoP[5] = {1.98758, 2.27217, 2.26799, 2.38455, 2.52428};
0065 
0066       // initialize weights based on desired Shift
0067 
0068       for (int ibin = 0; ibin < 20; ibin++) {
0069         if (Shift < .0) {
0070           Pweight_[ibin] = p0_minus[ibin] + p1_minus[ibin] * Shift + p2_minus[ibin] * Shift * Shift;
0071         } else {
0072           Pweight_[ibin] = p0_plus[ibin] + p1_plus[ibin] * Shift + p2_plus[ibin] * Shift * Shift;
0073         }
0074       }
0075 
0076       // last few bins fit better to an exponential...
0077 
0078       for (int ibin = 20; ibin < 25; ibin++) {
0079         if (Shift < 0.) {
0080           Pweight_[ibin] = p1_expoM[ibin - 20] * exp(p2_expoM[ibin - 20] * Shift);
0081         } else {
0082           Pweight_[ibin] = p1_expoP[ibin - 20] * exp(p2_expoP[ibin - 20] * Shift);
0083         }
0084       }
0085     };
0086 
0087     double ShiftWeight(int ibin) {
0088       if (ibin < 25 && ibin >= 0) {
0089         return Pweight_[ibin];
0090       } else {
0091         return 0;
0092       }
0093     };
0094 
0095     double ShiftWeight(float pvnum) { return ShiftWeight(int(pvnum)); };
0096 
0097   private:
0098     double Pweight_[25];
0099   };
0100 
0101 }  // namespace reweight
0102 
0103 namespace edm {
0104   class EventBase;
0105   class LumiReWeighting {
0106   public:
0107     LumiReWeighting(std::string generatedFile,
0108                     std::string dataFile,
0109                     std::string GenHistName = "pileup",
0110                     std::string DataHistName = "pileup",
0111                     const edm::InputTag& PileupSumInfoInputTag = edm::InputTag("addPileupInfo"));
0112 
0113     LumiReWeighting(const std::vector<float>& MC_distr,
0114                     const std::vector<float>& Lumi_distr,
0115                     const edm::InputTag& PileupSumInfoInputTag = edm::InputTag("addPileupInfo"));
0116 
0117     LumiReWeighting() {}
0118 
0119     double weight(int npv);
0120 
0121     double weight(float npv);
0122 
0123     double weight(const edm::EventBase& e);
0124 
0125     double weightOOT(const edm::EventBase& e);
0126 
0127   protected:
0128     std::string generatedFileName_;
0129     std::string dataFileName_;
0130     std::string GenHistName_;
0131     std::string DataHistName_;
0132     edm::InputTag pileupSumInfoTag_;
0133     std::shared_ptr<TFile> generatedFile_;
0134     std::shared_ptr<TFile> dataFile_;
0135     std::shared_ptr<TH1> weights_;
0136 
0137     //keep copies of normalized distributions:
0138 
0139     std::shared_ptr<TH1> MC_distr_;
0140     std::shared_ptr<TH1> Data_distr_;
0141 
0142     int OldLumiSection_;
0143     bool FirstWarning_;
0144   };
0145 }  // namespace edm
0146 
0147 #endif