removeVars = [ 'tagCatP2VV' ]
selection = 'hlt2_biased==1'
numDataSets = 40
weightName = ''

import P2VV.RooFitWrappers
from ROOT import TFile, RooArgSet
protoFile = TFile.Open(protoFilePathIn)
nTupleFileIn = TFile.Open(nTupleFilePathIn)
protoData = protoFile.Get('JpsiKK_sigSWeight')
nTupleIn = nTupleFileIn.Get('DecayTree')

from ROOT import RooRealVar, RooCategory
obsSet = RooArgSet( protoData.get() )
if runPeriods :
    rp = obsSet.find('runPeriod')
    if rp : obsSet.remove(rp)
    rp = RooCategory( 'runPeriod', 'runPeriod' )
    for per in runPeriods : rp.defineType( 'p%d' % per, per )
    obsSet.add(rp)

if KKMassBins :
    KKCat = obsSet.find('KKMassCat')
    if KKCat : obsSet.remove(KKCat)
    KKCat = RooCategory( 'KKMassCat', 'KKMassCat' )
    for ind in range( len(KKMassBins) - 1 ) : KKCat.defineType( 'bin%d' % ind, ind )
    obsSet.add(KKCat)

    from array import array
    KKBinsArray = array( 'd', KKMassBins )
    from ROOT import RooBinning
Beispiel #2
0
dataNames = [ 'JpsiKK', 'JpsiKK_sigSWeight', 'JpsiKK_cbkgSWeight' ]
cut = 'hlt2_biased==1'
removeObs = [ 'wMC', 'mdau1', 'tagCatP2VV' ] #, 'polarity', 'hlt2_prescale', 'nPVCat', 'BpTCat' ]
dataFilePathIn  = 'P2VVDataSets20112012Reco14_I2Mass_6KKMassBins_4TagCats.root'
dataFilePathOut = 'P2VVDataSets20112012Reco14_I2Mass_6KKMassBins_4TagCats_HLT2B.root'

import P2VV.RooFitWrappers
from ROOT import TObject, TFile, RooFit, RooDataSet, RooArgSet
dataFile = TFile.Open(dataFilePathIn)
newDataFile = TFile.Open( dataFilePathOut, 'RECREATE' )
newData = [ ]
print 'read datasets from file "%s"' % dataFile.GetName()
for dataName in dataNames :
    print 'reading dataset "%s"' % dataName
    data = dataFile.Get(dataName)
    data.Print()

    newArgSet = RooArgSet( data.get() )
    for name in removeObs : newArgSet.remove( newArgSet.find(name) )
    newData.append( RooDataSet( dataName, dataName, newArgSet, RooFit.Import(data), RooFit.Cut(cut) ) )
    newData[-1].Print()
    newDataFile.Add( newData[-1] )

print 'write dataset to file "%s"' % newDataFile.GetName()
newDataFile.Write( dataFilePathOut, TObject.kOverwrite )
# create data sets with final columns
obsSetMain = obsSetNTuple + [ var for var in weightVars ]
mainDataSet = dataTree.buildDataSet( Observables = obsSetMain, Name = 'JpsiKK', Title = 'JpsiKK', IndexName = 'index'
                                    , OrigDataSet = preSDataSet )
del preSDataSet
del preDataSet
dataTreeFile.Close()
from ROOT import gROOT
gROOT.cd('PyROOT:/')

from ROOT import RooArgSet, RooDataSet
dataSets = { }
for it, var in enumerate(weightVars) :
    argSet = RooArgSet( mainDataSet.get() )
    for remVar in weightVars :
        if remVar != var : argSet.remove( argSet.find( remVar.GetName() ) )
    dataName = dataSetNameOut if it == 0 else 'JpsiKK_%d' % it
    dataSets[var] = RooDataSet( dataName, dataName, argSet, Import = mainDataSet, WeightVar = ( var.GetName(), True ) )
    if not printYields : break

print 'P2VV - INFO: createBs2JpsiKKDataSet: produced data sets:'
mainDataSet.Print()
for var in weightVars :
    dataSets[var].Print()
    if not printYields : break
print

if printYields :
    # print event yields
    print 'P2VV - INFO: createBs2JpsiKKDataSet: event yields:'
    allCats = [  mainDataSet.get().find( obsDict['runPeriod'][0] )
dataSets.pop('preS')
dataTreeFile.Close()
from ROOT import gROOT
gROOT.cd('PyROOT:/')

print 'P2VV - INFO: createB2CCFitNTuple: produced data set:\n' + ' ' * 13,
dataSets['main'][0].Print()
print

# create weighted data sets
from ROOT import RooArgSet
obsSets = dict( [ ( var.GetName(), RooArgSet( dataSets['main'][0].get() ) ) for var in weightVars ] )
for varName, obsSet in obsSets.iteritems() :
    for var in weightVars :
        if var.GetName() == varName : continue
        obsSet.remove( obsSet.find( var.GetName() ) )

from ROOT import RooDataSet
dataSets['sigSWeight']  = ( RooDataSet( 'JpsiKK_sigSWeight',  'JpsiKK_sigSWeight', obsSets['N_sigMass_sw']
                                       , Import = dataSets['main'][0], WeightVar = ( weightVars[0].GetName(), True ) ), [ ] )
dataSets['cbkgSWeight'] = ( RooDataSet( 'JpsiKK_cbkgSWeight', 'JpsiKK_cbkgSWeight', obsSets['N_cbkgMass_sw']
                                       , Import = dataSets['main'][0], WeightVar = ( weightVars[1].GetName(), True ) ), [ ] )
for sample, data in zip( samples, dataSets['main'][1] ) :
    dataSets['sigSWeight'][1].append(  RooDataSet( 'JpsiKK_sigSWeight_'  + sample[0], 'JpsiKK_sigSWeight', obsSets['N_sigMass_sw']
                                                  , Import = data, WeightVar = ( weightVars[0].GetName(), True ) ) )
    dataSets['cbkgSWeight'][1].append( RooDataSet( 'JpsiKK_cbkgSWeight_' + sample[0], 'JpsiKK_cbkgSWeight', obsSets['N_cbkgMass_sw']
                                                  , Import = data, WeightVar = ( weightVars[1].GetName(), True ) ) )

print 'P2VV - INFO: createB2CCFitNTuple: signal data set:\n' + ' ' * 13,
dataSets['sigSWeight'][0].Print()
print
Beispiel #5
0
dataNames = [ 'JpsiKK_sigSWeight' ] #[ 'JpsiKK', 'JpsiKK_sigSWeight', 'JpsiKK_cbkgSWeight' ]
removeObs = [ 'wMC', 'mdau1', 'tagCatP2VV' ] #, 'polarity', 'hlt2_prescale', 'nPVCat', 'BpTCat' ]
dataFilePathIn  = 'P2VVDataSets20112012Reco14_I2Mass_6KKMassBins_2TagCats_20140309.root'
dataFilePathOut = 'P2VVDataSets2012Reco14_I2Mass_6KKMassBins_2TagCats_20140309.root'

import P2VV.RooFitWrappers
from ROOT import TObject, TFile, RooFit, RooDataSet, RooArgSet, RooCategory
dataFile = TFile.Open(dataFilePathIn)
newDataFile = TFile.Open( dataFilePathOut, 'RECREATE' )
newData = [ ]
print 'read datasets from file "%s"' % dataFile.GetName()
for dataName in dataNames :
    print 'reading dataset "%s"' % dataName
    data = dataFile.Get(dataName)
    data.Print()

    newArgSet = RooArgSet( data.get() )
    for name in removeObs : newArgSet.remove( newArgSet.find(name) )
    if runPeriod :
        newArgSet.remove( newArgSet.find('runPeriod') )
        rp = RooCategory( 'runPeriod', 'runPeriod' )
        rp.defineType( 'p%d' % runPeriod, runPeriod )
        newArgSet.add(rp)

    newData.append( RooDataSet( dataName, dataName, newArgSet, RooFit.Import(data), RooFit.Cut(cut) ) )
    newData[-1].Print()
    newDataFile.Add( newData[-1] )

print 'write dataset to file "%s"' % newDataFile.GetName()
newDataFile.Write( dataFilePathOut, TObject.kOverwrite )
Beispiel #6
0
# workspace
from P2VV.RooFitWrappers import RooObject
ws = RooObject( workspace = 'JpsiphiWorkspace' ).ws()

# read data set with events in two asymmetry categories
print 'pdfAsymmetry: reading dataset with events in two asymmetry categories'
from ROOT import TFile
dataFile = TFile.Open(dataSetFilePath)
dataSetAsym = dataFile.Get('asymData')
dataFile.Close()
dataSetAsym.Print()

# create weighted data set
from ROOT import RooProduct, RooArgSet, RooArgList
obsSet = RooArgSet( dataSetAsym.get() )
prodList = RooArgList( obsSet.find('sigWeight') )
if applyPlotWeights : prodList.add( obsSet.find('dilution') )
weightVar = RooProduct( 'weightVar', 'weightVar', prodList )
weightVar = dataSetAsym.addColumn(weightVar)
obsSet.add(weightVar)

from ROOT import RooDataSet
dataSetAsymW = RooDataSet( 'asymDataW', 'asymDataW', obsSet, Import = dataSetAsym, WeightVar = ( 'weightVar', True ) )
del dataSetAsym
ws.put(dataSetAsymW)
del dataSetAsymW
dataSetAsymW = ws['asymDataW']
obsSet = RooArgSet( dataSetAsymW.get() )
dataSetAsymW.Print()

# build PDF
Beispiel #7
0
    dataFile.Write( dataSetFileOut, TObject.kOverwrite )
    dataFile.Close()

else :
    # read data set with events in two asymmetry categories
    print 'plotAsymmetry: reading dataset with events in two asymmetry categories'
    dataFile = TFile.Open(dataSetFileOut)
    dataSetAsym = dataFile.Get('asymData')
    dataFile.Close()

dataSetAsym.Print()

# create weighted data set
from ROOT import RooProduct, RooArgSet, RooArgList
obsSet = RooArgSet( dataSetAsym.get() )
prodList = RooArgList( obsSet.find('sigWeight') )
if applyDilWeights : prodList.add( obsSet.find('dilution') )
if applyAngWeights : prodList.add( obsSet.find( 'angWeight_%s' % applyAngWeights ) )
weightVar = RooProduct( 'weightVar', 'weightVar', prodList )
weightVar = dataSetAsym.addColumn(weightVar)
obsSet.add(weightVar)

dataSetAsymW = RooDataSet( 'asymDataW', 'asymDataW', obsSet, Import = dataSetAsym, WeightVar = ( 'weightVar', True ) )
del dataSetAsym
ws.put(dataSetAsymW)
del dataSetAsymW
dataSetAsymW = ws['asymDataW']
obsSet = RooArgSet( dataSetAsymW.get() )
dataSetAsymW.Print()

# get sums of weights
        pdfs['cbkg'][stateName] = simMassPdf.getPdf(stateName).pdfList().at(1)
    else :
        pdfs['sig'][stateName]  = simMassPdf.getPdf(stateName).pdfList().at(0).createIntegral( intSet, normSet )
        pdfs['cbkg'][stateName] = simMassPdf.getPdf(stateName).pdfList().at(1).createIntegral( intSet, normSet )
for comp in comps :
    yields[comp]['total'] = sum( y for y in yields[comp].itervalues() )

print 'P2VV - INFO: signal and background yields:'
for comp in comps :
    print '  %4s: %9.2f' % ( comp, yields[comp]['total'] )

# create datasets
from ROOT import RooFit, RooDataSet, RooRealVar
dataObs = RooArgSet( dataSet.get() )
if genMass :
    var = dataObs.find('mass')
    if var : dataObs.remove(var)
    splitData = dict( [ ( comp, dict( [ ( state.GetName(), RooDataSet('%s_%s' % (dataSet.GetName(), comp), dataSet.GetTitle(), dataObs) )\
                                        for state in splitCat ] ) ) for comp in comps ] )
else :
    sigWeight = RooRealVar( 'sigWeight', 'sigWeight', 1. )
    dataObs.add(sigWeight)
    if weightData :
        splitData = dict( [ ( comp, dict( [ ( 'total', RooDataSet( '%s_%s' % ( dataSet.GetName(), comp ), dataSet.GetTitle()
                                                                  , dataObs, RooFit.WeightVar(sigWeight) ) ) ] ) ) for comp in comps ] )
    else :
        splitData = dict( [ ( comp, dict( [ ( 'total', RooDataSet( '%s_%s' % ( dataSet.GetName(), comp ), dataSet.GetTitle()
                                                                  , dataObs ) ) ] ) ) for comp in comps ] )

# function to get observable value from dataset
def getObsVal( names, obsSet ) :
Beispiel #9
0
def readDataSet(
    config,             # configuration dictionary
    ws,                 # workspace to which to add data set
    observables,        # observables
    rangeName = None    # name of range to clip dataset to
    ):
    """
    read data set from given Ntuple (or a RooDataSet inside a workspace) into
    a RooDataSet

    arguments:
    config      -- configuration dictionary (see below for relevant keys)
    ws          -- workspace into which to import data from tuple
    observables -- RooArgSet containing the observables to be read
    rangeName   -- optional, can be the name of a range of one observable, if
                   the data read from the tuple needs to be explicitly clipped
                   to that range for some reason

    the routine returns the data set that has been read in and stored inside
    ws.

    relevant configuration dictionary keys:
    'DataFileName' -- file name of data file from which to read ntuple or data
                      set
    'DataSetNames' -- name (TTree or RooDataSet) of the data set to be read;
                      more than one can be given in a dictionary, providing a
                      mapping between the sample name and the data set to be
                      read (see below for an explanation)
    'DataWorkSpaceName'
                   -- name of workspace to read data from (if any - leave None
                      for reading tuples)
    'DataSetCuts' --  cuts to apply to data sets on import - anything that
                      RooDataSet.reduce will understand is permissible here
                      (set to None to not apply any cuts on import)
    'DataSetVarNameMapping'
                   -- mapping from variable names in set of observables to
                      what these variable names are called in the
                      tuple/workspace to be imported

    "special" observable names:
    The routine treats some variable names special on import based on their
    likely meaning:
    'weight' -- this variable name must be used to read in (s)weighted events
    'qf'     -- final state charge (e.g. +1 for K+ vs -1 for K-); only the
                sign is important here, and the import code enforces that
    'qt'     -- tagging decision; this can be any integer number (positive or
                negative); if the tuple should contain a float/double with
                that information, it is rounded appropriately
    'mistag' -- predicted mistag; the import code makes sure that events with
                qt == 0 have mistag = 0.5
    'sample' -- if more than one final state is studied (e.g. Ds final states
                phipi, kstk, nonres, kpipi, pipipi), the events for these
                subsamples often reside in different samples; therefore the
                config dictionary entry 'DataSetNames' can contain a
                dictionary which maps the category labels (phipi etc) to the
                names of the data samples in the ROOT file/workspace

    The config dict key 'DataSetVarNameMapping' contains a useful feature:
    Instead of providing a one-to-one-mapping of observables to tuple/data set
    names, an observable can be calculated from more than one tuple column.
    This is useful e.g. to convert a tuple that's stored the tagging decision
    as untagged/mixed/unmixed, or to sum up sweights for the different
    samples. Most simple formulae should be supported, but constants in
    scientific notation (1.0E+00) are not for now (until someone write a
    better parser for this).

    Example:
    @code
    seed = 42 # it's easy to modify the filename depending on the seed number
    configdict = {
        # file to read from
        'DataFileName': '/some/path/to/file/with/toy_%04d.root' % seed,
        # data set is in a workspace already
        'DataWorkSpaceName':    'FitMeToolWS',
        # name of data set inside workspace
        'DataSetNames':         'combData',
        # mapping between observables and variable name in data set
        'DataSetVarNameMapping': {
            'sample':  'sample',
            'mass':    'lab0_MassFitConsD_M',
            'pidk':    'lab1_PIDK',
            'dsmass':  'lab2_MM',
            'time':    'lab0_LifetimeFit_ctau',
            'timeerr': 'lab0_LifetimeFit_ctauErr',
            'mistag':  'tagOmegaComb',
            'qf':      'lab1_ID',
            'qt': '    tagDecComb',
            # sweights need to be combined from different branches in this
            # case, only one of the branches is ever set to a non-zero value,
            # depending on which subsample the event is in
            'weight': ('nSig_both_nonres_Evts_sw+nSig_both_phipi_Evts_sw+'
                       'nSig_both_kstk_Evts_sw+nSig_both_kpipi_Evts_sw+'
                       'nSig_both_pipipi_Evts_sw')
            }
        }
    # define all observables somewhere, and put the into a RooArgSet called obs
    # import observables in to a workspace saved in ws

    # now read the data set
    data = readDataSet(configdict, ws, observables)
    @endcode
    """
    from ROOT import ( TFile, RooWorkspace, RooRealVar, RooCategory,
        RooBinningCategory, RooUniformBinning, RooMappedCategory,
        RooDataSet, RooArgSet, RooArgList )
    import sys, math
    # local little helper routine
    def round_to_even(x):
        xfl = int(math.floor(x))
        rem = x - xfl
        if rem < 0.5: return xfl
        elif rem > 0.5: return xfl + 1
        else:
            if xfl % 2: return xfl + 1
            else: return xfl
    # another small helper routine
    def tokenize(s, delims = '+-*/()?:'):
        # FIXME: this goes wrong for numerical constants like 1.4e-3
        # proposed solution: regexp for general floating point constants,
        # replace occurences of matches with empty string
        delims = [ c for c in delims ]
        delims.insert(0, None)
        for delim in delims:
            tmp = s.split(delim)
            tmp = list(set(( s + ' ' for s in tmp)))
            s = ''.join(tmp)
        tmp = list(set(s.split(None)))
        return tmp
    # figure out which names from the mapping we need - look at the observables
    names = ()
    for n in config['DataSetVarNameMapping'].keys():
        if None != observables.find(n):
            names += (n,)
    # build RooArgSets and maps with source and destination variables
    dmap = { }
    for k in names: dmap[k] = observables.find(k)
    if None in dmap.values():
        raise NameError('Some variables not found in destination: %s' % str(dmap))
    dset = RooArgSet()
    for v in dmap.values(): dset.add(v)
    if None != dset.find('weight'):
        # RooFit insists on weight variable being first in set
        tmpset = RooArgSet()
        tmpset.add(dset.find('weight'))
        it = dset.fwdIterator()
        while True:
            obj = it.next()
            if None == obj: break
            if 'weight' == obj.GetName(): continue
            tmpset.add(obj)
        dset = tmpset
        del tmpset
        ddata = RooDataSet('agglomeration', 'of positronic circuits', dset, 'weight')
    else:
        ddata = RooDataSet('agglomeration', 'of positronic circuits', dset)
    # open file with data sets
    f = TFile(config['DataFileName'], 'READ')
    # get workspace
    fws = f.Get(config['DataWorkSpaceName'])
    ROOT.SetOwnership(fws, True)
    if None == fws or not fws.InheritsFrom('RooWorkspace'):
        # ok, no workspace, so try to read a tree of the same name and
        # synthesize a workspace
        from ROOT import RooWorkspace, RooDataSet, RooArgList
        fws = RooWorkspace(config['DataWorkSpaceName'])
        iset = RooArgSet()
        addiset = RooArgList()
        it = observables.fwdIterator()
        while True:
            obj = it.next()
            if None == obj: break
            name = config['DataSetVarNameMapping'][obj.GetName()]
            vnames = tokenize(name)
            if len(vnames) > 1 and not obj.InheritsFrom('RooAbsReal'):
                print 'Error: Formulae not supported for categories'
                return None
            if obj.InheritsFrom('RooAbsReal'):
                if 1 == len(vnames):
                    # simple case, just add variable
                    var = WS(fws, RooRealVar(name, name, -sys.float_info.max,
                        sys.float_info.max))
                    iset.addClone(var)
                else:
                    # complicated case - add a bunch of observables, and
                    # compute something in a RooFormulaVar
                    from ROOT import RooFormulaVar
                    args = RooArgList()
                    for n in vnames:
                        try:
                            # skip simple numerical factors
                            float(n)
                        except:
                            var = iset.find(n)
                            if None == var:
                                var = WS(fws, RooRealVar(n, n, -sys.float_info.max,
                                    sys.float_info.max))
                                iset.addClone(var)
                                args.add(iset.find(n))
                    var = WS(fws, RooFormulaVar(name, name, name, args))
                    addiset.addClone(var)
            else:
                for dsname in ((config['DataSetNames'], )
                        if type(config['DataSetNames']) == str else
                        config['DataSetNames']):
                    break
                leaf = f.Get(dsname).GetLeaf(name)
                if None == leaf:
                    leaf = f.Get(dsname).GetLeaf(name + '_idx')
                if leaf.GetTypeName() in (
                        'char', 'unsigned char', 'Char_t', 'UChar_t',
                        'short', 'unsigned short', 'Short_t', 'UShort_t',
                        'int', 'unsigned', 'unsigned int', 'Int_t', 'UInt_t',
                        'long', 'unsigned long', 'Long_t', 'ULong_t',
                        'Long64_t', 'ULong64_t', 'long long',
                        'unsigned long long'):
                    var = WS(fws, RooCategory(name, name))
                    tit = obj.typeIterator()
                    ROOT.SetOwnership(tit, True)
                    while True:
                        tobj = tit.Next()
                        if None == tobj: break
                        var.defineType(tobj.GetName(), tobj.getVal())
                else:
                    var = WS(fws, RooRealVar(name, name, -sys.float_info.max,
                        sys.float_info.max))
                iset.addClone(var)
        for dsname in ((config['DataSetNames'], )
               if type(config['DataSetNames']) == str else
               config['DataSetNames']):
            tmpds = WS(fws, RooDataSet(dsname, dsname,f.Get(dsname), iset), [])
            if 0 != addiset.getSize():
                # need to add columns with RooFormulaVars
                tmpds.addColumns(addiset)
            del tmpds
    # local data conversion routine
    def doIt(config, rangeName, dsname, sname, names, dmap, dset, ddata, fws):
        sdata = fws.obj(dsname)
        if None == sdata: return 0
        if None != config['DataSetCuts']:
            # apply any user-supplied cuts
            newsdata = sdata.reduce(config['DataSetCuts'])
            ROOT.SetOwnership(newsdata, True)
            del sdata
            sdata = newsdata
            del newsdata
        sset = sdata.get()
        smap = { }
        for k in names:
            smap[k] = sset.find(config['DataSetVarNameMapping'][k])
        if 'sample' in smap.keys() and None == smap['sample'] and None != sname:
            smap.pop('sample')
            dmap['sample'].setLabel(sname)
        if None in smap.values():
            raise NameError('Some variables not found in source: %s' % str(smap))
        # # additional complication: toys save decay time in ps, data is in nm
        # # figure out which time conversion factor to use
        # timeConvFactor = 1e9 / 2.99792458e8
        # meantime = sdata.mean(smap['time'])
        # if ((dmap['time'].getMin() <= meantime and
        #         meantime <= dmap['time'].getMax() and config['IsToy']) or
        #         not config['IsToy']):
        #     timeConvFactor = 1.
        # print 'DEBUG: Importing data sample meantime = %f, timeConvFactor = %f' % (
        #         meantime, timeConvFactor)
        timeConvFactor = 1.
        # loop over all entries of data set
        ninwindow = 0
        if None != sname:
            sys.stdout.write('Dataset conversion and fixup: %s: progress: ' % sname)
        else:
            sys.stdout.write('Dataset conversion and fixup: progress: ')
        for i in xrange(0, sdata.numEntries()):
            sdata.get(i)
            if 0 == i % 128:
                sys.stdout.write('*')
            vals = { }
            for vname in smap.keys():
                obj = smap[vname]
                if obj.InheritsFrom('RooAbsReal'):
                    val = obj.getVal()
                    vals[vname] = val
                else:
                    val = obj.getIndex()
                    vals[vname] = val
            # first fixup: apply time/timeerr conversion factor
            if 'time' in dmap.keys():
                vals['time'] *= timeConvFactor
            if 'timeerr' in dmap.keys():
                vals['timeerr'] *= timeConvFactor
            # second fixup: only sign of qf is important
            if 'qf' in dmap.keys():
                vals['qf'] = 1 if vals['qf'] > 0.5 else (-1 if vals['qf'] <
                        -0.5 else 0.)
            # third fixup: untagged events are forced to 0.5 mistag
            if ('qt' in dmap.keys() and 'mistag' in dmap.keys() and 0 ==
                    vals['qt']):
                vals['mistag'] = 0.5
            # apply cuts
            inrange = True
            for vname in dmap.keys():
                if not dmap[vname].InheritsFrom('RooAbsReal'): continue
                # no need to cut on untagged events
                if 'mistag' == vname and 0 == vals['qt']: continue
                if None != rangeName and dmap[vname].hasRange(rangeName):
                    if (dmap[vname].getMin(rangeName) > vals[vname] or
                            vals[vname] >= dmap[vname].getMax(rangeName)):
                        inrange = False
                        break
                else:
                    if (dmap[vname].getMin() > vals[vname] or
                            vals[vname] >= dmap[vname].getMax()):
                        inrange = False
                        break
            # skip cuts which are not within the allowed range
            if not inrange: continue
            # copy values over, doing real-category conversions as needed
            for vname in smap.keys():
                dvar, svar = dmap[vname], vals[vname]
                if dvar.InheritsFrom('RooAbsRealLValue'):
                    if float == type(svar): dvar.setVal(svar)
                    elif int == type(svar): dvar.setVal(svar)
                elif dvar.InheritsFrom('RooAbsCategoryLValue'):
                    if int == type(svar): dvar.setIndex(svar)
                    elif float == type(svar):
                        dvar.setIndex(round_to_even(svar))
            if 'weight' in dmap:
                ddata.add(dset, vals['weight'])
            else:
                ddata.add(dset)
            ninwindow = ninwindow + 1
        del sdata
        sys.stdout.write(', done - %d events\n' % ninwindow)
        return ninwindow
    ninwindow = 0
    if type(config['DataSetNames']) == str:
        ninwindow += doIt(config, rangeName, config['DataSetNames'],
                None, names, dmap, dset, ddata, fws)
    else:
        for sname in config['DataSetNames'].keys():
            ninwindow += doIt(config, rangeName, config['DataSetNames'][sname],
                    sname, names, dmap, dset, ddata, fws)
    # free workspace and close file
    del fws
    f.Close()
    del f
    # put the new dataset into our proper workspace
    ddata = WS(ws, ddata, [])
    # for debugging
    if config['Debug']:
        ddata.Print('v')
        if 'qt' in dmap.keys():
            data.table(dmap['qt']).Print('v')
        if 'qf' in dmap.keys():
            data.table(dmap['qf']).Print('v')
        if 'qf' in dmap.keys() and 'qt' in dmap.keys():
            data.table(RooArgSet(dmap['qt'], dmap['qf'])).Print('v')
        if 'sample' in dmap.keys():
            data.table(dmap['sample']).Print('v')
    # all done, return Data to the bridge
    return ddata
Beispiel #10
0
from ROOT import TFile, RooArgSet
dataFile = TFile.Open('P2VVDataSets20112012Reco14_I2Mass_6KKMassBins_2TagCats_kinematics.root')
data = dataFile.Get('JpsiKK_sigSWeight')
dataFile.Close()
varSet = RooArgSet( data.get() )
for var in [ 'wMC', 'hlt2_prescale', 'polarity', 'tagCatP2VV', 'nPVCat', 'BpTCat', 'runPeriod' ] :
    varSet.remove( varSet.find(var) )

from ROOT import RooCategory
varSet11 = RooArgSet(varSet)
varSet12 = RooArgSet(varSet)
runPeriod11 = RooCategory( 'runPeriod', 'runPeriod' )
runPeriod12 = RooCategory( 'runPeriod', 'runPeriod' )
runPeriod11.defineType( 'p2011', 2011 )
runPeriod12.defineType( 'p2012', 2012 )
varSet11.add(runPeriod11)
varSet12.add(runPeriod12)
from ROOT import RooFit, RooDataSet
data11 = RooDataSet( data.GetName(), data.GetTitle(), varSet11, RooFit.Import(data), RooFit.Cut('hlt2_biased==1 && runPeriod==2011') )
data12 = RooDataSet( data.GetName(), data.GetTitle(), varSet12, RooFit.Import(data), RooFit.Cut('hlt2_biased==1 && runPeriod==2012') )

from ROOT import TObject
dataFile11 = TFile.Open( 'P2VVDataSets2011Reco14_I2Mass_6KKMassBins_2TagCats_kinematics_HLT2B.root', 'RECREATE' )
dataFile11.Add(data11)
dataFile11.Write( 'P2VVDataSets2011Reco14_I2Mass_6KKMassBins_2TagCats_kinematics_HLT2B.root', TObject.kOverwrite )
dataFile11.Close()

dataFile12 = TFile.Open( 'P2VVDataSets2012Reco14_I2Mass_6KKMassBins_2TagCats_kinematics_HLT2B.root', 'RECREATE' )
dataFile12.Add(data12)
dataFile12.Write( 'P2VVDataSets2012Reco14_I2Mass_6KKMassBins_2TagCats_kinematics_HLT2B.root', TObject.kOverwrite )
dataFile12.Close()