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 -o, --output = OUTPUT: filename of ROOT file with plots.
28 -x, --xcrossing = XCROSSING : Bunch crossing number.
30 Francisco Yumiceva (yumiceva@fnal.gov)
34 from __future__
import print_function
37 from builtins
import range
38 import os, string, re, sys, math
40 from BeamSpotObj
import BeamSpot
41 from IOVObj
import IOV
42 from CommonMethods
import *
47 print(
"\nCannot load PYROOT, make sure you have setup ROOT in the path")
48 print(
"and pyroot library is also defined in the variable PYTHONPATH, try:\n")
49 if (os.getenv(
"PYTHONPATH")):
50 print(
" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
52 print(
" setenv PYTHONPATH $ROOTSYS/lib\n")
56 from array
import array
66 run0 =
int(json[i][0])
67 run1 =
int(json[i][1])
68 if run>= run0
and run<=run1:
73 if __name__ ==
'__main__':
79 runsfile = open(
"FillandRuns.txt")
81 if line.find(
'fill:') != -1:
86 FillList[
int(afill)] = [
int(run0),
int(run1)]
96 Float_t widthError[3];\
98 Float_t slopeError[2];\
106 fntuple = TFile(
'bntuple.root',
'RECREATE' )
107 tbylumi = TTree(
'bylumi',
'beam spot data lumi by lumi' )
108 tbylumi.Branch(
'fill', AddressOf( bntuple,
'fill'),
'fill/I' )
109 tbylumi.Branch(
'run', AddressOf( bntuple,
'run'),
'run/I' )
110 tbylumi.Branch(
'lumi', AddressOf( bntuple,
'lumi'),
'lumi[2]/I' )
111 tbylumi.Branch(
'position', AddressOf( bntuple,
'position'),
'position[3]/F')
112 tbylumi.Branch(
'posErr', AddressOf( bntuple,
'posError'),
'posError[3]/F')
113 tbylumi.Branch(
'width', AddressOf( bntuple,
'width'),
'width[3]/F')
114 tbylumi.Branch(
'widthErr', AddressOf( bntuple,
'widthError'),
'widthError[3]/F')
115 tbylumi.Branch(
'slope', AddressOf( bntuple,
'slope'),
'slope[2]/F')
116 tbylumi.Branch(
'slopeErr', AddressOf( bntuple,
'slopeError'),
'slopeError[2]/F')
117 tbylumi.Branch(
'time', AddressOf( bntuple,
'time'),
'time[2]/F')
119 tbyIOV = TTree(
'byIOV',
'beam spot data by IOV' )
120 tbyIOV.Branch(
'fill', AddressOf( bntuple,
'fill'),
'fill/I' )
121 tbyIOV.Branch(
'run', AddressOf( bntuple,
'run'),
'run/I' )
122 tbyIOV.Branch(
'lumi', AddressOf( bntuple,
'lumi'),
'lumi[2]/I' )
123 tbyIOV.Branch(
'position', AddressOf( bntuple,
'position'),
'position[3]/F')
124 tbyIOV.Branch(
'posErr', AddressOf( bntuple,
'posError'),
'posError[3]/F')
125 tbyIOV.Branch(
'width', AddressOf( bntuple,
'width'),
'width[3]/F')
126 tbyIOV.Branch(
'widthErr', AddressOf( bntuple,
'widthError'),
'widthError[3]/F')
127 tbyIOV.Branch(
'slope', AddressOf( bntuple,
'slope'),
'slope[2]/F')
128 tbyIOV.Branch(
'slopeErr', AddressOf( bntuple,
'slopeError'),
'slopeError[2]/F')
129 tbyIOV.Branch(
'time', AddressOf( bntuple,
'time'),
'time[2]/F')
131 tbyrun = TTree(
'byrun',
'beam spot data by run' )
132 tbyrun.Branch(
'fill', AddressOf( bntuple,
'fill'),
'fill/I' )
133 tbyrun.Branch(
'run', AddressOf( bntuple,
'run'),
'run/I' )
134 tbyrun.Branch(
'lumi', AddressOf( bntuple,
'lumi'),
'lumi[2]/I' )
135 tbyrun.Branch(
'position', AddressOf( bntuple,
'position'),
'position[3]/F')
136 tbyrun.Branch(
'posErr', AddressOf( bntuple,
'posError'),
'posError[3]/F')
137 tbyrun.Branch(
'width', AddressOf( bntuple,
'width'),
'width[3]/F')
138 tbyrun.Branch(
'widthErr', AddressOf( bntuple,
'widthError'),
'widthError[3]/F')
139 tbyrun.Branch(
'slope', AddressOf( bntuple,
'slope'),
'slope[2]/F')
140 tbyrun.Branch(
'slopeErr', AddressOf( bntuple,
'slopeError'),
'slopeError[2]/F')
141 tbyrun.Branch(
'time', AddressOf( bntuple,
'time'),
'time[2]/F')
147 if not args
and not option:
exit()
150 print(
" need to provide beam spot data file")
154 ROOT.gROOT.SetBatch()
156 datafilename =
"tmp_beamspot.dat"
158 datafilename = option.create
166 lastRun =
"4999999999:4999999999"
169 firstRun = option.initial
171 lastRun = option.final
178 print(
" read DB to get list of IOVs for the given tag")
179 acommand =
'cmscond_list_iov -c frontier://PromptProd/CMS_COND_31X_BEAMSPOT -P /afs/cern.ch/cms/DB/conddb -t '+ tag
180 tmpstatus = commands.getstatusoutput( acommand )
181 tmplistiov = tmpstatus[1].
split(
'\n')
187 totlines = len(tmplistiov)
188 for line
in tmplistiov:
190 if line.find(
'since') != -1:
193 if passline
and iline > jline
and iline < totlines-1:
194 linedata = line.split()
197 aIOV.since =
int(linedata[0])
198 aIOV.till =
int(linedata[1])
199 iovlist.append( aIOV )
202 print(
" total number of IOVs = " +
str(len(iovlist)))
210 otherArgs =
" -d " + option.destDB
212 otherArgs = otherArgs +
" -a "+ option.auth
214 print(
" get beam spot data from DB for IOVs. This can take a few minutes ...")
216 tmpfile = open(datafilename,
'w')
220 tmprunfirst = firstRun
223 tmplumilast = 9999999
224 if IOVbase==
"lumibase":
229 tmprunfirst =
pack(
int(firstRun.split(
":")[0]) ,
int(firstRun.split(
":")[1]) )
230 tmprunlast =
pack(
int(lastRun.split(
":")[0]) ,
int(lasstRun.split(
":")[1]) )
232 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) < 0
and iIOV.since <=
int(tmprunfirst):
235 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) > 0
and iIOV.till <=
int(tmprunlast):
236 print(
" IOV: " +
str(iIOV.since) +
" to " +
str(iIOV.till))
238 if iIOV.since >=
int(tmprunlast)
and iIOV.till >= 4294967295:
239 print(
" IOV: " +
str(iIOV.since) +
" to " +
str(iIOV.till))
242 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(iIOV.since) +otherArgs
243 if IOVbase==
"lumibase":
246 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(tmprun) +
" -l "+tmplumi +otherArgs
247 status = commands.getstatusoutput( acommand )
248 tmpfile.write(status[1])
250 print(
" beam spot data collected and stored in file " + datafilename)
260 if os.path.isdir(option.data):
261 tmp = commands.getstatusoutput(
"ls "+option.data)
263 datafilename =
"combined_all.txt"
264 output = open(datafilename,
"w")
267 if os.path.isdir(option.data+
"/"+f)
is False:
268 input = open(option.data +
"/"+f)
269 output.writelines(input.readlines())
271 print(
" data files have been collected in "+datafilename)
273 elif os.path.exists(option.data):
274 datafilename = option.data
276 print(
" input beam spot data DOES NOT exist, file " + option.data)
284 print(
"List of bunch crossings in the file:")
285 print(listmap.keys())
286 listbeam = listmap[option.Xrossing]
295 for ii
in range(0,len(listbeam)):
303 bntuple.run =
int(ibeam.Run)
305 bntuple.lumi =
array(
'i', [
int(ibeam.IOVfirst),
int(ibeam.IOVlast)])
306 line = ibeam.IOVBeginTime
307 begintime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
308 line = ibeam.IOVEndTime
309 endtime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
310 bntuple.time =
array(
'f', [begintime, endtime])
317 for ii
in range(0,len(iovlist)):
323 bntuple.width =
array(
'f', [
float(ibeam.beamWidthX),
float(ibeam.beamWidthY),
float(ibeam.sigmaZ)])
324 bntuple.widthError =
array(
'f',[
float(ibeam.beamWidthXerr),
float(ibeam.beamWidthYerr),
float(ibeam.sigmaZerr)])
325 bntuple.run =
int(ibeam.Run)
327 bntuple.lumi =
array(
'i', [
int(ibeam.IOVfirst),
int(ibeam.IOVlast)])
328 line = ibeam.IOVBeginTime
329 begintime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
330 line = ibeam.IOVEndTime
331 endtime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
332 bntuple.time =
array(
'f', [begintime, endtime])
336 weightedlist = listbeam
339 for ii
in range(0,len(weightedlist)):
341 ibeam = weightedlist[ii]
345 bntuple.width =
array(
'f', [
float(ibeam.beamWidthX),
float(ibeam.beamWidthY),
float(ibeam.sigmaZ)])
346 bntuple.widthError =
array(
'f',[
float(ibeam.beamWidthXerr),
float(ibeam.beamWidthYerr),
float(ibeam.sigmaZerr)])
347 bntuple.run =
int(ibeam.Run)
349 bntuple.lumi =
array(
'i', [
int(ibeam.IOVfirst),
int(ibeam.IOVlast)])
350 line = ibeam.IOVBeginTime
351 begintime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
352 line = ibeam.IOVEndTime
353 endtime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
354 bntuple.time =
array(
'f', [begintime, endtime])
359 os.system(
'rm tmp.txt')