CMS 3D CMS Logo

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