Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
##########################################################################
# Creates histograms of the modules of a part of a structure and combines it
# with a plot of the modules of the hole structure. Returns a nested
# list with the PlotData of the histograms
##

from builtins import range
import logging

import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True
ROOT.gROOT.SetBatch()

import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style as mpsv_style
import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes


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(mpsv_classes.PlotData(mode))

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

        for i in range(3):
            if (mode == "xyz"):
                plot.histo.append(ROOT.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(ROOT.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].SetLineColor(6)
            plot.histo[i].SetStats(0)

        # add labels
        plot.title = ROOT.TPaveLabel(
            0.1, 0.8, 0.9, 0.9, "Module: {0} {1}".format(struct.get_name(), mode))
        plot.text = ROOT.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 = ROOT.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 = ROOT.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].SetYTitle("normalized")
        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 = mpsv_style.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 = ROOT.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 = mpsv_classes.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)