CMS 3D CMS Logo

LumiReWeighting.cc
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_interface_LumiReWeighting_cc
2 #define PhysicsTools_Utilities_interface_LumiReWeighting_cc
3 
19 #include "TFile.h"
20 #include "TH1.h"
21 #include "TRandom1.h"
22 #include "TRandom2.h"
23 #include "TRandom3.h"
24 #include "TStopwatch.h"
25 #include <algorithm>
26 #include <iostream>
27 #include <memory>
28 #include <string>
29 
36 
37 using namespace edm;
38 
40  std::string dataFile,
41  std::string GenHistName,
42  std::string DataHistName,
43  const edm::InputTag& PileupSumInfoInputTag)
44  : generatedFileName_(generatedFile),
45  dataFileName_(dataFile),
46  GenHistName_(GenHistName),
47  DataHistName_(DataHistName),
48  pileupSumInfoTag_(PileupSumInfoInputTag) {
49  generatedFile_ = std::make_shared<TFile>(generatedFileName_.c_str()); //MC distribution
50  dataFile_ = std::make_shared<TFile>(dataFileName_.c_str()); //Data distribution
51 
52  Data_distr_ = std::shared_ptr<TH1>((static_cast<TH1*>(dataFile_->Get(DataHistName_.c_str())->Clone())));
53  MC_distr_ = std::shared_ptr<TH1>((static_cast<TH1*>(generatedFile_->Get(GenHistName_.c_str())->Clone())));
54 
55  // MC * data/MC = data, so the weights are data/MC:
56 
57  // normalize both histograms first
58 
59  Data_distr_->Scale(1.0 / Data_distr_->Integral());
60  MC_distr_->Scale(1.0 / MC_distr_->Integral());
61 
62  weights_ = std::shared_ptr<TH1>(static_cast<TH1*>(Data_distr_->Clone()));
63 
64  weights_->SetName("lumiWeights");
65 
66  TH1* den = dynamic_cast<TH1*>(MC_distr_->Clone());
67 
68  //den->Scale(1.0/ den->Integral());
69 
70  weights_->Divide(den); // so now the average weight should be 1.0
71 
72  std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
73 
74  int NBins = weights_->GetNbinsX();
75 
76  for (int ibin = 1; ibin < NBins + 1; ++ibin) {
77  std::cout << " " << ibin - 1 << " " << weights_->GetBinContent(ibin) << std::endl;
78  }
79 
80  FirstWarning_ = true;
81  OldLumiSection_ = -1;
82 }
83 
84 LumiReWeighting::LumiReWeighting(const std::vector<float>& MC_distr,
85  const std::vector<float>& Lumi_distr,
86  const edm::InputTag& PileupSumInfoInputTag)
87  : pileupSumInfoTag_(PileupSumInfoInputTag) {
88  // no histograms for input: use vectors
89 
90  // now, make histograms out of them:
91 
92  // first, check they are the same size...
93 
94  if (MC_distr.size() != Lumi_distr.size()) {
95  std::cerr << "ERROR: LumiReWeighting: input vectors have different sizes. Quitting... \n";
96  return;
97  }
98 
99  Int_t NBins = MC_distr.size();
100 
101  MC_distr_ = std::shared_ptr<TH1>(new TH1F("MC_distr", "MC dist", NBins, -0.5, float(NBins) - 0.5));
102  Data_distr_ = std::shared_ptr<TH1>(new TH1F("Data_distr", "Data dist", NBins, -0.5, float(NBins) - 0.5));
103 
104  weights_ = std::shared_ptr<TH1>(new TH1F("luminumer", "luminumer", NBins, -0.5, float(NBins) - 0.5));
105  TH1* den = new TH1F("lumidenom", "lumidenom", NBins, -0.5, float(NBins) - 0.5);
106 
107  for (int ibin = 1; ibin < NBins + 1; ++ibin) {
108  weights_->SetBinContent(ibin, Lumi_distr[ibin - 1]);
109  Data_distr_->SetBinContent(ibin, Lumi_distr[ibin - 1]);
110  den->SetBinContent(ibin, MC_distr[ibin - 1]);
111  MC_distr_->SetBinContent(ibin, MC_distr[ibin - 1]);
112  }
113 
114  // check integrals, make sure things are normalized
115 
116  float deltaH = weights_->Integral();
117  if (fabs(1.0 - deltaH) > 0.02) { //*OOPS*...
118  weights_->Scale(1.0 / weights_->Integral());
119  Data_distr_->Scale(1.0 / Data_distr_->Integral());
120  }
121  float deltaMC = den->Integral();
122  if (fabs(1.0 - deltaMC) > 0.02) {
123  den->Scale(1.0 / den->Integral());
124  MC_distr_->Scale(1.0 / MC_distr_->Integral());
125  }
126 
127  weights_->Divide(den); // so now the average weight should be 1.0
128 
129  std::cout << " Lumi/Pileup Reweighting: Computed Weights per In-Time Nint " << std::endl;
130 
131  for (int ibin = 1; ibin < NBins + 1; ++ibin) {
132  std::cout << " " << ibin - 1 << " " << weights_->GetBinContent(ibin) << std::endl;
133  }
134 
135  FirstWarning_ = true;
136  OldLumiSection_ = -1;
137 }
138 
139 double LumiReWeighting::weight(int npv) {
140  int bin = weights_->GetXaxis()->FindBin(npv);
141  return weights_->GetBinContent(bin);
142 }
143 
144 double LumiReWeighting::weight(float npv) {
145  int bin = weights_->GetXaxis()->FindBin(npv);
146  return weights_->GetBinContent(bin);
147 }
148 
149 // This version of weight does all of the work for you, assuming you want to re-weight
150 // using the true number of interactions in the in-time beam crossing.
151 
153  if (FirstWarning_) {
154  e.processHistory();
155  // SetFirstFalse();
156  FirstWarning_ = false;
157  }
159  e.getByLabel(pileupSumInfoTag_, PupInfo);
160 
161  std::vector<PileupSummaryInfo>::const_iterator PVI;
162 
163  int npv = -1;
164  for (PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
165  int BX = PVI->getBunchCrossing();
166 
167  if (BX == 0) {
168  npv = PVI->getPU_NumInteractions();
169  continue;
170  }
171  }
172 
173  if (npv < 0)
174  std::cerr << " no in-time beam crossing found\n! ";
175 
176  return weight(npv);
177 }
178 
179 // Use this routine to re-weight out-of-time pileup to match the in-time distribution
180 // As of May 2011, CMS is only sensitive to a bunch that is 50ns "late", which corresponds to
181 // BunchCrossing +1. So, we use that here for re-weighting.
182 
184  //int Run = e.run();
185  int LumiSection = e.luminosityBlock();
186  // do some caching here, attempt to catch file boundaries
187 
188  if (LumiSection != OldLumiSection_) {
189  e.processHistory(); // keep the function call
190  OldLumiSection_ = LumiSection;
191  }
192  // find the pileup summary information
193 
195  e.getByLabel(pileupSumInfoTag_, PupInfo);
196 
197  std::vector<PileupSummaryInfo>::const_iterator PVI;
198 
199  int npv = -1;
200  int npv50ns = -1;
201 
202  for (PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
203  int BX = PVI->getBunchCrossing();
204 
205  if (BX == 0) {
206  npv = PVI->getPU_NumInteractions();
207  }
208 
209  if (BX == 1) {
210  npv50ns = PVI->getPU_NumInteractions();
211  }
212  }
213 
214  // Note: for the "uncorrelated" out-of-time pileup, reweighting is only done on the 50ns
215  // "late" bunch (BX=+1), since that is basically the only one that matters in terms of
216  // energy deposition.
217 
218  if (npv < 0) {
219  std::cerr << " no in-time beam crossing found\n! ";
220  std::cerr << " Returning event weight=0\n! ";
221  return 0.;
222  }
223  if (npv50ns < 0) {
224  std::cerr << " no out-of-time beam crossing found\n! ";
225  std::cerr << " Returning event weight=0\n! ";
226  return 0.;
227  }
228 
229  int bin = weights_->GetXaxis()->FindBin(npv);
230 
231  double inTimeWeight = weights_->GetBinContent(bin);
232 
233  double TotalWeight = 1.0;
234 
235  TotalWeight = inTimeWeight;
236 
237  return TotalWeight;
238 }
239 
240 #endif
Handle.h
PileupSummaryInfo.h
edm::LumiReWeighting::generatedFile_
std::shared_ptr< TFile > generatedFile_
Definition: LumiReWeighting.h:133
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::LumiReWeighting::weight
double weight(int npv)
Definition: LumiReWeighting.cc:139
gather_cfg.cout
cout
Definition: gather_cfg.py:144
edm::LumiReWeighting::Data_distr_
std::shared_ptr< TH1 > Data_distr_
Definition: LumiReWeighting.h:140
edm::LumiReWeighting::pileupSumInfoTag_
edm::InputTag pileupSumInfoTag_
Definition: LumiReWeighting.h:132
edm::LumiReWeighting::FirstWarning_
bool FirstWarning_
Definition: LumiReWeighting.h:143
edm::LumiReWeighting::weightOOT
double weightOOT(const edm::EventBase &e)
Definition: LumiReWeighting.cc:183
EventBase.h
edm::Handle
Definition: AssociativeIterator.h:50
ProcessHistory.h
Provenance.h
edm::LumiReWeighting::dataFileName_
std::string dataFileName_
Definition: LumiReWeighting.h:129
edm::LumiReWeighting::MC_distr_
std::shared_ptr< TH1 > MC_distr_
Definition: LumiReWeighting.h:139
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::LumiReWeighting::OldLumiSection_
int OldLumiSection_
Definition: LumiReWeighting.h:142
edm::LumiReWeighting::LumiReWeighting
LumiReWeighting()
Definition: LumiReWeighting.h:117
edm::LumiReWeighting::generatedFileName_
std::string generatedFileName_
Definition: LumiReWeighting.h:128
SiStripSourceConfigP5_cff.NBins
NBins
Definition: SiStripSourceConfigP5_cff.py:4
LumiReWeighting.h
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
edm::EventBase
Definition: EventBase.h:46
edm::LumiReWeighting::DataHistName_
std::string DataHistName_
Definition: LumiReWeighting.h:131
L1TStage2uGTEmulatorClient_cff.BX
BX
Definition: L1TStage2uGTEmulatorClient_cff.py:9
edm::LumiReWeighting::weights_
std::shared_ptr< TH1 > weights_
Definition: LumiReWeighting.h:135
edm::LumiReWeighting::GenHistName_
std::string GenHistName_
Definition: LumiReWeighting.h:130
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
edm::LumiReWeighting::dataFile_
std::shared_ptr< TFile > dataFile_
Definition: LumiReWeighting.h:134
edm::InputTag
Definition: InputTag.h:15
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37