CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pileupReCalc_Lumis.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 external corrections (Pixel luminosity, for example). 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  # loop over pileup JSON as source, since it should have more lumi sections
97 
98  for (run, LSPUlist) in sorted (inputPileupRange.iteritems() ):
99  # now, look for matching run, then match lumi sections
100  #print "searching for run %d" % (run)
101  if run in inputRange.keys():
102  OUTPUTLINE+= ('"%d":' % run )
103  OUTPUTLINE+= ' ['
104 
105  lslist = inputRange[run]
106  #print "LSPUlist", LSPUlist
107  for LSnumber in LSPUlist:
108  if LSnumber in lslist.keys(): # do we find a match in pixel list for this LS?
109  PUlumiInfo = LSPUlist[LSnumber]
110  PixlumiInfo = lslist[LSnumber]
111  #print "found LS %d" % (LSnumber)
112  #print HLTlumiInfo
113  scale = 0
114  if PUlumiInfo[0] > 0.:
115  scale=PixlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
116 
117  if scale !=0 and (scale < 0.2 or scale > 5.0):
118  print 'Run %d, LS %d, Scale (%f), PixL (%f), PUL (%f) big change - please check!' % (run, LSnumber, scale, PixlumiInfo[1],PUlumiInfo[0])
119  # scale=1.01 # HLT integrated values are wrong, punt
120 
121  newIntLumi = scale*PUlumiInfo[0]
122  newRmsLumi = scale*PUlumiInfo[1]
123  newInstLumi = scale*PUlumiInfo[2]
124  if scale == 0: # keep old HF values - maybe lumis was zero anyway
125  newIntLumi = PUlumiInfo[0]
126  newRmsLumi = PUlumiInfo[1]
127  newInstLumi = PUlumiInfo[2]
128 
129  print 'Run %d, LS %d, Scale (%f), PixL (%f), PUL (%f) - 0 please check!' % (run, LSnumber, scale, PixlumiInfo[1],PUlumiInfo[0])
130  LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
131  OUTPUTLINE += LumiString
132 
133  #for study
134  #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
135 
136  else: # no match, keep HF values
137  newIntLumi = PUlumiInfo[0]
138  newRmsLumi = PUlumiInfo[1]
139  newInstLumi = PUlumiInfo[2]
140 
141  #print PUlumiInfo[0],HLTlumiInfo[1]
142  LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
143  OUTPUTLINE += LumiString
144 
145 
146  lastindex=len(OUTPUTLINE)-1
147  trunc = OUTPUTLINE[0:lastindex]
148  OUTPUTLINE = trunc
149  OUTPUTLINE += '], '
150 
151  else: # trouble
152  print "Run %d not found in Lumi/Pileup input file. Check your files!" % (run)
153 
154 
155 # print run
156 # print lslist
157 
158 # histFile = ROOT.TFile.Open (output, 'recreate')
159 # if not histFile:
160 # raise RuntimeError, \
161 # "Could not open '%s' as an output root file" % output
162 # pileupHist.Write()
163  #for hist in histList:
164  # hist.Write()
165 # histFile.Close()
166 # sys.exit()
167 
168  lastindex=len(OUTPUTLINE)-2
169  trunc = OUTPUTLINE[0:lastindex]
170  OUTPUTLINE = trunc
171  OUTPUTLINE += ' }'
172 
173  outputfile = open(options.outputfile,'w')
174  if not outputfile:
175  raise RuntimeError, \
176  "Could not open '%s' as an output JSON file" % output
177 
178  outputfile.write(OUTPUTLINE)
179  outputfile.close()
180 
181 
182 
183  sys.exit()