CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/RecoLuminosity/LumiDB/scripts/pileupReCalc_HLTpaths.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 VERSION='1.00'
00003 import os,sys,time
00004 import optparse
00005 from RecoLuminosity.LumiDB import pileupParser
00006 from RecoLuminosity.LumiDB import selectionParser
00007 from RecoLuminosity.LumiDB import csvLumibyLSParser
00008 from math import exp
00009 from math import sqrt
00010 
00011 def parseInputFile(inputfilename):
00012     '''
00013     output ({run:[ls:[inlumi, meanint]]})
00014     '''
00015     selectf=open(inputfilename,'r')
00016     inputfilecontent=selectf.read()
00017     p=pileupParser.pileupParser(inputfilecontent)                            
00018     
00019 #    p=inputFilesetParser.inputFilesetParser(inputfilename)
00020     runlsbyfile=p.runsandls()
00021     return runlsbyfile
00022 
00023 
00024 
00025 ##############################
00026 ## ######################## ##
00027 ## ## ################## ## ##
00028 ## ## ## Main Program ## ## ##
00029 ## ## ################## ## ##
00030 ## ######################## ##
00031 ##############################
00032 
00033 if __name__ == '__main__':
00034 
00035     parser = optparse.OptionParser ("Usage: %prog [--options]",
00036                                     description = "Script to rescale pileup distributions using inputs derived by calculating luminosity for a given set of HLT paths.  Input format must be -lumibyls-")
00037 #
00038 #    parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Pileup Lumi Calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00039     CalculationModeChoices = ['truth', 'observed']
00040 
00041     #
00042     # parse arguments
00043     #  
00044     #
00045     # basic arguments
00046     #
00047     #parser.add_argument('action',choices=allowedActions,
00048     #                    help='command actions')
00049     parser.add_option('-o',dest='outputfile',action='store',
00050                         default='PileupRecalcJSON.txt',
00051                         help='output pileup JSON file')
00052     parser.add_option('-i',dest='inputfile',action='store',
00053                         help='Input Run/LS/lumis file for your trigger selection  (required)')
00054     parser.add_option('--inputLumiJSON',dest='inputLumiJSON',action='store',
00055                         help='Input Lumi/Pileup file in JSON format (required)')
00056     parser.add_option('--verbose',dest='verbose',action='store_true',help='verbose mode for printing' )
00057     
00058     # parse arguments
00059     try:
00060         (options, args) = parser.parse_args()
00061     except Exception , e:
00062         print e
00063 #    if not args:
00064 #        parser.print_usage()
00065 #        sys.exit()
00066 #    if len (args) != 1:
00067 #        parser.print_usage()
00068 #        raise RuntimeError, "Exactly one output file must be given"
00069 #    output = args[0]
00070     
00071 #    options=parser.parse_args()
00072 
00073     if options.verbose:
00074         print 'General configuration'
00075         print '\toutputfile: ',options.outputfile
00076         print '\tinput selection file: ',options.inputfile
00077 
00078 
00079     #inpf = open (options.inputfile, 'r')
00080     #inputfilecontent = inpf.read()
00081     inputRange =  csvLumibyLSParser.csvLumibyLSParser (options.inputfile).runsandls()
00082 
00083     #print 'number of runs processed %d' % csvLumibyLSParser.csvLumibyLSParser (options.inputfile).numruns()
00084 
00085     #inputRange=inputFilesetParser.inputFilesetParser(options.inputfile)
00086 
00087     
00088     inputPileupRange=parseInputFile(options.inputLumiJSON)
00089 
00090     # now, we have to find the information for the input runs and LumiSections 
00091     # in the Lumi/Pileup list. First, loop over inputs
00092 
00093     OUTPUTLINE = ""
00094     OUTPUTLINE+='{'
00095 
00096     for (run, lslist) in sorted (inputRange.iteritems() ):
00097         # now, look for matching run, then match lumi sections
00098         #print "searching for run %d" % (run)
00099         if run in inputPileupRange.keys():
00100             OUTPUTLINE+= ('"%d":' % run )
00101             OUTPUTLINE+= ' ['
00102             
00103             LSPUlist = inputPileupRange[run]
00104             #print "LSPUlist", LSPUlist
00105             for LSnumber in lslist:
00106                 if LSnumber in LSPUlist.keys():
00107                     PUlumiInfo = LSPUlist[LSnumber]
00108                     HLTlumiInfo = lslist[LSnumber]
00109                     #print "found LS %d" % (LSnumber)
00110                     #print HLTlumiInfo
00111                     scale = 0
00112                     if PUlumiInfo[0] > 0.:
00113                         scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
00114 
00115                     if scale > 1.001:
00116                         print 'Run %d, LS %d, HLT Scale (%f), HLTL (%f), PUL (%f) larger than one - please check!' % (run, LSnumber, scale, HLTlumiInfo[1],PUlumiInfo[0])
00117                         scale=1.01  # HLT integrated values are wrong, punt                        
00118 
00119                     newIntLumi = scale*PUlumiInfo[0]
00120                     newRmsLumi = PUlumiInfo[1]
00121                     newInstLumi = PUlumiInfo[2]
00122                     if scale == 0:
00123                         newInstLumi = PUlumiInfo[2]  # keep non-zero value, with zero weight
00124                                                      # to avoid spike at zero interactions
00125                     #print PUlumiInfo[0],HLTlumiInfo[1]
00126                     LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
00127                     OUTPUTLINE += LumiString
00128 
00129                     #for study
00130                     #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
00131 
00132                 else: # no match, use zero for int lumi
00133                     newInstLumi = 10.0  # keep non-zero value, with zero weight
00134                                         # to avoid spike at zero interactions
00135                     #print PUlumiInfo[0],HLTlumiInfo[1]
00136                     LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
00137                     OUTPUTLINE += LumiString
00138                     
00139 
00140             lastindex=len(OUTPUTLINE)-1
00141             trunc = OUTPUTLINE[0:lastindex]
00142             OUTPUTLINE = trunc
00143             OUTPUTLINE += '], '
00144 
00145         else:  # trouble
00146             print "Run %d not found in Lumi/Pileup input file.  Check your files!" % (run)
00147 
00148 
00149 #            print run
00150 #            print lslist
00151 
00152 #        histFile = ROOT.TFile.Open (output, 'recreate')
00153 #        if not histFile:
00154 #            raise RuntimeError, \
00155 #                 "Could not open '%s' as an output root file" % output
00156 #        pileupHist.Write()
00157         #for hist in histList:
00158         #    hist.Write()
00159 #        histFile.Close()
00160 #        sys.exit()
00161 
00162     lastindex=len(OUTPUTLINE)-2
00163     trunc = OUTPUTLINE[0:lastindex]
00164     OUTPUTLINE = trunc
00165     OUTPUTLINE += ' }'
00166 
00167     outputfile = open(options.outputfile,'w')
00168     if not outputfile:
00169         raise RuntimeError, \
00170               "Could not open '%s' as an output JSON file" % output
00171                     
00172     outputfile.write(OUTPUTLINE)
00173     outputfile.close()
00174 
00175 
00176 
00177     sys.exit()