def create_readers(self): if self.verbose: print "Setting up btag calibration readers" self.v_sys = getattr(ROOT, 'vector<string>')() for syst in self.iterative_systs: self.v_sys.push_back('up_' + syst) self.v_sys.push_back('down_' + syst) self.reader = BTagCalibrationReader(3, 'central', self.v_sys) self.reader.load(self.calibrator, 0, 'iterativefit') self.reader.load(self.calibrator, 1, 'iterativefit') self.reader.load(self.calibrator, 2, 'iterativefit')
def create_readers(self): if self.verbose > 0: print "Setting up btag calibration readers" self.readers = {} # Create empty vector for systematics. # It would be better to create a single reader, as pointed out here https://hypernews.cern.ch/HyperNews/CMS/get/btag/1341.html , but this would mean reworking this whole module. # For now, adopted the quicker solution. Will implement the full one in a longer timescale. P. v_sys = getattr(ROOT, 'vector<string>')() for wp in [0, 1, 2]: for syst in [ 'central', 'up', 'down', 'up_correlated', 'down_correlated' ]: for flavor in [0, 1, 2]: self.readers[(self.mtypes[flavor], wp, syst, flavor)] = BTagCalibrationReader( wp, syst, v_sys) self.readers[(self.mtypes[flavor], wp, syst, flavor)].load(self.calibrator, flavor, self.mtypes[flavor]) if self.csvfastsim and 'correlated' not in syst: self.readers[('fastsim', wp, syst, flavor)] = BTagCalibrationReader( wp, syst, v_sys) self.readers[('fastsim', wp, syst, flavor)].load(self.calibrator_fastsim, flavor, 'fastsim') allsysts = ["up_%s" % s for s in self.iterative_systs] allsysts += ["down_%s" % s for s in self.iterative_systs] allsysts += ["central"] for syst in allsysts: v_iterativefit_sys = getattr(ROOT, 'vector<string>')() #v_iterativefit_sys.push_back(syst) self.readers[('iterative', syst)] = BTagCalibrationReader( 3, syst, v_iterativefit_sys) self.readers[('iterative', syst)].load( self.calibrator, 0, #..., #flavour 'iterativefit')
def __init__(self, tagger, wp='medium', sigma='central', channel='mutau', year=2017): """Load b tag weights from CSV file.""" print "Loading BTagWeightTool for %s (%s WP)..." % (tagger, wp) assert (year in [2016, 2017, 2018]), "You must choose a year from: 2016, 2017, or 2018." assert (tagger in [ 'CSVv2', 'DeepCSV' ]), "BTagWeightTool: You must choose a tagger from: CSVv2, DeepCSV!" assert (wp in [ 'loose', 'medium', 'tight' ]), "BTagWeightTool: You must choose a WP from: loose, medium, tight!" assert (sigma in [ 'central', 'up', 'down' ]), "BTagWeightTool: You must choose a WP from: central, up, down!" #assert(channel in ['mutau','eletau','tautau','mumu']), "BTagWeightTool: You must choose a channel from: mutau, eletau, tautau, mumu!" # FILE if year == 2016: if 'deep' in tagger.lower(): csvname = path + 'DeepCSV_Moriond17_B_H.csv' effname = path + 'DeepCSV_2016_Moriond17_eff.root' else: csvname = path + 'CSVv2_Moriond17_B_H.csv' effname = path + 'CSVv2_2016_Moriond17_eff.root' elif year == 2017: if 'deep' in tagger.lower(): csvname = path + 'DeepCSV_94XSF_V3_B_F.csv' effname = path + 'DeepCSV_2017_12Apr2017_eff.root' else: csvname = path + 'CSVv2_94XSF_V2_B_F.csv' effname = path + 'CSVv2_2017_12Apr2017_eff.root' elif year == 2018: if 'deep' in tagger.lower(): csvname = path + 'DeepCSV_94XSF_V3_B_F.csv' effname = path + 'DeepCSV_2018_Autumn18_eff.root' else: csvname = path + 'CSVv2_94XSF_V2_B_F.csv' effname = path + 'CSVv2_2018_Autumn18_eff.root' # TAGGING WP self.wp = getattr(BTagWPs(tagger, year), wp) if 'deep' in tagger.lower(): tagged = lambda e, i: e.Jet_btagDeepB[i] > self.wp else: tagged = lambda e, i: e.Jet_btagCSVV2[i] > self.wp # CSV READER op = OP_LOOSE if wp == 'loose' else OP_MEDIUM if wp == 'medium' else OP_TIGHT if wp == 'tight' else OP_RESHAPING type_udsg = 'incl' type_bc = 'comb' # 'mujets' for QCD; 'comb' for QCD+TT calib = BTagCalibration(tagger, csvname) reader = BTagCalibrationReader(op, sigma) reader.load(calib, FLAV_B, type_bc) reader.load(calib, FLAV_C, type_bc) reader.load(calib, FLAV_UDSG, type_udsg) # EFFICIENCIES ptbins = array( 'd', [10, 20, 30, 50, 70, 100, 140, 200, 300, 500, 1000, 1500]) etabins = array('d', [-2.5, -1.5, 0.0, 1.5, 2.5]) bins = (len(ptbins) - 1, ptbins, len(etabins) - 1, etabins) hists = {} effs = {} efffile = ensureTFile(effname) for flavor in [0, 4, 5]: flavor = flavorToString(flavor) histname = "%s_%s_%s" % (tagger, flavor, wp) effname = "%s/eff_%s_%s_%s" % (channel, tagger, flavor, wp) hists[flavor] = TH2F(histname, histname, *bins) hists[flavor + '_all'] = TH2F(histname + '_all', histname + '_all', *bins) effs[flavor] = efffile.Get(effname) hists[flavor].SetDirectory(0) hists[flavor + '_all'].SetDirectory(0) effs[flavor].SetDirectory(0) efffile.Close() self.tagged = tagged self.calib = calib self.reader = reader self.hists = hists self.effs = effs
def __init__(self, tagger, wp='medium', sigma='central', channel='mutau', year=2017, maxeta=2.4): """Load b tag weights from CSV file.""" assert (year in [2016, 2017, 2018]), "You must choose a year from: 2016, 2017, or 2018." assert (tagger in [ 'CSVv2', 'DeepCSV' ]), "BTagWeightTool: You must choose a tagger from: CSVv2, DeepCSV!" assert (wp in [ 'loose', 'medium', 'tight' ]), "BTagWeightTool: You must choose a WP from: loose, medium, tight!" assert (sigma in [ 'central', 'up', 'down' ]), "BTagWeightTool: You must choose a WP from: central, up, down!" #assert(channel in ['mutau','eletau','tautau','mumu']), "BTagWeightTool: You must choose a channel from: mutau, eletau, tautau, mumu!" # FILE if year == 2016: if 'deep' in tagger.lower(): csvname = path + 'DeepCSV_Moriond17_B_H.csv' effname = path + 'DeepCSV_2016_Moriond17_eff.root' else: csvname = path + 'CSVv2_Moriond17_B_H.csv' effname = path + 'CSVv2_2016_Moriond17_eff.root' elif year == 2017: if 'deep' in tagger.lower(): csvname = path + 'DeepCSV_94XSF_V3_B_F.csv' effname = path + 'DeepCSV_2017_12Apr2017_eff.root' else: csvname = path + 'CSVv2_94XSF_V2_B_F.csv' effname = path + 'CSVv2_2017_12Apr2017_eff.root' elif year == 2018: if 'deep' in tagger.lower(): csvname = path + 'DeepCSV_94XSF_V3_B_F.csv' effname = path + 'DeepCSV_2018_Autumn18_eff.root' else: csvname = path + 'CSVv2_94XSF_V2_B_F.csv' effname = path + 'CSVv2_2018_Autumn18_eff.root' # TAGGING WP self.wpname = wp self.wp = getattr(BTagWPs(tagger, year), wp) if 'deep' in tagger.lower(): tagged = lambda e, i: e.Jet_btagDeepB[i] > self.wp else: tagged = lambda e, i: e.Jet_btagCSVV2[i] > self.wp # CSV READER print "Loading BTagWeightTool for %s (%s WP)..." % (tagger, wp) op = OP_LOOSE if wp == 'loose' else OP_MEDIUM if wp == 'medium' else OP_TIGHT if wp == 'tight' else OP_RESHAPING type_udsg = 'incl' type_bc = 'comb' # 'mujets' for QCD; 'comb' for QCD+TT calib = BTagCalibration(tagger, csvname) reader = BTagCalibrationReader(op, sigma) reader.load(calib, FLAV_B, type_bc) reader.load(calib, FLAV_C, type_bc) reader.load(calib, FLAV_UDSG, type_udsg) # EFFICIENCIES hists = {} # histograms to compute the b tagging efficiencies in MC effmaps = { } # b tag efficiencies in MC to compute b tagging weight for an event efffile = ensureTFile(effname) default = False if not efffile: warning( "File %s with efficiency histograms does not exist! Reverting to default efficiency histogram..." % (effname), title="BTagWeightTool") default = True for flavor in [0, 4, 5]: flavor = flavorToString(flavor) histname = "%s_%s_%s" % (tagger, flavor, wp) effname = "%s/eff_%s_%s_%s" % (channel, tagger, flavor, wp) hists[flavor] = createEfficienyMap( histname) # numerator = b tagged jets hists[flavor + '_all'] = createEfficienyMap( histname + '_all') # denominator = all jets if efffile: effmaps[flavor] = efffile.Get(effname) if not effmaps[flavor]: warning( "histogram '%s' does not exist in %s! Reverting to default efficiency histogram..." % (effname, efffile.GetName()), title="BTagWeightTool") default = True effmaps[flavor] = createDefaultEfficiencyMap( effname, flavor, wp) else: effmaps[flavor] = createDefaultEfficiencyMap( effname, flavor, wp) effmaps[flavor].SetDirectory(0) efffile.Close() if default: warning("Made use of default efficiency histograms! The b tag weights from this module should be regarded as placeholders only,\n"+\ "and should NOT be used for analyses. B (mis)tag efficiencies in MC are analysis dependent. Please create your own\n"+\ "efficiency histogram with corrections/btag/getBTagEfficiencies.py after running all MC samples with BTagWeightTool.",title="BTagWeightTool") self.tagged = tagged self.calib = calib self.reader = reader self.hists = hists self.effmaps = effmaps self.maxeta = maxeta
def __init__(self, tagger, jettype, wp='loose', sigma='central', channel='ll', year=2017): """Load b tag weights from CSV file.""" #print "Loading BTagWeightTool for %s (%s WP)..."%(tagger,wp) assert (year in [2016, 2017, 2018]), "You must choose a year from: 2016, 2017, or 2018." assert (tagger in [ 'CSVv2', 'DeepCSV' ]), "BTagWeightTool: You must choose a tagger from: CSVv2, DeepCSV!" assert (wp in [ 'loose', 'medium', 'tight' ]), "BTagWeightTool: You must choose a WP from: loose, medium, tight!" assert (sigma in [ 'central', 'up', 'down' ]), "BTagWeightTool: You must choose a WP from: central, up, down!" self.jettype = jettype # FILE if year == 2016: csvname = path + 'DeepCSV_2016LegacySF_V1.csv' effname = path + 'DeepCSV_%s_2016_eff.root' % jettype elif year == 2017: csvname = path + 'DeepCSV_94XSF_V5_B_F.csv' effname = path + 'DeepCSV_%s_2017_eff.root' % jettype elif year == 2018: csvname = path + 'DeepCSV_102XSF_V1.csv' effname = path + 'DeepCSV_%s_2018_eff.root' % jettype # TAGGING WP self.wp = getattr(BTagWPs(tagger, year), wp) # CSV READER op = OP_LOOSE if wp == 'loose' else OP_MEDIUM if wp == 'medium' else OP_TIGHT if wp == 'tight' else OP_RESHAPING type_udsg = 'incl' type_bc = 'comb' # 'mujets' for QCD; 'comb' for QCD+TT calib = BTagCalibration(tagger, csvname) reader = BTagCalibrationReader(op, sigma) reader.load(calib, FLAV_B, type_bc) reader.load(calib, FLAV_C, type_bc) reader.load(calib, FLAV_UDSG, type_udsg) self.sigma = sigma # EFFICIENCIES ptbins = array( 'd', [10, 20, 30, 50, 70, 100, 140, 200, 300, 500, 1000, 1500]) etabins = array('d', [-2.5, -1.5, 0.0, 1.5, 2.5]) bins = (len(ptbins) - 1, ptbins, len(etabins) - 1, etabins) hists = {} effs = {} if tagger in ['CSVv2', 'DeepCSV']: efffile = ensureTFile(effname) for flavor in [0, 4, 5]: flavor = flavorToString(flavor) histname = "%s_%s_%s" % (tagger, flavor, wp) effname = "%s/eff_%s_%s_%s" % (channel, tagger, flavor, wp) hists[flavor] = TH2F(histname, histname, *bins) hists[flavor + '_all'] = TH2F(histname + '_all', histname + '_all', *bins) effs[flavor] = efffile.Get(effname) hists[flavor].SetDirectory(0) hists[flavor + '_all'].SetDirectory(0) effs[flavor].SetDirectory(0) efffile.Close() else: efffile = ensureTFile(effname) for flavor in [0, 4, 5]: flavor = flavorToString(flavor) histname = "%s_%s_%s" % (tagger, flavor, wp) effname = "%s/eff_%s_%s_%s" % (channel, tagger, flavor, wp) hists[flavor] = TH2F(histname, histname, *bins) hists[flavor + '_all'] = TH2F(histname + '_all', histname + '_all', *bins) #effs[flavor] = efffile.Get(effname) hists[flavor].SetDirectory(0) hists[flavor + '_all'].SetDirectory(0) #effs[flavor].SetDirectory(0) efffile.Close() self.calib = calib self.reader = reader self.hists = hists self.effs = effs
class BTagCSVFullShape(object): def __init__(self, csvfile, verbose=False): self.csvfile = csvfile self.verbose = True #verbose self.algo = 'csvv2' self.iterative_systs = [ 'jes', 'lf', 'hf', 'hfstats1', 'hfstats2', 'lfstats1', 'lfstats2', 'cferr1', 'cferr2' ] self.init() self.create_readers() def init(self): if self.verbose: print "Initializing btag calibrator from %s" % self.csvfile self.calibrator = BTagCalibration(self.algo, self.csvfile) def create_readers(self): if self.verbose: print "Setting up btag calibration readers" self.v_sys = getattr(ROOT, 'vector<string>')() for syst in self.iterative_systs: self.v_sys.push_back('up_' + syst) self.v_sys.push_back('down_' + syst) self.reader = BTagCalibrationReader(3, 'central', self.v_sys) self.reader.load(self.calibrator, 0, 'iterativefit') self.reader.load(self.calibrator, 1, 'iterativefit') self.reader.load(self.calibrator, 2, 'iterativefit') def get_SF(self, pt, eta, flavor, val, syst='central'): fl_index = min(-flavor + 5, 2) """ Note the flavor convention: hadronFlavor is b=5, c=4, f=0 Convert them to the btagging group convention of 0,1,2 """ syst = syst.lower() return self.reader.eval_auto_bounds( syst if self.relevant_iterative_systs(fl_index, syst) else 'central', fl_index, eta, pt, val) def relevant_iterative_systs(self, flavor, syst): """Returns true if a flavor/syst combination is relevant""" if flavor == 0: return syst in [ "central", "up_jes", "down_jes", "up_lf", "down_lf", "up_hfstats1", "down_hfstats1", "up_hfstats2", "down_hfstats2" ] elif flavor == 1: return syst in [ "central", "up_cferr1", "down_cferr1", "up_cferr2", "down_cferr2" ] elif flavor == 2: return syst in [ "central", "up_jes", "down_jes", "up_hf", "down_hf", "up_lfstats1", "down_lfstats1", "up_lfstats2", "down_lfstats2" ] return True
def __init__(self, tagger='DeepCSV', wp='medium', sigmabc='central', sigmalight='central', channel='ttbar', year=2016): """Load b tag weights from CSV file.""" assert(year in [2016,2017,2018]), "You must choose a year from: 2016, 2017, or 2018." assert(tagger in ['CSVv2','DeepCSV']), "BTagWeightTool: You must choose a tagger from: CSVv2, DeepCSV!" assert(wp in ['loose','medium','tight']), "BTagWeightTool: You must choose a WP from: loose, medium, tight!" assert(sigmabc in ['central','up_correlated','down_correlated','up_uncorrelated','down_uncorrelated']), "BTagWeightTool: You must choose a WP for b/c jets from: central, up_correlated, down_correlated, up_uncorrelated, down_uncorrelated!" assert(sigmalight in ['central','up_correlated','down_correlated','up_uncorrelated','down_uncorrelated']), "BTagWeightTool: You must choose a WP for light jets from: central, up_correlated, down_correlated, up_uncorrelated, down_uncorrelated!" #assert(channel in ['mutau','eletau','tautau','mumu']), "BTagWeightTool: You must choose a channel from: mutau, eletau, tautau, mumu!" # FILE if year==2016: if 'deep' in tagger.lower(): csvnamebc = path+'DeepCSV_2016LegacySF_V1_YearCorrelation-V1.csv' csvnamelight = path+'DeepCSV_2016LegacySF_WP_V1.csv' effname = path+'DeepCSV_2016_Moriond17_eff.root' else: csvnamebc = path+'CSVv2_Moriond17_B_H.csv' csvnamelight = path+'CSVv2_Moriond17_B_H.csv' effname = path+'CSVv2_2016_Moriond17_eff.root' elif year==2017: if 'deep' in tagger.lower(): csvnamebc = path+'DeepCSV_94XSF_V4_B_F_YearCorrelation-V1.csv' csvnamelight = path+'DeepCSV_94XSF_WP_V4_B_F.csv' effname = path+'DeepCSV_2017_Fall17_eff.root' else: csvnamebc = path+'CSVv2_Moriond17_B_H.csv' csvnamelight = path+'CSVv2_Moriond17_B_H.csv' effname = path+'CSVv2_2017_Fall17_eff.root' elif year==2018: if 'deep' in tagger.lower(): csvnamebc = path+'DeepCSV_102XSF_V1_YearCorrelation-V1.csv' csvnamelight = path+'DeepCSV_102XSF_WP_V1.csv' effname = path+'DeepCSV_2018_Autumn18_eff.root' else: csvnamebc = path+'CSVv2_94XSF_V2_B_F.csv' csvnamelight = path+'CSVv2_94XSF_V2_B_F.csv' effname = path+'CSVv2_2018_Autumn18_eff.root' # TAGGING WP self.wpname = wp self.wp = getattr(BTagWPs(tagger,year),wp) if 'deep' in tagger.lower(): tagged = lambda j: j.btagDeepB>self.wp else: tagged = lambda j: j.btagCSVV2>self.wp # CSV READER print "Loading BTagWeightTool for %s (%s WP)..."%(tagger,wp) op = OP_LOOSE if wp=='loose' else OP_MEDIUM if wp=='medium' else OP_TIGHT if wp=='tight' else OP_RESHAPING type_udsg = 'incl' type_bc = 'mujets' # 'mujets' for QCD; 'comb' for QCD+TT #Load reader for b/c jets calibbc = BTagCalibration(tagger, csvnamebc) readerbc = BTagCalibrationReader(op, sigmabc) readerbc.load(calibbc, FLAV_B, type_bc) readerbc.load(calibbc, FLAV_C, type_bc) #Load reader for light jets caliblight = BTagCalibration(tagger, csvnamelight) readerlight = BTagCalibrationReader(op, sigmalight) readerlight.load(caliblight, FLAV_UDSG, type_udsg) # EFFICIENCIES effmaps = { } # b tag efficiencies in MC to compute b tagging weight for an event efffile = ensureTFile(effname) default = False if not efffile: warning("File %s with efficiency histograms does not exist! Reverting to default efficiency histogram..."%(effname),title="BTagWeightTool") default = True for flavor in [0,4,5]: flavor = flavorToString(flavor) histname = "%s_%s_%s"%(tagger,flavor,wp) effname = "%s/eff_%s_%s_%s"%(channel,tagger,flavor,wp) if efffile: effmaps[flavor] = efffile.Get(effname) if not effmaps[flavor]: warning("histogram '%s' does not exist in %s! Reverting to default efficiency histogram..."%(effname,efffile.GetName()),title="BTagWeightTool") default = True effmaps[flavor] = createDefaultEfficiencyMap(effname,flavor,wp) else: effmaps[flavor] = createDefaultEfficiencyMap(effname,flavor,wp) effmaps[flavor].SetDirectory(0) efffile.Close() if default: warning("Made use of default efficiency histograms! The b tag weights from this module should be regarded as placeholders only,\n"+\ "and should NOT be used for analyses. B (mis)tag efficiencies in MC are analysis dependent. Please create your own\n"+\ "efficiency histogram with corrections/btag/getBTagEfficiencies.py after running all MC samples with BTagWeightTool.",title="BTagWeightTool") self.tagged = tagged self.calibbc = calibbc self.readerbc = readerbc self.caliblight = caliblight self.readerlight = readerlight self.effmaps = effmaps