108 #include "TRestRawMemoryBufferToSignalProcess.h"
119 #if (defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)) || __APPLE__
124 struct semid_ds* buf;
125 unsigned short int* array;
126 struct seminfo* __buf;
130 struct sembuf Operacion;
154 LoadConfig(configFilename);
167 Operacion.sem_num = 0;
168 Operacion.sem_op = -1;
169 Operacion.sem_flg = 0;
171 semop(
id, &Operacion, 1);
179 Operacion.sem_num = 0;
180 Operacion.sem_op = 1;
181 Operacion.sem_flg = 0;
183 semop(
id, &Operacion, 1);
190 SetName(
"sharedMemoryBufferToSignal-Default");
191 SetTitle(
"Default config");
193 cout <<
"SharedMemoryBufferToSignal metadata not found. Loading default "
211 if (LoadConfigFromFile(configFilename, name)) {
221 SetSectionName(this->ClassName());
222 SetLibraryVersion(LIBRARY_VERSION);
230 cout <<
"TRestRawMemoryBufferToSignalProcess::InitProcess. Creating "
231 "access to shared memory"
234 key_t MemKey = ftok(
"/bin/ls", fKeyDaqInfo);
235 int memId = shmget(MemKey,
sizeof(daqInfo), 0777);
237 printf(
"Failed to access daqInfo resource\n");
241 fShMem_daqInfo = (daqInfo*)shmat(memId, (
char*)0, 0);
243 key_t SemaphoreKey = ftok(
"/bin/ls", fKeySemaphore);
244 fSemaphoreId = semget(SemaphoreKey, 1, 0777);
246 if (fSemaphoreId == -1) {
247 printf(
"Failed to access semaphore resource\n");
252 printf(
"Sem id : %d\n", fSemaphoreId);
254 printf(
"Data ready : %d\n", fShMem_daqInfo->dataReady);
256 printf(
"Number of signals : %d\n", fShMem_daqInfo->nSignals);
258 printf(
"Timestamp : %lf\n", fShMem_daqInfo->timeStamp);
260 printf(
"Event id : %d\n", fShMem_daqInfo->eventId);
262 printf(
"Max signals : %d\n", fShMem_daqInfo->maxSignals);
263 printf(
"Max samples : %d\n", fShMem_daqInfo->maxSamples);
264 printf(
"Buffer size : %d\n", fShMem_daqInfo->bufferSize);
269 int N_DATA = fShMem_daqInfo->bufferSize;
271 MemKey = ftok(
"/bin/ls", fKeyBuffer);
272 memId = shmget(MemKey, N_DATA *
sizeof(
unsigned short int), 0777 | IPC_CREAT);
274 printf(
"Failed to access buffer resource\n");
278 fShMem_Buffer = (
unsigned short int*)shmat(memId, (
char*)0, 0);
286 fOutputRawSignalEvent->Initialize();
294 SemaphoreRed(fSemaphoreId);
295 int dataReady = fShMem_daqInfo->dataReady;
296 int maxSamples = fShMem_daqInfo->maxSamples;
297 SemaphoreGreen(fSemaphoreId);
302 if (dataReady == 2) {
304 SemaphoreRed(fSemaphoreId);
306 for (
unsigned int s = 0; s < fShMem_daqInfo->nSignals; s++) {
308 signal.
SetSignalID(fShMem_Buffer[s * (maxSamples + 1)]);
311 cout <<
"s : " << s <<
" id : " << signal.
GetSignalID() << endl;
313 for (
int n = 0; n < maxSamples; n++) {
314 signal.
AddPoint((Short_t)fShMem_Buffer[s * (maxSamples + 1) + 1 + n]);
316 fOutputRawSignalEvent->AddSignal(signal);
325 for (
unsigned int n = 0; n < fShMem_daqInfo->bufferSize; n++) fShMem_Buffer[n] = 0;
328 fShMem_daqInfo->dataReady = 0;
330 fOutputRawSignalEvent->SetID(fShMem_daqInfo->eventId);
331 fOutputRawSignalEvent->SetTime(fShMem_daqInfo->timeStamp);
333 SemaphoreGreen(fSemaphoreId);
337 cout <<
"------------------------------------------" << endl;
338 cout <<
"Event ID : " << fOutputRawSignalEvent->GetID() << endl;
339 cout <<
"Time stamp : " << fOutputRawSignalEvent->GetTimeStamp() << endl;
340 cout <<
"Number of Signals : " << fOutputRawSignalEvent->GetNumberOfSignals() << endl;
341 cout <<
"------------------------------------------" << endl;
344 for (Int_t n = 0; n < fOutputRawSignalEvent->GetNumberOfSignals(); n++)
345 cout <<
"Signal N : " << n
346 <<
" daq id : " << fOutputRawSignalEvent->GetSignal(n)->GetID() << endl;
351 if (fOutputRawSignalEvent->GetNumberOfSignals() == 0)
return nullptr;
353 return fOutputRawSignalEvent;
357 return fOutputRawSignalEvent;
A base class for any REST event.
void SemaphoreRed(int id)
This method will increase the semaphore red level to protect shared memory regions.
TRestRawMemoryBufferToSignalProcess()
Default constructor.
void Initialize() override
Function to initialize input/output event members and define the section name.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
void BeginOfEventProcess(TRestEvent *inputEvent=nullptr) override
Function including required initialization before each event starts to process.
void InitFromConfigFile() override
Function reading input parameters from the RML TRestRawMemoryBufferToSignalProcess metadata section.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
void SemaphoreGreen(int id)
This method will increase the semaphore green level to release shared memory regions.
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
~TRestRawMemoryBufferToSignalProcess()
Default destructor.
An event container for time rawdata signals with fixed length.
It defines a Short_t array with a physical parameter that evolves in time using a fixed time bin.
void Print() const
It prints the signal data on screen.
void SetSignalID(Int_t sID)
It sets the id number of the signal.
void AddPoint(Short_t)
Adds a new point to the end of the signal data array.
Int_t GetSignalID() const
Returns the value of signal ID.
@ REST_Extreme
show everything
@ REST_Info
+show most of the information for each steps
@ REST_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Int_t StringToInteger(std::string in)
Gets an integer from a string.