CMS 3D CMS Logo

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