コード例 #1
0
def prepare_trees(configfile,
                  outputpath,
                  mintrk=0,
                  verbose=False,
                  scans=None,
                  noerror=False,
                  start=None):
    if scans is None:
        scans = ('1X', '1Y', '2X', '2Y')
    with open(configfile) as f:
        config = load(f)
    fill = config['fill']
    version = config['version']
    bcids = config['bcids']
    name = 'Fill{0}_{1}'.format(fill, version)
    for scan in scans:
        print '<<< Prepare scan files: {0}'.format(scan)
        basefilelist = config['scan{0}MoveFiles'.format(scan)]
        begin = config['scan{0}MoveBegin'.format(scan)]
        end = config['scan{0}MoveEnd'.format(scan)]
        times = zip(begin, end)
        if start is None:
            filelists = (('{0}/{1}_{2}.root'.format(outputpath, name,
                                                    scan), basefilelist), )
        else:
            filelists = map(
                lambda (i, f):
                ('{0}/{1}_{2}_file{3}.root'.format(outputpath, name, scan, i),
                 (f, )), enumerate(basefilelist[start:], start=start))
        for i, (filename, filelist) in enumerate(filelists):
            print '<<< Now at filelist {0} of {1}'.format(
                (0 if start is None else start) + i, len(filelists))
            trees = make_trees(filelist, times, bcids, mintrk, verbose,
                               noerror, qualities)
            with BareRootFile(filename, 'RECREATE') as f:
                for tree in trees.itervalues():
                    nam = tree.GetName()
                    treename = 'Beam{0}Move{1}_{2}'.format(
                        scan[0], scan[1], nam)
                    tree.SetName(treename)
                    tree.Write(treename)
                Timestamp().Write()
                NamedString('name', name).Write()
                NamedString('scan', scan).Write()
コード例 #2
0
def prepare_histograms(configfile,
                       outputpath,
                       suffix,
                       nbins,
                       mintrk,
                       scaling=1.0,
                       verbose=False,
                       stepsize=None,
                       stepsize1Y=None,
                       stepsize2X=None,
                       stepsize2Y=None,
                       extracond=None,
                       singlepair=False,
                       beamcorrectionsfile=None,
                       betacorrectionsfile=None):
    scans = ['1X', '1Y', '2X', '2Y']
    if singlepair:
        sourcescan = {'1X': '1X', '1Y': '1Y', '2X': '1X', '2Y': '1Y'}
    else:
        sourcescan = {'1X': '1X', '1Y': '1Y', '2X': '2X', '2Y': '2Y'}
    with open(configfile) as f:
        config = load(f)
    fill = config['fill']
    version = config['version']
    bcids = config['bcids']
    heavyion = bool('heavyion' in config and config['heavyion'])
    name = 'Fill{0}_{1}'.format(fill, version)
    xerror = TH1F('xerror', '', 100, 0.0, 0.03)
    yerror = TH1F('yerror', '', 100, 0.0, 0.03)
    histograms = []
    if stepsize is None:
        steps = False
    elif None in (stepsize1Y, stepsize2X, stepsize2Y):
        steps = {
            '1X': (stepsize[0], 0.0),
            '1Y': (0.0, stepsize[1]),
            '2X': (-stepsize[0], 0.0),
            '2Y': (0.0, -stepsize[1])
        }
    else:
        steps = {
            '1X': stepsize,
            '1Y': stepsize1Y,
            '2X': stepsize2X,
            '2Y': stepsize2Y
        }
    if heavyion:
        crange = (-20.0, 20.0)
    else:
        crange = (-10.0, 10.0)
    for scan in scans:
        filename = '{0}/{1}_{2}.root'.format(outputpath, name,
                                             sourcescan[scan])
        if not exists(filename):
            filename = '{0}/{1}_{2}_file*.root'.format(outputpath, name,
                                                       sourcescan[scan])
        trees = {}
        for bcid in bcids:
            treename = 'Beam{0}Move{1}_bunch{2}Add' \
                       .format(sourcescan[scan][0], sourcescan[scan][1], bcid)
            trees[bcid] = TChain(treename)
            trees[bcid].Add(filename)
            print trees[bcid].GetEntries()
            if singlepair and scan != sourcescan[scan]:
                trees[bcid].SetName(
                    replace(trees[bcid].GetName(), 'Beam1', 'Beam2'))
        if stepsize:
            hists = make_vdmhistos(trees,
                                   nbins,
                                   mintrk,
                                   steps[scan],
                                   scaling=scaling,
                                   crange=crange,
                                   verbose=verbose)
        else:
            hists = make_histograms_forbcid(
                trees,
                nbins,
                mintrk,
                scaling=scaling,
                verbose=verbose,
                extracond=extracond,
                beamcorrectionsfile=beamcorrectionsfile,
                betacorrectionsfile=betacorrectionsfile)
            mergehistname = 'Beam{0}Move{1}' \
                       .format(sourcescan[scan][0], sourcescan[scan][1])
            mergedhists = make_histograms(hists, mergehistname, nbins)
            mergedhists.SetDirectory(0)
            histograms.append(mergedhists)
        for hist in hists.itervalues():
            hist.SetDirectory(0)
            histograms.append(hist)

        if not singlepair or not scan.startswith('2'):
            condition = 'vtx_nTrk>={0}'.format(mintrk)
            errx = TH1F('errx', '', 100, 0.0, 0.03)
            erry = TH1F('erry', '', 100, 0.0, 0.03)
            for tree in trees.itervalues():
                tree.Draw('vtx_xError>>+errx', condition, 'goff')
                tree.Draw('vtx_yError>>+erry', condition, 'goff')
            xerror.Add(errx)
            yerror.Add(erry)
            errx.Delete()
            erry.Delete()
    output = {
        'fill': fill,
        'name': '{0}_{1}'.format(name, suffix),
        'bcids': bcids,
        'datapath': outputpath,
        'dataname': '{0}_{1}'.format(name, suffix),
        'vtxresx': round(xerror.GetMean(), 6),
        'vtxresy': round(yerror.GetMean(), 6),
        'scaling': scaling,
        'mintrk': mintrk,
        'nbins': nbins,
        'heavyion': bool('heavyion' in config and config['heavyion'])
    }
    if extracond is not None:
        output['extracond'] = extracond
    filename1 = 'res/hist/{0}.json'.format(output['name'])
    if not exists('res'):
        mkdir('res')
    if not exists('res/hist'):
        mkdir('res/hist')
    with open(filename1, 'w') as f:
        dump(output, f, indent=4, separators=(',', ': '))
    filename2 = '{0}/{1}.root'.format(outputpath, output['name'])
    with BareRootFile(filename2, 'RECREATE') as f:
        for hist in histograms:
            hist.Write()
        xerror.Write()
        yerror.Write()
        Timestamp().Write()
        for n, v in (('stepsize1X', stepsize), ('stepsize1Y', stepsize1Y),
                     ('stepsize2X', stepsize2X), ('stepsize2Y', stepsize2Y)):
            if v is not None:
                NamedFloat(n + '_x', v[0]).Write()
                NamedFloat(n + '_y', v[1]).Write()
        NamedString('name', output['name']).Write()
    return filename2
コード例 #3
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()
コード例 #4
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)
            )