Ejemplo n.º 1
0
def run_example(save=True):
    ##########################################
    # first define some objects to play with #
    ##########################################

    random = TRandom(getpid())

    # Create a histogram to play with
    bin_edges = array("d", [2, 3, 5, 7, 11, 13, 17, 19])
    hist_1 = TH1F("some_histogram_1", "", len(bin_edges) - 1, bin_edges)
    hist_2 = TH1F("some_histogram_2", "", len(bin_edges) - 1, bin_edges)
    # Fill histogram from a random number generation
    for i in range(len(bin_edges) + 1):
        hist_1.SetBinContent(i + 1, random.Poisson(42))
        hist_1.SetBinError(i + 1, random.Gaus(10))
        hist_2.SetBinContent(i + 1, random.Poisson(42))
        hist_2.SetBinError(i + 1, random.Gaus(10))

    ###################################
    # The actual plotting starts here #
    ###################################

    # Generate 3 styles with constant markerstyle
    styles = generate_styles(StyleObject1D, 2, markerstyle=34)

    # Create a figure with one cell (1 column and one row)
    figure = ROOTFigure(1, 1, size=(400, 600), row_margin=0.05, column_margin=0.1)

    # plot is actually defined automatically now since it's a 1x1 grid
    # hence the following should be skipped
    # plot = figure.define_plot(0, 0)

    # so one can add an object via the figure...
    figure.add_object(hist_1, style=styles[0], label="MyHist1")

    # ...or obtain the current single plot cell
    plot = figure.change_plot()

    # ...and add another object via
    plot.add_object(hist_2, style=styles[1], label="MyHist2")

    # Set some axis limits and titles explicitly
    plot.axes("x", title="x_title")
    plot.axes("y", limits=[0, 120], title="y_title")

    figure.create()
    if save:
        figure.save("example_one_plot.eps")

    return True
Ejemplo n.º 2
0
def run_example(save=True):
    ##########################################
    # first define some objects to play with #
    ##########################################

    random = TRandom(getpid())

    # Create a histogram to play with
    bin_edges = array("d", [2, 3, 5, 7, 11, 13, 17, 19])
    hist_1 = TH2F("some_histogram_1", "", len(bin_edges) - 1, bin_edges, len(bin_edges) - 1, bin_edges)
    #hist_2 = TH1F("some_histogram_2", "", len(bin_edges) - 1, bin_edges)
    # Fill histogram from a random number generation
    for i in range(len(bin_edges) + 1):
        for j in range(len(bin_edges) + 1):
            hist_1.SetBinContent(i + 1, j + 1, random.Poisson(42))
            hist_1.SetBinError(i + 1, j + 1, random.Gaus(10))
    hist_2 = hist_1.ProjectionX()

    ###################################
    # The actual plotting starts here #
    ###################################

    # Generate 3 styles with constant markerstyle
    style = StyleObject1D()
    style.draw_options = "colz"

    style_2 = StyleObject1D()

    # Create a figure with one cell (1 column and one row)
    figure = ROOTFigure(1, 2, size=(400, 600), row_margin=((0.06, 0.005), (0.005, 0.05)), column_margin=(0.11, 0.1))

    # plot is actually defined automatically now since it's a 1x1 grid
    # hence the following should be skipped
    plot_1 = figure.define_plot()
    plot_1.axes("x", title="x_title", title_offset=2.5)
    plot_1.axes("y", title="y_title_1", title_offset=1.9)

    # so one can add an object via the figure...
    plot_1.add_object(hist_1, style=style)

    plot = figure.define_plot(share_x=plot_1)
    plot.add_object(hist_2, style=style_2)
    plot.axes("y", title="ProjectionX", title_offset=1.9)


    figure.create()
    if save:
        figure.save("test_2D_plot.eps")

    return True
Ejemplo n.º 3
0
pvalHist = TH1D("pval", "pval", 100000, 0, 1.0)
allpvalHist = TH1D("allpval", "allpval", 100000, 0, 1.0)
pvalCdfHist = TH1D("pvalCdf", "pvalCdf", 100000, 0, 1.0)

lastTime = time.time()
for runnum in xrange(0, n):
    newTime = time.time()
    if newTime - lastTime > 10:
        print runnum
        lastTime = newTime
    #print runnum
    data.Reset()
    massArr = array.array('d')
    massWindowArr = array.array('d')
    candArr = array.array('d')
    data.FillRandom("eventpdf", rand.Poisson(nEvents))
    for i in range(0, n_massbins):
        mass = minmass + i * (maxmass - minmass) / (n_massbins - 1)
        massArr.append(mass)
        massWindowArr.append(1.4 * mres.Eval(mass))
        minbin = data.GetXaxis().FindBin(mass - 1.4 * mres.Eval(mass))
        maxbin = data.GetXaxis().FindBin(mass + 1.4 * mres.Eval(mass))
        candArr.append(data.Integral(minbin, maxbin))

    minPval = 1.0
    nbkg = 0.5
    for i in xrange(0, len(massArr)):
        if fitBackground:
            mass = massArr[i]
            sigMassArr = array.array('d')
            sigCandArr = array.array('d')
def run_example(save=True):
    ##########################################
    # first define some objects to play with #
    ##########################################

    random = TRandom(getpid())

    # Create a histogram to play with
    bin_edges = array("d", [2, 3, 5, 7, 11, 13, 17, 19])
    hist_1 = TH1F("some_histogram_1", "", len(bin_edges) - 1, bin_edges)
    hist_2 = TH1F("some_histogram_2", "", len(bin_edges) - 1, bin_edges)
    # Fill histogram from a random number generation
    for i in range(len(bin_edges) + 1):
        hist_1.SetBinContent(i + 1, random.Poisson(42))
        hist_1.SetBinError(i + 1, random.Gaus(10))
        hist_2.SetBinContent(i + 1, random.Poisson(42))
        hist_2.SetBinError(i + 1, random.Gaus(10))

    ###################################
    # The actual plotting starts here #
    ###################################

    # Generate 5 styles, keep markerstyle the same for all
    styles = generate_styles(StyleObject1D, 5, markerstyle=21)

    # the overall figure with a grid of 3 columns and 4 rows (default is 1 column and 1 row)
    figure = ROOTFigure(3, 4, size=(600, 600), column_margin=(0.05, 0.025))

    # define a plot from cell (1, 1) to cell (2, 2)
    figure.define_plot(1, 1, 2, 2, y_log=True)
    # internally, set to current plot so we can add objects
    figure.add_object(hist_1, style=styles[0], label="MyLabel1")
    figure.add_object(hist_2, style=styles[1], label="MyLabel2")

    # define another plot and store the return value in plot2 cause we want to share its x and y-axis later
    # since this should only take one cell, we only need low column and low row
    plot2 = figure.define_plot(0, 0, 1, 0)
    # add same objects as above
    figure.add_object(hist_1, style=styles[0], label="MyLabel1")
    figure.add_object(hist_2, style=styles[1], label="MyLabel2")

    # now add another plot and we want to share the y-axis again
    figure.define_plot(2, 0, share_y=plot2)
    figure.add_object(hist_1, style=styles[2], label="MyLabel4")

    # now add another plot and we want to share the x-axis
    plot3 = figure.define_plot(0, 1)
    figure.add_object(hist_1, style=styles[2], label="MyLabel5")

    # now add another plot and we want to share the x-axis again
    figure.define_plot(0, 2, 0, 3, share_x=plot3)
    figure.add_object(hist_1, style=styles[2], label="MyLabel6")

    plot4 = figure.define_plot(1, 3)
    figure.add_object(hist_1, style=styles[2], label="MyLabel7")

    figure.define_plot(2, 3, share_y=plot4)
    figure.add_object(hist_1, style=styles[3], label="MyLabel8")
    figure.add_object(hist_2, style=styles[4], label="MyLabel9")

    # until now, nothing actually happened, the above is really only a specification
    # only with this call things are actually created and drawn
    figure.create()
    if save:
        figure.save("test_plot_arrangement.eps")

    return True
Ejemplo n.º 5
0
def run_example(save=True):

    ##########################################
    # first define some objects to play with #
    ##########################################

    random = TRandom(getpid())

    # Construct a TGraph to play with
    n_points = 100
    graph = TGraph(n_points)
    for i in range(n_points):
        graph.SetPoint(i, random.Gaus(16.5, 5), random.Poisson(21))

    # Create a histogram to play with
    bin_edges = array("d", [2, 3, 5, 7, 11, 13, 17, 19])
    hist = TH1F("some_histogram", "", len(bin_edges) - 1, bin_edges)

    # Fill histogram from a random number generation
    for i in range(len(bin_edges) - 1):
        hist.SetBinContent(i + 1, random.Poisson(42))
        hist.SetBinError(i + 1, random.Gaus(10))

    # And now add a function
    func = TF1("func", "84*sin(x)*sin(x)/x", 1, 10)

    ###################################
    # The actual plotting starts here #
    ###################################

    # Generate 5 styles, keep markerstyle the same for all
    styles = generate_styles(StyleObject1D, 5, markerstyle=34)
    styles_graphs = generate_styles(StyleObject1D,
                                    5,
                                    markerstyle=21,
                                    draw_options=["P"])
    # make explicity style for the function
    func_style = StyleObject1D()
    func_style.linecolor = kBlack

    # number of columns and rows
    n_cols = 3
    n_rows = 4

    # Define a MxN grid with overall margins on the left, bottom, right and top
    figure = make_grid(ROOTFigure, (n_cols, n_rows),
                       0.08,
                       0.05,
                       0.05,
                       0.05,
                       size=(1000, 1000),
                       x_title="x_title",
                       y_title="y_title")

    for i in range(n_cols * n_rows):
        # change current plot
        figure.change_plot(i)
        # Add a histogram
        figure.add_object(hist,
                          style=styles[i % len(styles)],
                          label=f"MyHist{i}")
        # Add another object, say a TGraph
        figure.add_object(graph,
                          style=styles_graphs[i % len(styles_graphs)],
                          label=f"MyGraph_{i}")
        # Add another object, now a TF1
        figure.add_object(func, style=func_style, label=f"MyFunc_{i}")
        # And some text
        figure.add_text("Text", 0.5, 0.1)

    figure.create()
    if save:
        figure.save("test_grid_mxn.eps")

    return True
Ejemplo n.º 6
0
def run_example(save=True):
    ##########################################
    # first define some objects to play with #
    ##########################################

    random = TRandom(getpid())

    # Create a histogram to play with
    bin_edges = array("d", [2, 3, 5, 7, 11, 13, 17, 19])
    hist_1 = TH1F("some_histogram_1", "", len(bin_edges) - 1, bin_edges)
    hist_2 = TH1F("some_histogram_2", "", len(bin_edges) - 1, bin_edges)
    # Fill histogram from a random number generation
    for i in range(len(bin_edges) + 1):
        hist_1.SetBinContent(i + 1, random.Poisson(42))
        hist_1.SetBinError(i + 1, random.Gaus(10))
        hist_2.SetBinContent(i + 1, random.Poisson(42))
        hist_2.SetBinError(i + 1, random.Gaus(10))

    # Ratio, make use of function to clone a ROOT object which will immediately
    # acquire a new name so no problems with overlapping names and ROOT complaining
    h_ratio = clone_root(hist_1)
    h_ratio.Divide(hist_2)

    ###################################
    # The actual plotting starts here #
    ###################################

    # Generate 2 styles with constant markerstyle
    styles = generate_styles(StyleObject1D, 2, markerstyles=34, markersizes=2)

    # Create a figure with one cell (1 column and one row)
    figure = ROOTFigure(1,
                        2,
                        size=(400, 600),
                        row_margin=[(0.1, 0), (0, 0.1)],
                        column_margin=0.1,
                        height_ratios=[1, 2])

    # since counting starts in the bottom left corner and we use the automatic generation of plot
    # the first one will be the ratio
    ratio_plot = figure.define_plot()
    # add the ratio
    figure.add_object(h_ratio, style=styles[0])
    # set axis
    ratio_plot.axes("x", title="x_axis", title_offset=3)
    ratio_plot.axes("y", title="Hist1 / Hist2", title_offset=1.8)

    # share the x-axis with the ratio plot
    main_plot = figure.define_plot(share_x=ratio_plot)
    # add nominal histograms
    figure.add_object(hist_1, style=styles[0], label="Hist1")
    figure.add_object(hist_2, style=styles[1], label="Hist2")
    # set y-axis
    main_plot.axes("y", title="Entries")

    # create and save
    figure.create()
    if save:
        figure.save("example_ratio_plot.eps")

    return True