Skip to content

Commit

Permalink
fiz
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Aug 3, 2024
1 parent bd44aef commit 0db15da
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 41 deletions.
31 changes: 24 additions & 7 deletions ostap/histos/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,24 @@ def _gr_setitem_ ( graph , ipoint , point ) :
graph.SetPoint ( ipoint , x , y )


# ==============================================================================
## Does the graph constain the point with this index ?
# Negative indices are allowed
# @code
# graph = ..
# if 1 in graph : ...
# if -1 in graph : ...
# @endcode
def _gr_contains_ ( graph , index ) :
"""Does the graph constain the point with this index ?
- Negative indices are allowed
>>> graph = ..
>>> if 1 in graph : ...
>>> if -1 in graph : ...
"""
size = len ( graph )
return 0 <= index < size or 0 <= index + size < size

# ==============================================================================
## remove the point from the graph
# @code
Expand All @@ -779,12 +797,13 @@ def _gr_delitem_ ( graph, ipoint ) :
"""
#
if isinstance ( ipoint , slice ) :
points = sorted ( ( i for i in range ( *ipoint.indices ( len ( graph ) ) ) ) , reverse = True )
points = sorted ( ( i for i in range ( *ipoint.indices ( len ( graph ) ) ) ) )
while points : graph.RemovePoint ( points.pop () )
elif not ipoint in graph : raise IndexError
elif not ipoint in graph : raise IndexError
#
d = graph.RemovePoint ( ipoint )

if ipoint < 0 : ipoint += len ( graph )
graph.RemovePoint ( ipoint )

# =============================================================================
## iterate over the points in TGraph
# @code
Expand Down Expand Up @@ -1644,7 +1663,7 @@ def _grae_transform_ ( graph , fun = lambda x , y : y ) :

# =============================================================================
ROOT.TGraph . __len__ = ROOT.TGraph . GetN
ROOT.TGraph . __contains__ = lambda s,i : i in range(0,len(s))
ROOT.TGraph . __contains__ = _gr_contains_
ROOT.TGraph . __iter__ = _gr_iter_
ROOT.TGraph . __call__ = _gr_call_

Expand Down Expand Up @@ -1676,7 +1695,6 @@ def _grae_transform_ ( graph , fun = lambda x , y : y ) :
ROOT.TH1D.toGraph = hToGraph

ROOT.TGraphAsymmErrors.__len__ = ROOT.TGraphAsymmErrors . GetN
ROOT.TGraphAsymmErrors.__contains__ = lambda s,i : i in range(0,len(s))
ROOT.TGraphAsymmErrors.__iter__ = _gr_iter_
ROOT.TGraphAsymmErrors. items = _grae_iteritems_
ROOT.TGraphAsymmErrors. iteritems = _grae_iteritems_
Expand Down Expand Up @@ -3657,7 +3675,6 @@ def _rplot_iadd_ ( rp , other ) :
ROOT.TGraphErrors . __irshift__ ,
#
ROOT.TGraphAsymmErrors.__len__ ,
ROOT.TGraphAsymmErrors.__contains__ ,
ROOT.TGraphAsymmErrors.__iter__ ,
ROOT.TGraphAsymmErrors. items ,
ROOT.TGraphAsymmErrors. iteritems ,
Expand Down
28 changes: 14 additions & 14 deletions ostap/histos/histos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4135,25 +4135,25 @@ def _h1_add_function_integral_ ( h1 , func ) :
## get the running sum over the histogram bins
# @code
# h = ...
# ht = h.sumv ( increasing = True )
# hf = h.sumv ( increasing = False )
# ht = h.sumv ( forward = True )
# hf = h.sumv ( forward = False )
# @endcode
# It creates a new historgma with the same binning
# such that each bin<code>i</code> contains the sum over
# all bins `j` such as
# - \f$ j\le i \f$ if <code>increasing=True</code>
# - \f$ j\ge i \f$ if <code>increasing=False</code>
# - \f$ j\le i \f$ if <code>forward=True</code>
# - \f$ j\ge i \f$ if <code>forward=False</code>
# @author Vanya BELYAEV [email protected]
# @date 2011-06-07
def h1_sumv ( histo , increasing = True ) :
def h1_sumv ( histo , forward = True ) :
"""Create the `running sum' over the histogram bins
>>> h = ...
>>> h1 = h.sumv( increasing = True )
>>> h2 = h.sumv( increasing = False )
>>> h1 = h.sumv( forward = True )
>>> h2 = h.sumv( forward = False )
It creates a new histogram with the same binning
such that each bin `i` contains the sum over all bins `j` such as
- `j<=i` if `increasing=True`
- `i<=j` if `increasing=False`
- `j<=i` if `forward=True`
- `i<=j` if `forward=False`
"""
assert isinstance ( histo , ROOT.TH1 ) and 1 == histo.dim() , \
"Invalid `histo' type %s" % type ( histo )
Expand All @@ -4162,7 +4162,7 @@ def h1_sumv ( histo , increasing = True ) :
result.Reset()
if not result.GetSumw2() : result.Sumw2()

if increasing :
if forward :

sumi = VE ( 0 , 0 )
for i , x , y in histo.items() :
Expand Down Expand Up @@ -4869,7 +4869,7 @@ def _fom_2_ ( h1 , increase = True ) :
>>> f1 = h1.FoM2 ()
"""
#
h = h1.sumv( increase )
h = h1.sumv( forward = increase )
#
return _h1_transform_ ( h , func = lambda x,y : y.precision() )

Expand Down Expand Up @@ -4914,8 +4914,8 @@ def _fom_1_ ( s , b , alpha = 1 , increase = True ) :
h = s.Clone( hID() )
if not h.GetSumw2() : h.Sumw2()
#
hs = s.sumv ( increase )
hb = b.sumv ( increase )
hs = s.sumv ( forward = increase )
hb = b.sumv ( forward = increase )
#
from math import sqrt, pow
#
Expand Down Expand Up @@ -8354,7 +8354,7 @@ def histo_book ( ranges , kwargs , title = '' ) :
def _h1_roc_ ( h1 , h2 ) :

h1sum = h1.sumv ()
h2sum= h2.sumv ()
h2sum = h2.sumv ()

import ostap.histos.graphs
graph = ROOT.TGraphErrors ( len ( h1 ) + 2 )
Expand Down
44 changes: 34 additions & 10 deletions ostap/histos/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'roc_curve' , # Make ROC curve form signal & backgrund distributions
)
# =============================================================================
from ostap.core.ostap_types import string_types
from ostap.core.ostap_types import string_types
from ostap.math.ve import VE
import ostap.histos.histos
import ostap.histos.graphs
import ROOT
Expand All @@ -35,6 +36,8 @@
_effs = ( 'e' , 'eff' , 'effs' , 'effic' , 'efficiency' )
## symbols to indicate the rejection
_rejs = ( 'r' , 'rej' , 'reject' , 'rejection' )
## symbols to indicate the suppression
_sups = ( 's' , 'sup' , 'supp' , 'suppress' , 'suppression' )
# =============================================================================
## Build the ROC-curve from signal and background disctributuions
# @param signal (histogram) of signal distribution
Expand All @@ -46,7 +49,7 @@
# roc = roc_curve ( signal = hsig ,
# backgrund = hbkg ,
# cut_low = True , ## "keep valeus less than cut value"
# show_sinal = 'efficiency' ,
# show_signal = 'efficiency' ,
# show_backgrund = 'rejection' )
#
# import ostap.math,integral as I
Expand Down Expand Up @@ -78,21 +81,25 @@ def roc_curve ( signal ,
"Invalid `signal' type: %s" % type ( signal )
assert isinstance ( background , ROOT.TH1 ) and 1 == background.dim() , \
"Invalid `background' type: %s" % type ( background )

if show_signal is None : show_signal = lambda e : e
if show_background is None : show_background = lambda e : 1.0-e


def _fun_ ( obj ) :
if callable ( obj ) : return obj
assert isinstance ( obj , string_types ) , 'Invalid type: %s' % type ( obj )
obj = str ( obj ).strip ().lower ()
if obj in _effs : return lambda e : e
elif obj in _rejs : return lambda e : 1.0-e
elif obj in _sups : return lambda e : 1.0/e
raise TypeError ( 'Invalid object: %s' % obj )

## transformations :

sig_fun = _fun_ ( show_signal )
bkg_fun = _fun_ ( show_background )



hs = signal
hb = background

Expand All @@ -106,19 +113,36 @@ def _fun_ ( obj ) :
np = len ( hse )
graph = ROOT.TGraphErrors ( np )

bad_points = set()
## loop over signal efficiency
for i , xs , es in hse.items() :

## background efficiency
eb = hbe ( xs.value() )

## transform if requested:

es = sig_fun ( es )
eb = bkg_fun ( eb )

graph [ i - 1 ] = es , eb
ipoint = i - 1

try :

## transform if requested:
es = VE ( sig_fun ( es ) )
eb = VE ( bkg_fun ( eb ) )

if es.isgood () ) and eb.isgood() :
graph [ ipoint ] = es , eb
else :
bad_points.add ( ipoint )

except ( ArithmeticError, ValueError ) :
bad_points.add ( ipoint )

if bad_points :
logger.warning ( "%d bad points are removed" % len ( bad_points ) )
bad_points = sorted ( list ( bad_points ) )
while bad_points :
index = bad_points.pop()
del graph [ index ]

return graph


Expand Down
16 changes: 7 additions & 9 deletions ostap/histos/tests/test_histos_histos.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
# =============================================================================
logger.info ( 'Test for basic operations with histograms')
# =============================================================================
# =============================================================================
## Test for very basic operations with 1D-histograms
def test_basic_1D() :

Expand Down Expand Up @@ -86,17 +85,16 @@ def test_basic_1D() :

## running sum
h1_d = h1.sumv() ## default
h1_i = h1.sumv( increasing = True ) ## ditto
h1_r = h1.sumv( increasing = False ) ## ditto
h1_i = h1.sumv( forward = True ) ## ditto
h1_r = h1.sumv( forward = False ) ## ditto

## histogram efficiney of cuts
eff_i = h1.effic( increasing = True )
eff_r = h1.effic( increasing = False )
## histogram efficiemcy of cuts
eff_i = h1.effic( cut_low = True )
eff_r = h1.effic( cut_low = False )

## efficiency of certaine value
e1_i = h1.efficiency( 0.3 , increasing = True )
e2_i = h1.efficiency( 0.3 , increasing = False )

e1_i = h1.efficiency( 0.3 , cut_low = True )
e2_i = h1.efficiency( 0.3 , cut_low = False )

## smear the histogram
h0 = ROOT.TH1F( hID() , '', 400 , -1 , 1 )
Expand Down
2 changes: 1 addition & 1 deletion ostap/histos/tests/test_histos_roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_roc () :
( grs[-1][1].toString( '%+.3f +/- %-.3f' ) ,
grb[-1][1].toString( '%+.3f +/- %-.3f' ) ) )

## make ROC corve
## make ROC curve
roc = roc_curve ( signal = hs ,
background = hb ,
cut_low = True )
Expand Down

0 comments on commit 0db15da

Please sign in to comment.