forked from CMS-LUMI-POG/VdMFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeBeamCurrentFile.py
268 lines (198 loc) · 8.69 KB
/
makeBeamCurrentFile.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import ROOT
def mkdata(dipfile, runNumber):
import os
vdmdir = os.getenv("VDMPATH")
ROOT.gROOT.LoadMacro(vdmdir+'/dict/DIP_dict.C+')
points = [] # to collect per-scanpoint times and bunch intensities
# get TTree with recorded HF data
f = ROOT.TFile(dipfile)
tree = f.Get('VdMDIPCombined')
# loop over tree entries and read all lumi sections
for i in range(tree.GetEntriesFast()):
if tree.GetEntry(i) <= 0:
raise Exception('TTree::GetEntry() failed')
# a shorthand notation
dip = tree.VdMDIPCombined
if dip.runNumber != runNumber:
print('WARN: dip.runNumber != runNumber, entry skipped')
continue
# take stable beams
beamMode = dip.beamMode.strip('\x00') # remove zero characters at the end
if beamMode != 'STABLE BEAMS':
print('WARN: dip.beamMode = "{0}", entry skipped'.format(beamMode))
continue
# take lumi sections with recorded data
if dip.VdMScan.RecordDataFlag == 0:
print('WARN: dip.VdMScan.RecordDataFlag = 0, entry skipped')
continue
beam_info1 = ROOT.BEAM_INFO()
beam_info2 = ROOT.BEAM_INFO()
ROOT.getBeam(dip, beam_info1, 0)
ROOT.getBeam(dip, beam_info2, 1)
beam1 = list(beam_info1.averageBunchIntensities)
beam2 = list(beam_info2.averageBunchIntensities)
dcct1 = beam_info1.averageBeamIntensity
dcct2 = beam_info2.averageBeamIntensity
# verify that FBCT data was written indeed
if sum(beam1) < 1e9 or sum(beam2) < 1e9:
print('WARN: sum of averageBunchIntensities < 1e9, entry skipped')
continue
point = {}
point.setdefault('times', []).append(dip.timestamp)
point.setdefault('beam1', []).append(beam1)
point.setdefault('beam2', []).append(beam2)
point.setdefault('dcct1', []).append(dcct1)
point.setdefault('dcct2', []).append(dcct2)
points.append(point)
return points
def checkFBCTcalib(table, CalibrateFBCTtoDCCT):
h_ratioB1 = ROOT.TGraph()
h_ratioB1.SetMarkerStyle(8)
h_ratioB1.SetMarkerSize(0.4)
h_ratioB1.SetTitle("SumFBCT/DCCT for B1, for scan "+str(table[0][1]))
h_ratioB1.GetXaxis().SetTitle("Scan point number")
h_ratioB1.GetYaxis().SetTitle("SumFBCT(active bunches)/DCCT")
h_ratioB2 = ROOT.TGraph()
h_ratioB2.SetMarkerStyle(8)
h_ratioB2.SetMarkerSize(0.4)
h_ratioB2.SetTitle("SumFBCT/DCCT for B2, for scan "+str(table[0][1]))
h_ratioB2.GetXaxis().SetTitle("Scan point number")
h_ratioB2.GetYaxis().SetTitle("SumFBCT(active bunches)/DCCT")
for idx, entry in enumerate(table):
h_ratioB1.SetPoint(idx, entry[2], entry[5]/entry[3])
h_ratioB2.SetPoint(idx, entry[2], entry[6]/entry[4])
h_ratioB1.Fit("pol0")
h_ratioB2.Fit("pol0")
fB1 = ROOT.TF1()
fB2 = ROOT.TF1()
fB1 = h_ratioB1.GetFunction("pol0")
fB2 = h_ratioB2.GetFunction("pol0")
corrB1 = fB1.GetParameter(0)
corrB2 = fB2.GetParameter(0)
if CalibrateFBCTtoDCCT == True:
print "Applying FBCT to DCCT calibration"
for idx, entry in enumerate(table):
old1 = entry[7]
# entry[7] = entry[5]/entry[3]*old1
entry[7] = corrB1*old1
old2 = entry[8]
# entry[8] = entry[6]/entry[4]*old2
entry[8] = corrB2*old1
return [h_ratioB1, h_ratioB2]
def doMakeBeamCurrentFile(ConfigInfo):
import csv, pickle
AnalysisDir = str(ConfigInfo['AnalysisDir'])
InputDIPFile = str(ConfigInfo['InputDIPFile'])
InputScanFile = './' + AnalysisDir + '/' + str(ConfigInfo['InputScanFile'])
OutputSubDir = str(ConfigInfo['OutputSubDir'])
outpath = './' + AnalysisDir + '/' + OutputSubDir
CalibrateFBCTtoDCCT = False
CalibrateFBCTtoDCCT = str(ConfigInfo['CalibrateFBCTtoDCCT'])
with open(InputScanFile, 'rb') as f:
scanInfo = pickle.load(f)
points = mkdata(InputDIPFile, int(scanInfo["Run"]))
Fill = scanInfo["Fill"]
ScanNames = scanInfo["ScanNames"]
dcct1 = [[] for i in range(len(ScanNames))]
dcct2 = [[] for i in range(len(ScanNames))]
# Associate individual measurements (points) per small LS with one scanpoint;
# since small LS is about 1.5 s and scanpoint is typically measured over 30s, there should be about 20 points per scanpoint
for point in points:
for i in range(len(ScanNames)):
key = "Scan_" + str(i+1)
scanpoints = scanInfo[key]
if len(scanpoints) == 0:
print "Problem with scan point info, appears to be empty, please check contents of ", InputScanFile
import sys
sys.exit(1)
for j in range(len(scanpoints)):
if scanpoints[j][3] <= point['times'][0] <= scanpoints[j][4]:
scanpoints[j].append(point)
# Now determine current averages
maskB1 = [0.0 for i in range(3564)]
for entry in scanInfo['FilledBunchesB1']:
maskB1[int(entry)-1] = 1.0
maskB2 = [0.0 for i in range(3564)]
for entry in scanInfo['FilledBunchesB2']:
maskB2[int(entry)-1] = 1.0
table = {}
for i in range(len(ScanNames)):
key = "Scan_" + str(i+1)
scanpoints = scanInfo[key]
table["Scan_" + str(i+1)]=[]
for j in range(len(scanpoints)):
sumdcct1 = 0.0
sumdcct2 = 0.0
fbctB1 = [0.0 for k in range(3564)]
fbctB2 = [0.0 for k in range(3564)]
numAssociatedPoints = len(scanpoints[j][6:])
for k in range(6,len(scanpoints[j])):
sumdcct1 += (scanpoints[j][k]['dcct1'][0])
sumdcct2 += (scanpoints[j][k]['dcct2'][0])
# zero fbct values in nominally empty bunches (noise)
fbct1_helper = scanpoints[j][k]['beam1'][0]
fbct1_helper = [a*b for a,b in zip(fbct1_helper,maskB1)]
fbct2_helper = scanpoints[j][k]['beam2'][0]
fbct2_helper = [a*b for a,b in zip(fbct2_helper,maskB2)]
# now do sums
fbctB1 = [a+b for a,b in zip(fbctB1, fbct1_helper)]
fbctB2 = [a+b for a,b in zip(fbctB2, fbct2_helper)]
# average
avrgdcct1 = sumdcct1/numAssociatedPoints
avrgdcct2 = sumdcct2/numAssociatedPoints
avrgfbctB1 = [a/numAssociatedPoints for a in fbctB1]
avrgfbctB2 = [a/numAssociatedPoints for a in fbctB2]
# now rearrange fbct lists into dictionnary with bx as key
avrgfbct1={}
avrgfbct2={}
for entry in scanInfo['FilledBunchesB1']:
avrgfbct1[str(entry)]=avrgfbctB1[entry-1]
for entry in scanInfo['FilledBunchesB2']:
avrgfbct2[str(entry)]=avrgfbctB2[entry-1]
row = [i+1, str(ScanNames[i]), j+1, avrgdcct1, avrgdcct2, sum(avrgfbctB1), sum(avrgfbctB2), avrgfbct1, avrgfbct2]
table["Scan_" + str(i+1)].append(row)
canvas = ROOT.TCanvas()
ROOT.gStyle.SetOptFit(111)
ROOT.gStyle.SetOptStat(0)
h_ratioB1 = ROOT.TGraph()
h_ratioB2 = ROOT.TGraph()
outpdf = outpath+'/checkFBCTcalib_'+str(Fill)+'.pdf'
for i in range(len(ScanNames)):
key = "Scan_" + str(i+1)
[h_ratioB1, h_ratioB2] = checkFBCTcalib(table[key], CalibrateFBCTtoDCCT)
h_ratioB1.Draw("AP")
canvas.SaveAs(outpdf + '(')
h_ratioB2.Draw("AP")
canvas.SaveAs(outpdf + '(')
canvas.SaveAs(outpdf + ']')
csvtable = []
csvtable.append(["ScanNumber, ScanNames, ScanPointNumber, avrgdcct1, avrgdcct2, sum(avrgfbctB1), sum(avrgfbctB2), fbct1 per Bx, fbct2 per BX"])
for i in range(len(ScanNames)):
key = "Scan_" + str(i+1)
csvtable.append([str(key)] )
for j in range(len(scanpoints)):
row = table[str(key)][j]
csvtable.append(row)
return table, csvtable
if __name__ == '__main__':
import pickle, csv, sys, json
ConfigFile = sys.argv[1]
Config=open(ConfigFile)
ConfigInfo = json.load(Config)
Config.close()
AnalysisDir = str(ConfigInfo["AnalysisDir"])
OutputSubDir = str(ConfigInfo["OutputSubDir"])
outpath = './' + AnalysisDir + '/' + OutputSubDir
InputScanFile = './' + AnalysisDir + '/' + str(ConfigInfo['InputScanFile'])
with open(InputScanFile, 'rb') as f:
scanInfo = pickle.load(f)
Fill = scanInfo["Fill"]
table = {}
csvtable = []
table, csvtable = doMakeBeamCurrentFile(ConfigInfo)
csvfile = open(outpath+'/BeamCurrents_'+str(Fill)+'.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerows(csvtable)
csvfile.close()
with open(outpath+'/BeamCurrents_'+str(Fill)+'.pkl', 'wb') as f:
pickle.dump(table, f)