Exemplo n.º 1
0
Arquivo: kkmass2.py Projeto: xshi/dhad
def fit_single_mode(datatype, mode, label, lowmass, highmass, test):
    evtpath = os.path.join(attr.datpath, 'sel', label, 'kkmass')
    modekey = tools.get_modekey(mode)
    tag = 'single'
    evtfile = tools.set_file('evt', datatype, modekey, tag,  prefix='',
                             forceCombine=1, extbase=evtpath)
    
    load_roofit_lib(datatype, label)
    cuts = '%f<kkmass && kkmass<%f' % (lowmass, highmass)
    #err_type = 'ASYM'
    err_type = 'SYMM'
    setGamma, setMres, setR, mc = get_common_parameters(datatype, label)
    Sigma, Fa, Fb, Sa, Sb = get_resolution_paras(modekey, label)

    N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1, xi = init_paras_single(
        label, datatype, 's', modekey)

    title1 = '%s : %s < KK mass < %s ' %(attr.modes[modekey]['uname'],
                                         lowmass, highmass)
    title2 = '%s : %s < KK mass < %s ' %(attr.modes[modekey]['unamebar'],
                                         lowmass, highmass)
    prefix='dir_%s/kkmass2/%s_%s' % (label, lowmass, highmass)
    epsfile = tools.set_file('eps', datatype, modekey, tag,
                             prefix=prefix, extbase=attr.figpath)
    txtfile = tools.set_file('txt', datatype, modekey, tag,
                             prefix=prefix, extbase=attr.fitpath)

    #mbc_gau_che(evtfile, mc, setMres, setGamma, setR, sigmap1, Sa, Sb, Fa,
    mbc_dline_che(evtfile, mc, setMres, setGamma, setR, sigmap1, Sa, Sb, Fa,
                  Fb, md, p, xi, N1, N2, Nbkgd1, Nbkgd2, title1, title2,
                  epsfile, txtfile, cuts=cuts, err_type=err_type, test=test)
    
    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 2
0
def main(opts, args):

    if len(args) == 3:
        brsfile = create_brsfile_three(args)
    elif len(args) == 2:
        brsfile = create_brsfile_two(args)
    else:
        raise NameError(args)
    
    brspath, brsname = os.path.split(brsfile)
    os.chdir(brspath)

    figname = brsname.replace('.mnf', '.eps')

    p = pexpect.spawn ('mn_fit')
    p.expect ('Give screen device name')
    p.sendline ('\n')
    p.expect ('MN_CMD> ')
    p.sendline ('exec %s' %brsname)

    if len(args) == 3:
        p.expect ('any character to skip')
        p.sendline ('\n')
    
    p.expect ('End of Macro %s' %brsname)
    p.sendline ('\n')
    p.expect ('MN_CMD> ')
    p.sendline ('quit')

    shutil.copyfile('plot.eps', figname)
    eps2pdf(figname)
    eps2png(figname)
    sys.stdout.write('Save as %s/%s \n' %(brspath, figname))
Exemplo n.º 3
0
def stage_nondiag(dt_type, label, x, y, interact=False):
    if label != '281ipbv0.2':
        load_roofit_lib(dt_type, label)

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle("Plain")

    # STAGE 1: fit for non-diagonal
    sys.stdout.write('Stage 1: fit non-diagonal.\n')

    single_modes = [(pair, sign) for pair in modes for sign in [-1, 1]]

    prefix='dir_'+label+'/crossfeeds'
    
    #for x, y  in [(x, y) for x in single_modes for y in single_modes if x!=y]:
    if x[1] == 1:
        uname = modes[x[0]]['uname']
        fname = modes[x[0]]['fname']
    else:
        uname = modes[x[0]]['unamebar']
        fname = modes[x[0]]['fnamebar']
    if y[1] == 1:
        unameb = modes[y[0]]['uname']
        fnameb = modes[y[0]]['fname']
    else:
        unameb = modes[y[0]]['unamebar']
        fnameb = modes[y[0]]['fnamebar']

    title  = '%s fakes %s' % (uname, unameb)

    bkgname = ('%s_Single_%s_fakes_Single_%s.evt' %(dt_type, fname, fnameb))
    bkgfile = get_bkgfile(bkgname, label)

    epsname = '%s_Single_%s_fakes_Single_%s.eps' % (dt_type, fname, fnameb)
    epsfile  = tools.set_file(extbase=attr.figpath, prefix=prefix, comname=epsname)
    
    txtname = '%s_Single_%s_fakes_Single_%s.txt' % (dt_type, fname, fnameb)
    txtfile  = tools.set_file(extbase=attr.fitpath, prefix=prefix, comname=txtname)
    
    alpha, mass, n, sigma = get_paras(dt_type, label, fnameb)
    
    par_str = '("%s", "%s", "%s", "%s", %s, %s, %s, %s)'
    par_tuple = (title, bkgfile, epsfile, txtfile, alpha, mass, n, sigma)
    par = par_str % par_tuple
    
    source = 'crossfeeds_nondiag.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, source)

    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    ROOT.gROOT.Macro(source + par)
    
    tools.eps2png(epsfile)
    tools.eps2pdf(epsfile)
Exemplo n.º 4
0
def stage_diag(dt_type, label, mode, sign, interact=False):

    if label != '281ipbv0.2':
        load_roofit_lib(dt_type, label)

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle("Plain")

    sys.stdout.write('Stage 0: Do fits for diagonal. Mode: (%s, %s) \n' %(mode, sign))

    prefix='dir_'+label+'/crossfeeds'

    if sign == 1:
        uname = modes[mode]['uname']
        fname = modes[mode]['fname']
    else:
        uname = modes[mode]['unamebar']
        fname = modes[mode]['fnamebar']

    title  = '%s correct reco' % uname

    bkgname = ('%s_Single_%s_fakes_Single_%s.evt' %(dt_type, fname, fname))
    bkgfile = get_bkgfile(bkgname, label)

    epsname = '%s_Single_%s_fakes_Single_%s.eps' % (dt_type, fname, fname)
    epsfile  = tools.set_file(extbase=attr.figpath, prefix=prefix, comname=epsname)
    
    txtname = '%s_Single_%s_fakes_Single_%s.txt' % (dt_type, fname, fname)
    txtfile  = tools.set_file(extbase=attr.fitpath, prefix=prefix, comname=txtname)

    par_str = '("%s", "%s", "%s", "%s")'
    par_tuple = (title, bkgfile, epsfile, txtfile)
    par = par_str % par_tuple
    
    source = 'crossfeeds.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, source)
        
    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()

    ROOT.gROOT.Macro(source + par)
    tools.eps2png(epsfile)
    tools.eps2pdf(epsfile)
Exemplo n.º 5
0
def create_fig_nondiag_mode(dt_type, stage, label, x, y):
    prefix='dir_'+label
    if x[1] == 1:
        uname = modes[x[0]]['uname']
        fname = modes[x[0]]['fname']
        title1 = modes[x[0]]['orgname']
    else:
        uname = modes[x[0]]['unamebar']
        fname = modes[x[0]]['fnamebar']
        title1 = modes[x[0]]['orgnamebar']

    if y[1] == 1:
        unameb = modes[y[0]]['uname']
        fnameb = modes[y[0]]['fname']
        title2 = modes[y[0]]['orgname']
    else:
        unameb = modes[y[0]]['unamebar']
        fnameb = modes[y[0]]['fnamebar']
        title2 = modes[y[0]]['orgnamebar']

    title = '%s fakes %s' % (title1, title2)

    comname = '%s_Single_%s_fakes_Single_%s' % (dt_type, fname, fnameb)

    epsfile  = tools.set_file(extbase=attr.figpath,
                              prefix=prefix, comname=comname+'.eps')

    pdffile  = epsfile.replace('.eps', '.pdf')
    
    figlabel = label.split('/')[0]
    relpath = label.replace(figlabel, '')
    pdflink = '.%s/%s.pdf' %(relpath, comname)
    pnglink = '.%s/%s.png' %(relpath, comname)

    if not os.access(pdffile, os.F_OK):
        if not os.access(epsfile, os.F_OK):
            sys.stdout.write('epsfile is not ready for %s, %s \n' % (x, y))
        else:
            sys.stdout.write('Converting %s ...' % epsfile)
            tools.eps2png(epsfile)
            tools.eps2pdf(epsfile)
            sys.stdout.write(' OK.\n')
        
    figlink = '[[%s][%s]]' %(pdflink, pnglink)
    
    txtfile  = tools.set_file(extbase=attr.fitpath,
                              prefix=prefix, comname=comname+'.txt')
    xstr = pair_to_str(x)
    ystr = pair_to_str(y)
    
    logname = 'stage_%s_%s_%s.txt' %(stage, xstr,ystr)
    logfile = tools.set_file(extbase=attr.logpath,
                              prefix=prefix, comname=logname)
    if not os.access(epsfile, os.F_OK):
        sys.stdout.write('Please check log: %s\n' % logfile)

    loglink = ''
    if os.access(logfile, os.F_OK):
        loglink = '[[../../log/%s/%s][log]]' %(label, logname)
    tablink = ''

    if os.access(txtfile, os.F_OK):
        orgtabfile = txtfile.replace('.txt', '.org')
        tab = DHadTable(txtfile)
        tab.output_org(orgtabfile)

        abspath = os.path.join(attr.base)#, attr.analysis)
        orgtabfile = orgtabfile.replace(abspath, '../..')

        tablink = '#+INCLUDE: "%s"\n' % orgtabfile

    msg = '\n* %s \n  %s \n\n%s\n %s\n' % (
        title, figlink, tablink, loglink)

    return msg
Exemplo n.º 6
0
def double_all_dps(dt_type, mode, label, test=False):
    tag = 'double'
    prefix = 'dir_'+label
    evtprefix = get_evtprefix(dt_type, label)

    if label == '818ipbv12.1':
         label = '818ipbv12'

    mode1 = 200
    mode2 = 200

    title1 = attr.modes[mode1]['uname']
    title2 = attr.modes[mode2]['unamebar']

    code1 = attr.interfacecodes[mode1]
    code2 = attr.interfacecodes[mode2]
    
    Gamma, Mres, R, mc = get_common_parameters(dt_type, label)

    Sigma1, F1a, F1b, S1a, S1b = get_resolution_paras(mode1, label)
    Sigma2, F2a, F2b, S2a, S2b = get_resolution_paras(mode2, label)

    opt, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat = get_double_paras(label)

    epsfile =  tools.set_file('eps', dt_type, mode, tag,
                              prefix=prefix, extbase=attr.figpath)
    fitpath = attr.fitpath 
    outfile =  tools.set_file('txt', dt_type, mode, tag,
                              prefix=prefix, extbase=fitpath)
    evtpath = attr.evtpath
    evtfile =  tools.set_file('evt', dt_type, mode, tag, prefix=evtprefix,
                              forceCombine=0, extbase=evtpath)

    
    if not os.access(evtfile, os.F_OK):
        forceCombine = 'all_dps'  
        evtfile =  tools.set_file('evt', dt_type, mode, tag, prefix=evtprefix,
                                  forceCombine=forceCombine, extbase=evtpath)
    tools.print_sep()

    par_str = '("%s", %f, %f, %f, %f, %f, "%s", %f, %f, %f, \
    %f, %f, "%s", "%s", "%s", %d, "%s", "%s", "%s", %f,\
    %f, %f,  %f, %f, %f, %f, %f)'

    par_tuple = (code1, Sigma1, F1a, F1b, S1a, S1b,
                 code2, Sigma2, F2a, F2b, S2a, S2b,
                 evtfile, epsfile, outfile, mc, opt, title1, title2,
                 Gamma, Mres, R, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat)
    
    par = par_str % par_tuple
        
    print_paras_double(par_tuple)

    load_roofit_lib(dt_type, label)

    sourcename = 'lineshapefit2d.C'
    if prefix == 'dir_818ipbv12.1':
         sourcename = 'lineshapefit2d_1.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, sourcename)
        
    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    if test:
        return

    ROOT.gROOT.Macro( source + par)
    
    sys.stdout.write('Save output in %s\n'  % outfile)
    tools.eps2png(epsfile)
    tools.eps2pdf(epsfile)
Exemplo n.º 7
0
def fit_backgrounds_single_mode(datatype, mode, label, test):
    comname = '%s_%s' %(datatype.replace('/', '_'), mode)
    input_label = label
    if 'nogaus' in label:
        input_label = label.split('/')[0]

    bkgpath = os.path.join(attr.bkgpath(), input_label)
    bkgfile = set_file(extbase=bkgpath, comname=comname, ext='evt')
    if not os.access(bkgfile, os.F_OK):
        input_prefix = 'dir_%s' % input_label
        bkgfile = set_file(extbase=attr.bkgpath(),
                           dt_type=datatype.replace('/', '_'),
                           tag='s', mode=mode, prefix=input_prefix,
                           comname=comname, ext='evt', forceCombine=1)

    epspath = os.path.join(attr.figpath, label)    
    epsfile = set_file(extbase=epspath, comname=comname, ext='eps')

    txtpath = os.path.join(attr.fitpath(), label)    
    txtfile = set_file(extbase=txtpath, comname=comname, ext='txt')

    if test:
        epsfile = epsfile + '.test'
        txtfile = txtfile + '.test'

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle('Plain')
    
    mbc = ROOT.RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV')
    mbc_aset = ROOT.RooArgSet(mbc)

    ebeam = ROOT.RooRealVar('ebeam','Ebeam',1.8815,1.892,'GeV')

    dflav = ROOT.RooCategory('dflav','D0 flavor')
    dflav.defineType('dflav',1)
    dflav.defineType('dbarflav',-1)

    dataset_args = ROOT.RooArgList(mbc, ebeam, dflav)

    dataset = ROOT.RooDataSet.read(bkgfile, dataset_args)


    arg_cutoff = ROOT.RooRealVar('arg_cutoff', 'Argus cutoff', 1.88, 1.89, 'GeV')
    arg_slope = ROOT.RooRealVar('arg_slope', 'Argus slope', -10, -100, -1)
    yld = ROOT.RooRealVar('yield', 'D yield', 100, 0, 200000)
    bkg = ROOT.RooRealVar('bkg', 'Background', 100, 0, 1000000)

    sigma = ROOT.RooRealVar('sigma', 'D width', 0.0001, 0.005, 'GeV')
    
    pars = [arg_cutoff, arg_slope, bkg, sigma, yld]

    mbc_d0 = ROOT.RooRealVar('mbc_d0', 'D0 Mbc', 1.8647, 'GeV')
    mbc_dp = ROOT.RooRealVar('mbc_dp', 'D+ Mbc', 1.8694, 'GeV')
    
    gauss_d0 = ROOT.RooGaussian('gauss_d0', 'D0 gaussian', mbc, mbc_d0, sigma)
    gauss_dp = ROOT.RooGaussian('gauss_dp', 'D+ gaussian', mbc, mbc_dp, sigma)

    argus = ROOT.RooArgusBG('argus', 'Argus BG', mbc, arg_cutoff, arg_slope)
    sumpdf_d0 = ROOT.RooAddPdf('sumpdf_d0', 'D0 sum pdf',
                          ROOT.RooArgList(gauss_d0, argus),
                          ROOT.RooArgList(yld, bkg))
    sumpdf_dp = ROOT.RooAddPdf('sumpdf_dp', 'Dp sum pdf',
                               ROOT.RooArgList(gauss_dp, argus),
                               ROOT.RooArgList(yld, bkg))

    Extended = ROOT.RooFit.Extended(ROOT.kTRUE)
    Save = ROOT.RooFit.Save(ROOT.kTRUE)
    Hesse = ROOT.RooFit.Hesse(ROOT.kFALSE)
    Verbose = ROOT.RooFit.Verbose(ROOT.kFALSE)

    if 'D0' in mode:
        pdf = sumpdf_d0
    else:
        pdf = sumpdf_dp


    if 'nogaus' in label:
        yld.setVal(1)
        yld.setConstant(ROOT.kTRUE)
        pars.remove(yld)

    if 'floatsigma' not in label:
        datatype = 'data'
        tag = 'single'

        if '281ipb' in label:
            prefix = 'dir_281ipbv7'
            frame_max = bkg_generic_ddbar_281_max[mode]
        elif '537ipb' in label:
            prefix = 'dir_537ipbv7'
            frame_max = bkg_generic_ddbar_537_max[mode]
        elif '818ipb' in label:
            prefix = 'dir_818ipbv7'
            frame_max = bkg_generic_ddbar_818_max[mode]
            if 'noxfeed' in label:
                frame_max = bkg_generic_ddbar_818_max_noxfeed[mode]

        else:
            raise NameError(label)
        
        tabfile = set_file('txt', datatype, mode, tag,
                           prefix=prefix,
                           extbase=attr.fitpath())

        sys.stdout.write('Using width from %s \n' %tabfile)
        tab = DHadTable(tabfile)
        sigmap1 = float(tab.cell_get('sigmap1', 'Value'))
        sigma.setVal(sigmap1)
        sigma.setConstant(ROOT.kTRUE)
        pars.remove(sigma)
        
    res = pdf.fitTo(dataset, Extended, Save, Verbose)
    res.Print('v')
    c1 = ROOT.TCanvas('c1', 'canvas1', 600, 600)
    c1.SetFixedAspectRatio(0)

    xframe = mbc.frame()
    
    xframe.SetMaximum(frame_max)
    xframe.SetMarkerSize(0.3)
    dataset.plotOn(xframe)
    pdf.plotOn(xframe)
    nfitParam = len(pars)
    chisq_ndof = xframe.chiSquare(nfitParam)

    nbin = xframe.GetNbinsX()
    ndof = nbin - nfitParam
    chisq = chisq_ndof * ndof
    
    chisqbox = ROOT.TPaveText(0.1, 0.1, 0.4, 0.15, 'BRNDC')
    chisqbox.SetFillColor(0)
    chisqbox.SetBorderSize(1)
    chisqbox.AddText('#chi^{2}/ndof = %.3f/%d =  %.3f' %
                     (chisq, ndof, chisq_ndof))
    xframe.addObject(chisqbox)

    pdf.paramOn(xframe, dataset)        

    xframe.SetTitle('Generic faking %s' % mode)
    xframe.Draw()

    c1.Print(epsfile)
    tools.save_fit_result(pars, txtfile)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 8
0
def fit_single_mode(datatype, mode, label, lowmass, highmass, test):
    evtpath = os.path.join(attr.datpath, 'sel', label, 'kpimass')
    modekey = tools.get_modekey(mode)
    tag = 'single'
    evtfile = tools.set_file('evt',
                             datatype,
                             modekey,
                             tag,
                             prefix='',
                             forceCombine=1,
                             extbase=evtpath)

    load_roofit_lib(datatype, label)
    cuts = '%f<kpimass && kpimass<%f' % (lowmass, highmass)
    err_type = 'ASYM'
    setGamma, setMres, setR, mc = get_common_parameters(datatype, label)
    Sigma, Fa, Fb, Sa, Sb = get_resolution_paras(modekey, label)

    N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1, xi = init_paras_single(
        label, datatype, 's', modekey)

    title1 = '%s : %s < Kpi mass < %s ' % (attr.modes[modekey]['uname'],
                                           lowmass, highmass)
    title2 = '%s : %s < Kpi mass < %s ' % (attr.modes[modekey]['unamebar'],
                                           lowmass, highmass)
    prefix = 'dir_%s/kpimass/%s_%s' % (label, lowmass, highmass)
    epsfile = tools.set_file('eps',
                             datatype,
                             modekey,
                             tag,
                             prefix=prefix,
                             extbase=attr.figpath)
    txtfile = tools.set_file('txt',
                             datatype,
                             modekey,
                             tag,
                             prefix=prefix,
                             extbase=attr.fitpath)

    mbc_single_3s(evtfile,
                  mc,
                  setMres,
                  setGamma,
                  setR,
                  sigmap1,
                  Sa,
                  Sb,
                  Fa,
                  Fb,
                  md,
                  p,
                  xi,
                  N1,
                  N2,
                  Nbkgd1,
                  Nbkgd2,
                  title1,
                  title2,
                  epsfile,
                  txtfile,
                  cuts=cuts,
                  err_type=err_type,
                  test=test)

    #mbc_gau_che(evtfile, mc, setMres, setGamma, setR, sigmap1, Sa, Sb, Fa,
    #            Fb, md, p, xi, N1, N2, Nbkgd1, Nbkgd2, title1, title2,
    #            epsfile, txtfile, cuts=cuts, err_type=err_type, test=test)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 9
0
def fit_backgrounds_single(datatype, modes, label, test):
    epsname = '%s_background' % (datatype.replace('/', '_'))
    epspath = os.path.join(attr.figpath, label)
    epsfile = set_file(extbase=epspath, comname=epsname, ext='eps')

    if test:
        epsfile = epsfile + '.test'

    input_label = label

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle('Plain')

    mbc = ROOT.RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV')
    mbc_aset = ROOT.RooArgSet(mbc)

    ebeam = ROOT.RooRealVar('ebeam', 'Ebeam', 1.8815, 1.892, 'GeV')

    dflav = ROOT.RooCategory('dflav', 'D0 flavor')
    dflav.defineType('dflav', 1)
    dflav.defineType('dbarflav', -1)

    dataset_args = ROOT.RooArgList(mbc, ebeam, dflav)

    arg_cutoff = ROOT.RooRealVar('arg_cutoff', 'Argus cutoff', 1.88, 1.89,
                                 'GeV')
    arg_slope = ROOT.RooRealVar('arg_slope', 'Argus slope', -10, -100, -1)
    yld = ROOT.RooRealVar('yield', 'D yield', 100, 0, 200000)
    bkg = ROOT.RooRealVar('bkg', 'Background', 100, 0, 1000000)

    sigma = ROOT.RooRealVar('sigma', 'D width', 0.0001, 0.005, 'GeV')

    mbc_d0 = ROOT.RooRealVar('mbc_d0', 'D0 Mbc', 1.8647, 'GeV')
    mbc_dp = ROOT.RooRealVar('mbc_dp', 'D+ Mbc', 1.8694, 'GeV')

    gauss_d0 = ROOT.RooGaussian('gauss_d0', 'D0 gaussian', mbc, mbc_d0, sigma)
    gauss_dp = ROOT.RooGaussian('gauss_dp', 'D+ gaussian', mbc, mbc_dp, sigma)

    argus = ROOT.RooArgusBG('argus', 'Argus BG', mbc, arg_cutoff, arg_slope)
    sumpdf_d0 = ROOT.RooAddPdf('sumpdf_d0', 'D0 sum pdf',
                               ROOT.RooArgList(gauss_d0, argus),
                               ROOT.RooArgList(yld, bkg))
    sumpdf_dp = ROOT.RooAddPdf('sumpdf_dp', 'Dp sum pdf',
                               ROOT.RooArgList(gauss_dp, argus),
                               ROOT.RooArgList(yld, bkg))

    Extended = ROOT.RooFit.Extended(ROOT.kTRUE)
    Save = ROOT.RooFit.Save(ROOT.kTRUE)
    Hesse = ROOT.RooFit.Hesse(ROOT.kFALSE)
    Verbose = ROOT.RooFit.Verbose(ROOT.kFALSE)

    if '281ipb' in label:
        prefix = 'dir_281ipbv7'
    elif '537ipb' in label:
        prefix = 'dir_537ipbv7'
    elif '818ipb' in label:
        prefix = 'dir_818ipbv7'
    else:
        raise NameError(label)

    c1 = ROOT.TCanvas('c1', 'canvas1', 800, 1000)
    #c1.SetFixedAspectRatio(0)
    c1.Divide(3, 3)
    pad = 1
    for mode in modes:
        mode_root_name = get_mode_root_name(mode)
        if datatype == 'generic/ddbar':
            frametitle = 'Generic faking %s' % mode_root_name
        elif datatype == 'generic/cont':
            frametitle = 'Continuum production faking %s' % mode_root_name
        elif datatype == 'generic/tau':
            frametitle = '#tau #tau production faking %s' % mode_root_name
        elif datatype == 'generic/radret':
            frametitle = 'Radiative return faking %s' % mode_root_name
        else:
            raise NameError(datatype)

        c1.cd(pad)
        pad += 1
        comname = '%s_%s' % (datatype.replace('/', '_'), mode)
        bkgpath = os.path.join(attr.bkgpath(), input_label)
        bkgfile = set_file(extbase=bkgpath, comname=comname, ext='evt')
        if not os.access(bkgfile, os.F_OK):
            input_prefix = 'dir_%s' % input_label
            bkgfile = set_file(extbase=attr.bkgpath(),
                               dt_type=datatype.replace('/', '_'),
                               tag='s',
                               mode=mode,
                               prefix=input_prefix,
                               comname=comname,
                               ext='evt',
                               forceCombine=1)

        dataset = ROOT.RooDataSet.read(bkgfile, dataset_args)

        if 'D0' in mode:
            pdf = sumpdf_d0
        else:
            pdf = sumpdf_dp

        if 'floatsigma' not in label:
            tab_datatype = 'data'
            tag = 'single'

        tabfile = set_file('txt',
                           tab_datatype,
                           mode,
                           tag,
                           prefix=prefix,
                           extbase=attr.fitpath())

        sys.stdout.write('Using width from %s \n' % tabfile)
        tab = DHadTable(tabfile)
        sigmap1 = float(tab.cell_get('sigmap1', 'Value'))
        sigma.setVal(sigmap1)
        sigma.setConstant(ROOT.kTRUE)

        res = pdf.fitTo(dataset, Extended, Save, Verbose)
        res.Print('v')

        xframe = mbc.frame()
        xframe.SetMarkerSize(0.3)
        if '818ipb' in label and 'noxfeed' in label:
            frame_max = bkg_generic_ddbar_818_max_noxfeed[mode]
            xframe.SetMaximum(frame_max)

        dataset.plotOn(xframe)
        pdf.plotOn(xframe)

        pdf.paramOn(xframe, dataset)

        xframe.SetTitle(frametitle)
        xframe.Draw()

    c1.Print(epsfile)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 10
0
def fit_backgrounds_single_mode(datatype, mode, label, test):
    comname = '%s_%s' % (datatype.replace('/', '_'), mode)
    input_label = label
    if 'nogaus' in label:
        input_label = label.split('/')[0]

    bkgpath = os.path.join(attr.bkgpath(), input_label)
    bkgfile = set_file(extbase=bkgpath, comname=comname, ext='evt')
    if not os.access(bkgfile, os.F_OK):
        input_prefix = 'dir_%s' % input_label
        bkgfile = set_file(extbase=attr.bkgpath(),
                           dt_type=datatype.replace('/', '_'),
                           tag='s',
                           mode=mode,
                           prefix=input_prefix,
                           comname=comname,
                           ext='evt',
                           forceCombine=1)

    epspath = os.path.join(attr.figpath, label)
    epsfile = set_file(extbase=epspath, comname=comname, ext='eps')

    txtpath = os.path.join(attr.fitpath(), label)
    txtfile = set_file(extbase=txtpath, comname=comname, ext='txt')

    if test:
        epsfile = epsfile + '.test'
        txtfile = txtfile + '.test'

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle('Plain')

    mbc = ROOT.RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV')
    mbc_aset = ROOT.RooArgSet(mbc)

    ebeam = ROOT.RooRealVar('ebeam', 'Ebeam', 1.8815, 1.892, 'GeV')

    dflav = ROOT.RooCategory('dflav', 'D0 flavor')
    dflav.defineType('dflav', 1)
    dflav.defineType('dbarflav', -1)

    dataset_args = ROOT.RooArgList(mbc, ebeam, dflav)

    dataset = ROOT.RooDataSet.read(bkgfile, dataset_args)

    arg_cutoff = ROOT.RooRealVar('arg_cutoff', 'Argus cutoff', 1.88, 1.89,
                                 'GeV')
    arg_slope = ROOT.RooRealVar('arg_slope', 'Argus slope', -10, -100, -1)
    yld = ROOT.RooRealVar('yield', 'D yield', 100, 0, 200000)
    bkg = ROOT.RooRealVar('bkg', 'Background', 100, 0, 1000000)

    sigma = ROOT.RooRealVar('sigma', 'D width', 0.0001, 0.005, 'GeV')

    pars = [arg_cutoff, arg_slope, bkg, sigma, yld]

    mbc_d0 = ROOT.RooRealVar('mbc_d0', 'D0 Mbc', 1.8647, 'GeV')
    mbc_dp = ROOT.RooRealVar('mbc_dp', 'D+ Mbc', 1.8694, 'GeV')

    gauss_d0 = ROOT.RooGaussian('gauss_d0', 'D0 gaussian', mbc, mbc_d0, sigma)
    gauss_dp = ROOT.RooGaussian('gauss_dp', 'D+ gaussian', mbc, mbc_dp, sigma)

    argus = ROOT.RooArgusBG('argus', 'Argus BG', mbc, arg_cutoff, arg_slope)
    sumpdf_d0 = ROOT.RooAddPdf('sumpdf_d0', 'D0 sum pdf',
                               ROOT.RooArgList(gauss_d0, argus),
                               ROOT.RooArgList(yld, bkg))
    sumpdf_dp = ROOT.RooAddPdf('sumpdf_dp', 'Dp sum pdf',
                               ROOT.RooArgList(gauss_dp, argus),
                               ROOT.RooArgList(yld, bkg))

    Extended = ROOT.RooFit.Extended(ROOT.kTRUE)
    Save = ROOT.RooFit.Save(ROOT.kTRUE)
    Hesse = ROOT.RooFit.Hesse(ROOT.kFALSE)
    Verbose = ROOT.RooFit.Verbose(ROOT.kFALSE)

    if 'D0' in mode:
        pdf = sumpdf_d0
    else:
        pdf = sumpdf_dp

    if 'nogaus' in label:
        yld.setVal(1)
        yld.setConstant(ROOT.kTRUE)
        pars.remove(yld)

    if 'floatsigma' not in label:
        datatype = 'data'
        tag = 'single'

        if '281ipb' in label:
            prefix = 'dir_281ipbv7'
            frame_max = bkg_generic_ddbar_281_max[mode]
        elif '537ipb' in label:
            prefix = 'dir_537ipbv7'
            frame_max = bkg_generic_ddbar_537_max[mode]
        elif '818ipb' in label:
            prefix = 'dir_818ipbv7'
            frame_max = bkg_generic_ddbar_818_max[mode]
            if 'noxfeed' in label:
                frame_max = bkg_generic_ddbar_818_max_noxfeed[mode]

        else:
            raise NameError(label)

        tabfile = set_file('txt',
                           datatype,
                           mode,
                           tag,
                           prefix=prefix,
                           extbase=attr.fitpath())

        sys.stdout.write('Using width from %s \n' % tabfile)
        tab = DHadTable(tabfile)
        sigmap1 = float(tab.cell_get('sigmap1', 'Value'))
        sigma.setVal(sigmap1)
        sigma.setConstant(ROOT.kTRUE)
        pars.remove(sigma)

    res = pdf.fitTo(dataset, Extended, Save, Verbose)
    res.Print('v')
    c1 = ROOT.TCanvas('c1', 'canvas1', 600, 600)
    c1.SetFixedAspectRatio(0)

    xframe = mbc.frame()

    xframe.SetMaximum(frame_max)
    xframe.SetMarkerSize(0.3)
    dataset.plotOn(xframe)
    pdf.plotOn(xframe)
    nfitParam = len(pars)
    chisq_ndof = xframe.chiSquare(nfitParam)

    nbin = xframe.GetNbinsX()
    ndof = nbin - nfitParam
    chisq = chisq_ndof * ndof

    chisqbox = ROOT.TPaveText(0.1, 0.1, 0.4, 0.15, 'BRNDC')
    chisqbox.SetFillColor(0)
    chisqbox.SetBorderSize(1)
    chisqbox.AddText('#chi^{2}/ndof = %.3f/%d =  %.3f' %
                     (chisq, ndof, chisq_ndof))
    xframe.addObject(chisqbox)

    pdf.paramOn(xframe, dataset)

    xframe.SetTitle('Generic faking %s' % mode)
    xframe.Draw()

    c1.Print(epsfile)
    tools.save_fit_result(pars, txtfile)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 11
0
def create_fig_diag_mode(dt_type, stage, label, mode, sign):

    prefix = 'dir_' + label

    if sign == 1:
        uname = modes[mode]['uname']
        fname = modes[mode]['fname']
        title = modes[mode]['orgname']
    else:
        uname = modes[mode]['unamebar']
        fname = modes[mode]['fnamebar']
        title = modes[mode]['orgnamebar']

    comname = '%s_Single_%s_fakes_Single_%s' % (dt_type, fname, fname)

    epsfile = tools.set_file(extbase=attr.figpath,
                             prefix=prefix,
                             comname=comname + '.eps')

    pdffile = epsfile.replace('.eps', '.pdf')

    figlabel = label.split('/')[0]
    relpath = label.replace(figlabel, '')

    pdflink = '.%s/%s.pdf' % (relpath, comname)
    pnglink = '.%s/%s.png' % (relpath, comname)

    if not os.access(pdffile, os.F_OK):
        if not os.access(epsfile, os.F_OK):
            sys.stdout.write('epsfile is not ready for (%s %s) \n' %
                             (mode, sign))
        else:
            tools.eps2png(epsfile)
            tools.eps2pdf(epsfile)

    figlink = '[[%s][%s]]' % (pdflink, pnglink)

    txtfile = tools.set_file(extbase=attr.fitpath,
                             prefix=prefix,
                             comname=comname + '.txt')

    logname = 'stage_%s_%s_%s.txt' % (stage, mode, sign)
    logfile = tools.set_file(extbase=attr.logpath,
                             prefix=prefix,
                             comname=logname)
    loglink = ''
    if os.access(logfile, os.F_OK):
        loglink = '[[../../log/%s/%s][log]]' % (label, logname)

    tablink = ''
    if os.access(txtfile, os.F_OK):
        orgtabfile = txtfile.replace('.txt', '.org')
        tab = DHadTable(txtfile)
        tab.output_org(orgtabfile)

        abspath = os.path.join(attr.base)  #, attr.analysis)
        orgtabfile = orgtabfile.replace(abspath, '../..')

        tablink = '#+INCLUDE: "%s"\n' % orgtabfile

    msg = '\n* %s \n  %s \n\n%s\n %s\n' % (title, figlink, tablink, loglink)

    return msg
Exemplo n.º 12
0
def create_fig_nondiag_mode(dt_type, stage, label, x, y):
    prefix = 'dir_' + label
    if x[1] == 1:
        uname = modes[x[0]]['uname']
        fname = modes[x[0]]['fname']
        title1 = modes[x[0]]['orgname']
    else:
        uname = modes[x[0]]['unamebar']
        fname = modes[x[0]]['fnamebar']
        title1 = modes[x[0]]['orgnamebar']

    if y[1] == 1:
        unameb = modes[y[0]]['uname']
        fnameb = modes[y[0]]['fname']
        title2 = modes[y[0]]['orgname']
    else:
        unameb = modes[y[0]]['unamebar']
        fnameb = modes[y[0]]['fnamebar']
        title2 = modes[y[0]]['orgnamebar']

    title = '%s fakes %s' % (title1, title2)

    comname = '%s_Single_%s_fakes_Single_%s' % (dt_type, fname, fnameb)

    epsfile = tools.set_file(extbase=attr.figpath,
                             prefix=prefix,
                             comname=comname + '.eps')

    pdffile = epsfile.replace('.eps', '.pdf')

    figlabel = label.split('/')[0]
    relpath = label.replace(figlabel, '')
    pdflink = '.%s/%s.pdf' % (relpath, comname)
    pnglink = '.%s/%s.png' % (relpath, comname)

    if not os.access(pdffile, os.F_OK):
        if not os.access(epsfile, os.F_OK):
            sys.stdout.write('epsfile is not ready for %s, %s \n' % (x, y))
        else:
            sys.stdout.write('Converting %s ...' % epsfile)
            tools.eps2png(epsfile)
            tools.eps2pdf(epsfile)
            sys.stdout.write(' OK.\n')

    figlink = '[[%s][%s]]' % (pdflink, pnglink)

    txtfile = tools.set_file(extbase=attr.fitpath,
                             prefix=prefix,
                             comname=comname + '.txt')
    xstr = pair_to_str(x)
    ystr = pair_to_str(y)

    logname = 'stage_%s_%s_%s.txt' % (stage, xstr, ystr)
    logfile = tools.set_file(extbase=attr.logpath,
                             prefix=prefix,
                             comname=logname)
    if not os.access(epsfile, os.F_OK):
        sys.stdout.write('Please check log: %s\n' % logfile)

    loglink = ''
    if os.access(logfile, os.F_OK):
        loglink = '[[../../log/%s/%s][log]]' % (label, logname)
    tablink = ''

    if os.access(txtfile, os.F_OK):
        orgtabfile = txtfile.replace('.txt', '.org')
        tab = DHadTable(txtfile)
        tab.output_org(orgtabfile)

        abspath = os.path.join(attr.base)  #, attr.analysis)
        orgtabfile = orgtabfile.replace(abspath, '../..')

        tablink = '#+INCLUDE: "%s"\n' % orgtabfile

    msg = '\n* %s \n  %s \n\n%s\n %s\n' % (title, figlink, tablink, loglink)

    return msg
Exemplo n.º 13
0
def double_tag_mode(dt_type, mode, label, interact=False, test=False):
    mode = get_modekey(mode)
    tag = 'double'
    prefix = 'dir_'+label
    evtprefix = get_evtprefix(dt_type, label)

    mode1 = mode[0]
    mode2 = mode[1]

    title1 = attr.modes[mode1]['uname']
    title2 = attr.modes[mode2]['unamebar']

    code1 = attr.interfacecodes[mode1]
    code2 = attr.interfacecodes[mode2]
    
    forceCombine = None # Different from single tag case

    Gamma, Mres, R, mc = get_common_parameters(dt_type, label)

    Sigma1, F1a, F1b, S1a, S1b = get_resolution_paras(mode1, label)
    Sigma2, F2a, F2b, S2a, S2b = get_resolution_paras(mode2, label)

    opt, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat = get_double_paras(label)
    
    epsfile =  tools.set_file('eps', dt_type, mode, tag,
                              prefix=prefix, extbase=attr.figpath)
    fitpath = attr.fitpath 
    outfile =  tools.set_file('txt', dt_type, mode, tag,
                              prefix=prefix, extbase=fitpath)
    evtpath = attr.evtpath
    evtfile =  tools.set_file('evt', dt_type, mode, tag, prefix=evtprefix,
                              forceCombine=forceCombine, extbase=evtpath)
    if not os.access(evtfile, os.F_OK):
        if '818ipb' in evtprefix:
            evtfile_281ipb = evtfile.replace('818ipb', '281ipb')
            evtfile_537ipb = evtfile.replace('818ipb', '537ipb')
            combine_files(evtfile_281ipb, evtfile_537ipb, evtfile)
        else:
            raise ValueError(evtfile)

    tools.print_sep()

    par_str = '("%s", %f, %f, %f, %f, %f, "%s", %f, %f, %f, \
    %f, %f, "%s", "%s", "%s", %d, "%s", "%s", "%s", %f,\
    %f, %f,  %f, %f, %f, %f, %f)'

    par_tuple = (code1, Sigma1, F1a, F1b, S1a, S1b,
                 code2, Sigma2, F2a, F2b, S2a, S2b,
                 evtfile, epsfile, outfile, mc, opt, title1, title2,
                 Gamma, Mres, R, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat)
    
    par = par_str % par_tuple
        
    print_paras_double(par_tuple)

    load_roofit_lib(dt_type, label)

    sourcename = 'lineshapefit2d.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, sourcename)
        
    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    if test:
        return

    ROOT.gROOT.Macro( source + par)
    
    sys.stdout.write('Save output in %s\n'  % outfile)
    tools.eps2png(epsfile)
    tools.eps2pdf(epsfile)
Exemplo n.º 14
0
def create_fig_diag_mode(dt_type, stage, label, mode, sign):

    prefix='dir_'+label

    if sign == 1:
        uname = modes[mode]['uname']
        fname = modes[mode]['fname']
        title = modes[mode]['orgname']
    else:
        uname = modes[mode]['unamebar']
        fname = modes[mode]['fnamebar']
        title = modes[mode]['orgnamebar']

    comname = '%s_Single_%s_fakes_Single_%s' % (dt_type, fname, fname)

    epsfile  = tools.set_file(extbase=attr.figpath,
                              prefix=prefix, comname=comname+'.eps')

    pdffile  = epsfile.replace('.eps', '.pdf')

    figlabel = label.split('/')[0]
    relpath = label.replace(figlabel, '')

    pdflink = '.%s/%s.pdf' %(relpath, comname)
    pnglink = '.%s/%s.png' %(relpath, comname)

    if not os.access(pdffile, os.F_OK):
        if not os.access(epsfile, os.F_OK):
            sys.stdout.write('epsfile is not ready for (%s %s) \n' % (mode, sign))
        else:
            tools.eps2png(epsfile)
            tools.eps2pdf(epsfile)


    figlink = '[[%s][%s]]' %(pdflink, pnglink)
    
    txtfile  = tools.set_file(extbase=attr.fitpath,
                              prefix=prefix, comname=comname+'.txt')


    logname = 'stage_%s_%s_%s.txt' %(stage, mode, sign)
    logfile = tools.set_file(extbase=attr.logpath,
                              prefix=prefix, comname=logname)
    loglink = ''
    if os.access(logfile, os.F_OK):
        loglink = '[[../../log/%s/%s][log]]' %(label, logname)

    tablink = ''
    if os.access(txtfile, os.F_OK):
        orgtabfile = txtfile.replace('.txt', '.org')
        tab = DHadTable(txtfile)
        tab.output_org(orgtabfile)

        abspath = os.path.join(attr.base)#, attr.analysis)
        orgtabfile = orgtabfile.replace(abspath, '../..')

        tablink = '#+INCLUDE: "%s"\n' % orgtabfile

    msg = '\n* %s \n  %s \n\n%s\n %s\n' % (
        title, figlink, tablink, loglink)

    return msg
Exemplo n.º 15
0
def single_tag_mode(dt_type, mode, label, interact=False, test=False):
    mode = get_modekey(mode)
    tag = 'single'
    prefix = 'dir_' + label
    evtprefix = get_evtprefix(dt_type, label)
    Gamma, Mres, R, mc = get_common_parameters(dt_type, label)
    Sigma, Fa, Fb, Sa, Sb = get_resolution_paras(mode, label)
    N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1, xi = init_paras_single(
        label, dt_type, tag, mode)
    # ------------------- Parameters for the fit ----------
    title1 = attr.modes[mode]['uname']
    title2 = attr.modes[mode]['unamebar']
    num_fcn = 3  # : 0 for only background
    xi_side = 0.0
    p_side = 0.0
    int_d = 1
    optstr = 'p'  # To plot,  f : fast, w: fixwidth
    floatwidth = 0
    Min = 0.5
    options = ""
    MINUIT = "ermh4"
    # -------------------------------------------------

    if '/p/0.5' in label:
        p_side = 0.5

    if '/argus' in label:
        options = 'fix_xi,fix_p'
        xi, p = get_argus_paras_single(label, dt_type, tag, mode)

    if '/fix_sigmap1' in label:
        options = 'fix_sigmap1'
        if '/kssideband' in label:
            sys.stdout.write('Fixing the sigmap1 as %s ...\n' % Sigma)
            sigmap1 = Sigma

    if '/fix_n1n2' in label and '/desideband' in label:
        options = 'fix_n1n2'
        N1 = 1
        N2 = 1

    if dt_type == 'signal' and mode in [0, 200, 202]:
        p_side = 0.5
        tools.print_sep()

    forceCombine = 1
    epsfile = tools.set_file('eps',
                             dt_type,
                             mode,
                             tag,
                             prefix=prefix,
                             extbase=attr.figpath)
    fitpath = attr.fitpath
    outfile = tools.set_file('txt',
                             dt_type,
                             mode,
                             tag,
                             prefix=prefix,
                             extbase=fitpath)
    evtpath = attr.evtpath
    evtfile = tools.set_file('evt',
                             dt_type,
                             mode,
                             tag,
                             prefix=evtprefix,
                             forceCombine=forceCombine,
                             extbase=evtpath)
    tools.print_sep()
    par_str = '( "%s", "%s", "%s", "%s", "%s", %d, %d, %f, %f, %f, %f, \
    %f, %f, %f, %d, "%s", %f, %d, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
    %f, "%s", "%s" )'

    par_tuple = (title1, title2, evtfile, epsfile, outfile, mc, num_fcn,
                 xi_side, p_side, Sigma, Sa, Sb, Fa, Fb, int_d, optstr, Gamma,
                 floatwidth, R, Mres, N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1,
                 xi, Min, options, MINUIT)
    par = par_str % par_tuple
    print_paras(par_tuple)
    load_roofit_lib(dt_type, label)
    sourcename = 'mbc_singletag_3s.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, sourcename)
    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    if test:
        return
    ROOT.gROOT.Macro(source + par)

    if test:
        evtfile = evtfile + '.test'
        epsfile = epsfile + '.test'
        outfile = outfile + '.test'

    # fit_mbc_single(title1, title2, evtfile, epsfile, outfile, mc, num_fcn,
    #                xi_side, p_side, Sigma, Sa, Sb, Fa, Fb, int_d, optstr,
    #                Gamma, floatwidth, R, Mres, N1, N2, Nbkgd1, Nbkgd2, md,
    #                p, sigmap1, xi, Min, options, MINUIT)

    sys.stdout.write('Save output in %s\n' % outfile)
    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 16
0
def fit_sidebands_single_mode(datatype, mode, label, test):
    load_roofit_lib(datatype, label)

    dt_type=datatype.replace('/', '_')
    modekey = tools.get_modekey(mode)
    
    comname = '%s_%s' %(dt_type, mode)
    input_label = label

    sdbpath = os.path.join(attr.evtpath, input_label)
    sdbfile = set_file(extbase=sdbpath, comname=comname, ext='evt')

    has_charge_conjugate = False
    if '__' in mode:
        has_charge_conjugate = True
    
    if not os.access(sdbfile, os.F_OK):
        if has_charge_conjugate:
            sdbfile = set_file(extbase=sdbpath, dt_type=dt_type,
                               tag='s', mode=modekey, 
                               comname=comname, ext='evt', forceCombine=1)
        else:
            raise ValueError(sdbfile)
        
    epspath = os.path.join(attr.figpath, label)    
    epsfile = set_file(extbase=epspath, comname=comname, ext='eps')

    txtpath = os.path.join(attr.fitpath, label)    
    txtfile = set_file(extbase=txtpath, comname=comname, ext='txt')

    if test:
        epsfile = epsfile + '.test'
        txtfile = txtfile + '.test'

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle('Plain')

    mbc = ROOT.RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV')
    mbc_aset = ROOT.RooArgSet(mbc)

    ebeam = ROOT.RooRealVar('ebeam', 'Ebeam', 1.8815, 1.892, 'GeV')

    dflav = ROOT.RooCategory('dflav','D0 flavor')

    dflav.defineType('dflav',1)
    dflav.defineType('dbarflav',-1)

    dataset_args = ROOT.RooArgList(mbc, ebeam, dflav)

    dataset = ROOT.RooDataSet.read(sdbfile, dataset_args)

    arg_cutoff = ebeam
    arg_slope = ROOT.RooRealVar('arg_slope', 'Argus slope', -10, -100, -1)
    arg_power = ROOT.RooRealVar('arg_power', 'Argus power', 0.5, 0.1, 1.5)
    
    yld = ROOT.RooRealVar('yield', 'D yield', 100, 0, 200000)
    bkg = ROOT.RooRealVar('bkg', 'Background', 100, 0, 1000000)

    sigma = ROOT.RooRealVar('sigma', 'D width', 0.0001, 0.005, 'GeV')

    pars = [arg_slope, arg_power, bkg, sigma, yld]
    
    mbc_d0 = ROOT.RooRealVar('mbc_d0', 'D0 Mbc', 1.8647, 'GeV')
    mbc_dp = ROOT.RooRealVar('mbc_dp', 'D+ Mbc', 1.8694, 'GeV')
    
    gauss_d0 = ROOT.RooGaussian('gauss_d0', 'D0 gaussian', mbc, mbc_d0, sigma)
    gauss_dp = ROOT.RooGaussian('gauss_dp', 'D+ gaussian', mbc, mbc_dp, sigma)

    argus = ROOT.RooArgusBG('argus', 'Argus BG', mbc, arg_cutoff, arg_slope,
                            arg_power)
    sumpdf_d0 = ROOT.RooAddPdf('sumpdf_d0', 'D0 sum pdf',
                          ROOT.RooArgList(gauss_d0, argus),
                          ROOT.RooArgList(yld, bkg))
    sumpdf_dp = ROOT.RooAddPdf('sumpdf_dp', 'Dp sum pdf',
                               ROOT.RooArgList(gauss_dp, argus),
                               ROOT.RooArgList(yld, bkg))


    Extended = ROOT.RooFit.Extended(ROOT.kTRUE)  # e
    Save = ROOT.RooFit.Save(ROOT.kTRUE)          # r
    Hesse = ROOT.RooFit.Hesse(ROOT.kFALSE)       # no h
    Verbose = ROOT.RooFit.Verbose(ROOT.kFALSE)   # no q 

    if 'D0' in mode:
        pdf = sumpdf_d0
    else:
        pdf = sumpdf_dp


    if has_charge_conjugate:
        yld_bar = ROOT.RooRealVar('yield_bar', 'Dbar yield', 100, 0, 200000)
        bkg_bar = ROOT.RooRealVar('bkg_bar', 'Background Bar', 100, 0, 1000000)
        pars.extend([yld_bar, bkg_bar])

        mbc_d0bar = ROOT.RooRealVar('mbc_d0bar', 'D0Bar Mbc', 1.8647, 'GeV')
        mbc_dm = ROOT.RooRealVar('mbc_dm', 'D- Mbc', 1.8694, 'GeV')
        
        gauss_d0bar = ROOT.RooGaussian('gauss_d0bar', 'D0bar gaussian',
                                       mbc, mbc_d0bar, sigma)
        gauss_dm = ROOT.RooGaussian('gauss_dm', 'D- gaussian',
                                    mbc, mbc_dm, sigma)
        
        sumpdf_d0bar = ROOT.RooAddPdf('sumpdf_d0bar', 'D0bar sum pdf',
                                      ROOT.RooArgList(gauss_d0bar, argus),
                                      ROOT.RooArgList(yld_bar, bkg_bar))
        sumpdf_dm = ROOT.RooAddPdf('sumpdf_dm', 'Dm sum pdf',
                                   ROOT.RooArgList(gauss_dm, argus),
                                   ROOT.RooArgList(yld_bar, bkg_bar))

        pdf = ROOT.RooSimultaneous('totalPdf', 'totalPdf', dflav)

        if 'D0' in mode:
            pdf.addPdf(sumpdf_d0, 'dflav')
            pdf.addPdf(sumpdf_d0bar, 'dbarflav')
        else:
            pdf.addPdf(sumpdf_dp, 'dflav')
            pdf.addPdf(sumpdf_dm, 'dbarflav')
            

    if 'nogaus' in label:
        yld.setVal(1)
        yld.setConstant(ROOT.kTRUE)
        pars.remove(yld)

    if 'floatsigma' not in label:
        datatype = 'data'
        tag = 'single'

        if '818ipb' in label:
            prefix = 'dir_818ipbv12'
            frame_max = 1500 #bkg_generic_ddbar_818_max[mode]
        else:
            raise NameError(label)


        tabfile = set_file('txt', datatype, modekey, tag,
                           prefix=prefix,
                           extbase=attr.fitpath)

        sys.stdout.write('Using width from %s \n' %tabfile)
        tab = DHadTable(tabfile)
        sigmap1 = float(tab.cell_get('sigmap1', 'Value'))
        sigma.setVal(sigmap1)
        sigma.setConstant(ROOT.kTRUE)
        pars.remove(sigma)
        
    #res = pdf.fitTo(dataset, Extended, Save, Verbose)
    res = pdf.fitTo(dataset, 'emr') #Migrad only, no MINOS
    res.Print('v')


    if not has_charge_conjugate:
        canvas = ROOT.TCanvas('canvas', 'canvas1', 600, 600)
        canvas.SetFixedAspectRatio(0)
        
        xframe = mbc.frame()
        
        xframe.SetMaximum(frame_max)
        xframe.SetMarkerSize(0.3)
        dataset.plotOn(xframe)
        pdf.plotOn(xframe)
        nfitParam = len(pars)
        chisq_ndof = xframe.chiSquare(nfitParam)
        
        nbin = xframe.GetNbinsX()
        ndof = nbin - nfitParam
        chisq = chisq_ndof * ndof
    
        chisqbox = ROOT.TPaveText(0.1, 0.1, 0.4, 0.15, 'BRNDC')
        chisqbox.SetFillColor(0)
        chisqbox.SetBorderSize(1)
        chisqbox.AddText('#chi^{2}/ndof = %.2f/%d =  %.2f' %
                         (chisq, ndof, chisq_ndof))
        xframe.addObject(chisqbox)

        pdf.paramOn(xframe, dataset)        

        xframe.SetTitle('Sidebands of %s' % mode)
        xframe.GetYaxis().SetTitleSize(0.03) 
        xframe.GetYaxis().SetLabelSize(0.03) 
        xframe.GetYaxis().SetTitleOffset(1.8)
        xframe.GetXaxis().SetTitleSize(0.03) 
        xframe.GetXaxis().SetLabelSize(0.03) 
        xframe.GetXaxis().SetTitleOffset(1.2)
        xframe.Draw()
    else:
        canvas = ROOT.TCanvas('canvas','mbc', 1200, 400);
        canvas.Divide(3,1)
        title1 = attr.modes[modekey]['uname']
        title2 = attr.modes[modekey]['unamebar']
        
        canvas_1 = canvas.GetListOfPrimitives().FindObject('canvas_1')
        canvas_2 = canvas.GetListOfPrimitives().FindObject('canvas_2')
        canvas_1.SetLogy() 
        canvas_2.SetLogy()
        
        ebeam.setBins(900)

        canvas.cd(1)
        mbcFrame=mbc.frame()
        mbcFrame=mbc.frame(60)
        dflav.setLabel('dflav')
        ebeam_aset = ROOT.RooArgSet(ebeam, dflav)
        ebeamdata = ROOT.RooDataHist("ebeamdata", "ebeamdata", 
                                     ebeam_aset, dataset)

        Cut = ROOT.RooFit.Cut("dflav==dflav::dflav")

        LineColor = ROOT.RooFit.LineColor(ROOT.kRed)
        LineWidth = ROOT.RooFit.LineWidth(1)
        #Slice = ROOT.RooFit.Slice(dflav, 'dflav')
        Slice = ROOT.RooFit.Slice(dflav)

        ProjWData = ROOT.RooFit.ProjWData(ebeam_aset, ebeamdata)
        
        dataset.plotOn(mbcFrame, Cut)
        mbcFrame.getAttMarker().SetMarkerSize(0.6)
        mbcFrame.Draw()


        pdf.plotOn(mbcFrame, LineColor, LineWidth, Slice, ProjWData)

        nfitParam = len(pars) - 2
        chisq_ndof = mbcFrame.chiSquare(nfitParam)
        
        nbin = mbcFrame.GetNbinsX()
        ndof = nbin - nfitParam
        chisq = chisq_ndof * ndof
    
        chisqbox = ROOT.TPaveText(0.1, 0.1, 0.55, 0.2, 'BRNDC')
        chisqbox.SetFillColor(0)
        chisqbox.SetBorderSize(1)
        chisqbox.AddText('#chi^{2}/ndof = %.2f/%d =  %.2f' %
                         (chisq, ndof, chisq_ndof))

        mbcFrame.addObject(chisqbox)

        mbcFrame.SetTitle(title1)
        mbcFrame.Draw()

        canvas.cd(2)
        mbcFrame = mbc.frame()
        mbcFrame=mbc.frame(60)
        dflav.setLabel('dbarflav')

        ebeam_aset = ROOT.RooArgSet(ebeam, dflav)
        ebeamdata = ROOT.RooDataHist("ebeamdata", "ebeamdata", 
                                     ebeam_aset, dataset)

        Cut = ROOT.RooFit.Cut("dflav==dflav::dbarflav")

        LineColor = ROOT.RooFit.LineColor(ROOT.kRed)
        LineWidth = ROOT.RooFit.LineWidth(1)
        #Slice = ROOT.RooFit.Slice(dflav, 'dbarflav')
        Slice = ROOT.RooFit.Slice(dflav)

        ProjWData = ROOT.RooFit.ProjWData(ebeam_aset, ebeamdata)
        
        dataset.plotOn(mbcFrame, Cut)
        mbcFrame.getAttMarker().SetMarkerSize(0.6)
        mbcFrame.Draw()


        pdf.plotOn(mbcFrame, LineColor, LineWidth, Slice, ProjWData)

        nfitParam = len(pars) - 2
        chisq_ndof = mbcFrame.chiSquare(nfitParam)
        
        nbin = mbcFrame.GetNbinsX()
        ndof = nbin - nfitParam
        chisq = chisq_ndof * ndof
    
        chisqbox = ROOT.TPaveText(0.1, 0.1, 0.55, 0.2, 'BRNDC')
        chisqbox.SetFillColor(0)
        chisqbox.SetBorderSize(1)
        chisqbox.AddText('#chi^{2}/ndof = %.2f/%d =  %.2f' %
                         (chisq, ndof, chisq_ndof))

        mbcFrame.addObject(chisqbox)

        mbcFrame.SetTitle(title2)
        mbcFrame.Draw()


        canvas.cd(3)
        mbcFrame = mbc.frame()
        
        paramWin1 = pdf.paramOn(mbcFrame,dataset,
                                "",2,"NELU",0.1,0.9,0.9)
        mbcFrame.GetXaxis().SetLabelSize(0) 
        mbcFrame.GetXaxis().SetTickLength(0) 
        mbcFrame.GetXaxis().SetLabelSize(0) 
        mbcFrame.GetXaxis().SetTitle("") 
        mbcFrame.GetXaxis().CenterTitle() 
        
        mbcFrame.GetYaxis().SetLabelSize(0) 
        mbcFrame.GetYaxis().SetTitleSize(0.03) 
        mbcFrame.GetYaxis().SetTickLength(0) 
        
        paramWin1.getAttText().SetTextSize(0.06) 
        
        mbcFrame.Draw() 
        mbcFrame.SetTitle("Fit Parameters") 
        mbcFrame.Draw() 
 

    canvas.Print(epsfile)
    tools.save_fit_result(pars, txtfile)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 17
0
def double_all_dps(dt_type, mode, label, test=False):
    tag = 'double'
    prefix = 'dir_' + label
    evtprefix = get_evtprefix(dt_type, label)

    if label == '818ipbv12.1':
        label = '818ipbv12'

    mode1 = 200
    mode2 = 200

    title1 = attr.modes[mode1]['uname']
    title2 = attr.modes[mode2]['unamebar']

    code1 = attr.interfacecodes[mode1]
    code2 = attr.interfacecodes[mode2]

    Gamma, Mres, R, mc = get_common_parameters(dt_type, label)

    Sigma1, F1a, F1b, S1a, S1b = get_resolution_paras(mode1, label)
    Sigma2, F2a, F2b, S2a, S2b = get_resolution_paras(mode2, label)

    opt, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat = get_double_paras(label)

    epsfile = tools.set_file('eps',
                             dt_type,
                             mode,
                             tag,
                             prefix=prefix,
                             extbase=attr.figpath)
    fitpath = attr.fitpath
    outfile = tools.set_file('txt',
                             dt_type,
                             mode,
                             tag,
                             prefix=prefix,
                             extbase=fitpath)
    evtpath = attr.evtpath
    evtfile = tools.set_file('evt',
                             dt_type,
                             mode,
                             tag,
                             prefix=evtprefix,
                             forceCombine=0,
                             extbase=evtpath)

    if not os.access(evtfile, os.F_OK):
        forceCombine = 'all_dps'
        evtfile = tools.set_file('evt',
                                 dt_type,
                                 mode,
                                 tag,
                                 prefix=evtprefix,
                                 forceCombine=forceCombine,
                                 extbase=evtpath)
    tools.print_sep()

    par_str = '("%s", %f, %f, %f, %f, %f, "%s", %f, %f, %f, \
    %f, %f, "%s", "%s", "%s", %d, "%s", "%s", "%s", %f,\
    %f, %f,  %f, %f, %f, %f, %f)'

    par_tuple = (code1, Sigma1, F1a, F1b, S1a, S1b, code2, Sigma2, F2a, F2b,
                 S2a, S2b, evtfile, epsfile, outfile, mc, opt, title1, title2,
                 Gamma, Mres, R, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat)

    par = par_str % par_tuple

    print_paras_double(par_tuple)

    load_roofit_lib(dt_type, label)

    sourcename = 'lineshapefit2d.C'
    if prefix == 'dir_818ipbv12.1':
        sourcename = 'lineshapefit2d_1.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, sourcename)

    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    if test:
        return

    ROOT.gROOT.Macro(source + par)

    sys.stdout.write('Save output in %s\n' % outfile)
    tools.eps2png(epsfile)
    tools.eps2pdf(epsfile)
Exemplo n.º 18
0
def fit_backgrounds_single(datatype, modes, label, test):
    epsname = '%s_background' %(datatype.replace('/', '_'))
    epspath = os.path.join(attr.figpath, label)    
    epsfile = set_file(extbase=epspath, comname=epsname, ext='eps')

    if test:
        epsfile = epsfile + '.test'

    input_label = label

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle('Plain')
    
    mbc = ROOT.RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV')
    mbc_aset = ROOT.RooArgSet(mbc)

    ebeam = ROOT.RooRealVar('ebeam','Ebeam',1.8815,1.892,'GeV')

    dflav = ROOT.RooCategory('dflav','D0 flavor')
    dflav.defineType('dflav',1)
    dflav.defineType('dbarflav',-1)

    dataset_args = ROOT.RooArgList(mbc, ebeam, dflav)

    arg_cutoff = ROOT.RooRealVar('arg_cutoff', 'Argus cutoff', 1.88, 1.89, 'GeV')
    arg_slope = ROOT.RooRealVar('arg_slope', 'Argus slope', -10, -100, -1)
    yld = ROOT.RooRealVar('yield', 'D yield', 100, 0, 200000)
    bkg = ROOT.RooRealVar('bkg', 'Background', 100, 0, 1000000)

    sigma = ROOT.RooRealVar('sigma', 'D width', 0.0001, 0.005, 'GeV')
    

    mbc_d0 = ROOT.RooRealVar('mbc_d0', 'D0 Mbc', 1.8647, 'GeV')
    mbc_dp = ROOT.RooRealVar('mbc_dp', 'D+ Mbc', 1.8694, 'GeV')
    
    gauss_d0 = ROOT.RooGaussian('gauss_d0', 'D0 gaussian', mbc, mbc_d0, sigma)
    gauss_dp = ROOT.RooGaussian('gauss_dp', 'D+ gaussian', mbc, mbc_dp, sigma)

    argus = ROOT.RooArgusBG('argus', 'Argus BG', mbc, arg_cutoff, arg_slope)
    sumpdf_d0 = ROOT.RooAddPdf('sumpdf_d0', 'D0 sum pdf',
                          ROOT.RooArgList(gauss_d0, argus),
                          ROOT.RooArgList(yld, bkg))
    sumpdf_dp = ROOT.RooAddPdf('sumpdf_dp', 'Dp sum pdf',
                               ROOT.RooArgList(gauss_dp, argus),
                               ROOT.RooArgList(yld, bkg))

    Extended = ROOT.RooFit.Extended(ROOT.kTRUE)
    Save = ROOT.RooFit.Save(ROOT.kTRUE)
    Hesse = ROOT.RooFit.Hesse(ROOT.kFALSE)
    Verbose = ROOT.RooFit.Verbose(ROOT.kFALSE)

    if '281ipb' in label:
        prefix = 'dir_281ipbv7'
    elif '537ipb' in label:
        prefix = 'dir_537ipbv7'
    elif '818ipb' in label:
        prefix = 'dir_818ipbv7'
    else:
        raise NameError(label)

    c1 = ROOT.TCanvas('c1', 'canvas1', 800, 1000)
    #c1.SetFixedAspectRatio(0)
    c1.Divide(3,3)
    pad = 1
    for mode in modes:
        mode_root_name = get_mode_root_name(mode)
        if datatype == 'generic/ddbar':
            frametitle = 'Generic faking %s' % mode_root_name
        elif datatype == 'generic/cont':
            frametitle = 'Continuum production faking %s' % mode_root_name
        elif datatype == 'generic/tau':
            frametitle = '#tau #tau production faking %s' % mode_root_name
        elif datatype == 'generic/radret':
            frametitle = 'Radiative return faking %s' % mode_root_name
        else:
            raise NameError(datatype)
    
        c1.cd(pad)
        pad += 1
        comname = '%s_%s' %(datatype.replace('/', '_'), mode)
        bkgpath = os.path.join(attr.bkgpath(), input_label)
        bkgfile = set_file(extbase=bkgpath, comname=comname, ext='evt')
        if not os.access(bkgfile, os.F_OK):
            input_prefix = 'dir_%s' % input_label
            bkgfile = set_file(extbase=attr.bkgpath(),
                               dt_type=datatype.replace('/', '_'),
                               tag='s', mode=mode, prefix=input_prefix,
                               comname=comname, ext='evt', forceCombine=1)

        dataset = ROOT.RooDataSet.read(bkgfile, dataset_args)

        if 'D0' in mode:
            pdf = sumpdf_d0
        else:
            pdf = sumpdf_dp

        if 'floatsigma' not in label:
            tab_datatype = 'data'
            tag = 'single'
        
        tabfile = set_file('txt', tab_datatype, mode, tag,
                           prefix=prefix,
                           extbase=attr.fitpath())

        sys.stdout.write('Using width from %s \n' %tabfile)
        tab = DHadTable(tabfile)
        sigmap1 = float(tab.cell_get('sigmap1', 'Value'))
        sigma.setVal(sigmap1)
        sigma.setConstant(ROOT.kTRUE)
        
        res = pdf.fitTo(dataset, Extended, Save, Verbose)
        res.Print('v')

        xframe = mbc.frame()
        xframe.SetMarkerSize(0.3)
        if '818ipb' in label and 'noxfeed' in label:
            frame_max = bkg_generic_ddbar_818_max_noxfeed[mode]
            xframe.SetMaximum(frame_max)

        dataset.plotOn(xframe)
        pdf.plotOn(xframe)

        pdf.paramOn(xframe, dataset)        

        xframe.SetTitle(frametitle)
        xframe.Draw()

    c1.Print(epsfile)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 19
0
def double_tag_mode(dt_type, mode, label, interact=False, test=False):
    mode = get_modekey(mode)
    tag = 'double'
    prefix = 'dir_' + label
    evtprefix = get_evtprefix(dt_type, label)

    mode1 = mode[0]
    mode2 = mode[1]

    title1 = attr.modes[mode1]['uname']
    title2 = attr.modes[mode2]['unamebar']

    code1 = attr.interfacecodes[mode1]
    code2 = attr.interfacecodes[mode2]

    forceCombine = None  # Different from single tag case

    Gamma, Mres, R, mc = get_common_parameters(dt_type, label)

    Sigma1, F1a, F1b, S1a, S1b = get_resolution_paras(mode1, label)
    Sigma2, F2a, F2b, S2a, S2b = get_resolution_paras(mode2, label)

    opt, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat = get_double_paras(label)

    epsfile = tools.set_file('eps',
                             dt_type,
                             mode,
                             tag,
                             prefix=prefix,
                             extbase=attr.figpath)
    fitpath = attr.fitpath
    outfile = tools.set_file('txt',
                             dt_type,
                             mode,
                             tag,
                             prefix=prefix,
                             extbase=fitpath)
    evtpath = attr.evtpath
    evtfile = tools.set_file('evt',
                             dt_type,
                             mode,
                             tag,
                             prefix=evtprefix,
                             forceCombine=forceCombine,
                             extbase=evtpath)
    if not os.access(evtfile, os.F_OK):
        if '818ipb' in evtprefix:
            evtfile_281ipb = evtfile.replace('818ipb', '281ipb')
            evtfile_537ipb = evtfile.replace('818ipb', '537ipb')
            combine_files(evtfile_281ipb, evtfile_537ipb, evtfile)
        else:
            raise ValueError(evtfile)

    tools.print_sep()

    par_str = '("%s", %f, %f, %f, %f, %f, "%s", %f, %f, %f, \
    %f, %f, "%s", "%s", "%s", %d, "%s", "%s", "%s", %f,\
    %f, %f,  %f, %f, %f, %f, %f)'

    par_tuple = (code1, Sigma1, F1a, F1b, S1a, S1b, code2, Sigma2, F2a, F2b,
                 S2a, S2b, evtfile, epsfile, outfile, mc, opt, title1, title2,
                 Gamma, Mres, R, N, Nbkgd1, Nbkgd2, Nbkgd3, NbkgdFlat)

    par = par_str % par_tuple

    print_paras_double(par_tuple)

    load_roofit_lib(dt_type, label)

    sourcename = 'lineshapefit2d.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, sourcename)

    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    if test:
        return

    ROOT.gROOT.Macro(source + par)

    sys.stdout.write('Save output in %s\n' % outfile)
    tools.eps2png(epsfile)
    tools.eps2pdf(epsfile)
Exemplo n.º 20
0
def fit_sidebands_single_mode(datatype, mode, label, test):
    load_roofit_lib(datatype, label)

    dt_type = datatype.replace('/', '_')
    modekey = tools.get_modekey(mode)

    comname = '%s_%s' % (dt_type, mode)
    input_label = label

    sdbpath = os.path.join(attr.evtpath, input_label)
    sdbfile = set_file(extbase=sdbpath, comname=comname, ext='evt')

    has_charge_conjugate = False
    if '__' in mode:
        has_charge_conjugate = True

    if not os.access(sdbfile, os.F_OK):
        if has_charge_conjugate:
            sdbfile = set_file(extbase=sdbpath,
                               dt_type=dt_type,
                               tag='s',
                               mode=modekey,
                               comname=comname,
                               ext='evt',
                               forceCombine=1)
        else:
            raise ValueError(sdbfile)

    epspath = os.path.join(attr.figpath, label)
    epsfile = set_file(extbase=epspath, comname=comname, ext='eps')

    txtpath = os.path.join(attr.fitpath, label)
    txtfile = set_file(extbase=txtpath, comname=comname, ext='txt')

    if test:
        epsfile = epsfile + '.test'
        txtfile = txtfile + '.test'

    ROOT.gROOT.SetBatch(1)
    ROOT.gROOT.SetStyle('Plain')

    mbc = ROOT.RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV')
    mbc_aset = ROOT.RooArgSet(mbc)

    ebeam = ROOT.RooRealVar('ebeam', 'Ebeam', 1.8815, 1.892, 'GeV')

    dflav = ROOT.RooCategory('dflav', 'D0 flavor')

    dflav.defineType('dflav', 1)
    dflav.defineType('dbarflav', -1)

    dataset_args = ROOT.RooArgList(mbc, ebeam, dflav)

    dataset = ROOT.RooDataSet.read(sdbfile, dataset_args)

    arg_cutoff = ebeam
    arg_slope = ROOT.RooRealVar('arg_slope', 'Argus slope', -10, -100, -1)
    arg_power = ROOT.RooRealVar('arg_power', 'Argus power', 0.5, 0.1, 1.5)

    yld = ROOT.RooRealVar('yield', 'D yield', 100, 0, 200000)
    bkg = ROOT.RooRealVar('bkg', 'Background', 100, 0, 1000000)

    sigma = ROOT.RooRealVar('sigma', 'D width', 0.0001, 0.005, 'GeV')

    pars = [arg_slope, arg_power, bkg, sigma, yld]

    mbc_d0 = ROOT.RooRealVar('mbc_d0', 'D0 Mbc', 1.8647, 'GeV')
    mbc_dp = ROOT.RooRealVar('mbc_dp', 'D+ Mbc', 1.8694, 'GeV')

    gauss_d0 = ROOT.RooGaussian('gauss_d0', 'D0 gaussian', mbc, mbc_d0, sigma)
    gauss_dp = ROOT.RooGaussian('gauss_dp', 'D+ gaussian', mbc, mbc_dp, sigma)

    argus = ROOT.RooArgusBG('argus', 'Argus BG', mbc, arg_cutoff, arg_slope,
                            arg_power)
    sumpdf_d0 = ROOT.RooAddPdf('sumpdf_d0', 'D0 sum pdf',
                               ROOT.RooArgList(gauss_d0, argus),
                               ROOT.RooArgList(yld, bkg))
    sumpdf_dp = ROOT.RooAddPdf('sumpdf_dp', 'Dp sum pdf',
                               ROOT.RooArgList(gauss_dp, argus),
                               ROOT.RooArgList(yld, bkg))

    Extended = ROOT.RooFit.Extended(ROOT.kTRUE)  # e
    Save = ROOT.RooFit.Save(ROOT.kTRUE)  # r
    Hesse = ROOT.RooFit.Hesse(ROOT.kFALSE)  # no h
    Verbose = ROOT.RooFit.Verbose(ROOT.kFALSE)  # no q

    if 'D0' in mode:
        pdf = sumpdf_d0
    else:
        pdf = sumpdf_dp

    if has_charge_conjugate:
        yld_bar = ROOT.RooRealVar('yield_bar', 'Dbar yield', 100, 0, 200000)
        bkg_bar = ROOT.RooRealVar('bkg_bar', 'Background Bar', 100, 0, 1000000)
        pars.extend([yld_bar, bkg_bar])

        mbc_d0bar = ROOT.RooRealVar('mbc_d0bar', 'D0Bar Mbc', 1.8647, 'GeV')
        mbc_dm = ROOT.RooRealVar('mbc_dm', 'D- Mbc', 1.8694, 'GeV')

        gauss_d0bar = ROOT.RooGaussian('gauss_d0bar', 'D0bar gaussian', mbc,
                                       mbc_d0bar, sigma)
        gauss_dm = ROOT.RooGaussian('gauss_dm', 'D- gaussian', mbc, mbc_dm,
                                    sigma)

        sumpdf_d0bar = ROOT.RooAddPdf('sumpdf_d0bar', 'D0bar sum pdf',
                                      ROOT.RooArgList(gauss_d0bar, argus),
                                      ROOT.RooArgList(yld_bar, bkg_bar))
        sumpdf_dm = ROOT.RooAddPdf('sumpdf_dm', 'Dm sum pdf',
                                   ROOT.RooArgList(gauss_dm, argus),
                                   ROOT.RooArgList(yld_bar, bkg_bar))

        pdf = ROOT.RooSimultaneous('totalPdf', 'totalPdf', dflav)

        if 'D0' in mode:
            pdf.addPdf(sumpdf_d0, 'dflav')
            pdf.addPdf(sumpdf_d0bar, 'dbarflav')
        else:
            pdf.addPdf(sumpdf_dp, 'dflav')
            pdf.addPdf(sumpdf_dm, 'dbarflav')

    if 'nogaus' in label:
        yld.setVal(1)
        yld.setConstant(ROOT.kTRUE)
        pars.remove(yld)

    if 'floatsigma' not in label:
        datatype = 'data'
        tag = 'single'

        if '818ipb' in label:
            prefix = 'dir_818ipbv12'
            frame_max = 1500  #bkg_generic_ddbar_818_max[mode]
        else:
            raise NameError(label)

        tabfile = set_file('txt',
                           datatype,
                           modekey,
                           tag,
                           prefix=prefix,
                           extbase=attr.fitpath)

        sys.stdout.write('Using width from %s \n' % tabfile)
        tab = DHadTable(tabfile)
        sigmap1 = float(tab.cell_get('sigmap1', 'Value'))
        sigma.setVal(sigmap1)
        sigma.setConstant(ROOT.kTRUE)
        pars.remove(sigma)

    #res = pdf.fitTo(dataset, Extended, Save, Verbose)
    res = pdf.fitTo(dataset, 'emr')  #Migrad only, no MINOS
    res.Print('v')

    if not has_charge_conjugate:
        canvas = ROOT.TCanvas('canvas', 'canvas1', 600, 600)
        canvas.SetFixedAspectRatio(0)

        xframe = mbc.frame()

        xframe.SetMaximum(frame_max)
        xframe.SetMarkerSize(0.3)
        dataset.plotOn(xframe)
        pdf.plotOn(xframe)
        nfitParam = len(pars)
        chisq_ndof = xframe.chiSquare(nfitParam)

        nbin = xframe.GetNbinsX()
        ndof = nbin - nfitParam
        chisq = chisq_ndof * ndof

        chisqbox = ROOT.TPaveText(0.1, 0.1, 0.4, 0.15, 'BRNDC')
        chisqbox.SetFillColor(0)
        chisqbox.SetBorderSize(1)
        chisqbox.AddText('#chi^{2}/ndof = %.2f/%d =  %.2f' %
                         (chisq, ndof, chisq_ndof))
        xframe.addObject(chisqbox)

        pdf.paramOn(xframe, dataset)

        xframe.SetTitle('Sidebands of %s' % mode)
        xframe.GetYaxis().SetTitleSize(0.03)
        xframe.GetYaxis().SetLabelSize(0.03)
        xframe.GetYaxis().SetTitleOffset(1.8)
        xframe.GetXaxis().SetTitleSize(0.03)
        xframe.GetXaxis().SetLabelSize(0.03)
        xframe.GetXaxis().SetTitleOffset(1.2)
        xframe.Draw()
    else:
        canvas = ROOT.TCanvas('canvas', 'mbc', 1200, 400)
        canvas.Divide(3, 1)
        title1 = attr.modes[modekey]['uname']
        title2 = attr.modes[modekey]['unamebar']

        canvas_1 = canvas.GetListOfPrimitives().FindObject('canvas_1')
        canvas_2 = canvas.GetListOfPrimitives().FindObject('canvas_2')
        canvas_1.SetLogy()
        canvas_2.SetLogy()

        ebeam.setBins(900)

        canvas.cd(1)
        mbcFrame = mbc.frame()
        mbcFrame = mbc.frame(60)
        dflav.setLabel('dflav')
        ebeam_aset = ROOT.RooArgSet(ebeam, dflav)
        ebeamdata = ROOT.RooDataHist("ebeamdata", "ebeamdata", ebeam_aset,
                                     dataset)

        Cut = ROOT.RooFit.Cut("dflav==dflav::dflav")

        LineColor = ROOT.RooFit.LineColor(ROOT.kRed)
        LineWidth = ROOT.RooFit.LineWidth(1)
        #Slice = ROOT.RooFit.Slice(dflav, 'dflav')
        Slice = ROOT.RooFit.Slice(dflav)

        ProjWData = ROOT.RooFit.ProjWData(ebeam_aset, ebeamdata)

        dataset.plotOn(mbcFrame, Cut)
        mbcFrame.getAttMarker().SetMarkerSize(0.6)
        mbcFrame.Draw()

        pdf.plotOn(mbcFrame, LineColor, LineWidth, Slice, ProjWData)

        nfitParam = len(pars) - 2
        chisq_ndof = mbcFrame.chiSquare(nfitParam)

        nbin = mbcFrame.GetNbinsX()
        ndof = nbin - nfitParam
        chisq = chisq_ndof * ndof

        chisqbox = ROOT.TPaveText(0.1, 0.1, 0.55, 0.2, 'BRNDC')
        chisqbox.SetFillColor(0)
        chisqbox.SetBorderSize(1)
        chisqbox.AddText('#chi^{2}/ndof = %.2f/%d =  %.2f' %
                         (chisq, ndof, chisq_ndof))

        mbcFrame.addObject(chisqbox)

        mbcFrame.SetTitle(title1)
        mbcFrame.Draw()

        canvas.cd(2)
        mbcFrame = mbc.frame()
        mbcFrame = mbc.frame(60)
        dflav.setLabel('dbarflav')

        ebeam_aset = ROOT.RooArgSet(ebeam, dflav)
        ebeamdata = ROOT.RooDataHist("ebeamdata", "ebeamdata", ebeam_aset,
                                     dataset)

        Cut = ROOT.RooFit.Cut("dflav==dflav::dbarflav")

        LineColor = ROOT.RooFit.LineColor(ROOT.kRed)
        LineWidth = ROOT.RooFit.LineWidth(1)
        #Slice = ROOT.RooFit.Slice(dflav, 'dbarflav')
        Slice = ROOT.RooFit.Slice(dflav)

        ProjWData = ROOT.RooFit.ProjWData(ebeam_aset, ebeamdata)

        dataset.plotOn(mbcFrame, Cut)
        mbcFrame.getAttMarker().SetMarkerSize(0.6)
        mbcFrame.Draw()

        pdf.plotOn(mbcFrame, LineColor, LineWidth, Slice, ProjWData)

        nfitParam = len(pars) - 2
        chisq_ndof = mbcFrame.chiSquare(nfitParam)

        nbin = mbcFrame.GetNbinsX()
        ndof = nbin - nfitParam
        chisq = chisq_ndof * ndof

        chisqbox = ROOT.TPaveText(0.1, 0.1, 0.55, 0.2, 'BRNDC')
        chisqbox.SetFillColor(0)
        chisqbox.SetBorderSize(1)
        chisqbox.AddText('#chi^{2}/ndof = %.2f/%d =  %.2f' %
                         (chisq, ndof, chisq_ndof))

        mbcFrame.addObject(chisqbox)

        mbcFrame.SetTitle(title2)
        mbcFrame.Draw()

        canvas.cd(3)
        mbcFrame = mbc.frame()

        paramWin1 = pdf.paramOn(mbcFrame, dataset, "", 2, "NELU", 0.1, 0.9,
                                0.9)
        mbcFrame.GetXaxis().SetLabelSize(0)
        mbcFrame.GetXaxis().SetTickLength(0)
        mbcFrame.GetXaxis().SetLabelSize(0)
        mbcFrame.GetXaxis().SetTitle("")
        mbcFrame.GetXaxis().CenterTitle()

        mbcFrame.GetYaxis().SetLabelSize(0)
        mbcFrame.GetYaxis().SetTitleSize(0.03)
        mbcFrame.GetYaxis().SetTickLength(0)

        paramWin1.getAttText().SetTextSize(0.06)

        mbcFrame.Draw()
        mbcFrame.SetTitle("Fit Parameters")
        mbcFrame.Draw()

    canvas.Print(epsfile)
    tools.save_fit_result(pars, txtfile)

    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)
Exemplo n.º 21
0
def single_tag_mode(dt_type, mode, label, interact=False, test=False):
    mode = get_modekey(mode)
    tag = 'single'
    prefix = 'dir_' + label
    evtprefix = get_evtprefix(dt_type, label)
    Gamma, Mres, R, mc = get_common_parameters(dt_type, label)
    Sigma, Fa, Fb, Sa, Sb = get_resolution_paras(mode, label)
    N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1, xi = init_paras_single(
        label, dt_type, tag, mode)
    # ------------------- Parameters for the fit ----------
    title1 = attr.modes[mode]['uname']
    title2 = attr.modes[mode]['unamebar']
    num_fcn  = 3  # : 0 for only background
    xi_side  = 0.0
    p_side   = 0.0
    int_d    = 1
    optstr   = 'p'  # To plot,  f : fast, w: fixwidth
    floatwidth = 0 
    Min      = 0.5
    options  = ""
    MINUIT   = "ermh4"
    # -------------------------------------------------

    if '/p/0.5' in label:
        p_side = 0.5

    if '/argus' in label:
        options = 'fix_xi,fix_p'
        xi, p = get_argus_paras_single(label, dt_type, tag, mode)

    if '/fix_sigmap1' in label:
        options = 'fix_sigmap1'
        if '/kssideband' in label:
            sys.stdout.write('Fixing the sigmap1 as %s ...\n' %Sigma)
            sigmap1 = Sigma 

    if '/fix_n1n2' in label and '/desideband' in label:
        options = 'fix_n1n2'
        N1 = 1
        N2 = 1
        
    if dt_type == 'signal' and mode in [0, 200, 202]:
        p_side = 0.5
        tools.print_sep()

    forceCombine = 1
    epsfile =  tools.set_file('eps', dt_type, mode, tag,
                              prefix=prefix, extbase=attr.figpath)
    fitpath = attr.fitpath 
    outfile =  tools.set_file('txt', dt_type, mode, tag,
                              prefix=prefix, extbase=fitpath)
    evtpath = attr.evtpath
    evtfile =  tools.set_file('evt', dt_type, mode, tag, prefix=evtprefix,
                              forceCombine=forceCombine, extbase=evtpath)
    tools.print_sep()
    par_str = '( "%s", "%s", "%s", "%s", "%s", %d, %d, %f, %f, %f, %f, \
    %f, %f, %f, %d, "%s", %f, %d, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
    %f, "%s", "%s" )'
    par_tuple = (title1, title2, evtfile, epsfile, outfile, mc, num_fcn,
                 xi_side, p_side, Sigma, Sa, Sb, Fa, Fb, int_d, optstr,
                 Gamma, floatwidth, R, Mres, N1, N2, Nbkgd1, Nbkgd2, md,
                 p, sigmap1, xi, Min, options, MINUIT)
    par = par_str % par_tuple
    print_paras(par_tuple)
    load_roofit_lib(dt_type, label)
    sourcename = 'mbc_singletag_3s.C'
    #if interact:
    source = os.path.join(attr.srcfitpath, sourcename)
    tools.print_sep()
    sys.stdout.write('       ROOT macro: %s \n' % source)
    tools.print_sep()
    if test:
        return
    ROOT.gROOT.Macro( source + par)

    if test:
        evtfile = evtfile + '.test'
        epsfile = epsfile + '.test'
        outfile = outfile + '.test'

    # fit_mbc_single(title1, title2, evtfile, epsfile, outfile, mc, num_fcn,
    #                xi_side, p_side, Sigma, Sa, Sb, Fa, Fb, int_d, optstr,
    #                Gamma, floatwidth, R, Mres, N1, N2, Nbkgd1, Nbkgd2, md,
    #                p, sigmap1, xi, Min, options, MINUIT)

    sys.stdout.write('Save output in %s\n'  % outfile)
    if not test:
        tools.eps2png(epsfile)
        tools.eps2pdf(epsfile)