00001
00002 VERSION='2.00'
00003 import os,sys,time,re
00004 import coral
00005 from RecoLuminosity.LumiDB import sessionManager,lumiTime,inputFilesetParser,csvSelectionParser,selectionParser,csvReporter,argparse,CommonUtil,lumiCalcAPI,lumiReport,lumiCorrections
00006
00007 class RegexValidator(object):
00008 def __init__(self, pattern, statement=None):
00009 self.pattern = re.compile(pattern)
00010 self.statement = statement
00011 if not self.statement:
00012 self.statement = "must match pattern %s" % self.pattern
00013
00014 def __call__(self, string):
00015 match = self.pattern.search(string)
00016 if not match:
00017 raise ValueError(self.statement)
00018 return string
00019
00020 beamChoices=['PROTPHYS','IONPHYS','PAPHYS']
00021
00022 def parseInputFiles(inputfilename,dbrunlist,optaction):
00023 '''
00024 output ({run:[cmsls,cmsls,...]},[[resultlines]])
00025 '''
00026 selectedrunlsInDB={}
00027 resultlines=[]
00028 p=inputFilesetParser.inputFilesetParser(inputfilename)
00029 runlsbyfile=p.runsandls()
00030 selectedProcessedRuns=p.selectedRunsWithresult()
00031 selectedNonProcessedRuns=p.selectedRunsWithoutresult()
00032 resultlines=p.resultlines()
00033 for runinfile in selectedNonProcessedRuns:
00034 if runinfile not in dbrunlist:
00035 continue
00036 if optaction=='delivered':
00037 selectedrunlsInDB[runinfile]=None
00038 else:
00039 selectedrunlsInDB[runinfile]=runlsbyfile[runinfile]
00040 return (selectedrunlsInDB,resultlines)
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 if __name__ == '__main__':
00051 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Lumi Calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00052 allowedActions = ['overview', 'delivered', 'recorded', 'lumibyls','lumibylsXing','status','checkforupdate']
00053 beamModeChoices = [ "stable", "quiet", "either"]
00054 amodetagChoices = [ "PROTPHYS","IONPHYS",'PAPHYS' ]
00055 xingAlgoChoices =[ "OCC1","OCC2","ET"]
00056
00057
00058
00059
00060
00061
00062 parser.add_argument('action',choices=allowedActions,
00063 help='command actions')
00064 parser.add_argument('-c',dest='connect',action='store',
00065 required=False,
00066 help='connect string to lumiDB,optional',
00067 default='frontier://LumiCalc/CMS_LUMI_PROD')
00068 parser.add_argument('-P',dest='authpath',action='store',
00069 required=False,
00070 help='path to authentication file (optional)')
00071 parser.add_argument('-r',dest='runnumber',action='store',
00072 type=int,
00073 required=False,
00074 help='run number (optional)')
00075 parser.add_argument('-o',dest='outputfile',action='store',
00076 required=False,
00077 help='output to csv file (optional)')
00078
00079
00080
00081 parser.add_argument('-i',dest='inputfile',action='store',
00082 required=False,
00083 help='lumi range selection file (optional)')
00084
00085
00086
00087 parser.add_argument('--hltpath',dest='hltpath',action='store',
00088 default=None,required=False,
00089 help='specific hltpath or hltpath pattern to calculate the effectived luminosity (optional)')
00090
00091
00092
00093 parser.add_argument('-b',dest='beammode',action='store',
00094 choices=beamModeChoices,
00095 required=False,
00096 help='beam mode choices [stable] (optional)')
00097 parser.add_argument('-f','--fill',dest='fillnum',action='store',
00098 default=None,required=False,
00099 help='fill number (optional) ')
00100 parser.add_argument('--amodetag',dest='amodetag',action='store',
00101 choices=amodetagChoices,
00102 required=False,
00103 help='specific accelerator mode choices [PROTOPHYS,IONPHYS,PAPHYS] (optional)')
00104 parser.add_argument('--beamenergy',dest='beamenergy',action='store',
00105 type=float,
00106 default=None,
00107 help='nominal beam energy in GeV')
00108 parser.add_argument('--beamfluctuation',dest='beamfluctuation',
00109 type=float,action='store',
00110 default=0.2,
00111 required=False,
00112 help='fluctuation in fraction allowed to nominal beam energy, default 0.2, to be used together with -beamenergy (optional)'
00113 )
00114 parser.add_argument('--begin',dest='begin',action='store',
00115 default=None,
00116 required=False,
00117 type=RegexValidator("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$","must be form mm/dd/yy hh:mm:ss"),
00118 help='min run start time, mm/dd/yy hh:mm:ss (optional)'
00119 )
00120 parser.add_argument('--end',dest='end',action='store',
00121 default=None,
00122 required=False,
00123 type=RegexValidator("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$","must be form mm/dd/yy hh:mm:ss"),
00124 help='max run start time, mm/dd/yy hh:mm:ss (optional)'
00125 )
00126
00127
00128
00129 parser.add_argument('--xingMinLum', dest = 'xingMinLum',
00130 type=float,
00131 default=1e-03,
00132 required=False,
00133 help='Minimum luminosity considered for lumibylsXing action, default=1e-03')
00134 parser.add_argument('--xingAlgo', dest = 'xingAlgo',
00135 default='OCC1',
00136 required=False,
00137 help='algorithm name for per-bunch lumi ')
00138
00139
00140
00141 parser.add_argument('--lumiversion',dest='lumiversion',action='store',
00142 default=None,
00143 required=False,
00144 help='data version, optional')
00145 parser.add_argument('--norm',dest='normfactor',action='store',
00146 default=None,
00147 required=False,
00148 help='use specify the name or the value of the normalization to use,optional')
00149 parser.add_argument('-n',dest='scalefactor',action='store',
00150 type=float,
00151 default=1.0,
00152 required=False,
00153 help='user defined global scaling factor on displayed lumi values,optional')
00154
00155
00156
00157 parser.add_argument('--siteconfpath',dest='siteconfpath',action='store',
00158 default=None,
00159 required=False,
00160 help='specific path to site-local-config.xml file, optional. If path undefined, fallback to cern proxy&server')
00161
00162
00163
00164 parser.add_argument('--without-correction',dest='withoutFineCorrection',action='store_true',
00165 help='without fine correction on calibration' )
00166 parser.add_argument('--correctionv3',dest='correctionv3',action='store_true',
00167 help='apply correction v3' )
00168 parser.add_argument('--verbose',dest='verbose',action='store_true',
00169 help='verbose mode for printing' )
00170 parser.add_argument('--nowarning',dest='nowarning',action='store_true',
00171 help='suppress bad for lumi warnings' )
00172 parser.add_argument('--debug',dest='debug',action='store_true',
00173 help='debug')
00174
00175 options=parser.parse_args()
00176 if options.action=='checkforupdate':
00177 from RecoLuminosity.LumiDB import checkforupdate
00178 cmsswWorkingBase=os.environ['CMSSW_BASE']
00179 c=checkforupdate.checkforupdate()
00180 workingversion=c.runningVersion(cmsswWorkingBase,'lumiCalc2.py')
00181 c.checkforupdate(workingversion)
00182 exit(0)
00183 if options.authpath:
00184 os.environ['CORAL_AUTH_PATH'] = options.authpath
00185
00186 pbeammode = None
00187 normfactor=options.normfactor
00188 if options.beammode=='stable':
00189 pbeammode = 'STABLE BEAMS'
00190 if options.verbose:
00191 print 'General configuration'
00192 print '\tconnect: ',options.connect
00193 print '\tauthpath: ',options.authpath
00194 print '\tlumi data version: ',options.lumiversion
00195 print '\tsiteconfpath: ',options.siteconfpath
00196 print '\toutputfile: ',options.outputfile
00197 print '\tscalefactor: ',options.scalefactor
00198 if options.action=='recorded' and options.hltpath:
00199 print 'Action: effective luminosity in hltpath: ',options.hltpath
00200 else:
00201 print 'Action: ',options.action
00202 if options.normfactor:
00203 if CommonUtil.is_floatstr(normfactor):
00204 print '\tuse norm factor value ',normfactor
00205 else:
00206 print '\tuse specific norm factor name ',normfactor
00207 else:
00208 print '\tuse norm factor in context (amodetag,beamenergy)'
00209 if options.runnumber:
00210 print '\tselect specific run== ',options.runnumber
00211 else:
00212 print '\trun selections == '
00213 print '\tinput selection file: ',options.inputfile
00214 print '\tbeam mode: ',options.beammode
00215 print '\tfill: ',options.fillnum
00216 print '\tamodetag: ',options.amodetag
00217 print '\tbegin: ',options.begin
00218 print '\tend: ',options.end
00219 print '\tbeamenergy: ',options.beamenergy
00220 if options.beamenergy:
00221 print '\tbeam energy: ',str(options.beamenergy)+'+/-'+str(options.beamfluctuation*options.beamenergy)+'(GeV)'
00222 if options.action=='lumibylsXing':
00223 print '\tLS filter for lumibylsXing xingMinLum: ',options.xingMinLum
00224
00225 svc=sessionManager.sessionManager(options.connect,
00226 authpath=options.authpath,
00227 siteconfpath=options.siteconfpath,
00228 debugON=options.debug)
00229 session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00230
00231 irunlsdict={}
00232 iresults=[]
00233 if options.runnumber:
00234 irunlsdict[options.runnumber]=None
00235 else:
00236 reqTrg=False
00237 reqHlt=False
00238 if options.action=='recorded':
00239 reqTrg=True
00240 reqHlt=True
00241 session.transaction().start(True)
00242 schema=session.nominalSchema()
00243 runlist=lumiCalcAPI.runList(schema,options.fillnum,runmin=None,runmax=None,startT=options.begin,stopT=options.end,l1keyPattern=None,hltkeyPattern=None,amodetag=options.amodetag,nominalEnergy=options.beamenergy,energyFlut=options.beamfluctuation,requiretrg=reqTrg,requirehlt=reqHlt)
00244 session.transaction().commit()
00245 if options.inputfile:
00246 (irunlsdict,iresults)=parseInputFiles(options.inputfile,runlist,options.action)
00247 else:
00248 for run in runlist:
00249 irunlsdict[run]=None
00250 if options.verbose:
00251 print 'Selected run:ls'
00252 for run in sorted(irunlsdict):
00253 if irunlsdict[run] is not None:
00254 print '\t%d : %s'%(run,','.join([str(ls) for ls in irunlsdict[run]]))
00255 else:
00256 print '\t%d : all'%run
00257
00258 finecorrections=None
00259 driftcorrections=None
00260 if not options.withoutFineCorrection:
00261 rruns=irunlsdict.keys()
00262 schema=session.nominalSchema()
00263 session.transaction().start(True)
00264 if options.correctionv3:
00265 cterms=lumiCorrections.nonlinearV3()
00266 else:
00267 cterms=lumiCorrections.nonlinearV2()
00268 finecorrections=lumiCorrections.correctionsForRangeV2(schema,rruns,cterms)
00269 driftcorrections=lumiCorrections.driftcorrectionsForRange(schema,rruns,cterms)
00270 if options.verbose:
00271 print finecorrections,driftcorrections
00272 session.transaction().commit()
00273
00274 if options.action == 'delivered':
00275 session.transaction().start(True)
00276
00277 result=lumiCalcAPI.deliveredLumiForRange(session.nominalSchema(),irunlsdict,amodetag=options.amodetag,egev=options.beamenergy,beamstatus=pbeammode,norm=normfactor,finecorrections=finecorrections,driftcorrections=driftcorrections,usecorrectionv2=True)
00278 session.transaction().commit()
00279 if not options.outputfile:
00280 lumiReport.toScreenTotDelivered(result,iresults,options.scalefactor,options.verbose)
00281 else:
00282 lumiReport.toCSVTotDelivered(result,options.outputfile,iresults,options.scalefactor,options.verbose)
00283 if options.action == 'overview':
00284 session.transaction().start(True)
00285 result=lumiCalcAPI.lumiForRange(session.nominalSchema(),irunlsdict,amodetag=options.amodetag,egev=options.beamenergy,beamstatus=pbeammode,norm=normfactor,finecorrections=finecorrections,driftcorrections=driftcorrections,usecorrectionv2=True)
00286 session.transaction().commit()
00287 if not options.outputfile:
00288 lumiReport.toScreenOverview(result,iresults,options.scalefactor,options.verbose)
00289 else:
00290 lumiReport.toCSVOverview(result,options.outputfile,iresults,options.scalefactor,options.verbose)
00291 if options.action == 'lumibyls':
00292 if not options.hltpath:
00293 session.transaction().start(True)
00294 result=lumiCalcAPI.lumiForRange(session.nominalSchema(),irunlsdict,amodetag=options.amodetag,egev=options.beamenergy,beamstatus=pbeammode,norm=normfactor,finecorrections=finecorrections,driftcorrections=driftcorrections,usecorrectionv2=True)
00295 session.transaction().commit()
00296 if not options.outputfile:
00297 lumiReport.toScreenLumiByLS(result,iresults,options.scalefactor,options.verbose)
00298 else:
00299 lumiReport.toCSVLumiByLS(result,options.outputfile,iresults,options.scalefactor,options.verbose)
00300 else:
00301 hltname=options.hltpath
00302 hltpat=None
00303 if hltname=='*' or hltname=='all':
00304 hltname=None
00305 elif 1 in [c in hltname for c in '*?[]']:
00306 hltpat=hltname
00307 hltname=None
00308 session.transaction().start(True)
00309 result=lumiCalcAPI.effectiveLumiForRange(session.nominalSchema(),irunlsdict,hltpathname=hltname,hltpathpattern=hltpat,amodetag=options.amodetag,egev=options.beamenergy,beamstatus=pbeammode,norm=normfactor,finecorrections=finecorrections,driftcorrections=driftcorrections,usecorrectionv2=True)
00310 session.transaction().commit()
00311 if not options.outputfile:
00312 lumiReport.toScreenLSEffective(result,iresults,options.scalefactor,options.verbose)
00313 else:
00314 lumiReport.toCSVLSEffective(result,options.outputfile,iresults,options.scalefactor,options.verbose)
00315 if options.action == 'recorded':
00316 session.transaction().start(True)
00317 hltname=options.hltpath
00318 hltpat=None
00319 if hltname is not None:
00320 if hltname=='*' or hltname=='all':
00321 hltname=None
00322 elif 1 in [c in hltname for c in '*?[]']:
00323 hltpat=hltname
00324 hltname=None
00325 result=lumiCalcAPI.effectiveLumiForRange(session.nominalSchema(),irunlsdict,hltpathname=hltname,hltpathpattern=hltpat,amodetag=options.amodetag,egev=options.beamenergy,beamstatus=pbeammode,norm=normfactor,finecorrections=finecorrections,driftcorrections=driftcorrections,usecorrectionv2=True)
00326 session.transaction().commit()
00327 if not options.outputfile:
00328 lumiReport.toScreenTotEffective(result,iresults,options.scalefactor,options.verbose)
00329 else:
00330 lumiReport.toCSVTotEffective(result,options.outputfile,iresults,options.scalefactor,options.verbose)
00331 if options.action == 'lumibylsXing':
00332 session.transaction().start(True)
00333 result=lumiCalcAPI.lumiForRange(session.nominalSchema(),irunlsdict,amodetag=options.amodetag,egev=options.beamenergy,beamstatus=pbeammode,norm=normfactor,xingMinLum=options.xingMinLum,withBeamIntensity=False,withBXInfo=True,bxAlgo=options.xingAlgo,finecorrections=finecorrections,driftcorrections=True)
00334 session.transaction().commit()
00335 if not options.outputfile:
00336 lumiReport.toScreenLumiByLS(result,iresults,options.scalefactor,options.verbose)
00337 else:
00338 lumiReport.toCSVLumiByLSXing(result,options.scalefactor,options.outputfile)
00339 del session
00340 del svc