CMS 3D CMS Logo

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