forked from CMS-LUMI-POG/VdMFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeGraphsFile.py
227 lines (164 loc) · 6.58 KB
/
makeGraphsFile.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
import CorrectionManager
import BeamBeam_Corr
import LengthScale_Corr
import Ghosts_Corr
import Satellites_Corr
import ROOT as r
import sys
import json
from inputDataReader import *
from vdmUtilities import showAvailableCorrs
def doMakeGraphsFile(ConfigInfo):
AnalysisDir = str(ConfigInfo['AnalysisDir'])
Luminometer = str(ConfigInfo['Luminometer'])
inputScanFile = str(ConfigInfo['InputScanFile'])
inputBeamCurrentFile = str(ConfigInfo['InputBeamCurrentFile'])
inputLuminometerData = str(ConfigInfo['InputLuminometerData'])
corrName = ConfigInfo['Corr']
# For scan 1, which is always there as long as there are any scans at all:
inData1 = vdmInputData(1)
inData1.GetScanInfo(AnalysisDir + '/'+ inputScanFile)
#inData1.PrintScanInfo()
#print inData1.fill
#print inData1.date
#print inData1.run
#print inData1.inputDIPFile
#print inData1.scanName
#print inData1.scanNamesAll
#print inData1.scanTimeWindows
#print inData1.betaStar
#print inData1.angle
#print inData1.particleTypeB1
#print inData1.particleTypeB2
#print inData1.filledBunchesB1
#print inData1.filledBunchesB2
#print inData1.collidingBunches
#print inData1.scanNumber
# print inData1.sp
#print inData1.tStart
#print inData1.tStop
#print inData1.displacement
inData1.GetBeamCurrentsInfo(AnalysisDir + '/' + inputBeamCurrentFile)
#inData1.PrintBeamCurrentsInfo()
#print inData1.curr
#print inData1.avrgDcctB1
#print inData1.avrgDcctB2
#print inData1.sumAvrgFbctB1
#print inData1.sumAvrgFbctB2
#print "len(inData1.avrgFbctB1)", len(inData1.avrgFbctB1)
#print inData1.avrgFbctB1
#print inData1.avrgFbctB2
#print "len(inData1.avrgFbctB1PerSP)", len(inData1.avrgFbctB1PerSP)
#print inData1.avrgFbctB1PerSP
#print inData1.avrgFbctB2PerSP
inData1.GetLuminometerData(AnalysisDir + '/' + inputLuminometerData)
#inData1.PrintLuminometerData()
#print inData1.lum
# print inData1.usedCollidingBunches
#for entry in inData1.lumiPerSP:
# print entry
#for entry in inData1.lumiErrPerSP:
# print entry
Fill = inData1.fill
inData = []
inData.append(inData1)
# For the remaining scans:
for i in range(1,len(inData1.scanNamesAll)):
inDataNext = vdmInputData(i+1)
inDataNext.GetScanInfo(AnalysisDir + '/' + inputScanFile)
inDataNext.GetBeamCurrentsInfo(AnalysisDir + '/' + inputBeamCurrentFile)
inDataNext.GetLuminometerData(AnalysisDir + '/' + inputLuminometerData)
inData.append(inDataNext)
# Apply corrections
# Note that calibrating SumFBCT to DCCT is done in makeBeamCurrentFile.py if calibration flag in config file is set to true
showAvailableCorrs()
availableCorr = CorrectionManager.get_plugins(CorrectionManager.CorrectionProvider)
print "The following corrections will be applied, in order: "
for i, entry in enumerate(corrName):
print "Corr #"+str(i+1) + ": " +entry
corrFull = ''
for entry in corrName:
print "Now applying correction: ", entry
# Check whether correction requested in config json actually exists
key = entry+'_Corr'
if key in availableCorr:
corrector= availableCorr[entry+'_Corr']()
else:
if not entry == "noCorr":
print "Correction " + entry + " requested via json file does not exist, ignore."
continue
# Read Corr config in here
corrValueFile = AnalysisDir + '/corr/'+ entry + '_' + Fill +'.pkl'
if entry == "BeamBeam":
corrValueFile = AnalysisDir + '/corr/'+ entry + '_' +Luminometer + '_' + Fill +'.pkl'
# for element in inData:
# print "before current correction B1 ", element.scanNumber
# for i, bx in enumerate(element.collidingBunches):
# print ">>>", i, element.avrgFbctB1[i]
corrector.doCorr(inData, corrValueFile)
# for element in inData:
# print "after current correction B1 ", element.scanNumber
# for i, bx in enumerate(element.collidingBunches):
# print ">>>", i, element.avrgFbctB1[i]
corrFull = corrFull + '_' + entry
# check if any corrections are to be applied at all, if yes, define corr description string accordingly
# if no use "noCorr"
import os
# empty strings are false, so if no corrections are to be applied, use noCorr as corr descriptor
if not corrFull:
corrFull = "_noCorr"
# Now fill graphs for all scans
# Counting of scans starts with 1
graphsListAll = {'Scan_'+ str(n+1):{} for n in range(len(inData))}
for entry in inData:
scanNumber = entry.scanNumber
print "Now at Scan number ", scanNumber
nBX = len(entry.collidingBunches)
prefix = ''
if 'X' in entry.scanName:
prefix = str(scanNumber) +'_X_'
if 'Y' in entry.scanName:
prefix = str(scanNumber)+'_Y_'
# convert for TGraph
from array import array
graphsList = {}
for i, bx in enumerate(entry.usedCollidingBunches):
coord = entry.spPerBX[bx]
coorde = [0.0 for a in coord]
coord = array("d",coord)
coorde = array("d", coorde)
currProduct = [ a*b/1e22 for a,b in zip(entry.avrgFbctB1[i],entry.avrgFbctB2[i])]
lumi = [a/b for a,b in zip(entry.lumi[i],currProduct)]
lumie = [a/b for a,b in zip(entry.lumiErr[i],currProduct)]
lumie = array("d",lumie)
lumi = array("d",lumi)
name = prefix +str(bx)
graph = r.TGraphErrors(len(coord),coord,lumi,coorde,lumie)
graph.SetName(name)
graph.SetTitle(name)
graphsList[bx] = graph
graphsListAll['Scan_'+ str(scanNumber)]=graphsList
return corrFull, graphsListAll
if __name__ == '__main__':
configFile = sys.argv[1]
config=open(configFile)
ConfigInfo = json.load(config)
config.close()
AnalysisDir = str(ConfigInfo['AnalysisDir'])
Luminometer = str(ConfigInfo['Luminometer'])
Fill = str(ConfigInfo['Fill'])
OutputSubDir = str(ConfigInfo['OutputSubDir'])
graphsListAll = {}
corrFull, graphsListAll = doMakeGraphsFile(ConfigInfo)
outputDir = AnalysisDir +'/' + Luminometer + '/' + OutputSubDir + '/'
outFileName = 'graphs_' + str(Fill) + corrFull
# save TGraphs in a ROOT file
rfile = r.TFile(outputDir + outFileName + '.root',"recreate")
for key in sorted(graphsListAll.iterkeys()):
graphsList = graphsListAll[key]
for key_bx in sorted(graphsList.iterkeys()):
graphsList[key_bx].Write()
rfile.Write()
rfile.Close()
with open(outputDir + outFileName + '.pkl', 'wb') as file:
pickle.dump(graphsListAll, file)