Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:26:34

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 HLT paths.  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     parser.add_option('--runperiod',dest='runperiod',action='store', default='Run1',help='select runperiod Run1 or Run2, default Run1' )
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     #print options.runperiod
0080     #inpf = open (options.inputfile, 'r')
0081     #inputfilecontent = inpf.read()
0082       
0083     inputRange =  csvLumibyLSParser.csvLumibyLSParser (options.inputfile,options.runperiod).runsandls()
0084 
0085     #print 'number of runs processed %d' % csvLumibyLSParser.csvLumibyLSParser (options.inputfile).numruns()
0086 
0087     #inputRange=inputFilesetParser.inputFilesetParser(options.inputfile)
0088 
0089     
0090     inputPileupRange=parseInputFile(options.inputLumiJSON)
0091 
0092     # now, we have to find the information for the input runs and LumiSections 
0093     # in the Lumi/Pileup list. First, loop over inputs
0094 
0095     OUTPUTLINE = ""
0096     OUTPUTLINE+='{'
0097 
0098     for (run, lslist) in sorted (inputRange.items()):
0099         # now, look for matching run, then match lumi sections
0100         #print "searching for run %d" % (run)
0101         if run in inputPileupRange.keys():
0102             OUTPUTLINE+= ('"%d":' % run )
0103             OUTPUTLINE+= ' ['
0104             
0105             LSPUlist = inputPileupRange[run]
0106             #print "LSPUlist", LSPUlist
0107             for LSnumber in lslist:
0108                 if LSnumber in LSPUlist.keys():
0109                     PUlumiInfo = LSPUlist[LSnumber]
0110                     HLTlumiInfo = lslist[LSnumber]
0111                     #print "found LS %d" % (LSnumber)
0112                     #print HLTlumiInfo
0113                     scale = 0
0114                     if PUlumiInfo[0] > 0.:
0115                         scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
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  # HLT integrated values are wrong, punt                        
0120 
0121                     newIntLumi = scale*PUlumiInfo[0]
0122                     newRmsLumi = PUlumiInfo[1]
0123                     newInstLumi = PUlumiInfo[2]
0124                     if scale == 0:
0125                         newInstLumi = PUlumiInfo[2]  # keep non-zero value, with zero weight
0126                                                      # to avoid spike at zero interactions
0127                     #print PUlumiInfo[0],HLTlumiInfo[1]
0128                     LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
0129                     OUTPUTLINE += LumiString
0130 
0131                     #for study
0132                     #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
0133 
0134                 else: # no match, use zero for int lumi
0135                     newInstLumi = 10.0  # keep non-zero value, with zero weight
0136                                         # to avoid spike at zero interactions
0137                     #print PUlumiInfo[0],HLTlumiInfo[1]
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:  # trouble
0148             print("Run %d not found in Lumi/Pileup input file.  Check your files!" % (run))
0149 
0150 
0151 #            print run
0152 #            print lslist
0153 
0154 #        histFile = ROOT.TFile.Open (output, 'recreate')
0155 #        if not histFile:
0156 #            raise RuntimeError, \
0157 #                 "Could not open '%s' as an output root file" % output
0158 #        pileupHist.Write()
0159         #for hist in histList:
0160         #    hist.Write()
0161 #        histFile.Close()
0162 #        sys.exit()
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()