Skip to content

Commit

Permalink
fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Dec 8, 2023
1 parent e30b746 commit c14f67f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
38 changes: 28 additions & 10 deletions ostap/fitting/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2289,18 +2289,30 @@ def _ds_wname_ ( dataset ) :

attr = '_weight_var_name'
if not hasattr ( dataset , attr ) :

if ( 6 , 26 ) <= root_info :
wv = dataset.weightVar()
wn = wv.name if wv else Ostap.Utils.getWeight ( dataset )
else : wn = Ostap.Utils.getWeight ( dataset )

wn = Ostap.Utils.getWeight ( dataset )
setattr ( dataset , attr , wn )

return getattr ( dataset , attr , '' )
# =============================================================================


# =============================================================================
## Get the weight variable from the dataset
# @code
# dataset = ...
# wvar = dataset.weightVar()
# @endcode
def _ds_weight_var_ ( dataset ) :
"""Get the weight variable from the dataset
>>> dataset = ...
>>> wvar = dataset.weightVar()
"""
wvar = Ostap.Utils.getWeightVar ( dataset )
return wvar
## return wvar if valid_pointer ( wvar ) else None

# =============================================================================
## Are weight errors stored in dataset?
# @code
Expand Down Expand Up @@ -2363,21 +2375,27 @@ def _ds_store_asym_error_ ( dataset ) :
# =============================================================================

ROOT.RooDataSet.wname = _ds_wname_
ROOT.RooDataSet.weight_name = _ds_wname_
ROOT.RooDataSet.store_error = _ds_store_error_
ROOT.RooDataSet.store_asym_error = _ds_store_asym_error_

if not hasattr ( ROOT.RooDataSet , 'weightVar' ) :
ROOT.RooDataSet.weightVar = _ds_weight_var_


_new_methods_ += [
ROOT.RooDataSet.wname ,
ROOT.RooDataSet.weight_name ,
ROOT.RooDataSet.store_error ,
ROOT.RooDataSet.store_asym_error ,
]

if sys.version_info < (3,0) :
def f_open ( name , mode , **kwargs ) :
return open ( name , mode )
else :
if (3,0) <= sys.version_info :
def f_open ( name , mode , **kwargs ) :
return open ( name , mode , **kwargs )
else :
def f_open ( name , mode , **kwargs ) :
return open ( name , mode )
# =============================================================================
## Convert dataset to CSV format
# @code
Expand Down
12 changes: 7 additions & 5 deletions ostap/fitting/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
from builtins import range
from ostap.core.meta_info import root_info
from ostap.math.base import doubles, iszero, isequal
from ostap.core.core import VE, hID, Ostap
from ostap.core.core import VE, hID, Ostap, valid_pointer
from ostap.math.reduce import root_factory
from ostap.core.ostap_types import ( num_types , list_types ,
integer_types , string_types ,
Expand Down Expand Up @@ -185,19 +185,21 @@ def _rrv_contains_ ( var , value ) :
## convert to float
ROOT.RooRealVar . __float__ = lambda s : s.getVal()
## print it in more suitable form
ROOT.RooRealVar . __repr__ = lambda s : "'%s' : %s " % ( s.GetName() , s.ve() )
ROOT.RooRealVar . __str__ = lambda s : "'%s' : %s " % ( s.GetName() , s.ve() )
ROOT.RooAbsReal . __repr__ = lambda s : 'nullptr' if not valid_pointer ( s ) else "'%s' : %s " % ( s.GetName() , s.getVal() )
ROOT.RooAbsReal . __str__ = lambda s : 'nullptr' if not valid_pointer ( s ) else "'%s' : %s " % ( s.GetName() , s.getVal() )
ROOT.RooRealVar . __repr__ = lambda s : 'nullptr' if not valid_pointer ( s ) else "'%s' : %s " % ( s.GetName() , s.ve() )
ROOT.RooRealVar . __str__ = lambda s : 'nullptr' if not valid_pointer ( s ) else "'%s' : %s " % ( s.GetName() , s.ve() )


ROOT.RooConstVar . as_VE = lambda s : VE ( s.getVal() , 0 )
ROOT.RooFormulaVar . as_VE = lambda s : VE ( s.getVal() , 0 )
ROOT.RooConstVar . asVE = lambda s : VE ( s.getVal() , 0 )
ROOT.RooFormulaVar . asVE = lambda s : VE ( s.getVal() , 0 )


ROOT.RooAbsReal . __float__ = lambda s : s.getVal() ## NB!!!
ROOT.RooRealVar . __float__ = lambda s : s.getVal()
ROOT.RooConstVar . __float__ = lambda s : s.getVal()
ROOT.RooAbsReal . __float__ = lambda s : s.getVal() ## NB!!!


ROOT.RooAbsReal .__contains__ = lambda s,v : False ## ??? do we need it???
ROOT.RooAbsRealLValue .__contains__ = _rrv_contains_
Expand Down
3 changes: 2 additions & 1 deletion ostap/stats/gof.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ def normalize ( ds , *others , weight = () , first = True ) :
else :
code2 = """
def normalize ( ds , others = () , weight = () , first = True ) :
result = normalize2 ( ( ds , *others ) , weight = weight , first = first )
datasets = [ ds ] + [ d for d in others ]
result = normalize2 ( datasets , weight = weight , first = first )
return result [ 0 ] if not others else resut
"""
exec ( code2 )
Expand Down
2 changes: 1 addition & 1 deletion source/include/Ostap/GetWeight.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Ostap
* @param data (INPUT) dataset
* @return the name of weigth variable, if and when possible
*/
std::string getWeight ( const RooAbsData* data ) ;
std::string getWeight ( const RooAbsData* data ) ;
// ========================================================================
/** get the weight variable from data set (if and when possible)
* @param data (INPUT) dataset
Expand Down
2 changes: 2 additions & 0 deletions source/src/GetWeight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace
* @return weigth variable, when possible
*/
// ============================================================================
#include <iostream>

const RooAbsReal* Ostap::Utils::getWeightVar ( const RooAbsData* data )
{
if ( nullptr == data || !data->isWeighted() ) { return nullptr ; }
Expand Down

0 comments on commit c14f67f

Please sign in to comment.