test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
summaryLumi.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 #########################################################################
4 # Command to produce fill summary lumi files using lumiCalc2.py lumibyls#
5 # output #
6 # #
7 # Author: Zhen Xie #
8 #########################################################################
9 #
10 import os,os.path,sys,math,array,datetime,time,re
11 
12 from RecoLuminosity.LumiDB import argparse,lumiTime,lumiCalcAPI,sessionManager,lumiParameters
13 MINFILL=1800
14 MAXFILL=9999
15 allfillname='allfills.txt'
16 runtofilldqmfile='runtofill_dqm.txt'
17 
18 def listfilldir(indir):
19  fillnamepat=r'^[0-9]{4}$'
20  p=re.compile(fillnamepat)
21  processedfills=[]
22  dirList=os.listdir(indir)
23  for fname in dirList:
24  if p.match(fname) and os.path.isdir(os.path.join(indir,fname)):#found fill dir
25  allfs=os.listdir(os.path.join(indir,fname))
26  for myfile in allfs:
27  sumfilenamepat=r'^[0-9]{4}_bxsum_CMS.txt$'
28  s=re.compile(sumfilenamepat)
29  if s.match(myfile):
30  #only if fill_summary_CMS.txt file exists
31  processedfills.append(int(fname))
32  return processedfills
33 
34 def lastcompleteFill(infile):
35  lastfill=None
36  hlinepat=r'(LASTCOMPLETEFILL )([0-9]{4})'
37  h=re.compile(hlinepat)
38  dqmfile=open(infile,'r')
39  for line in dqmfile:
40  result=h.match(line)
41  if result:
42  lastfill=result.group(2)
43  break
44  return int(lastfill)
45 
46 ##############################
47 ## ######################## ##
48 ## ## ################## ## ##
49 ## ## ## Main Program ## ## ##
50 ## ## ################## ## ##
51 ## ######################## ##
52 ##############################
53 
54 if __name__ == '__main__':
55  parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Dump Fill",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
56  # parse arguments
57  parser.add_argument('-c',
58  dest='connect',
59  action='store',
60  required=False,
61  help='connect string to lumiDB,optional',
62  default='oracle://cms_orcon_adg/cms_lumi_prod')
63  parser.add_argument('-P',
64  dest='authpath',
65  action='store',
66  required=True,
67  help='authentication.xml dir')
68  parser.add_argument('-i',
69  dest='inputdir',
70  action='store',
71  required=False,
72  help='input dir to runtofill_dqm.txt',
73  default='.')
74  parser.add_argument('-o',
75  dest='outputdir',
76  action='store',
77  required=False,
78  help='output dir',
79  default='.')
80  parser.add_argument('-f','--fill',
81  dest='fillnum',
82  action='store',
83  required=False,
84  help='specific fill',
85  default=None)
86  parser.add_argument('--datatag',
87  dest='datatag',
88  action='store',
89  required=False,
90  help='datatag',
91  default=None)
92  parser.add_argument('--normtag',
93  dest='normtag',
94  action='store',
95  required=False,
96  help='normtag',
97  default=None)
98  parser.add_argument('--minfill',
99  dest='minfill',
100  action='store',
101  required=False,
102  help='minimal fillnumber',
103  default=None)
104  parser.add_argument('--maxfill',
105  dest='maxfill',
106  action='store',
107  required=False,
108  help='maximum fillnumber ',
109  default=MAXFILL)
110  parser.add_argument('--amodetag',
111  dest='amodetag',
112  action='store',
113  required=False,
114  help='specific accelerator mode choices [PROTOPHYS,IONPHYS,PAPHYS] (optional)')
115  parser.add_argument('--beamenergy',
116  dest='beamenergy',
117  action='store',
118  type=float,
119  default=None,
120  help='nominal beam energy in GeV')
121  parser.add_argument('--beamfluctuation',
122  dest='beamfluctuation',
123  type=float,action='store',
124  default=0.2,
125  required=False,
126  help='fluctuation in fraction allowed to nominal beam energy, default 0.2, to be used together with -beamenergy (optional)')
127  parser.add_argument('--debug',
128  dest='debug',
129  action='store_true',
130  help='debug')
131  parser.add_argument('--without-stablebeam',
132  dest='withoutStablebeam',
133  action='store_true',
134  required=False,
135  help='without requirement on stable beams')
136  parser.add_argument('--without-correction',
137  dest='withoutFineCorrection',
138  action='store_true',
139  required=False,
140  help='without fine correction')
141  options=parser.parse_args()
142  if options.minfill:
143  MINFILL=int(options.minfill)
144  fillstoprocess=[]
145  maxfillnum=options.maxfill
146  summaryfilenameTMP='_summary_CMS.txt'
147  dbname=options.connect
148  authdir=options.authpath
149  if options.fillnum is not None: #if process a specific single fill
150  fillstoprocess.append(int(options.fillnum))
151  else: #if process fills automatically
152  svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,debugON=options.debug)
153  session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
154  session.transaction().start(True)
155  schema=session.nominalSchema()
156  allfillsFromDB=lumiCalcAPI.fillInRange(schema,fillmin=MINFILL,fillmax=maxfillnum,amodetag=options.amodetag)
157  session.transaction().commit()
158  processedfills=listfilldir(options.outputdir)
159  lastcompletedFill=lastcompleteFill(os.path.join(options.inputdir,'runtofill_dqm.txt'))
160  for pf in processedfills:
161  if pf>lastcompletedFill:
162  print '\tremove unfinished fill from processed list ',pf
163  processedfills.remove(pf)
164  for fill in allfillsFromDB:
165  if fill not in processedfills :
166  if int(fill)<=lastcompletedFill:
167  if int(fill)>MINFILL:
168  fillstoprocess.append(fill)
169  else:
170  print 'ongoing fill...',fill
171  print 'fills to process : ',fillstoprocess
172  if len(fillstoprocess)==0:
173  print 'no fill to process, exit '
174  exit(0)
176  lslength=lumip.lslengthsec()
177  import commands,os,RecoLuminosity.LumiDB.lumiTime,datetime,time
178  for fillnum in fillstoprocess:
179  clineElements=['lumiCalc2.py','lumibyls','-c',dbname,'-P',authdir,'-f',str(fillnum),'-o','tmp.out','--without-checkforupdate','--nowarning']
180  if not options.withoutStablebeam:
181  clineElements.append('-b stable')
182  if options.withoutFineCorrection:
183  clineElements.append('--without-correction')
184  if options.datatag:
185  clineElements.append('--datatag '+options.datatag)
186  if options.normtag:
187  clineElements.append('--normtag '+options.normtag)
188  if options.beamenergy:
189  clineElements.append('--beamenergy '+str(options.beamenergy))
190  if options.beamfluctuation:
191  clineElements.append('--beamfluctuation '+str(options.beamfluctuation))
192 
193  finalcmmd=' '.join(clineElements)
194  print 'cmmd executed:',finalcmmd
195  (exestat,resultStr)=commands.getstatusoutput(finalcmmd)
196  if exestat!=0:
197  print 'lumiCalc2.py execution error ',resultStr
198  exit(exestat)
199  f=open('tmp.out','r')
200  lcount=0
201  lines=f.readlines()
202  stablefillmap={}#{run:([ts],[lumi])}
203  for line in lines:
204  lcount=lcount+1
205  if lcount==1:
206  continue
207  #print line.strip()
208  line=line.strip()
209  lineList=line.split(',')
210  runnum=int(lineList[0].split(':')[0])
211  if not stablefillmap.has_key(runnum):
212  stablefillmap[runnum]=([],[])
213  timestamp=lineList[2]
214  bstatus=lineList[3]
216  pydate=t.StrToDatetime(timestamp,'%m/%d/%y %H:%M:%S')
217 
218  os.environ['TZ']='UTC'
219  time.tzset()
220  unixts=int(time.mktime(pydate.timetuple()))
221  deliveredintl=float(lineList[5])
222  if bstatus=='STABLE BEAMS':
223  stablefillmap[runnum][0].append(unixts)
224  stablefillmap[runnum][1].append(deliveredintl)
225  filloutdir=os.path.join(options.outputdir,str(fillnum))
226  if not os.path.exists(filloutdir):
227  os.mkdir(filloutdir)
228  #print 'options.outputdir ',options.outputdir
229  #print 'fillnum ',fillnum
230  #print 'str(fillnum)+summaryfilename ',str(fillnum)+summaryfilenameTMP
231  summaryfilename=os.path.join(options.outputdir,str(fillnum),str(fillnum)+summaryfilenameTMP)
232  #print 'summaryfilename ',summaryfilename
233  ofile=open(summaryfilename,'w')
234  if len(stablefillmap)==0:
235  print >>ofile,'%s'%('#no stable beams')
236  else:
237  for r in sorted(stablefillmap):
238  rundata=stablefillmap[r]
239  print >>ofile,'%d\t%d\t%.6e\t%.6e'%(min(rundata[0]),max(rundata[0]), max(rundata[1])/lslength,sum(rundata[1]))
240  ofile.close()
241  os.remove('tmp.out')
242  f.close()
243 
Definition: start.py:1
def lastcompleteFill
Definition: summaryLumi.py:34
def listfilldir
Definition: summaryLumi.py:18
T min(T a, T b)
Definition: MathUtil.h:58
def fillInRange
Definition: lumiCalcAPI.py:35
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
double split
Definition: MVATrainer.cc:139