CMS 3D CMS Logo

pileupReCalc_HLTpaths.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 
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 HLT paths. 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  parser.add_option('--runperiod',dest='runperiod',action='store', default='Run1',help='select runperiod Run1 or Run2, default Run1' )
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  #print options.runperiod
81  #inpf = open (options.inputfile, 'r')
82  #inputfilecontent = inpf.read()
83 
84  inputRange = csvLumibyLSParser.csvLumibyLSParser (options.inputfile,options.runperiod).runsandls()
85 
86  #print 'number of runs processed %d' % csvLumibyLSParser.csvLumibyLSParser (options.inputfile).numruns()
87 
88  #inputRange=inputFilesetParser.inputFilesetParser(options.inputfile)
89 
90 
91  inputPileupRange=parseInputFile(options.inputLumiJSON)
92 
93  # now, we have to find the information for the input runs and LumiSections
94  # in the Lumi/Pileup list. First, loop over inputs
95 
96  OUTPUTLINE = ""
97  OUTPUTLINE+='{'
98 
99  for (run, lslist) in sorted (six.iteritems(inputRange)):
100  # now, look for matching run, then match lumi sections
101  #print "searching for run %d" % (run)
102  if run in inputPileupRange.keys():
103  OUTPUTLINE+= ('"%d":' % run )
104  OUTPUTLINE+= ' ['
105 
106  LSPUlist = inputPileupRange[run]
107  #print "LSPUlist", LSPUlist
108  for LSnumber in lslist:
109  if LSnumber in LSPUlist.keys():
110  PUlumiInfo = LSPUlist[LSnumber]
111  HLTlumiInfo = lslist[LSnumber]
112  #print "found LS %d" % (LSnumber)
113  #print HLTlumiInfo
114  scale = 0
115  if PUlumiInfo[0] > 0.:
116  scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
117 
118  if scale > 1.001:
119  print('Run %d, LS %d, HLT Scale (%f), HLTL (%f), PUL (%f) larger than one - please check!' % (run, LSnumber, scale, HLTlumiInfo[1],PUlumiInfo[0]))
120  scale=1.01 # HLT integrated values are wrong, punt
121 
122  newIntLumi = scale*PUlumiInfo[0]
123  newRmsLumi = PUlumiInfo[1]
124  newInstLumi = PUlumiInfo[2]
125  if scale == 0:
126  newInstLumi = PUlumiInfo[2] # keep non-zero value, with zero weight
127  # to avoid spike at zero interactions
128  #print PUlumiInfo[0],HLTlumiInfo[1]
129  LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
130  OUTPUTLINE += LumiString
131 
132  #for study
133  #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
134 
135  else: # no match, use zero for int lumi
136  newInstLumi = 10.0 # keep non-zero value, with zero weight
137  # to avoid spike at zero interactions
138  #print PUlumiInfo[0],HLTlumiInfo[1]
139  LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
140  OUTPUTLINE += LumiString
141 
142 
143  lastindex=len(OUTPUTLINE)-1
144  trunc = OUTPUTLINE[0:lastindex]
145  OUTPUTLINE = trunc
146  OUTPUTLINE += '], '
147 
148  else: # trouble
149  print("Run %d not found in Lumi/Pileup input file. Check your files!" % (run))
150 
151 
152 # print run
153 # print lslist
154 
155 # histFile = ROOT.TFile.Open (output, 'recreate')
156 # if not histFile:
157 # raise RuntimeError, \
158 # "Could not open '%s' as an output root file" % output
159 # pileupHist.Write()
160  #for hist in histList:
161  # hist.Write()
162 # histFile.Close()
163 # sys.exit()
164 
165  lastindex=len(OUTPUTLINE)-2
166  trunc = OUTPUTLINE[0:lastindex]
167  OUTPUTLINE = trunc
168  OUTPUTLINE += ' }'
169 
170  outputfile = open(options.outputfile,'w')
171  if not outputfile:
172  raise RuntimeError("Could not open '%s' as an output JSON file" % output)
173 
174  outputfile.write(OUTPUTLINE)
175  outputfile.close()
176 
177 
178 
179  sys.exit()
pileupParser.pileupParser
Definition: pileupParser.py:4
csvLumibyLSParser.csvLumibyLSParser
Definition: csvLumibyLSParser.py:13
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
pileupReCalc_HLTpaths.parseInputFile
def parseInputFile(inputfilename)
Definition: pileupReCalc_HLTpaths.py:13