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), "", 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  ######################################################################
114  # fill histogram
115  #
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  ######################################################################
137  # find maximum/minimum
138  #
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  ######################################################################
155  # make the plots
156  #
157 
158  # loop over all objids
159  for index, objid in enumerate(objids):
160 
161  canvas = TCanvas("canvasTimeBigStrucutres_{0}_{1}".format(
162  mode, alignables.get_name_by_objid(objid)), "Parameter", 300, 0, 800, 600)
163  canvas.Divide(2, 2)
164 
165  # add text
166  title = TPaveLabel(0.1, 0.8, 0.9, 0.9, "{0} over time {1}".format(
167  alignables.get_name_by_objid(objid), mode))
168 
169  legend = 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 = 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(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, alignables.get_name_by_objid(objid)))
255 
256  # export as png
257  image = TImage.Create()
258  image.FromPad(canvas)
259  image.WriteImage("{0}/plots/png/timeStructures_{1}_{2}.png".format(
260  config.outputPath, mode, alignables.get_name_by_objid(objid)))
261 
262  # add to output list
263  output = OutputData(plottype="time", name=alignables.get_name_by_objid(
264  objid), parameter=mode, filename="timeStructures_{0}_{1}".format(mode, alignables.get_name_by_objid(objid)))
265  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