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]
def h2(file_name, xvar, x_bins, yvar, y_bins, zvar): "Create 2d histo" h2 = h2_axes(x_bins, y_bins) print("\nCreating 2D-histo:") print("FILE : " + file_name) print("HEADER : " + header(file_name)) print(" x_var : " + xvar) print(" y_var : " + yvar) print(" z_var : " + zvar) xidx, yidx, zidx = find_vars(file_name, xvar, yvar, zvar) with open(file_name, "r") as f: for line in f: if line[0:2] == " |": wl = line[1:-1].split("|") for w in wl: x_min = float(wl[xidx].split(",")[0]) y_min = float(wl[yidx].split(",")[0]) z_val = float(wl[zidx].split("+-")[0]) z_err = float(wl[zidx].split("+-")[1]) h2[get_2d_bin(x_min, x_bins, y_min, y_bins)] = VE(z_val, z_err**2) return h2
def test_basic_2D () : logger.info ( 'Test for very basic operations with 2D-histograms') h2 = h2_axes ( [1,2,3,4,5,6,7] , [1,2,3,4,5] ) h2 += lambda x,y: VE(1,1)+VE(x*x+2*y*y,x*x+2*y*y) ## access the content logger.info ( "h[%1d, %1d]=%s" % ( 2 , 3 , h2[2,3] ) ) logger.info ( "h[%1d][%1d]=%s" % ( 2 , 3 , h2[2][3] ) ) logger.info ( "h[%1d](%.2f)=%s" % ( 2 , 3.01 , h2[2](3.01 ) ) ) logger.info ( "h(%.2f,%.2f)=%s" % ( 2.01 , 3.01 , h2(2.01,3.01 ) ) ) ## get the 1st X-slice hx = h2.sliceX ( 1 ) ## get the slice in 1,4,5 X-bins hx = h2.sliceX ( [1,4,5] ) ## get the 2nd Y-slice hy = h2.sliceY ( 2 ) ## get the slice in 1,3,5 Y-bins hy = h2.sliceY ( [1,3,4] ) ## projection in X hx = h2.projX() ## projection in Y hy = h2.projY() logger.info ( ' minmax %20s' % str( h2. minmax() ) ) logger.info ( 'x-minmax %20s' % str( h2.xminmax() ) ) logger.info ( 'y-minmax %20s' % str( h2.yminmax() ) ) logger.info ( 'z-minmax %20s' % str( h2.zminmax() ) )
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()
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