1 from __future__
import print_function
7 Calculates the jet and event correction factor as a weight based on the b-tagger shape-dependent data/mc 10 Currently, the recipe is only described in https://twiki.cern.ch/twiki/bin/viewauth/CMS/TTbarHbbRun2ReferenceAnalysis#Applying_CSV_weights 12 In short, jet-by-jet correction factors as a function of pt, eta and CSV have been derived. 13 This code accesses the flavour of MC jets and gets the correct weight histogram 14 corresponding to the pt, eta and flavour of the jet. 15 From there, the per-jet weight is just accessed according to the value of the discriminator. 32 self.
btag =
"pfCombinedInclusiveSecondaryVertexV2BJetTags" 33 self.
init(fn_hf, fn_lf)
37 "HFStats1Up",
"HFStats1Down",
"HFStats2Up",
"HFStats2Down"]
40 "LFStats1Up",
"LFStats1Down",
"LFStats2Up",
"LFStats2Down"]
43 return int(bvec.searchsorted(val, side=
"right")) - 1
45 def init(self, fn_hf, fn_lf):
47 fn_hf (string) - path to the heavy flavour weight file 48 fn_lf (string) - path to the light flavour weight file 50 print(
"[BTagWeightCalculator]: Initializing from files", fn_hf, fn_lf)
61 Initialized the lookup table for b-tag weight histograms based on jet 63 The format of the weight file is similar to: 64 KEY: TH1D csv_ratio_Pt0_Eta0_final;1 65 KEY: TH1D csv_ratio_Pt0_Eta0_final_JESUp;1 66 KEY: TH1D csv_ratio_Pt0_Eta0_final_JESDown;1 67 KEY: TH1D csv_ratio_Pt0_Eta0_final_LFUp;1 68 KEY: TH1D csv_ratio_Pt0_Eta0_final_LFDown;1 69 KEY: TH1D csv_ratio_Pt0_Eta0_final_Stats1Up;1 70 KEY: TH1D csv_ratio_Pt0_Eta0_final_Stats1Down;1 71 KEY: TH1D csv_ratio_Pt0_Eta0_final_Stats2Up;1 72 KEY: TH1D csv_ratio_Pt0_Eta0_final_Stats2Down;1 73 KEY: TH1D c_csv_ratio_Pt0_Eta0_final;2 74 KEY: TH1D c_csv_ratio_Pt0_Eta0_final;1 75 KEY: TH1D c_csv_ratio_Pt0_Eta0_final_cErr1Up;1 76 KEY: TH1D c_csv_ratio_Pt0_Eta0_final_cErr1Down;1 77 KEY: TH1D c_csv_ratio_Pt0_Eta0_final_cErr2Up;1 78 KEY: TH1D c_csv_ratio_Pt0_Eta0_final_cErr2Down;1 82 if not tf
or tf.IsZombie():
83 raise FileNotFoundError(
"Could not open file {0}".
format(fn))
85 for k
in tf.GetListOfKeys():
87 if not (kn.startswith(
"csv_ratio")
or kn.startswith(
"c_csv_ratio") ):
90 is_c = 1
if kn.startswith(
"c_csv_ratio")
else 0
92 if spl[2+is_c] ==
"all":
98 ptbin =
int(spl[2+is_c][2:])
99 etabin =
int(spl[3+is_c][3:])
101 if len(spl)==(6+is_c):
105 ret[(is_c, ptbin, etabin, kind, syst)] = k.ReadObj().Clone()
110 Calculates the per-jet correction factor. 111 jet: either an object with the attributes pt, eta, mcFlavour, self.btag 113 kind: string specifying the name of the corrections. Usually "final". 114 systematic: the correction systematic, e.g. "nominal", "JESUp", etc 117 if isinstance(getattr(jet,
"pt"), float):
118 pt = getattr(jet,
"pt")
119 aeta =
abs(getattr(jet,
"eta"))
120 fl =
abs(getattr(jet,
"hadronFlavour"))
121 csv = getattr(jet, self.
btag)
126 aeta =
abs(jet.eta())
127 fl =
abs(jet.hadronFlavour())
128 csv = jet.btag(self.
btag)
135 is_l =
not (is_b
or is_c)
138 if systematic !=
"nominal":
140 systematic =
"nominal" 143 if "Stats" in systematic:
144 systematic = systematic[2:]
153 if ptbin < 0
or etabin < 0:
157 k = (is_c, ptbin, etabin, kind, systematic)
158 hdict = self.
pdfs[
"lf"]
160 hdict = self.
pdfs[
"hf"]
161 h = hdict.get(k,
None)
170 csvbin = h.FindBin(csv)
174 if csvbin > h.GetNbinsX():
175 csvbin = h.GetNbinsX()
177 w = h.GetBinContent(csvbin)
182 The per-event weight is just a product of per-jet weights. 189 wtot = np.prod(weights)
def getBin(self, bvec, val)
def init(self, fn_hf, fn_lf)
S & print(S &os, JobReport::InputFile const &f)
Abs< T >::type abs(const T &t)
def getHistosFromFile(self, fn)
def calcJetWeight(self, jet, kind, systematic)
def calcJetWeightImpl(self, pt, aeta, fl, csv, kind, systematic)
def __init__(self, fn_hf, fn_lf)
def calcEventWeight(self, jets, kind, systematic)