test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
bigStructure.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ##########################################################################
4 # Creates a histogram where the the names of the structures are present
5 # as humanreadable text.
6 ##
7 
8 import logging
9 
10 from ROOT import (TH1F, TCanvas, TGraph, TImage, TPaveLabel, TPaveText, TTree,
11  gROOT, gStyle)
12 
13 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes import OutputData, PlotData
14 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.geometry import Alignables, Structure
15 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style import identification
16 
17 
18 def plot(MillePedeUser, alignables, config):
19  logger = logging.getLogger("mpsvalidate")
20 
21  # more space for labels
22  gStyle.SetPadBottomMargin(0.25)
23  gStyle.SetOptStat("emrs")
24 
25  for mode in ["xyz", "rot"]:
26  big = PlotData(mode)
27 
28  # count number of needed bins and max shift
29  for line in MillePedeUser:
30  if (line.ObjId != 1):
31  for i in range(3):
32  if (abs(line.Par[big.data[i]]) != 999999):
33  if (mode == "xyz"):
34  line.Par[big.data[i]] *= 10000
35  big.numberOfBins[i] += 1
36  if (abs(line.Par[big.data[i]]) > abs(big.maxShift[i])):
37  big.maxShift[i] = line.Par[big.data[i]]
38 
39  # initialize histograms
40  for i in range(3):
41  big.histo.append(TH1F("Big Structure {0} {1}".format(big.xyz[i], mode), "Parameter {0}".format(
42  big.xyz[i]), big.numberOfBins[i], 0, big.numberOfBins[i]))
43  big.histo[i].SetYTitle(big.unit)
44  big.histo[i].SetStats(0)
45  big.histo[i].SetMarkerStyle(21)
46  big.histoAxis.append(big.histo[i].GetXaxis())
47  # bigger labels for the text
48  big.histoAxis[i].SetLabelSize(0.06)
49  big.histo[i].GetYaxis().SetTitleOffset(1.6)
50 
51  # add labels
52  big.title = TPaveLabel(
53  0.1, 0.8, 0.9, 0.9, "High Level Structures {0}".format(mode))
54  big.text = TPaveText(0.05, 0.1, 0.95, 0.75)
55  big.text.SetTextAlign(12)
56 
57  # error if shift is bigger than limit
58  limit = config.limit[mode]
59  for i in range(3):
60  big.text.AddText("max. shift {0}: {1:.2}".format(
61  big.xyz[i], float(big.maxShift[i])))
62  if (abs(big.maxShift[i]) > limit):
63  big.text.AddText(
64  "! {0} shift bigger than {1} !".format(big.xyz[i], limit))
65 
66  # fill histograms with value and name
67  for line in MillePedeUser:
68  if (line.ObjId != 1):
69  for i in range(3):
70  if (abs(line.Par[big.data[i]]) != 999999):
71  # set name of the structure
72  big.histoAxis[i].SetBinLabel(
73  big.binPosition[i], alignables.get_name_by_objid(line.ObjId))
74  # fill with data, big.data[i] xyz or rot data
75  # transform xyz data from cm to #mu m
76  if (mode == "xyz"):
77  big.histo[i].SetBinContent(
78  big.binPosition[i], 10000 * line.Par[big.data[i]])
79  else:
80  big.histo[i].SetBinContent(
81  big.binPosition[i], line.Par[big.data[i]])
82  big.binPosition[i] += 1
83 
84  # rotate labels
85  for i in range(3):
86  big.histoAxis[i].LabelsOption("v")
87 
88  # reset y range
89  # two types of ranges
90 
91  # 1. show all
92  if (config.rangemodeHL == "all"):
93  for i in range(3):
94  big.usedRange[i] = big.maxShift[i]
95 
96  # 2. use given values
97  if (config.rangemodeHL == "given"):
98  # loop over coordinates
99  for i in range(3):
100  if (mode == "xyz"):
101  valuelist = config.rangexyzHL
102  if (mode == "rot"):
103  valuelist = config.rangerotHL
104  # loop over given values
105  # without last value
106  for value in valuelist:
107  # maximum smaller than given value
108  if (abs(big.maxShift[i]) < value):
109  big.usedRange[i] = value
110  break
111  # if not possible, force highest
112  if (abs(big.maxShift[i]) > valuelist[-1]):
113  big.usedRange[i] = valuelist[-1]
114 
115  # all the same range
116  if (config.samerangeHL == 1):
117  # apply new range
118  for i in range(3):
119  big.usedRange[i] = max(map(abs, big.usedRange))
120 
121  # count outlieres
122  if (config.rangemodeHL == "given"):
123  for i in range(3):
124  for binNumber in range(1, big.numberOfBins[i] + 1):
125  if (abs(big.histo[i].GetBinContent(binNumber)) > big.usedRange[i]):
126  big.hiddenEntries[i] += 1
127 
128  # add number of outlieres to text
129  for i in range(3):
130  if (big.hiddenEntries[i] != 0):
131  big.text.AddText("! {0} {1} outlier !".format(
132  big.xyz[i], int(big.hiddenEntries[i])))
133 
134  # create canvas
135  cBig = TCanvas("canvasBigStrucutres_{0}".format(
136  mode), "Parameter", 300, 0, 800, 600)
137  cBig.Divide(2, 2)
138 
139  # draw histograms
140  cBig.cd(1)
141  big.title.Draw()
142  big.text.Draw()
143 
144  # draw identification
145  ident = identification(config)
146  ident.Draw()
147 
148  # TGraph copy to hide outlier
149  copy = 3 * [None]
150 
151  # loop over coordinates
152  for i in range(3):
153  cBig.cd(i + 2)
154  # option "AXIS" to only draw the axis
155  big.histo[i].SetLineColor(0)
156  big.histo[i].Draw("P")
157  # set new range
158  big.histo[i].GetYaxis().SetRangeUser(-1.1 *
159  abs(big.usedRange[i]), 1.1 * abs(big.usedRange[i]))
160 
161  # TGraph object to hide outlier
162  copy[i] = TGraph(big.histo[i])
163  # set the new range
164  copy[i].SetMaximum(1.1 * abs(big.usedRange[i]))
165  copy[i].SetMinimum(-1.1 * abs(big.usedRange[i]))
166  # draw the data
167  copy[i].Draw("PSAME")
168 
169  cBig.Update()
170 
171  # save as pdf
172  cBig.Print(
173  "{0}/plots/pdf/structures_{1}.pdf".format(config.outputPath, mode))
174 
175  # export as png
176  image = TImage.Create()
177  image.FromPad(cBig)
178  image.WriteImage(
179  "{0}/plots/png/structures_{1}.png".format(config.outputPath, mode))
180 
181  # add to output list
182  output = OutputData(plottype="big", parameter=mode,
183  filename="structures_{0}".format(mode))
184  config.outputList.append(output)
185 
186  # reset BottomMargin
187  gStyle.SetPadBottomMargin(0.1)
def identification
creates the identification text in the top left corner
Definition: style.py:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22