示例#1
0
    def calculate_cls(self, workspace_name):
        """
        returns a dictionary of CLs values
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import Util
        from ROOT import RooStats
        if not isfile(workspace_name):
            raise OSError("can't find workspace {}".format(workspace_name))
        workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined')

        Util.SetInterpolationCode(workspace,4)
        # NOTE: We're completely silencing the fitter. Add an empty string
        # to the accept_strings to get all output.
        with OutputFilter(accept_strings={}):
            limit = RooStats.get_Pvalue(
                workspace,
                True,                   # doUL
                1,                      # n_toys
                2,                      # asymtotic calculator
                3,                      # test type (3 is atlas standard)
                )
        return dict(
            cls=limit.GetCLs(),
            cls_exp=limit.GetCLsexp(),
            cls_up_1_sigma=limit.GetCLsu1S(),
            cls_down_1_sigma=limit.GetCLsd1S(),
            cls_up_2_sigma=limit.GetCLsu2S(),
            cls_down_2_sigma=limit.GetCLsd2S())
    def observed_upper_limit(self, workspace_name):
        """
        returns a 3-tuple of limits
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import Util
        from ROOT import RooStats
        if not isfile(workspace_name):
            raise OSError("can't find workspace {}".format(workspace_name))
        workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined')

        Util.SetInterpolationCode(workspace,4)

        # NOTE: We're completely silencing the fitter. Add an empty string
        # to the accept_strings to get all output.
        with OutputFilter(accept_strings={}):
            inverted = RooStats.DoHypoTestInversion(
                workspace,
                self._n_toys,
                self._calc_type,
                3,                      # test type (3 is atlas standard)
                True,                   # use CLs
                20,                     # number of points
                0,                      # POI min
                -1,
                )

        try:
            return inverted.UpperLimit()
        except ReferenceError:
            return -1
示例#3
0
    def lim_range(self, workspace_name):
        """
        returns a 3-tuple of limits
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import Util
        from ROOT import RooStats
        if not isfile(workspace_name):
            raise OSError("can't find workspace {}".format(workspace_name))
        workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined')

        Util.SetInterpolationCode(workspace,4)
        # NOTE: We're completely silencing the fitter. Add an empty string
        # to the accept_strings to get all output.
        with OutputFilter(accept_strings={}):
            inverted = RooStats.DoHypoTestInversion(
                workspace,
                1,                      # n_toys
                2,                      # asymtotic calculator
                3,                      # test type (3 is atlas standard)
                True,                   # use CLs
                20,                     # number of points
                0,                      # POI min
                -1,                     # POI max (why -1?)
                )

        # one might think that inverted.GetExpectedLowerLimit(-1)
        # would do something different from GetExpectedUpperLimit(-1).
        # This doesn't seem to be true, from what I can tell both
        # functions do exactly the same thing.
        mean_limit = inverted.GetExpectedUpperLimit(0)
        lower_limit = inverted.GetExpectedUpperLimit(-1)
        upper_limit = inverted.GetExpectedUpperLimit(1)
        return lower_limit, mean_limit, upper_limit
def get_fit_results( filename, resultName="RooExpandedFitResult_afterFit"):
    from scharmfit.utils import load_susyfit
    load_susyfit()

    from ROOT import Util, gROOT

    gROOT.Reset()

    workspacename = 'w'
    w = Util.GetWorkspaceFromFile(filename,workspacename)

    if w==None:
        print "ERROR : Cannot open workspace : ", workspacename
        sys.exit(1)

    result = w.obj(resultName)
    if result==None:
        print "ERROR : Cannot open fit result ", resultName
        sys.exit(1)

    # calculate error per parameter on  fitresult
    fpf = result.floatParsFinal()
    fpi = result.floatParsInit()

    regSys = {}

    # set all floating parameters constant
    for idx in range(fpf.getSize()):
        parname = fpf[idx].GetName()
        regSys[parname] = {
            'before': _dict_from_par(fpi[idx]),
            'after': _dict_from_par(fpf[idx]),
            }
    return regSys
def do_upper_limits(verbose=False, prefix='upperlim'):
    from scharmfit import utils
    utils.load_susyfit()
    from ROOT import ConfigMgr, Util
    mgr = ConfigMgr.getInstance()
    mgr.m_outputFileName = prefix + '.root'
    mgr.m_nToys = 1
    mgr.m_calcType = 2
    mgr.m_testStatType = 3
    mgr.m_useCLs = True
    mgr.m_nPoints = -1
    if verbose:
        mgr.doUpperLimitAll()
    else:
        with OutputFilter():
            mgr.doUpperLimitAll()
def _fit_ws(ws_path, output_dir, toys=0):
    """Print some plots, figures out file name by the ws_path"""
    make_dir_if_none(output_dir)
    cfg = ws_path.split('/')[-2]
    out_pfx = join(output_dir, cfg)

    load_susyfit()
    from ROOT import Util, RooStats

    ctype = 0 if toys else 2    # use asymtotic if toys is zero
    test_stat_type = 3
    use_cls = True
    points = 20                 # mu values to use

    ws = Util.GetWorkspaceFromFile(ws_path, 'combined')
    result = RooStats.MakeUpperLimitPlot(
        out_pfx, ws, ctype, test_stat_type, toys, use_cls, points)
示例#7
0
    def do_histfitter_magic(self, input_workspace, verbose=False):
        """
        Here we break into histfitter voodoo. The functions here are pulled
        out of the HistFitter.py script.
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import ConfigMgr, Util
        mgr = ConfigMgr.getInstance()
        mgr.initialize()
        mgr.setNToys(1)

        # the fit configs seem to need unique names, use random numbers
        import random
        fc_number = 0
        # such a hack... but this is to check if the fit config is unique
        with OutputFilter():
            while mgr.getFitConfig(str(fc_number)):
                fc_number += 1
        fc = mgr.addFitConfig(str(fc_number))
        fc.m_inputWorkspaceFileName = input_workspace
        fc.m_signalSampleName = basename(input_workspace).split('.')[0]

        for chan in self.channels:
            if chan == 'signal':
                continue
            fc.m_bkgConstrainChannels.push_back(chan)
        fc.m_signalChannels.push_back('signal')

        accept_strings = {'ERROR:','WARNING:'} if not verbose else {''}
        # this snapshot error appears to be a hackish check, can ignore
        veto = {'snapshot_paramsVals_initial'}

        with OutputFilter(accept_strings=accept_strings, veto_strings=veto):
            Util.GenerateFitAndPlot(
                fc.m_name,
                "ana_name",
                False, #drawBeforeFit,
                False, #drawAfterFit,
                False, #drawCorrelationMatrix,
                False, #drawSeparateComponents,
                False, #drawLogLikelihood,
                False, #runMinos,
                "", #minosPars
                )
    def lim_range(self, workspace_name):
        """
        returns a 3-tuple of limits
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import Util
        from ROOT import RooStats
        if not isfile(workspace_name):
            raise OSError("can't find workspace {}".format(workspace_name))
        workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined')

        Util.SetInterpolationCode(workspace,4)

        # using -1 as the poi max means auto range (I think)
        poi_max = self._prefit_ul(workspace) if self._do_prefit else -1

        # NOTE: We're completely silencing the fitter. Add an empty string
        # to the accept_strings to get all output.
        with OutputFilter(accept_strings={}):
            inverted = RooStats.DoHypoTestInversion(
                workspace,
                self._n_toys,
                self._calc_type,
                3,                      # test type (3 is atlas standard)
                True,                   # use CLs
                20,                     # number of points
                0,                      # POI min
                poi_max,
                )

        try:
            mean_limit = inverted.GetExpectedUpperLimit(0)
            lower_limit = inverted.GetExpectedUpperLimit(-1)
            upper_limit = inverted.GetExpectedUpperLimit(1)
        except ReferenceError:
            return -1, -1, -1
        return lower_limit, mean_limit, upper_limit
    def calculate_cls(self, workspace_name):
        """
        returns a dictionary of CLs values
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import Util
        from ROOT import RooStats
        if not isfile(workspace_name):
            raise OSError("can't find workspace {}".format(workspace_name))
        workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined')

        Util.SetInterpolationCode(workspace,4)
        # NOTE: We're completely silencing the fitter. Add an empty string
        # to the accept_strings to get all output.
        with OutputFilter(accept_strings={}):
            limit = RooStats.get_Pvalue(
                workspace,
                True,                   # doUL
                1,                      # n_toys
                2,                      # asymtotic calculator
                3,                      # test type (3 is atlas standard)
                )
        ws_type = workspace_name.rsplit('_',1)[1].split('.')[0]
        if ws_type == self.nominal:
            return {
                'obs':limit.GetCLs(),
                'exp':limit.GetCLsexp(),
                'exp_u1s':limit.GetCLsu1S(),
                'exp_d1s':limit.GetCLsd1S(),
                }
        elif ws_type == self.up1s:
            return {'obs_u1s':limit.GetCLs()}
        elif ws_type == self.down1s:
            return {'obs_d1s':limit.GetCLs()}
        # should never get here
        raise ValueError('can\'t classify {} as type of limit'.format(
                workspace_name))
def get_corr_matrix( filename, resultName="RooExpandedFitResult_afterFit"):
    """
    Returns (list_of_parameters, matrix) tuple.

    The matrix is a nested list, such that matrix[1][0] will give the
    correlation between parameter 1 and 2.
    """
    from scharmfit.utils import load_susyfit
    load_susyfit()
    from ROOT import Util, gROOT

    result = Util.GetWorkspaceFromFile(filename, 'w').obj(resultName)
    paramenters = result.floatParsFinal()
    n_par = paramenters.getSize()
    all_par_nm = [paramenters[iii].GetName() for iii in xrange(n_par)]
    par_names = all_par_nm
    # par_names = [x for x in all_par_nm if not x.startswith('gamma_stat_')]
    matrix_list = []
    for par1 in par_names:
        matrix_list.append([])
        for par2 in par_names:
            corr = result.correlation(par1, par2)
            matrix_list[-1].append(corr)
    return par_names, matrix_list
示例#11
0
#!/usr/bin/env python2.7
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True
from ROOT import gROOT,gSystem,gDirectory

from scharmfit.utils import load_susyfit
load_susyfit()
# from ROOT import ConfigMgr,FitConfig #this module comes from gSystem.Load("libSusyFitter.so")
gROOT.Reset()

from ROOT import TFile, RooWorkspace, TObject, TString, RooAbsReal, RooRealVar, RooFitResult, RooDataSet, RooAddition, RooArgSet,RooAbsData,RooRandom 
from ROOT import Util, TMath
from ROOT import RooFit
from ROOT import RooExpandedFitResult
    
import os
import sys
from sys import exit

from PrintFitResultTex import *
import pickle


def getnamemap():

  namemap = {}
  namemap['alpha_JSig'] = 'Jet energy scale signal'

  namemap['alpha_BT'] = 'B tagging'
  namemap['alpha_JER'] = 'Jet energy resolution'
  namemap['alpha_JES'] = 'Jet energy scale'
示例#12
0
#!/usr/bin/env python2.7
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True
from ROOT import gROOT, gSystem, gDirectory

from scharmfit.utils import load_susyfit
load_susyfit()
# from ROOT import ConfigMgr,FitConfig #this module comes from gSystem.Load("libSusyFitter.so")
gROOT.Reset()

from ROOT import TFile, RooWorkspace, TObject, TString, RooAbsReal, RooRealVar, RooFitResult, RooDataSet, RooAddition, RooArgSet, RooAbsData, RooRandom
from ROOT import Util, TMath
from ROOT import RooFit
from ROOT import RooExpandedFitResult

import os
import sys
from sys import exit

from PrintFitResultTex import *
import pickle


def getnamemap():

    namemap = {}
    namemap['alpha_JSig'] = 'Jet energy scale signal'

    namemap['alpha_BT'] = 'B tagging'
    namemap['alpha_JER'] = 'Jet energy resolution'
    namemap['alpha_JES'] = 'Jet energy scale'
示例#13
0
    def do_histfitter_magic(self, ws_dir, verbose=False):
        """
        Here we break into histfitter voodoo. The functions here are pulled
        out of the HistFitter.py script. The input workspace is the one
        produced by the `save_workspace` function above.
        """
        from scharmfit import utils
        utils.load_susyfit()
        from ROOT import ConfigMgr, Util

        ws_name = self._get_ws_name()
        ws_path = join(ws_dir, ws_name)

        # The histfitter authors somehow thought that creating a
        # singleton configuration manager was good design. Maybe it is
        # when you're wrapping ROOT code (with all its global variable
        # glory). In any case, most of the hacking below is to work
        # around this.
        mgr = ConfigMgr.getInstance()
        mgr.initialize()
        mgr.setNToys(1)         # make configurable?

        # such a hack... but this is to check if the fit config is unique
        fc_number = 0
        with OutputFilter():
            # name fig configs '0', '1', '2', '3', etc...
            while mgr.getFitConfig(str(fc_number)):
                fc_number += 1
        fc = mgr.addFitConfig(str(fc_number))

        # had to dig pretty deep into the HistFitter code to find this
        # stuff, but this seems to be how it sets things up.
        fc.m_inputWorkspaceFileName = ws_path
        # HistFitter name convention seems to be that the background only fit
        # is called "Bkg" or "" (empty string).
        fc.m_signalSampleName = self._signal_point or ''

        # HistFitter doesn't seem to distinguish between background
        # and signal channels. The only possible difference is a
        # commented out line that sets 'lumiConst' to true if there
        # are no signal channels. May be worth looking into...
        for chan in self._channels:
            if chan not in self._non_fit_regions:
                fc.m_bkgConstrainChannels.push_back(chan)
            # fc.m_signalChannels.push_back(chan)

        accept_strings = {'ERROR:','WARNING:'} if not verbose else {''}
        # this snapshot error appears to be a hackish check, can ignore
        veto = {'snapshot_paramsVals_initial'}

        with OutputFilter(accept_strings=accept_strings, veto_strings=veto):
            Util.GenerateFitAndPlot(
                fc.m_name,
                "ana_name",
                False, #drawBeforeFit,
                False, #drawAfterFit,
                False, #drawCorrelationMatrix,
                False, #drawSeparateComponents,
                False, #drawLogLikelihood,
                False, #runMinos,
                "", #minosPars
                )