Skip to content

Commit

Permalink
Addressing TRESTDAQFEMINOS issues and few bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
usuario authored and usuario committed May 30, 2024
1 parent f24b311 commit c03956b
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 95 deletions.
2 changes: 1 addition & 1 deletion daq/FEMProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FEMProxy : public TRESTDAQSocket {

public:
FEMProxy(){ }
bool pendingEvent=false;
bool pendingEvent=true;
TRestRawDAQMetadata::FECMetadata fecMetadata;
//std::atomic_int
int cmd_sent=0;
Expand Down
2 changes: 1 addition & 1 deletion daq/TRESTDAQ.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TRESTDAQ {

// Pure virtual methods to start, stop and configure the DAQ
virtual void configure() = 0;
virtual void startDAQ() = 0;
virtual void startDAQ(bool configure=true) = 0;
virtual void stopDAQ() = 0;
virtual void initialize() = 0;
virtual void startUp(){};
Expand Down
8 changes: 4 additions & 4 deletions daq/TRESTDAQDCC.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ void TRESTDAQDCC::configure() {
// SendCommand("isobus 0x0F", -1);//Reset event counter, timestamp set eventType to test
}

void TRESTDAQDCC::startDAQ() {
void TRESTDAQDCC::startDAQ(bool configure) {

if ( acqType == daq_metadata_types::acqTypes::PEDESTAL) {
pedestal();
} else {
dataTaking();
dataTaking(configure);
}
}

Expand Down Expand Up @@ -149,14 +149,14 @@ void TRESTDAQDCC::pedestal() {
FillTree(restRun, &fSignalEvent);
}

void TRESTDAQDCC::dataTaking() {
void TRESTDAQDCC::dataTaking(bool configure) {
std::cout << "Starting data taking run" << std::endl;

char cmd[200];
SendCommand("fem 0"); // Needed?
// if(comp)SendCommand("skipempty 1", -1);//Skip empty frames in compress mode
// else SendCommand("skipempty 0", -1);//Save empty frames if not
SendCommand("isobus 0x4F"); // Reset event counter, timestamp for type 11
if(configure)SendCommand("isobus 0x4F"); // Reset event counter, timestamp for type 11

while ( !abrt && !nextFile && (daqMetadata->GetNEvents() == 0 || event_cnt < daqMetadata->GetNEvents())) {
SendCommand("fem 0");
Expand Down
4 changes: 2 additions & 2 deletions daq/TRESTDAQDCC.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class TRESTDAQDCC : public TRESTDAQ {
TRESTDAQDCC(TRestRun* rR, TRestRawDAQMetadata* dM);

void configure() override;
void startDAQ() override;
void startDAQ(bool configure=true) override;
void stopDAQ() override;
void initialize() override;

private:
void pedestal();
void dataTaking();
void dataTaking(bool configure=true);
DCCPacket::packetReply SendCommand(const char* cmd, DCCPacket::packetType type = DCCPacket::packetType::ASCII, size_t nPackets = 0, DCCPacket::packetDataType dataType = DCCPacket::packetDataType::NONE);

void waitForTrigger();
Expand Down
2 changes: 1 addition & 1 deletion daq/TRESTDAQDummy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void TRESTDAQDummy::initialize() {}

void TRESTDAQDummy::configure() { std::cout << "Configuring readout" << std::endl; }

void TRESTDAQDummy::startDAQ() {
void TRESTDAQDummy::startDAQ(bool configure) {
std::srand(static_cast<unsigned int>(std::time(nullptr)));

while ( !abrt && !nextFile && (daqMetadata->GetNEvents() == 0 || event_cnt < daqMetadata->GetNEvents() ) ) {
Expand Down
2 changes: 1 addition & 1 deletion daq/TRESTDAQDummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TRESTDAQDummy : public TRESTDAQ {
TRESTDAQDummy(TRestRun* rR, TRestRawDAQMetadata* dM);

void configure() override;
void startDAQ() override;
void startDAQ(bool configure=true) override;
void stopDAQ() override;
void initialize() override;
};
Expand Down
166 changes: 88 additions & 78 deletions daq/TRESTDAQFEMINOS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Author: JuanAn Garcia 18/08/2021


std::atomic<bool> TRESTDAQFEMINOS::stopReceiver(false);
std::atomic<bool> TRESTDAQFEMINOS::isPed(false);

TRESTDAQFEMINOS::TRESTDAQFEMINOS(TRestRun* rR, TRestRawDAQMetadata* dM) : TRESTDAQ(rR, dM) { initialize(); }

Expand Down Expand Up @@ -41,6 +42,7 @@ void TRESTDAQFEMINOS::initialize() {
}

//Start receive and event builder threads
stopReceiver=false;
receiveThread = std::thread( TRESTDAQFEMINOS::ReceiveThread, &FEMArray);
eventBuilderThread = std::thread( TRESTDAQFEMINOS::EventBuilderThread, &FEMArray, restRun, &fSignalEvent);
}
Expand Down Expand Up @@ -154,7 +156,7 @@ void TRESTDAQFEMINOS::configure() {
}
}

void TRESTDAQFEMINOS::startDAQ() {
void TRESTDAQFEMINOS::startDAQ(bool configure) {
auto rT = daq_metadata_types::acqTypes_map.find(std::string(daqMetadata->GetAcquisitionType()));
if (rT == daq_metadata_types::acqTypes_map.end()) {
std::cout << "Unknown acquisition type " << daqMetadata->GetAcquisitionType() << " skipping" << std::endl;
Expand All @@ -166,7 +168,7 @@ void TRESTDAQFEMINOS::startDAQ() {
if (rT->second == daq_metadata_types::acqTypes::PEDESTAL) {
pedestal();
} else {
dataTaking();
dataTaking(configure);
}
}

Expand Down Expand Up @@ -234,67 +236,68 @@ void TRESTDAQFEMINOS::pedestal() {
BroadcastCommand("serve_target 1",FEMArray);
}

void TRESTDAQFEMINOS::dataTaking() {
void TRESTDAQFEMINOS::dataTaking(bool configure) {
std::cout << "Starting data taking run" << std::endl;
//BroadcastCommand("daq 0x000000 F",FEMArray,false);
BroadcastCommand("daq 0xFFFFFF F",FEMArray);
BroadcastCommand("sca enable 0",FEMArray);
std::this_thread::sleep_for(std::chrono::seconds(1));
BroadcastCommand("serve_target 0",FEMArray);
std::this_thread::sleep_for(std::chrono::seconds(4));
//Readout settings
BroadcastCommand("modify_hit_reg 0",FEMArray);
BroadcastCommand("emit_hit_cnt 1",FEMArray);
BroadcastCommand("emit_empty_ch 0",FEMArray);
BroadcastCommand("emit_lst_cell_rd 1",FEMArray);
BroadcastCommand("keep_rst 1",FEMArray);
BroadcastCommand("skip_rst 0",FEMArray);
//AGET settings
if(compressMode == daq_metadata_types::compressModeTypes::ALLCHANNELS || compressMode == daq_metadata_types::compressModeTypes::ZEROSUPPRESSION){
BroadcastCommand("aget * mode 0x1",FEMArray);//Mode: 0x0: hit/selected channels 0x1:all channels
} else if(compressMode == daq_metadata_types::compressModeTypes::TRIGGEREDCHANNELS){
BroadcastCommand("aget * mode 0x0",FEMArray);//Mode: 0x0: hit/selected channels 0x1:all channels
}
BroadcastCommand("aget * tst_digout 1",FEMArray);//??
if(configure){
BroadcastCommand("daq 0xFFFFFF F",FEMArray);
BroadcastCommand("sca enable 0",FEMArray);
std::this_thread::sleep_for(std::chrono::seconds(1));
BroadcastCommand("serve_target 0",FEMArray);
std::this_thread::sleep_for(std::chrono::seconds(4));
//Readout settings
BroadcastCommand("modify_hit_reg 0",FEMArray);
BroadcastCommand("emit_hit_cnt 1",FEMArray);
BroadcastCommand("emit_empty_ch 0",FEMArray);
BroadcastCommand("emit_lst_cell_rd 1",FEMArray);
BroadcastCommand("keep_rst 1",FEMArray);
BroadcastCommand("skip_rst 0",FEMArray);
//AGET settings
if(compressMode == daq_metadata_types::compressModeTypes::ALLCHANNELS || compressMode == daq_metadata_types::compressModeTypes::ZEROSUPPRESSION){
BroadcastCommand("aget * mode 0x1",FEMArray);//Mode: 0x0: hit/selected channels 0x1:all channels
} else if(compressMode == daq_metadata_types::compressModeTypes::TRIGGEREDCHANNELS){
BroadcastCommand("aget * mode 0x0",FEMArray);//Mode: 0x0: hit/selected channels 0x1:all channels
}
BroadcastCommand("aget * tst_digout 1",FEMArray);//??

BroadcastCommand("subtract_ped 1",FEMArray);

if(compressMode == daq_metadata_types::compressModeTypes::ZEROSUPPRESSION){
BroadcastCommand("zero_suppress 1",FEMArray);
BroadcastCommand("zs_pre_post 8 4",FEMArray);
} else {
BroadcastCommand("zero_suppress 0",FEMArray);
BroadcastCommand("zs_pre_post 8 4",FEMArray);
}

//Event generator
BroadcastCommand("clr tstamp",FEMArray);
BroadcastCommand("clr evnct",FEMArray);
BroadcastCommand("event_limit 0x0",FEMArray);//Infinite
if (triggerType == daq_metadata_types::triggerTypes::INTERNAL){
BroadcastCommand("trig_rate 1 50",FEMArray);
BroadcastCommand("trig_enable 0x1",FEMArray);
} else if (triggerType == daq_metadata_types::triggerTypes::TCM) {
BroadcastCommand("trig_enable 0x8",FEMArray);//tcm???
} else if (triggerType == daq_metadata_types::triggerTypes::AUTO) {
BroadcastCommand("trig_enable 0x0",FEMArray);
BroadcastCommand("subtract_ped 1",FEMArray);

if(compressMode == daq_metadata_types::compressModeTypes::ZEROSUPPRESSION){
BroadcastCommand("zero_suppress 1",FEMArray);
BroadcastCommand("zs_pre_post 8 4",FEMArray);
} else {
BroadcastCommand("zero_suppress 0",FEMArray);
BroadcastCommand("zs_pre_post 8 4",FEMArray);
}

for (auto &FEM : FEMArray){
char cmd[200];
for(int a=0;a<TRestRawDAQMetadata::nAsics;a++){
if(!FEM.fecMetadata.asic_isActive[a])continue;
sprintf(cmd, "aget %d dac 0x%X",a, FEM.fecMetadata.asic_coarseThr[a]);
SendCommand(cmd,FEM);
sprintf(cmd, "aget %d threshold 0x%X",a, FEM.fecMetadata.asic_fineThr[a]);
SendCommand(cmd,FEM);
sprintf(cmd, "aget %d mult_thr %d",a, FEM.fecMetadata.asic_multThr[a]);
SendCommand(cmd,FEM);
sprintf(cmd, "aget %d mult_limit %d",a, FEM.fecMetadata.asic_multLimit[a]);
SendCommand(cmd,FEM);
//Event generator
BroadcastCommand("clr tstamp",FEMArray);
BroadcastCommand("clr evnct",FEMArray);
BroadcastCommand("event_limit 0x0",FEMArray);//Infinite
if (triggerType == daq_metadata_types::triggerTypes::INTERNAL){
BroadcastCommand("trig_rate 1 50",FEMArray);
BroadcastCommand("trig_enable 0x1",FEMArray);
} else if (triggerType == daq_metadata_types::triggerTypes::TCM) {
BroadcastCommand("trig_enable 0x8",FEMArray);//tcm???
} else if (triggerType == daq_metadata_types::triggerTypes::AUTO) {
BroadcastCommand("trig_enable 0x0",FEMArray);
}
}

for (auto &FEM : FEMArray){
char cmd[200];
for(int a=0;a<TRestRawDAQMetadata::nAsics;a++){
if(!FEM.fecMetadata.asic_isActive[a])continue;
sprintf(cmd, "aget %d dac 0x%X",a, FEM.fecMetadata.asic_coarseThr[a]);
SendCommand(cmd,FEM);
sprintf(cmd, "aget %d threshold 0x%X",a, FEM.fecMetadata.asic_fineThr[a]);
SendCommand(cmd,FEM);
sprintf(cmd, "aget %d mult_thr %d",a, FEM.fecMetadata.asic_multThr[a]);
SendCommand(cmd,FEM);
sprintf(cmd, "aget %d mult_limit %d",a, FEM.fecMetadata.asic_multLimit[a]);
SendCommand(cmd,FEM);
}
}
}
BroadcastCommand("serve_target 1",FEMArray);//1: send to DAQ
BroadcastCommand("sca enable 1",FEMArray);//Enable data taking
BroadcastCommand("daq 0xFFFFFE F",FEMArray, false);//DAQ request
Expand All @@ -304,6 +307,7 @@ void TRESTDAQFEMINOS::dataTaking() {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
BroadcastCommand("sca enable 0",FEMArray);
BroadcastCommand("serve_target 0",FEMArray);
BroadcastCommand("daq 0xFFFFFF F",FEMArray,false);//Stop DAQ request
std::this_thread::sleep_for(std::chrono::milliseconds(500));//Wait some time till DAQ command is propagated
}
Expand Down Expand Up @@ -427,7 +431,7 @@ void TRESTDAQFEMINOS::ReceiveThread( std::vector<FEMProxy> *FEMA ) {
} else {
std::unique_lock<std::mutex> lock_mem(FEM.mutex_mem);
//const std::deque<uint16_t> frame (&buf_rcv[1], &buf_rcv[size -1]);
FEM.buffer.insert(FEM.buffer.end(), &buf_rcv[1], &buf_rcv[size -1]);
FEM.buffer.insert(FEM.buffer.end(), &buf_rcv[1], &buf_rcv[size]);
int bufferSize = FEM.buffer.size();
lock_mem.unlock();
if (verboseLevel >= TRestStringOutput::REST_Verbose_Level::REST_Debug)
Expand All @@ -446,49 +450,55 @@ void TRESTDAQFEMINOS::ReceiveThread( std::vector<FEMProxy> *FEMA ) {
}

void TRESTDAQFEMINOS::EventBuilderThread(std::vector<FEMProxy> *FEMA, TRestRun *rR, TRestRawSignalEvent* sEvent){
sEvent->Initialize();

sEvent->Initialize();

uint32_t ev_count;
uint64_t ts;

bool newEvent = false;
bool pendingEvent = false;
uint32_t ev_count=0;
uint64_t ts=0;

while (!stopReceiver ){
bool newEvent = true;
bool emptyBuffer = true;

do {
emptyBuffer=true;
for (auto &FEM : *FEMA){
std::unique_lock<std::mutex> lock(FEM.mutex_mem);
if(!FEM.buffer.empty()){//Do nothing
if(FEM.pendingEvent){//Wait till we reach end of event for all the FEMINOS
emptyBuffer &= FEM.buffer.empty();
if(!FEM.buffer.empty()){
if(FEM.pendingEvent){//Wait till we reach end of event for all the ARC
FEM.pendingEvent = !FEMINOSPacket::GetNextEvent( FEM.buffer, sEvent, ts, ev_count);
} else if( !FEM.pendingEvent && !pendingEvent) {
FEM.pendingEvent = !FEMINOSPacket::GetNextEvent( FEM.buffer, sEvent, ts, ev_count);
newEvent=true;
}
}
lock.unlock();
}

pendingEvent=false;

newEvent = true;
for (const auto &FEM : *FEMA){
//std::unique_lock<std::mutex> lock(FEM.mutex_mem);
if(FEM.pendingEvent)pendingEvent=true;//Check if the event is pending
//lock.unlock();
newEvent &= !FEM.pendingEvent;//Check if the event is pending
}

if(newEvent && !pendingEvent){//Save Event if closed
if(newEvent){//Save Event if closed
if(rR){
sEvent->SetID(ev_count);
sEvent->SetTime( rR->GetStartTimestamp() + (double) ts * 2E-8 );
FillTree(rR, sEvent);
sEvent->Initialize();
newEvent = false;
if(event_cnt%100 == 0)std::cout<<"Events "<<event_cnt<<std::endl;
for (auto &FEM : *FEMA)FEM.pendingEvent = true;
}
}

if(emptyBuffer)std::this_thread::sleep_for(std::chrono::milliseconds(100));
} while(!(emptyBuffer && stopReceiver));

//Save pedestal event
if(isPed && emptyBuffer){
if(rR){
sEvent->SetID(ev_count);
sEvent->SetTime( rR->GetStartTimestamp() + (double) ts * 2E-8 );
FillTree(rR, sEvent);
sEvent->Initialize();
}
}
}

}

Expand Down
7 changes: 3 additions & 4 deletions daq/TRESTDAQFEMINOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TRESTDAQFEMINOS : public TRESTDAQ {
TRESTDAQFEMINOS(TRestRun* rR, TRestRawDAQMetadata* dM);

void configure() override;
void startDAQ() override;
void startDAQ(bool configure=true) override;
void stopDAQ() override;
void initialize() override;
void startUp() override;
Expand All @@ -34,12 +34,11 @@ class TRESTDAQFEMINOS : public TRESTDAQ {
static void EventBuilderThread(std::vector<FEMProxy> *FEMA, TRestRun *rR, TRestRawSignalEvent* sEvent);
static void waitForCmd(FEMProxy &FEM);
static std::atomic<bool> stopReceiver;

inline static std::mutex mutex;
static std::atomic<bool> isPed;

private:
void pedestal();
void dataTaking();
void dataTaking(bool configure=true);
void BroadcastCommand(const char* cmd, std::vector<FEMProxy> &FEMA, bool wait=true);
void SendCommand(const char* cmd, FEMProxy &FEM, bool wait=true);

Expand Down
7 changes: 4 additions & 3 deletions daq/TRESTDAQManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void TRESTDAQManager::startUp() {

TRestRawDAQMetadata daqMetadata(sM->cfgFile);

sM->status = 1;
sM->status = 2;
DetachSharedMemory(&sM);

try{
Expand Down Expand Up @@ -186,9 +186,9 @@ void TRESTDAQManager::dataTaking() {
if(daq){
if(parentRunNumber == 0){
daq->configure();
std::cout << "Electronics configured, starting data taking run type " << std::endl;
std::cout << "Electronics configured, starting data taking run type "<<daqMetadata.GetAcquisitionType() << std::endl;
}
daq->startDAQ(); // Should wait till completion or stopped
daq->startDAQ(parentRunNumber == 0); // Should wait till completion or stopped
daq->stopDAQ();
}
} catch(const TRESTDAQException& e) {
Expand Down Expand Up @@ -302,6 +302,7 @@ void TRESTDAQManager::InitializeSharedMemory(sharedMemoryStruct* sM) {
sprintf(sM->cfgFile, "none");
sprintf(sM->runTag, "none");
sprintf(sM->runName, "none");
sprintf(sM->runType, "none");
sM->startDAQ = 0;
sM->startUp = 0;
sM->status = 0;
Expand Down
1 change: 1 addition & 0 deletions restDAQManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void help() {
std::cout << " --e : Exit manager" << std::endl;
std::cout << " --s : Stop run (if ongoing)" << std::endl;
std::cout << " --c : Set configFile (single run)" << std::endl;
std::cout << " --u : Start up electronics (FEMINOS or ARC)" << std::endl;
std::cout << " --h : Print this help" << std::endl;
std::cout << "If no arguments are provided it starts at infinite loop which is controller via shared memory" << std::endl;
}
Expand Down

0 comments on commit c03956b

Please sign in to comment.