This commit is contained in:
Erik Frojdh 2021-03-31 16:26:36 +02:00
parent fa25340e5c
commit 01c785271f
9 changed files with 71 additions and 74 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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<char *>(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<char *>(&myMod.reg),
sizeof(myMod.reg));
infile.read(reinterpret_cast<char *>(myMod.dacs),
sizeof(int) * (myMod.ndac));
for (int i = 0; i < myMod.ndac; ++i) {

View File

@ -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);
}

View File

@ -3,6 +3,7 @@
#include "sls/sls_detector_exceptions.h"
#include <errno.h>
#include <ios>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
@ -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

View File

@ -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

View File

@ -0,0 +1,28 @@
#include "catch.hpp"
#include "sls/file_utils.h"
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <vector>
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<char> 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!
}