REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRawToSignalProcess.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  *************************************************************************/
22 
59 #include "TRestRawToSignalProcess.h"
60 
61 #include <sys/stat.h>
62 
63 using namespace std;
64 
65 #include "TTimeStamp.h"
66 
67 ClassImp(TRestRawToSignalProcess);
68 
69 TRestRawToSignalProcess::TRestRawToSignalProcess() { Initialize(); }
70 
71 TRestRawToSignalProcess::TRestRawToSignalProcess(const char* configFilename) {
72  Initialize();
73 
74  if (LoadConfigFromFile(configFilename)) LoadDefaultConfig();
75 }
76 
77 TRestRawToSignalProcess::~TRestRawToSignalProcess() {
78  // TRestRawToSignalProcess destructor
79  delete fSignalEvent;
80 }
81 
82 void TRestRawToSignalProcess::LoadConfig(const string& configFilename, const string& name) {
83  if (LoadConfigFromFile(configFilename, name) == -1) {
84  cout << "Loading default" << endl;
85  LoadDefaultConfig();
86  }
87 }
88 
90  SetSectionName(this->ClassName());
91  SetLibraryVersion(LIBRARY_VERSION);
92 
93  delete fSignalEvent;
94  fSignalEvent = new TRestRawSignalEvent();
95 
96  fInputBinFile = nullptr;
97 
98  fMinPoints = 512;
99 
100  fSingleThreadOnly = true;
101  fIsExternal = true;
102  fgKeepFileOpen = true;
103 
104  totalBytes = 0;
105  totalBytesReaded = 0;
106 }
107 
109  fElectronicsType = GetParameter("electronics");
110  fShowSamples = StringToInteger(GetParameter("showSamples", "10"));
111  fMinPoints = StringToInteger(GetParameter("minPoints", "512"));
112 
113  PrintMetadata();
114 
115  if (fElectronicsType == "SingleFeminos" || fElectronicsType == "TCMFeminos" || fElectronicsType == "TDS")
116  return;
117 
118  if (fElectronicsType == "FEUDream") {
119  fgKeepFileOpen = false;
120  return;
121  }
122 
123  if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning) {
124  cout << "REST WARNING: TRestRawToSignalProcess::InitFromConfigFile" << endl;
125  cout << "Electronic type " << fElectronicsType << " not found " << endl;
126  // cout << "Loading default config" << endl;
127  }
128 
129  LoadDefaultConfig();
130 }
131 
132 void TRestRawToSignalProcess::LoadDefaultConfig() {
133  fElectronicsType = "SingleFeminos";
134  fMinPoints = 512;
135 }
136 
137 Bool_t TRestRawToSignalProcess::OpenInputFiles(const vector<string>& files) {
138  nFiles = 0;
139  fInputFiles.clear();
140  fInputFileNames.clear();
141  totalBytes = 0;
142  totalBytesReaded = 0;
143 
144  for (const auto& file : files) {
145  AddInputFile(file);
146  }
147 
148  if (nFiles > 0) {
149  fInputBinFile = fInputFiles[0];
150  } else {
151  RESTError << "No input file is opened, in process: " << this->ClassName() << "!" << RESTendl;
152  exit(1);
153  }
154 
155  RESTDebug << this->GetName() << " : opened " << nFiles << " files" << RESTendl;
156  return nFiles;
157 }
158 
159 Bool_t TRestRawToSignalProcess::AddInputFile(const string& file) {
160  for (auto& inputFileName : fInputFileNames) {
161  if (inputFileName == file) {
162  RESTError << "file: \"" << file << "\" already added!" << RESTendl;
163  return false;
164  }
165  }
166 
167  FILE* f = fopen(file.c_str(), "rb");
168 
169  if (f == nullptr) {
170  RESTWarning << "REST WARNING. Input file for " << this->ClassName() << " does not exist!" << RESTendl;
171  RESTWarning << "File : " << file << RESTendl;
172  return false;
173  }
174 
175  fInputFiles.push_back(f);
176  fInputFileNames.push_back(file);
177 
178  struct stat statbuf;
179  stat(file.c_str(), &statbuf);
180  totalBytes += statbuf.st_size;
181 
182  nFiles++;
183 
184  return true;
185 }
186 
187 Bool_t TRestRawToSignalProcess::ResetEntry() {
188  for (auto f : fInputFiles) {
189  if (f != nullptr) {
190  if (fseek(f, 0, 0) != 0) return false;
191  }
192  }
193  InitProcess();
194 
195  return true;
196 }
197 
199  BeginPrintProcess();
200 
201  RESTMetadata << " " << RESTendl;
202  RESTMetadata << " ==================================== " << RESTendl;
203  RESTMetadata << "DAQ : " << GetTitle() << RESTendl;
204  RESTMetadata << "Electronics type : " << fElectronicsType << RESTendl;
205  RESTMetadata << "Minimum number of points : " << fMinPoints << RESTendl;
206  RESTMetadata << "All raw files open at beginning : " << fgKeepFileOpen << RESTendl;
207  RESTMetadata << " ==================================== " << RESTendl;
208 
209  RESTMetadata << " " << RESTendl;
210 
211  EndPrintProcess();
212 }
213 
214 Bool_t TRestRawToSignalProcess::GoToNextFile() {
215  iCurFile++;
216  if (iCurFile < nFiles) {
217  if (fgKeepFileOpen) {
218  fInputBinFile = fInputFiles[iCurFile];
219  } else {
220  fclose(fInputBinFile);
221  fInputBinFile = fopen(fInputFileNames[iCurFile].c_str(), "rb");
222  }
223  RESTInfo << "GoToNextFile(): Going to the next raw input file number " << iCurFile << " over "
224  << nFiles << RESTendl;
225  RESTInfo << " Reading file name: " << fInputFileNames[iCurFile] << RESTendl;
226  return true;
227  } else {
228  RESTInfo << "GoToNextFile(): No more file to read" << RESTendl;
229  }
230  return false;
231 }
An event container for time rawdata signals with fixed length.
A base class for any process reading a binary external file as input to REST.
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
void Initialize() override
Making default settings.
virtual void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
Int_t StringToInteger(std::string in)
Gets an integer from a string.