Skip to content

Commit

Permalink
tiny fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Feb 8, 2024
1 parent c1320f0 commit 3995958
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 60 deletions.
10 changes: 8 additions & 2 deletions ostap/fitting/pyselectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,8 +1849,14 @@ def make_dataset ( tree ,

total = len ( tree )

if not silent :
pb = frame.ProgressBar ( total )
if not silent :
if ( 6 , 29 ) <= root_info :
from ostap.frames.frames import frame_progress2
pb = frame_progress2 ( frame )
else :
from ostap.frames.frames import frame_progress
pb = frame_progress ( frame , total )


columns = set ( tree.branches() ) | set ( tree.leaves() )

Expand Down
29 changes: 20 additions & 9 deletions ostap/frames/tree_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import ostap.trees.trees
from ostap.core.meta_info import root_info
from ostap.core.core import cpp, Ostap
from ostap.utils.cleanup import CleanUp
from ostap.utils.cleanup import CleanUp
from ostap.utils.basic import make_dirs
import ROOT, os, sys
# =============================================================================
# logging
Expand Down Expand Up @@ -62,14 +63,19 @@ def __init__ ( self ,
tmp_keep = False , ## keep the temporary file
silent = False ): ## silent processing

from ostap.frames.frames import DataFrame, frame_prescale
from ostap.frames.frames import DataFrame, frame_prescale
frame = DataFrame ( chain )
report = None

self.__frame_main = frame

if not silent :
pbar = frame.ProgressBar ( len ( chain ) )
if ( 6 , 29 ) <= root_info :
from ostap.frames.frames import frame_progress2
cnt = frame_progress2 ( frame )
else :
from ostap.frames.frames import frame_progress
cnt = frame_progress ( frame , len ( chain ) )

## add overall prescale (if requested)
if 1 != prescale :
Expand Down Expand Up @@ -133,8 +139,8 @@ def __init__ ( self ,
all_vars = list ( bvars ) + [ v for v in nvars if not v in bvars ]
save_vars = tuple ( [ v for v in all_vars if not v in no_vars ] )

nb_ = len ( chain.branches () )
ne_ = len ( chain )
nb0 = len ( chain.branches () )
ne0 = len ( chain )

if not silent and output and os.path.exists ( output ) and os.path.isfile ( output ) :
logger.warning ("Existing file %s will be overwritten!" % output )
Expand All @@ -153,6 +159,10 @@ def __init__ ( self ,
output = self.tempfile ( prefix = 'ostap-frame-' , suffix = '.root' )
if not tmp_keep : self.trash.add ( output )

dirname = os.path.dirname ( output )
if dirname and not os.path.exists ( dirname ) :
make_dirs ( dirname )

if not save_vars :
snapshot = frame.Snapshot ( name , output )
else :
Expand Down Expand Up @@ -192,10 +202,11 @@ def __init__ ( self ,

nb = len ( self.__chain.branches () )
ne = len ( self.__chain )

self.__report += '\n# Reduce %d -> %d branches, %d -> %d entries' % ( nb_ , nb , ne_ , ne )
self.__report += '\n# Output:%s size:%s' % ( self.__output , fs )
self.__report += '\n# %s' % str ( self.__chain )
ff = float ( nb * ne * 100 ) / ( nb0 * ne0 )

self.__report += '\n# Reduce: (%dx%d) -> (%dx%d) branches x entries => %.1f%% ' % ( nb0 , ne0 , nb , ne , ff ) )
self.__report += '\n# Output:%s size:%s' % ( self.__output , fs )
## self.__report += '\n# %s' % str ( self.__chain )

del self.__frame_main

Expand Down
20 changes: 17 additions & 3 deletions ostap/parallel/parallel_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
# =============================================================================
from ostap.parallel.parallel import Task, WorkManager
import ROOT
import ROOT, os
# =============================================================================
# logging
# =============================================================================
Expand Down Expand Up @@ -176,8 +176,22 @@ def reduce ( chain ,

nb = len ( result.chain.branches() )
ne = len ( result.chain )
f = float ( nb0 * ne0 ) / ( nb * ne )
logger.info ( 'reduce: (%dx%d) -> (%dx%d) %.1f (branches x entries) ' % ( nb0 , ne0 , nb , ne , f ) )
ff = float ( nb * ne * 100 ) / ( nb0 * ne0 )
logger.info ( 'Reduce: (%dx%d) -> (%dx%d) branches x entries => %.1f%% ' % ( nb0 , ne0 , nb , ne , ff ) )

if output and os.path.exists ( output ) and os.path.isfile ( output ) :
fs = os.path.getsize ( output )
gb , r = divmod ( fs , 1024 * 1024 * 1024 )
mb , r = divmod ( r , 1024 * 1024 )
kb , r = divmod ( r , 1024 )

if gb : fs = '%.1fGB' % ( float ( fs ) / 1024 / 1024 / 1024 )
elif mb : fs = '%.1fMB' % ( float ( fs ) / 1024 / 1024 )
elif kb : fs = '%.1fkB' % ( float ( fs ) / 1024 )
else : fs = '%sB' % fs

logger.info ( 'Output:%s size:%s' % ( output , fs )


return result

Expand Down
13 changes: 9 additions & 4 deletions ostap/plotting/graph_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,21 @@ def error_band2 ( value , epos , eneg , min_value , max_value , **kwargs ) :

## fill color
fcolor = config.get ( 'fill_color' , ROOT.kOrange )


delta = max_value - min_value
mnv = min_value + 0.001 * delta
mxv = max_value - 0.001 * delta

from itertools import count
for fc , ep , en in zip ( count ( fcolor , -1 ) , reversed ( epos ) , reversed ( eneg ) ) :

if not transpose :
box1 = ROOT.TBox ( value - en , min_value , value + ep , max_value )
box2 = ROOT.TBox ( value - en , min_value , value + ep , max_value )
box1 = ROOT.TBox ( value - en , mnv , value + ep , mxv )
box2 = ROOT.TBox ( value - en , mnv , value + ep , mxv )
else :
box1 = ROOT.TBox ( min_value , value - en , max_value , value + ep )
box2 = ROOT.TBox ( min_value , value - en , max_value , value + ep )
box1 = ROOT.TBox ( mnv , value - en , mxv , value + ep )
box2 = ROOT.TBox ( mnv , value - en , mxv , value + ep )

box1.set_fill_attributes ( **config )
box2.set_fill_attributes ( **config )
Expand Down
59 changes: 43 additions & 16 deletions ostap/tools/chopping.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,6 @@ def __init__ ( self ,
self.__signal_weight = signal_weight
self.__signal_cuts = ROOT.TCut ( signal_cuts )

self.__signal_vars = {}
if signal_vars : self.__signal_vars.update ( signal_vars )
self.__background_vars = {}
if background_vars : self.__background_vars.update ( background_vars )

self.__prefilter = ROOT.TCut ( prefilter )
self.__prefilter_signal = prefilter_signal if prefilter_signal else ''
self.__prefilter_background = prefilter_background if prefilter_background else ''
Expand All @@ -341,6 +336,9 @@ def __init__ ( self ,

variables = list ( variables )

_sig_vars = {}
_bkg_vars = {}

vars = []
for v in variables :
a , s1 , b = v.partition ( ':' ) ## a : b
Expand All @@ -363,6 +361,18 @@ def __init__ ( self ,

variables = vars
variables.sort ()

if signal_vars : _sig_vars.update ( signal_vars )
if background_vars : _bkg_vars.update ( background_vars )

signal_vars = _sig_vars
background_vars = _bkg_vars

self.__signal_vars = {}
if signal_vars : self.__signal_vars.update ( signal_vars )
self.__background_vars = {}
if background_vars : self.__background_vars.update ( background_vars )


self.__variables = tuple ( variables )

Expand Down Expand Up @@ -402,18 +412,28 @@ def __init__ ( self ,


##if self.verbose :
all_vars = [ self.prefilter ]
all_vars = []
for v in self.variables :
vv = v
if isinstance ( vv , str ) : vv = ( vv , 'F' )
if vv[0] in self.signal_vars : continue
elif vv[0] in self.background_vars : continue
else : all_vars.append ( vv[0] )

varexp = vv [ 0 ].strip()
nick , sep , expr = varexp.partition ( ":=" )
nick = nick.strip()
expr = expr.strip()
if nick and sep and expr : varexp = expr
else : nick = varexp

if varexp in self.signal_vars or ( nick and nick in self.signal_vars ) : continue
elif varexp in self.background_vars or ( nick and nick in self.background_vars ) : continue
else : all_vars.append ( varexp )

for v in self.spectators :
vv = v
if isinstance ( vv , str ) : vv = ( vv , 'F' )
all_vars.append ( vv[0] )

if self.prefilter : all_vars.append ( self.prefilter )
## if self.signal_cuts : all_vars.append ( self.signal_cuts )
## if self.signal_weight : all_vars.append ( self.signal_weight )
## if self.background_cuts : all_vars.append ( self.background_cuts )
Expand All @@ -429,10 +449,13 @@ def __init__ ( self ,

import ostap.trees.trees
avars = self.signal.the_variables ( all_vars )

keys = set ( self.background_vars.keys () ) - set ( self.signal_vars .keys () )
avars = tuple ( sorted ( set ( avars ).union ( keys ) ) )

scuts = {}
if self.prefilter_signal : scuts.update ( { 'PreFilter/Signal' : self.prefilter_signal } )
if self.prefilter : scuts.update ( { 'PreFilter' : self.prefilter } )
if self.prefilter : scuts.update ( { 'PreFilter/Common' : self.prefilter } )
if self.signal_cuts : scuts.update ( { 'Signal' : self.signal_cuts } )

if ( 6 , 24 ) <= root_info :
Expand All @@ -449,7 +472,7 @@ def __init__ ( self ,
new_vars = self.signal_vars ,
prescale = self.prescale_signal ,
silent = False )

self.__signal = self.__SigTR
self.__signal_cuts = ROOT.TCut()

Expand All @@ -460,11 +483,14 @@ def __init__ ( self ,
if self.background_weight : all_vars.append ( self.background_weight )

import ostap.trees.trees
avars = self.background.the_variables ( all_vars )
bvars = self.background.the_variables ( all_vars )

keys = set ( self.signal_vars.keys () ) - set ( self.background_vars .keys () )
bvars = tuple ( sorted ( set ( bvars ).union ( keys ) ) )

bcuts = {}
if self.prefilter_background : bcuts.update ( { 'PreFilter/Background' : self.prefilter_background } )
if self.prefilter : bcuts.update ( { 'PreFilter' : self.prefilter } )
if self.prefilter : bcuts.update ( { 'PreFilter/Common' : self.prefilter } )
if self.background_cuts : bcuts.update ( { 'Background' : self.background_cuts } )

if ( 6 , 24 ) <= root_info :
Expand All @@ -477,10 +503,10 @@ def __init__ ( self ,
self.logger.info ( 'Pre-filter Background before processing' )
self.__BkgTR = TR.reduce ( self.background ,
selection = bcuts ,
save_vars = avars ,
save_vars = bvars ,
new_vars = self.background_vars ,
prescale = self.prescale_background ,
silent = False )
silent = False )

self.__background = self.__BkgTR
self.__background_cuts = ROOT.TCut()
Expand Down Expand Up @@ -1249,7 +1275,8 @@ def make_tarfile ( self , tarfiles , logfiles = [] ) :
self.logger.verbose ( "Remove existing tar-logfile %s" % lfile )

with tarfile.open ( lfile , 'w:gz' ) as tar :
for x in logfiles: tar.add ( x )
for x in logfiles:
if os.path.exists ( x ) : tar.add ( x )
self.logger.info ( "Tar/gz logfile : %s" % lfile )
## if self.verbose : tar.list ()

Expand Down
Loading

0 comments on commit 3995958

Please sign in to comment.