# globals, and aliases from ROOT import gPad, gStyle, gSystem, kRed, kBlue, kAzure, kGreen, kBlack const = RooRealConstant.value # setup gStyle.SetOptStat('nemrou') canvas = TCanvas('canvas', 'canvas', 800, 600) canvas.Print('{}['.format(plotfile)) # read workspace ffile = TFile.Open(rfile, 'read') workspace = ffile.Get('workspace') pdfs = RooArgList(workspace.allPdfs()) for i in range(pdfs.getSize()): name = pdfs[i].GetName() if name.find(mode) >= 0: mykpdf = pdfs[i] assert(mykpdf) ## variables time = RooRealVar('time', 'Time [ps]', 0.2, 15.0) time.setBins(bins) time.setBins(bins*3, 'cache') kfactor = workspace.var('kfactorVar') kfactor.setRange(0.85, 1.05) gamma = RooRealVar('gamma', 'gamma', 0.661, 0., 3.) kgamma = RooProduct('kgamma', 'kgamma', RooArgList(gamma, kfactor)) dGamma = RooRealVar('dGamma', 'dGamma', -0.106, -3., 3.)
usig2 = 0. totalYield = 0. sigYield = 0. sigErrs = {} sigYieldFilename = 'last_H%i_%s_%iJets_signalYield.txt' % (opts.mH, modeString, opts.Nj) sigYieldsFile = open(sigYieldFilename, 'w') WpJNonPoissonError = 0 print print '-------------------------------' print 'Yields in signal box' print '-------------------------------' for i in range(0, yields.getSize()): theName = yields.at(i).GetName() if theName[0] == 'n': totalYield += yields.at(i).getVal() theIntegral = 1. if (theName == 'nDiboson'): theIntegral = dibosonInt.getVal()/dibosonFullInt.getVal() elif (theName == 'nWjets'): theIntegral = WpJInt.getVal()/WpJFullInt.getVal() if (yields.at(i).getError()**2 > yields.at(i).getVal()): WpJNonPoissonError = sqrt(yields.at(i).getError()**2 - \ yields.at(i).getVal()) else: WpJNonPoissonError = 0. elif (theName == 'nTTbar'): theIntegral = ttbarInt.getVal()/ttbarFullInt.getVal()
def findOnePe(hist, ws, name='x', Npe = 1): fitPed(hist, ws, name) x = ws.var(name) ped = ws.pdf('ped') pedWidth = ws.var('pedWidth') pdfs = RooArgList(ped) pdfList = [] fped = RooRealVar('fped', 'f_{ped}', 0.8, 0., 1.) fractions = RooArgList(fped) fList = [] peList = [] peMean = RooRealVar('peMean', 'mean_{pe}', 6., 0., 20.) peWidth = RooRealVar('peWidth', 'width_{pe}', pedWidth.getVal(), 0., 10.) for i in range(0, Npe): pem = RooFormulaVar('pem{0}'.format(i+1), '@0+{0}*@1'.format(i+1), RooArgList(ws.var('pedMean'), peMean)) peList.append(pem) npepdf = RooGaussian('pe{0}pdf'.format(i+1), 'pe{0}pdf'.format(i+1), x, pem, pedWidth) pdfs.add(npepdf) pdfList.append(npepdf) fnpe = RooRealVar('fpe{0}'.format(i+1), 'fpe{0}'.format(i+1), 0.5, -0.1, 1.0) fractions.add(fnpe) fList.append(fnpe) #bgMean = RooRealVar("bgMean", "bgMean", 6.0, x.getMin(), x.getMax()) bgScale = RooRealVar("bgScale", "bgScale", 0.5, -1.0, Npe + 1.0) bgMean = RooFormulaVar("bgMean", "@1+@0*@2", RooArgList(peMean, ws.var('pedMean'), bgScale)) bgWidthL = RooRealVar("bgWidthL", "bgWidthL", pedWidth.getVal()*2, 0., 25.) bgWidthR = RooRealVar("bgWidthR", "bgWidthR", pedWidth.getVal()*7, 0., 25.) bgGauss = RooBifurGauss("bgGauss", "bgGauss", x, bgMean, bgWidthR, bgWidthR) if (Npe > 1): pdfs.add(bgGauss) else: fractions.remove(fractions.at(fractions.getSize()-1)) ## pem = RooFormulaVar('pem', '@0+@1', RooArgList(peMean, ws.var('pedMean'))) ## firstPe = RooGaussian('firstPe', 'firstPe', x, pem, peWidth) ## pdfs.Print("v") ## fractions.Print("v") pedPlusOne = RooAddPdf('pedPlusOne', 'pedPlusOne', pdfs, fractions, True) ## pedWidth = ped.GetParameter(2) ## pedMean = ped.GetParameter(1) ## pedA = ped.GetParameter(0) secondMax = hist.GetMaximumBin() + 1 goingDown = True maxVal = hist.GetBinContent(secondMax) foundMax = False while (not foundMax) and (secondMax < hist.GetNbinsX()): tmpVal = hist.GetBinContent(secondMax+1) if (tmpVal < maxVal): if not goingDown: foundMax = True else: goingDown = True maxVal = tmpVal secondMax += 1 elif (tmpVal > maxVal): goingDown = False maxVal = tmpVal secondMax += 1 else: maxVal = tmpVal secondMax += 1 secondMaxx = hist.GetBinCenter(secondMax) print 'found 2nd maximum in bin',secondMax,'value',secondMaxx ## peMean.setVal(secondMaxx) ## bgMean.setVal(secondMaxx*0.6) x.setRange('pedPlus_fit', x.getMin(), ws.var('pedMean').getVal()+pedWidth.getVal()*6.*(Npe+0)) pedPlusOne.fitTo(ws.data('ds'), RooFit.Minos(False), RooFit.Range('pedPlus_fit'), RooFit.PrintLevel(1)) getattr(ws, 'import')(pedPlusOne)
parlist = fitresult.floatParsFinal() cmatrix = fitresult.covarianceMatrix() veclist = RooArgList() for i in range(parlist.getSize()): name = "%s_%d" % (parlist[i].GetName(), i) veclist.add(parlist[i].clone(name)) multigauss = RooMultiVarGaussian("multigauss", "multigauss", veclist, parlist, cmatrix) dset = multigauss.generate(RooArgSet(veclist), 1000) fns = [] for entry in range(dset.numEntries()): vecset = dset.get(entry) veclist = RooArgList(vecset) for pars in range(veclist.getSize()): acceptance.SetParameter(pars, veclist[pars].getVal()) fns += [acceptance.Clone("%s_%d" % (acceptance.GetName(), entry))] avgfn = BinnedAvgFunction(fns, xbincs) avgfn.calculate() accfns += [avgfn.get_avg_fn()] accfnerrs += [avgfn.get_avg_fn_var()] means = numpy.zeros(nbins, dtype=float) varis = numpy.zeros(nbins, dtype=float) for ibin in range(nbins): means[ibin] = accfns[0][ibin] / accfns[1][ibin] varis[ibin] = accfnerrs[0][ibin] + accfnerrs[1][ibin]
def accbuilder(time, knots, coeffs): # build acceptance function from copy import deepcopy myknots = deepcopy(knots) mycoeffs = deepcopy(coeffs) from ROOT import (RooBinning, RooArgList, RooPolyVar, RooCubicSplineFun) if (len(myknots) != len(mycoeffs) or 0 >= min(len(myknots), len(mycoeffs))): raise ValueError('ERROR: Spline knot position list and/or coefficient' 'list mismatch') # create the knot binning knotbinning = WS(ws, RooBinning(time.getMin(), time.getMax(), 'knotbinning')) for v in myknots: knotbinning.addBoundary(v) knotbinning.removeBoundary(time.getMin()) knotbinning.removeBoundary(time.getMax()) knotbinning.removeBoundary(time.getMin()) knotbinning.removeBoundary(time.getMax()) oldbinning, lo, hi = time.getBinning(), time.getMin(), time.getMax() time.setBinning(knotbinning, 'knotbinning') time.setBinning(oldbinning) time.setRange(lo, hi) del knotbinning del oldbinning del lo del hi # create the knot coefficients coefflist = RooArgList() i = 0 for v in mycoeffs: coefflist.add(WS(ws, RooRealVar('SplineAccCoeff%u' % i, 'SplineAccCoeff%u' % i, v))) i = i + 1 del mycoeffs coefflist.add(one) i = i + 1 myknots.append(time.getMax()) myknots.reverse() fudge = (myknots[0] - myknots[1]) / (myknots[2] - myknots[1]) lastmycoeffs = RooArgList( WS(ws, RooConstVar('SplineAccCoeff%u_coeff0' % i, 'SplineAccCoeff%u_coeff0' % i, 1. - fudge)), WS(ws, RooConstVar('SplineAccCoeff%u_coeff1' % i, 'SplineAccCoeff%u_coeff1' % i, fudge))) del myknots coefflist.add(WS(ws, RooPolyVar( 'SplineAccCoeff%u' % i, 'SplineAccCoeff%u' % i, coefflist.at(coefflist.getSize() - 2), lastmycoeffs))) del i # create the spline itself tacc = WS(ws, RooCubicSplineFun('SplineAcceptance', 'SplineAcceptance', time, 'knotbinning', coefflist)) del lastmycoeffs # make sure the acceptance is <= 1 for generation m = max([coefflist.at(j).getVal() for j in xrange(0, coefflist.getSize())]) from ROOT import RooProduct c = WS(ws, RooConstVar('SplineAccNormCoeff', 'SplineAccNormCoeff', 0.99 / m)) tacc_norm = WS(ws, RooProduct('SplineAcceptanceNormalised', 'SplineAcceptanceNormalised', RooArgList(tacc, c))) del c del m del coefflist return tacc, tacc_norm
def findOnePe(hist, ws, name='x', Npe=1): fitPed(hist, ws, name) x = ws.var(name) ped = ws.pdf('ped') pedWidth = ws.var('pedWidth') pdfs = RooArgList(ped) pdfList = [] fped = RooRealVar('fped', 'f_{ped}', 0.8, 0., 1.) fractions = RooArgList(fped) fList = [] peList = [] peMean = RooRealVar('peMean', 'mean_{pe}', 6., 0., 20.) peWidth = RooRealVar('peWidth', 'width_{pe}', pedWidth.getVal(), 0., 10.) for i in range(0, Npe): pem = RooFormulaVar('pem{0}'.format(i + 1), '@0+{0}*@1'.format(i + 1), RooArgList(ws.var('pedMean'), peMean)) peList.append(pem) npepdf = RooGaussian('pe{0}pdf'.format(i + 1), 'pe{0}pdf'.format(i + 1), x, pem, pedWidth) pdfs.add(npepdf) pdfList.append(npepdf) fnpe = RooRealVar('fpe{0}'.format(i + 1), 'fpe{0}'.format(i + 1), 0.5, -0.1, 1.0) fractions.add(fnpe) fList.append(fnpe) #bgMean = RooRealVar("bgMean", "bgMean", 6.0, x.getMin(), x.getMax()) bgScale = RooRealVar("bgScale", "bgScale", 0.5, -1.0, Npe + 1.0) bgMean = RooFormulaVar("bgMean", "@1+@0*@2", RooArgList(peMean, ws.var('pedMean'), bgScale)) bgWidthL = RooRealVar("bgWidthL", "bgWidthL", pedWidth.getVal() * 2, 0., 25.) bgWidthR = RooRealVar("bgWidthR", "bgWidthR", pedWidth.getVal() * 7, 0., 25.) bgGauss = RooBifurGauss("bgGauss", "bgGauss", x, bgMean, bgWidthR, bgWidthR) if (Npe > 1): pdfs.add(bgGauss) else: fractions.remove(fractions.at(fractions.getSize() - 1)) ## pem = RooFormulaVar('pem', '@0+@1', RooArgList(peMean, ws.var('pedMean'))) ## firstPe = RooGaussian('firstPe', 'firstPe', x, pem, peWidth) ## pdfs.Print("v") ## fractions.Print("v") pedPlusOne = RooAddPdf('pedPlusOne', 'pedPlusOne', pdfs, fractions, True) ## pedWidth = ped.GetParameter(2) ## pedMean = ped.GetParameter(1) ## pedA = ped.GetParameter(0) secondMax = hist.GetMaximumBin() + 1 goingDown = True maxVal = hist.GetBinContent(secondMax) foundMax = False while (not foundMax) and (secondMax < hist.GetNbinsX()): tmpVal = hist.GetBinContent(secondMax + 1) if (tmpVal < maxVal): if not goingDown: foundMax = True else: goingDown = True maxVal = tmpVal secondMax += 1 elif (tmpVal > maxVal): goingDown = False maxVal = tmpVal secondMax += 1 else: maxVal = tmpVal secondMax += 1 secondMaxx = hist.GetBinCenter(secondMax) print 'found 2nd maximum in bin', secondMax, 'value', secondMaxx ## peMean.setVal(secondMaxx) ## bgMean.setVal(secondMaxx*0.6) x.setRange('pedPlus_fit', x.getMin(), ws.var('pedMean').getVal() + pedWidth.getVal() * 6. * (Npe + 0)) pedPlusOne.fitTo(ws.data('ds'), RooFit.Minos(False), RooFit.Range('pedPlus_fit'), RooFit.PrintLevel(1)) getattr(ws, 'import')(pedPlusOne)
from ROOT import gPad, gStyle, kRed, kBlue, kAzure, kGreen, kBlack gStyle.SetOptStat('nemrou') canvas = TCanvas('canvas', 'canvas', 800, 600) canvas.Print('{}['.format(plotfile)) ffile = TFile.Open(rfile, 'read') if ifpdf: workspace = ffile.Get('workspace') kfactor = workspace.var('kfactorVar') kfactor.setRange(0.9, 1.1) pdfs = RooArgList(workspace.allPdfs()) for i in range(pdfs.getSize()): fr = kfactor.frame() pdfs[i].plotOn(fr, RooFit.LineColor(kBlack), RooFit.FillColor(kAzure+1), RooFit.DrawOption('lf')) # FIXME: doesn't draw the line!' fr.Draw() canvas.Print(plotfile) else: modes = {} klist = ffile.GetListOfKeys() for item in klist: name = item.GetName() if not name.startswith('mBresn'): print 'MSG: Skipping, unknown object: %s' % name continue # ntuples are named mBresn_* sample = name.split('_') modes[sample[1]] = modes.get(sample[1],[]) + [item] # up and down for each mode
# knot coefficients coefflist = RooArgList() for i, v in enumerate(spline_coeffs): coefflist.add(const(v)) i = len(spline_coeffs) coefflist.add(one) spline_knots.append(time.getMax()) spline_knots.reverse() fudge = (spline_knots[0] - spline_knots[1]) / (spline_knots[2] - spline_knots[1]) lastmycoeffs = RooArgList() lastmycoeffs.add(const(1. - fudge)) lastmycoeffs.add(const(fudge)) polyvar = RooPolyVar('{}_SplineAccCoeff{}'.format(mode, i), '', coefflist.at(coefflist.getSize() - 2), lastmycoeffs) coefflist.add(polyvar) del i # create the spline itself tacc = RooCubicSplineFun('{}_SplineAcceptance'.format(mode), '', time, '{}_knotbinning'.format(mode), coefflist) #del lastmycoeffs, coefflist from ROOT import TCanvas, TLine #tacc.DrawClass(); aCanvas = canvas import sys
parlist = fitresult.floatParsFinal() cmatrix = fitresult.covarianceMatrix() veclist = RooArgList() for i in range(parlist.getSize()): name = '%s_%d' % (parlist[i].GetName(), i) veclist.add(parlist[i].clone(name)) multigauss = RooMultiVarGaussian('multigauss', 'multigauss', veclist, parlist, cmatrix) dset = multigauss.generate(RooArgSet(veclist), 1000) fns = [] for entry in range(dset.numEntries()): vecset = dset.get(entry) veclist = RooArgList(vecset) for pars in range(veclist.getSize()): acceptance.SetParameter(pars, veclist[pars].getVal()) fns += [acceptance.Clone('%s_%d' % (acceptance.GetName(), entry))] avgfn = BinnedAvgFunction(fns, xbincs) avgfn.calculate() accfns += [avgfn.get_avg_fn()] accfnerrs += [avgfn.get_avg_fn_var()] means = numpy.zeros(nbins, dtype=float) varis = numpy.zeros(nbins, dtype=float) for ibin in range(nbins): means[ibin] = accfns[0][ibin] / accfns[1][ibin] varis[ibin] = accfnerrs[0][ibin] + accfnerrs[1][ibin]
inFile.ls() #s = raw_input("Press Enter to continue"); theTH1DHist = TH1F() inFile.GetObject("etaHist", theTH1DHist) #rooRealVarList += [RooRealVar('x','x',0.0,1.0)]; x = RooRealVar('x', 'x', 0.0, 1.0) xRegions, x = divideEtaTH1Ds(theTH1DHist, x, 5, fNum) fNum += 1 #xList += [RooAbsReal(x)] xRegionsList.add(xRegions.clone(xRegions.GetName() + "_clone")) #etasetDataset.add(); import sys #sys.exit(0); for i in range(xRegionsList.getSize()): xRegionsList.at(0).Print() #.Print();#writeToStream(ROOT.cout,True); print "\n\nNOCRASH2\n\n" from ROOT import TFile import time os.chdir(os.environ['B2DXFITTERSROOT'] + '/tutorial') f = TFile('xRegionsList_%f.root' % time.time(), 'recreate') f.WriteTObject(xRegionsList, 'xRegionsList_%f' % time.time()) f.Close() del f
def buildBDecayTimePdf( config, # configuration dictionary name, # 'Signal', 'DsPi', ... ws, # RooWorkspace into which to put the PDF time, timeerr, qt, qf, mistag, tageff, # potential observables Gamma, DeltaGamma, DeltaM, # decay parameters C, D, Dbar, S, Sbar, # CP parameters timeresmodel = None, # decay time resolution model acceptance = None, # acceptance function timeerrpdf = None, # pdf for per event time error mistagpdf = None, # pdf for per event mistag mistagobs = None, # real mistag observable kfactorpdf = None, # distribution k factor smearing kvar = None, # variable k which to integrate out aprod = None, # production asymmetry adet = None, # detection asymmetry atageff = None # asymmetry in tagging efficiency ): """ build a B decay time pdf parameters: ----------- config -- config dictionary name -- name prefix for newly created objects ws -- workspace into which to import created objects time -- time observable timeerr -- time error (constant for average, or per-event obs.) qt -- tagging decision (RooCategory) qf -- final state charge (RooCategory) mistag -- mistag (either average, or calibrated omega(eta)) tageff -- tagging efficiency Gamma -- width of decay (1/lifetime) DeltaGamma -- width difference DeltaM -- mass difference C -- term in front of cos in RooBDecay D -- term in front of sinh in RooBDecay (f final state) Dbar -- term in front of sinh in RooBDecay (fbar final state) S -- term in front of sin in RooBDecay (f final state) Sbar -- term in front of sin in RooBDecay (fbar final state) timeresmodel -- decay time resolution model (or None for delta fn) acceptance -- acceptance (or None for flat efficiency(decay time)) timeerrpdf -- decay time error distribution (or None for average) mistagpdf -- per event mistag distribution(s) (or None for average) mistagobs -- (uncalibrated) per event mistag observable (or None) kfactorpdf -- k factor distribution (None for no k-factor correction) kvar -- k factor variable (None for no k-factor correction) aprod -- production asymmetry (None for zero asymmetry) adet -- detection asymmetry (None for zero asymmetry) atageff -- tagging eff. asymmetry (None, or list, 1 per tagger/cat.) returns: -------- a time pdf built according to specification This routine is a rather flexible beast and can accomodate a multitude of use cases: - ideal, average or per-event decay time resolution (pdf needed for per-event case) - ideal, average, per category, or per-event mistag: * ideal: mistag = zero, mistagpdf = mistagobs = None * average: mistag = RooRealVar/RooConstVar, mistagpdf = mistagobs = None * per category: qt = -Ncat, ... , 0, 1, ..., Ncat, mistag = RooArgList of per-category average mistags (1, ..., Ncat),i mistagpdf = mistagobs = None (can be used for calibrations!) * per event: qt = -1, 0, +1, mistag = calibrated mistag omega(eta), mistagpdf = P(eta), mistagobs = eta * per event, mutually exclusive taggers: like per category, but mistagpdf becomes a list of pdfs as well * one can have as many tagging asymmetries as there are categories/mutually exclusive taggers (for a single one, just pass the RooRealVar, else a RooArgList of RooRealVars) * if the calibration needs to differ between B and Bbar, mistag can also be a list of two calibrations "[ calB, calBbar ]", or a list of RooArgLists (one per category/mutually exclusive tagger) - optionally, a k-factor correction can be applied for partially reconstructed or misidentified modes See the classes RooBDecay (in RooFit), DecRateCoeff (B2DXFitters) and RooKResModel (also B2DXFitters) for details on the precise meaning and definition of these variables. relevant config dictionary keys: -------------------------------- 'Debug': print all arguments to buildBDecayTimePdf before doing anything - useful to see what "really goes in" when it doesn't do what it should... 'UseKFactor': if True, apply the k-factor correction, if False, k-factor correction can be disabled globally (useful for fast fitback look toys, systematic studies, etc.) 'ParameteriseIntegral': if True, save a huge amount of CPU by parametrising the resolutition model integral and its convolutions with the decay time pdf as a function of (per-event) decay time; see parameteriseResModelIntegrals for details For more information on how various bits affect the pdf built, see also the documentation for the following helper routines: applyBinnedAcceptance, applyKFactorSmearing, applyDecayTimeErrPdf, applyUnbinnedAcceptance, parameteriseResModelIntegrals """ # Look in LHCb-INT-2011-051 for the conventions used from ROOT import ( RooConstVar, RooProduct, RooTruthModel, RooGaussModel, Inverse, RooBDecay, RooProdPdf, RooArgSet, DecRateCoeff, RooArgList ) if config['Debug']: print 72 * '#' kwargs = { 'config': config, 'name': name, 'ws': ws, 'time': time, 'timeerr': timeerr, 'qt': qt, 'qf': qf, 'mistag': mistag, 'tageff': tageff, 'Gamma': Gamma, 'DeltaGamma': DeltaGamma, 'DeltaM': DeltaM, 'C': C, 'D': D, 'Dbar':Dbar, 'S': S, 'Sbar': Sbar, 'timeresmodel': timeresmodel, 'acceptance': acceptance, 'timeerrpdf': timeerrpdf, 'mistagpdf': mistagpdf, 'mistagobs': mistagobs, 'kfactorpdf': kfactorpdf, 'kvar': kvar, 'aprod': aprod, 'adet': adet, 'atageff': atageff } print 'buildBDecayTimePdf(' for kw in kwargs: print '\t%s = %s' % (kw, kwargs[kw]) print '\t)' print 72 * '#' # constants used zero = WS(ws, RooConstVar('zero', 'zero', 0.)) one = WS(ws, RooConstVar('one', 'one', 1.)) if None == aprod: aprod = zero if None == adet: adet = zero if None == atageff: atageff = [ zero ] if None == mistagpdf: mistagobs = None else: # None != mistagpdf if None == mistagobs: raise NameError('mistag pdf set, but no mistag observable given') # if no time resolution model is set, fake one if timeresmodel == None: timeresmodel = WS(ws, RooTruthModel('%s_TimeResModel' % name, '%s time resolution model' % name, time)) elif timeresmodel == 'Gaussian': timeresmodel = WS(ws, RooGaussModel('%s_TimeResModel' % name, '%s time resolution model' % name, time, zero, timeerr)) # apply acceptance (if needed) timeresmodel = applyBinnedAcceptance( config, ws, time, timeresmodel, acceptance) if config['UseKFactor']: timeresmodel = applyKFactorSmearing(config, ws, time, timeresmodel, kvar, kfactorpdf, [ Gamma, DeltaGamma, DeltaM ]) if config['ParameteriseIntegral']: parameteriseResModelIntegrals(config, ws, timeerrpdf, timeerr, timeresmodel) # if there is a per-event mistag distributions and we need to do things # correctly if None != mistagpdf: otherargs = [ mistagobs, RooArgList(*mistagpdf), RooArgList(*tageff) ] else: otherargs = [ RooArgList(*tageff) ] bcalib = RooArgList() bbarcalib = RooArgList() for t in mistag: bcalib.add(t[0]) if len(t) > 1: bbarcalib.add(t[1]) otherargs.append(bcalib) if (bbarcalib.getSize()): otherargs.append(bbarcalib) otherargs += [ aprod, adet, RooArgList(*atageff) ] flag = 0 if 'Bs2DsK' == name and 'CADDADS' == config['Bs2DsKCPObs']: flag = DecRateCoeff.AvgDelta # build coefficients to go into RooBDecay cosh = WS(ws, DecRateCoeff('%s_cosh' % name, '%s_cosh' % name, DecRateCoeff.CPEven, qf, qt, one, one, *otherargs)) sinh = WS(ws, DecRateCoeff('%s_sinh' % name, '%s_sinh' % name, flag | DecRateCoeff.CPEven, qf, qt, D, Dbar, *otherargs)) cos = WS(ws, DecRateCoeff('%s_cos' % name, '%s_cos' % name, DecRateCoeff.CPOdd, qf, qt, C, C, *otherargs)) sin = WS(ws, DecRateCoeff('%s_sin' % name, '%s_sin' % name, flag | DecRateCoeff.CPOdd | DecRateCoeff.Minus, qf, qt, S, Sbar, *otherargs)) del flag del otherargs # build (raw) time pdf tau = WS(ws, Inverse('%sTau' % Gamma.GetName(), '%s #tau' % Gamma.GetName(), Gamma)) retVal = WS(ws, RooBDecay( '%s_RawTimePdf' % name, '%s raw time pdf' % name, time, tau, DeltaGamma, cosh, sinh, cos, sin, DeltaM, timeresmodel, RooBDecay.SingleSided)) retVal = applyDecayTimeErrPdf(config, name, ws, time, timeerr, qt, qf, mistagobs, retVal, timeerrpdf, mistagpdf) # if we do not bin the acceptance, we apply it here retVal = applyUnbinnedAcceptance(config, name, ws, retVal, acceptance) retVal.SetNameTitle('%s_TimePdf' % name, '%s full time pdf' % name) # return the copy of retVal which is inside the workspace return WS(ws, retVal)
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
def buildSplineAcceptance( ws, # workspace into which to import time, # time variable pfx, # prefix to be used in names knots, # knots coeffs, # acceptance coefficients floatParams = False, # float acceptance parameters debug = False # debug printout ): """ build a spline acceptance function ws -- workspace into which to import acceptance functions time -- time observable pfx -- prefix (mode name) from which to build object names knots -- list of knot positions coeffs -- spline coefficients floatParams -- if True, spline acceptance parameters will be floated debug -- if True, print some debugging output returns a pair of acceptance functions, first the unnormalised one for fitting, then the normalised one for generation The minimum and maximum of the range of the time variable implicitly defines the position of the first and last knot. The other knot positions are passed in knots. Conversely, the coeffs parameter records the height of the sline at all but the last two knot positions. The next to last knot coefficient is fixed to 1.0, thus fixing the overall scale of the acceptance function. The spline coefficient for the last knot is fixed by extrapolating linearly from the two knots before; this prevents statistical fluctuations at the low stats high lifetime end of the spectrum to curve the spline. """ # build acceptance function from copy import deepcopy myknots = deepcopy(knots) mycoeffs = deepcopy(coeffs) from ROOT import (RooBinning, RooArgList, RooPolyVar, RooCubicSplineFun, RooConstVar, RooProduct, RooRealVar) if (len(myknots) != len(mycoeffs) or 0 >= min(len(myknots), len(mycoeffs))): raise ValueError('ERROR: Spline knot position list and/or coefficient' 'list mismatch') one = WS(ws, RooConstVar('one', '1', 1.0)) # create the knot binning knotbinning = WS(ws, RooBinning(time.getMin(), time.getMax(), '%s_knotbinning' % pfx)) for v in myknots: knotbinning.addBoundary(v) knotbinning.removeBoundary(time.getMin()) knotbinning.removeBoundary(time.getMax()) knotbinning.removeBoundary(time.getMin()) knotbinning.removeBoundary(time.getMax()) oldbinning, lo, hi = time.getBinning(), time.getMin(), time.getMax() time.setBinning(knotbinning, '%s_knotbinning' % pfx) time.setBinning(oldbinning) time.setRange(lo, hi) del knotbinning del oldbinning del lo del hi # create the knot coefficients coefflist = RooArgList() i = 0 for v in mycoeffs: if floatParams: coefflist.add(WS(ws, RooRealVar('%s_SplineAccCoeff%u' % (pfx, i), 'v_{%u}' % (i+1), v, 0., 3.))) else: coefflist.add(WS(ws, RooConstVar('%s_SplineAccCoeff%u' % (pfx, i), 'v_{%u}' % (i+1), v))) i = i + 1 del mycoeffs coefflist.add(one) i = i + 1 myknots.append(time.getMax()) myknots.reverse() fudge = (myknots[0] - myknots[1]) / (myknots[2] - myknots[1]) lastmycoeffs = RooArgList( WS(ws, RooConstVar('%s_SplineAccCoeff%u_coeff0' % (pfx, i), '%s_SplineAccCoeff%u_coeff0' % (pfx, i), 1. - fudge)), WS(ws, RooConstVar('%s_SplineAccCoeff%u_coeff1' % (pfx, i), '%s_SplineAccCoeff%u_coeff1' % (pfx, i), fudge))) del myknots coefflist.add(WS(ws, RooPolyVar( '%s_SplineAccCoeff%u' % (pfx, i), 'v_{%u}' % (i+1), coefflist.at(coefflist.getSize() - 2), lastmycoeffs))) del i if debug: print 'DEBUG: Spline Coeffs: %s' % str([ coefflist.at(i).getVal() for i in xrange(0, coefflist.getSize()) ]) # create the spline itself tacc = WS(ws, RooCubicSplineFun('%s_SplineAcceptance' % pfx, '%s_SplineAcceptance' % pfx, time, '%s_knotbinning' % pfx, coefflist)) del lastmycoeffs if not floatParams: # make sure the acceptance is <= 1 for generation m = max([coefflist.at(j).getVal() for j in xrange(0, coefflist.getSize())]) c = WS(ws, RooConstVar('%s_SplineAccNormCoeff' % pfx, '%s_SplineAccNormCoeff' % pfx, 0.99 / m)) tacc_norm = WS(ws, RooProduct('%s_SplineAcceptanceNormalised' % pfx, '%s_SplineAcceptanceNormalised' % pfx, RooArgList(tacc, c))) del c del m else: tacc_norm = None # not supported when floating del coefflist return tacc, tacc_norm