diff --git a/src/core/main.cpp b/src/core/main.cpp index de5d4461..ef17fa32 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -346,6 +346,10 @@ int main(int argc, char *argv[]) } } + if (clp.option_exists("--add_background")) + { + analysis_job.add_background = true; + } //TODO: add --quantify-only option if you already did the fits and just want to add quantification @@ -410,7 +414,8 @@ int main(int argc, char *argv[]) //analysis_job.update_scalers || analysis_job.export_int_fitted_to_csv || analysis_job.update_us_amps_str.length() > 0 || - analysis_job.update_quant_us_amps_str.length() > 0; + analysis_job.update_quant_us_amps_str.length() > 0 || + analysis_job.add_background; bool update_h5_fit = analysis_job.fitting_routines.size() > 0 || optimize_fit_override_params || diff --git a/src/core/process_whole.cpp b/src/core/process_whole.cpp index e493adf4..00476fe5 100644 --- a/src/core/process_whole.cpp +++ b/src/core/process_whole.cpp @@ -890,6 +890,20 @@ void interate_datasets_and_update(data_struct::Analysis_Job& analysis_job) io::generate_h5_averages(analysis_job.dataset_directory, dataset_file, analysis_job.detector_num_arr); } + if (analysis_job.add_background) + { + data_struct::Detector* detector = analysis_job.get_detector(0); + + if (detector != nullptr) + { + io::file::HDF5_IO::inst()->add_background(analysis_job.dataset_directory, dataset_file, detector->fit_params_override_dict); + } + else + { + logW << "Detector == nullptr for add_background\n"; + } + } + //generate a list of dataset to update std::vector hdf5_dataset_list; diff --git a/src/data_struct/analysis_job.cpp b/src/data_struct/analysis_job.cpp index f58163c4..a1a358c0 100644 --- a/src/data_struct/analysis_job.cpp +++ b/src/data_struct/analysis_job.cpp @@ -70,6 +70,7 @@ Analysis_Job::Analysis_Job() stream_over_network = false; //update_scalers = false; export_int_fitted_to_csv = false; + add_background = false; command_line = ""; theta_pv = ""; network_source_ip = ""; diff --git a/src/data_struct/analysis_job.h b/src/data_struct/analysis_job.h index 36279622..fbdac4a3 100644 --- a/src/data_struct/analysis_job.h +++ b/src/data_struct/analysis_job.h @@ -137,6 +137,8 @@ class DLL_EXPORT Analysis_Job bool export_int_fitted_to_csv; + bool add_background; + long long mem_limit; std::string update_us_amps_str; diff --git a/src/io/file/hdf5_io.cpp b/src/io/file/hdf5_io.cpp index 4d3e58a7..3aba1275 100644 --- a/src/io/file/hdf5_io.cpp +++ b/src/io/file/hdf5_io.cpp @@ -7450,6 +7450,97 @@ bool HDF5_IO::save_scan_scalers_bnl(std::string path, //----------------------------------------------------------------------------- +bool HDF5_IO::add_background(std::string directory, std::string filename, data_struct::Params_Override& params) +{ + + std::lock_guard lock(_mutex); + + std::string fullname = directory + DIR_END_CHAR + "img.dat" + DIR_END_CHAR + filename; + logI << fullname << "\n"; + hid_t file_id = H5Fopen(fullname.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + if (file_id < 0) + { + logE << "Could not open file\n"; + return false; + } + hid_t mca_arr_id = H5Dopen(file_id, "/MAPS/mca_arr", H5P_DEFAULT); + if (mca_arr_id < 0) + { + H5Fclose(file_id); + logW << "Could not open /MAPS/mca_arr\n"; + return false; + } + + hid_t back_arr_id = H5Dopen(file_id, "/MAPS/mca_background", H5P_DEFAULT); + if (back_arr_id < 0) + { + hid_t ocpypl_id = H5Pcreate(H5P_OBJECT_COPY); + hid_t status = H5Ocopy(file_id, "/MAPS/mca_arr", file_id, "/MAPS/mca_background", ocpypl_id, H5P_DEFAULT); + if (status < 0) + { + H5Fclose(file_id); + logW << "Could not copy /MAPS/mca_arr to /MAPS/mca_background\n"; + return false; + } + back_arr_id = H5Dopen(file_id, "/MAPS/mca_background", H5P_DEFAULT); + if (back_arr_id < 0) + { + H5Fclose(file_id); + logW << "Could not open mca_background after copy /MAPS/mca_arr to /MAPS/mca_background\n"; + return false; + } + } + + hid_t mca_arr_space = H5Dget_space(mca_arr_id); + + int rank = H5Sget_simple_extent_ndims(mca_arr_space); + + hsize_t dims_in[3]; + hsize_t offset[3] = { 0,0,0 }; + hsize_t count[3] = { 0,1,1 }; + int status_n = H5Sget_simple_extent_dims(mca_arr_space, &dims_in[0], NULL); + if (status_n < 0) + { + H5Fclose(file_id); + logW << "Can't get dims\n"; + return false; + } + count[0] = dims_in[0]; + hid_t memoryspace_id = H5Screate_simple(1, dims_in, nullptr); + + data_struct::ArrayXr buffer(count[0]); + fitting::models::Range energy_range = data_struct::get_energy_range(dims_in[0], &(params.fit_params)); + + logI << params.fit_params.value(STR_ENERGY_OFFSET) << " " << params.fit_params.value(STR_ENERGY_SLOPE) << " " << params.fit_params.value(STR_ENERGY_QUADRATIC) << " " << 0.0f << " " << params.fit_params.value(STR_SNIP_WIDTH) << " " << energy_range.min << " " << energy_range.max << "\n "; + + for (hsize_t x = 0; x < dims_in[1]; x++) + { + logI << fullname << " " < -1 ) + { + ArrayXr background = data_struct::snip_background((data_struct::Spectra*)&buffer, params.fit_params.value(STR_ENERGY_OFFSET), params.fit_params.value(STR_ENERGY_SLOPE), params.fit_params.value(STR_ENERGY_QUADRATIC), 0.0f, params.fit_params.value(STR_SNIP_WIDTH), energy_range.min, energy_range.max); + error = H5Dwrite(back_arr_id, H5T_NATIVE_REAL, memoryspace_id, mca_arr_space, H5P_DEFAULT, background.data()); + if (error < 0) + { + logE << x << " " << y << " bad write\n"; + } + } + } + } + H5Dclose(mca_arr_id); + H5Dclose(back_arr_id); + H5Fclose(file_id); + +} + +//----------------------------------------------------------------------------- + bool HDF5_IO::generate_avg(std::string avg_filename, std::vector files_to_avg) { std::lock_guard lock(_mutex); diff --git a/src/io/file/hdf5_io.h b/src/io/file/hdf5_io.h index 9f8d0d68..724395aa 100644 --- a/src/io/file/hdf5_io.h +++ b/src/io/file/hdf5_io.h @@ -241,6 +241,8 @@ class DLL_EXPORT HDF5_IO bool end_save_seq(bool loginfo=true); + bool add_background(std::string directory, std::string filename, data_struct::Params_Override& params); + private: HDF5_IO(); diff --git a/src/io/file/mda_io.cpp b/src/io/file/mda_io.cpp index 55a4f0cb..8877a2f1 100644 --- a/src/io/file/mda_io.cpp +++ b/src/io/file/mda_io.cpp @@ -1236,6 +1236,15 @@ void MDA_IO::_load_extra_pvs_vector() _scan_info.extra_pvs.push_back(e_pv); } } + else + { + data_struct::Extra_PV e_pv; + e_pv.name = "NULL"; + e_pv.description = "NULL"; + e_pv.unit = "NULL"; + e_pv.value = "NULL"; + _scan_info.extra_pvs.push_back(e_pv); + } }