babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
get_sig_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 
28 mcfile.Add("/net/cms29/cms29r0/babymaker/babies/2017_02_07/T1tttt/unprocessed/fullbaby_SMS-T1tttt_mGluino-*.root");
29 print mcfile.GetEntries()
30 hmc = TH1D("hmc","hmc",75,0,75)
31 mcfile.Draw("ntrupv_mean>>hmc","","norm")
32 
33 # To reweight both the in- and out-of-time pile up
34 # pileupCalc should be used in mode "true" and the variable to reweight is npvtru_mean:
35 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookExercisesHATS_Pileup_2013
36 calc_mode = "true"
37 
38 # ----------------------------------------------------------
39 
40 mbxsec_dict = {
41  "nom": mbxsec#,
42 # "up": mbxsec*(1+mbxsec_relunc),
43 # "down": mbxsec*(1-mbxsec_relunc)
44 }
45 
46 for imb in mbxsec_dict:
47  cmd = "pileupCalc.py"
48  cmd += " -i "+json
49  cmd += " --inputLumiJSON "+lumi_json
50  cmd += " --calcMode "+calc_mode
51  cmd += " --minBiasXsec "+str(mbxsec_dict[imb])
52  cmd += " --maxPileupBin "+str(hmc.GetNbinsX())
53  cmd += " --numPileupBins "+str(hmc.GetNbinsX())
54  cmd += " sig_pileup_"+imb+".root"
55 
56  print "Obtaining data pile up distribution for variation:", imb
57  print cmd
58  os.system(cmd)
59 
60  fdata = TFile("sig_pileup_"+imb+".root","update")
61  htmp = fdata.Get("pileup").Clone("norm_data_"+imb)
62  htmp.Scale(1./htmp.Integral())
63  htmp.SetDirectory(0)
64  htmp.Write()
65  hmc.Write()
66  fdata.Close()
67  wgt = [htmp.GetBinContent(i+1) for i in range(htmp.GetNbinsX())]
68  if imb=="nom":
69  print "Data PU distribution:"
70  for j,iwgt in enumerate(wgt):
71  print "NPV: "+'{:>3d}'.format(j+1),
72  print "Density: "+'{:>10.3e}'.format(iwgt)
73 
74  print "------> Vector for syscalc_scan:"
75  print " vector<double>({"+', '.join('{:.3e}'.format(x) for x in wgt)+"});"
76 
77  print "Weighted centers of signal tru npv bins:"
78  hmc.SetAxisRange(0,20)
79  print "0 to 20: %.3f" % hmc.GetMean()
80  hmc.SetAxisRange(21,100)
81  print "21+: %.3f" % hmc.GetMean()
82 
83 
84