CMS 3D CMS Logo

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