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) 44 from __future__
import print_function
47 import os, string, re, sys, math
49 from BeamSpotObj
import BeamSpot
50 from IOVObj
import IOV
51 from CommonMethods
import *
56 print(
"\nCannot load PYROOT, make sure you have setup ROOT in the path")
57 print(
"and pyroot library is also defined in the variable PYTHONPATH, try:\n")
58 if (os.getenv(
"PYTHONPATH")):
59 print(
" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
61 print(
" setenv PYTHONPATH $ROOTSYS/lib\n")
64 from ROOT
import TFile, TGraphErrors, TGaxis, TDatime
65 from ROOT
import TCanvas, TH1F
72 ROOT.gStyle.SetCanvasBorderMode(0)
73 ROOT.gStyle.SetCanvasColor(0)
74 ROOT.gStyle.SetCanvasDefH(600)
75 ROOT.gStyle.SetCanvasDefW(600)
76 ROOT.gStyle.SetCanvasDefX(0)
77 ROOT.gStyle.SetCanvasDefY(0)
79 ROOT.gStyle.SetPadBorderMode(0)
80 ROOT.gStyle.SetPadColor(0)
81 ROOT.gStyle.SetPadGridX(
False)
82 ROOT.gStyle.SetPadGridY(
False)
83 ROOT.gStyle.SetGridColor(0)
84 ROOT.gStyle.SetGridStyle(3)
85 ROOT.gStyle.SetGridWidth(1)
87 ROOT.gStyle.SetFrameBorderMode(0)
88 ROOT.gStyle.SetFrameFillColor(0)
89 ROOT.gStyle.SetTitleColor(1)
90 ROOT.gStyle.SetStatColor(0)
93 ROOT.gStyle.SetPaperSize(20,26)
94 ROOT.gStyle.SetPadTopMargin(0.04)
95 ROOT.gStyle.SetPadRightMargin(0.04)
96 ROOT.gStyle.SetPadBottomMargin(0.14)
97 ROOT.gStyle.SetPadLeftMargin(0.11)
98 ROOT.gStyle.SetPadTickX(1)
99 ROOT.gStyle.SetPadTickY(1)
101 ROOT.gStyle.SetTextFont(42)
102 ROOT.gStyle.SetTextSize(0.09)
103 ROOT.gStyle.SetLabelFont(42,
"xyz")
104 ROOT.gStyle.SetTitleFont(42,
"xyz")
105 ROOT.gStyle.SetLabelSize(0.035,
"xyz")
106 ROOT.gStyle.SetTitleSize(0.045,
"xyz")
107 ROOT.gStyle.SetTitleOffset(1.1,
"y")
110 ROOT.gStyle.SetMarkerStyle(8)
111 ROOT.gStyle.SetHistLineWidth(2)
112 ROOT.gStyle.SetLineWidth(1)
115 ROOT.gStyle.SetMarkerSize(0.6)
118 ROOT.gStyle.SetOptTitle(0)
119 ROOT.gStyle.SetOptStat(0)
120 ROOT.gStyle.SetOptFit(0)
124 ROOT.gROOT.ForceStyle()
128 if __name__ ==
'__main__':
136 Banner =
"CMS Preliminary" 141 if not args
and not option:
exit()
144 if not option.tag
and not option.data:
145 print(
" need to provide DB tag name or beam spot data file")
151 ROOT.gROOT.SetBatch()
153 datafilename =
"tmp_beamspot.dat" 155 datafilename = option.create
163 if option.IOVbase !=
"runbase" and option.IOVbase !=
"lumibase" and option.IOVbase !=
"timebase":
164 print(
"\n\n unknown iov base option: "+ option.IOVbase +
" \n\n\n")
166 IOVbase = option.IOVbase
169 lastRun =
"4999999999" 170 if IOVbase ==
"lumibase":
172 lastRun =
"4999999999:4999999999" 175 firstRun = option.initial
177 lastRun = option.final
184 print(
" read DB to get list of IOVs for the given tag")
185 mydestdb =
'frontier://PromptProd/CMS_COND_31X_BEAMSPOT' 187 mydestdb = option.destDB
188 acommand =
'cmscond_list_iov -c '+mydestdb+
' -P /afs/cern.ch/cms/DB/conddb -t '+ tag
189 tmpstatus = commands.getstatusoutput( acommand )
190 tmplistiov = tmpstatus[1].
split(
'\n')
196 totlines = len(tmplistiov)
197 for line
in tmplistiov:
199 if line.find(
'since') != -1:
202 if passline
and iline > jline
and iline < totlines-1:
203 linedata = line.split()
206 aIOV.since =
int(linedata[0])
207 aIOV.till =
int(linedata[1])
208 iovlist.append( aIOV )
211 print(
" total number of IOVs = " +
str(len(iovlist)))
219 otherArgs =
" -d " + option.destDB
221 otherArgs = otherArgs +
" -a "+ option.auth
223 print(
" get beam spot data from DB for IOVs. This can take a few minutes ...")
225 tmpfile = open(datafilename,
'w')
229 tmprunfirst = firstRun
232 tmplumilast = 9999999
233 if IOVbase==
"lumibase":
238 tmprunfirst =
pack(
int(firstRun.split(
":")[0]) ,
int(firstRun.split(
":")[1]) )
239 tmprunlast =
pack(
int(lastRun.split(
":")[0]) ,
int(lastRun.split(
":")[1]) )
241 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) < 0
and iIOV.since <=
int(tmprunfirst):
244 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) > 0
and iIOV.till <=
int(tmprunlast):
245 print(
" a IOV: " +
str(iIOV.since) +
" to " +
str(iIOV.till))
251 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(iIOV.since) +otherArgs
252 if IOVbase==
"lumibase":
255 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(tmprun) +
" -l "+
str(tmplumi) +otherArgs
257 status = commands.getstatusoutput( acommand )
258 tmpfile.write(status[1])
260 print(
" beam spot data collected and stored in file " + datafilename)
270 if os.path.isdir(option.data):
271 tmp = commands.getstatusoutput(
"ls "+option.data)
273 datafilename =
"combined_all.txt" 274 output = open(datafilename,
"w")
277 input = open(option.data +
"/"+f)
278 output.writelines(input.readlines())
280 print(
" data files have been collected in "+datafilename)
282 elif os.path.exists(option.data):
283 datafilename = option.data
285 print(
" input beam spot data DOES NOT exist, file " + option.data)
293 print(
"List of bunch crossings in the file:")
294 print(listmap.keys())
295 listbeam = listmap[option.Xrossing]
301 if IOVbase ==
"lumibase" and option.payload:
303 if not option.weighted:
307 print(
" no plots requested, exit now.")
311 TGaxis.SetMaxDigits(8)
314 graphnamelist = [
'X',
'Y',
'Z',
'SigmaZ',
'dxdz',
'dydz',
'beamWidthX',
'beamWidthY']
315 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']
316 graphXaxis =
'Run number' 317 if IOVbase ==
'runbase':
318 graphXaxis =
"Run number" 319 if IOVbase ==
'lumibase':
320 graphXaxis =
'Lumi section' 321 if IOVbase ==
'timebase' or option.Time:
324 ROOT.gStyle.SetTimeOffset(0)
326 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]']
330 for ig
in range(0,8):
331 cvlist.append( TCanvas(graphnamelist[ig],graphtitlelist[ig], 1200, 600) )
333 graphlist.append( TGraphErrors( len(listbeam) ) )
335 graphlist.append( TH1F(
"name",
"title",len(listbeam),0,len(listbeam)) )
337 graphlist[ig].SetName(graphnamelist[ig])
338 graphlist[ig].SetTitle(graphtitlelist[ig])
340 for ii
in range(0,len(listbeam)):
343 datax = dataxerr = 0.
344 datay = datayerr = 0.
345 if graphnamelist[ig] ==
'X':
347 datayerr = ibeam.Xerr
348 if graphnamelist[ig] ==
'Y':
350 datayerr = ibeam.Yerr
351 if graphnamelist[ig] ==
'Z':
353 datayerr = ibeam.Zerr
354 if graphnamelist[ig] ==
'SigmaZ':
356 datayerr = ibeam.sigmaZerr
357 if graphnamelist[ig] ==
'dxdz':
359 datayerr = ibeam.dxdzerr
360 if graphnamelist[ig] ==
'dydz':
362 datayerr = ibeam.dydzerr
363 if graphnamelist[ig] ==
'beamWidthX':
364 datay = ibeam.beamWidthX
365 datayerr = ibeam.beamWidthXerr
366 if graphnamelist[ig] ==
'beamWidthY':
367 datay = ibeam.beamWidthY
368 datayerr = ibeam.beamWidthYerr
370 datax = ibeam.IOVfirst
371 if IOVbase==
"lumibase":
372 datax =
str(ibeam.Run) +
":" +
str(ibeam.IOVfirst)
373 if ibeam.IOVfirst != ibeam.IOVlast:
374 datax =
str(ibeam.Run) +
":" +
str(ibeam.IOVfirst)+
"-"+
str(ibeam.IOVlast)
377 if IOVbase==
"lumibase":
380 first = ibeam.IOVfirst
383 atime = ibeam.IOVBeginTime
384 first = time.mktime( time.strptime(atime.split()[0] +
" " + atime.split()[1] +
" " + atime.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
385 atime = ibeam.IOVEndTime
386 last = time.mktime( time.strptime(atime.split()[0] +
" " + atime.split()[1] +
" " + atime.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
387 da_first = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',time.localtime(first - time.timezone)))
388 da_last = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',time.localtime(last - time.timezone)))
393 print(
"GMT = " +
str(time.strftime(
'%Y-%m-%d %H:%M:%S',time.gmtime(first - time.timezone))))
395 ptm = time.localtime(reftime)
396 da = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',ptm))
397 if time.daylight
and ptm.tm_isdst:
398 offset_daylight = time.timezone - time.altzone
399 ROOT.gStyle.SetTimeOffset(da.Convert(1) - 3600)
401 datax = (
float(last) -
float(first))/2 +
float(first) - da.Convert() + 3600
403 if time.daylight
and ptm.tm_isdst:
404 datax += offset_daylight
408 graphlist[ig].SetPoint(ipoint,
float(datax),
float(datay) )
409 graphlist[ig].SetPointError(ipoint,
float(dataxerr),
float(datayerr) )
411 graphlist[ig].GetXaxis().SetBinLabel(ipoint +1 ,
str(datax) )
412 graphlist[ig].SetBinContent(ipoint +1,
float(datay) )
413 graphlist[ig].SetBinError(ipoint +1,
float(datayerr) )
419 print(
"GMT = " +
str(time.strftime(
'%Y-%m-%d %H:%M:%S',time.gmtime(last - time.timezone))))
420 graphlist[ig].GetXaxis().SetTimeDisplay(1);
421 graphlist[ig].GetXaxis().SetTimeFormat(
"#splitline{%Y/%m/%d}{%H:%M}")
423 graphlist[ig].Draw(
'AP')
425 graphlist[ig].Draw(
'P E1 X0')
426 graphlist[ig].GetXaxis().SetTitle(graphXaxis)
427 graphlist[ig].GetYaxis().SetTitle(graphYaxis[ig])
434 suffix = option.suffix
435 cvlist[ig].Print(graphnamelist[ig]+
"_"+suffix+
".png")
437 raw_input(
'Press ENTER to continue\n ' )
441 outroot = TFile(option.output,
"RECREATE")
446 print(
" plots have been written to "+option.output)
def readBeamSpotFile(fileName, listbeam=[], IOVbase="runbase", firstRun='1', lastRun='4999999999')
S & print(S &os, JobReport::InputFile const &f)
def createWeightedPayloads(fileName, listbeam=[], weighted=True)
CREATE FILE FOR PAYLOADS.
def sortAndCleanBeamList(listbeam=[], IOVbase="lumibase")
Sort and clean list of data for consecutive duplicates and bad fits.