11import os
2+ from IPython .core .debugger import Tracer ;
23
34try :
45 import numpy as np
@@ -126,12 +127,12 @@ def _get_timber_data(beam,t1,t2,db=None):
126127 try :
127128 if len (data [db .search (nm + '1%NB_GATES%' )[0 ]][1 ]) != 0 :
128129 wire += '1'
130+ except (KeyError ,IndexError ): pass
131+ try :
129132 if len (data [db .search (nm + '2%NB_GATES%' )[0 ]][1 ]) != 0 :
130133 wire += '2'
131- except KeyError :
132- pass
133- if wire == '1' or wire == '2' :
134- pass
134+ except (KeyError ,IndexError ): pass
135+ if wire == '1' or wire == '2' : pass
135136 elif wire == '' :
136137 raise ValueError ("No data found for wire 1 or wire 2 as " +
137138 "db.search('%s') is empty!" % (name + "%NB_GATES%" ))
@@ -145,7 +146,7 @@ def _get_timber_data(beam,t1,t2,db=None):
145146 "and db.search('%s') = %s!" % (name + "%NB_GATES%" ,
146147 db .search (name + '%NB_GATES%' )))
147148 # extract variable names for wires from database
148- for var in ['NB_GATES' ,'BUNCH_SELECTION' ,'PROF_POSITION_' ,
149+ for var in ['NB_GATES' ,'GAIN' , ' BUNCH_SELECTION' ,'PROF_POSITION_' ,
149150 'PROF_DATA_' ]:
150151 nm = name + plane .upper ()+ wire
151152 var_names .extend (db .search (nm + '%' + var + '%' ))
@@ -159,7 +160,7 @@ def _get_timber_data(beam,t1,t2,db=None):
159160 if var not in var_check [beam .upper ()]:
160161 print ('WARNING: variable name %s changed!' % var )
161162 flag_check = False
162- if flag_check == False :
163+ if flag_check is False :
163164 print ('Hardcoded variable names are: %s' % var_check )
164165 # get data
165166 data = db .get (var_names ,t1 ,t2 )
@@ -206,21 +207,23 @@ def _timber_to_dict(beam,plane,direction,data,db):
206207 where
207208 """
208209 keys_timber = ['NB_GATES' ,'BUNCH_SELECTION' ,'BETA' ,'EMITTANCE_NORM' ,
209- 'PROF_POSITION' ,'PROF_DATA' ]
210+ 'PROF_POSITION' ,'PROF_DATA' , 'GAIN' ]
210211 keys_dic = ['gate' ,'bunch' ,'beta' ,'emit' ,
211- 'pos' ,'amp' ]
212+ 'pos' ,'amp' , 'gain' ]
212213 # dictionary of time,value
213214 tt ,vv = {},{}
214215 name = '%LHC%BWS%' + beam .upper ()+ plane .upper () # make sure to have upper letters
215216 # check which wire is used by checking the gates
216- if db .search (name + '1%NB_GATES%' )[0 ] in data .keys ():
217- wire = '1'
218- elif db .search (name + '2%NB_GATES%' )[0 ] in data .keys ():
219- wire = '2'
217+ try :
218+ if db .search (name + '1%NB_GATES%' )[0 ] in data .keys (): wire = '1'
219+ except IndexError : pass
220+ try :
221+ if db .search (name + '2%NB_GATES%' )[0 ] in data .keys (): wire = '2'
222+ except IndexError : pass
220223 for kt ,kd in zip (keys_timber ,keys_dic ):
221224 # db.search() gives correctly back which wire is used. Use it to
222225 # assign the data to tt,vv etc.
223- if kd in ['gate' ,'bunch' ]:
226+ if kd in ['gate' ,'bunch' , 'gain' ]:
224227 var_str = name + wire + '%' + kt + '%'
225228 elif kd in ['beta' ,'emit' ]:
226229 var_str = name + '%' + direction + '%' + kt + '%'
@@ -232,19 +235,22 @@ def _timber_to_dict(beam,plane,direction,data,db):
232235 raise ValueError ("Only one variable name should be returned " +
233236 "here!\n db.search('" + var_str + "')=%s" % (var_name ))
234237 tt [kd ],vv [kd ] = data [var_name [0 ]]
238+ # convert binary format to float values
235239 if kd == 'bunch' :
236240 vv [kd ] = np .array ([extract_bunch_selection (vv [kd ][i ])
237241 for i in xrange (len (vv [kd ]))])
238242 dbws = {}
239243 for t in tt ['pos' ]:
240244 pos = vv ['pos' ][tt ['pos' ] == t ][0 ] # position
241245 ngate = vv ['gate' ][tt ['gate' ] == t ]
246+ gain = vv ['gain' ][tt ['gain' ] == t ]
242247 amp = (vv ['amp' ][tt ['amp' ] == t ][0 ]).reshape (int (ngate ),len (pos ))
243248 slots = vv ['bunch' ][tt ['bunch' ] == t ].flatten ()
244249 # beta and eps time stamps are different but have the same ordering
245250 tbe = tt ['beta' ][tt ['pos' ] == t ]
246251 beta = vv ['beta' ][tt ['beta' ] == tbe ]
247252 emit = vv ['emit' ][tt ['emit' ] == tbe ].flatten ()
253+ # print 'MF',pos,ngate,gain,amp,slots,tbe,beta,emit
248254 # trouble with getting the energy
249255 igev = np .where (t - data ['LHC.BOFSU:OFC_ENERGY' ][0 ]>= 0. )[0 ][- 1 ]
250256 egev = data ['LHC.BOFSU:OFC_ENERGY' ][1 ][igev ]
@@ -259,18 +265,27 @@ def _timber_to_dict(beam,plane,direction,data,db):
259265 amp [idx ] = amp [idx ]- np .min (amp [idx ])
260266 dx = np .abs (pos [1 :]- pos [0 :- 1 ])
261267 int_dist = (dx * amp [idx ][:- 1 ]).sum ()
262- amp_norm = amp [idx ]/ int_dist
263- p ,pcov = curve_fit (f = tb .gauss_pdf ,xdata = pos ,ydata = amp_norm ,p0 = [0 ,1 ,0 ,1000 ])
264- sigma_gauss = p [3 ]
265- sigma_gauss_err = np .sqrt (pcov [3 ,3 ])
266- emit_gauss = tb .emitnorm (sigma_gauss ** 2 / beta ,egev )* 1.e-6
267- emit_gauss_err = tb .emitnorm (2 * sigma_gauss * sigma_gauss_err /
268- beta ,egev )* 1.e-6
269- dbws [sl ].append ((t ,tbe ,egev ,pos ,amp [idx ],amp_norm ,beta ,emit [idx ],
268+ # case where amplitude =0
269+ if int_dist == 0 :
270+ amp_norm = amp [idx ]
271+ sigma_gauss ,sigma_gauss_err ,emit_gauss ,emit_gauss_err = 0 ,0 ,0 ,0
272+ p = np .zeros (4 )
273+ pcov = np .zeros ((4 ,4 ))
274+ else :
275+ amp_norm = amp [idx ]/ int_dist
276+ p ,pcov = curve_fit (f = tb .gauss_pdf ,xdata = pos ,ydata = amp_norm ,
277+ p0 = [0 ,1 ,0 ,1000 ])
278+ sigma_gauss = p [3 ]
279+ sigma_gauss_err = np .sqrt (pcov [3 ,3 ])
280+ emit_gauss = tb .emitnorm (sigma_gauss ** 2 / beta ,egev )* 1.e-6
281+ emit_gauss_err = tb .emitnorm (2 * sigma_gauss * sigma_gauss_err /
282+ beta ,egev )* 1.e-6
283+ Tracer ()()
284+ dbws [sl ].append ((t ,tbe ,gain ,egev ,pos ,amp [idx ],amp_norm ,beta ,emit [idx ],
270285 emit_gauss ,emit_gauss_err ,p ,pcov ))
271286 for k in dbws .keys ():
272287 dbws [k ]= np .array (dbws [k ],dtype = [('time' ,float ),
273- ('time_app' ,float ),('egev' ,float ),('pos' ,np .ndarray ),
288+ ('time_app' ,float ),('gain' , float ),( ' egev' ,float ),('pos' ,np .ndarray ),
274289 ('amp' ,np .ndarray ),('amp_norm' ,np .ndarray ),
275290 ('beta' ,float ),('emit' ,float ),
276291 ('emit_gauss' ,float ),('emit_gauss_err' ,float ),
0 commit comments