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 external corrections (Pixel luminosity, for example). 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
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 inputRange = csvLumibyLSParser.csvLumibyLSParser (options.inputfile).runsandls()
0083
0084
0085
0086
0087
0088
0089 inputPileupRange=parseInputFile(options.inputLumiJSON)
0090
0091
0092
0093
0094 OUTPUTLINE = ""
0095 OUTPUTLINE+='{'
0096
0097
0098
0099 for (run, LSPUlist) in sorted (inputPileupRange.items()):
0100
0101
0102 if run in inputRange.keys():
0103 OUTPUTLINE+= ('"%d":' % run )
0104 OUTPUTLINE+= ' ['
0105
0106 lslist = inputRange[run]
0107
0108 for LSnumber in LSPUlist:
0109 if LSnumber in lslist.keys():
0110 PUlumiInfo = LSPUlist[LSnumber]
0111 PixlumiInfo = lslist[LSnumber]
0112
0113
0114 scale = 0
0115 if PUlumiInfo[0] > 0.:
0116 scale=PixlumiInfo[1]/PUlumiInfo[0]
0117
0118 if scale !=0 and (scale < 0.2 or scale > 5.0):
0119 print('Run %d, LS %d, Scale (%f), PixL (%f), PUL (%f) big change - please check!' % (run, LSnumber, scale, PixlumiInfo[1],PUlumiInfo[0]))
0120
0121
0122 newIntLumi = scale*PUlumiInfo[0]
0123 newRmsLumi = scale*PUlumiInfo[1]
0124 newInstLumi = scale*PUlumiInfo[2]
0125 if scale == 0:
0126 newIntLumi = PUlumiInfo[0]
0127 newRmsLumi = PUlumiInfo[1]
0128 newInstLumi = PUlumiInfo[2]
0129
0130 print('Run %d, LS %d, Scale (%f), PixL (%f), PUL (%f) - 0 please check!' % (run, LSnumber, scale, PixlumiInfo[1],PUlumiInfo[0]))
0131 LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
0132 OUTPUTLINE += LumiString
0133
0134
0135
0136
0137 else:
0138 newIntLumi = PUlumiInfo[0]
0139 newRmsLumi = PUlumiInfo[1]
0140 newInstLumi = PUlumiInfo[2]
0141
0142
0143 LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
0144 OUTPUTLINE += LumiString
0145
0146
0147 lastindex=len(OUTPUTLINE)-1
0148 trunc = OUTPUTLINE[0:lastindex]
0149 OUTPUTLINE = trunc
0150 OUTPUTLINE += '], '
0151
0152 else:
0153 print("Run %d not found in Lumi/Pileup input file. Check your files!" % (run))
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169 lastindex=len(OUTPUTLINE)-2
0170 trunc = OUTPUTLINE[0:lastindex]
0171 OUTPUTLINE = trunc
0172 OUTPUTLINE += ' }'
0173
0174 outputfile = open(options.outputfile,'w')
0175 if not outputfile:
0176 raise RuntimeError("Could not open '%s' as an output JSON file" % output)
0177
0178 outputfile.write(OUTPUTLINE)
0179 outputfile.close()
0180
0181
0182
0183 sys.exit()