REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRealTimeAddInputFileProcess.cxx
1 /*************************************************************************
2  * This file is part of the REST software framework. *
3  * *
4  * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5  * For more information see http://gifna.unizar.es/trex *
6  * *
7  * REST is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * REST is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have a copy of the GNU General Public License along with *
18  * REST in $REST_PATH/LICENSE. *
19  * If not, see http://www.gnu.org/licenses/. *
20  * For the list of contributors see $REST_PATH/CREDITS. *
21  *************************************************************************/
66 
67 #include "TRestRealTimeAddInputFileProcess.h"
68 
69 #ifdef __APPLE__
70 #include <unistd.h>
71 #endif
72 
73 using namespace std;
74 
76 
77 thread* TRestRealTimeAddInputFileProcess::fMonitorThread = nullptr;
78 int TRestRealTimeAddInputFileProcess::fMonitorFlag = 0;
79 
80 TRestRealTimeAddInputFileProcess::TRestRealTimeAddInputFileProcess() { Initialize(); }
81 
82 TRestRealTimeAddInputFileProcess::~TRestRealTimeAddInputFileProcess() {}
83 
85  SetSectionName(this->ClassName());
86  fEvent = nullptr;
87 
88  // Assign initial values for the parameters here
89 }
90 
92  // Write here the jobs to do before processing
93  fMessenger = GetMetadata<TRestMessenger>();
94 
95  if (fMessenger == nullptr) {
96  RESTError << "messenger not found!" << RESTendl;
97  exit(1);
98  }
99 
100  if (fMonitorThread == nullptr) {
101  fRunInfo->HangUpEndFile();
102  fMonitorFlag = 1;
103  // SysMonitorFunc(fPid, fRefreshRate);
104  fMonitorThread = new thread(&TRestRealTimeAddInputFileProcess::FileNotificationFunc, this);
105  fMonitorThread->detach();
106  }
107 }
108 
110  fEvent = (TRestEvent*)inputEvent;
111 
112  // Write here the main logic of process: TRestRealTimeAddInputFileProcess
113  // Read data from input event, write data to output event, and save observables to tree
114 
115  return fEvent;
116 }
117 
118 void TRestRealTimeAddInputFileProcess::FileNotificationFunc() {
119  while (fMonitorFlag == 1) {
120  string message = fMessenger->ConsumeMessage();
121  if (message != "") {
122  RESTEssential << "Recieveing message: " << message << RESTendl;
123  // 7788->/data2/2MM/M1/graw/CoBo_AsAd1_2020-07-19T13:50:49.519_0000.graw
124  // 7788->finish
125 
126  auto runid_filename = Split(message, "->");
127  if (runid_filename.size() == 2) {
128  int runid = atoi(runid_filename[0].c_str());
129  if (runid == fRunInfo->GetRunNumber()) {
130  string msgContent = runid_filename[1];
131  if (TRestTools::fileExists(msgContent)) {
132  RESTEssential << "Adding file... " << RESTendl;
133  fRunInfo->AddInputFileExternal(msgContent);
134  } else if (msgContent == "finish") {
135  fRunInfo->ReleaseEndFile();
136  } else {
137  RESTWarning << "illegal message!" << RESTendl;
138  }
139  } else {
140  // if the runnumber does not match, we put this message back to pool
141  // maybe other processes need it
142  fMessenger->SendMessage(message);
143  }
144  } else {
145  RESTWarning << "illegal message!" << RESTendl;
146  }
147  }
148  usleep(1000);
149  }
150 }
151 
153  // Write here the jobs to do when all the events are processed
154  if (fMonitorThread != nullptr) {
155  fMonitorFlag = 0;
156  usleep(2000);
157  // fMonitorThread->join();
158  delete fMonitorThread;
159  fMonitorThread = nullptr;
160  }
161 }
162 
165  // Don't need to write code here besides the advanced logic of parameter loading
166 }
virtual void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
A base class for any REST event.
Definition: TRestEvent.h:38
void InitFromConfigFile() override
//0: return, 1: run
void Initialize() override
Making default settings.
void EndProcess() override
To be executed at the end of the run (outside event loop)
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
Definition: TRestTools.cxx:728
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.