CMS 3D CMS Logo

LheWeightValidation.cc
Go to the documentation of this file.
2 
5 
6 using namespace edm;
7 
9  : lheLabel_(iPSet.getParameter<edm::InputTag>("lheProduct")),
10  genParticleToken_(consumes<reco::GenParticleCollection>(iPSet.getParameter<edm::InputTag>("genParticles"))),
11  lheEvtToken_(consumes<LHEEventProduct>(lheLabel_)),
12  lheRunToken_(consumes<LHERunInfoProduct, edm::InRun>(lheLabel_)),
13  genJetToken_(consumes<reco::GenJetCollection>(iPSet.getParameter<edm::InputTag>("genJets"))),
14  dumpLHEheader_(iPSet.getParameter<bool>("dumpLHEheader")),
15  leadLepPtNbin_(iPSet.getParameter<int>("leadLepPtNbin")),
16  rapidityNbin_(iPSet.getParameter<int>("rapidityNbin")),
17  leadLepPtRange_(iPSet.getParameter<double>("leadLepPtRange")),
18  leadLepPtCut_(iPSet.getParameter<double>("leadLepPtCut")),
19  lepEtaCut_(iPSet.getParameter<double>("lepEtaCut")),
20  rapidityRange_(iPSet.getParameter<double>("rapidityRange")),
21  nJetsNbin_(iPSet.getParameter<int>("nJetsNbin")),
22  jetPtNbin_(iPSet.getParameter<int>("jetPtNbin")),
23  jetPtCut_(iPSet.getParameter<double>("jetPtCut")),
24  jetEtaCut_(iPSet.getParameter<double>("jetEtaCut")),
25  jetPtRange_(iPSet.getParameter<double>("jetPtRange")),
26  nScaleVar_(iPSet.getParameter<int>("nScaleVar")),
27  idxPdfStart_(iPSet.getParameter<int>("idxPdfStart")),
28  idxPdfEnd_(iPSet.getParameter<int>("idxPdfEnd")),
29  nPdfVar_(idxPdfEnd_ - idxPdfStart_ + 1) {}
30 
32  // check LHE product exists
34  // getByToken throws an exception unless we're in the endRun (see https://github.com/cms-sw/cmssw/pull/18499)
36 
37  if (!lheInfo.isValid())
38  return;
39 
41  std::string folderName = "Generator/LHEWeight";
42  DQMHelper aDqmHelper(&iBook);
44 
45  // Number of analyzed events
46  nEvt_ = aDqmHelper.book1dHisto("nEvt", "n analyzed Events", 1, 0., 1., "bin", "Number of Events");
47  nlogWgt_ = aDqmHelper.book1dHisto("nlogWgt", "Log10(n weights)", 100, 0., 5., "log_{10}(nWgts)", "Number of Events");
48  wgtVal_ = aDqmHelper.book1dHisto("wgtVal", "weights", 100, -1.5, 3., "weight", "Number of Weights");
49 
50  bookTemplates(aDqmHelper,
54  "leadLepPt",
55  "leading lepton Pt",
57  0.,
59  "Pt_{l} (GeV)",
60  "Number of Events");
61  bookTemplates(aDqmHelper,
65  "leadLepEta",
66  "leading lepton #eta",
70  "#eta_{l}",
71  "Number of Events");
72  bookTemplates(aDqmHelper,
76  "JetMultiplicity",
77  "Gen jet multiplicity",
78  nJetsNbin_,
79  0,
80  nJetsNbin_,
81  "n",
82  "Number of Events");
83  bookTemplates(aDqmHelper,
87  "leadJetPt",
88  "leading Gen jet Pt",
89  jetPtNbin_,
90  0.,
92  "Pt_{j} (GeV)",
93  "Number of Events");
94  bookTemplates(aDqmHelper,
98  "leadJetEta",
99  "leading Gen jet #eta",
103  "#eta_{j}",
104  "Number of Events");
105 
106  return;
107 }
108 
110  std::vector<std::unique_ptr<TH1F>>& scaleVar,
111  std::vector<std::unique_ptr<TH1F>>& pdfVar,
112  std::vector<MonitorElement*>& tmps,
113  const std::string& name,
114  const std::string& title,
115  int nbin,
116  float low,
117  float high,
118  const std::string& xtitle,
119  const std::string& ytitle) {
120  tmps.push_back(aDqmHelper.book1dHisto(name, title, nbin, low, high, xtitle, ytitle));
121  tmps.push_back(aDqmHelper.book1dHisto(name + "ScaleUp", title + " scale up", nbin, low, high, xtitle, ytitle));
122  tmps.at(1)->getTH1()->Sumw2(false);
123  tmps.push_back(aDqmHelper.book1dHisto(name + "ScaleDn", title + " scale down", nbin, low, high, xtitle, ytitle));
124  tmps.at(2)->getTH1()->Sumw2(false);
125  tmps.push_back(aDqmHelper.book1dHisto(
126  name + "ScaleUp_ratio", "Ratio of " + title + " scale upper envelop / Nominal", nbin, low, high, xtitle, ytitle));
127  tmps.at(3)->setEfficiencyFlag();
128  tmps.push_back(aDqmHelper.book1dHisto(
129  name + "ScaleDn_ratio", "Ratio of " + title + " scale lower envelop / Nominal", nbin, low, high, xtitle, ytitle));
130  tmps.at(4)->setEfficiencyFlag();
131  tmps.push_back(aDqmHelper.book1dHisto(name + "PdfUp", title + " PDF upper RMS", nbin, low, high, xtitle, ytitle));
132  tmps.at(5)->getTH1()->Sumw2(false);
133  tmps.push_back(aDqmHelper.book1dHisto(name + "PdfDn", title + " PDF lower RMS", nbin, low, high, xtitle, ytitle));
134  tmps.at(6)->getTH1()->Sumw2(false);
135  tmps.push_back(aDqmHelper.book1dHisto(
136  name + "PdfUp_ratio", "Ratio of " + title + " PDF upper RMS / Nominal", nbin, low, high, xtitle, ytitle));
137  tmps.at(7)->setEfficiencyFlag();
138  tmps.push_back(aDqmHelper.book1dHisto(
139  name + "PdfDn_ratio", "Ratio of " + title + " PDF lower RMS / Nominal", nbin, low, high, xtitle, ytitle));
140  tmps.at(8)->setEfficiencyFlag();
141 
142  for (int idx = 0; idx < nScaleVar_; idx++) {
143  scaleVar.push_back(
144  std::make_unique<TH1F>(std::string(name + "Scale" + std::to_string(idx)).c_str(),
145  std::string(";" + std::string(xtitle) + ";" + std::string(ytitle)).c_str(),
146  nbin,
147  low,
148  high));
149  scaleVar.at(idx)->Sumw2();
150  }
151 
152  for (int idx = 0; idx < nPdfVar_; idx++) {
153  pdfVar.push_back(std::make_unique<TH1F>(std::string(name + "Pdf" + std::to_string(idx)).c_str(),
154  std::string(";" + std::string(xtitle) + ";" + std::string(ytitle)).c_str(),
155  nbin,
156  low,
157  high));
158  pdfVar.at(idx)->Sumw2();
159  }
160 } // to get ratio plots correctly - need to modify PostProcessor_cff.py as well!
161 
163 
166 
168  iEvent.getByToken(lheEvtToken_, lheEvt);
169 
170  if (!lheEvt.isValid())
171  return; // do nothing if there is no LHE product
172 
173  orgWgt_ = lheEvt->originalXWGTUP();
175  weights_ = lheEvt->weights();
176 
177  nEvt_->Fill(0.5, weight_);
178  nlogWgt_->Fill(std::log10(lheEvt->weights().size()), weight_);
179 
180  for (unsigned idx = 0; idx < lheEvt->weights().size(); idx++)
181  wgtVal_->Fill(weights_[idx].wgt / orgWgt_);
182 
184  iEvent.getByToken(genParticleToken_, ptcls);
186  iEvent.getByToken(genJetToken_, genjets);
187 
188  std::vector<reco::GenParticleRef> leptons;
189 
190  for (unsigned iptc = 0; iptc < ptcls->size(); iptc++) {
191  reco::GenParticleRef ptc(ptcls, iptc);
192  if (ptc->status() == 1 && (std::abs(ptc->pdgId()) == 11 || std::abs(ptc->pdgId()) == 13)) {
193  if (ptc->pt() > leadLepPtCut_ && std::abs(ptc->eta()) < lepEtaCut_)
194  leptons.push_back(ptc);
195  }
196  }
197 
198  std::sort(leptons.begin(), leptons.end(), HepMCValidationHelper::sortByPtRef<reco::GenParticleRef>);
199 
200  if (!leptons.empty()) {
201  reco::GenParticleRef leadLep = leptons.at(0);
204  }
205 
206  std::vector<reco::GenJetRef> genjetVec;
207 
208  for (unsigned igj = 0; igj < genjets->size(); igj++) {
209  reco::GenJetRef genjet(genjets, igj);
210 
211  if (genjet->pt() > jetPtCut_ && std::abs(genjet->eta()) < jetEtaCut_)
212  genjetVec.push_back(genjet);
213  }
214 
215  fillTemplates(jetMultScaleVar_, jetMultPdfVar_, jetMultTemp_, (float)genjetVec.size());
216 
217  if (!genjetVec.empty()) {
218  std::sort(genjetVec.begin(), genjetVec.end(), HepMCValidationHelper::sortByPtRef<reco::GenJetRef>);
219 
220  auto leadJet = genjetVec.at(0);
223  }
224 } //analyze
225 
226 void LheWeightValidation::fillTemplates(std::vector<std::unique_ptr<TH1F>>& scaleVar,
227  std::vector<std::unique_ptr<TH1F>>& pdfVar,
228  std::vector<MonitorElement*>& tmps,
229  float obs) {
230  tmps.at(0)->Fill(obs, weight_);
231 
232  if (static_cast<int>(weights_.size()) >= nScaleVar_) {
233  for (int iWgt = 0; iWgt < nScaleVar_; iWgt++)
234  scaleVar.at(iWgt)->Fill(obs, weights_[iWgt].wgt / orgWgt_);
235  }
236 
237  if (static_cast<int>(weights_.size()) >= idxPdfEnd_) {
238  for (int iWgt = 0; iWgt < nPdfVar_; iWgt++)
239  pdfVar.at(iWgt)->Fill(obs, weights_[idxPdfStart_ + iWgt].wgt / orgWgt_);
240  }
241 }
242 
245  return;
246 
249 
250  if (!lheInfo.isValid())
251  return;
252 
263 
264  if (dumpLHEheader_) {
265  for (auto it = lheInfo->headers_begin(); it != lheInfo->headers_end(); it++) {
266  std::cout << "Header start" << std::endl;
267  std::cout << "Tag: " << it->tag() << std::endl;
268  for (const auto& l : it->lines()) {
269  std::cout << l << std::endl;
270  }
271  std::cout << "Header end" << std::endl;
272  }
273  }
274 }
275 
276 void LheWeightValidation::envelop(const std::vector<std::unique_ptr<TH1F>>& var, std::vector<MonitorElement*>& tmps) {
277  if (var.empty())
278  return;
279 
280  for (int b = 0; b < var.at(0)->GetNbinsX() + 2; b++) {
281  float valU = var.at(0)->GetBinContent(b);
282  float valD = valU;
283 
284  if (valU == 0.)
285  continue;
286 
287  for (unsigned v = 1; v < var.size(); v++) {
288  if (var.at(v)->GetEntries() == 0.)
289  continue;
290 
291  valU = std::max(valU, (float)var.at(v)->GetBinContent(b));
292  valD = std::min(valD, (float)var.at(v)->GetBinContent(b));
293  }
294  tmps.at(1)->setBinContent(b, valU);
295  tmps.at(2)->setBinContent(b, valD);
296  }
297 
298  tmps.at(1)->setEntries(var.at(0)->GetEntries());
299  tmps.at(2)->setEntries(var.at(0)->GetEntries());
300  tmps.at(1)->getTH1()->Sumw2(true);
301  tmps.at(2)->getTH1()->Sumw2(true);
302 
303  return;
304 }
305 
306 void LheWeightValidation::pdfRMS(const std::vector<std::unique_ptr<TH1F>>& var, std::vector<MonitorElement*>& tmps) {
307  if (var.empty())
308  return;
309 
310  float denom = var.size();
311  for (int b = 0; b < tmps.at(0)->getNbinsX() + 2; b++) {
312  float valNom = tmps.at(0)->getBinContent(b);
313  float rmsSq = 0.;
314  if (valNom == 0.)
315  continue;
316 
317  for (unsigned v = 0; v < var.size(); v++) {
318  if (var.at(v)->GetEntries() == 0.)
319  continue;
320 
321  float dev = (float)var.at(v)->GetBinContent(b) - valNom;
322  rmsSq += dev * dev;
323  }
324 
325  float rms = std::sqrt(rmsSq / denom);
326  float rmsup = valNom + rms;
327  float rmsdn = valNom - rms;
328  tmps.at(5)->setBinContent(b, rmsup);
329  tmps.at(6)->setBinContent(b, rmsdn);
330  }
331 
332  tmps.at(5)->setEntries(tmps.at(0)->getTH1F()->GetEntries());
333  tmps.at(6)->setEntries(tmps.at(0)->getTH1F()->GetEntries());
334  tmps.at(5)->getTH1()->Sumw2(true);
335  tmps.at(6)->getTH1()->Sumw2(true);
336 
337  return;
338 }
const edm::InputTag lheLabel_
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
void envelop(const std::vector< std::unique_ptr< TH1F >> &var, std::vector< MonitorElement *> &tmps)
void pdfRMS(const std::vector< std::unique_ptr< TH1F >> &var, std::vector< MonitorElement *> &tmps)
std::vector< MonitorElement * > leadLepPtTemp_
MonitorElement * wgtVal_
std::vector< LHEEventProduct::WGT > weights_
std::vector< MonitorElement * > leadJetPtTemp_
std::vector< std::unique_ptr< TH1F > > leadLepEtaScaleVar_
double originalXWGTUP() const
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
std::vector< GenJet > GenJetCollection
collection of GenJet objects
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:99
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
std::string to_string(const V &value)
Definition: OMSAccess.h:71
std::vector< std::unique_ptr< TH1F > > jetMultScaleVar_
void fillTemplates(std::vector< std::unique_ptr< TH1F >> &scaleVar, std::vector< std::unique_ptr< TH1F >> &pdfVar, std::vector< MonitorElement *> &tmps, float obs)
std::vector< std::unique_ptr< TH1F > > leadJetPtScaleVar_
std::vector< std::unique_ptr< TH1F > > leadJetEtaPdfVar_
std::vector< std::unique_ptr< TH1F > > leadLepPtScaleVar_
std::vector< std::unique_ptr< TH1F > > jetMultPdfVar_
LheWeightValidation(const edm::ParameterSet &)
void Fill(long long x)
int iEvent
Definition: GenABIO.cc:224
void dqmBeginRun(const edm::Run &, const edm::EventSetup &) override
const edm::EDGetTokenT< reco::GenParticleCollection > genParticleToken_
MonitorElement * nlogWgt_
T sqrt(T t)
Definition: SSEVec.h:19
void analyze(const edm::Event &, const edm::EventSetup &) override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:283
void bookTemplates(DQMHelper &aDqmHelper, std::vector< std::unique_ptr< TH1F >> &scaleVar, std::vector< std::unique_ptr< TH1F >> &pdfVar, std::vector< MonitorElement *> &tmps, const std::string &name, const std::string &title, int nbin, float low, float high, const std::string &xtitle, const std::string &ytitle)
std::vector< MonitorElement * > jetMultTemp_
MonitorElement * book1dHisto(const std::string &name, const std::string &title, int n, double xmin, double xmax, const std::string &xaxis, const std::string &yaxis)
Definition: DQMHelper.cc:7
const edm::EDGetTokenT< LHEEventProduct > lheEvtToken_
MonitorElement * nEvt_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:318
double b
Definition: hdecay.h:118
std::vector< MonitorElement * > leadLepEtaTemp_
bool isValid() const
Definition: HandleBase.h:70
const double leadLepPtRange_
std::vector< std::unique_ptr< TH1F > > leadLepEtaPdfVar_
fixed size matrix
HLT enums.
const edm::EDGetTokenT< reco::GenJetCollection > genJetToken_
const edm::EDGetTokenT< LHERunInfoProduct > lheRunToken_
std::vector< std::unique_ptr< TH1F > > leadJetEtaScaleVar_
std::vector< std::unique_ptr< TH1F > > leadJetPtPdfVar_
void dqmEndRun(const edm::Run &, const edm::EventSetup &) override
const std::vector< WGT > & weights() const
Definition: Run.h:45
std::vector< MonitorElement * > leadJetEtaTemp_
std::vector< std::unique_ptr< TH1F > > leadLepPtPdfVar_