Merge branch 'm3' into eiger

This commit is contained in:
2020-07-29 17:21:14 +02:00
24 changed files with 517 additions and 455 deletions

View File

@ -1625,17 +1625,19 @@ std::string CmdProxy::VetoPhoton(int action) {
std::ostringstream os;
os << cmd << ' ';
if (action == defs::HELP_ACTION) {
os << "[n_chip] [#photons] [energy in keV] [reference "
os << "[ichip] [#photons] [energy in keV] [reference "
"file]\n\t[Gotthard2] Set veto reference for 128 channels for "
"chip n_chip according to referenc file and #photons and energy "
"in keV."
"chip ichip according to reference file and #photons and energy "
"in keV.\n"
<< "[ichip] [output file]\n\t Get gain indices and veto reference "
"for 128 channels for chip ichip, saved to file."
<< '\n';
} else if (action == defs::GET_ACTION) {
if (args.size() != 1) {
WrongNumberOfParameters(1);
if (args.size() != 2) {
WrongNumberOfParameters(2);
}
auto t = det->getVetoPhoton(StringTo<int>(args[0]), {det_id});
os << args[0] << ' ' << OutStringHex(t) << '\n';
det->getVetoPhoton(StringTo<int>(args[0]), args[1], {det_id});
os << "saved to file " << args[1] << '\n';
} else if (action == defs::PUT_ACTION) {
if (args.size() != 4) {
WrongNumberOfParameters(4);
@ -1653,7 +1655,7 @@ std::string CmdProxy::VetoReference(int action) {
std::ostringstream os;
os << cmd << ' ';
if (action == defs::HELP_ACTION) {
os << "[gain index] [12 bit value in hex] \n\t[Gotthard2] Set veto "
os << "[gain index] [12 bit value] \n\t[Gotthard2] Set veto "
"reference for all 128 channels for all chips."
<< '\n';
} else if (action == defs::GET_ACTION) {
@ -1677,7 +1679,7 @@ std::string CmdProxy::VetoFile(int action) {
if (action == defs::HELP_ACTION) {
os << "[chip index 0-10, -1 for all] [file name] \n\t[Gotthard2] Set "
"veto reference for each 128 channels for specific chip. The "
"file should have 128 rows of gain index and 12 bit value in hex"
"file should have 128 rows of gain index and 12 bit value in dec"
<< '\n';
} else if (action == defs::GET_ACTION) {
throw sls::RuntimeError(

View File

@ -1290,9 +1290,9 @@ void Detector::setInjectChannel(const int offsetChannel,
incrementChannel);
}
Result<std::vector<int>> Detector::getVetoPhoton(const int chipIndex,
Positions pos) {
return pimpl->Parallel(&Module::getVetoPhoton, pos, chipIndex);
void Detector::getVetoPhoton(const int chipIndex, const std::string &fname,
Positions pos) {
pimpl->Parallel(&Module::getVetoPhoton, pos, chipIndex, fname);
}
void Detector::setVetoPhoton(const int chipIndex, const int numPhotons,

View File

@ -1392,7 +1392,37 @@ void Module::setInjectChannel(const int offsetChannel,
sendToDetector(F_SET_INJECT_CHANNEL, args, nullptr);
}
std::vector<int> Module::getVetoPhoton(const int chipIndex) const {
void Module::sendVetoPhoton(const int chipIndex, std::vector<int> gainIndices,
std::vector<int> values) {
const int nch = gainIndices.size();
if (gainIndices.size() != values.size()) {
throw RuntimeError("Number of Gain Indices and values do not match! "
"Gain Indices size: " +
std::to_string(gainIndices.size()) +
", values size: " + std::to_string(values.size()));
}
LOG(logDEBUG1) << "Sending veto photon/file to detector [chip:" << chipIndex
<< ", nch:" << nch << "]";
int fnum = F_SET_VETO_PHOTON;
int ret = FAIL;
int args[]{chipIndex, nch};
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum));
client.Send(args, sizeof(args));
client.Send(gainIndices.data(), sizeof(int) * nch);
client.Send(values.data(), sizeof(int) * nch);
client.Receive(&ret, sizeof(ret));
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) +
" returned error: " + std::string(mess));
}
}
void Module::getVetoPhoton(const int chipIndex,
const std::string &fname) const {
LOG(logDEBUG1) << "Getting veto photon [" << chipIndex << "]\n";
int fnum = F_GET_VETO_PHOTON;
int ret = FAIL;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
@ -1405,16 +1435,29 @@ std::vector<int> Module::getVetoPhoton(const int chipIndex) const {
throw RuntimeError("Detector " + std::to_string(moduleId) +
" returned error: " + std::string(mess));
}
int nch = -1;
client.Receive(&nch, sizeof(nch));
if (nch != shm()->nChan.x) {
throw RuntimeError("Could not get veto photon. Expected " +
std::to_string(shm()->nChan.x) + " channels, got " +
std::to_string(nch));
}
std::vector<int> gainIndices(nch);
std::vector<int> values(nch);
client.Receive(gainIndices.data(), nch * sizeof(int));
client.Receive(values.data(), nch * sizeof(int));
int adus[nch];
memset(adus, 0, sizeof(adus));
client.Receive(adus, sizeof(adus));
std::vector<int> retvals(adus, adus + nch);
LOG(logDEBUG1) << "Getting veto photon [" << chipIndex << "]: " << nch
<< " channels\n";
return retvals;
// save to file
std::ofstream outfile;
outfile.open(fname.c_str(), std::ios_base::out);
if (!outfile.is_open()) {
throw RuntimeError("Could not create file to save veto photon");
}
for (int i = 0; i < nch; ++i) {
outfile << gainIndices[i] << ' ' << values[i] << '\n';
}
LOG(logDEBUG1) << nch << " veto photon saved to file";
}
void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
@ -1441,14 +1484,11 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
throw RuntimeError("Could not set veto photon. Could not open file: " +
fname);
}
LOG(logDEBUG1) << "Setting veto photon. Reading Gain values from file";
int totalEnergy = numPhotons * energy;
int ch = shm()->nChan.x;
int gainIndex = 2;
int nRead = 0;
int value[ch];
memset(value, 0, sizeof(value));
bool firstLine = true;
std::vector<int> gainIndices;
std::vector<int> values;
while (infile.good()) {
std::string line;
@ -1460,65 +1500,41 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
continue;
}
std::istringstream ss(line);
// first line: caluclate gain index from gain thresholds from file
if (firstLine) {
int g0 = -1, g1 = -1;
ss >> g0 >> g1;
if (ss.fail()) {
throw RuntimeError(
"Could not set veto photon. Invalid gain thresholds");
}
// set gain index and gain bit values
if (totalEnergy < g0) {
gainIndex = 0;
} else if (totalEnergy < g1) {
gainIndex = 1;
}
LOG(logINFO) << "Setting veto photon. Reading Gain " << gainIndex
<< " values";
firstLine = false;
// read pedestal, gain values, gain thresholds
double gainPedestal[3] = {-1, -1, -1};
double gainValue[3] = {-1, -1, -1};
int gainThreshold[2] = {-1, -1};
ss >> gainPedestal[0] >> gainPedestal[1] >> gainPedestal[2] >>
gainValue[0] >> gainValue[1] >> gainValue[2] >> gainThreshold[0] >>
gainThreshold[1];
if (ss.fail()) {
throw RuntimeError("Could not set veto photon. Invalid pedestals, "
"gain values or gain thresholds for channel " +
std::to_string(gainIndices.size()));
}
// read pedestal and gain values
else {
double p[3] = {-1, -1, -1}, g[3] = {-1, -1, -1};
ss >> p[0] >> p[1] >> p[2] >> g[0] >> g[1] >> g[2];
if (ss.fail()) {
throw RuntimeError("Could not set veto photon. Invalid "
"pedestal or gain values for channel " +
std::to_string(nRead));
}
value[nRead] =
p[gainIndex] +
(g[gainIndex] *
totalEnergy); // ADU value = pedestal + gain * total energy
++nRead;
if (nRead >= ch) {
break;
}
// caluclate gain index from gain thresholds and threhsold energy
int gainIndex = 2;
if (totalEnergy < gainThreshold[0]) {
gainIndex = 0;
} else if (totalEnergy < gainThreshold[1]) {
gainIndex = 1;
}
gainIndices.push_back(gainIndex);
// calculate ADU value
values.push_back(gainPedestal[gainIndex] +
(gainValue[gainIndex] * totalEnergy));
}
if (nRead != ch) {
throw RuntimeError("Could not set veto photon. Insufficient pedestal "
"pr gain values: " +
std::to_string(nRead));
// check size
if ((int)gainIndices.size() != shm()->nChan.x) {
throw RuntimeError("Could not set veto photon. Invalid number of "
"entries in file. Expected " +
std::to_string(shm()->nChan.x) + ", read " +
std::to_string(gainIndices.size()));
}
int fnum = F_SET_VETO_PHOTON;
int ret = FAIL;
int args[]{chipIndex, gainIndex, ch};
LOG(logDEBUG) << "Sending veto photon value to detector [chip:" << chipIndex
<< ", G" << gainIndex << "]: " << args;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum));
client.Send(args, sizeof(args));
client.Send(value, sizeof(value));
client.Receive(&ret, sizeof(ret));
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) +
" returned error: " + std::string(mess));
}
sendVetoPhoton(chipIndex, gainIndices, values);
}
void Module::setVetoReference(const int gainIndex, const int value) {
@ -1548,12 +1564,8 @@ void Module::setVetoFile(const int chipIndex, const std::string &fname) {
" for reading");
}
int ch = shm()->nChan.x;
int nRead = 0;
int gainIndices[ch];
memset(gainIndices, 0, sizeof(gainIndices));
int values[ch];
memset(values, 0, sizeof(values));
std::vector<int> gainIndices;
std::vector<int> values;
for (std::string line; std::getline(input_file, line);) {
if (line.find('#') != std::string::npos) {
@ -1564,43 +1576,34 @@ void Module::setVetoFile(const int chipIndex, const std::string &fname) {
// convert command and string to a vector
std::istringstream iss(line);
std::string val;
iss >> gainIndices[nRead] >> val;
int gainIndex = -1;
int value = -1;
iss >> gainIndex >> val;
if (iss.fail()) {
throw RuntimeError("Could not set veto file. Invalid gain "
"or reference value for channel " +
std::to_string(nRead));
std::to_string(gainIndices.size()));
}
try {
values[nRead] = StringTo<int>(val);
value = StringTo<int>(val);
} catch (...) {
throw RuntimeError("Could not set veto file. Invalid value " +
val + " for channel " +
std::to_string(nRead));
}
++nRead;
if (nRead >= ch) {
break;
std::to_string(gainIndices.size()));
}
gainIndices.push_back(gainIndex);
values.push_back(value);
}
}
int fnum = F_SET_VETO_FILE;
int ret = FAIL;
int args[]{chipIndex, ch};
LOG(logDEBUG) << "Sending veto file value to detector [chip:" << chipIndex
<< "]: " << args;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum));
client.Send(args, sizeof(args));
client.Send(gainIndices, sizeof(gainIndices));
client.Send(values, sizeof(values));
client.Receive(&ret, sizeof(ret));
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) +
" returned error: " + std::string(mess));
// check size
if ((int)gainIndices.size() != shm()->nChan.x) {
throw RuntimeError("Could not set veto file. Invalid number of "
"entries in file. Expected " +
std::to_string(shm()->nChan.x) + ", read " +
std::to_string(gainIndices.size()));
}
sendVetoPhoton(chipIndex, gainIndices, values);
}
slsDetectorDefs::burstMode Module::getBurstMode() const {
@ -1693,7 +1696,7 @@ void Module::getBadChannels(const std::string &fname) const {
std::ofstream outfile;
outfile.open(fname.c_str(), std::ios_base::out);
if (!outfile.is_open()) {
throw RuntimeError("Could not create file to save pattern");
throw RuntimeError("Could not create file to save bad channels");
}
for (int i = 0; i < nch; ++i) {
outfile << badchannels[i] << '\n';

View File

@ -371,7 +371,9 @@ class Module : public virtual slsDetectorDefs {
void setBurstPeriod(int64_t value);
std::array<int, 2> getInjectChannel() const;
void setInjectChannel(const int offsetChannel, const int incrementChannel);
std::vector<int> getVetoPhoton(const int chipIndex) const;
void sendVetoPhoton(const int chipIndex, std::vector<int> gainIndices,
std::vector<int> values);
void getVetoPhoton(const int chipIndex, const std::string &fname) const;
void setVetoPhoton(const int chipIndex, const int numPhotons,
const int energy, const std::string &fname);
void setVetoReference(const int gainIndex, const int value);
@ -519,7 +521,7 @@ class Module : public virtual slsDetectorDefs {
private:
void checkArgs(const void *args, size_t args_size, void *retval,
size_t retval_size) const;
size_t retval_size) const;
/**
* Send function parameters to detector (control server)