CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pileupReCalc_HLTpaths.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 VERSION='1.00'
3 import os,sys,time
4 import optparse
5 from RecoLuminosity.LumiDB import pileupParser
6 from RecoLuminosity.LumiDB import selectionParser
7 from RecoLuminosity.LumiDB import csvLumibyLSParser
8 from math import exp
9 from math import sqrt
10 
11 def parseInputFile(inputfilename):
12  '''
13  output ({run:[ls:[inlumi, meanint]]})
14  '''
15  selectf=open(inputfilename,'r')
16  inputfilecontent=selectf.read()
17  p=pileupParser.pileupParser(inputfilecontent)
18 
19 # p=inputFilesetParser.inputFilesetParser(inputfilename)
20  runlsbyfile=p.runsandls()
21  return runlsbyfile
22 
23 
24 
25 ##############################
26 ## ######################## ##
27 ## ## ################## ## ##
28 ## ## ## Main Program ## ## ##
29 ## ## ################## ## ##
30 ## ######################## ##
31 ##############################
32 
33 if __name__ == '__main__':
34 
35  parser = optparse.OptionParser ("Usage: %prog [--options]",
36  description = "Script to rescale pileup distributions using inputs derived by calculating luminosity for a given set of HLT paths. Input format must be -lumibyls-")
37 #
38 # parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Pileup Lumi Calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
39  CalculationModeChoices = ['truth', 'observed']
40 
41  #
42  # parse arguments
43  #
44  #
45  # basic arguments
46  #
47  #parser.add_argument('action',choices=allowedActions,
48  # help='command actions')
49  parser.add_option('-o',dest='outputfile',action='store',
50  default='PileupRecalcJSON.txt',
51  help='output pileup JSON file')
52  parser.add_option('-i',dest='inputfile',action='store',
53  help='Input Run/LS/lumis file for your trigger selection (required)')
54  parser.add_option('--inputLumiJSON',dest='inputLumiJSON',action='store',
55  help='Input Lumi/Pileup file in JSON format (required)')
56  parser.add_option('--verbose',dest='verbose',action='store_true',help='verbose mode for printing' )
57 
58  # parse arguments
59  try:
60  (options, args) = parser.parse_args()
61  except Exception , e:
62  print e
63 # if not args:
64 # parser.print_usage()
65 # sys.exit()
66 # if len (args) != 1:
67 # parser.print_usage()
68 # raise RuntimeError, "Exactly one output file must be given"
69 # output = args[0]
70 
71 # options=parser.parse_args()
72 
73  if options.verbose:
74  print 'General configuration'
75  print '\toutputfile: ',options.outputfile
76  print '\tinput selection file: ',options.inputfile
77 
78 
79  #inpf = open (options.inputfile, 'r')
80  #inputfilecontent = inpf.read()
81  inputRange = csvLumibyLSParser.csvLumibyLSParser (options.inputfile).runsandls()
82 
83  #print 'number of runs processed %d' % csvLumibyLSParser.csvLumibyLSParser (options.inputfile).numruns()
84 
85  #inputRange=inputFilesetParser.inputFilesetParser(options.inputfile)
86 
87 
88  inputPileupRange=parseInputFile(options.inputLumiJSON)
89 
90  # now, we have to find the information for the input runs and LumiSections
91  # in the Lumi/Pileup list. First, loop over inputs
92 
93  OUTPUTLINE = ""
94  OUTPUTLINE+='{'
95 
96  for (run, lslist) in sorted (inputRange.iteritems() ):
97  # now, look for matching run, then match lumi sections
98  #print "searching for run %d" % (run)
99  if run in inputPileupRange.keys():
100  OUTPUTLINE+= ('"%d":' % run )
101  OUTPUTLINE+= ' ['
102 
103  LSPUlist = inputPileupRange[run]
104  #print "LSPUlist", LSPUlist
105  for LSnumber in lslist:
106  if LSnumber in LSPUlist.keys():
107  PUlumiInfo = LSPUlist[LSnumber]
108  HLTlumiInfo = lslist[LSnumber]
109  #print "found LS %d" % (LSnumber)
110  #print HLTlumiInfo
111  scale = 0
112  if PUlumiInfo[0] > 0.:
113  scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
114 
115  if scale > 1.001:
116  print 'Run %d, LS %d, HLT Scale (%f), HLTL (%f), PUL (%f) larger than one - please check!' % (run, LSnumber, scale, HLTlumiInfo[1],PUlumiInfo[0])
117  scale=1.01 # HLT integrated values are wrong, punt
118 
119  newIntLumi = scale*PUlumiInfo[0]
120  newRmsLumi = PUlumiInfo[1]
121  newInstLumi = PUlumiInfo[2]
122  if scale == 0:
123  newInstLumi = PUlumiInfo[2] # keep non-zero value, with zero weight
124  # to avoid spike at zero interactions
125  #print PUlumiInfo[0],HLTlumiInfo[1]
126  LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
127  OUTPUTLINE += LumiString
128 
129  #for study
130  #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
131 
132  else: # no match, use zero for int lumi
133  newInstLumi = 10.0 # keep non-zero value, with zero weight
134  # to avoid spike at zero interactions
135  #print PUlumiInfo[0],HLTlumiInfo[1]
136  LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
137  OUTPUTLINE += LumiString
138 
139 
140  lastindex=len(OUTPUTLINE)-1
141  trunc = OUTPUTLINE[0:lastindex]
142  OUTPUTLINE = trunc
143  OUTPUTLINE += '], '
144 
145  else: # trouble
146  print "Run %d not found in Lumi/Pileup input file. Check your files!" % (run)
147 
148 
149 # print run
150 # print lslist
151 
152 # histFile = ROOT.TFile.Open (output, 'recreate')
153 # if not histFile:
154 # raise RuntimeError, \
155 # "Could not open '%s' as an output root file" % output
156 # pileupHist.Write()
157  #for hist in histList:
158  # hist.Write()
159 # histFile.Close()
160 # sys.exit()
161 
162  lastindex=len(OUTPUTLINE)-2
163  trunc = OUTPUTLINE[0:lastindex]
164  OUTPUTLINE = trunc
165  OUTPUTLINE += ' }'
166 
167  outputfile = open(options.outputfile,'w')
168  if not outputfile:
169  raise RuntimeError, \
170  "Could not open '%s' as an output JSON file" % output
171 
172  outputfile.write(OUTPUTLINE)
173  outputfile.close()
174 
175 
176 
177  sys.exit()