diff --git a/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer b/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer index fbae13647..80bf2f7a8 100755 Binary files a/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer and b/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer differ diff --git a/slsDetectorServers/mythen3DetectorServer/mythen3.c b/slsDetectorServers/mythen3DetectorServer/mythen3.c index 04f4d5979..3da3cd92d 100644 --- a/slsDetectorServers/mythen3DetectorServer/mythen3.c +++ b/slsDetectorServers/mythen3DetectorServer/mythen3.c @@ -35,32 +35,6 @@ int getChipStatusRegister(){ return chipStatusRegister; } -// int setGainCaps(int caps){ -// int csr = getChipStatusRegister(); - -// int gain_mask = 0; -// gain_mask |= 1 << CSR_C10pre; -// gain_mask |= 1 << CSR_C15sh; -// gain_mask |= 1 << CSR_C30sh; -// gain_mask |= 1 << CSR_C50sh; -// gain_mask |= 1 << CSR_C225ACsh; -// gain_mask |= 1 << CSR_C15pre; - -// LOG(logINFO, ("gain_mask: 0x%x\n", gain_mask)); -// LOG(logINFO, ("csr: 0x%x\n", csr)); -// csr &= ~gain_mask; //zero out the bits in the gain mask -// LOG(logINFO, ("csr: 0x%x\n", csr)); -// caps &= gain_mask; -// csr |= caps; - -// LOG(logINFO, ("csr: 0x%x\n", csr)); - -// //now comes the actual setting - - -// return 0; -// } - patternParameters *setChipStatusRegister(int csr) { int iaddr=0; int nbits=18; diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index fbe0044a9..61d724ba3 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -1061,30 +1061,13 @@ int64_t getMeasurementTime() { /* parameters - module, speed, readout */ int setModule(sls_detector_module myMod, char *mess) { - LOG(logINFO, ("Setting module\n")); - // settings - if (myMod.reg >= 0) { - setSettings((enum detectorSettings)myMod.reg); - if (getSettings() != (enum detectorSettings)myMod.reg) { - sprintf( - mess, - "Could not set module. Could not set settings to %d, read %d\n", - myMod.reg, (int)getSettings()); - LOG(logERROR, (mess)); - return FAIL; - } - detectorModules->reg = myMod.reg; - } - // custom trimbit file - else { - // changed for setsettings (direct), - // custom trimbit file (setmodule with myMod.reg as -1), - // change of dac (direct) - for (int i = 0; i < NCOUNTERS; ++i) { - setThresholdEnergy(i, -1); - } + + if (setGainCaps(myMod.reg)){ + sprintf(mess, "Could not set module gain caps\n"); + LOG(logERROR, (mess)); + return FAIL; } // dacs @@ -1108,24 +1091,12 @@ int setModule(sls_detector_module myMod, char *mess) { } } - // if settings given and cannot be validated (after setting dacs), return - // error - if (myMod.reg >= 0) { - if (getSettings() != (enum detectorSettings)myMod.reg) { - sprintf( - mess, - "Could not set module. The dacs in file do not correspond to " - "settings %d\n", - myMod.reg); - LOG(logERROR, (mess)); - return FAIL; - } - } - // threshold for (int i = 0; i < NCOUNTERS; ++i) { if (myMod.eV[i] >= 0) { setThresholdEnergy(i, myMod.eV[i]); + }else{ + setThresholdEnergy(i, -1); } } diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 2418567b9..496d97efa 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -1549,18 +1549,11 @@ int set_module(int file_des) { // only set else if (Server_VerifyLock() == OK) { // check index + +#if !(defined(EIGERD) || defined(MYTHEN3D)) +//TODO! Check if this is used for any detector switch (module.reg) { -#ifdef EIGERD - case STANDARD: - case HIGHGAIN: - case LOWGAIN: - case VERYHIGHGAIN: - case VERYLOWGAIN: -#elif MYTHEN3D - case STANDARD: - case FAST: - case HIGHGAIN: -#elif JUNGFRAUD +#ifdef JUNGFRAUD case DYNAMICGAIN: case DYNAMICHG0: case FIXGAIN1: @@ -1579,10 +1572,12 @@ int set_module(int file_des) { modeNotImplemented("Settings", (int)module.reg); break; } - +#endif ret = setModule(module, mess); enum detectorSettings retval = getSettings(); +#if !(defined(EIGERD) || defined(MYTHEN3D)) validate(module.reg, (int)retval, "set module (settings)", DEC); +#endif LOG(logDEBUG1, ("Settings: %d\n", retval)); } free(myChan); diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index bb57e9509..8582e15e5 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -3203,6 +3203,9 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, throw RuntimeError("Could not open settings file: " + fname); } + auto file_size = getFileSize(infile); + + // eiger if (shm()->myDetectorType == EIGER) { infile.read(reinterpret_cast(myMod.dacs), @@ -3228,6 +3231,16 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, // mythen3 (dacs, trimbits) else if (shm()->myDetectorType == MYTHEN3) { + int expected_size = + sizeof(int) * myMod.ndac + sizeof(int) * myMod.nchan + sizeof(myMod.reg); + if (file_size != expected_size) { + throw RuntimeError("The size of the settings file: " + fname + + " differs from the expected size, " + + std::to_string(file_size) + " instead of " + + std::to_string(expected_size) + " bytes"); + } + infile.read(reinterpret_cast(&myMod.reg), + sizeof(myMod.reg)); infile.read(reinterpret_cast(myMod.dacs), sizeof(int) * (myMod.ndac)); for (int i = 0; i < myMod.ndac; ++i) { diff --git a/slsSupportLib/include/sls/file_utils.h b/slsSupportLib/include/sls/file_utils.h index 1b01b2398..3f330c3b6 100644 --- a/slsSupportLib/include/sls/file_utils.h +++ b/slsSupportLib/include/sls/file_utils.h @@ -48,3 +48,7 @@ int writeDataFile(std::string fname, int nch, short int *data); // mkdir -p path implemented by recursive calls void mkdir_p(const std::string &path, std::string dir = ""); + +namespace sls { +int getFileSize(std::ifstream &ifs); +} diff --git a/slsSupportLib/src/file_utils.cpp b/slsSupportLib/src/file_utils.cpp index d23c619f1..a560e6fe0 100644 --- a/slsSupportLib/src/file_utils.cpp +++ b/slsSupportLib/src/file_utils.cpp @@ -3,6 +3,7 @@ #include "sls/sls_detector_exceptions.h" #include +#include #include #include #include @@ -92,3 +93,13 @@ void mkdir_p(const std::string &path, std::string dir) { if (i + 1 < path.length()) mkdir_p(path.substr(i + 1), dir); } + +namespace sls { +int getFileSize(std::ifstream &ifs) { + auto current_pos = ifs.tellg(); + ifs.seekg(0, std::ios::end); + int file_size = ifs.tellg(); + ifs.seekg(current_pos); + return file_size; +} +} // namespace sls diff --git a/slsSupportLib/tests/CMakeLists.txt b/slsSupportLib/tests/CMakeLists.txt index 8265c7159..75d20250c 100755 --- a/slsSupportLib/tests/CMakeLists.txt +++ b/slsSupportLib/tests/CMakeLists.txt @@ -1,5 +1,6 @@ target_sources(tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test-bit_utils.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test-file_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test-container_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test-network_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test-string_utils.cpp diff --git a/slsSupportLib/tests/test-file_utils.cpp b/slsSupportLib/tests/test-file_utils.cpp new file mode 100644 index 000000000..85c7dc9d3 --- /dev/null +++ b/slsSupportLib/tests/test-file_utils.cpp @@ -0,0 +1,28 @@ +#include "catch.hpp" +#include "sls/file_utils.h" +#include +#include +#include +#include + +TEST_CASE("Get size of empty file") { + char fname[] = "temfile_XXXXXX"; + int fh = mkstemp(fname); + std::ifstream ifs(fname); + auto size = sls::getFileSize(ifs); + REQUIRE(size == 0); +} + +TEST_CASE("Get size of file with data") { + constexpr size_t n_bytes = 137; + std::vector data(n_bytes); + char fname[] = "temfile_XXXXXX"; + int fh = mkstemp(fname); + write(fh, data.data(), n_bytes); + + std::ifstream ifs(fname); + auto size = sls::getFileSize(ifs); + REQUIRE(size == n_bytes); + REQUIRE(ifs.tellg() == 0); //getting size resets pos! +} +