def test_parameterize_3D(): with ROOT.TFile.Open(data_file, 'READ') as f: tree = f.S lx = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -4, 4, -4, 6) ly = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -4, 4, -4, 6) lz = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -2, 2, -4, 6) lu = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -2, 2, -4, 4) lx.parameterize(tree, 'y', 'z', 'u', cuts) ly.parameterize(tree, 'x', 'z', 'u', cuts) lz.parameterize(tree, 'x', 'y', 'u', cuts) lu.parameterize(tree, 'x', 'y', 'z', cuts) lxy = lz.integralZ() lzu = lx.integralX() lxu = ly.integralY() hxy = ROOT.TH2F(hID(), '', 20, -2, 2, 20, -2, 2) hzu = ROOT.TH2F(hID(), '', 20, -4, 4, 20, -4, 6) hxu = ROOT.TH2F(hID(), '', 20, -2, 2, 20, -4, 6) tree.project(hxy, 'y:x', cuts) tree.project(hzu, 'u:z', cuts) tree.project(hxu, 'u:x', cuts) lxy *= (4.0 / 20) * (4.0 / 20) lzu *= (8.0 / 20) * (10.0 / 20) lxu *= (4.0 / 20) * (10.0 / 20) d1 = SE() d2 = SE() d3 = SE() for i in range(100): x = random.uniform(-2, 2) y = random.uniform(-2, 2) z = random.uniform(-4, 4) u = random.uniform(-4, 6) d1 += (lxy(x, y) - hxy(x, y)) / max(lxy(x, y), hxy(x, y)) d2 += (lzu(z, u) - hzu(z, u)) / max(lzu(z, u), hzu(z, u)) d3 += (lxu(x, u) - hxu(x, u)) / max(lxu(x, u), hxu(x, u)) logger.info('3D-(xy)-DIFFERENCES are %s ' % d1) logger.info('3D-(zu)-DIFFERENCES are %s ' % d2) logger.info('3D-(xu)-DIFFERENCES are %s ' % d3)
def test_shapes_1d(): logger = getLogger("test_shapes_1d") if root_info < (6, 18): logger.info("Test is distabled for ROOT %s" % str(root_info)) return ## C++ callable as shape xvar = ROOT.RooRealVar('x', '', -5, 5) s1 = Models.Shape1D_pdf('S1', shape=Ostap.Math.BifurcatedGauss(), xvar=xvar) with wait(1), use_canvas("shape1d : C++ functor"): s1.draw() ## histogram as shape h2 = ROOT.TH1D(hID(), '', 50, -5, 5) h2 += lambda x: 100 * gauss(x) s2 = Models.Shape1D_pdf('S2', shape=h2, xvar=xvar) with wait(1), use_canvas("shape1d : histogram "): s2.draw()
def test_shapes_2d(): logger = getLogger("test_shapes_2d") if root_info < (6, 18): logger.info("Test is distabled for ROOT %s" % str(root_info)) return ## histogram as shape h2 = ROOT.TH2D(hID(), '', 40, -5, 5, 40, -10, 10) h2 += lambda x, y: 100 * gauss(x) * gauss(y, sigma=2) s2 = Models.Shape2D_pdf('S3', shape=h2, xvar=(-5, 5), yvar=(-10, 10)) with wait(1), use_canvas("shape2d : histogram/x"): s2.draw1() with wait(1), use_canvas("shape2d : histogram/x in y-range"): s2.draw1(in_range=(-6, 6)) with wait(1), use_canvas("shape2d : histogram/y"): s2.draw2() with wait(1), use_canvas("shape2d : histogram/y in x-range"): s2.draw2(in_range=(-3, 3))
def _rrv_as_H1_(v, bins=100, double=True): """Make TH1 histogram from RooRealVar >>> variable = ... >>> histo = variable.histo ( 100 ) """ _hT = ROOT.TH1D if double else ROOT.TH1F _h = _hT(hID(), v.GetTitle(), bins, v.getMin(), v.getMax()) _h.Sumw2() return _h
def make_histos(entries=100000): nbins = 2, 3, 4, 5, 10 h = {} for i in nbins: for j in nbins: for k in nbins: histo = ROOT.TH3F(hID(), '', i, -3, 3, j, -3, 3, k, -3, 3) for n in range(entries): histo.Fill(random.gauss(0, 1), random.gauss(0, 1), random.gauss(0, 1)) key = i, j, k h[key] = histo return h
def test_parallel_toys2 ( ) : """Perform toys-study for possible fit bias and correct uncertainty evaluation - generate `nToys` pseudoexperiments with some PDF `gen_pdf` - fit teach experiment with the PDF `fit_pdf` - store fit results - fill distributions of fit results """ logger = getLogger ( 'ostap.test_parallel_toys2' ) results , stats = Toys.parallel_toys2 ( gen_pdf = gen_gauss , fit_pdf = fit_gauss , nToys = 1000 , nSplit = 20 , data = [ mass ] , gen_config = { 'nEvents' : 200 , 'sample' : True } , fit_config = { 'silent' : True } , gen_pars = { 'mean_GG' : 0.4 , 'sigma_GG' : 0.1 } , fit_pars = { 'mean_GF' : 0.4 , 'sigma_GF' : 0.1 } , silent = True , progress = False ) for p in stats : logger.info ( "Toys: %-20s : %s" % ( p, stats [ p ] ) ) ## make histos h_mean = ROOT.TH1F ( hID() , 'mean of Gauss ' , 50 , 0 , 0.80 ) h_sigma = ROOT.TH1F ( hID() , 'sigma of Gauss' , 50 , 0.05 , 0.15 ) for r in results ['mean_FG' ] : h_mean .Fill ( r ) for r in results ['sigma_FG' ] : h_sigma.Fill ( r ) for h in ( h_mean , h_sigma ) : with wait ( 1 ) , use_canvas ( 'test_parallel_toys2: %s' % h.GetTitle() ) : h.draw() logger.info ( "%s :\n%s" % ( h.GetTitle() , h.dump ( 30 , 10 ) ) )
def test_parallel_significance_toys ( ) : """Perform toy-study for significance of the signal - generate `nToys` pseudoexperiments using background-only hypothesis - fit each experiment with signal+background hypothesis - store fit results - fill distributions for fit results """ logger = getLogger ( 'ostap.test_parallel_significance_toys' ) ## only background hypothesis bkg_only = Models.Bkg_pdf ( "BKG" , xvar = mass , power = 0 , tau = 0 ) signal = Models.Gauss_pdf ( 'S' , xvar = mass , mean = 0.5 , sigma = 0.1 ) signal.mean .fix ( 0.4 ) signal.sigma.fix ( 0.1 ) ## signal + background hypothesis model = Models.Fit1D ( signal = signal , background = 1 ) model.background.tau.fix ( 0 ) results , stats = Toys.parallel_toys2 ( gen_pdf = bkg_only , fit_pdf = model , nToys = 1000 , nSplit = 20 , data = [ mass ] , gen_config = { 'nEvents' : 100 , 'sample' : True } , fit_config = { 'silent' : True } , gen_pars = { 'tau_BKG' : 0. } , ## initial values for generation fit_pars = { 'B' : 100 , 'S' : 10 , 'phi0_Bkg_S': 0.0 } , ## initial fit values for parameters silent = True , progress = False ) for p in stats : logger.info ( "Toys: %-20s : %s" % ( p, stats [ p ] ) ) h_S = ROOT.TH1F ( hID() , '#S' , 60 , 0 , 60 ) for r in results ['S' ] : h_S .Fill ( r ) for h in ( h_S , ) : with wait ( 1 ) , use_canvas ( 'test_parallel_significance_toys: %s' % h.GetTitle() ) : h.draw() logger.info ( "%s :\n%s" % ( h.GetTitle() , h.dump ( 30 , 10 ) ) )
def test_dalitz4 () : logger = getLogger ('test_dalitz4' ) histos = [] for p in plots : h = ROOT.TH2D ( hID() , 'Dalitz plot %s' % p , 25 , 0 , 1 , 25 , 0 , 1 ) for s1,s2,s3 in p.random ( 10000 ) : h.Fill ( s1 , s2 ) with wait ( 2 ) , use_canvas( 'test_dalitz4 #%s' % d ) : gr = p.graph21 ( masses = False ) h.colz () gr.draw ( 'l' , linecolor = 2, linewidth = 5 ) histos.append ( ( h , gr ) )
def test_shapes_3d(): logger = getLogger("test_shapes_3d") if root_info < (6, 18): logger.info("Test is distabled for ROOT %s" % str(root_info)) return ## histogram as shape h2 = ROOT.TH3D(hID(), '', 30, -5, 5, 30, -10, 10, 30, -15, 15) h2 += lambda x, y, z: 100 * gauss(x) * gauss(y, sigma=2) * gauss(z, sigma=3) s2 = Models.Shape3D_pdf('S4', shape=h2, xvar=(-5, 5), yvar=(-10, 10), zvar=(-15, 15)) with wait(1), use_canvas("shape3d : histogram/x"): s2.draw1() with wait(1), use_canvas("shape3d : histogram/x in y-range"): s2.draw1(in_range2=(-3, 3)) with wait(1), use_canvas("shape3d : histogram/x in y&z-range"): s2.draw1(in_range2=(-3, 3), in_range3=(-3, 3)) with wait(1), use_canvas("shape3d : histogram/y"): s2.draw2() with wait(1), use_canvas("shape3d : histogram/y in x-range"): s2.draw2(in_range1=(-3, 3)) with wait(1), use_canvas("shape3d : histogram/y in x&z-range"): s2.draw2(in_range1=(-3, 3), in_range3=(-3, 3)) with wait(1), use_canvas("shape3d : histogram/z"): s2.draw3() with wait(1), use_canvas("shape3d : histogram/z in x-range"): s2.draw3(in_range1=(-3, 3)) with wait(1), use_canvas("shape3d : histogram/y in x&y-range"): s2.draw3(in_range1=(-3, 3), in_range2=(-3, 3))
def uPlot(pdf, data, bins=None, args=None, silent=False): """Make the plot of U-statistics >>> pdf = ... ## pdf >>> data = ... ## dataset >>> pdf.fitTo( data , ... ) ## fit it! >>> import ostap.stats.ustat as uStat >>> r,histo = uStat.uPlot ( pdf , data ) >>> print r ## print fit results >>> histo.Draw() ## plot the results """ if not bins or bins <= 0: nEntries = float(data.numEntries()) bins = 10 for nbins in (50, 40, 25, 20, 16, 10, 8, 5): if nEntries / nbins < 100: continue bins = nbins break print '#bins %s' % bins histo = ROOT.TH1F(hID(), 'U-statistics', bins, 0, 1) histo.Sumw2() histo.SetMinimum(0) if not args: args = pdf.getObservables(data) h, tStat = uCalc(pdf, args, data, histo, silent) res = histo.Fit('pol0', 'SLQ0+') func = histo.GetFunction('pol0') if func: func.SetLineWidth(3) func.SetLineColor(2) func.ResetBit(1 << 9) return res, histo, float(tStat)
def test_parameterize_1D(): with ROOT.TFile.Open(data_file, 'READ') as f: tree = f.S lx = Ostap.Math.LegendreSum(4, -2, 2) ly = Ostap.Math.LegendreSum(4, -2, 2) lz = Ostap.Math.LegendreSum(4, -4, 4) lu = Ostap.Math.LegendreSum(4, -4, 6) lx.parameterize(tree, 'x', cuts) ly.parameterize(tree, 'y', cuts) lz.parameterize(tree, 'z', cuts) lu.parameterize(tree, 'u', cuts) hx = ROOT.TH1D(hID(), '', 100, -2, 2) hy = ROOT.TH1D(hID(), '', 100, -2, 2) hz = ROOT.TH1D(hID(), '', 100, -4, 4) hu = ROOT.TH1D(hID(), '', 100, -4, 6) tree.project(hx, 'x', cuts) tree.project(hy, 'y', cuts) tree.project(hz, 'z', cuts) tree.project(hu, 'u', cuts) hx.SetMinimum(0) hx.draw() lx *= 0.04 ## bin-width lx.draw('same', linecolor=2) hy.SetMinimum(0) hy.draw() ly *= 0.04 ## bin-width ly.draw('same', linecolor=2) hz.SetMinimum(0) hz.draw() lz *= 0.08 ## bin-width lz.draw('same', linecolor=2) hu.SetMinimum(0) hu.draw() lu *= 0.10 ## bin-width lu.draw('same', linecolor=2) d1 = SE() d2 = SE() d3 = SE() d4 = SE() for i in range(1000): x = random.uniform(-2, 2) y = random.uniform(-2, 2) z = random.uniform(-4, 4) u = random.uniform(-4, 6) d1 += (hx(x) - lx(x)) / max(hx(x), lx(x)) d2 += (hy(y) - ly(y)) / max(hy(y), ly(y)) d3 += (hz(z) - lz(z)) / max(hz(z), lz(z)) d4 += (hu(u) - lu(u)) / max(hu(u), lu(u)) logger.info('1D-(x)-DIFFERENCES are %s ' % d1) logger.info('1D-(y)-DIFFERENCES are %s ' % d2) logger.info('1D-(z)-DIFFERENCES are %s ' % d3) logger.info('1D-(u)-DIFFERENCES are %s ' % d4)
if '__main__' == __name__ or '__builtin__' == __name__ : logger = getLogger ( 'test_histo_compare' ) else : logger = getLogger ( __name__ ) # ============================================================================= logger.info ( 'Test for 1D-histogram compare') # ============================================================================= from ostap.math.ve import VE from ostap.core.core import hID from ostap.histos.histos import h1_axis import ostap.histos.compare # ## 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 #
# ============================================================================= logger.info('Test for histogram parameterisation') # ============================================================================= use_scipy = False try: import scipy use_scipy = True except ImportError: use_scipy = False # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID from ostap.utils.timing import timing h1 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h1.Sumw2() h2 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h2.Sumw2() h3 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h3.Sumw2() h4 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h4.Sumw2() h5 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h5.Sumw2() h6 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h6.Sumw2() f1 = ROOT.TF1('f3', '(x-1)**2', 0, 1) f2 = ROOT.TF1('f4', 'x**2', 0, 1) f3 = ROOT.TF1('f3', '1-(x-1)**2', 0, 1)
else : logger = getLogger ( __name__ ) # ============================================================================= logger.info ( 'Test for histogram parameterisation') # ============================================================================= try : import scipy except ImportError : scipy = None # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID, fID from ostap.utils.timing import timing h1 = ROOT.TH1F ( hID () , 'decreasing convex ' , 100 , 0 , 1 ) ; h1.Sumw2 () h2 = ROOT.TH1F ( hID () , 'increasing convex ' , 100 , 0 , 1 ) ; h2.Sumw2 () h3 = ROOT.TH1F ( hID () , 'increasing concave' , 100 , 0 , 1 ) ; h3.Sumw2 () h4 = ROOT.TH1F ( hID () , 'decreasing concave' , 100 , 0 , 1 ) ; h4.Sumw2 () h5 = ROOT.TH1F ( hID () , 'symmetric convex ' , 100 , 0 , 1 ) ; h5.Sumw2 () h6 = ROOT.TH1F ( hID () , 'symmetric concave' , 100 , 0 , 1 ) ; h6.Sumw2 () f1 = ROOT.TF1 ( fID () , '(x-1)**2' , 0 , 1 ) f2 = ROOT.TF1 ( fID () , 'x**2' , 0 , 1 ) f3 = ROOT.TF1 ( fID () , '1-(x-1)**2' , 0 , 1 ) f4 = ROOT.TF1 ( fID () , '1-x**2' , 0 , 1 ) f5 = ROOT.TF1 ( fID () , '4*(x-0.5)**2' , 0 , 1 ) f6 = ROOT.TF1 ( fID () , '1-4*(x-0.5)**2' , 0 , 1 ) f_2 = ROOT.TF2 ( fID () , 'x*x+y*y' , -1 , 1 , 0 , 2 ) f_3 = ROOT.TF3 ( fID () , 'x*x+y*y+z*z' , -1 , 1 , 0 , 2 , -1 , 2 )
# ============================================================================= logger.info ( 'Test for histogram parameterisation') # ============================================================================= use_scipy = False try : import scipy use_scipy = True except ImportError : use_scipy = False # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID from ostap.utils.timing import timing h1 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h1.Sumw2() h2 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h2.Sumw2() h3 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h3.Sumw2() h4 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h4.Sumw2() h5 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h5.Sumw2() h6 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h6.Sumw2() f1 = ROOT.TF1 ( 'f3' , '(x-1)**2' , 0 , 1 ) f2 = ROOT.TF1 ( 'f4' , 'x**2' , 0 , 1 ) f3 = ROOT.TF1 ( 'f3' , '1-(x-1)**2' , 0 , 1 ) f4 = ROOT.TF1 ( 'f4' , '1-x**2' , 0 , 1 ) f5 = ROOT.TF1 ( 'f5' , '4*(x-0.5)**2' , 0 , 1 ) f6 = ROOT.TF1 ( 'f5' , '1-4*(x-0.5)**2' , 0 , 1 ) entries = 100000
# ============================================================================= use_scipy = False try : import numpy import scipy use_scipy = True except ImportError : use_scipy = False logger.warning ("Numpy/scipy-dependent are disables!") # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID , fID from ostap.utils.timing import timing h1 = ROOT.TH1F ( hID () , 'decreasing convex ' , 100 , 0 , 1 ) ; h1.Sumw2 () h2 = ROOT.TH1F ( hID () , 'increasing convex ' , 100 , 0 , 1 ) ; h2.Sumw2 () h3 = ROOT.TH1F ( hID () , 'increasing concave' , 100 , 0 , 1 ) ; h3.Sumw2 () h4 = ROOT.TH1F ( hID () , 'decreasing concave' , 100 , 0 , 1 ) ; h4.Sumw2 () h5 = ROOT.TH1F ( hID () , 'symmetric convex ' , 100 , 0 , 1 ) ; h5.Sumw2 () h6 = ROOT.TH1F ( hID () , 'symmetric concave' , 100 , 0 , 1 ) ; h6.Sumw2 () f1 = ROOT.TF1 ( fID () , '(x-1)**2' , 0 , 1 ) f2 = ROOT.TF1 ( fID () , 'x**2' , 0 , 1 ) f3 = ROOT.TF1 ( fID () , '1-(x-1)**2' , 0 , 1 ) f4 = ROOT.TF1 ( fID () , '1-x**2' , 0 , 1 ) f5 = ROOT.TF1 ( fID () , '4*(x-0.5)**2' , 0 , 1 ) f6 = ROOT.TF1 ( fID () , '1-4*(x-0.5)**2' , 0 , 1 ) entries = 1000000
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() ) )
def draw_summary(data=[], transpose=False, average=None, vmin=None, vmax=None, offset=0.5): """Prepare and draw the ``summary'' plot >>> data = [ Record ( 1.0 , 0.1 ,(-0.2, 0.5 ), label = 'LHCb' , color = 4 ) , ... Record ( 2.0 , 0.5 ,0.5 , label = 'Belle' , color = 3 , marker_style = 23 ) , ... Limit ( 2.5 , label = 'BESIII' ) ] >>> result = draw_summary ( data , vmax = 5 ) Also one can add colored bands for ``average'': >>> data = [ Record ( 1.0 , 0.1 ,(-0.2, 0.5 ), label = 'LHCb' , color = 4 ) , ... Record ( 2.0 , 0.5 ,0.5 , label = 'Belle' , color = 3 , marker_style = 23 ) , ... Limit ( 2.5 , label = 'BESIII' ) ] >>> average = Average ( 2.2 , 0.3 , Label = 'PDG' ) >>> result = draw_summary ( data , average = average , vmax = 5 ) ``Averag'' data can be also added into list of data points: >>> data = [ Record ( 1.0 , 0.1 ,(-0.2, 0.5 ), label = 'LHCb' , color = 4 ) , ... Record ( 2.0 , 0.5 ,0.5 , label = 'Belle' , color = 3 , marker_style = 23 ) , ... Limit ( 2.5 , label = 'BESIII' ) ] >>> average = Average ( 2.2 , 0.3 , Label = 'PDG' ) >>> result = draw_summary ( data + [ average ] , average = average , vmax = 5 ) """ summary = make_summary(data, average=average, transpose=transpose, offset=offset) xmin, xmax = summary.xminmax() ymin, ymax = summary.xminmax() if transpose: if vmin is None: vmin = ymin if vmax is None: vmax = ymax histo = ROOT.TH1F(hID(), '', 10, 0, len(summary) - 1 + 2 * offset) histo.GetXaxis().SetNdivisions(0) histo.SetMinimum(vmin) histo.SetMaximum(vmax) else: if vmin is None: vmin = xmin if vmax is None: vmax = xmax histo = ROOT.TH1F(hID(), '', 10, vmin, vmax) histo.GetYaxis().SetNdivisions(0) histo.SetMinimum(0) histo.SetMaximum(len(summary) - 1 + 2 * offset) summary.histo = histo summary.draw() return summary
import ostap.histos.histos import ostap.fitting.funcs # ============================================================================= logger.info('Test for histogram parameterisation') # ============================================================================= try: import scipy except ImportError: scipy = None # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID, fID from ostap.utils.timing import timing h1 = ROOT.TH1F(hID(), 'decreasing convex ', 100, 0, 1) h1.Sumw2() h2 = ROOT.TH1F(hID(), 'increasing convex ', 100, 0, 1) h2.Sumw2() h3 = ROOT.TH1F(hID(), 'increasing concave', 100, 0, 1) h3.Sumw2() h4 = ROOT.TH1F(hID(), 'decreasing concave', 100, 0, 1) h4.Sumw2() h5 = ROOT.TH1F(hID(), 'symmetric convex ', 100, 0, 1) h5.Sumw2() h6 = ROOT.TH1F(hID(), 'symmetric concave', 100, 0, 1) h6.Sumw2() f1 = ROOT.TF1(fID(), '(x-1)**2', 0, 1) f2 = ROOT.TF1(fID(), 'x**2', 0, 1) f3 = ROOT.TF1(fID(), '1-(x-1)**2', 0, 1)
# ============================================================================= # logging # ============================================================================= from ostap.logger.logger import getLogger if '__main__' == __name__ or '__builtin__' == __name__: logger = getLogger('test_histo_interpolation') else: logger = getLogger(__name__) # ============================================================================= logger.info('Test for 1,2&3D-histogram interpolation') # ============================================================================= import ROOT, random, ostap.histos.histos from ostap.core.core import hID, SE h1 = ROOT.TH1D(hID(), '', 5, 0, 1) h2 = ROOT.TH2D(hID(), '', 5, 0, 1, 5, 0, 1) h3 = ROOT.TH3D(hID(), '', 5, 0, 1, 5, 0, 1, 5, 0, 1) fun1 = lambda x: 0.1 + x**3 fun2 = lambda x, y: 0.1 + x**3 + y**2 + x * y fun3 = lambda x, y, z: 0.1 + x**3 + y**2 + x * y * z + z * x h1 += fun1 h2 += fun2 h3 += fun3 from collections import defaultdict ## test interpolation for 1D-historgams
else: logger = getLogger(__name__) # ============================================================================= logger.info('Test for 1D-histogram compare') # ============================================================================= from ostap.math.ve import VE from ostap.core.core import hID from ostap.histos.histos import h1_axis import ostap.histos.compare from builtins import range # ## 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)
else: logger = getLogger(__name__) # ============================================================================= logger.info('Test for histogram parameterisation') # ============================================================================= try: import scipy except ImportError: scipy = None # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID, fID from ostap.utils.timing import timing h1 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h1.Sumw2() h2 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h2.Sumw2() h3 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h3.Sumw2() h4 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h4.Sumw2() h5 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h5.Sumw2() h6 = ROOT.TH1F(hID(), 'histogram', 100, 0, 1) h6.Sumw2() f1 = ROOT.TF1(fID(), '(x-1)**2', 0, 1) f2 = ROOT.TF1(fID(), 'x**2', 0, 1) f3 = ROOT.TF1(fID(), '1-(x-1)**2', 0, 1)
# ============================================================================= logger.info('Test for histogram parameterisation') # ============================================================================= use_scipy = False try: import scipy use_scipy = True except ImportError: use_scipy = False # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID, fID from ostap.utils.timing import timing h1 = ROOT.TH1F(hID(), 'decreasing convex ', 100, 0, 1) h1.Sumw2() h2 = ROOT.TH1F(hID(), 'increasing convex ', 100, 0, 1) h2.Sumw2() h3 = ROOT.TH1F(hID(), 'increasing concave', 100, 0, 1) h3.Sumw2() h4 = ROOT.TH1F(hID(), 'decreasing concave', 100, 0, 1) h4.Sumw2() h5 = ROOT.TH1F(hID(), 'symmetric convex ', 100, 0, 1) h5.Sumw2() h6 = ROOT.TH1F(hID(), 'symmetric concave', 100, 0, 1) h6.Sumw2() f1 = ROOT.TF1(fID(), '(x-1)**2', 0, 1) f2 = ROOT.TF1(fID(), 'x**2', 0, 1) f3 = ROOT.TF1(fID(), '1-(x-1)**2', 0, 1)
def test_efficiency() : logger.info ( 'Test for "efficiencies" ') hA = ROOT.TH1F ( hID() , 'accepted' , 10 , 0 , 10 ) hR = ROOT.TH1F ( hID() , 'rejected' , 10 , 0 , 10 ) hT = ROOT.TH1F ( hID() , 'total' , 10 , 0 , 10 ) random.seed (100) for i in range(10000) : vx = random.uniform ( *hA.xminmax() ) accepted = random.uniform (0,1) < 0.01 if accepted : hA.Fill ( vx ) else : hR.Fill ( vx ) hT.Fill ( vx ) # ## applicbale for all cases: # eff_0 = hA / hT ## incorrect estimate of uncertainties efficiency ## (almost) correct estimate of uncertainties eff_1 = 1 / ( 1 + hR/hA ) ## correct estimate of uncertainties ## use Zech's prescription eff_2 = hA % hT ## use Zech's prescription, applicable for all cases eff_3 = hA.zechEff ( hT ) ## ditto ## only for histograms with true natural entries: eff_4 = hA // hT ## use binomial uncertainties eff_5 = hA.binomEff ( hT ) ## ditto eff_6 = hA.agrestiCoullEff ( hT ) ## use Argesti-Coull's recipe eff_7 = hA.wilsonEff ( hT ) ## use Wilson's recipe ## asymmetric binomial intervals # only for true natural entries # result in form of graph geff_8 = hA.eff_wald ( hR ) geff_9 = hA.eff_wilson_score ( hR ) geff_10 = hA.eff_wilson_score_continuity ( hR ) geff_11 = hA.eff_arcsin ( hR ) geff_12 = hA.eff_agresti_coull ( hR ) geff_13 = hA.eff_jeffreys ( hR ) geff_14 = hA.eff_clopper_pearson ( hR ) for e,n in [ ( eff_0 , 'Naive' ) , ( eff_1 , 'General' ) , ( eff_2 , 'Zech' ) , ( eff_3 , 'Zech/Operator' ) , ( eff_4 , 'Binomial: Simple' ) , ( eff_5 , 'Binomial: Simple/Operator' ) , ( eff_6 , 'Binomial: Agresti-Coull' ) , ( eff_7 , 'Binomial: Wilson' ) , ( geff_8 , 'Binomial: Wald interval' ) , ( geff_9 , 'Binomial: Wilson score interval' ) , ( geff_10 , 'Binomial: Wilson score/continuity interval' ) , ( geff_11 , 'Binomial: Arcsin interval' ) , ( geff_12 , 'Binomial: Agresti-Coull interval' ) , ( geff_13 , 'Binomial: Jeffreys interval' ) , ( geff_14 , 'Binomial: Clopper-Pearson interval' ) ] : if isinstance ( e , ROOT.TH1 ) : logger.info ( "%43s: %s" % ( n , [ e[i]*100 for i in e[:4] ] ) ) else : vals = [ (e[i][3]-abs(e[i][4]),e[i][3]+e[i][5]) for i in e[:3] ] vals = [ "(%7.4f,%7.4f)" % ( e[0]*100 , e[1]*100 ) for e in vals ] logger.info ( "%43s: %s" % ( n , vals ) )
def ds_draw(dataset, what, cuts='', opts='', *args): """Helper draw method for drawing of RooDataSet >>> dataset.draw ( 'm', 'chi2<10' ) ## cuts & weight >>> dataset.draw ( 'm', '(chi2<10)*weight' ) ## use drawing options >>> dataset.draw ( 'm', '(chi2<10)*weight' , 'e1' ) ## start form event #1000 >>> dataset.draw ( 'm', '(chi2<10)*weight' , 'e1' , 1000 ) ## for event in range 1000< i <10000 >>> dataset.draw ( 'm', '(chi2<10)*weight' , 'e1' , 1000 , 100000 ) """ if isinstance(cuts, ROOT.TCut): cuts = str(cuts).strip() if isinstance(what, str): what = what.strip() if isinstance(cuts, str): cuts = cuts.strip() if isinstance(opts, str): opts = opts.strip() ## delegate to TTree for non-weighted datasets with TTree-based storage type if hasattr ( dataset , 'isWeighted') and not dataset.isWeighted() \ and isinstance ( what , str ) \ and isinstance ( cuts , str ) \ and isinstance ( opts , str ) : if hasattr(dataset, 'store'): store = dataset.store() if store: tree = store.tree() if tree: return tree.Draw(what, cuts, opts, *args) if isinstance(what, str): vars = [v.strip() for v in what.split(':')] return ds_draw(dataset, vars, cuts, opts, *args) if isinstance(what, ROOT.RooFormulaVar): return ds_draw(dataset, what.GetTitle(), cuts, opts, *args) if isinstance(what, ROOT.RooAbsReal): return ds_draw(dataset, what.GetName(), cuts, opts, *args) if not 1 <= len(what) <= 3: raise AttributeError('DataSet::draw, invalid length %s' % what) if 1 == len(what): w1 = what[0] mn1, mx1 = ds_var_minmax(dataset, w1, cuts) histo = ROOT.TH1F(hID(), w1, 200, mn1, mx1) histo.Sumw2() ds_project(dataset, histo, what, cuts, *args) histo.Draw(opts) return histo if 2 == len(what): w1 = what[0] mn1, mx1 = ds_var_minmax(dataset, w1, cuts) w2 = what[1] mn2, mx2 = ds_var_minmax(dataset, w2, cuts) histo = ROOT.TH2F(hID(), "%s:%s" % (w1, w2), 50, mn1, mx1, 50, mn2, mx2) histo.Sumw2() ds_project(dataset, histo, what, cuts, *args) histo.Draw(opts) return histo if 3 == len(what): w1 = what[0] mn1, mx1 = ds_var_minmax(dataset, w1, cuts) w2 = what[1] mn2, mx2 = ds_var_minmax(dataset, w2, cuts) w3 = what[2] mn3, mx3 = ds_var_minmax(dataset, w3, cuts) histo = ROOT.TH3F(hID(), "%s:%s:%s" % (w1, w2, w3), 20, mn1, mx1, 20, mn2, mx2, 20, mn2, mx2) histo.Sumw2() ds_project(dataset, histo, what, cuts, *args) histo.Draw(opts) return histo raise AttributeError('DataSet::draw, invalid case')
if cuts : s = tree.statVar ( var , cuts ) else : s = tree.statVar ( var ) mn,mx = s.minmax() if mn < mn and 0.0 < delta : dx = delta * 1.0 * ( mx - mn ) mx += dx mn -= dx return mn , mx ROOT.TTree . vminmax = _tc_minmax_ ROOT.TChain . vminmax = _tc_minmax_ # ============================================================================= ## @var _h_one_ # special helper histogram for summation _h_one_ = ROOT.TH1D( hID() , '' , 3 , -1 , 2 ) ; _h_one_.Sumw2() # ============================================================================= ## make a sum over expression in Tree/Dataset # # @code # # >>> dataset = ... # ## get corrected number of events # >>> n_corr = dataset.sumVar ( "S_sw/effic" ) # # @endcode # # @author Vanya BELYAEV [email protected] # @date 2013-09-15 def _sum_var_old_ ( tree , expression ) : """Make a sum over expression in Tree/Dataset
else : logger = getLogger ( __name__ ) # ============================================================================= logger.info ( 'Test for histogram parameterisation') # ============================================================================= try : import scipy except ImportError : scipy = None # ============================================================================= from ostap.histos.param import legendre_sum, chebyshev_sum from ostap.core.core import hID, fID from ostap.utils.timing import timing h1 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h1.Sumw2() h2 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h2.Sumw2() h3 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h3.Sumw2() h4 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h4.Sumw2() h5 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h5.Sumw2() h6 = ROOT.TH1F ( hID() , 'histogram' , 100, 0 , 1 ) ; h6.Sumw2() f1 = ROOT.TF1 ( fID() , '(x-1)**2' , 0 , 1 ) f2 = ROOT.TF1 ( fID() , 'x**2' , 0 , 1 ) f3 = ROOT.TF1 ( fID() , '1-(x-1)**2' , 0 , 1 ) f4 = ROOT.TF1 ( fID() , '1-x**2' , 0 , 1 ) f5 = ROOT.TF1 ( fID() , '4*(x-0.5)**2' , 0 , 1 ) f6 = ROOT.TF1 ( fID() , '1-4*(x-0.5)**2' , 0 , 1 ) f_2 = ROOT.TF2 ( fID() , 'x*x+y*y' , -1 , 1 , 0 , 2 ) f_3 = ROOT.TF3 ( fID() , 'x*x+y*y+z*z' , -1 , 1 , 0 , 2 , -1 , 2 )