Exemplo n.º 1
0
def fit_shape(
    model, bcid, hists, name, crange
):
    fitmethod = lambda pdf, data: pdf.fitTo(
        data, RooFit.Save(), RooFit.PrintLevel(1), RooFit.Verbose(0)
    )
    result, modfuncs, datahist = fit(model, hists, fitmethod)
    hdata = data_hist(model.xvar(), model.yvar(), datahist)
    hmodel = model_hist(model.xvar(), model.yvar(), modfuncs)
    chisqs, dofs = compute_chisq(hmodel, hdata)
    __, __, scRes = residual_hist(hdata, hmodel, 1.0, crange=crange)
    return result, scRes, chisqs, dofs
Exemplo n.º 2
0
def compare_binning(filename):
    scans = ('X1', 'Y1', 'X2', 'Y2')

    with BareRootFile(filename) as f:
        oldres = [f.get('residualHist{0}'.format(scan)) for scan in scans]
        oldchi = [f.get_val('chisq{0}'.format(scan)) for scan in scans]
        olddof = [f.get_val('dof{0}'.format(scan)) for scan in scans]
        name = f.get_val('name')
    bcidpos = name.find('bcid')
    dataname = name[16:name.rfind('_', 0, bcidpos - 1)]
    fitname = name[name.rfind('_', 0, bcidpos - 1) + 1:bcidpos - 1]
    bcid = int(name[bcidpos + 4:])

    model = {
        'SG': SingleGauss,
        'DG': DoubleGaussFit,
        'TG': TripleGaussFit
    }[fitname]()
    model.load_root(filename)
    modfuncs = model.model_functions()

    with open('res/hist/{0}.json'.format(dataname)) as f:
        json = load(f)
    nbins = json['nbins']
    scaling = json['scaling']
    datafile = '{0}/{1}.root'.format(json['datapath'], json['name'])

    hists = []
    with BareRootFile(datafile) as f:
        for histname in [
                'hist_Beam2MoveX_bunch{0}Add', 'hist_Beam2MoveY_bunch{0}Add',
                'hist_Beam1MoveX_bunch{0}Add', 'hist_Beam1MoveY_bunch{0}Add'
        ]:
            hist = f.get(histname.format(bcid))
            hists.append(hist)
    datahist = [
        RooDataHist('scan{0}Beam{1}RestDataHist'.format(c, i),
                    'scan{0}Beam{1}RestDataHist'.format(c, i),
                    RooArgList(model.xvar(), model.yvar()), hists[j])
        for j, (i, c) in enumerate([('1', 'X'), ('1', 'Y'), ('2',
                                                             'X'), ('2', 'Y')])
    ]

    chisq, dof, hdata, hmodel = compute_chisq(model, modfuncs, datahist, nbins)
    scDat, scMod, scRes = residual_hist(hdata, hmodel, scaling)

    for i, scan in enumerate(scans):
        oldval = oldchi[i] / olddof[i]
        newval = chisq[i] / dof[i]
        print '{0}: {1:.4f} {2:.4f}'.format(scan, oldval, newval)

    return scRes, oldres
Exemplo n.º 3
0
def do_closureTest(
    suffix, toymodel, fitmodel, vtxresx, vtxresy=None,
    n=1, eachtoy=None, eachfit=None
):
    if vtxresy is None:
        vtxresy = vtxresx
    if eachtoy is None:
        eachtoy = lambda toy, r: toy.overlap_func()
    if eachfit is None:
        eachfit = lambda fit: fit

    name = 'ClosureTest_v1_{0}_{1}_{2}' \
           .format(toymodel.name(), fitmodel.name(), suffix)

    rand = TRandom3()
    rand.SetSeed(0)

    fitmodel.set_vtxres(vtxresx, vtxresy)
    for par in ['x011', 'x012', 'x021', 'x022', 'y011', 'y012', 'y021', 'y022']:
        fitmodel.parameter(par).setConstant()
    fitmethod = lambda pdf, data: pdf.fitTo(
        data, RooFit.Save(), RooFit.PrintLevel(1), RooFit.Verbose(0)
    )

    tree = RootTree('closureTest', 'Closure Test')
    for par in toymodel.fit_parameters:
        tree.branch_f('toy_{0}'.format(par))
    for par in fitmodel.fit_parameters:
        tree.branch_f('fit_{0}'.format(par))
    tree.branch_f('toy_overlapTrue')
    tree.branch_f('toy_overlapDiff')
    tree.branch_f('toy_overlapDiff_error')
    tree.branch_f('fit_overlapTrue')
    tree.branch_f('fit_overlapDiff')
    tree.branch_f('fit_overlapDiff_error')
    tree.branch_f('fit_chisq')
    tree.branch_i('fit_dof')
    tree.branch_f('fit_minNll')
    tree.branch_f('nEntries', 4)

    for i in range(n):
        print '<<< {0}: Generate toy model ({1})'.format(i, Timestamp())
        overlap = eachtoy(toymodel, rand)
        hists, nevents = generate_toys(
            overlap, vtxresx, vtxresy=vtxresy, rand=rand, nbins=760#, verbose=True
        )
        for par in toymodel.fit_parameters:
            tree.set('toy_{0}'.format(par), toymodel.parameter(par).val())
        for j, value in enumerate(nevents):
            tree.set('nEntries', value, j)

        print '<<< {0}: Compute correction for toy model ({1})' \
              .format(i, Timestamp())
        toy_tru, toy_fit, toy_dif = \
            compute_correction(overlap, rand=rand, extended=False)
        tree.set('toy_overlapTrue', toy_tru)
        mean = sum(toy_dif)/len(toy_dif)
        error = (sum([(d-mean)**2 for d in toy_dif])/len(toy_dif))**0.5
        tree.set('toy_overlapDiff', mean)
        tree.set('toy_overlapDiff_error', error)

        print '<<< {0}: Fit toy model ({1})'.format(i, Timestamp())
        fitmodel = eachfit(fitmodel)
        result, modfuncs, datahist = fit(fitmodel, hists, fitmethod)
        print '<<< {0}: Create model histogram for fit ({1})' \
              .format(i, Timestamp())
        hmodel = model_hist(fitmodel.xvar(), fitmodel.yvar(), modfuncs)
        print '<<< {0}: Create data histogram for fit ({1})' \
              .format(i, Timestamp())
        hdata = data_hist(fitmodel.xvar(), fitmodel.yvar(), datahist)
        print '<<< {0}: Compute chisquare of fit ({1})'.format(i, Timestamp())
        chisqs, dofs = compute_chisq(hmodel, hdata)
        for par in fitmodel.fit_parameters:
            tree.set('fit_{0}'.format(par), fitmodel.parameter(par).val())
        tree.set('fit_minNll', result.minNll())
        tree.set('fit_chisq', sum(chisqs))
        tree.set('fit_dof', sum(dofs))

        print '<<< {0}: Compute correction for fit model ({1})' \
              .format(i, Timestamp())
        overlap = fitmodel.overlap_func()
        fit_tru, fit_fit, fit_dif = \
            compute_correction(overlap, rand=rand, extended=False)
        tree.set('fit_overlapTrue', fit_tru)
        mean = sum(fit_dif)/len(fit_dif)
        error = (sum([(d-mean)**2 for d in fit_dif])/len(fit_dif))**0.5
        tree.set('fit_overlapDiff', mean)
        tree.set('fit_overlapDiff_error', error)

        print '<<< {0}: Fill tree ({1})'.format(i, Timestamp())
        tree.Fill()

    output = RootFile(name, 'RECREATE')
    tree.Write()
    Timestamp().Write()
    output.Write()
    return output.close()
Exemplo n.º 4
0
def fit_shape(model,
              bcid,
              datafile,
              inputfile,
              name,
              nbins,
              vtxresx,
              vtxresy=None,
              scaling=1.0,
              heavyion=False):
    if heavyion:
        parameters = model.load_json(
            parameterfile='res/shapes/{}hi.json'.format(model.name()))
        crange = (-20.0, 20.0)
    else:
        parameters = model.load_json()
        crange = (-10.0, 10.0)
    supermod = bool(model.name() not in ('SG', 'DG', 'TG'))
    with RootFile(inputfile) as f:
        for par in model.parameters():
            if par.is_formula():
                continue
            parname = par.GetName()
            if match('^[xy]0[12][12]$', parname):
                value = f.get_val('final/{0}'.format(parname))
                par.setVal(value)
                par.setConstant(True)
                parameters.append(
                    NamedFloat('{0}_const'.format(parname), value))
                continue
            if supermod and match('^rho[MW][12]$', parname):
                value = f.get_val('final/rho{0}{1}'.format({
                    'M': 'N',
                    'W': 'M'
                }[parname[3]], parname[4]))
                lo, hi = max(value - 0.1, -0.9), min(value + 0.1, 0.9)
            elif supermod and match('^[xy]WidthM[12]$', parname):
                value = f.get_val('final/{0}WidthN{1}'.format(
                    parname[0], parname[7]))
                lo, hi = value * 0.9, value * 1.1
            elif supermod and match('^[xy]WidthW[12]Diff$', parname):
                value = f.get_val('final/{0}WidthM{1}Diff'.format(
                    parname[0], parname[7]))
                lo, hi = max(value * 0.9, 0.001), value * 1.1
            elif supermod and match('^w[12]MFraction$', parname):
                value = f.get_val('final/w{0}N'.format(parname[1]))
                lo, hi = max(value - 0.01, 0.0), min(value + 0.01, 1.0)
            elif not supermod and match('^[xy]Width[NM][12]$', parname):
                try:
                    value = f.get_val('final/{0}'.format(parname))
                    lo, hi = value * 0.8, value * 1.2
                except:
                    continue
            elif (not supermod and match('^rho[NMW][12]$', parname)
                  and not model.name() == 'SG'):
                value = f.get_val('final/rhoN{0}'.format(parname[4]))
                lo, hi = max(value - 0.1, -0.9), min(value + 0.1, 0.9)
            else:
                continue
            par.setRange(lo, hi)
            par.setVal(value)
            for p in parameters:
                if p.GetName() == '{0}_min'.format(parname):
                    p.SetVal(lo)
                if p.GetName() == '{0}_max'.format(parname):
                    p.SetVal(hi)
                if p.GetName() == '{0}_ini'.format(parname):
                    p.SetVal(value)
    if vtxresy is None:
        vtxresy = vtxresx
    model.set_vtxres(vtxresx / scaling, vtxresy / scaling)

    hists = []
    with BareRootFile(datafile) as f:
        for histname in [
                'hist_Beam2MoveX_bunch{0}Add', 'hist_Beam2MoveY_bunch{0}Add',
                'hist_Beam1MoveX_bunch{0}Add', 'hist_Beam1MoveY_bunch{0}Add'
        ]:
            hist = f.Get(histname.format(bcid))
            hist.SetDirectory(0)
            hists.append(hist)

    fitmethod = lambda pdf, data: pdf.fitTo(data, RooFit.Save(
    ), RooFit.PrintLevel(1), RooFit.Verbose(0))

    result, modfuncs, datahist = fit(model, hists, fitmethod)

    hdata = data_hist(model.xvar(), model.yvar(), datahist)
    hmodel = model_hist(model.xvar(), model.yvar(), modfuncs)
    chisqs, dofs = compute_chisq(hmodel, hdata)

    true, avg, rms = overlap_variations(model)

    scDat, scMod, scRes = residual_hist(hdata, hmodel, scaling, crange=crange)

    model.factor = 100.0
    corrTree = compute_correction(model.overlap_func())

    outputname = 'BeamImaging_v2_{0}_{1}_bcid{2}' \
                 .format(name, model.name(), bcid)
    with RootFile(outputname, 'RECREATE') as f:
        result.Write('fitResult')
        for hist in scDat + scMod + scRes:
            hist.Write()
        corrTree.Write()
        for i, scan in enumerate(('X1', 'Y1', 'X2', 'Y2')):
            NamedFloat('chisq{0}'.format(scan), chisqs[i]).Write()
            NamedFloat('dof{0}'.format(scan), dofs[i]).Write()
        NamedFloat('overlap_true', true).Write()
        NamedFloat('overlap_average', avg).Write()
        NamedFloat('overlap_rms', rms).Write()
        NamedFloat('scaling', scaling).Write()
        Timestamp().Write()
        NamedString('name', outputname).Write()
        f.mkdir('initial').cd()
        for par in parameters:
            par.Write()
        f.mkdir('final').cd()
        for par in model.parameters():
            NamedFloat(par.GetName(), par.val()).Write()
            NamedFloat('{0}_error'.format(par.GetName()),
                       par.err(model.parameter)).Write()
Exemplo n.º 5
0
def do_closureTest(
    suffix, toymodel, tempmodel, fitmodel, inputfile, vtxresx, vtxresy=None
):
    toyparameters = toymodel.load_json(inputfile)
    tempparameters = tempmodel.load_json()
    fitparameters = fitmodel.load_json()

    if vtxresy is None:
        vtxresy = vtxresx
    toymodel.set_vtxres(vtxresx, vtxresy)
    tempmodel.set_vtxres(vtxresx, vtxresy)
    fitmodel.set_vtxres(vtxresx, vtxresy)

    fitmethod = lambda pdf, data: pdf.fitTo(
        data, RooFit.Save(), RooFit.PrintLevel(1), RooFit.Verbose(0)
    )

    name = 'ClosureTest_v3_{0}_{1}_{2}' \
           .format(toymodel.name(), fitmodel.name(), suffix)

    rand = TRandom3()
    rand.SetSeed(0)

    overlap = toymodel.overlap_func()
    hists, nevents = generate_toys(
        overlap, vtxresx, vtxresy=vtxresy, rand=rand, nbins=760#, verbose=True
    )

    toy_tru, toy_fit, toy_dif = \
        compute_correction(overlap, rand=rand, extended=False)
    mean = sum(toy_dif)/len(toy_dif)
    error = (sum([(d-mean)**2 for d in toy_dif])/len(toy_dif))**0.5

    tresult, tmodfuncs, tdatahist = fit(tempmodel, hists, fitmethod)
    tresult.SetName('temp_fitResult')
    tmodel = model_hist(tempmodel.xvar(), tempmodel.yvar(), tmodfuncs)
    tdata = data_hist(tempmodel.xvar(), tempmodel.yvar(), tdatahist)
    for hist in tmodel + tdata:
        hist.SetName('temp_{0}'.format(hist.GetName()))
    tchisqs, tdofs = compute_chisq(tmodel, tdata)

    for par in fitmodel.parameters():
        if par.is_formula():
            continue
        parname = par.GetName()
        if match('^[xy]0[12][12]$', parname):
            value = tempmodel.parameter(parname).val()
            par.setVal(value)
            par.setConstant(True)
            continue
        if match('^rho[MW][12]$', parname):
            value = tempmodel.parameter('rho{0}{1}'.format(
                {'M': 'N', 'W': 'M'}[parname[3]], parname[4])
            ).val()
            lo, hi = value-0.1, value+0.1
            if lo < -0.9:
                lo = -0.9
            if hi > 0.9:
                hi = 0.9
        elif match('^[xy]WidthM[12]$', parname):
            value = tempmodel.parameter('{0}WidthN{1}'.format(
                parname[0], parname[7]
            )).val()
            lo, hi = value*0.9, value*1.1
        elif match('^[xy]WidthW[12]Diff$', parname):
            value = tempmodel.parameter('{0}WidthM{1}Diff'.format(
                parname[0], parname[7]
            )).val()
            lo, hi = value*0.9, value*1.1
            if lo < 0.001:
                lo = 0.001
        elif match('^w[12]MFraction$', parname):
            value = tempmodel.parameter('w{0}N'.format(parname[1])).val()
            lo, hi = value-0.01, value+0.01
            if lo < 0.0:
                lo = 0.0
            if hi > 1.0:
                hi = 1.0
        else:
            continue
        par.setRange(lo, hi)
        par.setVal(value)
    result, modfuncs, datahist = fit(fitmodel, hists, fitmethod)
    result.SetName('fit_fitResult')
    hmodel = model_hist(fitmodel.xvar(), fitmodel.yvar(), modfuncs)
    hdata = data_hist(fitmodel.xvar(), fitmodel.yvar(), datahist)
    for hist in hmodel + hdata:
        hist.SetName('fit_{0}'.format(hist.GetName()))
    chisqs, dofs = compute_chisq(hmodel, hdata)

    ttrue, tavg, trms = overlap_variations(tempmodel)
    true, avg, rms = overlap_variations(fitmodel)

    crange = (-10.0, 10.0)
    stDat, stMod, stRes = residual_hist(tdata, tmodel, 1.0, crange=crange)
    for hist in stDat + stMod + stRes:
        hist.SetName('temp_{0}'.format(hist.GetName()))
    scDat, scMod, scRes = residual_hist(hdata, hmodel, 1.0, crange=crange)
    for hist in scDat + scMod + scRes:
        hist.SetName('fit_{0}'.format(hist.GetName()))

    tempmodel.factor = 100.0
    tempTree = compute_correction(tempmodel.overlap_func(), rand=rand)
    tempTree.SetName('temp_corrTree')

    fitmodel.factor = 100.0
    corrTree = compute_correction(fitmodel.overlap_func(), rand=rand)
    corrTree.SetName('fit_corrTree')

    with RootFile(name, 'RECREATE') as f:
        for obj in stDat + stMod + stRes + scDat + scMod + scRes + [
            tresult, result, tempTree, corrTree,
            NamedFloat('temp_overlap_true', ttrue),
            NamedFloat('temp_overlap_average', tavg),
            NamedFloat('temp_overlap_rms', trms),
            NamedFloat('fit_overlap_true', true),
            NamedFloat('fit_overlap_average', avg),
            NamedFloat('fit_overlap_rms', rms),
            Timestamp(), NamedString('name', name)
        ]:
            obj.Write()
        for i, scan in enumerate(('X1', 'Y1', 'X2', 'Y2')):
            NamedFloat('temp_chisq{0}'.format(scan), tchisqs[i]).Write()
            NamedFloat('temp_dof{0}'.format(scan), tdofs[i]).Write()
            NamedFloat('fit_chisq{0}'.format(scan), chisqs[i]).Write()
            NamedFloat('fit_dof{0}'.format(scan), dofs[i]).Write()
        f.mkdir('toyparameters').cd()
        for par in toyparameters:
            par.Write()
        f.mkdir('temp_initial').cd()
        for par in tempparameters:
            par.Write()
        f.mkdir('fit_initial').cd()
        for par in fitparameters:
            par.Write()
        f.mkdir('temp_final').cd()
        for par in tempmodel.parameters():
            NamedFloat(par.GetName(), par.val()).Write()
            NamedFloat(
                '{0}_error'.format(par.GetName()), par.err(tempmodel.parameter)
            )
        f.mkdir('fit_final').cd()
        for par in fitmodel.parameters():
            NamedFloat(par.GetName(), par.val()).Write()
            NamedFloat(
                '{0}_error'.format(par.GetName()), par.err(fitmodel.parameter)
            )
Exemplo n.º 6
0
def integrate_residuals(model,
                        bcid,
                        datafile,
                        outputname,
                        nbins,
                        scaling=1.0,
                        crange=None):
    model.load_root(outputname)
    hists = []
    with BareRootFile(datafile) as f:
        for histname in [
                'hist_Beam2MoveX_bunch{0}Add', 'hist_Beam2MoveY_bunch{0}Add',
                'hist_Beam1MoveX_bunch{0}Add', 'hist_Beam1MoveY_bunch{0}Add'
        ]:
            hist = f.Get(histname.format(bcid))
            hist.SetDirectory(0)
            hists.append(hist)

    modfuncs = model.model_functions()
    datahist = [
        RooDataHist('scan{0}Beam{1}RestDataHist'.format(c, i),
                    'scan{0}Beam{1}RestDataHist'.format(c, i),
                    RooArgList(model.xvar(), model.yvar()), hists[j])
        for j, (i, c) in enumerate(ic)
    ]

    hdata = data_hist(model.xvar(), model.yvar(), datahist, nbins=nbins)
    hmodel = model_hist(model.xvar(),
                        model.yvar(),
                        modfuncs,
                        nbins=nbins,
                        crange=crange)
    chisqs, dofs = compute_chisq(hmodel, hdata, nbins=nbins)
    scDat, scMod, scRes = residual_hist(hdata, hmodel, scaling, crange=crange)

    with RootFile(outputname) as old, RootFile(outputname, 'RECREATE') as new:
        condition = lambda key: key.GetName() not in [
            'dataHistX1',
            'dataHistY1',
            'dataHistX2',
            'dataHistY2',
            'modelHistX1',
            'modelHistY1',
            'modelHistX2',
            'modelHistY2',
            'residualHistX1',
            'residualHistY1',
            'residualHistX2',
            'residualHistY2',
            'chisqX1',
            'chisqY1',
            'chisqX2',
            'chisqY2',
            'dofX1',
            'dofY1',
            'dofX2',
            'dofY2',
        ]
        copy_directory(new, old, condition=condition)
        new.cd()
        for hist in scDat + scMod + scRes:
            hist.Write()
        for i, scan in enumerate(('X1', 'Y1', 'X2', 'Y2')):
            NamedFloat('chisq{0}'.format(scan), chisqs[i]).Write()
            NamedFloat('dof{0}'.format(scan), dofs[i]).Write()
        Timestamp('resTimestamp').Write()