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
Exemple #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
Exemple #3
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