forked from CMS-LUMI-POG/VdMFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateCalibrationConstant.py
215 lines (152 loc) · 7.89 KB
/
calculateCalibrationConstant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import sys, json, csv, pickle
import luminometers
from vdmUtilities import makeCorrString
from fitResultReader import fitResultReader
# [in Hz]
LHC_revolution_frequency = 11245
import math
pi = math.pi
class XsecCalculationOptions:
class LuminometerOptions:
LuminometerTypes = ("HF", "PCC", "Vtx", "BCM1F", "PLT")
WhatIsMeasured = ("CountsPerTime","Counts")
NormalizationGraphs = ("None", "CurrentProduct")
OldNormalizationAvailable = ("Yes", "No")
class FormulaOptions:
FormulaToUse = ("1D-Gaussian-like", "2D-like", "numerical-Integration")
def xsecFormula_1DGaussianLike(CapSigmaX, CapSigmaY, peakX, peakY):
# units, want visible cross section in microbarn !
CapSigmaX[0] =CapSigmaX[0]*1000
CapSigmaX[1] =CapSigmaX[1]*1000
CapSigmaY[0] =CapSigmaY[0]*1000
CapSigmaY[1] =CapSigmaY[1]*1000
# with approximation peakX ~ peakY ~ 0.5(peakX+peakY)
xsec = pi * CapSigmaX[0] * CapSigmaY[0] * (peakX[0] + peakY[0])
xsecErr = ( CapSigmaX[1]*CapSigmaX[1]/CapSigmaX[0]/CapSigmaX[0] + \
CapSigmaY[1]*CapSigmaY[1]/CapSigmaY[0]/CapSigmaY[0] + \
(peakX[1]*peakX[1] + peakY[1]*peakY[1])/(peakX[0]+peakY[0])/(peakX[0]+peakY[0]))
xsecErr = math.sqrt(xsecErr) * xsec
return xsec, xsecErr
def xsecFormula_2DLike(fitResult):
return xsec
def xsecFormula_numericalIntegration(fitFunc):
return xsec
if __name__ == '__main__':
# check that options chosen in json do actually exist
# if non-standard luminometer chosen, check that all options provided are allowed, i.e. are in LuminometerOptions
# either use xsec as returned by function, for "Counts", or xsec/LHC_frequency, for "CountsPerTime"
configFile = sys.argv[1]
config=open(configFile)
ConfigInfo = json.load(config)
config.close()
Fill = ConfigInfo['Fill']
AnalysisDir = ConfigInfo['AnalysisDir']
Luminometer = ConfigInfo['Luminometer']
Corr = ConfigInfo['Corr']
InputFitResultsFile = ConfigInfo['InputFitResultsFile']
fit = InputFitResultsFile.strip("FitResults.pkl")
corrFull = makeCorrString(Corr)
InputFitResultsFile = AnalysisDir + "/" + Luminometer + "/results/" + corrFull + "/" + InputFitResultsFile
OutputDir = './' + AnalysisDir + '/' + Luminometer + '/results/' + corrFull + '/'
predefinedTypes = XsecCalculationOptions.LuminometerOptions.LuminometerTypes
from luminometers import *
oldNormAvailable = False
WhatIsMeasured = ConfigInfo['LuminometerSettings']['WhatIsMeasured']
NormalizationGraphs = ConfigInfo['LuminometerSettings']['NormalizationGraphs']
OldNormAvailable = ConfigInfo['LuminometerSettings']['OldNormAvailable']
if Luminometer in predefinedTypes:
defaults = LuminometerDefaults(Luminometer)
if WhatIsMeasured == "default":
WhatIsMeasured = defaults.WhatIsMeasured
if NormalizationGraphs== "default":
NormalizationGraphs = defaults.NormalizationGraphs
if OldNormAvailable == "default":
OldNormAvailable = defaults.OldNormAvailable
print "defaults ", WhatIsMeasured, NormalizationGraphs, OldNormAvailable
Total_inel_Xsec = ConfigInfo['Total_inel_Xsec']
if OldNormAvailable:
oldNormalization = ConfigInfo['OldNormalization']
if oldNormalization < 0:
print "Value of old normalization from json makes no sense, is negative, hence assume no old normalization available"
OldNormAvailable = False
# print "OldNormAvailable, oldNormalization", OldNormAvailable, oldNormalization
FormulaToUse = ConfigInfo['FormulaToUse']
Scanpairs = ConfigInfo['Scanpairs']
fitResult = fitResultReader(InputFitResultsFile)
CapSigmaDict = fitResult.getFitParam("CapSigma")
# CapSigmaDict = fitResult.getFitParam("sigma")
# print CapSigmaDict["Scan_1"]["1040"]
CapSigmaErrDict = fitResult.getFitParam("CapSigmaErr")
# CapSigmaErrDict = fitResult.getFitParam("sigmaErr")
# print CapSigmaErrDict["Scan_1"]["1040"]
peakDict = fitResult.getFitParam("peak")
# peakDict = fitResult.getFitParam("Amp")
peakErrDict = fitResult.getFitParam("peakErr")
# peakErrDict = fitResult.getFitParam("AmpErr")
fitstatusDict = fitResult.getFitParam("fitStatus")
chi2Dict = fitResult.getFitParam("chi2")
table =[]
csvtable = []
csvtable.append(["XscanNumber_YscanNumber","Type", "BCID", "xsec", "xsecErr", "normChange", "normChangeErr"] )
table.append(["XscanNumber_YscanNumber","Type", "BCID", "xsec", "xsecErr", "normChange", "normChangeErr"] )
for entry in Scanpairs:
XscanNumber = entry[0]
YscanNumber = entry[1]
from collections import defaultdict
xsec = defaultdict(float)
xsecErr = defaultdict(float)
xsecDict = defaultdict(dict)
xsecErrDict = defaultdict(dict)
XscanID = 'Scan_'+str(XscanNumber)
YscanID = 'Scan_'+str(YscanNumber)
XY_ID = 'Scan_'+str(XscanNumber) + '_'+str(YscanNumber)
# csvtable.append([XY_ID])
# print "CapSigmaDict[XscanID] for " + XscanID
# print CapSigmaDict[XscanID]
for bx in CapSigmaDict[XscanID]:
considerInMean = True
print "now at bx", bx
CapSigmaX = [CapSigmaDict[XscanID][bx], CapSigmaErrDict[XscanID][bx]]
CapSigmaY = [CapSigmaDict[YscanID][bx], CapSigmaErrDict[YscanID][bx]]
peakX = [peakDict[XscanID][bx], peakErrDict[XscanID][bx]]
peakY = [peakDict[YscanID][bx], peakErrDict[YscanID][bx]]
# need to replace with something that takes FormulaToUse as argument and applies selected formula
if FormulaToUse == "1D-Gaussian-like":
value, err = xsecFormula_1DGaussianLike(CapSigmaX, CapSigmaY, peakX, peakY)
if WhatIsMeasured == "CountsPerTime":
value = value/LHC_revolution_frequency
err = err/LHC_revolution_frequency
xsec[bx] = value
xsecErr[bx] = err
# print "bx, [CapSigmaX, CapSigmaXErr],[CapSigmaY, CapSigmaYErr], [peakX, peakXErr], [peakY, peakYErr], xsec, xsecErr"
# print ">>>>", bx, CapSigmaX, CapSigmaY, peakX, peakY, xsec[bx], xsecErr[bx]
if fitstatusDict[XscanID][bx] >0:
print "fitstatus Xscan for bx", bx, fitstatusDict[XscanID][bx]
if fitstatusDict[YscanID][bx] >0:
print "fitstatus Yscan for bx", bx, fitstatusDict[YscanID][bx]
## FIXME I hacked it
###if chi2Dict[XscanID][bx] >100:
### print "chi2 Xscan for bx", bx, chi2Dict[XscanID][bx]
### considerInMean = False
###if chi2Dict[YscanID][bx] >100:
### print "chi2 Yscan for bx", bx, chi2Dict[YscanID][bx]
### considerInMean = False
# printall(bx, CapSigmaX, CapSigmaY, peakX, peakY, xsec[bx], xsecErr[bx])
normChange = -999.
normChangeErr = -999.
if OldNormAvailable:
normChange = LHC_revolution_frequency/xsec[bx] * 1/oldNormalization
normChangeErr = normChange*xsecErr[bx]/xsec[bx]
row = [str(XscanNumber)+"_"+str(YscanNumber), "XY", bx, xsec[bx], xsecErr[bx], normChange, normChangeErr]
if considerInMean:
table.append(row)
csvtable.append(row)
else:
print "bcid ", bx, " excluded because chi2 value too high: ", chi2Dict[XscanID][bx], chi2Dict[YscanID][bx]
# need to name output file such that fit function name in file name
csvfile = open(OutputDir+'/LumiCalibration_'+ Luminometer+ '_'+ fit + str(Fill)+'.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerows(csvtable)
csvfile.close()
with open(OutputDir+'/LumiCalibration_'+ Luminometer+ '_'+ fit + str(Fill)+'.pkl', 'wb') as f:
pickle.dump(table, f)