You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# TODO: check how many wires are saving and save appropriately.
importnumpyasnpfromosimportpath, makedirsimporterrnoimportosfrompyemittance.emittance_calc_baseimportEmitCalcBasefrompyemittance.opticsimportestimate_sigma_mat_multiwire, normalize_emitfrompyemittance.machine_settingsimportget_rmat_wires, get_loc_wiresfrompyemittance.saving_ioimportsave_emit_runclassMultiWireCalc(EmitCalcBase):
""" Multiwire emittance measurement type """definit_class_attr(self):
# here self.meas_vals attribute is constant for x and yself.wire_rmat=get_rmat_wires(self.config_dict['beamline_info'])
self.wire_loc=get_loc_wires(self.config_dict['beamline_info'])
self.meas_vals=self.wire_rmat# Initialize paths and dirs for savingself.init_saving()
defdefine_default_config(self):
print("No configuration specified. Taking default LCLS-WS02 configs.")
self.config_name="LCLS_WS02"self.config_dict=self.load_config()
defget_emit(self):
""" Get emittance at the FIRST wire from beamsizes and wire locations :return: normalized emittance and error """fordiminself.dims:
# run emit calc for x and ybs=self.beam_vals[dim]
bs_err=self.beam_vals_err[dim]
weights=self.weighting_func(bs, bs_err) # 1/sigma# Storing quadvals and beamsizes in self.out_dict for plotting purposesself.out_dict[f'locations'] =list(self.wire_loc.values())
self.out_dict[f'beamsizes{dim}'] =list(bs)
self.out_dict[f'beamsizeserr{dim}'] =list(bs_err)
res=estimate_sigma_mat_multiwire(self.wire_rmat, bs, bs_err, weights, self.out_dict[f'locations'],
dim=dim, plot=self.plot, verbose=self.verbose)
# res is [emit, emit_err, beta_err/beta, alpha_err/alpha, s11, s12, s22]ifnp.isnan(res[0]):
self.out_dict['nemitx'], self.out_dict['nemity'] =np.nan, np.nanself.out_dict['nemitx_err'], self.out_dict['nemity_err'] =np.nan, np.nanself.out_dict['bmagx'], self.out_dict['bmagy'] =np.nan, np.nanself.out_dict['bmagx_err'], self.out_dict['bmagy_err'] =np.nan, np.nanreturnself.out_dictelse:
emit, emit_err, beta_rel_err, alpha_rel_err=res[0:4]
ifself.calc_bmag:
sig_11, sig_12, sig_22=res[4:]
norm_emit_res=normalize_emit(emit, emit_err, self.energy)
self.out_dict[f'nemit{dim}'] =norm_emit_res[0]
self.out_dict[f'nemit{dim}_err'] =norm_emit_res[1]
ifself.calc_bmag:
# bmag is calculated at the first wireself.sig_mat_meas[dim] = [sig_11, sig_12, sig_22]
self.beta_err=beta_rel_errself.alpha_err=alpha_rel_errbmag_calc_res=self.get_twiss_bmag(dim=dim)
# Get bmag and bmag_errself.out_dict[f'bmag{dim}'] =bmag_calc_res[0]
self.out_dict[f'bmag{dim}_err'] =bmag_calc_res[1]
# get geometric mean (set to None when x and y are not calculated)self.get_gmean_emit()
ifself.save_runs:
self.save_run()
returnself.out_dictdefsave_run(self):
save_emit_run(self.out_dict, path=self.config_dict['savepaths']['fits'])
definit_saving(self):
"""Initialize dirs and files for saving"""savepaths=self.config_dict['savepaths']
defmkdir_p(path_):
"""Set up dirs for results in working dir"""try:
makedirs(path_)
exceptOSErrorasexc:
ifexc.errno==errno.EEXISTandos.path.isdir(path_):
passelse:
raise# Make directories if neededtry:
mkdir_p(savepaths['summaries'])
mkdir_p(savepaths['fits'])
mkdir_p(savepaths['raw_saves'])
exceptOSError:
print("Savepaths not set. Please set them in 'configs/savepaths.json'")
frompathlibimportPathparent=Path(__file__).resolve().parentexamples_dir=str(parent)[:-11] +"docs/examples"print("Using docs examples directory: ", examples_dir)
savepaths['summaries'] =examples_dir+"/summaries/"savepaths['fits'] =examples_dir+"/saved_fits/"savepaths['raw_saves'] =examples_dir+"/raw_saves/"mkdir_p(savepaths['summaries'])
mkdir_p(savepaths['fits'])
mkdir_p(savepaths['raw_saves'])
file_exists=path.exists(savepaths['summaries'] +"beamsize_config_info.csv")
ifnotfile_exists:
# TODO: check how many wires are saving and save appropriately.# This is hardcoded for single measurements right now.f=open(savepaths['summaries'] +"beamsize_config_info.csv", "a+")
f.write(
f"{'timestamp'},{'varx_cur'},{'vary_cur'},{'varz_cur'},"f"{'xrms'},{'yrms'},{'xrms_err'},{'yrms_err'},"f"{'xrms'},{'yrms'},{'xrms_err'},{'yrms_err'},"f"{'xrms'},{'yrms'},{'xrms_err'},{'yrms_err'},"f"{'xrms'},{'yrms'},{'xrms_err'},{'yrms_err'}\n"
)
f.close()
The text was updated successfully, but these errors were encountered:
This is hardcoded for single measurements right now.
PyEmittance/pyemittance/emittance_calc_multiwire.py
Line 124 in 3862eff
The text was updated successfully, but these errors were encountered: