109#include "TRestEventTimeSelectionProcess.h"
128 fIsActiveTime =
true;
130 fStartEndTimes.clear();
131 fTimeOffsetInSeconds = 0;
132 fTimeStartMarginInSeconds = 0;
133 fTimeEndMarginInSeconds = 0;
135 fNEventsSelected = 0;
136 fTotalTimeInSeconds = 0;
145 if (!fFileWithTimes.empty()) {
146 fStartEndTimes = ReadFileWithTimes(fFileWithTimes, fDelimiter);
150 fNEventsSelected = 0;
153std::vector<std::pair<std::string, std::string>> TRestEventTimeSelectionProcess::ReadFileWithTimes(
154 std::string fileWithTimes, Char_t delimiter) {
155 std::vector<std::pair<std::string, std::string>> startEndTimes;
157 ifstream file(fileWithTimes);
158 if (file.is_open()) {
159 while (getline(file, line)) {
160 if (line[0] ==
'#') {
163 std::istringstream lineStream(line);
164 std::string startDate, endDate;
165 if (std::getline(lineStream, startDate, delimiter) &&
166 std::getline(lineStream, endDate, delimiter)) {
173 startEndTimes.emplace_back(startDate, endDate);
178 return startEndTimes;
187 Double_t totalTime = 0;
188 for (
auto id : fStartEndTimes) {
192 startTime.Add(TTimeStamp(fTimeStartMarginInSeconds));
193 endTime.Add(TTimeStamp(-fTimeEndMarginInSeconds));
194 auto timeDiff = endTime.AsDouble() - startTime.AsDouble();
196 RESTDebug <<
"End time is before start time in time range: " <<
id.first <<
" to " <<
id.second
200 totalTime += endTime.AsDouble() - startTime.AsDouble();
211 TTimeStamp eventTime = fEvent->GetTimeStamp();
212 eventTime.Add(TTimeStamp(fTimeOffsetInSeconds));
214 Bool_t isInsideAnyTimeRange =
false;
215 for (
auto id : fStartEndTimes) {
219 startTime.Add(TTimeStamp(fTimeStartMarginInSeconds));
220 endTime.Add(TTimeStamp(-fTimeEndMarginInSeconds));
221 if (eventTime >= startTime && eventTime <= endTime) {
222 isInsideAnyTimeRange =
true;
230 if (isInsideAnyTimeRange) {
235 if (!isInsideAnyTimeRange) {
267 Bool_t useMargins, Int_t nTimes) {
268 std::string timeCut =
"";
269 std::string timeStampObsNameWithOffset = timeStampObsName;
270 if (useOffset && fTimeOffsetInSeconds != 0) {
271 timeStampObsNameWithOffset +=
"+" + to_string(fTimeOffsetInSeconds);
273 if (nTimes < 0) nTimes = fStartEndTimes.size();
275 for (
auto id : fStartEndTimes) {
276 if (c++ >= nTimes)
break;
281 startTime += fTimeStartMarginInSeconds;
282 endTime -= fTimeEndMarginInSeconds;
285 if (startTime >= endTime) {
290 if (!timeCut.empty()) {
296 if (!fIsActiveTime) timeCut +=
"!";
299 timeCut +=
"(" + timeStampObsNameWithOffset +
">=" + to_string(startTime) +
")";
301 timeCut +=
"(" + timeStampObsNameWithOffset +
"<=" + to_string(endTime) +
")";
311 std::string typeOfTime = fIsActiveTime ?
"Active" :
"Dead";
313 RESTMetadata <<
"File with times: " << fFileWithTimes <<
RESTendl;
315 RESTMetadata <<
"Offset time: " << fTimeOffsetInSeconds <<
" seconds" <<
RESTendl;
316 RESTMetadata <<
"Start margin time: " << fTimeStartMarginInSeconds <<
" seconds" <<
RESTendl;
317 RESTMetadata <<
"End margin time: " << fTimeEndMarginInSeconds <<
" seconds" <<
RESTendl;
318 RESTMetadata << typeOfTime <<
" time periods: " <<
RESTendl;
319 for (
auto id : fStartEndTimes) {
320 RESTMetadata <<
id.first <<
" to " <<
id.second <<
RESTendl;
326 TTimeStamp totalTime = TTimeStamp(fTotalTimeInSeconds, 0);
327 if (!fStartEndTimes.empty()) {
328 TTimeStamp firstTime = TTimeStamp(
StringToTimeStamp(fStartEndTimes.front().first), 0);
329 TTimeStamp lastTime = TTimeStamp(
StringToTimeStamp(fStartEndTimes.back().second), 0);
330 totalTime = lastTime - firstTime;
333 double fractionOfTime = fTotalTimeInSeconds / totalTime.AsDouble() * 100;
334 std::string fractionOfTimeStr = StringWithPrecision(fractionOfTime, 4) +
" %";
335 if ((Int_t)(fTotalTimeInSeconds / 24 / 3600) != 0)
336 RESTMetadata <<
"Total " << typeOfTime <<
" time: " << fTotalTimeInSeconds / 24 / 3600 <<
" days"
337 <<
" (" << fractionOfTimeStr <<
")" <<
RESTendl;
338 else if ((Int_t)(fTotalTimeInSeconds / 3600) != 0)
339 RESTMetadata <<
"Total " << typeOfTime <<
" time: " << fTotalTimeInSeconds / 3600 <<
" hours"
340 <<
" (" << fractionOfTimeStr <<
")" <<
RESTendl;
341 else if ((Int_t)(fTotalTimeInSeconds / 60) != 0)
342 RESTMetadata <<
"Total " << typeOfTime <<
" time: " << fTotalTimeInSeconds / 60 <<
" minutes"
343 <<
" (" << fractionOfTimeStr <<
")" <<
RESTendl;
345 RESTMetadata <<
"Total " << typeOfTime <<
" time: " << fTotalTimeInSeconds <<
" seconds"
346 <<
" (" << fractionOfTimeStr <<
")" <<
RESTendl;
350 RESTMetadata <<
"Number of events selected: " << fNEventsSelected <<
" ("
void BeginPrintProcess()
[name, cut range]
Double_t CalculateTotalTimeInSeconds()
Function to calculate the total time in seconds of all the time ranges (active or dead periods of tim...
TRestEventTimeSelectionProcess()
Default constructor.
void PrintMetadata() override
Prints on screen the process data members.
void EndProcess() override
Function to include required actions after all events have been processed.
Int_t fNEventsRejected
Information about the events processed.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void Initialize() override
Function to initialize input/output event members and define the section name.
std::string GetTimeStampCut(std::string timeStampObsName="timeStamp", Bool_t useOffset=true, Bool_t useMargins=true, Int_t nTimes=-1)
Function to get the cut string that reproduce the time selection done by this process (useful for TRe...
void InitProcess() override
Process initialization.
A base class for any REST event.
time_t StringToTimeStamp(std::string time)
A method to convert a date/time formatted string to a timestamp.