Skip to content

Commit

Permalink
more tweaks for style manaement
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Aug 5, 2024
1 parent 531e5f6 commit ddfd077
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 88 deletions.
72 changes: 37 additions & 35 deletions ostap/plotting/makestyles.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,22 +573,32 @@ def make_styles ( config = None ) :
ok = section.getboolean ( 'ostaplike' , fallback = True )

## create ostap-like style
if ok :
make_ostap_style ( name , description , section )
if ok : style = make_ostap_style ( name , description , section )
else :
## generic style
groot = ROOT.ROOT.GetROOT()
obj = groot.FindObject ( name )
if obj and isinstance ( obj , ROOT.TStyle ) :
logger.info ( 'Reuse Generic style %s/%s' % ( name , description ) )
style = style
else :
logger.debug ( 'Create Generic style %s/%s' % ( name , description ) )
## generic style

groot = ROOT.ROOT.GetROOT()
slst = groot.GetListOfStyles()
for s in slst :
if s and s.GetName() == name :
style = s
break
else :
logger.info ( 'Create new generic style %s/%s' % ( name , description ) )
style = ROOT.TStyle ( name , description )

set_style ( style , section )
if name in StyleStore.styles :
logger.warning ( "The configuration %s replaced" % name )
StyleStore.styles.update ( { name : style } )

if name in StyleStore.styles :
logger.warning ( "The configuration %s replaced" % name )
StyleStore.styles.update ( { name : style } )

if name.startswith('Style') :
nname = name[5:]
if nname in StyleStore.styles :
logger.info ( "The configuration %s replaced" % nname )
StyleStore.styles.update ( { nname : style } )


# ==============================================================================
def get_float ( config , name , default ) :
Expand Down Expand Up @@ -649,7 +659,7 @@ def make_ostap_style ( name ,
line_width = kw.pop ( 'line_width' , get_int ( config , 'line_width' , ostap_line_width ) )
if kw : logger.warning ("make_ostap_style: unprocessed keys: %s" % kw )

description = config.get ( 'description' , "The Style '%s'" % name )
description = config.get ( 'description' , description )

conf = {}
conf.update ( config )
Expand Down Expand Up @@ -876,8 +886,7 @@ def make_ostap_style ( name ,

conf [ 'MarkerColor' ] = get_int ( config , 'MarkerColor' , 1 )
conf [ 'MarkerStyle' ] = get_int ( config , 'MarkerStyle' , 20 )
conf [ 'MarkerSize' ] = get_float ( config , 'MarkerSize' , 1.0 * scale )

conf [ 'MarkerSize' ] = get_float ( config , 'MarkerSize' , 1.0 * ( 1 + ( scale - 1 ) * 0.5 ) )

##
## Text attributes
Expand All @@ -892,27 +901,20 @@ def make_ostap_style ( name ,
## maximal number of digits for the axis labels
conf [ 'TGaxisMaxDigits' ] = 3


## create the style
groot = ROOT.ROOT.GetROOT()
obj = groot.FindObject ( name )
if obj and isinstance ( obj , ROOT.TStyle ) :
logger.info ('Update existing Ostap style %s' % obj.GetName () )
style = obj
else :
logger.debug ('Create new Ostap style %s' % name )
style = ROOT.TStyle ( name , description )

set_style ( style , conf )
if name in StyleStore.styles :
logger.info ( "The configuration %s replaced" % name )
StyleStore.styles.update ( { name : style } )

if name.startswith('Style') :
nname = name[5:]
if nname in StyleStore.styles :
logger.info ( "The configuration %s replaced" % nname )
StyleStore.styles.update ( { nname : style } )
groot = ROOT.ROOT.GetROOT()
slst = groot.GetListOfStyles()
for s in slst :
if s and s.GetName() == name :
style = s
break
else :
logger.debug ( "Create new Ostap style `%s'" % name )
style = ROOT.TStyle ( name , description )

set_style ( style , conf )

return style

# =============================================================================
Expand Down
115 changes: 62 additions & 53 deletions ostap/plotting/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
'OstapStyle' , ## the function to instantiate the style
##
'StyleZ' , ## the style for COLZ plots
'Style1' , ## the default Ostap style
'Style2' , ## the style for downscaled 2-in-row plots
'Style3' , ## the style for downscaled 3-in-row plots
'Style1Z' , ## the style for COLZ plots
'Style2Z' , ## the style for downscaled 2-in-row COLZ plots
'Style3Z' , ## the style for downscaled 3-in-row COLZ plots
)
Expand Down Expand Up @@ -63,15 +65,17 @@ def OstapStyle ( name ,

## Look for "[Style:Nick]" in configuration
for key in CONFIG.config :
if not key.upper().startswith('STYLE') : continue
lkey = key.lower()
if not lkey.startswith('style') : continue
s , c , n = key.partition(':')
if not c : continue
n1 = n.strip()
n1 = n.strip() if c else s.strip()

if n1 == n2 :
config = CONFIG.config[key]
logger.info ( 'Use existing configuration style %s' % name )
break

import ostap.plotting.makestyles as MS
style = MS.make_ostap_style ( name = name ,
description = description ,
Expand All @@ -90,44 +94,49 @@ def OstapStyle ( name ,

return style

# =============================================================================
## style for standard COLZ plots
Style1Z = OstapStyle (
'Ostap1Z' ,
description = "Ostap style, suitable to produce plots with large right margin (COLZ)",
colz = True )
## ditto
StyleZ = Style1Z
# =============================================================================
## style for half-page-width COLZ plots
Style2Z = OstapStyle (
'Style2Z' ,
'Ostap2Z' ,
description = "Style, suitable to produce downscaled (2-in-row) plot with large right margin (COLZ)",
scale = 1.41 , colz = True )

scale = 1.41 ,
colz = True )
# =============================================================================
## style for one-third-page-width COLZ plots
Style3Z = OstapStyle (
'Style3Z' ,
'Ostap3Z' ,
description = "Style, suitable to produce downscaled (3-in-row) plot with large right margin (COLZ)",
scale = 1.71 , colz = True )

## style for standard COLZ plots
StyleZ = OstapStyle (
'Style1Z' ,
description = "Style, suitable to produce plot with large right margin (COLZ)",
colz = True )

scale = 1.71 ,
colz = True )
# ===========================================================================
## style for half-page-width plots
Style2 = OstapStyle (
'Style2' ,
description = "Style, suitable to produce downscaled (2-in-row) plots",
'Ostap2' ,
description = "Ostap style, suitable to produce downscaled (2-in-row) plots",
scale = 1.41 )

# ============================================================================
## style for one-third-page-width plots
Style3 = OstapStyle (
'Style3' ,
description = "Style, suitable to produce downscaled (3-in-row) plots",
'Ostap3' ,
description = "Ostap style, suitable to produce downscaled (3-in-row) plots",
scale = 1.71 )

# ============================================================================
## default style
Style = OstapStyle (
'Style' ,
description = 'The basic/default Ostap style for plots'
## default Ostap style
Style = OstapStyle (
'Ostap' ,
description = 'The basic/default Ostap style for plots' ,
force = True
)

## ditto
Style1 = Style
# ============================================================================
## default style
ostapStyle = Style
Expand All @@ -147,36 +156,36 @@ class UseStyle(object):
"""
def __init__ ( self, style = None , **config ) :

if isinstance ( style , int ):
if style is None : style = ostapStyle
elif isinstance ( style , int ):

if 1 == style : style = Style
if 0 == style : style = ostapStyle
elif 1 == style : style = Style1
elif 2 == style : style = Style2
elif 3 == style : style = Style3

if isinstance ( style , str ):
elif isinstance ( style , str ):

import ostap.plotting.makestyles as MS
if style in MS.StyleStore.styles : style = MS.StyleStore.styles [ style ]
elif style.upper() in ( '' , '0' , '1' ) : style = Style
elif style.upper() in ( '' , '0' , '1' , 'def' , 'default') : style = ostapStyle
elif '2' == style : style = Style2
elif '3' == style : style = Style3
elif style.upper() in ( 'Z' , '1Z' , 'Z1' ) : style = StyleZ
elif style.upper() in ( '2Z' , 'Z2' ) : style = Style2Z
elif style.upper() in ( '3Z' , 'Z3' ) : style = Style3Z

## use the style by name
if isinstance ( style , str ) :
groot = ROOT.ROOT.GetROOT()
styles = groot.GetListOfStyles()
for s in styles :
if s.GetName() == style :
style = s
break

if style is None : style = ROOT.gStyle
elif not isinstance ( style , ROOT.TStyle ) :
logger.warning ( 'No valid style "%s" is found, use default style' % style )
style = ostapStyle
else :
groot = ROOT.ROOT.GetROOT ()
slst = groot.GetListOfStyles()
for s in slst :
if s and s.GetName () == style :
style = s
break

if ( not style ) or ( not isinstance ( style , ROOT.TStyle ) ) :
unknown = style
style = ostapStyle
logger.warning ( "No valid style `%s'is found, use default `%s' style" % ( str ( unknown ) , style.GetName() ) )

self.__new_style = style
self.__old_style = None
Expand All @@ -192,45 +201,45 @@ def __enter__ ( self ) :
if self.__new_style :
self.__old_style = ROOT.gStyle
self.__new_style.cd ()

if self.__config :
self.__changed = set_style ( self.__new_style , self.__config )

groot.ForceStyle ( True )
if ROOT.gPad : ROOT.gPad.UseCurrentStyle()

logger.debug ( "Swith to `%s' style!" % self.__new_style.GetName() )

## context manager: exit
def __exit__ ( self , *_ ) :

if self.__changed :
self.__changed = set_style ( self.__new_style , self.__changed )
set_style ( self.__new_style , self.__changed )

if self.__old_style:
if self.__old_style :
self.__old_style.cd()
groot = ROOT.ROOT.GetROOT()
groot.ForceStyle ( self.__force_style )
logger.debug ( "Swith to `%s' style!" % self.__old_style.GetName() )

self.__new_style = None
self.__old_style = None

@property
def new_style ( self ) :
"``new_style'' : new style"
"`new_style' : new style"
return self.__new_style

@property
def old_style ( self ) :
"``old_style'' : old style"
"`old_style' : old style"
return self.__old_style

@property
def config ( self ) :
"""``config'' : addtional configuration parameters """
"""`config' : addtional configuration parameters """
return self.__config

@property
def changed ( self ) :
"""``changed'' : changed configuration parameters """
"""`changed' : changed configuration parameters """
return self.__changed

# =============================================================================
Expand Down

0 comments on commit ddfd077

Please sign in to comment.