CMS 3D CMS Logo

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