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 from builtins
import range
48 import os, string, re, sys, math
50 from BeamSpotObj
import BeamSpot
51 from IOVObj
import IOV
52 from CommonMethods
import *
57 print(
"\nCannot load PYROOT, make sure you have setup ROOT in the path")
58 print(
"and pyroot library is also defined in the variable PYTHONPATH, try:\n")
59 if (os.getenv(
"PYTHONPATH")):
60 print(
" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
62 print(
" setenv PYTHONPATH $ROOTSYS/lib\n")
65 from ROOT
import TFile, TGraphErrors, TGaxis, TDatime
66 from ROOT
import TCanvas, TH1F
73 ROOT.gStyle.SetCanvasBorderMode(0)
74 ROOT.gStyle.SetCanvasColor(0)
75 ROOT.gStyle.SetCanvasDefH(600)
76 ROOT.gStyle.SetCanvasDefW(600)
77 ROOT.gStyle.SetCanvasDefX(0)
78 ROOT.gStyle.SetCanvasDefY(0)
80 ROOT.gStyle.SetPadBorderMode(0)
81 ROOT.gStyle.SetPadColor(0)
82 ROOT.gStyle.SetPadGridX(
False)
83 ROOT.gStyle.SetPadGridY(
False)
84 ROOT.gStyle.SetGridColor(0)
85 ROOT.gStyle.SetGridStyle(3)
86 ROOT.gStyle.SetGridWidth(1)
88 ROOT.gStyle.SetFrameBorderMode(0)
89 ROOT.gStyle.SetFrameFillColor(0)
90 ROOT.gStyle.SetTitleColor(1)
91 ROOT.gStyle.SetStatColor(0)
94 ROOT.gStyle.SetPaperSize(20,26)
95 ROOT.gStyle.SetPadTopMargin(0.04)
96 ROOT.gStyle.SetPadRightMargin(0.04)
97 ROOT.gStyle.SetPadBottomMargin(0.14)
98 ROOT.gStyle.SetPadLeftMargin(0.11)
99 ROOT.gStyle.SetPadTickX(1)
100 ROOT.gStyle.SetPadTickY(1)
102 ROOT.gStyle.SetTextFont(42)
103 ROOT.gStyle.SetTextSize(0.09)
104 ROOT.gStyle.SetLabelFont(42,
"xyz")
105 ROOT.gStyle.SetTitleFont(42,
"xyz")
106 ROOT.gStyle.SetLabelSize(0.035,
"xyz")
107 ROOT.gStyle.SetTitleSize(0.045,
"xyz")
108 ROOT.gStyle.SetTitleOffset(1.1,
"y")
111 ROOT.gStyle.SetMarkerStyle(8)
112 ROOT.gStyle.SetHistLineWidth(2)
113 ROOT.gStyle.SetLineWidth(1)
116 ROOT.gStyle.SetMarkerSize(0.6)
119 ROOT.gStyle.SetOptTitle(0)
120 ROOT.gStyle.SetOptStat(0)
121 ROOT.gStyle.SetOptFit(0)
125 ROOT.gROOT.ForceStyle()
129 if __name__ ==
'__main__':
137 Banner =
"CMS Preliminary" 142 if not args
and not option:
exit()
145 if not option.tag
and not option.data:
146 print(
" need to provide DB tag name or beam spot data file")
152 ROOT.gROOT.SetBatch()
154 datafilename =
"tmp_beamspot.dat" 156 datafilename = option.create
164 if option.IOVbase !=
"runbase" and option.IOVbase !=
"lumibase" and option.IOVbase !=
"timebase":
165 print(
"\n\n unknown iov base option: "+ option.IOVbase +
" \n\n\n")
167 IOVbase = option.IOVbase
170 lastRun =
"4999999999" 171 if IOVbase ==
"lumibase":
173 lastRun =
"4999999999:4999999999" 176 firstRun = option.initial
178 lastRun = option.final
185 print(
" read DB to get list of IOVs for the given tag")
186 mydestdb =
'frontier://PromptProd/CMS_COND_31X_BEAMSPOT' 188 mydestdb = option.destDB
189 acommand =
'cmscond_list_iov -c '+mydestdb+
' -P /afs/cern.ch/cms/DB/conddb -t '+ tag
190 tmpstatus = commands.getstatusoutput( acommand )
191 tmplistiov = tmpstatus[1].
split(
'\n')
197 totlines = len(tmplistiov)
198 for line
in tmplistiov:
200 if line.find(
'since') != -1:
203 if passline
and iline > jline
and iline < totlines-1:
204 linedata = line.split()
207 aIOV.since =
int(linedata[0])
208 aIOV.till =
int(linedata[1])
209 iovlist.append( aIOV )
212 print(
" total number of IOVs = " +
str(len(iovlist)))
220 otherArgs =
" -d " + option.destDB
222 otherArgs = otherArgs +
" -a "+ option.auth
224 print(
" get beam spot data from DB for IOVs. This can take a few minutes ...")
226 tmpfile = open(datafilename,
'w')
230 tmprunfirst = firstRun
233 tmplumilast = 9999999
234 if IOVbase==
"lumibase":
239 tmprunfirst =
pack(
int(firstRun.split(
":")[0]) ,
int(firstRun.split(
":")[1]) )
240 tmprunlast =
pack(
int(lastRun.split(
":")[0]) ,
int(lastRun.split(
":")[1]) )
242 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) < 0
and iIOV.since <=
int(tmprunfirst):
245 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) > 0
and iIOV.till <=
int(tmprunlast):
246 print(
" a IOV: " +
str(iIOV.since) +
" to " +
str(iIOV.till))
252 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(iIOV.since) +otherArgs
253 if IOVbase==
"lumibase":
256 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(tmprun) +
" -l "+
str(tmplumi) +otherArgs
258 status = commands.getstatusoutput( acommand )
259 tmpfile.write(status[1])
261 print(
" beam spot data collected and stored in file " + datafilename)
271 if os.path.isdir(option.data):
272 tmp = commands.getstatusoutput(
"ls "+option.data)
274 datafilename =
"combined_all.txt" 275 output = open(datafilename,
"w")
278 input = open(option.data +
"/"+f)
279 output.writelines(input.readlines())
281 print(
" data files have been collected in "+datafilename)
283 elif os.path.exists(option.data):
284 datafilename = option.data
286 print(
" input beam spot data DOES NOT exist, file " + option.data)
294 print(
"List of bunch crossings in the file:")
295 print(listmap.keys())
296 listbeam = listmap[option.Xrossing]
302 if IOVbase ==
"lumibase" and option.payload:
304 if not option.weighted:
308 print(
" no plots requested, exit now.")
312 TGaxis.SetMaxDigits(8)
315 graphnamelist = [
'X',
'Y',
'Z',
'SigmaZ',
'dxdz',
'dydz',
'beamWidthX',
'beamWidthY']
316 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']
317 graphXaxis =
'Run number' 318 if IOVbase ==
'runbase':
319 graphXaxis =
"Run number" 320 if IOVbase ==
'lumibase':
321 graphXaxis =
'Lumi section' 322 if IOVbase ==
'timebase' or option.Time:
325 ROOT.gStyle.SetTimeOffset(0)
327 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]']
331 for ig
in range(0,8):
332 cvlist.append( TCanvas(graphnamelist[ig],graphtitlelist[ig], 1200, 600) )
334 graphlist.append( TGraphErrors( len(listbeam) ) )
336 graphlist.append( TH1F(
"name",
"title",len(listbeam),0,len(listbeam)) )
338 graphlist[ig].SetName(graphnamelist[ig])
339 graphlist[ig].SetTitle(graphtitlelist[ig])
341 for ii
in range(0,len(listbeam)):
344 datax = dataxerr = 0.
345 datay = datayerr = 0.
346 if graphnamelist[ig] ==
'X':
348 datayerr = ibeam.Xerr
349 if graphnamelist[ig] ==
'Y':
351 datayerr = ibeam.Yerr
352 if graphnamelist[ig] ==
'Z':
354 datayerr = ibeam.Zerr
355 if graphnamelist[ig] ==
'SigmaZ':
357 datayerr = ibeam.sigmaZerr
358 if graphnamelist[ig] ==
'dxdz':
360 datayerr = ibeam.dxdzerr
361 if graphnamelist[ig] ==
'dydz':
363 datayerr = ibeam.dydzerr
364 if graphnamelist[ig] ==
'beamWidthX':
365 datay = ibeam.beamWidthX
366 datayerr = ibeam.beamWidthXerr
367 if graphnamelist[ig] ==
'beamWidthY':
368 datay = ibeam.beamWidthY
369 datayerr = ibeam.beamWidthYerr
371 datax = ibeam.IOVfirst
372 if IOVbase==
"lumibase":
373 datax =
str(ibeam.Run) +
":" +
str(ibeam.IOVfirst)
374 if ibeam.IOVfirst != ibeam.IOVlast:
375 datax =
str(ibeam.Run) +
":" +
str(ibeam.IOVfirst)+
"-"+
str(ibeam.IOVlast)
378 if IOVbase==
"lumibase":
381 first = ibeam.IOVfirst
384 atime = ibeam.IOVBeginTime
385 first = time.mktime( time.strptime(atime.split()[0] +
" " + atime.split()[1] +
" " + atime.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
386 atime = ibeam.IOVEndTime
387 last = time.mktime( time.strptime(atime.split()[0] +
" " + atime.split()[1] +
" " + atime.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
388 da_first = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',time.localtime(first - time.timezone)))
389 da_last = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',time.localtime(last - time.timezone)))
394 print(
"GMT = " +
str(time.strftime(
'%Y-%m-%d %H:%M:%S',time.gmtime(first - time.timezone))))
396 ptm = time.localtime(reftime)
397 da = TDatime(time.strftime(
'%Y-%m-%d %H:%M:%S',ptm))
398 if time.daylight
and ptm.tm_isdst:
399 offset_daylight = time.timezone - time.altzone
400 ROOT.gStyle.SetTimeOffset(da.Convert(1) - 3600)
402 datax = (
float(last) -
float(first))/2 +
float(first) - da.Convert() + 3600
404 if time.daylight
and ptm.tm_isdst:
405 datax += offset_daylight
409 graphlist[ig].SetPoint(ipoint,
float(datax),
float(datay) )
410 graphlist[ig].SetPointError(ipoint,
float(dataxerr),
float(datayerr) )
412 graphlist[ig].GetXaxis().SetBinLabel(ipoint +1 ,
str(datax) )
413 graphlist[ig].SetBinContent(ipoint +1,
float(datay) )
414 graphlist[ig].SetBinError(ipoint +1,
float(datayerr) )
420 print(
"GMT = " +
str(time.strftime(
'%Y-%m-%d %H:%M:%S',time.gmtime(last - time.timezone))))
421 graphlist[ig].GetXaxis().SetTimeDisplay(1);
422 graphlist[ig].GetXaxis().SetTimeFormat(
"#splitline{%Y/%m/%d}{%H:%M}")
424 graphlist[ig].Draw(
'AP')
426 graphlist[ig].Draw(
'P E1 X0')
427 graphlist[ig].GetXaxis().SetTitle(graphXaxis)
428 graphlist[ig].GetYaxis().SetTitle(graphYaxis[ig])
435 suffix = option.suffix
436 cvlist[ig].Print(graphnamelist[ig]+
"_"+suffix+
".png")
438 raw_input(
'Press ENTER to continue\n ' )
442 outroot = TFile(option.output,
"RECREATE")
447 print(
" plots have been written to "+option.output)
std::vector< std::string_view > split(std::string_view, const char *)
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.
std::pair< unsigned int, unsigned int > unpack(cond::Time_t since)
def sortAndCleanBeamList(listbeam=[], IOVbase="lumibase")
Sort and clean list of data for consecutive duplicates and bad fits.