babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
get_puweights.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os, math
4 from ROOT import *
5 
6 # ----- NEEDED INPUTS --------------------------------------
7 # Instructions:
8 # https://twiki.cern.ch/twiki/bin/viewauth/CMS/PileupJSONFileforData
9 
10 cmssw = os.getenv("CMSSW_BASE")+"/src/"
11 # retrieve latest json with per-lumiblock luminosity information
12 # /afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions16/13TeV/PileUp/pileup_latest.txt
13 # fyi, how it gets derived: https://hypernews.cern.ch/HyperNews/CMS/get/luminosity/660/1.html
14 lumi_json = cmssw + "babymaker/data/json/pileup_latest.txt"
15 
16 # obtain certified lumis JSON for dataset we are trying to reweight to
17 # /afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions16/13TeV/
18 json = cmssw + "babymaker/data/json/golden_Cert_271036-284044_13TeV_23Sep2016ReReco_Collisions16.json"
19 
20 # Minbias cross-section, (corrected) announcement for ICHEP16:
21 # https://hypernews.cern.ch/HyperNews/CMS/get/luminosity/613/2/1/1/1.html
22 mbxsec, mbxsec_relunc = 69200, 0.046
23 
24 # get hist of the MC PU profile
25 gROOT.SetBatch(kTRUE)
26 mcfile = TChain("tree")
27 mcfile.Add("/net/cms2/cms2r0/babymaker/babies/2017_01_21/mc/unprocessed/fullbaby_TTJets_SingleLeptFromT_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_RunIISummer16MiniAODv2-PUMoriond17_80X_mcRun2_asymptotic_2016_TrancheIV_v6-v1_*")
28 hmc = TH1D("hmc","hmc",75,0,75)
29 mcfile.Draw("ntrupv_mean>>hmc","","norm")
30 
31 # To reweight both the in- and out-of-time pile up
32 # pileupCalc should be used in mode "true" and the variable to reweight is npvtru_mean:
33 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookExercisesHATS_Pileup_2013
34 calc_mode = "true"
35 
36 # ----------------------------------------------------------
37 
38 mbxsec_dict = {
39  "nom": mbxsec,
40  "up": mbxsec*(1+mbxsec_relunc),
41  "down": mbxsec*(1-mbxsec_relunc)
42 }
43 
44 for imb in mbxsec_dict:
45  cmd = "pileupCalc.py"
46  cmd += " -i "+json
47  cmd += " --inputLumiJSON "+lumi_json
48  cmd += " --calcMode "+calc_mode
49  cmd += " --minBiasXsec "+str(mbxsec_dict[imb])
50  cmd += " --maxPileupBin "+str(hmc.GetNbinsX())
51  cmd += " --numPileupBins "+str(hmc.GetNbinsX())
52  cmd += " pileup_"+imb+".root"
53 
54  print "Obtaining data pile up distribution for variation:", imb
55  print cmd
56  os.system(cmd)
57 
58  fdata = TFile("pileup_"+imb+".root","READ")
59  htmp = fdata.Get("pileup").Clone("pu_"+imb)
60  htmp.Scale(1./htmp.Integral())
61  htmp.Divide(hmc)
62  htmp.SetDirectory(0)
63  fdata.Close()
64  wgt = [htmp.GetBinContent(i+1) for i in range(htmp.GetNbinsX())]
65  if imb=="nom":
66  print "Nominal weights:"
67  for j,iwgt in enumerate(wgt):
68  print "NPV: "+'{:>3d}'.format(j+1),
69  print " Weight: "+'{:>10.3e}'.format(iwgt)
70 
71  print "------> Vector for weight_tools:"
72  print "w_pu_"+imb,
73  print " = vector<double>({"+', '.join('{:.3e}'.format(x) for x in wgt)+"});"
74 
75 
76