Skip to content

Commit

Permalink
implement start and end time margins
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroEzq committed Jan 14, 2025
1 parent 36ec98a commit 6ff8b6a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
18 changes: 13 additions & 5 deletions source/framework/analysis/inc/TRestEventTimeSelectionProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class TRestEventTimeSelectionProcess : public TRestEventProcess {
std::string fFileWithTimes;
Bool_t fIsActiveTime;
Char_t fDelimiter;
Long_t fOffsetTimeInSeconds;
std::vector<std::pair<std::string, std::string>> fStartEndTimes;
Long_t fTimeOffsetInSeconds;
Long_t fTimeStartMarginInSeconds;
Long_t fTimeEndMarginInSeconds;
std::vector<std::pair<std::string,std::string>> fStartEndTimes;

/// Information about the events processed

Expand Down Expand Up @@ -73,13 +75,19 @@ class TRestEventTimeSelectionProcess : public TRestEventProcess {
Int_t GetNEventsRejected() const { return fNEventsRejected; }
Int_t GetNEventsSelected() const { return fNEventsSelected; }
Double_t GetTotalTimeInSeconds() const { return fTotalTimeInSeconds; }
Long_t GetTimeOffsetInSeconds() const { return fTimeOffsetInSeconds; }
Long_t GetTimeStartMarginInSeconds() const { return fTimeStartMarginInSeconds; }
Long_t GetTimeEndMarginInSeconds() const { return fTimeEndMarginInSeconds; }

Double_t CalculateTotalTimeInSeconds();

void SetFileWithTimes(const std::string& fileWithTimes) { fFileWithTimes = fileWithTimes; }
void SetIsActiveTime(Bool_t isActiveTime) { fIsActiveTime = isActiveTime; }
void SetDelimiter(Char_t delimiter) { fDelimiter = delimiter; }
void SetStartEndTimes(const std::vector<std::pair<std::string, std::string>>& startEndTimes) {
fStartEndTimes = startEndTimes;
}
void SetStartEndTimes(const std::vector<std::pair<std::string,std::string>>& startEndTimes) { fStartEndTimes = startEndTimes; }
void SetTimeOffsetInSeconds(Long_t timeOffsetInSeconds) { fTimeOffsetInSeconds = timeOffsetInSeconds; }
void SetTimeStartMarginInSeconds(Long_t timeStartMarginInSeconds) { fTimeStartMarginInSeconds = timeStartMarginInSeconds; }
void SetTimeEndMarginInSeconds(Long_t timeEndMarginInSeconds) { fTimeEndMarginInSeconds = timeEndMarginInSeconds; }

ClassDefOverride(TRestEventTimeSelectionProcess, 1);
};
Expand Down
48 changes: 36 additions & 12 deletions source/framework/analysis/src/TRestEventTimeSelectionProcess.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ void TRestEventTimeSelectionProcess::Initialize() {
fIsActiveTime = true;
fDelimiter = ',';
fStartEndTimes.clear();
fOffsetTimeInSeconds = 0;
fTimeOffsetInSeconds = 0;
fTimeStartMarginInSeconds = 0;
fTimeEndMarginInSeconds = 0;
fNEventsRejected = 0;
fNEventsSelected = 0;
fTotalTimeInSeconds = 0;
Expand Down Expand Up @@ -144,14 +146,27 @@ void TRestEventTimeSelectionProcess::InitProcess() {
file.close();
}
}
fTotalTimeInSeconds = 0;
fTotalTimeInSeconds = CalculateTotalTimeInSeconds();
fNEventsRejected = 0;
fNEventsSelected = 0;
}

Double_t TRestEventTimeSelectionProcess::CalculateTotalTimeInSeconds(){
Double_t totalTime = 0;
for (auto id : fStartEndTimes) {
TTimeStamp startTime = TTimeStamp(StringToTimeStamp(id.first), 0);
TTimeStamp endTime = TTimeStamp(StringToTimeStamp(id.second), 0);
fTotalTimeInSeconds += endTime.AsDouble() - startTime.AsDouble();
// Reduce the time by the margin in both sides
startTime.Add(TTimeStamp(fTimeStartMarginInSeconds));
endTime.Add(TTimeStamp(-fTimeEndMarginInSeconds));
auto timeDiff = endTime.AsDouble() - startTime.AsDouble();
if (timeDiff < 0) {
RESTDebug << "End time is before start time in time range: " << id.first << " to " << id.second << RESTendl;
continue;
}
totalTime += endTime.AsDouble() - startTime.AsDouble();
}
fNEventsRejected = 0;
fNEventsSelected = 0;
return totalTime;
}

///////////////////////////////////////////////
Expand All @@ -166,6 +181,9 @@ TRestEvent* TRestEventTimeSelectionProcess::ProcessEvent(TRestEvent* inputEvent)
for (auto id : fStartEndTimes) {
TTimeStamp startTime = TTimeStamp(StringToTimeStamp(id.first), 0);
TTimeStamp endTime = TTimeStamp(StringToTimeStamp(id.second), 0);
// Reduce the time by the margin in both sides
startTime.Add(TTimeStamp(fTimeStartMarginInSeconds));
endTime.Add(TTimeStamp(-fTimeEndMarginInSeconds));
if (eventTime >= startTime && eventTime <= endTime) {
fNEventsSelected++;
return fEvent;
Expand All @@ -178,6 +196,9 @@ TRestEvent* TRestEventTimeSelectionProcess::ProcessEvent(TRestEvent* inputEvent)
for (auto id : fStartEndTimes) {
TTimeStamp startTime = TTimeStamp(StringToTimeStamp(id.first), 0);
TTimeStamp endTime = TTimeStamp(StringToTimeStamp(id.second), 0);
// Reduce the time by the margin in both sides
startTime.Add(TTimeStamp(fTimeStartMarginInSeconds));
endTime.Add(TTimeStamp(-fTimeEndMarginInSeconds));
if (eventTime >= startTime && eventTime <= endTime) {
isInDeadPeriod = true;
break;
Expand Down Expand Up @@ -218,19 +239,20 @@ std::string TRestEventTimeSelectionProcess::GetTimeStampCut(std::string timeStam
std::string timeCut = "";
std::string timeStampObsNameWithOffset = timeStampObsName;
if (useOffset) {
timeStampObsNameWithOffset += "+" + to_string(fOffsetTimeInSeconds);
timeStampObsNameWithOffset += "+" + to_string(fTimeOffsetInSeconds);
}
if (nTimes < 0) nTimes = fStartEndTimes.size();
Int_t c = 0;
for (auto id : fStartEndTimes) {
if (c++ >= nTimes) break;
auto startTime = StringToTimeStamp(id.first);
auto endTime = StringToTimeStamp(id.second);
if (!timeCut.empty()) {
if (fIsActiveTime)
timeCut += " || ";
else
timeCut += " && ";
// Reduce the time by the margin in both sides
startTime += fTimeStartMarginInSeconds;
endTime -= fTimeEndMarginInSeconds;
if (!timeCut.empty()){
if (fIsActiveTime) timeCut += " || ";
else timeCut += " && ";
}
if (!fIsActiveTime) timeCut += "!";
timeCut += "(";
Expand All @@ -250,7 +272,9 @@ void TRestEventTimeSelectionProcess::PrintMetadata() {

RESTMetadata << "File with times: " << fFileWithTimes << RESTendl;
// print periods
RESTMetadata << "Offset time: " << fOffsetTimeInSeconds << " seconds" << RESTendl;
RESTMetadata << "Offset time: " << fTimeOffsetInSeconds << " seconds" << RESTendl;
RESTMetadata << "Start margin time: " << fTimeStartMarginInSeconds << " seconds" << RESTendl;
RESTMetadata << "End margin time: " << fTimeEndMarginInSeconds << " seconds" << RESTendl;
RESTMetadata << typeOfTime << " time periods: " << RESTendl;
for (auto id : fStartEndTimes) {
RESTMetadata << id.first << " to " << id.second << RESTendl;
Expand Down

0 comments on commit 6ff8b6a

Please sign in to comment.