Skip to content

Commit

Permalink
1. Some tweaks for style configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Aug 4, 2024
1 parent a403603 commit a78e9fb
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 76 deletions.
1 change: 1 addition & 0 deletions ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
1. Some tweaks for moments & counters
1. Activate a new `draw` method (via `tree_draw`) for `ROOT.TTree`
1. add `progress` and `report` optioal argumens for (almost) all Frame-related functions
1. Some tweaks for styel configuration

## Backward incompatible

Expand Down
34 changes: 30 additions & 4 deletions ostap/histos/histos.py
Original file line number Diff line number Diff line change
Expand Up @@ -8267,7 +8267,11 @@ def _h3_scale_axes_ ( h3 , scalex , scaley = 1.0 , scalez = 1.0 ) :
'xmin' , 'xmax' ,
'ymin' , 'ymax' ,
'zmin' , 'zmax' ,
'title' )
'title' ,
'xtitle', 'titlex' ,
'ytitle', 'titley' ,
'ztitle', 'titlez' )

# =============================================================================
## helper method to book/create 1/2/3-dimension histograms
# @code
Expand Down Expand Up @@ -8331,20 +8335,42 @@ def histo_book ( ranges , kwargs , title = '' ) :

histo = None
if 1 == nvars :
title = kwargs.pop ( 'title' , 'x: %s' % xvar )

title = kwargs.pop ( 'title' , "x=`%s'" % xvar )
histo = ROOT.TH1D ( hID() , title , xbins , xmin , xmax ) ; histo.Sumw2()

xtitle = kwargs.pop ( 'xtitle' , kw.pop ( 'titlex' , xvar ) )
histo.GetXaxis().SetTitle ( xtitle )

elif 2 == nvars :
title = kwargs.pop ( 'title' , 'x: %s ; y: %s' % ( xvar, yvar ) )

title = kwargs.pop ( 'title' , "x=`%s' y=`%s' z=`%s'" % ( xvar, yvar ) )
histo = ROOT.TH2F ( hID() , title ,
xbins , xmin , xmax ,
ybins , ymin , ymax ) ; histo.Sumw2()

xtitle = kwargs.pop ( 'xtitle' , kw.pop ( 'titlex' , xvar ) )
ytitle = kwargs.pop ( 'ytitle' , kw.pop ( 'titley' , yvar ) )

histo.GetXaxis().SetTitle ( xtitle )
histo.GetYaxis().SetTitle ( ytitle )

elif 3 == nvars :
title = kwargs.pop ( 'title' , 'x: %s ; y: %s; z: %s' % ( xvar, yvar , zvar ) )

title = kwargs.pop ( 'title' , "x=`%s' y=`%s' z=`%s'" % ( xvar, yvar , zvar ) )
histo = ROOT.TH3F ( hID() , title ,
xbins , xmin , xmax ,
ybins , ymin , ymax ,
zbins , zmin , zmax ) ; histo.Sumw2()

xtitle = kwargs.pop ( 'xtitle' , kw.pop ( 'titlex' , xvar ) )
ytitle = kwargs.pop ( 'ytitle' , kw.pop ( 'titley' , yvar ) )
ztitle = kwargs.pop ( 'ztitle' , kw.pop ( 'titlez' , zvar ) )

histo.GetXaxis().SetTitle ( xtitle )
histo.GetYaxis().SetTitle ( ytitle )
histo.GetZaxis().SetTitle ( ztitle )

return histo
# =============================================================================

Expand Down
93 changes: 55 additions & 38 deletions ostap/plotting/makestyles.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@
ostap_latex . SetTextColor ( 1 )
ostap_latex . SetTextSize ( 0.04 )
ostap_latex . SetTextAlign ( 12 )
# ==============================================================================

# ==============================================================================
## @class StyleStore
# Store for all created/configured styles
Expand Down Expand Up @@ -562,7 +560,7 @@ def make_styles ( config = None ) :
if config is None :
import ostap.core.config as _CONFIG
config = _CONFIG.config

for key in config :

if not key.upper().startswith('STYLE') : continue
Expand All @@ -572,14 +570,21 @@ def make_styles ( config = None ) :
## the style name
name = n.strip ( )
description = section.get ( 'description' , fallback = 'The style %s' % name )
ok = section.getboolean ( 'ostaplike' , fallback = False )
ok = section.getboolean ( 'ostaplike' , fallback = True )

## create ostap-like style
if ok : make_ostap_style ( name , description , section )
else :
if ok :
make_ostap_style ( name , description , section )
else :
## generic style
logger.info ( 'Create Generic style %s/%s' % ( name , description ) )
style = ROOT.TStyle ( name , description )
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 ) )
style = ROOT.TStyle ( name , description )
set_style ( style , section )
if name in StyleStore.styles :
logger.warning ( "The configuration %s replaced" % name )
Expand Down Expand Up @@ -630,18 +635,25 @@ def get_str ( config , name , default ) :

# ============================================================================
## make Ostap-like style
def make_ostap_style ( name ,
description = 'The Style' ,
config = {} ,
colz = False ,
scale = 1.0 ,
font = ostap_font ,
line_width = ostap_line_width ) :
def make_ostap_style ( name ,
description = 'The Style' ,
config = {} , **kwargs ) :


description = config.get ( 'description' , 'The Style' )
kw = cidict ( transform = cidict_fun )
kw.update ( kwargs )

colz = kw.pop ( 'colz' , get_bool ( config , 'colz' , False ) )
scale = kw.pop ( 'scale' , get_float ( config , 'scale' , 1.0 ) )
font = kw.pop ( 'font' , get_int ( config , 'font' , ostap_font ) )
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 )

conf = {}
conf.update ( config )


conf [ 'AxisColor_X' ] = get_int ( config , 'AxisColor_X' , 1 )
conf [ 'AxisColor_Y' ] = get_int ( config , 'AxisColor_Y' , 1 )
Expand All @@ -664,7 +676,7 @@ def make_ostap_style ( name ,

conf [ 'DrawBorder' ] = get_int ( config , 'DrawBorder' , 0 )

conf [ 'EndErrorSize' ] = get_float ( config , 'EndErrorSize' , 2.0 )
conf [ 'EndErrorSize' ] = get_float ( config , 'EndErrorSize' , 2.0 * scale )
conf [ 'ErrorX' ] = get_float ( config , 'ErrorX' , 0.5 )

conf [ 'FitFormat' ] = get_str ( config , 'FitFormat' , '5.4g' )
Expand Down Expand Up @@ -692,7 +704,7 @@ def make_ostap_style ( name ,
conf [ 'HistFillStyle' ] = get_int ( config , 'HistFillStyle' , 1001 )
conf [ 'HistLineColor' ] = get_int ( config , 'HistLineColor' , 1 )
conf [ 'HistLineStyle' ] = get_int ( config , 'HistLineStyle' , 1 )
conf [ 'HistLineWidth' ] = get_int ( config , 'HistLineStyle' , line_width )
conf [ 'HistLineWidth' ] = get_int ( config , 'HistLineStyle' , line_width )

conf [ 'HistMinimumZero' ] = get_bool ( config , 'HistMinimumZero' , False )
conf [ 'HistTopMargin' ] = get_float ( config , 'HistTopMargin' , 0.05 )
Expand All @@ -711,9 +723,9 @@ def make_ostap_style ( name ,
conf [ 'LabelOffset_Y' ] = get_float ( config , 'LabelOffset_Y' , 0.005 )
conf [ 'LabelOffset_Z' ] = get_float ( config , 'LabelOffset_Z' , 0.005 )

conf [ 'LabelSize_X' ] = get_float ( config , 'LabelSize_X' , 0.05 )
conf [ 'LabelSize_Y' ] = get_float ( config , 'LabelSize_Y' , 0.05 )
conf [ 'LabelSize_Z' ] = get_float ( config , 'LabelSize_Z' , 0.05 )
conf [ 'LabelSize_X' ] = get_float ( config , 'LabelSize_X' , 0.05 * scale )
conf [ 'LabelSize_Y' ] = get_float ( config , 'LabelSize_Y' , 0.05 * scale )
conf [ 'LabelSize_Z' ] = get_float ( config , 'LabelSize_Z' , 0.05 * scale )

conf [ 'LegendBorderSize' ] = get_int ( config , 'LegendBorderSize' , 4 )
conf [ 'LegendFillColor' ] = get_int ( config , 'LegendFillColor' , 0 )
Expand Down Expand Up @@ -742,8 +754,8 @@ def make_ostap_style ( name ,

conf [ 'NumberContours' ] = get_int ( config , 'NumberContours' , 127 )

## conf [ 'NumberOfColors' ] = get_int ( config , 'NumberOfColors' , 255 )
## conf [ 'NumberOfColors' ] = get_int ( config , 'NumberOfColors' , 255 )

conf [ 'OptDate' ] = get_int ( config , 'OptDate' , 0 )
conf [ 'OptFile' ] = get_int ( config , 'OptFile' , 0 )
conf [ 'OptFit' ] = get_int ( config , 'OptFit' , 0 )
Expand Down Expand Up @@ -789,7 +801,7 @@ def make_ostap_style ( name ,
conf [ 'StatBorderSize' ] = get_int ( config , 'StatBorderSize' , 0 )
conf [ 'StatColor' ] = get_int ( config , 'StatColor' , 0 )
conf [ 'StatFont' ] = get_int ( config , 'StatFont' , font )
conf [ 'StatFontSize' ] = get_float ( config , 'StatFontSize' , 0.05 )
conf [ 'StatFontSize' ] = get_float ( config , 'StatFontSize' , 0.05 * scale )
conf [ 'StatFormat' ] = get_str ( config , 'StatFormat' , '6.3g' )
conf [ 'StatH' ] = get_float ( config , 'StatH' , 0.15 ) ## ???
conf [ 'StatStyle' ] = get_int ( config , 'StatStyle' , 1001 )
Expand Down Expand Up @@ -819,7 +831,7 @@ def make_ostap_style ( name ,
conf [ 'TitleFont_Y' ] = get_int ( config , 'TitleFont_Y' , font )
conf [ 'TitleFont_Z' ] = get_int ( config , 'TitleFont_Z' , font )

conf [ 'TitleFontSize' ] = get_int ( config , 'TitleFontSize' , 0.0 )
conf [ 'TitleFontSize' ] = get_int ( config , 'TitleFontSize' , 0.0 * scale )

conf [ 'TitleH' ] = get_float ( config , 'TitleH' , 0.0 )

Expand All @@ -829,20 +841,20 @@ def make_ostap_style ( name ,

conf [ 'TitlePS' ] = get_str ( config , 'TitlePS' , '' )

conf [ 'TitleSize_X' ] = get_float ( config , 'TitleSize_X' , -1.0 )
conf [ 'TitleSize_Y' ] = get_float ( config , 'TitleSize_Y' , 0.05 )
conf [ 'TitleSize_Z' ] = get_float ( config , 'TitleSize_Z' , 0.05 )
conf [ 'TitleSize_X' ] = get_float ( config , 'TitleSize_X' , -1.0 * scale )
conf [ 'TitleSize_Y' ] = get_float ( config , 'TitleSize_Y' , 0.05 * scale )
conf [ 'TitleSize_Z' ] = get_float ( config , 'TitleSize_Z' , 0.05 * scale )

conf [ 'TitleStyle' ] = get_int ( config , 'TitleStyle' , 1001 )
conf [ 'TitleTextColor' ] = get_int ( config , 'TitleTextColor' , 1 )

conf [ 'TitleW' ] = get_float ( config , 'TitleW' , 0.0 )
conf [ 'TitleX' ] = get_float ( config , 'TitleX' , 0.01 )
conf [ 'TitleXOffset' ] = get_float ( config , 'TitleXOffset' , 1.0 )
conf [ 'TitleXSize' ] = get_float ( config , 'TitleXSize' , -1.0 )
conf [ 'TitleY' ] = get_float ( config , 'TitleY' , 0.99 )
conf [ 'TitleYOffset' ] = get_float ( config , 'TitleYOffset' , 0.0 )
conf [ 'TitleYSize' ] = get_float ( config , 'TitleYSize' , 0.05 )
conf [ 'TitleXSize' ] = get_float ( config , 'TitleXSize' , -1.0 * scale )
conf [ 'TitleY' ] = get_float ( config , 'TitleY' , 0.99 * scale )
conf [ 'TitleYOffset' ] = get_float ( config , 'TitleYOffset' , 0.0 * scale )
conf [ 'TitleYSize' ] = get_float ( config , 'TitleYSize' , 0.05 * scale )

##
## Line attributes
Expand All @@ -864,7 +876,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 )
conf [ 'MarkerSize' ] = get_float ( config , 'MarkerSize' , 1.0 * scale )


##
Expand All @@ -875,17 +887,22 @@ def make_ostap_style ( name ,
conf [ 'TextAngle' ] = get_float ( config , 'TextAngle' , 0.0 )
conf [ 'TextColor' ] = get_int ( config , 'TextColor' , 1 )
conf [ 'TextFont' ] = get_int ( config , 'TextFont' , font )
conf [ 'TextSize' ] = get_float ( config , 'TextSize' , 0.08 )
conf [ 'TextSize' ] = get_float ( config , 'TextSize' , 0.08 * scale )

## 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 )

## create the style
style = ROOT.TStyle ( name , description )
set_style ( style , conf )
logger.debug ('Create Ostap style %s' % style.GetName() )

if name in StyleStore.styles :
logger.info ( "The configuration %s replaced" % name )
StyleStore.styles.update ( { name : style } )
Expand Down
54 changes: 20 additions & 34 deletions ostap/plotting/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,58 +48,43 @@ def OstapStyle ( name ,
description ,
line_width = ostap_line_width ,
font = ostap_font ,
makeNew = False ,
force = True ,
scale = 1.0 ,
colz = False ) :
"""Create Ostap-style for the plots
"""
groot = ROOT.ROOT.GetROOT()
obj = groot.FindObject ( name )
if obj and isinstance ( obj , ROOT.TStyle ) and not makeNew :
logger.info ('The style %s is reused' % obj.GetName() )
if force :
obj.cd ()
logger.info ('The style %s is forced' % obj.GetName() )
groot.SetStyle ( obj.GetName() )
groot.ForceStyle ( )
return obj

nam = name
i = 1
while obj :
nam = name + '_%d' % i
obj = groot.FindObject ( nam )
i += 1

# ================================================================
## check the configuration
import ostap.core.config as CONFIG

config = {}
n2 = name.strip()

## Look for "[Style:Nick]" in configuration
for key in CONFIG.config :
if not key.upper().startswith('STYLE') : continue
s , c , n = key.partition(':')
if not c : continue
n1 = n.strip()
n2 = name.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 = nam ,
style = MS.make_ostap_style ( name = name ,
description = description ,
config = config ,
colz = colz ,
scale = scale ,
font = font ,
line_width = line_width )
# =======================================================================
if force :
style . cd()
logger.debug ('The style %s is forced' % style.GetName() )
groot = ROOT.ROOT.GetROOT()
groot.SetStyle ( style.GetName() )
groot.ForceStyle ()

Expand All @@ -108,39 +93,40 @@ def OstapStyle ( name ,
# =============================================================================
## style for half-page-width COLZ plots
Style2Z = OstapStyle (
'Style2Z' ,
'Style2Z' ,
description = "Style, suitable to produce downscaled (2-in-row) plot with large right margin (COLZ)",
scale = 1.41 , makeNew = True , colz = True )
scale = 1.41 , colz = True )

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

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

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

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

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

# ============================================================================
## default style
Expand Down Expand Up @@ -170,7 +156,7 @@ def __init__ ( self, style = None , **config ) :
if isinstance ( style , str ):

import ostap.plotting.makestyles as MS
if style in MS.StyleStore.styles : style = MC.StyleStore.styles [ style ]
if style in MS.StyleStore.styles : style = MS.StyleStore.styles [ style ]
elif style.upper() in ( '' , '0' , '1' ) : style = Style
elif '2' == style : style = Style2
elif '3' == style : style = Style3
Expand Down

0 comments on commit a78e9fb

Please sign in to comment.