Beispiel #1
0
class HistogramSaver:
    def __init__(self, settings=None):
        print 'Starting Histogram Saver'
        self.settings = settings
        self.run = array(self.settings.runInfo['run'], 'I')
        self.eventNumber = array(int(0), 'I')
        self.plotPath = '.'
        self.plotRootPath = './root/'
        self.plotPdfPath = './pdf/'
        self.paveTextOptions = {}
        self.dateTime = TDatime()
        self.paveText = TPaveText(0.07, 0, 0.22, 0.10, 'NDC')
        self.UpdatePaveText()
        self.verb = self.settings.runInfo['verbose']
        self.optStat1D = 'nemr'
        self.optStat2D = 'ne'
        self.DefaultPlotStyle()

    def DefaultPlotStyle(self):
        gStyle.SetPalette(55)  # 55 is kRainBow. 53 is kDarkBodyRadiator
        gStyle.SetOptStat(self.optStat1D)
        gStyle.SetOptFit(11111)
        gStyle.SetStatH(0.12)
        gStyle.SetStatW(0.15)
        gStyle.SetPadBottomMargin(0.15)
        gStyle.SetPadTopMargin(0.15)

    def SetPath(self, path='.'):
        self.plotPath = self.RemoveExtraBackSlashes(path, 2)
        self.plotRootPath = self.RemoveExtraBackSlashes(path + '/root/', 3)
        self.plotPdfPath = self.RemoveExtraBackSlashes(path + '/pdf/', 3)
        self.settings.CheckDirExistence(self.plotPath, True)
        self.settings.CheckDirExistence(self.plotRootPath, True)
        self.settings.CheckDirExistence(self.plotPdfPath, True)

    def RemoveExtraBackSlashes(self, string, times=1):
        if times == 0:
            return string
        string = string.replace('//', '/')
        return self.RemoveExtraBackSlashes(string, times - 1)

    def UpdatePaveText(self):
        self.paveText.Clear()
        self.paveText.SetTextSize(0.025)
        self.paveTextOptions['svn'] = 'Rev: ' + self.settings.version
        self.paveTextOptions['run'] = 'Run ' + str(self.run)
        self.paveTextOptions['nEvents'] = 'with ' + str(
            self.settings.runInfo['nEvents']) + 'Events'
        self.paveTextOptions['DateTime'] = self.dateTime.AsSQLString()
        self.paveText.AddText(self.paveTextOptions['svn'])
        self.paveText.AddText(self.paveTextOptions['run'])
        self.paveText.AddText(self.paveTextOptions['nEvents'])
        self.paveText.AddText(self.paveTextOptions['DateTime'])
        self.paveText.SetBorderSize(0)
        self.paveText.SetFillColor(0)
Beispiel #2
0
def MakeOneDHist(pathToDir, distribution):

    numFittingSamples = 0

    HeaderLabel = TPaveLabel(header_x_left, header_y_bottom, header_x_right,
                             header_y_top, HeaderText, "NDC")
    HeaderLabel.SetTextAlign(32)
    HeaderLabel.SetBorderSize(0)
    HeaderLabel.SetFillColor(0)
    HeaderLabel.SetFillStyle(0)

    LumiLabel = TPaveLabel(topLeft_x_left, topLeft_y_bottom, topLeft_x_right,
                           topLeft_y_top, LumiText, "NDC")
    LumiLabel.SetBorderSize(0)
    LumiLabel.SetFillColor(0)
    LumiLabel.SetFillStyle(0)

    NormLabel = TPaveLabel()
    NormLabel.SetDrawOption("NDC")
    NormLabel.SetX1NDC(topLeft_x_left)
    NormLabel.SetX2NDC(topLeft_x_right)

    NormLabel.SetBorderSize(0)
    NormLabel.SetFillColor(0)
    NormLabel.SetFillStyle(0)

    NormText = ""
    if arguments.normalizeToUnitArea:
        NormText = "Scaled to unit area"
    elif arguments.normalizeToData:
        NormText = "MC scaled to data"
        NormLabel.SetLabel(NormText)

    YieldsLabel = TPaveText(0.39, 0.7, 0.59, 0.9, "NDC")
    YieldsLabel.SetBorderSize(0)
    YieldsLabel.SetFillColor(0)
    YieldsLabel.SetFillStyle(0)
    YieldsLabel.SetTextAlign(12)

    RatiosLabel = TPaveText()
    RatiosLabel.SetDrawOption("NDC")
    RatiosLabel.SetBorderSize(0)
    RatiosLabel.SetFillColor(0)
    RatiosLabel.SetFillStyle(0)
    RatiosLabel.SetTextAlign(32)

    Legend = TLegend()
    Legend.SetBorderSize(0)
    Legend.SetFillColor(0)
    Legend.SetFillStyle(0)

    fittingIntegral = 0
    scaleFactor = 1

    HistogramsToFit = []
    TargetDataset = distribution['target_dataset']

    FittingLegendEntries = []
    DataLegendEntries = []

    FittingHistogramDatasets = []

    Stack_list = []
    Stack_list.append(THStack("stack_before", distribution['name']))
    Stack_list.append(THStack("stack_after", distribution['name']))

    fileName = condor_dir + "/" + distribution['target_dataset'] + ".root"
    if not os.path.exists(fileName):
        return
    inputFile = TFile(fileName)
    if inputFile.IsZombie() or not inputFile.GetNkeys():
        return

    Target = inputFile.Get("OSUAnalysis/" + distribution['channel'] + "/" +
                           distribution['name']).Clone()
    Target.SetDirectory(0)
    inputFile.Close()

    Target.SetMarkerStyle(20)
    Target.SetMarkerSize(0.8)
    Target.SetFillStyle(0)
    Target.SetLineColor(colors[TargetDataset])
    Target.SetLineStyle(1)
    Target.SetLineWidth(2)
    targetIntegral = Target.Integral()
    if (arguments.normalizeToUnitArea and Target.Integral() > 0):
        Target.Scale(1. / Target.Integral())
    if arguments.rebinFactor:
        RebinFactor = int(arguments.rebinFactor)
        #don't rebin histograms which will have less than 5 bins or any gen-matching histograms
        if Target.GetNbinsX() >= RebinFactor * 5 and Target.GetName().find(
                "GenMatch") is -1:
            Target.Rebin(RebinFactor)

    ### formatting target histogram and adding to legend
    legendIndex = 0
    Legend.AddEntry(Target, labels[TargetDataset], "LEP")
    legendIndex = legendIndex + 1

    if not outputFile.Get("OSUAnalysis"):
        outputFile.mkdir("OSUAnalysis")
    if not outputFile.Get("OSUAnalysis/" + distribution['channel']):
        outputFile.Get("OSUAnalysis").mkdir(distribution['channel'])

    for sample in distribution[
            'datasets']:  # loop over different samples requested to be fit

        dataset_file = "%s/%s.root" % (condor_dir, sample)
        inputFile = TFile(dataset_file)
        HistogramObj = inputFile.Get(pathToDir + "/" +
                                     distribution['channel'] + "/" +
                                     distribution['name'])
        if not HistogramObj:
            print "WARNING:  Could not find histogram " + pathToDir + "/" + distribution[
                'channel'] + "/" + distribution[
                    'name'] + " in file " + dataset_file + ".  Will skip it and continue."
            continue
        Histogram = HistogramObj.Clone()
        Histogram.SetDirectory(0)
        inputFile.Close()
        if arguments.rebinFactor:
            RebinFactor = int(arguments.rebinFactor)
            #don't rebin histograms which will have less than 5 bins or any gen-matching histograms
            if Histogram.GetNbinsX() >= RebinFactor * 5 and Histogram.GetName(
            ).find("GenMatch") is -1:
                Histogram.Rebin(RebinFactor)

        xAxisLabel = Histogram.GetXaxis().GetTitle()
        unitBeginIndex = xAxisLabel.find("[")
        unitEndIndex = xAxisLabel.find("]")

        if unitBeginIndex is not -1 and unitEndIndex is not -1:  #x axis has a unit
            yAxisLabel = "Entries / " + str(Histogram.GetXaxis().GetBinWidth(
                1)) + " " + xAxisLabel[unitBeginIndex + 1:unitEndIndex]
        else:
            yAxisLabel = "Entries per bin (" + str(
                Histogram.GetXaxis().GetBinWidth(1)) + " width)"

        if not arguments.makeFancy:
            histoTitle = Histogram.GetTitle()
        else:
            histoTitle = ""

        legLabel = labels[sample]
        if (arguments.printYields):
            yieldHist = Histogram.Integral()
            legLabel = legLabel + " (%.1f)" % yieldHist
        FittingLegendEntries.append(legLabel)

        if (types[sample] == "bgMC"):

            numFittingSamples += 1
            fittingIntegral += Histogram.Integral()

            Histogram.SetLineStyle(1)
            if (arguments.noStack):
                Histogram.SetFillStyle(0)
                Histogram.SetLineColor(colors[sample])
                Histogram.SetLineWidth(2)
            else:
                Histogram.SetFillStyle(1001)
                Histogram.SetFillColor(colors[sample])
                Histogram.SetLineColor(1)
                Histogram.SetLineWidth(1)

        elif (types[sample] == "signalMC"):

            numFittingSamples += 1

            Histogram.SetFillStyle(0)
            Histogram.SetLineColor(colors[sample])
            Histogram.SetLineStyle(1)
            Histogram.SetLineWidth(2)
        if (arguments.normalizeToUnitArea and Histogram.Integral() > 0):
            Histogram.Scale(1. / Histogram.Integral())

        HistogramsToFit.append(Histogram)
        FittingHistogramDatasets.append(sample)

    #scaling histograms as per user's specifications
    if targetIntegral > 0 and fittingIntegral > 0:
        scaleFactor = targetIntegral / fittingIntegral
    for fittingHist in HistogramsToFit:
        if arguments.normalizeToData:
            fittingHist.Scale(scaleFactor)

        if arguments.normalizeToUnitArea and not arguments.noStack and fittingIntegral > 0:
            fittingHist.Scale(1. / fittingIntegral)
        elif arguments.normalizeToUnitArea and arguments.noStack and fittingHist.Integral(
        ) > 0:
            fittingHist.Scale(1. / fittingHist.Integral())

    def fitf(x, par):
        xBin = HistogramsToFit[0].FindBin(x[0])
        value = 0.0

        for i in range(0, len(HistogramsToFit)):
            value += par[i] * HistogramsToFit[i].GetBinContent(xBin) + par[
                i +
                len(HistogramsToFit)] * HistogramsToFit[i].GetBinError(xBin)

        return value

    lowerLimit = Target.GetBinLowEdge(1)
    upperLimit = Target.GetBinLowEdge(Target.GetNbinsX()) + Target.GetBinWidth(
        Target.GetNbinsX())
    if 'lowerLimit' in distribution:
        lowerLimit = distribution['lowerLimit']
    if 'upperLimit' in distribution:
        upperLimit = distribution['upperLimit']
    func = TF1("fit", fitf, lowerLimit, upperLimit, 2 * len(HistogramsToFit))

    for i in range(0, len(HistogramsToFit)):
        if 'fixed_datasets' in distribution and distribution['datasets'][
                i] in distribution['fixed_datasets']:
            func.FixParameter(i, 1.0)
        else:
            func.SetParameter(i, 1.0)
#            func.SetParLimits (i, 0.0, 1.0e2) # comment this out so we don't have to pre-normalize the QCD input sample
        func.SetParName(i, labels[FittingHistogramDatasets[i]])

    shiftedScaleFactors = []
    if arguments.parametricErrors:
        # loop over all input histograms and shift them +- 1 sigma
        for i in range(0, len(HistogramsToFit)):
            sfs = []

            # -1 => -1 sigma, +1 => +1 sigma
            for j in [-1, 1]:
                # loop over the parameters holding the errors for each dataset, fixing all to 0
                for k in range(len(HistogramsToFit), 2 * len(HistogramsToFit)):
                    func.FixParameter(k, 0)
                # fix the error of the dataset of interest to +-1
                func.FixParameter(i + len(HistogramsToFit), j)

                # perform new fit
                for k in range(0, distribution['iterations'] - 1):
                    if j == -1:
                        print "Scale down " + labels[FittingHistogramDatasets[
                            i]] + " iteration " + str(k + 1) + "..."
                    if j == 1:
                        print "Scale up " + labels[FittingHistogramDatasets[
                            i]] + " iteration " + str(k + 1) + "..."
                    Target.Fit("fit", "QEMR0")
                Target.Fit("fit", "VEMR0")

                # save the new scale factors for each dataset
                for k in range(0, len(HistogramsToFit)):
                    sfs.append(func.GetParameter(k))

            shiftedScaleFactors.append(sfs)

    # reset the parameters with the errors of each dataset to 0
    for i in range(len(HistogramsToFit), 2 * len(HistogramsToFit)):
        func.FixParameter(i, 0)
    # do the fit to get the central values
    for i in range(0, distribution['iterations'] - 1):
        print "Iteration " + str(i + 1) + "..."
        Target.Fit("fit", "QEMR0")
    Target.Fit("fit", "VEMR0")

    if arguments.parametricErrors:
        # make a list of the largest errors on each contribution by shifting any other contribution
        parErrors = []
        # loop over all the datasets
        for i in range(0, len(HistogramsToFit)):
            centralValue = func.GetParameter(i)
            maxError = 0
            # find the maximum deviation from the central value and save that
            for shiftedScaleFactor in shiftedScaleFactors[i]:
                currentError = abs(shiftedScaleFactor - centralValue)
                if currentError > maxError:
                    maxError = currentError

            parErrors.append(maxError)

    finalMax = 0
    if not arguments.noStack:
        for fittingHist in HistogramsToFit:
            finalMax += fittingHist.GetMaximum()
        else:
            for fittingHist in HistogramsToFit:
                if (fittingHist.GetMaximum() > finalMax):
                    finalMax = fittingHist.GetMaximum()
    if (Target.GetMaximum() > finalMax):
        finalMax = Target.GetMaximum()

    Target.SetMaximum(1.1 * finalMax)
    Target.SetMinimum(0.0001)

    Canvas = TCanvas(distribution['name'] + "_FitFunction")
    Canvas.cd(1)
    Target.Draw()
    func.Draw("same")

    outputFile.cd("OSUAnalysis/" + distribution['channel'])
    Canvas.Write()
    if arguments.savePDFs:
        if histogram == input_histograms[0]:
            Canvas.Print(pdfFileName + "(", "pdf")
        else:
            Canvas.Print(pdfFileName, "pdf")
    Target.SetStats(0)

    ### formatting bgMC histograms and adding to legend
    legendIndex = numFittingSamples - 1
    for Histogram in reversed(HistogramsToFit):
        if (arguments.noStack):
            Legend.AddEntry(Histogram, FittingLegendEntries[legendIndex], "L")
        else:
            Legend.AddEntry(Histogram, FittingLegendEntries[legendIndex], "F")
        legendIndex = legendIndex - 1

    ### Drawing histograms to canvas

    makeRatioPlots = arguments.makeRatioPlots
    makeDiffPlots = arguments.makeDiffPlots

    yAxisMin = 0.0001
    if arguments.setYMin:
        yAxisMin = float(arguments.setYMin)

    ### Draw everything to the canvases !!!!

    for i in range(0, 2):  # 0 => before, 1 => after

        integrals = []
        ratios = []
        errors = []

        if i == 1:
            # loop over each dataset, saving it's yield and the errors on it
            for j in range(0, len(HistogramsToFit)):

                integrals.append(HistogramsToFit[j].Integral())
                HistogramsToFit[j].Scale(func.GetParameter(j))
                ratios.append(func.GetParameter(j))
                errors.append(func.GetParError(j))

        for fittingHist in HistogramsToFit:
            if not arguments.noStack:
                Stack_list[i].Add(fittingHist)

        #creating the histogram to represent the statistical errors on the stack
        if not arguments.noStack:
            ErrorHisto = HistogramsToFit[0].Clone("errors")
            ErrorHisto.SetFillStyle(3001)
            ErrorHisto.SetFillColor(13)
            ErrorHisto.SetLineWidth(0)
            if i == 1:
                Legend.AddEntry(ErrorHisto, "Stat. Errors", "F")
            for Histogram in HistogramsToFit:
                if Histogram is not HistogramsToFit[0]:
                    ErrorHisto.Add(Histogram)

        if i == 0:
            Canvas = TCanvas(distribution['name'] + "_Before")
        if i == 1:
            Canvas = TCanvas(distribution['name'] + "_After")

        if makeRatioPlots or makeDiffPlots:
            Canvas.SetFillStyle(0)
            Canvas.Divide(1, 2)
            Canvas.cd(1)
            gPad.SetPad(0, 0.25, 1, 1)
            gPad.SetMargin(0.15, 0.05, 0.01, 0.07)
            gPad.SetFillStyle(0)
            gPad.Update()
            gPad.Draw()
            if arguments.setLogY:
                gPad.SetLogy()
            Canvas.cd(2)
            gPad.SetPad(0, 0, 1, 0.25)
            # format: gPad.SetMargin(l,r,b,t)
            gPad.SetMargin(0.15, 0.05, 0.4, 0.01)
            gPad.SetFillStyle(0)
            gPad.SetGridy(1)
            gPad.Update()
            gPad.Draw()

            Canvas.cd(1)

        ### finding the maximum value of anything going on the canvas, so we know how to set the y-axis
        finalMax = 0
        if numFittingSamples is not 0 and not arguments.noStack:
            finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(
                ErrorHisto.GetMaximumBin())
        else:
            for bgMCHist in HistogramsToFit:
                if (bgMCHist.GetMaximum() > finalMax):
                    finalMax = bgMCHist.GetMaximum()
        if (Target.GetMaximum() > finalMax):
            finalMax = Target.GetMaximum() + Target.GetBinError(
                Target.GetMaximumBin())
        finalMax = 1.15 * finalMax
        if arguments.setYMax:
            finalMax = float(arguments.setYMax)

        if not arguments.noStack:  # draw stacked background samples
            Stack_list[i].SetTitle(histoTitle)
            Stack_list[i].Draw("HIST")
            Stack_list[i].GetXaxis().SetTitle(xAxisLabel)
            Stack_list[i].GetYaxis().SetTitle(yAxisLabel)
            Stack_list[i].SetMaximum(finalMax)
            Stack_list[i].SetMinimum(yAxisMin)
            if makeRatioPlots or makeDiffPlots:
                Stack_list[i].GetHistogram().GetXaxis().SetLabelSize(0)
            #draw shaded error bands
            ErrorHisto.Draw("A E2 SAME")

        else:  #draw the unstacked backgrounds
            HistogramsToFit[0].SetTitle(histoTitle)
            HistogramsToFit[0].Draw("HIST")
            HistogramsToFit[0].GetXaxis().SetTitle(xAxisLabel)
            HistogramsToFit[0].GetYaxis().SetTitle(yAxisLabel)
            HistogramsToFit[0].SetMaximum(finalMax)
            HistogramsToFit[0].SetMinimum(yAxisMin)
            for bgMCHist in HistogramsToFit:
                bgMCHist.Draw("A HIST SAME")

        Target.Draw("A E X0 SAME")

        #legend coordinates, empirically determined :-)
        x_left = 0.6761745
        x_right = 0.9328859
        x_width = x_right - x_left
        y_max = 0.9
        entry_height = 0.05

        if (numFittingSamples is not 0):  #then draw the data & bgMC legend

            numExtraEntries = 2  # count the target and (lack of) title
            Legend.SetX1NDC(x_left)
            numExtraEntries = numExtraEntries + 1  # count the stat. errors entry

            Legend.SetY1NDC(y_max - entry_height *
                            (numExtraEntries + numFittingSamples))
            Legend.SetX2NDC(x_right)
            Legend.SetY2NDC(y_max)
            Legend.Draw()

            RatiosLabel.SetX1NDC(x_left - 0.1)
            RatiosLabel.SetX2NDC(x_right)
            RatiosLabel.SetY2NDC(Legend.GetY1NDC() - 0.1)
            RatiosLabel.SetY1NDC(RatiosLabel.GetY2NDC() - entry_height *
                                 (numFittingSamples))

            # Deciding which text labels to draw and drawing them
            drawLumiLabel = False
            drawNormLabel = False
            offsetNormLabel = False
            drawHeaderLabel = False

            if not arguments.normalizeToUnitArea:  #don't draw the lumi label if there's no data and it's scaled to unit area
                drawLumiLabel = True
                # move the normalization label down before drawing if we drew the lumi. label
                offsetNormLabel = True
            if arguments.normalizeToUnitArea or arguments.normalizeToData:
                drawNormLabel = True
            if arguments.makeFancy:
                drawHeaderLabel = True
                drawLumiLabel = False

            # now that flags are set, draw the appropriate labels

            if drawLumiLabel:
                LumiLabel.Draw()

            if drawNormLabel:
                if offsetNormLabel:
                    NormLabel.SetY1NDC(topLeft_y_bottom - topLeft_y_offset)
                    NormLabel.SetY2NDC(topLeft_y_top - topLeft_y_offset)
                else:
                    NormLabel.SetY1NDC(topLeft_y_bottom)
                    NormLabel.SetY2NDC(topLeft_y_top)
                NormLabel.Draw()

            if drawHeaderLabel:
                HeaderLabel.Draw()

            YieldsLabel.Clear()
            mcYield = Stack_list[i].GetStack().Last().Integral()
            dataYield = Target.Integral()
            if i == 0:
                YieldsLabel.AddText("Before Fit to Data")
            if i == 1:
                YieldsLabel.AddText("After Fit to Data")
            YieldsLabel.AddText("data yield: " + '%.1f' % dataYield)
            YieldsLabel.AddText("bkgd yield: " + '%.1f' % mcYield)
            YieldsLabel.AddText("data/bkgd: " + '%.2f' % (dataYield / mcYield))
            if i == 1:
                for j in range(0, len(FittingLegendEntries)):
                    if abs(ratios[j] - 1) < 0.001 and abs(
                            errors[j]
                    ) < 0.001:  #then it probably was held fixed
                        continue
                    if arguments.showFittedYields:
                        yield_ = ratios[j] * integrals[j]
                        yielderror_ = errors[j] * yield_
                        text = FittingLegendEntries[
                            j] + " yield: " + '%.0f' % yield_ + ' #pm %.0f' % yielderror_
                    else:
                        text = FittingLegendEntries[
                            j] + " ratio: " + '%.2f' % ratios[
                                j] + ' #pm %.2f' % errors[j]
                    text = text + " (fit)"
                    if arguments.parametricErrors:
                        yield_ = ratios[j] * integrals[j]
                        yieldParError_ = parErrors[j] * yield_
                        if arguments.showFittedYields:
                            text += ' #pm %.2f' % yieldParError_
                        else:
                            text += ' #pm %.2f' % parErrors[j]
                        text = text + " (sys)"
                    RatiosLabel.AddText(text)
            YieldsLabel.Draw()
            RatiosLabel.Draw()

        # drawing the ratio or difference plot if requested
        if (makeRatioPlots or makeDiffPlots):
            Canvas.cd(2)
            BgSum = Stack_list[i].GetStack().Last()
            if makeRatioPlots:
                if arguments.ratioRelErrMax:
                    Comparison = ratioHistogram(Target, BgSum,
                                                arguments.ratioRelErrMax)
                else:
                    Comparison = ratioHistogram(Target, BgSum)
            elif makeDiffPlots:
                Comparison = Target.Clone("diff")
                Comparison.Add(BgSum, -1)
                Comparison.SetTitle("")
                Comparison.GetYaxis().SetTitle("Data-Bkgd")
            Comparison.GetXaxis().SetTitle(xAxisLabel)
            Comparison.GetYaxis().CenterTitle()
            Comparison.GetYaxis().SetTitleSize(0.1)
            Comparison.GetYaxis().SetTitleOffset(0.5)
            Comparison.GetXaxis().SetTitleSize(0.15)
            Comparison.GetYaxis().SetLabelSize(0.1)
            Comparison.GetXaxis().SetLabelSize(0.15)
            if makeRatioPlots:
                RatioYRange = 1.15
                if arguments.ratioYRange:
                    RatioYRange = float(arguments.ratioYRange)
                Comparison.GetYaxis().SetRangeUser(-1 * RatioYRange,
                                                   RatioYRange)
            elif makeDiffPlots:
                YMax = Comparison.GetMaximum()
                YMin = Comparison.GetMinimum()
                if YMax <= 0 and YMin <= 0:
                    Comparison.GetYaxis().SetRangeUser(-1.2 * YMin, 0)
                elif YMax >= 0 and YMin >= 0:
                    Comparison.GetYaxis().SetRangeUser(0, 1.2 * YMax)
                else:  #axis crosses y=0
                    if abs(YMax) > abs(YMin):
                        Comparison.GetYaxis().SetRangeUser(
                            -1.2 * YMax, 1.2 * YMax)
                    else:
                        Comparison.GetYaxis().SetRangeUser(
                            -1.2 * YMin, 1.2 * YMin)

            Comparison.GetYaxis().SetNdivisions(205)
            Comparison.Draw("E0")

        if i == 0:
            Canvas.Write(distribution['name'] + "_Before")
            if arguments.savePDFs:
                pathToDirString = plainTextString(pathToDir)
                Canvas.SaveAs(condor_dir + "/fitting_histogram_pdfs/" +
                              pathToDirString + "/" + distribution['name'] +
                              "_Before.pdf")

        if i == 1:
            Canvas.Write(distribution['name'] + "_After")
            if arguments.savePDFs:
                pathToDirString = plainTextString(pathToDir)
                Canvas.SaveAs(condor_dir + "/fitting_histogram_pdfs/" +
                              pathToDirString + "/" + distribution['name'] +
                              "_After.pdf")
Beispiel #3
0
def main():

    cc.cd()
    cc.SetBorderMode(0)
    cc.SetFixedAspectRatio(1)
    cc.FeedbackMode(1)

    gStyle.SetOptStat(0)
    gStyle.SetGridStyle(1)
    gStyle.SetGridColor(11)

    hh=TH2D('hh',';X;Y',22,-10.5,11.5,22,-10.5,11.5)
    hi=TH2I('hi',';X;Y',22,-10.5,11.5,22,-10.5,11.5)
    setupHists([hh,hi])
    xax,yax=hh.GetXaxis(),hh.GetYaxis()
    hh.Draw('COLZ')
    hi.Draw('TEXTSAME')

    gPad.SetLogz()
    gPad.SetGrid(1,1)
    gPad.SetLeftMargin(0.09)
    gPad.SetRightMargin(0.11)

    #tt2=TPaveText(0.7,0.96,0.9,0.99,'NDC')
    ttM=TPaveText(-3+0.05, 7-4.45, 4.0, 8-4.51)
    tt2=TPaveText(-3+0.05, 7-5.45, 4.0, 8-5.51)

    ttX=TPaveText(-2, 7-8.00, 3, 8-8.00)
    ttY=TPaveText(-2, 7-9.00, 3, 8-9.00)
    ttZ=TPaveText(-2, 6-8.80, 3, 8-9.30)
    ttZ.AddText("positive = beam top/right")

    ttime=TPaveText(-10,-12.5,10,-11.8)
    tchan=TPaveText(0,0,0.9,1)
    setupPaveTexts([tt2,ttM,ttime,tchan,ttX,ttY,ttZ])
    ttM.SetTextColor(2)
    ttM.SetFillStyle(0)
    ttZ.SetFillStyle(0)
    tt2.SetFillStyle(0)

    tarrow=TText(-0.9,0.7,'Beam Right')
    tarrow.SetTextSizePixels(15)
    arrow=TArrow(-1.4,0.5,2.4,0.5,0.02,'|>')
    arrow.SetAngle(40)
    arrow.SetFillColor(1)
    arrow.SetLineWidth(2)

    tt=TText()
    tt.SetTextColor(1)
    tt.SetTextAngle(90)
    tt.SetTextSize(0.04)
    tt.DrawText(12.4,0,'kHz')
    tt.SetTextAngle(0)
    tt.SetTextColor(1)
    tt.DrawTextNDC(0.3,0.92,'FTC FADC SCALERS')

    bb=TBox()
    bb.SetFillStyle(1001)
    bb.SetFillColor(0)
    bb.SetLineWidth(1)
    bb.SetLineColor(1)
    bb.DrawBox(-3.47,-1.47,4.47,2.46)
    bb.DrawBox(-1.47,-3.47,2.49,4.47)
    bb.DrawBox(-2.47,-2.47,3.49,3.47)

    cc.cd()
    for xx in [ttM,tt2,ttime,arrow,tarrow,ttX,ttY,ttZ]: xx.Draw()
    cc2.cd()
    tchan.Draw('NDC')
    cc.cd()

    gPad.SetEditable(0)

    while True:

            for ch in ECAL.chans:
              loadPV(ch)
              ch=ch.vals
              xx,yy=ch['X'],ch['Y']
              #if (ch['PVVAL']>10):
               # print xx,yy,ch['PVVAL']

              # swap x to make it downstream view:
              xx=-xx

              #after, fix the fact x=0 / y=0 don't exists
              if xx<0: xx+=1
              if yy<0: yy+=1
              hh.SetBinContent(xax.FindBin(xx),yax.FindBin(yy),ch['PVVAL'])
              hi.SetBinContent(xax.FindBin(xx),yax.FindBin(yy),ch['PVVAL'])

            for xx in [ttime,tt2,ttM,ttX,ttY]: xx.Clear()
            [total,maximum,top,bottom,left,right]=calcRates(ECAL.chans)

            tt2.AddText('Total:  %.1f MHz'%(total/1000))
            ttM.AddText('Max:  %.0f kHz'%(maximum))

            if total>1e2:
              xasy = (right-left)/total
              yasy = (top-bottom)/total
              ttX.AddText('X-Asy:  %+.1f%%'%(100*xasy))
              ttY.AddText('Y-Asy:  %+.1f%%'%(100*yasy))
            else:
              ttX.AddText('X-Asy:  N/A')
              ttY.AddText('Y-Asy:  N/A')

            ttime.AddText(makeTime())

            if not gPad: sys.exit()

            if gPad.GetEvent()==11:
                xy=pix2xy(gPad)
                ee=ECAL.findChannelXY(xy[0],xy[1])
                if ee:
                    tchan.Clear()
                    tchan.AddText(printChannel(ee))
                    cc2.Modified()
                    cc2.Update()
            elif gPad.GetEvent()==12:
                tchan.Clear()
                cc2.Modified()
                cc2.Update()


            cc.Modified()
            cc.Update()

            time.sleep(2)
def main():

    if True:
        mf=TGMainFrame(gClient.GetRoot(),1500,475)
        gvf=TGVerticalFrame(mf,1500,475)
        rec=TRootEmbeddedCanvas("ccc",gvf,1500,450)
        rec2=TRootEmbeddedCanvas("ccc2",gvf,1500,25)
        gvf.AddFrame(rec,TGLayoutHints(ROOT.kLHintsExpandX|ROOT.kLHintsTop))
        gvf.AddFrame(rec2,TGLayoutHints(ROOT.kLHintsExpandX|ROOT.kLHintsBottom))
        mf.AddFrame(gvf,TGLayoutHints(ROOT.kLHintsExpandX))
        cc=rec.GetCanvas()
        cc2=rec2.GetCanvas()
        mf.SetEditable(0)
        mf.SetWindowName('HPS ECAL FADC SCALERS')
        mf.MapSubwindows()
        mf.Resize(1501,476)# resize to get proper frame placement
        mf.MapWindow()
    else:
        cc=TCanvas('cc','',1500,450)
   
    cc.cd()
    cc.SetBorderMode(0)
    cc.SetFixedAspectRatio(1)
    cc.FeedbackMode(1)
    
    gStyle.SetOptStat(0)
    gStyle.SetGridStyle(1)
    gStyle.SetGridColor(11)

    hh=TH2D('hh',';X;Y',46,-22,24,11,-5,6)
    hi=TH2I('hi',';X;Y',46,-22,24,11,-5,6)
    setupHists([hh,hi])
    xax,yax=hh.GetXaxis(),hh.GetYaxis()
    hh.Draw('COLZ')
    hi.Draw('TEXTSAME')
    
    gPad.SetLogz()
    gPad.SetGrid(1,1)
    gPad.SetLeftMargin(0.05)
    
    tt1=TPaveText(0.1,0.9,0.3,1.0,'NDC')
    tt2=TPaveText(0.7,0.91,0.9,0.99,'NDC')
    ttT=TPaveText(-22+13+0.05,6-5,-22+22,7-5-0.05)
    ttB=TPaveText(-22+13+0.05,4-5+0.05,-22+22,5-5)
    ttM=TPaveText(-22+0+0.05,5-5+0.05,-22+13,6-5.01)
    ttime=TPaveText(-10,-6.5,10,-5.8)
    tchan=TPaveText(0,0,0.9,1)
    setupPaveTexts([tt1,tt2,ttT,ttB,ttM,ttime,tchan])
    ttM.SetTextColor(2)
    
    bb=TBox()
    bb.SetFillStyle(1001)
    bb.SetFillColor(0)
    bb.SetLineWidth(1)
    bb.SetLineColor(1)
    bb.DrawBox(-9+0.05,-1,0,1.97)
    bb.DrawBox(-24,0,24.05,0.97)
    
    tarrow=TText(14.5,0.3,'Beam Left')
    arrow=TArrow(19,0.5,23,0.5,0.02,'|>')
    arrow.SetAngle(40)
    arrow.SetFillColor(1)
    arrow.SetLineWidth(2)
    
    tt=TText()
    tt.SetTextColor(1)
    tt.SetTextAngle(90)
    tt.SetTextSize(0.08)
    tt.DrawText(25.4,0,'kHz')
    tt.SetTextAngle(0)
    tt.SetTextColor(2)
    tt.DrawTextNDC(0.3,0.92,'ECAL FADC SCALERS')
   
    cc.cd()
    for xx in [tt2,ttT,ttB,ttM,arrow,tarrow,ttime]: xx.Draw()
    cc2.cd()
    tchan.Draw('NDC')
    cc.cd()
    
    ll=TLine()
    ll.DrawLine(xax.GetXmin(),yax.GetXmin(),xax.GetXmax(),yax.GetXmin())
    ll.DrawLine(xax.GetXmin(),yax.GetXmax(),xax.GetXmax(),yax.GetXmax())
    ll.DrawLine(xax.GetXmin(),yax.GetXmin(),xax.GetXmin(),0)
    ll.DrawLine(xax.GetXmax(),yax.GetXmin(),xax.GetXmax(),0)
    ll.DrawLine(xax.GetXmin(),yax.GetXmax(),xax.GetXmin(),1)
    ll.DrawLine(xax.GetXmax(),yax.GetXmax(),xax.GetXmax(),1)
    ll.DrawLine(xax.GetXmax(),0,0,0)
    ll.DrawLine(xax.GetXmax(),1,0,1)
    ll.DrawLine(xax.GetXmin(),0,-9,0)
    ll.DrawLine(xax.GetXmin(),1,-9,1)
    ll.DrawLine(-9,-1,0,-1)
    ll.DrawLine(-9,2,0,2)
    ll.DrawLine(-9,1,-9,2)
    ll.DrawLine(-9,-1,-9,0)
    ll.DrawLine(0,-1,0,0)
    ll.DrawLine(0,1,0,2)
   
    gPad.SetEditable(0)

    while True:

#        try:

            zvals=getPVS()
            for ii in range(len(zvals)):
                hh.SetBinContent(xax.FindBin(XVALS[ii]),yax.FindBin(YVALS[ii]),zvals[ii])
                hi.SetBinContent(xax.FindBin(XVALS[ii]),yax.FindBin(YVALS[ii]),zvals[ii])
            
            for xx in [ttime,tt2,ttT,ttB,ttM]: xx.Clear()
            [top,bottom,maximum]=calcRates(zvals)
            tt2.AddText('Total:  %.1f MHz'%((top+bottom)/1000))
            ttT.AddText('%.1f MHz'%(top/1000))
            ttB.AddText('%.1f MHz'%(bottom/1000))
            ttM.AddText('MAX SINGLE CRYSTAL = %.0f kHz'%(maximum))
            ttime.AddText(makeTime())
          
            if gPad.GetEvent()==11:
                xy=pix2xy(gPad)
                ee=ECAL.findChannelXY(xy[0],xy[1])
                if ee:
                    tchan.Clear()
                    tchan.AddText(printChannel(ee))
                    cc2.Modified()
                    cc2.Update()
            elif gPad.GetEvent()==12:
                tchan.Clear()
                cc2.Modified()
                cc2.Update()
    
    
            cc.Modified()
            cc.Update()
        
            time.sleep(1)
Beispiel #5
0
def main():

    cc.cd()
    cc.SetBorderMode(0)
    cc.SetFixedAspectRatio(1)
    cc.FeedbackMode(1)

    gStyle.SetOptStat(0)
    gStyle.SetGridStyle(1)
    gStyle.SetGridColor(18)

    hh = TH2D('hh', ';X;Y', 8 * 28, 0, 28, 8 * 23, 0, 23)
    hi = TH2I('hi', ';X;Y', 8 * 28, 0, 28, 8 * 23, 0, 23)
    setupHists([hh, hi])
    xax, yax = hh.GetXaxis(), hh.GetYaxis()
    hh.Draw('COLZ')
    hh.SetMinimum(1)
    hh.SetMaximum(1.5e3)
    #hi.Draw('TEXTSAME')

    gPad.SetLogz()
    gPad.SetGrid(1, 1)
    gPad.SetLeftMargin(0.09)
    gPad.SetRightMargin(0.11)

    #tt2=TPaveText(0.7,0.96,0.9,0.99,'NDC')
    ttM = TPaveText(-3 + 0.05, 7 - 4.45, 4.0, 8 - 4.51)
    tt2 = TPaveText(-3 + 0.05, 7 - 5.45, 4.0, 8 - 5.51)

    ttX = TPaveText(-2, 7 - 8.00, 3, 8 - 8.00)
    ttY = TPaveText(-2, 7 - 9.00, 3, 8 - 9.00)

    ttime = TPaveText(8, -2, 20, -1)
    tchan = TPaveText(0, 0, 0.9, 1)
    setupPaveTexts([tt2, ttM, ttime, tchan, ttX, ttY])
    ttM.SetTextColor(2)
    ttM.SetFillStyle(0)
    tt2.SetFillStyle(0)

    tt = TText()
    tt.SetTextColor(1)
    tt.SetTextAngle(90)
    tt.SetTextSize(0.04)
    tt.DrawText(29.5, 10, 'kHz')
    tt.SetTextAngle(0)
    tt.SetTextColor(1)
    tt.DrawTextNDC(0.3, 0.92, 'RICH SSP Scalers')

    #    bb=TBox()
    #    bb.SetFillStyle(1001)
    #    bb.SetFillColor(0)
    #    bb.SetLineWidth(1)
    #    bb.SetLineColor(1)
    #    bb.DrawBox(-3.47,-1.47,4.47,2.46)
    #    bb.DrawBox(-1.47,-3.47,2.49,4.47)
    #    bb.DrawBox(-2.47,-2.47,3.49,3.47)

    cc.cd()
    for xx in [ttime]:
        xx.Draw()  #ttM,tt2,ttime,ttX,ttY]: xx.Draw()
    cc2.cd()
    tchan.Draw('NDC')
    cc.cd()

    gPad.SetEditable(0)

    while True:

        for ch in ECAL.chans:
            loadPV(ch)
            ch = ch.vals
            xx, yy = ch['X'], ch['Y']
            data = ch['PVVAL']

            for ix in range(8):
                for iy in range(8):
                    ii = ix * 8 + iy
                    #print ch['PVNAME'],data[ii]
                    xoff = float(ix) / 8
                    yoff = float(iy) / 8

                    hh.SetBinContent(xax.FindBin(xx + xoff),
                                     yax.FindBin(yy + yoff), data[ii])


#                  hi.SetBinContent(xax.FindBin(xx+xoff),yax.FindBin(yy+yoff),data[ii])
#
        for xx in [ttime]:
            ttime.Clear()  # xx.,tt2,ttM,ttX,ttY]: xx.Clear()
        #            [total,maximum,top,bottom,left,right]=calcRates(ECAL.chans)

        #            tt2.AddText('Total:  %.1f MHz'%(total/1000))
        #            ttM.AddText('Max:  %.0f kHz'%(maximum))

        #            if total>1e2:
        #              xasy = (right-left)/total
        #              yasy = (top-bottom)/total
        #              ttX.AddText('X-Asy:  %+.1f%%'%(100*xasy))
        #              ttY.AddText('Y-Asy:  %+.1f%%'%(100*yasy))
        #            else:
        #              ttX.AddText('X-Asy:  N/A')
        #              ttY.AddText('Y-Asy:  N/A')

        ttime.AddText(makeTime())

        if not gPad: sys.exit()

        #            if gPad.GetEvent()==11:
        #                xy=pix2xy(gPad)
        #                ee=ECAL.findChannelXY(xy[0],xy[1])
        #                if ee:
        #                    tchan.Clear()
        #                    tchan.AddText(printChannel(ee))
        #                    cc2.Modified()
        #                    cc2.Update()
        #            elif gPad.GetEvent()==12:
        #                tchan.Clear()
        #                cc2.Modified()
        #                cc2.Update()

        cc.Modified()
        cc.Update()

        time.sleep(1)