Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:58

0001 #!/usr/bin/env python3
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 #    p=inputFilesetParser.inputFilesetParser(inputfilename)
0020     runlsbyfile=p.runsandls()
0021     return runlsbyfile
0022 
0023 
0024 
0025 ##############################
0026 ## ######################## ##
0027 ## ## ################## ## ##
0028 ## ## ## Main Program ## ## ##
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 #    parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Pileup Lumi Calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
0039     CalculationModeChoices = ['truth', 'observed']
0040 
0041     #
0042     # parse arguments
0043     #  
0044     #
0045     # basic arguments
0046     #
0047     #parser.add_argument('action',choices=allowedActions,
0048     #                    help='command actions')
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     # parse arguments
0059     try:
0060         (options, args) = parser.parse_args()
0061     except Exception as e:
0062         print(e)
0063 #    if not args:
0064 #        parser.print_usage()
0065 #        sys.exit()
0066 #    if len (args) != 1:
0067 #        parser.print_usage()
0068 #        raise RuntimeError, "Exactly one output file must be given"
0069 #    output = args[0]
0070     
0071 #    options=parser.parse_args()
0072 
0073     if options.verbose:
0074         print('General configuration')
0075         print('\toutputfile: ',options.outputfile)
0076         print('\tinput selection file: ',options.inputfile)
0077 
0078     #print options.runperiod
0079     #inpf = open (options.inputfile, 'r')
0080     #inputfilecontent = inpf.read()
0081       
0082     inputRange =  csvLumibyLSParser.csvLumibyLSParser (options.inputfile,options.runperiod).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     for (run, lslist) in sorted (inputRange.items()):
0098         # now, look for matching run, then match lumi sections
0099         #print "searching for run %d" % (run)
0100         if run in inputPileupRange.keys():
0101             OUTPUTLINE+= ('"%d":' % run )
0102             OUTPUTLINE+= ' ['
0103             
0104             LSPUlist = inputPileupRange[run]
0105             #print "LSPUlist", LSPUlist
0106             for LSnumber in lslist:
0107                 if LSnumber in LSPUlist.keys():
0108                     PUlumiInfo = LSPUlist[LSnumber]
0109                     HLTlumiInfo = lslist[LSnumber]
0110                     #print "found LS %d" % (LSnumber)
0111                     #print HLTlumiInfo
0112                     scale = 0
0113                     if PUlumiInfo[0] > 0.:
0114                         scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
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  # HLT integrated values are wrong, punt                        
0119 
0120                     newIntLumi = scale*PUlumiInfo[0]
0121                     newRmsLumi = PUlumiInfo[1]
0122                     newInstLumi = PUlumiInfo[2]
0123                     if scale == 0:
0124                         newInstLumi = PUlumiInfo[2]  # keep non-zero value, with zero weight
0125                                                      # to avoid spike at zero interactions
0126                     #print PUlumiInfo[0],HLTlumiInfo[1]
0127                     LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
0128                     OUTPUTLINE += LumiString
0129 
0130                     #for study
0131                     #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
0132 
0133                 else: # no match, use zero for int lumi
0134                     newInstLumi = 10.0  # keep non-zero value, with zero weight
0135                                         # to avoid spike at zero interactions
0136                     #print PUlumiInfo[0],HLTlumiInfo[1]
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:  # trouble
0147             print("Run %d not found in Lumi/Pileup input file.  Check your files!" % (run))
0148 
0149 
0150 #            print run
0151 #            print lslist
0152 
0153 #        histFile = ROOT.TFile.Open (output, 'recreate')
0154 #        if not histFile:
0155 #            raise RuntimeError, \
0156 #                 "Could not open '%s' as an output root file" % output
0157 #        pileupHist.Write()
0158         #for hist in histList:
0159         #    hist.Write()
0160 #        histFile.Close()
0161 #        sys.exit()
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()