ATLAS Offline Software
TileDCSDataPlotter.py
Go to the documentation of this file.
1 #!/bin/env python
2 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 # Author: nils.gollub@cern.ch
4 
5 from __future__ import print_function
6 
7 import sys, os
8 import time, datetime
9 import ROOT
10 import logging
11 from ROOT import TCanvas, TH1D, TH2D, TArrayD, TLegend
12 from TileCoolDcs.TileDCSDataGrabber import TileDCSDataGrabber
13 from TileCoolDcs.ProgressBar import progressBar
14 
15 
17 
18  #_______________________________________________________________________________________
19  def __init__( self, argv, useCool, useTestBeam, verbose, dbstring=None, putDuplicates=False, hvonly=False ):
20 
21  dbSource = "ORACLE"
22  self.useCool = useCool or hvonly
23  if useCool:
24  dbSource = "COOL"
25  self.useTestBeam = useTestBeam
26  if useTestBeam:
27  dbSource = "TESTBEAM"
28  logLvl = logging.WARNING
29  if verbose:
30  logLvl = logging.DEBUG
31 
32 # self.dataGrabber = TileDCSDataGrabber(dbSource, logLvl, dbstring, putDuplicates)
33 # self.info = self.dataGrabber.info
34 
35  self.cmd = argv[1]
36  self.drawer = argv[2]
37  self.varExp = argv[3]
38  beg = argv[4]
39  end = argv[5]
40  self.iovBeg = int(time.mktime(time.strptime(beg,"%Y-%m-%d %H:%M:%S")))
41  self.iovEnd = int(time.mktime(time.strptime(end,"%Y-%m-%d %H:%M:%S")))
42 
43  self.dataGrabber = TileDCSDataGrabber(dbSource, logLvl, dbstring, putDuplicates, self.iovBeg)
44  self.info = self.dataGrabber.info
45 
46  self.cutExp = ""
47  if len(argv) >6:
48  self.cutExp = argv[6]
49  beg = beg.replace(' ','_')
50  end = end.replace(' ','_')
51  self.outName = self.drawer+"_"+self.varExp[:100]+"__"+beg+"__"+end
52  self.outName = self.outName.replace('/',':')
53  self.outName = self.outName.replace('(','0')
54  self.outName = self.outName.replace(')','0')
55  if len(argv) >7:
56  self.outName = argv[7]
57  print ("---> OUTNAME: " , self.outName)
58 
59  timeBegInfo = time.localtime(self.iovBeg)
60  timeEndInfo = time.localtime(self.iovEnd)
61  self.rangeStr = "FROM: "+time.asctime(timeBegInfo)+" UNTIL: "+time.asctime(timeEndInfo)
62 
63  part=["LBA","LBC","EBA","EBC"]
64  if self.drawer == "ALL":
65  self.drawer=""
66  for p in range(4):
67  for m in range(64):
68  self.drawer+="%s%2.2d," % (part[p],m+1)
69  self.drawer=self.drawer[:-1]
70  elif self.drawer in part:
71  p=self.drawer
72  self.drawer=""
73  for m in range(64):
74  self.drawer+="%s%2.2d," % (p,m+1)
75  self.drawer=self.drawer[:-1]
76 
77 
78  #_______________________________________________________________________________________
79  def parseVarExpression(self, varExpression):
80 
81  knownVars = self.info.get_all_variables()
82  varDict = {}
83  for var in knownVars:
84  pos = varExpression.find(var)
85  if pos>-1:
86  oldVar=""
87  if pos in varDict:
88  #=== other variable found at same place?
89  #=== -> choose the longer one
90  oldVar = varDict[pos]
91  if len(oldVar) > len(var):
92  vtmp = var
93  var = oldVar
94  oldVar = vtmp
95  varDict[pos] = var
96  if oldVar!="":
97  #=== check if shorter variable is also present at another place
98  varExpr = varExpression
99  for v in list(varDict.values()):
100  varExpr=varExpr.replace(v," "*len(v))
101  oldPos = varExpr.find(oldVar)
102  if oldPos>-1:
103  varDict[oldPos] = oldVar
104 
105  #=== check for overlap
106  positions = sorted(varDict.keys())
107  posLength = len(positions)
108  if posLength > 1:
109  iPos = 1
110  while iPos < posLength:
111  if positions[iPos] < positions[iPos-1]+len(varDict[positions[iPos-1]]):
112  varDict.pop(positions[iPos])
113  positions.pop(iPos)
114  posLength=len(positions)
115  else:
116  iPos+=1
117 
118  varList = list(varDict.values())
119  if "ALL_LVPS_AI" in varExpression:
120  varList.extend(list(self.info.vars_LVPS_AI.keys()))
121  if "ALL_LVPS_STATES" in varExpression:
122  varList.extend(list(self.info.vars_LVPS_STATES.keys()))
123  if "ALL_HVSET" in varExpression or "ALL_SETHV" in varExpression:
124  varList.extend(list(self.info.vars_HVSET.keys()))
125  if self.useCool or ("ALL_HV-ALL_SETHV" in varExpression or "ALL_HV-ALL_HVSET" in varExpression):
126  for i in range(2):
127  if "Set.vFix1%d" % (i+1) in varList:
128  varList.remove("Set.vFix1%d" % (i+1))
129  for i in range(4):
130  varList.remove("Set.hvIn%d" % (i+1))
131  for i in range(7):
132  varList.remove("Set.volt%d" % (i+1))
133  for i in range(7):
134  varList.remove("Set.temp%d" % (i+1))
135  if ("ALL_HV" in varExpression and "ALL_HVSET" not in varExpression) or "ALL_HV-ALL_HVSET" in varExpression:
136  varList.extend(list(self.info.vars_HV.keys()))
137  if self.useCool or ("ALL_HV-ALL_SETHV" in varExpression or "ALL_HV-ALL_HVSET" in varExpression):
138  for i in range(4):
139  varList.remove("hvIn%d" % (i+1))
140  for i in range(7):
141  varList.remove("volt%d" % (i+1))
142  for i in range(7):
143  varList.remove("temp%d" % (i+1))
144 
145  return list(set(varList))
146 
147 
148  #_______________________________________________________________________________________
149  def getTree(self, drawer=None, var=None,lastOnly=False,firstAndLast=False):
150 
151  #=== parse for variables needed
152  varList = self.parseVarExpression(self.varExp+" "+self.cutExp)
153 
154  #=== get the variable
155  if drawer is None:
156  t = self.dataGrabber.getDCSTree(self.iovBeg, self.iovEnd, self.drawer.split(","), varList, lastOnly, firstAndLast)
157  elif var is None:
158  drl=[drawer]
159  t = self.dataGrabber.getDCSTree(self.iovBeg, self.iovEnd, drl, varList, lastOnly, firstAndLast)
160  else:
161  drl=[drawer]
162  varl=[var]
163  t = self.dataGrabber.getDCSTree(self.iovBeg, self.iovEnd, drl, varl, lastOnly, firstAndLast)
164 
165  if t.GetEntries()==0:
166  print ("ERROR: No data in time interval!")
167  sys.exit(1)
168  else:
169  print ("Found number of entries: ", t.GetEntries())
170 
171  #=== append drawer to all variables
172  if self.cmd!="tree" and drawer is None and var is None:
173  for var in varList:
174  if "," in self.drawer:
175  newvar=""
176  newcut=""
177  if var in self.cutExp:
178  cut=self.cutExp
179  else:
180  cut=None
181  for dr in self.drawer.split(","):
182  newvar += dr+"."+var+","
183  if cut:
184  newcut += dr+"."+cut+","
185  self.varExp = self.varExp.replace(var,newvar[:-1])
186  if cut:
187  self.cutExp = newcut[:-1]
188  self.varExp = self.varExp.replace("Set."+newvar[:-1],"Set."+var)
189  if cut and "Set." in cut and "Set." not in var:
190  self.cutExp=cut
191  else:
192  self.varExp = self.varExp.replace(var,self.drawer+"."+var)
193  self.cutExp = self.cutExp.replace(var,self.drawer+"."+var)
194  self.varExp = self.varExp.replace(self.drawer+"."+self.drawer,self.drawer)
195  self.cutExp = self.cutExp.replace(self.drawer+"."+self.drawer,self.drawer)
196  self.varExp = self.varExp.replace("Set."+self.drawer,"Set")
197  self.cutExp = self.cutExp.replace("Set."+self.drawer,"Set")
198  if "ALL_LVPS_AI" in self.varExp:
199  newvar=""
200  for dr in self.drawer.split(","):
201  for var in list(self.info.vars_LVPS_AI.keys()):
202  newvar += dr+"."+var+","
203  self.varExp = self.varExp.replace("ALL_LVPS_AI",newvar[:-1])
204 
205  if "ALL_LVPS_STATES" in self.varExp:
206  newvar=""
207  for dr in self.drawer.split(","):
208  for var in list(self.info.vars_LVPS_STATES.keys()):
209  newvar += dr+"."+var+","
210  self.varExp = self.varExp.replace("ALL_LVPS_STATES",newvar[:-1])
211 
212  if "ALL_HV-ALL_SETHV" in self.varExp or "ALL_HV-ALL_HVSET" in self.varExp:
213  newvar=""
214  for dr in self.drawer.split(","):
215  for var in list(self.info.vars_HVSET.keys()):
216  if not ("Set.vFix" in var or "Set.hvIn" in var or "Set.volt" in var or "Set.temp" in var):
217  newvar += dr+"."+var.replace("Set.","")+"-"+dr+"."+var+","
218  self.varExp = self.varExp.replace("ALL_HV-ALL_SETHV",newvar[:-1])
219  self.varExp = self.varExp.replace("ALL_HV-ALL_HVSET",newvar[:-1])
220  if "ALL_HVSET" in self.varExp or "ALL_SETHV" in self.varExp:
221  newvar=""
222  for dr in self.drawer.split(","):
223  for var in list(self.info.vars_HVSET.keys()):
224  if not self.useCool or not ("Set.vFix" in var or "Set.hvIn" in var or "Set.volt" in var or "Set.temp" in var):
225  newvar += dr+"."+var+","
226  self.varExp = self.varExp.replace("ALL_HVSET",newvar[:-1])
227  self.varExp = self.varExp.replace("ALL_SETHV",newvar[:-1])
228  if "ALL_HV" in self.varExp and "ALL_HVSET" not in self.varExp :
229  newvar=""
230  for dr in self.drawer.split(","):
231  for var in list(self.info.vars_HV.keys()):
232  if not self.useCool or not ("hvIn" in var or "volt" in var or "temp" in var):
233  newvar += dr+"."+var+","
234  self.varExp = self.varExp.replace("ALL_HV",newvar[:-1])
235 
236  print ("self.drawer: ", self.drawer)
237  print ("self.varExp: ", self.varExp)
238  print ("self.cutExp: ", self.cutExp)
239 
240  return t
241 
242 
243  #_______________________________________________________________________________________
244  def cutReplace(self, incut, var):
245  """
246  Relace "ALL" keywords in cut variable by appropriate names from var variable
247 
248  """
249 
250  cut = incut
251  for vv in var.split("-"):
252  if "hvOut" in vv:
253  if "Set" in vv:
254  cut = cut.replace("ALL_SETHV",vv)
255  cut = cut.replace("ALL_HVSET",vv)
256  vvv=vv.replace("Set.hv","hv")
257  cut = cut.replace("ALL_HV",vvv)
258  else:
259  vvv=vv.replace("hv","Set.hv")
260  cut = cut.replace("ALL_SETHV",vvv)
261  cut = cut.replace("ALL_HVSET",vvv)
262  cut = cut.replace("ALL_HV",vv)
263  cut = cut.replace("ALL",var)
264  return cut
265 
266 
267  #_______________________________________________________________________________________
268  def scan(self):
269  """
270  Scan ROOT TTree and show all values on the screen
271 
272  """
273 
274  t = self.getTree()
275  if "ALL" in self.cutExp or self.varExp.count(',')>2 :
276  #=== extract the values
277  for var in self.varExp.split(","):
278  treevar="EvTime:"+var
279  cut=self.cutReplace(self.cutExp,var)
280  self.scantree(t,treevar,cut)
281  else:
282  treevar="EvTime:"+self.varExp.replace(",",":")
283  self.scantree(t,treevar,self.cutExp)
284 
285  return
286 
287 
288  #_______________________________________________________________________________________
289  def scantree(self,t,treevar,cut):
290  """
291  Scan ROOT TTree and show up to 4 variables on the screen
292 
293  """
294 
295  #t.Scan("-1000000000+"+treevar,cut)
296  t.Draw(treevar,cut,"goff")
297  n = t.GetSelectedRows()
298  if n>0: # at least one point passed the cut
299  vars = treevar.split(":")
300  nv = len(vars)
301  fmt = [ "%d" ]
302  for i in range(1,nv):
303  if "DAQ" in vars[i]:
304  fmt += [ "\t%6d\t\t" ]
305  else:
306  fmt += [ "\t%6.2f\t" ]
307  x = t.GetV1()
308  v1 = None
309  v2 = None
310  v3 = None
311  v=vars[0]
312  if v=="EvTime": v += 13*" "
313  if nv>1:
314  v1 = t.GetV2()
315  v += "\t"+vars[1]
316  if nv>2:
317  v2 = t.GetV3()
318  v += "\t"+vars[2]
319  if nv>3:
320  v3 = t.GetV4()
321  v += "\t"+vars[3]
322  print(v)
323  for i in range(n):
324  v=""
325  if v1 is not None:
326  v += fmt[1] % v1[i]
327  if v2 is not None:
328  v += fmt[2] % v2[i]
329  if v3 is not None:
330  v += fmt[3] % v3[i]
331  dt = datetime.datetime.fromtimestamp(int(x[i])).strftime('%Y-%m-%d %H:%M:%S')
332  print(dt,v)
333  return
334 
335 
336  #_______________________________________________________________________________________
337  def getTimelinePlot(self):
338  """
339  Returns a TCanvas with the variable plotted vs time.
340 
341  """
342 
343  ROOT.gStyle.SetOptStat(0)
344  t = self.getTree()
345 
346  if t.GetEntries()==1:
347  print ("ERROR: Only one data point, need at least 2")
348  print ("---> Try increasing the time span")
349  sys.exit(1)
350 
351  #=== extract the values
352  hh=[]
353  ih=0
354  hmin=1000000000
355  hmax=-1000000000
356  color = [2,4,3,5,6,7]
357  for var in self.varExp.split(","):
358  cut=self.cutReplace(self.cutExp,var)
359  #print (var)
360  #print (cut)
361  t.Draw(var+":EvTime",cut,"goff")
362  n = t.GetSelectedRows()
363  if len(cut)>0:
364  print ("var=",var," \tcut=",cut," \tnpoints=",n)
365  else:
366  print ("var=",var," \tnpoints=",n)
367  if n>0: # at least one point passed the cut
368  x1 = t.GetV2()
369  y1 = t.GetV1()
370  x = TArrayD(n)
371  y = TArrayD(n)
372  for i in range(n):
373  x[i] = x1[i]
374  y[i] = y1[i]
375 
376  t.Draw(var+":EvTime","","goff") # the same but without cut
377  n0 = t.GetSelectedRows()
378  x0 = t.GetV2()
379 
380  #=== fix for **** root time convention and add end time
381  offset = 788918400 # = 1.1.1995(UTC), the root offset
382  xarr = TArrayD(n0+1)
383  for i in range(n0):
384  xarr[i] = x0[i]-offset
385  xarr[n0] = self.iovEnd-offset
386  #=== create and fill histogram with values
387  title = var
388  h = TH1D("hDCS"+str(ih),title,n0,xarr.GetArray())
389  for i in range(n):
390  h.Fill(x[i]-offset,y[i])
391  if y[i]>hmax:
392  hmax=y[i]
393  if y[i]<hmin:
394  hmin=y[i]
395 
396  #=== set time display
397  sec_min = 60
398  sec_hrs = sec_min*60
399  sec_day = sec_hrs*24
400  sec_mon = sec_day*30
401  timeDiff = self.iovEnd-self.iovBeg
402  h.GetXaxis().SetTimeFormat("%S")
403  h.SetXTitle("time [seconds]")
404  if timeDiff > 12*sec_mon:
405  h.GetXaxis().SetTimeFormat("%m/%y")
406  h.SetXTitle("time [month/year]")
407  #elif timeDiff >3.33448*sec_mon:
408  elif timeDiff > 6*sec_day:
409  h.GetXaxis().SetTimeFormat("%d/%m")
410  h.SetXTitle("time [day/month]")
411  elif timeDiff > 2*sec_day:
412  h.GetXaxis().SetTimeFormat("%d(%H)")
413  h.SetXTitle("time [day(hour)]")
414  elif timeDiff > 2*sec_hrs:
415  h.GetXaxis().SetTimeFormat("%H:%M")
416  h.SetXTitle("time [hour:min]")
417  elif timeDiff > 2*sec_min:
418  h.GetXaxis().SetTimeFormat("%M")
419  h.SetXTitle("time [min]")
420  h.GetXaxis().SetTimeDisplay(1)
421  h.SetLineColor(color[ih%len(color)])
422 
423  ih+=1
424  hh+=[h]
425 
426  if len(hh)>1:
427  delta=(hmax-hmin)/20.
428  if delta<=0:
429  delta=0.5
430  hmin-=delta
431  hmax+=delta
432  for h in hh:
433  h.SetMaximum(hmax)
434  h.SetMinimum(hmin)
435 
436  return hh
437 
438 
439  #_______________________________________________________________________________________
440  def getDiffPlot(self,opt):
441  """
442  Returns a TCanvas with 2D plot showing difference between last and first entries.
443 
444  """
445 
446  ROOT.gStyle.SetOptStat(0)
447  tree = self.getTree(firstAndLast=(opt==1))
448 
449  if tree.GetEntries()==1:
450  print ("ERROR: Only one data point, need at least 2")
451  print ("---> Try increasing the time span")
452  sys.exit(1)
453 
454  if len(self.cutExp) and "ALL" not in self.cutExp:
455  print ("Cut expression is ignored in diff plot")
456 
457  #=== extract the values
458  leaves=tree.GetListOfLeaves()
459  xaxis=[]
460  yaxis=[]
461  for leaf in leaves:
462  z=leaf.GetTitle().split('.')
463  if len(z)==2 or (len(z)==3 and z[1]=="Set"):
464  x=z[0]
465  y=z[1]
466  if len(z)==3:
467  y+= "."+z[2]
468  if not y[-2].isdigit() and y[-1].isdigit():
469  y=y[:-2]+'0'+y[-1]
470  if x not in xaxis:
471  xaxis+=[x]
472  if y not in yaxis:
473  yaxis+=[y]
474  xaxis.sort()
475  yaxis.sort()
476  nx=len(xaxis)
477  ny=len(yaxis)
478  if opt==1:
479  pref="Diff "
480  elif opt==2:
481  pref="Diff_Prof "
482  elif opt==3:
483  pref="Prof "
484  else:
485  pref=""
486  if opt==1:
487  hist=ROOT.TH2F("hist2D",pref+self.outName,nx,-0.5,nx-0.5,ny,-0.5,ny-0.5)
488  else:
489  hist=ROOT.TProfile2D("hist2D",pref+self.outName,nx,-0.5,nx-0.5,ny,-0.5,ny-0.5)
490 
491  XA=hist.GetXaxis()
492  n=0
493  for i in range(nx):
494  if n:
495  XA.SetBinLabel(i + 1, '')
496  else:
497  XA.SetBinLabel(i + 1, xaxis[i])
498  n=1-n
499 
500  YA=hist.GetYaxis()
501  for i in range(ny):
502  YA.SetBinLabel(i + 1, yaxis[i])
503 
504  tree.GetEntry(0)
505  val = {}
506  for leaf in leaves:
507  if opt>2:
508  val[leaf.GetTitle()] = 0
509  else:
510  val[leaf.GetTitle()] = leaf.GetValue()
511 
512  ne=tree.GetEntries()
513  if opt==1:
514  n1=ne-1
515  elif opt==2:
516  n1=1
517  else:
518  n1=0
519  bar = progressBar(n1,ne, 78)
520  for n in range(n1,ne):
521  tree.GetEntry(n)
522  bar.update(n)
523  for leaf in leaves:
524  v1=val[leaf.GetTitle()]
525  z=leaf.GetTitle().split('.')
526  if len(z)==2 or (len(z)==3 and z[1]=="Set"):
527  x=z[0]
528  y=z[1]
529  if len(z)==3:
530  y+= "."+z[2]
531  if not y[-2].isdigit() and y[-1].isdigit():
532  y=y[:-2]+'0'+y[-1]
533  v2=leaf.GetValue()
534  if "ALL" in self.cutExp:
535  var=str(v2)
536  vardiff=str(v2-v1)
537  cut = self.cutExp.replace("ALL_DIFF",vardiff)
538  cut = self.cutReplace(cut,var)
539  ok = eval(cut)
540  #print (cut)
541  #print (ok)
542  else:
543  ok = True
544  if ok:
545  hist.Fill(xaxis.index(x),yaxis.index(y),v2-v1)
546  bar.done()
547 
548  return hist
549 
550 
551  #_______________________________________________________________________________________
553  """
554  Returns a TCanvas with the variable distribution plotted (using weights)
555 
556  """
557 
558  t = self.getTree()
559 
560  hh=[]
561  ih=0
562  color = [2,4,3,5,6,7]
563  xmin=1000000000
564  xmax=-1000000000
565  ymin=1000000000
566  ymax=-1000000000
567  hmin=1000000000
568  hmax=-1000000000
569  dim=0
570  xtit=""
571  ytit=""
572  for var in sorted(self.varExp.split(",")):
573  cut=self.cutExp
574  dim = var.count(':')
575  if dim>0:
576  varX = var.split(":")[1]
577  varY = var.split(":")[0]
578  if len(cut):
579  cutY = self.cutReplace(cut,varY)
580  cut = self.cutReplace(cut,varX)
581  cut = "("+cut+") && ("+cutY+")"
582  else:
583  varX=var
584  varY=""
585  if len(cut):
586  cut = self.cutReplace(cut,varX)
587  if cut!="":
588  cut = "weight*("+cut+")"
589  else:
590  cut = "weight"
591  #print (var)
592  #print (cut)
593 
594  t.Draw(var,cut,"goff")
595  h = ROOT.gDirectory.Get("htemp")
596  h.SetXTitle(var.split(":")[0])
597  h.SetTitle(self.rangeStr)
598  h.SetName("TileDCSDataPlotter"+str(ih))
599  #print ("n=",h.GetEntries())
600 
601  if h.GetEntries()>0:
602  xmi = h.GetXaxis().GetBinLowEdge(1)
603  xma = h.GetXaxis().GetBinLowEdge(h.GetNbinsX())+h.GetXaxis().GetBinWidth(h.GetNbinsX())
604  if xmi<xmin:
605  xmin=xmi
606  if xma>xmax:
607  xmax=xma
608  if dim>0:
609  ymi = h.GetYaxis().GetBinLowEdge(1)
610  yma = h.GetYaxis().GetBinLowEdge(h.GetNbinsY())+h.GetYaxis().GetBinWidth(h.GetNbinsY())
611  if ymi<ymin:
612  ymin=ymi
613  if yma>ymax:
614  ymax=yma
615  hmi = h.GetMinimum()
616  hma = h.GetMaximum()
617  if hmi<hmin:
618  hmin=hmi
619  if hma>hmax:
620  hmax=hma
621  xtit+=varX+","
622  if dim>0:
623  ytit+=varY+","
624 
625  h.SetLineColor(color[ih%len(color)])
626  h.SetMarkerColor(color[ih%len(color)])
627 
628  ih+=1
629  hh+=[h]
630 
631  if len(hh)>1:
632  if xmin>xmax:
633  xmin=0
634  xmax=0
635  delta=(xmax-xmin)/20.
636  if delta<=0:
637  delta=0.5
638  xmin-=delta
639  xmax+=delta
640  if ymin>ymax:
641  ymin=0
642  ymax=0
643  delta=(ymax-ymin)/20.
644  if delta<=0:
645  delta=0.5
646  ymin-=delta
647  ymax+=delta
648  if hmin>hmax:
649  hmin=0
650  hmax=1
651  delta=(hmax-hmin)/20.
652  if delta<=0:
653  delta=0.5
654  if hmin!=0:
655  hmin-=delta
656  hmax+=delta
657  if dim>0:
658  h0 = TH2D("hDCS","dummy",2,xmin,xmax,2,ymin,ymax)
659  h0.SetYTitle(ytit[:-1])
660  else:
661  h0 = TH1D("hDCS","dummy",2,xmin,xmax)
662  h0.SetXTitle(xtit[:-1])
663  h0.SetTitle(self.rangeStr)
664  h0.SetMaximum(hmax)
665  if hmin!=0:
666  h0.SetMinimum(hmin)
667  hh=[h0]+hh
668  ROOT.gStyle.SetOptStat(0)
669 
670  return hh
671 
672 
673  #_______________________________________________________________________________________
675  """
676  Print mean values from histograms
677 
678  """
679 
680  t = self.getTree()
681  if self.cutExp!="":
682  for v, c in zip(self.varExp.split(","), self.cutExp.split(",")):
683  t.Draw(v,c,"goff")
684  h = ROOT.gDirectory.Get("htemp")
685 
686  print ("%s %s %s Nentries %d Mean %7.3f RMS %8.4f" % \
687  (v,c,self.rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
688  else:
689  cut = ""
690  for v in self.varExp.split(","):
691  t.Draw(v,cut,"goff")
692  h = ROOT.gDirectory.Get("htemp")
693  print ("%s %s Nentries %d Mean %7.3f RMS %8.4f" % \
694  (v,self.rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
695 
696  return 0
697 
698 
699  #_______________________________________________________________________________________
700  def printDistributionStat(self, lastOnly=False):
701  """
702  Print mean values from histograms
703 
704  """
705 
706  if "ALL_LVPS_AI" in self.varExp:
707  self.varExp = self.varExp.replace("ALL_LVPS_AI",",".join(sorted(self.info.vars_LVPS_AI.keys())))
708  if "ALL_LVPS_STATES" in self.varExp:
709  self.varExp = self.varExp.replace("ALL_LVPS_STATES",",".join(sorted(self.info.vars_LVPS_STATES.keys())))
710  if "ALL_HV-ALL_SETHV" in self.varExp or "ALL_HV-ALL_HVSET" in self.varExp:
711  vlist = []
712  for var in list(self.info.vars_HVSET.keys()):
713  if not ("Set.vFix" in var or "Set.hvIn" in var or "Set.volt" in var or "Set.temp" in var):
714  vlist += [var.replace("Set.","")+"-"+var]
715  self.varExp = self.varExp.replace("ALL_HV-ALL_SETHV",",".join(sorted(vlist)))
716  self.varExp = self.varExp.replace("ALL_HV-ALL_HVSET",",".join(sorted(vlist)))
717  if "ALL_HVSET" in self.varExp or "ALL_SETHV" in self.varExp:
718  vlist = list(self.info.vars_HVSET.keys())
719  if self.useCool:
720  for i in range(2):
721  if "Set.vFix1%d" % (i+1) in vlist:
722  vlist.remove("Set.vFix1%d" % (i+1))
723  for i in range(4):
724  vlist.remove("Set.hvIn%d" % (i+1))
725  for i in range(7):
726  vlist.remove("Set.volt%d" % (i+1))
727  for i in range(7):
728  vlist.remove("Set.temp%d" % (i+1))
729  self.varExp = self.varExp.replace("ALL_HVSET",",".join(sorted(vlist)))
730  self.varExp = self.varExp.replace("ALL_SETHV",",".join(sorted(vlist)))
731  if "ALL_HV" in self.varExp and "ALL_HVSET" not in self.varExp:
732  vlist = list(self.info.vars_HV.keys())
733  if self.useCool:
734  for i in range(4):
735  vlist.remove("hvIn%d" % (i+1))
736  for i in range(7):
737  vlist.remove("volt%d" % (i+1))
738  for i in range(7):
739  vlist.remove("temp%d" % (i+1))
740  self.varExp = self.varExp.replace("ALL_HV",",".join(sorted(vlist)))
741  varlist=self.parseVarExpression(self.varExp)
742  print ("var list is",varlist)
743 
744  reslist=[]
745  cutlist = self.parseVarExpression(self.cutExp)
746  print ("cut list is",cutlist)
747  fulllist=sorted(list(set(varlist+cutlist)))
748  print ("full list is",fulllist)
749  t = None
750  drlistprev=[]
751  varlistprev=[]
752  for drawer in self.drawer.split(","):
753  drlist=[drawer]
754  for v in self.varExp.split(","):
755  varlist=self.parseVarExpression(v)
756  varlist += cutlist
757  varlist=sorted(list(set(varlist)))
758  #for drawer in self.drawer.split(","):
759  # drlist=[drawer]
760  if not (t and drlist==drlistprev and varlist==varlistprev):
761  t = self.dataGrabber.getDCSTree(self.iovBeg, self.iovEnd, drlist, varlist, lastOnly)
762  drlistprev=drlist
763  varlistprev=varlist
764  if t and t.GetEntries()>0:
765  var = v
766  cut = self.cutReplace(self.cutExp,var)
767  for va in varlist:
768  var = var.replace(va,drawer+"."+va)
769  cut = cut.replace(va,drawer+"."+va)
770  var = var.replace(drawer+"."+drawer,drawer)
771  cut = cut.replace(drawer+"."+drawer,drawer)
772  var = var.replace("Set."+drawer,"Set")
773  cut = cut.replace("Set."+drawer,"Set")
774  print ("var is",var)
775  print ("cut is",cut)
776  t.Draw(var,cut,"goff")
777  h = ROOT.gDirectory.Get("htemp")
778  if h:
779  if cut!="":
780  reslist.append((var+" CUT "+cut,self.rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
781  else:
782  reslist.append((var,self.rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
783  else:
784  reslist.append((var,self.rangeStr,-1,0.0,0.0))
785 
786  if lastOnly:
787  for res in reslist:
788  print ("%-25s %s Last %-9.3f" % (res[0],res[1],res[3]))
789  else:
790  for res in reslist:
791  print ("%-25s %s Nentries %-7d Mean %-9.3f RMS %-9.4f" % res)
792  return 0
793 
794 
795 #================================================================================================
796 
797 
798 #==== interactive use
799 if __name__ == "__main__":
800 
801  #=== check if we are in batch mode
802  batch = False
803  if "-b" in sys.argv:
804  batch=True
805  sys.argv.remove("-b")
806 
807 
808  #=== check if we are in Oracle mode
809  useCool = False
810  if "--cool" in sys.argv:
811  useCool=True
812  sys.argv.remove("--cool")
813 
814  #=== check if we are in testbeam mode
815  useTestBeam = False
816  if "--testbeam" in sys.argv:
817  useTestBeam=True
818  sys.argv.remove("--testbeam")
819 
820  #=== check if we are in hvonly mode
821  hvonly = False
822  if "--hvonly" in sys.argv:
823  hvonly=True
824  sys.argv.remove("--hvonly")
825 
826  #=== check if we are in verbose mode
827  verbose = False
828  if "-v" in sys.argv:
829  verbose=True
830  sys.argv.remove("-v")
831 
832  #=== catch invalid commands
833  cmd = sys.argv[1]
834  if not(cmd=="plot" or cmd=="diff" or cmd=="diffprof" or cmd=="prof" or cmd=="dist" or cmd=="tree" or cmd=="mean" or cmd=="last" or cmd=="scan"):
835  print (""" Please use one of the following commands: plot, diff, dist, tree, mean, last, scan !""")
836  sys.exit(1)
837 
838  #=== command is recognized, we go on....
839  ROOT.gROOT.SetBatch()
840  dp = TileDCSDataPlotter(sys.argv, useCool, useTestBeam, verbose, putDuplicates=(cmd =="mean" or cmd =="dist"), hvonly=hvonly)
841 
842  #=== no graphics if only get tree
843  if cmd == "tree":
844  t = dp.getTree()
845  f = ROOT.TFile(dp.outName+".root", "recreate")
846  t.Write()
847  f.Close()
848 
849  elif cmd == "scan":
850  dp.scan()
851 
852  #=== distribution plot (using weights)
853  elif cmd =="mean":
854  dp.printDistributionStat()
855  elif cmd =="last":
856  dp.printDistributionStat(True)
857 
858  else:
859  #=== graphics output below -> configure style and canvas
860  ROOT.gROOT.SetStyle("Plain")
861  ROOT.gROOT.ForceStyle()
862  if cmd =="diff" or cmd=="diffprof" or cmd=="prof":
863  can = TCanvas("c_TileDCSDataPlotter","c_TileDCSDataPlotter",0,0,1200,900)
864  else:
865  can = TCanvas("c_TileDCSDataPlotter","c_TileDCSDataPlotter")
866 
867  #=== simple timeline plot
868  if cmd =="plot":
869  can.SetGridx()
870  can.SetGridy()
871  hh = dp.getTimelinePlot()
872  if len(hh)>0:
873  h0 = TH1D(hh[0])
874  tt=dp.rangeStr
875  if len(sys.argv)>6:
876  tt+=" with cut "+sys.argv[6]
877  XA=h0.GetXaxis()
878  tx=XA.GetTitle()
879  ttt=tt+";"+tx+";"+sys.argv[2]+" :: "+sys.argv[3]
880  h0.SetTitle(ttt)
881  if len(hh)>2:
882  ymin=h0.GetMinimum()
883  ymax=h0.GetMaximum()
884  max1=ymax+ymax-ymin
885  h0.SetMaximum(max1)
886  h0.Draw("HIST")
887  leg = TLegend(0.1,0.55,0.9,0.9)
888  for h in hh:
889  h.Draw("HISTSAME")
890  tit=h.GetTitle()
891  leg.AddEntry(h,tit,"l")
892  if len(hh)>2:
893  if len(hh)>30:
894  leg.SetNColumns(4)
895  elif len(hh)>20:
896  leg.SetNColumns(3)
897  elif len(hh)>5:
898  leg.SetNColumns(2)
899  leg.Draw()
900 
901  #=== simple diff plot
902  if cmd =="diff":
903  h = dp.getDiffPlot(1)
904  h.Draw('COLZ')
905 
906  #=== simple diff profile plot
907  if cmd =="diffprof":
908  h = dp.getDiffPlot(2)
909  h.Draw('COLZ')
910 
911  #=== simple diff profile plot
912  if cmd =="prof":
913  h = dp.getDiffPlot(3)
914  h.Draw('COLZ')
915 
916  #=== distribution plot (using weights)
917  elif cmd =="dist":
918  hh = dp.getDistributionPlot()
919  if len(hh)>0:
920  dim = dp.varExp.split(',')[0].count(':')
921  opt=""
922  pr=(len(hh)==1)
923  for h in hh:
924  if dim==0:
925  h.Draw("EHIST"+opt)
926  if pr:
927  print ("%s %s Nentries %d Mean %7.3f RMS %8.4f" % \
928  (h.GetXaxis().GetTitle(),h.GetTitle(),h.GetEntries(),h.GetMean(),h.GetRMS()))
929  else:
930  pr=True
931  elif dim==1:
932  print ("2D plot, opt ",opt)
933  h.Draw("BOX"+opt)
934  else:
935  h.Draw(opt)
936  opt="SAME"
937 
938  #=== save output in eps and png
939  can.Print(dp.outName+"_"+cmd+".eps")
940  can.Print(dp.outName+"_"+cmd+".png")
941 
942  #=== display plot
943  if not batch:
944  os.system("display "+dp.outName+"_"+cmd+".png")
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
TileDCSDataPlotter.TileDCSDataPlotter.printDistributionStatAll
def printDistributionStatAll(self)
Definition: TileDCSDataPlotter.py:674
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
TileDCSDataPlotter.TileDCSDataPlotter.iovEnd
iovEnd
Definition: TileDCSDataPlotter.py:41
TileDCSDataPlotter.TileDCSDataPlotter.cmd
cmd
Definition: TileDCSDataPlotter.py:35
TileDCSDataPlotter.TileDCSDataPlotter.drawer
drawer
Definition: TileDCSDataPlotter.py:36
TileDCSDataPlotter.TileDCSDataPlotter.useTestBeam
useTestBeam
Definition: TileDCSDataPlotter.py:25
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
TileDCSDataPlotter.TileDCSDataPlotter.varExp
varExp
Definition: TileDCSDataPlotter.py:37
TileDCSDataPlotter.TileDCSDataPlotter.getDiffPlot
def getDiffPlot(self, opt)
Definition: TileDCSDataPlotter.py:440
TileDCSDataPlotter.TileDCSDataPlotter.getDistributionPlot
def getDistributionPlot(self)
Definition: TileDCSDataPlotter.py:552
TileDCSDataPlotter.TileDCSDataPlotter.info
info
Definition: TileDCSDataPlotter.py:44
TileDCSDataPlotter.TileDCSDataPlotter.rangeStr
rangeStr
Definition: TileDCSDataPlotter.py:61
TileDCSDataPlotter.TileDCSDataPlotter.printDistributionStat
def printDistributionStat(self, lastOnly=False)
Definition: TileDCSDataPlotter.py:700
TileDCSDataPlotter.TileDCSDataPlotter.iovBeg
iovBeg
Definition: TileDCSDataPlotter.py:40
TileDCSDataPlotter.TileDCSDataPlotter.parseVarExpression
def parseVarExpression(self, varExpression)
Definition: TileDCSDataPlotter.py:79
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
TileDCSDataPlotter.TileDCSDataPlotter.__init__
def __init__(self, argv, useCool, useTestBeam, verbose, dbstring=None, putDuplicates=False, hvonly=False)
Definition: TileDCSDataPlotter.py:19
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
TileDCSDataPlotter.TileDCSDataPlotter.outName
outName
Definition: TileDCSDataPlotter.py:51
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
TileDCSDataPlotter.TileDCSDataPlotter.cutReplace
def cutReplace(self, incut, var)
Definition: TileDCSDataPlotter.py:244
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
pickleTool.object
object
Definition: pickleTool.py:30
str
Definition: BTagTrackIpAccessor.cxx:11
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
TileDCSDataPlotter.TileDCSDataPlotter
Definition: TileDCSDataPlotter.py:16
TileDCSDataPlotter.TileDCSDataPlotter.scantree
def scantree(self, t, treevar, cut)
Definition: TileDCSDataPlotter.py:289
TileDCSDataPlotter.TileDCSDataPlotter.getTree
def getTree(self, drawer=None, var=None, lastOnly=False, firstAndLast=False)
Definition: TileDCSDataPlotter.py:149
TileDCSDataPlotter.TileDCSDataPlotter.cutExp
cutExp
Definition: TileDCSDataPlotter.py:46
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
TileDCSDataPlotter.TileDCSDataPlotter.getTimelinePlot
def getTimelinePlot(self)
Definition: TileDCSDataPlotter.py:337
TileDCSDataPlotter.TileDCSDataPlotter.scan
def scan(self)
Definition: TileDCSDataPlotter.py:268
TileDCSDataPlotter.TileDCSDataPlotter.useCool
useCool
Definition: TileDCSDataPlotter.py:22
TileDCSDataPlotter.TileDCSDataPlotter.dataGrabber
dataGrabber
Definition: TileDCSDataPlotter.py:43