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: # happens by default for HF in Run2, therefore only print out if significant
117  if scale > 1.006:
118  print 'Run %d, LS %d, HLT Scale (%f), HLTL (%f), PUL (%f) larger than one - please check!' % (run, LSnumber, scale, HLTlumiInfo[1],PUlumiInfo[0])
119  scale=1.0 # HLT integrated values are wrong, punt
120 
121  newIntLumi = scale*PUlumiInfo[0]
122  newRmsLumi = PUlumiInfo[1]
123  newInstLumi = PUlumiInfo[2]
124  if scale == 0:
125  newInstLumi = PUlumiInfo[2] # keep non-zero value, with zero weight
126  # to avoid spike at zero interactions
127  #print PUlumiInfo[0],HLTlumiInfo[1]
128  LumiString = "[%d,%2.4e,%2.4e,%2.4e]," % (LSnumber, newIntLumi, newRmsLumi ,newInstLumi)
129  OUTPUTLINE += LumiString
130 
131  #for study
132  #print '%d %d %f %f' % (run,LSnumber,PUlumiInfo[0],HLTlumiInfo[1])
133 
134  else: # no match, use zero for int lumi
135  newInstLumi = 10.0 # keep non-zero value, with zero weight
136  # to avoid spike at zero interactions
137  #print PUlumiInfo[0],HLTlumiInfo[1]
138  LumiString = "[%d,0.0,0.0,%2.4e]," % (LSnumber, newInstLumi)
139  OUTPUTLINE += LumiString
140 
141 
142  lastindex=len(OUTPUTLINE)-1
143  trunc = OUTPUTLINE[0:lastindex]
144  OUTPUTLINE = trunc
145  OUTPUTLINE += '], '
146 
147  else: # trouble
148  print "Run %d not found in Lumi/Pileup input file. Check your files!" % (run)
149 
150 
151 # print run
152 # print lslist
153 
154 # histFile = ROOT.TFile.Open (output, 'recreate')
155 # if not histFile:
156 # raise RuntimeError, \
157 # "Could not open '%s' as an output root file" % output
158 # pileupHist.Write()
159  #for hist in histList:
160  # hist.Write()
161 # histFile.Close()
162 # sys.exit()
163 
164  lastindex=len(OUTPUTLINE)-2
165  trunc = OUTPUTLINE[0:lastindex]
166  OUTPUTLINE = trunc
167  OUTPUTLINE += ' }'
168 
169  outputfile = open(options.outputfile,'w')
170  if not outputfile:
171  raise RuntimeError("Could not open '%s' as an output JSON file" % output)
172 
173  outputfile.write(OUTPUTLINE)
174  outputfile.close()
175 
176 
177 
178  sys.exit()