test
CMS 3D CMS Logo

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

Functions

def plot
 

Function Documentation

def timeStructure.plot (   treeFile,
  alignables,
  config 
)

Definition at line 19 of file timeStructure.py.

References funct.abs(), any(), style.identification(), and bookConverter.max.

Referenced by mps_validate.main().

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