Пример #1
0
def addTaggingObservables( dataSet, iTagName, tagCatName, tagDecisionName, estimWTagName, tagCatBins ) :
    """add tagging observables to data set
    """

    # get observables from data set
    obsSet      = dataSet.get(0)
    tagDecision = obsSet.find(tagDecisionName)
    estimWTag   = obsSet.find(estimWTagName)

    # create initial state tag
    from P2VV.Load import P2VVLibrary
    from ROOT import RooTagDecisionWrapper
    iTagWrapper = RooTagDecisionWrapper(iTagName, 'Flavour tag', tagDecision)

    # create tagging category
    from ROOT import RooThresholdCategory
    tagCatFormula = RooThresholdCategory( tagCatName, 'P2VV tagging category', estimWTag, tagCatBins[0][0], tagCatBins[0][1] )
    for cat in range( 1, len(tagCatBins) ) : tagCatFormula.addThreshold( tagCatBins[cat][2], tagCatBins[cat][0], tagCatBins[cat][1] )

    # add tagging category binning to estimated wrong-tag probability variable
    from array import array
    from ROOT import RooBinning
    binBounds = array( 'd', [ 0. ] + [ tagCatBins[ len(tagCatBins) - it ][2] for it in range( 1, len(tagCatBins) + 1 ) ] )
    tagCatBinning = RooBinning( len(binBounds) - 1, binBounds, 'tagCats' )
    estimWTag.setBinning( tagCatBinning, 'tagCats' )

    # create new columns in data set
    dataSet.addColumn(iTagWrapper)
    dataSet.addColumn(tagCatFormula)

    # check tagging columns
    for obsSet in dataSet :
        assert obsSet.getCatIndex(iTagName) == +1 or obsSet.getCatIndex(iTagName) == -1,\
                'P2VV - ERROR: addTaggingObservables: initial state flavour tag has value %+d' % obsSet.getCatIndex(iTagName)
        if len(binBounds) < 3 : continue
        assert obsSet.getCatIndex(tagDecisionName) == 0 or obsSet.getCatIndex(iTagName) == obsSet.getCatIndex(tagDecisionName),\
                'P2VV - ERROR: addTaggingObservables: initial state flavour tag and tag decision have different values: %+d and %+d'\
                % ( obsSet.getCatIndex(iTagName), obsSet.getCatIndex(tagDecisionName) )
        assert ( obsSet.getCatIndex(tagDecisionName) == 0 and obsSet.getRealValue(estimWTagName) >= binBounds[-2] )\
                or ( obsSet.getCatIndex(tagDecisionName) != 0 and obsSet.getRealValue(estimWTagName) < binBounds[-2] ),\
                'P2VV - ERROR: addTaggingObservables: tag decision = %+d, while estimated wrong-tag probability = %.10f (threshold = %.10f)'\
                % ( obsSet.getCatIndex(tagDecisionName), obsSet.getRealValue(estimWTagName), binBounds[-2] )
        assert ( obsSet.getCatIndex(tagDecisionName) == 0 and obsSet.getCatIndex(tagCatName) == 0 )\
                or ( obsSet.getCatIndex(tagDecisionName) != 0 and obsSet.getCatIndex(tagCatName) > 0 ),\
                'P2VV - ERROR: addTaggingObservables: tag decision = %+d, while tagging category = %d'\
                % ( obsSet.getCatIndex(tagDecisionName), obsSet.getCatIndex(tagCatName) )
Пример #2
0
def createCategoryHistogram(ds,x,numCat,categoryList = None):

    from ROOT import RooAbsData, RooDataSet, RooPrintable, RooThresholdCategory, THStack, TF1
    from ROOT.Math import GaussIntegrator
    import ROOT

    if categoryList==None:
        categoryList = []
        for i in range(numCat):
            categoryList += ["Cat"+str(i+1)]

    etaHist = RooAbsData.createHistogram(ds,'eta',100);

    histSum = etaHist.Integral();
    #print "HISTSUM",histSum, etaHist.Integral(0,50)+etaHist.Integral(51,100),etaHist.Integral(0,50),etaHist.Integral(51,100),"\n";

    limList = [-1];

    for i in range(1,numCat):
        start = limList[i-1]+1;
        targetVal = histSum/numCat;
        left = start;
        right = etaHist.GetNbinsX();
        #print "\n\n\n",etaHist.Integral(start,start+1),"\n\n\n\n";
        while abs(left-right)>1:
            if etaHist.Integral(start,int((left+right)/2))>targetVal:
                right = (left + right) / 2;
            else:
                left = (left + right) / 2;
            #print start, left, right, etaHist.Integral(start,int((left+right)/2)),targetVal,",",

        if abs(etaHist.Integral(start,left)-targetVal) > abs(etaHist.Integral(start,right)-targetVal):
            limList += [right];
        else:
            limList += [left];

    limList[0] = 0;

    print limList
    
    xRegions = RooThresholdCategory("tageffRegion", "region of tageff", x, categoryList[numCat-1]);

    for i in range(0,numCat-1):
        xRegions.addThreshold(etaHist.GetXaxis().GetBinCenter(limList[i+1]),categoryList[i]);

    xRegions.writeToStream(ROOT.cout,False);
    xRegions.writeToStream(ROOT.cout,True);
    '''
    #etaSet = RooDataSet('etaSet','etaSet',ds,RooArgSet(ds.get().find['eta']));
    #etaSet.Print();
    #etaHist.SetAxisRange(0.1,0.2);
    #etaHist.SetAxisRange(0.1,0.3);
    ROOT.gStyle.SetPalette(ROOT.kOcean);
    
    #create stack to contain the chopped TH1Ds
    etaHistStack = THStack("etaHistStack", "Stack of TH1Ds");

    limList += [etaHist.GetNbinsX()];
    histCutterFunc = TF1("histCutterFunc","((x>=[0])?((x<[1])?1:0):0)*x",0.0,1.0);

    for i in range(len(limList)-1):
        
        etaHistClone = etaHist.Clone();
        #etaHistClone.SetBins(limList[i+1]-limList[i],etaHist.GetBinContent(limList[i]),etaHist.GetBinContent(limList[i+1]));
        histCutterFunc.SetParameter(0,etaHist.GetXaxis().GetBinCenter(limList[i]));
        histCutterFunc.SetParameter(1,etaHist.GetXaxis().GetBinCenter(limList[i+1]));
        etaHistClone.Multiply(histCutterFunc);
        etaHistClone.SetFillColor(38+i);
        #etaHist.DrawCopy("hist PFC");
        etaHistStack.Add(etaHistClone);

    etaHistStack.Draw("hist PFC");
    s = raw_input("Press Enter to continue...");'''
    
    return xRegions#,x
Пример #3
0
def divideEtaTH1Ds(etaHist,
                   numCat,
                   sourceFileName=None,
                   categoryList=None,
                   drawHist=False):
    # etaHist - the TH1D to split into categories
    # numCat - number of tagging categories
    # categoryList - list of tagging category names
    # drawHist - whether or not to display each category-split histogram
    # sourceFileName - name of etaHist root file

    #normalize etaHist
    etaHist.Scale(1.0 / etaHist.Integral())

    #create default category names if none are given
    if categoryList == None:
        categoryList = []
        for i in range(numCat):
            categoryList += ["Cat" + str(i)]

    histSum = etaHist.Integral()

    #get list of limits
    limList = [-1]

    for i in range(1, numCat):
        start = limList[i - 1] + 1
        targetVal = histSum / numCat
        left = start
        right = etaHist.GetNbinsX()
        #print "\n\n\n",etaHist.Integral(start,start+1),"\n\n\n\n";
        while abs(left - right) > 1:
            if etaHist.Integral(start, int((left + right) / 2)) > targetVal:
                right = (left + right) / 2
            else:
                left = (left + right) / 2
            #print start, left, right, etaHist.Integral(start,int((left+right)/2)),targetVal,",",

        if abs(etaHist.Integral(start, left) -
               targetVal) > abs(etaHist.Integral(start, right) - targetVal):
            limList += [right]
        else:
            limList += [left]

    limList[0] = 0
    limList += [etaHist.GetNbinsX()]

    print limList

    #rangeFit is a 4-element TList containing:
    # - the sourceFileName as element 0
    # - a RooThresholdCategory as element 2, and its RooRealVar as element 1
    # - a tList of tFitResult as element 3
    rangeFit = TList()
    rangeFit.AddLast(TObjString(sourceFileName))
    rangeFit.AddLast(RooRealVar('x', 'x', 0.0, 1.0))
    rangeFit.AddLast(
        RooThresholdCategory("tageffRegion", "region of tageff",
                             rangeFit.At(1), "Cat" + str(numCat)))
    rangeFit.AddLast(TList())

    #plot each category-split histogram, if necessary
    if drawHist == True:

        ROOT.gStyle.SetPalette(ROOT.kOcean)

        #create stack to contain the category TH1Ds
        etaHistStack = THStack("etaHistStack", "Stack of TH1Ds")

        #create category-masking function for TH1D clones
        histCutterFunc = TF1("histCutterFunc",
                             "((x>=[0])?((x<[1])?1.0:0.0):0.0)", 0.0, 1.0)

        for i in range(len(limList) - 1):

            etaHistClone = etaHist.Clone()
            histCutterFunc.SetParameter(
                0,
                etaHist.GetXaxis().GetBinCenter(limList[i]))
            histCutterFunc.SetParameter(
                1,
                etaHist.GetXaxis().GetBinCenter(limList[i + 1]))
            #histCutterFunc.Draw();
            #raw_input("Press Enter to continue to next hisCutterFunc");
            etaHistClone.Multiply(histCutterFunc)
            etaHistClone.SetFillColor(38 + i)
            etaHistStack.Add(etaHistClone)

        etaHistClone = etaHist.Clone()
        etaHistClone.SetFillColor(38 + len(limList))
        #etaHistStack.Add(etaHistClone);

        import time

        os.chdir(os.environ['B2DXFITTERSROOT'] + '/tutorial')

        if (not (os.path.isdir('fits'))):
            os.mkdir('fits')

        os.chdir('fits')
        histCanvas = TCanvas()
        etaHistStack.Draw("hist PFC")
        #etaHistClone.Draw("hist PFC");
        #histCanvas.SaveAs('tagRegionFitList_%f.pdf' % time.time());

    #create thresholds and fitting functions (all linear, but with different ranges corresponding to the categories)

    currentRangeTF1List = TList()

    for i in range(1, numCat + 1):
        if (i <= numCat):
            rangeFit.At(2).addThreshold(
                etaHist.GetXaxis().GetBinCenter(limList[i]),
                categoryList[i - 1])
        currentRangeTF1List.AddLast(
            TF1(
                "fitFuncEtaset", "[0]+[1]*(x-" + str(
                    etaHist.Integral(limList[i - 1], limList[i]) /
                    (limList[i] - limList[i - 1])) + ")",
                etaHist.GetXaxis().GetBinCenter(limList[i - 1]),
                etaHist.GetXaxis().GetBinCenter(limList[i])))
        #currentRangeTF1 = TF1("fitFuncEtaset","[0]+[1]*x",etaHist.GetXaxis().GetBinCenter(limList[i-1]),etaHist.GetXaxis().GetBinCenter(limList[i]));
        rangeFit.Last().AddLast(
            etaHist.Fit(currentRangeTF1List.Last(), "R0S").Get().Clone())
        if (drawHist == True):
            currentRangeTF1List.Last().DrawCopy('same')
        #raw_input('Press Enter to continue to next fit function');
        #currentRangeTF1.IsA().Destructor(currentRangeTF1);
        #print "P0, P1 = ",rangeFit.Last().Last().Parameter(0), rangeFit.Last().Last().Parameter(1);

    histCanvas.SaveAs('tagRegionFitList_%f.pdf' % time.time())
    currentRangeTF1List.Delete()
    #for i in range(1,numCat+1):
    #currentRangeTF1List.Last().IsA().Destructor(currentRangeTF1List.Last());

    #s = raw_input("Press Enter to continue...");

    return rangeFit
def divideEtaTH1Ds(etaHist,
                   x,
                   numCat,
                   fileNumber,
                   categoryList=None,
                   drawHist=False):

    if categoryList == None:
        categoryList = []
        for i in range(numCat):
            categoryList += ["Cat" + str(i)]

    histSum = etaHist.Integral()
    #print "HISTSUM",histSum, etaHist.Integral(0,50)+etaHist.Integral(51,100),etaHist.Integral(0,50),etaHist.Integral(51,100),"\n";

    limList = [-1]

    for i in range(1, numCat):
        start = limList[i - 1] + 1
        targetVal = histSum / numCat
        left = start
        right = etaHist.GetNbinsX()
        #print "\n\n\n",etaHist.Integral(start,start+1),"\n\n\n\n";
        while abs(left - right) > 1:
            if etaHist.Integral(start, int((left + right) / 2)) > targetVal:
                right = (left + right) / 2
            else:
                left = (left + right) / 2
            #print start, left, right, etaHist.Integral(start,int((left+right)/2)),targetVal,",",

        if abs(etaHist.Integral(start, left) -
               targetVal) > abs(etaHist.Integral(start, right) - targetVal):
            limList += [right]
        else:
            limList += [left]

    limList[0] = 0

    print limList

    xRegions = RooThresholdCategory("tageffRegion", "region of tageff", x,
                                    "Cat" + str(numCat))
    xRegions.writeToStream(cout, False)
    xRegions.writeToStream(cout, True)

    fitResultList = []

    #create thresholds and list of fitting functions (all linear, but with
    #different ranges corresponding to the categories
    for i in range(1, numCat):
        xRegions.addThreshold(etaHist.GetXaxis().GetBinCenter(limList[i]),
                              categoryList[i - 1])
        currentRangeTF1 = TF1(
            "fitFuncEtaset" + str(fileNumber) + "Cat" + str(i - 1),
            "[0]+[1]*x",
            etaHist.GetXaxis().GetBinCenter(limList[i - 1]),
            etaHist.GetXaxis().GetBinCenter(limList[i]))
        fitResultList += [etaHist.Fit(currentRangeTF1, "R0S").Get()]
        currentRangeTF1.IsA().Destructor(currentRangeTF1)
        print "P0, P1 = ", fitResultList[-1].Parameter(
            0), fitResultList[-1].Parameter(1)
        #raw_input("Press Enter to continue");

    print "\n\n\n\n", drawHist, "\n\n\n\n"
    if drawHist == True:

        #etaSet = RooDataSet('etaSet','etaSet',ds,RooArgSet(ds.get().find['eta']));
        #etaSet.Print();
        #etaHist.SetAxisRange(0.1,0.2);
        #etaHist.SetAxisRange(0.1,0.3);
        ROOT.gStyle.SetPalette(ROOT.kOcean)

        #create stack to contain the chopped TH1Ds
        etaHistStack = THStack("etaHistStack", "Stack of TH1Ds")

        limList += [etaHist.GetNbinsX()]
        histCutterFunc = TF1("histCutterFunc", "((x>=[0])?((x<[1])?1:0):0)*x",
                             0.0, 1.0)

        for i in range(len(limList) - 1):

            etaHistClone = etaHist.Clone()
            #etaHistClone.SetBins(limList[i+1]-limList[i],etaHist.GetBinContent(limList[i]),etaHist.GetBinContent(limList[i+1]));
            histCutterFunc.SetParameter(
                0,
                etaHist.GetXaxis().GetBinCenter(limList[i]))
            histCutterFunc.SetParameter(
                1,
                etaHist.GetXaxis().GetBinCenter(limList[i + 1]))
            etaHistClone.Multiply(histCutterFunc)
            etaHistClone.SetFillColor(38 + i)
            #etaHist.DrawCopy("hist PFC");
            etaHistStack.Add(etaHistClone)

        etaHistStack.Draw("hist PFC")
        #s = raw_input("Press Enter to continue...");

    return xRegions, x
Пример #5
0
def createCategoryHistogram(ds, x, numCat, categoryList=None):

    from ROOT import RooAbsData, RooDataSet, RooPrintable, RooThresholdCategory, THStack, TF1
    from ROOT.Math import GaussIntegrator
    import ROOT

    if categoryList == None:
        categoryList = []
        for i in range(numCat):
            categoryList += ["Cat" + str(i + 1)]

    etaHist = RooAbsData.createHistogram(ds, 'eta', 100)

    histSum = etaHist.Integral()
    #print "HISTSUM",histSum, etaHist.Integral(0,50)+etaHist.Integral(51,100),etaHist.Integral(0,50),etaHist.Integral(51,100),"\n";

    limList = [-1]

    for i in range(1, numCat):
        start = limList[i - 1] + 1
        targetVal = histSum / numCat
        left = start
        right = etaHist.GetNbinsX()
        #print "\n\n\n",etaHist.Integral(start,start+1),"\n\n\n\n";
        while abs(left - right) > 1:
            if etaHist.Integral(start, int((left + right) / 2)) > targetVal:
                right = (left + right) / 2
            else:
                left = (left + right) / 2
            #print start, left, right, etaHist.Integral(start,int((left+right)/2)),targetVal,",",

        if abs(etaHist.Integral(start, left) -
               targetVal) > abs(etaHist.Integral(start, right) - targetVal):
            limList += [right]
        else:
            limList += [left]

    limList[0] = 0

    print limList

    xRegions = RooThresholdCategory("tageffRegion", "region of tageff", x,
                                    categoryList[numCat - 1])

    for i in range(0, numCat - 1):
        xRegions.addThreshold(etaHist.GetXaxis().GetBinCenter(limList[i + 1]),
                              categoryList[i])

    xRegions.writeToStream(ROOT.cout, False)
    xRegions.writeToStream(ROOT.cout, True)

    #etaSet = RooDataSet('etaSet','etaSet',ds,RooArgSet(ds.get().find['eta']));
    #etaSet.Print();
    #etaHist.SetAxisRange(0.1,0.2);
    #etaHist.SetAxisRange(0.1,0.3);

    from ROOT import TCanvas

    histCanvas = TCanvas()

    ROOT.gStyle.SetPalette(ROOT.kOcean)

    #create stack to contain the chopped TH1Ds
    #etaHistStack = THStack("etaHistStack", "Distribution of #eta;#eta;number of pseudo-experiments");

    limList += [etaHist.GetNbinsX()]
    histCutterFunc = TF1("histCutterFunc", "((x>=[0])?((x<[1])?1:0):0)", 0.0,
                         1.0)

    etaMax = etaHist.GetMaximum()
    print etaMax

    cloneList = []

    etaHist.GetYaxis().SetRangeUser(0, etaMax * 1.05)

    etaHist.SetLineColor(ROOT.kWhite)
    etaHist.Draw("histSame")
    etaHist.SetTitle(
        'Distribution of #eta (%s %s)' %
        ((originSuffix if originSuffix == 'MC' else 'Data'), taggerType))
    etaHist.GetXaxis().SetTitle('#eta')

    for i in range(len(limList) - 1):

        cloneList = [etaHist.Clone()]
        #etaHistClone.SetBins(limList[i+1]-limList[i],etaHist.GetBinContent(limList[i]),etaHist.GetBinContent(limList[i+1]));
        histCutterFunc.SetParameter(
            0,
            etaHist.GetXaxis().GetBinCenter(limList[i]))
        histCutterFunc.SetParameter(
            1,
            etaHist.GetXaxis().GetBinCenter(limList[i + 1]))
        cloneList[-1].Multiply(histCutterFunc)
        cloneList[-1].SetFillColor(38 + i)
        cloneList[-1].SetLineColor(ROOT.kBlack)
        if i == 0:
            cloneList[-1].GetYaxis().SetRangeUser(0, etaMax * 1.05)
        cloneList[-1].Draw("histSAME")
        #etaHistStack.Add(etaHistClone);

    #etaHistStack.Draw();
    histCanvas.Update()
    histCanvas.SaveAs('catHist-%s-%s.pdf' % (originSuffix, taggerType))
    s = raw_input("Press Enter to continue...")
    sys.exit(0)

    return xRegions  #,x