Example #1
0
def drawhistos():
    global hx, hy, hz, hmean, hxd, hmeand, hw, hwm, hxw, hyw, hzw, hxwm 
    global hywm, hzwm, hxy, hxz, hyz

    for hist in [hx, hy, hz, hmean, hxd, hmeand, hw, hwm, hxw, hyw, hzw, hxwm, 
                 hywm, hzwm]:
        canvases.next(hist.GetName())
        hist.Draw()
    
    for hist in [hxy, hxz, hyz]:
        canvases.next(hist.GetName())
        hist.Draw('colz')
    
    canvases.next('ioverlay')
    hxd.Draw()
    hxw.Draw('same')
    hyw.Draw('same')
    hzw.Draw('same')
    
    canvases.next('moverlay')
    hxd.Draw()
    hxwm.Draw('same')
    hywm.Draw('same')
    hzwm.Draw('same')

    canvases.update()
    def makeplot(self):
        '''Produces a canvas with a plot. Assumes configuration 
        Data is present.'''
        self.sourcepath = os.path.join(basepath, 
                                       self.sourcedir, 
                                       self.sourcefilename)
        self.source = ROOT.TFile.Open(self.sourcepath)
        histo = self.source.Get(self.sourcehistoname).Clone()
        ## Set the titles
        for obj, txt in [(histo           , self.title ),
                         (histo.GetXaxis(), self.xtitle), 
                         (histo.GetYaxis(), self.ytitle)]:
            obj.SetTitle(txt)

        histo.SetStats(False)

        self.canvas = canvases.next(self.name)
        histo.DrawCopy('colz')
        self.histo = self.canvas.GetListOfPrimitives().FindObject(
            histo.GetName()
            )
        self.histo.GetZaxis().SetRangeUser(0.9, 1.0)
        Latex([self.leftlabel], (0.15, 0.92), textsize=25).draw()
        Latex([self.rightlabel], (0.615, 0.92), textsize=25).draw()

        canvases.update()
Example #3
0
def main(varnames = 'r9b sieieb setab'.split()[:1],
         raw_name = 's12-zllm50-v7n',
         target_name = 'r12a-pho-j22-v1',
         option = 'skim10k',
         max_entries = 50000,
         prescale = 1,
         prescale_phase = 0,
         rho=0.8):
    '''
    Main entry point of execution.
    '''
    global extractors
    extractors = []
    name = '_'.join([raw_name.split('-')[0], 'to',
                     target_name.split('-')[0], 'qqcorrector'])
    out_file_name = '_'.join([raw_name.split('-')[0], 'to',
                              target_name.split('-')[0], 'qqcorrections.root'])
    if os.path.isfile(out_file_name):
        os.remove(out_file_name)
    for varname in varnames:
        print 'Q-Q Extractor: Processing', varname, '...'
        extractor = QQExtractor(varname, raw_name, target_name, option, 
                                max_entries, prescale, prescale_phase, rho)
        extractors.append(extractor)
        extractor.make_plots()
        canvases.update()
Example #4
0
def main(varnames='r9b sieieb setab'.split()[:1],
         raw_name='s12-zllm50-v7n',
         target_name='r12a-pho-j22-v1',
         option='skim10k',
         max_entries=50000,
         prescale=1,
         prescale_phase=0,
         rho=0.8):
    '''
    Main entry point of execution.
    '''
    global extractors
    extractors = []
    name = '_'.join([
        raw_name.split('-')[0], 'to',
        target_name.split('-')[0], 'qqcorrector'
    ])
    out_file_name = '_'.join([
        raw_name.split('-')[0], 'to',
        target_name.split('-')[0], 'qqcorrections.root'
    ])
    if os.path.isfile(out_file_name):
        os.remove(out_file_name)
    for varname in varnames:
        print 'Q-Q Extractor: Processing', varname, '...'
        extractor = QQExtractor(varname, raw_name, target_name, option,
                                max_entries, prescale, prescale_phase, rho)
        extractors.append(extractor)
        extractor.make_plots()
        canvases.update()
Example #5
0
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to test_pkeys - test of the mode and effective sigma'
    print 'extraction for a generic distribution using ParametrizedKeysPdf.'

    w = ROOT.RooWorkspace('w', 'w')

    truepdf = w.factory('Gaussian::truepdf(x[-5,5],m[0],s[1])')
    data = truepdf.generate(ROOT.RooArgSet(w.var('x')), 10000)
    canvases.next('truepdf')
    plot = w.var('x').frame()
    data.plotOn(plot)
    truepdf.plotOn(plot)
    plot.Draw()

    toypdf = ParameterizedKeysPdf('toypdf',
                                  'toypdf',
                                  w.var('x'),
                                  w.factory('mtoy[0,-5,5]'),
                                  w.factory('stoy[1,0.1,5]'),
                                  data,
                                  rho=3)
    toypdf.shape.plotOn(plot, roo.LineColor(ROOT.kRed))
    toypdf.fitTo(data)
    toypdf.plotOn(plot, roo.LineColor(ROOT.kGreen))
    plot.Draw()

    canvases.update()
    print 'Exiting test_pkeys with success!'
Example #6
0
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to test_pkeys - test of the mode and effective sigma'
    print 'extraction for a generic distribution using ParametrizedKeysPdf.'
    
    w = ROOT.RooWorkspace('w', 'w')
    
    truepdf = w.factory('Gaussian::truepdf(x[-5,5],m[0],s[1])')
    data = truepdf.generate(ROOT.RooArgSet(w.var('x')), 10000)
    canvases.next('truepdf')
    plot = w.var('x').frame()
    data.plotOn(plot)
    truepdf.plotOn(plot)
    plot.Draw()
    
    toypdf = ParameterizedKeysPdf('toypdf', 'toypdf', w.var('x'), 
                                  w.factory('mtoy[0,-5,5]'), 
                                  w.factory('stoy[1,0.1,5]'), data, rho=3)
    toypdf.shape.plotOn(plot, roo.LineColor(ROOT.kRed))
    toypdf.fitTo(data)
    toypdf.plotOn(plot, roo.LineColor(ROOT.kGreen))
    plot.Draw()
        
    
    canvases.update()
    print 'Exiting test_pkeys with success!'
Example #7
0
def drawhistos():
    global hx, hy, hz, hmean, hxd, hmeand, hw, hwm, hxw, hyw, hzw, hxwm
    global hywm, hzwm, hxy, hxz, hyz

    for hist in [
            hx, hy, hz, hmean, hxd, hmeand, hw, hwm, hxw, hyw, hzw, hxwm, hywm,
            hzwm
    ]:
        canvases.next(hist.GetName())
        hist.Draw()

    for hist in [hxy, hxz, hyz]:
        canvases.next(hist.GetName())
        hist.Draw('colz')

    canvases.next('ioverlay')
    hxd.Draw()
    hxw.Draw('same')
    hyw.Draw('same')
    hzw.Draw('same')

    canvases.next('moverlay')
    hxd.Draw()
    hxwm.Draw('same')
    hywm.Draw('same')
    hzwm.Draw('same')

    canvases.update()
Example #8
0
def main():
    '''
    RooWorkspace main()
    This is the entry point of execution.  Returns a workspace with the results
    '''
    ntoys = 500
    wlist = []
    for i in range(ntoys):
        w = ROOT.RooWorkspace('w%d' % i, 'Per-bin p-value toy %d' % i)
        setup_model(w)
        generate_events(w)
        calculate_pvalue_graph(w)
        make_pvalue_hist(w)
        wlist.append(w)

    pvalue_hist_tot = wlist[0].obj('pvalue_hist').Clone('pvalue_hist_tot')
    for w in wlist[1:]:
        pvalue_hist_tot.Add(w.obj('pvalue_hist'))

    plot_all(wlist[0])

    canvases.next('pvalue_hist_tot')
    pvalue_hist_tot.Draw('e0')
    canvases.update()

    return wlist, pvalue_hist_tot
Example #9
0
def main():
    '''
    RooWorkspace main()
    This is the entry point of execution.  Returns a workspace with the results
    '''
    ntoys = 500
    wlist = []    
    for i in range(ntoys):
        w = ROOT.RooWorkspace('w%d' % i, 
                              'Per-bin p-value toy %d' % i)
        setup_model(w)
        generate_events(w)    
        calculate_pvalue_graph(w)
        make_pvalue_hist(w)        
        wlist.append(w)
   
    pvalue_hist_tot = wlist[0].obj('pvalue_hist').Clone('pvalue_hist_tot')
    for w in wlist[1:]:
        pvalue_hist_tot.Add( w.obj('pvalue_hist') )
        
    plot_all(wlist[0])

    canvases.next('pvalue_hist_tot')
    pvalue_hist_tot.Draw('e0')
    canvases.update()
    
    return wlist, pvalue_hist_tot    
def make_comparison_plot(data_raw, data_target, x_corr):
    '''
    Plot x data with the overlayed fitted model.
    '''
    data_corr = get_corrected_data(data_raw, x_corr)
    # global plot
    if plot_range:
        plot = x.frame(roo.Range(*plot_range))
    else:
        plot = x.frame()
    plot.SetTitle('Raw (red) vs Corrected (blue) vs Target (black) Data')
    if b_corr.getVal() > 1.:
        data_raw.plotOn(plot, roo.LineColor(ROOT.kRed), 
                        roo.MarkerColor(ROOT.kRed))
        data_corr.plotOn(plot, roo.LineColor(ROOT.kBlue), 
                         roo.MarkerColor(ROOT.kBlue))
        data_target.plotOn(
          plot, 
          roo.Rescale(data_raw.sumEntries() / data_target.sumEntries())
          )
    else:
        data_target.plotOn(plot)
        data_raw.plotOn(
            plot, roo.LineColor(ROOT.kRed), roo.MarkerColor(ROOT.kRed),
            roo.Rescale(data_target.sumEntries() / data_raw.sumEntries())
            )
        data_corr.plotOn(
            plot, roo.LineColor(ROOT.kBlue), roo.MarkerColor(ROOT.kBlue),
            roo.Rescale(data_target.sumEntries() / data_corr.sumEntries())
            )
        
    canvases.next('Raw_vs_Corrected_vs_Target_Data').SetGrid()
    plot.Draw()
    canvases.update()
Example #11
0
 def make_plots(self):
     """
     Create canvases with plots demonstrating the transform and the
     original and transformed distributions.
     """
     self.plot_x()
     self.plot_transform()
     self.plot_inverse_transform()
     self.plot_uniform()
     canvases.update()
Example #12
0
def main():
    '''
    Main entry point of execution.
    '''
    #plot_aggregate_throughput()
    #plot_outbound_throughput()
    #plot_inbound_throughput()
    plot_all_throughputs_overlaid()
    plot_cpu_usage()
    canvases.update()
Example #13
0
def main():
    '''
    Main entry point of execution.
    '''
    #plot_aggregate_throughput()
    #plot_outbound_throughput()
    #plot_inbound_throughput()
    plot_all_throughputs_overlaid()
    plot_cpu_usage()
    canvases.update()
Example #14
0
 def make_plots(self):
     '''
     Create canvases with plots demonstrating the transform and the
     original and transformed distributions.
     '''
     self.plot_x()
     self.plot_transform()
     self.plot_inverse_transform()
     self.plot_uniform()
     canvases.update()
Example #15
0
def main():
    '''
    Main entry point of execution of a test.
    '''
    global widthprofile
    basepath = '/home/cmorgoth/scratch/CMSSW_5_2_5/src/UserCode/CPena/src/PhosphorCorrFunctor/SIXIE_LAST_VERSION'

    basecuts = [
        'DileptonMass + Mass < 180',
        #'0.4 < MinDeltaR',
        '0.05 < min(abs(Mu1Eta - PhotonEta), abs(Mu2Eta - PhotonEta)) || 0.3 < MinDeltaR',    
        'MinDeltaR < 1.5', 
        'Mu2Pt > 10.5',
        'Mu1Pt > 21', 
        'DileptonMass > 55',
        ]
        
    catcuts = [
        'PhotonIsEB',
        '25 < PhotonPt && PhotonPt < 99',
        'PhotonR9 >= 0.94',
        #'PhotonR9 < 0.94',
        #'RunNumber >= 197770', ## Beginning of 2012C
        'RunNumber < 197770', ## Beginning of 2012C
        ]

    name = 'EB_highR9_2012CD'
    title = 'Barrel, R9 > 0.94, 2012CD'

    sources = [
        Source(
            name = 'regression',
            title = 'Regression',
            filename = os.path.join(
                basepath, 
                'PhotonRegression/ZmumuGammaNtuple_Full2012_MuCorr.root'
                ),
            cuts = basecuts + catcuts,
            ),
        
        Source(
            name = 'default',
            title = 'Default',
            filename = os.path.join(
                basepath, 
                'NoPhotonRegression/ZmumuGammaNtuple_Full2012_MuCorr.root',
                ),
            cuts = basecuts + catcuts,
            )
        ]

    widthprofile = WidthProfile(name, title, sources, 20, 500)
    widthprofile.run()
    
    canvases.update()
def make_plot(x, data, model):
    '''
    Plot x data with the overlayed fitted model.
    '''
    global plot
    plot = x.frame()
    data.plotOn(plot)
    model.plotOn(plot)
    model.paramOn(plot)
    canvases.next(workspace)
    plot.Draw()
    canvases.update()
Example #17
0
def test():
    global w, ux, uy, uuplots, xyplot, yxplot
    w = ROOT.RooWorkspace("test", "test")
    fX = w.factory("Gaussian::fX(x[-3,3], m[0], s[1])")
    fY = w.factory("Gamma::fY(y[0,20], beta[2], gamma[2], mu[0])")
    ux = Uniformizer(w.var('x'), fX)
    uy = Uniformizer(w.var('y'), fY, color=ROOT.kRed)
    ux.make_plots()
    uy.make_plots()
    uuplot = make_uuplot(ux, uy)
    xyplot, yxplot = make_qqplots(ux, uy)
    canvases.update()
Example #18
0
def test():
    global w, ux, uy, uuplots, xyplot, yxplot
    w = ROOT.RooWorkspace("test", "test")
    fX = w.factory("Gaussian::fX(x[-3,3], m[0], s[1])")
    fY = w.factory("Gamma::fY(y[0,20], beta[2], gamma[2], mu[0])")
    ux = Uniformizer(w.var("x"), fX)
    uy = Uniformizer(w.var("y"), fY, color=ROOT.kRed)
    ux.make_plots()
    uy.make_plots()
    uuplot = make_uuplot(ux, uy)
    xyplot, yxplot = make_qqplots(ux, uy)
    canvases.update()
Example #19
0
def main():
    '''
    Main entry point of execution of a test.
    '''
    global widthprofile
    basepath = '/home/cmorgoth/scratch/CMSSW_5_2_5/src/UserCode/CPena/src/PhosphorCorrFunctor/SIXIE_LAST_VERSION'

    basecuts = [
        'DileptonMass + Mass < 180',
        #'0.4 < MinDeltaR',
        '0.05 < min(abs(Mu1Eta - PhotonEta), abs(Mu2Eta - PhotonEta)) || 0.3 < MinDeltaR',
        'MinDeltaR < 1.5',
        'Mu2Pt > 10.5',
        'Mu1Pt > 21',
        'DileptonMass > 55',
    ]

    catcuts = [
        'PhotonIsEB',
        '25 < PhotonPt && PhotonPt < 99',
        'PhotonR9 >= 0.94',
        #'PhotonR9 < 0.94',
        #'RunNumber >= 197770', ## Beginning of 2012C
        'RunNumber < 197770',  ## Beginning of 2012C
    ]

    name = 'EB_highR9_2012CD'
    title = 'Barrel, R9 > 0.94, 2012CD'

    sources = [
        Source(
            name='regression',
            title='Regression',
            filename=os.path.join(
                basepath,
                'PhotonRegression/ZmumuGammaNtuple_Full2012_MuCorr.root'),
            cuts=basecuts + catcuts,
        ),
        Source(
            name='default',
            title='Default',
            filename=os.path.join(
                basepath,
                'NoPhotonRegression/ZmumuGammaNtuple_Full2012_MuCorr.root',
            ),
            cuts=basecuts + catcuts,
        )
    ]

    widthprofile = WidthProfile(name, title, sources, 20, 500)
    widthprofile.run()

    canvases.update()
def make_plot(x, data, model, name=name):
    '''
    Plot x data with the overlayed fitted model.
    '''
    global plot
    plot = x.frame(roo.Range('plot'))
    data.plotOn(plot)
    model.plotOn(plot)
    model.paramOn(plot)
    canvases.next(name)
    plot.Draw()
    canvases.update()
def make_plot(x, data, model):
    '''
    Plot x data with the overlayed fitted model.
    '''
    global plot
    plot = x.frame()
    data.plotOn(plot)
    model.plotOn(plot)
    model.paramOn(plot)
    canvases.next(workspace)
    plot.Draw()
    canvases.update()
Example #22
0
def main():
    '''
    Main entry point for execution
    '''
    global widthprofiles
    widthprofiles = []
    for name in names_to_process:
        catcuts = [cut_title_map[tok][0] for tok in name.split('_')]
        title = ', '.join([cut_title_map[tok][1] for tok in name.split('_')])
        wp = WidthProfile(name, title, get_sources(catcuts), 20, 500)
        wp.run()
        widthprofiles.append(wp)
    canvases.update()
Example #23
0
def main():
    '''
    Main entry point for execution
    '''
    global widthprofiles
    widthprofiles = []
    for name in names_to_process:
        catcuts = [cut_title_map[tok][0] for tok in name.split('_')]
        title = ', '.join([cut_title_map[tok][1] for tok in name.split('_')])
        wp = WidthProfile(name, title, get_sources(catcuts), 20, 500)
        wp.run()
        widthprofiles.append(wp)
    canvases.update()
Example #24
0
def make_plot(x, data, model, name=name, title=name):
    '''
    Plot x data with the overlayed fitted model.
    '''
    global plot
    x.setBins(30)
    plot = x.frame(roo.Range('plot'))
    plot.SetTitle(title)
    data.plotOn(plot)
    model.plotOn(plot, roo.Range('plot'), roo.NormRange('plot'))
    #model.paramOn(plot)
    canvases.next(name)
    plot.Draw()
    canvases.update()
def make_plot(x, data, model):
    '''
    Plot x data with the overlayed fitted model.
    '''
    # global plot
    if plot_range:
        plot = x.frame(roo.Range(*plot_range), roo.Title(data.GetName()))
    else:
        plot = x.frame(roo.Title(data.GetName()))
    data.plotOn(plot)
    model.plotOn(plot)
    model.paramOn(plot)
    canvases.next(data.GetName()).SetGrid()
    plot.Draw()
    canvases.update()
Example #26
0
 def draw_plot(self, frame):
     ## Draw the plot on a canvas
     #print "## Draw the plot on a canvas"
     canvases.wwidth = 600
     canvases.wheight = 600
     canvas = canvases.next(self.name)
     canvas.SetGrid()
     canvas.SetLeftMargin(0.15)
     canvas.SetTopMargin(0.1)
     frame.GetYaxis().SetTitleOffset(1.0)
     frame.GetYaxis().SetTitle('E_{#gamma} Resolution '
                               '#sigma_{eff}/E (%)')
     if self.yrange:
         frame.GetYaxis().SetRangeUser(*self.yrange)
     frame.Draw()
     canvas.RedrawAxis('g')
     canvases.update()
def test(max_entries=-1):
    '''
    Tests the PhotonIdCorrector class.
    '''
    global raw_data, target_data, corr, vplot
    varname = 'setab'
    option = 'skim10k'
    raw_name = 's12-zllm50-v7n'
    target_name = 'r12a-pho-j22-v1'
    raw_data = get_dataset(raw_name, varname, max_entries, option)
    target_data = get_dataset(target_name, varname, max_entries, option)
    xvar = raw_data.get().first()

    raw_data.SetTitle('Raw ' + raw_name.split('-')[0].capitalize())
    target_data.SetTitle('Raw ' + target_name.split('-')[0].capitalize())

    corr = PhotonIdCorrector(raw_data, target_data, rho=0.7)
    corr.SetName('_'.join([
        raw_name.split('-')[0], 'to',
        target_name.split('-')[0], varname, 'qqcorrector'
    ]))
    corr.SetTitle(' '.join([
        raw_name.split('-')[0].capitalize(), 'to',
        target_name.split('-')[0].capitalize(),
        xvar.GetTitle(), 'Q-Q Corrector'
    ]))

    plot = xvar.frame(roo.Title(raw_data.GetTitle()))
    raw_data.plotOn(plot)
    corr.xpdf.plotOn(plot)
    canvases.next(varname + '_' + raw_name.split('-')[0]).SetGrid()
    draw_and_append(plot)

    plot = xvar.frame(roo.Title(target_data.GetTitle()))
    target_data.plotOn(plot)
    corr.ypdf.plotOn(plot)
    canvases.next(varname + '_' + target_name.split('-')[0]).SetGrid()
    draw_and_append(plot)

    canvases.next(corr.GetName()).SetGrid()
    draw_and_append(corr.get_correction_plot())

    canvases.next(corr.GetName() + '_validation').SetGrid()
    draw_and_append(corr.get_validation_plot())

    canvases.update()
Example #28
0
    def run(self):
        ## P e r f o r m   c h i 2   f i t   t o   X + / - d x   a n d   Y + / - d Y   v a l u e s
        ## ---------------------------------------------------------------------------------------

        if self.noxerrors:
            ## Fit chi^2 using Y errors only
            self.fresult = self.f.chi2FitTo(self.dxy_noxerrors,
                                            roo.YVar(self.y), roo.Save(),
                                            roo.Minos())
        else:
            ## Fit chi^2 using X and Y errors
            self.fresult = self.f.chi2FitTo(self.dxy, roo.YVar(self.y), roo.Save(),
                                            roo.Minos())
        
        self.frame = self.make_plot()                
        self.draw_plot(self.frame)
        self.decorate_plot()
        canvases.update()
 def run(self):
     ## Set the range of deltaE to cover all the date plus a small margin.
     mi = self.modal_interval
     mi.setFraction(1.0)
     self.deltaE.setRange(mi.lowerBound() - 1, mi.upperBound() + 1)
     self.model = ParameterizedKeysPdf(
         self.name + "_model", self.name + "_model", self.deltaE, self.mode, self.effsigma, self.train_data, rho=2
     )
     mit = self.modal_interval_training
     mit.setFraction(0.99)
     fitrange = roo.Range(mit.lowerBound(), mit.upperBound())
     self.fit_result = self.model.fitTo(self.fit_data, roo.NumCPU(8), roo.Save(), fitrange)
     self.fit_result.SetName(self.name + "_fit_result")
     self.make_log_plot()
     self.make_zoom_plot()
     self.make_fixed_range_log_plot()
     self.make_fixed_range_zoom_plot()
     canvases.update()
def test(max_entries = -1):
    '''
    Tests the PhotonIdCorrector class.
    '''
    global raw_data, target_data, corr, vplot
    varname = 'setab'
    option = 'skim10k'
    raw_name = 's12-zllm50-v7n'
    target_name = 'r12a-pho-j22-v1'
    raw_data    = get_dataset(raw_name, varname, max_entries, option)
    target_data = get_dataset(target_name, varname, max_entries, option)
    xvar = raw_data.get().first()

    raw_data.SetTitle('Raw ' + raw_name.split('-')[0].capitalize())
    target_data.SetTitle('Raw ' + target_name.split('-')[0].capitalize())
    
    corr = PhotonIdCorrector(raw_data, target_data, rho=0.7)
    corr.SetName('_'.join([raw_name.split('-')[0], 'to',
                           target_name.split('-')[0], varname, 'qqcorrector']))
    corr.SetTitle(' '.join([raw_name.split('-')[0].capitalize(), 'to',
                            target_name.split('-')[0].capitalize(),
                            xvar.GetTitle(), 'Q-Q Corrector']))
    
    plot = xvar.frame(roo.Title(raw_data.GetTitle()))
    raw_data.plotOn(plot)
    corr.xpdf.plotOn(plot)
    canvases.next(varname + '_' + raw_name.split('-')[0]).SetGrid()
    draw_and_append(plot)

    plot = xvar.frame(roo.Title(target_data.GetTitle()))
    target_data.plotOn(plot)
    corr.ypdf.plotOn(plot)
    canvases.next(varname + '_' + target_name.split('-')[0]).SetGrid()
    draw_and_append(plot)
    
    canvases.next(corr.GetName()).SetGrid()
    draw_and_append(corr.get_correction_plot())
    
    canvases.next(corr.GetName() + '_validation').SetGrid()
    draw_and_append(corr.get_validation_plot())
    
    canvases.update()
Example #31
0
 def run(self):
     ## Set the range of deltaE to cover all the date plus a small margin.
     mi = self.modal_interval
     mi.setFraction(1.)
     self.deltaE.setRange(mi.lowerBound() - 1, mi.upperBound() + 1)
     self.model = ParameterizedKeysPdf(self.name + '_model',
                                       self.name + '_model',
                                       self.deltaE,
                                       self.mode,
                                       self.effsigma,
                                       self.train_data,
                                       rho=2)
     mit = self.modal_interval_training
     mit.setFraction(0.99)
     fitrange = roo.Range(mit.lowerBound(), mit.upperBound())
     self.fit_result = self.model.fitTo(self.fit_data, roo.NumCPU(8),
                                        roo.Save(), fitrange)
     self.fit_result.SetName(self.name + '_fit_result')
     self.make_log_plot()
     self.make_zoom_plot()
     self.make_fixed_range_log_plot()
     self.make_fixed_range_zoom_plot()
     canvases.update()
Example #32
0
def test_bootstrapping():
    '''
    Test the prescaling.
    '''
    import FWLite.Tools.canvases as canvases
    print '== Bootstrapping Test =='
    data = get_toy_data(5)
    resampler = Resampler(data)
    data_boot1 = resampler.bootstrap(name='boot1')
    data_boot2 = resampler.bootstrap(name='boot2')
    for d in [data, data_boot1, data_boot2]:
        d.Print()
        for i in range(d.numEntries()):
            print 'Entry', i, 
            d.get(i).Print('v')
    print
    
    data = get_toy_data(20)
    xvar = data.get()['x']
    
    resampler = Resampler(data)
    boot_mean_hist = ROOT.TH1F('mean', 'mean', 100, -1, 1)
    boot_rms_hist = ROOT.TH1F('RMS', 'RMS', 100, 0, 2)
    for i in range(1000):
        boot_mean_hist.Fill(resampler.bootstrap().mean(xvar))
        boot_rms_hist.Fill(resampler.bootstrap().rmsVar(xvar).getVal())
    
    canvases.next('boot_mean')
    boot_mean_hist.DrawCopy()
    canvases.next('boot_rms')
    boot_rms_hist.DrawCopy()
    canvases.update()

    data.meanVar(xvar).Print()
    print '  bootstrap error:', boot_mean_hist.GetRMS()
    data.rmsVar(xvar).Print()
    print '  bootstrap error:', boot_rms_hist.GetRMS()
Example #33
0
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to r9_scaling_fitter!'
    global data, model, x, fitresults
    init()
    data = getdata()
    ## Reduce data for debugging
    for source, dataset in data.items():
        # data[source] = dataset.reduce(roo.EventRange(0, 1000))
        pass
    x = variable
    old_precision = set_default_integrator_precision(1e-8, 1e-8)
    model = ParameterizedKeysPdf('model', 'model', x, mode, effsigma, data['z'],
                                 rho=0.7, forcerange=True)
    fix_model_parameters(model)
    # make_plot(x, data['z'], model.shape, name + 'MC_Shape', '')
    fitresults = {}
    for source in 'z data'.split():
        dataset = data[source]
        label = {'z': 'MC', 'data': 'Data'}[source]
        dataset.SetName(label)
        if source == 'data':
            # mode.setConstant(True)
            pass
        fitresult = model.fitTo(dataset, roo.SumW2Error(True),
                                roo.NumCPU(8), roo.Strategy(2), 
                                roo.Save(), roo.Range('fit'))
        fitresults[label] = fitresult
        make_plot(x, dataset, model, '_'.join([name,label]), '')
        decorate_plot(labels)
        # save_result(fitresult, label)

    canvases.update()
    print_report(fitresults)
    set_default_integrator_precision(*old_precision)
    print '\nExiting r9_scaling_fitter with success.'
    return(get_scaling(fitresults))
Example #34
0
def test():
    global w, qq12, qq21, plot
    w = ROOT.RooWorkspace('w', 'Q-Q Corrections test')
    g1 = w.factory('Gaussian::g1(x[-5, 5], m1[0], s1[1])')
    g2 = w.factory('Gamma::g2(y[0,20], beta[2], gamma[2], mu[0])')
    
    qq12 = QQCorrector(w.var('x'), g1, w.var('y'), g2, 1e-3)
    plot12 = w.var('x').frame()
    plot12.SetTitle('')
    plot12.GetXaxis().SetTitle('Raw x')
    plot12.GetYaxis().SetTitle('Corrected x')
    qq12.plotOn(plot12)
    canvases.next('qq12').SetGrid()
    plot12.Draw()
    
    qq21 = QQCorrector(w.var('y'), g2, w.var('x'), g1, 1e-3)
    plot21 = w.var('y').frame()
    plot21.SetTitle('')
    plot21.GetXaxis().SetTitle('Raw y')
    plot21.GetYaxis().SetTitle('Corrected y')
    qq21.plotOn(plot21, roo.LineColor(ROOT.kRed))
    canvases.next('qq21').SetGrid()
    plot21.Draw()

    func12 = qq12.get_hist_func()
    plot12f = w.var('x').frame()
    plot12f.SetTitle('')
    plot12f.GetXaxis().SetTitle('Raw x')
    plot12f.GetYaxis().SetTitle('Corrected x')
    qq12.plotOn(plot12f)
    func12.plotOn(plot12f, roo.LineColor(ROOT.kRed), 
                  roo.LineStyle(ROOT.kDashed))
    canvases.next('qq12f').SetGrid()
    plot12f.Draw()
    
    canvases.update()
Example #35
0
            if hasattr(cfg, 'binning'):
                varexp += '(' + cfg.binning + ')'
        else:
            varexp = expression + '>>+' + cfg.name
        tree.Draw(varexp, selection, option)
    hist[cfg.name] = ihist = ROOT.gDirectory.Get(cfg.name)
    binwidth = ihist.GetBinWidth(1)
    title_item = lambda e, c: c and '%s {%s}' % (e, c) or e
    title = ', '.join(
        [title_item(e, c) for e, c in zip(cfg.expressions, cfg.selections)])
    xtitle = cfg.title
    ytitle = 'Events / %g' % binwidth
    if hasattr(cfg, 'unit') and cfg.unit:
        xtitle += ' (' + cfg.unit + ')'
        ytitle += ' ' + cfg.unit
    ihist.SetTitle(title)
    ihist.GetXaxis().SetTitle(xtitle)
    ihist.GetYaxis().SetTitle(ytitle)
    ihist.Draw()
    canvases.update()

#canvases.make_pdf_from_eps()
#outfile.Write()
#outfile.Close()

#formula = ROOT.TTreeFormula('mass', 'mass', tree)

#for i in range(10):
#tree.GetEntry(i)
#print i, formula.EvalInstance()
Example #36
0
def main():
    ntoys=20
    ndata=1000
    randseed=15
    outputname = 'response_fits_ntoys1k_ndata1k_rho2.7_seed15.root'
    # outputname = 'response_fits_test.root'
    ROOT.RooRandom.randomGenerator().SetSeed(randseed)
    sw = ROOT.TStopwatch()
    global fitters, workspaces
    fitters, workspaces = [], []
    global histos
    histos = {
        'hsm_mode_val': ROOT.TH1F('hsm_mode_val', 'hsm_mode_val', 100, -1, 1),
        'hsm_mode_err': ROOT.TH1F('hsm_mode_err', 'hsm_mode_err', 100, 0.01, 0.5),
        'hsm_sigma_val': ROOT.TH1F('hsm_sigma_val', 'hsm_sigma_val', 100, 0.8, 1.2),
        'hsm_sigma_err': ROOT.TH1F('hsm_sigma_err', 'hsm_sigma_err', 100, 0.01, 0.05),
        'gauss_mode_val': ROOT.TH1F('gauss_mode_val', 'gauss_mode_val', 100, -1, 1),
        'gauss_mode_err': ROOT.TH1F('gauss_mode_err', 'gauss_mode_err', 100, 0.01, 0.05),
        'gauss_sigma_val': ROOT.TH1F('gauss_sigma_val', 'gauss_sigma_val', 100, 0.8, 1.2),
        'gauss_sigma_err': ROOT.TH1F('gauss_sigma_err', 'gauss_sigma_err', 100, 0.01, 0.05),
        }
    
    #set_default_integrator_precision(1e-8, 1e-8)
    
    sw.Start()
    for i in range(ntoys):
        w = ROOT.RooWorkspace('test')
        model = w.factory('Gaussian::model(x[-5,5], m[0,-5,5], s[1,0.01,10])')
        x = w.var('x')
        data = model.generate(ROOT.RooArgSet(x), ndata)
        model.fitTo(data)
        workspaces.append(w)        
        
        fitter = HSMFitter(data, nboot=500)    
        fitter.dofit()
        # fitter.makeplot()
        fitters.append(fitter)
        histos['hsm_mode_val'].Fill(fitter.mode.getVal())
        histos['hsm_mode_err'].Fill(fitter.mode.getError())
        histos['hsm_sigma_val'].Fill(fitter.effsigma.getVal())
        histos['hsm_sigma_err'].Fill(fitter.effsigma.getError())
        histos['gauss_mode_val'].Fill(w.var('m').getVal())
        histos['gauss_mode_err'].Fill(w.var('m').getError())
        histos['gauss_sigma_val'].Fill(w.var('s').getVal())
        histos['gauss_sigma_err'].Fill(w.var('s').getError())
        

    sw.Stop()
    # print 'Gaussian fits:'
    # for w in workspaces:
    #     print '% 5.3f +/- %5.3f   %5.3f +/- %5.3f' % (
    #       w.var('m').getVal(), w.var('m').getError(), w.var('s').getVal(),
    #       w.var('s').getError()
    #       )
    
    # print 'KEYS fits:'
    # for f in fitters:
    #     print '% 5.3f +/- %5.3f   %5.3f +/- %5.3f' % (
    #       f.mode.getVal(), f.mode.getError(), f.effsigma.getVal(),
    #       f.effsigma.getError()
    #       )
    names = histos.keys()
    names.sort()
    for n in names:
        canvases.next(n)
        histos[n].Draw()
    canvases.update()
    print 'Real time: %.1f s' % sw.RealTime()

    global output
    output = ROOT.TFile(outputname, 'recreate')
    for h in histos.values():
        h.SetDirectory(output)
    output.Write()
Example #37
0
def test():
    '''
    Tests the RooRhoKeysPdf class.
    '''
    import FWLite.Tools.canvases as canvases
    import FWLite.Tools.cmsstyle as cmsstyle

    ROOT.RooRandom.randomGenerator().SetSeed(2)
    global gnlls, hrhoval, hrhoerr
    hrhoval = ROOT.TH1F('hrhoval', 'hrhoval', 100, 0, 5)
    hrhoerr = ROOT.TH1F('hrhoerr', 'hrhoerr', 100, 0, 1)
    gnlls = []
    for itoy in range(1):
        global w
        w = ROOT.RooWorkspace('w', 'w')
        # model = w.factory('Gaussian::model(x[-50, 50], mean[0], sigma[1])')
        model = w.factory('BreitWigner::model(x[-5, 5], mean[0], sigma[1])')
        x = w.var('x')
        oset = ROOT.RooArgSet(x)
        data = model.generate(oset, 1000)
        w.Import(data)
        # rho = w.factory('rho[1, 0, 100]')
        # testpdf = RooRhoKeysPdf('testpdf', 'testpdf', x, rho, data)
        # w.Import(testpdf)
        testpdf = w.factory('RooRhoKeysPdf::testpdf(x, rho[1, 0, 100], modelData)')
        rho = w.var('rho')
        plot = x.frame()
        data.plotOn(plot)
        model.plotOn(plot)
        testpdf.plotOn(plot, roo.LineColor(ROOT.kRed))

        rho.setVal(2)
        testpdf.LoadDataSet(data)
        testpdf.plotOn(plot, roo.LineColor(ROOT.kGreen))

        rho.setVal(3)
        testpdf.LoadDataSet(data)
        testpdf.plotOn(plot, roo.LineColor(ROOT.kBlack))

        canvases.next('RooRhoKeysPdf_Test%d' % itoy)
        plot.Draw()
        canvases.update()

        resampler = Resampler(data)
        data0 = resampler.prescale(2, [0], 'data0')
        data1 = resampler.prescale(2, [1], 'data1')
        w.Import(data0)
        w.Import(data1)
        testpdf0 = w.factory('RooRhoKeysPdf::testpdf0(x, rho, data0)')
        testpdf1 = w.factory('RooRhoKeysPdf::testpdf1(x, rho, data1)')

        gnll = ROOT.TGraph()
        for rhoval in [0.5 + 0.05 * i for i in range(50)]:
            rho.setVal(rhoval)
            testpdf0.LoadDataSet(data0)
            testpdf1.LoadDataSet(data1)
            nll = 0
            nll += testpdf0.createNLL(data1).getVal()
            nll += testpdf1.createNLL(data0).getVal()
            # print rhoval, nll
            gnll.SetPoint(gnll.GetN(), rhoval, nll)

        locmin = ROOT.TMath.LocMin(gnll.GetN(), gnll.GetY())
        xmin = gnll.GetX()[max(locmin-5, 0)]
        xmax = gnll.GetX()[min(locmin+5, gnll.GetN()-1)]
        fres = gnll.Fit('pol2', 's', '', xmin, xmax)
        p1 = fres.Get().GetParams()[1]
        p2 = fres.Get().GetParams()[2]
        rhoval =  - 0.5 * p1 / p2
        rhoerr = 1/ROOT.TMath.Sqrt(2 * p2)
        hrhoval.Fill(rhoval)
        hrhoerr.Fill(rhoerr)
        
        canvases.next('gnll%d' % itoy)
        gnll.Draw('ap')
        gnll.GetXaxis().SetTitle('#rho')
        gnll.GetYaxis().SetTitle('- log L')
        gnlls.append(gnll)
    canvases.next('rhoerr')
    hrhoerr.Draw()
    canvases.next('rhoval')
    hrhoval.Draw()
    canvases.update()

    from FWLite.Tools.modalinterval import ModalInterval
    global mi
    mi = ModalInterval(w.data('data0'))
    print mi.halfSampleMode()
Example #38
0
def main():
    ntoys=100
    ndata=1000
    rho=1.5
    randseed=19
    outputname = 'response_fits_ntoys1k_ndata100_rho1.5_seed19.root'
    # outputname = 'response_fits_test.root'
    ROOT.RooRandom.randomGenerator().SetSeed(randseed)
    sw = ROOT.TStopwatch()
    global fitters, workspaces
    fitters, workspaces = [], []
    global histos
    histos = {
        'keys_mode_val': ROOT.TH1F('keys_mode_val', 'keys_mode_val', 100, -1, 1),
        'keys_mode_err': ROOT.TH1F('keys_mode_err', 'keys_mode_err', 100, 0.01, 0.1),
        'keys_sigma_val': ROOT.TH1F('keys_sigma_val', 'keys_sigma_val', 100, 0.8, 1.2),
        'keys_sigma_err': ROOT.TH1F('keys_sigma_err', 'keys_sigma_err', 100, 0.01, 0.1),
        'keys_sigma_shape': ROOT.TH1F('keys_sigma_shape', 'keys_sigma_shape', 150, 0.5, 2.0),
        'gauss_mode_val': ROOT.TH1F('gauss_mode_val', 'gauss_mode_val', 100, -1, 1),
        'gauss_mode_err': ROOT.TH1F('gauss_mode_err', 'gauss_mode_err', 100, 0.01, 0.1),
        'gauss_sigma_val': ROOT.TH1F('gauss_sigma_val', 'gauss_sigma_val', 100, 0.8, 1.2),
        'gauss_sigma_err': ROOT.TH1F('gauss_sigma_err', 'gauss_sigma_err', 100, 0.01, 0.1),
        }
    
    set_default_integrator_precision(1e-8, 1e-8)
    
    sw.Start()
    for i in range(ntoys):
        w = ROOT.RooWorkspace('test')
        model = w.factory('Gaussian::model(x[-5,5], m[0,-5,5], s[1,0.01,10])')
        x = w.var('x')
        data = model.generate(ROOT.RooArgSet(x), ndata)
        model.fitTo(data)
        workspaces.append(w)        
        
        fitter = KeysResponseFitter(data, rho=rho, option='split7')    
        fitter.dofit()
        # fitter.makeplot()
        fitters.append(fitter)
        histos['keys_mode_val'].Fill(fitter.mode.getVal())
        histos['keys_mode_err'].Fill(fitter.mode.getError())
        histos['keys_sigma_val'].Fill(fitter.effsigma.getVal())
        histos['keys_sigma_err'].Fill(fitter.effsigma.getError())
        if fitter.option == 'monolithic':
            histos['keys_sigma_shape'].Fill(fitter.model.shapewidth)
        elif fitter.option == 'split':
            for model in fitter.submodel:
                histos['keys_sigma_shape'].Fill(model.shapewidth)
        histos['gauss_mode_val'].Fill(w.var('m').getVal())
        histos['gauss_mode_err'].Fill(w.var('m').getError())
        histos['gauss_sigma_val'].Fill(w.var('s').getVal())
        histos['gauss_sigma_err'].Fill(w.var('s').getError())
        

    sw.Stop()
    # print 'Gaussian fits:'
    # for w in workspaces:
    #     print '% 5.3f +/- %5.3f   %5.3f +/- %5.3f' % (
    #       w.var('m').getVal(), w.var('m').getError(), w.var('s').getVal(),
    #       w.var('s').getError()
    #       )
    
    # print 'KEYS fits:'
    # for f in fitters:
    #     print '% 5.3f +/- %5.3f   %5.3f +/- %5.3f' % (
    #       f.mode.getVal(), f.mode.getError(), f.effsigma.getVal(),
    #       f.effsigma.getError()
    #       )
    names = histos.keys()
    names.sort()
    for n in names:
        canvases.next(n)
        histos[n].DrawCopy()
    canvases.update()
    print 'Real time: %.1f s' % sw.RealTime()

    output = ROOT.TFile(outputname, 'recreate')
    for h in histos.values():
        h.SetDirectory(output)
    output.Write()
    output.Close()
Example #39
0
def main():
    ntoys = 20
    ndata = 1000
    randseed = 15
    outputname = 'response_fits_ntoys1k_ndata1k_rho2.7_seed15.root'
    # outputname = 'response_fits_test.root'
    ROOT.RooRandom.randomGenerator().SetSeed(randseed)
    sw = ROOT.TStopwatch()
    global fitters, workspaces
    fitters, workspaces = [], []
    global histos
    histos = {
        'hsm_mode_val':
        ROOT.TH1F('hsm_mode_val', 'hsm_mode_val', 100, -1, 1),
        'hsm_mode_err':
        ROOT.TH1F('hsm_mode_err', 'hsm_mode_err', 100, 0.01, 0.5),
        'hsm_sigma_val':
        ROOT.TH1F('hsm_sigma_val', 'hsm_sigma_val', 100, 0.8, 1.2),
        'hsm_sigma_err':
        ROOT.TH1F('hsm_sigma_err', 'hsm_sigma_err', 100, 0.01, 0.05),
        'gauss_mode_val':
        ROOT.TH1F('gauss_mode_val', 'gauss_mode_val', 100, -1, 1),
        'gauss_mode_err':
        ROOT.TH1F('gauss_mode_err', 'gauss_mode_err', 100, 0.01, 0.05),
        'gauss_sigma_val':
        ROOT.TH1F('gauss_sigma_val', 'gauss_sigma_val', 100, 0.8, 1.2),
        'gauss_sigma_err':
        ROOT.TH1F('gauss_sigma_err', 'gauss_sigma_err', 100, 0.01, 0.05),
    }

    #set_default_integrator_precision(1e-8, 1e-8)

    sw.Start()
    for i in range(ntoys):
        w = ROOT.RooWorkspace('test')
        model = w.factory('Gaussian::model(x[-5,5], m[0,-5,5], s[1,0.01,10])')
        x = w.var('x')
        data = model.generate(ROOT.RooArgSet(x), ndata)
        model.fitTo(data)
        workspaces.append(w)

        fitter = HSMFitter(data, nboot=500)
        fitter.dofit()
        # fitter.makeplot()
        fitters.append(fitter)
        histos['hsm_mode_val'].Fill(fitter.mode.getVal())
        histos['hsm_mode_err'].Fill(fitter.mode.getError())
        histos['hsm_sigma_val'].Fill(fitter.effsigma.getVal())
        histos['hsm_sigma_err'].Fill(fitter.effsigma.getError())
        histos['gauss_mode_val'].Fill(w.var('m').getVal())
        histos['gauss_mode_err'].Fill(w.var('m').getError())
        histos['gauss_sigma_val'].Fill(w.var('s').getVal())
        histos['gauss_sigma_err'].Fill(w.var('s').getError())

    sw.Stop()
    # print 'Gaussian fits:'
    # for w in workspaces:
    #     print '% 5.3f +/- %5.3f   %5.3f +/- %5.3f' % (
    #       w.var('m').getVal(), w.var('m').getError(), w.var('s').getVal(),
    #       w.var('s').getError()
    #       )

    # print 'KEYS fits:'
    # for f in fitters:
    #     print '% 5.3f +/- %5.3f   %5.3f +/- %5.3f' % (
    #       f.mode.getVal(), f.mode.getError(), f.effsigma.getVal(),
    #       f.effsigma.getError()
    #       )
    names = histos.keys()
    names.sort()
    for n in names:
        canvases.next(n)
        histos[n].Draw()
    canvases.update()
    print 'Real time: %.1f s' % sw.RealTime()

    global output
    output = ROOT.TFile(outputname, 'recreate')
    for h in histos.values():
        h.SetDirectory(output)
    output.Write()
Example #40
0
def main():
    '''
    Main entry point of execution.
    '''
    ## C r e a t e   d a t a s e t   w i t h   X   a n d   Y   v a l u e s
    ## -------------------------------------------------------------------

    ## Make weighted XY dataset with asymmetric errors stored
    ## The StoreError() argument is essential as it makes
    ## the dataset store the error in addition to the values
    ## of the observables. If errors on one or more observables
    ## are asymmetric, one can store the asymmetric error
    ## using the StoreAsymError() argument
    x = ROOT.RooRealVar('x', 'x', -11, 11)
    y = ROOT.RooRealVar('y', 'y', -10, 200)
    dxy = ROOT.RooDataSet('dxy', 'dxy', ROOT.RooArgSet(x, y),
                          roo.StoreError(ROOT.RooArgSet(x, y)))

    ## Fill an example dataset with X,err(X),Y,err(Y) values
    for i in range(11):

        ## Set X value and error
        x.setVal(-10 + 2 * i)
        if i < 5:
            x.setError(0.5 / 1.)
        else:
            x.setError(1.0 / 1.)

        ## Set Y value and error
        y.setVal(x.getVal() * x.getVal() + 4 * math.fabs(ROOT.gRandom.Gaus()))
        y.setError(math.sqrt(y.getVal()))

        dxy.add(ROOT.RooArgSet(x, y))
    ## End of loop over dxy entries

    ## P e r f o r m   c h i 2   f i t   t o   X + / - d x   a n d   Y + / - d Y   v a l u e s
    ## ---------------------------------------------------------------------------------------

    ## Make fit function
    a = ROOT.RooRealVar('a', 'a', 0.0, -10, 10)
    b = ROOT.RooRealVar('b', 'b', 0, -100, 100)
    f = ROOT.RooPolyVar('f', 'f', x, ROOT.RooArgList(b, a, roo.RooConst(1)))

    ## Plot dataset in X-Y interpretation
    frame = x.frame(
        roo.Title('#chi^{2} fit of function set of '
                  '(X#pmdX,Y#pmdY) values'))
    dxy.plotOnXY(frame, roo.YVar(y))

    ## Fit chi^2 using X and Y errors
    f.chi2FitTo(dxy, roo.YVar(y))

    ## Overlay fitted function
    f.plotOn(frame)

    ## Alternative: fit chi^2 integrating f(x) over ranges defined by X errors,
    ## rather than taking point at center of bin
    f.chi2FitTo(dxy, roo.YVar(y), roo.Integrate(True))

    ## Overlay alternate fit result
    f.plotOn(frame, roo.LineStyle(ROOT.kDashed), roo.LineColor(ROOT.kRed))

    ## Draw the plot on a canvas
    canvases.wwidth = 600
    canvases.wheight = 600
    canvases.next('rf609_xychi2fit')
    ROOT.gPad.SetLeftMargin(0.15)
    ROOT.gPad.SetTopMargin(0.1)
    frame.GetYaxis().SetTitleOffset(1.0)
    frame.Draw()
    canvases.update()
Example #41
0
        '''Draw a legend given the items.'''
        legend = Legend(items.keys(), items.values())
        for entry in list(legend.GetListOfPrimitives()):
            if entry.GetLabel() != 'Data':
                entry.SetOption('l')
        legend.Draw()
        self.legend = legend
    ## End of Fitter.draw_legend().
    

    #___________________________________________________________________________
    def write_outputs(self):
        '''Write the workspace and canvases to files.'''
        pass
    ## End of Fitter.write_outputs().
    
## End of Fitter



#===============================================================================
if __name__ == '__main__':
    print "Welcome to the test of the rescaling fitter!"
    fitter = Fitter()
    fitter.run()
    fitter.w.Print()
    canvases.update()
    print "Exiting rescaling fitter test with success."
    import user
    
Example #42
0
def main():
    '''
    Main entry point of execution.
    '''
    ## C r e a t e   d a t a s e t   w i t h   X   a n d   Y   v a l u e s
    ## -------------------------------------------------------------------

    ## Make weighted XY dataset with asymmetric errors stored
    ## The StoreError() argument is essential as it makes
    ## the dataset store the error in addition to the values
    ## of the observables. If errors on one or more observables
    ## are asymmetric, one can store the asymmetric error
    ## using the StoreAsymError() argument
    x = ROOT.RooRealVar('x', 'x', -11, 11)
    y = ROOT.RooRealVar('y', 'y', -10, 200)
    dxy = ROOT.RooDataSet('dxy', 'dxy', ROOT.RooArgSet(x, y),
                          roo.StoreError(ROOT.RooArgSet(x, y)))
    
    ## Fill an example dataset with X,err(X),Y,err(Y) values
    for i in range(11):
      
        ## Set X value and error
        x.setVal(-10 + 2 * i)
        if i < 5:
            x.setError(0.5 / 1.)
        else:
            x.setError(1.0 / 1. )
          
        ## Set Y value and error
        y.setVal(x.getVal() * x.getVal() + 4 * math.fabs(ROOT.gRandom.Gaus()))
        y.setError(math.sqrt(y.getVal()))
        
        dxy.add(ROOT.RooArgSet(x, y))
    ## End of loop over dxy entries

    
    ## P e r f o r m   c h i 2   f i t   t o   X + / - d x   a n d   Y + / - d Y   v a l u e s
    ## ---------------------------------------------------------------------------------------

    ## Make fit function
    a = ROOT.RooRealVar('a', 'a', 0.0, -10, 10)
    b = ROOT.RooRealVar('b', 'b', 0, -100, 100)
    f = ROOT.RooPolyVar('f', 'f', x, ROOT.RooArgList(b, a, roo.RooConst(1)))
    
    ## Plot dataset in X-Y interpretation
    frame = x.frame(roo.Title('#chi^{2} fit of function set of '
                              '(X#pmdX,Y#pmdY) values'))
    dxy.plotOnXY(frame, roo.YVar(y))

    ## Fit chi^2 using X and Y errors
    f.chi2FitTo(dxy, roo.YVar(y))
    
    ## Overlay fitted function
    f.plotOn(frame)
    
    ## Alternative: fit chi^2 integrating f(x) over ranges defined by X errors,
    ## rather than taking point at center of bin
    f.chi2FitTo(dxy, roo.YVar(y), roo.Integrate(True))
    
    ## Overlay alternate fit result
    f.plotOn(frame, roo.LineStyle(ROOT.kDashed), roo.LineColor(ROOT.kRed))
    
    
    ## Draw the plot on a canvas
    canvases.wwidth = 600
    canvases.wheight = 600
    canvases.next('rf609_xychi2fit')
    ROOT.gPad.SetLeftMargin(0.15)
    ROOT.gPad.SetTopMargin(0.1)
    frame.GetYaxis().SetTitleOffset(1.0)
    frame.Draw()
    canvases.update()