Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-18 03:26:52

0001 ##########################################################################
0002 # Creates histograms of the modules of a part of a structure and combines it
0003 # with a plot of the modules of the hole structure. Returns a nested
0004 # list with the PlotData of the histograms
0005 ##
0006 
0007 from builtins import range
0008 import logging
0009 
0010 import ROOT
0011 ROOT.PyConfig.IgnoreCommandLineOptions = True
0012 ROOT.gROOT.SetBatch()
0013 
0014 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style as mpsv_style
0015 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes
0016 
0017 
0018 def plot(MillePedeUser, alignables, mode, struct, parentPlot, config):
0019     logger = logging.getLogger("mpsvalidate")
0020     
0021     # skip empty
0022     number = 0
0023     for i in range(3):
0024         if(parentPlot.histo[i].GetEntries() == 0):
0025             number += 1
0026     if (number == 3):
0027         return
0028 
0029     # number of bins to start
0030     numberOfBins = 10000
0031 
0032     ######################################################################
0033     # initialize data hierarchy
0034     # plots[subStrucut]
0035     #
0036 
0037     plots = []
0038 
0039     # initialize histograms
0040     for subStructNumber, subStruct in enumerate(struct.get_children()):
0041         plots.append(mpsv_classes.PlotData(mode))
0042 
0043         # use a copy for shorter name
0044         plot = plots[subStructNumber]
0045 
0046         for i in range(3):
0047             if (mode == "xyz"):
0048                 plot.histo.append(ROOT.TH1F("{0} {1} {2}".format(struct.get_name() + " " + subStruct.get_name(), plot.xyz[
0049                                   i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -1000, 1000))
0050             else:
0051                 plot.histo.append(ROOT.TH1F("{0} {1} {2}".format(struct.get_name() + " " + subStruct.get_name(), plot.xyz[
0052                                   i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -0.1, 0.1))
0053 
0054             plot.histo[i].SetLineColor(6)
0055             plot.histo[i].SetStats(0)
0056 
0057         # add labels
0058         plot.title = ROOT.TPaveLabel(
0059             0.1, 0.8, 0.9, 0.9, "Module: {0} {1}".format(struct.get_name(), mode))
0060         plot.text = ROOT.TPaveText(0.05, 0.1, 0.95, 0.75)
0061         plot.text.SetTextAlign(12)
0062         plot.text.SetTextSizePixels(20)
0063 
0064         # save copy
0065         plots[subStructNumber] = plot
0066 
0067     ######################################################################
0068     # fill histogram
0069     #
0070 
0071     for line in MillePedeUser:
0072         # is module ?
0073         if (line.ObjId == 1):
0074             for subStructNumber, subStruct in enumerate(struct.get_children()):
0075                 # use a copy for shorter name
0076                 plot = plots[subStructNumber]
0077 
0078                 # module in struct ?
0079                 if (subStruct.contains_detid(line.Id)):
0080                     for i in range(3):
0081                         if (abs(line.Par[plot.data[i]]) != 999999):
0082                             # transform xyz data from cm to #mu m
0083                             if (mode == "xyz"):
0084                                 plot.histo[i].Fill(
0085                                     10000 * line.Par[plot.data[i]])
0086                             else:
0087                                 plot.histo[i].Fill(line.Par[plot.data[i]])
0088 
0089                 # save copy
0090                 plots[subStructNumber] = plot
0091 
0092     ######################################################################
0093     # find the best range
0094     #
0095     for subStructNumber, subStruct in enumerate(struct.get_children()):
0096         # use a copy for shorter name
0097         plot = plots[subStructNumber]
0098         for i in range(3):
0099             if (plot.histo[i].GetEntries() != 0 and plot.histo[i].GetStdDev() != 0):
0100                 # use binShift of the hole structure
0101                 binShift = parentPlot.usedRange[i]
0102 
0103                 # count entries which are not shown anymore
0104                 # bin 1 to begin of histogram
0105                 for j in range(1, numberOfBins // 2 - binShift):
0106                     plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
0107                 # from the end of shown bins to the end of histogram
0108                 for j in range(numberOfBins // 2 + binShift, plot.histo[i].GetNbinsX()):
0109                     plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
0110 
0111                 # merge bins, ca. 100 should be visible in the resulting plot
0112                 mergeNumberBins = binShift
0113                 # skip empty histogram
0114                 if (mergeNumberBins != 0):
0115                     # the 2*maxBinShift bins should shrink to 100 bins
0116                     mergeNumberBins = int(
0117                         2. * mergeNumberBins / config.numberofbins)
0118                     # the total number of bins should be dividable by the bins
0119                     # shrinked together
0120                     if (mergeNumberBins == 0):
0121                         mergeNumberBins = 1
0122                     while (numberOfBins % mergeNumberBins != 0 and mergeNumberBins != 1):
0123                         mergeNumberBins -= 1
0124 
0125                     # Rebin and save new created histogram and axis
0126                     plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)
0127 
0128                     # set view range. it is important to note that the number of bins have changed with the rebinning
0129                     # the total number and the number of shift must be
0130                     # corrected with / mergeNumberBins
0131                     plot.histo[i].GetXaxis().SetRange(int(numberOfBins // (2 * mergeNumberBins) - binShift /
0132                                                           mergeNumberBins), int(numberOfBins // (2 * mergeNumberBins) + binShift / mergeNumberBins))
0133 
0134         # save copy
0135         plots[subStructNumber] = plot
0136 
0137     ######################################################################
0138     # make the plots
0139     #
0140 
0141     canvas = ROOT.TCanvas("SubStruct_{0}_{1}".format(
0142         struct.get_name(), mode), "Parameter", 300, 0, 800, 600)
0143     canvas.Divide(2, 2)
0144 
0145     canvas.cd(1)
0146     parentPlot.title.Draw()
0147 
0148     legend = ROOT.TLegend(0.05, 0.1, 0.95, 0.75)
0149 
0150     for i in range(3):
0151         canvas.cd(i + 2)
0152 
0153         # find y maximum
0154         maximum = []
0155 
0156         if (parentPlot.histo[i].GetEntries() == 0):
0157             continue
0158 
0159         # normalize parent
0160         parentPlot.histo[i].Scale(1. / parentPlot.histo[i].Integral())
0161         maximum.append(parentPlot.histo[i].GetMaximum())
0162 
0163         for subStructNumber, subStruct in enumerate(struct.get_children()):
0164             # use a copy for shorter name
0165             plot = plots[subStructNumber]
0166 
0167             if (plot.histo[i].GetEntries() > 0):
0168                 plot.histo[i].Scale(1. / plot.histo[i].Integral())
0169                 maximum.append(plot.histo[i].GetMaximum())
0170 
0171             # save copy
0172             plots[subStructNumber] = plot
0173 
0174         # set range and plot
0175         parentPlot.histo[i].GetYaxis().SetRangeUser(0., 1.1 * max(maximum))
0176         parentPlot.histo[i].SetYTitle("normalized")
0177         parentPlot.histo[i].Draw()
0178 
0179         for subStructNumber, subStruct in enumerate(struct.get_children()):
0180             # use a copy for shorter name
0181             plot = plots[subStructNumber].histo[i]
0182 
0183             plot.SetLineColorAlpha(subStructNumber + 2, 0.5)
0184             plot.Draw("same")
0185             if (i == 0):
0186                 legend.AddEntry(plot, subStruct.get_name(), "l")
0187 
0188     canvas.cd(1)
0189 
0190     legend.Draw()
0191     # draw identification
0192     ident = mpsv_style.identification(config)
0193     ident.Draw()
0194 
0195     canvas.Update()
0196 
0197     # save as pdf
0198     canvas.Print(
0199         "{0}/plots/pdf/subModules_{1}_{2}.pdf".format(config.outputPath, mode, struct.get_name()))
0200 
0201     # export as png
0202     image = ROOT.TImage.Create()
0203     image.FromPad(canvas)
0204     image.WriteImage(
0205         "{0}/plots/png/subModules_{1}_{2}.png".format(config.outputPath, mode, struct.get_name()))
0206 
0207     # add to output list
0208     output = mpsv_classes.OutputData(plottype="subMod", name=struct.get_name(), number=subStructNumber + 1,
0209                                      parameter=mode, filename="subModules_{0}_{1}".format(mode, struct.get_name()))
0210     config.outputList.append(output)