test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
timeStructure.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ##########################################################################
4 # Creates a histogram where the the names of the structures are present
5 # as humanreadable text. Multiple MillePedeUser TTrees are used to
6 # get a time dependent plot.
7 ##
8 
9 import logging
10 
11 from ROOT import (TH1F, TCanvas, TGraph, TImage, TLegend, TPaveLabel,
12  TPaveText, TTree, gROOT, gStyle)
13 
14 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes import PedeDumpData, OutputData, PlotData
15 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.geometry import Alignables, Structure
16 from Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style import identification
17 
18 
19 def plot(treeFile, alignables, config):
20  logger = logging.getLogger("mpsvalidate")
21 
22  for mode in ["xyz", "rot"]:
23 
24  time = PlotData(mode)
25 
26  # list of all avaible TTrees
27  listMillePedeUser = []
28  MillePedeUser = []
29  for i in range(config.firsttree, 101):
30  if (treeFile.GetListOfKeys().Contains("MillePedeUser_{0}".format(i))):
31  listMillePedeUser.append(i)
32 
33  # load MillePedeUser_X TTrees
34  for i in listMillePedeUser:
35  MillePedeUser.append(treeFile.Get("MillePedeUser_{0}".format(i)))
36 
37  ######################################################################
38  # remove TTrees without results
39  #
40 
41  # check if there is a TTree without any results
42  # therefor search for the first alignable
43  first = 0
44  newlistMillePedeUser = []
45  # find first alignable
46  for line in MillePedeUser[0]:
47  if (line.ObjId != 1 and any(abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
48  first = line.Id
49  newlistMillePedeUser.append(config.firsttree)
50  break
51 
52  # check the following TTrees
53  for ttreeNumber, ttree in enumerate(MillePedeUser[1:]):
54  for line in ttree:
55  if (line.Id == first):
56  if (any(abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
57  # note that the first tree was checked
58  newlistMillePedeUser.append(
59  ttreeNumber + config.firsttree + 1)
60  break
61 
62  listMillePedeUser = newlistMillePedeUser
63 
64  # reload MillePedeUser_X TTrees
65  MillePedeUser = []
66  for i in listMillePedeUser:
67  MillePedeUser.append(treeFile.Get("MillePedeUser_{0}".format(i)))
68 
69  if not listMillePedeUser:
70  logger.error("Timeplots: no TTrees found")
71  return
72 
73  if not MillePedeUser:
74  logger.error("Timeplots: no TTree could be opened")
75  return
76 
77  ######################################################################
78  # initialize data hierarchy
79  #
80 
81  plots = []
82  # objids which were found in the TTree
83  objids = []
84 
85  # loop over first tree to initialize
86  for line in MillePedeUser[0]:
87  if (line.ObjId != 1 and any(abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
88  plots.append(PlotData(mode))
89 
90  # new objid?
91  if (line.ObjId not in objids):
92  objids.append(line.ObjId)
93 
94  # initialize histograms
95  for i in range(3):
96  plots[-1].histo.append(TH1F("Time Structure {0} {1} {2} {3}".format(mode, alignables.get_name_by_objid(
97  line.ObjId), len(plots), i), "Parameter {0}".format(time.xyz[i]), len(listMillePedeUser), 0, len(listMillePedeUser)))
98  plots[-1].label = line.Id
99  plots[-1].objid = line.ObjId
100 
101  plots[-1].histo[i].SetYTitle(time.unit)
102  plots[-1].histo[i].SetXTitle("IOV")
103  plots[-1].histo[i].SetStats(0)
104  plots[-1].histo[i].SetMarkerStyle(21)
105  # bigger labels for the text
106  plots[-1].histo[i].GetXaxis().SetLabelSize(0.06)
107  plots[-1].histo[i].GetYaxis().SetTitleOffset(1.6)
108 
109  ######################################################################
110  # fill histogram
111  #
112 
113  # loop over TTrees
114  for treeNumber, tree in enumerate(MillePedeUser):
115  for line in tree:
116  if (line.ObjId != 1 and any(abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
117  # find the right plot
118  for plot in plots:
119  if (plot.label == line.Id):
120  for i in range(3):
121  # note that the first bin is referenced by 1
122  plot.histo[i].GetXaxis().SetBinLabel(
123  treeNumber + 1, str(listMillePedeUser[treeNumber]))
124  # transform xyz data from cm to #mu m
125  if (mode == "xyz"):
126  plot.histo[i].SetBinContent(
127  treeNumber + 1, 10000 * line.Par[plot.data[i]])
128  else:
129  plot.histo[i].SetBinContent(
130  treeNumber + 1, line.Par[plot.data[i]])
131 
132  ######################################################################
133  # find maximum/minimum
134  #
135 
136  maximum = [[0, 0, 0] for x in range(len(objids))]
137  minimum = [[0, 0, 0] for x in range(len(objids))]
138 
139  for index, objid in enumerate(objids):
140  for plot in plots:
141  if (plot.objid == objid):
142  for i in range(3):
143  # maximum
144  if (plot.histo[i].GetMaximum() > maximum[index][i]):
145  maximum[index][i] = plot.histo[i].GetMaximum()
146  # minimum
147  if (plot.histo[i].GetMinimum() < minimum[index][i]):
148  minimum[index][i] = plot.histo[i].GetMinimum()
149 
150  ######################################################################
151  # make the plots
152  #
153 
154  # loop over all objids
155  for index, objid in enumerate(objids):
156 
157  canvas = TCanvas("canvasTimeBigStrucutres_{0}_{1}".format(
158  mode, alignables.get_name_by_objid(objid)), "Parameter", 300, 0, 800, 600)
159  canvas.Divide(2, 2)
160 
161  # add text
162  title = TPaveLabel(0.1, 0.8, 0.9, 0.9, "{0} over time {1}".format(
163  alignables.get_name_by_objid(objid), mode))
164 
165  legend = TLegend(0.05, 0.1, 0.95, 0.75)
166 
167  # draw on canvas
168  canvas.cd(1)
169  title.Draw()
170 
171  # draw identification
172  ident = identification(config)
173  ident.Draw()
174 
175  # TGraph copies to hide outlier
176  copy = []
177 
178  # reset y range of first plot
179  # two types of ranges
180  for i in range(3):
181  for plot in plots:
182  if (plot.objid == objid):
183  # 1. show all
184  if (config.rangemodeHL == "all"):
185  plot.usedRange[i] = max(
186  abs(maximum[index][i]), abs(minimum[index][i]))
187 
188  # 2. use given values
189  if (config.rangemodeHL == "given"):
190  # loop over coordinates
191  if (mode == "xyz"):
192  valuelist = config.rangexyzHL
193  if (mode == "rot"):
194  valuelist = config.rangerotHL
195  # loop over given values
196  # without last value
197  for value in valuelist:
198  # maximum smaller than given value
199  if (max(abs(maximum[index][i]), abs(minimum[index][i])) < value):
200  plot.usedRange[i] = value
201  break
202  # if not possible, force highest
203  if (max(abs(maximum[index][i]), abs(minimum[index][i])) > valuelist[-1]):
204  plot.usedRange[i] = valuelist[-1]
205 
206  # draw plots on canvas
207  for i in range(3):
208  canvas.cd(2 + i)
209 
210  number = 1
211 
212  for plot in plots:
213  if (plot.objid == objid):
214  # all the same range
215  if (config.samerangeHL == 1):
216  plot.usedRange[i] = max(map(abs, plot.usedRange))
217 
218  # set new range
219  plot.histo[i].GetYaxis(
220  ).SetRangeUser(-1.2 * abs(plot.usedRange[i]), 1.2 * abs(plot.usedRange[i]))
221 
222  plot.histo[i].SetLineColorAlpha(number + 2, 0.5)
223  plot.histo[i].SetMarkerColorAlpha(number + 2, 1)
224 
225  # option "AXIS" to only draw the axis
226  plot.histo[i].SetLineColor(0)
227  plot.histo[i].Draw("PSAME")
228 
229  # TGraph object to hide outlier
230  copy.append(TGraph(plot.histo[i]))
231  # set the new range
232  copy[-1].SetMaximum(1.2 * abs(plot.usedRange[i]))
233  copy[-1].SetMinimum(-1.2 * abs(plot.usedRange[i]))
234  # draw the data
235  copy[-1].SetLineColorAlpha(number + 2, 0.5)
236  copy[-1].Draw("LPSAME")
237 
238  if (i == 0):
239  legend.AddEntry(
240  plot.histo[i], "{0}".format(number))
241  number += 1
242 
243  canvas.cd(1)
244  legend.Draw()
245 
246  canvas.Update()
247 
248  # save as pdf
249  canvas.Print("{0}/plots/pdf/timeStructures_{1}_{2}.pdf".format(
250  config.outputPath, mode, alignables.get_name_by_objid(objid)))
251 
252  # export as png
253  image = TImage.Create()
254  image.FromPad(canvas)
255  image.WriteImage("{0}/plots/png/timeStructures_{1}_{2}.png".format(
256  config.outputPath, mode, alignables.get_name_by_objid(objid)))
257 
258  # add to output list
259  output = OutputData(plottype="time", name=alignables.get_name_by_objid(
260  objid), parameter=mode, filename="timeStructures_{0}_{1}".format(mode, alignables.get_name_by_objid(objid)))
261  config.outputList.append(output)
def identification
creates the identification text in the top left corner
Definition: style.py:16
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:34
Abs< T >::type abs(const T &t)
Definition: Abs.h:22