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