CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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                     # print "found LS %d" % (LSnumber)
00108                     PUlumiInfo = LSPUlist[LSnumber]
00109                     HLTlumiInfo = lslist[LSnumber]
00110                     scale = 0
00111                     if PUlumiInfo[0] > 0.:
00112                         scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
00113 
00114                     if scale > 1.001:
00115                         print 'Run %d, LS %d, HLT Scale (%f) larger than one - please check!' % (run, LSnumber, scale)
00116                         scale=1.01  # HLT integrated values are wrong, punt                        
00117 
00118                     newIntLumi = scale*PUlumiInfo[0]
00119                     newRmsLumi = PUlumiInfo[1]
00120                     newInstLumi = PUlumiInfo[2]
00121                     if scale == 0:
00122                         newInstLumi = PUlumiInfo[2]  # keep non-zero value, with zero weight
00123                                                      # to avoid spike at zero interactions
00124                     #print PUlumiInfo[0],HLTlumiInfo[1]
00125                     LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
00126                     OUTPUTLINE += LumiString
00127 
00128                     #for study
00129                     #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
00130 
00131                 else: # no match, use zero for int lumi
00132                     newInstLumi = 10.0  # keep non-zero value, with zero weight
00133                                         # to avoid spike at zero interactions
00134                     #print PUlumiInfo[0],HLTlumiInfo[1]
00135                     LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
00136                     OUTPUTLINE += LumiString
00137                     
00138 
00139             lastindex=len(OUTPUTLINE)-1
00140             trunc = OUTPUTLINE[0:lastindex]
00141             OUTPUTLINE = trunc
00142             OUTPUTLINE += '], '
00143 
00144         else:  # trouble
00145             print "Run %d not found in Lumi/Pileup input file.  Check your files!" % (run)
00146 
00147 
00148 #            print run
00149 #            print lslist
00150 
00151 #        histFile = ROOT.TFile.Open (output, 'recreate')
00152 #        if not histFile:
00153 #            raise RuntimeError, \
00154 #                 "Could not open '%s' as an output root file" % output
00155 #        pileupHist.Write()
00156         #for hist in histList:
00157         #    hist.Write()
00158 #        histFile.Close()
00159 #        sys.exit()
00160 
00161     lastindex=len(OUTPUTLINE)-2
00162     trunc = OUTPUTLINE[0:lastindex]
00163     OUTPUTLINE = trunc
00164     OUTPUTLINE += ' }'
00165 
00166     outputfile = open(options.outputfile,'w')
00167     if not outputfile:
00168         raise RuntimeError, \
00169               "Could not open '%s' as an output JSON file" % output
00170                     
00171     outputfile.write(OUTPUTLINE)
00172     outputfile.close()
00173 
00174 
00175 
00176     sys.exit()