Esempio n. 1
0
def getQuantiles(limits, median, onesigma, twosigma):

    nit = len(limits)
    if nit==0:
        return

    # sort the list with limits
    limits = sorted(limits)
    limits_array = array('d',limits)

    # median for the expected limit
    median[0] = TMath.Median(nit, limits_array)

    # quantiles for the expected limit bands
    prob = array('d',[]) # array with quantile boundaries
    prob.append(0.021)
    prob.append(0.159)
    prob.append(0.841)
    prob.append(0.979)

    # array for the results

    quantiles = array('d',[0., 0., 0., 0.])
    TMath.Quantiles(nit, 4, limits_array, quantiles, prob) # evaluate quantiles

    onesigma[0] = quantiles[1]
    onesigma[1] = quantiles[2]
    twosigma[0] = quantiles[0]
    twosigma[1] = quantiles[3]

    return
Esempio n. 2
0
    def getMedian(self, h):
        #compute the median for 1-d histogram h1
        nbins = h.GetXaxis().GetNbins()
        xList = []
        yList = []
        for i in range(0, nbins):
            xList.append(h.GetBinCenter(i + 1))
            yList.append(h.GetBinContent(i + 1))

        return TMath.Median(len(xList), np.array(xList, 'd'),
                            np.array(yList, 'd'))
Esempio n. 3
0
def getMedian(h):
    #compute the median for 1-d histogram h1
    nbins = h.GetXaxis().GetNbins()
    xList = []
    yList = []
    for i in range(0, nbins):
        xList.append(h.GetBinCenter(i + 1))
        yList.append(h.GetBinContent(i + 1))
    # For update to ROOT 6, explicit type is needed. Seems to be in connection with
    # only templated method available in ROOT 6. Write:
    #
    # return TMath.Median('double')(len(xList),np.array(xList,'d'),np.array(yList,'d'))
    #
    return TMath.Median(len(xList), np.array(xList, 'd'), np.array(yList, 'd'))
Esempio n. 4
0
    def add_expected(self, cat, filename, legend, scale = 1.0,
                     line_color = 1, line_style = 1, line_width = 1,
                     marker_color = 1, marker_size = 1.2, marker_style=8,
                     value_type = 'median',
                     error_type = 'rms',
                     fill_style = 1002,
                     fill_color = ROOT.kYellow,
                     fill2_style = 1002,
                     fill2_color = ROOT.kRed,
                     extra_scale_name = 'unit',
                     smooth = None # 'single' or 'comb' or None: special tricks for smoothing expected bands
                     ):

        self.type[cat] = 'expected'

        self.cat.append(cat)
        self.file = filename
        self.tlegend[cat] = legend
        self.line_color[cat] = line_color
        self.line_style[cat] = line_style
        self.line_width[cat] = line_width
        self.marker_color[cat] = marker_color
        self.marker_size[cat] = marker_size
        self.marker_style[cat] = marker_style
        self.fill_style[cat] = fill_style
        self.fill2_style[cat] = fill2_style
        self.fill_color[cat] = fill_color
        self.fill2_color[cat] = fill2_color

        _x  = array('d')
        _y  = array('d')
        _exh = array('d')
        _exl = array('d')
        _eyh = array('d')
        _eyl = array('d')
        _exh2 = array('d')
        _exl2 = array('d')
        _eyh2 = array('d')
        _eyl2 = array('d')
        _d = [] # list of tuples, one tuple per mass point
        with open(filename, 'r') as _file:
            _mass = None
            _arr = array('d')
            _prev_limit = 0.0
            for line in _file:
                #print self.legend, line

                _buf = line.split()

                aMass = float(_buf[0])
                aLim = float(_buf[1])*scale*get_scale(extra_scale_name, aMass)

                if _mass == None:
                    _mass = aMass
                
                if abs(_mass-aMass) > 0.1:
                    _d.append((_mass, _arr))
                    #print self.legend, cat, 'mass =', _mass, 'PEs per point:', len(_arr)
                    _mass = aMass
                    _arr=array('d')

                    
                #if abs(aLim - _prev_limit) < 0.000001:
                #    #print self.legend, 'WARNING: duplicate expected limit, ignoring...'
                #    continue
                
                #_arr.append(float(_buf[1])*scale)
                _arr.append(aLim)
                _prev_limit = aLim
                
            _d.append((_mass, _arr))

        #print self.legend, _d

        for t in _d:

            _x.append(t[0])
            _mean = TMath.Mean(len(t[1]), t[1])
            _median = TMath.Median(len(t[1]), t[1])
            _rms  = TMath.RMS(len(t[1]), t[1])
            _nrms = _rms/len(t[1])

            #print self.legend, t[0], _mean, _rms, _nrms
            if value_type == 'mean':
                _value = _mean
                _y.append(_mean)
            elif value_type == 'median':
                _value = _median
                _y.append(_median)
            _exh.append(0)
            _exl.append(0)
            _exh2.append(0)
            _exl2.append(0)
            if error_type == 'rms':
                _eyh.append(_rms/2)
                _eyl.append(_rms/2)
                _eyh2.append(_rms)
                _eyl2.append(_rms)
            elif error_type == 'quantile':
                _prob = array('d')
                _prob.append(0.021)
                _prob.append(0.159)
                _prob.append(0.841)
                _prob.append(0.979)
                _nprob = 4
                _quantiles = array('d')
                _quantiles.append(0)
                _quantiles.append(0)
                _quantiles.append(0)
                _quantiles.append(0)
                TMath.Quantiles(len(t[1]), _nprob, t[1], _quantiles, _prob)
                #print self.legend, 'Quantiles:', _quantiles

                _eyh.append(abs(_value-_quantiles[2]))
                _eyl.append(abs(_value-_quantiles[1]))
                _eyh2.append(abs(_value-_quantiles[3]))
                _eyl2.append(abs(_value-_quantiles[0]))

        self.x[cat] = _x
        self.y[cat] = _y
        self.exh[cat] = _exh
        self.exl[cat] = _exl
        self.exh2[cat] = _exh2
        self.exl2[cat] = _exl2

        # non-smoothed bands
        #self.eyh[cat] = _eyh
        #self.eyl[cat] = _eyl
        #self.eyh2[cat] = _eyh2
        #self.eyl2[cat] = _eyl2

        # smoothed bands
        # ee, mumu
        #self.eyh[cat] = self.smooth_band(_x,_eyh,_y,[1],300,600,'high')
        #self.eyh2[cat] = self.smooth_band(_x,_eyh2,_y,[1],300,600,'high')
        #self.eyl[cat] = self.smooth_band(_x,_eyl,_y,[],300,600)
        #self.eyl2[cat] = self.smooth_band(_x,_eyl2,_y,[1],300,600)
        # comb
        self.eyh[cat] = self.smooth_band(_x,_eyh,_y,[],300,600,'high')
        self.eyh2[cat] = self.smooth_band(_x,_eyh2,_y,[1],300,600,'high')
        self.eyl[cat] = self.smooth_band(_x,_eyl,_y,[1],300,600)
        self.eyl2[cat] = self.smooth_band(_x,_eyl2,_y,[1],300,600)
Esempio n. 5
0
    def add_median(self, cat, filename, legend, scale = 1.0,
                   line_color = 1, line_style = 1, line_width = 1,
                   marker_color = 1, marker_size = 1.2, marker_style=8,
                   dofit=False, fit_min = 100, fit_max = 3000,
                   var_scale = None,
                   fill_style = 1002,
                   fill_color = None,
                   fill2_style = 1002,
                   fill2_color = ROOT.kRed,
                   extra_scale_name = 'unit',
                   value_type = 'median',
                   error_type = 'quantile'):
        #
        # same as add() but for multiple values
        #
        

        if fill_color == None:
            fill_color = line_color

        self.type[cat] = 'observed'

        self.dofit[cat] = dofit
        self.fit_min[cat] = fit_min
        self.fit_max[cat] = fit_max

        self.cat.append(cat)
        self.file = filename
        self.tlegend[cat] = legend
        self.line_color[cat] = line_color
        self.line_style[cat] = line_style
        self.line_width[cat] = line_width
        self.marker_color[cat] = marker_color
        self.marker_size[cat] = marker_size
        self.marker_style[cat] = marker_style
        self.fill_style[cat] = fill_style
        self.fill2_style[cat] = fill2_style
        self.fill_color[cat] = fill_color
        self.fill2_color[cat] = fill2_color

        _x = array('d')
        _y = array('d')
        _exh = array('d')
        _exl = array('d')
        _eyh = array('d')
        _eyl = array('d')
        _d = [] # list of tuples, one tuple per mass point
        with open(filename, 'r') as _file:
            _mass = None
            _arr = array('d')
            _prev_limit = 0.0
            for line in _file:
                #print self.legend, line

                _buf = line.split()

                if _buf[0][0] == '#':
                    continue

                #_mass = float(_buf[0])
                aMass = float(_buf[0])
                _scale = get_scale(var_scale, aMass)
                _scale_err = get_scale_err(var_scale, aMass)

                if _scale != None:
                    _scale = _scale*scale
                else:
                    _scale = scale

                #_xval = float(_buf[0])
                #_x.append(_xval)

                #_yval = float(_buf[1])*_scale*get_scale(extra_scale_name, _xval)
                aLim = float(_buf[1])*_scale*get_scale(extra_scale_name, aMass)
                #_y.append(_yval)

                if _mass == None:
                    _mass = aMass
                
                if abs(_mass-aMass) > 0.1:
                    _d.append((_mass, _arr))
                    #print self.legend, cat, 'mass =', _mass, 'PEs per point:', len(_arr)
                    _mass = aMass
                    _arr=array('d')

                    
                #if abs(aLim - _prev_limit) < 0.000001:
                #    #print self.legend, 'WARNING: duplicate expected limit, ignoring...'
                #    continue
                
                #_arr.append(float(_buf[1])*scale)
                _arr.append(aLim)
                _prev_limit = aLim
                
            _d.append((_mass, _arr))
                
        for t in _d:

            _x.append(t[0])
            _mean = TMath.Mean(len(t[1]), t[1])
            _median = TMath.Median(len(t[1]), t[1])
            _rms  = TMath.RMS(len(t[1]), t[1])
            _nrms = _rms/len(t[1])

            #print self.legend, t[0], _mean, _rms, _nrms
            if value_type == 'mean':
                _value = _mean
                _y.append(_mean)
            elif value_type == 'median':
                _value = _median
                _y.append(_median)
            _exh.append(0)
            _exl.append(0)
            if error_type == 'rms':
                _eyh.append(_rms/2)
                _eyl.append(_rms/2)
            elif error_type == 'quantile':
                _prob = array('d')
                _prob.append(0.021)
                _prob.append(0.159)
                _prob.append(0.841)
                _prob.append(0.979)
                _nprob = 4
                _quantiles = array('d')
                _quantiles.append(0)
                _quantiles.append(0)
                _quantiles.append(0)
                _quantiles.append(0)
                TMath.Quantiles(len(t[1]), _nprob, t[1], _quantiles, _prob)
                #print self.legend, 'Quantiles:', _quantiles

                #_eyh.append(abs(_value-_quantiles[2]))
                #_eyl.append(abs(_value-_quantiles[1]))
                _eyh.append(0)
                _eyl.append(0)

        self.x[cat] = _x
        self.y[cat] = _y
        self.exh[cat] = _exh
        self.exl[cat] = _exl
        self.eyh[cat] = _eyh
        self.eyl[cat] = _eyl