CMS 3D CMS Logo

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

Functions

def plot
 

Function Documentation

def timeStructure.plot (   treeFile,
  alignables,
  config 
)

Definition at line 18 of file timeStructure.py.

References funct.abs(), any(), SiStripPI.max, sistrip::SpyUtilities.range(), and str.

18 
19 def plot(treeFile, alignables, config):
20  logger = logging.getLogger("mpsvalidate")
21 
22  for mode in ["xyz", "rot"]:
23 
24  time = mpsv_classes.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  obj_names = []
85 
86  # loop over first tree to initialize
87  for line in MillePedeUser[0]:
88  if (line.ObjId != 1 and any(abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
89  plots.append(mpsv_classes.PlotData(mode))
90 
91  # new objid?
92  if (line.ObjId not in objids):
93  objids.append(line.ObjId)
94  obj_names.append(str(line.Name))
95 
96  # initialize histograms
97  for i in range(3):
98  plots[-1].histo.append(ROOT.TH1F("Time Structure {0} {1} {2} {3}".format(mode, str(line.Name),
99  len(plots), i), "", len(listMillePedeUser), 0, len(listMillePedeUser)))
100  plots[-1].label = line.Id
101  plots[-1].objid = line.ObjId
102 
103  if (time.unit!=""):
104  plots[-1].histo[i].SetYTitle("#Delta"+time.xyz[i]+" ["+time.unit+"]")
105  else:
106  plots[-1].histo[i].SetYTitle("#Delta"+time.xyz[i])
107  plots[-1].histo[i].SetXTitle("IOV")
108  plots[-1].histo[i].SetStats(0)
109  plots[-1].histo[i].SetMarkerStyle(21)
110  # bigger labels for the text
111  plots[-1].histo[i].GetXaxis().SetLabelSize(0.08)
112  plots[-1].histo[i].GetYaxis().SetTitleOffset(1.6)
113 
114  ######################################################################
115  # fill histogram
116  #
117 
118  # loop over TTrees
119  for treeNumber, tree in enumerate(MillePedeUser):
120  for line in tree:
121  if (line.ObjId != 1 and any(abs(line.Par[time.data[i]]) != 999999 for i in [0, 1, 2])):
122  # find the right plot
123  for plot in plots:
124  if (plot.label == line.Id):
125  for i in range(3):
126  # note that the first bin is referenced by 1
127  plot.histo[i].GetXaxis().SetBinLabel(
128  treeNumber + 1, str(listMillePedeUser[treeNumber]))
129  # transform xyz data from cm to #mu m
130  if (mode == "xyz"):
131  plot.histo[i].SetBinContent(
132  treeNumber + 1, 10000 * line.Par[plot.data[i]])
133  else:
134  plot.histo[i].SetBinContent(
135  treeNumber + 1, line.Par[plot.data[i]])
136 
137  ######################################################################
138  # find maximum/minimum
139  #
140 
141  maximum = [[0, 0, 0] for x in range(len(objids))]
142  minimum = [[0, 0, 0] for x in range(len(objids))]
143 
144  for index, objid in enumerate(objids):
145  for plot in plots:
146  if (plot.objid == objid):
147  for i in range(3):
148  # maximum
149  if (plot.histo[i].GetMaximum() > maximum[index][i]):
150  maximum[index][i] = plot.histo[i].GetMaximum()
151  # minimum
152  if (plot.histo[i].GetMinimum() < minimum[index][i]):
153  minimum[index][i] = plot.histo[i].GetMinimum()
154 
155  ######################################################################
156  # make the plots
157  #
158 
159  # loop over all objids
160  for index, objid in enumerate(objids):
161 
162  canvas = ROOT.TCanvas("canvasTimeBigStrucutres_{0}_{1}".format(
163  mode, obj_names[index]), "Parameter", 300, 0, 800, 600)
164  canvas.Divide(2, 2)
165 
166  # add text
167  title = ROOT.TPaveLabel(0.1, 0.8, 0.9, 0.9, "{0} over time {1}".format(
168  obj_names[index], mode))
169 
170  legend = ROOT.TLegend(0.05, 0.1, 0.95, 0.75)
171 
172  # draw on canvas
173  canvas.cd(1)
174  title.Draw()
175 
176  # draw identification
177  ident = mpsv_style.identification(config)
178  ident.Draw()
179 
180  # TGraph copies to hide outlier
181  copy = []
182 
183  # reset y range of first plot
184  # two types of ranges
185  for i in range(3):
186  for plot in plots:
187  if (plot.objid == objid):
188  # 1. show all
189  if config.rangemodeHL == "all":
190  plot.usedRange[i] = max(
191  abs(maximum[index][i]), abs(minimum[index][i]))
192 
193  # 2. use given values
194  if (config.rangemodeHL == "given"):
195  # loop over coordinates
196  if mode == "xyz":
197  valuelist = config.rangexyzHL
198  if mode == "rot":
199  valuelist = config.rangerotHL
200  # loop over given values
201  # without last value
202  for value in valuelist:
203  # maximum smaller than given value
204  if max(abs(maximum[index][i]), abs(minimum[index][i])) < value:
205  plot.usedRange[i] = value
206  break
207  # if not possible, force highest
208  if (max(abs(maximum[index][i]), abs(minimum[index][i])) > valuelist[-1]):
209  plot.usedRange[i] = valuelist[-1]
210 
211  # draw plots on canvas
212  for i in range(3):
213  canvas.cd(2 + i)
214 
215  number = 1
216 
217  for plot in plots:
218  if (plot.objid == objid):
219  # all the same range
220  if (config.samerangeHL == 1):
221  plot.usedRange[i] = max(map(abs, plot.usedRange))
222 
223  # set new range
224  plot.histo[i].GetYaxis(
225  ).SetRangeUser(-1.2 * abs(plot.usedRange[i]), 1.2 * abs(plot.usedRange[i]))
226 
227  plot.histo[i].SetLineColorAlpha(number + 2, 0.5)
228  plot.histo[i].SetMarkerColorAlpha(number + 2, 1)
229 
230  # option "AXIS" to only draw the axis
231  plot.histo[i].SetLineColor(0)
232  plot.histo[i].Draw("PSAME")
233 
234  # TGraph object to hide outlier
235  copy.append(ROOT.TGraph(plot.histo[i]))
236  # set the new range
237  copy[-1].SetMaximum(1.2 * abs(plot.usedRange[i]))
238  copy[-1].SetMinimum(-1.2 * abs(plot.usedRange[i]))
239  # draw the data
240  copy[-1].SetLineColorAlpha(number + 2, 0.5)
241  copy[-1].Draw("LPSAME")
242 
243  if (i == 0):
244  legend.AddEntry(
245  plot.histo[i], "{0}".format(number))
246  number += 1
247 
248  canvas.cd(1)
249  legend.Draw()
250 
251  canvas.Update()
252 
253  # save as pdf
254  canvas.Print("{0}/plots/pdf/timeStructures_{1}_{2}.pdf".format(
255  config.outputPath, mode, obj_names[index]))
256 
257  # export as png
258  image = ROOT.TImage.Create()
259  image.FromPad(canvas)
260  image.WriteImage("{0}/plots/png/timeStructures_{1}_{2}.png".format(
261  config.outputPath, mode, obj_names[index]))
262 
263  # add to output list
264  output = mpsv_classes.OutputData(plottype="time", name=obj_names[index],
265  parameter=mode, filename="timeStructures_{0}_{1}".format(mode, obj_names[index]))
266  config.outputList.append(output)
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
const uint16_t range(const Frame &aFrame)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define str(s)