@@ -57,22 +57,7 @@ def __init__(self,
5757 self .init_saving ()
5858
5959 # Main output of emittance calc
60- self .out_dict = {'nemitx' : None ,
61- 'nemity' : None ,
62- 'nemitx_err' : None ,
63- 'nemity_err' : None ,
64- 'nemit' : None ,
65- 'nemit_err' : None ,
66- 'bmagx' : None ,
67- 'bmagy' : None ,
68- 'bmagx_err' : None ,
69- 'bmagy_err' : None ,
70- 'bmag_emit' : None ,
71- 'bmag_emit_err' : None ,
72- 'opt_q_x' : None ,
73- 'opt_q_y' : None ,
74- 'total_points_measured' : None
75- }
60+ self .output = {}
7661
7762 def load_config (self ):
7863 # if path exists, load from path
@@ -121,48 +106,55 @@ def get_emit(self):
121106
122107 weights = self .weighting_func (bs , bs_err ) # 1/sigma
123108
124- # Storing quadvals and beamsizes in self.out_dict for plotting purposes
125- self .out_dict [f'quadvals{ dim } ' ] = list (q )
126- self .out_dict [f'beamsizes{ dim } ' ] = list (bs )
127- self .out_dict [f'beamsizeserr{ dim } ' ] = list (bs_err )
109+ # Storing quadvals and beamsizes in self.output for plotting purposes
110+ self .output [f'quadvals{ dim } ' ] = list (q )
111+ self .output [f'beamsizes{ dim } ' ] = list (bs )
112+ self .output [f'beamsizeserr{ dim } ' ] = list (bs_err )
128113
129114 res = estimate_sigma_mat_thick_quad (bs , kL , bs_err , weights , dim = dim , Lquad = self .quad_len ,
130- energy = self .energy , rmat = self .rmat , calc_bmag = self . calc_bmag ,
115+ energy = self .energy , rmat = self .rmat ,
131116 plot = self .plot , verbose = self .verbose )
132- if np .isnan (res [0 ]):
133- self .out_dict ['nemitx' ], self .out_dict ['nemity' ] = np .nan , np .nan
134- self .out_dict ['nemitx_err' ], self .out_dict ['nemity_err' ] = np .nan , np .nan
135- self .out_dict ['bmagx' ], self .out_dict ['bmagy' ] = np .nan , np .nan
136- self .out_dict ['bmagx_err' ], self .out_dict ['bmagy_err' ] = np .nan , np .nan
137- return self .out_dict
138- else :
139- emit , emit_err , beta_rel_err , alpha_rel_err = res [0 :4 ]
140- if self .calc_bmag :
141- sig_11 , sig_12 , sig_22 = res [4 :]
142-
143- norm_emit_res = normalize_emit (emit , emit_err , self .energy )
144- self .out_dict [f'nemit{ dim } ' ] = normalize_emit (emit , emit_err , self .energy )[0 ]
145- self .out_dict [f'nemit{ dim } _err' ] = normalize_emit (emit , emit_err , self .energy )[1 ]
117+ # Add all results
118+ self .output .update (res )
119+
120+ # Skip further calcs if there was an error
121+ if res [f'error_{ dim } ' ]:
122+ continue
146123
147124 if self .calc_bmag :
148- self .sig_mat_screen [dim ] = [sig_11 , sig_12 , sig_22 ]
125+ if dim == 'x' :
126+ sig_11 = res ['screen_sigma_11' ]
127+ sig_12 = res ['screen_sigma_12' ]
128+ sig_22 = res ['screen_sigma_22' ]
129+
130+ else :
131+ sig_11 = res ['screen_sigma_33' ]
132+ sig_12 = res ['screen_sigma_34' ]
133+ sig_22 = res ['screen_sigma_44' ]
134+ self .sig_mat_screen [dim ] = [sig_11 , sig_12 , sig_22 ]
135+
136+ beta_rel_err = res [f'beta_{ dim } _rel_err' ]
137+ alpha_rel_err = res [f'alpha_{ dim } _rel_err' ]
138+
139+
149140 self .beta_err = beta_rel_err
150141 self .alpha_err = alpha_rel_err
151142
152143 bmag_calc_res = self .get_twiss_bmag (dim = dim )
153144 # Get bmag and bmag_err
154- self .out_dict [f'bmag { dim } ' ] = bmag_calc_res [0 ]
155- self .out_dict [f'bmag { dim } _err' ] = bmag_calc_res [1 ]
145+ self .output [f'screen_bmag { dim } ' ] = bmag_calc_res [0 ]
146+ self .output [f'screen_bmag { dim } _err' ] = bmag_calc_res [1 ]
156147 # Get best value for scanning quad
157- self .out_dict [f'opt_q_ { dim } ' ] = q [bmag_calc_res [2 ]]
148+ self .output [f'optimal_quadval_ { dim } ' ] = q [bmag_calc_res [2 ]]
158149
159- # get geometric mean (set to None when x and y are not calculated)
160- self .get_gmean_emit ()
150+ # get geometric mean if possible
151+ if (not self .output ['error_x' ]) and (not self .output ['error_y' ]) :
152+ self .get_gmean_emit ()
161153
162154 if self .save_runs :
163155 self .save_run ()
164156
165- return self .out_dict
157+ return self .output
166158
167159 def get_twiss_bmag (self , dim = 'x' ):
168160
@@ -185,31 +177,31 @@ def get_twiss_bmag(self, dim='x'):
185177 def get_gmean_emit (self ):
186178
187179 try :
188- nemit = np .sqrt ( self .out_dict [ 'nemitx ' ] * self .out_dict [ 'nemity ' ] )
189- nemit_err = nemit * ( (self .out_dict [ 'nemitx_err ' ]/ self .out_dict [ 'nemitx ' ])** 2 +
190- (self .out_dict [ 'nemity_err ' ]/ self .out_dict [ 'nemity ' ])** 2 )** 0.5
180+ nemit = np .sqrt ( self .output [ 'norm_emit_x ' ] * self .output [ 'norm_emit_y ' ] )
181+ nemit_err = nemit * ( (self .output [ 'norm_emit_x_err ' ]/ self .output [ 'norm_emit_x ' ])** 2 +
182+ (self .output [ 'norm_emit_y_err ' ]/ self .output [ 'norm_emit_y ' ])** 2 )** 0.5
191183
192- self .out_dict [ 'nemit ' ] = nemit
193- self .out_dict [ 'nemit_err ' ] = nemit_err
184+ self .output [ 'sqrt_norm_emit_4d ' ] = nemit
185+ self .output [ 'sqrt_norm_emit_4d_err ' ] = nemit_err
194186
195- if self . out_dict [ 'bmagx' ] is not None and self . out_dict [ 'bmagy' ] is not None :
196- nbmag = np .sqrt ( self .out_dict [ 'bmagx ' ] * self .out_dict [ 'bmagy ' ] )
187+ if 'bmag_x' in self . output and 'bmag_y' in self . output :
188+ nbmag = np .sqrt ( self .output [ 'bmag_x ' ] * self .output [ 'bmag_y ' ] )
197189 bmag_emit_err = nemit * nbmag * (
198- (self .out_dict [ 'nemitx_err ' ]/ self .out_dict [ 'nemitx ' ])** 2 +
199- (self .out_dict [ 'nemity_err ' ]/ self .out_dict [ 'nemity ' ])** 2 +
200- (self .out_dict [ 'bmagx_err ' ]/ self .out_dict [ 'bmagx ' ])** 2 +
201- (self .out_dict [ 'bmagy_err ' ]/ self .out_dict [ 'bmagy ' ])** 2 )** 0.5
202- self .out_dict ['bmag_emit' ] = nemit * nbmag
203- self .out_dict ['bmag_emit_err' ] = bmag_emit_err
190+ (self .output [ 'norm_emit_x_err ' ]/ self .output [ 'norm_emit_x ' ])** 2 +
191+ (self .output [ 'norm_emit_y_err ' ]/ self .output [ 'norm_emit_y ' ])** 2 +
192+ (self .output [ 'bmag_x_err ' ]/ self .output [ 'bmag_x ' ])** 2 +
193+ (self .output [ 'bmag_y_err ' ]/ self .output [ 'bmag_y ' ])** 2 )** 0.5
194+ self .output ['bmag_emit' ] = nemit * nbmag
195+ self .output ['bmag_emit_err' ] = bmag_emit_err
204196
205197 except TypeError :
206- self .out_dict [ 'nemit ' ] = np .nan
207- self .out_dict [ 'nemit_err ' ] = np .nan
208- self .out_dict ['bmag_emit' ] = np .nan
209- self .out_dict ['bmag_emit_err' ] = np .nan
198+ self .output [ 'sqrt_norm_emit_4d ' ] = np .nan
199+ self .output [ 'sqrt_norm_emit_4d_err ' ] = np .nan
200+ self .output ['bmag_emit' ] = np .nan
201+ self .output ['bmag_emit_err' ] = np .nan
210202
211203 def save_run (self ):
212- save_emit_run (self .out_dict , path = self .config_dict ['savepaths' ]['fits' ])
204+ save_emit_run (self .output , path = self .config_dict ['savepaths' ]['fits' ])
213205
214206 def init_saving (self ):
215207 """Initialize dirs and files for saving"""
0 commit comments