26
26
'setWebDisplay' , ## set WebDisplay, see ROOT.TROOT.SetWebDisplay
27
27
'use_pad' , ## context manager to modifty TPad
28
28
'KeepCanvas' , ## context manager to keep/preserve currect canvas
29
- 'keepCanvas' , ## context manager to keep/preserve currect canvas
29
+ 'InvisibleCanvas' , ## context manager to keep/preserve currect canvas
30
30
'Canvas' , ## context manager to create currect canvas
31
31
'use_canvas' , ## context manager to create currect canvas
32
- 'KeepCanvas' , ## context manager to keep the current ROOT canvas
33
32
'keepCanvas' , ## context manager to keep the current ROOT canvas
34
- 'InvisibleCanvas' , ## context manager to use the invisible current ROOT canvas
35
33
'invisibleCanvas' , ## context manager to use the invisible current ROOT canvas
36
34
##
37
35
)
40
38
from ostap .utils .cidict import cidict , cidict_fun
41
39
from ostap .core .core import rootWarning , Ostap
42
40
from ostap .utils .utils import which
41
+ from ostap .utils .root_utils import Batch
43
42
from ostap .utils .timing import Wait
44
43
from ostap .plotting .makestyles import ( canvas_width , canvas_height ,
45
44
margin_left , margin_right ,
46
45
margin_top , margin_bottom )
46
+ from ostap .plotting .style import UseStyle
47
47
import ostap .core .core
48
- import ostap .plotting .style
49
48
import ROOT , os , tempfile , math
50
49
# =============================================================================
51
50
# logging
@@ -190,17 +189,17 @@ def old_canvas ( self ) :
190
189
def current ( self ) :
191
190
"""`current` : get the pointer to the current TCanvas
192
191
"""
193
- return Ostap . Utils . get_canvas ()
192
+ return self . current_canvas
194
193
195
194
@property
196
195
def current_canvas ( self ) :
197
- """`current_canvas` : get the pointer to the current TCanvas
196
+ """`current_canvas` : get the current TCanvas
198
197
"""
199
198
return Ostap .Utils .get_canvas ()
200
199
201
200
@property
202
201
def current_pad ( self ) :
203
- """`current_pad` : get the pointer to the current TPad
202
+ """`current_pad` : get the current TPad
204
203
"""
205
204
return Ostap .Utils .get_pad ()
206
205
@@ -210,12 +209,12 @@ def current_pad ( self ) :
210
209
# with keepCanvas() :
211
210
# ... do something here
212
211
# @endcode
213
- def keepCanvas ( ) :
212
+ def keepCanvas ( * args , ** kwargs ) :
214
213
""" Keep the current canvas
215
214
>>> with keepCanvas() :
216
215
... do something here
217
216
"""
218
- return KeepCanvas ()
217
+ return KeepCanvas ( * args , ** kwargs )
219
218
220
219
# =============================================================================
221
220
## @class InvisibleCanvas
@@ -1166,27 +1165,36 @@ class UsePad(object) :
1166
1165
""" Helper context manager for `TAttPad` objects
1167
1166
- see `TAttPad`
1168
1167
"""
1169
-
1170
1168
def __init__ ( self , pad = None , ** config ) :
1171
1169
1172
1170
self .__pad = pad if ( pad and isinstance ( pad , ROOT .TAttPad ) ) else None
1173
1171
self .__config = config
1174
1172
self .__changed = {}
1175
-
1173
+
1174
+ ## Context manager: ENTER
1176
1175
def __enter__ ( self ) :
1177
-
1176
+ """ Context manager: ENTER
1177
+ Apply modification to the TPad
1178
+ """
1179
+
1178
1180
if not self .__pad : self .__pad = Ostap .Utils .get_pad ()
1179
1181
1180
1182
if self .pad :
1181
1183
self .__changed = set_pad ( self .pad , ** self .config )
1182
1184
1183
1185
return self
1184
1186
1187
+ ## Context manager: EXIT
1185
1188
def __exit__ ( self , * _ ) :
1186
-
1189
+ """ Context manager: EXIT
1190
+ Restore configuration of TPad
1191
+ """
1192
+
1187
1193
if self .pad and self .changed :
1188
1194
set_pad ( self .pad , ** self .changed )
1189
-
1195
+
1196
+ self .__pad = None
1197
+
1190
1198
@property
1191
1199
def pad ( self ) :
1192
1200
"""`pad' : pad to be configured"""
@@ -1220,43 +1228,57 @@ def use_pad ( pad , **config ) :
1220
1228
# with Canvas ( title = 'Canvas #2' , width = 1000 ) :
1221
1229
# ...
1222
1230
# @endcode
1223
- class Canvas (KeepCanvas ) :
1231
+ class Canvas (KeepCanvas , UseStyle , UsePad , Batch ) :
1224
1232
""" Helper context manager to create and configure a canvas (and pad)
1225
1233
>>> with Canvas ( title = 'Canvas #2' , width = 1000 ) :
1226
1234
>>> ...
1227
1235
"""
1228
- def __init__ ( self ,
1229
- name = '' ,
1230
- title = '' ,
1231
- width = canvas_width , ## canvas width
1232
- height = canvas_height , ## canvas height
1233
- wait = 0 , ## pause before exit
1234
- keep = True , ## keep canvas open ?
1235
- plot = '' , ## produce the plot at __exit__
1236
- ** kwargs ) : ## Pad configuration
1236
+ def __init__ ( self ,
1237
+ name = '' ,
1238
+ title = '' ,
1239
+ width = canvas_width , ## canvas width
1240
+ height = canvas_height , ## canvas height
1241
+ wait = 0 , ## pause before exit
1242
+ keep = True , ## keep canvas open ?
1243
+ plot = '' , ## produce the plot at __exit__
1244
+ invisible = False , ## invisible (==batch)?
1245
+ style = None , ## use this style
1246
+ ** kwargs ) : ## Pad configuration
1237
1247
1238
1248
self .__name = name
1239
1249
self .__title = title
1240
1250
self .__width = width
1241
1251
self .__height = height
1242
- self .__kwargs = kwargs
1243
1252
self .__keep = True if keep else False
1244
1253
self .__cnv = None
1245
1254
1246
1255
if plot is True : plot = name if name else 'plot'
1247
1256
1248
1257
if plot : plot = plot .strip ().replace ( ' ' , '_' )
1249
1258
1250
- self .__plot = plot
1251
- ##
1252
- KeepCanvas .__init__ ( self , wait )
1259
+ self .__plot = plot
1260
+ ##
1261
+ self .__invisible = True if invisible else False
1262
+ if not self .__invisible :
1263
+ groot = ROOT .ROOT .GetROOT ()
1264
+ if groot : self .__invisible = groot .IsBatch ()
1265
+
1266
+ self .__style = style
1253
1267
1268
+ ##
1269
+ KeepCanvas .__init__ ( self , wait )
1270
+ UseStyle .__init__ ( self , self .__style )
1271
+ UsePad .__init__ ( self , ** kwargs )
1272
+ Batch .__init__ ( self , self .__invisible )
1273
+
1254
1274
## context manager: exit
1255
1275
def __enter__ ( self ) :
1256
1276
1257
1277
## (1) use context manager
1258
1278
KeepCanvas .__enter__ ( self )
1259
-
1279
+ UseStyle .__enter__ ( self )
1280
+ Batch .__enter__ ( self )
1281
+
1260
1282
if not self .__name :
1261
1283
groot = ROOT .ROOT .GetROOT ()
1262
1284
cnvlst = groot .GetListOfCanvases ()
@@ -1266,7 +1288,7 @@ def __enter__ ( self ) :
1266
1288
self .__name = 'gl_canvas#%d' % hash ( h )
1267
1289
1268
1290
if not self .__title :
1269
- self .__title = self .__name
1291
+ self .__title = 'Ostap Canvas %s' % self .__name
1270
1292
1271
1293
## (2) create/use new canvas
1272
1294
self .__cnv = getCanvas ( name = self .__name ,
@@ -1280,13 +1302,12 @@ def __enter__ ( self ) :
1280
1302
## (3) make it active
1281
1303
self .__cnv .cd ()
1282
1304
1283
- ## (4) apply pad settings
1284
- if self .__kwargs :
1285
- pad = Ostap .Utils .get_pad ()
1286
- if pad : set_pad ( pad , ** self .__kwargs )
1287
-
1305
+ ## (4) apply the pad modifications : it must be after (3)
1306
+ UsePad .__enter__ ( self )
1307
+
1288
1308
## (5) keep it open ?
1289
- if self .__keep : _keep .append ( self .__cnv )
1309
+ if self .__keep :
1310
+ _keep .append ( self .__cnv )
1290
1311
1291
1312
return self .__cnv ## return current canvas
1292
1313
@@ -1302,8 +1323,15 @@ def __exit__ ( self , *_ ) :
1302
1323
if self .__plot :
1303
1324
self .__cnv >> self .__plot
1304
1325
1305
- ## switch to the previous canvas
1306
- KeepCanvas .__exit__ ( self , * _ )
1326
+ ## swith back to pad configuration
1327
+ UsePad .__exit__ ( self , * _ )
1328
+ ## switch back to batch regine
1329
+ Batch .__exit__ ( self , * _ )
1330
+ ## swith back to old style
1331
+ UseStyle .__exit__ ( self , * _ )
1332
+ ## switch to the previous canvas
1333
+ KeepCanvas .__exit__ ( self , * _ )
1334
+ ##
1307
1335
self .__cnv = None
1308
1336
1309
1337
# =============================================================================
@@ -1312,27 +1340,30 @@ def __exit__ ( self , *_ ) :
1312
1340
# with use_canvas ( title = 'Canvas #2' , width = 1000 ) :
1313
1341
# ...
1314
1342
# @endcode
1315
- def use_canvas ( name = '' ,
1316
- title = '' ,
1317
- width = canvas_width , ## canvas width
1318
- height = canvas_height , ## canvas height
1319
- wait = 0 , ## pause before exit
1320
- keep = True , ## keep canvas open ?
1321
- ** kwargs ) : ## Pad configuration
1343
+ def use_canvas ( name = '' ,
1344
+ title = '' ,
1345
+ width = canvas_width , ## canvas width
1346
+ height = canvas_height , ## canvas height
1347
+ wait = 0 , ## pause before exit
1348
+ keep = True , ## keep canvas open ?
1349
+ plot = '' , ## print canvas as the exit
1350
+ invisible = False , ## invisible (==batch)?
1351
+ style = None , ## use this style
1352
+ ** kwargs ) : ## Pad configuration
1322
1353
""" Helper context manager to create and configure a canvas (and pad)
1323
1354
>>> with use_canvas ( title = 'Canvas #2' , width = 1000 ) :
1324
1355
>>> ...
1325
1356
"""
1326
- return Canvas ( name = name ,
1327
- title = title ,
1328
- width = width , ## canvas width
1329
- height = height , ## canvas height
1330
- wait = wait , ## pause before exit
1331
- keep = keep , ## keep canvas open?
1332
- ** kwargs ) ## Pad configuration
1333
-
1357
+ return Canvas ( name = name , ## canvas name
1358
+ title = title , ## canvas title
1359
+ width = width , ## canvas width
1360
+ height = height , ## canvas height
1361
+ wait = wait , ## pause before exit
1362
+ keep = keep , ## keep canvas open?
1363
+ invisible = invisible , ## invisble/batch
1364
+ style = style , ## use this style
1365
+ ** kwargs ) ## Pad configuration
1334
1366
1335
-
1336
1367
# =============================================================================
1337
1368
_decorated_classes_ = (
1338
1369
ROOT .TVirtualPad ,
0 commit comments