def plot(config):
    logger = logging.getLogger("mpsvalidate")

    # adjust the plot style
    # show the skewness in the legend
    gStyle.SetOptStat("emrs")
    gStyle.SetPadLeftMargin(0.07)

    # loop over all millepedemonitor_X.root files
    for filename in os.listdir("{0}".format(config.jobDataPath)):
        if (filename.endswith(".root")
                and filename.startswith("millepedemonitor_")):
            # get X out of millepedemonitor_X.root files
            inputname = filename[17:-5]

            # open file
            rootfile = TFile("{0}/{1}".format(config.jobDataPath, filename))

            plotPaths = [
                "usedTrackHists/usedptTrack", "usedTrackHists/usedetaTrack",
                "usedTrackHists/usedphiTrack", "usedTrackHists/usednHitTrack"
            ]

            # loop over plots which should be plotted
            for plotNumber, plotPath in enumerate(plotPaths):
                # get plotname
                plotName = plotPath.split("/")[1]
                # get plot
                plot = rootfile.Get(plotPath)

                if (plotNumber == 0):
                    # get number of used tracks
                    ntracks = int(plot.GetEntries())
                    MonitorData(inputname.replace("_", " "), ntracks)

                # create canvas
                canvas = TCanvas("canvas{0}_{1}".format(inputname, plotName),
                                 "Monitor", 300, 0, 800, 600)
                canvas.cd()

                # set statistics size
                setstatsize(canvas, plot, config)

                # draw
                plot.Draw()

                # save as pdf
                canvas.Print("{0}/plots/pdf/monitor_{1}_{2}.pdf".format(
                    config.outputPath, inputname.replace(".", "_"), plotName))

                # export as png
                image = TImage.Create()
                image.FromPad(canvas)
                image.WriteImage("{0}/plots/png/monitor_{1}_{2}.png".format(
                    config.outputPath, inputname.replace(".", "_"), plotName))

                # add to output list
                output = OutputData(plottype="monitor",
                                    name=inputname.replace("_", " "),
                                    number=plotName,
                                    filename="monitor_{1}_{2}".format(
                                        config.outputPath,
                                        inputname.replace(".", "_"), plotName))
                config.outputList.append(output)

    # reset the plot style
    gStyle.SetOptStat(0)
    gStyle.SetPadLeftMargin(0.17)
Exemple #2
0
def plot(MillePedeUser, alignables, mode, struct, parentPlot, config):
    logger = logging.getLogger("mpsvalidate")

    # skip empty
    number = 0
    for i in range(3):
        if (parentPlot.histo[i].GetEntries() == 0):
            number += 1
    if (number == 3):
        return

    # number of bins to start
    numberOfBins = 10000

    ######################################################################
    # initialize data hierarchy
    # plots[subStrucut]
    #

    plots = []

    # initialize histograms
    for subStructNumber, subStruct in enumerate(struct.get_children()):
        plots.append(PlotData(mode))

        # use a copy for shorter name
        plot = plots[subStructNumber]

        for i in range(3):
            if (mode == "xyz"):
                plot.histo.append(
                    TH1F(
                        "{0} {1} {2}".format(
                            struct.get_name() + " " + subStruct.get_name(),
                            plot.xyz[i], mode),
                        "Parameter {0}".format(plot.xyz[i]), numberOfBins,
                        -1000, 1000))
            else:
                plot.histo.append(
                    TH1F(
                        "{0} {1} {2}".format(
                            struct.get_name() + " " + subStruct.get_name(),
                            plot.xyz[i], mode),
                        "Parameter {0}".format(plot.xyz[i]), numberOfBins,
                        -0.1, 0.1))

            plot.histo[i].SetXTitle(plot.unit)
            plot.histo[i].SetLineColor(6)
            plot.histo[i].SetStats(0)

        # add labels
        plot.title = TPaveLabel(
            0.1, 0.8, 0.9, 0.9,
            "Module: {0} {1}".format(struct.get_name(), mode))
        plot.text = TPaveText(0.05, 0.1, 0.95, 0.75)
        plot.text.SetTextAlign(12)
        plot.text.SetTextSizePixels(20)

        # save copy
        plots[subStructNumber] = plot

    ######################################################################
    # fill histogram
    #

    for line in MillePedeUser:
        # is module ?
        if (line.ObjId == 1):
            for subStructNumber, subStruct in enumerate(struct.get_children()):
                # use a copy for shorter name
                plot = plots[subStructNumber]

                # module in struct ?
                if (subStruct.contains_detid(line.Id)):
                    for i in range(3):
                        if (abs(line.Par[plot.data[i]]) != 999999):
                            # transform xyz data from cm to #mu m
                            if (mode == "xyz"):
                                plot.histo[i].Fill(10000 *
                                                   line.Par[plot.data[i]])
                            else:
                                plot.histo[i].Fill(line.Par[plot.data[i]])

                # save copy
                plots[subStructNumber] = plot

    ######################################################################
    # find the best range
    #
    for subStructNumber, subStruct in enumerate(struct.get_children()):
        # use a copy for shorter name
        plot = plots[subStructNumber]
        for i in range(3):
            if (plot.histo[i].GetEntries() != 0
                    and plot.histo[i].GetStdDev() != 0):
                # use binShift of the hole structure
                binShift = parentPlot.usedRange[i]

                # count entries which are not shown anymore
                # bin 1 to begin of histogram
                for j in range(1, numberOfBins / 2 - binShift):
                    plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
                # from the end of shown bins to the end of histogram
                for j in range(numberOfBins / 2 + binShift,
                               plot.histo[i].GetNbinsX()):
                    plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)

                # merge bins, ca. 100 should be visible in the resulting plot
                mergeNumberBins = binShift
                # skip empty histogram
                if (mergeNumberBins != 0):
                    # the 2*maxBinShift bins should shrink to 100 bins
                    mergeNumberBins = int(2. * mergeNumberBins /
                                          config.numberofbins)
                    # the total number of bins should be dividable by the bins
                    # shrinked together
                    if (mergeNumberBins == 0):
                        mergeNumberBins = 1
                    while (numberOfBins % mergeNumberBins != 0
                           and mergeNumberBins != 1):
                        mergeNumberBins -= 1

                    # Rebin and save new created histogram and axis
                    plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)

                    # set view range. it is important to note that the number of bins have changed with the rebinning
                    # the total number and the number of shift must be
                    # corrected with / mergeNumberBins
                    plot.histo[i].GetXaxis().SetRange(
                        int(numberOfBins / (2 * mergeNumberBins) -
                            binShift / mergeNumberBins),
                        int(numberOfBins / (2 * mergeNumberBins) +
                            binShift / mergeNumberBins))

        # save copy
        plots[subStructNumber] = plot

    ######################################################################
    # make the plots
    #

    canvas = TCanvas("SubStruct_{0}_{1}".format(struct.get_name(), mode),
                     "Parameter", 300, 0, 800, 600)
    canvas.Divide(2, 2)

    canvas.cd(1)
    parentPlot.title.Draw()

    legend = TLegend(0.05, 0.1, 0.95, 0.75)

    for i in range(3):
        canvas.cd(i + 2)

        # find y maximum
        maximum = []

        if (parentPlot.histo[i].GetEntries() == 0):
            continue

        # normalize parent
        parentPlot.histo[i].Scale(1. / parentPlot.histo[i].Integral())
        maximum.append(parentPlot.histo[i].GetMaximum())

        for subStructNumber, subStruct in enumerate(struct.get_children()):
            # use a copy for shorter name
            plot = plots[subStructNumber]

            if (plot.histo[i].GetEntries() > 0):
                plot.histo[i].Scale(1. / plot.histo[i].Integral())
                maximum.append(plot.histo[i].GetMaximum())

            # save copy
            plots[subStructNumber] = plot

        # set range and plot
        parentPlot.histo[i].GetYaxis().SetRangeUser(0., 1.1 * max(maximum))
        parentPlot.histo[i].Draw()

        for subStructNumber, subStruct in enumerate(struct.get_children()):
            # use a copy for shorter name
            plot = plots[subStructNumber].histo[i]

            plot.SetLineColorAlpha(subStructNumber + 2, 0.5)
            plot.Draw("same")
            if (i == 0):
                legend.AddEntry(plot, subStruct.get_name(), "l")

    canvas.cd(1)

    legend.Draw()
    # draw identification
    ident = identification(config)
    ident.Draw()

    canvas.Update()

    # save as pdf
    canvas.Print("{0}/plots/pdf/subModules_{1}_{2}.pdf".format(
        config.outputPath, mode, struct.get_name()))

    # export as png
    image = TImage.Create()
    image.FromPad(canvas)
    image.WriteImage("{0}/plots/png/subModules_{1}_{2}.png".format(
        config.outputPath, mode, struct.get_name()))

    # add to output list
    output = OutputData(plottype="subMod",
                        name=struct.get_name(),
                        number=subStructNumber + 1,
                        parameter=mode,
                        filename="subModules_{0}_{1}".format(
                            mode, struct.get_name()))
    config.outputList.append(output)
Exemple #3
0
def plot(MillePedeUser, alignables, config):
    logger = logging.getLogger("mpsvalidate")

    alignables.create_list(MillePedeUser)

    # number of bins to start
    numberOfBins = 10000

    ######################################################################
    # initialize data hierarchy
    # plots[mode][struct]
    #

    plots = []
    # loop over mode
    for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
        plots.append([])
        # loop over structures
        for structNumber, struct in enumerate(alignables.structures):
            plots[modeNumber].append(PlotData(mode))

    # initialize histograms
    for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
        for structNumber, struct in enumerate(alignables.structures):
            # use a copy for shorter name
            plot = plots[modeNumber][structNumber]

            for i in range(3):
                if (mode == "xyz"):
                    plot.histo.append(
                        TH1F(
                            "{0} {1} {2}".format(struct.get_name(),
                                                 plot.xyz[i], mode),
                            "Parameter {0}".format(plot.xyz[i]), numberOfBins,
                            -1000, 1000))
                else:
                    plot.histo.append(
                        TH1F(
                            "{0} {1} {2}".format(struct.get_name(),
                                                 plot.xyz[i], mode),
                            "Parameter {0}".format(plot.xyz[i]), numberOfBins,
                            -0.1, 0.1))

                plot.histo[i].SetXTitle(plot.unit)
                plot.histo[i].GetXaxis().SetTitleOffset(0.85)
                plot.histoAxis.append(plot.histo[i].GetXaxis())

            # add labels
            plot.title = TPaveLabel(
                0.1, 0.8, 0.9, 0.9,
                "Module: {0} {1}".format(struct.get_name(), mode))
            plot.text = TPaveText(0.05, 0.1, 0.95, 0.75)
            plot.text.SetTextAlign(12)
            plot.text.SetTextSizePixels(20)

            # save copy
            plots[modeNumber][structNumber] = plot

    ######################################################################
    # fill histogram
    #

    for line in MillePedeUser:
        # is module ?
        if (line.ObjId == 1):
            for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
                for structNumber, struct in enumerate(alignables.structures):
                    # use a copy for shorter name
                    plot = plots[modeNumber][structNumber]

                    # module in struct ?
                    if (struct.contains_detid(line.Id)):
                        for i in range(3):
                            if (abs(line.Par[plot.data[i]]) != 999999):
                                # transform xyz data from cm to #mu m
                                if (mode == "xyz"):
                                    plot.histo[i].Fill(10000 *
                                                       line.Par[plot.data[i]])
                                else:
                                    plot.histo[i].Fill(line.Par[plot.data[i]])

                    # save copy
                    plots[modeNumber][structNumber] = plot

    ######################################################################
    # find the best range
    #

    for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
        for structNumber, struct in enumerate(alignables.structures):
            # use a copy for shorter name
            plot = plots[modeNumber][structNumber]

            for i in range(3):
                # get first and last bin with content and chose the one which
                # has a greater distance to the center
                if (abs(numberOfBins / 2 - plot.histo[i].FindFirstBinAbove()) >
                        abs(plot.histo[i].FindLastBinAbove() -
                            numberOfBins / 2)):
                    plot.maxBinShift[i] = abs(
                        numberOfBins / 2 - plot.histo[i].FindFirstBinAbove())
                    # set the maxShift value
                    plot.maxShift[i] = plot.histo[i].GetBinCenter(
                        plot.histo[i].FindFirstBinAbove())
                else:
                    plot.maxBinShift[i] = abs(
                        plot.histo[i].FindLastBinAbove() - numberOfBins / 2)
                    # set the maxShift value
                    plot.maxShift[i] = plot.histo[i].GetBinCenter(
                        plot.histo[i].FindLastBinAbove())
                # skip empty histogram
                if (abs(plot.maxBinShift[i]) == numberOfBins / 2 + 1):
                    plot.maxBinShift[i] = 0

            # three types of ranges

            # 1. multiple of standard dev
            if (config.rangemode == "stddev"):
                for i in range(3):
                    if (plot.histo[i].GetEntries() != 0
                            and plot.histo[i].GetStdDev() != 0):
                        # if the plotrange is much bigger than the standard
                        # deviation use config.widthstdev * StdDev als Range
                        if (max(plot.maxShift) / plot.histo[i].GetStdDev() >
                                config.defpeak):
                            # corresponding bin config.widthstdev*StdDev
                            binShift = int(plot.histo[i].FindBin(
                                config.widthstddev *
                                plot.histo[i].GetStdDev()) - numberOfBins / 2)
                        else:
                            binShift = max(plot.maxBinShift)

                        # save used binShift
                        plot.binShift[i] = binShift

            # 2. show all
            if (config.rangemode == "all"):
                for i in range(3):
                    plot.binShift[i] = plot.maxBinShift[i]

            # 3. use given ranges
            if (config.rangemode == "given"):
                for i in range(3):
                    if (mode == "xyz"):
                        valuelist = config.rangexyzM
                    if (mode == "rot"):
                        valuelist = config.rangerotM
                    if (mode == "dist"):
                        valuelist = config.rangedistM

                    for value in valuelist:
                        # maximum smaller than given value
                        if (abs(plot.maxShift[i]) < value):
                            binShift = value
                            break
                    # if not possible, force highest
                    if (abs(plot.maxShift[i]) > valuelist[-1]):
                        binShift = valuelist[-1]
                    # calculate binShift
                    plot.binShift[i] = int(binShift /
                                           plot.histo[i].GetBinWidth(1))

            # all plot the same range
            if (config.samerange == 1):
                for i in range(3):
                    plot.binShift[i] = max(plot.binShift)

            # save used range
            for i in range(3):
                plot.usedRange[i] = plot.binShift[i]

            # count entries which are not shown anymore
            for i in range(3):
                # bin 1 to begin of histogram
                for j in range(1, numberOfBins / 2 - plot.binShift[i]):
                    plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
                # from the end of shown bins to the end of histogram
                for j in range(numberOfBins / 2 + plot.binShift[i],
                               plot.histo[i].GetNbinsX()):
                    plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)

            # apply new range
            for i in range(3):
                if (plot.histo[i].GetEntries() != 0):
                    # merge bins, ca. 100 should be visible in the resulting
                    # plot
                    mergeNumberBins = plot.binShift[i]
                    # skip empty histogram
                    if (mergeNumberBins != 0):
                        # the 2*maxBinShift bins should shrink to 100 bins
                        mergeNumberBins = int(2. * mergeNumberBins /
                                              config.numberofbins)
                        # the total number of bins should be dividable by the
                        # bins shrinked together
                        if (mergeNumberBins == 0):
                            mergeNumberBins = 1
                        while (numberOfBins % mergeNumberBins != 0
                               and mergeNumberBins != 1):
                            mergeNumberBins -= 1

                        # Rebin and save new created histogram and axis
                        plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)
                        plot.histoAxis[i] = plot.histo[i].GetXaxis()

                        # set view range. it is important to note that the number of bins have changed with the rebinning
                        # the total number and the number of shift must be
                        # corrected with / mergeNumberBins
                        plot.histoAxis[i].SetRange(
                            int(numberOfBins / (2 * mergeNumberBins) -
                                plot.binShift[i] / mergeNumberBins),
                            int(numberOfBins / (2 * mergeNumberBins) +
                                plot.binShift[i] / mergeNumberBins))

            # error if shift is bigger than limit
            limit = config.limit[mode]
            for i in range(3):
                # skip empty
                if (plot.histo[i].GetEntries() > 0):
                    plot.text.AddText("max. shift {0}: {1:.2}".format(
                        plot.xyz[i], plot.maxShift[i]))
                    if (abs(plot.maxShift[i]) > limit):
                        plot.text.AddText(
                            "! {0} shift bigger than {1} !".format(
                                plot.xyz[i], limit))
                    if (plot.hiddenEntries[i] != 0):
                        plot.text.AddText("! {0} {1} outlier !".format(
                            plot.xyz[i], int(plot.hiddenEntries[i])))

            # save copy
            plots[modeNumber][structNumber] = plot

    ######################################################################
    # make the plots
    #

    # show the skewness in the legend
    gStyle.SetOptStat("emrs")

    for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
        for structNumber, struct in enumerate(alignables.structures):
            # use a copy for shorter name
            plot = plots[modeNumber][structNumber]

            canvas = TCanvas(
                "canvasModules{0}_{1}".format(struct.get_name(), mode),
                "Parameter", 300, 0, 800, 600)
            canvas.Divide(2, 2)

            canvas.cd(1)
            plot.title.Draw()
            plot.text.Draw()

            # draw identification
            ident = identification(config)
            ident.Draw()

            # is there any plot?
            plotNumber = 0

            # loop over coordinates
            for i in range(3):
                if (plot.histo[i].GetEntries() > 0):
                    plotNumber += 1
                    canvas.cd(i + 2)
                    setstatsize(canvas, plot.histo[i], config)
                    plot.histo[i].DrawCopy()

            if (plotNumber == 0):
                break

            canvas.Update()

            # save as pdf
            canvas.Print("{0}/plots/pdf/modules_{1}_{2}.pdf".format(
                config.outputPath, mode, struct.get_name()))

            # export as png
            image = TImage.Create()
            image.FromPad(canvas)
            image.WriteImage("{0}/plots/png/modules_{1}_{2}.png".format(
                config.outputPath, mode, struct.get_name()))

            # add to output list
            output = OutputData(plottype="mod",
                                name=struct.get_name(),
                                parameter=mode,
                                filename="modules_{0}_{1}".format(
                                    mode, struct.get_name()))
            config.outputList.append(output)

    ######################################################################
    # make plots with substructure
    #

    if (config.showsubmodule == 1):
        alignables.create_children_list()
        for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
            for structNumber, struct in enumerate(alignables.structures):
                # use a copy for shorter name
                plot = plots[modeNumber][structNumber]

                subModule.plot(MillePedeUser, alignables, mode, struct, plot,
                               config)
Exemple #4
0
def plot(MillePedeUser, alignables, config):
    logger = logging.getLogger("mpsvalidate")

    # more space for labels
    gStyle.SetPadBottomMargin(0.25)
    gStyle.SetOptStat("emrs")

    for mode in ["xyz", "rot"]:
        big = PlotData(mode)

        # count number of needed bins and max shift
        for line in MillePedeUser:
            if (line.ObjId != 1):
                for i in range(3):
                    if (abs(line.Par[big.data[i]]) != 999999):
                        if (mode == "xyz"):
                            line.Par[big.data[i]] *= 10000
                        big.numberOfBins[i] += 1
                        if (abs(line.Par[big.data[i]]) > abs(big.maxShift[i])):
                            big.maxShift[i] = line.Par[big.data[i]]

        # initialize histograms
        for i in range(3):
            big.histo.append(
                TH1F("Big Structure {0} {1}".format(big.xyz[i], mode), "",
                     big.numberOfBins[i], 0, big.numberOfBins[i]))
            if (big.unit != ""):
                big.histo[i].SetYTitle("#Delta" + big.xyz[i] + " [" +
                                       big.unit + "]")
            else:
                big.histo[i].SetYTitle("#Delta" + big.xyz[i])
            big.histo[i].SetStats(0)
            big.histo[i].SetMarkerStyle(21)
            big.histoAxis.append(big.histo[i].GetXaxis())
            # bigger labels for the text
            big.histoAxis[i].SetLabelSize(0.06)
            big.histo[i].GetYaxis().SetTitleOffset(1.6)

        # add labels
        big.title = TPaveLabel(0.1, 0.8, 0.9, 0.9,
                               "High Level Structures {0}".format(mode))
        big.text = TPaveText(0.05, 0.1, 0.95, 0.75)
        big.text.SetTextAlign(12)

        # error if shift is bigger than limit
        limit = config.limit[mode]
        for i in range(3):
            if (big.unit != ""):
                big.text.AddText("max. shift {0}: {1:.2} {2}".format(
                    big.xyz[i], float(big.maxShift[i]), big.unit))
                if (abs(big.maxShift[i]) > limit):
                    big.text.AddText("! {0} shift bigger than {1} {2}".format(
                        big.xyz[i], limit, big.unit))
            else:
                big.text.AddText("max. shift {0}: {1:.2}".format(
                    big.xyz[i], float(big.maxShift[i])))
                if (abs(big.maxShift[i]) > limit):
                    big.text.AddText("! {0} shift bigger than {1}".format(
                        big.xyz[i], limit))

        # fill histograms with value and name
        for line in MillePedeUser:
            if (line.ObjId != 1):
                for i in range(3):
                    if (abs(line.Par[big.data[i]]) != 999999):
                        # set name of the structure
                        big.histoAxis[i].SetBinLabel(
                            big.binPosition[i],
                            alignables.get_name_by_objid(line.ObjId))
                        # fill with data, big.data[i] xyz or rot data
                        # transform xyz data from cm to #mu m
                        if (mode == "xyz"):
                            big.histo[i].SetBinContent(
                                big.binPosition[i],
                                10000 * line.Par[big.data[i]])
                        else:
                            big.histo[i].SetBinContent(big.binPosition[i],
                                                       line.Par[big.data[i]])
                        big.binPosition[i] += 1

        # rotate labels
        for i in range(3):
            big.histoAxis[i].LabelsOption("v")

        # reset y range
        # two types of ranges

        # 1. show all
        if (config.rangemodeHL == "all"):
            for i in range(3):
                big.usedRange[i] = big.maxShift[i]

        # 2. use given values
        if (config.rangemodeHL == "given"):
            # loop over coordinates
            for i in range(3):
                if (mode == "xyz"):
                    valuelist = config.rangexyzHL
                if (mode == "rot"):
                    valuelist = config.rangerotHL
                # loop over given values
                # without last value
                for value in valuelist:
                    # maximum smaller than given value
                    if (abs(big.maxShift[i]) < value):
                        big.usedRange[i] = value
                        break
                    # if not possible, force highest
                if (abs(big.maxShift[i]) > valuelist[-1]):
                    big.usedRange[i] = valuelist[-1]

        # all the same range
        if (config.samerangeHL == 1):
            # apply new range
            for i in range(3):
                big.usedRange[i] = max(map(abs, big.usedRange))

        # count outlieres
        if (config.rangemodeHL == "given"):
            for i in range(3):
                for binNumber in range(1, big.numberOfBins[i] + 1):
                    if (abs(big.histo[i].GetBinContent(binNumber)) >
                            big.usedRange[i]):
                        big.hiddenEntries[i] += 1

            # add number of outlieres to text
            for i in range(3):
                if (big.hiddenEntries[i] != 0):
                    big.text.AddText("! {0}: {1} outlier !".format(
                        big.xyz[i], int(big.hiddenEntries[i])))

        # create canvas
        cBig = TCanvas("canvasBigStrucutres_{0}".format(mode), "Parameter",
                       300, 0, 800, 600)
        cBig.Divide(2, 2)

        # draw histograms
        cBig.cd(1)
        big.title.Draw()
        big.text.Draw()

        # draw identification
        ident = identification(config)
        ident.Draw()

        # TGraph copy to hide outlier
        copy = 3 * [None]

        # loop over coordinates
        for i in range(3):
            cBig.cd(i + 2)
            # option "AXIS" to only draw the axis
            big.histo[i].SetLineColor(0)
            big.histo[i].Draw("AXIS")
            # set new range
            big.histo[i].GetYaxis().SetRangeUser(-1.1 * abs(big.usedRange[i]),
                                                 1.1 * abs(big.usedRange[i]))

            # TGraph object to hide outlier
            copy[i] = TGraph(big.histo[i])
            # set the new range
            copy[i].SetMaximum(1.1 * abs(big.usedRange[i]))
            copy[i].SetMinimum(-1.1 * abs(big.usedRange[i]))
            # draw the data
            copy[i].Draw("PSAME")

        cBig.Update()

        # save as pdf
        cBig.Print("{0}/plots/pdf/structures_{1}.pdf".format(
            config.outputPath, mode))

        # export as png
        image = TImage.Create()
        image.FromPad(cBig)
        image.WriteImage("{0}/plots/png/structures_{1}.png".format(
            config.outputPath, mode))

        # add to output list
        output = OutputData(plottype="big",
                            parameter=mode,
                            filename="structures_{0}".format(mode))
        config.outputList.append(output)

    # reset BottomMargin
    gStyle.SetPadBottomMargin(0.1)
Exemple #5
0
def plot(treeFile, alignables, config):
    logger = logging.getLogger("mpsvalidate")

    for mode in ["xyz", "rot"]:

        time = PlotData(mode)

        # list of all avaible TTrees
        listMillePedeUser = []
        MillePedeUser = []
        for i in range(config.firsttree, 101):
            if (treeFile.GetListOfKeys().Contains(
                    "MillePedeUser_{0}".format(i))):
                listMillePedeUser.append(i)

        # load MillePedeUser_X TTrees
        for i in listMillePedeUser:
            MillePedeUser.append(treeFile.Get("MillePedeUser_{0}".format(i)))

        ######################################################################
        # remove TTrees without results
        #

        # check if there is a TTree without any results
        # therefor search for the first alignable
        first = 0
        newlistMillePedeUser = []
        # find first alignable
        for line in MillePedeUser[0]:
            if (line.ObjId != 1 and any(
                    abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
                first = line.Id
                newlistMillePedeUser.append(config.firsttree)
                break

        # check the following TTrees
        for ttreeNumber, ttree in enumerate(MillePedeUser[1:]):
            for line in ttree:
                if (line.Id == first):
                    if (any(
                            abs(line.Par[time.data[i]]) != 999999
                            for i in [0, 1, 2])):
                        # note that the first tree was checked
                        newlistMillePedeUser.append(ttreeNumber +
                                                    config.firsttree + 1)
                    break

        listMillePedeUser = newlistMillePedeUser

        # reload MillePedeUser_X TTrees
        MillePedeUser = []
        for i in listMillePedeUser:
            MillePedeUser.append(treeFile.Get("MillePedeUser_{0}".format(i)))

        if not listMillePedeUser:
            logger.error("Timeplots: no TTrees found")
            return

        if not MillePedeUser:
            logger.error("Timeplots: no TTree could be opened")
            return

        ######################################################################
        # initialize data hierarchy
        #

        plots = []
        # objids which were found in the TTree
        objids = []

        # loop over first tree to initialize
        for line in MillePedeUser[0]:
            if (line.ObjId != 1 and any(
                    abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
                plots.append(PlotData(mode))

                # new objid?
                if (line.ObjId not in objids):
                    objids.append(line.ObjId)

                # initialize histograms
                for i in range(3):
                    plots[-1].histo.append(
                        TH1F(
                            "Time Structure {0} {1} {2} {3}".format(
                                mode, alignables.get_name_by_objid(line.ObjId),
                                len(plots), i), "", len(listMillePedeUser), 0,
                            len(listMillePedeUser)))
                    plots[-1].label = line.Id
                    plots[-1].objid = line.ObjId

                    if (time.unit != ""):
                        plots[-1].histo[i].SetYTitle("#Delta" + time.xyz[i] +
                                                     " [" + time.unit + "]")
                    else:
                        plots[-1].histo[i].SetYTitle("#Delta" + time.xyz[i])
                    plots[-1].histo[i].SetXTitle("IOV")
                    plots[-1].histo[i].SetStats(0)
                    plots[-1].histo[i].SetMarkerStyle(21)
                    # bigger labels for the text
                    plots[-1].histo[i].GetXaxis().SetLabelSize(0.08)
                    plots[-1].histo[i].GetYaxis().SetTitleOffset(1.6)

        ######################################################################
        # fill histogram
        #

        # loop over TTrees
        for treeNumber, tree in enumerate(MillePedeUser):
            for line in tree:
                if (line.ObjId != 1 and any(
                        abs(line.Par[time.data[i]]) != 999999
                        for i in [0, 1, 2])):
                    # find the right plot
                    for plot in plots:
                        if (plot.label == line.Id):
                            for i in range(3):
                                # note that the first bin is referenced by 1
                                plot.histo[i].GetXaxis().SetBinLabel(
                                    treeNumber + 1,
                                    str(listMillePedeUser[treeNumber]))
                                # transform xyz data from cm to #mu m
                                if (mode == "xyz"):
                                    plot.histo[i].SetBinContent(
                                        treeNumber + 1,
                                        10000 * line.Par[plot.data[i]])
                                else:
                                    plot.histo[i].SetBinContent(
                                        treeNumber + 1, line.Par[plot.data[i]])

        ######################################################################
        # find maximum/minimum
        #

        maximum = [[0, 0, 0] for x in range(len(objids))]
        minimum = [[0, 0, 0] for x in range(len(objids))]

        for index, objid in enumerate(objids):
            for plot in plots:
                if (plot.objid == objid):
                    for i in range(3):
                        # maximum
                        if (plot.histo[i].GetMaximum() > maximum[index][i]):
                            maximum[index][i] = plot.histo[i].GetMaximum()
                        # minimum
                        if (plot.histo[i].GetMinimum() < minimum[index][i]):
                            minimum[index][i] = plot.histo[i].GetMinimum()

        ######################################################################
        # make the plots
        #

        # loop over all objids
        for index, objid in enumerate(objids):

            canvas = TCanvas(
                "canvasTimeBigStrucutres_{0}_{1}".format(
                    mode, alignables.get_name_by_objid(objid)), "Parameter",
                300, 0, 800, 600)
            canvas.Divide(2, 2)

            # add text
            title = TPaveLabel(
                0.1, 0.8, 0.9, 0.9,
                "{0} over time {1}".format(alignables.get_name_by_objid(objid),
                                           mode))

            legend = TLegend(0.05, 0.1, 0.95, 0.75)

            # draw on canvas
            canvas.cd(1)
            title.Draw()

            # draw identification
            ident = identification(config)
            ident.Draw()

            # TGraph copies to hide outlier
            copy = []

            # reset y range of first plot
            # two types of ranges
            for i in range(3):
                for plot in plots:
                    if (plot.objid == objid):
                        # 1. show all
                        if (config.rangemodeHL == "all"):
                            plot.usedRange[i] = max(abs(maximum[index][i]),
                                                    abs(minimum[index][i]))

                        # 2. use given values
                        if (config.rangemodeHL == "given"):
                            # loop over coordinates
                            if (mode == "xyz"):
                                valuelist = config.rangexyzHL
                            if (mode == "rot"):
                                valuelist = config.rangerotHL
                            # loop over given values
                            # without last value
                            for value in valuelist:
                                # maximum smaller than given value
                                if (max(abs(maximum[index][i]),
                                        abs(minimum[index][i])) < value):
                                    plot.usedRange[i] = value
                                    break
                            # if not possible, force highest
                            if (max(abs(maximum[index][i]),
                                    abs(minimum[index][i])) > valuelist[-1]):
                                plot.usedRange[i] = valuelist[-1]

            # draw plots on canvas
            for i in range(3):
                canvas.cd(2 + i)

                number = 1

                for plot in plots:
                    if (plot.objid == objid):
                        # all the same range
                        if (config.samerangeHL == 1):
                            plot.usedRange[i] = max(map(abs, plot.usedRange))

                        # set new range
                        plot.histo[i].GetYaxis().SetRangeUser(
                            -1.2 * abs(plot.usedRange[i]),
                            1.2 * abs(plot.usedRange[i]))

                        plot.histo[i].SetLineColorAlpha(number + 2, 0.5)
                        plot.histo[i].SetMarkerColorAlpha(number + 2, 1)

                        # option "AXIS" to only draw the axis
                        plot.histo[i].SetLineColor(0)
                        plot.histo[i].Draw("PSAME")

                        # TGraph object to hide outlier
                        copy.append(TGraph(plot.histo[i]))
                        # set the new range
                        copy[-1].SetMaximum(1.2 * abs(plot.usedRange[i]))
                        copy[-1].SetMinimum(-1.2 * abs(plot.usedRange[i]))
                        # draw the data
                        copy[-1].SetLineColorAlpha(number + 2, 0.5)
                        copy[-1].Draw("LPSAME")

                        if (i == 0):
                            legend.AddEntry(plot.histo[i],
                                            "{0}".format(number))
                        number += 1

            canvas.cd(1)
            legend.Draw()

            canvas.Update()

            # save as pdf
            canvas.Print("{0}/plots/pdf/timeStructures_{1}_{2}.pdf".format(
                config.outputPath, mode, alignables.get_name_by_objid(objid)))

            # export as png
            image = TImage.Create()
            image.FromPad(canvas)
            image.WriteImage("{0}/plots/png/timeStructures_{1}_{2}.png".format(
                config.outputPath, mode, alignables.get_name_by_objid(objid)))

            # add to output list
            output = OutputData(plottype="time",
                                name=alignables.get_name_by_objid(objid),
                                parameter=mode,
                                filename="timeStructures_{0}_{1}".format(
                                    mode, alignables.get_name_by_objid(objid)))
            config.outputList.append(output)