badchannels done

This commit is contained in:
maliakal_d 2020-07-15 18:24:17 +02:00
parent d7f490701b
commit ca298580f3
13 changed files with 362 additions and 24 deletions

View File

@ -78,14 +78,11 @@
/* Status register */
#define STATUS_REG (0x04 * REG_OFFSET + BASE_CONTROL)
/* Look at me read only register */
#define LOOK_AT_ME_REG (0x05 * REG_OFFSET + BASE_CONTROL)
/* System status register */
#define SYSTEM_STATUS_REG (0x06 * REG_OFFSET + BASE_CONTROL)
#define SYSTEM_STATUS_REG (0x05 * REG_OFFSET + BASE_CONTROL)
/* Config RW regiseter */
#define CONFIG_REG (0x20 * REG_OFFSET + BASE_CONTROL)
#define CONFIG_REG (0x08 * REG_OFFSET + BASE_CONTROL)
#define CONFIG_VETO_ENBL_OFST (0)
#define CONFIG_VETO_ENBL_MSK (0x00000001 << CONFIG_VETO_ENBL_OFST)
@ -93,7 +90,7 @@
#define CONFIG_VETO_CH_10GB_ENBL_MSK (0x00000001 << CONFIG_VETO_CH_10GB_ENBL_OFST)
/* Control RW register */
#define CONTROL_REG (0x21 * REG_OFFSET + BASE_CONTROL)
#define CONTROL_REG (0x09 * REG_OFFSET + BASE_CONTROL)
#define CONTROL_STRT_ACQSTN_OFST (0)
#define CONTROL_STRT_ACQSTN_MSK (0x00000001 << CONTROL_STRT_ACQSTN_OFST)
@ -111,7 +108,11 @@
#define CONTROL_PWR_CHIP_MSK (0x00000001 << CONTROL_PWR_CHIP_OFST)
/** DTA Offset Register */
#define DTA_OFFSET_REG (0x24 * REG_OFFSET + BASE_CONTROL)
#define DTA_OFFSET_REG (0x0A * REG_OFFSET + BASE_CONTROL)
/** Mask Strip Registers (40) */
#define MASK_STRIP_START_REG (0x18 * REG_OFFSET + BASE_CONTROL)
#define MASK_STRIP_NUM_REGS (40)
/* ASIC registers --------------------------------------------------*/

View File

@ -2316,6 +2316,72 @@ int getVeto() {
CONFIG_VETO_ENBL_OFST);
}
void setBadChannels(int nch, int *channels) {
LOG(logINFO, ("Setting %d bad channels\n", nch));
int numAddr = MASK_STRIP_NUM_REGS;
int startAddr = MASK_STRIP_START_REG;
// resetting all mask registers first
for (int iaddr = 0; iaddr < numAddr; ++iaddr) {
uint32_t addr = startAddr + iaddr * REG_OFFSET;
bus_w(addr, 0);
}
// setting badchannels, loop through list
for (int i = 0; i < nch; ++i) {
LOG(logINFO, ("\t[%d]: %d\n", i, channels[i]));
int iaddr = channels[i] / 32;
int iBit = channels[i] % 32;
uint32_t addr = startAddr + iaddr * REG_OFFSET;
LOG(logDEBUG1,
("val:%d iaddr:%d iBit:%d, addr:0x%x old:0x%x val:0x%x\n",
channels[i], iaddr, iBit, addr, bus_r(addr), (1 << iBit)));
bus_w(addr, bus_r(addr) | (1 << iBit));
}
}
int *getBadChannels(int *nch) {
int *retvals = NULL;
// count number of bad channels
*nch = 0;
for (int i = 0; i < MASK_STRIP_NUM_REGS; ++i) {
uint32_t addr = MASK_STRIP_START_REG + i * REG_OFFSET;
*nch += __builtin_popcount(bus_r(addr));
}
if (*nch > 0) {
// get list of bad channels
retvals = malloc(*nch * sizeof(int));
if (retvals == NULL) {
*nch = -1;
return NULL;
}
int chIndex = 0;
int numAddr = MASK_STRIP_NUM_REGS;
// loop through registers
for (int iaddr = 0; iaddr < numAddr; ++iaddr) {
// calculate address and get value
uint32_t addr = MASK_STRIP_START_REG + iaddr * REG_OFFSET;
uint32_t val = bus_r(addr);
// loop through 32 bits
for (int iBit = 0; iBit < 32; ++iBit) {
// masked, add to list
if ((val >> iBit) & 0x1) {
LOG(logDEBUG1, ("iaddr:%d iBit:%d val:0x%x, ch:%d\n", iaddr,
iBit, val, iaddr * 32 + iBit));
retvals[chIndex++] = iaddr * 32 + iBit;
}
}
}
}
// debugging
LOG(logDEBUG1, ("Reading Bad channel list\n"));
for (int i = 0; i < (*nch); ++i) {
LOG(logDEBUG1, ("[%d]: %d\n", i, retvals[i]));
}
return retvals;
}
/* aquisition */
int startStateMachine() {

View File

@ -523,6 +523,8 @@ void setTimingSource(enum timingSourceType value);
enum timingSourceType getTimingSource();
void setVeto(int enable);
int getVeto();
void setBadChannels(int nch, int *channels);
int *getBadChannels(int *nch);
#endif
#if defined(JUNGFRAUD) || defined(EIGERD)

View File

@ -236,3 +236,5 @@ int set_filter(int);
int set_veto_file(int);
int get_adc_config(int);
int set_adc_config(int);
int get_bad_channels(int);
int set_bad_channels(int);

View File

@ -353,6 +353,8 @@ void function_table() {
flist[F_SET_VETO_FILE] = &set_veto_file;
flist[F_GET_ADC_CONFIGURATION] = &get_adc_config;
flist[F_SET_ADC_CONFIGURATION] = &set_adc_config;
flist[F_GET_BAD_CHANNELS] = &get_bad_channels;
flist[F_SET_BAD_CHANNELS] = &set_bad_channels;
// check
if (NUM_DET_FUNCTIONS >= RECEIVER_ENUM_START) {
@ -7926,3 +7928,100 @@ int set_adc_config(int file_des) {
#endif
return Server_SendResult(file_des, INT32, NULL, 0);
}
int get_bad_channels(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int nretvals = 0;
int *retvals = NULL;
LOG(logDEBUG1, ("Getting bad channels\n"));
#ifndef GOTTHARD2D
functionNotImplemented();
#else
// get only
retvals = getBadChannels(&nretvals);
if (nretvals == -1) {
ret = FAIL;
strcpy(mess, "Could not get bad channels. Memory allcoation error\n");
LOG(logERROR, (mess));
}
#endif
Server_SendResult(file_des, INT32, NULL, 0);
if (ret != FAIL) {
sendData(file_des, &nretvals, sizeof(nretvals), INT32);
if (nretvals > 0) {
sendData(file_des, retvals, sizeof(int) * nretvals, INT32);
}
}
if (retvals != NULL) {
free(retvals);
}
return ret;
}
int set_bad_channels(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int nargs = 0;
int *args = NULL;
if (receiveData(file_des, &nargs, sizeof(nargs), INT32) < 0)
return printSocketReadError();
if (nargs > 0) {
args = malloc(nargs * sizeof(int));
if (receiveData(file_des, args, nargs * sizeof(int), INT32) < 0)
return printSocketReadError();
}
LOG(logDEBUG1, ("Setting %d bad channels\n", nargs));
#ifndef GOTTHARD2D
functionNotImplemented();
#else
// only set
if (Server_VerifyLock() == OK) {
// validate bad channel number
for (int i = 0; i < nargs; ++i) {
LOG(logDEBUG1, ("\t[%d]:%d\n", i, args[i]));
if (args[i] < 0 || args[i] >= (NCHAN * NCHIP)) {
ret = FAIL;
sprintf(mess,
"Could not set bad channels. Invalid bad channel "
"number %d. Options [0-%d]\n",
args[i], NCHIP * NCHAN - 1);
LOG(logERROR, (mess));
break;
}
}
if (ret == OK) {
setBadChannels(nargs, args);
int nretvals = 0;
int *retvals = getBadChannels(&nretvals);
if (nretvals == -1) {
ret = FAIL;
strcpy(mess,
"Could not get bad channels. Memory allcoation error\n");
LOG(logERROR, (mess));
} else if (nretvals != nargs) {
ret = FAIL;
sprintf(
mess,
"Could not set bad channels. Set %d channels, but read %d "
"channels\n",
nargs, nretvals);
LOG(logERROR, (mess));
}
if (retvals != NULL) {
free(retvals);
}
}
}
if (args != NULL) {
free(args);
}
#endif
return Server_SendResult(file_des, INT32, NULL, 0);
}

View File

@ -1031,6 +1031,12 @@ class Detector {
void setADCConfiguration(const int chipIndex, const int adcIndex,
const int value, Positions pos = {});
/** [Gotthard2] */
void getBadChannels(const std::string &fname, Positions pos = {}) const;
/** [Gotthard2] */
void setBadChannels(const std::string &fname, Positions pos = {});
/**************************************************
* *
* Mythen3 Specific *

View File

@ -1752,7 +1752,7 @@ std::string CmdProxy::ConfigureADC(int action) {
WrongNumberOfParameters(2);
}
auto t = det->getADCConfiguration(StringTo<int>(args[0]),
StringTo<int>(args[1]));
StringTo<int>(args[1]), {det_id});
os << OutStringHex(t) << '\n';
} else if (action == defs::PUT_ACTION) {
if (args.size() != 3) {
@ -1769,6 +1769,31 @@ std::string CmdProxy::ConfigureADC(int action) {
return os.str();
}
std::string CmdProxy::BadChannels(int action) {
std::ostringstream os;
os << cmd << ' ';
if (action == defs::HELP_ACTION) {
os << "[fname]\n\t[Gotthard2] Sets the bad channels (from file of bad "
"channel numbers) to be masked out."
<< '\n';
} else if (action == defs::GET_ACTION) {
if (args.size() != 1) {
WrongNumberOfParameters(1);
}
det->getBadChannels(args[0], {det_id});
os << "successfully retrieved" << '\n';
} else if (action == defs::PUT_ACTION) {
if (args.size() != 1) {
WrongNumberOfParameters(1);
}
det->setBadChannels(args[0], {det_id});
os << "successfully loaded" << '\n';
} else {
throw sls::RuntimeError("Unknown action");
}
return os.str();
}
/* Mythen3 Specific */
std::string CmdProxy::Counters(int action) {

View File

@ -372,6 +372,27 @@
return os.str(); \
}
/** set only, 1 argument */
#define EXECUTE_SET_COMMAND_1ARG(CMDNAME, SETFCN, HLPSTR) \
std::string CMDNAME(const int action) { \
std::ostringstream os; \
os << cmd << ' '; \
if (action == slsDetectorDefs::HELP_ACTION) \
os << HLPSTR << '\n'; \
else if (action == slsDetectorDefs::GET_ACTION) { \
throw sls::RuntimeError("Cannot get"); \
} else if (action == slsDetectorDefs::PUT_ACTION) { \
if (args.size() != 1) { \
WrongNumberOfParameters(1); \
} \
det->SETFCN(args[0]); \
os << args.front() << '\n'; \
} else { \
throw sls::RuntimeError("Unknown action"); \
} \
return os.str(); \
}
/** get only */
#define GET_COMMAND(CMDNAME, GETFCN, HLPSTR) \
std::string CMDNAME(const int action) { \
@ -842,6 +863,7 @@ class CmdProxy {
{"timingsource", &CmdProxy::timingsource},
{"veto", &CmdProxy::veto},
{"confadc", &CmdProxy::ConfigureADC},
{"badchannels", &CmdProxy::BadChannels},
/* Mythen3 Specific */
{"counters", &CmdProxy::Counters},
@ -1020,6 +1042,7 @@ class CmdProxy {
std::string VetoFile(int action);
std::string BurstMode(int action);
std::string ConfigureADC(int action);
std::string BadChannels(int action);
/* Mythen3 Specific */
std::string Counters(int action);
std::string GateDelay(int action);
@ -1093,7 +1116,7 @@ class CmdProxy {
"g2_lc_hg | g2_lc_lg | g4_hg | g4_lg]"
"\n\t[Eiger] Use threshold or thresholdnotb.");
EXECUTE_SET_COMMAND_NOID_1ARG(
EXECUTE_SET_COMMAND_1ARG(
trimbits, loadTrimbits,
"[fname]\n\t[Eiger][Mythen3] Loads the trimbit file to detector. If no "
"extension specified, serial number of each module is attached.");

View File

@ -1363,6 +1363,14 @@ void Detector::setADCConfiguration(const int chipIndex, const int adcIndex,
value);
}
void Detector::getBadChannels(const std::string &fname, Positions pos) const {
pimpl->Parallel(&Module::getBadChannels, pos, fname);
}
void Detector::setBadChannels(const std::string &fname, Positions pos) {
pimpl->Parallel(&Module::setBadChannels, pos, fname);
}
// Mythen3 Specific
Result<uint32_t> Detector::getCounterMask(Positions pos) const {

View File

@ -1400,7 +1400,7 @@ std::vector<int> Module::getVetoPhoton(const int chipIndex) {
client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) +
" returned error: " + std::string(mess));
} else {
}
int nch = -1;
client.Receive(&nch, sizeof(nch));
@ -1412,7 +1412,6 @@ std::vector<int> Module::getVetoPhoton(const int chipIndex) {
<< " channels\n";
return retvals;
}
}
void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
const int energy, const std::string &fname) {
@ -1460,7 +1459,8 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
// first line: caluclate gain index from gain thresholds from file
if (firstLine) {
int g0 = -1, g1 = -1;
if (!(ss >> g0 >> g1)) {
ss >> g0 >> g1;
if (ss.fail()) {
throw RuntimeError(
"Could not set veto photon. Invalid gain thresholds");
}
@ -1477,7 +1477,8 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
// read pedestal and gain values
else {
double p[3] = {-1, -1, -1}, g[3] = {-1, -1, -1};
if (!(ss >> p[0] >> p[1] >> p[2] >> g[0] >> g[1] >> g[2])) {
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));
@ -1559,7 +1560,8 @@ void Module::setVetoFile(const int chipIndex, const std::string &fname) {
// convert command and string to a vector
std::istringstream iss(line);
std::string val;
if (!(iss >> gainIndices[nRead] >> val)) {
iss >> gainIndices[nRead] >> val;
if (iss.fail()) {
throw RuntimeError("Could not set veto file. Invalid gain "
"or reference value for channel " +
std::to_string(nRead));
@ -1656,6 +1658,90 @@ void Module::setADCConfiguration(const int chipIndex, const int adcIndex,
sendToDetector(F_SET_ADC_CONFIGURATION, args, nullptr);
}
void Module::getBadChannels(const std::string &fname) {
LOG(logDEBUG1) << "Getting bad channels to " << fname;
int fnum = F_GET_BAD_CHANNELS;
int ret = FAIL;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum));
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));
}
// receive badchannels
int nch = -1;
std::vector<int> badchannels;
client.Receive(&nch, sizeof(nch));
if (nch > 0) {
int temp[nch];
memset(temp, 0, sizeof(temp));
client.Receive(temp, sizeof(temp));
badchannels.insert(badchannels.end(), &temp[0], &temp[nch]);
for (int i = 0; i < (int)badchannels.size(); ++i) {
LOG(logDEBUG1) << i << ":" << badchannels[i];
}
}
// 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 pattern");
}
for (int i = 0; i < nch; ++i) {
outfile << badchannels[i] << '\n';
}
LOG(logDEBUG1) << nch << " bad channels saved to file";
}
void Module::setBadChannels(const std::string &fname) {
// read bad channels file
std::ifstream input_file(fname);
if (!input_file.is_open()) {
throw RuntimeError("Could not open bad channels file " + fname +
" for reading");
}
std::vector<int> badchannels;
for (std::string line; std::getline(input_file, line);) {
if (line.find(' ') != std::string::npos) {
line.erase(line.find(' '));
}
if (line.length() >= 1) {
std::istringstream iss(line);
int ival = 0;
iss >> ival;
if (iss.fail()) {
throw RuntimeError("Could not load bad channels file. Invalid "
"channel number at position " +
std::to_string(badchannels.size()));
}
badchannels.push_back(ival);
}
}
// send bad channels to module
int fnum = F_SET_BAD_CHANNELS;
int ret = FAIL;
int nch = badchannels.size();
LOG(logDEBUG1) << "Sending bad channels to detector, nch:" << nch;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum));
client.Send(&nch, sizeof(nch));
if (nch > 0) {
client.Send(badchannels.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));
}
}
// Mythen3 Specific
uint32_t Module::getCounterMask() {

View File

@ -389,6 +389,8 @@ class Module : public virtual slsDetectorDefs {
int getADCConfiguration(const int chipIndex, const int adcIndex);
void setADCConfiguration(const int chipIndex, const int adcIndex,
int value);
void getBadChannels(const std::string &fname);
void setBadChannels(const std::string &fname);
/**************************************************
* *

View File

@ -584,3 +584,17 @@ TEST_CASE("confadc", "[.cmd][.new]") {
REQUIRE_THROWS(proxy.Call("confadc", {}, -1, GET));
}
}
TEST_CASE("badchannels", "[.cmd][.new]") {
Detector det;
CmdProxy proxy(&det);
auto det_type = det.getDetectorType().squash();
if (det_type == defs::GOTTHARD2) {
REQUIRE_THROWS(proxy.Call("badchannels", {}, -1, GET));
REQUIRE_NOTHROW(proxy.Call("badchannels", {"/tmp/bla.txt"}, -1, GET));
REQUIRE_NOTHROW(proxy.Call("badchannels", {"/tmp/bla.txt"}, -1, PUT));
} else {
REQUIRE_THROWS(proxy.Call("badchannels", {}, -1, GET));
}
}

View File

@ -210,6 +210,8 @@ enum detFuncs {
F_SET_VETO_FILE,
F_GET_ADC_CONFIGURATION,
F_SET_ADC_CONFIGURATION,
F_GET_BAD_CHANNELS,
F_SET_BAD_CHANNELS,
NUM_DET_FUNCTIONS,
RECEIVER_ENUM_START = 256, /**< detector function should not exceed this
@ -518,6 +520,8 @@ const char* getFunctionNameFromEnum(enum detFuncs func) {
case F_SET_VETO_FILE: return "F_SET_VETO_FILE";
case F_SET_ADC_CONFIGURATION: return "F_SET_ADC_CONFIGURATION";
case F_GET_ADC_CONFIGURATION: return "F_GET_ADC_CONFIGURATION";
case F_GET_BAD_CHANNELS: return "F_GET_BAD_CHANNELS";
case F_SET_BAD_CHANNELS: return "F_SET_BAD_CHANNELS";
case NUM_DET_FUNCTIONS: return "NUM_DET_FUNCTIONS";
case RECEIVER_ENUM_START: return "RECEIVER_ENUM_START";