CMS 3D CMS Logo

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