Skip to content

Commit

Permalink
1. add full_path mehtods for ROOT.TDirectory and ROOT.TTree
Browse files Browse the repository at this point in the history
 1. improve `addTMVAresponce` functons
 1. rmeove comma separator sfrom `ds_project`
 1. slightly improve the prints from `tree_reduce`
  • Loading branch information
VanyaBelyaev committed Nov 13, 2022
1 parent 012c860 commit b50eea4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 24 deletions.
4 changes: 2 additions & 2 deletions ostap/fitting/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def ds_project ( dataset , histo , what , cuts = '' , first = 0 , last = -1 , p
if isinstance ( what , string_types ) :

## ATTENTION reverse here!
what = tuple ( reversed ( [ v.strip() for v in split_string ( what , ':;,' ) ] ) )
what = tuple ( reversed ( [ v.strip() for v in split_string ( what , ':;' ) ] ) )
return ds_project ( dataset , histo , what , cuts , *events , progress = progress )

elif isinstance ( what , ROOT.RooArgList ) and isinstance ( dataset , ROOT.RooAbsData ) :
Expand Down Expand Up @@ -729,7 +729,7 @@ def ds_project ( dataset , histo , what , cuts = '' , first = 0 , last = -1 , p
what = w ,
cuts = cuts ,
first = first ,
lasrt = last ,
last = last ,
progress = progress )
total += n
histo += htmp
Expand Down
6 changes: 4 additions & 2 deletions ostap/frames/tree_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ostap.core.meta_info import root_info
from ostap.core.core import cpp, Ostap
from ostap.utils.cleanup import CleanUp
import ROOT, os
import ROOT, os, sys
# =============================================================================
# logging
# =============================================================================
Expand Down Expand Up @@ -155,14 +155,16 @@ def __init__ ( self ,
self.__name = name

if not save_vars :
snapshot = frame.Snapshot ( name , output )
snapshot = frame.Snapshot ( name , output )
else :
bvars = chain.the_variables ( *save_vars )
all_vars = list ( bvars ) + [ v for v in nvars if not v in bvars ]
from ostap.core.core import strings as _strings
all_vars = _strings ( all_vars )
snapshot = frame.Snapshot ( name , output , all_vars )

sys.stdout.flush()

## if selections or 1 != prescale :
## report = snapshot.Report()

Expand Down
25 changes: 23 additions & 2 deletions ostap/io/root_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,13 +993,34 @@ def top_dir ( rdir ) :
while top :
moth = top.GetMotherDir()
if not moth : return top
top = moth
top = moth
else :
return None

ROOT.TDirectory.topdir = property ( top_dir , None , None )


# ==============================================================================
## get a full path of the directory
# @code
# rdir = ...
# path = rdirt.full_path
# @endcode
def full_path ( rdir ) :
"""Get a full path of the directory
>>> rdir = ...
>>> path = rdir.full_path
"""
assert rdir , 'Invalid ROOT TDirectory'

path = rdir.GetPath()
n = path.find (':/')
if n <= 0 :
logger.error('Invalid directory path')
return ''
return path[n+2:]

ROOT.TDirectory.full_path = property ( full_path , None , None )

# ==============================================================================
## Trivial context manager to treat TFile.ReOpen for 'UPDATE' mode
# @code
Expand Down
65 changes: 48 additions & 17 deletions ostap/tools/tmva.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from ostap.core.core import items_loop, WSE, Ostap, rootWarning
from ostap.core.ostap_types import num_types, string_types, integer_types
from ostap.core.meta_info import root_version_int, root_info
import ostap.io.root_file
import ostap.io.root_file
import ROOT, os, glob, math, tarfile, shutil, itertools
# =============================================================================
# logging
Expand Down Expand Up @@ -1400,7 +1400,8 @@ def make_Plots ( name , output , show_plots = True ) :
##
( ROOT.TMVA.likelihoodrefs , ( name , output ) ) ,
]


logger.warning ( 'make_Plots: Skip macro ROOT.TMVA.%s%s' % ( 'mvaeffs' , str ( ( name , output ) ) ) )
## if (6,24) <= root_info :
## plots.append ( ( ROOT.TMVA.mvaeffs , ( name , output ) ) )

Expand Down Expand Up @@ -1995,24 +1996,42 @@ def _add_response_tree ( tree , verbose , *args ) :
from ostap.io.root_file import REOPEN
from ostap.utils.progress_conf import progress_conf

tdir = tree.GetDirectory()
vars = set()
if verbose :
vars = set ( tree.branches() ) | set ( tree.leaves () )

tdir = tree.GetDirectory ()
tname = tree.full_path
with ROOTCWD () , REOPEN ( tdir ) as tfile :

fname = tfile.GetName()
logger.debug ( "Procesing %s file" % fname )

if not tfile.IsWritable() :
logger.error ( "Can't write TTree back to the file %s" % fname )
return Ostap.StatusCode ( 1000 ), tree

tdir.cd()

## add the progress bar
if verbose : sc = Ostap.TMVA.addResponse ( tree , progress_conf () , *args )
else : sc = Ostap.TMVA.addResponse ( tree , *args )

if sc.isFailure() : logger.error ( 'Error from Ostap::TMVA::addResponse %s' % sc )

if tfile.IsWritable() :
tfile.Write( "" , ROOT.TFile.kOverwrite )
return sc , tdir.Get ( tree.GetName() )
## tfile.Write() ## "" ) ## , ROOT.TFile.kOverwrite )
tfile.Write( "" , ROOT.TFile.kOverwrite )

else : logger.warning ( "Can't write TTree back to the file" )

return sc , tree
tree = ROOT.TChain ( tname )
tree.Add ( fname )

if verbose :
vars = sorted ( ( set ( tree.branches() ) | set ( tree.leaves () ) ) - vars )
title = "Added TMVA responses [%d entries in '%s'" % ( len ( tree ) , fname )
table = tree.table ( vars , prefix = '# ' , title = title )
logger.info ( '%s\n%s' % ( title , table ) )

return sc , tree

# =============================================================================
def _add_response_chain ( chain , verbose , *args ) :
Expand All @@ -2032,8 +2051,13 @@ def _add_response_chain ( chain , verbose , *args ) :

tree_verbose = verbose and len ( files ) < 5
chain_verbose = verbose and 5 <= len ( files )


vars = set()
if verbose :
vars = set ( chain.branches() ) | set ( chain.leaves () )

from ostap.utils.progress_bar import progress_bar
nfiles = 0
for f in progress_bar ( files , len ( files ) , silent = not chain_verbose ) :

with ROOT.TFile.Open ( f , 'UPDATE' , exception = True ) as rfile :
Expand All @@ -2042,10 +2066,17 @@ def _add_response_chain ( chain , verbose , *args ) :
## treat the tree
sc , nt = _add_response_tree ( tt , tree_verbose , *args )
if status is None or sc.isFailure() : status = sc
nfiles += 1

newc = ROOT.TChain ( cname )
for f in files : newc.Add ( f )


if verbose :
vars = sorted ( ( set ( newc.branches() ) | set ( newc.leaves () ) ) - vars )
title = 'Added TMVA responses [%d entries in %d files]' % ( len ( newc ) , nfiles )
table = newc.table ( vars , prefix = '# ' , title = title )
logger.info ( '%s\n%s' % ( title , table ) )

return status, newc

# =============================================================================
Expand Down Expand Up @@ -2084,21 +2115,21 @@ def addTMVAResponse ( dataset , ## input dataset to be updated

from ostap.core.core import cpp, std, Ostap
from ostap.utils.progress_conf import progress_conf
from ostap.utils.basic import isatty

_inputs = _inputs2map_ ( inputs )

weights_files = WeightsFiles ( weights_files )
_map , _w = _weights2map_ ( weights_files )

options = opts_replace ( options , 'V:' , verbose )
options = opts_replace ( options , 'Silent:' , not verbose )

from ostap.utils.basic import isatty
options = opts_replace ( options , 'Color:' , verbose and isatty() )
options = opts_replace ( options , 'V:' , verbose )
options = opts_replace ( options , 'Silent:' , not verbose )
options = opts_replace ( options , 'Color:' , isatty () )

args = _inputs, _map, options, prefix , suffix , aux

if isinstance ( dataset , ROOT.TChain ) :

sc , newdata = _add_response_chain ( dataset , verbose , *args )
if sc.isFailure() : logger.error ( 'Error from Ostap::TMVA::addResponse %s' % sc )
return newdata
Expand Down
22 changes: 21 additions & 1 deletion ostap/trees/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import ostap.trees.treereduce
import ostap.histos.histos
import ostap.trees.param
import ostap.io.root_file
import ROOT, os, math, array
# =============================================================================
# logging
Expand Down Expand Up @@ -1194,7 +1195,7 @@ def _in_types ( t ) :
def _rt_table_0_ ( tree , pattern = None , cuts = '' , prefix = '' , title = '' , *args ) :
"""
"""
## get list of branches
## get list of branches/leaves
brs = tree.leaves ( pattern )
if 'TObject' in brs :
brs = list ( brs )
Expand Down Expand Up @@ -3054,6 +3055,25 @@ def tree ( self ) :
"""``tree'' : get the underlying tree/chain"""
return self.chain



# ==============================================================================
## get a full path of the TTree obnject
# @code
# rdir = ...
# path = rdirt.full_path
# @endcode
def tree_path ( tree ) :
"""Get a full path of the directory
>>> tree = ...
>>> path = tree.full_path
"""
rdir = tree.GetDirectory()
if not rdir : return tree.GetName()
return os.path.join ( rdir.full_path , tree.GetName() )

ROOT.TTree.full_path = property ( tree_path , None , None )

# =============================================================================
_decorated_classes_ = (
ROOT.TTree ,
Expand Down

0 comments on commit b50eea4

Please sign in to comment.