CMS 3D CMS Logo

subModule.py
Go to the documentation of this file.
1 ##########################################################################
2 # Creates histograms of the modules of a part of a structure and combines it
3 # with a plot of the modules of the hole structure. Returns a nested
4 # list with the PlotData of the histograms
5 
6 
7 import logging
8 
9 import ROOT
10 ROOT.PyConfig.IgnoreCommandLineOptions = True
11 ROOT.gROOT.SetBatch()
12 
13 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style as mpsv_style
14 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes
15 
16 
17 def plot(MillePedeUser, alignables, mode, struct, parentPlot, config):
18  logger = logging.getLogger("mpsvalidate")
19 
20  # skip empty
21  number = 0
22  for i in range(3):
23  if(parentPlot.histo[i].GetEntries() == 0):
24  number += 1
25  if (number == 3):
26  return
27 
28  # number of bins to start
29  numberOfBins = 10000
30 
31  ######################################################################
32  # initialize data hierarchy
33  # plots[subStrucut]
34  #
35 
36  plots = []
37 
38  # initialize histograms
39  for subStructNumber, subStruct in enumerate(struct.get_children()):
40  plots.append(mpsv_classes.PlotData(mode))
41 
42  # use a copy for shorter name
43  plot = plots[subStructNumber]
44 
45  for i in range(3):
46  if (mode == "xyz"):
47  plot.histo.append(ROOT.TH1F("{0} {1} {2}".format(struct.get_name() + " " + subStruct.get_name(), plot.xyz[
48  i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -1000, 1000))
49  else:
50  plot.histo.append(ROOT.TH1F("{0} {1} {2}".format(struct.get_name() + " " + subStruct.get_name(), plot.xyz[
51  i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -0.1, 0.1))
52 
53  plot.histo[i].SetLineColor(6)
54  plot.histo[i].SetStats(0)
55 
56  # add labels
57  plot.title = ROOT.TPaveLabel(
58  0.1, 0.8, 0.9, 0.9, "Module: {0} {1}".format(struct.get_name(), mode))
59  plot.text = ROOT.TPaveText(0.05, 0.1, 0.95, 0.75)
60  plot.text.SetTextAlign(12)
61  plot.text.SetTextSizePixels(20)
62 
63  # save copy
64  plots[subStructNumber] = plot
65 
66  ######################################################################
67  # fill histogram
68  #
69 
70  for line in MillePedeUser:
71  # is module ?
72  if (line.ObjId == 1):
73  for subStructNumber, subStruct in enumerate(struct.get_children()):
74  # use a copy for shorter name
75  plot = plots[subStructNumber]
76 
77  # module in struct ?
78  if (subStruct.contains_detid(line.Id)):
79  for i in range(3):
80  if (abs(line.Par[plot.data[i]]) != 999999):
81  # transform xyz data from cm to #mu m
82  if (mode == "xyz"):
83  plot.histo[i].Fill(
84  10000 * line.Par[plot.data[i]])
85  else:
86  plot.histo[i].Fill(line.Par[plot.data[i]])
87 
88  # save copy
89  plots[subStructNumber] = plot
90 
91  ######################################################################
92  # find the best range
93  #
94  for subStructNumber, subStruct in enumerate(struct.get_children()):
95  # use a copy for shorter name
96  plot = plots[subStructNumber]
97  for i in range(3):
98  if (plot.histo[i].GetEntries() != 0 and plot.histo[i].GetStdDev() != 0):
99  # use binShift of the hole structure
100  binShift = parentPlot.usedRange[i]
101 
102  # count entries which are not shown anymore
103  # bin 1 to begin of histogram
104  for j in range(1, numberOfBins / 2 - binShift):
105  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
106  # from the end of shown bins to the end of histogram
107  for j in range(numberOfBins / 2 + binShift, plot.histo[i].GetNbinsX()):
108  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
109 
110  # merge bins, ca. 100 should be visible in the resulting plot
111  mergeNumberBins = binShift
112  # skip empty histogram
113  if (mergeNumberBins != 0):
114  # the 2*maxBinShift bins should shrink to 100 bins
115  mergeNumberBins = int(
116  2. * mergeNumberBins / config.numberofbins)
117  # the total number of bins should be dividable by the bins
118  # shrinked together
119  if (mergeNumberBins == 0):
120  mergeNumberBins = 1
121  while (numberOfBins % mergeNumberBins != 0 and mergeNumberBins != 1):
122  mergeNumberBins -= 1
123 
124  # Rebin and save new created histogram and axis
125  plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)
126 
127  # set view range. it is important to note that the number of bins have changed with the rebinning
128  # the total number and the number of shift must be
129  # corrected with / mergeNumberBins
130  plot.histo[i].GetXaxis().SetRange(int(numberOfBins / (2 * mergeNumberBins) - binShift /
131  mergeNumberBins), int(numberOfBins / (2 * mergeNumberBins) + binShift / mergeNumberBins))
132 
133  # save copy
134  plots[subStructNumber] = plot
135 
136  ######################################################################
137  # make the plots
138  #
139 
140  canvas = ROOT.TCanvas("SubStruct_{0}_{1}".format(
141  struct.get_name(), mode), "Parameter", 300, 0, 800, 600)
142  canvas.Divide(2, 2)
143 
144  canvas.cd(1)
145  parentPlot.title.Draw()
146 
147  legend = ROOT.TLegend(0.05, 0.1, 0.95, 0.75)
148 
149  for i in range(3):
150  canvas.cd(i + 2)
151 
152  # find y maximum
153  maximum = []
154 
155  if (parentPlot.histo[i].GetEntries() == 0):
156  continue
157 
158  # normalize parent
159  parentPlot.histo[i].Scale(1. / parentPlot.histo[i].Integral())
160  maximum.append(parentPlot.histo[i].GetMaximum())
161 
162  for subStructNumber, subStruct in enumerate(struct.get_children()):
163  # use a copy for shorter name
164  plot = plots[subStructNumber]
165 
166  if (plot.histo[i].GetEntries() > 0):
167  plot.histo[i].Scale(1. / plot.histo[i].Integral())
168  maximum.append(plot.histo[i].GetMaximum())
169 
170  # save copy
171  plots[subStructNumber] = plot
172 
173  # set range and plot
174  parentPlot.histo[i].GetYaxis().SetRangeUser(0., 1.1 * max(maximum))
175  parentPlot.histo[i].SetYTitle("normalized")
176  parentPlot.histo[i].Draw()
177 
178  for subStructNumber, subStruct in enumerate(struct.get_children()):
179  # use a copy for shorter name
180  plot = plots[subStructNumber].histo[i]
181 
182  plot.SetLineColorAlpha(subStructNumber + 2, 0.5)
183  plot.Draw("same")
184  if (i == 0):
185  legend.AddEntry(plot, subStruct.get_name(), "l")
186 
187  canvas.cd(1)
188 
189  legend.Draw()
190  # draw identification
191  ident = mpsv_style.identification(config)
192  ident.Draw()
193 
194  canvas.Update()
195 
196  # save as pdf
197  canvas.Print(
198  "{0}/plots/pdf/subModules_{1}_{2}.pdf".format(config.outputPath, mode, struct.get_name()))
199 
200  # export as png
201  image = ROOT.TImage.Create()
202  image.FromPad(canvas)
203  image.WriteImage(
204  "{0}/plots/png/subModules_{1}_{2}.png".format(config.outputPath, mode, struct.get_name()))
205 
206  # add to output list
207  output = mpsv_classes.OutputData(plottype="subMod", name=struct.get_name(), number=subStructNumber + 1,
208  parameter=mode, filename="subModules_{0}_{1}".format(mode, struct.get_name()))
209  config.outputList.append(output)
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, mode, struct, parentPlot, config)
Definition: subModule.py:17