-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path004_plot_bunch_lengths.py
199 lines (161 loc) · 7.48 KB
/
004_plot_bunch_lengths.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
import os, sys, time, string
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mlc
import matplotlib.dates as mdt
import LHCMeasurementTools.mystyle as ms
import LHCMeasurementTools.TimberManager as tm
import LHCMeasurementTools.TimestampHelpers as th
import LHCMeasurementTools.LHC_BCT as BCT
import LHCMeasurementTools.LHC_BQM as BQM
from LHCMeasurementTools.LHC_Fill_LDB_Query import load_fill_dict_from_json
# merge pickles and add info on location
from data_folders import data_folder_list
# merge jsons and add info on location
dict_fill_bmodes={}
for df in data_folder_list:
this_dict_fill_bmodes = load_fill_dict_from_json(
df+'/fills_and_bmodes.json')
for kk in this_dict_fill_bmodes:
this_dict_fill_bmodes[kk]['data_folder'] = df
dict_fill_bmodes.update(this_dict_fill_bmodes)
if len(sys.argv)>1:
print('--> Processing fill {:s}'.format(sys.argv[1]))
filln = int(sys.argv[1])
t_ref = dict_fill_bmodes[filln]['t_startfill']
t_end = dict_fill_bmodes[filln]['t_endfill']
tref_string = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(t_ref))
traces_times = np.linspace(0.1, (t_end-t_ref)/3600., 20)
i_bun_obs_list = np.arange(0,500)[::2]
# t_loss_ref = 1.97
N_traces_set = None
if len(sys.argv)>1:
if np.any([('--n_traces'in s) for s in sys.argv]):
i_arg = np.where([('--n_traces'in s) for s in sys.argv])[0]
arg_temp = sys.argv[i_arg]
N_traces_set = float(arg_temp.split('=')[-1])
traces_times = np.linspace(0.1, (t_end-t_ref)/3600., N_traces_set)
if '--injection' in sys.argv:
print('Scans in the INJPHYS-PRERAMP beam modes')
t_start_INJPHYS = dict_fill_bmodes[filln]['t_start_INJPHYS']
t_start_RAMP = dict_fill_bmodes[filln]['t_start_RAMP']
if N_traces_set==None: N_traces_set=30
traces_times = np.linspace((t_start_INJPHYS-t_ref)/3600., (t_start_RAMP-t_ref)/3600., N_traces_set)
if '--highenergy' in sys.argv:
print('Scans in the FLATTOP-STABLE beam modes')
t_start_FLATTOP = dict_fill_bmodes[filln]['t_start_FLATTOP']
t_start_STABLE = dict_fill_bmodes[filln]['t_start_STABLE']
if N_traces_set==None: N_traces_set=30
traces_times = np.linspace((t_start_FLATTOP-t_ref)/3600., (t_start_STABLE-t_ref)/3600.+0.5, N_traces_set)
if '--stablebeams' in sys.argv:
print('Scans in the STABLE BEAMS')
t_start_STABLE = dict_fill_bmodes[filln]['t_start_STABLE']
t_end_STABLE = dict_fill_bmodes[filln]['t_stop_STABLE']
if N_traces_set==None: N_traces_set=30
traces_times = np.linspace((t_start_STABLE-t_ref)/3600., (t_end_STABLE-t_ref)/3600.+0.5, N_traces_set)
if '--ramp' in sys.argv:
print('Scans in the RAMP')
t_start_RAMP= dict_fill_bmodes[filln]['t_start_RAMP']
t_end_RAMP = dict_fill_bmodes[filln]['t_stop_RAMP']
if N_traces_set==None: N_traces_set=10
traces_times = np.linspace((t_start_RAMP-t_ref)/3600., (t_end_RAMP-t_ref)/3600, N_traces_set)
if np.any([('--interval'in s) for s in sys.argv]):
i_arg = np.where([('--interval'in s) for s in sys.argv])[0]
arg_temp = sys.argv[i_arg]
t_start_man = float(arg_temp.split('=')[-1].split(',')[0])
t_end_man = float(arg_temp.split('=')[-1].split(',')[1])
print('Interval manually set: %.2fh to %.2fh'%(t_start_man, t_end_man))
if N_traces_set==None: N_traces_set=30
traces_times = np.linspace(t_start_man, t_end_man, N_traces_set)
if '--notrace' in sys.argv:
traces_times = []
plt.rcParams.update({'axes.labelsize': 18,
'axes.linewidth': 2,
'xtick.labelsize': 'large',
'ytick.labelsize': 'large',
'xtick.major.pad': 14,
'ytick.major.pad': 14})
format_datetime = mdt.DateFormatter('%m-%d %H:%M')
# get location of current data
data_folder_fill = dict_fill_bmodes[filln]['data_folder']
fill_dict = {}
if os.path.isdir(data_folder_fill+'/fill_basic_data_csvs'):
fill_dict.update(tm.parse_timber_file(data_folder_fill
+'/fill_basic_data_csvs/basic_data_fill_%d.csv'%filln,
verbose=False))
fill_dict.update(tm.parse_timber_file(data_folder_fill
+'/fill_bunchbybunch_data_csvs/bunchbybunch_data_fill_%d.csv'%filln,
verbose=False))
elif os.path.isdir(data_folder_fill+'/fill_basic_data_h5s'):
fill_dict.update(tm.CalsVariables_from_h5(data_folder_fill
+'/fill_basic_data_h5s/basic_data_fill_%d.h5'%filln,
))
fill_dict.update(tm.CalsVariables_from_h5(data_folder_fill
+'/fill_bunchbybunch_data_h5s/bunchbybunch_data_fill_%d.h5'%filln,
))
n_traces = len(traces_times)
blen_thresh = 0.
ax_t = None
i_fig = 0
plt.close('all')
ms.mystyle_arial(fontsz=16, dist_tick_lab=5)
beam_col = ['b','r']
fig_list = []
axbl = None
for beam in [1,2]:
blength = BQM.blength(fill_dict, beam = beam)
bct = BCT.BCT(fill_dict, beam=beam)
fig1 = plt.figure(i_fig, figsize=(14, 8))
fig_list.append(fig1)
fig1.patch.set_facecolor('w')
ax0 = plt.subplot(211, sharex=ax_t)
ax_t = ax0
ax1 = plt.subplot(212, sharex=axbl, sharey=axbl)
axbl = ax1
ax0.plot((bct.t_stamps-t_ref)/3600., bct.values, color=beam_col[beam-1], lw=2)
for i in range(0, n_traces):
t_cut_h = traces_times[i]
t_curr = t_ref+t_cut_h*3600.
blength_curr, t_blength_curr = blength.nearest_older_sample(t_curr, flag_return_time=True)
ax1.plot(blength_curr, '.', color=ms.colorprog(i, n_traces),
label='%.2f h'%((t_blength_curr-t_ref)/3600.))
ax0.axvline((t_blength_curr-t_ref)/3600., lw=1.5, color=ms.colorprog(i, n_traces))
ax1.set_xlabel('25 ns slot')
ax1.set_xlim(0, 3500)
ax1.set_ylabel('Bunch length [s]')
ax1.grid('on')
ax0.set_xlabel('Time [h]')
ax0.grid('on')
ax0.set_ylim(bottom=.5e-9)
plt.subplots_adjust(top=0.9, bottom=0.1, right=0.95,
left=0.1, hspace=0.3, wspace=0.4)
fig1.suptitle('Fill %d: B%d, started on %s'%(filln, beam, tref_string), fontsize=20)
i_fig += 1
#find the filled slots
mask_bunches = np.ma.masked_greater(blength.blen, blen_thresh).mask
list_nbunches = np.sum(mask_bunches, axis=0)
i_bunches = np.where(list_nbunches > 0)[0]
N_bunches = len(i_bunches)
figbbb = plt.figure(100+i_fig, figsize=(14, 8))
figbbb.patch.set_facecolor('w')
axbbb_bct = plt.subplot(211, sharex=ax_t)
axbbb_traces = plt.subplot(212, sharex=axbbb_bct)
#figlosref = plt.figure(200+i_fig, figsize=(14, 8), tight_layout=False)
#figlosref.patch.set_facecolor('w')
#axlref_bct = plt.subplot(211)
#axlref_traces = plt.subplot(212, sharex=axbbb_bct)
i_bunches_sel = list(set(i_bunches) & set(i_bun_obs_list))
#totint_at_loss_ref = bct.nearest_older_sample(t_ref+t_loss_ref*3600)
axbbb_bct.plot((bct.t_stamps-t_ref)/3600., bct.values, color=beam_col[beam-1], lw=2)
for i_line, i_bun in enumerate(i_bunches_sel):
axbbb_traces.plot((blength.t_stamps-t_ref)/3600., blength.blen[:, i_bun],
color=ms.colorprog(i_line, len(i_bunches_sel)))
axbbb_bct.grid('on')
axbbb_traces.grid('on')
axbbb_bct.set_ylabel('Beam intensity [p]')
ax0.set_ylabel('Beam intensity [p]')
axbbb_traces.set_ylabel('Bunch length [s]')
axbbb_traces.set_xlabel('Time [h]')
axbbb_traces.set_ylim(.8e-9, None)
ax1.set_ylim(.8e-9, None)
plt.show()