Exemple #1
0
    def make_fixed_range_log_plot(self):
        c1 = canvases.next(self.name + '_log_fixedrange')
        c1.SetGrid()
        c1.SetLogy()
        self.canvases.append(c1)

        ## Use the ModalInterval class to display the shortest range containing
        ## 100% of all the entries.
        mi = self.modal_interval
        mi.setFraction(1)

        fullrange = (mi.lowerBound(), mi.upperBound())
        plot = self.deltaE.frame(roo.Range(*self.fixed_range_log))
        plot.SetTitle(', '.join(self.labels))
        self.fit_data.plotOn(plot, roo.MarkerColor(self.color_data),
                             roo.MarkerStyle(self.marker_style),
                             roo.LineColor(self.color_data))
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        if fullrange[0] + fullrange[1] > 0:
            layout = (0.6, 0.9, 0.87)
        else:
            layout = (0.2, 0.5, 0.87)
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)),
                           roo.Layout(*layout))
        ## Fine tune the y-axis range so that we see all the events.
        plot.SetMinimum(0.5)
        ## Add a larger top margin to the y-axis range
        plot.SetMaximum(pow(plot.GetMaximum(), 1.1))
        plot.Draw()
        self.plots.append(plot)
Exemple #2
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!'
Exemple #3
0
 def plot_x(self):
     '''
     Create a canvas with x data overlayed with its PDF.
     '''
     self.xplot = self.xvar.frame()
     self.data.plotOn(self.xplot, roo.LineColor(self.color),
                      roo.MarkerColor(self.color))
     self.xpdf.plotOn(self.xplot, roo.LineColor(self.color))
     canvases.next(self.xvar.GetTitle()).SetGrid()
     self.xplot.SetTitle('')
     self.xplot.Draw()
Exemple #4
0
def make_uuplot(ux, uy):
    plot = ux.uniform_var.frame()
    ux.data.plotOn(plot, roo.LineColor(ux.color), roo.MarkerColor(ux.color))
    ux.uniform_pdf.plotOn(plot, roo.LineColor(ux.color), roo.Range(0, 1))
    uy.uniform_pdf.plotOn(plot, roo.LineColor(uy.color), roo.Range(0, 1),
                          roo.LineStyle(ROOT.kDashed))
    uy.data.plotOn(plot, roo.LineColor(uy.color), roo.MarkerColor(uy.color),
                   roo.MarkerStyle(4))
    canvases.next('uu')
    plot.Draw()
    return plot
Exemple #5
0
 def make_fixed_range_zoom_plot(self):
     c1 = canvases.next(self.name + '_lin_fixedrange')
     c1.SetGrid()
     self.canvases.append(c1)
     plot = self.deltaE.frame(roo.Range(*self.fixed_range_zoom))
     plot.SetTitle(', '.join(self.labels))
     self.fit_data.plotOn(plot, roo.MarkerColor(self.color_data),
                          roo.MarkerStyle(self.marker_style),
                          roo.LineColor(self.color_data))
     self.model.plotOn(plot, roo.LineColor(self.color_model))
     self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)),
                        roo.Layout(0.2, 0.52, 0.87))
     plot.Draw()
     self.plots.append(plot)
Exemple #6
0
 def plot_uniform(self):
     '''
     Create a canvas with transformed x data overlayed with a uniform pdf.
     '''
     self.uplot = self.uniform_var.frame()
     self.data.plotOn(self.uplot, roo.LineColor(self.color),
                      roo.MarkerColor(self.color))
     self.uniform_pdf.plotOn(self.uplot, roo.Range(0, 1),
                             roo.LineColor(self.color))
     cname = '_'.join(
         [self.uniform_var.GetName(), 'of',
          self.xvar.GetName()])
     canvases.next(cname).SetGrid()
     self.uplot.SetTitle('')
     self.uplot.Draw()
Exemple #7
0
def make_qqplots(ux, uy):
    xyplot = uy.xvar.frame()
    xyplot.SetTitle('')
    xycorr = QQCorrector(ux.xvar, ux.xpdf, uy.xvar, uy.xpdf)
    xycorr.SetName('y')
    ux.data.addColumn(xycorr)
    uy.data.plotOn(xyplot, roo.LineColor(uy.color), roo.MarkerColor(uy.color))
    uy.xpdf.plotOn(xyplot, roo.LineColor(uy.color))
    ux.data.plotOn(xyplot, roo.LineColor(ux.color), roo.MarkerColor(ux.color))

    yxplot = ux.xvar.frame()
    yxplot.SetTitle('')
    yxcorr = QQCorrector(uy.xvar, uy.xpdf, ux.xvar, ux.xpdf)
    yxcorr.SetName('x')
    uy.data.addColumn(yxcorr)
    ux.data.plotOn(yxplot, roo.LineColor(ux.color), roo.MarkerColor(ux.color))
    ux.xpdf.plotOn(yxplot, roo.LineColor(ux.color))
    uy.data.plotOn(yxplot, roo.LineColor(uy.color), roo.MarkerColor(uy.color))

    canvases.next('qqxy')
    xyplot.Draw()

    canvases.next('qqyx')
    yxplot.Draw()

    return xyplot, yxplot
Exemple #8
0
def test_downsampling():
    '''
    Tests the downsampling.
    '''
    import FWLite.Tools.canvases as canvases
    print '== Downsampling Test =='
    big_data = get_toy_data(1000)
    resampler = Resampler(big_data)
    small_data = resampler.downsample(50)
    keep(big_data, small_data)
    big_data.Print()
    small_data.Print()

    xvar = big_data.get()['x']
    xvar.setBins(20)
    plot = xvar.frame()
    big_data.plotOn(plot, roo.MarkerColor(ROOT.kRed), roo.LineColor(ROOT.kRed))
    small_data.plotOn(plot, roo.MarkerStyle(24))
    canvases.next('downsampling_test')
    plot.Draw()
    keep(plot)
Exemple #9
0
def plot_model_and_change_parameter_values(w):
    '''
    void plot_model_and_change_parameter_values(w)
    
    Plots model `gaus' and changes the parameter values.
    '''
    ## Construct plot frame in 'x'
    xframe = w.var('x').frame(roo.Title('Gaussian PDF'))

    ## Plot gauss in frame (i.e. in x).
    w.pdf('gauss').plotOn(xframe)

    ## Change the value of sigma to 3.
    w.var('sigma').setVal(3)

    ## Plot gauss in frame (i.e. in x) and draw frame on canvases.
    w.pdf('gauss').plotOn(xframe, roo.LineColor(ROOT.kRed))

    ## Create a canvas and draw the plot frame on it.
    canvases.next('Gaussian_PDF')
    xframe.Draw()
Exemple #10
0
graph = qq12.get_interpolation_graph(granularity=20)
graph.SetName('qqcorr_graph')
graph.Write()
qqfile.Close()

#______________________________________________________________________________
## Read the corrector from a file and plot on top of the original
qqfile = ROOT.TFile.Open('test_qqcorrector.root')
qqfile.ls()
workspace = qqfile.Get('qqcorr')
workspace.Print()
func12 = workspace.function('qqcorr')
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))
graph = qqfile.Get('qqcorr_graph')
canvases.next('qq12f').SetGrid()
plot12f.Draw()
graph.Draw('p')

canvases.update()
#qqfile.Close()

#______________________________________________________________________________
if __name__ == '__main__':
    import user
Exemple #11
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()
Exemple #12
0
 def make_tplot(self):
     '''
     Create the plot of the transform
     '''
     self.tplot = self.xvar.frame()
     self.xcdf.plotOn(self.tplot, roo.LineColor(self.color))
Exemple #13
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()
Exemple #14
0
def color_args(color):
    '''
    Returns the two RooFit commands to set marker and line color to the 
    givne one.
    '''
    return roo.MarkerColor(color), roo.LineColor(color)