File indexing completed on 2024-04-06 12:24:22
0001 #ifndef PhysicsTools_Utilities_interface_LumiReWeighting_cc
0002 #define PhysicsTools_Utilities_interface_LumiReWeighting_cc
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include "TFile.h"
0020 #include "TH1.h"
0021 #include "TRandom1.h"
0022 #include "TRandom2.h"
0023 #include "TRandom3.h"
0024 #include "TStopwatch.h"
0025 #include <algorithm>
0026 #include <iostream>
0027 #include <memory>
0028 #include <string>
0029
0030 #include "SimDataFormats/PileupSummaryInfo/interface/PileupSummaryInfo.h"
0031 #include "PhysicsTools/Utilities/interface/LumiReWeighting.h"
0032 #include "DataFormats/Common/interface/Handle.h"
0033 #include "DataFormats/Provenance/interface/ProcessHistory.h"
0034 #include "DataFormats/Provenance/interface/Provenance.h"
0035 #include "FWCore/Common/interface/EventBase.h"
0036
0037 using namespace edm;
0038
0039 LumiReWeighting::LumiReWeighting(std::string generatedFile,
0040 std::string dataFile,
0041 std::string GenHistName,
0042 std::string DataHistName,
0043 const edm::InputTag& PileupSumInfoInputTag)
0044 : generatedFileName_(generatedFile),
0045 dataFileName_(dataFile),
0046 GenHistName_(GenHistName),
0047 DataHistName_(DataHistName),
0048 pileupSumInfoTag_(PileupSumInfoInputTag) {
0049 generatedFile_ = std::make_shared<TFile>(generatedFileName_.c_str());
0050 dataFile_ = std::make_shared<TFile>(dataFileName_.c_str());
0051
0052 Data_distr_ = std::shared_ptr<TH1>((static_cast<TH1*>(dataFile_->Get(DataHistName_.c_str())->Clone())));
0053 MC_distr_ = std::shared_ptr<TH1>((static_cast<TH1*>(generatedFile_->Get(GenHistName_.c_str())->Clone())));
0054
0055
0056
0057
0058
0059 Data_distr_->Scale(1.0 / Data_distr_->Integral());
0060 MC_distr_->Scale(1.0 / MC_distr_->Integral());
0061
0062 weights_ = std::shared_ptr<TH1>(static_cast<TH1*>(Data_distr_->Clone()));
0063
0064 weights_->SetName("lumiWeights");
0065
0066 TH1* den = dynamic_cast<TH1*>(MC_distr_->Clone());
0067
0068
0069
0070 weights_->Divide(den);
0071
0072 std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
0073
0074 int NBins = weights_->GetNbinsX();
0075
0076 for (int ibin = 1; ibin < NBins + 1; ++ibin) {
0077 std::cout << " " << ibin - 1 << " " << weights_->GetBinContent(ibin) << std::endl;
0078 }
0079
0080 FirstWarning_ = true;
0081 OldLumiSection_ = -1;
0082 }
0083
0084 LumiReWeighting::LumiReWeighting(const std::vector<float>& MC_distr,
0085 const std::vector<float>& Lumi_distr,
0086 const edm::InputTag& PileupSumInfoInputTag)
0087 : pileupSumInfoTag_(PileupSumInfoInputTag) {
0088
0089
0090
0091
0092
0093
0094 if (MC_distr.size() != Lumi_distr.size()) {
0095 std::cerr << "ERROR: LumiReWeighting: input vectors have different sizes. Quitting... \n";
0096 return;
0097 }
0098
0099 Int_t NBins = MC_distr.size();
0100
0101 MC_distr_ = std::shared_ptr<TH1>(new TH1F("MC_distr", "MC dist", NBins, -0.5, float(NBins) - 0.5));
0102 Data_distr_ = std::shared_ptr<TH1>(new TH1F("Data_distr", "Data dist", NBins, -0.5, float(NBins) - 0.5));
0103
0104 weights_ = std::shared_ptr<TH1>(new TH1F("luminumer", "luminumer", NBins, -0.5, float(NBins) - 0.5));
0105 TH1* den = new TH1F("lumidenom", "lumidenom", NBins, -0.5, float(NBins) - 0.5);
0106
0107 for (int ibin = 1; ibin < NBins + 1; ++ibin) {
0108 weights_->SetBinContent(ibin, Lumi_distr[ibin - 1]);
0109 Data_distr_->SetBinContent(ibin, Lumi_distr[ibin - 1]);
0110 den->SetBinContent(ibin, MC_distr[ibin - 1]);
0111 MC_distr_->SetBinContent(ibin, MC_distr[ibin - 1]);
0112 }
0113
0114
0115
0116 float deltaH = weights_->Integral();
0117 if (fabs(1.0 - deltaH) > 0.02) {
0118 weights_->Scale(1.0 / weights_->Integral());
0119 Data_distr_->Scale(1.0 / Data_distr_->Integral());
0120 }
0121 float deltaMC = den->Integral();
0122 if (fabs(1.0 - deltaMC) > 0.02) {
0123 den->Scale(1.0 / den->Integral());
0124 MC_distr_->Scale(1.0 / MC_distr_->Integral());
0125 }
0126
0127 weights_->Divide(den);
0128
0129 std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
0130
0131 for (int ibin = 1; ibin < NBins + 1; ++ibin) {
0132 std::cout << " " << ibin - 1 << " " << weights_->GetBinContent(ibin) << std::endl;
0133 }
0134
0135 FirstWarning_ = true;
0136 OldLumiSection_ = -1;
0137 }
0138
0139 double LumiReWeighting::weight(int npv) {
0140 int bin = weights_->GetXaxis()->FindBin(npv);
0141 return weights_->GetBinContent(bin);
0142 }
0143
0144 double LumiReWeighting::weight(float npv) {
0145 int bin = weights_->GetXaxis()->FindBin(npv);
0146 return weights_->GetBinContent(bin);
0147 }
0148
0149
0150
0151
0152 double LumiReWeighting::weight(const edm::EventBase& e) {
0153 if (FirstWarning_) {
0154 e.processHistory();
0155
0156 FirstWarning_ = false;
0157 }
0158 Handle<std::vector<PileupSummaryInfo> > PupInfo;
0159 e.getByLabel(pileupSumInfoTag_, PupInfo);
0160
0161 std::vector<PileupSummaryInfo>::const_iterator PVI;
0162
0163 int npv = -1;
0164 for (PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
0165 int BX = PVI->getBunchCrossing();
0166
0167 if (BX == 0) {
0168 npv = PVI->getPU_NumInteractions();
0169 continue;
0170 }
0171 }
0172
0173 if (npv < 0)
0174 std::cerr << " no in-time beam crossing found\n! ";
0175
0176 return weight(npv);
0177 }
0178
0179
0180
0181
0182
0183 double LumiReWeighting::weightOOT(const edm::EventBase& e) {
0184
0185 int LumiSection = e.luminosityBlock();
0186
0187
0188 if (LumiSection != OldLumiSection_) {
0189 e.processHistory();
0190 OldLumiSection_ = LumiSection;
0191 }
0192
0193
0194 Handle<std::vector<PileupSummaryInfo> > PupInfo;
0195 e.getByLabel(pileupSumInfoTag_, PupInfo);
0196
0197 std::vector<PileupSummaryInfo>::const_iterator PVI;
0198
0199 int npv = -1;
0200 int npv50ns = -1;
0201
0202 for (PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
0203 int BX = PVI->getBunchCrossing();
0204
0205 if (BX == 0) {
0206 npv = PVI->getPU_NumInteractions();
0207 }
0208
0209 if (BX == 1) {
0210 npv50ns = PVI->getPU_NumInteractions();
0211 }
0212 }
0213
0214
0215
0216
0217
0218 if (npv < 0) {
0219 std::cerr << " no in-time beam crossing found\n! ";
0220 std::cerr << " Returning event weight=0\n! ";
0221 return 0.;
0222 }
0223 if (npv50ns < 0) {
0224 std::cerr << " no out-of-time beam crossing found\n! ";
0225 std::cerr << " Returning event weight=0\n! ";
0226 return 0.;
0227 }
0228
0229 int bin = weights_->GetXaxis()->FindBin(npv);
0230
0231 double inTimeWeight = weights_->GetBinContent(bin);
0232
0233 double TotalWeight = 1.0;
0234
0235 TotalWeight = inTimeWeight;
0236
0237 return TotalWeight;
0238 }
0239
0240 #endif