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 import os, string, re, sys, math
39 from BeamSpotObj
import BeamSpot
40 from IOVObj
import IOV
41 from CommonMethods
import *
46 print(
"\nCannot load PYROOT, make sure you have setup ROOT in the path")
47 print(
"and pyroot library is also defined in the variable PYTHONPATH, try:\n")
48 if (os.getenv(
"PYTHONPATH")):
49 print(
" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
51 print(
" setenv PYTHONPATH $ROOTSYS/lib\n")
55 from array
import array
65 run0 =
int(json[i][0])
66 run1 =
int(json[i][1])
67 if run>= run0
and run<=run1:
72 if __name__ ==
'__main__':
78 runsfile = open(
"FillandRuns.txt")
80 if line.find(
'fill:') != -1:
85 FillList[
int(afill)] = [
int(run0),
int(run1)]
95 Float_t widthError[3];\ 97 Float_t slopeError[2];\ 105 fntuple = TFile(
'bntuple.root',
'RECREATE' )
106 tbylumi = TTree(
'bylumi',
'beam spot data lumi by lumi' )
107 tbylumi.Branch(
'fill', AddressOf( bntuple,
'fill'),
'fill/I' )
108 tbylumi.Branch(
'run', AddressOf( bntuple,
'run'),
'run/I' )
109 tbylumi.Branch(
'lumi', AddressOf( bntuple,
'lumi'),
'lumi[2]/I' )
110 tbylumi.Branch(
'position', AddressOf( bntuple,
'position'),
'position[3]/F')
111 tbylumi.Branch(
'posErr', AddressOf( bntuple,
'posError'),
'posError[3]/F')
112 tbylumi.Branch(
'width', AddressOf( bntuple,
'width'),
'width[3]/F')
113 tbylumi.Branch(
'widthErr', AddressOf( bntuple,
'widthError'),
'widthError[3]/F')
114 tbylumi.Branch(
'slope', AddressOf( bntuple,
'slope'),
'slope[2]/F')
115 tbylumi.Branch(
'slopeErr', AddressOf( bntuple,
'slopeError'),
'slopeError[2]/F')
116 tbylumi.Branch(
'time', AddressOf( bntuple,
'time'),
'time[2]/F')
118 tbyIOV = TTree(
'byIOV',
'beam spot data by IOV' )
119 tbyIOV.Branch(
'fill', AddressOf( bntuple,
'fill'),
'fill/I' )
120 tbyIOV.Branch(
'run', AddressOf( bntuple,
'run'),
'run/I' )
121 tbyIOV.Branch(
'lumi', AddressOf( bntuple,
'lumi'),
'lumi[2]/I' )
122 tbyIOV.Branch(
'position', AddressOf( bntuple,
'position'),
'position[3]/F')
123 tbyIOV.Branch(
'posErr', AddressOf( bntuple,
'posError'),
'posError[3]/F')
124 tbyIOV.Branch(
'width', AddressOf( bntuple,
'width'),
'width[3]/F')
125 tbyIOV.Branch(
'widthErr', AddressOf( bntuple,
'widthError'),
'widthError[3]/F')
126 tbyIOV.Branch(
'slope', AddressOf( bntuple,
'slope'),
'slope[2]/F')
127 tbyIOV.Branch(
'slopeErr', AddressOf( bntuple,
'slopeError'),
'slopeError[2]/F')
128 tbyIOV.Branch(
'time', AddressOf( bntuple,
'time'),
'time[2]/F')
130 tbyrun = TTree(
'byrun',
'beam spot data by run' )
131 tbyrun.Branch(
'fill', AddressOf( bntuple,
'fill'),
'fill/I' )
132 tbyrun.Branch(
'run', AddressOf( bntuple,
'run'),
'run/I' )
133 tbyrun.Branch(
'lumi', AddressOf( bntuple,
'lumi'),
'lumi[2]/I' )
134 tbyrun.Branch(
'position', AddressOf( bntuple,
'position'),
'position[3]/F')
135 tbyrun.Branch(
'posErr', AddressOf( bntuple,
'posError'),
'posError[3]/F')
136 tbyrun.Branch(
'width', AddressOf( bntuple,
'width'),
'width[3]/F')
137 tbyrun.Branch(
'widthErr', AddressOf( bntuple,
'widthError'),
'widthError[3]/F')
138 tbyrun.Branch(
'slope', AddressOf( bntuple,
'slope'),
'slope[2]/F')
139 tbyrun.Branch(
'slopeErr', AddressOf( bntuple,
'slopeError'),
'slopeError[2]/F')
140 tbyrun.Branch(
'time', AddressOf( bntuple,
'time'),
'time[2]/F')
146 if not args
and not option:
exit()
149 print(
" need to provide beam spot data file")
153 ROOT.gROOT.SetBatch()
155 datafilename =
"tmp_beamspot.dat" 157 datafilename = option.create
165 lastRun =
"4999999999:4999999999" 168 firstRun = option.initial
170 lastRun = option.final
177 print(
" read DB to get list of IOVs for the given tag")
178 acommand =
'cmscond_list_iov -c frontier://PromptProd/CMS_COND_31X_BEAMSPOT -P /afs/cern.ch/cms/DB/conddb -t '+ tag
179 tmpstatus = commands.getstatusoutput( acommand )
180 tmplistiov = tmpstatus[1].
split(
'\n')
186 totlines = len(tmplistiov)
187 for line
in tmplistiov:
189 if line.find(
'since') != -1:
192 if passline
and iline > jline
and iline < totlines-1:
193 linedata = line.split()
196 aIOV.since =
int(linedata[0])
197 aIOV.till =
int(linedata[1])
198 iovlist.append( aIOV )
201 print(
" total number of IOVs = " +
str(len(iovlist)))
209 otherArgs =
" -d " + option.destDB
211 otherArgs = otherArgs +
" -a "+ option.auth
213 print(
" get beam spot data from DB for IOVs. This can take a few minutes ...")
215 tmpfile = open(datafilename,
'w')
219 tmprunfirst = firstRun
222 tmplumilast = 9999999
223 if IOVbase==
"lumibase":
228 tmprunfirst =
pack(
int(firstRun.split(
":")[0]) ,
int(firstRun.split(
":")[1]) )
229 tmprunlast =
pack(
int(lastRun.split(
":")[0]) ,
int(lasstRun.split(
":")[1]) )
231 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) < 0
and iIOV.since <=
int(tmprunfirst):
234 if iIOV.since >=
int(tmprunfirst)
and int(tmprunlast) > 0
and iIOV.till <=
int(tmprunlast):
235 print(
" IOV: " +
str(iIOV.since) +
" to " +
str(iIOV.till))
237 if iIOV.since >=
int(tmprunlast)
and iIOV.till >= 4294967295:
238 print(
" IOV: " +
str(iIOV.since) +
" to " +
str(iIOV.till))
241 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(iIOV.since) +otherArgs
242 if IOVbase==
"lumibase":
245 acommand =
'getBeamSpotDB.py -t '+ tag +
" -r " +
str(tmprun) +
" -l "+tmplumi +otherArgs
246 status = commands.getstatusoutput( acommand )
247 tmpfile.write(status[1])
249 print(
" beam spot data collected and stored in file " + datafilename)
259 if os.path.isdir(option.data):
260 tmp = commands.getstatusoutput(
"ls "+option.data)
262 datafilename =
"combined_all.txt" 263 output = open(datafilename,
"w")
266 if os.path.isdir(option.data+
"/"+f)
is False:
267 input = open(option.data +
"/"+f)
268 output.writelines(input.readlines())
270 print(
" data files have been collected in "+datafilename)
272 elif os.path.exists(option.data):
273 datafilename = option.data
275 print(
" input beam spot data DOES NOT exist, file " + option.data)
283 print(
"List of bunch crossings in the file:")
284 print(listmap.keys())
285 listbeam = listmap[option.Xrossing]
294 for ii
in range(0,len(listbeam)):
302 bntuple.run =
int(ibeam.Run)
304 bntuple.lumi =
array(
'i', [
int(ibeam.IOVfirst),
int(ibeam.IOVlast)])
305 line = ibeam.IOVBeginTime
306 begintime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
307 line = ibeam.IOVEndTime
308 endtime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
309 bntuple.time =
array(
'f', [begintime, endtime])
316 for ii
in range(0,len(iovlist)):
322 bntuple.width =
array(
'f', [
float(ibeam.beamWidthX),
float(ibeam.beamWidthY),
float(ibeam.sigmaZ)])
323 bntuple.widthError =
array(
'f',[
float(ibeam.beamWidthXerr),
float(ibeam.beamWidthYerr),
float(ibeam.sigmaZerr)])
324 bntuple.run =
int(ibeam.Run)
326 bntuple.lumi =
array(
'i', [
int(ibeam.IOVfirst),
int(ibeam.IOVlast)])
327 line = ibeam.IOVBeginTime
328 begintime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
329 line = ibeam.IOVEndTime
330 endtime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
331 bntuple.time =
array(
'f', [begintime, endtime])
335 weightedlist = listbeam
338 for ii
in range(0,len(weightedlist)):
340 ibeam = weightedlist[ii]
344 bntuple.width =
array(
'f', [
float(ibeam.beamWidthX),
float(ibeam.beamWidthY),
float(ibeam.sigmaZ)])
345 bntuple.widthError =
array(
'f',[
float(ibeam.beamWidthXerr),
float(ibeam.beamWidthYerr),
float(ibeam.sigmaZerr)])
346 bntuple.run =
int(ibeam.Run)
348 bntuple.lumi =
array(
'i', [
int(ibeam.IOVfirst),
int(ibeam.IOVlast)])
349 line = ibeam.IOVBeginTime
350 begintime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
351 line = ibeam.IOVEndTime
352 endtime = time.mktime( time.strptime(line.split()[0] +
" " + line.split()[1] +
" " + line.split()[2],
"%Y.%m.%d %H:%M:%S %Z") )
353 bntuple.time =
array(
'f', [begintime, endtime])
358 os.system(
'rm tmp.txt')
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.