Пример #1
0
        def dip_residual_method():

            hh = TH2F("hh", "", 20, hodo_center - 4.0, hodo_center + 4.0, 200,
                      -2, 2)
            cuts = 'n_tracks==1 && amp_max[{}]>1000 && amp_max[{}]>1000'.format(
                self.xtal[0], self.xtal[1])
            t_tree.Draw("(fit_time[{0}]-fit_time[{1}]):{2}>>hh".format(
                self.xtal[0], self.xtal[1], axis[0]), cuts,
                        "COLZ")  # Plot dt vs (X or Y)
            hh.FitSlicesY()  # Fit slices with gaussians
            gr = TGraphErrors(t_file.Get(
                "hh_1"))  # hh_1 is the histo of means from FitSlicesY

            ## Sorry for the confusing names. We plot dt vs (X or Y), so dt is our y_var, and dx is our x_var, the distance term (ie X OR Y)
            points = range(gr.GetN())
            dx = array('d', gr.GetX())
            dt = array('d', gr.GetY())
            p1 = TF1("p1", "pol1")
            TGraph(gr.GetN(), dx, dt).Fit("p1",
                                          "WQ")  # Fit dt_mean vs Y linearly

            ## Sum each 3 consecutive residuals, take the max from this value's abs(), and the middle index is where the "dip" is farthest from the fit, the "center"
            res = [dt[i] - p1.Eval(dx[i])
                   for i in points]  # The residual between the fit and data
            sum_res = [abs(sum(res[i:i + 3])) for i in points[:-2]
                       ]  # Sum 3 residuals ([:-2] to avoid index out of range)
            axis_center = dx[
                sum_res.index(max(sum_res)) +
                1]  # +1 b/c we index the 1st out of 3 residuals, but we want the middle one

            return axis_center
Пример #2
0
def _convert_tgrapherrors(root_obj: TGraphErrors) -> dict:
    """
    Take the values from a TGraphErrors and add them to a series
    of arrays

    Parameters
    ----------
    root_obj : TGraphError
        ROOT TGraphError

    Returns
    -------
    dict
        Dictionary with x, y, xerr, yerr points
    """
    xm, ym, x_errm, y_errm = (root_obj.GetX(),
                              root_obj.GetY(),
                              root_obj.GetEX(),
                              root_obj.GetEY())
    x, y, x_err, y_err = [], [], [], []
    for n in range(0, root_obj.GetN()):
        x.append(xm[n])
        y.append(ym[n])
        x_err.append(x_errm[n])
        y_err.append(y_errm[n])

    return {'x': np.array(x), 'y': np.array(y),
            'xerr': np.array(x_err), 'yerr': np.array(y_err)}
Пример #3
0
def sub2pull(dataHist, pdfwErrs):
    from ROOT import TGraphErrors
    from math import sqrt

    ThePull = TGraphErrors(dataHist.GetN())

    pdfi = 0
    for i in range(0, ThePull.GetN()):
        while (dataHist.GetX()[i] > pdfwErrs.GetX()[pdfi]):
            pdfi += 1
        #print 'pull point:',i,'pdfi:',pdfi
        diff = dataHist.GetY()[i] - pdfwErrs.GetY()[pdfi]
        if (diff < 0):
            err2 = pdfwErrs.GetErrorY(pdfi)**2 + dataHist.GetErrorYhigh(i)**2
        else:
            err2 = pdfwErrs.GetErrorY(pdfi)**2 + dataHist.GetErrorYlow(i)**2
        if (err2>0):
            pull = diff/(sqrt(err2)*1.2)
        else:
            pull = 0

        ThePull.SetPoint(i, dataHist.GetX()[i], pull)
        ThePull.SetPointError(i, 0., 1.)

    ThePull.SetName(dataHist.GetName() + "_pull")
    ThePull.SetTitle("data")

    return ThePull
Пример #4
0
def testIthr():
    lines = get_lines('DAC_scan_ithr_0x40to0xf0.dat')

    gr1 = TGraphErrors()
    gr2 = TGraphErrors()

    fUnit = 1000. / 0.7
    yUnit = 'e^{-}'

    for line in lines:
        if len(line) == 0: continue
        if line[0] in ['#', '\n']: continue
        fs = line.rstrip().split()

        ix = int(fs[0])
        gr1.SetPoint(ix, float(fs[1]), float(fs[2]) * fUnit)
        gr1.SetPointError(ix, 0, float(fs[3]) * fUnit)
        gr2.SetPoint(ix, float(fs[1]), float(fs[4]) * fUnit)
        gr2.SetPointError(ix, 0, float(fs[5]) * fUnit)

    useAtlasStyle()
    gStyle.SetMarkerStyle(20)

    gr1.SetMarkerStyle(20)
    gr1.Draw('AP')
    h1 = gr1.GetHistogram()
    h1.GetYaxis().SetTitle("Threshold [" + yUnit + "]")
    h1.GetXaxis().SetTitle("I_{Thre} code")
    # h1.GetYaxis().SetRangeUser(0,0.2)

    gPad.SetTicks(1, 0)
    gPad.SetRightMargin(0.16)

    y1b = 0
    y2b = 15
    x1 = h1.GetXaxis().GetXmax()
    y1 = h1.GetYaxis().GetXmin()
    y2 = h1.GetYaxis().GetXmax()
    raxis = TGaxis(x1, y1, x1, y2, y1b, y2b, 506, "+L")
    raxis.SetLineColor(2)
    raxis.SetLabelColor(2)
    raxis.SetTitleColor(2)
    raxis.SetTitle("ENC [" + yUnit + "]")
    raxis.Draw()

    nP = gr2.GetN()
    Ys = gr2.GetY()
    EYs = gr2.GetEY()
    Y = array(
        'd', [y1 + (y2 - y1) / (y2b - y1b) * (Ys[i] - y1b) for i in range(nP)])
    EY = array('d', [(y2 - y1) / (y2b - y1b) * EYs[i] for i in range(nP)])
    gr2x = TGraphErrors(nP, gr2.GetX(), Y, gr2.GetEX(), EY)
    gr2x.SetMarkerStyle(24)
    gr2x.SetLineColor(2)
    gr2x.SetMarkerColor(2)

    gr2x.Draw('Psame')

    waitRootCmdX()
Пример #5
0
def main():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-i", "--inputfile", dest="inputfile")
    parser.add_option("",
                      "--spline3",
                      action="store_true",
                      dest="spline3",
                      default=False)
    (options, args) = parser.parse_args()

    from ROOT import TFile, TGraph, TGraphErrors, NULL
    import csv

    with open(options.inputfile) as f:
        reader = csv.reader(f, delimiter=" ", skipinitialspace=True)
        rows = list(reader)
        outfile = TFile("%s.root" % options.inputfile, "RECREATE")
        g = TGraphErrors(len(rows))

        for i, row in enumerate(rows):
            g.SetPoint(i, float(row[1]) / 1000., float(row[2]))
            g.SetPointError(i, 0.0, float(row[3]))

        if options.spline3:
            g_spline3 = TGraph(10 * (g.GetN() - 1))
            x = g.GetX()
            y = g.GetY()
            for i in range(g.GetN() - 1):
                step = (x[i + 1] - x[i]) / 10
                for j in range(10):
                    index = 10 * i + j
                    x_ = x[i] + j * step
                    y_ = g.Eval(x_, NULL, "S")
                    g_spline3.SetPoint(index, x_, y_)
            g_spline3.SetName(options.inputfile)
            g_spline3.Write()
        else:
            g.SetName(options.inputfile)
            g.Write()

        outfile.Close()
Пример #6
0
def addErrors(err1, err2):
    from ROOT import TGraphErrors
    from math import sqrt

    newErrs = TGraphErrors(err1)

    for i in range(0, newErrs.GetN()):
        newErrs.SetPoint(i, newErrs.GetX()[i], err1.GetY()[i] + err2.GetY()[i])
        newErrs.SetPointError(i, err1.GetErrorX(i),
                              sqrt(err1.GetErrorY(i)**2 + \
                                   err2.GetErrorY(i)**2)
                              )
    return newErrs
Пример #7
0
    def ApplyBinShiftCorrectionGeneral(self, hist, fit):
        """
        Alternative method for bin shift correction:
        - Apply user-default model for bin-shift correction
        - don't multiply by pt
        @param hist: Input spectrum for the bin shift correction
        @param fit: Model for the bin-shift correction
        @return: The bin-shift corrected spectrum as graph
        """
        h = deepcopy(hist)
        hist.SetName("htemp")

        result = TGraphErrors(h)
        for i in range(0, result.GetN()):
            result.GetEX()[i] = 0.
        y = 0

        #for now 10 iterations fixes
        for k in range(0, 10):
            for i in range(1, h.GetNbinsX() + 1):
                y = fit.Integral(h.GetBinLowEdge(i),
                                 h.GetBinUpEdge(i)) / h.GetBinWidth(i)
                x = self.FindX(y, fit, h.GetBinLowEdge(i), h.GetBinUpEdge(i))
                result.GetX()[i - 1] = x

        # remove points that are 0
        while result.GetY()[0] < 1.e-99:
            result.RemovePoint(0)

        mybin = 0
        for biniter in range(0, result.GetN()):
            if result.GetY()[biniter] < 1.e-99:
                mybin = biniter
                break
        while result.RemovePoint(mybin) > 0:
            continue

        return result
Пример #8
0
def GetData(file, scale=1., sed=True, title='SED', barUL=True):
    GetData.Ng += 1
    g = TGraphErrors(file)
    gUL = TGraphErrors()

    if sed:
        for i in range(g.GetN()):
            g.GetY()[i] = pow(g.GetX()[i], 2) * g.GetY()[i] * 1e-6 / scale
            g.GetEY()[i] = pow(g.GetX()[i], 2) * g.GetEY()[i] * 1e-6 / scale
            g.GetX()[i] *= 1e-3

    idel = 0
    nUL = 0
    while idel < g.GetN():
        if g.GetEY()[idel] < 0:
            gUL.SetPoint(nUL, g.GetX()[idel], g.GetY()[idel])
            if barUL:
                gUL.SetPointError(nUL, 0, g.GetY()[idel] * 1e-5)
            nUL += 1
            g.RemovePoint(idel)
        else:
            idel += 1

    if sed:
        g.SetTitle(title + ";Energy [GeV];E^{2}dN/dE [TeV cm^{-2} s^{-1}]")
    else:
        g.SetTitle(title + ";Energy [MeV];dN/dE [cm^{-2} s^{-1} MeV^{-1}]")

    g.SetLineColor(kRed)
    g.SetMarkerColor(kRed)
    g.SetMarkerStyle(20)
    g.SetName("g%d" % GetData.Ng)
    gUL.SetLineColor(g.GetLineColor())
    gUL.SetName("gUL%d" % GetData.Ng)

    return g, gUL
Пример #9
0
def getGraph(Ts, filename):
    lines = None
    with open(filename) as f1:
        lines = f1.readlines()

    Tx = None
    for line in lines:
        ### find the associated tamperature
        line = line.rstrip()
        if len(line) == 0:
            Tx = None
            continue

        if line[0] == '#':
            fs = line.split()
            data = fs[1] + ' ' + fs[2]
            b = parse(data)
            if b is None: continue

            for x in Ts:
                for y in x.Times:
                    if b > y[0] and b < y[1]:
                        Tx = x
                        print b, Tx.T
                        break
                if Tx is not None: break
            continue

        if Tx is None: continue
        ### Add the samples to it
        fs = line.split(',')
        if len(fs) < 3:
            continue
        Tx.data.append(float(fs[2]))

    gr1 = TGraphErrors()
    for t in Ts:

        if len(t.data) == 0: continue
        ### calculate the mean and err
        ### Fill in the tgraph
        print t.T, len(t.data), nm.mean(t.data), nm.std(t.data)
        i = gr1.GetN()
        gr1.SetPoint(i, t.T, nm.mean(t.data))
        gr1.SetPointError(i, 0, nm.std(t.data))

    return gr1
Пример #10
0
def scanKK():
    '''Used to test the scan of one or more parameters'''
    nChips = 19 # the magic number
#     nChan = len(chans)

    iP = 1
    cd = CommonData()
    cd.setupConnection()
    sc1 = SensorConfig(cd)

    ### get list of chains to be updated
#     chains = set([sc1.tms1mmX19chainSensors[sc1.tms1mmX19sensorInChain[c]][0] for c in chans])

    chan = 5
#     cd.inputVs = [3., 0., 3., 0., 0., 0.]
#     cd.inputVs = [3., 0., 0.732, 1.68, 0., 0.]
#     cd.inputVs = [0.75, 2.15, 0.7, 0., 0., 0.]

    show = True
    g1 = None
    if show:
        g1 = TGraphErrors()

    xj = open('scan_KK_6.ttl','w')

    ### point insert scan
    ipar = 2
    pts = [(0.,None),(3.,None)]
    while True:
        print pts
        pt, xy, r1, k = insertPoint(pts)
        print '-'*30
        print pt,xy,r1, k
        print '-'*30
        if xy<3 and (r1<0.01 or xy/r1<0.1 or k<0.007): break

        cd.inputVs[ipar] = pt
        cd.updatePars(chan, None, False)
        sc1.update_sensor(chan)
        time.sleep(3)
        cd.fetch()

        m,v = getMeanVar(cd.adcData[chan])
        print ' '.join([str(x) for x in [chan, m, v]+cd.inputVs])
        xj.write(' '.join([str(x) for x in [chan, m, v]+cd.inputVs])+'\n')

        if g1:
            n1 = g1.GetN()
            g1.SetPoint(n1, pt, m)
            g1.SetPointError(n1, 0, v)

        if xy == 999:
            pts[r1] = (pt,m)
        else:
            pts.append((pt,m))

    ### simple scan
#     while cd.inputVs[0]>0.001:
#         cd.updatePars(chan, None, False)
#         sc1.update_sensor(chan)
#         time.sleep(2)
#         cd.fetch()
# 
#         m,v = getMeanVar(cd.adcData[chan])
#         print ' '.join([str(x) for x in [chan, m, v]+cd.inputVs])
#         xj.write(' '.join([str(x) for x in [chan, m, v]+cd.inputVs])+'\n')
# 
#         if g1:
#             g1.SetPoint(g1.GetN(), cd.inputVs[0], m)
#         cd.inputVs[0] *= 2./3
    if g1:
        g1.SetMarkerStyle(4)
        g1.SetMarkerColor(2)
        g1.SetLineColor(2)
        g1.Draw("AP")
        h1 = g1.GetHistogram()
        h1.GetXaxis().SetTitle(cd.voltsNames[ipar]+' [V]')
        h1.GetYaxis().SetTitle("V_{out} [V]")
        lt = TLatex()
        lt.DrawLatexNDC(0.2,0.92,"Chip %d"%chan)
        waitRootCmdX()
Пример #11
0
def analyzeData(country, total, active, recovered, deaths, tStart, tStop,
                totalPopulation, symptomaticFraction, transmissionProbability,
                recoveryRate, doSmearing, doFit):
    ntuple = [
        tStart, tStop, totalPopulation, symptomaticFraction,
        transmissionProbability, recoveryRate
    ]
    farFromMax = 0.95
    growthRate = 0.13
    carryingCapacity = 4e5

    ################
    # Active cases #
    ################
    myCanvActive = TCanvas('myCanvActive_' + country,
                           'Active cases ' + country)

    xValues = [
        i for i in range(len(active.keys())) if i >= tStart and i <= tStop
    ]
    yValues = [
        active[k] for i, k in enumerate(sorted(active.keys()))
        if i >= tStart and i <= tStop
    ]
    erryValues = assignErrors(yValues)

    myGraphActive = TGraphErrors()
    myGraphActive.SetMarkerStyle(20)
    for i in range(len(xValues)):
        myGraphActive.SetPoint(myGraphActive.GetN(), xValues[i], yValues[i])
        myGraphActive.SetPointError(myGraphActive.GetN() - 1, 0, erryValues[i])
    myGraphActive.Draw('APE1')
    myGraphActive.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphActive.GetHistogram().GetYaxis().SetTitle(
        'Active cases affected by CoViD-19')

    historyActive = 0.
    for i, k in enumerate(sorted(active.keys())):
        if i < tStart:
            historyActive += active[k]
    ntuple.extend([historyActive, xValues, yValues, erryValues])
    print('==> History active cases:', historyActive)

    ###################
    # Build the model #
    ###################
    evActive = evolution([yValues[0], carryingCapacity, growthRate], tStart,
                         tStop, totalPopulation, recoveryRate,
                         symptomaticFraction, transmissionProbability,
                         historyActive)

    if doFit == True:
        evActive.runOptimization(xValues, yValues, erryValues, [], doSmearing)
        evActive.evolve(evActive.tStop, evActive.parValues, True)
        if doSmearing == True:
            evActive.smearing()
        evActiveGraphN = evActive.getGraphN()
        evActiveGraphN.Draw('PL same')
        statActive = evActive.addStats(evActive.parNames, evActive.parValues)

        print(
            '==> Active cases, history active cases, p-infected, Carrying capacity, total population alive',
            evActive.evolve(tStop, evActive.parValues), 'at day', tStop)
        print('==> Percentage population with antibodies',
              round(100. * evActive.totalInfected(tStop) / totalPopulation),
              '% at day', tStop, '(herd immunity at', evActive.herdImmunity(),
              '%)')
        print('==> Doubling time:', round(log(2.) / evActive.parValues[2], 1),
              'days')

        now = TLine(
            len(active) - 1, 0,
            len(active) - 1, evActive.fitFun.Eval(len(active) - 1))
        now = TLine(len(active) - 1, 0, len(active) - 1, 1)
        now.SetLineColor(4)
        now.SetLineWidth(2)
        now.Draw('same')

        willbe = TLine(evActive.fitFun.GetMaximumX(), 0,
                       evActive.fitFun.GetMaximumX(),
                       evActive.fitFun.GetMaximum())
        willbe.SetLineColor(6)
        willbe.SetLineWidth(2)
        willbe.Draw('same')

        myCanvActiveR0 = TCanvas('myCanvActiveR0_' + country, 'R0 ' + country)

        evActiveGraphR0 = evActive.getGraphR0(evActiveGraphN)
        evActiveGraphR0.Draw('APL')
        evActiveGraphR0.GetHistogram().GetXaxis().SetTitle('Time (days)')
        evActiveGraphR0.GetHistogram().GetYaxis().SetTitle('R')

        myCanvActiveR0.SetGrid()
        myCanvActiveR0.Modified()
        myCanvActiveR0.Update()

    myCanvActive.SetGrid()
    myCanvActive.Modified()
    myCanvActive.Update()

    #################
    # CONTROL PLOTS #
    #################

    ###############
    # Total cases #
    ###############
    myCanvTotal = TCanvas('myCanvTotal_' + country, 'Total cases ' + country)
    myGraphTotal = TGraphErrors()
    myGraphTotal.SetMarkerStyle(20)

    for k in sorted(total.keys()):
        myGraphTotal.SetPoint(myGraphTotal.GetN(), myGraphTotal.GetN(),
                              total[k])
        myGraphTotal.SetPointError(myGraphTotal.GetN() - 1, 0, sqrt(total[k]))

    myGraphTotal.Draw('APE1')
    myGraphTotal.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphTotal.GetHistogram().GetYaxis().SetTitle(
        'Total cases affected by CoViD-19')

    myCanvTotal.SetGrid()
    myCanvTotal.Modified()
    myCanvTotal.Update()

    ##########
    # Deaths #
    ##########
    myCanvDeaths = TCanvas('myCanvDeaths_' + country, 'Deaths ' + country)
    myGraphDeaths = TGraphErrors()
    myGraphDeaths.SetMarkerStyle(20)

    for k in sorted(deaths.keys()):
        myGraphDeaths.SetPoint(myGraphDeaths.GetN(), myGraphDeaths.GetN(),
                               deaths[k])
        myGraphDeaths.SetPointError(myGraphDeaths.GetN() - 1, 0,
                                    sqrt(deaths[k]))

    myGraphDeaths.Draw('APE1')
    myGraphDeaths.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphDeaths.GetHistogram().GetYaxis().SetTitle('Total deaths')

    myCanvDeaths.SetGrid()
    myCanvDeaths.Modified()
    myCanvDeaths.Update()

    ########################
    # Ratio deaths / total #
    ########################
    myCanvRatio01 = TCanvas('myCanvRatio01_' + country, 'Ratio ' + country)
    myGraphRatio01 = TGraphErrors()
    myGraphRatio01.SetMarkerStyle(20)

    for k in sorted(deaths.keys()):
        myGraphRatio01.SetPoint(myGraphRatio01.GetN(), myGraphRatio01.GetN(),
                                deaths[k] / total[k])
        myGraphRatio01.SetPointError(
            myGraphRatio01.GetN() - 1, 0,
            myGraphRatio01.GetY()[myGraphRatio01.GetN() - 1] *
            sqrt(deaths[k] / (deaths[k] * deaths[k]) + total[k] /
                 (total[k] * total[k])))

    myGraphRatio01.Draw('APE1')
    myGraphRatio01.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphRatio01.GetHistogram().GetYaxis().SetTitle(
        'Total deaths / Total cases')

    myCanvRatio01.SetGrid()
    myCanvRatio01.Modified()
    myCanvRatio01.Update()

    ############################################
    # Ratio delta(deaths + recovered) / active #
    ############################################
    myCanvRatio02 = TCanvas('myCanvRatio02_' + country, 'Ratio ' + country)
    myGraphRatio02 = TGraphErrors()
    myGraphRatio02.SetMarkerStyle(20)

    sortedKeys = sorted(deaths.keys())
    for i, k in enumerate(sortedKeys[1:]):
        numerator = abs(deaths[k] - deaths[sortedKeys[i]] + recovered[k] -
                        recovered[sortedKeys[i]])
        denominator = active[k]
        myGraphRatio02.SetPoint(
            myGraphRatio02.GetN(),
            myGraphRatio02.GetN() + 1,
            numerator / denominator if denominator != 0 else 0)
        myGraphRatio02.SetPointError(
            myGraphRatio02.GetN() - 1, 0,
            (myGraphRatio02.GetY()[myGraphRatio02.GetN() - 1] *
             sqrt(numerator / pow(numerator, 2) +
                  denominator / pow(denominator, 2)))
            if denominator != 0 else 0)

    myGraphRatio02.Draw('AP')
    myGraphRatio02.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphRatio02.GetHistogram().GetYaxis().SetTitle(
        '#Delta Recovered (alive + dead) / Active cases')

    myCanvRatio02.SetGrid()
    myCanvRatio02.Modified()
    myCanvRatio02.Update()

    ######################################
    # Ratio delta(deaths) / delta(total) #
    ######################################
    myCanvRatio03 = TCanvas('myCanvRatio03_' + country, 'Ratio ' + country)
    myGraphRatio03 = TGraphErrors()
    myGraphRatio03.SetMarkerStyle(20)

    sortedKeys = sorted(deaths.keys())
    for i, k in enumerate(sortedKeys[1:]):
        numerator = abs(deaths[k] - deaths[sortedKeys[i]])
        denominator = total[k] - total[sortedKeys[i]]
        myGraphRatio03.SetPoint(
            myGraphRatio03.GetN(),
            myGraphRatio03.GetN() + 1,
            numerator / denominator if denominator != 0 else 0)
        myGraphRatio03.SetPointError(
            myGraphRatio03.GetN() - 1, 0,
            (myGraphRatio03.GetY()[myGraphRatio03.GetN() - 1] *
             sqrt(numerator / pow(numerator, 2) +
                  denominator / pow(denominator, 2)))
            if denominator != 0 else 0)

    myGraphRatio03.Draw('AP')
    myGraphRatio03.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphRatio03.GetHistogram().GetYaxis().SetTitle(
        '#Delta deaths / #Delta total')

    myCanvRatio03.SetGrid()
    myCanvRatio03.Modified()
    myCanvRatio03.Update()

    if doFit == False:
        return [
            ntuple, myCanvTotal, myGraphTotal, myCanvActive, myGraphActive,
            myCanvDeaths, myGraphDeaths, myCanvRatio01, myGraphRatio01,
            myCanvRatio02, myGraphRatio02, myCanvRatio03, myGraphRatio03
        ]

    return [
        ntuple, myCanvTotal, myGraphTotal, myCanvActive, myGraphActive,
        evActive, evActiveGraphN, statActive, now, willbe, myCanvActiveR0,
        evActiveGraphR0, myCanvDeaths, myGraphDeaths, myCanvRatio01,
        myGraphRatio01, myCanvRatio02, myGraphRatio02, myCanvRatio03,
        myGraphRatio03
    ]
Пример #12
0
def plot2BodyDist(theFitter, pars, chi2, ndf, 
                  Err = -1, NP = False, Prefix = "Mjj", Left = False):
    from ROOT import gPad, TLatex, TCanvas, kRed, kCyan, kBlue, \
         RooFit, RooPlot, RooCurve, RooAbsReal, TGraphErrors, TLine, \
         RooWjjMjjFitter

    if pars.includeMuons and pars.includeElectrons:
        modeString = ''
    elif pars.includeMuons:
        modeString = 'Muon'
    elif pars.includeElectrons:
        modeString = 'Electron'
    else:
        modeString = ''

    mf = theFitter.stackedPlot(False, RooWjjMjjFitter.mjj, Left)
    mf.SetName("%s_Stacked" % (Prefix));
    sf = theFitter.residualPlot(mf, "h_background", "dibosonPdf", False)
    sf.SetName("%s_Subtracted" % (Prefix));
    pf = theFitter.residualPlot(mf, "h_total", "", True)
    pf.SetName("%s_Pull" % (Prefix))
    pf2 = pf.emptyClone("%s_Pull_Corrected" % (Prefix))
    pf2.SetMinimum(-5.)
    pf2.SetMaximum(5.)
    corrPull = False
    lf = theFitter.stackedPlot(True, RooWjjMjjFitter.mjj, Left)
    lf.SetName("%s_Stacked_Log" % (Prefix));

    if Err > 0:
        totalPdf = theFitter.getWorkSpace().pdf('totalPdf')
        ## Ntotal = totalPdf.expectedEvents(iset)

        ## print 'Ntotal:',Ntotal
        h_dibosonPdf = sf.getCurve('h_dibosonPdf')
        totalPdf.plotOn(sf,
                        RooFit.ProjWData(theFitter.getWorkSpace().data('data')),
                        RooFit.Normalization(Err, RooAbsReal.Raw),
                        #RooFit.AddTo('h_dibosonPdf', 1., 1.),
                        #RooFit.Invisible(),
                        RooFit.Name('h_ErrUp'),
                        RooFit.Range('RangeForPlot'),
                        RooFit.NormRange('RangeForPlot'),
                        RooFit.LineColor(kRed), RooFit.LineStyle(3))
        h_ErrUp = sf.getCurve('h_ErrUp')
        sf.remove('h_ErrUp', False)

        ErrBand = TGraphErrors(h_dibosonPdf.GetN(), h_dibosonPdf.GetX(),
                               h_dibosonPdf.GetY())
        for pt in range(1, ErrBand.GetN()):
            ErrBand.SetPointError(pt, 0,
                                  h_ErrUp.interpolate(ErrBand.GetX()[pt]))
        ErrBand.SetName("ErrBand")
        ErrBand.SetTitle("Uncertainty")
        ErrBand.SetLineColor(kRed)
##         ErrBand.SetLineWidth(0)
##         ErrBand.SetLineStyle(0)
        ErrBand.SetFillColor(kRed)
        ErrBand.SetFillStyle(3353)

        
        #ErrBand.Draw('ap3')
        #h_ErrUp.Draw('lp')
        #gPad.Update()
        #gPad.WaitPrimitive()
##         h_ErrUp.Draw("al")
##         h_ErrUp.GetXaxis().Set(36, 40., 400.)
##         gPad.Update()
##         gPad.WaitPrimitive()
##         h_UpBand = RooCurve("h_UpBand", "Uncertainty", h_dibosonPdf, h_ErrUp,
##                             1., 1.)
##         h_UpBand.SetLineStyle(3)
##         h_UpBand.SetLineColor(kBlue+1)
##         h_DownBand = RooCurve("h_DownBand", "Uncertainty", h_dibosonPdf, h_ErrUp,
##                               1., -1.)
##         h_DownBand.SetLineStyle(3)
##         h_DownBand.SetLineColor(kBlue+1)

##         sf.addPlotable(h_UpBand, "L")
##         sf.addPlotable(h_DownBand, "L")
        sf.addObject(ErrBand, "3")
        #sf.Print("v")
        sf.drawAfter('h_dibosonPdf', 'ErrBand')
        #sf.Print("v")
        sf.drawAfter('ErrBand', 'theData')
        #sf.Print("v")
        sf.findObject('theLegend').AddEntry(ErrBand, 'Uncertainty', 'f')
        sf.findObject('theLegend').SetY1NDC(sf.findObject('theLegend').GetY1NDC() - 0.057)
        sf.findObject('theLegend').SetY1(sf.findObject('theLegend').GetY1NDC())

        corrPull = True
        pf2.addObject(sub2pull(sf.getHist('theData'),
                               sf.findObject('ErrBand')),
                      'p0')
        for item in range(0, int(pf.numItems())):
            firstItem = pf.getObject(item)
            if (type(firstItem) == TLine):
                newLine = TLine(firstItem)
                newLine.SetY1(4.)
                newLine.SetY2(-4.)
                pf2.addObject(newLine, 'l')
                #SetOwnership(newLine, False)


    if NP:
        NPPdf = theFitter.makeNPPdf();
        NPNorm = 4.*0.11*46.8/12.*pars.intLumi

        if (modeString == 'Electron'):
            if pars.njets == 2:
                NPNorm *= 0.0381
            elif pars.njets == 3:
                NPNorm *= 0.0123
        else:
            if pars.njets == 2:
                NPNorm *= 0.0550
            elif pars.njets == 3:
                NPNorm *= 0.0176

        print '**** N_NP:', NPNorm,'****'

        NPPdf.plotOn(sf, RooFit.ProjWData(theFitter.getWorkSpace().data('data')),
                     RooFit.Normalization(NPNorm, RooAbsReal.Raw),
                     RooFit.AddTo('h_dibosonPdf', 1., 1.),
                     RooFit.Name('h_NP'),
                     RooFit.Range('RangeForPlot'),
                     RooFit.NormRange('RangeForPlot'),
                     RooFit.LineColor(kBlue), RooFit.LineStyle(2))

        h_NP = sf.getCurve('h_NP')

        sf.drawBefore('h_dibosonPdf', 'h_NP')
        #sf.Print("v")
        sf.findObject('theLegend').AddEntry(h_NP, "CDF-like Signal", "L")
        sf.findObject('theLegend').SetY1NDC(sf.findObject('theLegend').GetY1NDC() - 0.057)
        sf.findObject('theLegend').SetY1(sf.findObject('theLegend').GetY1NDC())

    l = TLatex()
    l.SetNDC()
    l.SetTextSize(0.045)
    l.SetTextFont(42)

    cstacked = TCanvas("cstacked", "stacked")
    mf.Draw()
    if (chi2 > 0):
        l.DrawLatex(0.55, 0.49,
                    '#chi^{2}/dof = %0.3f/%d' % (chi2, ndf)
                    )
    pyroot_logon.cmsLabel(cstacked, pars.intLumi/1000, prelim = True)
    cstacked.Print('Wjj_%s_%s_%ijets_Stacked.pdf' % (Prefix, modeString,
                                                     pars.njets))
    cstacked.Print('Wjj_%s_%s_%ijets_Stacked.png' % (Prefix, modeString,
                                                     pars.njets))
    c2 = TCanvas("c2", "stacked_log")
    c2.SetLogy()
    lf.Draw()
    pyroot_logon.cmsPrelim(c2, pars.intLumi/1000)
    c2.Print('Wjj_%s_%s_%ijets_Stacked_log.pdf' % (Prefix, modeString,
                                                    pars.njets))
    c2.Print('Wjj_%s_%s_%ijets_Stacked_log.png' % (Prefix, modeString,
                                                    pars.njets))
    c3 = TCanvas("c3", "subtracted")
    sf.Draw()
    pyroot_logon.cmsLabel(c3, pars.intLumi/1000, prelim = True)
    c3.Print('Wjj_%s_%s_%ijets_Subtracted.pdf' % (Prefix, modeString,
                                                  pars.njets))
    c3.Print('Wjj_%s_%s_%ijets_Subtracted.png' % (Prefix, modeString,
                                                  pars.njets))
    c4 = TCanvas("c4", "pull")
    pf.Draw()
    c4.SetGridy()
    pyroot_logon.cmsPrelim(c4, pars.intLumi/1000)
    c4.Print('Wjj_%s_%s_%ijets_Pull.pdf' % (Prefix, modeString, pars.njets))
    c4.Print('Wjj_%s_%s_%ijets_Pull.png' % (Prefix, modeString, pars.njets))

    c5 = None
    if corrPull:
        c5 = TCanvas("c5", "corrected pull")
        pf2.Draw()
        c5.SetGridy()
        pyroot_logon.cmsPrelim(c5, pars.intLumi/1000)
        c5.Print('Wjj_%s_%s_%ijets_Pull_Corrected.pdf' % (Prefix, modeString,
                                                          pars.njets))
        c5.Print('Wjj_%s_%s_%ijets_Pull_Corrected.png' % (Prefix, modeString,
                                                          pars.njets))

    return ([mf,sf,pf2,lf],[cstacked,c2,c3,c5])
Пример #13
0
def runGlobalFit(country, active, totalPopulation, symptomaticFraction,
                 transmissionProbability, recoveryRate, doSmearing, doFit):
    tStart = 0
    tStop = 9 + 6 + 12 + 42 + 61 + 40 + 50 + 200
    timeList = [
        9, 9 + 6, 9 + 6 + 12, 9 + 6 + 12 + 42, 9 + 6 + 12 + 42 + 61,
        9 + 6 + 12 + 42 + 61 + 40, 9 + 6 + 12 + 42 + 61 + 40 + 50, tStop
    ]

    ntuple = [
        tStart, tStop, totalPopulation, symptomaticFraction,
        transmissionProbability, recoveryRate
    ]

    ################
    # Active cases #
    ################
    myCanvActive = TCanvas('myCanvActive_' + country,
                           'Active cases ' + country)

    xValues = [
        i for i in range(len(active.keys())) if i >= tStart and i <= tStop
    ]
    yValues = [
        active[k] for i, k in enumerate(sorted(active.keys()))
        if i >= tStart and i <= tStop
    ]
    erryValues = assignErrors(yValues)

    myGraphActive = TGraphErrors()
    myGraphActive.SetMarkerStyle(20)
    for i in range(len(xValues)):
        myGraphActive.SetPoint(myGraphActive.GetN(), xValues[i], yValues[i])
        myGraphActive.SetPointError(myGraphActive.GetN() - 1, 0, erryValues[i])
    myGraphActive.Draw('APE1')
    myGraphActive.GetHistogram().GetXaxis().SetTitle('Time (days)')
    myGraphActive.GetHistogram().GetYaxis().SetTitle(
        'Active cases affected by CoViD-19')

    ntuple.extend([
        0, xValues, yValues, erryValues, timeList, 1894, 16052, 0.435, 0.438,
        0.308, 0.216, 0.054, 0.435 / 2.5, 0.435 / 1.5, 0.435
    ])  # 0.0133, 0.175, 0.217, 0.393

    if doFit == True:
        ###################
        # Build the model #
        ###################
        evActive = evolution([ntuple[11], ntuple[12], ntuple[13]], tStart,
                             timeList[0], totalPopulation, recoveryRate,
                             symptomaticFraction, transmissionProbability)
        evolutions = [
            evolution([0, 0, ntuple[14 + i]], timeList[i], timeList[i + 1], 0,
                      recoveryRate, symptomaticFraction,
                      transmissionProbability) for i in range(3)
        ]
        evolutions.extend([
            evolution([0, 0, ntuple[14 + 3]], timeList[3], timeList[4], 0,
                      0.035, symptomaticFraction, transmissionProbability / 8)
        ])
        evolutions.extend([
            evolution([0, 0, ntuple[14 + 4]], timeList[4], timeList[5], 0,
                      recoveryRate, symptomaticFraction,
                      transmissionProbability / 2.5)
        ])
        evolutions.extend([
            evolution([0, 0, ntuple[14 + 5]], timeList[5], timeList[6], 0,
                      recoveryRate, symptomaticFraction,
                      transmissionProbability / 1.5)
        ])
        evolutions.extend([
            evolution([0, 0, ntuple[14 + 6]], timeList[6], timeList[7], 0,
                      recoveryRate, symptomaticFraction,
                      transmissionProbability)
        ])

        istat, parValues, parNames = evActive.runGlobalOptimization(
            evolutions, xValues, yValues, erryValues, [0, 1, 2, 3, 4, 5],
            doSmearing)

        evActive.evolveGlobal(evolutions, evolutions[-1].tStop, parValues,
                              True)
        if doSmearing == True:
            evActive.smearing()
        evActive.setFitFun(evActive.evolveLookUpWrapper, tStart, tStop,
                           parNames, parValues)
        evActiveGraphN = evActive.getGraphN()
        evActiveGraphN.Draw('PL same')
        statActive = evActive.addStats(parNames, parValues)

        print(
            '==> Active cases, history active cases * dt, p-infected, Carrying capacity, total population alive',
            evActive.evolveGlobal(evolutions, tStop, parValues), 'at day',
            tStop)
        print(
            '==> Percentage population with antibodies',
            round(100. *
                  evActive.totalInfectedGlobal(evolutions, tStop, parValues) /
                  totalPopulation), '% at day', tStop)

        now = TLine(
            len(active) - 1, 0,
            len(active) - 1, evActive.fitFun.Eval(len(active) - 1))
        now.SetLineColor(4)
        now.SetLineWidth(2)
        now.Draw('same')

        willbe = TLine(evActive.fitFun.GetMaximumX(), 0,
                       evActive.fitFun.GetMaximumX(),
                       evActive.fitFun.GetMaximum())
        willbe.SetLineColor(6)
        willbe.SetLineWidth(2)
        willbe.Draw('same')

        myCanvActiveR0 = TCanvas('myCanvActiveR0_' + country, 'R0 ' + country)

        evActiveGraphR0 = evActive.getGraphR0(evActiveGraphN)
        evActiveGraphR0.Draw('APL')
        evActiveGraphR0.GetHistogram().GetXaxis().SetTitle('Time (days)')
        evActiveGraphR0.GetHistogram().GetYaxis().SetTitle('R')

        myCanvActiveR0.SetGrid()
        myCanvActiveR0.Modified()
        myCanvActiveR0.Update()

    myCanvActive.SetGrid()
    myCanvActive.Modified()
    myCanvActive.Update()

    if doFit == False:
        return [ntuple, myCanvActive, myGraphActive]
    return [
        ntuple, myCanvActive, myGraphActive, evActive, evActiveGraphN,
        statActive, now, willbe, myCanvActiveR0, evActiveGraphR0
    ]
Пример #14
0
def main():
    gStyle.SetOptStat(0)
    BIAS_DIR = global_paths.BIASDIR+args.btagging+"/"
    if args.year == 'run2c':
        BIAS_DIR += "combined_run2/"
        #BIAS_DIR += "combined_run2_signal{}/"
        ## individual plots stored in run2c_masspoints    
    
    ## extract pulls
    pulls = TGraphErrors()
    for m in range(1600,8001,100):
        #try:
            pull0=int(PULL0[m])
            #pull0=10.

            #tfile = TFile.Open(BIAS_DIR+"fitDiagnostics_M{mass}.root".format(mass=m), "READ")
            #tfile = TFile.Open(BIAS_DIR.format(pull0)+"fitDiagnostics_M{mass}.root".format(mass=m), "READ")
            #tree = tfile.Get("tree_fit_sb")
            tree = TChain("tree_fit_sb")
            for seed in ['123456', '234567', '345678', '456789', '567891', '678912', '789123', '891234', '912345', '123459']:
                tree.Add(BIAS_DIR+"fitDiagnostics_M{mass}_{seed}.root".format(mass=m, seed=seed))
 
            ## the method proposed in the documemtation
            #hist = TH1D("hist", "hist", 20, -5, 5)
            #tree.Project("hist", "(r-1)/(0.5*(rHiErr+rLoErr))")
            #fit_func = TF1("gaussfit","gaus" , -5., 5.)
            #hist.Fit(fit_func, "E")
            #pulls.SetPoint(pulls.GetN(), m, fit_func.GetParameter(1)) ## get mean of gaussian fit
            
            ## Alberto's method
            #hist = TH1D("s_pulls", ";%s/#sigma_{r};Number of toys" % ("(r - "+str(pull0)+")"), 25, -5, +5) #
            hist = TH1D("s_pulls", ";%s/#sigma_{r};Number of toys" % ("#Deltar"), 25, -5, +5) #
            for i in range(tree.GetEntries()):
                if hist.GetEntries() >= 1000: continue
                tree.GetEntry(i)
                #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr)
                ##if tree.rLoErr < 0.: continue
                if abs(tree.r+1.) < 0.001: continue
                if abs(tree.r-1.) < 0.001: continue
                if abs(tree.r-0.) < 0.001: continue
                #if abs(tree.rLoErr)>8.: continue # trying to skip these values FIXME
                if tree.rHiErr==0. or tree.rLoErr==0.: continue
                #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr)
                #pull = (tree.r-pull0)/(0.5*(abs(tree.rHiErr)+abs(tree.rLoErr))) ## documentation approach
                #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r-pull0 < 0. else (tree.r-pull0)/abs(tree.rLoErr)  ## Alberto's sign convention
                pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r-pull0 > 0. else (tree.r-pull0)/abs(tree.rLoErr) ## my own approach
                #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r < 0. else (tree.r-pull0)/abs(tree.rLoErr)  ## Alberto's sign convention but depending directly on the sign of r
                #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r > 0. else (tree.r-pull0)/abs(tree.rLoErr) ## my own approach with an rErr dependence on r, not r-1
                hist.Fill(pull)

            ## individual plots for checking the fit quality
            c1 = TCanvas("c1", "Pulls", 600, 600)
            c1.cd()
            #c1.GetPad(0).SetTopMargin(0.06)
            #c1.GetPad(0).SetRightMargin(0.05)
            #c1.GetPad(0).SetBottomMargin(0.15)
            #c1.GetPad(0).SetTicks(1, 1)
            hist.GetXaxis().SetTitleSize(0.045)
            hist.GetYaxis().SetTitleSize(0.045)
            hist.GetYaxis().SetTitleOffset(1.1)
            hist.GetXaxis().SetTitleOffset(1.05)
            hist.GetXaxis().SetLimits(-5, 5.)
            hist.GetYaxis().SetLimits(0, 200.)
            hist.SetMinimum(0.)
            hist.SetMaximum(190.)
            c1.SetTopMargin(0.05)

            ##print "@ m= {}: \t mean = {}".format(m, hist.GetMean())
            #pulls.SetPoint(pulls.GetN(), m, hist.GetMean()) ## get actual mean of histogram
            fit_func = TF1("gaussfit","gaus" , -3., 3.)
            ###fit_func.SetParameter(1, hist.GetMean())
            #fit_func.SetParameter(1, 0.)
            ###fit_func.SetParLimits(1, -0.8, 0.8)
            #fit_func.SetParameter(2, 1.)
            ###fit_func.SetParameter(0, 45.)
            ###fit_func.SetParLimits(0, 30., 100.)
            hist.Fit(fit_func, "E")

            hist.Draw()

            drawCMS(-1, "Simulation Preliminary", year='run2')
            drawMass("m_{Z'} = "+str(m)+" GeV")
            c1.Print("plots/bias/run2c_masspoints/bias_fit_"+str(m)+"_"+args.year+".pdf")
            c1.Print("plots/bias/run2c_masspoints/bias_fit_"+str(m)+"_"+args.year+".png")

            n = pulls.GetN()
            pulls.SetPoint(n, m, fit_func.GetParameter(1)) ## get fitted gaussian mean
            pulls.SetPointError(n, 0., fit_func.GetParError(1)) ## set gaussian width as error

            hist.Delete()
            c1.Delete()
            #tfile.Close()
        #except:
        #    print "something went wrong in m =", m

    ## draw pulls
    c = TCanvas("canvas", "canvas", 800, 600)
    pulls.SetTitle(";m_{Z'} (GeV);mean #Deltar/#sigma_{r}")
    pulls.SetMarkerStyle(2)
    pulls.SetMarkerColor(2)
    pulls.SetLineColor(2)
    pulls.SetLineWidth(2)
    #pulls.GetYaxis().SetNdivisions(1020)
    pulls.SetMinimum(-0.5)
    pulls.SetMaximum(0.5)
    pulls.Draw("APL")
    zeroline = TGraph()
    zeroline.SetPoint(zeroline.GetN(), 1000, 0)
    zeroline.SetPoint(zeroline.GetN(), 8600, 0)
    zeroline.SetMarkerStyle(7)
    zeroline.SetMarkerSize(0)
    zeroline.SetLineStyle(15)
    zeroline.SetLineColor(1)
    zeroline.Draw("PL")
    c.SetGrid()
    pulls.GetXaxis().SetTitleSize(0.045)
    pulls.GetYaxis().SetTitleSize(0.045)
    pulls.GetYaxis().SetTitleOffset(1.1)
    pulls.GetXaxis().SetTitleOffset(1.05)
    pulls.GetXaxis().SetLimits(1350., 8150.)
    c.SetTopMargin(0.05)
    drawCMS(-1, "Simulation Preliminary", year='run2')
    c.Print("plots/bias/bias_study_"+args.year+".png")
    c.Print("plots/bias/bias_study_"+args.year+".pdf")
            jmean = gmean.Eval(m, 0, "S") 
            jsigma = gsigma.Eval(m, 0, "S")
            jalpha1 = galpha1.Eval(m, 0, "S")
            jslope1 = gslope1.Eval(m, 0, "S")
            jalpha2 = galpha2.Eval(m, 0, "S")
            jslope2 = gslope2.Eval(m, 0, "S")

        else:
            jmean = fmean.GetParameter(0) + fmean.GetParameter(1)*m + fmean.GetParameter(2)*m*m
            jsigma = fsigma.GetParameter(0) + fsigma.GetParameter(1)*m + fsigma.GetParameter(2)*m*m
            jalpha1 = falpha1.GetParameter(0) + falpha1.GetParameter(1)*m + falpha1.GetParameter(2)*m*m
            jslope1 = fslope1.GetParameter(0) + fslope1.GetParameter(1)*m + fslope1.GetParameter(2)*m*m
            jalpha2 = falpha2.GetParameter(0) + falpha2.GetParameter(1)*m + falpha2.GetParameter(2)*m*m
            jslope2 = fslope2.GetParameter(0) + fslope2.GetParameter(1)*m + fslope2.GetParameter(2)*m*m

        inorm.SetPoint(inorm.GetN(), m, syield)
        signalNorm[m].setVal(max(0., syield))

        imean.SetPoint(imean.GetN(), m, jmean)
        if jmean > 0: vmean[m].setVal(jmean)

        isigma.SetPoint(isigma.GetN(), m, jsigma)
        if jsigma > 0: vsigma[m].setVal(jsigma)

        ialpha1.SetPoint(ialpha1.GetN(), m, jalpha1)
        if not jalpha1==0: valpha1[m].setVal(jalpha1)

        islope1.SetPoint(islope1.GetN(), m, jslope1)
        if jslope1 > 0: vslope1[m].setVal(jslope1)
       
        ialpha2.SetPoint(ialpha2.GetN(), m, jalpha2)
Пример #16
0
    def showENC(self):
        tree1 = TTree()
        header = 'idX/i:vL/F:vH:A:D/i:R:W'
        first = True
        for f in self.dataFiles:
            if first: tree1.ReadFile(f, header)
            else: tree1.ReadFile(f)

        p1 = TProfile('p1', 'p1;#DeltaU [V];Prob', self.bins[0],
                      tree1.GetMinimum('vH-vL') * 0.8,
                      tree1.GetMaximum('vH-vL') * 1.2)
        tree1.Draw("D:(vH-vL)>>p1", "", "profE")

        ### change it to tgraph
        g1 = TGraphErrors()
        for i in range(p1.GetNbinsX() + 2):
            N = p1.GetBinEntries(i)
            if N > 0:
                print i, N, p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(
                    i), p1.GetBinError(i)
                n = g1.GetN()
                g1.SetPoint(n,
                            p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(i))
                g1.SetPointError(n, 0, p1.GetBinError(i))

        p1.Draw("axis")
        g1.Draw('Psame')

        fun1 = TF1('fun1', '0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1])))',
                   0.05, 0.3)
        fun1.SetParameter(0, 0.155)
        fun1.SetParameter(1, 0.005)

        g1.Fit(fun1)
        fun1a = g1.GetFunction('fun1')

        fun1a.SetLineColor(2)

        v0 = fun1a.GetParameter(0)
        e0 = fun1a.GetParError(0)
        v1 = fun1a.GetParameter(1)
        e1 = fun1a.GetParError(1)

        print v0, v1

        fUnit = 1000.
        self.lt.DrawLatexNDC(
            0.185, 0.89,
            '#mu = {0:.1f} #pm {1:.1f} mV'.format(v0 * fUnit, e0 * fUnit))
        self.lt.DrawLatexNDC(
            0.185, 0.84,
            '#sigma = {0:.1f} #pm {1:.1f} mV'.format(v1 * fUnit, e1 * fUnit))
        if self.Info:
            self.lt.DrawLatexNDC(0.185, 0.6, self.Info)

        print 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1)
        fun2 = TF1('gaus1', 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1))
        fun2.SetLineColor(4)
        fun2.SetLineStyle(2)
        fun2.Draw('same')

        lg = TLegend(0.7, 0.4, 0.95, 0.5)
        lg.SetFillStyle(0)
        lg.AddEntry(p1, 'Measurement', 'p')
        lg.AddEntry(fun1a, 'Fit', 'l')
        lg.AddEntry(fun2, 'Gaus', 'l')
        lg.Draw()

        waitRootCmdX()
Пример #17
0
def showENC():
    fname1 = '/data/repos/Mic4Test_KC705/Software/Analysis/data/ENC/ENC_Chip5Col12_scan1.dat'

    tree1 = TTree()
    tree1.ReadFile(
        '/data/repos/Mic4Test_KC705/Software/Analysis/data/ENC/ENC_Chip5Col12_scan1.dat',
        'idX/i:vL/F:vH:A:D/i:R:W')
    tree1.ReadFile(
        '/data/repos/Mic4Test_KC705/Software/Analysis/data/ENC/ENC_Chip5Col12_scan2_mod.dat'
    )

    tree1.Show(500)

    p1 = TProfile('p1', 'p1;#DeltaU [V];Prob', 50, 0.12, 0.2)
    tree1.Draw("D:(vH-vL)>>p1", "", "profE")

    ### change it to tgraph
    g1 = TGraphErrors()
    for i in range(p1.GetNbinsX() + 2):
        N = p1.GetBinEntries(i)
        if N > 0:
            print i, N, p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(
                i), p1.GetBinError(i)
            n = g1.GetN()
            g1.SetPoint(n, p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(i))
            g1.SetPointError(n, 0, p1.GetBinError(i))


#     g1.SetMarkerColor(3)
#     g1.SetLineColor(3)

    p1.Draw("axis")
    g1.Draw('Psame')

    fun1 = TF1('fun1', '0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1])))',
               0.05, 0.3)
    fun1.SetParameter(0, 0.155)
    fun1.SetParameter(1, 0.005)

    g1.Fit(fun1)
    fun1a = g1.GetFunction('fun1')

    #     p1.Fit(fun1)
    #     fun1a = p1.GetFunction('fun1')
    fun1a.SetLineColor(2)

    #     p1.Draw("Esame")

    v0 = fun1a.GetParameter(0)
    e0 = fun1a.GetParError(0)
    v1 = fun1a.GetParameter(1)
    e1 = fun1a.GetParError(1)

    print v0, v1

    fUnit = 1000.
    lt = TLatex()
    lt.DrawLatexNDC(
        0.185, 0.89,
        '#mu = {0:.1f} #pm {1:.1f} mV'.format(v0 * fUnit, e0 * fUnit))
    lt.DrawLatexNDC(
        0.185, 0.84,
        '#sigma = {0:.1f} #pm {1:.1f} mV'.format(v1 * fUnit, e1 * fUnit))

    print 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1)
    fun2 = TF1('gaus1', 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1))
    fun2.SetLineColor(4)
    fun2.SetLineStyle(2)
    fun2.Draw('same')

    lg = TLegend(0.7, 0.4, 0.95, 0.5)
    lg.SetFillStyle(0)
    lg.AddEntry(p1, 'Measurement', 'p')
    lg.AddEntry(fun1a, 'Fit', 'l')
    lg.AddEntry(fun2, 'Gaus', 'l')
    lg.Draw()

    waitRootCmdX()
Пример #18
0
    def ApplyBinShiftCorrection(self, hist):
        """
        Apply bin-shift correction to the input spectrum using an iterative procedure
        @param hist: Input spectrum
        @return: Bin-shift corrected spectrum 
        """

        h = deepcopy(hist)
        h.SetName("htemp")

        # Bin shift correction performed in model specturm * pt
        for i in range(1, h.GetNbinsX() + 1):
            pt = h.GetBinCenter(i)
            h.SetBinContent(i, h.GetBinContent(i) * pt)
            h.SetBinError(i, h.GetBinError(i) * pt)

        result = TGraphErrors(h)
        for i in range(0, result.GetN()):
            result.GetEX()[i] = 0.

        fitfun = TF1("fitfun", "([0]*(1.+x/[1])^(-[2])*x)-[3]", 0.15, 100.0)
        fitfun.SetParameter(0, 1000)
        fitfun.SetParameter(1, 1)
        fitfun.SetParameter(2, 5)
        fitfun.FixParameter(3, 0)
        h.Fit(fitfun, "")
        self.__StableFit(h, fitfun, True)

        # Iterative approach:
        # - Use model to get the mean of the function inside the bin
        # - Get the X where the mean is found
        # - Use the new coordinate (x,y) for the next iteration of the fit
        # for now 10 iterations fixed
        for k in range(1, 11):
            for i in range(1, h.GetNbinsX() + 1):
                y = fitfun.Integral(h.GetBinLowEdge(i),
                                    h.GetBinUpEdge(i)) / h.GetBinWidth(i)
                result.GetX()[i - 1] = self.FindX(y, fitfun,
                                                  h.GetBinLowEdge(i),
                                                  h.GetBinUpEdge(i))
            self.__StableFit(result, fitfun, False)

        # Undo multiplication with pt
        for i in range(0, result.GetN()):
            pt = result.GetX()[i]
            result.GetY()[i] /= pt
            result.GetEY()[i] /= pt

        #remove points that are 0
        while result.GetY()[0] < 1.e-99:
            result.RemovePoint(0)

        bval = 0
        for mybin in range(0, result.GetN() + 1):
            if result.GetY()[bin] < 1.e-99:
                bval = mybin
                break

        while result.RemovePoint(bval) > 0:
            continue
        return result
Пример #19
0
def showDAC(fname, Infox=None, saveName='temp_figs/test', mode=1):

    lines = None
    with open(fname, 'r') as f1:
        lines = f1.readlines()

    gr1 = TGraphErrors()
    gr2 = TGraph()
    gr3 = TGraph()
    gr4 = TGraph()

    largeError = -1
    largeErrorI = None
    largeErrorMS = None

    for line in lines:
        line = line.rstrip()
        if len(line) == 0:
            continue
        elif line[0] == '#':
            fs = line.split()
            code = int(fs[3][5:], 16)
            print code
        else:
            ms = [float(x) for x in line.split(',')][1::2]
            if mode == 0: ms = [float(line)]
            mean = nm.mean(ms)
            error = nm.std(ms)

            if error > largeError:
                largeError = error
                largeErrorI = code
                largeErrorMS = ms

            gr1.SetPoint(code, code, mean)
            gr1.SetPointError(code, 0, error)

    print largeErrorI, largeErrorMS, nm.mean(largeErrorMS), nm.mean(ms)
    for i in range(len(largeErrorMS)):
        gr4.SetPoint(i, i, largeErrorMS[i])

    N = gr1.GetN()
    Ys = gr1.GetY()
    print Ys[0], Ys[N - 1], Ys[largeErrorI]
    LSB = (Ys[N - 1] - Ys[0]) / (N - 1)
    print LSB
    for i in range(N):
        gr2.SetPoint(i, i, Ys[i] - (Ys[0] + i * LSB))
        gr3.SetPoint(i, i, Ys[i] - Ys[i - 1] - LSB if i > 0 else 0)

    line = TLine()
    lt = TLatex()

    cav1 = TCanvas('cav1', 'cav1', 1000, 800)
    cav1.Divide(2, 2)
    cav1.cd(1)
    gr1.Draw('AP')
    h1 = gr1.GetHistogram()
    h1.GetXaxis().SetTitle('Code')
    h1.GetYaxis().SetTitle('U [V]')
    ln1 = line.DrawLine(0, Ys[0], N - 1, Ys[N - 1])
    ln1.SetLineColor(2)

    rgInfo = '[{0:.2g},{1:.2g}] V'.format(Ys[0], Ys[N - 1])
    if Infox:
        rgInfo = Infox + ': ' + rgInfo
    lt.DrawLatexNDC(0.2, 0.85, rgInfo)

    cav1.cd(2)
    gr4.SetMarkerStyle(20)
    gr4.Draw('AP')
    h4 = gr4.GetHistogram()
    h4.GetXaxis().SetTitle('#it{i}th')
    h4.GetYaxis().SetTitle('U [V]')
    lt.DrawLatexNDC(
        0.2, 0.85,
        "{0:d} measurements for code={1:d}".format(len(largeErrorMS),
                                                   largeErrorI))

    cav1.cd(3)
    gr2.SetFillColor(2)
    gr2.Draw('APB')
    h2 = gr2.GetHistogram()
    h2.GetXaxis().SetTitle('Code')
    h2.GetYaxis().SetTitle('INL [V]')

    lY = LSB if gr2.GetMean(2) > 0 else -LSB
    ln2 = line.DrawLine(0, lY, N, lY)
    ln2.SetLineStyle(2)

    lt.DrawLatexNDC(
        0.6, 0.85,
        "Max INL={0:.1f} LSB".format(max([abs(x) for x in gr2.GetY()]) / LSB))

    cav1.cd(4)
    gr3.SetFillColor(4)
    gr3.Draw('APB')
    h3 = gr3.GetHistogram()
    h3.GetXaxis().SetTitle('Code')
    h3.GetYaxis().SetTitle('DNL [V]')

    lY = LSB if gr3.GetMean(2) > 0 else -LSB
    ln3 = line.DrawLine(0, lY, N, lY)
    ln3.SetLineStyle(2)
    lt.DrawLatexNDC(
        0.6, 0.85,
        "Max DNL={0:.1f} LSB".format(max([abs(x) for x in gr3.GetY()]) / LSB))

    cav1.cd()
    waitRootCmdX(saveName)
def studyVqqResolution(rootFile):

    #get all from file
    histos = {}
    inF = TFile.Open(rootFile)
    keys = inF.GetListOfKeys()
    for k in keys:
        obj = inF.Get(k.GetName())
        obj.SetDirectory(0)
        histos[k.GetName()] = obj
    inF.Close()

    #plot
    gROOT.SetBatch()
    gROOT.SetStyle('Plain')
    gStyle.SetOptStat(0)
    gStyle.SetOptFit(1111)
    gStyle.SetOptTitle(0)
    gStyle.SetStatFont(42)

    kin = ['', '30to40', '40to50', '50to75', '75to100', '100toInf']
    for k in kin:
        c = TCanvas('c', 'c', 600, 600)
        c.cd()
        c.SetCanvasSize(1000, 500)
        c.SetWindowSize(1000, 500)
        c.Divide(2, 1)
        c.cd(1)
        histos['deta' + k + 'barrel'].SetLineWidth(2)
        histos['deta' + k + 'barrel'].SetTitle('barrel')
        histos['deta' + k + 'barrel'].Draw('hist')
        histos['deta' + k + 'endcap'].SetLineWidth(2)
        histos['deta' + k + 'endcap'].SetLineStyle(7)
        histos['deta' + k + 'endcap'].SetTitle('endcap')
        histos['deta' + k + 'endcap'].Draw('histsame')
        leg = TLegend(0.6, 0.92, 0.9, 0.98)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.SetTextFont(42)
        leg.AddEntry(histos['deta' + k + 'barrel'], 'barrel', 'f')
        leg.AddEntry(histos['deta' + k + 'endcap'], 'endcap', 'f')
        leg.SetNColumns(2)
        leg.Draw()
        drawHeader()
        c.cd(2)
        histos['dphi' + k + 'barrel'].SetLineWidth(2)
        histos['dphi' + k + 'barrel'].SetTitle('barrel')
        histos['dphi' + k + 'barrel'].Draw('hist')
        histos['dphi' + k + 'endcap'].SetLineWidth(2)
        histos['dphi' + k + 'endcap'].SetLineStyle(7)
        histos['dphi' + k + 'endcap'].SetTitle('endcap')
        histos['dphi' + k + 'endcap'].Draw('histsame')
        c.Modified()
        c.Update()
        c.SaveAs('dr_%s.png' % k)

    labels = []
    responseVars = ['dpt', 'den', 'dphi', 'deta', 'dr']
    for r in responseVars:
        barrelResponse = TGraphErrors()
        barrelResponse.SetName(r + 'barrelresponse')
        barrelResponse.SetLineWidth(2)
        barrelResponse.SetFillStyle(0)
        barrelResponse.SetMarkerStyle(20)
        barrelCoreResponse = barrelResponse.Clone(r + 'barrelcoreresponse')
        endcapResponse = TGraphErrors()
        endcapResponse.SetName(r + 'endcapresponse')
        endcapResponse.SetLineWidth(2)
        endcapResponse.SetFillStyle(0)
        endcapResponse.SetMarkerStyle(24)
        endcapCoreResponse = endcapResponse.Clone(r + 'endcapresponse')
        for k in kin:
            c.cd()
            c.Clear()
            c.SetWindowSize(1000, 500)
            c.Divide(2, 1)
            for i in [1, 2]:
                c.cd(i)
                reg = 'barrel'
                if i == 2: reg = 'endcap'

                h = histos[r + k + reg]
                x = RooRealVar("x",
                               h.GetXaxis().GetTitle(),
                               h.GetXaxis().GetXmin(),
                               h.GetXaxis().GetXmax())
                data = RooDataHist("data", "dataset with x", RooArgList(x), h)
                frame = x.frame()
                RooAbsData.plotOn(data, frame,
                                  RooFit.DataError(RooAbsData.SumW2))

                mean1 = RooRealVar("mean1", "mean1", 0, -0.5, 0.5)
                sigma1 = RooRealVar("sigma1", "sigma1", 0.1, 0.01, 1.0)
                gauss1 = RooGaussian("g1", "g", x, mean1, sigma1)

                if r == 'dpt' or r == 'den':
                    mean2 = RooRealVar("mean2", "mean2", 0, -0.5, 0.5)
                    sigma2 = RooRealVar("sigma2", "sigma2", 0.1, 0.01, 1.0)
                    alphacb = RooRealVar("alphacb", "alphacb", 1, 0.1, 3)
                    ncb = RooRealVar("ncb", "ncb", 4, 1, 100)
                    gauss2 = RooCBShape("cb2", "cb", x, mean2, sigma2, alphacb,
                                        ncb)
                else:
                    mean1.setRange(0, 0.5)
                    mean2 = RooRealVar("mean2", "mean", 0, 0, 1)
                    sigma2 = RooRealVar("sigma2", "sigma", 0.1, 0.01, 1.0)
                    gauss2 = RooGaussian("g2", "g", x, mean2, sigma2)

                frac = RooRealVar("frac", "fraction", 0.9, 0.0, 1.0)
                if data.sumEntries() < 100:
                    frac.setVal(1.0)
                    frac.setConstant(True)
                model = RooAddPdf("sum", "g1+g2", RooArgList(gauss1, gauss2),
                                  RooArgList(frac))

                status = model.fitTo(data, RooFit.Save()).status()
                if status != 0: continue

                model_cdf = model.createCdf(RooArgSet(x))
                cl = 0.90
                ul = 0.5 * (1.0 + cl)
                closestToCL = 1.0
                closestToUL = -1
                closestToMedianCL = 1.0
                closestToMedian = -1
                for ibin in xrange(1, h.GetXaxis().GetNbins() * 10):
                    xval = h.GetXaxis().GetXmin() + (
                        ibin - 1) * h.GetXaxis().GetBinWidth(ibin) / 10.
                    x.setVal(xval)
                    cdfValToCL = math.fabs(model_cdf.getVal() - ul)
                    if cdfValToCL < closestToCL:
                        closestToCL = cdfValToCL
                        closestToUL = xval
                    cdfValToCL = math.fabs(model_cdf.getVal() - 0.5)
                    if cdfValToCL < closestToMedianCL:
                        closestToMedianCL = cdfValToCL
                        closestToMedian = xval

                RooAbsPdf.plotOn(model, frame)
                frame.Draw()

                if i == 1: drawHeader()
                labels.append(TPaveText(0.6, 0.92, 0.9, 0.98, 'brNDC'))
                ilab = len(labels) - 1
                labels[ilab].SetName(r + k + 'txt')
                labels[ilab].SetBorderSize(0)
                labels[ilab].SetFillStyle(0)
                labels[ilab].SetTextFont(42)
                labels[ilab].SetTextAlign(12)
                kinReg = k.replace('to', '-')
                kinReg = kinReg.replace('Inf', '#infty')
                labels[ilab].AddText('[' + reg + '] ' + kinReg)
                labels[ilab].Draw()

                resolutionVal = math.fabs(closestToUL - closestToMedian)
                responseGr = barrelResponse
                responseCoreGr = barrelCoreResponse
                coreResolutionVal = sigma1.getVal()
                coreResolutionErr = sigma1.getError()
                if frac.getVal() < 0.7 and (sigma2.getVal() < sigma1.getVal()):
                    coreResolutionVal = sigma2.getVal()
                    coreResolutionErr = sigma2.getError()

                if i == 2:
                    responseGr = endcapResponse
                    responseCoreGr = endcapCoreResponse
                if k != '':
                    nrespPts = responseGr.GetN()
                    kinAvg = 150
                    kinWidth = 50
                    if k == '30to40':
                        kinAvg = 35
                        kinWidth = 5
                    if k == '40to50':
                        kinAvg = 45
                        kinWidth = 5
                    if k == '50to75':
                        kinAvg = 62.5
                        kinWidth = 12.5
                    elif k == '75to100':
                        kinAvg = 87.5
                        kinWidth = 12.5
                    responseGr.SetPoint(nrespPts, kinAvg, resolutionVal)
                    responseCoreGr.SetPoint(nrespPts, kinAvg,
                                            coreResolutionVal)
                    responseCoreGr.SetPointError(nrespPts, kinWidth,
                                                 coreResolutionErr)

                labels.append(TPaveText(0.15, 0.7, 0.4, 0.9, 'brNDC'))
                ilab = len(labels) - 1
                labels[ilab].SetName(r + k + 'fitrestxt')
                labels[ilab].SetBorderSize(0)
                labels[ilab].SetFillStyle(0)
                labels[ilab].SetTextFont(42)
                labels[ilab].SetTextAlign(12)
                labels[ilab].AddText('Gaussian #1 (f=%3.3f)' % frac.getVal())
                labels[ilab].AddText('#mu=%3.3f#pm%3.3f' %
                                     (mean1.getVal(), mean1.getError()))
                labels[ilab].AddText('#sigma=%3.3f#pm%3.3f' %
                                     (sigma1.getVal(), sigma1.getError()))
                labels[ilab].AddText('Gaussian #2 (f=%3.3f)' %
                                     (1 - frac.getVal()))
                labels[ilab].AddText('#mu=%3.3f#pm%3.3f' %
                                     (mean2.getVal(), mean2.getError()))
                labels[ilab].AddText('#sigma=%3.3f#pm%3.3f' %
                                     (sigma2.getVal(), sigma2.getError()))

                labels[ilab].Draw()

            c.Modified()
            c.Update()
            c.SaveAs(r + 'res_' + k + '.png')

        frame = TGraphErrors()
        frame.SetPoint(0, 0, 0)
        frame.SetPoint(1, 200, 0.3)
        frame.SetMarkerStyle(1)
        frame.SetFillStyle(0)
        frame.SetName('frame')
        cresp = TCanvas('cresp', 'cresp', 500, 500)
        cresp.cd()
        frame.Draw('ap')
        barrelResponse.Draw('pl')
        endcapResponse.Draw('pl')
        frame.GetXaxis().SetTitle("Quark transverse momentum [GeV]")
        frame.GetYaxis().SetTitle("Resolution %3.2f C.L." % cl)
        frame.GetYaxis().SetTitleOffset(1.4)
        frame.GetYaxis().SetNdivisions(10)
        drawHeader()
        leg = TLegend(0.6, 0.92, 0.9, 0.98)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.SetTextFont(42)
        leg.AddEntry(barrelResponse, 'barrel', 'fp')
        leg.AddEntry(endcapResponse, 'endcap', 'fp')
        leg.SetNColumns(2)
        leg.Draw()
        cresp.Modified()
        cresp.Update()
        cresp.SaveAs(r + 'res_evol.png')

        frameCore = frame.Clone('framecore')
        cresp.Clear()
        frameCore.Draw('ap')
        barrelCoreResponse.Draw('pl')
        endcapCoreResponse.Draw('pl')
        frameCore.GetXaxis().SetTitle("Quark transverse momentum [GeV]")
        frameCore.GetYaxis().SetTitle("Core resolution")
        frameCore.GetYaxis().SetTitleOffset(1.4)
        frameCore.GetYaxis().SetNdivisions(10)
        frameCore.GetYaxis().SetRangeUser(0, 0.2)
        drawHeader()
        leg = TLegend(0.6, 0.92, 0.9, 0.98)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.SetTextFont(42)
        leg.AddEntry(barrelCoreResponse, 'barrel', 'fp')
        leg.AddEntry(endcapCoreResponse, 'endcap', 'fp')
        leg.SetNColumns(2)
        leg.Draw()
        cresp.Modified()
        cresp.Update()
        cresp.SaveAs(r + 'rescore_evol.png')

    bosons = ['h', 'z', 'w']
    kin = ['', '50', '100']
    region = ['', 'bb', 'eb', 'ee']
    for k in kin:
        for r in region:

            c = TCanvas('c', 'c', 600, 600)
            c.cd()
            histos['mjj' + k + r].Rebin()
            histos['mjj' + k + r].Draw()
            ic = 1
            leg = TLegend(0.6, 0.92, 0.9, 0.98)
            leg.SetFillStyle(0)
            leg.SetBorderSize(0)
            leg.SetTextFont(42)
            leg.AddEntry(histos['mjj' + k + r], 'inclusive', 'f')
            for b in bosons:
                if histos[b + 'mjj' + k + r].Integral() <= 0: continue
                ic = ic + 1
                histos[b + 'mjj' + k + r].Rebin()
                histos[b + 'mjj' + k + r].SetLineColor(ic)
                histos[b + 'mjj' + k + r].SetLineWidth(2)
                histos[b + 'mjj' + k + r].SetMarkerColor(ic)
                histos[b + 'mjj' + k + r].SetMarkerStyle(1)
                histos[b + 'mjj' + k + r].SetFillStyle(3000 + ic)
                histos[b + 'mjj' + k + r].SetFillColor(ic)
                histos[b + 'mjj' + k + r].Draw('histsame')
                leg.AddEntry(histos[b + 'mjj' + k + r], b, "f")
            leg.SetNColumns(ic)
            leg.Draw()
            drawHeader()
            labels.append(TPaveText(0.65, 0.8, 0.9, 0.9, 'brNDC'))
            ilab = len(labels) - 1
            labels[ilab].SetName(k + r + 'mjj')
            labels[ilab].SetBorderSize(0)
            labels[ilab].SetFillStyle(0)
            labels[ilab].SetTextFont(42)
            labels[ilab].SetTextAlign(12)
            regionTitle = "inclusive"
            if r == 'bb': regionTitle = 'barrel-barrel'
            if r == 'eb': regionTitle = 'endcap-barrel'
            if r == 'ee': regionTitle = 'endcap-endcap'
            labels[ilab].AddText(regionTitle)
            ptthreshold = 30
            if k != '': ptthreshold = float(k)
            labels[ilab].AddText('p_{T}>%3.0f GeV' % ptthreshold)
            labels[ilab].Draw()

            c.Modified()
            c.Update()
            c.SaveAs('mjj' + k + r + '.png')

    massResolutionGrs = []
    for r in region:
        massResolution = TGraphErrors()
        massResolution.SetName(r + 'dm')
        massResolution.SetLineWidth(2)
        massResolution.SetFillStyle(0)
        massResolution.SetMarkerStyle(20 + len(massResolutionGrs))
        massResolution.SetMarkerColor(1 + len(massResolutionGrs))
        massResolution.SetLineColor(1 + len(massResolutionGrs))
        massResolution.SetFillColor(1 + len(massResolutionGrs))
        massResolutionGrs.append(massResolution)

        for k in kin:

            c = TCanvas('c', 'c', 600, 600)
            c.cd()
            h = histos['dmjj' + k + r]
            x = RooRealVar("x",
                           h.GetXaxis().GetTitle(),
                           h.GetXaxis().GetXmin(),
                           h.GetXaxis().GetXmax())
            data = RooDataHist("data", "dataset with x", RooArgList(x), h)
            frame = x.frame()
            RooAbsData.plotOn(data, frame, RooFit.DataError(RooAbsData.SumW2))

            mean1 = RooRealVar("mean1", "mean1", 0, -0.5, 0.5)
            sigma1 = RooRealVar("sigma1", "sigma1", 0.1, 0.01, 1.0)
            gauss1 = RooGaussian("g1", "g", x, mean1, sigma1)
            mean2 = RooRealVar("mean2", "mean2", 0, -0.5, 0.5)
            sigma2 = RooRealVar("sigma2", "sigma2", 0.1, 0.01, 1.0)
            alphacb = RooRealVar("alphacb", "alphacb", 1, 0.1, 3)
            ncb = RooRealVar("ncb", "ncb", 4, 1, 100)
            gauss2 = RooCBShape("cb2", "cb", x, mean2, sigma2, alphacb, ncb)
            frac = RooRealVar("frac", "fraction", 0.9, 0.0, 1.0)
            model = RooAddPdf("sum", "g1+g2", RooArgList(gauss1, gauss2),
                              RooArgList(frac))
            status = model.fitTo(data, RooFit.Save()).status()
            if status != 0: continue
            RooAbsPdf.plotOn(model, frame)
            frame.Draw()

            labels.append(TPaveText(0.6, 0.65, 0.85, 0.9, 'brNDC'))
            ilab = len(labels) - 1
            labels[ilab].SetName(r + k + 'dmfitrestxt')
            labels[ilab].SetBorderSize(0)
            labels[ilab].SetFillStyle(0)
            labels[ilab].SetTextFont(42)
            labels[ilab].SetTextAlign(12)
            labels[ilab].AddText('Gaussian #1 (f=%3.3f)' % frac.getVal())
            labels[ilab].AddText('#mu=%3.3f#pm%3.3f' %
                                 (mean1.getVal(), mean1.getError()))
            labels[ilab].AddText('#sigma=%3.3f#pm%3.3f' %
                                 (sigma1.getVal(), sigma1.getError()))
            labels[ilab].AddText('Gaussian #2 (f=%3.3f)' % (1 - frac.getVal()))
            labels[ilab].AddText('#mu=%3.3f#pm%3.3f' %
                                 (mean2.getVal(), mean2.getError()))
            labels[ilab].AddText('#sigma=%3.3f#pm%3.3f' %
                                 (sigma2.getVal(), sigma2.getError()))
            labels[ilab].Draw()

            drawHeader()
            labels.append(TPaveText(0.15, 0.8, 0.4, 0.9, 'brNDC'))
            ilab = len(labels) - 1
            labels[ilab].SetName(k + r + 'dmjj')
            labels[ilab].SetBorderSize(0)
            labels[ilab].SetFillStyle(0)
            labels[ilab].SetTextFont(42)
            labels[ilab].SetTextAlign(12)
            regionTitle = "inclusive"
            if r == 'bb': regionTitle = 'barrel-barrel'
            if r == 'eb': regionTitle = 'endcap-barrel'
            if r == 'ee': regionTitle = 'endcap-endcap'
            labels[ilab].AddText(regionTitle)
            ptthreshold = 30
            if k != '': ptthreshold = float(k)
            labels[ilab].AddText('p_{T}>%3.0f GeV' % ptthreshold)
            labels[ilab].Draw()

            c.Modified()
            c.Update()
            c.SaveAs('dmjj' + k + r + '.png')

            massResolution.SetTitle(regionTitle)
            ip = massResolution.GetN()
            x = 40
            xerr = 10
            if k == '50':
                x = 75
                xerr = 25
            elif k == '100':
                x = 150
                xerr = 50
            y = sigma1.getVal()
            yerr = sigma1.getError()
            if frac.getVal() < 0.8:
                if sigma2.getVal() < sigma1.getVal():
                    y = sigma2.getVal()
                    ey = sigma2.getError()
            massResolution.SetPoint(ip, x, y)
            massResolution.SetPointError(ip, xerr, yerr)

    frame = TGraphErrors()
    frame.SetPoint(0, 0, 0)
    frame.SetPoint(1, 200, 0.2)
    frame.SetMarkerStyle(1)
    frame.SetFillStyle(0)
    frame.SetName('dmframe')
    cdmevol = TCanvas('cdmevol', 'cdmevol', 500, 500)
    cdmevol.cd()
    frame.Draw('ap')
    leg = TLegend(0.6, 0.92, 0.9, 0.98)
    leg.SetFillStyle(0)
    leg.SetBorderSize(0)
    leg.SetTextFont(42)
    for dmGr in massResolutionGrs:
        dmGr.Draw('pl')
        leg.AddEntry(dmGr, dmGr.GetTitle(), 'fp')
    frame.GetXaxis().SetTitle("Leading quark transverse momentum [GeV]")
    frame.GetYaxis().SetTitle("Core resolution")
    frame.GetYaxis().SetTitleOffset(1.4)
    frame.GetYaxis().SetNdivisions(10)
    drawHeader()
    leg.SetNColumns(2)
    leg.Draw()
    cdmevol.Modified()
    cdmevol.Update()
    cdmevol.SaveAs('dm_evol.png')

    c = TCanvas('c', 'c', 600, 600)
    c.cd()
    histos['sel'].Draw('histtext')
    drawHeader()
    c.Modified()
    c.Update()
    c.SaveAs('selection.png')

    return
Пример #21
0
def acceptance(year):
    genPoints = [
        1800, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 7000, 8000
    ]

    treeSign = {}
    ngenSign = {}
    nevtSign = {}
    nevtSign_eta = {}
    nevtSign_dEta = {}
    eff = TGraphErrors()
    eff_eta = TGraphErrors()
    eff_dEta = TGraphErrors()

    for i, m in enumerate(genPoints):
        ngenSign[m] = 0.
        nevtSign[m] = 0.
        nevtSign_eta[m] = 0.
        nevtSign_dEta[m] = 0.

        if year == "run2":
            years = ['2016', '2017', '2018']
        else:
            years = [year]

        for yr in years:
            signName = "MC_signal_{}_M{}".format(yr, m)
            sfile = TFile(ACCEPTANCEDIR + signName + "_acceptanceHist.root",
                          "READ")

            ngenSign[m] += sample["ZpBB_M" + str(m)]['genEvents'][yr]

            #all_events_hist = sfile.Get('all_events')
            #nEvents = all_events_hist.GetBinContent(1)
            #ngenSign[m] += nEvents

            passing_events_hist = sfile.Get('passing')
            eta_flag_hist = sfile.Get('eta_flag')
            dEta_flag_hist = sfile.Get('dEta_flag')

            nEvents = passing_events_hist.GetBinContent(1)
            nEvents_eta = eta_flag_hist.GetBinContent(1)
            nEvents_dEta = dEta_flag_hist.GetBinContent(1)

            nevtSign[m] += nEvents
            nevtSign_eta[m] += nEvents_eta
            nevtSign_dEta[m] += nEvents_dEta

            sfile.Close()

        print m, ":", nevtSign[m], "/", ngenSign[
            m], "=", nevtSign[m] / ngenSign[m]
        if nevtSign[m] == 0 or ngenSign[m] < 0: continue
        n = eff.GetN()
        eff.SetPoint(n, m, nevtSign[m] / ngenSign[m])
        eff.SetPointError(n, 0, math.sqrt(nevtSign[m]) / ngenSign[m])
        eff_eta.SetPoint(n, m, nevtSign_eta[m] / ngenSign[m])
        eff_eta.SetPointError(n, 0, math.sqrt(nevtSign_eta[m]) / ngenSign[m])
        eff_dEta.SetPoint(n, m, nevtSign_dEta[m] / ngenSign[m])
        eff_dEta.SetPointError(n, 0, math.sqrt(nevtSign_dEta[m]) / ngenSign[m])

    eff.SetMarkerColor(4)
    eff.SetMarkerStyle(24)
    eff.SetMarkerSize(2)
    eff.SetLineColor(4)
    eff.SetLineWidth(3)
    eff_eta.SetMarkerColor(2)
    eff_eta.SetMarkerStyle(23)
    eff_eta.SetMarkerSize(2)
    eff_eta.SetLineColor(2)
    eff_eta.SetLineWidth(2)
    eff_eta.SetLineStyle(2)
    eff_dEta.SetMarkerColor(418)
    eff_dEta.SetMarkerStyle(23)
    eff_dEta.SetMarkerSize(2)
    eff_dEta.SetLineColor(418)
    eff_dEta.SetLineWidth(2)
    eff_dEta.SetLineStyle(2)

    n = eff.GetN()
    maxEff = 0.

    leg = TLegend(0.15, 0.7, 0.95, 0.8)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    #leg.SetY1(leg.GetY2()-len([x for x in channels if eff[x].GetN() > 0])/2.*0.045)

    leg.AddEntry(eff, "total")
    leg.AddEntry(eff_eta, "|#eta|<2.5")
    leg.AddEntry(eff_dEta, "#Delta#eta<1.1")

    #legS = TLegend(0.5, 0.85-0.045, 0.9, 0.85)
    #legS.SetBorderSize(0)
    #legS.SetFillStyle(0) #1001
    #legS.SetFillColor(0)
    #legS.AddEntry(eff['sum'], "Total efficiency (1 b tag + 2 b tag)", "pl")

    c1 = TCanvas("c1", "Signal Acceptance", 1200, 800)
    c1.cd(1)
    eff.Draw("APL")
    eff_eta.Draw("SAME, PL")
    eff_dEta.Draw("SAME, PL")
    leg.Draw()
    #legS.Draw()
    #setHistStyle(eff["sum"], 1.1)
    eff.SetTitle(";m_{Z'} (GeV);Acceptance")
    eff.SetMinimum(0.)
    eff.SetMaximum(max(1.5, maxEff * 1.5))  #0.65

    eff.GetXaxis().SetTitleSize(0.045)
    eff.GetYaxis().SetTitleSize(0.045)
    eff.GetYaxis().SetTitleOffset(1.1)
    eff.GetXaxis().SetTitleOffset(1.05)
    eff.GetXaxis().SetRangeUser(1500, 8000)
    c1.SetTopMargin(0.05)
    #drawCMS(-1, "Simulation Preliminary", year=year) #Preliminary
    #drawCMS(-1, "Work in Progress", year=year, suppressCMS=True)
    drawCMS(-1, "", year=year, suppressCMS=True)
    drawAnalysis("")

    c1.Print("plots/Efficiency/" + year + "_Acceptance.pdf")
    c1.Print("plots/Efficiency/" + year + "_Acceptance.png")
Пример #22
0
            x[0] - steps[0] - steps[1] - steps[2])
    return tax / x[0] * 100.


data = []
data.append([10, [0, 8025]])
data.append([15, [8025, 32550]])
data.append([25, [32550, 78850]])
data.append([28, [78850, 164550]])
data.append([33, [164550, 357700]])
data.append([35, [357700, 1000000]])

gr = TGraphErrors(0)
for dd in data:
    yVal = dd[0]
    xVal = dd[1]
    xAve = (xVal[0] + xVal[1]) / 2.
    xErr = (xVal[1] - xVal[0]) / 2.
    gr.SetPoint(gr.GetN(), xAve, yVal)
    gr.SetPointError(gr.GetN() - 1, xErr, 0.)

gr.Draw("alp")

canTax = TF1("canTax", canadianTax, 0., 1000000., 0)

canTax.Draw("same")

yy = raw_input("wanna quit?")
if yy == "n":
    print "ok"
Пример #23
0
def signal(category):

    interPar = True
    n = len(genPoints)

    cColor = color[category] if category in color else 4
    nBtag = category.count('b')
    isAH = False  #relict from using Alberto's more complex script

    if not os.path.exists(PLOTDIR + "MC_signal_" + YEAR):
        os.makedirs(PLOTDIR + "MC_signal_" + YEAR)

    #*******************************************************#
    #                                                       #
    #              Variables and selections                 #
    #                                                       #
    #*******************************************************#

    X_mass = RooRealVar("jj_mass_widejet", "m_{jj}", X_min, X_max, "GeV")
    j1_pt = RooRealVar("jpt_1", "jet1 pt", 0., 13000., "GeV")
    jj_deltaEta = RooRealVar("jj_deltaEta_widejet", "", 0., 5.)
    jbtag_WP_1 = RooRealVar("jbtag_WP_1", "", -1., 4.)
    jbtag_WP_2 = RooRealVar("jbtag_WP_2", "", -1., 4.)
    fatjetmass_1 = RooRealVar("fatjetmass_1", "", -1., 2500.)
    fatjetmass_2 = RooRealVar("fatjetmass_2", "", -1., 2500.)
    jid_1 = RooRealVar("jid_1", "j1 ID", -1., 8.)
    jid_2 = RooRealVar("jid_2", "j2 ID", -1., 8.)
    jnmuons_1 = RooRealVar("jnmuons_1", "j1 n_{#mu}", -1., 8.)
    jnmuons_2 = RooRealVar("jnmuons_2", "j2 n_{#mu}", -1., 8.)
    jnmuons_loose_1 = RooRealVar("jnmuons_loose_1", "jnmuons_loose_1", -1., 8.)
    jnmuons_loose_2 = RooRealVar("jnmuons_loose_2", "jnmuons_loose_2", -1., 8.)
    nmuons = RooRealVar("nmuons", "n_{#mu}", -1., 10.)
    nelectrons = RooRealVar("nelectrons", "n_{e}", -1., 10.)
    HLT_AK8PFJet500 = RooRealVar("HLT_AK8PFJet500", "", -1., 1.)
    HLT_PFJet500 = RooRealVar("HLT_PFJet500", "", -1., 1.)
    HLT_CaloJet500_NoJetID = RooRealVar("HLT_CaloJet500_NoJetID", "", -1., 1.)
    HLT_PFHT900 = RooRealVar("HLT_PFHT900", "", -1., 1.)
    HLT_AK8PFJet550 = RooRealVar("HLT_AK8PFJet550", "", -1., 1.)
    HLT_PFJet550 = RooRealVar("HLT_PFJet550", "", -1., 1.)
    HLT_CaloJet550_NoJetID = RooRealVar("HLT_CaloJet550_NoJetID", "", -1., 1.)
    HLT_PFHT1050 = RooRealVar("HLT_PFHT1050", "", -1., 1.)
    #HLT_DoublePFJets100_CaloBTagDeepCSV_p71                 =RooRealVar("HLT_DoublePFJets100_CaloBTagDeepCSV_p71"                , "", -1., 1. )
    #HLT_DoublePFJets116MaxDeta1p6_DoubleCaloBTagDeepCSV_p71 =RooRealVar("HLT_DoublePFJets116MaxDeta1p6_DoubleCaloBTagDeepCSV_p71", "", -1., 1. )
    #HLT_DoublePFJets128MaxDeta1p6_DoubleCaloBTagDeepCSV_p71 =RooRealVar("HLT_DoublePFJets128MaxDeta1p6_DoubleCaloBTagDeepCSV_p71", "", -1., 1. )
    #HLT_DoublePFJets200_CaloBTagDeepCSV_p71                 =RooRealVar("HLT_DoublePFJets200_CaloBTagDeepCSV_p71"                , "", -1., 1. )
    #HLT_DoublePFJets350_CaloBTagDeepCSV_p71                 =RooRealVar("HLT_DoublePFJets350_CaloBTagDeepCSV_p71"                , "", -1., 1. )
    #HLT_DoublePFJets40_CaloBTagDeepCSV_p71                  =RooRealVar("HLT_DoublePFJets40_CaloBTagDeepCSV_p71"                 , "", -1., 1. )

    weight = RooRealVar("eventWeightLumi", "", -1.e9, 1.e9)

    # Define the RooArgSet which will include all the variables defined before
    # there is a maximum of 9 variables in the declaration, so the others need to be added with 'add'
    variables = RooArgSet(X_mass)
    variables.add(
        RooArgSet(j1_pt, jj_deltaEta, jbtag_WP_1, jbtag_WP_2, fatjetmass_1,
                  fatjetmass_2, jnmuons_1, jnmuons_2, weight))
    variables.add(
        RooArgSet(nmuons, nelectrons, jid_1, jid_2, jnmuons_loose_1,
                  jnmuons_loose_2))
    variables.add(
        RooArgSet(HLT_AK8PFJet500, HLT_PFJet500, HLT_CaloJet500_NoJetID,
                  HLT_PFHT900, HLT_AK8PFJet550, HLT_PFJet550,
                  HLT_CaloJet550_NoJetID, HLT_PFHT1050))
    #variables.add(RooArgSet(HLT_DoublePFJets100_CaloBTagDeepCSV_p71, HLT_DoublePFJets116MaxDeta1p6_DoubleCaloBTagDeepCSV_p71, HLT_DoublePFJets128MaxDeta1p6_DoubleCaloBTagDeepCSV_p71, HLT_DoublePFJets200_CaloBTagDeepCSV_p71, HLT_DoublePFJets350_CaloBTagDeepCSV_p71, HLT_DoublePFJets40_CaloBTagDeepCSV_p71))
    X_mass.setRange("X_reasonable_range", X_mass.getMin(), X_mass.getMax())
    X_mass.setRange("X_integration_range", X_mass.getMin(), X_mass.getMax())

    if VARBINS:
        binsXmass = RooBinning(len(abins) - 1, abins)
        X_mass.setBinning(binsXmass)
        plot_binning = RooBinning(
            int((X_mass.getMax() - X_mass.getMin()) / 100.), X_mass.getMin(),
            X_mass.getMax())
    else:
        X_mass.setBins(int((X_mass.getMax() - X_mass.getMin()) / 10))
        binsXmass = RooBinning(int((X_mass.getMax() - X_mass.getMin()) / 100.),
                               X_mass.getMin(), X_mass.getMax())
        plot_binning = binsXmass

    X_mass.setBinning(plot_binning, "PLOT")

    #X_mass.setBins(int((X_mass.getMax() - X_mass.getMin())/10))
    #binsXmass = RooBinning(int((X_mass.getMax() - X_mass.getMin())/100), X_mass.getMin(), X_mass.getMax())
    #X_mass.setBinning(binsXmass, "PLOT")
    massArg = RooArgSet(X_mass)

    # Cuts
    if BTAGGING == 'semimedium':
        SRcut = aliasSM[category]
        #SRcut = aliasSM[category+"_vetoAK8"]
    else:
        SRcut = alias[category].format(WP=working_points[BTAGGING])
        #SRcut = alias[category+"_vetoAK8"].format(WP=working_points[BTAGGING])

    if ADDSELECTION: SRcut += SELECTIONS[options.selection]

    print "  Cut:\t", SRcut

    #*******************************************************#
    #                                                       #
    #                    Signal fits                        #
    #                                                       #
    #*******************************************************#

    treeSign = {}
    setSignal = {}

    vmean = {}
    vsigma = {}
    valpha1 = {}
    vslope1 = {}
    valpha2 = {}
    vslope2 = {}
    smean = {}
    ssigma = {}
    salpha1 = {}
    sslope1 = {}
    salpha2 = {}
    sslope2 = {}
    sbrwig = {}
    signal = {}
    signalExt = {}
    signalYield = {}
    signalIntegral = {}
    signalNorm = {}
    signalXS = {}
    frSignal = {}
    frSignal1 = {}
    frSignal2 = {}
    frSignal3 = {}

    # Signal shape uncertainties (common amongst all mass points)
    xmean_jes = RooRealVar(
        "CMS" + YEAR + "_sig_" + category + "_p1_scale_jes",
        "Variation of the resonance position with the jet energy scale", 0.02,
        -1., 1.)  #0.001
    smean_jes = RooRealVar(
        "CMS" + YEAR + "_sig_" + category + "_p1_jes",
        "Change of the resonance position with the jet energy scale", 0., -10,
        10)

    xsigma_jer = RooRealVar(
        "CMS" + YEAR + "_sig_" + category + "_p2_scale_jer",
        "Variation of the resonance width with the jet energy resolution",
        0.10, -1., 1.)
    ssigma_jer = RooRealVar(
        "CMS" + YEAR + "_sig_" + category + "_p2_jer",
        "Change of the resonance width with the jet energy resolution", 0.,
        -10, 10)

    xmean_jes.setConstant(True)
    smean_jes.setConstant(True)

    xsigma_jer.setConstant(True)
    ssigma_jer.setConstant(True)

    for m in massPoints:

        signalMass = "%s_M%d" % (stype, m)
        signalName = "ZpBB_{}_{}_M{}".format(YEAR, category, m)
        sampleName = "ZpBB_M{}".format(m)

        signalColor = sample[sampleName][
            'linecolor'] if signalName in sample else 1

        # define the signal PDF
        vmean[m] = RooRealVar(signalName + "_vmean", "Crystal Ball mean", m,
                              m * 0.96, m * 1.05)
        smean[m] = RooFormulaVar(signalName + "_mean", "@0*(1+@1*@2)",
                                 RooArgList(vmean[m], xmean_jes, smean_jes))

        vsigma[m] = RooRealVar(signalName + "_vsigma", "Crystal Ball sigma",
                               m * 0.0233, m * 0.019, m * 0.025)
        ssigma[m] = RooFormulaVar(
            signalName + "_sigma", "@0*(1+@1*@2)",
            RooArgList(vsigma[m], xsigma_jer, ssigma_jer))

        valpha1[m] = RooRealVar(
            signalName + "_valpha1", "Crystal Ball alpha 1", 0.2, 0.05, 0.28
        )  # number of sigmas where the exp is attached to the gaussian core. >0 left, <0 right
        salpha1[m] = RooFormulaVar(signalName + "_alpha1", "@0",
                                   RooArgList(valpha1[m]))

        #vslope1[m] = RooRealVar(signalName + "_vslope1", "Crystal Ball slope 1", 10., 0.1, 20.) # slope of the power tail
        vslope1[m] = RooRealVar(signalName + "_vslope1",
                                "Crystal Ball slope 1", 13., 10.,
                                20.)  # slope of the power tail
        sslope1[m] = RooFormulaVar(signalName + "_slope1", "@0",
                                   RooArgList(vslope1[m]))

        valpha2[m] = RooRealVar(signalName + "_valpha2",
                                "Crystal Ball alpha 2", 1.)
        valpha2[m].setConstant(True)
        salpha2[m] = RooFormulaVar(signalName + "_alpha2", "@0",
                                   RooArgList(valpha2[m]))

        #vslope2[m] = RooRealVar(signalName + "_vslope2", "Crystal Ball slope 2", 6., 2.5, 15.) # slope of the higher power tail
        ## FIXME test FIXME
        vslope2_estimation = -5.88111436852 + m * 0.00728809389442 + m * m * (
            -1.65059568762e-06) + m * m * m * (1.25128996309e-10)
        vslope2[m] = RooRealVar(signalName + "_vslope2",
                                "Crystal Ball slope 2", vslope2_estimation,
                                vslope2_estimation * 0.9, vslope2_estimation *
                                1.1)  # slope of the higher power tail
        ## FIXME end FIXME
        sslope2[m] = RooFormulaVar(
            signalName + "_slope2", "@0",
            RooArgList(vslope2[m]))  # slope of the higher power tail

        signal[m] = RooDoubleCrystalBall(signalName,
                                         "m_{%s'} = %d GeV" % ('X', m), X_mass,
                                         smean[m], ssigma[m], salpha1[m],
                                         sslope1[m], salpha2[m], sslope2[m])

        # extend the PDF with the yield to perform an extended likelihood fit
        signalYield[m] = RooRealVar(signalName + "_yield", "signalYield", 50,
                                    0., 1.e15)
        signalNorm[m] = RooRealVar(signalName + "_norm", "signalNorm", 1., 0.,
                                   1.e15)
        signalXS[m] = RooRealVar(signalName + "_xs", "signalXS", 1., 0., 1.e15)
        signalExt[m] = RooExtendPdf(signalName + "_ext", "extended p.d.f",
                                    signal[m], signalYield[m])

        # ---------- if there is no simulated signal, skip this mass point ----------
        if m in genPoints:
            if VERBOSE: print " - Mass point", m

            # define the dataset for the signal applying the SR cuts
            treeSign[m] = TChain("tree")

            if YEAR == 'run2':
                pd = sample[sampleName]['files']
                if len(pd) > 3:
                    print "multiple files given than years for a single masspoint:", pd
                    sys.exit()
                for ss in pd:
                    if not '2016' in ss and not '2017' in ss and not '2018' in ss:
                        print "unknown year given in:", ss
                        sys.exit()
            else:
                pd = [x for x in sample[sampleName]['files'] if YEAR in x]
                if len(pd) > 1:
                    print "multiple files given for a single masspoint/year:", pd
                    sys.exit()

            for ss in pd:

                if options.unskimmed:
                    j = 0
                    while True:
                        if os.path.exists(NTUPLEDIR + ss + "/" + ss +
                                          "_flatTuple_{}.root".format(j)):
                            treeSign[m].Add(NTUPLEDIR + ss + "/" + ss +
                                            "_flatTuple_{}.root".format(j))
                            j += 1
                        else:
                            print "found {} files for sample:".format(j), ss
                            break
                else:
                    if os.path.exists(NTUPLEDIR + ss + ".root"):
                        treeSign[m].Add(NTUPLEDIR + ss + ".root")
                    else:
                        print "found no file for sample:", ss

            if treeSign[m].GetEntries() <= 0.:
                if VERBOSE:
                    print " - 0 events available for mass", m, "skipping mass point..."
                signalNorm[m].setVal(-1)
                vmean[m].setConstant(True)
                vsigma[m].setConstant(True)
                salpha1[m].setConstant(True)
                sslope1[m].setConstant(True)
                salpha2[m].setConstant(True)
                sslope2[m].setConstant(True)
                signalNorm[m].setConstant(True)
                signalXS[m].setConstant(True)
                continue

            #setSignal[m] = RooDataSet("setSignal_"+signalName, "setSignal", variables, RooFit.Cut(SRcut), RooFit.WeightVar("eventWeightLumi*BTagAK4Weight_deepJet"), RooFit.Import(treeSign[m]))
            setSignal[m] = RooDataSet("setSignal_" + signalName, "setSignal",
                                      variables, RooFit.Cut(SRcut),
                                      RooFit.WeightVar(weight),
                                      RooFit.Import(treeSign[m]))
            if VERBOSE:
                print " - Dataset with", setSignal[m].sumEntries(
                ), "events loaded"

            # FIT
            entries = setSignal[m].sumEntries()
            if entries < 0. or entries != entries: entries = 0
            signalYield[m].setVal(entries)
            # Instead of eventWeightLumi
            #signalYield[m].setVal(entries * LUMI / (300000 if YEAR=='run2' else 100000) )

            if treeSign[m].GetEntries(SRcut) > 5:
                if VERBOSE: print " - Running fit"
                frSignal[m] = signalExt[m].fitTo(setSignal[m], RooFit.Save(1),
                                                 RooFit.Extended(True),
                                                 RooFit.SumW2Error(True),
                                                 RooFit.PrintLevel(-1))
                if VERBOSE:
                    print "********** Fit result [", m, "] **", category, "*" * 40, "\n", frSignal[
                        m].Print(), "\n", "*" * 80
                if VERBOSE: frSignal[m].correlationMatrix().Print()
                drawPlot(signalMass + "_" + category, stype + category, X_mass,
                         signal[m], setSignal[m], frSignal[m])

            else:
                print "  WARNING: signal", stype, "and mass point", m, "in category", category, "has 0 entries or does not exist"

            # Remove HVT cross sections
            #xs = getCrossSection(stype, channel, m)
            xs = 1.
            signalXS[m].setVal(xs * 1000.)

            signalIntegral[m] = signalExt[m].createIntegral(
                massArg, RooFit.NormSet(massArg),
                RooFit.Range("X_integration_range"))
            boundaryFactor = signalIntegral[m].getVal()
            if boundaryFactor < 0. or boundaryFactor != boundaryFactor:
                boundaryFactor = 0
            if VERBOSE:
                print " - Fit normalization vs integral:", signalYield[
                    m].getVal(), "/", boundaryFactor, "events"
            signalNorm[m].setVal(boundaryFactor * signalYield[m].getVal() /
                                 signalXS[m].getVal()
                                 )  # here normalize to sigma(X) x Br = 1 [fb]

        vmean[m].setConstant(True)
        vsigma[m].setConstant(True)
        valpha1[m].setConstant(True)
        vslope1[m].setConstant(True)
        valpha2[m].setConstant(True)
        vslope2[m].setConstant(True)
        signalNorm[m].setConstant(True)
        signalXS[m].setConstant(True)

    #*******************************************************#
    #                                                       #
    #                 Signal interpolation                  #
    #                                                       #
    #*******************************************************#

    ### FIXME FIXME just for a test FIXME FIXME

    #print
    #print
    #print "slope2 fit results:"
    #print
    #y_vals = []
    #for m in genPoints:
    #    y_vals.append(vslope2[m].getVal())
    #print "m =", genPoints
    #print "y =", y_vals
    #sys.exit()

    ### FIXME FIXME test end FIXME FIXME

    # ====== CONTROL PLOT ======
    color_scheme = [
        636, 635, 634, 633, 632, 633, 636, 635, 634, 633, 632, 633, 636, 635,
        634, 633, 632, 633, 636, 635, 634, 633, 632, 633, 636, 635, 634, 633,
        632, 633, 636, 635, 634, 633, 632, 633, 636, 635, 634, 633, 632, 633
    ]
    c_signal = TCanvas("c_signal", "c_signal", 800, 600)
    c_signal.cd()
    frame_signal = X_mass.frame()
    for j, m in enumerate(genPoints):
        if m in signalExt.keys():
            #print "color:",(j%9)+1
            #print "signalNorm[m].getVal() =", signalNorm[m].getVal()
            #print "RooAbsReal.NumEvent =", RooAbsReal.NumEvent
            signal[m].plotOn(
                frame_signal, RooFit.LineColor(color_scheme[j]),
                RooFit.Normalization(signalNorm[m].getVal(),
                                     RooAbsReal.NumEvent),
                RooFit.Range("X_reasonable_range"))
    frame_signal.GetXaxis().SetRangeUser(0, 10000)
    frame_signal.Draw()
    drawCMS(-1, "Simulation Preliminary", year=YEAR)
    #drawCMS(-1, "Work in Progress", year=YEAR, suppressCMS=True)
    #drawCMS(-1, "", year=YEAR, suppressCMS=True)
    drawAnalysis(category)
    drawRegion(category)

    c_signal.SaveAs(PLOTDIR + "MC_signal_" + YEAR + "/" + stype + "_" +
                    category + "_Signal.pdf")
    c_signal.SaveAs(PLOTDIR + "MC_signal_" + YEAR + "/" + stype + "_" +
                    category + "_Signal.png")
    #if VERBOSE: raw_input("Press Enter to continue...")
    # ====== CONTROL PLOT ======

    # Normalization
    gnorm = TGraphErrors()
    gnorm.SetTitle(";m_{X} (GeV);integral (GeV)")
    gnorm.SetMarkerStyle(20)
    gnorm.SetMarkerColor(1)
    gnorm.SetMaximum(0)
    inorm = TGraphErrors()
    inorm.SetMarkerStyle(24)
    fnorm = TF1("fnorm", "pol9", 700, 3000)
    fnorm.SetLineColor(920)
    fnorm.SetLineStyle(7)
    fnorm.SetFillColor(2)
    fnorm.SetLineColor(cColor)

    # Mean
    gmean = TGraphErrors()
    gmean.SetTitle(";m_{X} (GeV);gaussian mean (GeV)")
    gmean.SetMarkerStyle(20)
    gmean.SetMarkerColor(cColor)
    gmean.SetLineColor(cColor)
    imean = TGraphErrors()
    imean.SetMarkerStyle(24)
    fmean = TF1("fmean", "pol1", 0, 10000)
    fmean.SetLineColor(2)
    fmean.SetFillColor(2)

    # Width
    gsigma = TGraphErrors()
    gsigma.SetTitle(";m_{X} (GeV);gaussian width (GeV)")
    gsigma.SetMarkerStyle(20)
    gsigma.SetMarkerColor(cColor)
    gsigma.SetLineColor(cColor)
    isigma = TGraphErrors()
    isigma.SetMarkerStyle(24)
    fsigma = TF1("fsigma", "pol1", 0, 10000)
    fsigma.SetLineColor(2)
    fsigma.SetFillColor(2)

    # Alpha1
    galpha1 = TGraphErrors()
    galpha1.SetTitle(";m_{X} (GeV);crystal ball lower alpha")
    galpha1.SetMarkerStyle(20)
    galpha1.SetMarkerColor(cColor)
    galpha1.SetLineColor(cColor)
    ialpha1 = TGraphErrors()
    ialpha1.SetMarkerStyle(24)
    falpha1 = TF1("falpha", "pol1", 0, 10000)  #pol0
    falpha1.SetLineColor(2)
    falpha1.SetFillColor(2)

    # Slope1
    gslope1 = TGraphErrors()
    gslope1.SetTitle(";m_{X} (GeV);exponential lower slope (1/Gev)")
    gslope1.SetMarkerStyle(20)
    gslope1.SetMarkerColor(cColor)
    gslope1.SetLineColor(cColor)
    islope1 = TGraphErrors()
    islope1.SetMarkerStyle(24)
    fslope1 = TF1("fslope", "pol1", 0, 10000)  #pol0
    fslope1.SetLineColor(2)
    fslope1.SetFillColor(2)

    # Alpha2
    galpha2 = TGraphErrors()
    galpha2.SetTitle(";m_{X} (GeV);crystal ball upper alpha")
    galpha2.SetMarkerStyle(20)
    galpha2.SetMarkerColor(cColor)
    galpha2.SetLineColor(cColor)
    ialpha2 = TGraphErrors()
    ialpha2.SetMarkerStyle(24)
    falpha2 = TF1("falpha", "pol1", 0, 10000)  #pol0
    falpha2.SetLineColor(2)
    falpha2.SetFillColor(2)

    # Slope2
    gslope2 = TGraphErrors()
    gslope2.SetTitle(";m_{X} (GeV);exponential upper slope (1/Gev)")
    gslope2.SetMarkerStyle(20)
    gslope2.SetMarkerColor(cColor)
    gslope2.SetLineColor(cColor)
    islope2 = TGraphErrors()
    islope2.SetMarkerStyle(24)
    fslope2 = TF1("fslope", "pol1", 0, 10000)  #pol0
    fslope2.SetLineColor(2)
    fslope2.SetFillColor(2)

    n = 0
    for i, m in enumerate(genPoints):
        if not m in signalNorm.keys(): continue
        if signalNorm[m].getVal() < 1.e-6: continue

        if gnorm.GetMaximum() < signalNorm[m].getVal():
            gnorm.SetMaximum(signalNorm[m].getVal())
        gnorm.SetPoint(n, m, signalNorm[m].getVal())
        #gnorm.SetPointError(i, 0, signalNorm[m].getVal()/math.sqrt(treeSign[m].GetEntriesFast()))
        gmean.SetPoint(n, m, vmean[m].getVal())
        gmean.SetPointError(n, 0,
                            min(vmean[m].getError(), vmean[m].getVal() * 0.02))
        gsigma.SetPoint(n, m, vsigma[m].getVal())
        gsigma.SetPointError(
            n, 0, min(vsigma[m].getError(), vsigma[m].getVal() * 0.05))
        galpha1.SetPoint(n, m, valpha1[m].getVal())
        galpha1.SetPointError(
            n, 0, min(valpha1[m].getError(), valpha1[m].getVal() * 0.10))
        gslope1.SetPoint(n, m, vslope1[m].getVal())
        gslope1.SetPointError(
            n, 0, min(vslope1[m].getError(), vslope1[m].getVal() * 0.10))
        galpha2.SetPoint(n, m, salpha2[m].getVal())
        galpha2.SetPointError(
            n, 0, min(valpha2[m].getError(), valpha2[m].getVal() * 0.10))
        gslope2.SetPoint(n, m, sslope2[m].getVal())
        gslope2.SetPointError(
            n, 0, min(vslope2[m].getError(), vslope2[m].getVal() * 0.10))
        #tmpVar = w.var(var+"_"+signalString)
        #print m, tmpVar.getVal(), tmpVar.getError()
        n = n + 1

    gmean.Fit(fmean, "Q0", "SAME")
    gsigma.Fit(fsigma, "Q0", "SAME")
    galpha1.Fit(falpha1, "Q0", "SAME")
    gslope1.Fit(fslope1, "Q0", "SAME")
    galpha2.Fit(falpha2, "Q0", "SAME")
    gslope2.Fit(fslope2, "Q0", "SAME")
    #    gnorm.Fit(fnorm, "Q0", "", 700, 5000)
    #for m in [5000, 5500]: gnorm.SetPoint(gnorm.GetN(), m, gnorm.Eval(m, 0, "S"))
    #gnorm.Fit(fnorm, "Q", "SAME", 700, 6000)
    gnorm.Fit(fnorm, "Q", "SAME", 1800, 8000)  ## adjusted recently

    for m in massPoints:

        if vsigma[m].getVal() < 10.: vsigma[m].setVal(10.)

        # Interpolation method
        syield = gnorm.Eval(m)
        spline = gnorm.Eval(m, 0, "S")
        sfunct = fnorm.Eval(m)

        #delta = min(abs(1.-spline/sfunct), abs(1.-spline/syield))
        delta = abs(1. - spline / sfunct) if sfunct > 0 else 0
        syield = spline

        if interPar:
            #jmean = gmean.Eval(m)
            #jsigma = gsigma.Eval(m)
            #jalpha1 = galpha1.Eval(m)
            #jslope1 = gslope1.Eval(m)
            #jalpha2 = galpha2.Eval(m)
            #jslope2 = gslope2.Eval(m)
            jmean = gmean.Eval(m, 0, "S")
            jsigma = gsigma.Eval(m, 0, "S")
            jalpha1 = galpha1.Eval(m, 0, "S")
            jslope1 = gslope1.Eval(m, 0, "S")
            jalpha2 = galpha2.Eval(m, 0, "S")
            jslope2 = gslope2.Eval(m, 0, "S")

        else:
            jmean = fmean.GetParameter(
                0) + fmean.GetParameter(1) * m + fmean.GetParameter(2) * m * m
            jsigma = fsigma.GetParameter(0) + fsigma.GetParameter(
                1) * m + fsigma.GetParameter(2) * m * m
            jalpha1 = falpha1.GetParameter(0) + falpha1.GetParameter(
                1) * m + falpha1.GetParameter(2) * m * m
            jslope1 = fslope1.GetParameter(0) + fslope1.GetParameter(
                1) * m + fslope1.GetParameter(2) * m * m
            jalpha2 = falpha2.GetParameter(0) + falpha2.GetParameter(
                1) * m + falpha2.GetParameter(2) * m * m
            jslope2 = fslope2.GetParameter(0) + fslope2.GetParameter(
                1) * m + fslope2.GetParameter(2) * m * m

        inorm.SetPoint(inorm.GetN(), m, syield)
        signalNorm[m].setVal(max(0., syield))

        imean.SetPoint(imean.GetN(), m, jmean)
        if jmean > 0: vmean[m].setVal(jmean)

        isigma.SetPoint(isigma.GetN(), m, jsigma)
        if jsigma > 0: vsigma[m].setVal(jsigma)

        ialpha1.SetPoint(ialpha1.GetN(), m, jalpha1)
        if not jalpha1 == 0: valpha1[m].setVal(jalpha1)

        islope1.SetPoint(islope1.GetN(), m, jslope1)
        if jslope1 > 0: vslope1[m].setVal(jslope1)

        ialpha2.SetPoint(ialpha2.GetN(), m, jalpha2)
        if not jalpha2 == 0: valpha2[m].setVal(jalpha2)

        islope2.SetPoint(islope2.GetN(), m, jslope2)
        if jslope2 > 0: vslope2[m].setVal(jslope2)

        #### newly introduced, not yet sure if helpful:
        vmean[m].removeError()
        vsigma[m].removeError()
        valpha1[m].removeError()
        valpha2[m].removeError()
        vslope1[m].removeError()
        vslope2[m].removeError()

        #signalNorm[m].setConstant(False)  ## newly put here to ensure it's freely floating in the combine fit

    #c1 = TCanvas("c1", "Crystal Ball", 1200, 1200) #if not isAH else 1200
    #c1.Divide(2, 3)
    c1 = TCanvas("c1", "Crystal Ball", 1800, 800)
    c1.Divide(3, 2)
    c1.cd(1)
    gmean.SetMinimum(0.)
    gmean.Draw("APL")
    imean.Draw("P, SAME")
    drawRegion(category)
    drawCMS(-1, "Simulation Preliminary", year=YEAR)  ## new FIXME
    c1.cd(2)
    gsigma.SetMinimum(0.)
    gsigma.Draw("APL")
    isigma.Draw("P, SAME")
    drawRegion(category)
    drawCMS(-1, "Simulation Preliminary", year=YEAR)  ## new FIXME
    c1.cd(3)
    galpha1.Draw("APL")
    ialpha1.Draw("P, SAME")
    drawRegion(category)
    drawCMS(-1, "Simulation Preliminary", year=YEAR)  ## new FIXME
    galpha1.GetYaxis().SetRangeUser(0., 1.1)  #adjusted upper limit from 5 to 2
    c1.cd(4)
    gslope1.Draw("APL")
    islope1.Draw("P, SAME")
    drawRegion(category)
    drawCMS(-1, "Simulation Preliminary", year=YEAR)  ## new FIXME
    gslope1.GetYaxis().SetRangeUser(0.,
                                    150.)  #adjusted upper limit from 125 to 60
    if True:  #isAH:
        c1.cd(5)
        galpha2.Draw("APL")
        ialpha2.Draw("P, SAME")
        drawRegion(category)
        drawCMS(-1, "Simulation Preliminary", year=YEAR)  ## new FIXME
        galpha2.GetYaxis().SetRangeUser(0., 2.)
        c1.cd(6)
        gslope2.Draw("APL")
        islope2.Draw("P, SAME")
        drawRegion(category)
        drawCMS(-1, "Simulation Preliminary", year=YEAR)  ## new FIXME
        gslope2.GetYaxis().SetRangeUser(0., 20.)

    c1.Print(PLOTDIR + "MC_signal_" + YEAR + "/" + stype + "_" + category +
             "_SignalShape.pdf")
    c1.Print(PLOTDIR + "MC_signal_" + YEAR + "/" + stype + "_" + category +
             "_SignalShape.png")

    c2 = TCanvas("c2", "Signal Efficiency", 800, 600)
    c2.cd(1)
    gnorm.SetMarkerColor(cColor)
    gnorm.SetMarkerStyle(20)
    gnorm.SetLineColor(cColor)
    gnorm.SetLineWidth(2)
    gnorm.Draw("APL")
    inorm.Draw("P, SAME")
    gnorm.GetXaxis().SetRangeUser(genPoints[0] - 100, genPoints[-1] + 100)
    gnorm.GetYaxis().SetRangeUser(0., gnorm.GetMaximum() * 1.25)
    drawCMS(-1, "Simulation Preliminary", year=YEAR)
    #drawCMS(-1, "Work in Progress", year=YEAR, suppressCMS=True)
    #drawCMS(-1, "", year=YEAR, suppressCMS=True)
    drawAnalysis(category)
    drawRegion(category)
    c2.Print(PLOTDIR + "MC_signal_" + YEAR + "/" + stype + "_" + category +
             "_SignalNorm.pdf")
    c2.Print(PLOTDIR + "MC_signal_" + YEAR + "/" + stype + "_" + category +
             "_SignalNorm.png")

    #*******************************************************#
    #                                                       #
    #                   Generate workspace                  #
    #                                                       #
    #*******************************************************#

    # create workspace
    w = RooWorkspace("Zprime_" + YEAR, "workspace")
    for m in massPoints:
        getattr(w, "import")(signal[m], RooFit.Rename(signal[m].GetName()))
        getattr(w, "import")(signalNorm[m],
                             RooFit.Rename(signalNorm[m].GetName()))
        getattr(w, "import")(signalXS[m], RooFit.Rename(signalXS[m].GetName()))
    w.writeToFile(WORKDIR + "MC_signal_%s_%s.root" % (YEAR, category), True)
    print "Workspace", WORKDIR + "MC_signal_%s_%s.root" % (
        YEAR, category), "saved successfully"
def plotDistributionComparisonPlot(cfg):

    multiGraph = TMultiGraph()
    multiGraph.SetName("triggerRateMultiGraph")

    tfiles = []

    histograms = []

    canvas = TCanvas("canvas", "canvas", 800, 800)
    '''Contains the legend'''
    legend = TLegend(0.3, 0.7, 0.90, 0.9)
    '''Maximum value container, used to scale histograms'''
    maximumY = float("-inf")

    pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0)
    pad1.SetBottomMargin(0.05)  # Upper and lower plot are joined
    #pad1.SetBottomMargin(0) # Upper and lower plot are joined
    pad1.SetGridx()  # Vertical grid
    pad1.Draw()  # Draw the upper pad: pad1
    pad1.cd()  # pad1 becomes the current pad

    for histogramFileNameAndTitle in cfg.plots:
        tfile = TFile(histogramFileNameAndTitle[0])
        tfiles.append(tfile)
        histogram = tfile.Get(histogramFileNameAndTitle[1])
        histograms.append(histogram)
        if histogram.ClassName() == "TH1F":
            histogram.SetStats(0)  # No statistics on upper plot
        maximumY = histogram.GetMaximum(
        ) if histogram.GetMaximum() > maximumY else maximumY
        legend.AddEntry(histogram, histogramFileNameAndTitle[2], "l")

    # histograms[0] settings
    histograms[0].SetMarkerColor(4)
    histograms[0].SetLineColor(4)
    histograms[0].SetLineWidth(1)

    # Y axis histograms[0] plot settings
    histograms[0].GetYaxis().SetTitleSize(20)
    histograms[0].GetYaxis().SetTitleFont(43)
    histograms[0].GetYaxis().SetTitleOffset(1.55)

    #histograms[0].Scale(1./histograms[0].GetEntries())
    if histograms[0].ClassName() == "TH1F":
        histograms[0].Draw(
            "SAME HIST")  # Draw histograms[1] on top of histograms[0]
    else:
        histograms[0].Draw(
            "SAME APE")  # Draw histograms[1] on top of histograms[0]
        #multiGraph.Add(histograms[0])

    if getattr(cfg, "xRange", None) is not None:
        histograms[0].GetXaxis().SetRangeUser(cfg.xRange[0], cfg.xRange[1])
        gPad.RedrawAxis()

    if getattr(cfg, "xAxisLabel", None) is not None:
        histograms[0].GetXaxis().SetTitle(cfg.xAxisLabel)
        gPad.RedrawAxis()

    if getattr(cfg, "yAxisLabel", None) is not None:
        histograms[0].GetYaxis().SetTitle(cfg.yAxisLabel)
        gPad.RedrawAxis()

    if getattr(cfg, "yRange", None) is not None:
        histograms[0].GetYaxis().SetRangeUser(cfg.yRange[0], cfg.yRange[1])
        gPad.RedrawAxis()
    else:
        maximumY *= 1.1
        histograms[0].GetYaxis().SetRangeUser(1e-6, maximumY)

    if getattr(cfg, "logY", False):
        canvas.SetLogy()

    # histograms[1] settings
    histograms[1].SetMarkerColor(2)
    histograms[1].SetLineColor(2)
    histograms[1].SetLineWidth(1)
    #histograms[1].Scale(1./histograms[1].GetEntries())
    if histograms[1].ClassName() == "TH1F":
        histograms[1].Draw(
            "SAME HIST")  # Draw histograms[1] on top of histograms[0]
    else:
        histograms[1].Draw(
            "SAME PE")  # Draw histograms[1] on top of histograms[0]
        #multiGraph.Add(histograms[1])

    #if multiGraph.GetListOfGraphs() != None:
    #  multiGraph.Draw("SAME PE")

    # Do not draw the Y axis label on the upper plot and redraw a small
    # axis instead, in order to avoid the first label (0) to be clipped.
    #histograms[0].GetYaxis().SetLabelSize(0.)
    #axis = TGaxis( 0, 20, 0, maximumY, 20, maximumY, 510,"")
    #axis.SetLabelFont(43) # Absolute font size in pixel (precision 3)
    #axis.SetLabelSize(15)
    #axis.Draw()

    # Adding a small text with the chi-squared

    chiSquared = 0
    if (histograms[0].ClassName() == "TGraph") or (histograms[0].ClassName()
                                                   == "TGraphErrors"):
        numberOfBins = histograms[0].GetN()
        numberOfDegreesOfFreedom = numberOfBins
    else:
        numberOfBins = histograms[0].GetNbinsX()
        numberOfDegreesOfFreedom = numberOfBins

    for x in xrange(
            1, numberOfBins + 1
    ):  # numberOfBins contains last bin, numberOfBins+1 contains the overflow (latter excluded), underflow also excluded
        if (histograms[0].ClassName()
                == "TGraph") or (histograms[0].ClassName() == "TGraphErrors"):
            binContent0 = histograms[0].GetY()[x - 1]
        else:
            binContent0 = histograms[0].GetBinContent(x)
        if (histograms[1].ClassName()
                == "TGraph") or (histograms[1].ClassName() == "TGraphErrors"):
            binContent1 = histograms[1].GetY()[x - 1]
        else:
            binContent1 = histograms[1].GetBinContent(x)
        bin0ErrorSquared = binContent0
        bin1ErrorSquared = binContent1
        #bin1ErrorSquared = 0
        if (binContent0 == 0) and (binContent1 == 0):
            numberOfDegreesOfFreedom -= 1  #No data means one less degree of freedom
        else:
            binDifferenceSquared = (binContent0 - binContent1)**2
            chiSquaredTerm = binDifferenceSquared / (bin0ErrorSquared +
                                                     bin1ErrorSquared)
            chiSquared += chiSquaredTerm
            if chiSquaredTerm > chiSquaredWarningThreshold:
                if (histograms[0].ClassName()
                        == "TGraph") or (histograms[0].ClassName()
                                         == "TGraphErrors"):
                    print "Bin", x, "-", histograms[0].GetX()[
                        x - 1], "has a CS=", chiSquaredTerm
                else:
                    print "Bin", x, "-", histograms[0].GetBinCenter(
                        x), "has a CS=", chiSquaredTerm

    chiSquareLabel = TPaveText(0.7, 0.6, 0.9, 0.4)
    chiSquareLabel.AddText("#chi^{2}/ndf = " + str(chiSquared) + "/" +
                           str(numberOfDegreesOfFreedom) + " = " +
                           str(chiSquared / numberOfDegreesOfFreedom))
    chiSquareLabel.Draw()
    print "FINAL CS IS", format(
        chiSquared,
        ".2f") + "/" + str(numberOfDegreesOfFreedom) + " = " + format(
            chiSquared / numberOfDegreesOfFreedom, ".2f")
    legend.SetHeader(
        "#chi^{2}/ndf = " + format(chiSquared, ".2f") + "/" +
        str(numberOfDegreesOfFreedom) + " = " +
        format(chiSquared / numberOfDegreesOfFreedom, ".2f"), "C")
    legend.Draw()
    # lower plot will be in pad
    canvas.cd()  # Go back to the main canvas before defining pad2
    pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.3)
    pad2.SetTopMargin(0)
    pad2.SetBottomMargin(0.2)
    pad2.SetGridx()  # vertical grid
    pad2.Draw()
    pad2.cd()  # pad2 becomes the current pad
    pad2.SetGridy()

    # Define the ratio plot
    ratioPlot = TGraphErrors(histograms[0])
    ratioPlot.SetName("ratioPlot")
    graph_histo0 = TGraphErrors(histograms[0])
    graph_histo1 = TGraphErrors(histograms[1])
    ratioPlot.SetLineColor(1)
    ratioPlot.SetMinimum(0.6)  # Define Y ..
    ratioPlot.SetMaximum(1.5)  # .. range
    #ratioPlot.Sumw2()
    #ratioPlot.SetStats(0)      # No statistics on lower plot

    #Dividing point by point

    for index in xrange(0, ratioPlot.GetN()):
        if graph_histo1.GetY()[index] == 0:
            ratioPlot.GetY()[index] = 0
            ratioPlot.GetEY()[index] = 0
        else:
            ratioPlot.GetY()[index] /= graph_histo1.GetY()[index]
            ratioPlot.GetEY()[index] = sqrt(
                ((graph_histo1.GetY()[index])**2 *
                 (graph_histo0.GetEY()[index])**2 +
                 (graph_histo0.GetY()[index])**2 *
                 (graph_histo1.GetEY()[index])**2) /
                (graph_histo1.GetY()[index])**4)

    ratioPlot.SetMarkerStyle(21)

    if getattr(cfg, "xRange", None) is not None:
        ratioPlot.GetXaxis().SetRangeUser(cfg.xRange[0], cfg.xRange[1])
        gPad.RedrawAxis()

    if getattr(cfg, "yRangeRatio", None) is not None:
        ratioPlot.GetYaxis().SetRangeUser(cfg.yRangeRatio[0],
                                          cfg.yRangeRatio[1])
        gPad.RedrawAxis()

    ratioPlot.Draw("APE")  # Draw the ratio plot

    line0 = TLine(ratioPlot.GetXaxis().GetXmin(), 1,
                  ratioPlot.GetXaxis().GetXmax(), 1)
    line0.SetLineColor(2)
    line0.SetLineWidth(2)
    line0.SetLineStyle(2)
    line0.Draw()

    # Ratio plot (ratioPlot) settings
    ratioPlot.SetTitle("")  # Remove the ratio title

    # Y axis ratio plot settings
    ratioPlot.GetYaxis().SetTitle("Ratio #frac{blue}{red}")
    ratioPlot.GetYaxis().SetNdivisions(505)
    ratioPlot.GetYaxis().SetTitleSize(20)
    ratioPlot.GetYaxis().SetTitleFont(43)
    ratioPlot.GetYaxis().SetTitleOffset(1.55)
    ratioPlot.GetYaxis().SetLabelFont(
        43)  # Absolute font size in pixel (precision 3)
    ratioPlot.GetYaxis().SetLabelSize(15)

    # X axis ratio plot settings
    ratioPlot.GetXaxis().SetTitleSize(20)
    ratioPlot.GetXaxis().SetTitleFont(43)
    ratioPlot.GetXaxis().SetTitleOffset(4.)
    ratioPlot.GetXaxis().SetLabelFont(
        43)  # Absolute font size in pixel (precision 3)
    ratioPlot.GetXaxis().SetLabelSize(15)

    xRangeBinning = getattr(cfg, "simplifiedRatioPlotXRangeBinning", None)
    if xRangeBinning is not None:
        simplifiedRatioPlot = TGraphErrors(len(xRangeBinning) - 1)
        simplifiedRatioPlot.SetName("simplifiedRatioPlot")
        ratioPlotIndex = 0

        for idx in xrange(0, simplifiedRatioPlot.GetN()):
            yAverage = 0.
            yMax = float("-inf")
            yMin = float("+inf")

            nPoints = 0.
            simplifiedRatioPlot.GetX()[idx] = (xRangeBinning[idx] +
                                               xRangeBinning[idx + 1]) / 2.
            simplifiedRatioPlot.GetEX()[idx] = (xRangeBinning[idx + 1] -
                                                xRangeBinning[idx]) / 2.

            while (ratioPlot.GetX()[ratioPlotIndex] < xRangeBinning[idx]):
                ratioPlotIndex += 1
            while ((ratioPlotIndex < ratioPlot.GetN()) and
                   (ratioPlot.GetX()[ratioPlotIndex] < xRangeBinning[idx + 1])
                   and
                   (ratioPlot.GetX()[ratioPlotIndex] >= xRangeBinning[idx])):
                yAverage += ratioPlot.GetY()[ratioPlotIndex]
                if (yMax < ratioPlot.GetY()[ratioPlotIndex] +
                        ratioPlot.GetEY()[ratioPlotIndex]):
                    yMax = ratioPlot.GetY()[ratioPlotIndex] + ratioPlot.GetEY(
                    )[ratioPlotIndex]
                if (yMin > ratioPlot.GetY()[ratioPlotIndex] -
                        ratioPlot.GetEY()[ratioPlotIndex]):
                    yMin = ratioPlot.GetY()[ratioPlotIndex] - ratioPlot.GetEY(
                    )[ratioPlotIndex]
                nPoints += 1.
                ratioPlotIndex += 1

            simplifiedRatioPlot.GetY()[idx] = yAverage / nPoints
            simplifiedRatioPlot.GetEY()[idx] = (yMax - yMin) / 2.

    saveFile = TFile(cfg.saveFileName, "RECREATE")
    saveFile.cd()
    canvas.Write()
    histograms[0].Write()
    histograms[1].Write()
    if multiGraph.GetListOfGraphs() != None:
        multiGraph.Write()
    ratioPlot.Write()
    if xRangeBinning is not None:
        simplifiedRatioPlot.Write()
    saveFile.Close()
    for tfile in tfiles:
        tfile.Close()
Пример #25
0
def signal(channel, stype):
    if 'VBF' in channel:
        stype = 'XZHVBF'
    else:
        stype = 'XZH'
    # HVT model
    if stype.startswith('X'):
        signalType = 'HVT'
        genPoints = [800, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000, 3500, 4000, 4500, 5000]
        massPoints = [x for x in range(800, 5000+1, 100)]
        interPar = True
    else:
        print "Signal type", stype, "not recognized"
        return
    
    n = len(genPoints)  
    
    category = channel
    cColor = color[category] if category in color else 1

    nElec = channel.count('e')
    nMuon = channel.count('m')
    nLept = nElec + nMuon
    nBtag = channel.count('b')
    if '0b' in channel:
        nBtag = 0

    X_name = "VH_mass"

    if not os.path.exists(PLOTDIR+stype+category): os.makedirs(PLOTDIR+stype+category)

    #*******************************************************#
    #                                                       #
    #              Variables and selections                 #
    #                                                       #
    #*******************************************************#
    X_mass = RooRealVar(  "X_mass",    "m_{ZH}",       XBINMIN, XBINMAX, "GeV")
    J_mass = RooRealVar(  "H_mass",   "jet mass",        LOWMIN, HIGMAX, "GeV")
    V_mass = RooRealVar(  "V_mass", "V jet mass",           -9.,  1.e6, "GeV")
    CSV1    = RooRealVar( "H_csv1",           "",         -999.,     2.     )
    CSV2    = RooRealVar( "H_csv2",           "",         -999.,     2.     )
    DeepCSV1= RooRealVar( "H_deepcsv1",       "",         -999.,     2.     )
    DeepCSV2= RooRealVar( "H_deepcsv2",       "",         -999.,     2.     )
    H_ntag  = RooRealVar( "H_ntag",           "",           -9.,     9.     )
    H_dbt   = RooRealVar( "H_dbt",            "",           -2.,     2.     )
    H_tau21 = RooRealVar( "H_tau21",          "",           -9.,     2.     )
    H_eta = RooRealVar( "H_eta",              "",           -9.,     9.     )
    H_tau21_ddt = RooRealVar( "H_ddt",  "",           -9.,     2.     )
    MaxBTag = RooRealVar( "MaxBTag",          "",          -10.,     2.     )
    H_chf   = RooRealVar( "H_chf",            "",           -1.,     2.     )
    MinDPhi = RooRealVar( "MinDPhi",          "",           -1.,    99.     )
    DPhi    = RooRealVar( "DPhi",             "",           -1.,    99.     )
    DEta    = RooRealVar( "DEta",             "",           -1.,    99.     )
    Mu1_relIso = RooRealVar( "Mu1_relIso",    "",           -1.,    99.     )
    Mu2_relIso = RooRealVar( "Mu2_relIso",    "",           -1.,    99.     )
    nTaus   = RooRealVar( "nTaus",            "",           -1.,    99.     )
    Vpt     = RooRealVar( "V.Pt()",           "",           -1.,   1.e6     )
    V_pt     = RooRealVar( "V_pt",            "",           -1.,   1.e6     )
    H_pt     = RooRealVar( "H_pt",            "",           -1.,   1.e6     )
    VH_deltaR=RooRealVar( "VH_deltaR",        "",           -1.,    99.     )
    isZtoNN = RooRealVar( "isZtoNN",          "",            0.,     2.     )
    isZtoEE = RooRealVar( "isZtoEE",          "",            0.,     2.     )
    isZtoMM = RooRealVar( "isZtoMM",          "",            0.,     2.     )
    isHtobb = RooRealVar( "isHtobb",          "",            0.,     2.     )
    isVBF   = RooRealVar( "isVBF",            "",            0.,     2.     )
    isMaxBTag_loose = RooRealVar( "isMaxBTag_loose", "",     0.,     2.     )
    weight  = RooRealVar( "eventWeightLumi",  "",         -1.e9,   1.e9     )

    Xmin = XBINMIN
    Xmax = XBINMAX

    # Define the RooArgSet which will include all the variables defined before
    # there is a maximum of 9 variables in the declaration, so the others need to be added with 'add'
    variables = RooArgSet(X_mass, J_mass, V_mass, CSV1, CSV2, H_ntag, H_dbt, H_tau21)
    variables.add(RooArgSet(DEta, DPhi, MaxBTag, MinDPhi, nTaus, Vpt))
    variables.add(RooArgSet(DeepCSV1, DeepCSV2,VH_deltaR, H_tau21_ddt))
    variables.add(RooArgSet(isZtoNN, isZtoEE, isZtoMM, isHtobb, isMaxBTag_loose, weight))
    variables.add(RooArgSet(isVBF, Mu1_relIso, Mu2_relIso, H_chf, H_pt, V_pt,H_eta))
    #X_mass.setRange("X_extended_range", X_mass.getMin(), X_mass.getMax())
    X_mass.setRange("X_reasonable_range", X_mass.getMin(), X_mass.getMax())
    X_mass.setRange("X_integration_range", Xmin, Xmax)
    X_mass.setBins(int((X_mass.getMax() - X_mass.getMin())/100))
    binsXmass = RooBinning(int((X_mass.getMax() - X_mass.getMin())/100), X_mass.getMin(), X_mass.getMax())
    X_mass.setBinning(binsXmass, "PLOT")
    massArg = RooArgSet(X_mass)

    # Cuts
    SRcut = selection[category]+selection['SR']
    print "  Cut:\t", SRcut
    #*******************************************************#
    #                                                       #
    #                    Signal fits                        #
    #                                                       #
    #*******************************************************#

    treeSign = {}
    setSignal = {}

    vmean  = {}
    vsigma = {}
    valpha1 = {}
    vslope1 = {}
    smean  = {}
    ssigma = {}
    salpha1 = {}
    sslope1 = {}
    salpha2 = {}
    sslope2 = {}
    a1 = {}
    a2 = {}
    sbrwig = {}
    signal = {}
    signalExt = {}
    signalYield = {}
    signalIntegral = {}
    signalNorm = {}
    signalXS = {}
    frSignal = {}
    frSignal1 = {}
    frSignal2 = {}
    frSignal3 = {}

    # Signal shape uncertainties (common amongst all mass points)
    xmean_fit = RooRealVar("sig_p1_fit", "Variation of the resonance position with the fit uncertainty", 0.005, -1., 1.)
    smean_fit = RooRealVar("CMSRunII_sig_p1_fit", "Change of the resonance position with the fit uncertainty", 0., -10, 10)
    xmean_jes = RooRealVar("sig_p1_scale_jes", "Variation of the resonance position with the jet energy scale", 0.010, -1., 1.) #0.001
    smean_jes = RooRealVar("CMSRunII_sig_p1_jes", "Change of the resonance position with the jet energy scale", 0., -10, 10)
    xmean_e = RooRealVar("sig_p1_scale_e", "Variation of the resonance position with the electron energy scale", 0.001, -1., 1.)
    smean_e = RooRealVar("CMSRunII_sig_p1_scale_e", "Change of the resonance position with the electron energy scale", 0., -10, 10)
    xmean_m = RooRealVar("sig_p1_scale_m", "Variation of the resonance position with the muon energy scale", 0.001, -1., 1.)
    smean_m = RooRealVar("CMSRunII_sig_p1_scale_m", "Change of the resonance position with the muon energy scale", 0., -10, 10)

    xsigma_fit = RooRealVar("sig_p2_fit", "Variation of the resonance width with the fit uncertainty", 0.02, -1., 1.)
    ssigma_fit = RooRealVar("CMSRunII_sig_p2_fit", "Change of the resonance width with the fit uncertainty", 0., -10, 10)
    xsigma_jes = RooRealVar("sig_p2_scale_jes", "Variation of the resonance width with the jet energy scale", 0.010, -1., 1.) #0.001
    ssigma_jes = RooRealVar("CMSRunII_sig_p2_jes", "Change of the resonance width with the jet energy scale", 0., -10, 10)
    xsigma_jer = RooRealVar("sig_p2_scale_jer", "Variation of the resonance width with the jet energy resolution", 0.020, -1., 1.)
    ssigma_jer = RooRealVar("CMSRunII_sig_p2_jer", "Change of the resonance width with the jet energy resolution", 0., -10, 10)
    xsigma_e = RooRealVar("sig_p2_scale_e", "Variation of the resonance width with the electron energy scale", 0.001, -1., 1.)
    ssigma_e = RooRealVar("CMSRunII_sig_p2_scale_e", "Change of the resonance width with the electron energy scale", 0., -10, 10)
    xsigma_m = RooRealVar("sig_p2_scale_m", "Variation of the resonance width with the muon energy scale", 0.040, -1., 1.)
    ssigma_m = RooRealVar("CMSRunII_sig_p2_scale_m", "Change of the resonance width with the muon energy scale", 0., -10, 10)
    
    xalpha1_fit = RooRealVar("sig_p3_fit", "Variation of the resonance alpha with the fit uncertainty", 0.03, -1., 1.)
    salpha1_fit = RooRealVar("CMSRunII_sig_p3_fit", "Change of the resonance alpha with the fit uncertainty", 0., -10, 10)
    
    xslope1_fit = RooRealVar("sig_p4_fit", "Variation of the resonance slope with the fit uncertainty", 0.10, -1., 1.)
    sslope1_fit = RooRealVar("CMSRunII_sig_p4_fit", "Change of the resonance slope with the fit uncertainty", 0., -10, 10)

    xmean_fit.setConstant(True)
    smean_fit.setConstant(True)
    xmean_jes.setConstant(True)
    smean_jes.setConstant(True)
    xmean_e.setConstant(True)
    smean_e.setConstant(True)
    xmean_m.setConstant(True)
    smean_m.setConstant(True)
    
    xsigma_fit.setConstant(True)
    ssigma_fit.setConstant(True)
    xsigma_jes.setConstant(True)
    ssigma_jes.setConstant(True)
    xsigma_jer.setConstant(True)
    ssigma_jer.setConstant(True)
    xsigma_e.setConstant(True)
    ssigma_e.setConstant(True)
    xsigma_m.setConstant(True)
    ssigma_m.setConstant(True)
    
    xalpha1_fit.setConstant(True)
    salpha1_fit.setConstant(True)
    xslope1_fit.setConstant(True)
    sslope1_fit.setConstant(True)

    # the alpha method is now done.
    for m in massPoints:
        signalString = "M%d" % m
        signalMass = "%s_M%d" % (stype, m)
        signalName = "%s%s_M%d" % (stype, category, m)
        signalColor = sample[signalMass]['linecolor'] if signalName in sample else 1

        # define the signal PDF
        vmean[m] = RooRealVar(signalName + "_vmean", "Crystal Ball mean", m, m*0.5, m*1.25)
        smean[m] = RooFormulaVar(signalName + "_mean", "@0*(1+@1*@2)*(1+@3*@4)*(1+@5*@6)*(1+@7*@8)", RooArgList(vmean[m], xmean_e, smean_e, xmean_m, smean_m, xmean_jes, smean_jes, xmean_fit, smean_fit))

        vsigma[m] = RooRealVar(signalName + "_vsigma", "Crystal Ball sigma", m*0.035, m*0.01, m*0.4)
        sigmaList = RooArgList(vsigma[m], xsigma_e, ssigma_e, xsigma_m, ssigma_m, xsigma_jes, ssigma_jes, xsigma_jer, ssigma_jer)
        sigmaList.add(RooArgList(xsigma_fit, ssigma_fit))
        ssigma[m] = RooFormulaVar(signalName + "_sigma", "@0*(1+@1*@2)*(1+@3*@4)*(1+@5*@6)*(1+@7*@8)*(1+@9*@10)", sigmaList)
        
        valpha1[m] = RooRealVar(signalName + "_valpha1", "Crystal Ball alpha", 1.,  0., 5.) # number of sigmas where the exp is attached to the gaussian core. >0 left, <0 right
        salpha1[m] = RooFormulaVar(signalName + "_alpha1", "@0*(1+@1*@2)", RooArgList(valpha1[m], xalpha1_fit, salpha1_fit))

        vslope1[m] = RooRealVar(signalName + "_vslope1", "Crystal Ball slope", 10., 1., 60.) # slope of the power tail   #10 1 60
        sslope1[m] = RooFormulaVar(signalName + "_slope1", "@0*(1+@1*@2)", RooArgList(vslope1[m], xslope1_fit, sslope1_fit))

        salpha2[m] = RooRealVar(signalName + "_alpha2", "Crystal Ball alpha", 2,  1., 5.) # number of sigmas where the exp is attached to the gaussian core. >0 left, <0 right
        sslope2[m] = RooRealVar(signalName + "_slope2", "Crystal Ball slope", 10, 1.e-1, 115.) # slope of the power tail
        #define polynomial
        #a1[m] = RooRealVar(signalName + "_a1", "par 1 for polynomial", m, 0.5*m, 2*m)
        a1[m] = RooRealVar(signalName + "_a1", "par 1 for polynomial", 0.001*m, 0.0005*m, 0.01*m)
        a2[m] = RooRealVar(signalName + "_a2", "par 2 for polynomial", 0.05, -1.,1.)
        #if channel=='nnbbVBF' or channel=='nn0bVBF':
        #    signal[m] = RooPolynomial(signalName,"m_{%s'} = %d GeV" % (stype[1], m) , X_mass, RooArgList(a1[m],a2[m]))
        #else:
        #    signal[m] = RooCBShape(signalName, "m_{%s'} = %d GeV" % (stype[1], m), X_mass, smean[m], ssigma[m], salpha1[m], sslope1[m]) # Signal name does not have the channel
        signal[m] = RooCBShape(signalName, "m_{%s'} = %d GeV" % (stype[1], m), X_mass, smean[m], ssigma[m], salpha1[m], sslope1[m]) # Signal name does not have the channel
        # extend the PDF with the yield to perform an extended likelihood fit
        signalYield[m] = RooRealVar(signalName+"_yield", "signalYield", 100, 0., 1.e6)
        signalNorm[m] = RooRealVar(signalName+"_norm", "signalNorm", 1., 0., 1.e6)
        signalXS[m] = RooRealVar(signalName+"_xs", "signalXS", 1., 0., 1.e6)
        signalExt[m] = RooExtendPdf(signalName+"_ext", "extended p.d.f", signal[m], signalYield[m])
        
        vslope1[m].setMax(50.)
        vslope1[m].setVal(20.)
        #valpha1[m].setVal(1.0)
        #valpha1[m].setConstant(True)
        
        if 'bb' in channel and 'VBF' not in channel:
            if 'nn' in channel:
                valpha1[m].setVal(0.5)
        elif '0b' in channel and 'VBF' not in channel:
            if 'nn' in channel:
                if m==800:
                    valpha1[m].setVal(2.)
                    vsigma[m].setVal(m*0.04)
            elif 'ee' in channel:
                valpha1[m].setVal(0.8)
                if m==800:
                    #valpha1[m].setVal(1.2)
                    valpha1[m].setVal(2.5)
                    vslope1[m].setVal(50.)
            elif 'mm' in channel:
                if m==800:
                    valpha1[m].setVal(2.)
                    vsigma[m].setVal(m*0.03)
                else:
                    vmean[m].setVal(m*0.9)
                    vsigma[m].setVal(m*0.08)
        elif 'bb' in channel and 'VBF' in channel:
            if 'nn' in channel:
                if m!=1800:
                    vmean[m].setVal(m*0.8)
                vsigma[m].setVal(m*0.08)
                valpha1[m].setMin(1.)
            elif 'ee' in channel:
                valpha1[m].setVal(0.7)
            elif 'mm' in channel:
                if m==800:
                    vslope1[m].setVal(50.)
                valpha1[m].setVal(0.7)
        elif '0b' in channel and 'VBF' in channel:
            if 'nn' in channel:
                valpha1[m].setVal(3.) 
                vmean[m].setVal(m*0.8)
                vsigma[m].setVal(m*0.08)
                valpha1[m].setMin(1.)
            elif 'ee' in channel:
                if m<2500:
                    valpha1[m].setVal(2.)
                if m==800:
                    vsigma[m].setVal(m*0.05)
                elif m==1000:
                    vsigma[m].setVal(m*0.03)
                elif m>1000 and m<1800:
                    vsigma[m].setVal(m*0.04)
            elif 'mm' in channel:
                if m<2000:
                    valpha1[m].setVal(2.)
                if m==1000 or m==1800:
                    vsigma[m].setVal(m*0.03)
                elif m==1200 or m==1600:
                    vsigma[m].setVal(m*0.04)

            
        #if m < 1000: vsigma[m].setVal(m*0.06)

        # If it's not the proper channel, make it a gaussian
        #if nLept==0 and 'VBF' in channel:
        #    valpha1[m].setVal(5)
        #    valpha1[m].setConstant(True)
        #    vslope1[m].setConstant(True)
        #    salpha2[m].setConstant(True)
        #    sslope2[m].setConstant(True)

        
        # ---------- if there is no simulated signal, skip this mass point ----------
        if m in genPoints:
            if VERBOSE: print " - Mass point", m

            # define the dataset for the signal applying the SR cuts
            treeSign[m] = TChain("tree")
            for j, ss in enumerate(sample[signalMass]['files']):
                treeSign[m].Add(NTUPLEDIR + ss + ".root")
            
            if treeSign[m].GetEntries() <= 0.:
                if VERBOSE: print " - 0 events available for mass", m, "skipping mass point..."
                signalNorm[m].setVal(-1)
                vmean[m].setConstant(True)
                vsigma[m].setConstant(True)
                salpha1[m].setConstant(True)
                sslope1[m].setConstant(True)
                salpha2[m].setConstant(True)
                sslope2[m].setConstant(True)
                signalNorm[m].setConstant(True)
                signalXS[m].setConstant(True)
                continue
            
            setSignal[m] = RooDataSet("setSignal_"+signalName, "setSignal", variables, RooFit.Cut(SRcut), RooFit.WeightVar(weight), RooFit.Import(treeSign[m]))
            if VERBOSE: print " - Dataset with", setSignal[m].sumEntries(), "events loaded"
            
            # FIT
            signalYield[m].setVal(setSignal[m].sumEntries())
            
            if treeSign[m].GetEntries(SRcut) > 5:
                if VERBOSE: print " - Running fit"
 
                frSignal[m] = signalExt[m].fitTo(setSignal[m], RooFit.Save(1), RooFit.Extended(True), RooFit.SumW2Error(True), RooFit.PrintLevel(-1))
                if VERBOSE: print "********** Fit result [", m, "] **", category, "*"*40, "\n", frSignal[m].Print(), "\n", "*"*80
                if VERBOSE: frSignal[m].correlationMatrix().Print()
                drawPlot(signalMass, stype+channel, X_mass, signal[m], setSignal[m], frSignal[m])
            
            else:
                print "  WARNING: signal", stype, "and mass point", m, "in channel", channel, "has 0 entries or does not exist"          
            # Remove HVT cross section (which is the same for Zlep and Zinv)
            if stype == "XZHVBF":
                sample_name = 'Zprime_VBF_Zh_Zlephinc_narrow_M-%d' % m
            else:
                sample_name = 'ZprimeToZHToZlepHinc_narrow_M%d' % m

            xs = xsection[sample_name]['xsec']
            
            signalXS[m].setVal(xs * 1000.)
            
            signalIntegral[m] = signalExt[m].createIntegral(massArg, RooFit.NormSet(massArg), RooFit.Range("X_integration_range"))
            boundaryFactor = signalIntegral[m].getVal()
            if VERBOSE: 
                print " - Fit normalization vs integral:", signalYield[m].getVal(), "/", boundaryFactor, "events"
            if channel=='nnbb' and m==5000:
                signalNorm[m].setVal(2.5)
            elif channel=='nn0b' and m==5000:
                signalNorm[m].setVal(6.7)
            else:
                signalNorm[m].setVal( boundaryFactor * signalYield[m].getVal() / signalXS[m].getVal()) # here normalize to sigma(X) x Br(X->VH) = 1 [fb]
            
            
        a1[m].setConstant(True)
        a2[m].setConstant(True)
        vmean[m].setConstant(True)
        vsigma[m].setConstant(True)
        valpha1[m].setConstant(True)
        vslope1[m].setConstant(True)
        salpha2[m].setConstant(True)
        sslope2[m].setConstant(True)
        signalNorm[m].setConstant(True)
        signalXS[m].setConstant(True)

    #*******************************************************#
    #                                                       #
    #                 Signal interpolation                  #
    #                                                       #
    #*******************************************************#


    # ====== CONTROL PLOT ======
    c_signal = TCanvas("c_signal", "c_signal", 800, 600)
    c_signal.cd()
    frame_signal = X_mass.frame()
    for m in genPoints[:-2]:
        if m in signalExt.keys():
            signal[m].plotOn(frame_signal, RooFit.LineColor(sample["%s_M%d" % (stype, m)]['linecolor']), RooFit.Normalization(signalNorm[m].getVal(), RooAbsReal.NumEvent), RooFit.Range("X_reasonable_range"))
    frame_signal.GetXaxis().SetRangeUser(0, 6500)
    frame_signal.Draw()
    drawCMS(-1, YEAR, "Simulation")
    drawAnalysis(channel)
    drawRegion(channel)
    c_signal.SaveAs(PLOTDIR+"/"+stype+category+"/"+stype+"_Signal.pdf")
    c_signal.SaveAs(PLOTDIR+"/"+stype+category+"/"+stype+"_Signal.png")
    #if VERBOSE: raw_input("Press Enter to continue...")
    # ====== CONTROL PLOT ======

    # Normalization
    gnorm = TGraphErrors()
    gnorm.SetTitle(";m_{X} (GeV);integral (GeV)")
    gnorm.SetMarkerStyle(20)
    gnorm.SetMarkerColor(1)
    gnorm.SetMaximum(0)
    inorm = TGraphErrors()
    inorm.SetMarkerStyle(24)
    fnorm = TF1("fnorm", "pol9", 800, 5000) #"pol5" if not channel=="XZHnnbb" else "pol6" #pol5*TMath::Floor(x-1800) + ([5]*x + [6]*x*x)*(1-TMath::Floor(x-1800))
    fnorm.SetLineColor(920)
    fnorm.SetLineStyle(7)
    fnorm.SetFillColor(2)
    fnorm.SetLineColor(cColor)

    # Mean
    gmean = TGraphErrors()
    gmean.SetTitle(";m_{X} (GeV);gaussian mean (GeV)")
    gmean.SetMarkerStyle(20)
    gmean.SetMarkerColor(cColor)
    gmean.SetLineColor(cColor)
    imean = TGraphErrors()
    imean.SetMarkerStyle(24)
    fmean = TF1("fmean", "pol1", 0, 5000)
    fmean.SetLineColor(2)
    fmean.SetFillColor(2)

    # Width
    gsigma = TGraphErrors()
    gsigma.SetTitle(";m_{X} (GeV);gaussian width (GeV)")
    gsigma.SetMarkerStyle(20)
    gsigma.SetMarkerColor(cColor)
    gsigma.SetLineColor(cColor)
    isigma = TGraphErrors()
    isigma.SetMarkerStyle(24)
    fsigma = TF1("fsigma", "pol1", 0, 5000)
    fsigma.SetLineColor(2)
    fsigma.SetFillColor(2)

    # Alpha1
    galpha1 = TGraphErrors()
    galpha1.SetTitle(";m_{X} (GeV);crystal ball lower alpha")
    galpha1.SetMarkerStyle(20)
    galpha1.SetMarkerColor(cColor)
    galpha1.SetLineColor(cColor)
    ialpha1 = TGraphErrors()
    ialpha1.SetMarkerStyle(24)
    falpha1 = TF1("falpha", "pol0", 0, 5000)
    falpha1.SetLineColor(2)
    falpha1.SetFillColor(2)

    # Slope1
    gslope1 = TGraphErrors()
    gslope1.SetTitle(";m_{X} (GeV);exponential lower slope (1/Gev)")
    gslope1.SetMarkerStyle(20)
    gslope1.SetMarkerColor(cColor)
    gslope1.SetLineColor(cColor)
    islope1 = TGraphErrors()
    islope1.SetMarkerStyle(24)
    fslope1 = TF1("fslope", "pol0", 0, 5000)
    fslope1.SetLineColor(2)
    fslope1.SetFillColor(2)

    # Alpha2
    galpha2 = TGraphErrors()
    galpha2.SetTitle(";m_{X} (GeV);crystal ball upper alpha")
    galpha2.SetMarkerStyle(20)
    galpha2.SetMarkerColor(cColor)
    galpha2.SetLineColor(cColor)
    ialpha2 = TGraphErrors()
    ialpha2.SetMarkerStyle(24)
    falpha2 = TF1("falpha", "pol0", 0, 5000)
    falpha2.SetLineColor(2)
    falpha2.SetFillColor(2)

    # Slope2
    gslope2 = TGraphErrors()
    gslope2.SetTitle(";m_{X} (GeV);exponential upper slope (1/Gev)")
    gslope2.SetMarkerStyle(20)
    gslope2.SetMarkerColor(cColor)
    gslope2.SetLineColor(cColor)
    islope2 = TGraphErrors()
    islope2.SetMarkerStyle(24)
    fslope2 = TF1("fslope", "pol0", 0, 5000)
    fslope2.SetLineColor(2)
    fslope2.SetFillColor(2)



    n = 0
    for i, m in enumerate(genPoints):
        if not m in signalNorm.keys(): continue
        if signalNorm[m].getVal() < 1.e-6: continue
        signalString = "M%d" % m
        signalName = "%s_M%d" % (stype, m)

        if gnorm.GetMaximum() < signalNorm[m].getVal(): gnorm.SetMaximum(signalNorm[m].getVal())
        gnorm.SetPoint(n, m, signalNorm[m].getVal())
        gmean.SetPoint(n, m, vmean[m].getVal())
        gmean.SetPointError(n, 0, min(vmean[m].getError(), vmean[m].getVal()*0.02))
        gsigma.SetPoint(n, m, vsigma[m].getVal())
        gsigma.SetPointError(n, 0, min(vsigma[m].getError(), vsigma[m].getVal()*0.05))
        galpha1.SetPoint(n, m, valpha1[m].getVal())
        galpha1.SetPointError(n, 0, min(valpha1[m].getError(), valpha1[m].getVal()*0.10))
        gslope1.SetPoint(n, m, vslope1[m].getVal())
        gslope1.SetPointError(n, 0, min(vslope1[m].getError(), vslope1[m].getVal()*0.10))
        galpha2.SetPoint(n, m, salpha2[m].getVal())
        galpha2.SetPointError(n, 0, min(salpha2[m].getError(), salpha2[m].getVal()*0.10))
        gslope2.SetPoint(n, m, sslope2[m].getVal())
        gslope2.SetPointError(n, 0, min(sslope2[m].getError(), sslope2[m].getVal()*0.10))
        n = n + 1
    print "fit on gmean:"
    gmean.Fit(fmean, "Q0", "SAME")
    print "fit on gsigma:"
    gsigma.Fit(fsigma, "Q0", "SAME")
    print "fit on galpha:"
    galpha1.Fit(falpha1, "Q0", "SAME")
    print "fit on gslope:"
    gslope1.Fit(fslope1, "Q0", "SAME")
    galpha2.Fit(falpha2, "Q0", "SAME")
    gslope2.Fit(fslope2, "Q0", "SAME")
    #for m in [5000, 5500]: gnorm.SetPoint(gnorm.GetN(), m, gnorm.Eval(m, 0, "S"))
    gnorm.Fit(fnorm, "Q", "SAME", 700, 5000)

    for m in massPoints:
        signalName = "%s_M%d" % (stype, m)
        
        if vsigma[m].getVal() < 10.: vsigma[m].setVal(10.)

        # Interpolation method
        syield = gnorm.Eval(m)
        spline = gnorm.Eval(m, 0, "S")
        sfunct = fnorm.Eval(m)
        
        #delta = min(abs(1.-spline/sfunct), abs(1.-spline/syield))
        delta = abs(1.-spline/sfunct) if sfunct > 0 else 0
        syield = spline
               
        if interPar:
            jmean = gmean.Eval(m)
            jsigma = gsigma.Eval(m)
            jalpha1 = galpha1.Eval(m)
            jslope1 = gslope1.Eval(m)
        else:
            jmean = fmean.GetParameter(0) + fmean.GetParameter(1)*m + fmean.GetParameter(2)*m*m
            jsigma = fsigma.GetParameter(0) + fsigma.GetParameter(1)*m + fsigma.GetParameter(2)*m*m
            jalpha1 = falpha1.GetParameter(0) + falpha1.GetParameter(1)*m + falpha1.GetParameter(2)*m*m
            jslope1 = fslope1.GetParameter(0) + fslope1.GetParameter(1)*m + fslope1.GetParameter(2)*m*m

        inorm.SetPoint(inorm.GetN(), m, syield)
        signalNorm[m].setVal(syield)

        imean.SetPoint(imean.GetN(), m, jmean)
        if jmean > 0: vmean[m].setVal(jmean)

        isigma.SetPoint(isigma.GetN(), m, jsigma)
        if jsigma > 0: vsigma[m].setVal(jsigma)

        ialpha1.SetPoint(ialpha1.GetN(), m, jalpha1)
        if not jalpha1==0: valpha1[m].setVal(jalpha1)

        islope1.SetPoint(islope1.GetN(), m, jslope1)
        if jslope1 > 0: vslope1[m].setVal(jslope1)
    

    c1 = TCanvas("c1", "Crystal Ball", 1200, 800)
    c1.Divide(2, 2)
    c1.cd(1)
    gmean.SetMinimum(0.)
    gmean.Draw("APL")
    imean.Draw("P, SAME")
    drawRegion(channel)
    c1.cd(2)
    gsigma.SetMinimum(0.)
    gsigma.Draw("APL")
    isigma.Draw("P, SAME")
    drawRegion(channel)
    c1.cd(3)
    galpha1.Draw("APL")
    ialpha1.Draw("P, SAME")
    drawRegion(channel)
    galpha1.GetYaxis().SetRangeUser(0., 5.)
    c1.cd(4)
    gslope1.Draw("APL")
    islope1.Draw("P, SAME")
    drawRegion(channel)
    gslope1.GetYaxis().SetRangeUser(0., 125.)
    if False:
        c1.cd(5)
        galpha2.Draw("APL")
        ialpha2.Draw("P, SAME")
        drawRegion(channel)
        c1.cd(6)
        gslope2.Draw("APL")
        islope2.Draw("P, SAME")
        drawRegion(channel)
        gslope2.GetYaxis().SetRangeUser(0., 10.)


    c1.Print(PLOTDIR+stype+category+"/"+stype+"_SignalShape.pdf")
    c1.Print(PLOTDIR+stype+category+"/"+stype+"_SignalShape.png")


    c2 = TCanvas("c2", "Signal Efficiency", 800, 600)
    c2.cd(1)
    gnorm.SetMarkerColor(cColor)
    gnorm.SetMarkerStyle(20)
    gnorm.SetLineColor(cColor)
    gnorm.SetLineWidth(2)
    gnorm.Draw("APL")
    inorm.Draw("P, SAME")
    gnorm.GetXaxis().SetRangeUser(genPoints[0]-100, genPoints[-1]+100)
    gnorm.GetYaxis().SetRangeUser(0., gnorm.GetMaximum()*1.25)
    drawCMS(-1,YEAR , "Simulation")
    drawAnalysis(channel)
    drawRegion(channel)
    c2.Print(PLOTDIR+stype+category+"/"+stype+"_SignalNorm.pdf")
    c2.Print(PLOTDIR+stype+category+"/"+stype+"_SignalNorm.png")





    #*******************************************************#
    #                                                       #
    #                   Generate workspace                  #
    #                                                       #
    #*******************************************************#

    # create workspace
    w = RooWorkspace("ZH_RunII", "workspace")
    for m in massPoints:
        getattr(w, "import")(signal[m], RooFit.Rename(signal[m].GetName()))
        getattr(w, "import")(signalNorm[m], RooFit.Rename(signalNorm[m].GetName()))
        getattr(w, "import")(signalXS[m], RooFit.Rename(signalXS[m].GetName()))
    w.writeToFile("%s%s.root" % (WORKDIR, stype+channel), True)
    print "Workspace", "%s%s.root" % (WORKDIR, stype+channel), "saved successfully"
    sys.exit()
Пример #26
0
class RatePlotProducerPileUp(Analyzer):
    '''Analyzer creating a rate plot with pile up events and saving it to ROOT and SVG format.
  
  Example::
  
    rate = cfg.Analyzer(
      RatePlotProducerPileUp,
      file_label = 'tfile1',
      plot_name = 'rate',
      plot_title = 'A rate plot',
      zerobias_rate = 1/25e-9,
      input_objects = 'jets',
      bins = [30, 40, 50, 60],
      yscale = 1e6,
      scale_factors = [3, 5, 4]
    )
    
  * file_label: (Facultative) Name of a TFileService. If specified, the histogram will be saved in that root file, otherwise it will be saved in a <plot_name>.png and <plot_name>.root file
  * plot_name: Name of the plot (Key in the output root file).
  * plot_title: Title of the plot.
  * bins: Array containing the bins to be tested
  * zerobias_rate: zero bias trigger rate, 40 MHz @ 25 ns bunch spacing
  * input_objects: name of the particle collection
  * yscale: y level of the reference line
  * scale_factors: custom factors to be applied to the bin, 1 is assumed in case of missing parameter or if less factors than bins are provided
  '''
    '''Generates a threshold function'''
    def thresholdTriggerGenerator(self, threshold):
        def thresholdTrigger(ptc):
            return ptc.pt() > threshold

        return thresholdTrigger

    def beginLoop(self, setup):
        super(RatePlotProducerPileUp, self).beginLoop(setup)

        self.hasTFileService = hasattr(self.cfg_ana, "file_label")
        if self.hasTFileService:
            servname = '_'.join([
                'heppy.framework.services.tfile.TFileService',
                self.cfg_ana.file_label
            ])
            tfileservice = setup.services[servname]
            self.rootfile = tfileservice.file
        else:
            self.rootfile = TFile(
                '/'.join([self.dirName, self.cfg_ana.plot_name + '.root']),
                'recreate')

        bins = array("f", self.cfg_ana.bins)
        self.histogram = TH1F(self.cfg_ana.plot_name, self.cfg_ana.plot_title,
                              len(bins) - 1, bins)
        self.numberOfEvents = self.cfg_comp.nGenEvents

    def process(self, event):
        '''Process the event.
      event must contain
    
    * self.cfg_ana.input_objects: collection of objects to be selected
       These objects must be usable by the filtering function
       self.cfg_ana.trigger_func.
    '''

        input_collection = getattr(event, self.cfg_ana.input_objects)

        #We want accept events without objects if the threshold is 0 or less
        startIdx = 0

        #Adding events for that thresholds
        for x in range(0, len(self.cfg_ana.bins) - 1):
            if self.cfg_ana.bins[x] <= 0:
                self.histogram.AddBinContent(x + 1)
                startIdx = x + 1

        #startIdx keeps track of where the positive thresholds start

        # MET is not iterable, it is a single object
        # We treat here single objects
        if not isinstance(input_collection, collections.Iterable):

            for x in range(startIdx, len(self.cfg_ana.bins) - 1):
                # Preparing the check function
                trigger_func = self.thresholdTriggerGenerator(
                    self.cfg_ana.bins[x])
                # Checking if the object passes the trigger
                if trigger_func(input_collection):
                    self.histogram.AddBinContent(x + 1)
                else:
                    #If no item passes the threshold I can stop
                    break

        elif isinstance(input_collection, collections.Mapping):

            # Iterating through all the objects
            for x in range(startIdx, len(self.cfg_ana.bins) - 1):
                # Checking what thresholds are satisfied
                isPassed = False
                for key, val in input_collection.iteritems():

                    # Preparing the check function
                    trigger_func = self.thresholdTriggerGenerator(
                        self.cfg_ana.bins[x])
                    # Checking if the object passes the trigger
                    if trigger_func(val):
                        self.histogram.AddBinContent(x + 1)
                        isPassed = True
                        # We don't need to check for other objects
                        break

                if not isPassed:
                    #If no objects passes the threshold I can stop
                    break

        else:

            for x in range(startIdx, len(self.cfg_ana.bins) - 1):
                # Checking what thresholds are satisfied
                isPassed = False
                for obj in input_collection:
                    # Preparing the check function
                    trigger_func = self.thresholdTriggerGenerator(
                        self.cfg_ana.bins[x])
                    # Checking if the object passes the trigger
                    if trigger_func(obj):
                        self.histogram.AddBinContent(x + 1)
                        isPassed = True
                        # We don't need to check for other objects
                        break

                if not isPassed:
                    #If no objects passes the threshold I can stop
                    break

    def write(self, setup):

        self.rootfile.cd()
        #Rescaling to corresponding rate
        if self.cfg_ana.normalise:
            normalisation = self.cfg_ana.zerobias_rate / self.numberOfEvents
            self.graphErrors = TGraphErrors(self.histogram)
            for x in xrange(0, self.graphErrors.GetN()):
                #Rescaling everything to have rates
                self.graphErrors.GetEY()[x] *= normalisation
                self.graphErrors.GetY()[x] *= normalisation
                self.graphErrors.SetName(self.cfg_ana.plot_name)
                self.histogram = self.graphErrors

        if hasattr(self.cfg_ana, "scale_factors"):
            for x in xrange(0, len(self.cfg_ana.scale_factors)):
                self.histogram.SetBinContent(
                    x + 1,
                    self.histogram.GetBinContent(x + 1) *
                    self.cfg_ana.scale_factors[x])

        self.histogram.Write()
        xMax = self.histogram.GetXaxis().GetXmax()
        xMin = self.histogram.GetXaxis().GetXmin()
        yMin = self.histogram.GetMinimum()
        yMax = self.histogram.GetMaximum()

        self.histogram.SetMarkerStyle(20)
        self.histogram.SetMarkerColor(4)
        self.histogram.SetLineColor(1)

        c1 = TCanvas("canvas_" + self.cfg_ana.plot_name,
                     self.cfg_ana.plot_title, 600, 600)
        c1.SetGridx()
        c1.SetGridy()
        self.histogram.Draw("PE")
        c1.SetLogy(True)

        c1.Update()
        c1.Print(self.cfg_ana.plot_name + ".svg", "svg")
            NSMassPoint.GetXaxis().SetLabelSize(0.15)
            NSMassPoint.GetYaxis().SetLabelSize(0.05)
            NSMassPoint.GetYaxis().SetTitleOffset(0.42) # 1.2 = 20% larger
            NSMassPoint.SetTitleOffset(0.42) # 1.2 = 20% larger
            NSMassPoint.SetTitle("Bkg Estimtae in each signal injected mass point: Trijet Inclusive Window: "+str(windowWidth))
            NSMassPoint.SetMarkerStyle(1)
            NSMassPoint.GetYaxis().SetRange(1, 1000000)
            NSMassPoint.SetMarkerColor(0)
            NSMassPoint.SetLineColor(0)
            NSMassPoint.Draw() #this is to make sure the x axis of pad1 mataches that of pad2
            NSErrorBar.SetFillColor(29)
            NSErrorBar.SetMarkerColor(1)
            NSErrorBar.Draw("LE3 same")
            NSErrorBar.Draw("P same")
            #NSMassPoint.Draw("E same")
            print("N : ", NSErrorBar.GetN())
            #NSErrorBar.Draw("P")

            #JBMassPoint.SetMarkerStyle(2)
            #JBMassPoint.SetMarkerColor(2)
            #JBMassPoint.SetLineColor(2)
            #JBMassPoint.Draw("P same")
            JBGraph.SetMarkerStyle(5)
            JBGraph.SetMarkerColor(2)
            JBGraph.SetLineColor(2)
            JBGraph.Draw("P same")
            r.gStyle.SetOptStat(0);

            #leg = ROOT.TLegend(0.5,0.34,0.81,0.51)
            leg = ROOT.TLegend(0.6,0.7,0.99,0.90)
Пример #28
0
def main():
    args = parser()

    filename = args.filename

    os.system('mkdir -p rootfiles')
    os.system('mkdir -p plots')

    f = tables.open_file(filename)
    print(f"Successfully opened file: {filename}.")

    data = f.get_node('/data')
    print("Read /data")

    event_ids = getDataCol('event_id', data)
    waveforms = getDataCol('waveform', data)
    times = getDataCol('time', data)
    timestamps = getDataCol('timestamp', data)
    thresFlags = getDataCol('thresholdFlags', data)
    i_1V1 = getDataCol('i_1V1', data)
    i_1V35 = getDataCol('i_1V35', data)
    i_1V8 = getDataCol('i_1V8', data)
    i_2V5 = getDataCol('i_2V5', data)
    i_3V3 = getDataCol('i_3V3', data)
    v_1V1 = getDataCol('v_1V1', data)
    v_1V35 = getDataCol('v_1V35', data)
    v_1V8 = getDataCol('v_1V8', data)
    v_2V5 = getDataCol('v_2V5', data)
    v_3V3 = getDataCol('v_3V3', data)

    nsamples = len(waveforms[0])
    fq = np.linspace(0, 240, nsamples)

    gmean = np.mean(waveforms[0])
    globalmean = np.mean(waveforms)
    print(np.mean(waveforms[0]))

    c = TCanvas("c", "c", 800, 600)
    c.Draw()
    c.SetGrid()

    of = TFile('rootfiles/{0}.root'.format(filename.split('.h', 1)[0]),
               "RECREATE")

    h = TH1D('qdist', 'qdist;ADC count;Entry', 500, -100, 900)
    hpk = TH1D('peak', 'peak;ADC count;Entry', 500, -100, 900)
    havg = TH1D('avgwf', 'Averaged Waveform;Sampling Bin;ADC count', nsamples,
                0, nsamples)
    havgfft = TH1D('avgfft',
                   'FFT Averaged Waveform;Frequency [MHz];Amplitude [LSB]',
                   int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
    hifft = TH1D('Subtwf', 'Averaged Waveform;Sampling Bin;ADC count',
                 nsamples, 0, nsamples)
    hsfft = TH1D('Subtfft',
                 'FFT Averaged Waveform;Frequency [MHz];Amplitude [LSB]',
                 int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
    hsspe = TH1D('qdist_subt', 'qdist_subt;ADC count;Entry', 500, -100, 900)
    hmax = TH1D('hmax', 'hmax;ADC count;Entry', 500, -100, 900)
    bsfluc = TH1D('bsfluc', 'bsfluc;ADC count;Entry', 100,
                  int(globalmean) - 50,
                  int(globalmean) + 50)
    bsshift = TGraphErrors()
    bsshift.SetName('bsshift')
    bsshift.SetTitle('bsshift;Event Number;Baseline [LSB]')
    nrshift = TGraphErrors()
    nrshift.SetName('nrshift')
    nrshift.SetTitle('NoiseRateShift;Event Number;Noise Rate [Hz]')
    rmsshift = TGraphErrors()
    rmsshift.SetName('rmsshift')
    rmsshift.SetTitle('RMSNoiseShift;Event Number;RMS Noise [LSB]')
    rmstime = TGraphErrors()
    rmstime.SetName('rmstime')
    rmstime.SetTitle('rmstime;Unix time [s];RMS Noise [LSB]')
    powerc = TGraphErrors()
    powerc.SetName('powerc')
    powerc.SetTitle('powerc;Event Number;Power Consumption [W]')
    h_powerc = TH1D('h_powerc', 'h_powerc;Power Consumption [W];Entry', 100, 0,
                    5)

    winmin = args.minimum
    winmax = args.minimum + args.window
    bsstart = args.baselineEst

    print(f'Total #Events: {len(waveforms)}')
    print(
        f'Setting... Window [{winmin}:{winmax}] and Pedestal start from {bsstart}.\n'
    )

    topdir = gDirectory.GetDirectory(gDirectory.GetPath())
    subdir = topdir.mkdir("Waveforms")
    subdir2 = topdir.mkdir("FFT")
    subdir3 = topdir.mkdir("proj")
    thrdir = topdir.mkdir("thresFlags")

    fltwfs = []
    starttimestamp = timestamps[0]

    procN = len(waveforms)
    if args.nevents > 0:
        procN = args.nevents

    for i in tqdm(range(procN)):
        if i < args.nskip:
            continue

        waveform = waveforms[i]
        timestamp = timestamps[i]

        bsfluc.Fill(np.mean(waveform))
        n = bsshift.GetN()
        bsshift.Set(n + 1)
        bsshift.SetPoint(n, i, np.mean(waveform))
        bsshift.SetPointError(n, 0, np.std(waveform))

        n = rmsshift.GetN()
        rmsshift.Set(n + 1)
        rmsshift.SetPoint(n, i, np.std(waveform))
        rmsshift.SetPointError(n, 0, 0)

        hmax.Fill(np.max(waveform) - np.mean(waveform))

        timeinterval = ((timestamp - starttimestamp - i * nsamples) / 240.e6 -
                        i * 0.501)
        nr = 0
        nrerr = 0
        if timeinterval > 0:
            nr = (i + 1) / timeinterval
            nrerr = np.sqrt(i + 1) / timeinterval
        n = nrshift.GetN()
        nrshift.Set(n + 1)
        nrshift.SetPoint(n, i, nr)
        nrshift.SetPointError(n, 0, nrerr)

        # FFT & IFFT ###
        F_abs_amp = doFFT(waveform)

        F = np.fft.fft(waveform)
        F2 = np.copy(F)
        fc = 60
        df = 2
        F2[(fq > fc - df) & (fq < fc + df)] = 0
        F2[(fq > 2 * fc - df) & (fq < 2 * fc + df)] = 0
        F2[(fq > 240 - fc - df) & (fq < 240 - fc + df)] = 0
        F2_abs = np.abs(F2)
        F2_abs_amp = F2_abs / len(waveform) * 2
        F2_abs_amp[0] = F_abs_amp[0] / 2

        F2_ifft = np.fft.ifft(F2)
        F2_ifft_real = F2_ifft.real
        ######

        baseline_mean = np.mean(waveform[bsstart:nsamples])
        if args.fixbs:
            baseline_mean = globalmean

        center = int(baseline_mean)
        proj = TH1D(f'proj{i}', f'Projection waveform{i};ADC count;Entry', 300,
                    center - 150, center + 150)
        selected = waveform[waveform < np.mean(waveform) + 10]
        for j in range(len(selected)):
            proj.Fill(selected[j])
        #proj.Fit("gaus")
        #if len(selected) > 0:
        #    f = proj.GetFunction('gaus')
        #    norm = f.GetParameter(0)
        #    mean = f.GetParameter(1)
        #    sigma = f.GetParameter(2)

        reduced_waveform = waveform - baseline_mean
        scale = (nsamples - bsstart) / (winmax - winmin)
        #h.Fill(sum(waveform[winmin:winmax])-sum(waveform[bsstart:nsamples])/scale)
        h.Fill(sum(reduced_waveform[winmin:winmax]))

        hsspe.Fill(
            np.sum(F2_ifft_real[winmin:winmax]) - baseline_mean *
            (winmax - winmin))

        hpk.Fill(
            max(waveform[winmin:winmax]) - np.mean(waveform[bsstart:nsamples]))

        hfft = TH1D(f'FFT{i}', 'FFT{i};Frequency [MHz];Amplitude [LSB]',
                    int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
        hfft2 = TH1D(f'FFT2{i}', 'FFT2{i};Frequency [MHz];Amplitude [LSB]',
                     int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
        for j in range(int(len(F_abs_amp) / 2. + 1)):
            hfft.Fill(fq[j], F_abs_amp[j])
            hfft2.Fill(fq[j], F2_abs_amp[j])

        h2 = TH1D(f'w{i}', 'Waveform{i};Sampling Bin;ADC count', nsamples, 0,
                  nsamples)
        for j in range(len(waveform)):
            h2.Fill(j, waveform[j])
        #h2.Draw("hist")

        #if np.max(F2_abs_amp[1:int(len(F2_abs_amp)/2.+1)]) > 0.5:
        #    c.SetLogy(0)
        #    h2.SetLineColor(4)
        #    h2.Draw("hist")
        #    c.Print(f"plots/w{i}.pdf(")
        #    c.SetLogy(1)
        #    hfft.SetLineColor(4)
        #    hfft.Draw("hist")
        #    c.Print(f"plots/w{i}.pdf)")

        htot = TH1D(f'thresflags{i}',
                    'ThresholdFlags{i};Sampling Bin;Threshold Flag', nsamples,
                    0, nsamples)
        for j in range(len(thresFlags[i])):
            htot.Fill(j, thresFlags[i][j])

        if max(waveform) - baseline_mean < args.threshold:
            continue

        fltwfs.append(waveform)

        if args.silent:
            del hfft
            del hfft2
            del h2
            del htot
            del proj
            del waveform
            del timestamp
            del F
            del F2
            gc.collect()
            continue

        subdir2.cd()
        hfft.Write()

        subdir.cd()
        h2.Write()

        subdir3.cd()
        proj.Write()

        thrdir.cd()
        htot.Write()

    print('')

    for i in range(len(i_1V1)):
        internal_power = v_1V1[i] * i_1V1[i] + v_1V35[i] * i_1V35[i] + v_1V8[
            i] * i_1V8[i] + v_2V5[i] * i_2V5[i] + v_3V3[i] * i_3V3[i]
        n = powerc.GetN()
        powerc.Set(n + 1)
        powerc.SetPoint(n, i, internal_power * 1e-3)
        powerc.SetPointError(n, 0, 0)
        h_powerc.Fill(internal_power * 1e-3)

    avgfltwfs = np.mean(fltwfs, axis=0)
    for i in range(len(avgfltwfs)):
        havg.Fill(i, avgfltwfs[i])

    Favg_abs_amp = doFFT(avgfltwfs)

    Favg = np.fft.fft(avgfltwfs)
    Favg2 = np.copy(Favg)
    Favg2[(fq > 59) & (fq < 61)] = 0
    Favg2[(fq > 119) & (fq < 121)] = 0
    Favg2[(fq > 179) & (fq < 181)] = 0

    Favg2_abs = np.abs(Favg2)
    Favg2_abs_amp = Favg2_abs / len(avgfltwfs) * 2
    Favg2_abs_amp[0] = Favg_abs_amp[0] / 2

    Favg2_ifft = np.fft.ifft(Favg2)
    Favg2_ifft_real = Favg2_ifft.real

    for i in range(int(len(Favg_abs_amp) / 2. + 1)):
        havgfft.Fill(fq[i], Favg_abs_amp[i])

    for i in range(int(len(Favg2_abs_amp) / 2. + 1)):
        hsfft.Fill(fq[i], Favg2_abs_amp[i])

    for i in range(len(Favg2_ifft_real)):
        hifft.Fill(i, Favg2_ifft_real[i])

    topdir.cd()
    h.Write()
    hpk.Write()
    havg.Write()
    havgfft.Write()
    hsfft.Write()
    hifft.Write()
    hsspe.Write()
    hmax.Write()
    bsfluc.Write()
    bsshift.Write()
    nrshift.Write()
    rmsshift.Write()
    powerc.Write()
    h_powerc.Write()

    of.Close()
    f.close()