CMS 3D CMS Logo

ntuplemaker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #____________________________________________________________
3 #
4 #
5 # A very simple way to make plots with ROOT via an XML file
6 #
7 # Francisco Yumiceva
8 # yumiceva@fnal.gov
9 #
10 # Fermilab, 2010
11 #
12 #____________________________________________________________
13 
14 """
15  ntuplemaker
16 
17  A very simple script to plot the beam spot data stored in condDB
18 
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.
29 
30  Francisco Yumiceva (yumiceva@fnal.gov)
31  Fermilab 2010
32 
33 """
34 from __future__ import print_function
35 
36 
37 from builtins import range
38 import os, string, re, sys, math
39 import commands, time
40 from BeamSpotObj import BeamSpot
41 from IOVObj import IOV
42 from CommonMethods import *
43 
44 try:
45  import ROOT
46 except:
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")
51  else:
52  print(" setenv PYTHONPATH $ROOTSYS/lib\n")
53  sys.exit()
54 
55 from ROOT import *
56 from array import array
57 
58 def getFill( json, run ):
59 
60  thefill = 0
61  run = int(run)
62  keys = json.keys()
63 
64  for i in keys:
65 
66  run0 = int(json[i][0])
67  run1 = int(json[i][1])
68  if run>= run0 and run<=run1:
69  thefill = i
70 
71  return int(thefill)
72 
73 if __name__ == '__main__':
74 
75 
76 
77  # fill and runs
78  FillList = {}
79  runsfile = open("FillandRuns.txt")
80  for line in runsfile:
81  if line.find('fill:') != -1:
82  aline = line.split()
83  afill = aline[1]
84  run0 = aline[3]
85  run1 = aline[5]
86  FillList[int(afill)] = [int(run0),int(run1)]
87 
88  #print FillList
89 
90  # create ntuple
91  gROOT.ProcessLine(
92  "struct spot {\
93  Float_t position[3];\
94  Float_t posError[3];\
95  Float_t width[3];\
96  Float_t widthError[3];\
97  Float_t slope[2];\
98  Float_t slopeError[2];\
99  Float_t time[2];\
100  Int_t run;\
101  Int_t lumi[2];\
102  Int_t fill;\
103  };" );
104 
105  bntuple = spot()
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')
118 
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')
130 
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')
142 
143 
144  # COMMAND LINE OPTIONS
145  #################################
146  option,args = parse(__doc__)
147  if not args and not option: exit()
148 
149  if not option.data:
150  print(" need to provide beam spot data file")
151  exit()
152 
153  if option.batch:
154  ROOT.gROOT.SetBatch()
155 
156  datafilename = "tmp_beamspot.dat"
157  if option.create:
158  datafilename = option.create
159 
160  getDBdata = True
161  if option.data:
162  getDBdata = False
163 
164  IOVbase = 'lumibase'
165  firstRun = "0:0"
166  lastRun = "4999999999:4999999999"
167 
168  if option.initial:
169  firstRun = option.initial
170  if option.final:
171  lastRun = option.final
172 
173  # GET IOVs
174  ################################
175 
176  if getDBdata:
177 
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')
182  #print tmplistiov
183 
184  iovlist = []
185  passline = False
186  iline = jline = 0
187  totlines = len(tmplistiov)
188  for line in tmplistiov:
189 
190  if line.find('since') != -1:
191  passline = True
192  jline = iline
193  if passline and iline > jline and iline < totlines-1:
194  linedata = line.split()
195  #print linedata
196  aIOV = IOV()
197  aIOV.since = int(linedata[0])
198  aIOV.till = int(linedata[1])
199  iovlist.append( aIOV )
200  iline += 1
201 
202  print(" total number of IOVs = " + str(len(iovlist)))
203 
204 
205  # GET DATA
206  ################################
207 
208  otherArgs = ''
209  if option.destDB:
210  otherArgs = " -d " + option.destDB
211  if option.auth:
212  otherArgs = otherArgs + " -a "+ option.auth
213 
214  print(" get beam spot data from DB for IOVs. This can take a few minutes ...")
215 
216  tmpfile = open(datafilename,'w')
217 
218  for iIOV in iovlist:
219  passiov = False
220  tmprunfirst = firstRun
221  tmprunlast = lastRun
222  tmplumifirst = 1
223  tmplumilast = 9999999
224  if IOVbase=="lumibase":
225  #tmprunfirst = int(firstRun.split(":")[0])
226  #tmprunlast = int(lastRun.split(":")[0])
227  #tmplumifirst = int(firstRun.split(":")[1])
228  #tmplumilast = int(lastRun.split(":")[1])
229  tmprunfirst = pack( int(firstRun.split(":")[0]) , int(firstRun.split(":")[1]) )
230  tmprunlast = pack( int(lastRun.split(":")[0]) , int(lasstRun.split(":")[1]) )
231  #print "since = " + str(iIOV.since) + " till = "+ str(iIOV.till)
232  if iIOV.since >= int(tmprunfirst) and int(tmprunlast) < 0 and iIOV.since <= int(tmprunfirst):
233  print(" IOV: " + str(iIOV.since))
234  passiov = True
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))
237  passiov = True
238  if iIOV.since >= int(tmprunlast) and iIOV.till >= 4294967295:
239  print(" IOV: " + str(iIOV.since) + " to " + str(iIOV.till))
240  passiov = True
241  if passiov:
242  acommand = 'getBeamSpotDB.py -t '+ tag + " -r " + str(iIOV.since) +otherArgs
243  if IOVbase=="lumibase":
244  tmprun = unpack(iIOV.since)[0]
245  tmplumi = unpack(iIOV.since)[1]
246  acommand = 'getBeamSpotDB.py -t '+ tag + " -r " + str(tmprun) +" -l "+tmplumi +otherArgs
247  status = commands.getstatusoutput( acommand )
248  tmpfile.write(status[1])
249 
250  print(" beam spot data collected and stored in file " + datafilename)
251 
252  tmpfile.close()
253 
254 
255  # PROCESS DATA
256  ###################################
257 
258  # check if input data exists if given
259  if option.data:
260  if os.path.isdir(option.data):
261  tmp = commands.getstatusoutput("ls "+option.data)
262  files = tmp[1].split()
263  datafilename = "combined_all.txt"
264  output = open(datafilename,"w")
265 
266  for f in files:
267  if os.path.isdir(option.data+"/"+f) is False:
268  input = open(option.data +"/"+f)
269  output.writelines(input.readlines())
270  output.close()
271  print(" data files have been collected in "+datafilename)
272 
273  elif os.path.exists(option.data):
274  datafilename = option.data
275  else:
276  print(" input beam spot data DOES NOT exist, file " + option.data)
277  exit()
278 
279  listbeam = []
280 
281  if option.xcrossing:
282  listmap = readBeamSpotFile(datafilename,listbeam,IOVbase,firstRun,lastRun)
283  # bx
284  print("List of bunch crossings in the file:")
285  print(listmap.keys())
286  listbeam = listmap[option.Xrossing]
287  else:
288  readBeamSpotFile(datafilename,listbeam,IOVbase,firstRun,lastRun)
289 
290  sortAndCleanBeamList(listbeam,IOVbase)
291 
292 
293  ###################################
294 
295  for ii in range(0,len(listbeam)):
296 
297  ibeam = listbeam[ii]
298 
299  bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
300  bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
301  bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
302  bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
303  bntuple.run = int(ibeam.Run)
304  bntuple.fill = int( getFill( FillList, 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])
311  tbylumi.Fill()
312 
313 
314  iovlist = listbeam
315  iovlist = createWeightedPayloads("tmp.txt",iovlist,False)
316 
317  for ii in range(0,len(iovlist)):
318 
319  ibeam = iovlist[ii]
320 
321  bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
322  bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
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)
326  bntuple.fill = int( getFill( FillList, 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])
333 
334  tbyIOV.Fill()
335 
336  weightedlist = listbeam
337  weightedlist = createWeightedPayloads("tmp.txt",weightedlist,True)
338 
339  for ii in range(0,len(weightedlist)):
340 
341  ibeam = weightedlist[ii]
342 
343  bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
344  bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
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)
348  bntuple.fill = int( getFill( FillList, 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])
355 
356  tbyrun.Fill()
357 
358 
359  os.system('rm tmp.txt')
360  fntuple.cd()
361  tbylumi.Write()
362  tbyIOV.Write()
363  tbyrun.Write()
364  fntuple.Close()
365 
366  # CLEAN temporal files
367  ###################################
368  #os.system('rm tmp_beamspotdata.log')
def pack(high, low)
def readBeamSpotFile(fileName, listbeam=[], IOVbase="runbase", firstRun='1', lastRun='4999999999')
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
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.
def parse(path, config)
Definition: dumpparser.py:13
def getFill(json, run)
Definition: ntuplemaker.py:58
#define str(s)
double split
Definition: MVATrainer.cc:139