test
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), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -1000, 1000))
50  else:
51  plot.histo.append(TH1F("{0} {1} {2}".format(struct.get_name(), plot.xyz[
52  i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -0.1, 0.1))
53 
54  plot.histo[i].SetXTitle(plot.unit)
55  plot.histo[i].GetXaxis().SetTitleOffset(0.85)
56  plot.histoAxis.append(plot.histo[i].GetXaxis())
57 
58  # add labels
59  plot.title = TPaveLabel(
60  0.1, 0.8, 0.9, 0.9, "Module: {0} {1}".format(struct.get_name(), mode))
61  plot.text = TPaveText(0.05, 0.1, 0.95, 0.75)
62  plot.text.SetTextAlign(12)
63  plot.text.SetTextSizePixels(20)
64 
65  # save copy
66  plots[modeNumber][structNumber] = plot
67 
68  ######################################################################
69  # fill histogram
70  #
71 
72  for line in MillePedeUser:
73  # is module ?
74  if (line.ObjId == 1):
75  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
76  for structNumber, struct in enumerate(alignables.structures):
77  # use a copy for shorter name
78  plot = plots[modeNumber][structNumber]
79 
80  # module in struct ?
81  if (struct.contains_detid(line.Id)):
82  for i in range(3):
83  if (abs(line.Par[plot.data[i]]) != 999999):
84  # transform xyz data from cm to #mu m
85  if (mode == "xyz"):
86  plot.histo[i].Fill(
87  10000 * line.Par[plot.data[i]])
88  else:
89  plot.histo[i].Fill(line.Par[plot.data[i]])
90 
91  # save copy
92  plots[modeNumber][structNumber] = plot
93 
94  ######################################################################
95  # find the best range
96  #
97 
98  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
99  for structNumber, struct in enumerate(alignables.structures):
100  # use a copy for shorter name
101  plot = plots[modeNumber][structNumber]
102 
103  for i in range(3):
104  # get first and last bin with content and chose the one which
105  # has a greater distance to the center
106  if (abs(numberOfBins / 2 - plot.histo[i].FindFirstBinAbove()) > abs(plot.histo[i].FindLastBinAbove() - numberOfBins / 2)):
107  plot.maxBinShift[i] = abs(
108  numberOfBins / 2 - plot.histo[i].FindFirstBinAbove())
109  # set the maxShift value
110  plot.maxShift[i] = plot.histo[i].GetBinCenter(
111  plot.histo[i].FindFirstBinAbove())
112  else:
113  plot.maxBinShift[i] = abs(
114  plot.histo[i].FindLastBinAbove() - numberOfBins / 2)
115  # set the maxShift value
116  plot.maxShift[i] = plot.histo[i].GetBinCenter(
117  plot.histo[i].FindLastBinAbove())
118  # skip empty histogram
119  if (abs(plot.maxBinShift[i]) == numberOfBins / 2 + 1):
120  plot.maxBinShift[i] = 0
121 
122  # three types of ranges
123 
124  # 1. multiple of standard dev
125  if (config.rangemode == "stddev"):
126  for i in range(3):
127  if (plot.histo[i].GetEntries() != 0 and plot.histo[i].GetStdDev() != 0):
128  # if the plotrange is much bigger than the standard
129  # deviation use config.widthstdev * StdDev als Range
130  if (max(plot.maxShift) / plot.histo[i].GetStdDev() > config.defpeak):
131  # corresponding bin config.widthstdev*StdDev
132  binShift = int(plot.histo[i].FindBin(
133  config.widthstddev * plot.histo[i].GetStdDev()) - numberOfBins / 2)
134  else:
135  binShift = max(plot.maxBinShift)
136 
137  # save used binShift
138  plot.binShift[i] = binShift
139 
140  # 2. show all
141  if (config.rangemode == "all"):
142  for i in range(3):
143  plot.binShift[i] = plot.maxBinShift[i]
144 
145  # 3. use given ranges
146  if (config.rangemode == "given"):
147  for i in range(3):
148  if (mode == "xyz"):
149  valuelist = config.rangexyzM
150  if (mode == "rot"):
151  valuelist = config.rangerotM
152  if (mode == "dist"):
153  valuelist = config.rangedistM
154 
155  for value in valuelist:
156  # maximum smaller than given value
157  if (abs(plot.maxShift[i]) < value):
158  binShift = value
159  break
160  # if not possible, force highest
161  if (abs(plot.maxShift[i]) > valuelist[-1]):
162  binShift = valuelist[-1]
163  # calculate binShift
164  plot.binShift[i] = int(
165  binShift / plot.histo[i].GetBinWidth(1))
166 
167  # all plot the same range
168  if (config.samerange == 1):
169  for i in range(3):
170  plot.binShift[i] = max(plot.binShift)
171 
172  # save used range
173  for i in range(3):
174  plot.usedRange[i] = plot.binShift[i]
175 
176  # count entries which are not shown anymore
177  for i in range(3):
178  # bin 1 to begin of histogram
179  for j in range(1, numberOfBins / 2 - plot.binShift[i]):
180  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
181  # from the end of shown bins to the end of histogram
182  for j in range(numberOfBins / 2 + plot.binShift[i], plot.histo[i].GetNbinsX()):
183  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
184 
185  # apply new range
186  for i in range(3):
187  if (plot.histo[i].GetEntries() != 0):
188  # merge bins, ca. 100 should be visible in the resulting
189  # plot
190  mergeNumberBins = plot.binShift[i]
191  # skip empty histogram
192  if (mergeNumberBins != 0):
193  # the 2*maxBinShift bins should shrink to 100 bins
194  mergeNumberBins = int(
195  2. * mergeNumberBins / config.numberofbins)
196  # the total number of bins should be dividable by the
197  # bins shrinked together
198  if (mergeNumberBins == 0):
199  mergeNumberBins = 1
200  while (numberOfBins % mergeNumberBins != 0 and mergeNumberBins != 1):
201  mergeNumberBins -= 1
202 
203  # Rebin and save new created histogram and axis
204  plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)
205  plot.histoAxis[i] = plot.histo[i].GetXaxis()
206 
207  # set view range. it is important to note that the number of bins have changed with the rebinning
208  # the total number and the number of shift must be
209  # corrected with / mergeNumberBins
210  plot.histoAxis[i].SetRange(int(numberOfBins / (2 * mergeNumberBins) - plot.binShift[
211  i] / mergeNumberBins), int(numberOfBins / (2 * mergeNumberBins) + plot.binShift[i] / mergeNumberBins))
212 
213  # error if shift is bigger than limit
214  limit = config.limit[mode]
215  for i in range(3):
216  # skip empty
217  if (plot.histo[i].GetEntries() > 0):
218  plot.text.AddText("max. shift {0}: {1:.2}".format(
219  plot.xyz[i], plot.maxShift[i]))
220  if (abs(plot.maxShift[i]) > limit):
221  plot.text.AddText(
222  "! {0} shift bigger than {1} !".format(plot.xyz[i], limit))
223  if (plot.hiddenEntries[i] != 0):
224  plot.text.AddText("! {0} {1} outlier !".format(
225  plot.xyz[i], int(plot.hiddenEntries[i])))
226 
227  # save copy
228  plots[modeNumber][structNumber] = plot
229 
230  ######################################################################
231  # make the plots
232  #
233 
234  # show the skewness in the legend
235  gStyle.SetOptStat("emrs")
236 
237  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
238  for structNumber, struct in enumerate(alignables.structures):
239  # use a copy for shorter name
240  plot = plots[modeNumber][structNumber]
241 
242  canvas = TCanvas("canvasModules{0}_{1}".format(
243  struct.get_name(), mode), "Parameter", 300, 0, 800, 600)
244  canvas.Divide(2, 2)
245 
246  canvas.cd(1)
247  plot.title.Draw()
248  plot.text.Draw()
249 
250  # draw identification
251  ident = identification(config)
252  ident.Draw()
253 
254  # is there any plot?
255  plotNumber = 0
256 
257  # loop over coordinates
258  for i in range(3):
259  if(plot.histo[i].GetEntries() > 0):
260  plotNumber += 1
261  canvas.cd(i + 2)
262  setstatsize(canvas, plot.histo[i], config)
263  plot.histo[i].DrawCopy()
264 
265  if (plotNumber == 0):
266  break
267 
268  canvas.Update()
269 
270  # save as pdf
271  canvas.Print(
272  "{0}/plots/pdf/modules_{1}_{2}.pdf".format(config.outputPath, mode, struct.get_name()))
273 
274  # export as png
275  image = TImage.Create()
276  image.FromPad(canvas)
277  image.WriteImage(
278  "{0}/plots/png/modules_{1}_{2}.png".format(config.outputPath, mode, struct.get_name()))
279 
280  # add to output list
281  output = OutputData(plottype="mod", name=struct.get_name(),
282  parameter=mode, filename="modules_{0}_{1}".format(mode, struct.get_name()))
283  config.outputList.append(output)
284 
285  ######################################################################
286  # make plots with substructure
287  #
288 
289  if (config.showsubmodule == 1):
290  alignables.create_children_list()
291  for modeNumber, mode in enumerate(["xyz", "rot", "dist"]):
292  for structNumber, struct in enumerate(alignables.structures):
293  # use a copy for shorter name
294  plot = plots[modeNumber][structNumber]
295 
296  subModule.plot(MillePedeUser, alignables,
297  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-