Skip to content

Commit

Permalink
Merge pull request #122 from aglowacki/master
Browse files Browse the repository at this point in the history
Added dead_time and fix analysis during scan
  • Loading branch information
Arthur Glowacki authored Oct 8, 2021
2 parents ed8b4c8 + be303d0 commit 36226af
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ||
Expand Down
14 changes: 14 additions & 0 deletions src/core/process_whole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> hdf5_dataset_list;

Expand Down
1 change: 1 addition & 0 deletions src/data_struct/analysis_job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
2 changes: 2 additions & 0 deletions src/data_struct/analysis_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
91 changes: 91 additions & 0 deletions src/io/file/hdf5_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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 << " " <<x<< " " << dims_in[1] <<"\n";
for (hsize_t y = 0; y < dims_in[2]; y++)
{
offset[1] = x;
offset[2] = y;
H5Sselect_hyperslab(mca_arr_space, H5S_SELECT_SET, offset, nullptr, count, nullptr);
hid_t error = H5Dread(mca_arr_id, H5T_NATIVE_REAL, memoryspace_id, mca_arr_space, H5P_DEFAULT, buffer.data());
if (error > -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<std::string> files_to_avg)
{
std::lock_guard<std::mutex> lock(_mutex);
Expand Down
2 changes: 2 additions & 0 deletions src/io/file/hdf5_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/io/file/mda_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

Expand Down

0 comments on commit 36226af

Please sign in to comment.