Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-03 02:26:49

0001 #!/usr/bin/env python3
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 #    p=inputFilesetParser.inputFilesetParser(inputfilename)
0021     runlsbyfile=p.runsandls()
0022     return runlsbyfile
0023 
0024 
0025 
0026 ##############################
0027 ## ######################## ##
0028 ## ## ################## ## ##
0029 ## ## ## Main Program ## ## ##
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 #    parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Pileup Lumi Calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
0040     CalculationModeChoices = ['truth', 'observed']
0041 
0042     #
0043     # parse arguments
0044     #  
0045     #
0046     # basic arguments
0047     #
0048     #parser.add_argument('action',choices=allowedActions,
0049     #                    help='command actions')
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     # parse arguments
0060     try:
0061         (options, args) = parser.parse_args()
0062     except Exception as e:
0063         print(e)
0064 #    if not args:
0065 #        parser.print_usage()
0066 #        sys.exit()
0067 #    if len (args) != 1:
0068 #        parser.print_usage()
0069 #        raise RuntimeError, "Exactly one output file must be given"
0070 #    output = args[0]
0071     
0072 #    options=parser.parse_args()
0073 
0074     if options.verbose:
0075         print('General configuration')
0076         print('\toutputfile: ',options.outputfile)
0077         print('\tinput selection file: ',options.inputfile)
0078 
0079 
0080     #inpf = open (options.inputfile, 'r')
0081     #inputfilecontent = inpf.read()
0082     inputRange =  csvLumibyLSParser.csvLumibyLSParser (options.inputfile).runsandls()
0083 
0084     #print 'number of runs processed %d' % csvLumibyLSParser.csvLumibyLSParser (options.inputfile).numruns()
0085 
0086     #inputRange=inputFilesetParser.inputFilesetParser(options.inputfile)
0087 
0088     
0089     inputPileupRange=parseInputFile(options.inputLumiJSON)
0090 
0091     # now, we have to find the information for the input runs and LumiSections 
0092     # in the Lumi/Pileup list. First, loop over inputs
0093 
0094     OUTPUTLINE = ""
0095     OUTPUTLINE+='{'
0096 
0097     # loop over pileup JSON as source, since it should have more lumi sections
0098 
0099     for (run, LSPUlist) in sorted (inputPileupRange.items()):
0100         # now, look for matching run, then match lumi sections
0101         #print "searching for run %d" % (run)
0102         if run in inputRange.keys():
0103             OUTPUTLINE+= ('"%d":' % run )
0104             OUTPUTLINE+= ' ['
0105             
0106             lslist = inputRange[run]
0107             #print "LSPUlist", LSPUlist
0108             for LSnumber in LSPUlist:
0109                 if LSnumber in lslist.keys():  # do we find a match in pixel list for this LS?
0110                     PUlumiInfo = LSPUlist[LSnumber]
0111                     PixlumiInfo = lslist[LSnumber]
0112                     #print "found LS %d" % (LSnumber)
0113                     #print HLTlumiInfo
0114                     scale = 0
0115                     if PUlumiInfo[0] > 0.:
0116                         scale=PixlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
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                     #    scale=1.01  # HLT integrated values are wrong, punt                        
0121 
0122                     newIntLumi = scale*PUlumiInfo[0]
0123                     newRmsLumi = scale*PUlumiInfo[1]
0124                     newInstLumi = scale*PUlumiInfo[2]
0125                     if scale == 0:   # keep old HF values - maybe lumis was zero anyway
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                     #for study
0135                     #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
0136 
0137                 else: # no match, keep HF values
0138                     newIntLumi = PUlumiInfo[0]
0139                     newRmsLumi = PUlumiInfo[1]
0140                     newInstLumi = PUlumiInfo[2]
0141 
0142                     #print PUlumiInfo[0],HLTlumiInfo[1]
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:  # trouble
0153             print("Run %d not found in Lumi/Pileup input file.  Check your files!" % (run))
0154 
0155 
0156 #            print run
0157 #            print lslist
0158 
0159 #        histFile = ROOT.TFile.Open (output, 'recreate')
0160 #        if not histFile:
0161 #            raise RuntimeError, \
0162 #                 "Could not open '%s' as an output root file" % output
0163 #        pileupHist.Write()
0164         #for hist in histList:
0165         #    hist.Write()
0166 #        histFile.Close()
0167 #        sys.exit()
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()