17 A very simple script to plot the beam spot data stored in condDB
19 usage: %prog -t <tag name>
20 -a, --auth = AUTH: DB authorization path. online(/nfshome0/popcondev/conddb).
21 -b, --batch : Run ROOT in batch mode.
22 -c, --create = CREATE: name for beam spot data file.
23 -d, --data = DATA: input beam spot data file.
24 -D, --destDB = DESTDB: destination DB string. online(oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT).
25 -i, --initial = INITIAL: First IOV. Options: run number, or run:lumi, eg. \"133200:21\"
26 -f, --final = FINAL: Last IOV. Options: run number, or run:lumi
27 -g, --graph : create a TGraphError instead of a TH1 object
28 -n, --noplot : Only extract beam spot data, plots are not created..
29 -o, --output = OUTPUT: filename of ROOT file with plots.
30 -p, --payload = PAYLOAD: filename of output text file. Combine and splits lumi IOVs.
31 -P, --Print : create PNG plots from canvas.
32 -s, --suffix = SUFFIX: Suffix will be added to plots filename.
33 -t, --tag = TAG: Database tag name.
34 -T, --Time : create plots with time axis.
35 -I, --IOVbase = IOVBASE: options: runbase(default), lumibase, timebase
36 -w, --wait : Pause script after plotting a new histograms.
37 -W, --weighted : Create a weighted result for a range of lumi IOVs, skip lumi IOV combination and splitting.
38 -x, --xcrossing = XCROSSING : Bunch crossing number.
40 Francisco Yumiceva (yumiceva@fnal.gov)
46 import os, string, re, sys, math
48 from BeamSpotObj
import BeamSpot
49 from IOVObj
import IOV
50 from CommonMethods
import *
55 print "\nCannot load PYROOT, make sure you have setup ROOT in the path"
56 print "and pyroot library is also defined in the variable PYTHONPATH, try:\n"
57 if (os.getenv(
"PYTHONPATH")):
58 print " setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n"
60 print " setenv PYTHONPATH $ROOTSYS/lib\n"
63 from ROOT
import TFile, TGraphErrors, TGaxis, TDatime
64 from ROOT
import TCanvas, TH1F
71 ROOT.gStyle.SetCanvasBorderMode(0)
72 ROOT.gStyle.SetCanvasColor(0)
73 ROOT.gStyle.SetCanvasDefH(600)
74 ROOT.gStyle.SetCanvasDefW(600)
75 ROOT.gStyle.SetCanvasDefX(0)
76 ROOT.gStyle.SetCanvasDefY(0)
78 ROOT.gStyle.SetPadBorderMode(0)
79 ROOT.gStyle.SetPadColor(0)
80 ROOT.gStyle.SetPadGridX(
False)
81 ROOT.gStyle.SetPadGridY(
False)
82 ROOT.gStyle.SetGridColor(0)
83 ROOT.gStyle.SetGridStyle(3)
84 ROOT.gStyle.SetGridWidth(1)
86 ROOT.gStyle.SetFrameBorderMode(0)
87 ROOT.gStyle.SetFrameFillColor(0)
88 ROOT.gStyle.SetTitleColor(1)
89 ROOT.gStyle.SetStatColor(0)
92 ROOT.gStyle.SetPaperSize(20,26)
93 ROOT.gStyle.SetPadTopMargin(0.04)
94 ROOT.gStyle.SetPadRightMargin(0.04)
95 ROOT.gStyle.SetPadBottomMargin(0.14)
96 ROOT.gStyle.SetPadLeftMargin(0.11)
97 ROOT.gStyle.SetPadTickX(1)
98 ROOT.gStyle.SetPadTickY(1)
100 ROOT.gStyle.SetTextFont(42)
101 ROOT.gStyle.SetTextSize(0.09)
102 ROOT.gStyle.SetLabelFont(42,
"xyz")
103 ROOT.gStyle.SetTitleFont(42,
"xyz")
104 ROOT.gStyle.SetLabelSize(0.035,
"xyz")
105 ROOT.gStyle.SetTitleSize(0.045,
"xyz")
106 ROOT.gStyle.SetTitleOffset(1.1,
"y")
109 ROOT.gStyle.SetMarkerStyle(8)
110 ROOT.gStyle.SetHistLineWidth(2)
111 ROOT.gStyle.SetLineWidth(1)
114 ROOT.gStyle.SetMarkerSize(0.6)
117 ROOT.gStyle.SetOptTitle(0)
118 ROOT.gStyle.SetOptStat(0)
119 ROOT.gStyle.SetOptFit(0)
123 ROOT.gROOT.ForceStyle()
127 if __name__ ==
'__main__':
135 Banner =
"CMS Preliminary"
139 option,args =
parse(__doc__)
140 if not args
and not option:
exit()
143 if not option.tag
and not option.data:
144 print " need to provide DB tag name or beam spot data file"
150 ROOT.gROOT.SetBatch()
152 datafilename =
"tmp_beamspot.dat"
154 datafilename = option.create
162 if option.IOVbase !=
"runbase" and option.IOVbase !=
"lumibase" and option.IOVbase !=
"timebase":
163 print "\n\n unknown iov base option: "+ option.IOVbase +
" \n\n\n"
165 IOVbase = option.IOVbase
168 lastRun =
"4999999999"
169 if IOVbase ==
"lumibase":
171 lastRun =
"4999999999:4999999999"
174 firstRun = option.initial
176 lastRun = option.final
183 print " read DB to get list of IOVs for the given tag"
184 mydestdb =
'frontier://PromptProd/CMS_COND_31X_BEAMSPOT'
186 mydestdb = option.destDB
187 acommand =
'cmscond_list_iov -c '+mydestdb+
' -P /afs/cern.ch/cms/DB/conddb -t '+ tag
188 tmpstatus = commands.getstatusoutput( acommand )
189 tmplistiov = tmpstatus[1].
split(
'\n')
195 totlines = len(tmplistiov)
196 for line
in tmplistiov:
198 if line.find(
'since') != -1:
201 if passline
and iline > jline
and iline < totlines-1:
202 linedata = line.split()
205 aIOV.since = int(linedata[0])
206 aIOV.till = int(linedata[1])
207 iovlist.append( aIOV )
210 print " total number of IOVs = " + str(len(iovlist))
218 otherArgs =
" -d " + option.destDB
220 otherArgs = otherArgs +
" -a "+ option.auth
222 print " get beam spot data from DB for IOVs. This can take a few minutes ..."
224 tmpfile = open(datafilename,
'w')
228 tmprunfirst = firstRun
231 tmplumilast = 9999999
232 if IOVbase==
"lumibase":
237 tmprunfirst =
pack( int(firstRun.split(
":")[0]) , int(firstRun.split(
":")[1]) )
238 tmprunlast =
pack( int(lastRun.split(
":")[0]) , int(lastRun.split(
":")[1]) )
240 if iIOV.since >= int(tmprunfirst)
and int(tmprunlast) < 0
and iIOV.since <= int(tmprunfirst):
241 print " IOV: " + str(iIOV.since)
243 if iIOV.since >= int(tmprunfirst)
and int(tmprunlast) > 0
and iIOV.till <= int(tmprunlast):
244 print " a IOV: " + str(iIOV.since) +
" to " + str(iIOV.till)
250 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " + str(iIOV.since) +otherArgs
251 if IOVbase==
"lumibase":
254 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " + str(tmprun) +
" -l "+str(tmplumi) +otherArgs
256 status = commands.getstatusoutput( acommand )
257 tmpfile.write(status[1])
259 print " beam spot data collected and stored in file " + datafilename
269 if os.path.isdir(option.data):
270 tmp = commands.getstatusoutput(
"ls "+option.data)
272 datafilename =
"combined_all.txt"
273 output = open(datafilename,
"w")
276 input = open(option.data +
"/"+f)
277 output.writelines(input.readlines())
279 print " data files have been collected in "+datafilename
281 elif os.path.exists(option.data):
282 datafilename = option.data
284 print " input beam spot data DOES NOT exist, file " + option.data
292 print "List of bunch crossings in the file:"
294 listbeam = listmap[option.Xrossing]
300 if IOVbase ==
"lumibase" and option.payload:
302 if not option.weighted:
306 print " no plots requested, exit now."
310 TGaxis.SetMaxDigits(8)
313 graphnamelist = [
'X',
'Y',
'Z',
'SigmaZ',
'dxdz',
'dydz',
'beamWidthX',
'beamWidthY']
314 graphtitlelist = [
'beam spot X',
'beam spot Y',
'beam spot Z',
'beam spot #sigma_Z',
'beam spot dX/dZ',
'beam spot dY/dZ',
'beam width X',
'beam width Y']
315 graphXaxis =
'Run number'
316 if IOVbase ==
'runbase':
317 graphXaxis =
"Run number"
318 if IOVbase ==
'lumibase':
319 graphXaxis =
'Lumi section'
320 if IOVbase ==
'timebase' or option.Time:
323 ROOT.gStyle.SetTimeOffset(0)
325 graphYaxis = [
'beam spot X [cm]',
'beam spot Y [cm]',
'beam spot Z [cm]',
'beam spot #sigma_{Z} [cm]',
'beam spot dX/dZ',
'beam spot dY/dZ',
'beam width X [cm]',
'beam width Y [cm]']
329 for ig
in range(0,8):
330 cvlist.append( TCanvas(graphnamelist[ig],graphtitlelist[ig], 1200, 600) )
332 graphlist.append( TGraphErrors( len(listbeam) ) )
334 graphlist.append( TH1F(
"name",
"title",len(listbeam),0,len(listbeam)) )
336 graphlist[ig].SetName(graphnamelist[ig])
337 graphlist[ig].SetTitle(graphtitlelist[ig])
339 for ii
in range(0,len(listbeam)):
342 datax = dataxerr = 0.
343 datay = datayerr = 0.
344 if graphnamelist[ig] ==
'X':
346 datayerr = ibeam.Xerr
347 if graphnamelist[ig] ==
'Y':
349 datayerr = ibeam.Yerr
350 if graphnamelist[ig] ==
'Z':
352 datayerr = ibeam.Zerr
353 if graphnamelist[ig] ==
'SigmaZ':
355 datayerr = ibeam.sigmaZerr
356 if graphnamelist[ig] ==
'dxdz':
358 datayerr = ibeam.dxdzerr
359 if graphnamelist[ig] ==
'dydz':
361 datayerr = ibeam.dydzerr
362 if graphnamelist[ig] ==
'beamWidthX':
363 datay = ibeam.beamWidthX
364 datayerr = ibeam.beamWidthXerr
365 if graphnamelist[ig] ==
'beamWidthY':
366 datay = ibeam.beamWidthY
367 datayerr = ibeam.beamWidthYerr
369 datax = ibeam.IOVfirst
370 if IOVbase==
"lumibase":
371 datax = str(ibeam.Run) +
":" + str(ibeam.IOVfirst)
372 if ibeam.IOVfirst != ibeam.IOVlast:
373 datax = str(ibeam.Run) +
":" + str(ibeam.IOVfirst)+
"-"+str(ibeam.IOVlast)
376 if IOVbase==
"lumibase":
379 first = ibeam.IOVfirst
382 atime = ibeam.IOVBeginTime
383 first = time.mktime( time.strptime(atime.split()[0] +
" " + atime.split()[1] +
" " + atime.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
384 atime = ibeam.IOVEndTime
385 last = time.mktime( time.strptime(atime.split()[0] +
" " + atime.split()[1] +
" " + atime.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
386 da_first = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',time.localtime(first - time.timezone)))
387 da_last = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',time.localtime(last - time.timezone)))
392 print "GMT = " + str(time.strftime(
'%Y-%m-%d %H:%M:%S',time.gmtime(first - time.timezone)))
394 ptm = time.localtime(reftime)
395 da = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',ptm))
396 if time.daylight
and ptm.tm_isdst:
397 offset_daylight = time.timezone - time.altzone
398 ROOT.gStyle.SetTimeOffset(da.Convert(1) - 3600)
400 datax = (float(last) - float(first))/2 + float(first) - da.Convert() + 3600
402 if time.daylight
and ptm.tm_isdst:
403 datax += offset_daylight
406 dataxerr = (float(last) - float(first))/2
407 graphlist[ig].SetPoint(ipoint, float(datax), float(datay) )
408 graphlist[ig].SetPointError(ipoint, float(dataxerr), float(datayerr) )
410 graphlist[ig].GetXaxis().SetBinLabel(ipoint +1 , str(datax) )
411 graphlist[ig].SetBinContent(ipoint +1, float(datay) )
412 graphlist[ig].SetBinError(ipoint +1, float(datayerr) )
418 print "GMT = " + str(time.strftime(
'%Y-%m-%d %H:%M:%S',time.gmtime(last - time.timezone)))
419 graphlist[ig].GetXaxis().SetTimeDisplay(1);
420 graphlist[ig].GetXaxis().SetTimeFormat(
"#splitline{%Y/%m/%d}{%H:%M}")
422 graphlist[ig].Draw(
'AP')
424 graphlist[ig].Draw(
'P E1 X0')
425 graphlist[ig].GetXaxis().SetTitle(graphXaxis)
426 graphlist[ig].GetYaxis().SetTitle(graphYaxis[ig])
433 suffix = option.suffix
434 cvlist[ig].
Print(graphnamelist[ig]+
"_"+suffix+
".png")
436 raw_input(
'Press ENTER to continue\n ' )
440 outroot = TFile(option.output,
"RECREATE")
445 print " plots have been written to "+option.output
Evaluator * parse(const T &text)
def createWeightedPayloads
CREATE FILE FOR PAYLOADS.
def sortAndCleanBeamList
Sort and clean list of data for consecutive duplicates and bad fits.