10 from ROOT
import TCanvas, TH1D, TH2D, TArrayD, TLegend
11 from TileCoolDcs.TileDCSDataGrabber
import TileDCSDataGrabber
12 from TileCoolDcs.ProgressBar
import progressBar
18 def __init__( self, argv, useCool, useTestBeam, verbose, dbstring=None, putDuplicates=False, hvonly=False ):
27 logLvl = logging.WARNING
29 logLvl = logging.DEBUG
39 self.
iovBeg =
int(time.mktime(time.strptime(beg,
"%Y-%m-%d %H:%M:%S")))
40 self.
iovEnd =
int(time.mktime(time.strptime(end,
"%Y-%m-%d %H:%M:%S")))
42 self.
dataGrabber = TileDCSDataGrabber(dbSource, logLvl, dbstring, putDuplicates, self.
iovBeg)
48 beg = beg.replace(
' ',
'_')
49 end = end.replace(
' ',
'_')
56 print (
"---> OUTNAME: " , self.
outName)
58 timeBegInfo = time.localtime(self.
iovBeg)
59 timeEndInfo = time.localtime(self.
iovEnd)
60 self.
rangeStr =
"FROM: "+time.asctime(timeBegInfo)+
" UNTIL: "+time.asctime(timeEndInfo)
62 part=[
"LBA",
"LBC",
"EBA",
"EBC"]
67 self.
drawer+=
"%s%2.2d," % (part[p],m+1)
73 self.
drawer+=
"%s%2.2d," % (p,m+1)
80 knownVars = self.
info.get_all_variables()
83 pos = varExpression.find(var)
90 if len(oldVar) > len(var):
97 varExpr = varExpression
98 for v
in list(varDict.values()):
99 varExpr=varExpr.replace(v,
" "*len(v))
100 oldPos = varExpr.find(oldVar)
102 varDict[oldPos] = oldVar
105 positions =
sorted(varDict.keys())
106 posLength = len(positions)
109 while iPos < posLength:
110 if positions[iPos] < positions[iPos-1]+len(varDict[positions[iPos-1]]):
111 varDict.pop(positions[iPos])
113 posLength=len(positions)
117 varList =
list(varDict.values())
118 if "ALL_LVPS_AI" in varExpression:
119 varList.extend(
list(self.
info.vars_LVPS_AI.keys()))
120 if "ALL_LVPS_STATES" in varExpression:
121 varList.extend(
list(self.
info.vars_LVPS_STATES.keys()))
122 if "ALL_HVSET" in varExpression
or "ALL_SETHV" in varExpression:
123 varList.extend(
list(self.
info.vars_HVSET.keys()))
124 if self.
useCool or (
"ALL_HV-ALL_SETHV" in varExpression
or "ALL_HV-ALL_HVSET" in varExpression):
126 if "Set.vFix1%d" % (i+1)
in varList:
127 varList.remove(
"Set.vFix1%d" % (i+1))
129 varList.remove(
"Set.hvIn%d" % (i+1))
131 varList.remove(
"Set.volt%d" % (i+1))
133 varList.remove(
"Set.temp%d" % (i+1))
134 if (
"ALL_HV" in varExpression
and "ALL_HVSET" not in varExpression)
or "ALL_HV-ALL_HVSET" in varExpression:
135 varList.extend(
list(self.
info.vars_HV.keys()))
136 if self.
useCool or (
"ALL_HV-ALL_SETHV" in varExpression
or "ALL_HV-ALL_HVSET" in varExpression):
138 varList.remove(
"hvIn%d" % (i+1))
140 varList.remove(
"volt%d" % (i+1))
142 varList.remove(
"temp%d" % (i+1))
148 def getTree(self, drawer=None, var=None,lastOnly=False,firstAndLast=False):
164 if t.GetEntries()==0:
165 print (
"ERROR: No data in time interval!")
168 print (
"Found number of entries: ", t.GetEntries())
171 if self.
cmd!=
"tree" and drawer
is None and var
is None:
181 newvar += dr+
"."+var+
","
183 newcut += dr+
"."+cut+
","
188 if cut
and "Set." in cut
and "Set." not in var:
197 if "ALL_LVPS_AI" in self.
varExp:
200 for var
in list(self.
info.vars_LVPS_AI.keys()):
201 newvar += dr+
"."+var+
","
204 if "ALL_LVPS_STATES" in self.
varExp:
207 for var
in list(self.
info.vars_LVPS_STATES.keys()):
208 newvar += dr+
"."+var+
","
211 if "ALL_HV-ALL_SETHV" in self.
varExp or "ALL_HV-ALL_HVSET" in self.
varExp:
214 for var
in list(self.
info.vars_HVSET.keys()):
215 if not (
"Set.vFix" in var
or "Set.hvIn" in var
or "Set.volt" in var
or "Set.temp" in var):
216 newvar += dr+
"."+var.replace(
"Set.",
"")+
"-"+dr+
"."+var+
","
219 if "ALL_HVSET" in self.
varExp or "ALL_SETHV" in self.
varExp:
222 for var
in list(self.
info.vars_HVSET.keys()):
223 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):
224 newvar += dr+
"."+var+
","
227 if "ALL_HV" in self.
varExp and "ALL_HVSET" not in self.
varExp :
230 for var
in list(self.
info.vars_HV.keys()):
231 if not self.
useCool or not (
"hvIn" in var
or "volt" in var
or "temp" in var):
232 newvar += dr+
"."+var+
","
235 print (
"self.drawer: ", self.
drawer)
236 print (
"self.varExp: ", self.
varExp)
237 print (
"self.cutExp: ", self.
cutExp)
245 Relace "ALL" keywords in cut variable by appropriate names from var variable
250 for vv
in var.split(
"-"):
253 cut = cut.replace(
"ALL_SETHV",vv)
254 cut = cut.replace(
"ALL_HVSET",vv)
255 vvv=vv.replace(
"Set.hv",
"hv")
256 cut = cut.replace(
"ALL_HV",vvv)
258 vvv=vv.replace(
"hv",
"Set.hv")
259 cut = cut.replace(
"ALL_SETHV",vvv)
260 cut = cut.replace(
"ALL_HVSET",vvv)
261 cut = cut.replace(
"ALL_HV",vv)
262 cut = cut.replace(
"ALL",var)
269 Scan ROOT TTree and show all values on the screen
277 treevar=
"EvTime:"+var
290 Scan ROOT TTree and show up to 4 variables on the screen
295 t.Draw(treevar,cut,
"goff")
296 n = t.GetSelectedRows()
298 vars = treevar.split(
":")
301 for i
in range(1,nv):
303 fmt += [
"\t%6d\t\t" ]
305 fmt += [
"\t%6.2f\t" ]
311 if v==
"EvTime": v += 13*
" "
330 dt = datetime.datetime.fromtimestamp(
int(x[i])).strftime(
'%Y-%m-%d %H:%M:%S')
338 Returns a TCanvas with the variable plotted vs time.
342 ROOT.gStyle.SetOptStat(0)
345 if t.GetEntries()==1:
346 print (
"ERROR: Only one data point, need at least 2")
347 print (
"---> Try increasing the time span")
355 color = [2,4,3,5,6,7]
360 t.Draw(var+
":EvTime",cut,
"goff")
361 n = t.GetSelectedRows()
363 print (
"var=",var,
" \tcut=",cut,
" \tnpoints=",n)
365 print (
"var=",var,
" \tnpoints=",n)
375 t.Draw(var+
":EvTime",
"",
"goff")
376 n0 = t.GetSelectedRows()
383 xarr[i] = x0[i]-offset
384 xarr[n0] = self.
iovEnd-offset
387 h = TH1D(
"hDCS"+
str(ih),title,n0,xarr.GetArray())
389 h.Fill(x[i]-offset,y[i])
401 h.GetXaxis().SetTimeFormat(
"%S")
402 h.SetXTitle(
"time [seconds]")
403 if timeDiff > 12*sec_mon:
404 h.GetXaxis().SetTimeFormat(
"%m/%y")
405 h.SetXTitle(
"time [month/year]")
407 elif timeDiff > 6*sec_day:
408 h.GetXaxis().SetTimeFormat(
"%d/%m")
409 h.SetXTitle(
"time [day/month]")
410 elif timeDiff > 2*sec_day:
411 h.GetXaxis().SetTimeFormat(
"%d(%H)")
412 h.SetXTitle(
"time [day(hour)]")
413 elif timeDiff > 2*sec_hrs:
414 h.GetXaxis().SetTimeFormat(
"%H:%M")
415 h.SetXTitle(
"time [hour:min]")
416 elif timeDiff > 2*sec_min:
417 h.GetXaxis().SetTimeFormat(
"%M")
418 h.SetXTitle(
"time [min]")
419 h.GetXaxis().SetTimeDisplay(1)
420 h.SetLineColor(color[ih%len(color)])
426 delta=(hmax-hmin)/20.
441 Returns a TCanvas with 2D plot showing difference between last and first entries.
445 ROOT.gStyle.SetOptStat(0)
446 tree = self.
getTree(firstAndLast=(opt==1))
448 if tree.GetEntries()==1:
449 print (
"ERROR: Only one data point, need at least 2")
450 print (
"---> Try increasing the time span")
454 print (
"Cut expression is ignored in diff plot")
457 leaves=tree.GetListOfLeaves()
461 z=leaf.GetTitle().
split(
'.')
462 if len(z)==2
or (len(z)==3
and z[1]==
"Set"):
467 if not y[-2].isdigit()
and y[-1].isdigit():
486 hist=ROOT.TH2F(
"hist2D",pref+self.
outName,nx,-0.5,nx-0.5,ny,-0.5,ny-0.5)
488 hist=ROOT.TProfile2D(
"hist2D",pref+self.
outName,nx,-0.5,nx-0.5,ny,-0.5,ny-0.5)
494 XA.SetBinLabel(i + 1,
'')
496 XA.SetBinLabel(i + 1, xaxis[i])
501 YA.SetBinLabel(i + 1, yaxis[i])
507 val[leaf.GetTitle()] = 0
509 val[leaf.GetTitle()] = leaf.GetValue()
518 bar = progressBar(n1,ne, 78)
519 for n
in range(n1,ne):
523 v1=val[leaf.GetTitle()]
524 z=leaf.GetTitle().
split(
'.')
525 if len(z)==2
or (len(z)==3
and z[1]==
"Set"):
530 if not y[-2].isdigit()
and y[-1].isdigit():
544 hist.Fill(xaxis.index(x),yaxis.index(y),v2-v1)
553 Returns a TCanvas with the variable distribution plotted (using weights)
561 color = [2,4,3,5,6,7]
575 varX = var.split(
":")[1]
576 varY = var.split(
":")[0]
580 cut =
"("+cut+
") && ("+cutY+
")"
587 cut =
"weight*("+cut+
")"
593 t.Draw(var,cut,
"goff")
594 h = ROOT.gDirectory.Get(
"htemp")
595 h.SetXTitle(var.split(
":")[0])
597 h.SetName(
"TileDCSDataPlotter"+
str(ih))
601 xmi = h.GetXaxis().GetBinLowEdge(1)
602 xma = h.GetXaxis().GetBinLowEdge(h.GetNbinsX())+h.GetXaxis().GetBinWidth(h.GetNbinsX())
608 ymi = h.GetYaxis().GetBinLowEdge(1)
609 yma = h.GetYaxis().GetBinLowEdge(h.GetNbinsY())+h.GetYaxis().GetBinWidth(h.GetNbinsY())
624 h.SetLineColor(color[ih%len(color)])
625 h.SetMarkerColor(color[ih%len(color)])
634 delta=(xmax-xmin)/20.
642 delta=(ymax-ymin)/20.
650 delta=(hmax-hmin)/20.
657 h0 = TH2D(
"hDCS",
"dummy",2,xmin,xmax,2,ymin,ymax)
658 h0.SetYTitle(ytit[:-1])
660 h0 = TH1D(
"hDCS",
"dummy",2,xmin,xmax)
661 h0.SetXTitle(xtit[:-1])
667 ROOT.gStyle.SetOptStat(0)
675 Print mean values from histograms
683 h = ROOT.gDirectory.Get(
"htemp")
685 print (
"%s %s %s Nentries %d Mean %7.3f RMS %8.4f" % \
686 (v,c,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
691 h = ROOT.gDirectory.Get(
"htemp")
692 print (
"%s %s Nentries %d Mean %7.3f RMS %8.4f" % \
693 (v,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
701 Print mean values from histograms
705 if "ALL_LVPS_AI" in self.
varExp:
707 if "ALL_LVPS_STATES" in self.
varExp:
709 if "ALL_HV-ALL_SETHV" in self.
varExp or "ALL_HV-ALL_HVSET" in self.
varExp:
711 for var
in list(self.
info.vars_HVSET.keys()):
712 if not (
"Set.vFix" in var
or "Set.hvIn" in var
or "Set.volt" in var
or "Set.temp" in var):
713 vlist += [var.replace(
"Set.",
"")+
"-"+var]
716 if "ALL_HVSET" in self.
varExp or "ALL_SETHV" in self.
varExp:
717 vlist =
list(self.
info.vars_HVSET.keys())
720 if "Set.vFix1%d" % (i+1)
in vlist:
721 vlist.remove(
"Set.vFix1%d" % (i+1))
723 vlist.remove(
"Set.hvIn%d" % (i+1))
725 vlist.remove(
"Set.volt%d" % (i+1))
727 vlist.remove(
"Set.temp%d" % (i+1))
730 if "ALL_HV" in self.
varExp and "ALL_HVSET" not in self.
varExp:
731 vlist =
list(self.
info.vars_HV.keys())
734 vlist.remove(
"hvIn%d" % (i+1))
736 vlist.remove(
"volt%d" % (i+1))
738 vlist.remove(
"temp%d" % (i+1))
741 print (
"var list is",varlist)
745 print (
"cut list is",cutlist)
747 print (
"full list is",fulllist)
759 if not (t
and drlist==drlistprev
and varlist==varlistprev):
763 if t
and t.GetEntries()>0:
767 var = var.replace(va,drawer+
"."+va)
768 cut = cut.replace(va,drawer+
"."+va)
769 var = var.replace(drawer+
"."+drawer,drawer)
770 cut = cut.replace(drawer+
"."+drawer,drawer)
771 var = var.replace(
"Set."+drawer,
"Set")
772 cut = cut.replace(
"Set."+drawer,
"Set")
775 t.Draw(var,cut,
"goff")
776 h = ROOT.gDirectory.Get(
"htemp")
779 reslist.append((var+
" CUT "+cut,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
781 reslist.append((var,self.
rangeStr,h.GetEntries(),h.GetMean(),h.GetRMS()))
783 reslist.append((var,self.
rangeStr,-1,0.0,0.0))
787 print (
"%-25s %s Last %-9.3f" % (res[0],res[1],res[3]))
790 print (
"%-25s %s Nentries %-7d Mean %-9.3f RMS %-9.4f" % res)
798 if __name__ ==
"__main__":
804 sys.argv.remove(
"-b")
809 if "--oracle" in sys.argv:
811 sys.argv.remove(
"--oracle")
812 if "--cool" in sys.argv:
814 sys.argv.remove(
"--cool")
818 if "--testbeam" in sys.argv:
820 sys.argv.remove(
"--testbeam")
824 if "--hvonly" in sys.argv:
826 sys.argv.remove(
"--hvonly")
832 sys.argv.remove(
"-v")
836 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"):
837 print (
""" Please use one of the following commands: plot, diff, dist, tree, mean, last, scan !""")
841 ROOT.gROOT.SetBatch()
842 dp =
TileDCSDataPlotter(sys.argv, useCool, useTestBeam, verbose, putDuplicates=(cmd ==
"mean" or cmd ==
"dist"), hvonly=hvonly)
847 f = ROOT.TFile(dp.outName+
".root",
"recreate")
856 dp.printDistributionStat()
858 dp.printDistributionStat(
True)
862 ROOT.gROOT.SetStyle(
"Plain")
863 ROOT.gROOT.ForceStyle()
864 if cmd ==
"diff" or cmd==
"diffprof" or cmd==
"prof":
865 can = TCanvas(
"c_TileDCSDataPlotter",
"c_TileDCSDataPlotter",0,0,1200,900)
867 can = TCanvas(
"c_TileDCSDataPlotter",
"c_TileDCSDataPlotter")
873 hh = dp.getTimelinePlot()
878 tt+=
" with cut "+sys.argv[6]
881 ttt=tt+
";"+tx+
";"+sys.argv[2]+
" :: "+sys.argv[3]
889 leg = TLegend(0.1,0.55,0.9,0.9)
893 leg.AddEntry(h,tit,
"l")
905 h = dp.getDiffPlot(1)
910 h = dp.getDiffPlot(2)
915 h = dp.getDiffPlot(3)
920 hh = dp.getDistributionPlot()
922 dim = dp.varExp.split(
',')[0].
count(
':')
929 print (
"%s %s Nentries %d Mean %7.3f RMS %8.4f" % \
930 (h.GetXaxis().GetTitle(),h.GetTitle(),h.GetEntries(),h.GetMean(),h.GetRMS()))
934 print (
"2D plot, opt ",opt)
941 can.Print(dp.outName+
"_"+cmd+
".eps")
942 can.Print(dp.outName+
"_"+cmd+
".png")
946 os.system(
"display "+dp.outName+
"_"+cmd+
".png")