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