File indexing completed on 2023-03-17 11:19:54
0001
0002 from __future__ import print_function
0003 VERSION='1.00'
0004 import os,sys,time
0005 import optparse
0006 from RecoLuminosity.LumiDB import pileupParser
0007 from RecoLuminosity.LumiDB import selectionParser
0008 from RecoLuminosity.LumiDB import csvLumibyLSParser
0009 from math import exp
0010 from math import sqrt
0011
0012 def parseInputFile(inputfilename):
0013 '''
0014 output ({run:[ls:[inlumi, meanint]]})
0015 '''
0016 selectf=open(inputfilename,'r')
0017 inputfilecontent=selectf.read()
0018 p=pileupParser.pileupParser(inputfilecontent)
0019
0020
0021 runlsbyfile=p.runsandls()
0022 return runlsbyfile
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 if __name__ == '__main__':
0035
0036 parser = optparse.OptionParser ("Usage: %prog [--options]",
0037 description = "Script to rescale pileup distributions using inputs derived by calculating luminosity for a given set of HLT paths. Input format must be -lumibyls-")
0038
0039
0040 CalculationModeChoices = ['truth', 'observed']
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 parser.add_option('-o',dest='outputfile',action='store',
0051 default='PileupRecalcJSON.txt',
0052 help='output pileup JSON file')
0053 parser.add_option('-i',dest='inputfile',action='store',
0054 help='Input Run/LS/lumis file for your trigger selection (required)')
0055 parser.add_option('--inputLumiJSON',dest='inputLumiJSON',action='store',
0056 help='Input Lumi/Pileup file in JSON format (required)')
0057 parser.add_option('--verbose',dest='verbose',action='store_true',help='verbose mode for printing' )
0058 parser.add_option('--runperiod',dest='runperiod',action='store', default='Run1',help='select runperiod Run1 or Run2, default Run1' )
0059
0060 try:
0061 (options, args) = parser.parse_args()
0062 except Exception as e:
0063 print(e)
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 if options.verbose:
0075 print('General configuration')
0076 print('\toutputfile: ',options.outputfile)
0077 print('\tinput selection file: ',options.inputfile)
0078
0079
0080
0081
0082
0083 inputRange = csvLumibyLSParser.csvLumibyLSParser (options.inputfile,options.runperiod).runsandls()
0084
0085
0086
0087
0088
0089
0090 inputPileupRange=parseInputFile(options.inputLumiJSON)
0091
0092
0093
0094
0095 OUTPUTLINE = ""
0096 OUTPUTLINE+='{'
0097
0098 for (run, lslist) in sorted (inputRange.items()):
0099
0100
0101 if run in inputPileupRange.keys():
0102 OUTPUTLINE+= ('"%d":' % run )
0103 OUTPUTLINE+= ' ['
0104
0105 LSPUlist = inputPileupRange[run]
0106
0107 for LSnumber in lslist:
0108 if LSnumber in LSPUlist.keys():
0109 PUlumiInfo = LSPUlist[LSnumber]
0110 HLTlumiInfo = lslist[LSnumber]
0111
0112
0113 scale = 0
0114 if PUlumiInfo[0] > 0.:
0115 scale=HLTlumiInfo[1]/PUlumiInfo[0]
0116
0117 if scale > 1.001:
0118 print('Run %d, LS %d, HLT Scale (%f), HLTL (%f), PUL (%f) larger than one - please check!' % (run, LSnumber, scale, HLTlumiInfo[1],PUlumiInfo[0]))
0119 scale=1.01
0120
0121 newIntLumi = scale*PUlumiInfo[0]
0122 newRmsLumi = PUlumiInfo[1]
0123 newInstLumi = PUlumiInfo[2]
0124 if scale == 0:
0125 newInstLumi = PUlumiInfo[2]
0126
0127
0128 LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
0129 OUTPUTLINE += LumiString
0130
0131
0132
0133
0134 else:
0135 newInstLumi = 10.0
0136
0137
0138 LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
0139 OUTPUTLINE += LumiString
0140
0141
0142 lastindex=len(OUTPUTLINE)-1
0143 trunc = OUTPUTLINE[0:lastindex]
0144 OUTPUTLINE = trunc
0145 OUTPUTLINE += '], '
0146
0147 else:
0148 print("Run %d not found in Lumi/Pileup input file. Check your files!" % (run))
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164 lastindex=len(OUTPUTLINE)-2
0165 trunc = OUTPUTLINE[0:lastindex]
0166 OUTPUTLINE = trunc
0167 OUTPUTLINE += ' }'
0168
0169 outputfile = open(options.outputfile,'w')
0170 if not outputfile:
0171 raise RuntimeError("Could not open '%s' as an output JSON file" % output)
0172
0173 outputfile.write(OUTPUTLINE)
0174 outputfile.close()
0175
0176
0177
0178 sys.exit()