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