Beispiel #1
0
# B mass pdf
from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as Signal_BMass, LP2011_Background_Mass as Background_BMass
sig_m = Signal_BMass(Name = 'sig_m', mass = m, m_sig_mean = dict(Value = 5365, MinMax = (5363,5372)))

# J/psi mass pdf
mpsi_mean  = RealVar('mpsi_mean',   Unit = 'MeV', Value = 3097, MinMax = (3070, 3110))
mpsi_sigma = RealVar('mpsi_sigma',  Unit = 'MeV', Value = 10, MinMax = (5, 20))
mpsi_alpha = RealVar('mpsi_alpha',  Unit = '', Value = 1.36, MinMax = (0.5, 3))
mpsi_n = RealVar('mpsi_n',  Unit = '', Value = 2, MinMax = (0.1, 4), Constant = True)
psi_m  = Pdf(Name = 'psi_m', Type = CrystalBall, Parameters = [mpsi, mpsi_mean, mpsi_sigma, mpsi_alpha, mpsi_n])
# J/psi background
psi_c = RealVar( 'psi_c',  Unit = '1/MeV', Value = -0.0004, MinMax = (-0.1, -0.0000001))
bkg_mpsi = Pdf(Name = 'bkg_mpsi',  Type = Exponential, Parameters = [mpsi, psi_c])

# Create combinatorical background component
bkg_m = Background_BMass( Name = 'bkg_m', mass = m, m_bkg_exp  = dict( Name = 'm_bkg_exp' ) )
background = Component('background', (bkg_m.pdf(), bkg_mpsi), Yield = (100000,100,500000) )

psi_background = Component('psi_background', (bkg_m.pdf(), psi_m), Yield= (200000,500,500000) )

from P2VV.Utilities.DataHandling import readData
tree_name = 'DecayTree'
prefix = '/stuff/PhD' if os.path.exists('/stuff') else '/bfys/raaij'
if dataSample == '2011':
    input_file = os.path.join(prefix, 'p2vv/data/Bs2JpsiPhi_ntupleB_for_fitting_20121012_MagDownMagUp.root')
elif dataSample == '2012':
    input_file = os.path.join(prefix, '/stuff/PhD/p2vv/data/Bs2JpsiPhi_2012_ntupleB_20121212.root')
else:
    raise RuntimeError

cut = 'runNumber > 0 && sel == 1 && sel_cleantail == 1 && (hlt1_biased == 1 || hlt1_unbiased_dec == 1) && hlt2_biased == 1 && '
Beispiel #2
0
    def __init__(self, time, masses, t_diff = None, MassResult = None,
                 InputFile = "/bfys/raaij/p2vv/data/Bs2JpsiPhiPrescaled_2011.root",
                 Workspace = 'Bs2JpsiPhiPrescaled_2011_workspace', Data = 'data',
                 UseKeysPdf = False, Weights = 'B', Draw = False, Reweigh = {}):
        assert(Weights in ShapeBuilder.__weights)
        self.__weights = Weights
        self.__time = time
        self.__t_diff = t_diff
        pos = Workspace.find('201')
        self.__year = Workspace[pos : pos + 4]
        self.__input_ws = None
        self.__ws = RooObject().ws()
        self.__shapes = {}
        self.__diff__shapes = {}

        self.__masses = masses

        self._sig = Component('wpv_signal', [], Yield = (1000, 10, 5e5))
        self._psi = Component('wpv_jpsi',   [], Yield = (5000, 10, 5e5))
        self._bkg = Component('wpv_bkg',    [], Yield = (5000, 10, 5e5))

        if 'B' in masses:
            ## m_sig_mean  = RealVar('wpv_m_sig_mean',   Unit = 'MeV', Value = 5365, MinMax = (5363, 5372))
            ## m_sig_sigma = RealVar('wpv_m_sig_sigma',  Unit = 'MeV', Value = 10, MinMax = (1, 20))
            ## from ROOT import RooGaussian as Gaussian
            ## self._sig_mass = Pdf(Name = 'wpv_sig_m', Type = Gaussian, Parameters = (masses['B'], m_sig_mean, m_sig_sigma ))
            self._sig_mass = BMassPdf(masses['B'], Name = 'wpv_sig_mass', ParNamePrefix = "wpv",
                                      AvSigParameterisation = True)
            self._bkg_mass = BBkgPdf(masses['B'],  Name = 'wpv_bkg_mass', ParNamePrefix = "wpv",
                                     m_bkg_exp = dict(Name = 'm_bkg_exp', Value = -0.0017,
                                                      MinMax = (-0.01, -0.00001)))
            self._sig[masses['B']] = self._sig_mass.pdf()
            self._psi[masses['B']] = self._bkg_mass.pdf()
            self._bkg[masses['B']] = self._bkg_mass.pdf()
        if 'jpsi' in masses:
            self._sig_mpsi = PsiMassPdf(masses['jpsi'], Name = 'wpv_sig_mpsi', ParNamePrefix = "wpv")
            self._bkg_mpsi = PsiBkgPdf(masses['jpsi'], Name = 'wpv_bkg_mpsi', ParNamePrefix = "wpv")
            self._sig[masses['jpsi']] = self._sig_mpsi.pdf()
            self._psi[masses['jpsi']] = self._sig_mpsi.pdf()
            self._bkg[masses['jpsi']] = self._bkg_mpsi.pdf()

        self.__components = {'jpsi' : dict(jpsi = self._psi, bkg = self._bkg),
                             'B'    : dict(B = self._sig, bkg = self._bkg),
                             'both' : dict(B = self._sig, jpsi = self._psi, bkg = self._bkg)}
        self.__pdf = buildPdf(self.__components[Weights].values(), Observables = masses.values(),
                             Name = 'wpv_mass_pdf')
        if MassResult:
            ## Use the provided mass result to set all the parameter values, only float the yields
            pdf_params = self.__pdf.getParameters(RooArgSet(*masses.values()))
            for p in MassResult.floatParsFinal():
                ## ignore yields
                if any((p.GetName().startswith(n) for n in ['N_', 'mpsi_c'])):
                    continue
                ## Find pdf parameter, add "wpv_" prefix
                pdf_p = pdf_params.find('wpv_' + p.GetName())
                if pdf_p:
                    pdf_p.setVal(p.getVal())
                    pdf_p.setError(p.getError())
                    pdf_p.setConstant(True)

        self.__pdf.Print("t")

        from ROOT import TFile
        input_file = TFile.Open(InputFile)
        if not input_file or not input_file.IsOpen():
            raise OSError

        if Workspace:
            self.__input_ws = input_file.Get(Workspace)
            if not self.__input_ws:
                print 'Cannot find workspace %s in mixing file.' % Workspace
                raise RuntimeError
            self._data = self.__input_ws.data(Data)
            if not self._data:
                print 'Cannot find data in workspace %s.' % Workspace
                raise RuntimeError
        else:
            self._data = input_file.Get(Data)

        if not self._data.get().find(time.GetName()) and "refit" in time.GetName():
            from ROOT import RooFormulaVar, RooArgList
            def __add_alias(name, obs):
                obs_name = obs.GetName()[:-6]
                do = self._data.get().find(obs_name)
                rf = RooFormulaVar(name, name, "@0", RooArgList(do))
                a = self._data.addColumn(rf)
                a.setMin(obs.getMin())
                a.setMax(obs.getMax())
                return a

            ## Add refit observables
            time = __add_alias("time_refit", time)
            self.__time = time
            self.__time.Print()
            if t_diff:
                t_diff = __add_alias("time_diff_refit", t_diff)
                self.__t_diff = t_diff

        if t_diff:
            self._data = self._data.reduce("{0} > {1} && {0} < {2} && {3} > {4} && {3} < {5}".format(time.GetName(), time.getMin(), time.getMax(), t_diff.GetName(), t_diff.getMin(), t_diff.getMax()))
        else:
            self._data = self._data.reduce("{0} > {1} && {0} < {2}".format(time.GetName(), time.getMin(), time.getMax()))

        # self._data = self._data.reduce("mass > 5348 && mass < 5388")
        fitOpts = dict(NumCPU = 4, Save = True, Minimizer = 'Minuit2', Optimize = 2)
        self.__result = self.__pdf.fitTo(self._data, **fitOpts)

        from P2VV.Utilities.SWeights import SData
        for p in self.__pdf.Parameters(): p.setConstant(not p.getAttribute('Yield'))
        splot = SData(Pdf = self.__pdf, Data = self._data, Name = 'MixingMassSplot')
        self.__sdatas = {}
        self.__reweigh_weights = {}
        for key, c in self.__components[Weights].iteritems():
            sdata = splot.data(c.GetName())

            if 'Data' in Reweigh and key in Reweigh['Data']:
                from array import array
                from ROOT import RooBinning

                source = Reweigh['Data'][key]
                binning = Reweigh['Binning']
                if type(binning) == array:
                    binning = RooBinning(len(binning) - 1, binning)
                    binning.SetName('reweigh')
                    Reweigh['DataVar'].setBinning(binning, 'reweigh')

                source_obs = source.get().find('nPV')
                cat_name = 'nPVs_' + key
                source_cat = source.get().find(cat_name)
                if source_cat:
                    # Remove previous weights to make sure we get it right
                    new_vars = source.get()
                    new_vars.remove(source_cat)
                    source = source.reduce(new_vars)
                    source_obs = source.get().find(source_obs.GetName())

                source_obs.setBinning(binning, 'reweigh')                
                source_cat = BinningCategory(Name = cat_name, Observable = source_obs,
                                             Binning = binning, Data = source, Fundamental = True)
                from P2VV.Reweighing import reweigh
                sdata, weights = reweigh(sdata, sdata.get().find('nPV'),
                                         source, source_cat)
                self.__reweigh_weights[key] = weights

            sdata = self.__ws.put(sdata)
            self.__sdatas[c] = sdata

        rho_keys = dict((v, k) for k, v in self.__components[Weights].iteritems())
        self.__shapes = {}
        self.__diff_shapes = {}
        for c, sdata in self.__sdatas.iteritems():
            if UseKeysPdf:
                rk = rho_keys[c]
                rho = ShapeBuilder.__rho[rk] if rk in ShapeBuilder.__rho else 1.
                time_shape = KeysPdf(Name = 'wpv_%s_pdf' % c.GetName(), Observable = time, Data = sdata, Rho = rho)
                if t_diff:
                    diff_shape = KeysPdf(Name = 'wpv_%s_diff_pdf' % c.GetName(), Observable = t_diff, Data = sdata)

            else:
                time_shape = HistPdf(Name = 'wpv_%s_pdf' % c.GetName(), Observables = [time],
                                Data = sdata, Binning = {time : 35})
                if t_diff:
                    diff_shape = HistPdf(Name = 'wpv_%s_diff_pdf' % c.GetName(), Observables = [t_diff],
                                         Data = sdata, Binning = {time : 35})
            self.__shapes[c] = time_shape
            if t_diff:
                self.__diff_shapes[c] = diff_shape
        if Draw:
            self.__draw()
Beispiel #3
0
class ShapeBuilder(object):
    __weights = set(('jpsi', 'B', 'both'))
    __rho = dict(B = 2., jpsi = 2.)

    def __init__(self, time, masses, t_diff = None, MassResult = None,
                 InputFile = "/bfys/raaij/p2vv/data/Bs2JpsiPhiPrescaled_2011.root",
                 Workspace = 'Bs2JpsiPhiPrescaled_2011_workspace', Data = 'data',
                 UseKeysPdf = False, Weights = 'B', Draw = False, Reweigh = {}):
        assert(Weights in ShapeBuilder.__weights)
        self.__weights = Weights
        self.__time = time
        self.__t_diff = t_diff
        pos = Workspace.find('201')
        self.__year = Workspace[pos : pos + 4]
        self.__input_ws = None
        self.__ws = RooObject().ws()
        self.__shapes = {}
        self.__diff__shapes = {}

        self.__masses = masses

        self._sig = Component('wpv_signal', [], Yield = (1000, 10, 5e5))
        self._psi = Component('wpv_jpsi',   [], Yield = (5000, 10, 5e5))
        self._bkg = Component('wpv_bkg',    [], Yield = (5000, 10, 5e5))

        if 'B' in masses:
            ## m_sig_mean  = RealVar('wpv_m_sig_mean',   Unit = 'MeV', Value = 5365, MinMax = (5363, 5372))
            ## m_sig_sigma = RealVar('wpv_m_sig_sigma',  Unit = 'MeV', Value = 10, MinMax = (1, 20))
            ## from ROOT import RooGaussian as Gaussian
            ## self._sig_mass = Pdf(Name = 'wpv_sig_m', Type = Gaussian, Parameters = (masses['B'], m_sig_mean, m_sig_sigma ))
            self._sig_mass = BMassPdf(masses['B'], Name = 'wpv_sig_mass', ParNamePrefix = "wpv",
                                      AvSigParameterisation = True)
            self._bkg_mass = BBkgPdf(masses['B'],  Name = 'wpv_bkg_mass', ParNamePrefix = "wpv",
                                     m_bkg_exp = dict(Name = 'm_bkg_exp', Value = -0.0017,
                                                      MinMax = (-0.01, -0.00001)))
            self._sig[masses['B']] = self._sig_mass.pdf()
            self._psi[masses['B']] = self._bkg_mass.pdf()
            self._bkg[masses['B']] = self._bkg_mass.pdf()
        if 'jpsi' in masses:
            self._sig_mpsi = PsiMassPdf(masses['jpsi'], Name = 'wpv_sig_mpsi', ParNamePrefix = "wpv")
            self._bkg_mpsi = PsiBkgPdf(masses['jpsi'], Name = 'wpv_bkg_mpsi', ParNamePrefix = "wpv")
            self._sig[masses['jpsi']] = self._sig_mpsi.pdf()
            self._psi[masses['jpsi']] = self._sig_mpsi.pdf()
            self._bkg[masses['jpsi']] = self._bkg_mpsi.pdf()

        self.__components = {'jpsi' : dict(jpsi = self._psi, bkg = self._bkg),
                             'B'    : dict(B = self._sig, bkg = self._bkg),
                             'both' : dict(B = self._sig, jpsi = self._psi, bkg = self._bkg)}
        self.__pdf = buildPdf(self.__components[Weights].values(), Observables = masses.values(),
                             Name = 'wpv_mass_pdf')
        if MassResult:
            ## Use the provided mass result to set all the parameter values, only float the yields
            pdf_params = self.__pdf.getParameters(RooArgSet(*masses.values()))
            for p in MassResult.floatParsFinal():
                ## ignore yields
                if any((p.GetName().startswith(n) for n in ['N_', 'mpsi_c'])):
                    continue
                ## Find pdf parameter, add "wpv_" prefix
                pdf_p = pdf_params.find('wpv_' + p.GetName())
                if pdf_p:
                    pdf_p.setVal(p.getVal())
                    pdf_p.setError(p.getError())
                    pdf_p.setConstant(True)

        self.__pdf.Print("t")

        from ROOT import TFile
        input_file = TFile.Open(InputFile)
        if not input_file or not input_file.IsOpen():
            raise OSError

        if Workspace:
            self.__input_ws = input_file.Get(Workspace)
            if not self.__input_ws:
                print 'Cannot find workspace %s in mixing file.' % Workspace
                raise RuntimeError
            self._data = self.__input_ws.data(Data)
            if not self._data:
                print 'Cannot find data in workspace %s.' % Workspace
                raise RuntimeError
        else:
            self._data = input_file.Get(Data)

        if not self._data.get().find(time.GetName()) and "refit" in time.GetName():
            from ROOT import RooFormulaVar, RooArgList
            def __add_alias(name, obs):
                obs_name = obs.GetName()[:-6]
                do = self._data.get().find(obs_name)
                rf = RooFormulaVar(name, name, "@0", RooArgList(do))
                a = self._data.addColumn(rf)
                a.setMin(obs.getMin())
                a.setMax(obs.getMax())
                return a

            ## Add refit observables
            time = __add_alias("time_refit", time)
            self.__time = time
            self.__time.Print()
            if t_diff:
                t_diff = __add_alias("time_diff_refit", t_diff)
                self.__t_diff = t_diff

        if t_diff:
            self._data = self._data.reduce("{0} > {1} && {0} < {2} && {3} > {4} && {3} < {5}".format(time.GetName(), time.getMin(), time.getMax(), t_diff.GetName(), t_diff.getMin(), t_diff.getMax()))
        else:
            self._data = self._data.reduce("{0} > {1} && {0} < {2}".format(time.GetName(), time.getMin(), time.getMax()))

        # self._data = self._data.reduce("mass > 5348 && mass < 5388")
        fitOpts = dict(NumCPU = 4, Save = True, Minimizer = 'Minuit2', Optimize = 2)
        self.__result = self.__pdf.fitTo(self._data, **fitOpts)

        from P2VV.Utilities.SWeights import SData
        for p in self.__pdf.Parameters(): p.setConstant(not p.getAttribute('Yield'))
        splot = SData(Pdf = self.__pdf, Data = self._data, Name = 'MixingMassSplot')
        self.__sdatas = {}
        self.__reweigh_weights = {}
        for key, c in self.__components[Weights].iteritems():
            sdata = splot.data(c.GetName())

            if 'Data' in Reweigh and key in Reweigh['Data']:
                from array import array
                from ROOT import RooBinning

                source = Reweigh['Data'][key]
                binning = Reweigh['Binning']
                if type(binning) == array:
                    binning = RooBinning(len(binning) - 1, binning)
                    binning.SetName('reweigh')
                    Reweigh['DataVar'].setBinning(binning, 'reweigh')

                source_obs = source.get().find('nPV')
                cat_name = 'nPVs_' + key
                source_cat = source.get().find(cat_name)
                if source_cat:
                    # Remove previous weights to make sure we get it right
                    new_vars = source.get()
                    new_vars.remove(source_cat)
                    source = source.reduce(new_vars)
                    source_obs = source.get().find(source_obs.GetName())

                source_obs.setBinning(binning, 'reweigh')                
                source_cat = BinningCategory(Name = cat_name, Observable = source_obs,
                                             Binning = binning, Data = source, Fundamental = True)
                from P2VV.Reweighing import reweigh
                sdata, weights = reweigh(sdata, sdata.get().find('nPV'),
                                         source, source_cat)
                self.__reweigh_weights[key] = weights

            sdata = self.__ws.put(sdata)
            self.__sdatas[c] = sdata

        rho_keys = dict((v, k) for k, v in self.__components[Weights].iteritems())
        self.__shapes = {}
        self.__diff_shapes = {}
        for c, sdata in self.__sdatas.iteritems():
            if UseKeysPdf:
                rk = rho_keys[c]
                rho = ShapeBuilder.__rho[rk] if rk in ShapeBuilder.__rho else 1.
                time_shape = KeysPdf(Name = 'wpv_%s_pdf' % c.GetName(), Observable = time, Data = sdata, Rho = rho)
                if t_diff:
                    diff_shape = KeysPdf(Name = 'wpv_%s_diff_pdf' % c.GetName(), Observable = t_diff, Data = sdata)

            else:
                time_shape = HistPdf(Name = 'wpv_%s_pdf' % c.GetName(), Observables = [time],
                                Data = sdata, Binning = {time : 35})
                if t_diff:
                    diff_shape = HistPdf(Name = 'wpv_%s_diff_pdf' % c.GetName(), Observables = [t_diff],
                                         Data = sdata, Binning = {time : 35})
            self.__shapes[c] = time_shape
            if t_diff:
                self.__diff_shapes[c] = diff_shape
        if Draw:
            self.__draw()

    def __draw(self):
        from ROOT import kDashed, kRed, kGreen, kBlue, kBlack
        from ROOT import TCanvas
        from ROOT import TPaveText
        self.__year_label = TPaveText(0.71, 0.72, 0.89, 0.85, "NDC")
        self.__year_label.SetFillColor(0)
        self.__year_label.AddText(self.__year)
        self.__year_label.SetBorderSize(0)

        obs = self.__masses.values()
        self.__canvas = TCanvas('wpv_canvas', 'wpv_canvas', len(obs) * 600, 533)
        for (p,o) in zip(self.__canvas.pads(len(obs)), obs):
            from P2VV.Utilities.Plotting import plot
            plot(p, o, pdf = self.__pdf, data = self._data
                 , dataOpts = dict(MarkerSize = 0.8, MarkerColor = kBlack, Binning = 50)
                 , pdfOpts  = dict(LineWidth = 2)
                 , plotResidHist = 'BX'
                 , xTitle = 'M_{#mu^{+}#mu^{-}} [MeV/c^{2}]'
                 , yTitle = 'Candidates / (%2.1f MeV/c^{2})' % ((o.getMax() - o.getMin()) / float(50))
                 , components = { 'wpv_bkg_*'   : dict( LineColor = kRed,   LineStyle = kDashed )
                                  , 'wpv_psi_*' : dict( LineColor = kGreen, LineStyle = kDashed )
                                  , 'wpv_sig_*' : dict( LineColor = kBlue,  LineStyle = kDashed )
                                  }
                 )
            self.__canvas.cd(1)
            self.__year_label.Draw()
            self.__canvas.Update()
        self.__draw_time()

        if self.__t_diff:
            self.__draw_t_diff()

    def __draw_time(self):
        from ROOT import kDashed, kRed, kGreen, kBlue, kBlack
        from ROOT import TCanvas
        from itertools import product
        self.__time_canvases = []
        t = self.__time
        from P2VV.Utilities.Plotting import plot
        from ROOT import RooArgSet
        for logy, (c, shape) in product((True, False), self.__shapes.items()):
            cn = 'wpv_time_%s' % c.GetName()
            if logy: cn += '_log'
            p = TCanvas(cn, cn, 600, 400)
            self.__time_canvases.append(p)
            nBins = 80
            frame = plot(p, t, pdf = shape, data = self.__sdatas[c]
                         , frameOpts = dict(Title = c.GetName())
                         , dataOpts = dict(MarkerSize = 0.8, Binning = nBins, MarkerColor = kBlack)
                         , pdfOpts  = dict(LineWidth = 2)
                         , logy = logy, yScale = (1 if logy else 0, None)
                         , plotResidHist = False)[0]
            frame.GetXaxis().SetTitle('t [ps]')
            if logy:
                frame.GetYaxis().SetRangeUser(1, 7000 if self.__year == '2012' else 5000)
            frame.GetYaxis().SetTitle('Candidates / (%3.2f ps)' % ((t.getMax() - t.getMin()) / float(nBins)))
            self.__year_label.Draw()
            from P2VV.Utilities.Resolution import plot_dir
            p.Print(os.path.join(plot_dir, 'wpv_time_%s_%s_%s.pdf' % (c.GetName()[4:], self.__year, 'log' if logy else 'linear')), EmbedFonts = True)
            
    def __draw_t_diff(self):
        from ROOT import kDashed, kRed, kGreen, kBlue, kBlack
        from ROOT import TCanvas
        self.__diff_canvases = []
        t_diff = self.__t_diff
        from ROOT import RooArgSet
        for c, shape in self.__diff_shapes.iteritems():
            name = 'wpv_diff_%s' % c.GetName()
            canvas = TCanvas(name, name, 600, 400)
            p = canvas.cd()
            self.__diff_canvases.append(canvas)
            from P2VV.Utilities.Plotting import plot
            plot(p, t_diff, pdf = shape, data = self.__sdatas[c]
                 , dataOpts = dict(MarkerSize = 0.8, Binning = 80, MarkerColor = kBlack)
                 , pdfOpts  = dict(LineWidth = 2)
                 , xTitle = 't_{rec} - t_{true} [ps]'
                 , yTitle = 'Candidates / (12.5 fs)'
                 , logy = False
                 , plotResidHist = False)
            self.__year_label.Draw()
            from P2VV.Utilities.Resolution import plot_dir
            canvas.Print(os.path.join(plot_dir, 'wpv_tdiff_%s_%s_linear.pdf' % (c.GetName()[4:], self.__year)), EmbedFonts = True)

    def sdata(self, key):
        c = self.__components[self.__weights][key]
        return self.__sdatas[c]

    def shape(self, key):
        c = self.__components[self.__weights][key]
        return self.__shapes[c]

    def diff_shape(self, key):
        c = self.__components[self.__weights][key]
        return self.__diff_shapes[c]

    def input_ws(self):
        return self.__input_ws

    def reweigh_weights(self, key):
        try:
            return self.__reweigh_weights[key]
        except AttributeError:
            return {}
Beispiel #4
0
else :
    dataFile = TFile.Open(dataFilePath)
    dataSets = dict( [ ( key, dataFile.Get( 'data_' + key ) ) for key in cutKeys ] )
    dataFile.Close()

print 'build mass PDF'
from P2VV.RooFitWrappers import Component
sigMassComps = Component( 'sigMass', [ ], Yield = ( 85.e3, 0., 2.e5 ) )
bkgMassComps = Component( 'bkgMass', [ ], Yield = (  1.e5, 0., 2.e5 ) )
yields = [ comp.getYield() for comp in [ sigMassComps, bkgMassComps ] ]

from P2VV.Parameterizations.MassPDFs import DoubleGauss_Signal_Mass as SignalBMass
sigMass = SignalBMass( mass = mass )
sigMassComps += sigMass.pdf()
from P2VV.Parameterizations.MassPDFs import LP2011_Background_Mass as BackgroundBMass
bkgMass = BackgroundBMass( mass = mass )
bkgMassComps += bkgMass.pdf()
from P2VV.RooFitWrappers import buildPdf
massPdf = buildPdf( [ sigMassComps, bkgMassComps ], Observables = [ mass ], Name = 'JpsiKKMass' )

ws['N_sigMass'].setVal(123.e3)
ws['m_sig_mean'].setVal(5368.2)
ws['m_sig_sigma_1'].setVal(5.6)
ws['m_sig_sigma_2'].setVal(14.)
ws['m_sig_frac'].setVal(0.65)
ws['m_bkg_exp'].setVal(-0.0015)
massPdf.getVariables().Print('v')

print 'draw mass plots'
from P2VV.Load import LHCbStyle
from ROOT import gStyle, TCanvas, kFullDotLarge, kBlack, kBlue, kGreen, kRed, kYellow
if model == 'ipatia2' :
    from P2VV.Parameterizations.MassPDFs import Ipatia2_Signal_Mass as SignalBMass
    sigMassArgs['m_sig_lambda']  = -2.5
    sigMassArgs['m_sig_zeta']    =  0.01
    sigMassArgs['m_sig_alpha_1'] =  3.0
    sigMassArgs['m_sig_alpha_2'] =  2.5
    sigMassArgs['m_sig_n_1']     =  1.5
    sigMassArgs['m_sig_n_2']     =  2.0
else :
    from P2VV.Parameterizations.MassPDFs import DoubleGauss_Signal_Mass as SignalBMass
signalBMass = SignalBMass( **sigMassArgs )
sigMassComps += signalBMass.pdf()

# build the combinatorial background mass PDF
from P2VV.Parameterizations.MassPDFs import LP2011_Background_Mass as BackgroundBMass
backgroundBMass = BackgroundBMass( Name = 'cbkg_m', mass = observables['mass'] )
cbkgMassComps += backgroundBMass.pdf()

# build total mass PDF
from P2VV.RooFitWrappers import buildPdf
massPdf = buildPdf( massComps, Observables = [ observables['mass'] ], Name = 'JpsiKKMass' )

# get splitting categories and parameters
yieldNames  = [ par.GetName() for par in massPdf.Parameters() if par.getAttribute('Yield') ]
allParNames = [ par.GetName() for par in massPdf.Parameters() if not par.getAttribute('Yield') if not par.isConstant() ]
sigParNames = [ par.GetName() for par in signalBMass.parameters() if not par.isConstant() ]
bkgParNames = [ par.GetName() for par in backgroundBMass.parameters() if not par.isConstant() ]
splitCats = {  observables['runPeriod'].GetName() : set( yieldNames + bkgParNames )
             , observables['KKMassCat'].GetName() : set( yieldNames               )
            }
if not biasedOnly :
from P2VV.RooFitWrappers import Component
sigMassComps  = Component( 'sigMass',  [ ], Yield = ( nSignal,     0., nEvents ) )  # signal
cbkgMassComps = Component( 'cbkgMass', [ ], Yield = ( nBackground, 0., nEvents ) )  # combinatorial background

massComps  = [ sigMassComps, cbkgMassComps ]
yieldNames = [ comp.getYield().GetName() for comp in massComps ]

# build the signal mass PDF
sigMassArgs = dict( Name = 'sig_m', mass = observables['mass'] )
from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as SignalBMass
signalBMass = SignalBMass( **sigMassArgs )

# build the combinatorial background mass PDF
cbkgMassArgs = dict( Name = 'cbkg_m', mass = observables['mass'] )
from P2VV.Parameterizations.MassPDFs import LP2011_Background_Mass as BackgroundBMass
backgroundBMass = BackgroundBMass( **cbkgMassArgs )

# build mass PDF
from P2VV.RooFitWrappers import buildPdf
sigMassComps  += signalBMass.pdf()
cbkgMassComps += backgroundBMass.pdf()
massPdf = buildPdf( massComps, Observables = [ observables['mass'] ], Name = 'JpsiKKMass' )


###########################################################################################################################################
## fit J/psiKK mass distributions ##
####################################

# determine mass parameters with a fit
print 120 * '='
print 'P2VV - INFO: createB2CCFitNTuple: fitting with mass PDF'
Beispiel #7
0
nEvents = data.sumEntries()
nSignal = nEvents * sigFrac
nBackground = nEvents * (1.0 - sigFrac)

## from P2VV.Parameterizations.MassPDFs import Ipatia2_Signal_Mass as SignalBMass
## sig_m = SignalBMass(Name = 'sig_m', mass = mass, m_sig_lambda = -2.5, m_sig_zeta = 0.01,
##                     m_sig_alpha_1 = 3.0, m_sig_alpha_2 = 2.5, m_sig_n_1 = 1.5, m_sig_n_2 = 2.0)
from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as SignalBMass

sig_m = SignalBMass(Name="sig_m", mass=mass)

signal = Component("signal", [sig_m.pdf()], Yield=(nSignal, 0.0, nEvents))

from P2VV.Parameterizations.MassPDFs import LP2011_Background_Mass as BackgroundBMass

bkg_m = BackgroundBMass(Name="bkg_m", mass=mass)
background = Component("background", [bkg_m.pdf()], Yield=(nBackground, 0.0, nEvents))

mass_pdf = buildPdf([signal, background], Observables=[mass], Name="mass_pdf")

fitOpts = dict(NumCPU=4, Optimize=2, Save=True, Timer=True, Minimizer="Minuit2", Offset=True)

mass_result = mass_pdf.fitTo(data, **fitOpts)

# categories for splitting the PDF
if not options.prescaled:
    split_cats = [[excl_biased]]
    # get mass parameters that are split
    split_params = [[par for par in mass_pdf.Parameters() if par.getAttribute("Yield")]]

    # build simultaneous mass PDF
from P2VV.Utilities.DataHandling import readData
data = readData(filePath = input_file, dataSetName = 'DecayTree', NTuple = True,
                observables = [mass, t, st, excl_biased], Rename = 'JpsiphiData', ntupleCuts = cut)

sigFrac = 0.504
nEvents     = data.sumEntries()
nSignal     = nEvents * sigFrac
nBackground = nEvents * ( 1. - sigFrac )

from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as SignalBMass
sig_m = SignalBMass(Name = 'sig_m', mass = mass)

signal = Component( 'signal', [sig_m.pdf()], Yield = ( nSignal,     0., nEvents ) )

from P2VV.Parameterizations.MassPDFs import LP2011_Background_Mass as BackgroundBMass
bkg_m = BackgroundBMass(Name = 'bkg_m', mass = mass)
background = Component( 'background', [bkg_m.pdf()], Yield = ( nBackground, 0., nEvents ) )

mass_pdf = buildPdf( [ signal, background ], Observables = [ mass ], Name = 'mass_pdf')

fitOpts = dict(NumCPU = 4, Optimize = 2, Save = True, Timer = True, Minimizer = 'Minuit2',
               Offset = True)

mass_result = mass_pdf.fitTo(data, **fitOpts)

# categories for splitting the PDF
if data_type.endswith("signal"):
    split_cats = [[excl_biased]]
    # get mass parameters that are split
    split_params = [[par for par in mass_pdf.Parameters() if par.getAttribute('Yield')]]
# now build the actual signal PDF...
from ROOT import RooGaussian as Gaussian
from ROOT import RooExponential as Exponential
from ROOT import RooDecay as Decay

from P2VV.Parameterizations.TimeResolution import Gaussian_TimeResolution as TimeResolution
tres = TimeResolution(Name = 'resolution', time = t, BiasScaleFactor = False,
                      timeResSigma = dict(Value = 0.045, Constant = True),
                      timeResMu = dict(Value = 0, Constant = True), Cache = False)

# B mass pdf
from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as Signal_BMass, LP2011_Background_Mass as Background_BMass
sig_m = Signal_BMass(Name = 'sig_m', mass = m, m_sig_mean = dict( Value = 5365, MinMax = (5363,5372)))

# Create combinatorical background component
bkg_m = Background_BMass( Name = 'bkg_m', mass = m, m_bkg_exp = dict( Name = 'm_bkg_exp' ) )

# Create components
signal_mass = Component('signal', (sig_m.pdf(),), Yield = (30000,100,100000))
background_mass = Component('background', (bkg_m.pdf(),), Yield = (100000,100,300000) )

## Build mass PDF
mass_pdf = buildPdf(Components = (signal_mass, background_mass), Observables = (m, ), Name='mass_pdf')
mass_pdf.Print("t")

## base_location = '/home/raaij'
base_location = '/stuff/PhD/p2vv'

# Build the acceptance using the histogram as starting values
input_file = os.path.join(base_location, 'data/start_values.root')
Beispiel #10
0
from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as Signal_BMass, LP2011_Background_Mass as Background_BMass
sig_m = Signal_BMass(     Name = 'sig_m', mass = m, m_sig_mean = dict( Value = 5365, MinMax = (5363,5372) ) )

# J/psi mass pdf
mpsi_mean  = RealVar('mpsi_mean',   Unit = 'MeV', Value = 3097, MinMax = (3070, 3110))
mpsi_sigma = RealVar('mpsi_sigma',  Unit = 'MeV', Value = 10, MinMax = (5, 20))
mpsi_alpha = RealVar('mpsi_alpha',  Unit = '', Value = 1.8, MinMax = (0.5, 3), Constant = True)
mpsi_n = RealVar('mpsi_n',  Unit = '', Value = 2, MinMax = (0.1, 4), Constant = True)
psi_m  = Pdf(Name = 'psi_m', Type = CrystalBall, Parameters = [mpsi, mpsi_mean, mpsi_sigma, mpsi_alpha, mpsi_n])

# J/psi background
psi_c = RealVar( 'psi_c',  Unit = '1/MeV', Value = -0.0004, MinMax = (-0.1, -0.0000001))
bkg_mpsi = Pdf(Name = 'bkg_mpsi',  Type = Exponential, Parameters = [mpsi, psi_c])

# Create combinatorical background component
bkg_m = Background_BMass( Name = 'bkg_m', mass = m, m_bkg_exp  = dict( Name = 'm_bkg_exp' ) )

# Create components
signal_mass = Component('signal', (sig_m.pdf(), psi_m), Yield = (30000,100,100000))
psi_background_mass = Component('psi_background', (bkg_m.pdf(), psi_m), Yield= (100000,500,200000) )
background_mass = Component('background', (bkg_m.pdf(), bkg_mpsi), Yield = (100000,100,300000) )

## Build mass PDF
mass_pdf = buildPdf(Components = (signal_mass, background_mass), Observables = (m, ), Name='mass_pdf')
mass_pdf.Print("t")

## base_location = '/home/raaij'
base_location = '/stuff/PhD/p2vv'

# Build the acceptance using the histogram as starting values
input_file = os.path.join(base_location, 'data/start_values.root')