Exemple #1
0
    def init(self):
        if self.verbose > 0:
            print "Initializing btag calibrator from %s" % self.csvfile
        self.allowed = get_allowed_ranges(self.csvfile)
        self.calibrator = BTagCalibration(self.name, self.csvfile)

        if self.csvfastsim:
            if self.verbose > 0:
                print "Initializing fastsim btag calibrator from %s" % self.csvfastsim
            self.calibrator_fastsim = BTagCalibration("%s_fastsim" % self.name,
                                                      self.csvfastsim)
            self.allowed.update(get_allowed_ranges(self.csvfastsim))

        if self.eff_rootfile:
            if self.verbose > 0:
                print "Reading tagging efficiencies from %s" % self.eff_rootfile
            self.load_tagging_efficiencies(self.eff_rootfile)
Exemple #2
0
    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
 def init(self):
     if self.verbose:
         print "Initializing btag calibrator from %s" % self.csvfile
     self.calibrator = BTagCalibration(self.algo, self.csvfile)
Exemple #6
0
 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