test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
subModule.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ##########################################################################
4 # Creates histograms of the modules of a part of a structure and combines it
5 # with a plot of the modules of the hole structure. Returns a nested
6 # list with the PlotData of the histograms
7 ##
8 
9 import logging
10 
11 from ROOT import (TH1F, TCanvas, TImage, TLegend, TPaveLabel, TPaveText, TTree,
12  gROOT, gStyle)
13 
14 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes import PedeDumpData, OutputData, PlotData
15 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style import identification
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(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(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(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].SetXTitle(plot.unit)
55  plot.histo[i].SetLineColor(6)
56  plot.histo[i].SetStats(0)
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[subStructNumber] = plot
67 
68  ######################################################################
69  # fill histogram
70  #
71 
72  for line in MillePedeUser:
73  # is module ?
74  if (line.ObjId == 1):
75  for subStructNumber, subStruct in enumerate(struct.get_children()):
76  # use a copy for shorter name
77  plot = plots[subStructNumber]
78 
79  # module in struct ?
80  if (subStruct.contains_detid(line.Id)):
81  for i in range(3):
82  if (abs(line.Par[plot.data[i]]) != 999999):
83  # transform xyz data from cm to #mu m
84  if (mode == "xyz"):
85  plot.histo[i].Fill(
86  10000 * line.Par[plot.data[i]])
87  else:
88  plot.histo[i].Fill(line.Par[plot.data[i]])
89 
90  # save copy
91  plots[subStructNumber] = plot
92 
93  ######################################################################
94  # find the best range
95  #
96  for subStructNumber, subStruct in enumerate(struct.get_children()):
97  # use a copy for shorter name
98  plot = plots[subStructNumber]
99  for i in range(3):
100  if (plot.histo[i].GetEntries() != 0 and plot.histo[i].GetStdDev() != 0):
101  # use binShift of the hole structure
102  binShift = parentPlot.usedRange[i]
103 
104  # count entries which are not shown anymore
105  # bin 1 to begin of histogram
106  for j in range(1, numberOfBins / 2 - binShift):
107  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
108  # from the end of shown bins to the end of histogram
109  for j in range(numberOfBins / 2 + binShift, plot.histo[i].GetNbinsX()):
110  plot.hiddenEntries[i] += plot.histo[i].GetBinContent(j)
111 
112  # merge bins, ca. 100 should be visible in the resulting plot
113  mergeNumberBins = binShift
114  # skip empty histogram
115  if (mergeNumberBins != 0):
116  # the 2*maxBinShift bins should shrink to 100 bins
117  mergeNumberBins = int(
118  2. * mergeNumberBins / config.numberofbins)
119  # the total number of bins should be dividable by the bins
120  # shrinked together
121  if (mergeNumberBins == 0):
122  mergeNumberBins = 1
123  while (numberOfBins % mergeNumberBins != 0 and mergeNumberBins != 1):
124  mergeNumberBins -= 1
125 
126  # Rebin and save new created histogram and axis
127  plot.histo[i] = plot.histo[i].Rebin(mergeNumberBins)
128 
129  # set view range. it is important to note that the number of bins have changed with the rebinning
130  # the total number and the number of shift must be
131  # corrected with / mergeNumberBins
132  plot.histo[i].GetXaxis().SetRange(int(numberOfBins / (2 * mergeNumberBins) - binShift /
133  mergeNumberBins), int(numberOfBins / (2 * mergeNumberBins) + binShift / mergeNumberBins))
134 
135  # save copy
136  plots[subStructNumber] = plot
137 
138  ######################################################################
139  # make the plots
140  #
141 
142  canvas = TCanvas("SubStruct_{0}_{1}".format(
143  struct.get_name(), mode), "Parameter", 300, 0, 800, 600)
144  canvas.Divide(2, 2)
145 
146  canvas.cd(1)
147  parentPlot.title.Draw()
148 
149  legend = TLegend(0.05, 0.1, 0.95, 0.75)
150 
151  for i in range(3):
152  canvas.cd(i + 2)
153 
154  # find y maximum
155  maximum = []
156 
157  if (parentPlot.histo[i].GetEntries() == 0):
158  continue
159 
160  # normalize parent
161  parentPlot.histo[i].Scale(1. / parentPlot.histo[i].Integral())
162  maximum.append(parentPlot.histo[i].GetMaximum())
163 
164  for subStructNumber, subStruct in enumerate(struct.get_children()):
165  # use a copy for shorter name
166  plot = plots[subStructNumber]
167 
168  if (plot.histo[i].GetEntries() > 0):
169  plot.histo[i].Scale(1. / plot.histo[i].Integral())
170  maximum.append(plot.histo[i].GetMaximum())
171 
172  # save copy
173  plots[subStructNumber] = plot
174 
175  # set range and plot
176  parentPlot.histo[i].GetYaxis().SetRangeUser(0., 1.1 * max(maximum))
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 = 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 = 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 = 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)
def identification
creates the identification text in the top left corner
Definition: style.py:16
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def plot
Definition: subModule.py:18
if(dp >Float(M_PI)) dp-