From 8ea6d5fa9df51a339036f5ab5449f1e2af4e60d1 Mon Sep 17 00:00:00 2001 From: Vanya Belyaev Date: Tue, 30 Jul 2024 09:06:21 +0200 Subject: [PATCH] fix? --- ostap/fitting/dataset.py | 61 ++++++++++++---------------------------- ostap/stats/statvars.py | 39 ++++++++++++++++++++++--- ostap/trees/trees.py | 23 ++++++++------- 3 files changed, 64 insertions(+), 59 deletions(-) diff --git a/ostap/fitting/dataset.py b/ostap/fitting/dataset.py index 341ca9bd..e680b834 100644 --- a/ostap/fitting/dataset.py +++ b/ostap/fitting/dataset.py @@ -35,10 +35,13 @@ from ostap.core.ostap_types import ( integer_types , string_types , num_types , dictlike_types , list_types , sequence_types ) -from ostap.math.base import islong, axis_range +from ostap.math.base import islong from ostap.fitting.variables import valid_formula, make_formula from ostap.trees.cuts import expression_types, vars_and_cuts from ostap.utils.utils import evt_range, LAST_ENTRY + +from ostap.stats.statvars import data_decorate, data_range + import ostap.fitting.roocollections import ostap.fitting.printable import ROOT, random, math, sys, ctypes @@ -764,8 +767,6 @@ def _rds_make_unique_ ( dataset , ROOT.RooAbsData . sample = _rad_sample_ ROOT.RooAbsData . shuffle = _rad_shuffle_ -from ostap.stats.statvars import data_the_moment -ROOT.RooAbsData. the_moment = data_the_moment ROOT.RooDataSet . __sub__ = _rds_sub_ @@ -1231,30 +1232,6 @@ def ds_var_range ( dataset , var , cuts = '' ) : return axis_range ( mn , mx , delta = 0.05 ) -# =============================================================================\ -## get min/max for the expressions/variables -## @code -# dataset = ... -# result = ds_minmax ( dataset , 'sin(x)*100*y' , 'x<0' ) -# results = ds_minmax ( dataset , 'x,y,z,t,u,v' , 'x<0' ) -# @endcode -def ds_minmax ( dataset , - expressions , - cuts = '' , - cut_range = '' , - first = 0 , - last = LAST_ENTRY ) : - - if isinstance ( dataset , ROOT.RooAbsData ) : - args = dataset , expressions , cuts , cut_range , first, last - elif not cut_range : - args = dataset , expressions , cuts , first, last - else : - raise TypeError ( "dataset/cut_range : %s/`%s' are not consistent" % ( type ( dataset ), cut_range ) ) - - import ostap.stats.statvars as SV - return SV.data_minmax ( *args ) - # =============================================================================\ ## Get suitable ranges for drawing expressions/variables ## @code @@ -1269,20 +1246,18 @@ def ds_range ( dataset , first = 0 , last = LAST_ENTRY , delta = 0.05 ) : - - results = ds_minmax ( dataset , - expressions , - cuts = cuts , - cut_range = cut_range , - first = first , - last = last ) - - if isinstance ( results , dictlike_types ) : - for k , r in loop_items ( results ) : - results [ k ] = axis_range ( *r , delta = delta ) - else : results = axis_range ( *results , delta = delta ) - ## - return results + """ Get suitable ranges for drawing expressions/variables + >>> dataset = ... + >>> result = ds_range ( dataset , 'sin(x)*100*y' , 'x<0' ) + >>> results = ds_range ( dataset , 'x,y,z,t,u,v' , 'x<0' ) + """ + return data_range ( dataset , + expressions , + cuts , + delta , + cut_range , + first , + last ) # ============================================================================= ## clear dataset storage @@ -3132,8 +3107,8 @@ def _rad_rows_ ( dataset , variables = [] , cuts = '' , cutrange = '' , first = # ============================================================================ -from ostap.stats.statvars import data_decorate as _dd -_new_methods_ += list ( _dd ( ROOT.RooAbsData ) ) +_new_methods_ += list ( data_decorate ( ROOT.RooAbsData ) ) +del data_decorate _decorated_classes_ = ( ROOT.RooAbsData , diff --git a/ostap/stats/statvars.py b/ostap/stats/statvars.py index 567f2540..9e1d35cf 100644 --- a/ostap/stats/statvars.py +++ b/ostap/stats/statvars.py @@ -13,6 +13,7 @@ - data_central_moment - get the central moment (with uncertainty) - data_nEff - get the effective number of entries - data_sum - get the (weigted) sum +- data_minmax - get the (min/max) for variables - data_mean - get the mean (with uncertainty) - data_rms - get the RMS (with uncertainty) - data_variance - get the variance (with uncertainty) @@ -39,7 +40,7 @@ - data_power_mean - get the (weighted) power mean - data_lehmer_mean - get the (weighted) Lehmer mean - data_statistics - get the statistics for variables -- data_minmax - get the (min/max) for variables +- data_range - get the suitable ranged for drawing variables """ # ============================================================================= __version__ = "$Revision$" @@ -81,13 +82,16 @@ 'data_geometric_mean' , ## get the (weighted) geometric mean 'data_arithmetic_mean' , ## get the (weighted) arithmetic mean 'data_power_mean' , ## get the (weighted) power mean - 'data_lehmer_mean' , ## get the (weighted) Lehmer mean + 'data_lehmer_mean' , ## get the (weighted) Lehmer mean + ## + 'data_range' , ## get the suitable ranged for drawing variables + ## 'data_decorate' , ## technical function to decorate the class 'expression_types' , ## valid types for expressions/cuts/weights ) # ============================================================================= from builtins import range -from ostap.math.base import isequal, iszero +from ostap.math.base import isequal, iszero, axis_range from ostap.core.core import Ostap, rootException, WSE, VE, std from ostap.core.ostap_types import ( string_types , integer_types , num_types , dictlike_types ) @@ -308,6 +312,33 @@ def data_minmax ( data , expressions , cuts = '' , *args ) : ## return results +# =============================================================================\ +## Get suitable ranges for drawing expressions/variables +## @code +# dataset = ... +# result = data_range ( dataset , 'sin(x)*100*y' , 'x<0' ) +# results = data_range ( dataset , 'x,y,z,t,u,v' , 'x<0' ) ## as dictionary +# @endcode +# @see data_minmax +# @see data_statistics +# @see axis_range +def data_range ( data , + expressions , + cuts = '' , + delta = 0.05 , + *args ) : + """Get suitable ranges for drawing expressions/variables + >>> data = ... + >>> result = data_range ( data , 'sin(x)*100*y' , 'x<0' ) + >>> results = data_range ( dataset , 'x,y,z,t,u,v' , 'x<0' ) ## as dictionary + """ + results = data_minmax ( data, expressions , cuts , *args ) + if isinstance ( results , dictlike_types ) : + for k , r in loop_items ( results ) : + results [ k ] = axis_range ( *r , delta = delta ) + else : results = axis_range ( *results , delta = delta ) + ## + return results # ============================================================================== ## Get the covarince from dataxpressio @@ -417,7 +448,7 @@ def data_statvector ( data , N = len ( stats ) v = Ostap.Vector ( N ) () - for i in range ( N ) : v[i] = stats[i].mean() + for i in range ( N ) : v [ i ] = stats [ i ].mean() return Ostap.VectorE ( N ) ( v , cov2 ) diff --git a/ostap/trees/trees.py b/ostap/trees/trees.py index 57132ec5..c62d6d3e 100755 --- a/ostap/trees/trees.py +++ b/ostap/trees/trees.py @@ -37,6 +37,7 @@ from ostap.math.reduce import root_factory from ostap.utils.progress_bar import progress_bar from ostap.trees.cuts import vars_and_cuts +from ostap.stats.statvars import data_decorate , data_range import ostap.trees.treereduce import ostap.histos.histos import ostap.trees.param @@ -517,8 +518,7 @@ def target_reset ( t ) : ROOT.TTree .project = tree_project ROOT.TChain.project = tree_project - - +# ====================================================================== def tree_draw ( tree , what , cuts = '' , @@ -526,7 +526,8 @@ def tree_draw ( tree , last = LAST_ENTRY , use_frame = False , delta = 0.05 , **kwargs ) : ## use DataFrame ? - + + ## adjust first/last indices first , last = evt_range ( len ( tree ) , first , last ) @@ -537,7 +538,7 @@ def tree_draw ( tree , assert 1 <= nvars <= 3 , "Invalid number of variables: %s" % str ( varlst ) ## get the suitable ranges for the variables - ranges = tree_range ( dataset , varlst , cuts = cuts , first = first , last = last , delta = delta ) + ranges = data_range ( tree , varlst , cuts , delta , first, last ) for _, r in loop_items ( ranges ) : mn , mx = r ## no useful entries @@ -565,7 +566,7 @@ def tree_draw ( tree , # book the histogram histo = ROOT.TH1F ( hID() , "%s" % ( xvar ) , xbins , xmin , xmax ) ; histo.Sumw2() # - tree_project ( dataset , histo , varlst , cuts = cuts , first = first , last = last ) + tree_project ( tree , histo , varlst , cuts = cuts , first = first , last = last ) histo.draw ( opts , **kw ) return histo @@ -630,6 +631,9 @@ def tree_draw ( tree , return histo + + + # ============================================================================= ## check if object is in tree/chain : # @code @@ -663,11 +667,6 @@ def _rt_contains_ ( tree , obj ) : # ============================================================================= -from ostap.stats.statvars import data_the_moment - -ROOT.TTree . the_moment = data_the_moment -ROOT.TChain . the_moment = data_the_moment - # ============================================================================= ## Get min/max for the certain variable in chain/tree @@ -3185,8 +3184,8 @@ def use_aliases ( tree , **aliases ) : return UseAliases ( tree , **aliases ) # ============================================================================= -from ostap.stats.statvars import data_decorate as _dd -_new_methods_ = _dd ( ROOT.TTree ) +_new_methods_ = data_decorate ( ROOT.TTree ) +del data_decorate _new_methods_ += ( # ROOT.TTree .withCuts ,