CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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 ##############################
27 ## ######################## ##
28 ## ## ################## ## ##
29 ## ## ## Main Program ## ## ##
30 ## ## ################## ## ##
31 ## ######################## ##
32 ##############################
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 HLT paths. 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  parser.add_option('--runperiod',dest='runperiod',action='store', default='Run1',help='select runperiod Run1 or Run2, default Run1' )
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  #print options.runperiod
80  #inpf = open (options.inputfile, 'r')
81  #inputfilecontent = inpf.read()
82 
83  inputRange = csvLumibyLSParser.csvLumibyLSParser (options.inputfile,options.runperiod).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  for (run, lslist) in sorted (inputRange.items()):
99  # now, look for matching run, then match lumi sections
100  #print "searching for run %d" % (run)
101  if run in inputPileupRange.keys():
102  OUTPUTLINE+= ('"%d":' % run )
103  OUTPUTLINE+= ' ['
104 
105  LSPUlist = inputPileupRange[run]
106  #print "LSPUlist", LSPUlist
107  for LSnumber in lslist:
108  if LSnumber in LSPUlist.keys():
109  PUlumiInfo = LSPUlist[LSnumber]
110  HLTlumiInfo = lslist[LSnumber]
111  #print "found LS %d" % (LSnumber)
112  #print HLTlumiInfo
113  scale = 0
114  if PUlumiInfo[0] > 0.:
115  scale=HLTlumiInfo[1]/PUlumiInfo[0] # rescale to HLT recorded Lumi
116 
117  if scale > 1.001:
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.01 # 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()
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47