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