00001
00002 VERSION='1.00'
00003 import os,sys,time
00004 import optparse
00005 from RecoLuminosity.LumiDB import pileupParser
00006 from RecoLuminosity.LumiDB import selectionParser
00007 from RecoLuminosity.LumiDB import csvLumibyLSParser
00008 from math import exp
00009 from math import sqrt
00010
00011 def parseInputFile(inputfilename):
00012 '''
00013 output ({run:[ls:[inlumi, meanint]]})
00014 '''
00015 selectf=open(inputfilename,'r')
00016 inputfilecontent=selectf.read()
00017 p=pileupParser.pileupParser(inputfilecontent)
00018
00019
00020 runlsbyfile=p.runsandls()
00021 return runlsbyfile
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 if __name__ == '__main__':
00034
00035 parser = optparse.OptionParser ("Usage: %prog [--options]",
00036 description = "Script to rescale pileup distributions using inputs derived by calculating luminosity for a given set of HLT paths. Input format must be -lumibyls-")
00037
00038
00039 CalculationModeChoices = ['truth', 'observed']
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 parser.add_option('-o',dest='outputfile',action='store',
00050 default='PileupRecalcJSON.txt',
00051 help='output pileup JSON file')
00052 parser.add_option('-i',dest='inputfile',action='store',
00053 help='Input Run/LS/lumis file for your trigger selection (required)')
00054 parser.add_option('--inputLumiJSON',dest='inputLumiJSON',action='store',
00055 help='Input Lumi/Pileup file in JSON format (required)')
00056 parser.add_option('--verbose',dest='verbose',action='store_true',help='verbose mode for printing' )
00057
00058
00059 try:
00060 (options, args) = parser.parse_args()
00061 except Exception , e:
00062 print e
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 if options.verbose:
00074 print 'General configuration'
00075 print '\toutputfile: ',options.outputfile
00076 print '\tinput selection file: ',options.inputfile
00077
00078
00079
00080
00081 inputRange = csvLumibyLSParser.csvLumibyLSParser (options.inputfile).runsandls()
00082
00083
00084
00085
00086
00087
00088 inputPileupRange=parseInputFile(options.inputLumiJSON)
00089
00090
00091
00092
00093 OUTPUTLINE = ""
00094 OUTPUTLINE+='{'
00095
00096 for (run, lslist) in sorted (inputRange.iteritems() ):
00097
00098
00099 if run in inputPileupRange.keys():
00100 OUTPUTLINE+= ('"%d":' % run )
00101 OUTPUTLINE+= ' ['
00102
00103 LSPUlist = inputPileupRange[run]
00104
00105 for LSnumber in lslist:
00106 if LSnumber in LSPUlist.keys():
00107 PUlumiInfo = LSPUlist[LSnumber]
00108 HLTlumiInfo = lslist[LSnumber]
00109
00110
00111 scale = 0
00112 if PUlumiInfo[0] > 0.:
00113 scale=HLTlumiInfo[1]/PUlumiInfo[0]
00114
00115 if scale > 1.001:
00116 print 'Run %d, LS %d, HLT Scale (%f), HLTL (%f), PUL (%f) larger than one - please check!' % (run, LSnumber, scale, HLTlumiInfo[1],PUlumiInfo[0])
00117 scale=1.01
00118
00119 newIntLumi = scale*PUlumiInfo[0]
00120 newRmsLumi = PUlumiInfo[1]
00121 newInstLumi = PUlumiInfo[2]
00122 if scale == 0:
00123 newInstLumi = PUlumiInfo[2]
00124
00125
00126 LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
00127 OUTPUTLINE += LumiString
00128
00129
00130
00131
00132 else:
00133 newInstLumi = 10.0
00134
00135
00136 LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
00137 OUTPUTLINE += LumiString
00138
00139
00140 lastindex=len(OUTPUTLINE)-1
00141 trunc = OUTPUTLINE[0:lastindex]
00142 OUTPUTLINE = trunc
00143 OUTPUTLINE += '], '
00144
00145 else:
00146 print "Run %d not found in Lumi/Pileup input file. Check your files!" % (run)
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 lastindex=len(OUTPUTLINE)-2
00163 trunc = OUTPUTLINE[0:lastindex]
00164 OUTPUTLINE = trunc
00165 OUTPUTLINE += ' }'
00166
00167 outputfile = open(options.outputfile,'w')
00168 if not outputfile:
00169 raise RuntimeError, \
00170 "Could not open '%s' as an output JSON file" % output
00171
00172 outputfile.write(OUTPUTLINE)
00173 outputfile.close()
00174
00175
00176
00177 sys.exit()