コード例 #1
0
ファイル: csv_plotter.py プロジェクト: vchulikov/ostap
def complex_histo_draw(file_name, val_1_col, err_1_col, val_2_col, err_2_col,
                       val_3_col, err_3_col, val_4_col, err_4_col):
    #from csv to numpy-array
    data = numpy.genfromtxt(file_name, delimiter=',')

    #binning
    y_bins = [2.25, 2.75, 3.25, 3.75, 4.25]
    pt_bins = [3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20]
    length_x = numpy.size(data, 0)

    #histogram for all bins
    hist_os = h2_axes(y_bins, pt_bins)

    #histograms with ratios & yeilds
    hist_y1 = h1_axis(pt_bins)
    hist_y2 = h1_axis(pt_bins)
    hist_y3 = h1_axis(pt_bins)
    hist_y4 = h1_axis(pt_bins)
    hist_array = [hist_y1, hist_y2, hist_y3, hist_y4]

    #filling loop
    pt_bin = 1
    y_bin = 1
    for j in range(length_x - 1):
        if pt_bin == len(pt_bins):
            y_bin += 1
            pt_bin = 1
        bin_VE_1 = VE(data[j + 1, val_1_col], data[j + 1, err_1_col]**2)
        bin_VE_2 = VE(data[j + 1, val_2_col], data[j + 1, err_2_col]**2)
        bin_VE_3 = VE(data[j + 1, val_3_col], data[j + 1, err_3_col]**2)
        bin_VE_4 = VE(data[j + 1, val_4_col], data[j + 1, err_4_col]**2)

        #fill histogram with bins values
        bin_val = bin_VE_2 * bin_VE_4 / (bin_VE_1 * bin_VE_3)

        #1. uncomment if one need to non-round-values
        hist_os.SetBinContent(y_bin, pt_bin, bin_val.value())
        hist_os.SetBinError(y_bin, pt_bin, bin_val.error())

        #2. uncomment if one need to round-values
        #hist_os.SetBinContent(y_bin, pt_bin, round(bin_val.value()))
        #hist_os.SetBinError(y_bin, pt_bin, round(bin_val.error()))

        #stuff for changing bin's colors which determine by Z axis range in 2d hist
        #hist_os.SetMinimum(0.)
        #hist_os.SetMaximum(0.08)

        #fill histogram with R-values
        hist_array[y_bin - 1].SetBinContent(pt_bin, 100 * (bin_val.value()))
        hist_array[y_bin - 1].SetBinError(pt_bin, 100 * (bin_val.error()))

        pt_bin += 1

    return hist_os, hist_array[0], hist_array[1], hist_array[2], hist_array[3]
コード例 #2
0
def complex_histo_draw(file_name, val_1_col, err_1_col, val_2_col, err_2_col, corr_1_val): 
 #from csv to numpy-array
 data = numpy.genfromtxt(file_name, delimiter = ',')
 
 #binning
 #for statistic
 y_bins_1  = [2.25, 2.75, 3.25, 3.75, 4.25]
 pt_bins_1 = [ 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20 ]
 #for systematic
 y_bins_2  = [2.25, 2.75, 3.25, 3.75, 4.25]
 pt_bins_2 = [ 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20 ]
 length_x = numpy.size(data, 0)

 #histograms with ratios & yeilds
 #stat
 hist_array_1 = [h1_axis(pt_bins_1), h1_axis(pt_bins_1), h1_axis(pt_bins_1), h1_axis(pt_bins_1)]
 #syst
 hist_array_2 = [h1_axis(pt_bins_2), h1_axis(pt_bins_2), h1_axis(pt_bins_2), h1_axis(pt_bins_2)]

 #filling loop
 pt_bin = 1
 y_bin  = 1
 for j in range(length_x - 1):
  if pt_bin == len(pt_bins_1):
   y_bin += 1
   pt_bin = 1 
  bin_VE_1 = VE(data[j + 1, val_1_col]*data[j+1, corr_1_val], (data[j + 1, val_1_col]*data[j + 1, err_1_col])**2) #R + syst
  bin_VE_2 = VE(data[j + 1, val_2_col]*data[j+1, corr_1_val], (data[j + 1, val_2_col]*data[j + 1, err_2_col])**2) #R + stat

  #fill histogram with bins values
  #syst
  bin_val_1 = bin_VE_1
  #stat
  bin_val_2 = bin_VE_2

  #fill histogram with R-values
  hist_array_1[y_bin - 1].SetBinContent(pt_bin, bin_val_1.value())
  hist_array_1[y_bin - 1].SetBinError(pt_bin, bin_val_1.error())

  hist_array_2[y_bin - 1].SetBinContent(pt_bin, bin_val_2.value())
  hist_array_2[y_bin - 1].SetBinError(pt_bin, bin_val_2.error())

  pt_bin += 1
 return hist_array_1[0], hist_array_2[0]
コード例 #3
0
def prepare_data():
    #

    seed = 1234567890
    random.seed(seed)
    logger.info('Test *RANDOM* data will be generated/seed=%s' % seed)
    ## prepare "data" histograms:
    # 1) 2D histograms
    ix, iy = 30, 30
    hdata = h2_axes([xmax / ix * i for i in range(ix + 1)],
                    [ymax / iy * i for i in range(iy + 1)])
    # 2) non-equal binning 1D histograms for x-component
    hxdata = h1_axis([i for i in range(5)] + [5 + i * 0.2 for i in range(50)] +
                     [15 + i for i in range(6)])
    # 2) equal binning 1D histograms for y-component
    hydata = h1_axis([i * 0.5 for i in range(31)])

    assert hdata.xmax() == xmax, 'XMAX is invalid!'
    assert hdata.ymax() == ymax, 'YMAX is invalid!'
    assert hxdata.xmax() == xmax, 'XMAX is invalid!'
    assert hydata.xmax() == ymax, 'XMAX is invalid!'

    with ROOT.TFile.Open(testdata, 'recreate') as mc_file:
        mc_file.cd()

        datatree = ROOT.TTree('DATA_tree', 'data-tree')
        datatree.SetDirectory(mc_file)

        from array import array
        xvar = array('f', [0])
        yvar = array('f', [0])
        datatree.Branch('x', xvar, 'x/F')
        datatree.Branch('y', yvar, 'y/F')

        for i in range(N1):

            x, y = -1, -1

            while not 0 <= x < xmax or not 0 <= y < ymax:

                v1 = random.gauss(0, 3)
                v2 = random.gauss(0, 2)

                x = 5 + v1 + v2
                y = 12 + v1 - v2

            hdata.Fill(x, y)
            hxdata.Fill(x)
            hydata.Fill(y)

            xvar[0] = x
            yvar[0] = y

            datatree.Fill()

        for i in range(N1):

            x, y = -1, -1

            while not 0 <= x < xmax or not 0 <= y < ymax:

                v1 = random.gauss(0, 3)
                v2 = random.gauss(0, 2)

                x = 15 + v1
                y = 4 + v2

            hdata.Fill(x, y)
            hxdata.Fill(x)
            hydata.Fill(y)

            xvar[0] = x
            yvar[0] = y

            datatree.Fill()

        for i in range(N1):

            x, y = -1, -1

            while not 0 <= x < xmax or not 0 <= y < ymax:

                v1 = random.gauss(0, 3)
                v2 = random.gauss(0, 2)

                x = 15 + v1 - v2
                y = 12 + v1 + v2

            hdata.Fill(x, y)
            hxdata.Fill(x)
            hydata.Fill(y)

            xvar[0] = x
            yvar[0] = y

            datatree.Fill()

        for i in range(0, 4 * N1):

            x = random.uniform(0, xmax)
            y = random.uniform(0, ymax)

            hdata.Fill(x, y)
            hxdata.Fill(x)
            hydata.Fill(y)

            xvar[0] = x
            yvar[0] = y

            datatree.Fill()

        datatree.Write()

        ## write the histogram
        mc_file[tag_data] = hdata
        mc_file[tag_datax] = hxdata
        mc_file[tag_datay] = hydata

        mctree = ROOT.TTree(tag_mc, 'mc-tree')
        mctree.SetDirectory(mc_file)

        from array import array
        xvar = array('f', [0.0])
        yvar = array('f', [0.0])
        mctree.Branch('x', xvar, 'x/F')
        mctree.Branch('y', yvar, 'y/F')

        for i in range(2 * N2):

            xv = random.uniform(0, xmax)
            yv = random.uniform(0, ymax)

            xvar[0] = xv
            yvar[0] = yv

            mctree.Fill()

        fx = lambda x: (x / 10.0 - 1)**2
        fy = lambda y: (y / 7.5 - 1)**2

        for i in range(N2):

            while True:
                xv = random.uniform(0, xmax)
                if random.uniform(0, 1) < fx(xv): break

            while True:
                yv = random.uniform(0, ymax)
                if random.uniform(0, 1) < fy(yv): break

            xvar[0] = xv
            yvar[0] = yv

            mctree.Fill()

        mctree.Write()
        mc_file.ls()
コード例 #4
0
def test_basic_1D() :

    logger.info ( 'Test for very basic operations with 1D-histograms')

    h1 = ROOT.TH1D ( hID() , '' , 10 , 0 , 1 )

    ## clone it! 
    h2 = h1.clone() 

    ## set content for certain bins:
    h1[1] = 1.55
    h1[2] = VE(1,0.2**2)

    ## get content from certaom bins:
    a = h1[3]

    h1 += VE(3,0.5**2)
    
    ## add a function
    h1 += lambda x : VE(25*x*x,25*x*x)

    ## Divide by function
    h1 /= lambda x : float(1+x*x) 

    ## multiply by constant
    h1 *= 15.0

    ## loop over bins using index
    for i in h1 :
        logger.info( "bin# %2d, content %s" % (  i , h1[i] ) )

    ## loop over bins i nreverse order 
    for i in reversed( h1 ) :
        logger.info( "bin# %2d, content %s" % (  i , h1[i] ) )

    ## iterate over all 
    for item in h1.items() : 
        logger.info ( "items:  %s" % str ( item ) )

       
    ## interpolate
    for x in range(10) :
        x = random.uniform(0,1)
        logger.info ( 'h1(%.3f) = %s[d] %s[0] %s[1] %s[2] %s[3] ' % ( x     ,
                                                                      h1(x) ,
                                                                      h1(x, interpolate=0) ,
                                                                      h1(x, interpolate=1) ,
                                                                      h1(x, interpolate=2) ,
                                                                      h1(x, interpolate=3) ) )

    ## running sum
    h1_d = h1.sumv()                     ## default 
    h1_i = h1.sumv( increasing = True  ) ## ditto
    h1_r = h1.sumv( increasing = False ) ## ditto

    ## histogram efficiney of cuts
    eff_i = h1.effic( increasing = True   )
    eff_r = h1.effic( increasing = False  )

    ## efficiency of certaine value
    e1_i  = h1.efficiency( 0.3 , increasing = True  )
    e2_i  = h1.efficiency( 0.3 , increasing = False )


    ## smear the histogram
    h0    = ROOT.TH1F( hID() , '', 400 , -1 , 1 )
    for i in range( 1000 ) : h0.Fill( random.gauss(0,0.10 ) )
    hs    = h0.smear ( sigma = 0.10 )

    logger.info ( 'Original RMS %20s , smeared(0.10) %-20s' % ( h0.rms() , hs.rms() ) )  
    ## specific transformation:

    ## "precision"
    hp  = h1.precision()
    
    ## "B/S"
    hb2s = h1.b2s() 

    ## rescale histo

    hs1 = h1.scale(1)
    hs2 = h1.rescale_bins(1) 

    ## sample
    hr  = h1.sample()


    ## "figure of merit" to maximize precision:

    fm2 = h1.fom_2()
    fm2 = h1.FoM_2()

    ## rebin template:
    h_tmpl = h1_axis ( [ 0 , 0.1 , 0.5 , 0.55, 0.80 , 0.95 , 1.0 ] )

    ## rebin as "numbers"
    h1_n   = h1.rebinNumbers  ( h_tmpl )
    
    ## rebin as "function"
    h1_f   = h1.rebinFunction ( h_tmpl )

    ## slice for the histogram
    hs   = h1[4:30]

    ## accumulate
    a1 = h1.accumulate( low = 1    , high = 10 ) 
    a2 = h1.accumulate( xmin = 0.1 , xmax = 0.30 ) 

    ## shift
    hs = h1.shift ( -0.3 )

    ## other operations
    
    h2 = h1**2

    h2 = h1*h1

    h2 = h1*2

    h2 = h1/2

    h2 = 2*h1
    
    h2 = abs(h1) 
        
    from ostap.math.math_ve import sin, exp 
    h2 = sin ( h1 )
    h2 = exp ( h1 )

    
    ## shift for bins
    hs = h1 >> 4 
    hs = h1 << 5 

    ## integration (taking into account bin-width)
    i1 = h1.integrate()
    i2 = h1.integrate( lowx = 1   , highx = 15  ) 
    i2 = h1.integrate( xmin = 0.1 , xmax  = 0.6 )  

    ## statistics
    for i in range ( 0 , 5 ) :
        logger.info ( "        Moment (%d): %-20s" % ( i , h1.moment        ( i ) ) )
    ## statistics
    for i in range ( 0 , 5 ) :
        logger.info ( "Central moment (%d): %-20s" % ( i , h1.centralMoment ( i ) ) )

    logger.info (     "              Mean : %-20s" % h1.mean     () ) 
    logger.info (     "               RMS : %-20s" % h1.rms      () )  
    logger.info (     "          Skewness : %-20s" % h1.skewness () )  
    logger.info (     "          Kurtosis : %-20s" % h1.kurtosis () ) 

    logger.info (     "  Stat: %s" % h1. stat() )
    logger.info (     " WStat: %s" % h1.wstat() )
    logger.info (     " XStat: %s" % h1.xstat() )
        
    logger.info ( '  minmax  %20s' % str( h1. minmax() ) )
    logger.info ( 'x-minmax  %20s' % str( h1.xminmax() ) )
    logger.info ( 'y-minmax  %20s' % str( h1.yminmax() ) )
コード例 #5
0
ファイル: error_plot.py プロジェクト: vchulikov/ostap
def complex_histo_draw(file_name, epidlc_val, epidsc_val, se1_val, se1_err,
                       se0_val, se0_err, se2_val):
    #from csv to numpy-array
    data = numpy.genfromtxt(file_name, delimiter=',')
    srtformat = "{:7.5f}"

    #binning
    y_bins = [2.25, 2.75, 3.25, 3.75, 4.25]
    pt_bins = [3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20]
    length_x = numpy.size(data, 0)

    #histogram for all bins
    hist_os = h2_axes(y_bins, pt_bins)

    #histograms with ratios & yeilds
    hist_y1 = h1_axis(pt_bins)
    hist_y2 = h1_axis(pt_bins)
    hist_y3 = h1_axis(pt_bins)
    hist_y4 = h1_axis(pt_bins)
    hist_array = [hist_y1, hist_y2, hist_y3, hist_y4]

    #filling loop
    pt_bin = 1
    y_bin = 1
    for j in range(length_x - 1):
        if pt_bin == len(pt_bins):
            y_bin += 1
            pt_bin = 1
            print(str("\hline"))
            print("\multicolumn{6}{c}{$y\in$~(" + str(data[j + 1, 1]) + "," +
                  str(data[j + 1, 2]) + ")}" + "\\" + "\\")
            print(str("\hline"))
        r = data[j + 1, 13] / data[j + 1, 14]
        r_i = data[j + 1, 18] / data[j + 1, 19]
        r_s = data[j + 1, 20] / data[j + 1, 21]

        s1 = abs(r - r_s)
        s2 = abs(r - r_i)
        s3 = data[j + 1, 25]
        summ = (s1**2 + s2**2 + s3**2)**0.5
        bin_VE_1 = VE(summ, 0)

        #fill histogram with bins values
        bin_val = bin_VE_1

        #1. uncomment if one need to non-round-values
        hist_os.SetBinContent(y_bin, pt_bin, bin_val.value())
        hist_os.SetBinError(y_bin, pt_bin, bin_val.error())

        print("(" + str(data[j + 1, 3]) + "," + str(data[j + 1, 4]) + ") & " +
              srtformat.format(r) + " & " + srtformat.format(s1) + " & " +
              srtformat.format(s2) + " & " + srtformat.format(s3) + " & " +
              srtformat.format(summ) + "\\" + "\\")
        #2. uncomment if one need to round-values
        #hist_os.SetBinContent(y_bin, pt_bin, round(bin_val.value()))
        #hist_os.SetBinError(y_bin, pt_bin, round(bin_val.error()))

        #hist style
        #remove stat box
        hist_os.SetStats(0)
        #set z range
        hist_os.SetMinimum(0.)
        hist_os.SetMaximum(0.06)

        pt_bin += 1
    print("\n")
    return hist_os
コード例 #6
0
#
## histos for gaussian distributions
# 

h1g   = ROOT.TH1D ( hID() , '' ,  40 , -5 , 5 ) ; h1g.Sumw2() 
h2g   = ROOT.TH1D ( hID() , '' ,  40 , -5 , 5 ) ; h2g.Sumw2() 
h3g   = ROOT.TH1D ( hID() , '' ,  20 , -5 , 5 ) ; h3g.Sumw2() 

bins  = [ -5 ]
## 
random.seed(10) 
for i in range(0, 15 ) : bins.append ( random.uniform ( -5 , 5 ) )
bins += [  5 ]
bins.sort()

h4g   = h1_axis ( bins ) 

#
## histos for uniform distributions
# 
h1u = h1g.clone()
h2u = h2g.clone()
h3u = h3g.clone()
h4u = h4g.clone()

#
## histos for exponential distributions
# 
h1e = h1g.clone()
h2e = h2g.clone()
h3e = h3g.clone()
コード例 #7
0
ファイル: histo.py プロジェクト: aleksha/ostap-scripts
from ostap.histos.histos import h1_axis 
from   ostap.math.ve import VE

csv_file = open("325.csv","r")

pt_bins = [1,2,3,4,5,6,7,8]

hR_5tev = h1_axis(pt_bins)
hR_7tev = h1_axis(pt_bins)
hR_13tev = h1_axis(pt_bins)

hR_5tev.blue()
hR_13tev.red()

bin=0
for line in csv_file:
    bin+=1
    w = line[:-1].split(",")
    hR_5tev [bin] = VE(float(w[ 3]),float(w[ 6])**2)
    hR_7tev [bin] = VE(float(w[ 7]),float(w[10])**2)
    hR_13tev[bin] = VE(float(w[11]),float(w[14])**2)
    print(w)


hR_5tev.GetYaxis().SetRangeUser(0,4)
hR_5tev.GetXaxis().SetTitleSize(0.05)
hR_5tev.GetXaxis().SetTitle("p_{T}, GeV/c")
hR_5tev.GetYaxis().SetTitle("( d#sigma(D^{0})/dp_{T} ) / ( d#sigma(D*)/dp_{T} )")

hR_5tev.Draw("e1")
コード例 #8
0
ファイル: Functions.py プロジェクト: aleksha/PNPI-PhD
def draw_param(r_fit, w_fit, h_fit, N_BINS, var, W_max, name, XTitle, Prefix,
               Type, var_Units):

    var_list = []
    for idx in range(N_BINS + 1):
        var_list.append(var.getMin() + idx *
                        (var.getMax() - var.getMin()) / N_BINS)
    h_pull = h1_axis(var_list)
    for idx in range(1, N_BINS):
        h_pull[idx] = VE(h_fit[idx - 1][3], 0**2)

    h_pull.GetYaxis().SetRangeUser(-4, 4)
    h_pull.GetYaxis().SetTitle("#Delta / #sigma")
    h_pull.GetYaxis().SetTitle("#Delta / #sigma")
    h_pull.GetYaxis().SetTitleSize(0.2)
    h_pull.GetYaxis().SetTitleOffset(0.2)
    h_pull.GetYaxis().SetLabelSize(0.1)
    h_pull.GetXaxis().SetLabelSize(0.15)
    h_pull.SetFillColor(34)

    line_up = ROOT.TLine(var.getMin(), 2, var.getMax(), 2)
    line_down = ROOT.TLine(var.getMin(), -2, var.getMax(), -2)
    line_up.SetLineColor(2)
    line_down.SetLineColor(2)

    #-------------------------------------------------------------------
    # Prepare for drawing
    #-------------------------------------------------------------------

    Y_title = 0.92 * W_max
    Y_step = 0.05 * W_max

    x_pos = var.getMin() + 0.05 * (var.getMax() - var.getMin())

    percentage = " (" + "{:2.2f}".format(
        100. * r_fit("S")[0].error() / r_fit("S")[0].value()) + "%)"
    text_SIG = "SIGNAL = " + "{:5.0f}".format(r_fit("S")[0].value())
    text_SIG += " #pm" + "{:4.0f}".format(r_fit("S")[0].error()) + percentage
    text_BKG = "BKG = " + "{:5.0f}".format(r_fit("B")[0].value())
    text_BKG += " #pm" + "{:4.0f}".format(r_fit("B")[0].error())

    labels = []
    labels.append(ROOT.TLatex(x_pos, Y_title - 0.0 * Y_step, text_SIG))
    labels.append(ROOT.TLatex(x_pos, Y_title - 1.5 * Y_step, text_BKG))

    for lb in labels:
        lb.SetTextSize(0.04)

    w_fit.GetXaxis().SetTitle(XTitle + ", " + var_Units)
    w_fit.GetXaxis().SetTitleSize(0.05)
    w_fit.GetXaxis().SetTitleOffset(0.9)
    w_fit.GetYaxis().SetRangeUser(0.01, W_max)
    w_fit.GetYaxis().SetTitleOffset(0.9)
    w_fit.GetYaxis().SetTitle("Candidates per " +
                              str((var.getMax() - var.getMin()) / N_BINS) +
                              " " + var_Units)
    w_fit.GetYaxis().SetLabelSize(0.025)

    #-------------------------------------------------------------------
    # Draw it!
    #-------------------------------------------------------------------

    canv = ROOT.TCanvas("canv", "canv", 700, 800)
    canv.Divide(1, 2)

    pad1 = canv.GetListOfPrimitives().FindObject("canv_1")
    pad2 = canv.GetListOfPrimitives().FindObject("canv_2")
    pad1.SetPad(0.00, 0.11, 1.00, 1.00)
    pad2.SetPad(0.00, 0.00, 1.00, 0.10)

    canv.cd(1)
    w_fit.Draw()
    for lb in labels:
        lb.Draw()
    canv.cd(2)
    h_pull.Draw("HIST")
    line_up.Draw()
    line_down.Draw()

    canv.Print(Prefix + "_" + name + "." + Type)
    canv.Close()