def test_kisa2 () : from ostap.fitting.selectors import SelectorWithVars, Variable variables = [ Variable ( 'mass' , 'mass(mu+mu-)' , 3.09 , 3.11 ) , Variable ( 'c2dtf' , 'chi2(dtf)' , -1 , 10 ) , Variable ( 'mass2' , 'mass(mu+mu-)' , 1 , 5 , MASS() ) , Variable ( 'mass3' , 'mass(mu+mu-)' , 1 , 5 , 'mass' ) ] from sys import version_info as python_version if python_version.major < 3 : variables.append ( Variable ( 'mass1' , 'mass(mu+mu-)' , 2 , 4 , lambda s : s.mass ) ) ppservers = () ## 'lxplus051' , ) ## ppservers = 'auto' nf = len ( data.files ) nf //= 40 nf += 1 nf = min ( nf , 25 ) with timing('%d files in sequence %s' % ( nf , len( data.chain ) ) ) : selector = SelectorWithVars ( variables = variables , selection = '2<=mass && mass<4 && 0<=c2dtf && c2dtf<5' , silence = False ) chain = data.chain[:nf] st = chain.process ( selector , silent = False , shortcut = True ) ds = selector.data del selector logger.info ( 'Dataset:\n%s' % ds ) with timing('%s files in parallel %s' % ( len ( data.files ) , len( data.chain ) ) ) : selector = SelectorWithVars ( variables = variables , selection = '2<=mass && mass<4 && 0<=c2dtf && c2dtf<5' , silence = True ) st = data.chain.pprocess ( selector , silent = False , chunk_size = -1 , max_files = 1 , ppservers = ppservers ) ds = selector.data del selector logger.info ( 'Dataset:\n%s' % ds )
def process(self, jobid, item): import ROOT from ostap.logger.logger import logWarning with logWarning(): import ostap.core.pyrouts import ostap.trees.trees ## reconstruct chain from the item chain = item.chain ll = len(chain) first = item.first nevents = item.nevents all = 0 == first and (nevents < 0 or ll <= nevents) if self.trivial and all: import ostap.fitting.selectors self.__output = chain.make_dataset(self.variables, self.selection, silent=True) return self.__output from ostap.fitting.selectors import SelectorWithVars ## use selector selector = SelectorWithVars(self.variables, self.selection, silence=True) args = () if not all: args = nevents, first num = chain.process(selector, *args, shortcut=all and self.trivial, use_frame=self.use_frame) self.__output = selector.data, selector.stat if num < 0: logger.warning("Processing status %s (jobid #%s)" % (num % jobid)) ##del selector.data ##del selector logger.debug('Processed %s and filled %d entries (jobid #%s)' % (item, len(self.__output), jobid)) return self.__output
## configuration of reweighting weightings = ( ## variable address in DB Weight.Var('x', 'x-reweight'), Weight.Var('y', 'y-reweight'), Weight.Var(('x', 'y'), '2D-reweight'), ) # ============================================================================= ## variables to be used in MC-dataset variables = [ Variable('x', 'x-var', 0, 20), Variable('y', 'y-var', 0, 15), ] selector = SelectorWithVars(variables, '0<x && x<20 && 0<y && y<20', silence=True) mctree.process(selector, silent=True) mcds_ = selector.data ## dataset # ============================================================================= ## start reweighting iterations: for iter in range(1, maxIter + 1): logger.info(allright('Reweighting iteration %d ' % iter)) with timing('Prepare MC-dataset:', logger=logger): # ========================================================================= ## 0) The weighter object weighter = Weight(dbname, weightings) # =========================================================================
Weight.Var ( lambda s : (s.x,s.y) , '2D-reweight' ) , ) weighter = Weight( dbname , weightings ) ## variables to be used in MC-dataset variables = [ Variable( 'x' , 'x-var' , 0 , 20 , lambda s : s.x ) , Variable( 'y' , 'y-var' , 0 , 15 , lambda s : s.y ) , Variable( 'weight' , 'weight' , accessor = weighter ) ] # ## create new "weighted" mcdataset # selector = SelectorWithVars ( variables , '0<x && x<20 && 0<y && y<20' ) mctree.process ( selector ) mcds = selector.data ## new reweighted dataset logger.info ('MCDATA: %s' % mcds ) # ## update weights # plots = [ WeightingPlot ( 'y:x' , 'weight' , '2D-reweight' , hdata , hmc ) ] if 5 <= iter: plots = [ WeightingPlot ( 'x' , 'weight' , 'x-reweight' , hxdata , hmcx ) ,
## variable address in DB Weight.Var( accessor = lambda s : s.x , address = 'x-reweight' ) , ) weighter = Weight( dbname , weighting ) ## variables to be used in MC-dataset variables = [ Variable ( 'pt_x' , 'pt_x' , 0 , 100 , lambda s : s.x ) , Variable ( 'weight' , 'weight' , accessor = weighter ) ] # ## create new "weighted" mcdataset # selector = SelectorWithVars ( variables , '0<x && x<100 ' ) mctree.process ( selector ) mcds = selector.data ## new reweighted dataset # ## update weights # plots = [ WeightingPlot( 'pt_x' , 'weight' , 'x-reweight' , hdata , hmc ) ] more = makeWeights ( mcds , plots , dbname , delta = 0.001 ) ## make MC-histogram
## method = reader[m] ## logger.info ( 'Method %12s , response %s' % ( m , method ( 1.1 , 0.8 , 0.3 ) ) ) ## del method ## # ============================================================================= ## ## 2.2) declare/add TMVA variables with TMVAreader ## for m in methods : ## variables += [ Variable( 'tmva_%s' % m , 'TMVA(%s)' % m , accessor = reader[m] ) ] ## # ============================================================================= ## ## The END of TMVA.Reader fragment ## # ============================================================================= ## 3) Run Ostap to fill RooDataSet dsS = SelectorWithVars ( variables = variables , selection = "var1 < 100" , ) dsB = SelectorWithVars ( variables = variables , selection = "var1 < 100" , ) ## read input data file with ROOT.TFile.Open( data_file ,'READ') as datafile : datafile.ls() tSignal = datafile['S'] tBkg = datafile['B'] from ostap.utils.timing import timing with timing("Process signal" ) : tSignal.process ( dsS )
#Variable( 'var1' , 'variable#1' , accessor = lambda s : s.var1 ) , #Variable( 'var2' , 'variable#2' , accessor = lambda s : s.var2 ) , #Variable( 'var3' , 'variable#3' , accessor = lambda s : s.var3 ) , Variable('var1', 'variable#1'), Variable('var2', 'variable#2'), Variable('var3', 'variable#3'), ] ## ## 3) declare/add TMVA variables ## for m in methods : ## variables += [ Variable( 'tmva_%s' % m , 'TMVA(%s)' % m , accessor = reader[m] ) ] ## 4) Run Ostap to fill RooDataSet dsS = SelectorWithVars( variables= variables, ## + [ Variable ( 'signal' , 'signal' , -1 , 3 , lambda s : 1 ) ] , selection="var1 < 100", ) dsB = SelectorWithVars( variables= variables, ## + [ Variable ( 'signal' , 'signal' , -1 , 3 , lambda s : 0 ) ] , selection="var1 < 100", ) ## read input data file with ROOT.TFile.Open(data_file, 'READ') as datafile: datafile.ls() tSignal = datafile['S'] tBkg = datafile['B']
# ============================================================================= ## start iterations: for iter in range(1, maxIter + 1): logger.info(allright('Reweighting iteration %d ' % iter)) with timing('Prepare MC-dataset:', logger=logger): # ============================================================================= ## 0) The weighter object weighter = Weight(dbname, weighting) # =============================================================================== ## 1a) create mcdataset selector = SelectorWithVars(variables, '0<x && x<100 ', silence=True) mctree.process(selector, silent=True) mcds = selector.data ## dataset with timing('Add weight to MC-dataset', logger=logger): ## 1b) add "weight" variable to the dataset mcds.add_reweighting(weighter, name='weight') logger.info('MCDATA:\n%s' % mcds) with timing('Make one reweighting iteration:', logger=logger): # ============================================================================== ## 2) update weights plots = [WeightingPlot('x', 'weight', 'x-reweight', hdata, hmc)] more = makeWeights(mcds,