Skip to content

Commit aff3e0e

Browse files
authored
Merge pull request #229 from aglowacki/master
Fix bug loading intgrated spectra from fly scan for quantification
2 parents aecfc43 + 3575466 commit aff3e0e

File tree

7 files changed

+118
-48
lines changed

7 files changed

+118
-48
lines changed

src/core/process_whole.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -473,49 +473,23 @@ bool perform_quantification(data_struct::Analysis_Job<double>* analysis_job, boo
473473
{
474474
fitting::optimizers::Optimizer<double>* optimizer = analysis_job->optimizer();
475475
for (auto& quant_itr : detector->avg_quantification_scaler_map)
476-
{
477-
logI << Fitting_Routine_To_Str.at(fit_itr.first) << " " << quant_itr.first << "\n";
478-
std::string str_val = std::to_string( quant_itr.second );
479-
int cntr = 0;
480-
for (auto itr : str_val)
481-
{
482-
if(itr == '.')
483-
{
484-
break;
485-
}
486-
if(itr == '0' && cntr == 0) // ignore leading zero
487-
{
488-
cntr --;
489-
}
490-
cntr++;
491-
}
476+
{
477+
double reciprocal = 1.0 / quant_itr.second;
492478

493-
double recipricle = quant_itr.second;
494-
if( cntr > 0 )
495-
{
496-
if(cntr == 1)
497-
{
498-
recipricle = 1.0 / quant_itr.second;
499-
}
500-
else
501-
{
502-
recipricle = std::pow(10.0, cntr-1) / quant_itr.second;
503-
}
504-
}
505479
Fit_Parameters<double> fit_params;
506480
// min, and max values doen't matter because we are free fitting amplitude only
507-
fit_params.add_parameter(Fit_Param<double>("quantifier", 0., 1.0e20, recipricle, .1, E_Bound_Type::LIMITED_LO_HI));
481+
fit_params.add_parameter(Fit_Param<double>("quantifier", 0., 1.0e20, reciprocal, .01, E_Bound_Type::LIMITED_LO_HI));
508482
optimizer->minimize_quantification(&fit_params, &detector->all_element_quants[fit_itr.first][quant_itr.first], &quantification_model);
509483
double val = fit_params["quantifier"].value;
510484

511485
if(false == std::isfinite(val))
512486
{
513-
logW<<"Quantifier Value = Inf. setting it to 0.\n";
487+
logW << Fitting_Routine_To_Str.at(fit_itr.first) << " " << quant_itr.first <<"Quantifier Value = Inf. setting it to 0.\n";
514488
val = 0;
515489
}
516490
else
517491
{
518-
logI<<"Quantifier Value = "<<val<<"\n";
492+
logI<< Fitting_Routine_To_Str.at(fit_itr.first) << " " << quant_itr.first <<" Quantifier Value Start = (1/"<<quant_itr.second<<") = "<<reciprocal<< " :: Optimized = "<<val<<"\n";
519493
}
520494

521495
detector->update_calibration_curve(fit_itr.first, quant_itr.first, &quantification_model, val);

src/data_struct/scan_info.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ class DLL_EXPORT Scan_Info
137137
return nullptr;
138138
}
139139

140+
T_real scaler_avg_value(const std::string& scaler_name)
141+
{
142+
for (auto& itr : scaler_maps)
143+
{
144+
if (itr.name == scaler_name)
145+
{
146+
return itr.values.mean();
147+
}
148+
}
149+
return (T_real)0.0;
150+
}
151+
140152
Scan_Meta_Info<T_real> meta_info;
141153
std::vector<Scaler_Map<T_real>> scaler_maps;
142154
std::vector<Extra_PV> extra_pvs;

src/io/file/file_scan.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,17 @@ namespace io
396396
return dir_list;
397397
}
398398
// ----------------------------------------------------------------------------
399+
400+
size_t File_Scan::get_total_count()
401+
{
402+
return _edf_files.size() +
403+
_netcdf_files.size() +
404+
_bnp_netcdf_files.size() +
405+
_hdf_files.size() +
406+
_hdf_xspress_files.size() +
407+
_hdf_emd_files.size() +
408+
_netcdf_tetramm_files.size();
409+
}
410+
399411
}// end namespace file
400412
}// end namespace io

src/io/file/file_scan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ namespace io
112112

113113
const std::vector<std::string>& netcdf_tetramm_files() { return _netcdf_tetramm_files; }
114114

115+
size_t get_total_count();
116+
115117
private:
116118

117119
File_Scan();

src/io/file/hl_file_io.h

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,79 @@ DLL_EXPORT bool load_and_integrate_spectra_volume(std::string dataset_directory,
634634
integrated_spectra->setZero(2048);
635635
}
636636

637+
integrated_spectra->elapsed_livetime((T_real)0.0);
638+
integrated_spectra->elapsed_realtime((T_real)0.0);
639+
integrated_spectra->input_counts((T_real)0.0);
640+
integrated_spectra->output_counts((T_real)0.0);
637641

638-
if (hasNetcdf)
642+
if(hasNetcdf)
639643
{
640644
dims[1] -= 2; // remove 2 for the scaler channels flys scan hardware trigger bug
645+
}
646+
647+
// tetramm is scalers only
648+
if(hasTetraMM)
649+
{
650+
// tetramm has 2 files it can save scalers. filename_tetra1_0.nc or filename_tetra2_0.nc
651+
std::ifstream file_io(dataset_directory + "tetramm" + DIR_END_CHAR + tmp_dataset_file + "_0.nc");
652+
if (file_io.is_open())
653+
{
654+
file_io.close();
655+
std::string full_filename;
656+
for (size_t i = 0; i < dims[0]; i++)
657+
{
658+
full_filename = dataset_directory + "tetramm" + DIR_END_CHAR + tmp_dataset_file + "_" + std::to_string(i) + ".nc";
659+
//todo: add verbose option
660+
//logI<<"Loading file "<<full_filename<<"\n";
661+
// if tetramm tag is missing in file name, we assume it is tetra1
662+
size_t spec_size = io::file::NetCDF_IO<T_real>::inst()->load_scalers_line(full_filename, "tetra1_", i, mda_io.get_scan_info(), params_override);
663+
}
664+
}
665+
std::ifstream file_io1(dataset_directory + "tetramm" + DIR_END_CHAR + tmp_dataset_file + "_tetra1_0.nc");
666+
if (file_io1.is_open())
667+
{
668+
file_io1.close();
669+
std::string full_filename;
670+
for (size_t i = 0; i < dims[0]; i++)
671+
{
672+
full_filename = dataset_directory + "tetramm" + DIR_END_CHAR + tmp_dataset_file + "_tetra1_" + std::to_string(i) + ".nc";
673+
//todo: add verbose option
674+
//logI<<"Loading file "<<full_filename<<"\n";
675+
size_t spec_size = io::file::NetCDF_IO<T_real>::inst()->load_scalers_line(full_filename, "tetra1_", i, mda_io.get_scan_info(), params_override);
676+
}
677+
}
678+
std::ifstream file_io2(dataset_directory + "tetramm" + DIR_END_CHAR + tmp_dataset_file + "_tetra2_0.nc");
679+
if (file_io2.is_open())
680+
{
681+
file_io2.close();
682+
std::string full_filename;
683+
for (size_t i = 0; i < dims[0]; i++)
684+
{
685+
full_filename = dataset_directory + "tetramm" + DIR_END_CHAR + tmp_dataset_file + "_tetra2_" + std::to_string(i) + ".nc";
686+
//todo: add verbose option
687+
//logI<<"Loading file "<<full_filename<<"\n";
688+
size_t spec_size = io::file::NetCDF_IO<T_real>::inst()->load_scalers_line(full_filename, "tetra2_", i, mda_io.get_scan_info(), params_override);
689+
}
690+
}
691+
T_real val = mda_io.get_scan_info()->scaler_avg_value(STR_US_IC);
692+
if(val > (T_real)0.0)
693+
{
694+
params_override->US_IC = val;
695+
}
696+
val = mda_io.get_scan_info()->scaler_avg_value(STR_DS_IC);
697+
if(val > (T_real)0.0)
698+
{
699+
params_override->DS_IC = val;
700+
}
701+
val = mda_io.get_scan_info()->scaler_avg_value(STR_US_FM);
702+
if(val > (T_real)0.0)
703+
{
704+
params_override->US_FM = val;
705+
}
706+
707+
}
708+
if (hasNetcdf)
709+
{
641710
std::ifstream file_io(dataset_directory + "flyXRF" + DIR_END_CHAR + tmp_dataset_file + file_middle + "0.nc");
642711
if (file_io.is_open())
643712
{
@@ -984,6 +1053,18 @@ DLL_EXPORT bool load_spectra_volume(std::string dataset_directory,
9841053
if(true == io::file::HDF5_IO::inst()->load_spectra_vol_apsu(dataset_directory, dataset_file, detector_num, spectra_volume, scan_info_edf))
9851054
{
9861055
scan_type = scan_info_edf.meta_info.scan_type;
1056+
if (save_scalers)
1057+
{
1058+
io::file::HDF5_IO::inst()->start_save_seq(true);
1059+
1060+
// add ELT, ERT, INCNT, OUTCNT to scaler map
1061+
if (spectra_volume != nullptr)
1062+
{
1063+
spectra_volume->generate_scaler_maps(&(scan_info_edf.scaler_maps));
1064+
}
1065+
1066+
io::file::HDF5_IO::inst()->save_scan_scalers(&scan_info_edf, params_override);
1067+
}
9871068
return true;
9881069
}
9891070
// try ESRF dataset

src/io/file/mda_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ bool MDA_IO<T_real>::load_integrated_spectra(std::string path,
822822
return false;
823823
}
824824

825-
_load_scalers(false, hasNetCDF, false);
825+
_load_scalers(false, hasNetCDF, hasNetCDF);
826826
_load_meta_info(hasNetCDF, false);
827827
_load_extra_pvs_vector();
828828

src/io/file/netcdf_io.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -428,23 +428,12 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
428428
}
429429
}
430430

431-
432431
if (ltype == E_load_type::INTEGRATED)
433432
{
434-
if(cur_row == 0) //first spectra row being loaded
435-
{
436-
spectra->elapsed_livetime(sum_elapsed_livetime);
437-
spectra->elapsed_realtime(sum_elapsed_realtime);
438-
spectra->input_counts(sum_input_counts);
439-
spectra->output_counts(sum_output_counts);
440-
}
441-
else
442-
{
443-
spectra->elapsed_livetime(spectra->elapsed_livetime() + sum_elapsed_livetime);
444-
spectra->elapsed_realtime(spectra->elapsed_realtime() + sum_elapsed_realtime);
445-
spectra->input_counts(spectra->input_counts() + sum_input_counts);
446-
spectra->output_counts(spectra->output_counts() + sum_output_counts);
447-
}
433+
spectra->elapsed_livetime(spectra->elapsed_livetime() + sum_elapsed_livetime);
434+
spectra->elapsed_realtime(spectra->elapsed_realtime() + sum_elapsed_realtime);
435+
spectra->input_counts(spectra->input_counts() + sum_input_counts);
436+
spectra->output_counts(spectra->output_counts() + sum_output_counts);
448437
}
449438

450439
if ((retval = nc_close(ncid)))

0 commit comments

Comments
 (0)