CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
bigModule.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ##########################################################################
4 # Creates histograms of the modules of one structure. and returns them as
5 # a list of PlotData objects.
6 ##
7 
8 import logging
9 
10 from ROOT import (TH1F, TCanvas, TImage, TPaveLabel, TPaveText, TTree, gROOT,
11  gStyle)
12 
13 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate import subModule
14 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes import PedeDumpData, OutputData, PlotData
15 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.geometry import Alignables, Structure
16 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style import identification, setstatsize
17 
18 
19 def plot(MillePedeUser, alignables, config):
20  logger = logging.getLogger("mpsvalidate")
21 
22  alignables.create_list(MillePedeUser)
23 
24  # number of bins to start
25  numberOfBins = 10000
26 
27  ######################################################################
28  # initialize data hierarchy
29  # plots[mode][struct]
30  #
31 
32  plots = []
33  # loop over mode
34  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
35  plots.append([])
36  # loop over structures
37  for structNumber, struct in enumerate(alignables.structures):
38  plots[modeNumber].append(PlotData(mode))
39 
40  # initialize histograms
41  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
42  for structNumber, struct in enumerate(alignables.structures):
43  # use a copy for shorter name
44  plot = plots[modeNumber][structNumber]
45 
46  for i in range(3):
47  if (mode == "xyz"):
48  plot.histo.append(TH1F("{0} {1} {2}".format(struct.get_name(), plot.xyz[
49  i], mode), "", numberOfBins, -1000, 1000))
50  else:
51  plot.histo.append(TH1F("{0} {1} {2}".format(struct.get_name(), plot.xyz[
52  i], mode), "", numberOfBins, -0.1, 0.1))
53 
54  if (plot.unit!=""):
55  plot.histo[i].SetXTitle("#Delta"+plot.xyz[i]+" ["+plot.unit+"]")
56  else:
57  plot.histo[i].SetXTitle("#Delta"+plot.xyz[i])
58  plot.histo[i].SetYTitle("number of alignables")
59  plot.histo[i].GetXaxis().SetTitleOffset(0.8)
60  plot.histoAxis.append(plot.histo[i].GetXaxis())
61 
62  # add labels
63  plot.title = TPaveLabel(
64  0.1, 0.8, 0.9, 0.9, "Module: {0} {1}".format(struct.get_name(), mode))
65  plot.text = TPaveText(0.05, 0.1, 0.95, 0.75)
66  plot.text.SetTextAlign(12)
67  plot.text.SetTextSizePixels(20)
68 
69  # save copy
70  plots[modeNumber][structNumber] = plot
71 
72  ######################################################################
73  # fill histogram
74  #
75 
76  for line in MillePedeUser:
77  # is module ?
78  if (line.ObjId == 1):
79  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
80  for structNumber, struct in enumerate(alignables.structures):
81  # use a copy for shorter name
82  plot = plots[modeNumber][structNumber]
83 
84  # module in struct ?
85  if (struct.contains_detid(line.Id)):
86  for i in range(3):
87  if (abs(line.Par[plot.data[i]]) != 999999):
88  # transform xyz data from cm to #mu m
89  if (mode == "xyz"):
90  plot.histo[i].Fill(
91  10000 * line.Par[plot.data[i]])
92  else:
93  plot.histo[i].Fill(line.Par[plot.data[i]])
94 
95  # save copy
96  plots[modeNumber][structNumber] = plot
97 
98  ######################################################################
99  # find the best range
100  #
101 
102  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
103  for structNumber, struct in enumerate(alignables.structures):
104  # use a copy for shorter name
105  plot = plots[modeNumber][structNumber]
106 
107  for i in range(3):
108  # get first and last bin with content and chose the one which
109  # has a greater distance to the center
110  if (abs(numberOfBins / 2 - plot.histo[i].FindFirstBinAbove()) > abs(plot.histo[i].FindLastBinAbove() - numberOfBins / 2)):
111  plot.maxBinShift[i] = abs(
112  numberOfBins / 2 - plot.histo[i].FindFirstBinAbove())
113  # set the maxShift value
114  plot.maxShift[i] = plot.histo[i].GetBinCenter(
115  plot.histo[i].FindFirstBinAbove())
116  else:
117  plot.maxBinShift[i] = abs(
118  plot.histo[i].FindLastBinAbove() - numberOfBins / 2)
119  # set the maxShift value
120  plot.maxShift[i] = plot.histo[i].GetBinCenter(
121  plot.histo[i].FindLastBinAbove())
122  # skip empty histogram
123  if (abs(plot.maxBinShift[i]) == numberOfBins / 2 + 1):
124  plot.maxBinShift[i] = 0
125 
126  # three types of ranges
127 
128  # 1. multiple of standard dev
129  if (config.rangemode == "stddev"):
130  for i in range(3):
131  if (plot.histo[i].GetEntries() != 0 and plot.histo[i].GetStdDev() != 0):
132  # if the plotrange is much bigger than the standard
133  # deviation use config.widthstdev * StdDev als Range
134  if (max(plot.maxShift) / plot.histo[i].GetStdDev() > config.defpeak):
135  # corresponding bin config.widthstdev*StdDev
136  binShift = int(plot.histo[i].FindBin(
137  config.widthstddev * plot.histo[i].GetStdDev()) - numberOfBins / 2)
138  else:
139  binShift = max(plot.maxBinShift)
140 
141  # save used binShift
142  plot.binShift[i] = binShift
143 
144  # 2. show all
145  if (config.rangemode == "all"):
146  for i in range(3):
147  plot.binShift[i] = plot.maxBinShift[i]
148 
149  # 3. use given ranges
150  if (config.rangemode == "given"):
151  for i in range(3):
152  if (mode == "xyz"):
153  valuelist = config.rangexyzM
154  if (mode == "rot"):
155  valuelist = config.rangerotM
156  if (mode == "dist"):
157  valuelist = config.rangedistM
158 
159  for value in valuelist:
160  # maximum smaller than given value
161  if (abs(plot.maxShift[i]) < value):
162  binShift = value
163  break
164  # if not possible, force highest
165  if (abs(plot.maxShift[i]) > valuelist[-1]):
166  binShift = valuelist[-1]
167  # calculate binShift
168  plot.binShift[i] = int(
169  binShift / plot.histo[i].GetBinWidth(1))
170 
171  # all plot the same range
172  if (config.samerange == 1):
173  for i in range(3):
174  plot.binShift[i] = max(plot.binShift)
175 
176  # save used range
177  for i in range(3):
178  plot.usedRange[i] = plot.binShift[i]
179 
180  # count entries which are not shown anymore
181  for i in range(3):
182  # bin 1 to begin of histogram
183  for j in range(1, numberOfBins / 2 - plot.binShift[i]):
184  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
185  # from the end of shown bins to the end of histogram
186  for j in range(numberOfBins / 2 + plot.binShift[i], plot.histo[i].GetNbinsX()):
187  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
188 
189  # apply new range
190  for i in range(3):
191  if (plot.histo[i].GetEntries() != 0):
192  # merge bins, ca. 100 should be visible in the resulting
193  # plot
194  mergeNumberBins = plot.binShift[i]
195  # skip empty histogram
196  if (mergeNumberBins != 0):
197  # the 2*maxBinShift bins should shrink to 100 bins
198  mergeNumberBins = int(
199  2. * mergeNumberBins / config.numberofbins)
200  # the total number of bins should be dividable by the
201  # bins shrinked together
202  if (mergeNumberBins == 0):
203  mergeNumberBins = 1
204  while (numberOfBins % mergeNumberBins != 0 and mergeNumberBins != 1):
205  mergeNumberBins -= 1
206 
207  # Rebin and save new created histogram and axis
208  plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)
209  plot.histoAxis[i] = plot.histo[i].GetXaxis()
210 
211  # set view range. it is important to note that the number of bins have changed with the rebinning
212  # the total number and the number of shift must be
213  # corrected with / mergeNumberBins
214  plot.histoAxis[i].SetRange(int(numberOfBins / (2 * mergeNumberBins) - plot.binShift[
215  i] / mergeNumberBins), int(numberOfBins / (2 * mergeNumberBins) + plot.binShift[i] / mergeNumberBins))
216 
217  # error if shift is bigger than limit
218  limit = config.limit[mode]
219  for i in range(3):
220  # skip empty
221  if (plot.histo[i].GetEntries() > 0):
222  if (plot.unit!=""):
223  plot.text.AddText("max. shift {0}: {1:.2} {2}".format(plot.xyz[i], plot.maxShift[i], plot.unit))
224  if (abs(plot.maxShift[i]) > limit):
225  plot.text.AddText("! {0} shift bigger than {1} {2}".format(plot.xyz[i], limit, plot.unit))
226  else:
227  plot.text.AddText("max. shift {0}: {1:.2}".format(plot.xyz[i], plot.maxShift[i]))
228  if (abs(plot.maxShift[i]) > limit):
229  plot.text.AddText("! {0} shift bigger than {1}".format(plot.xyz[i], limit))
230 
231  if (plot.hiddenEntries[i] != 0):
232  plot.text.AddText("! {0}: {1} outlier !".format(
233  plot.xyz[i], int(plot.hiddenEntries[i])))
234 
235  # save copy
236  plots[modeNumber][structNumber] = plot
237 
238  ######################################################################
239  # make the plots
240  #
241 
242  # show the skewness in the legend
243  gStyle.SetOptStat("emrs")
244 
245  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
246  for structNumber, struct in enumerate(alignables.structures):
247  # use a copy for shorter name
248  plot = plots[modeNumber][structNumber]
249 
250  canvas = TCanvas("canvasModules{0}_{1}".format(
251  struct.get_name(), mode), "Parameter", 300, 0, 800, 600)
252  canvas.Divide(2, 2)
253 
254  canvas.cd(1)
255  plot.title.Draw()
256  plot.text.Draw()
257 
258  # draw identification
259  ident = identification(config)
260  ident.Draw()
261 
262  # is there any plot?
263  plotNumber = 0
264 
265  # loop over coordinates
266  for i in range(3):
267  if(plot.histo[i].GetEntries() > 0):
268  plotNumber += 1
269  canvas.cd(i + 2)
270  setstatsize(canvas, plot.histo[i], config)
271  plot.histo[i].DrawCopy()
272 
273  if (plotNumber == 0):
274  break
275 
276  canvas.Update()
277 
278  # save as pdf
279  canvas.Print(
280  "{0}/plots/pdf/modules_{1}_{2}.pdf".format(config.outputPath, mode, struct.get_name()))
281 
282  # export as png
283  image = TImage.Create()
284  image.FromPad(canvas)
285  image.WriteImage(
286  "{0}/plots/png/modules_{1}_{2}.png".format(config.outputPath, mode, struct.get_name()))
287 
288  # add to output list
289  output = OutputData(plottype="mod", name=struct.get_name(),
290  parameter=mode, filename="modules_{0}_{1}".format(mode, struct.get_name()))
291  config.outputList.append(output)
292 
293  ######################################################################
294  # make plots with substructure
295  #
296 
297  if (config.showsubmodule == 1):
298  alignables.create_children_list()
299  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
300  for structNumber, struct in enumerate(alignables.structures):
301  # use a copy for shorter name
302  plot = plots[modeNumber][structNumber]
303 
304  subModule.plot(MillePedeUser, alignables,
305  mode, struct, plot, config)
def identification
creates the identification text in the top left corner
Definition: style.py:16
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
def plot
Definition: bigModule.py:19
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def setstatsize
statistics size
Definition: style.py:31
def plot
Definition: subModule.py:18
if(dp >Float(M_PI)) dp-