CMS 3D CMS Logo

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