Go to the documentation of this file.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
00108 PUlumiInfo = LSPUlist[LSnumber]
00109 HLTlumiInfo = lslist[LSnumber]
00110 scale = 0
00111 if PUlumiInfo[0] > 0.:
00112 scale=HLTlumiInfo[1]/PUlumiInfo[0]
00113
00114 if scale > 1.001:
00115 print 'Run %d, LS %d, HLT Scale (%f) larger than one - please check!' % (run, LSnumber, scale)
00116 scale=1.01
00117
00118 newIntLumi = scale*PUlumiInfo[0]
00119 newRmsLumi = PUlumiInfo[1]
00120 newInstLumi = PUlumiInfo[2]
00121 if scale == 0:
00122 newInstLumi = PUlumiInfo[2]
00123
00124
00125 LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
00126 OUTPUTLINE += LumiString
00127
00128
00129
00130
00131 else:
00132 newInstLumi = 10.0
00133
00134
00135 LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
00136 OUTPUTLINE += LumiString
00137
00138
00139 lastindex=len(OUTPUTLINE)-1
00140 trunc = OUTPUTLINE[0:lastindex]
00141 OUTPUTLINE = trunc
00142 OUTPUTLINE += '], '
00143
00144 else:
00145 print "Run %d not found in Lumi/Pileup input file. Check your files!" % (run)
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 lastindex=len(OUTPUTLINE)-2
00162 trunc = OUTPUTLINE[0:lastindex]
00163 OUTPUTLINE = trunc
00164 OUTPUTLINE += ' }'
00165
00166 outputfile = open(options.outputfile,'w')
00167 if not outputfile:
00168 raise RuntimeError, \
00169 "Could not open '%s' as an output JSON file" % output
00170
00171 outputfile.write(OUTPUTLINE)
00172 outputfile.close()
00173
00174
00175
00176 sys.exit()