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
Exemple #2
0
def bkgtracks(N):
    tracks = []
    resolution = 0.001  ## cm (10 um)
    rnd = TRandom()
    rnd.SetSeed()
    for i in range(N):
        R = cfgmap["Rbeampipe"] - cfgmap["Wbeampipe"]
        phi = rnd.Uniform(0, 2 * ROOT.TMath.Pi())
        x0 = R * ROOT.TMath.Cos(phi)
        y0 = R * ROOT.TMath.Sin(phi)
        z0 = cfgmap["zDipoleExit"]

        z4 = cfgmap["zLayer4"]
        x4 = rnd.Uniform(1.15 * cfgmap["xPsideL"], 1.15 * cfgmap["xEsideR"])
        y4 = rnd.Uniform(-1.15 * cfgmap["Hstave"] / 2,
                         1.15 * cfgmap["Hstave"] / 2)

        r0 = [x0, y0, z0]
        r4 = [x4, y4, z4]

        z1 = cfgmap["zLayer1"]
        x1 = xofz(r4, r0, z1) + rnd.Gaus(0, resolution)
        y1 = yofz(r4, r0, z1) + rnd.Gaus(0, resolution)

        z2 = cfgmap["zLayer2"]
        x2 = xofz(r4, r0, z2) + rnd.Gaus(0, resolution)
        y2 = yofz(r4, r0, z2) + rnd.Gaus(0, resolution)

        z3 = cfgmap["zLayer3"]
        x3 = xofz(r4, r0, z3) + rnd.Gaus(0, resolution)
        y3 = yofz(r4, r0, z3) + rnd.Gaus(0, resolution)

        z5 = 360
        x5 = xofz(r4, r0, z5)
        y5 = yofz(r4, r0, z5)

        line = TPolyLine3D()
        line.SetNextPoint(x0, y0, z0)
        line.SetNextPoint(x1, y1, z1)
        line.SetNextPoint(x2, y2, z2)
        line.SetNextPoint(x3, y3, z3)
        line.SetNextPoint(x4, y4, z4)
        line.SetNextPoint(x5, y5, z5)
        line.SetLineColor(ROOT.kRed)
        tracks.append(line)

    return tracks
def generateToyFunctions(normNom, normErr, p1, p1err, p2, p2err, corrMatrix, covMatrix, nToys):
  from ROOT import TF1, TRandom
  from math import sqrt
  functions = []
  formula = "[0]*TMath::Power(x, [1]+[2]*TMath::Log(x))"
  gaussDist = TRandom()
  x1Nom = (-0.997412*p1-0.0719019*p2)
  x2Nom = (0.0719019*p1-0.997412*p2)
  print "p1 is ", p1
  print "p1err is ", p1err
  print "x1Nom is ", x1Nom
  print "p2 is ", p2
  print "p2err is ", p2err
  print "x2Nom is ", x2Nom
  x1err = -0.997412*p1err-0.0719019*p2err
  x2err = 0.0719019*p1err-0.997412*p2err
  print "x1err is ",  -0.997412*p1err-0.0719019*p2err
  print "x2err is ",  (0.0719019*p1err-0.997412*p2err)
  for i in range (0, nToys):
    print "--------"
    print "toy %i:" % i
    x1 = gaussDist.Gaus(x1Nom,x1err)
    x2 = gaussDist.Gaus(x2Nom, x2err)
    varp1 = (-0.997412*x1+0.0719019*x2)
    varp2 = (-0.0719019*x1-0.997412*x2) 
    print "original p1, p2:        %f, %f" % (p1, p2)
    print "original p1err, p2err:  %f, %f" % (p1err, p2err)
    print "translates to x1, x2:   %f, %f" % (x1Nom, x2Nom)
    print "x1err, x2err:           %f, %f" % (x1err, x2err)
    print "picked x1, x2:          %f, %f" % (x1, x2)
    print "translates to  p1, p2:  %f, %f" % (varp1, varp2)
    functions.append(TF1("f_%i" % i, formula, 700, 4700))
    functions[-1].SetParameters(1, varp1, varp2)
    if functions[-1].Integral(700, 4700)  == 0:
      del(functions[-1])
    else:
      prenorm = functions[-1].Integral(700, 4700)
      norm = gaussDist.Gaus(normNom, normErr)/prenorm
      print "norm is: ", norm
      functions[-1].SetParameter(0, norm)

  return functions
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
def AddFWHM(h, name, modelpars, mflags=[True, True, True]):
    import random
    from ROOT import TH1I, TRandom
    #seed = random.randint(0, 500)
    seed = 123
    ran = TRandom(seed)
    nbins = h.GetNbinsX()
    hout = TH1I(name, '', nbins, h.GetBinLowEdge(1),
                h.GetXaxis().GetBinUpEdge(nbins))
    for i in range(1, nbins + 1):
        for j in range(1, int(h.GetBinContent(i)) + 1):
            hout.Fill(
                ran.Gaus(
                    h.GetBinCenter(i),
                    FWHM(h.GetBinCenter(i), modelpars, mflags=mflags) / 2.35))
    return hout
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
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
 phi = rnd.Uniform(0, 2 * ROOT.TMath.Pi())
 x0 = R * ROOT.TMath.Cos(phi)
 y0 = R * ROOT.TMath.Sin(phi)
 z0 = cfgmap["zDipoleExit"]
 # chose a point in the exit of the tracker
 z4 = cfgmap["zLayer4"]
 x4 = rnd.Uniform(1.1 * cfgmap["xPsideL"], 1.1 * cfgmap["xEsideR"])
 y4 = rnd.Uniform(-1.1 * cfgmap["Hstave"] / 2,
                  1.1 * cfgmap["Hstave"] / 2)
 # require that the track is not pointing in the reange between the 2 trackr sides
 if (x4 > cfgmap["xPsideR"] and x4 < cfgmap["xEsideL"]): continue
 # place 3 points on layers 1,2,3 along the line between r4 and r0, with some error (resolution)
 r0 = [x0, y0, z0]
 r4 = [x4, y4, z4]
 z1 = cfgmap["zLayer1"]
 x1 = xofz(r4, r0, z1) + rnd.Gaus(0, resolution)
 y1 = yofz(r4, r0, z1) + rnd.Gaus(0, resolution)
 z2 = cfgmap["zLayer2"]
 x2 = xofz(r4, r0, z2) + rnd.Gaus(0, resolution)
 y2 = yofz(r4, r0, z2) + rnd.Gaus(0, resolution)
 z3 = cfgmap["zLayer3"]
 x3 = xofz(r4, r0, z3) + rnd.Gaus(0, resolution)
 y3 = yofz(r4, r0, z3) + rnd.Gaus(0, resolution)
 r1 = [x1, y1, z1]
 r2 = [x2, y2, z2]
 r3 = [x3, y3, z3]
 cluster = ROOT.TPolyMarker3D()
 cluster.SetNextPoint(r1[0], r1[1], r1[2])
 clusters_xyz.push_back(cluster)
 clusters_type.push_back(0)
 clusters_id.push_back(ids[0])
Exemple #9
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
Exemple #10
0
thickness = 150
chargePerUm = 77
totalCharge = chargePerUm * thickness

for i in range(0, nevents):
    xin = rang.Uniform(xmin, xmax)
    hin.Fill(xin)

    leftCharge = (1 - etaFunc(xin)) * totalCharge
    rightCharge = (etaFunc(xin)) * totalCharge

    #print "Pos: " + str(xin)
    #print "Charge: " + str(leftCharge) + " " + str(rightCharge)

    leftCharge += rang.Gaus(0, noise)
    rightCharge += rang.Gaus(0, noise)

    if leftCharge < threshold:
        leftCharge = 0
    if rightCharge < threshold:
        rightCharge = 0

    cog = centerOfGravity(leftCharge, rightCharge)

    #print "COG: " + str(cog)

    hout.Fill(cog)

    hres.Fill(cog - xin)
   pz.clear()
   E.clear()
   
   rnd = TRandom()
   rnd.SetSeed()
   
   ### generate tracks
   nbkgtrks = 0
   while(nbkgtrks<NbkgTracks):
      ### draw vertex position
      R = Rbeampipe-Wbeampipe ## for now assume vetex can be only on the beampipe
      SigmaZ = 25 ## cm, around the dipole exit
      phi = rnd.Uniform(0,2*ROOT.TMath.Pi())
      vx0 = R*ROOT.TMath.Cos(phi)
      vy0 = R*ROOT.TMath.Sin(phi)
      vz0 = zDipoleExit+rnd.Gaus(0,SigmaZ)
      vz0 = round(vz0,1) ## precision of stepsize in z for propagation in B-filed is 0.1

      
      ### draw the momentum
      # tau = -0.5
      tau = -0.5
      P0 = rnd.Exp(tau)
      rp = TVector3()
      # rp.SetXYZ( rnd.Gaus(0,0.01), rnd.Gaus(0,0.01), rnd.Exp(tau) ) ### for now, cannot go backards in z...
      rp.SetXYZ( rnd.Gaus(0,0.01), rnd.Gaus(0,0.01), rnd.Exp(tau) ) ### for now, cannot go backards in z...
      
      r4 = zLayer1*rp.Unit()
      # print("x4=%g, y4=%g" % (r4.X(),r4.Y()))
      # if(abs(r4.X())<=cfgmap["Rbeampipe"]):                       continue ## |x| must be >Rbeampipe
      # if(abs(r4.X())>cfgmap["RoffsetBfield"]+2*cfgmap["Lstave"]): continue ## |x| must be <=Rbeampipe+2*Lstave