Skip to content

Commit

Permalink
resguffle code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Apr 22, 2024
1 parent b471396 commit 2c57d40
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 42 deletions.
8 changes: 5 additions & 3 deletions ostap/fitting/fithelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
try : from string import ascii_letters
except ImportError : from string import letters as ascii_letters
# =============================================================================
_py2 = sys.version_info.major < 3
_py2 = sys.version_info.major < 3
## possible numericla types for variables
numvar_types = num_types + ( VE , )
# =============================================================================
## @class NameDuplicates
# Are name duplicates allowed?
Expand Down Expand Up @@ -357,12 +359,12 @@ def make_var ( self ,
# var = ( value )
# var = ( min , max )
# var = ( value , min , max )

if isinstance ( var , ROOT.RooAbsReal ) : pass
elif var is None :
assert name and args , "make_var: 'name' and 'args' must be specified when 'var' is None!"
var = name , title if title else name
elif isinstance ( var , num_types ) :
elif isinstance ( var , numvar_types ) :
assert name , "make_var: 'name' must be specified when 'var' is of numerical type!"
var = name , title if title else name , float ( var )

Expand Down
8 changes: 4 additions & 4 deletions ostap/fitting/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,21 +1183,21 @@ def __init__ ( self , vars , value = None ) :
for v in vars :
assert isinstance ( v , ROOT.RooAbsReal ),\
'FIXVAR: Invalid variable type %s/%s' % ( v , type ( v ) )
variables.append ( v )
if hasattr ( v , 'fix' ) : variables.append ( v )

self.__variables = tuple ( variables )
self.__fixed = ()

def __enter__ ( self ) :

self.__fixed = tuple ( [ c.isConstant() for c in self.variables ] )
for v in self.variables : v.fix ()
self.__fixed = [ c.isConstant() for c in self.variables ]
for i , v in enumerate ( self.variables ) : v.fix ()

return self

def __exit__ ( self , *_ ) :
for v , c in zip ( self.variables , self.fixed ) :
if not c : v.release()
if ( not c ) and hasattr ( v , 'release' ) : v.release()

@property
def variables ( self ) :
Expand Down
6 changes: 3 additions & 3 deletions ostap/utils/files.py → ostap/io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import os, glob, random, math
# =============================================================================
from ostap.logger.logger import getLogger
if '__main__' == __name__ : logger = getLogger( 'ostap.utils.files' )
else : logger = getLogger( __name__ )
if '__main__' == __name__ : logger = getLogger( 'ostap.io.files' )
else : logger = getLogger( __name__ )
del getLogger
# =============================================================================
## @class TreatFileTask
Expand Down Expand Up @@ -462,7 +462,7 @@ def sample_files ( self , n , sort ) :
if sort : files.sort()
return tuple ( files )
elif isinstance ( n , float ) and 0 < n < 1 :
n = int ( math.floor ( len ( self.files ) * n ) )
n = int ( math.ceil ( len ( self.files ) * n ) )
return self.sample_files ( n , sort )
else :
raise IndexError ( "Invalid fraction: %s/%s" % ( n , len( self.files ) ) )
Expand Down
2 changes: 1 addition & 1 deletion ostap/io/root_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
)
# =============================================================================
from ostap.core.core import ROOTCWD, valid_pointer
from ostap.utils.files import Files
from ostap.io.files import Files
import ROOT, sys, os
# =============================================================================
# logging
Expand Down
10 changes: 7 additions & 3 deletions ostap/parallel/parallel_add_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,21 @@ def add_new_branch ( chain ,
if isinstance ( chain , ROOT.TChain ) and 1 < len ( chain.files () ) : pass
elif isinstance ( chain , ROOT.TTree ) :
return _add_branch_ ( chain , branch_name , function , verbose = verbose , report = report )


cname = chain.name
ch = Chain ( chain )
branches = set ( chain.branches() ) | set ( chain.leaves() )

keep_it = branch_name , function

task = AddBranch ( branch_name , function )
wmgr = WorkManager ( silent = not verbose , **kwargs )
trees = ch.split ( max_files = 1 )


wmgr.process ( task , trees )
nc = ROOT.TChain ( chain.name )

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

if report :
Expand Down
16 changes: 9 additions & 7 deletions ostap/tools/splot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__ ( self ,
len ( pdf.alist1 ) == len ( pdf.alist2 ) , 'Invalid type of PDF!'

cmps = pdf.alist2
names = [ c.name for c in cmps ]
names = sorted ( set ( c.name for c in cmps ) )

## if datset is specified - perform the fit
if dataset :
Expand All @@ -109,18 +109,20 @@ def __init__ ( self ,
## make a proper (re)fit fixing everything but yields
to_fix = [ v for v in vars if not v in cmps ]
with FIXVAR ( to_fix ) :
logger.info ('Refit with the fixed parameters %s' % [ v.name for v in to_fix ] )
ns = ','.join ( sorted ( v.name for v in to_fix ) )
logger.info ( 'Refit with the fixed parameters: %s' % ns )

fitresult , f = pdf.fitTo ( dataset , silent = True , draw = False , **fitopts )
## fitresult , f = pdf.fitTo ( dataset , silent = True , draw = True , **fitopts )

elif fitresult :

pars = fitresult.floatParsFinal()
pnames = set( [ p.name for p in pars ] )
pnames = sorted ( set ( p.name for p in pars ) )

if set ( names ) != pnames :
raise RuntimeError("Rerun fit with with only %s floating " % names )
if names != pnames :
ns = ','.join ( n for n in names )
raise RuntimeError ( "Rerun fit with with only floating %s" % ns )

## template histogram
template = pdf.make_histo ( nbins )
Expand All @@ -131,12 +133,12 @@ def __init__ ( self ,
## the list of PDFs
cpdfs = [ Generic1D_pdf ( p , xvar = pdf.xvar ) for p in pdf.alist1 ]

for p , n in zip ( cpdfs , names ) :
for p , v in zip ( cpdfs , pdf.alist2 ) :

if fast : hc = p.roo_histo ( histo = template , events = False )
else : hc = p. histo ( histo = template , events = False , errors = False )

hcomponents [ n ] = hc
hcomponents [ v.name ] = hc

## sum of all histograms
hsum = template.clone()
Expand Down
2 changes: 1 addition & 1 deletion ostap/trees/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
)
# =============================================================================
from ostap.core.core import rootError, rootWarning
from ostap.utils.files import Files, fsize_unit
from ostap.io.files import Files, fsize_unit
from ostap.io.root_file import RootFiles
import ROOT, os, math
# =============================================================================
Expand Down
21 changes: 13 additions & 8 deletions ostap/trees/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -2373,12 +2373,14 @@ def add_new_branch ( tree ,
assert ( isinstance ( name , dictlike_types ) and function is None ) or btypes ( function ) ,\
"add_branch: invalid type of ``function'': %s/%s" % ( function , type ( function ) )

treepath = tree.path
treepath = tree.path
the_file = tree.topdir
assert treepath and the_file and ( not the_file is ROOT.gROOT ) and isinstance ( the_file , ROOT.TFile ) , \
'This is not file-resident TTree object, addition of new branch is not posisble!'
the_file = the_file.GetName()

funcs = []

if isinstance ( name , dictlike_types ) and function is None :

typeformula = False
Expand All @@ -2390,17 +2392,21 @@ def add_new_branch ( tree ,
elif isinstance ( v , Ostap.IFuncTree ) : typeformula = True
else : raise TypeError ('add_branch: Unknown branch %s/%s for %s' % ( v , type( v ) , k ) )

if typeformula : MMAP = Ostap.Trees.FUNCTREEMAP
else : MMAP = std.map ( 'std::string' , 'std::string' )

mmap = MMAP ()
if typeformula :
MMAP = Ostap.Trees.FUNCTREEMAP
PAIR = Ostap.Trees.FUNCTREEPAIR
else :
MMAP = std.map ( 'std::string' , 'std::string' )
PAIR = MMAP.value_type

mmap = MMAP ()
for k in name.keys() :
v = name [ k ]
if typeformula and isinstance ( v , string_types ) :
v = Ostap.Functions.FuncFormula ( v , tree )
funcs.append ( v )
## mmap.insert ( PAIR ( k , v ) )
mmap [ k ] = v
mmap.insert ( PAIR ( k , v ) )
## mmap [ k ] = v

names = list ( name.keys() )
args = mmap ,
Expand Down Expand Up @@ -2533,7 +2539,6 @@ def add_new_branch ( tree ,

branches = set ( tree.branches () ) | set ( tree.leaves() )
exists = set ( names ) & branches


## if exists : logger.warning ("Branches '%s' already exist(s)!" % exists )
assert not exists , "Branches '%s' already exist(s)!" % list ( exists )
Expand Down
5 changes: 5 additions & 0 deletions source/include/Ostap/AddBranch.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ namespace Ostap
* helper type to deal with map of functions
*/
typedef std::map<std::string,const Ostap::IFuncTree*> FUNCTREEMAP ;
// =======================================================================
/** @typedef FUNCTREEEMAP
* helper type to deal with map of functions
*/
typedef FUNCTREEMAP::value_type FUNCTREEPAIR ;
// ========================================================================
/** add new branch with name <code>name</code> to the tree
* the value of the branch is taken from function <code>func</code>
Expand Down
1 change: 1 addition & 0 deletions source/src/AddBranch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Ostap::Trees::add_branch
const std::string& name ,
const Ostap::IFuncTree& func )
{

if ( !tree ) { return Ostap::StatusCode ( INVALID_TREE ) ; }
//
Double_t value = 0 ;
Expand Down
24 changes: 12 additions & 12 deletions source/src/Funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,9 @@ Ostap::Functions::FuncTH1::FuncTH1
const bool edges ,
const bool extrapolate ,
const bool density )
: Func1D ( Ostap::Math::Histo1D ( histo , tx , edges , extrapolate , density ) ,
xvar ,
tree )
: FuncTH1 ( Ostap::Math::Histo1D ( histo , tx , edges , extrapolate , density ) ,
xvar ,
tree )
{}
// ============================================================================
/* constructor from the histogram
Expand Down Expand Up @@ -626,10 +626,10 @@ Ostap::Functions::FuncTH2::FuncTH2
const bool edges ,
const bool extrapolate ,
const bool density )
: Func2D ( Ostap::Math::Histo2D ( histo , tx , ty , edges , extrapolate , density ) ,
xvar ,
yvar ,
tree )
: FuncTH2 ( Ostap::Math::Histo2D ( histo , tx , ty , edges , extrapolate , density ) ,
xvar ,
yvar ,
tree )
{}
// ======================================================================
/* constructor from the histogram
Expand Down Expand Up @@ -685,11 +685,11 @@ Ostap::Functions::FuncTH3::FuncTH3
const bool edges ,
const bool extrapolate ,
const bool density )
: Func3D ( Ostap::Math::Histo3D ( histo , tx , ty , tz , edges , extrapolate , density ) ,
xvar ,
yvar ,
zvar ,
tree )
: FuncTH3 ( Ostap::Math::Histo3D ( histo , tx , ty , tz , edges , extrapolate , density ) ,
xvar ,
yvar ,
zvar ,
tree )
{}
// ======================================================================
/* constructor from the histogram
Expand Down

0 comments on commit 2c57d40

Please sign in to comment.