@@ -60,6 +60,7 @@ POSSIBILITY OF SUCH DAMAGE.
6060#include < limits>
6161#include < sstream>
6262#include < fstream>
63+ #include < typeinfo>
6364
6465#include < stdlib.h>
6566
@@ -119,8 +120,6 @@ DLL_EXPORT bool optimize_integrated_fit_params(data_struct::Analysis_Job<double>
119120
120121DLL_EXPORT void generate_optimal_params (data_struct::Analysis_Job<double >* analysis_job);
121122
122- void load_and_fit_quatification_datasets (data_struct::Analysis_Job<double >* analysis_job, size_t detector_num, std::vector<Quantification_Standard<double >>& standard_element_weights, std::unordered_map<size_t , double >& quant_map);
123-
124123DLL_EXPORT void optimize_single_roi (data_struct::Analysis_Job<double >& analysis_job, std::string roi_file_name, std::map<int , std::map<std::string, data_struct::Fit_Parameters<double >>>& out_roi_fit_params, Callback_Func_Status_Def* status_callback = nullptr );
125124
126125DLL_EXPORT void optimize_rois (data_struct::Analysis_Job<double >& analysis_job);
@@ -158,6 +157,45 @@ DLL_EXPORT data_struct::Fit_Count_Dict<T_real>* generate_fit_count_dict(const Fi
158157 return element_fit_counts_dict;
159158}
160159
160+ // ----------------------------------------------------------------------------
161+
162+ template <typename T_real>
163+ DLL_EXPORT data_struct::Fit_Count_Dict<T_real>* generate_fit_count_by_shell_dict (const Fit_Element_Map_Dict<T_real>* elements_to_fit, size_t height, size_t width, bool alloc_iter_count)
164+ {
165+ data_struct::Fit_Count_Dict<T_real>* element_fit_counts_dict = new data_struct::Fit_Count_Dict<T_real>();
166+ for (auto & e_itr : *elements_to_fit)
167+ {
168+ element_fit_counts_dict->emplace (std::pair<std::string, data_struct::ArrayXXr<T_real> >(e_itr.first , data_struct::ArrayXXr<T_real>()));
169+ element_fit_counts_dict->at (e_itr.first ).resize (height, width);
170+ for (auto & s_itr : e_itr.second ->generate_roi_centers_per_shell ())
171+ {
172+ element_fit_counts_dict->emplace (std::pair<std::string, data_struct::ArrayXXr<T_real> >(e_itr.first + " _" + s_itr.first , data_struct::ArrayXXr<T_real>()));
173+ element_fit_counts_dict->at (e_itr.first + " _" + s_itr.first ).resize (height, width);
174+ }
175+ }
176+
177+ if (alloc_iter_count)
178+ {
179+ // Allocate memeory to save number of fit iterations
180+ element_fit_counts_dict->emplace (std::pair<std::string, data_struct::ArrayXXr<T_real> >(STR_NUM_ITR, data_struct::ArrayXXr<T_real>()));
181+ element_fit_counts_dict->at (STR_NUM_ITR).resize (height, width);
182+ element_fit_counts_dict->emplace (std::pair<std::string, data_struct::ArrayXXr<T_real> >(STR_RESIDUAL, data_struct::ArrayXXr<T_real>()));
183+ element_fit_counts_dict->at (STR_RESIDUAL).resize (height, width);
184+ }
185+
186+ // TOTAL_FLUORESCENCE_YIELD
187+ element_fit_counts_dict->emplace (std::pair<std::string, data_struct::ArrayXXr<T_real> >(STR_TOTAL_FLUORESCENCE_YIELD, data_struct::ArrayXXr<T_real>()));
188+ element_fit_counts_dict->at (STR_TOTAL_FLUORESCENCE_YIELD).resize (height, width);
189+
190+ // SUM_ELASTIC_INELASTIC
191+ element_fit_counts_dict->emplace (std::pair<std::string, data_struct::ArrayXXr<T_real> >(STR_SUM_ELASTIC_INELASTIC_AMP, data_struct::ArrayXXr<T_real>()));
192+ element_fit_counts_dict->at (STR_SUM_ELASTIC_INELASTIC_AMP).resize (height, width);
193+
194+
195+ return element_fit_counts_dict;
196+ }
197+
198+
161199// ----------------------------------------------------------------------------
162200
163201template <typename T_real>
@@ -172,35 +210,26 @@ DLL_EXPORT bool fit_single_spectra(fitting::routines::Base_Fit_Routine<T_real>*
172210 std::unordered_map<std::string, T_real> counts_dict;
173211 fit_routine->fit_spectra (model, spectra, elements_to_fit, counts_dict);
174212 // save count / sec
175- for (auto & el_itr : *elements_to_fit )
213+ for (auto & itr : *out_fit_counts )
176214 {
177- (*out_fit_counts)[el_itr.first ](i, j) = counts_dict[el_itr.first ] / spectra->elapsed_livetime ();
215+ if (counts_dict.count (itr.first ) > 0 )
216+ {
217+ itr.second (i, j) = counts_dict[itr.first ] / spectra->elapsed_livetime ();
218+ }
219+ // (*out_fit_counts)[el_itr.first](i, j) = counts_dict[el_itr.first] / spectra->elapsed_livetime();
178220 }
179221 (*out_fit_counts)[STR_NUM_ITR](i, j) = counts_dict[STR_NUM_ITR];
180222
181223 (*out_fit_counts)[STR_RESIDUAL](i, j) = counts_dict[STR_RESIDUAL];
182224 // add total fluorescense yield
183225 if (out_fit_counts->count (STR_TOTAL_FLUORESCENCE_YIELD))
184226 {
185- (*out_fit_counts)[STR_TOTAL_FLUORESCENCE_YIELD](i, j) = spectra->sum ();
227+ (*out_fit_counts)[STR_TOTAL_FLUORESCENCE_YIELD](i, j) = spectra->sum () / spectra-> elapsed_livetime () ;
186228 }
187229 // add sum coherent and compton
188230 if (out_fit_counts->count (STR_SUM_ELASTIC_INELASTIC_AMP) > 0 && counts_dict.count (STR_COHERENT_SCT_AMPLITUDE) > 0 && counts_dict.count (STR_COMPTON_AMPLITUDE) > 0 )
189231 {
190232 (*out_fit_counts)[STR_SUM_ELASTIC_INELASTIC_AMP](i, j) = counts_dict[STR_COHERENT_SCT_AMPLITUDE] + counts_dict[STR_COMPTON_AMPLITUDE];
191- // add total fluorescense yield
192- if (out_fit_counts->count (STR_TOTAL_FLUORESCENCE_YIELD))
193- { // (sum - (elastic + inelastic)) / live time
194- (*out_fit_counts)[STR_TOTAL_FLUORESCENCE_YIELD](i, j) = (spectra->sum () - (*out_fit_counts)[STR_SUM_ELASTIC_INELASTIC_AMP](i, j)) / spectra->elapsed_livetime ();
195- }
196- }
197- else
198- {
199- // add total fluorescense yield
200- if (out_fit_counts->count (STR_TOTAL_FLUORESCENCE_YIELD))
201- {
202- (*out_fit_counts)[STR_TOTAL_FLUORESCENCE_YIELD](i, j) = spectra->sum () / spectra->elapsed_livetime ();
203- }
204233 }
205234
206235 return true ;
@@ -236,7 +265,7 @@ DLL_EXPORT void proc_spectra(data_struct::Spectra_Volume<T_real>* spectra_volume
236265 fitting::models::Range energy_range = data_struct::get_energy_range (spectra_volume->samples_size (), &(detector->fit_params_override_dict .fit_params ));
237266
238267 std::chrono::time_point<std::chrono::system_clock> start, end;
239-
268+
240269 for (auto & itr : detector->fit_routines )
241270 {
242271 fitting::routines::Base_Fit_Routine<T_real>* fit_routine = itr.second ;
@@ -253,8 +282,17 @@ DLL_EXPORT void proc_spectra(data_struct::Spectra_Volume<T_real>* spectra_volume
253282 // Fit job queue
254283 std::queue<std::future<bool > >* fit_job_queue = new std::queue<std::future<bool > >();
255284
285+ data_struct::Fit_Count_Dict<T_real>* element_fit_count_dict = nullptr ;
256286 // Allocate memeory to save fit counts
257- data_struct::Fit_Count_Dict<T_real>* element_fit_count_dict = generate_fit_count_dict (&override_params->elements_to_fit , spectra_volume->rows (), spectra_volume->cols (), true );
287+ if (typeid (*fit_routine) == typeid (fitting::routines::ROI_Fit_Routine<T_real>) && scan_type == STR_SCAN_TYPE_POLAR_XANES)
288+ {
289+ ((fitting::routines::ROI_Fit_Routine<T_real> *)fit_routine)->set_fit_separate_shells (true );
290+ element_fit_count_dict = generate_fit_count_by_shell_dict (&override_params->elements_to_fit , spectra_volume->rows (), spectra_volume->cols (), true );
291+ }
292+ else
293+ {
294+ element_fit_count_dict = generate_fit_count_dict (&override_params->elements_to_fit , spectra_volume->rows (), spectra_volume->cols (), true );
295+ }
258296
259297 for (size_t i = 0 ; i < spectra_volume->rows (); i++)
260298 {
@@ -280,7 +318,7 @@ DLL_EXPORT void proc_spectra(data_struct::Spectra_Volume<T_real>* spectra_volume
280318 cur_block++;
281319 }
282320
283- std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now ();
321+ end = std::chrono::system_clock::now ();
284322 std::chrono::duration<double > elapsed_seconds = end - start;
285323 logI << " Fitting [ " << fit_routine->get_name () << " ] elapsed time: " << elapsed_seconds.count () << " s" << " \n " ;
286324
@@ -302,8 +340,8 @@ DLL_EXPORT void proc_spectra(data_struct::Spectra_Volume<T_real>* spectra_volume
302340 }
303341 // save png
304342 std::string dataset_fullpath = io::file::HDF5_IO::inst ()->get_filename ();
305- int sidx = dataset_fullpath.find (" img.dat" );
306- if (dataset_fullpath.length () > 0 && sidx > 0 )
343+ size_t sidx = dataset_fullpath.find (" img.dat" );
344+ if (dataset_fullpath.length () > 0 && sidx != std::string::npos )
307345 {
308346 dataset_fullpath.replace (sidx, 7 , " output" ); // 7 = sizeof("img.dat")
309347 std::string str_path = dataset_fullpath + " _" + fit_routine->get_name () + " .png" ;
@@ -322,11 +360,10 @@ DLL_EXPORT void proc_spectra(data_struct::Spectra_Volume<T_real>* spectra_volume
322360 if (itr.first == data_struct::Fitting_Routines::GAUSS_MATRIX)
323361 {
324362 fitting::routines::Matrix_Optimized_Fit_Routine<T_real>* matrix_fit = (fitting::routines::Matrix_Optimized_Fit_Routine<T_real>*)fit_routine;
325- io::file::HDF5_IO::inst ()->save_max_10_spectra (fit_routine->get_name (),
326- matrix_fit->energy_range (),
327- matrix_fit->max_integrated_spectra (),
328- matrix_fit->max_10_integrated_spectra (),
329- matrix_fit->fitted_integrated_background ());
363+ io::file::HDF5_IO::inst ()->save_max_10_spectra (matrix_fit->energy_range (),
364+ matrix_fit->max_integrated_spectra (),
365+ matrix_fit->max_10_integrated_spectra (),
366+ matrix_fit->fitted_integrated_background ());
330367 }
331368
332369 delete fit_job_queue;
@@ -397,6 +434,17 @@ void find_quantifier_scalers(std::unordered_map<std::string, double>& pv_map, Qu
397434 }
398435 }
399436
437+ // itr again to look for scale_factor
438+ for (auto & itr : pv_map)
439+ {
440+ double multiplier = (T_real)1.0 ;
441+ if (pv_map.count (itr.first +" _SCALE_FACTOR" ) > 0 )
442+ {
443+ multiplier = pv_map.at (itr.first +" _SCALE_FACTOR" );
444+ }
445+ itr.second *= multiplier;
446+ }
447+
400448 // add any summded scalers to pv_map
401449 const std::vector<struct Summed_Scaler >* summed_scalers = data_struct::Scaler_Lookup::inst ()->get_summed_scaler_list (beamline);
402450 if (summed_scalers != nullptr )
0 commit comments