Skip to content

Commit

Permalink
fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Jul 30, 2024
1 parent e21511e commit 8ea6d5f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 59 deletions.
61 changes: 18 additions & 43 deletions ostap/fitting/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ,
Expand Down
39 changes: 35 additions & 4 deletions ostap/stats/statvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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$"
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 )

Expand Down
23 changes: 11 additions & 12 deletions ostap/trees/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -517,16 +518,16 @@ def target_reset ( t ) :
ROOT.TTree .project = tree_project
ROOT.TChain.project = tree_project



# ======================================================================
def tree_draw ( tree ,
what ,
cuts = '' ,
first = 0 ,
last = LAST_ENTRY ,
use_frame = False ,
delta = 0.05 , **kwargs ) : ## use DataFrame ?



## adjust first/last indices
first , last = evt_range ( len ( tree ) , first , last )

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -630,6 +631,9 @@ def tree_draw ( tree ,
return histo





# =============================================================================
## check if object is in tree/chain :
# @code
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ,
Expand Down

0 comments on commit 8ea6d5f

Please sign in to comment.