test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions
subModule Namespace Reference

Functions

def plot
 

Function Documentation

def subModule.plot (   MillePedeUser,
  alignables,
  mode,
  struct,
  parentPlot,
  config 
)

Definition at line 18 of file subModule.py.

References funct.abs(), HcalObjRepresent.Fill(), style.identification(), reco.if(), bookConverter.max, and MultipleCompare.Rebin().

Referenced by bigModule.plot().

18 
19 def plot(MillePedeUser, alignables, mode, struct, parentPlot, config):
20  logger = logging.getLogger("mpsvalidate")
21 
22  # skip empty
23  number = 0
24  for i in range(3):
25  if(parentPlot.histo[i].GetEntries() == 0):
26  number += 1
27  if (number == 3):
28  return
29 
30  # number of bins to start
31  numberOfBins = 10000
32 
33  ######################################################################
34  # initialize data hierarchy
35  # plots[subStrucut]
36  #
37 
38  plots = []
39 
40  # initialize histograms
41  for subStructNumber, subStruct in enumerate(struct.get_children()):
42  plots.append(PlotData(mode))
43 
44  # use a copy for shorter name
45  plot = plots[subStructNumber]
46 
47  for i in range(3):
48  if (mode == "xyz"):
49  plot.histo.append(TH1F("{0} {1} {2}".format(struct.get_name() + " " + subStruct.get_name(), plot.xyz[
50  i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -1000, 1000))
51  else:
52  plot.histo.append(TH1F("{0} {1} {2}".format(struct.get_name() + " " + subStruct.get_name(), plot.xyz[
53  i], mode), "Parameter {0}".format(plot.xyz[i]), numberOfBins, -0.1, 0.1))
54 
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].SetYTitle("normalized")
178  parentPlot.histo[i].Draw()
179 
180  for subStructNumber, subStruct in enumerate(struct.get_children()):
181  # use a copy for shorter name
182  plot = plots[subStructNumber].histo[i]
183 
184  plot.SetLineColorAlpha(subStructNumber + 2, 0.5)
185  plot.Draw("same")
186  if (i == 0):
187  legend.AddEntry(plot, subStruct.get_name(), "l")
188 
189  canvas.cd(1)
190 
191  legend.Draw()
192  # draw identification
193  ident = identification(config)
194  ident.Draw()
195 
196  canvas.Update()
197 
198  # save as pdf
199  canvas.Print(
200  "{0}/plots/pdf/subModules_{1}_{2}.pdf".format(config.outputPath, mode, struct.get_name()))
201 
202  # export as png
203  image = TImage.Create()
204  image.FromPad(canvas)
205  image.WriteImage(
206  "{0}/plots/png/subModules_{1}_{2}.png".format(config.outputPath, mode, struct.get_name()))
207 
208  # add to output list
209  output = OutputData(plottype="subMod", name=struct.get_name(), number=subStructNumber + 1,
210  parameter=mode, filename="subModules_{0}_{1}".format(mode, struct.get_name()))
211  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-