From 1008944a6a77c2c63a2831a00de3e3f81c2e53b5 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Mon, 12 Aug 2019 16:34:59 +0200 Subject: [PATCH] ROI workaroud --- slsDetectorSoftware/include/Result.h | 5 + slsDetectorSoftware/src/Detector.cpp | 306 ++++++++++++---------- slsDetectorSoftware/tests/test-Result.cpp | 16 ++ 3 files changed, 189 insertions(+), 138 deletions(-) diff --git a/slsDetectorSoftware/include/Result.h b/slsDetectorSoftware/include/Result.h index 9d5f2ae7e..54d19b1a5 100644 --- a/slsDetectorSoftware/include/Result.h +++ b/slsDetectorSoftware/include/Result.h @@ -98,6 +98,11 @@ template > class Result { vec.push_back(std::forward(value)); } + template + auto emplace_back(Args &&... args) -> decltype(vec.emplace_back(args...)){ + vec.emplace_back(std::forward(args)...); + } + auto operator[](size_type pos) -> decltype(vec[pos]) { return vec[pos]; } const_reference operator[](size_type pos) const { return vec[pos]; } diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 61e276d89..227670a06 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -41,10 +41,6 @@ void Detector::setConfig(const std::string &fname) { pimpl->readConfigurationFile(fname); } - - - - Result Detector::getStartingFrameNumber(Positions pos) const { return pimpl->Parallel(&slsDetector::getStartingFrameNumber, pos); } @@ -146,7 +142,10 @@ defs::coordinates Detector::getNumberOfDetectors() const { } Result Detector::getNumberOfChannels(Positions pos) const { - if (pos.empty() || (pos.size() == 1 && pos[0] == -1)) {// TODO: also check condition that pos.size == pimpl->size()?? for other occurences as well + if (pos.empty() || + (pos.size() == 1 && + pos[0] == -1)) { // TODO: also check condition that pos.size == + // pimpl->size()?? for other occurences as well return {pimpl->getNumberOfChannels()}; } return pimpl->Parallel(&slsDetector::getNumberOfChannels, pos); @@ -688,88 +687,99 @@ void Detector::setDAC(int value, defs::dacIndex index, bool mV, Positions pos) { pimpl->Parallel(&slsDetector::setDAC, pos, value, index, mV); } - Result Detector::getTimingMode(Positions pos) const { - return pimpl->Parallel(&slsDetector::setExternalCommunicationMode, pos, defs::GET_EXTERNAL_COMMUNICATION_MODE); - } +Result +Detector::getTimingMode(Positions pos) const { + return pimpl->Parallel(&slsDetector::setExternalCommunicationMode, pos, + defs::GET_EXTERNAL_COMMUNICATION_MODE); +} - void Detector::setTimingMode(defs::externalCommunicationMode value, Positions pos) { - pimpl->Parallel(&slsDetector::setExternalCommunicationMode, pos, value); - } +void Detector::setTimingMode(defs::externalCommunicationMode value, + Positions pos) { + pimpl->Parallel(&slsDetector::setExternalCommunicationMode, pos, value); +} - Result Detector::getExternalSignalFlags(Positions pos) const { - return pimpl->Parallel(&slsDetector::setExternalSignalFlags, pos, defs::GET_EXTERNAL_SIGNAL_FLAG); - } +Result +Detector::getExternalSignalFlags(Positions pos) const { + return pimpl->Parallel(&slsDetector::setExternalSignalFlags, pos, + defs::GET_EXTERNAL_SIGNAL_FLAG); +} - void Detector::setExternalSignalFlags(defs::externalSignalFlag value, Positions pos) { - pimpl->Parallel(&slsDetector::setExternalSignalFlags, pos, value); - } +void Detector::setExternalSignalFlags(defs::externalSignalFlag value, + Positions pos) { + pimpl->Parallel(&slsDetector::setExternalSignalFlags, pos, value); +} - Result Detector::getParallelMode(Positions pos) const { - auto res = pimpl->Parallel(&slsDetector::setReadOutFlags, pos, defs::GET_READOUT_FLAGS); - Result booleanRes; - for (unsigned int i = 0; i < res.size(); ++i) { - booleanRes[i] = (res[i] & defs::PARALLEL) ? true : false; +Result Detector::getParallelMode(Positions pos) const { + auto res = pimpl->Parallel(&slsDetector::setReadOutFlags, pos, + defs::GET_READOUT_FLAGS); + Result booleanRes; + for (unsigned int i = 0; i < res.size(); ++i) { + booleanRes[i] = (res[i] & defs::PARALLEL) ? true : false; + } + return booleanRes; +} + +void Detector::setParallelMode(bool value, Positions pos) { + pimpl->Parallel(&slsDetector::setReadOutFlags, pos, + value ? defs::PARALLEL : defs::NONPARALLEL); +} + +Result Detector::getOverFlowMode(Positions pos) const { + auto res = pimpl->Parallel(&slsDetector::setReadOutFlags, pos, + defs::GET_READOUT_FLAGS); + Result booleanRes; + for (unsigned int i = 0; i < res.size(); ++i) { + booleanRes[i] = (res[i] & defs::SHOW_OVERFLOW) ? true : false; + } + return booleanRes; +} + +void Detector::setOverFlowMode(bool value, Positions pos) { + pimpl->Parallel(&slsDetector::setReadOutFlags, pos, + value ? defs::SHOW_OVERFLOW : defs::NOOVERFLOW); +} + +Result Detector::getSignalType(Positions pos) const { + auto res = pimpl->Parallel(&slsDetector::setReadOutFlags, pos, + defs::GET_READOUT_FLAGS); + for (auto &it : res) { + if (it & defs::ANALOG_AND_DIGITAL) { + it = 2; + } else if (it & defs::DIGITAL_ONLY) { + it = 1; + } else if (it == defs::NORMAL_READOUT) { + it = 0; + } else { + throw RuntimeError("Unknown Signal Type"); } - return booleanRes; } + return res; +} - void Detector::setParallelMode(bool value, Positions pos) { - pimpl->Parallel(&slsDetector::setReadOutFlags, pos, value ? defs::PARALLEL : defs::NONPARALLEL); - } - - Result Detector::getOverFlowMode(Positions pos) const { - auto res = pimpl->Parallel(&slsDetector::setReadOutFlags, pos, defs::GET_READOUT_FLAGS); - Result booleanRes; - for (unsigned int i = 0; i < res.size(); ++i) { - booleanRes[i] = (res[i] & defs::SHOW_OVERFLOW) ? true : false; - } - return booleanRes; - } - - void Detector::setOverFlowMode(bool value, Positions pos) { - pimpl->Parallel(&slsDetector::setReadOutFlags, pos, value ? defs::SHOW_OVERFLOW : defs::NOOVERFLOW); - } - - Result Detector::getSignalType(Positions pos) const { - auto res = pimpl->Parallel(&slsDetector::setReadOutFlags, pos, defs::GET_READOUT_FLAGS); - for (auto &it : res) { - if (it & defs::ANALOG_AND_DIGITAL) { - it = 2; - } else if (it & defs::DIGITAL_ONLY) { - it = 1; - } else if (it == defs::NORMAL_READOUT) { - it = 0; - } else { - throw RuntimeError("Unknown Signal Type"); - } - } - return res; - } - - void Detector::setSignalType(int value, Positions pos) { - defs::readOutFlags flag; - switch (value) { - case 0: - flag = defs::NORMAL_READOUT; - break; - case 1: - flag = defs::DIGITAL_ONLY; - break; - case 2: - flag = defs::ANALOG_AND_DIGITAL; - break; - default: - throw RuntimeError("Unknown Signal Type"); - } - pimpl->Parallel(&slsDetector::setReadOutFlags, pos, flag); +void Detector::setSignalType(int value, Positions pos) { + defs::readOutFlags flag; + switch (value) { + case 0: + flag = defs::NORMAL_READOUT; + break; + case 1: + flag = defs::DIGITAL_ONLY; + break; + case 2: + flag = defs::ANALOG_AND_DIGITAL; + break; + default: + throw RuntimeError("Unknown Signal Type"); } + pimpl->Parallel(&slsDetector::setReadOutFlags, pos, flag); +} // Erik Result Detector::getInterruptSubframe(Positions pos) const { return pimpl->Parallel(&slsDetector::getInterruptSubframe, pos); } -void Detector::setInterruptSubframe(const bool enable, Positions pos){ +void Detector::setInterruptSubframe(const bool enable, Positions pos) { pimpl->Parallel(&slsDetector::setInterruptSubframe, pos, enable); } @@ -881,7 +891,8 @@ Result Detector::getNumberofUDPInterfaces(Positions pos) const { void Detector::setNumberofUDPInterfaces(int n, Positions pos) { bool previouslyClientStreaming = getDataStreamingToClient(); - bool previouslyReceiverStreaming = getDataStreamingFromReceiver(pos).squash(false); + bool previouslyReceiverStreaming = + getDataStreamingFromReceiver(pos).squash(false); pimpl->Parallel(&slsDetector::setNumberofUDPInterfaces, pos, n); // redo the zmq sockets if enabled if (previouslyClientStreaming) { @@ -908,7 +919,8 @@ Result Detector::getClientStreamingPort(Positions pos) const { void Detector::setClientDataStreamingInPort(int port, Positions pos) { if (pos.size() > 1 && pos.size() < pimpl->size()) { - throw RuntimeError("Cannot set client streaming port to a subset of modules"); + throw RuntimeError( + "Cannot set client streaming port to a subset of modules"); } pimpl->setClientDataStreamingInPort(port, pos.empty() ? -1 : pos[0]); } @@ -919,9 +931,10 @@ Result Detector::getReceiverStreamingPort(Positions pos) const { void Detector::setReceiverDataStreamingOutPort(int port, Positions pos) { if (pos.size() > 1 && pos.size() < pimpl->size()) { - throw RuntimeError("Cannot set receiver streaming port to a subset of modules"); + throw RuntimeError( + "Cannot set receiver streaming port to a subset of modules"); } - pimpl->setReceiverDataStreamingOutPort(port, pos.empty() ? -1 : pos[0]); + pimpl->setReceiverDataStreamingOutPort(port, pos.empty() ? -1 : pos[0]); } Result Detector::getClientStreamingIP(Positions pos) const { @@ -930,7 +943,7 @@ Result Detector::getClientStreamingIP(Positions pos) const { void Detector::setClientDataStreamingInIP(const std::string &ip, Positions pos) { - bool previouslyClientStreaming = getDataStreamingToClient(); + bool previouslyClientStreaming = getDataStreamingToClient(); // TODO! probably in one call ?? pimpl->Parallel(&slsDetector::setClientStreamingIP, pos, ip); if (previouslyClientStreaming) { @@ -945,7 +958,8 @@ Result Detector::getReceiverStreamingIP(Positions pos) const { void Detector::setReceiverDataStreamingOutIP(const std::string &ip, Positions pos) { - bool previouslyReceiverStreaming = getDataStreamingFromReceiver(pos).squash(false); + bool previouslyReceiverStreaming = + getDataStreamingFromReceiver(pos).squash(false); // TODO! probably in one call pimpl->Parallel(&slsDetector::setReceiverStreamingIP, pos, ip); if (previouslyReceiverStreaming) { @@ -1014,71 +1028,87 @@ void Detector::setAdditionalJsonParameter(const std::string &key, pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, key, value); } -Result Detector::getDetectorMinMaxEnergyThreshold(const bool isEmax, Positions pos) const { - auto res = pimpl->Parallel(&slsDetector::getAdditionalJsonParameter, pos, isEmax ? "emax" : "emin"); +Result Detector::getDetectorMinMaxEnergyThreshold(const bool isEmax, + Positions pos) const { + auto res = pimpl->Parallel(&slsDetector::getAdditionalJsonParameter, pos, + isEmax ? "emax" : "emin"); Result intResult; - try{ + try { for (unsigned int i = 0; i < res.size(); ++i) { intResult[i] = stoi(res[i]); } - } catch(...) { - throw RuntimeError("Cannot find or convert emin/emax string to integer"); + } catch (...) { + throw RuntimeError( + "Cannot find or convert emin/emax string to integer"); } return intResult; } -void Detector::setDetectorMinMaxEnergyThreshold(const bool isEmax, const int value, Positions pos) { - pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, isEmax ? "emax" : "emin", std::to_string(value)); +void Detector::setDetectorMinMaxEnergyThreshold(const bool isEmax, + const int value, + Positions pos) { + pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, + isEmax ? "emax" : "emin", std::to_string(value)); } Result Detector::getFrameMode(Positions pos) const { - auto res = pimpl->Parallel(&slsDetector::getAdditionalJsonParameter, pos, "frameMode"); + auto res = pimpl->Parallel(&slsDetector::getAdditionalJsonParameter, pos, + "frameMode"); Result intResult; - try{ + try { for (unsigned int i = 0; i < res.size(); ++i) { intResult[i] = defs::getFrameModeType(res[i]); } - } catch(...) { - throw RuntimeError("Cannot find or convert frameMode string to integer"); + } catch (...) { + throw RuntimeError( + "Cannot find or convert frameMode string to integer"); } return intResult; } void Detector::setFrameMode(defs::frameModeType value, Positions pos) { - pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, "frameMode", defs::getFrameModeType(value)); + pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, "frameMode", + defs::getFrameModeType(value)); } Result Detector::getDetectorMode(Positions pos) const { - auto res = pimpl->Parallel(&slsDetector::getAdditionalJsonParameter, pos, "detectorMode"); + auto res = pimpl->Parallel(&slsDetector::getAdditionalJsonParameter, pos, + "detectorMode"); Result intResult; - try{ + try { for (unsigned int i = 0; i < res.size(); ++i) { intResult[i] = defs::getDetectorModeType(res[i]); } - } catch(...) { - throw RuntimeError("Cannot find or convert detectorMode string to integer"); + } catch (...) { + throw RuntimeError( + "Cannot find or convert detectorMode string to integer"); } return intResult; } void Detector::setDetectorMode(defs::detectorModeType value, Positions pos) { - pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, "detectorMode", defs::getDetectorModeType(value)); + pimpl->Parallel(&slsDetector::setAdditionalJsonParameter, pos, + "detectorMode", defs::getDetectorModeType(value)); } Result Detector::getDigitalTestBit(Positions pos) { - return pimpl->Parallel(&slsDetector::digitalTest, pos, defs::DIGITAL_BIT_TEST, -1); + return pimpl->Parallel(&slsDetector::digitalTest, pos, + defs::DIGITAL_BIT_TEST, -1); } Result Detector::setDigitalTestBit(int value, Positions pos) { - return pimpl->Parallel(&slsDetector::digitalTest, pos, defs::DIGITAL_BIT_TEST, value); + return pimpl->Parallel(&slsDetector::digitalTest, pos, + defs::DIGITAL_BIT_TEST, value); } Result Detector::executeFirmwareTest(Positions pos) { - return pimpl->Parallel(&slsDetector::digitalTest, pos, defs::DETECTOR_FIRMWARE_TEST, -1); + return pimpl->Parallel(&slsDetector::digitalTest, pos, + defs::DETECTOR_FIRMWARE_TEST, -1); } Result Detector::executeBusTest(Positions pos) { - return pimpl->Parallel(&slsDetector::digitalTest, pos, defs::DETECTOR_BUS_TEST, -1); + return pimpl->Parallel(&slsDetector::digitalTest, pos, + defs::DETECTOR_BUS_TEST, -1); } void Detector::loadDarkImage(const std::string &fname, Positions pos) { @@ -1089,7 +1119,8 @@ void Detector::loadDarkImage(const std::string &fname, Positions pos) { if (pos.size() > 1) { throw RuntimeError("Cannot load dark image on a subset of modules"); } - pimpl->Parallel(&slsDetector::loadImageToDetector, pos, defs::DARK_IMAGE, fname); + pimpl->Parallel(&slsDetector::loadImageToDetector, pos, defs::DARK_IMAGE, + fname); } void Detector::loadGainImage(const std::string &fname, Positions pos) { @@ -1100,21 +1131,26 @@ void Detector::loadGainImage(const std::string &fname, Positions pos) { if (pos.size() > 1) { throw RuntimeError("Cannot load gain image on a subset of modules"); } - pimpl->Parallel(&slsDetector::loadImageToDetector, pos, defs::GAIN_IMAGE, fname); + pimpl->Parallel(&slsDetector::loadImageToDetector, pos, defs::GAIN_IMAGE, + fname); } -void Detector::getCounterMemoryBlock(const std::string &fname, bool startACQ, Positions pos) { +void Detector::getCounterMemoryBlock(const std::string &fname, bool startACQ, + Positions pos) { if (pos.empty() || (pos.size() == 1 && pos[0] == -1)) { pimpl->writeCounterBlockFile(fname, static_cast(startACQ), -1); } if (pos.size() > 1) { - throw RuntimeError("Cannot load get counter memory block on a subset of modules"); + throw RuntimeError( + "Cannot load get counter memory block on a subset of modules"); } - pimpl->Parallel(&slsDetector::writeCounterBlockFile, pos, fname, static_cast(startACQ)); + pimpl->Parallel(&slsDetector::writeCounterBlockFile, pos, fname, + static_cast(startACQ)); } void Detector::resetCounterBlock(bool startACQ, Positions pos) { - pimpl->Parallel(&slsDetector::resetCounterBlock, pos, static_cast(startACQ)); + pimpl->Parallel(&slsDetector::resetCounterBlock, pos, + static_cast(startACQ)); } Result Detector::getCounterBit(Positions pos) const { @@ -1126,29 +1162,38 @@ void Detector::setCounterBit(bool value, Positions pos) { } Result> Detector::getROI(Positions pos) const { - int n = 0; - if (pos.empty() || (pos.size() == 1 && pos[0] == -1)) { - auto res = pimpl->getROI(n, -1); - std::vector arrayRes(n); - std::copy_n(res, n * sizeof(defs::ROI), arrayRes.begin()); - return arrayRes; - } else if (pos.size() > 1) { - throw RuntimeError("Cannot get roi from a subset of modules"); - } else { - auto res = pimpl->Parallel(&slsDetector::getROI, pos, n); - std::vector arrayRes(n); - std::copy_n(res, n * sizeof(defs::ROI), arrayRes.begin()); - return arrayRes; + //vector holding module_id for the modules that should be read + std::vector id_vec = [&]() { + if (pos.empty() || (pos.size() == 1 && pos[0] == -1)){ + std::vector tmp; + for(size_t i=0; i!= pimpl->size(); ++i) + tmp.push_back(i); + return tmp; + }else{ + return pos; + } + }(); + + //values to return + Result> res; + + //for each detector id get the ROI + for (const auto& i :id_vec){ + int n = 0; + auto ptr = pimpl->getROI(n, i); + res.emplace_back(ptr, ptr+n); } + return res; } void Detector::setROI(std::vector value, Positions pos) { if (pos.empty() || (pos.size() == 1 && pos[0] == -1)) { pimpl->setROI(static_cast(value.size()), value.data(), -1); } else if (pos.size() > 1) { - throw RuntimeError("Cannot set roi to a subset of modules"); + throw RuntimeError("Cannot set roi to a subset of modules"); } else { - pimpl->Parallel(&slsDetector::setROI, pos, static_cast(value.size()), value.data()); + pimpl->Parallel(&slsDetector::setROI, pos, + static_cast(value.size()), value.data()); } } @@ -1204,12 +1249,6 @@ void Detector::writeAdcRegister(uint32_t addr, uint32_t value, Positions pos) { pimpl->Parallel(&slsDetector::writeAdcRegister, pos, addr, value); } - - - - - - Result Detector::getReceiverUDPSocketBufferSize(Positions pos) const { return pimpl->Parallel(&slsDetector::getReceiverUDPSocketBufferSize, pos); } @@ -1226,9 +1265,6 @@ Detector::getReceiverRealUDPSocketBufferSize(Positions pos) const { pos); } - - - Result Detector::getFramesCaughtByReceiver(Positions pos) const { return pimpl->Parallel(&slsDetector::getFramesCaughtByReceiver, pos); } @@ -1266,7 +1302,8 @@ void Detector::setDataStreamingToClient(bool enable) { } Result Detector::getDataStreamingFromReceiver(Positions pos) const { - return pimpl->Parallel(&slsDetector::enableDataStreamingFromReceiver, pos, -1); + return pimpl->Parallel(&slsDetector::enableDataStreamingFromReceiver, pos, + -1); } void Detector::setDataStreamingFromReceiver(bool enable, Positions pos) { @@ -1589,11 +1626,4 @@ Result Detector::getActive(Positions pos) const { return pimpl->Parallel(&slsDetector::activate, pos, -1); } - - - - - - - } // namespace sls \ No newline at end of file diff --git a/slsDetectorSoftware/tests/test-Result.cpp b/slsDetectorSoftware/tests/test-Result.cpp index 352df0f68..06c2f10e5 100644 --- a/slsDetectorSoftware/tests/test-Result.cpp +++ b/slsDetectorSoftware/tests/test-Result.cpp @@ -134,3 +134,19 @@ TEST_CASE("Convert from Result to Result") { REQUIRE(res2[1] == ns(50)); REQUIRE(res2[2] == ns(236)); } + + +TEST_CASE("Result of vectors"){ + using VecVec = std::vector>; + VecVec vecvec{{1,2,3}, {4,5,6}}; + Result res{vecvec}; +} + +TEST_CASE("emplace back"){ + std::vector vec{1,2,3,4,5}; + Result> res; + res.emplace_back(vec.begin(), vec.end()); + REQUIRE(res.size() == 1); + REQUIRE(res[0].size() == 5); + REQUIRE(res[0] == vec); +} \ No newline at end of file