Esempio n. 1
0
def test_jec_txt_scalefactors():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets([
        "testJEC * tests/samples/Fall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi.jec.txt",
        "* * tests/samples/Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs.jec.txt.gz",
        "* * tests/samples/Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi.junc.txt",
        "* * tests/samples/Autumn18_V8_MC_UncertaintySources_AK4PFchs.junc.txt",
        "* * tests/samples/Spring16_25nsV10_MC_SF_AK4PFPuppi.jersf.txt"
    ])
    extractor.finalize()

    evaluator = extractor.make_evaluator()

    counts, test_eta, test_pt = dummy_jagged_eta_pt()
    
    jec_out = evaluator['testJECFall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi'](test_eta,test_pt)

    print(evaluator['testJECFall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi'])
    
    jec_out = evaluator['Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs'](test_eta,test_pt)
    
    print(evaluator['Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs'])

    jersf = evaluator['Spring16_25nsV10_MC_SF_AK4PFPuppi']
    
    print(evaluator['Spring16_25nsV10_MC_SF_AK4PFPuppi'])
    
    junc_out = evaluator['Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi'](test_eta,test_pt)

    print(evaluator['Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi'])
    
    assert('Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale' in evaluator.keys())
    junc_out = evaluator['Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale'](test_eta,test_pt)
    print(evaluator['Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale'])
Esempio n. 2
0
def test_root_scalefactors():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets([
        "testSF2d scalefactors_Tight_Electron tests/samples/testSF2d.histo.root"
    ])

    extractor.finalize(reduce_list=["testSF2d"])

    evaluator = extractor.make_evaluator()

    counts, test_eta, test_pt = dummy_jagged_eta_pt()

    # test flat eval
    test_out = evaluator["testSF2d"](test_eta, test_pt)

    # print it
    print(evaluator["testSF2d"])

    # test structured eval
    test_eta_jagged = ak.unflatten(test_eta, counts)
    test_pt_jagged = ak.unflatten(test_pt, counts)
    test_out_jagged = evaluator["testSF2d"](test_eta_jagged, test_pt_jagged)

    assert ak.all(ak.num(test_out_jagged) == counts)
    assert ak.all(ak.flatten(test_out_jagged) == test_out)

    print(test_out)

    diff = np.abs(test_out - _testSF2d_expected_output)
    print("Max diff: %.16f" % diff.max())
    print("Median diff: %.16f" % np.median(diff))
    print("Diff over threshold rate: %.1f %%" %
          (100 * (diff >= 1.0e-8).sum() / diff.size))
    assert (diff < 1.0e-8).all()
Esempio n. 3
0
def test_extractor_exceptions():
    extractor = lookup_tools.extractor()

    # test comments
    extractor.add_weight_sets(
        ["#testSF2d asdfgh tests/samples/testSF2d.histo.root"])

    # test malformed statement
    try:
        extractor.add_weight_sets(
            ["testSF2d testSF2d asdfgh tests/samples/testSF2d.histo.root"])
    except Exception as e:
        assert (
            e.args[0] ==
            '"testSF2d testSF2d asdfgh tests/samples/testSF2d.histo.root" not formatted as "<local name> <name> <weights file>"'
        )

    # test not existant file entry
    try:
        extractor.add_weight_sets(
            ["testSF2d asdfgh tests/samples/testSF2d.histo.root"])
    except Exception as e:
        assert (
            e.args[0] ==
            'Weights named "asdfgh" not in tests/samples/testSF2d.histo.root!')

    # test unfinalized evaluator
    try:
        extractor.add_weight_sets([
            "testSF2d scalefactors_Tight_Electron tests/samples/testSF2d.histo.root"
        ])
        extractor.make_evaluator()
    except Exception as e:
        assert e.args[
            0] == "Cannot make an evaluator from unfinalized extractor!"
Esempio n. 4
0
def evaluator_from_config(cfg):
    """Initiates the SF evaluator and populates it with the right values

    :param cfg: Configuration
    :type cfg: DynaConf object
    :return: Ready-to-use SF evaluator
    :rtype: coffea.lookup_tools.evaluator
    """
    ext = extractor()

    for sfname, definition in cfg.SF.items():
        if not 'file' in definition:
            continue
        fpath = bucoffea_path(definition['file'])

        if fpath.endswith(".root"):
            ext.add_weight_sets(
                [f"{sfname} {definition['histogram']} {fpath}"])
            ext.add_weight_sets(
                [f"{sfname}_error {definition['histogram']}_error {fpath}"])
        else:
            continue

    ext.finalize()

    evaluator = ext.make_evaluator()
    return evaluator
Esempio n. 5
0
    def __init__(self,
        jec_tag,
        jec_tag_data,
        jer_tag,
        do_factorized_jec_unc=False):

        extract = extractor()
        
        #For MC
        extract.add_weight_sets([
            '* * data/jme/{0}_L1FastJet_AK4PFchs.txt'.format(jec_tag),
            '* * data/jme/{0}_L2L3Residual_AK4PFchs.txt'.format(jec_tag),
            '* * data/jme/{0}_L2Relative_AK4PFchs.txt'.format(jec_tag),
            '* * data/jme/{0}_L3Absolute_AK4PFchs.txt'.format(jec_tag),
            '* * data/jme/{0}_UncertaintySources_AK4PFchs.junc.txt'.format(jec_tag),
            '* * data/jme/{0}_Uncertainty_AK4PFchs.junc.txt'.format(jec_tag),
        ])

        if jer_tag:
            extract.add_weight_sets([
            '* * data/jme/{0}_PtResolution_AK4PFchs.jr.txt'.format(jer_tag),
            '* * data/jme/{0}_SF_AK4PFchs.jersf.txt'.format(jer_tag)])
        #For data, make sure we don't duplicate
        tags_done = []
        for run, tag in jec_tag_data.items():
            if not (tag in tags_done):
                extract.add_weight_sets([
                '* * data/jme/{0}_L1FastJet_AK4PFchs.txt'.format(tag),
                '* * data/jme/{0}_L2L3Residual_AK4PFchs.txt'.format(tag),
                '* * data/jme/{0}_L2Relative_AK4PFchs.txt'.format(tag),
                '* * data/jme/{0}_L3Absolute_AK4PFchs.txt'.format(tag)
                ])
                tags_done += [tag]
        
        extract.finalize()
        evaluator = extract.make_evaluator()
        
        jec_names_mc = [
            '{0}_L1FastJet_AK4PFchs'.format(jec_tag),
            '{0}_L2Relative_AK4PFchs'.format(jec_tag),
            '{0}_L2L3Residual_AK4PFchs'.format(jec_tag),
            '{0}_L3Absolute_AK4PFchs'.format(jec_tag),
        ]

        self.jec_mc = FactorizedJetCorrector(**{name: evaluator[name] for name in jec_names_mc})

        self.jec_data = {}
        for run, tag in jec_tag_data.items():
            jec_names_data = [
                '{0}_L1FastJet_AK4PFchs'.format(tag),
                '{0}_L2Relative_AK4PFchs'.format(tag),
                '{0}_L2L3Residual_AK4PFchs'.format(tag),
                '{0}_L3Absolute_AK4PFchs'.format(tag),
            ]
            self.jec_data[run] = FactorizedJetCorrector(**{name: evaluator[name] for name in jec_names_data})
Esempio n. 6
0
    def __init__(self, year=2016):
        self.year = year

        electronSF_2016 = os.path.expandvars("$TWHOME/data/leptons/ElectronScaleFactors_Run2016.root")
        electronReco_2016 = os.path.expandvars("$TWHOME/data/leptons/2016_EGM2D_BtoH_GT20GeV_RecoSF_Legacy2016.root")
        electronRecoLow_2016 = os.path.expandvars("$TWHOME/data/leptons/2016_EGM2D_BtoH_low_RecoSF_Legacy2016.root")
        
        electronSF_2017 = os.path.expandvars("$TWHOME/data/leptons/ElectronScaleFactors_Run2017.root")
        electronReco_2017 = os.path.expandvars("$TWHOME/data/leptons/2017_egammaEffi_txt_EGM2D_runBCDEF_passingRECO.root")
        electronRecoLow_2017 = os.path.expandvars("$TWHOME/data/leptons/2017_egammaEffi_txt_EGM2D_runBCDEF_passingRECO_lowEt.root")
        
        electronSF_2018 = os.path.expandvars("$TWHOME/data/leptons/ElectronScaleFactors_Run2018.root")
        electronReco_2018 = os.path.expandvars("$TWHOME/data/leptons/2018_egammaEffi_txt_EGM2D_updatedAll.root")
        
        muonID_2016 = os.path.expandvars("$TWHOME/data/leptons/2016_Muon_TnP_NUM_LooseID_DENOM_generalTracks_VAR_map_pt_eta.root")
        muonIso_2016 = os.path.expandvars("$TWHOME/data/leptons/2016_Muon_TnP_NUM_MiniIsoTight_DENOM_LooseID_VAR_map_pt_eta.root")
        
        muonID_2017 = os.path.expandvars("$TWHOME/data/leptons/2017_Muon_RunBCDEF_SF_ID.root")
        muonIso_2017 = os.path.expandvars("$TWHOME/data/leptons/Muon_multiIso_2017.root")
        
        muonID_2018 = os.path.expandvars("$TWHOME/data/leptons/2018_Muon_RunABCD_SF_ID.root")
        muonIso_2018 = os.path.expandvars("$TWHOME/data/leptons/Muon_multiIso_2018.root") # same as 2017
        
        self.ext = extractor()
        # several histograms can be imported at once using wildcards (*)
        if self.year == 2016:
            self.ext.add_weight_sets(["mu_2016_id SF %s"%muonID_2016])
            self.ext.add_weight_sets(["mu_2016_iso SF %s"%muonIso_2016])
       
            self.ext.add_weight_sets(["ele_2016_reco EGamma_SF2D %s"%electronReco_2016])
            self.ext.add_weight_sets(["ele_2016_reco_low EGamma_SF2D %s"%electronRecoLow_2016])
            self.ext.add_weight_sets(["ele_2016_id Run2016_MVATightIP2D3DIDEmu %s"%electronSF_2016])
            self.ext.add_weight_sets(["ele_2016_iso Run2016_MultiIsoEmu %s"%electronSF_2016])
        
        elif self.year == 2017:
            self.ext.add_weight_sets(["mu_2017_id NUM_MediumID_DEN_genTracks_pt_abseta %s"%muonID_2017])
            self.ext.add_weight_sets(["mu_2017_iso TnP_MC_NUM_MultiIsoMCut_DEN_MediumID_PAR_pt_eta %s"%muonIso_2017])
       
            self.ext.add_weight_sets(["ele_2017_reco EGamma_SF2D %s"%electronReco_2017])
            self.ext.add_weight_sets(["ele_2017_reco_low EGamma_SF2D %s"%electronRecoLow_2017])
            self.ext.add_weight_sets(["ele_2017_id Run2017_MVATightIP2D3DIDEmu %s"%electronSF_2017])
            self.ext.add_weight_sets(["ele_2017_iso Run2017_MultiIsoEmuJECv32 %s"%electronSF_2017]) # I gess this is the right one

        elif self.year == 2018:
            self.ext.add_weight_sets(["mu_2018_id NUM_MediumID_DEN_TrackerMuons_pt_abseta %s"%muonID_2018])
            self.ext.add_weight_sets(["mu_2018_iso SF2D %s"%muonIso_2018])
       
            self.ext.add_weight_sets(["ele_2018_reco EGamma_SF2D %s"%electronReco_2018])
            self.ext.add_weight_sets(["ele_2018_id Run2018_MVATightIP2D3DIDEmu %s"%electronSF_2018])
            self.ext.add_weight_sets(["ele_2018_iso Run2018_MultiIsoEmu %s"%electronSF_2018])
        
        
        self.ext.finalize()
        
        self.evaluator = self.ext.make_evaluator()
Esempio n. 7
0
 def __init__(self, tag_name):
     """Loads the b-tagging weights and creates the evaluator
     
     Args:
         tag_name (str): The filename to load
     """
     btag_extractor = extractor()
     btag_extractor.add_weight_sets(
         ['* * data/btagSF/{0}.csv'.format(tag_name)])
     btag_extractor.finalize()
     self.evaluator = btag_extractor.make_evaluator()
Esempio n. 8
0
def test_549():
    import awkward as ak
    from coffea.lookup_tools import extractor

    ext = extractor()
    f_in = "tests/samples/SFttbar_2016_ele_pt.root"
    ext.add_weight_sets(["ele_pt histo_eff_data %s" % f_in])
    ext.finalize()
    evaluator = ext.make_evaluator()

    evaluator["ele_pt"](ak.Array([[45]]), )
Esempio n. 9
0
    def __init__(self, year=2016):
        self.year = year

        triggerSF_2016 = os.path.expandvars(
            "$TWHOME/data/trigger/TriggerSF_2016.root")
        triggerSF_2017 = os.path.expandvars(
            "$TWHOME/data/trigger/TriggerSF_2017.root")
        triggerSF_2018 = os.path.expandvars(
            "$TWHOME/data/trigger/TriggerSF_2018.root")

        self.ext = extractor()
        # several histograms can be imported at once using wildcards (*)
        if self.year == 2016:
            self.ext.add_weight_sets([
                "mumu_2016 h2D_SF_mumu_lepABpt_FullError %s" % triggerSF_2016,
                "mumu_2016_error h2D_SF_mumu_lepABpt_FullError_error %s" %
                triggerSF_2016,
                "emu_2016 h2D_SF_emu_lepABpt_FullError %s" % triggerSF_2016,
                "emu_2016_error h2D_SF_emu_lepABpt_FullError_error %s" %
                triggerSF_2016,
                "ee_2016 h2D_SF_ee_lepABpt_FullError %s" % triggerSF_2016,
                "ee_2016_error h2D_SF_ee_lepABpt_FullError_error %s" %
                triggerSF_2016,
            ])

        elif self.year == 2017:
            self.ext.add_weight_sets([
                "mumu_2017 h2D_SF_mumu_lepABpt_FullError %s" % triggerSF_2017,
                "mumu_2017_error h2D_SF_mumu_lepABpt_FullError_error %s" %
                triggerSF_2017,
                "emu_2017 h2D_SF_emu_lepABpt_FullError %s" % triggerSF_2017,
                "emu_2017_error h2D_SF_emu_lepABpt_FullError_error %s" %
                triggerSF_2017,
                "ee_2017 h2D_SF_ee_lepABpt_FullError %s" % triggerSF_2017,
                "ee_2017_error h2D_SF_ee_lepABpt_FullError_error %s" %
                triggerSF_2017,
            ])
        elif self.year == 2018:
            self.ext.add_weight_sets([
                "mumu_2018 h2D_SF_mumu_lepABpt_FullError %s" % triggerSF_2018,
                "mumu_2018_error h2D_SF_mumu_lepABpt_FullError_error %s" %
                triggerSF_2018,
                "emu_2018 h2D_SF_emu_lepABpt_FullError %s" % triggerSF_2018,
                "emu_2018_error h2D_SF_emu_lepABpt_FullError_error %s" %
                triggerSF_2018,
                "ee_2018 h2D_SF_ee_lepABpt_FullError %s" % triggerSF_2018,
                "ee_2018_error h2D_SF_ee_lepABpt_FullError_error %s" %
                triggerSF_2018,
            ])

        self.ext.finalize()

        self.evaluator = self.ext.make_evaluator()
Esempio n. 10
0
def test_histo_json_scalefactors():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets(["testJson * tests/samples/EIDISO_WH_out.histo.json"])
    extractor.finalize()
    
    evaluator = extractor.make_evaluator()
    
    counts, test_eta, test_pt = dummy_jagged_eta_pt()
    
    sf_out = evaluator['testJsonEIDISO_WH/eta_pt_ratio_value'](test_eta, test_pt)
    sf_err_out = evaluator['testJsonEIDISO_WH/eta_pt_ratio_error'](test_eta, test_pt)
    print(sf_out)
    print(sf_err_out)
Esempio n. 11
0
    def __init__(self, year=2016):

        self.year = year

        self.ext = extractor()

        #FIXME these charge flip rates are still for EOY
        fr = os.path.expandvars("$TWHOME/data/chargeflip/ElectronChargeMisIdRates_era%s_2020Feb13.root"%self.year)

        self.ext.add_weight_sets(["el chargeMisId %s"%fr])

        self.ext.finalize()
        
        self.evaluator = self.ext.make_evaluator()
Esempio n. 12
0
def test_btag_csv_scalefactors():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets(["testBTag * tests/samples/testBTagSF.btag.csv"])
    extractor.finalize()

    evaluator = extractor.make_evaluator()

    counts, test_eta, test_pt = dummy_jagged_eta_pt()
    # discriminant used for reshaping, zero otherwise
    test_discr = np.zeros_like(test_eta)

    sf_out = evaluator['testBTagCSVv2_1_comb_up_0'](test_eta, test_pt,
                                                    test_discr)
    print(sf_out)
Esempio n. 13
0
    def prepare_lookups(self):
        # Rochester correction
        rochester_data = txt_converters.convert_rochester_file(
            self.parameters["roccor_file"], loaduncs=True)
        self.roccor_lookup = rochester_lookup.rochester_lookup(rochester_data)

        # JEC, JER and uncertainties
        self.jec_factories, self.jec_factories_data = jec_factories(self.year)

        # Muon scale factors
        self.musf_lookup = musf_lookup(self.parameters)
        # Pile-up reweighting
        self.pu_lookups = pu_lookups(self.parameters)
        # Btag weights
        self.btag_lookup = BTagScaleFactor(
            self.parameters["btag_sf_csv"],
            BTagScaleFactor.RESHAPE,
            "iterativefit,iterativefit,iterativefit",
        )
        # STXS VBF cross-section uncertainty
        self.stxs_acc_lookups, self.powheg_xsec_lookup = stxs_lookups()

        # --- Evaluator
        self.extractor = extractor()

        # Z-pT reweigting (disabled)
        zpt_filename = self.parameters["zpt_weights_file"]
        self.extractor.add_weight_sets([f"* * {zpt_filename}"])
        if "2016" in self.year:
            self.zpt_path = "zpt_weights/2016_value"
        else:
            self.zpt_path = "zpt_weights/2017_value"
        # PU ID weights
        puid_filename = self.parameters["puid_sf_file"]
        self.extractor.add_weight_sets([f"* * {puid_filename}"])
        # Calibration of event-by-event mass resolution
        for mode in ["Data", "MC"]:
            label = f"res_calib_{mode}_{self.year}"
            path = self.parameters["res_calib_path"]
            file_path = f"{path}/{label}.root"
            self.extractor.add_weight_sets([f"{label} {label} {file_path}"])

        self.extractor.finalize()
        self.evaluator = self.extractor.make_evaluator()

        self.evaluator[self.zpt_path]._axes = self.evaluator[
            self.zpt_path]._axes[0]

        return
Esempio n. 14
0
def test_root_scalefactors():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets([
        "testSF2d scalefactors_Tight_Electron tests/samples/testSF2d.histo.root"
    ])

    extractor.finalize(reduce_list=['testSF2d'])

    evaluator = extractor.make_evaluator()

    counts, test_eta, test_pt = dummy_jagged_eta_pt()

    # test flat eval
    test_out = evaluator["testSF2d"](test_eta, test_pt)

    # print it
    print(evaluator["testSF2d"])

    # test structured eval
    test_eta_jagged = ak.unflatten(test_eta, counts)
    test_pt_jagged = ak.unflatten(test_pt, counts)
    test_out_jagged = evaluator["testSF2d"](test_eta_jagged, test_pt_jagged)

    assert ak.all(ak.num(test_out_jagged) == counts)
    assert ak.all(ak.flatten(test_out_jagged) == test_out)

    # From make_expected_lookup.py
    expected_output = np.array([
        0.90780139, 0.82748538, 0.86332178, 0.86332178, 0.97981155, 0.79701495,
        0.88245934, 0.82857144, 0.91884059, 0.97466666, 0.94072163, 1.00775194,
        0.82748538, 1.00775194, 0.97203946, 0.98199672, 0.80655736, 0.90893763,
        0.88245934, 0.79701495, 0.82748538, 0.82857144, 0.91884059, 0.90893763,
        0.97520661, 0.97520661, 0.82748538, 0.91884059, 0.97203946, 0.88245934,
        0.79701495, 0.9458763, 1.00775194, 0.80655736, 1.00775194, 1.00775194,
        0.98976982, 0.98976982, 0.86332178, 0.94072163, 0.80655736, 0.98976982,
        0.96638656, 0.9458763, 0.90893763, 0.9529984, 0.9458763, 0.9529984,
        0.80655736, 0.80655736, 0.80655736, 0.98976982, 0.97466666, 0.98199672,
        0.86332178, 1.03286386, 0.94072163, 1.03398061, 0.82857144, 0.80655736,
        1.00775194, 0.80655736
    ])

    print(test_out)

    diff = np.abs(test_out - expected_output)
    print("Max diff: %.16f" % diff.max())
    print("Median diff: %.16f" % np.median(diff))
    print("Diff over threshold rate: %.1f %%" %
          (100 * (diff >= 1.e-8).sum() / diff.size))
    assert (diff < 1.e-8).all()
Esempio n. 15
0
def test_evaluator_exceptions():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets(["testSF2d scalefactors_Tight_Electron tests/samples/testSF2d.histo.root"])

    counts, test_eta, test_pt = dummy_jagged_eta_pt()
    test_eta_jagged = awkward.JaggedArray.fromcounts(counts, test_eta)
    test_pt_jagged = awkward.JaggedArray.fromcounts(counts, test_pt)

    extractor.finalize()
    evaluator = extractor.make_evaluator()
    
    try:
        test_out = evaluator["testSF2d"](test_pt_jagged, test_eta)
    except Exception as e:
        assert(e.args[0] == 'do not mix JaggedArrays and numpy arrays when calling a derived class of lookup_base')
Esempio n. 16
0
def jetmet_evaluator():
    from coffea.lookup_tools import extractor
    extract = extractor()

    extract.add_weight_sets(['* * tests/samples/Summer16_23Sep2016V3_MC_L1FastJet_AK4PFPuppi.jec.txt.gz',
                             '* * tests/samples/Summer16_23Sep2016V3_MC_L2L3Residual_AK4PFPuppi.jec.txt.gz',
                             '* * tests/samples/Summer16_23Sep2016V3_MC_L2Relative_AK4PFPuppi.jec.txt.gz',
                             '* * tests/samples/Summer16_23Sep2016V3_MC_L3Absolute_AK4PFPuppi.jec.txt.gz',
                             '* * tests/samples/Summer16_23Sep2016V3_MC_UncertaintySources_AK4PFPuppi.junc.txt.gz',
                             '* * tests/samples/Summer16_23Sep2016V3_MC_Uncertainty_AK4PFPuppi.junc.txt.gz',
                             '* * tests/samples/Spring16_25nsV10_MC_PtResolution_AK4PFPuppi.jr.txt.gz',
                             '* * tests/samples/Spring16_25nsV10_MC_SF_AK4PFPuppi.jersf.txt.gz'])
    
    extract.finalize()

    return extract.make_evaluator()
Esempio n. 17
0
def test_jec_txt_effareas():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets(['* * tests/samples/photon_id.ea.txt'])
    extractor.finalize()

    evaluator = extractor.make_evaluator()
    
    counts, test_eta, test_pt = dummy_jagged_eta_pt()

    ch_out = evaluator['photon_id_EA_CHad'](test_eta)
    print(evaluator['photon_id_EA_CHad'])

    nh_out = evaluator['photon_id_EA_NHad'](test_eta)
    print(evaluator['photon_id_EA_NHad'])

    ph_out = evaluator['photon_id_EA_Pho'](test_eta)
    print(evaluator['photon_id_EA_Pho'])
Esempio n. 18
0
def jet_factory_factory(files):
    ext = extractor()
    with contextlib.ExitStack() as stack:
        # this would work even in zipballs but since extractor keys on file extension and
        # importlib make a random tempfile, it won't work. coffea needs to enable specifying the type manually
        # for now we run this whole module as $ python -m boostedhiggs.build_jec boostedhiggs/data/jec_compiled.pkl.gz
        # so the compiled value can be loaded using the importlib tool in corrections.py
        real_files = [
            stack.enter_context(
                importlib.resources.path("boostedhiggs.data", f))
            for f in files
        ]
        ext.add_weight_sets([f"* * {file}" for file in real_files])
        ext.finalize()

    jec_stack = JECStack(ext.make_evaluator())
    return CorrectedJetsFactory(jec_name_map, jec_stack)
Esempio n. 19
0
def test_evaluator_exceptions():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets([
        "testSF2d scalefactors_Tight_Electron tests/samples/testSF2d.histo.root"
    ])

    counts, test_eta, test_pt = dummy_jagged_eta_pt()
    # test_eta_jagged = ak.unflatten(test_eta, counts)
    test_pt_jagged = ak.unflatten(test_pt, counts)

    extractor.finalize()
    evaluator = extractor.make_evaluator()

    try:
        evaluator["testSF2d"](test_pt_jagged, test_eta)
    except Exception as e:
        assert isinstance(e, TypeError)
Esempio n. 20
0
    def test_analyze_function(self):
        import hmumu_utils
        from hmumu_utils import analyze_data, load_puhist_target
        from analysis_hmumu import JetMetCorrections, BTagWeights
        from coffea.lookup_tools import extractor
        NUMPY_LIB = self.NUMPY_LIB
        hmumu_utils.NUMPY_LIB = self.NUMPY_LIB
        hmumu_utils.ha = self.ha

        analysis_parameters = self.analysis_parameters

        puid_maps = "data/puidSF/PUIDMaps.root"
        puid_extractor = extractor()
        puid_extractor.add_weight_sets(["* * {0}".format(puid_maps)])
        puid_extractor.finalize()

        random_seed = 0

        ret = analyze_data(self.dataset,
                           self.analysis_corrections,
                           analysis_parameters["baseline"],
                           "baseline",
                           random_seed,
                           do_fsr=True,
                           use_cuda=False)
        h = ret["hist__dimuon_invmass_z_peak_cat5__M_mmjj"]

        nev_zpeak_nominal = np.sum(h["nominal"].contents)

        if not USE_CUPY:
            self.assertAlmostEqual(nev_zpeak_nominal, 0.0034586303, places=4)

        self.assertTrue("Total__up" in h.keys())
        self.assertTrue("Total__down" in h.keys())
        self.assertTrue("jerB1__up" in h.keys())
        self.assertTrue("jerB1__down" in h.keys())
        self.assertTrue("jerB2__up" in h.keys())
        self.assertTrue("jerB2__down" in h.keys())
        self.assertTrue("jerF1__up" in h.keys())
        self.assertTrue("jerF1__down" in h.keys())
        self.assertTrue("jerF2__up" in h.keys())
        self.assertTrue("jerF2__down" in h.keys())
        self.assertTrue("jerEC1__up" in h.keys())
        self.assertTrue("jerEC1__down" in h.keys())
        self.assertTrue("jerEC2__up" in h.keys())
        self.assertTrue("jerEC2__down" in h.keys())
Esempio n. 21
0
def monojet_evaluator(cfg):
    """Initiates the SF evaluator and populates it with the right values

    :param cfg: Configuration
    :type cfg: DynaConf object
    :return: Ready-to-use SF evaluator
    :rtype: coffea.lookup_tools.evaluator
    """
    ext = extractor()

    for sfname, definition in cfg.SF.items():
        fpath = bucoffea_path(definition['file'])
        ext.add_weight_sets([f"{sfname} {definition['histogram']} {fpath}"])

    ext.finalize()

    evaluator = ext.make_evaluator()
    return evaluator
Esempio n. 22
0
def jet_factory_factory(files):
    from coffea.lookup_tools import extractor
    from coffea.jetmet_tools import JECStack, CorrectedJetsFactory

    ext = extractor()
    ext.add_weight_sets([f"* * {file}" for file in files])
    ext.finalize()

    jec_stack = JECStack(ext.make_evaluator())

    name_map = jec_stack.blank_name_map
    name_map['JetPt'] = 'pt'
    name_map['JetMass'] = 'mass'
    name_map['JetEta'] = 'eta'
    name_map['JetA'] = 'area'

    name_map['ptGenJet'] = 'pt_gen'
    name_map['ptRaw'] = 'pt_raw'
    name_map['massRaw'] = 'mass_raw'
    name_map['Rho'] = 'rho'

    return CorrectedJetsFactory(name_map, jec_stack)
Esempio n. 23
0
    def __init__(self, year=2016):
        self.year = year

        self.ext = extractor()

        fr = os.path.expandvars("$TWHOME/data/fakerate/fr_%s.root" % self.year)
        fr_data = os.path.expandvars(
            "$TWHOME/data/fakerate/fr_%s_recorrected.root" % self.year)

        self.ext.add_weight_sets(["el_QCD FR_mva080_el_QCD %s" % fr])
        self.ext.add_weight_sets(["el_QCD_NC FR_mva080_el_QCD_NC %s" % fr])
        self.ext.add_weight_sets(["el_TT FR_mva080_el_TT %s" % fr])
        self.ext.add_weight_sets(
            ["el_data FR_mva080_el_data_comb_NC_recorrected %s" % fr_data])
        self.ext.add_weight_sets(["mu_QCD FR_mva085_mu_QCD %s" % fr])
        self.ext.add_weight_sets(["mu_TT FR_mva085_mu_TT %s" % fr])
        self.ext.add_weight_sets(
            ["mu_data FR_mva085_mu_data_comb_recorrected %s" % fr_data])

        self.ext.finalize()

        self.evaluator = self.ext.make_evaluator()
Esempio n. 24
0
def jetmet_evaluator():
    from coffea.lookup_tools import extractor

    extract = extractor()

    extract.add_weight_sets([
        "* * tests/samples/Summer16_23Sep2016V3_MC_L1FastJet_AK4PFPuppi.jec.txt.gz",
        "* * tests/samples/Summer16_23Sep2016V3_MC_L2L3Residual_AK4PFPuppi.jec.txt.gz",
        "* * tests/samples/Summer16_23Sep2016V3_MC_L2Relative_AK4PFPuppi.jec.txt.gz",
        "* * tests/samples/Summer16_23Sep2016V3_MC_L3Absolute_AK4PFPuppi.jec.txt.gz",
        "* * tests/samples/Summer16_23Sep2016V3_MC_UncertaintySources_AK4PFPuppi.junc.txt.gz",
        "* * tests/samples/Summer16_23Sep2016V3_MC_Uncertainty_AK4PFPuppi.junc.txt.gz",
        "* * tests/samples/Fall17_17Nov2017_V6_MC_UncertaintySources_AK4PFchs.junc.txt.gz",
        "* * tests/samples/RegroupedV2_Fall17_17Nov2017_V32_MC_UncertaintySources_AK4PFchs.junc.txt.gz",
        "* * tests/samples/Regrouped_Fall17_17Nov2017_V32_MC_UncertaintySources_AK4PFchs.junc.txt",
        "* * tests/samples/Spring16_25nsV10_MC_PtResolution_AK4PFPuppi.jr.txt.gz",
        "* * tests/samples/Spring16_25nsV10_MC_SF_AK4PFPuppi.jersf.txt.gz",
        "* * tests/samples/Autumn18_V7_MC_SF_AK4PFchs.jersf.txt.gz",
    ])

    extract.finalize()

    return extract.make_evaluator()
Esempio n. 25
0
def test_jec_txt_scalefactors():
    extractor = lookup_tools.extractor()
    extractor.add_weight_sets([
        "testJEC * tests/samples/Fall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi.jec.txt",
        "* * tests/samples/Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs.jec.txt.gz",
        "* * tests/samples/Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi.junc.txt",
        "* * tests/samples/Autumn18_V8_MC_UncertaintySources_AK4PFchs.junc.txt",
        "* * tests/samples/Spring16_25nsV10_MC_SF_AK4PFPuppi.jersf.txt",
        "* * tests/samples/Autumn18_V7b_MC_SF_AK8PFchs.jersf.txt.gz",
        "* * tests/samples/Fall17_17Nov2017_V32_MC_L2Relative_AK4Calo.jec.txt.gz",
        "* * tests/samples/Fall17_17Nov2017_V32_MC_L1JPTOffset_AK4JPT.jec.txt.gz",
        "* * tests/samples/Fall17_17Nov2017B_V32_DATA_L2Relative_AK4Calo.txt.gz",
        "* * tests/samples/Autumn18_V7b_DATA_SF_AK4PF.jersf.txt",
        "* * tests/samples/Autumn18_RunC_V19_DATA_L2Relative_AK8PFchs.jec.txt.gz",
        "* * tests/samples/Autumn18_RunA_V19_DATA_L2Relative_AK4Calo.jec.txt",
    ])
    extractor.finalize()

    evaluator = extractor.make_evaluator()

    counts, test_eta, test_pt = dummy_jagged_eta_pt()

    # test structured eval
    test_eta_jagged = ak.unflatten(test_eta, counts)
    test_pt_jagged = ak.unflatten(test_pt, counts)

    jec_out = evaluator[
        "testJECFall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi"](test_eta,
                                                                test_pt)
    jec_out_jagged = evaluator[
        "testJECFall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi"](
            test_eta_jagged, test_pt_jagged)

    print(evaluator["testJECFall17_17Nov2017_V32_MC_L2Relative_AK4PFPuppi"])

    jec_out = evaluator["Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs"](
        test_eta, test_pt)
    jec_out_jagged = evaluator[
        "Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs"](test_eta_jagged,
                                                               test_pt_jagged)
    print(jec_out)
    print(jec_out_jagged)
    print(evaluator["Summer16_07Aug2017_V11_L1fix_MC_L2Relative_AK4PFchs"])

    jersf_out = evaluator["Spring16_25nsV10_MC_SF_AK4PFPuppi"](test_eta,
                                                               test_pt)
    jersf_out_jagged = evaluator["Spring16_25nsV10_MC_SF_AK4PFPuppi"](
        test_eta_jagged, test_pt_jagged)
    print(jersf_out)
    print(jersf_out_jagged)
    print(evaluator["Spring16_25nsV10_MC_SF_AK4PFPuppi"])

    junc_out = evaluator["Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi"](
        test_eta, test_pt)
    junc_out_jagged = evaluator[
        "Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi"](test_eta_jagged,
                                                          test_pt_jagged)
    print(junc_out)
    print(junc_out_jagged)
    print(evaluator["Fall17_17Nov2017_V32_MC_Uncertainty_AK4PFPuppi"])

    assert ("Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale"
            in evaluator.keys())
    junc_out = evaluator[
        "Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale"](test_eta,
                                                                    test_pt)
    junc_out_jagged = evaluator[
        "Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale"](
            test_eta_jagged, test_pt_jagged)
    print(junc_out)
    print(junc_out_jagged)
    print(
        evaluator["Autumn18_V8_MC_UncertaintySources_AK4PFchs_AbsoluteScale"])
Esempio n. 26
0
def save_corrections(year):
    corrections = {}

    # golden json
    if year == '2016':
        corrections[
            'golden'] = '/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions16/13TeV/ReReco/Final/Cert_271036-284044_13TeV_ReReco_07Aug2017_Collisions16_JSON.txt'
    if year == '2017':
        corrections[
            'golden'] = '/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions17/13TeV/ReReco/Cert_294927-306462_13TeV_EOY2017ReReco_Collisions17_JSON_v1.txt'
    if year == '2018':
        corrections[
            'golden'] = '/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions18/13TeV/ReReco/Cert_314472-325175_13TeV_17SeptEarlyReReco2018ABC_PromptEraD_Collisions18_JSON.txt'

    # cross sections
    corrections['xsec'] = xsec
    # manually add the test samples
    corrections['xsec']['DY'] = 6077.22
    corrections['xsec']['HZZ'] = 43.92 * 2.64e-02 * (3.3658e-2 * 3)**2
    corrections['xsec']['DoubleMuon'] = 1.

    extractor = lookup_tools.extractor()
    # electron
    # POG
    if year == '2016':
        extractor.add_weight_sets([
            'electron_id_Veto_ * data/scalefactors/electron/2016/2016_ElectronWPVeto_Fall17V2.root',
            'electron_id_Loose_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronLoose_Fall17V2.root',
            'electron_id_Medium_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronMedium_Fall17V2.root',
            'electron_id_Tight_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronTight_Fall17V2.root',
            'electron_id_MVA80_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronMVA80_Fall17V2.root',
            'electron_id_MVA90_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronMVA90_Fall17V2.root',
            'electron_id_MVA80noiso_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronMVA80noiso_Fall17V2.root',
            'electron_id_MVA90noiso_ * data/scalefactors/electron/2016/2016LegacyReReco_ElectronMVA90noiso_Fall17V2.root',
        ])
    elif year == '2017':
        extractor.add_weight_sets([
            'electron_id_Veto_ * data/scalefactors/electron/2017/2017_ElectronWPVeto_Fall17V2.root',
            'electron_id_Loose_ * data/scalefactors/electron/2017/2017_ElectronLoose.root',
            'electron_id_Medium_ * data/scalefactors/electron/2017/2017_ElectronMedium.root',
            'electron_id_Tight_ * data/scalefactors/electron/2017/2017_ElectronTight.root',
            'electron_id_MVA80_ * data/scalefactors/electron/2017/2017_ElectronMVA80.root',
            'electron_id_MVA90_ * data/scalefactors/electron/2017/2017_ElectronMVA90.root',
            'electron_id_MVA80noiso_ * data/scalefactors/electron/2017/2017_ElectronMVA80noiso.root',
            'electron_id_MVA90noiso_ * data/scalefactors/electron/2017/2017_ElectronMVA90noiso.root',
        ])
    elif year == '2018':
        extractor.add_weight_sets([
            'electron_id_Veto_ * data/scalefactors/electron/2018/2018_ElectronWPVeto_Fall17V2.root',
            'electron_id_Loose_ * data/scalefactors/electron/2018/2018_ElectronLoose.root',
            'electron_id_Medium_ * data/scalefactors/electron/2018/2018_ElectronMedium.root',
            'electron_id_Tight_ * data/scalefactors/electron/2018/2018_ElectronTight.root',
            'electron_id_MVA80_ * data/scalefactors/electron/2018/2018_ElectronMVA80.root',
            'electron_id_MVA90_ * data/scalefactors/electron/2018/2018_ElectronMVA90.root',
            'electron_id_MVA80noiso_ * data/scalefactors/electron/2018/2018_ElectronMVA80noiso.root',
            'electron_id_MVA90noiso_ * data/scalefactors/electron/2018/2018_ElectronMVA90noiso.root',
        ])

    # HZZ
    extractor.add_weight_sets([
        # electron reco
        f'electron_reco_ * data/scalefactors/electron/{year}/Ele_Reco_{year}.root',
        # electron hzz id
        f'electron_hzz_id_nogap_ * data/scalefactors/electron/{year}/ElectronSF_Legacy_{year}_NoGap.root',
        f'electron_hzz_id_gap_ * data/scalefactors/electron/{year}/ElectronSF_Legacy_{year}_Gap.root',
    ])

    # muon
    # POG
    if year == '2016':
        extractor.add_weight_sets([
            # id
            'muon_id_ * data/scalefactors/muon/2016/EfficienciesStudies_2016_legacy_rereco_rootfiles_RunBCDEF_SF_ID.root',
            'muon_id_2_ * data/scalefactors/muon/2016/EfficienciesStudies_2016_legacy_rereco_rootfiles_RunGH_SF_ID.root',
            # iso
            'muon_iso_ * data/scalefactors/muon/2016/EfficienciesStudies_2016_legacy_rereco_rootfiles_RunBCDEF_SF_ISO.root',
            'muon_iso_2_ * data/scalefactors/muon/2016/EfficienciesStudies_2016_legacy_rereco_rootfiles_RunGH_SF_ISO.root',
            # jpsi
            'muon_id_jpsi_ * data/scalefactors/muon/2016/EfficienciesStudies_2016_legacy_rereco_Jpsi_rootfiles_RunBCDEF_SF_ID.root',
            'muon_id_jpsi_2_ * data/scalefactors/muon/2016/EfficienciesStudies_2016_legacy_rereco_Jpsi_rootfiles_RunGH_SF_ID.root',
        ])
    elif year == '2017':
        extractor.add_weight_sets([
            # id
            'muon_id_ * data/scalefactors/muon/2017/EfficienciesStudies_2017_rootfiles_RunBCDEF_SF_ID.root',
            # iso
            'muon_iso_ * data/scalefactors/muon/2017/EfficienciesStudies_2017_rootfiles_RunBCDEF_SF_ISO.root',
            # jpsi
            'muon_id_jpsi_ * data/scalefactors/muon/2017/EfficienciesStudies_2017_rootfiles_RunBCDEF_SF_ID_JPsi.root',
        ])
    elif year == '2018':
        extractor.add_weight_sets([
            # id
            'muon_id_ * data/scalefactors/muon/2018/EfficienciesStudies_2018_rootfiles_RunABCD_SF_ID.root',
            # iso
            'muon_iso_ * data/scalefactors/muon/2018/EfficienciesStudies_2018_rootfiles_RunABCD_SF_ISO.root',
            # jpsi
            'muon_id_jpsi_ * data/scalefactors/muon/2018/EfficienciesStudies_2018_Jpsi_rootfiles_RunABCD_SF_ID.root',
        ])

    extractor.finalize()
    evaluator = extractor.make_evaluator()

    # EGamma POG corrections
    idnums = [
        'Veto',
        'Loose',
        'Medium',
        'Tight',
        'MVA80',
        'MVA90',
        'MVA80noiso',
        'MVA90noiso',
    ]

    for idnum in idnums:
        corrections[f'electron_id_{idnum}'] = evaluator[
            f'electron_id_{idnum}_EGamma_SF2D']

    # HZZ corrections
    corrections['electron_reco'] = evaluator['electron_reco_EGamma_SF2D']
    corrections['electron_hzz_id_nogap'] = evaluator[
        'electron_hzz_id_nogap_EGamma_SF2D']
    corrections['electron_hzz_id_gap'] = evaluator[
        'electron_hzz_id_gap_EGamma_SF2D']

    # Muon POG corrections
    if year == '2016':
        idnums = [
            'LooseID',
            'MediumID',
            'TightID',
            'HighPtID',
        ]
        iddenom = 'genTracks'
        effvars = 'eta_pt'
        highpt_effvars = 'eta_pair_newTuneP_probe_pt'
        iso_num_denoms = [
            ('LooseRelTkIso', 'HighPtIDandIPCut'),
            ('TightRelIso', 'MediumID'),
            ('TightRelIso', 'TightIDandIPCut'),
            ('LooseRelIso', 'LooseID'),
            ('LooseRelIso', 'MediumID'),
            ('LooseRelIso', 'TightIDandIPCut'),
        ]
        jpsinums = [
            'LooseID',
            'MediumID',
            'TightID',
            'SoftID',
        ]
        jpsidenom = 'genTracks'
        jpsieffvars = 'pt_abseta'
    elif year in ['2017', '2018']:
        idnums = [
            'LooseID',
            'MediumID',
            'MediumPromptID',
            'TightID',
            'SoftID',
            'HighPtID',
            'TrkHighPtID',
        ]
        iddenom = 'genTracks'
        if year == '2018':
            iddenom = 'TrackerMuons'
        effvars = 'pt_abseta'
        highpt_effvars = 'pair_newTuneP_probe_pt_abseta'
        iso_num_denoms = [
            ('LooseRelTkIso', 'HighPtIDandIPCut'),
            ('LooseRelTkIso', 'TrkHighPtID'),
            ('TightRelTkIso', 'HighPtIDandIPCut'),
            ('TightRelTkIso', 'TrkHighPtID'),
            ('TightRelIso', 'MediumID'),
            ('TightRelIso', 'TightIDandIPCut'),
            ('LooseRelIso', 'LooseID'),
            ('LooseRelIso', 'MediumID'),
            ('LooseRelIso', 'TightIDandIPCut'),
        ]
        jpsinums = [
            'LooseID',
            'MediumID',
            'TightID',
            'SoftID',
        ]
        jpsidenom = 'genTracks'
        jpsieffvars = 'pt_abseta'

    lumi2016_BCDEF = 19.721 / (16.146 + 19.721)
    lumi2016_GH = 16.146 / (16.146 + 19.721)

    for idnum in idnums:
        histkey = f'NUM_{idnum}_DEN_{iddenom}_{effvars}'
        if idnum in ['HighPtID', 'TrkHighPtID']:
            histkey = f'NUM_{idnum}_DEN_{iddenom}_{highpt_effvars}'
        corrections[f'muon_id_{idnum}'] = evaluator[f'muon_id_{histkey}']
        if year == '2016':
            corrections[f'muon_id_{idnum}']._values *= lumi2016_BCDEF
            corrections[f'muon_id_{idnum}']._values += evaluator[
                f'muon_id_2_{histkey}']._values * lumi2016_GH

    for isonum, isodenom in iso_num_denoms:
        histkey = f'NUM_{isonum}_DEN_{isodenom}_{effvars}'
        if isodenom in ['HighPtIDandIPCut', 'TrkHighPtID']:
            histkey = f'NUM_{isonum}_DEN_{isodenom}_{highpt_effvars}'
        corrections[f'muon_iso_{isonum}_{isodenom}'] = evaluator[
            f'muon_iso_{histkey}']
        if year == '2016':
            corrections[
                f'muon_iso_{isonum}_{isodenom}']._values *= lumi2016_BCDEF
            corrections[f'muon_iso_{isonum}_{isodenom}']._values += evaluator[
                f'muon_iso_2_{histkey}']._values * lumi2016_GH

    for jpsinum in jpsinums:
        histkey = f'NUM_{jpsinum}_DEN_{jpsidenom}_{jpsieffvars}'
        corrections[f'muon_id_jpsi_{jpsinum}'] = evaluator[
            f'muon_id_jpsi_{histkey}']
        if year == '2016':
            corrections[f'muon_id_jpsi_{jpsinum}']._values *= lumi2016_BCDEF
            corrections[f'muon_id_jpsi_{jpsinum}']._values += evaluator[
                f'muon_id_jpsi_2_{histkey}']._values * lumi2016_GH

    # pileup
    # from NanoAOD tools
    # 2016 has a bug
    #with uproot.open(f'data/pileup/dataPileup{year}.root') as f:
    #    norm = lambda x: x/x.sum()
    #    edges = f['pileup'].edges
    #    dataPileup = norm(f['pileup'].values)
    #    dataPileupUp = norm(f['pileup_plus'].values)
    #    dataPileupDown = norm(f['pileup_minus'].values)
    #with uproot.open(f'data/pileup/mcPileup{year}.root') as f:
    #    mcPileup = f['pu_mc'].values
    #def zeropad(a,n):
    #    _a = np.zeros(n)
    #    _a[:len(a)] = a
    #    return _a
    #nmax = max(len(dataPileup),len(mcPileup))
    #dataPileup = zeropad(dataPileup,nmax)
    #mcPileup = zeropad(mcPileup,nmax)
    #mask = (mcPileup>0)
    #pileupRatio = dataPileup.copy()
    #pileupRatioUp = dataPileupUp.copy()
    #pileupRatioDown = dataPileupDown.copy()
    #pileupRatio[mask] /= mcPileup[mask]
    #pileupRatioUp[mask] /= mcPileup[mask]
    #pileupRatioDown[mask] /= mcPileup[mask]
    # from HZZ
    with uproot.open(f'data/pileup/pu_weights_{year}.root') as f:
        edges = f['weights'].edges
        pileupRatio = f['weights'].values
        pileupRatioUp = f['weights_varUp'].values
        pileupRatioDown = f['weights_varDn'].values

    corrections['pileupWeight'] = lookup_tools.dense_lookup.dense_lookup(
        pileupRatio, edges)
    corrections['pileupWeightUp'] = lookup_tools.dense_lookup.dense_lookup(
        pileupRatioUp, edges)
    corrections['pileupWeightDown'] = lookup_tools.dense_lookup.dense_lookup(
        pileupRatioDown, edges)

    # rochester correction
    tag = 'roccor.Run2.v3'
    fname = f'data/rochester/{tag}/RoccoR{year}.txt'
    corrections[
        'rochester_data'] = lookup_tools.txt_converters.convert_rochester_file(
            fname, loaduncs=True)

    save(corrections, f'corrections/corrections_{year}.coffea')
    def __init__(self, year=2016):
        self.year = year

        ele_2016_loose = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loose_ele_2016.root")
        ele_2016_looseTTH = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loosettH_ele_2016.root")
        ele_2016_tight = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_ttH_ele_2016_2lss/passttH/egammaEffi.txt_EGM2D.root"
        )
        ele_2016_reco = os.path.expandvars(
            "$TWHOME/data/leptons/2016_EGM2D_BtoH_GT20GeV_RecoSF_Legacy2016.root"
        )
        ele_2016_reco_low = os.path.expandvars(
            "$TWHOME/data/leptons/2016_EGM2D_BtoH_low_RecoSF_Legacy2016.root")

        ele_2017_loose = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loose_ele_2017.root")
        ele_2017_looseTTH = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loosettH_ele_2017.root")
        ele_2017_tight = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_ttH_ele_2017_2lss/passttH/egammaEffi.txt_EGM2D.root"
        )
        ele_2017_reco = os.path.expandvars(
            "$TWHOME/data/leptons/2017_egammaEffi_txt_EGM2D_runBCDEF_passingRECO.root"
        )
        ele_2017_reco_low = os.path.expandvars(
            "$TWHOME/data/leptons/2017_egammaEffi_txt_EGM2D_runBCDEF_passingRECO_lowEt.root"
        )

        ele_2018_loose = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loose_ele_2018.root")
        ele_2018_looseTTH = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loosettH_ele_2018.root")
        ele_2018_tight = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_ttH_ele_2018_2lss/passttH/egammaEffi.txt_EGM2D.root"
        )
        ele_2018_reco = os.path.expandvars(
            "$TWHOME/data/leptons/2018_egammaEffi_txt_EGM2D_updatedAll.root")

        muon_2016_loose = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loose_muon_2016.root")
        muon_2016_tight = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_ttH_muon_2016_2lss/passttH/egammaEffi.txt_EGM2D.root"
        )

        muon_2017_loose = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loose_muon_2017.root")
        muon_2017_tight = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_ttH_muon_2017_2lss/passttH/egammaEffi.txt_EGM2D.root"
        )

        muon_2018_loose = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_loose_muon_2018.root")
        muon_2018_tight = os.path.expandvars(
            "$TWHOME/data/leptons/ttH/TnP_ttH_muon_2018_2lss/passttH/egammaEffi.txt_EGM2D.root"
        )

        self.ext = extractor()
        # several histograms can be imported at once using wildcards (*)
        if self.year == 2016:
            self.ext.add_weight_sets(
                ["mu_2016_loose EGamma_SF2D %s" % muon_2016_loose])
            self.ext.add_weight_sets(
                ["mu_2016_tight EGamma_SF2D %s" % muon_2016_tight])

            self.ext.add_weight_sets(
                ["ele_2016_reco EGamma_SF2D %s" % ele_2016_reco])
            self.ext.add_weight_sets(
                ["ele_2016_reco_low EGamma_SF2D %s" % ele_2016_reco_low])
            self.ext.add_weight_sets(
                ["ele_2016_loose EGamma_SF2D %s" % ele_2016_loose])
            self.ext.add_weight_sets(
                ["ele_2016_looseTTH EGamma_SF2D %s" % ele_2016_looseTTH])
            self.ext.add_weight_sets(
                ["ele_2016_tight EGamma_SF2D %s" % ele_2016_tight])

        elif self.year == 2017:
            self.ext.add_weight_sets(
                ["mu_2017_loose EGamma_SF2D %s" % muon_2017_loose])
            self.ext.add_weight_sets(
                ["mu_2017_tight EGamma_SF2D %s" % muon_2017_tight])

            self.ext.add_weight_sets(
                ["ele_2017_reco EGamma_SF2D %s" % ele_2017_reco])
            self.ext.add_weight_sets(
                ["ele_2017_reco_low EGamma_SF2D %s" % ele_2017_reco_low])
            self.ext.add_weight_sets(
                ["ele_2017_loose EGamma_SF2D %s" % ele_2017_loose])
            self.ext.add_weight_sets(
                ["ele_2017_looseTTH EGamma_SF2D %s" % ele_2017_looseTTH])
            self.ext.add_weight_sets(
                ["ele_2017_tight EGamma_SF2D %s" % ele_2017_tight])

        elif self.year == 2018:
            self.ext.add_weight_sets(
                ["mu_2018_loose EGamma_SF2D %s" % muon_2018_loose])
            self.ext.add_weight_sets(
                ["mu_2018_tight EGamma_SF2D %s" % muon_2018_tight])

            self.ext.add_weight_sets(
                ["ele_2018_reco EGamma_SF2D %s" % ele_2018_reco])
            self.ext.add_weight_sets(
                ["ele_2018_loose EGamma_SF2D %s" % ele_2018_loose])
            self.ext.add_weight_sets(
                ["ele_2018_looseTTH EGamma_SF2D %s" % ele_2018_looseTTH])
            self.ext.add_weight_sets(
                ["ele_2018_tight EGamma_SF2D %s" % ele_2018_tight])

        self.ext.finalize()

        self.evaluator = self.ext.make_evaluator()
Esempio n. 28
0
    name_map['ptRaw'] = 'pt_raw'
    name_map['massRaw'] = 'mass_raw'
    name_map['Rho'] = 'rho'
        # set name_map keys needed for CorrectedMETFactory
    name_map['METpt'] = 'pt'
    name_map['METphi'] = 'phi'
    name_map['JetPhi'] = 'phi'
    name_map['UnClusteredEnergyDeltaX'] = 'MetUnclustEnUpDeltaX'
    name_map['UnClusteredEnergyDeltaY'] = 'MetUnclustEnUpDeltaY'

    return name_map

years_to_run = ['2016', '2017', '2018'] if base_jobid == 'NanoAODv6' else ['2016APV', '2016', '2017', '2018']
#if args.test: years_to_run = ['2018']

Jetext = extractor()
for dirid in ['jec', 'junc', 'jr', 'jersf']:
    for dtype in ['DATA', 'MC']:
        for year in years_to_run:
            runs = jecfiles[year][dtype] if ( (dtype == 'DATA') and (split_by_era[dirid]) ) else ['']
            for run in runs:
                print(dirid, dtype, year, run)
                directory = os.path.join(proj_dir, 'inputs', 'data', base_jobid, dirid, dtype, year, run)
                for filename in os.listdir(directory):
                    if jet_type not in filename: continue
                    fname = os.path.join(directory, filename)
                    Jetext.add_weight_sets(['* * '+fname])
                    print('%s added to weights' % fname)
Jetext.finalize()
Jetevaluator = Jetext.make_evaluator()
Esempio n. 29
0
#!/usr/bin/env python
import json
import gzip
import uproot
import numexpr
import numpy as np
from coffea import hist, lookup_tools
from coffea.util import load, save
from coffea.hist import plot

corrections = {}

extractor = lookup_tools.extractor()
extractor.add_weight_sets(
    ["2016_n2ddt_ * correction_files/n2ddt_transform_2016MC.root"])
extractor.add_weight_sets(
    ["2017_n2ddt_ * correction_files/n2ddt_transform_2017MC.root"])
extractor.add_weight_sets(
    ["2018_n2ddt_ * correction_files/n2ddt_transform_2018MC.root"])

extractor.add_weight_sets([
    "2016BF_mutrigger_ * correction_files/Muon2016_TriggerEfficienciesAndSF_RunBtoF.root"
])
extractor.add_weight_sets([
    "2016GH_mutrigger_ * correction_files/Muon2016_TriggerEfficienciesAndSF_RunGH.root"
])
extractor.add_weight_sets([
    "2017_mutrigger_ * correction_files/EfficienciesAndSF_RunBtoF_Nov17Nov2017.root"
])
extractor.add_weight_sets([
    "2018_mutrigger_ * correction_files/Muon2018_RunABCD_AfterHLTUpdate_SF_trig.root"
Esempio n. 30
0
    def __init__(self, mcEventYields=None, jetSyst='nominal'):
        ################################
        # INITIALIZE COFFEA PROCESSOR
        ################################

        self.mcEventYields = mcEventYields

        if not jetSyst in ['nominal', 'JERUp', 'JERDown', 'JESUp', 'JESDown']:
            raise Exception(
                f'{jetSyst} is not in acceptable jet systematic types [nominal, JERUp, JERDown, JESUp, JESDown]'
            )

        self.jetSyst = jetSyst

        dataset_axis = hist.Cat("dataset", "Dataset")
        lep_axis = hist.Cat("lepFlavor", "Lepton Flavor")

        systematic_axis = hist.Cat("systematic", "Systematic Uncertainty")

        m3_axis = hist.Bin("M3", r"$M_3$ [GeV]", 200, 0., 1000)
        mass_axis = hist.Bin("mass", r"$m_{\ell\gamma}$ [GeV]", 400, 0., 400)
        pt_axis = hist.Bin("pt", r"$p_{T}$ [GeV]", 200, 0., 1000)
        eta_axis = hist.Bin("eta", r"$\eta_{\gamma}$", 300, -1.5, 1.5)
        chIso_axis = hist.Bin("chIso", r"Charged Hadron Isolation",
                              np.arange(-0.1, 20.001, .05))

        ## Define axis to keep track of photon category
        phoCategory_axis = hist.Bin("category", r"Photon Category",
                                    [1, 2, 3, 4, 5])
        phoCategory_axis.identifiers()[0].label = "Genuine Photon"
        phoCategory_axis.identifiers()[1].label = "Misidentified Electron"
        phoCategory_axis.identifiers()[2].label = "Hadronic Photon"
        phoCategory_axis.identifiers()[3].label = "Hadronic Fake"

        ### Accumulator for holding histograms
        self._accumulator = processor.dict_accumulator({
            # 3. ADD HISTOGRAMS
            ## book histograms for photon pt, eta, and charged hadron isolation
            #'photon_pt':
            #'photon_eta':
            #'photon_chIso':

            ## book histogram for photon/lepton mass in a 3j0t region
            #'photon_lepton_mass_3j0t':

            ## book histogram for M3 variable
            #'M3':
            'EventCount':
            processor.value_accumulator(int)
        })

        ext = extractor()
        ext.add_weight_sets([
            f"btag2016 * {cwd}/ScaleFactors/Btag/DeepCSV_2016LegacySF_V1.btag.csv"
        ])
        ext.finalize()
        self.evaluator = ext.make_evaluator()

        self.ele_id_sf = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/ele_id_sf.coffea')
        self.ele_id_err = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/ele_id_err.coffea')

        self.ele_reco_sf = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/ele_reco_sf.coffea')
        self.ele_reco_err = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/ele_reco_err.coffea')

        self.mu_id_sf = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/mu_id_sf.coffea')
        self.mu_id_err = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/mu_id_err.coffea')

        self.mu_iso_sf = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/mu_iso_sf.coffea')
        self.mu_iso_err = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/mu_iso_err.coffea')

        self.mu_trig_sf = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/mu_trig_sf.coffea')
        self.mu_trig_err = util.load(
            f'{cwd}/ScaleFactors/MuEGammaScaleFactors/mu_trig_err.coffea')