5 from __future__
import print_function
11 from ROOT
import TCanvas, TH1D, TH2D, TArrayD, TLegend
12 from TileCoolDcs.TileDCSDataGrabber
import TileDCSDataGrabber
13 from TileCoolDcs.ProgressBar
import progressBar
19 def __init__( self, argv, useCool, useTestBeam, verbose, dbstring=None, putDuplicates=False, hvonly=False ):
28 logLvl = logging.WARNING
30 logLvl = logging.DEBUG
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")))
43 self.
dataGrabber = TileDCSDataGrabber(dbSource, logLvl, dbstring, putDuplicates, self.
iovBeg)
49 beg = beg.replace(
' ',
'_')
50 end = end.replace(
' ',
'_')
57 print (
"---> OUTNAME: " , self.
outName)
59 timeBegInfo = time.localtime(self.
iovBeg)
60 timeEndInfo = time.localtime(self.
iovEnd)
61 self.
rangeStr =
"FROM: "+time.asctime(timeBegInfo)+
" UNTIL: "+time.asctime(timeEndInfo)
63 part=[
"LBA",
"LBC",
"EBA",
"EBC"]
68 self.
drawer+=
"%s%2.2d," % (part[p],m+1)
74 self.
drawer+=
"%s%2.2d," % (p,m+1)
81 knownVars = self.
info.get_all_variables()
84 pos = varExpression.find(var)
91 if len(oldVar) > len(var):
98 varExpr = varExpression
99 for v
in list(varDict.values()):
100 varExpr=varExpr.replace(v,
" "*len(v))
101 oldPos = varExpr.find(oldVar)
103 varDict[oldPos] = oldVar
106 positions =
sorted(varDict.keys())
107 posLength = len(positions)
110 while iPos < posLength:
111 if positions[iPos] < positions[iPos-1]+len(varDict[positions[iPos-1]]):
112 varDict.pop(positions[iPos])
114 posLength=len(positions)
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):
127 if "Set.vFix1%d" % (i+1)
in varList:
128 varList.remove(
"Set.vFix1%d" % (i+1))
130 varList.remove(
"Set.hvIn%d" % (i+1))
132 varList.remove(
"Set.volt%d" % (i+1))
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):
139 varList.remove(
"hvIn%d" % (i+1))
141 varList.remove(
"volt%d" % (i+1))
143 varList.remove(
"temp%d" % (i+1))
149 def getTree(self, drawer=None, var=None,lastOnly=False,firstAndLast=False):
165 if t.GetEntries()==0:
166 print (
"ERROR: No data in time interval!")
169 print (
"Found number of entries: ", t.GetEntries())
172 if self.
cmd!=
"tree" and drawer
is None and var
is None:
182 newvar += dr+
"."+var+
","
184 newcut += dr+
"."+cut+
","
189 if cut
and "Set." in cut
and "Set." not in var:
198 if "ALL_LVPS_AI" in self.
varExp:
201 for var
in list(self.
info.vars_LVPS_AI.keys()):
202 newvar += dr+
"."+var+
","
205 if "ALL_LVPS_STATES" in self.
varExp:
208 for var
in list(self.
info.vars_LVPS_STATES.keys()):
209 newvar += dr+
"."+var+
","
212 if "ALL_HV-ALL_SETHV" in self.
varExp or "ALL_HV-ALL_HVSET" in self.
varExp:
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+
","
220 if "ALL_HVSET" in self.
varExp or "ALL_SETHV" in self.
varExp:
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+
","
228 if "ALL_HV" in self.
varExp and "ALL_HVSET" not in self.
varExp :
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+
","
236 print (
"self.drawer: ", self.
drawer)
237 print (
"self.varExp: ", self.
varExp)
238 print (
"self.cutExp: ", self.
cutExp)
246 Relace "ALL" keywords in cut variable by appropriate names from var variable
251 for vv
in var.split(
"-"):
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)
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)
270 Scan ROOT TTree and show all values on the screen
278 treevar=
"EvTime:"+var
291 Scan ROOT TTree and show up to 4 variables on the screen
296 t.Draw(treevar,cut,
"goff")
297 n = t.GetSelectedRows()
299 vars = treevar.split(
":")
302 for i
in range(1,nv):
304 fmt += [
"\t%6d\t\t" ]
306 fmt += [
"\t%6.2f\t" ]
312 if v==
"EvTime": v += 13*
" "
331 dt = datetime.datetime.fromtimestamp(
int(x[i])).strftime(
'%Y-%m-%d %H:%M:%S')
339 Returns a TCanvas with the variable plotted vs time.
343 ROOT.gStyle.SetOptStat(0)
346 if t.GetEntries()==1:
347 print (
"ERROR: Only one data point, need at least 2")
348 print (
"---> Try increasing the time span")
356 color = [2,4,3,5,6,7]
361 t.Draw(var+
":EvTime",cut,
"goff")
362 n = t.GetSelectedRows()
364 print (
"var=",var,
" \tcut=",cut,
" \tnpoints=",n)
366 print (
"var=",var,
" \tnpoints=",n)
376 t.Draw(var+
":EvTime",
"",
"goff")
377 n0 = t.GetSelectedRows()
384 xarr[i] = x0[i]-offset
385 xarr[n0] = self.
iovEnd-offset
388 h = TH1D(
"hDCS"+
str(ih),title,n0,xarr.GetArray())
390 h.Fill(x[i]-offset,y[i])
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]")
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)])
427 delta=(hmax-hmin)/20.
442 Returns a TCanvas with 2D plot showing difference between last and first entries.
446 ROOT.gStyle.SetOptStat(0)
447 tree = self.
getTree(firstAndLast=(opt==1))
449 if tree.GetEntries()==1:
450 print (
"ERROR: Only one data point, need at least 2")
451 print (
"---> Try increasing the time span")
455 print (
"Cut expression is ignored in diff plot")
458 leaves=tree.GetListOfLeaves()
462 z=leaf.GetTitle().
split(
'.')
463 if len(z)==2
or (len(z)==3
and z[1]==
"Set"):
468 if not y[-2].isdigit()
and y[-1].isdigit():
487 hist=ROOT.TH2F(
"hist2D",pref+self.
outName,nx,-0.5,nx-0.5,ny,-0.5,ny-0.5)
489 hist=ROOT.TProfile2D(
"hist2D",pref+self.
outName,nx,-0.5,nx-0.5,ny,-0.5,ny-0.5)
495 XA.SetBinLabel(i + 1,
'')
497 XA.SetBinLabel(i + 1, xaxis[i])
502 YA.SetBinLabel(i + 1, yaxis[i])
508 val[leaf.GetTitle()] = 0
510 val[leaf.GetTitle()] = leaf.GetValue()
519 bar = progressBar(n1,ne, 78)
520 for n
in range(n1,ne):
524 v1=val[leaf.GetTitle()]
525 z=leaf.GetTitle().
split(
'.')
526 if len(z)==2
or (len(z)==3
and z[1]==
"Set"):
531 if not y[-2].isdigit()
and y[-1].isdigit():
545 hist.Fill(xaxis.index(x),yaxis.index(y),v2-v1)
554 Returns a TCanvas with the variable distribution plotted (using weights)
562 color = [2,4,3,5,6,7]
576 varX = var.split(
":")[1]
577 varY = var.split(
":")[0]
581 cut =
"("+cut+
") && ("+cutY+
")"
588 cut =
"weight*("+cut+
")"
594 t.Draw(var,cut,
"goff")
595 h = ROOT.gDirectory.Get(
"htemp")
596 h.SetXTitle(var.split(
":")[0])
598 h.SetName(
"TileDCSDataPlotter"+
str(ih))
602 xmi = h.GetXaxis().GetBinLowEdge(1)
603 xma = h.GetXaxis().GetBinLowEdge(h.GetNbinsX())+h.GetXaxis().GetBinWidth(h.GetNbinsX())
609 ymi = h.GetYaxis().GetBinLowEdge(1)
610 yma = h.GetYaxis().GetBinLowEdge(h.GetNbinsY())+h.GetYaxis().GetBinWidth(h.GetNbinsY())
625 h.SetLineColor(color[ih%len(color)])
626 h.SetMarkerColor(color[ih%len(color)])
635 delta=(xmax-xmin)/20.
643 delta=(ymax-ymin)/20.
651 delta=(hmax-hmin)/20.
658 h0 = TH2D(
"hDCS",
"dummy",2,xmin,xmax,2,ymin,ymax)
659 h0.SetYTitle(ytit[:-1])
661 h0 = TH1D(
"hDCS",
"dummy",2,xmin,xmax)
662 h0.SetXTitle(xtit[:-1])
668 ROOT.gStyle.SetOptStat(0)
676 Print mean values from histograms
684 h = ROOT.gDirectory.Get(
"htemp")
686 print (
"%s %s %s Nentries %d Mean %7.3f RMS %8.4f" % \
687 (v,c,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
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()))
702 Print mean values from histograms
706 if "ALL_LVPS_AI" in self.
varExp:
708 if "ALL_LVPS_STATES" in self.
varExp:
710 if "ALL_HV-ALL_SETHV" in self.
varExp or "ALL_HV-ALL_HVSET" in self.
varExp:
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]
717 if "ALL_HVSET" in self.
varExp or "ALL_SETHV" in self.
varExp:
718 vlist =
list(self.
info.vars_HVSET.keys())
721 if "Set.vFix1%d" % (i+1)
in vlist:
722 vlist.remove(
"Set.vFix1%d" % (i+1))
724 vlist.remove(
"Set.hvIn%d" % (i+1))
726 vlist.remove(
"Set.volt%d" % (i+1))
728 vlist.remove(
"Set.temp%d" % (i+1))
731 if "ALL_HV" in self.
varExp and "ALL_HVSET" not in self.
varExp:
732 vlist =
list(self.
info.vars_HV.keys())
735 vlist.remove(
"hvIn%d" % (i+1))
737 vlist.remove(
"volt%d" % (i+1))
739 vlist.remove(
"temp%d" % (i+1))
742 print (
"var list is",varlist)
746 print (
"cut list is",cutlist)
748 print (
"full list is",fulllist)
760 if not (t
and drlist==drlistprev
and varlist==varlistprev):
764 if t
and t.GetEntries()>0:
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")
776 t.Draw(var,cut,
"goff")
777 h = ROOT.gDirectory.Get(
"htemp")
780 reslist.append((var+
" CUT "+cut,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
782 reslist.append((var,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
784 reslist.append((var,self.
rangeStr,-1,0.0,0.0))
788 print (
"%-25s %s Last %-9.3f" % (res[0],res[1],res[3]))
791 print (
"%-25s %s Nentries %-7d Mean %-9.3f RMS %-9.4f" % res)
799 if __name__ ==
"__main__":
805 sys.argv.remove(
"-b")
810 if "--cool" in sys.argv:
812 sys.argv.remove(
"--cool")
816 if "--testbeam" in sys.argv:
818 sys.argv.remove(
"--testbeam")
822 if "--hvonly" in sys.argv:
824 sys.argv.remove(
"--hvonly")
830 sys.argv.remove(
"-v")
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 !""")
839 ROOT.gROOT.SetBatch()
840 dp =
TileDCSDataPlotter(sys.argv, useCool, useTestBeam, verbose, putDuplicates=(cmd ==
"mean" or cmd ==
"dist"), hvonly=hvonly)
845 f = ROOT.TFile(dp.outName+
".root",
"recreate")
854 dp.printDistributionStat()
856 dp.printDistributionStat(
True)
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)
865 can = TCanvas(
"c_TileDCSDataPlotter",
"c_TileDCSDataPlotter")
871 hh = dp.getTimelinePlot()
876 tt+=
" with cut "+sys.argv[6]
879 ttt=tt+
";"+tx+
";"+sys.argv[2]+
" :: "+sys.argv[3]
887 leg = TLegend(0.1,0.55,0.9,0.9)
891 leg.AddEntry(h,tit,
"l")
903 h = dp.getDiffPlot(1)
908 h = dp.getDiffPlot(2)
913 h = dp.getDiffPlot(3)
918 hh = dp.getDistributionPlot()
920 dim = dp.varExp.split(
',')[0].
count(
':')
927 print (
"%s %s Nentries %d Mean %7.3f RMS %8.4f" % \
928 (h.GetXaxis().GetTitle(),h.GetTitle(),h.GetEntries(),h.GetMean(),h.GetRMS()))
932 print (
"2D plot, opt ",opt)
939 can.Print(dp.outName+
"_"+cmd+
".eps")
940 can.Print(dp.outName+
"_"+cmd+
".png")
944 os.system(
"display "+dp.outName+
"_"+cmd+
".png")