test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
35 
36 import os, string, re, sys, math
37 import commands, time
38 from BeamSpotObj import BeamSpot
39 from IOVObj import IOV
40 from CommonMethods import *
41 
42 try:
43  import ROOT
44 except:
45  print "\nCannot load PYROOT, make sure you have setup ROOT in the path"
46  print "and pyroot library is also defined in the variable PYTHONPATH, try:\n"
47  if (os.getenv("PYTHONPATH")):
48  print " setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n"
49  else:
50  print " setenv PYTHONPATH $ROOTSYS/lib\n"
51  sys.exit()
52 
53 from ROOT import *
54 from array import array
55 
56 def getFill( json, run ):
57 
58  thefill = 0
59  run = int(run)
60  keys = json.keys()
61 
62  for i in keys:
63 
64  run0 = int(json[i][0])
65  run1 = int(json[i][1])
66  if run>= run0 and run<=run1:
67  thefill = i
68 
69  return int(thefill)
70 
71 if __name__ == '__main__':
72 
73 
74 
75  # fill and runs
76  FillList = {}
77  runsfile = open("FillandRuns.txt")
78  for line in runsfile:
79  if line.find('fill:') != -1:
80  aline = line.split()
81  afill = aline[1]
82  run0 = aline[3]
83  run1 = aline[5]
84  FillList[int(afill)] = [int(run0),int(run1)]
85 
86  #print FillList
87 
88  # create ntuple
89  gROOT.ProcessLine(
90  "struct spot {\
91  Float_t position[3];\
92  Float_t posError[3];\
93  Float_t width[3];\
94  Float_t widthError[3];\
95  Float_t slope[2];\
96  Float_t slopeError[2];\
97  Float_t time[2];\
98  Int_t run;\
99  Int_t lumi[2];\
100  Int_t fill;\
101  };" );
102 
103  bntuple = spot()
104  fntuple = TFile( 'bntuple.root', 'RECREATE' )
105  tbylumi = TTree( 'bylumi', 'beam spot data lumi by lumi' )
106  tbylumi.Branch('fill', AddressOf( bntuple, 'fill'), 'fill/I' )
107  tbylumi.Branch('run', AddressOf( bntuple, 'run'), 'run/I' )
108  tbylumi.Branch('lumi', AddressOf( bntuple, 'lumi'), 'lumi[2]/I' )
109  tbylumi.Branch('position', AddressOf( bntuple, 'position'),'position[3]/F')
110  tbylumi.Branch('posErr', AddressOf( bntuple, 'posError'),'posError[3]/F')
111  tbylumi.Branch('width', AddressOf( bntuple, 'width'),'width[3]/F')
112  tbylumi.Branch('widthErr', AddressOf( bntuple, 'widthError'),'widthError[3]/F')
113  tbylumi.Branch('slope', AddressOf( bntuple, 'slope'),'slope[2]/F')
114  tbylumi.Branch('slopeErr', AddressOf( bntuple, 'slopeError'),'slopeError[2]/F')
115  tbylumi.Branch('time', AddressOf( bntuple, 'time'),'time[2]/F')
116 
117  tbyIOV = TTree( 'byIOV', 'beam spot data by IOV' )
118  tbyIOV.Branch('fill', AddressOf( bntuple, 'fill'), 'fill/I' )
119  tbyIOV.Branch('run', AddressOf( bntuple, 'run'), 'run/I' )
120  tbyIOV.Branch('lumi', AddressOf( bntuple, 'lumi'), 'lumi[2]/I' )
121  tbyIOV.Branch('position', AddressOf( bntuple, 'position'),'position[3]/F')
122  tbyIOV.Branch('posErr', AddressOf( bntuple, 'posError'),'posError[3]/F')
123  tbyIOV.Branch('width', AddressOf( bntuple, 'width'),'width[3]/F')
124  tbyIOV.Branch('widthErr', AddressOf( bntuple, 'widthError'),'widthError[3]/F')
125  tbyIOV.Branch('slope', AddressOf( bntuple, 'slope'),'slope[2]/F')
126  tbyIOV.Branch('slopeErr', AddressOf( bntuple, 'slopeError'),'slopeError[2]/F')
127  tbyIOV.Branch('time', AddressOf( bntuple, 'time'),'time[2]/F')
128 
129  tbyrun = TTree( 'byrun', 'beam spot data by run' )
130  tbyrun.Branch('fill', AddressOf( bntuple, 'fill'), 'fill/I' )
131  tbyrun.Branch('run', AddressOf( bntuple, 'run'), 'run/I' )
132  tbyrun.Branch('lumi', AddressOf( bntuple, 'lumi'), 'lumi[2]/I' )
133  tbyrun.Branch('position', AddressOf( bntuple, 'position'),'position[3]/F')
134  tbyrun.Branch('posErr', AddressOf( bntuple, 'posError'),'posError[3]/F')
135  tbyrun.Branch('width', AddressOf( bntuple, 'width'),'width[3]/F')
136  tbyrun.Branch('widthErr', AddressOf( bntuple, 'widthError'),'widthError[3]/F')
137  tbyrun.Branch('slope', AddressOf( bntuple, 'slope'),'slope[2]/F')
138  tbyrun.Branch('slopeErr', AddressOf( bntuple, 'slopeError'),'slopeError[2]/F')
139  tbyrun.Branch('time', AddressOf( bntuple, 'time'),'time[2]/F')
140 
141 
142  # COMMAND LINE OPTIONS
143  #################################
144  option,args = parse(__doc__)
145  if not args and not option: exit()
146 
147  if not option.data:
148  print " need to provide beam spot data file"
149  exit()
150 
151  if option.batch:
152  ROOT.gROOT.SetBatch()
153 
154  datafilename = "tmp_beamspot.dat"
155  if option.create:
156  datafilename = option.create
157 
158  getDBdata = True
159  if option.data:
160  getDBdata = False
161 
162  IOVbase = 'lumibase'
163  firstRun = "0:0"
164  lastRun = "4999999999:4999999999"
165 
166  if option.initial:
167  firstRun = option.initial
168  if option.final:
169  lastRun = option.final
170 
171  # GET IOVs
172  ################################
173 
174  if getDBdata:
175 
176  print " read DB to get list of IOVs for the given tag"
177  acommand = 'cmscond_list_iov -c frontier://PromptProd/CMS_COND_31X_BEAMSPOT -P /afs/cern.ch/cms/DB/conddb -t '+ tag
178  tmpstatus = commands.getstatusoutput( acommand )
179  tmplistiov = tmpstatus[1].split('\n')
180  #print tmplistiov
181 
182  iovlist = []
183  passline = False
184  iline = jline = 0
185  totlines = len(tmplistiov)
186  for line in tmplistiov:
187 
188  if line.find('since') != -1:
189  passline = True
190  jline = iline
191  if passline and iline > jline and iline < totlines-1:
192  linedata = line.split()
193  #print linedata
194  aIOV = IOV()
195  aIOV.since = int(linedata[0])
196  aIOV.till = int(linedata[1])
197  iovlist.append( aIOV )
198  iline += 1
199 
200  print " total number of IOVs = " + str(len(iovlist))
201 
202 
203  # GET DATA
204  ################################
205 
206  otherArgs = ''
207  if option.destDB:
208  otherArgs = " -d " + option.destDB
209  if option.auth:
210  otherArgs = otherArgs + " -a "+ option.auth
211 
212  print " get beam spot data from DB for IOVs. This can take a few minutes ..."
213 
214  tmpfile = open(datafilename,'w')
215 
216  for iIOV in iovlist:
217  passiov = False
218  tmprunfirst = firstRun
219  tmprunlast = lastRun
220  tmplumifirst = 1
221  tmplumilast = 9999999
222  if IOVbase=="lumibase":
223  #tmprunfirst = int(firstRun.split(":")[0])
224  #tmprunlast = int(lastRun.split(":")[0])
225  #tmplumifirst = int(firstRun.split(":")[1])
226  #tmplumilast = int(lastRun.split(":")[1])
227  tmprunfirst = pack( int(firstRun.split(":")[0]) , int(firstRun.split(":")[1]) )
228  tmprunlast = pack( int(lastRun.split(":")[0]) , int(lasstRun.split(":")[1]) )
229  #print "since = " + str(iIOV.since) + " till = "+ str(iIOV.till)
230  if iIOV.since >= int(tmprunfirst) and int(tmprunlast) < 0 and iIOV.since <= int(tmprunfirst):
231  print " IOV: " + str(iIOV.since)
232  passiov = True
233  if iIOV.since >= int(tmprunfirst) and int(tmprunlast) > 0 and iIOV.till <= int(tmprunlast):
234  print " IOV: " + str(iIOV.since) + " to " + str(iIOV.till)
235  passiov = True
236  if iIOV.since >= int(tmprunlast) and iIOV.till >= 4294967295:
237  print " IOV: " + str(iIOV.since) + " to " + str(iIOV.till)
238  passiov = True
239  if passiov:
240  acommand = 'getBeamSpotDB.py -t '+ tag + " -r " + str(iIOV.since) +otherArgs
241  if IOVbase=="lumibase":
242  tmprun = unpack(iIOV.since)[0]
243  tmplumi = unpack(iIOV.since)[1]
244  acommand = 'getBeamSpotDB.py -t '+ tag + " -r " + str(tmprun) +" -l "+tmplumi +otherArgs
245  status = commands.getstatusoutput( acommand )
246  tmpfile.write(status[1])
247 
248  print " beam spot data collected and stored in file " + datafilename
249 
250  tmpfile.close()
251 
252 
253  # PROCESS DATA
254  ###################################
255 
256  # check if input data exists if given
257  if option.data:
258  if os.path.isdir(option.data):
259  tmp = commands.getstatusoutput("ls "+option.data)
260  files = tmp[1].split()
261  datafilename = "combined_all.txt"
262  output = open(datafilename,"w")
263 
264  for f in files:
265  if os.path.isdir(option.data+"/"+f) is False:
266  input = open(option.data +"/"+f)
267  output.writelines(input.readlines())
268  output.close()
269  print " data files have been collected in "+datafilename
270 
271  elif os.path.exists(option.data):
272  datafilename = option.data
273  else:
274  print " input beam spot data DOES NOT exist, file " + option.data
275  exit()
276 
277  listbeam = []
278 
279  if option.xcrossing:
280  listmap = readBeamSpotFile(datafilename,listbeam,IOVbase,firstRun,lastRun)
281  # bx
282  print "List of bunch crossings in the file:"
283  print listmap.keys()
284  listbeam = listmap[option.Xrossing]
285  else:
286  readBeamSpotFile(datafilename,listbeam,IOVbase,firstRun,lastRun)
287 
288  sortAndCleanBeamList(listbeam,IOVbase)
289 
290 
291  ###################################
292 
293  for ii in range(0,len(listbeam)):
294 
295  ibeam = listbeam[ii]
296 
297  bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
298  bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
299  bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
300  bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
301  bntuple.run = int(ibeam.Run)
302  bntuple.fill = int( getFill( FillList, int(ibeam.Run) ) )
303  bntuple.lumi = array('i', [int(ibeam.IOVfirst),int(ibeam.IOVlast)])
304  line = ibeam.IOVBeginTime
305  begintime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
306  line = ibeam.IOVEndTime
307  endtime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
308  bntuple.time = array('f', [begintime, endtime])
309  tbylumi.Fill()
310 
311 
312  iovlist = listbeam
313  iovlist = createWeightedPayloads("tmp.txt",iovlist,False)
314 
315  for ii in range(0,len(iovlist)):
316 
317  ibeam = iovlist[ii]
318 
319  bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
320  bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
321  bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
322  bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
323  bntuple.run = int(ibeam.Run)
324  bntuple.fill = int( getFill( FillList, int(ibeam.Run) ) )
325  bntuple.lumi = array('i', [int(ibeam.IOVfirst),int(ibeam.IOVlast)])
326  line = ibeam.IOVBeginTime
327  begintime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
328  line = ibeam.IOVEndTime
329  endtime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
330  bntuple.time = array('f', [begintime, endtime])
331 
332  tbyIOV.Fill()
333 
334  weightedlist = listbeam
335  weightedlist = createWeightedPayloads("tmp.txt",weightedlist,True)
336 
337  for ii in range(0,len(weightedlist)):
338 
339  ibeam = weightedlist[ii]
340 
341  bntuple.position = array('f', [float(ibeam.X), float(ibeam.Y), float(ibeam.Z)])
342  bntuple.posError = array('f', [float(ibeam.Xerr),float(ibeam.Yerr),float(ibeam.Zerr)])
343  bntuple.width = array('f', [float(ibeam.beamWidthX), float(ibeam.beamWidthY), float(ibeam.sigmaZ)])
344  bntuple.widthError = array('f',[float(ibeam.beamWidthXerr),float(ibeam.beamWidthYerr),float(ibeam.sigmaZerr)])
345  bntuple.run = int(ibeam.Run)
346  bntuple.fill = int( getFill( FillList, int(ibeam.Run) ) )
347  bntuple.lumi = array('i', [int(ibeam.IOVfirst),int(ibeam.IOVlast)])
348  line = ibeam.IOVBeginTime
349  begintime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
350  line = ibeam.IOVEndTime
351  endtime = time.mktime( time.strptime(line.split()[0] + " " + line.split()[1] + " " + line.split()[2],"%Y.%m.%d %H:%M:%S %Z") )
352  bntuple.time = array('f', [begintime, endtime])
353 
354  tbyrun.Fill()
355 
356 
357  os.system('rm tmp.txt')
358  fntuple.cd()
359  tbylumi.Write()
360  tbyIOV.Write()
361  tbyrun.Write()
362  fntuple.Close()
363 
364  # CLEAN temporal files
365  ###################################
366  #os.system('rm tmp_beamspotdata.log')
tuple array
Definition: mps_check.py:181
Evaluator * parse(const T &text)
def createWeightedPayloads
CREATE FILE FOR PAYLOADS.
def sortAndCleanBeamList
Sort and clean list of data for consecutive duplicates and bad fits.
double split
Definition: MVATrainer.cc:139