File indexing completed on 2023-03-17 11:23:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 """
0015 ntuplemaker
0016
0017 A very simple script to plot the beam spot data stored in condDB
0018
0019 usage: %prog -t <tag name>
0020 -a, --auth = AUTH: DB authorization path. online(/nfshome0/popcondev/conddb).
0021 -b, --batch : Run ROOT in batch mode.
0022 -c, --create = CREATE: name for beam spot data file.
0023 -d, --data = DATA: input beam spot data file.
0024 -D, --destDB = DESTDB: destination DB string. online(oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT).
0025 -i, --initial = INITIAL: First IOV. Options: run number, or run:lumi, eg. \"133200:21\"
0026 -f, --final = FINAL: Last IOV. Options: run number, or run:lumi
0027 -o, --output = OUTPUT: filename of ROOT file with plots.
0028 -x, --xcrossing = XCROSSING : Bunch crossing number.
0029
0030 Francisco Yumiceva (yumiceva@fnal.gov)
0031 Fermilab 2010
0032
0033 """
0034 from __future__ import print_function
0035
0036
0037 from builtins import range
0038 import os, string, re, sys, math
0039 import subprocess, time
0040 from BeamSpotObj import BeamSpot
0041 from IOVObj import IOV
0042 from CommonMethods import *
0043
0044 try:
0045 import ROOT
0046 except:
0047 print("\nCannot load PYROOT, make sure you have setup ROOT in the path")
0048 print("and pyroot library is also defined in the variable PYTHONPATH, try:\n")
0049 if (os.getenv("PYTHONPATH")):
0050 print(" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
0051 else:
0052 print(" setenv PYTHONPATH $ROOTSYS/lib\n")
0053 sys.exit()
0054
0055 from ROOT import *
0056 from array import array
0057
0058 def getFill( json, run ):
0059
0060 thefill = 0
0061 run = int(run)
0062 keys = json.keys()
0063
0064 for i in keys:
0065
0066 run0 = int(json[i][0])
0067 run1 = int(json[i][1])
0068 if run>= run0 and run<=run1:
0069 thefill = i
0070
0071 return int(thefill)
0072
0073 if __name__ == '__main__':
0074
0075
0076
0077
0078 FillList = {}
0079 runsfile = open("FillandRuns.txt")
0080 for line in runsfile:
0081 if line.find('fill:') != -1:
0082 aline = line.split()
0083 afill = aline[1]
0084 run0 = aline[3]
0085 run1 = aline[5]
0086 FillList[int(afill)] = [int(run0),int(run1)]
0087
0088
0089
0090
0091 gROOT.ProcessLine(
0092 "struct spot {\
0093 Float_t position[3];\
0094 Float_t posError[3];\
0095 Float_t width[3];\
0096 Float_t widthError[3];\
0097 Float_t slope[2];\
0098 Float_t slopeError[2];\
0099 Float_t time[2];\
0100 Int_t run;\
0101 Int_t lumi[2];\
0102 Int_t fill;\
0103 };" );
0104
0105 bntuple = spot()
0106 fntuple = TFile( 'bntuple.root', 'RECREATE' )
0107 tbylumi = TTree( 'bylumi', 'beam spot data lumi by lumi' )
0108 tbylumi.Branch('fill', AddressOf( bntuple, 'fill'), 'fill/I' )
0109 tbylumi.Branch('run', AddressOf( bntuple, 'run'), 'run/I' )
0110 tbylumi.Branch('lumi', AddressOf( bntuple, 'lumi'), 'lumi[2]/I' )
0111 tbylumi.Branch('position', AddressOf( bntuple, 'position'),'position[3]/F')
0112 tbylumi.Branch('posErr', AddressOf( bntuple, 'posError'),'posError[3]/F')
0113 tbylumi.Branch('width', AddressOf( bntuple, 'width'),'width[3]/F')
0114 tbylumi.Branch('widthErr', AddressOf( bntuple, 'widthError'),'widthError[3]/F')
0115 tbylumi.Branch('slope', AddressOf( bntuple, 'slope'),'slope[2]/F')
0116 tbylumi.Branch('slopeErr', AddressOf( bntuple, 'slopeError'),'slopeError[2]/F')
0117 tbylumi.Branch('time', AddressOf( bntuple, 'time'),'time[2]/F')
0118
0119 tbyIOV = TTree( 'byIOV', 'beam spot data by IOV' )
0120 tbyIOV.Branch('fill', AddressOf( bntuple, 'fill'), 'fill/I' )
0121 tbyIOV.Branch('run', AddressOf( bntuple, 'run'), 'run/I' )
0122 tbyIOV.Branch('lumi', AddressOf( bntuple, 'lumi'), 'lumi[2]/I' )
0123 tbyIOV.Branch('position', AddressOf( bntuple, 'position'),'position[3]/F')
0124 tbyIOV.Branch('posErr', AddressOf( bntuple, 'posError'),'posError[3]/F')
0125 tbyIOV.Branch('width', AddressOf( bntuple, 'width'),'width[3]/F')
0126 tbyIOV.Branch('widthErr', AddressOf( bntuple, 'widthError'),'widthError[3]/F')
0127 tbyIOV.Branch('slope', AddressOf( bntuple, 'slope'),'slope[2]/F')
0128 tbyIOV.Branch('slopeErr', AddressOf( bntuple, 'slopeError'),'slopeError[2]/F')
0129 tbyIOV.Branch('time', AddressOf( bntuple, 'time'),'time[2]/F')
0130
0131 tbyrun = TTree( 'byrun', 'beam spot data by run' )
0132 tbyrun.Branch('fill', AddressOf( bntuple, 'fill'), 'fill/I' )
0133 tbyrun.Branch('run', AddressOf( bntuple, 'run'), 'run/I' )
0134 tbyrun.Branch('lumi', AddressOf( bntuple, 'lumi'), 'lumi[2]/I' )
0135 tbyrun.Branch('position', AddressOf( bntuple, 'position'),'position[3]/F')
0136 tbyrun.Branch('posErr', AddressOf( bntuple, 'posError'),'posError[3]/F')
0137 tbyrun.Branch('width', AddressOf( bntuple, 'width'),'width[3]/F')
0138 tbyrun.Branch('widthErr', AddressOf( bntuple, 'widthError'),'widthError[3]/F')
0139 tbyrun.Branch('slope', AddressOf( bntuple, 'slope'),'slope[2]/F')
0140 tbyrun.Branch('slopeErr', AddressOf( bntuple, 'slopeError'),'slopeError[2]/F')
0141 tbyrun.Branch('time', AddressOf( bntuple, 'time'),'time[2]/F')
0142
0143
0144
0145
0146 option,args = parse(__doc__)
0147 if not args and not option: exit()
0148
0149 if not option.data:
0150 print(" need to provide beam spot data file")
0151 exit()
0152
0153 if option.batch:
0154 ROOT.gROOT.SetBatch()
0155
0156 datafilename = "tmp_beamspot.dat"
0157 if option.create:
0158 datafilename = option.create
0159
0160 getDBdata = True
0161 if option.data:
0162 getDBdata = False
0163
0164 IOVbase = 'lumibase'
0165 firstRun = "0:0"
0166 lastRun = "4999999999:4999999999"
0167
0168 if option.initial:
0169 firstRun = option.initial
0170 if option.final:
0171 lastRun = option.final
0172
0173
0174
0175
0176 if getDBdata:
0177
0178 print(" read DB to get list of IOVs for the given tag")
0179 acommand = 'cmscond_list_iov -c frontier://PromptProd/CMS_COND_31X_BEAMSPOT -P /afs/cern.ch/cms/DB/conddb -t '+ tag
0180 tmpstatus = subprocess.getstatusoutput( acommand )
0181 tmplistiov = tmpstatus[1].split('\n')
0182
0183
0184 iovlist = []
0185 passline = False
0186 iline = jline = 0
0187 totlines = len(tmplistiov)
0188 for line in tmplistiov:
0189
0190 if line.find('since') != -1:
0191 passline = True
0192 jline = iline
0193 if passline and iline > jline and iline < totlines-1:
0194 linedata = line.split()
0195
0196 aIOV = IOV()
0197 aIOV.since = int(linedata[0])
0198 aIOV.till = int(linedata[1])
0199 iovlist.append( aIOV )
0200 iline += 1
0201
0202 print(" total number of IOVs = " + str(len(iovlist)))
0203
0204
0205
0206
0207
0208 otherArgs = ''
0209 if option.destDB:
0210 otherArgs = " -d " + option.destDB
0211 if option.auth:
0212 otherArgs = otherArgs + " -a "+ option.auth
0213
0214 print(" get beam spot data from DB for IOVs. This can take a few minutes ...")
0215
0216 tmpfile = open(datafilename,'w')
0217
0218 for iIOV in iovlist:
0219 passiov = False
0220 tmprunfirst = firstRun
0221 tmprunlast = lastRun
0222 tmplumifirst = 1
0223 tmplumilast = 9999999
0224 if IOVbase=="lumibase":
0225
0226
0227
0228
0229 tmprunfirst = pack( int(firstRun.split(":")[0]) , int(firstRun.split(":")[1]) )
0230 tmprunlast = pack( int(lastRun.split(":")[0]) , int(lasstRun.split(":")[1]) )
0231
0232 if iIOV.since >= int(tmprunfirst) and int(tmprunlast) < 0 and iIOV.since <= int(tmprunfirst):
0233 print(" IOV: " + str(iIOV.since))
0234 passiov = True
0235 if iIOV.since >= int(tmprunfirst) and int(tmprunlast) > 0 and iIOV.till <= int(tmprunlast):
0236 print(" IOV: " + str(iIOV.since) + " to " + str(iIOV.till))
0237 passiov = True
0238 if iIOV.since >= int(tmprunlast) and iIOV.till >= 4294967295:
0239 print(" IOV: " + str(iIOV.since) + " to " + str(iIOV.till))
0240 passiov = True
0241 if passiov:
0242 acommand = 'getBeamSpotDB.py -t '+ tag + " -r " + str(iIOV.since) +otherArgs
0243 if IOVbase=="lumibase":
0244 tmprun = unpack(iIOV.since)[0]
0245 tmplumi = unpack(iIOV.since)[1]
0246 acommand = 'getBeamSpotDB.py -t '+ tag + " -r " + str(tmprun) +" -l "+tmplumi +otherArgs
0247 status = subprocess.getstatusoutput( acommand )
0248 tmpfile.write(status[1])
0249
0250 print(" beam spot data collected and stored in file " + datafilename)
0251
0252 tmpfile.close()
0253
0254
0255
0256
0257
0258
0259 if option.data:
0260 if os.path.isdir(option.data):
0261 tmp = subprocess.getstatusoutput("ls "+option.data)
0262 files = tmp[1].split()
0263 datafilename = "combined_all.txt"
0264 output = open(datafilename,"w")
0265
0266 for f in files:
0267 if os.path.isdir(option.data+"/"+f) is False:
0268 input = open(option.data +"/"+f)
0269 output.writelines(input.readlines())
0270 output.close()
0271 print(" data files have been collected in "+datafilename)
0272
0273 elif os.path.exists(option.data):
0274 datafilename = option.data
0275 else:
0276 print(" input beam spot data DOES NOT exist, file " + option.data)
0277 exit()
0278
0279 listbeam = []
0280
0281 if option.xcrossing:
0282 listmap = readBeamSpotFile(datafilename,listbeam,IOVbase,firstRun,lastRun)
0283
0284 print("List of bunch crossings in the file:")
0285 print(listmap.keys())
0286 listbeam = listmap[option.Xrossing]
0287 else:
0288 readBeamSpotFile(datafilename,listbeam,IOVbase,firstRun,lastRun)
0289
0290 sortAndCleanBeamList(listbeam,IOVbase)
0291
0292
0293
0294
0295 for ii in range(0,len(listbeam)):
0296
0297 ibeam = listbeam[ii]
0298
0299 bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
0300 bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
0301 bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
0302 bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
0303 bntuple.run = int(ibeam.Run)
0304 bntuple.fill = int( getFill( FillList, int(ibeam.Run) ) )
0305 bntuple.lumi = array('i', [int(ibeam.IOVfirst),int(ibeam.IOVlast)])
0306 line = ibeam.IOVBeginTime
0307 begintime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
0308 line = ibeam.IOVEndTime
0309 endtime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
0310 bntuple.time = array('f', [begintime, endtime])
0311 tbylumi.Fill()
0312
0313
0314 iovlist = listbeam
0315 iovlist = createWeightedPayloads("tmp.txt",iovlist,False)
0316
0317 for ii in range(0,len(iovlist)):
0318
0319 ibeam = iovlist[ii]
0320
0321 bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
0322 bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
0323 bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
0324 bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
0325 bntuple.run = int(ibeam.Run)
0326 bntuple.fill = int( getFill( FillList, int(ibeam.Run) ) )
0327 bntuple.lumi = array('i', [int(ibeam.IOVfirst),int(ibeam.IOVlast)])
0328 line = ibeam.IOVBeginTime
0329 begintime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
0330 line = ibeam.IOVEndTime
0331 endtime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
0332 bntuple.time = array('f', [begintime, endtime])
0333
0334 tbyIOV.Fill()
0335
0336 weightedlist = listbeam
0337 weightedlist = createWeightedPayloads("tmp.txt",weightedlist,True)
0338
0339 for ii in range(0,len(weightedlist)):
0340
0341 ibeam = weightedlist[ii]
0342
0343 bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
0344 bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
0345 bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
0346 bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
0347 bntuple.run = int(ibeam.Run)
0348 bntuple.fill = int( getFill( FillList, int(ibeam.Run) ) )
0349 bntuple.lumi = array('i', [int(ibeam.IOVfirst),int(ibeam.IOVlast)])
0350 line = ibeam.IOVBeginTime
0351 begintime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
0352 line = ibeam.IOVEndTime
0353 endtime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
0354 bntuple.time = array('f', [begintime, endtime])
0355
0356 tbyrun.Fill()
0357
0358
0359 os.system('rm tmp.txt')
0360 fntuple.cd()
0361 tbylumi.Write()
0362 tbyIOV.Write()
0363 tbyrun.Write()
0364 fntuple.Close()
0365
0366
0367
0368