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 external corrections (Pixel luminosity, for example).  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     
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 
0079     #inpf = open (options.inputfile, 'r')
0080     #inputfilecontent = inpf.read()
0081     inputRange =  csvLumibyLSParser.csvLumibyLSParser (options.inputfile).runsandls()
0082 
0083     #print 'number of runs processed %d' % csvLumibyLSParser.csvLumibyLSParser (options.inputfile).numruns()
0084 
0085     #inputRange=inputFilesetParser.inputFilesetParser(options.inputfile)
0086 
0087     
0088     inputPileupRange=parseInputFile(options.inputLumiJSON)
0089 
0090     # now, we have to find the information for the input runs and LumiSections 
0091     # in the Lumi/Pileup list. First, loop over inputs
0092 
0093     OUTPUTLINE = ""
0094     OUTPUTLINE+='{'
0095 
0096     # loop over pileup JSON as source, since it should have more lumi sections
0097 
0098     for (run, LSPUlist) in sorted (inputPileupRange.items()):
0099         # now, look for matching run, then match lumi sections
0100         #print "searching for run %d" % (run)
0101         if run in inputRange.keys():
0102             OUTPUTLINE+= ('"%d":' % run )
0103             OUTPUTLINE+= ' ['
0104             
0105             lslist = inputRange[run]
0106             #print "LSPUlist", LSPUlist
0107             for LSnumber in LSPUlist:
0108                 if LSnumber in lslist.keys():  # do we find a match in pixel list for this LS?
0109                     PUlumiInfo = LSPUlist[LSnumber]
0110                     PixlumiInfo = lslist[LSnumber]
0111                     #print "found LS %d" % (LSnumber)
0112                     #print HLTlumiInfo
0113                     scale = 0
0114                     if PUlumiInfo[0] > 0.:
0115                         scale=PixlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
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                     #    scale=1.01  # HLT integrated values are wrong, punt                        
0120 
0121                     newIntLumi = scale*PUlumiInfo[0]
0122                     newRmsLumi = scale*PUlumiInfo[1]
0123                     newInstLumi = scale*PUlumiInfo[2]
0124                     if scale == 0:   # keep old HF values - maybe lumis was zero anyway
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                     #for study
0134                     #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
0135 
0136                 else: # no match, keep HF values
0137                     newIntLumi = PUlumiInfo[0]
0138                     newRmsLumi = PUlumiInfo[1]
0139                     newInstLumi = PUlumiInfo[2]
0140 
0141                     #print PUlumiInfo[0],HLTlumiInfo[1]
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:  # trouble
0152             print("Run %d not found in Lumi/Pileup input file.  Check your files!" % (run))
0153 
0154 
0155 #            print run
0156 #            print lslist
0157 
0158 #        histFile = ROOT.TFile.Open (output, 'recreate')
0159 #        if not histFile:
0160 #            raise RuntimeError, \
0161 #                 "Could not open '%s' as an output root file" % output
0162 #        pileupHist.Write()
0163         #for hist in histList:
0164         #    hist.Write()
0165 #        histFile.Close()
0166 #        sys.exit()
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()