Exemplo n.º 1
0
    def build_fit_function(self, fitted, mc_tmplts, x_min, x_max):
        self.x_min, self.x_max = x_min, x_max
        self.fitted = fitted
        self.mc_tmplts = mc_tmplts

        theta_root_wrp = varial.wrappers.Wrapper(
            name="ThetaHistos",
            histo__DATA=fitted.histo,
        )
        self.template_names = []
        for i, tmplt in enumerate(mc_tmplts):
            name = 'template%02d' % (i + 1)
            self.template_names.append(name)
            setattr(theta_root_wrp, 'histo__' + name, tmplt.histo)
        self._store_histos_for_theta(theta_root_wrp)
        theta_auto.config.workdir = varial.analysis.cwd
        self.model = theta_auto.build_model_from_rootfile(
            os.path.join(varial.analysis.cwd, "ThetaHistos.root"),
            include_mc_uncertainties=True
        )
        self.model.set_signal_processes([self.template_names[-1]])
        for tmplt_name in self.template_names[:-1]:
            self.model.get_coeff(
                "histo", tmplt_name).add_factor(
                    'id', parameter='bg_' + tmplt_name)
            self.model.distribution.set_distribution(
                'bg_' + tmplt_name,
                'gauss', 1.0, theta_auto.inf, [0.0, theta_auto.inf]
            )
        self.ndf = fitted.histo.GetNbinsX() - len(self.template_names)
def get_bkg_only_model_flat(input_file=input0H, signal='TpTp_M-*', histogram_filter=lambda w: True):
    model = theta_auto.build_model_from_rootfile(input_file, histogram_filter=histogram_filter, include_mc_uncertainties=True)#,histogram_filter = (lambda s: s.count('jec')==0 and s.count('jer')==0)
    
    model.fill_histogram_zerobins()
    if signal:
        model.set_signal_processes(signal)
    else:
        model.set_signal_process_groups({'':[]})
    
    procs = model.processes
    obsvs = model.observables.keys()
    
    for obs in obsvs:
        if 'isE' in obs:
            try: model.add_lognormal_uncertainty('sfel_trg', math.log(1.05), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfel_id', math.log(1.01), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfel_iso', math.log(1.01), '*', obs)
            except RuntimeError: pass
        elif 'isM' in obs:
            try: model.add_lognormal_uncertainty('sfmu_trg', math.log(1.05), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfmu_id', math.log(1.01), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfmu_iso', math.log(1.01), '*', obs)
            except RuntimeError: pass
        elif 'El45' in obs:
            try: model.add_lognormal_uncertainty('sfel_trg', math.log(1.02), '*', obs)
            except RuntimeError: pass
    try: model.add_lognormal_uncertainty('luminosity', math.log(1.027), '*', '*')
    except RuntimeError: pass

    for obs in obsvs:
        # if 'SignalRegion' in obs:
        try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.80), 'TTbar', obs) # from ttbar CR
        except RuntimeError: pass
        try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.80), 'WJets', obs) # from ttbar CR
        except RuntimeError: pass
        try: model.add_lognormal_uncertainty('QCD_rate',  math.log(2.00), 'QCD', obs) # from ttbar CR
        except RuntimeError: pass
        try: model.add_lognormal_uncertainty('SingleTop_rate',  math.log(1.16), 'SingleTop', obs) # from ttbar CR
        except RuntimeError: pass
        try: model.add_lognormal_uncertainty('Diboson_rate',  math.log(1.15), 'Diboson', obs) # from ttbar CR
        except RuntimeError: pass


    dist_dict = {'mean': 0.0,
                'range': [float('-inf'), float('inf')],
                'typ': 'gauss',
                'width': float('inf')}
    # model.distribution.distributions.update({
    #     'TTbar_rate' : dist_dict,
    #     'WJets_rate' : dist_dict})


    return model
Exemplo n.º 3
0
    def build_fit_function(self, fitted, tmplts, x_min, x_max):
        self.x_min, self.x_max = x_min, x_max
        self.fitted = fitted
        self.tmplts = tmplts

        theta_root_wrp = varial.wrappers.Wrapper(name="ThetaHistos")
        if fitted:
            setattr(theta_root_wrp, 'histo__DATA', fitted.histo)
        self.template_names = []
        signal_procs = []
        for i, tmplt in enumerate(tmplts):
            name = 'template%02d' % (i + 1)
            self.template_names.append(name)
            setattr(theta_root_wrp, 'histo__' + name, tmplt.histo)
            if tmplt.is_signal:
                signal_procs.append(name)
        self._store_histos_for_theta(theta_root_wrp)
        theta_auto.config.workdir = varial.analysis.cwd
        theta_auto.config.report = theta_auto.html_report(os.path.join(
            varial.analysis.cwd, 'theta.html'
        ))
        plt_dir = os.path.join(varial.analysis.cwd, 'plots')
        if not os.path.exists(plt_dir):
            os.mkdir(plt_dir)
        self.model = theta_auto.build_model_from_rootfile(
            os.path.join(varial.analysis.cwd, "ThetaHistos.root"),
            include_mc_uncertainties=True
        )
        self.model.fill_histogram_zerobins()
        self.model.set_signal_processes(signal_procs)
        if self.model_callback:
            self.model_callback(self.model)
        #for tmplt_name in self.template_names[:-1]:
        #    self.model.get_coeff(
        #        "histo", tmplt_name).add_factor(
        #            'id', parameter='bg_' + tmplt_name)
        #    self.model.distribution.set_distribution(
        #        'bg_' + tmplt_name,
        #        'gauss', 1.0, theta_auto.inf, [0.0, theta_auto.inf]
        #    )
        # TODO: get ndf from theta directly
        self.ndf = tmplts[0].histo.GetNbinsX() - len(self.template_names)
Exemplo n.º 4
0
    def build_fit_function(self, fitted, mc_tmplts, x_min, x_max):
        self.x_min, self.x_max = x_min, x_max
        self.fitted = fitted
        self.mc_tmplts = mc_tmplts

        theta_root_wrp = wrp.Wrapper(
            name="ThetaHistos",
            chhadiso__DATA=fitted.histo,
            chhadiso__real=mc_tmplts[1].histo,
            chhadiso__fake=mc_tmplts[0].histo,
        )
        self._store_histos_for_theta(theta_root_wrp)
        theta_auto.config.workdir = settings.dir_result
        self.model = theta_auto.build_model_from_rootfile(
            os.path.join(settings.dir_result, "ThetaHistos.root"),
            include_mc_uncertainties=True
        )
        self.model.set_signal_processes(["real"])
        self.model.add_lognormal_uncertainty("fake_rate", 1., "fake")
        self.model.distribution.set_distribution_parameters(
            'fake_rate',
            width=theta_auto.inf
        )
        self.ndf = fitted.histo.GetNbinsX() - 2
def get_bkg_only_model(input_file=input0H, signal='TpTp_M-*', histogram_filter=lambda w: True):
    model = theta_auto.build_model_from_rootfile(input_file, histogram_filter=histogram_filter, include_mc_uncertainties=True)#,histogram_filter = (lambda s: s.count('jec')==0 and s.count('jer')==0)
    
    model.fill_histogram_zerobins()
    if signal:
        model.set_signal_processes(signal)
    else:
        model.set_signal_process_groups({'':[]})
    
    procs = model.processes
    obsvs = model.observables.keys()

    for obs in obsvs:
        if 'SignalRegion' in obs or 'SidebandRegion' in obs:
            try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.087), 'TTbar', obs) # from ttbar CR
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.06), 'WJets', obs) # from ttbar CR
            except RuntimeError: pass
            # try: model.add_lognormal_uncertainty('DYJets_rate',  math.log(1.2), 'DYJets', obs) # from ttbar CR
            # except RuntimeError: pass
            try: model.add_lognormal_uncertainty('QCD_rate',  math.log(2.00), 'QCD', obs) # from ttbar CR
            except RuntimeError: pass
            # try: model.add_lognormal_uncertainty('SingleTop_rate',  math.log(1.16), 'SingleTop', obs) # from ttbar CR
            # except RuntimeError: pass
            # try: model.add_lognormal_uncertainty('Diboson_rate',  math.log(1.15), 'Diboson', obs) # from ttbar CR
            # except RuntimeError: pass

    for obs in obsvs:
        if 'isE' in obs:
            try: model.add_lognormal_uncertainty('sfel_trg', math.log(1.05), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfel_id', math.log(1.01), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfel_iso', math.log(1.01), '*', obs)
            except RuntimeError: pass
        elif 'isM' in obs:
            try: model.add_lognormal_uncertainty('sfmu_trg', math.log(1.05), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfmu_id', math.log(1.01), '*', obs)
            except RuntimeError: pass
            try: model.add_lognormal_uncertainty('sfmu_iso', math.log(1.01), '*', obs)
            except RuntimeError: pass
        elif 'El45' in obs:
            try: model.add_lognormal_uncertainty('sfel_trg', math.log(1.02), '*', obs)
            except RuntimeError: pass

    try: model.add_lognormal_uncertainty('luminosity', math.log(1.027), '*', '*')
    except RuntimeError: pass

    try: model.add_lognormal_uncertainty('SingleTop_rate',  math.log(1.16), 'SingleTop', '*') # from ttbar CR
    except RuntimeError: pass
    try: model.add_lognormal_uncertainty('Diboson_rate',  math.log(1.15), 'Diboson', '*') # from ttbar CR
    except RuntimeError: pass

    for proc in procs:
        if proc != 'TTbar': continue # and proc != 'SingleTop': continue
        for obs in obsvs:
            if 'nW0_nB0' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.111), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW0_nB1' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.051), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW0_nB2' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.055), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW0_nB3p' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.055), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW1p_nB0' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.111), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW1p_nB1' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.051), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW1p_nB2' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.055), proc, obs) # from ttbar CR
                except RuntimeError: pass
            if 'nW1p_nB3p' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate',math.log(1.055), proc, obs) # from ttbar CR
                except RuntimeError: pass

    for proc in procs:
        if proc != 'WJets': continue #proc != 'DYJets' and 
        for obs in obsvs:
            if 'nW0_nB0' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.182), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW0_nB1' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.182), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW0_nB2' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.182), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW0_nB3p' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.182), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW1p_nB0' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.046), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW1p_nB1' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.046), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW1p_nB2' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.046), proc, obs) # from Wjets CR
                except RuntimeError: pass
            if 'nW1p_nB3p' in obs:
                try: model.add_lognormal_uncertainty('WJets_rate',math.log(1.046), proc, obs) # from Wjets CR
                except RuntimeError: pass


    return model
    def tmp(input_file=input0H):
        print input_file
        model = theta_auto.build_model_from_rootfile(input_file, histogram_filter=histogram_filter, include_mc_uncertainties=True)#,histogram_filter = (lambda s: s.count('jec')==0 and s.count('jer')==0)
        
        model.fill_histogram_zerobins()
        if signal:
            model.set_signal_processes(signal)
        else:
            model.set_signal_process_groups({'':[]})
        
        procs = model.processes
        obsvs = model.observables.keys()

        for obs in obsvs:
            if 'SignalRegion' in obs or 'SidebandRegion' in obs:
                try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.087), 'TTbar', obs) # from ttbar CR
                except RuntimeError: pass
                try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.06), 'WJets', obs) # from ttbar CR
                except RuntimeError: pass
                try: model.add_lognormal_uncertainty('QCD_rate',  math.log(2.00), 'QCD', obs) # from ttbar CR
                except RuntimeError: pass
                # try: model.add_lognormal_uncertainty('SingleTop_rate',  math.log(1.16), 'SingleTop', obs) # from ttbar CR
                # except RuntimeError: pass
                # try: model.add_lognormal_uncertainty('Diboson_rate',  math.log(1.15), 'Diboson', obs) # from ttbar CR
                # except RuntimeError: pass
                # try: model.add_lognormal_uncertainty("btag_bc", math.log(1.03), '*', obs)
                except RuntimeError: pass

        for proc in procs:
            if 'TpTp' in proc or 'BpBp' in proc:
                try: model.add_lognormal_uncertainty("higgs_py2hw", math.log(1.05), proc, '*')
                except RuntimeError:
                    print obs, signal, 'COULD NOT ADD higgs_py2hw UNCERTAINTY'
                    pass

        # for obs in obsvs:
        #     if 'minMlb' in obs:
        #         try: model.add_lognormal_uncertainty("btag_bc", math.log(0.97), '*', obs)
        #         except RuntimeError: pass

        for obs in obsvs:
            if 'isE' in obs:
                try: model.add_lognormal_uncertainty('sfel_trg', math.log(1.05), '*', obs)
                except RuntimeError: pass
                try: model.add_lognormal_uncertainty('sfel_id', math.log(1.01), '*', obs)
                except RuntimeError: pass
                try: model.add_lognormal_uncertainty('sfel_iso', math.log(1.01), '*', obs)
                except RuntimeError: pass
            elif 'isM' in obs:
                try: model.add_lognormal_uncertainty('sfmu_trg', math.log(1.05), '*', obs)
                except RuntimeError: pass
                try: model.add_lognormal_uncertainty('sfmu_id', math.log(1.01), '*', obs)
                except RuntimeError: pass
                try: model.add_lognormal_uncertainty('sfmu_iso', math.log(1.01), '*', obs)
                except RuntimeError: pass
            elif 'El45' in obs:
                try: model.add_lognormal_uncertainty('sfel_trg', math.log(1.02), '*', obs)
                except RuntimeError: pass

        try: model.add_lognormal_uncertainty('luminosity', math.log(1.027), '*', '*')
        except RuntimeError: pass


        try: model.add_lognormal_uncertainty('DYJets_rate',  math.log(1.03), 'DYJets', obs) # from ttbar CR
        except RuntimeError: pass
        try: model.add_lognormal_uncertainty('SingleTop_rate',  math.log(1.16), 'SingleTop', '*') # from ttbar CR
        except RuntimeError: pass
        try: model.add_lognormal_uncertainty('Diboson_rate',  math.log(1.15), 'Diboson', '*') # from ttbar CR
        except RuntimeError: pass

        for proc in procs:
            if proc != 'TTbar': continue # and proc != 'SingleTop': continue
            for obs in obsvs:
                if 'nW0_nB0' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.125), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW0_nB1' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.080), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW0_nB2' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate',  math.log(1.055), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW0_nB3p' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.055), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW1p_nB0' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.125), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW1p_nB1' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.080), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW1p_nB2' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate', math.log(1.055), proc, obs) # from ttbar CR
                    except RuntimeError: pass
                if 'nW1p_nB3p' in obs:
                    try: model.add_lognormal_uncertainty('TTbar_rate',math.log(1.055), proc, obs) # from ttbar CR
                    except RuntimeError: pass

        for proc in procs:
            if proc != 'WJets': continue #proc != 'DYJets' and 
            for obs in obsvs:
                if 'nW0_nB0' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.208), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW0_nB1' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.208), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW0_nB2' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate',  math.log(1.208), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW0_nB3p' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.208), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW1p_nB0' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.038), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW1p_nB1' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.038), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW1p_nB2' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate', math.log(1.038), proc, obs) # from Wjets CR
                    except RuntimeError: pass
                if 'nW1p_nB3p' in obs:
                    try: model.add_lognormal_uncertainty('WJets_rate',math.log(1.038), proc, obs) # from Wjets CR
                    except RuntimeError: pass


        return model
Exemplo n.º 7
0
import os
import theta_auto
theta_auto.config.workdir = os.getcwd()
theta_auto.config.report = theta_auto.html_report(os.path.join(theta_auto.config.workdir, 'index.html'))
theta_auto.config.theta_dir = "/afs/cern.ch/work/h/htholen/private/cmsWorkingDir/CMSSW_5_3_11_patch6/theta"
model = theta_auto.build_model_from_rootfile("ThetaHistos.root", include_mc_uncertainties=True)
model.set_signal_processes(["real"])
model.add_lognormal_uncertainty("fake_rate", 1., "fake")
res = theta_auto.mle(model, "data", 1, chi2=True)
print res
par_values = {"beta_signal":res["real"]["beta_signal"][0][0], "fake_rate":res["real"]["fake_rate"][0][0]}
bkg_val = model.get_coeff("chhadiso", "fake").get_value(par_values)
bkg_err = bkg_val * res["real"]["fake_rate"][0][1] / res["real"]["fake_rate"][0][0]
sig_val = res["real"]["beta_signal"][0][0]
sig_err = res["real"]["beta_signal"][0][1]