Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:35

0001 ##########################################################################
0002 # Creates a histogram where the the names of the structures are present
0003 # as humanreadable text.
0004 ##
0005 
0006 from builtins import range
0007 import logging
0008 
0009 import ROOT
0010 ROOT.PyConfig.IgnoreCommandLineOptions = True
0011 ROOT.gROOT.SetBatch()
0012 
0013 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style as mpsv_style
0014 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes
0015 
0016 
0017 def plot(MillePedeUser, alignables, config):
0018     logger = logging.getLogger("mpsvalidate")
0019     
0020     # more space for labels
0021     ROOT.gStyle.SetPadBottomMargin(0.25)
0022     ROOT.gStyle.SetOptStat("emrs")
0023 
0024     for mode in ["xyz", "rot"]:
0025         big = mpsv_classes.PlotData(mode)
0026 
0027         # count number of needed bins and max shift
0028         for line in MillePedeUser:
0029             if (line.ObjId != 1):
0030                 for i in range(3):
0031                     if (abs(line.Par[big.data[i]]) != 999999):
0032                         if (mode == "xyz"):
0033                             line.Par[big.data[i]] *= 10000
0034                         big.numberOfBins[i] += 1
0035                         if (abs(line.Par[big.data[i]]) > abs(big.maxShift[i])):
0036                             big.maxShift[i] = line.Par[big.data[i]]
0037 
0038         # initialize histograms
0039         for i in range(3):
0040             big.histo.append(ROOT.TH1F("Big Structure {0} {1}".format(big.xyz[i], mode), "", big.numberOfBins[i], 0, big.numberOfBins[i]))
0041             if (big.unit!=""):
0042                 big.histo[i].SetYTitle("#Delta"+big.xyz[i]+" ["+big.unit+"]")
0043             else:
0044                 big.histo[i].SetYTitle("#Delta"+big.xyz[i])
0045             big.histo[i].SetStats(0)
0046             big.histo[i].SetMarkerStyle(21)
0047             big.histoAxis.append(big.histo[i].GetXaxis())
0048             # bigger labels for the text
0049             big.histoAxis[i].SetLabelSize(0.06)
0050             big.histo[i].GetYaxis().SetTitleOffset(1.6)
0051 
0052         # add labels
0053         big.title = ROOT.TPaveLabel(
0054             0.1, 0.8, 0.9, 0.9, "High Level Structures {0}".format(mode))
0055         big.text = ROOT.TPaveText(0.05, 0.1, 0.95, 0.75)
0056         big.text.SetTextAlign(12)
0057 
0058         # error if shift is bigger than limit
0059         limit = config.limit[mode]
0060         for i in range(3):
0061             if (big.unit!=""):
0062                 big.text.AddText("max. shift {0}: {1:.2} {2}".format(big.xyz[i], float(big.maxShift[i]), big.unit))
0063                 if (abs(big.maxShift[i]) > limit):
0064                     big.text.AddText("! {0} shift bigger than {1} {2}".format(big.xyz[i], limit, big.unit))
0065             else:
0066                 big.text.AddText("max. shift {0}: {1:.2}".format(big.xyz[i], float(big.maxShift[i])))
0067                 if (abs(big.maxShift[i]) > limit):
0068                     big.text.AddText("! {0} shift bigger than {1}".format(big.xyz[i], limit))
0069 
0070         # fill histograms with value and name
0071         for line in MillePedeUser:
0072             if (line.ObjId != 1):
0073                 for i in range(3):
0074                     if (abs(line.Par[big.data[i]]) != 999999):
0075                         # set name of the structure
0076                         big.histoAxis[i].SetBinLabel(
0077                             big.binPosition[i],
0078                             str(line.Name) if len(line.Name) <= 13 else str(line.Name)[:12]+".")
0079                         # fill with data, big.data[i] xyz or rot data
0080                         # transform xyz data from cm to #mu m
0081                         if (mode == "xyz"):
0082                             big.histo[i].SetBinContent(
0083                                 big.binPosition[i], 10000 * line.Par[big.data[i]])
0084                         else:
0085                             big.histo[i].SetBinContent(
0086                                 big.binPosition[i], line.Par[big.data[i]])
0087                         big.binPosition[i] += 1
0088 
0089         # rotate labels
0090         for i in range(3):
0091             big.histoAxis[i].LabelsOption("v")
0092 
0093         # reset y range
0094         # two types of ranges
0095 
0096         # 1. show all
0097         if (config.rangemodeHL == "all"):
0098             for i in range(3):
0099                 big.usedRange[i] = big.maxShift[i]
0100 
0101         # 2. use given values
0102         if (config.rangemodeHL == "given"):
0103             # loop over coordinates
0104             for i in range(3):
0105                 if (mode == "xyz"):
0106                     valuelist = config.rangexyzHL
0107                 if (mode == "rot"):
0108                     valuelist = config.rangerotHL
0109                 # loop over given values
0110                 # without last value
0111                 for value in valuelist:
0112                     # maximum smaller than given value
0113                     if (abs(big.maxShift[i]) < value):
0114                         big.usedRange[i] = value
0115                         break
0116                     # if not possible, force highest
0117                 if (abs(big.maxShift[i]) > valuelist[-1]):
0118                     big.usedRange[i] = valuelist[-1]
0119 
0120         # all the same range
0121         if (config.samerangeHL == 1):
0122             # apply new range
0123             for i in range(3):
0124                 big.usedRange[i] = max(map(abs, big.usedRange))
0125 
0126         # count outlieres
0127         if (config.rangemodeHL == "given"):
0128             for i in range(3):
0129                 for binNumber in range(1, big.numberOfBins[i] + 1):
0130                     if (abs(big.histo[i].GetBinContent(binNumber)) > big.usedRange[i]):
0131                         big.hiddenEntries[i] += 1
0132 
0133             # add number of outlieres to text
0134             for i in range(3):
0135                 if (big.hiddenEntries[i] != 0):
0136                     big.text.AddText("! {0}: {1} outlier !".format(
0137                         big.xyz[i], int(big.hiddenEntries[i])))
0138 
0139         # create canvas
0140         cBig = ROOT.TCanvas("canvasBigStrucutres_{0}".format(
0141             mode), "Parameter", 300, 0, 800, 600)
0142         cBig.Divide(2, 2)
0143 
0144         # draw histograms
0145         cBig.cd(1)
0146         big.title.Draw()
0147         big.text.Draw()
0148 
0149         # draw identification
0150         ident = mpsv_style.identification(config)
0151         ident.Draw()
0152 
0153         # TGraph copy to hide outlier
0154         copy = 3 * [None]
0155 
0156         # loop over coordinates
0157         for i in range(3):
0158             cBig.cd(i + 2)
0159             # option "AXIS" to only draw the axis
0160             big.histo[i].SetLineColor(0)
0161             big.histo[i].Draw("AXIS")
0162             # set new range
0163             big.histo[i].GetYaxis().SetRangeUser(-1.1 *
0164                                                  abs(big.usedRange[i]), 1.1 * abs(big.usedRange[i]))
0165 
0166             # TGraph object to hide outlier
0167             copy[i] = ROOT.TGraph(big.histo[i])
0168             # set the new range
0169             copy[i].SetMaximum(1.1 * abs(big.usedRange[i]))
0170             copy[i].SetMinimum(-1.1 * abs(big.usedRange[i]))
0171             # draw the data
0172             copy[i].Draw("PSAME")
0173 
0174         cBig.Update()
0175 
0176         # save as pdf
0177         cBig.Print(
0178             "{0}/plots/pdf/structures_{1}.pdf".format(config.outputPath, mode))
0179 
0180         # export as png
0181         image = ROOT.TImage.Create()
0182         image.FromPad(cBig)
0183         image.WriteImage(
0184             "{0}/plots/png/structures_{1}.png".format(config.outputPath, mode))
0185 
0186         # add to output list
0187         output = mpsv_classes.OutputData(plottype="big", parameter=mode,
0188                                          filename="structures_{0}".format(mode))
0189         config.outputList.append(output)
0190 
0191     # reset BottomMargin
0192     ROOT.gStyle.SetPadBottomMargin(0.1)