mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-06 18:10:40 +02:00
Moved to class implementation
This commit is contained in:
parent
b112cf81c4
commit
5c79a1a1e8
@ -1433,13 +1433,12 @@ void init_det(py::module &m) {
|
||||
py::arg())
|
||||
.def("getDacNames", (std::vector<std::string>(Detector::*)() const) &
|
||||
Detector::getDacNames)
|
||||
.def("decodeNamedDac",
|
||||
.def("getDacIndex",
|
||||
(defs::dacIndex(Detector::*)(const std::string &)) &
|
||||
Detector::decodeNamedDac,
|
||||
Detector::getDacIndex,
|
||||
py::arg())
|
||||
.def("decodeNamedDac",
|
||||
(std::string(Detector::*)(defs::dacIndex)) &
|
||||
Detector::decodeNamedDac,
|
||||
.def("getDacName",
|
||||
(std::string(Detector::*)(defs::dacIndex)) & Detector::getDacName,
|
||||
py::arg())
|
||||
.def("setPattern",
|
||||
(void (Detector::*)(const std::string &, sls::Positions)) &
|
||||
|
@ -7,7 +7,7 @@ set(SOURCES
|
||||
src/CmdProxy.cpp
|
||||
src/CmdParser.cpp
|
||||
src/Pattern.cpp
|
||||
src/ctb_named_dacs.cpp
|
||||
src/CtbConfig.cpp
|
||||
)
|
||||
|
||||
add_library(slsDetectorObject OBJECT
|
||||
|
@ -1619,8 +1619,8 @@ class Detector {
|
||||
|
||||
std::vector<std::string> getDacNames() const;
|
||||
|
||||
defs::dacIndex decodeNamedDac(const std::string& name);
|
||||
std::string decodeNamedDac(defs::dacIndex i);
|
||||
defs::dacIndex getDacIndex(const std::string& name);
|
||||
std::string getDacName(defs::dacIndex i);
|
||||
///@}
|
||||
|
||||
/** @name Pattern */
|
||||
|
@ -1077,8 +1077,9 @@ std::string CmdProxy::Dac(int action) {
|
||||
WrongNumberOfParameters(1); // This prints slightly wrong
|
||||
|
||||
defs::dacIndex dacIndex{};
|
||||
//TODO! Remove if
|
||||
if (type == defs::CHIPTESTBOARD && !is_int(args[0])) {
|
||||
dacIndex = det->decodeNamedDac(args[0]);
|
||||
dacIndex = det->getDacIndex(args[0]);
|
||||
} else {
|
||||
dacIndex = StringTo<defs::dacIndex>(args[0]);
|
||||
}
|
||||
@ -1102,7 +1103,7 @@ std::string CmdProxy::Dac(int action) {
|
||||
|
||||
defs::dacIndex dacIndex{};
|
||||
if (type == defs::CHIPTESTBOARD && !is_int(args[0]))
|
||||
dacIndex = det->decodeNamedDac(args[0]);
|
||||
dacIndex = det->getDacIndex(args[0]);
|
||||
else
|
||||
dacIndex = StringTo<defs::dacIndex>(args[0]);
|
||||
bool mV = false;
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include "CmdProxy.h"
|
||||
#include "DetectorImpl.h"
|
||||
#include "Module.h"
|
||||
#include "ctb_named_dacs.h"
|
||||
#include "CtbConfig.h"
|
||||
#include "sls/Pattern.h"
|
||||
#include "sls/container_utils.h"
|
||||
#include "sls/file_utils.h"
|
||||
@ -22,8 +22,6 @@
|
||||
namespace sls {
|
||||
|
||||
void freeSharedMemory(int detectorIndex, int moduleIndex) {
|
||||
//
|
||||
remove_ctb_dacnames(detectorIndex);
|
||||
|
||||
// single module
|
||||
if (moduleIndex >= 0) {
|
||||
@ -48,6 +46,11 @@ void freeSharedMemory(int detectorIndex, int moduleIndex) {
|
||||
SharedMemory<sharedModule> moduleShm(detectorIndex, i);
|
||||
moduleShm.RemoveSharedMemory();
|
||||
}
|
||||
|
||||
// Ctb configuration
|
||||
SharedMemory<CtbConfig> ctbShm(detectorIndex, -1, CtbConfig::shm_tag());
|
||||
if (ctbShm.IsExisting())
|
||||
ctbShm.RemoveSharedMemory();
|
||||
}
|
||||
|
||||
using defs = slsDetectorDefs;
|
||||
@ -58,10 +61,7 @@ Detector::Detector(int shm_id)
|
||||
Detector::~Detector() = default;
|
||||
|
||||
// Configuration
|
||||
void Detector::freeSharedMemory() {
|
||||
remove_ctb_dacnames(pimpl->getDetectorIndex());
|
||||
pimpl->freeSharedMemory();
|
||||
}
|
||||
void Detector::freeSharedMemory() { pimpl->freeSharedMemory(); }
|
||||
|
||||
void Detector::loadConfig(const std::string &fname) {
|
||||
int shm_id = getShmId();
|
||||
@ -2077,36 +2077,37 @@ void Detector::setLEDEnable(bool enable, Positions pos) {
|
||||
void Detector::setDacNames(const std::vector<std::string> names) {
|
||||
if (getDetectorType().squash() != defs::CHIPTESTBOARD)
|
||||
throw RuntimeError("Named dacs only for CTB");
|
||||
set_ctb_dac_names(names, pimpl->getDetectorIndex());
|
||||
pimpl->setCtbDacNames(names);
|
||||
}
|
||||
|
||||
std::vector<std::string> Detector::getDacNames() const {
|
||||
std::vector<std::string> names;
|
||||
auto type = getDetectorType().squash();
|
||||
if (type == defs::CHIPTESTBOARD) {
|
||||
names = get_ctb_dac_names(pimpl->getDetectorIndex());
|
||||
}
|
||||
if (names.empty()) {
|
||||
for (const auto &index : getDacList())
|
||||
names.push_back(ToString(index));
|
||||
}
|
||||
if (type == defs::CHIPTESTBOARD)
|
||||
return pimpl->getCtbDacNames();
|
||||
|
||||
for (const auto &index : getDacList())
|
||||
names.push_back(ToString(index));
|
||||
return names;
|
||||
}
|
||||
|
||||
defs::dacIndex Detector::decodeNamedDac(const std::string &name) {
|
||||
auto names = getDacNames();
|
||||
auto it = std::find(names.begin(), names.end(), name);
|
||||
if (it == names.end())
|
||||
throw RuntimeError("Dacname not found");
|
||||
return static_cast<defs::dacIndex>(it - names.begin());
|
||||
defs::dacIndex Detector::getDacIndex(const std::string &name) {
|
||||
auto type = getDetectorType().squash();
|
||||
if (type == defs::CHIPTESTBOARD) {
|
||||
auto names = getDacNames();
|
||||
auto it = std::find(names.begin(), names.end(), name);
|
||||
if (it == names.end())
|
||||
throw RuntimeError("Dacname not found");
|
||||
return static_cast<defs::dacIndex>(it - names.begin());
|
||||
}
|
||||
return StringTo<defs::dacIndex>(name);
|
||||
}
|
||||
|
||||
std::string Detector::decodeNamedDac(defs::dacIndex i) {
|
||||
auto names = getDacNames();
|
||||
auto index = static_cast<size_t>(i);
|
||||
if (index >= names.size())
|
||||
throw RuntimeError("Dac index out of range");
|
||||
return names[index];
|
||||
std::string Detector::getDacName(defs::dacIndex i) {
|
||||
auto type = getDetectorType().squash();
|
||||
if (type == defs::CHIPTESTBOARD)
|
||||
return pimpl->getCtbDacName(i);
|
||||
return ToString(i);
|
||||
}
|
||||
|
||||
// Pattern
|
||||
|
@ -32,7 +32,7 @@
|
||||
namespace sls {
|
||||
|
||||
DetectorImpl::DetectorImpl(int detector_index, bool verify, bool update)
|
||||
: detectorIndex(detector_index), shm(detector_index, -1) {
|
||||
: detectorIndex(detector_index), shm(detector_index, -1),ctb_shm(detector_index, -1, CtbConfig::shm_tag()) {
|
||||
setupDetector(verify, update);
|
||||
}
|
||||
|
||||
@ -44,6 +44,9 @@ void DetectorImpl::setupDetector(bool verify, bool update) {
|
||||
if (update) {
|
||||
updateUserdetails();
|
||||
}
|
||||
|
||||
if (ctb_shm.IsExisting())
|
||||
ctb_shm.OpenSharedMemory();
|
||||
}
|
||||
|
||||
void DetectorImpl::setAcquiringFlag(bool flag) { shm()->acquiringFlag = flag; }
|
||||
@ -74,6 +77,10 @@ void DetectorImpl::freeSharedMemory(int detectorIndex, int detPos) {
|
||||
SharedMemory<sharedModule> moduleShm(detectorIndex, i);
|
||||
moduleShm.RemoveSharedMemory();
|
||||
}
|
||||
|
||||
SharedMemory<CtbConfig> ctbShm(detectorIndex, -1, CtbConfig::shm_tag());
|
||||
if (ctbShm.IsExisting())
|
||||
ctbShm.RemoveSharedMemory();
|
||||
}
|
||||
|
||||
void DetectorImpl::freeSharedMemory() {
|
||||
@ -86,6 +93,9 @@ void DetectorImpl::freeSharedMemory() {
|
||||
// clear detector shm
|
||||
shm.RemoveSharedMemory();
|
||||
client_downstream = false;
|
||||
|
||||
if(ctb_shm.IsExisting())
|
||||
ctb_shm.RemoveSharedMemory();
|
||||
}
|
||||
|
||||
std::string DetectorImpl::getUserDetails() {
|
||||
@ -144,6 +154,8 @@ void DetectorImpl::initSharedMemory(bool verify) {
|
||||
throw SharedMemoryError("Shared memory version mismatch!");
|
||||
}
|
||||
}
|
||||
|
||||
// std::cout <<
|
||||
}
|
||||
|
||||
void DetectorImpl::initializeDetectorStructure() {
|
||||
@ -254,6 +266,16 @@ void DetectorImpl::setHostname(const std::vector<std::string> &name) {
|
||||
i * numInterfaces);
|
||||
}
|
||||
}
|
||||
|
||||
//Here we know the detector type and can add ctb shared memory
|
||||
//if needed, CTB dac names are only on detector level
|
||||
|
||||
if (shm()->detType == defs::CHIPTESTBOARD){
|
||||
if(ctb_shm.IsExisting())
|
||||
ctb_shm.OpenSharedMemory();
|
||||
else
|
||||
ctb_shm.CreateSharedMemory();
|
||||
}
|
||||
}
|
||||
|
||||
void DetectorImpl::addModule(const std::string &hostname) {
|
||||
@ -1376,4 +1398,17 @@ void DetectorImpl::setDefaultDac(defs::dacIndex index, int defaultValue,
|
||||
Parallel(&Module::setDefaultDac, pos, index, defaultValue, sett);
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> DetectorImpl::getCtbDacNames() const {
|
||||
return ctb_shm()->getDacNames();
|
||||
}
|
||||
|
||||
void DetectorImpl::setCtbDacNames(const std::vector<std::string>& names){
|
||||
ctb_shm()->setDacNames(names);
|
||||
}
|
||||
|
||||
std::string DetectorImpl::getCtbDacName(defs::dacIndex i) const{
|
||||
return ctb_shm()->getDacName(static_cast<int>(i));
|
||||
}
|
||||
|
||||
} // namespace sls
|
@ -7,6 +7,8 @@
|
||||
#include "sls/logger.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
|
||||
|
||||
#include "CtbConfig.h"
|
||||
class ZmqSocket;
|
||||
class detectorData;
|
||||
|
||||
@ -299,6 +301,11 @@ class DetectorImpl : public virtual slsDetectorDefs {
|
||||
void setDefaultDac(defs::dacIndex index, int defaultValue,
|
||||
defs::detectorSettings sett, Positions pos);
|
||||
|
||||
|
||||
std::vector<std::string> getCtbDacNames() const;
|
||||
std::string getCtbDacName(defs::dacIndex i) const;
|
||||
void setCtbDacNames(const std::vector<std::string>& names);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Creates/open shared memory, initializes detector structure and members
|
||||
@ -382,6 +389,7 @@ class DetectorImpl : public virtual slsDetectorDefs {
|
||||
|
||||
const int detectorIndex{0};
|
||||
sls::SharedMemory<sharedDetector> shm{0, -1};
|
||||
sls::SharedMemory<CtbConfig> ctb_shm{0, -1, "a"};
|
||||
std::vector<std::unique_ptr<sls::Module>> modules;
|
||||
|
||||
/** data streaming (down stream) enabled in client (zmq sckets created) */
|
||||
|
@ -19,7 +19,6 @@
|
||||
#include <fcntl.h> // O_CREAT, O_TRUNC..
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
// #include <stdio.h> // printf
|
||||
#include <sys/mman.h> // shared memory
|
||||
#include <sys/stat.h> // fstat
|
||||
#include <unistd.h>
|
||||
@ -36,11 +35,9 @@ namespace sls {
|
||||
template <typename T> class SharedMemory {
|
||||
|
||||
public:
|
||||
/**
|
||||
* moduleid of -1 creates a detector only shared memory
|
||||
*/
|
||||
SharedMemory(int detectorId, int moduleIndex) {
|
||||
name = ConstructSharedMemoryName(detectorId, moduleIndex);
|
||||
//moduleid of -1 creates a detector only shared memory
|
||||
SharedMemory(int detectorId, int moduleIndex, const std::string& tag = "") {
|
||||
name = ConstructSharedMemoryName(detectorId, moduleIndex, tag);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,11 +100,7 @@ template <typename T> class SharedMemory {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shared memory name
|
||||
*/
|
||||
std::string GetName() const { return name; }
|
||||
|
||||
size_t size() const { return shmSize; }
|
||||
|
||||
/**
|
||||
@ -133,8 +126,10 @@ template <typename T> class SharedMemory {
|
||||
RemoveSharedMemory();
|
||||
throw SharedMemoryError(msg);
|
||||
}
|
||||
// int *pInt = new (buf) int(3);
|
||||
|
||||
shared_struct = MapSharedMemory();
|
||||
new (shared_struct) T{}; // is this ok?
|
||||
LOG(logINFO) << "Shared memory created " << name;
|
||||
}
|
||||
|
||||
@ -211,7 +206,7 @@ template <typename T> class SharedMemory {
|
||||
* @param moduleIndex module id, -1 if a detector shared memory
|
||||
* @returns shared memory name
|
||||
*/
|
||||
std::string ConstructSharedMemoryName(int detectorId, int moduleIndex) {
|
||||
std::string ConstructSharedMemoryName(int detectorId, int moduleIndex, const std::string& tag) {
|
||||
|
||||
// using environment path
|
||||
std::string sEnvPath;
|
||||
@ -222,8 +217,12 @@ template <typename T> class SharedMemory {
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
if (moduleIndex < 0)
|
||||
if (moduleIndex < 0){
|
||||
ss << SHM_DETECTOR_PREFIX << detectorId << sEnvPath;
|
||||
if (!tag.empty())
|
||||
ss << "_" << tag;
|
||||
}
|
||||
|
||||
else
|
||||
ss << SHM_DETECTOR_PREFIX << detectorId << SHM_MODULE_PREFIX
|
||||
<< moduleIndex << sEnvPath;
|
||||
@ -293,15 +292,10 @@ template <typename T> class SharedMemory {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Shared memory name */
|
||||
|
||||
std::string name;
|
||||
|
||||
/** File descriptor */
|
||||
int fd{-1};
|
||||
|
||||
/** shm size */
|
||||
size_t shmSize{0};
|
||||
|
||||
T *shared_struct{nullptr};
|
||||
};
|
||||
|
||||
|
@ -1,65 +0,0 @@
|
||||
|
||||
#include "SharedMemory.h"
|
||||
#include "ctb_named_dacs.h"
|
||||
#include "sls/string_utils.h"
|
||||
#include "sls/ToString.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
namespace sls {
|
||||
|
||||
std::vector<std::string> get_ctb_dac_names(int det_id) {
|
||||
std::ifstream ifs(ctb_dac_fname(det_id));
|
||||
if (!ifs)
|
||||
return {};
|
||||
|
||||
std::string dacnames;
|
||||
ifs.seekg(0, std::ios::end);
|
||||
dacnames.resize(ifs.tellg());
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
ifs.read(&dacnames[0], dacnames.size());
|
||||
|
||||
std::string chars = "[] ";
|
||||
|
||||
dacnames.erase(std::remove_if(dacnames.begin(), dacnames.end(),
|
||||
[&chars](const char &c) {
|
||||
return chars.find(c) != std::string::npos;
|
||||
}),
|
||||
dacnames.end());
|
||||
auto names = sls::split(dacnames, ',');
|
||||
return names;
|
||||
}
|
||||
|
||||
std::string ctb_dac_fname(int det_id) {
|
||||
std::string sEnvPath;
|
||||
char *envpath = getenv(SHM_ENV_NAME);
|
||||
if (envpath != nullptr) {
|
||||
sEnvPath.assign(envpath);
|
||||
sEnvPath.append("_");
|
||||
}
|
||||
sEnvPath.insert(0, "_");
|
||||
std::stringstream oss;
|
||||
oss << "/dev/shm" << SHM_DETECTOR_PREFIX << det_id << sEnvPath << "ctbdacs";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void set_ctb_dac_names(const std::vector<std::string>& names, int det_id){
|
||||
if (names.size() != 18)
|
||||
throw RuntimeError("Need to set all 18 dacs when naming dacs");
|
||||
|
||||
|
||||
std::ofstream ofs(ctb_dac_fname(det_id));
|
||||
if(!ofs)
|
||||
throw RuntimeError("Could not open dacnames file for writing");
|
||||
std::string s = sls::ToString(names);
|
||||
ofs.write(&s[0], s.size());
|
||||
}
|
||||
|
||||
|
||||
void remove_ctb_dacnames(int det_id){
|
||||
unlink(ctb_dac_fname(det_id).c_str());
|
||||
}
|
||||
|
||||
} // namespace sls
|
@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
namespace sls{
|
||||
|
||||
std::vector<std::string> get_ctb_dac_names(int det_id);
|
||||
void set_ctb_dac_names(const std::vector<std::string>& names, int det_id);
|
||||
std::string ctb_dac_fname(int det_id);
|
||||
void remove_ctb_dacnames(int det_id);
|
||||
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ target_sources(tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdParser.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-Module.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-Pattern.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_ctb_named_dacs.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-CtbConfig.cpp
|
||||
)
|
||||
|
||||
target_include_directories(tests PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src>")
|
@ -134,3 +134,31 @@ TEST_CASE("Create several shared memories", "[detector]") {
|
||||
CHECK(v[i].IsExisting() == false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Create create a shared memory with a tag"){
|
||||
SharedMemory<int> shm(0, -1, "ctbdacs");
|
||||
REQUIRE(shm.GetName() == "/slsDetectorPackage_detector_0_ctbdacs");
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Create create a shared memory with a tag when SLSDETNAME is set"){
|
||||
|
||||
// if SLSDETNAME is already set we unset it but
|
||||
// save the value
|
||||
std::string old_slsdetname;
|
||||
if (getenv(SHM_ENV_NAME))
|
||||
old_slsdetname = getenv(SHM_ENV_NAME);
|
||||
unsetenv(SHM_ENV_NAME);
|
||||
setenv(SHM_ENV_NAME, "myprefix", 1);
|
||||
|
||||
SharedMemory<int> shm(0, -1, "ctbdacs");
|
||||
REQUIRE(shm.GetName() == "/slsDetectorPackage_detector_0_myprefix_ctbdacs");
|
||||
|
||||
// Clean up after us
|
||||
if (old_slsdetname.empty())
|
||||
unsetenv(SHM_ENV_NAME);
|
||||
else
|
||||
setenv(SHM_ENV_NAME, old_slsdetname.c_str(), 1);
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,61 +0,0 @@
|
||||
#include "catch.hpp"
|
||||
#include <string>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SharedMemory.h"
|
||||
#include "ctb_named_dacs.h"
|
||||
using namespace sls;
|
||||
#include <fstream>
|
||||
TEST_CASE("Create name for shm dac file") {
|
||||
// if SLSDETNAME is already set we unset it but
|
||||
// save the value
|
||||
std::string old_slsdetname;
|
||||
if (getenv(SHM_ENV_NAME))
|
||||
old_slsdetname = getenv(SHM_ENV_NAME);
|
||||
unsetenv(SHM_ENV_NAME);
|
||||
|
||||
int det_id = 0;
|
||||
REQUIRE(ctb_dac_fname(det_id) == "/dev/shm/slsDetectorPackage_detector_0_ctbdacs");
|
||||
|
||||
setenv(SHM_ENV_NAME, "myprefix", 1);
|
||||
REQUIRE(ctb_dac_fname(det_id) ==
|
||||
"/dev/shm/slsDetectorPackage_detector_0_myprefix_ctbdacs");
|
||||
|
||||
// Clean up after us
|
||||
if (old_slsdetname.empty())
|
||||
unsetenv(SHM_ENV_NAME);
|
||||
else
|
||||
setenv(SHM_ENV_NAME, old_slsdetname.c_str(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Get ctb dac names returns an empty vector when not set"){
|
||||
int large_unlikely_number = 123203;
|
||||
auto vec = get_ctb_dac_names(large_unlikely_number);
|
||||
REQUIRE(vec.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Remove file fails silently when file is not present"){
|
||||
int large_unlikely_number = 123203;
|
||||
REQUIRE_NOTHROW(remove_ctb_dacnames(large_unlikely_number));
|
||||
}
|
||||
|
||||
TEST_CASE("Read dacs from file then remove file"){
|
||||
int large_unlikely_number = 998765;
|
||||
std::ofstream out(ctb_dac_fname(large_unlikely_number));
|
||||
std::string names = "[first, second, third]";
|
||||
out.write(&names[0], names.size());
|
||||
out.close();
|
||||
|
||||
auto dacnames = get_ctb_dac_names(large_unlikely_number);
|
||||
REQUIRE(dacnames.size() == 3);
|
||||
REQUIRE(dacnames[0] == "first");
|
||||
REQUIRE(dacnames[1] == "second");
|
||||
REQUIRE(dacnames[2] == "third");
|
||||
|
||||
remove_ctb_dacnames(large_unlikely_number);
|
||||
|
||||
std::ifstream in(ctb_dac_fname(large_unlikely_number));
|
||||
REQUIRE_FALSE(in);
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user