removed pointers added constructor

This commit is contained in:
Erik Frojdh 2019-04-17 16:19:18 +02:00
parent b46e107f35
commit ee20cfab1e
4 changed files with 83 additions and 68 deletions

View File

@ -1321,7 +1321,7 @@ class slsDetector : public virtual slsDetectorDefs{
* @returns ok or fail * @returns ok or fail
* \sa ::sls_detector_module * \sa ::sls_detector_module
*/ */
int setModule(sls_detector_module module, int tb = 1); int setModule(sls_detector_module& module, int tb = 1);
/** /**
* Get module structure from detector (all detectors) * Get module structure from detector (all detectors)
@ -1805,7 +1805,7 @@ class slsDetector : public virtual slsDetectorDefs{
* @param tb 1 to include trimbits, 0 to exclude (used for eiger) * @param tb 1 to include trimbits, 0 to exclude (used for eiger)
* @returns the pointer to the module structure with interpolated values or NULL if error * @returns the pointer to the module structure with interpolated values or NULL if error
*/ */
sls_detector_module *interpolateTrim( sls_detector_module interpolateTrim(
sls_detector_module *a, sls_detector_module *b, const int energy, sls_detector_module *a, sls_detector_module *b, const int energy,
const int e1, const int e2, int tb = 1); const int e1, const int e2, int tb = 1);

View File

@ -1280,10 +1280,7 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV,
break; break;
} }
} }
sls_detector_module myMod{detector_shm()->myDetectorType};
// fill detector module structure
sls_detector_module *myMod = nullptr;
// normal // normal
if (interpolate == false) { if (interpolate == false) {
// find their directory names // find their directory names
@ -1294,13 +1291,7 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV,
std::string settingsfname = ostfn.str(); std::string settingsfname = ostfn.str();
FILE_LOG(logDEBUG1) << "Settings File is " << settingsfname; FILE_LOG(logDEBUG1) << "Settings File is " << settingsfname;
// read the files if (readSettingsFile(settingsfname, &myMod, tb) == nullptr) {
// myMod = createModule(); // readSettings also checks if create module
// is null
if (readSettingsFile(settingsfname, myMod, tb) == nullptr) {
if (myMod != nullptr) {
deleteModule(myMod);
}
return FAIL; return FAIL;
} }
}else{ }else{
@ -1330,51 +1321,33 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV,
// read the files // read the files
FILE_LOG(logDEBUG1) << "Settings Files are " << settingsfname1 FILE_LOG(logDEBUG1) << "Settings Files are " << settingsfname1
<< " and " << settingsfname2; << " and " << settingsfname2;
sls_detector_module *myMod1 = createModule(); sls_detector_module myMod1{detector_shm()->myDetectorType};
sls_detector_module *myMod2 = createModule(); sls_detector_module myMod2{detector_shm()->myDetectorType};
if (nullptr == readSettingsFile(settingsfname1, myMod1, tb)) { if (readSettingsFile(settingsfname1, &myMod1, tb) == nullptr) {
deleteModule(myMod1);
deleteModule(myMod2);
throw RuntimeError( throw RuntimeError(
"setThresholdEnergyAndSettings: Could not open settings file"); "setThresholdEnergyAndSettings: Could not open settings file");
} }
if (nullptr == readSettingsFile(settingsfname2, myMod2, tb)) { if (readSettingsFile(settingsfname2, &myMod2, tb) == nullptr) {
deleteModule(myMod1);
deleteModule(myMod2);
throw RuntimeError( throw RuntimeError(
"setThresholdEnergyAndSettings: Could not open settings file"); "setThresholdEnergyAndSettings: Could not open settings file");
} }
if (myMod1->iodelay != myMod2->iodelay) { if (myMod1.iodelay != myMod2.iodelay) {
deleteModule(myMod1);
deleteModule(myMod2);
throw RuntimeError("setThresholdEnergyAndSettings: Iodelays do not " throw RuntimeError("setThresholdEnergyAndSettings: Iodelays do not "
"match between files"); "match between files");
} }
// interpolate module // interpolate module
myMod = interpolateTrim(myMod1, myMod2, e_eV, trim1, trim2, tb); myMod = interpolateTrim(&myMod1, &myMod2, e_eV, trim1, trim2, tb);
if (myMod == nullptr) {
deleteModule(myMod1);
deleteModule(myMod2);
throw RuntimeError("setThresholdEnergyAndSettings: Could not "
"interpolate, different "
"dac values in files");
}
// interpolate tau
myMod->iodelay = myMod1->iodelay;
myMod->tau =
linearInterpolation(e_eV, trim1, trim2, myMod1->tau, myMod2->tau);
// printf("new tau:%d\n",tau);
deleteModule(myMod1); // // interpolate tau
deleteModule(myMod2); myMod.iodelay = myMod1.iodelay;
myMod.tau =
linearInterpolation(e_eV, trim1, trim2, myMod1.tau, myMod2.tau);
} }
myMod->reg = detector_shm()->currentSettings; myMod.reg = detector_shm()->currentSettings;
myMod->eV = e_eV; myMod.eV = e_eV;
setModule(*myMod, tb); setModule(myMod, tb);
deleteModule(myMod);
if (getSettings() != is) { if (getSettings() != is) {
throw RuntimeError("setThresholdEnergyAndSettings: Could not set " throw RuntimeError("setThresholdEnergyAndSettings: Could not set "
"settings in detector"); "settings in detector");
@ -3670,7 +3643,7 @@ int slsDetector::getChanRegs(double *retval) {
return n; return n;
} }
int slsDetector::setModule(sls_detector_module module, int tb) { int slsDetector::setModule(sls_detector_module& module, int tb) {
int fnum = F_SET_MODULE; int fnum = F_SET_MODULE;
int ret = FAIL; int ret = FAIL;
int retval = -1; int retval = -1;
@ -4810,7 +4783,7 @@ int slsDetector::setDigitalIODelay(uint64_t pinMask, int delay) {
return ret; return ret;
} }
sls_detector_module * sls_detector_module
slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b, slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b,
const int energy, const int e1, const int e2, const int energy, const int e1, const int e2,
int tb) { int tb) {
@ -4821,10 +4794,7 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b,
"Interpolation of Trim values not implemented for this detector!"); "Interpolation of Trim values not implemented for this detector!");
} }
sls_detector_module *myMod = createModule(detector_shm()->myDetectorType); sls_detector_module myMod{detector_shm()->myDetectorType};
if (myMod == nullptr) {
throw RuntimeError("Could not create module");
}
enum eiger_DacIndex { enum eiger_DacIndex {
SVP, SVP,
VTR, VTR,
@ -4849,10 +4819,9 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b,
int num_dacs_to_copy = sizeof(dacs_to_copy) / sizeof(dacs_to_copy[0]); int num_dacs_to_copy = sizeof(dacs_to_copy) / sizeof(dacs_to_copy[0]);
for (int i = 0; i < num_dacs_to_copy; ++i) { for (int i = 0; i < num_dacs_to_copy; ++i) {
if (a->dacs[dacs_to_copy[i]] != b->dacs[dacs_to_copy[i]]) { if (a->dacs[dacs_to_copy[i]] != b->dacs[dacs_to_copy[i]]) {
deleteModule(myMod); throw RuntimeError("Interpolate module: dacs different");
return nullptr;
} }
myMod->dacs[dacs_to_copy[i]] = a->dacs[dacs_to_copy[i]]; myMod.dacs[dacs_to_copy[i]] = a->dacs[dacs_to_copy[i]];
} }
// Copy irrelevant dacs (without failing): CAL // Copy irrelevant dacs (without failing): CAL
@ -4864,7 +4833,7 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b,
"Taking first: " "Taking first: "
<< a->dacs[CAL]; << a->dacs[CAL];
} }
myMod->dacs[CAL] = a->dacs[CAL]; myMod.dacs[CAL] = a->dacs[CAL];
// Interpolate vrf, vcmp, vcp // Interpolate vrf, vcmp, vcp
int dacs_to_interpolate[] = {VRF, VCMP_LL, VCMP_LR, VCMP_RL, int dacs_to_interpolate[] = {VRF, VCMP_LL, VCMP_LR, VCMP_RL,
@ -4872,15 +4841,15 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b,
int num_dacs_to_interpolate = int num_dacs_to_interpolate =
sizeof(dacs_to_interpolate) / sizeof(dacs_to_interpolate[0]); sizeof(dacs_to_interpolate) / sizeof(dacs_to_interpolate[0]);
for (int i = 0; i < num_dacs_to_interpolate; ++i) { for (int i = 0; i < num_dacs_to_interpolate; ++i) {
myMod->dacs[dacs_to_interpolate[i]] = myMod.dacs[dacs_to_interpolate[i]] =
linearInterpolation(energy, e1, e2, a->dacs[dacs_to_interpolate[i]], linearInterpolation(energy, e1, e2, a->dacs[dacs_to_interpolate[i]],
b->dacs[dacs_to_interpolate[i]]); b->dacs[dacs_to_interpolate[i]]);
} }
// Interpolate all trimbits // Interpolate all trimbits
if (tb != 0) { if (tb != 0) {
for (int i = 0; i < myMod->nchan; ++i) { for (int i = 0; i < myMod.nchan; ++i) {
myMod->chanregs[i] = linearInterpolation( myMod.chanregs[i] = linearInterpolation(
energy, e1, e2, a->chanregs[i], b->chanregs[i]); energy, e1, e2, a->chanregs[i], b->chanregs[i]);
} }
} }

View File

@ -16,6 +16,7 @@
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
#include "sls_detector_exceptions.h" #include "sls_detector_exceptions.h"
#include <algorithm>
#include <bitset> #include <bitset>
#include <string> #include <string>
#endif #endif
@ -1043,18 +1044,43 @@ typedef struct {
sls_detector_module() sls_detector_module()
: serialnumber(0), nchan(0), nchip(0), ndac(0), reg(0), iodelay(0), : serialnumber(0), nchan(0), nchip(0), ndac(0), reg(0), iodelay(0),
tau(0), eV(0), dacs(nullptr), chanregs(nullptr) {} tau(0), eV(0), dacs(nullptr), chanregs(nullptr) {}
sls_detector_module(slsDetectorDefs::detectorType type) { sls_detector_module(slsDetectorDefs::detectorType type) {
detParameters parameters{type}; detParameters parameters{type};
int nch = parameters.nChanX * parameters.nChanY; int nch = parameters.nChanX * parameters.nChanY;
int nc = parameters.nChipX * parameters.nChipY; int nc = parameters.nChipX * parameters.nChipY;
int nd = parameters.nDacs; // int nd = parameters.nDacs;
ndac = nd; ndac = parameters.nDacs;
nchip = nc; nchip = nc;
nchan = nch * nc; nchan = nch * nc;
dacs = new int[nd]; dacs = new int[ndac];
chanregs = new int[nch * nc]; chanregs = new int[nchan];
} }
sls_detector_module(const sls_detector_module &other)
: dacs(nullptr), chanregs(nullptr) {
*this = other;
}
sls_detector_module &operator=(const sls_detector_module &other) {
delete[] dacs;
delete[] chanregs;
serialnumber = other.serialnumber;
nchan = other.nchan;
nchip = other.nchip;
ndac = other.ndac;
reg = other.reg;
iodelay = other.iodelay;
tau = other.tau;
eV = other.eV;
dacs = new int[ndac];
std::copy(other.dacs, other.dacs + ndac, dacs);
chanregs = new int[nchan];
std::copy(other.chanregs, other.chanregs + nchan, chanregs);
return *this;
}
~sls_detector_module() { ~sls_detector_module() {
delete[] dacs; delete[] dacs;
delete[] chanregs; delete[] chanregs;

View File

@ -1,6 +1,6 @@
#include "catch.hpp" #include "catch.hpp"
#include "sls_detector_defs.h"
#include "slsDetector.h" #include "slsDetector.h"
#include "sls_detector_defs.h"
using dt = slsDetectorDefs::detectorType; using dt = slsDetectorDefs::detectorType;
@ -18,7 +18,7 @@ TEST_CASE("sls_detector_module default construction", "[support][new]") {
CHECK(m.chanregs == nullptr); CHECK(m.chanregs == nullptr);
} }
TEST_CASE("sls_detector_module from type", "[support][new]") { TEST_CASE("sls_detector_module from type", "[support]") {
sls_detector_module m(dt::EIGER); sls_detector_module m(dt::EIGER);
CHECK(m.serialnumber == 0); CHECK(m.serialnumber == 0);
CHECK(m.nchan == 256 * 256 * 4); CHECK(m.nchan == 256 * 256 * 4);
@ -31,3 +31,23 @@ TEST_CASE("sls_detector_module from type", "[support][new]") {
CHECK(m.dacs != nullptr); CHECK(m.dacs != nullptr);
CHECK(m.chanregs != nullptr); CHECK(m.chanregs != nullptr);
} }
TEST_CASE("assign module", "[support]") {
sls_detector_module m0;
sls_detector_module m1(dt::EIGER);
m1.serialnumber = 14;
m1.reg = 500;
m1.iodelay = 750;
m0 = m1; // Assignment operator
CHECK(m0.serialnumber == 14);
CHECK(m0.reg == 500);
CHECK(m0.iodelay == 750);
CHECK(m0.nchan == 256 * 256 * 4);
auto m3 = m1; // Copy constructor
CHECK(m3.serialnumber == 14);
CHECK(m3.reg == 500);
CHECK(m3.iodelay == 750);
CHECK(m3.nchan == 256 * 256 * 4);
}