REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRun.h
1
2#ifndef RestCore_TRestRun
3#define RestCore_TRestRun
4
5#define REST_MAXIMUM_EVENTS 2E9
6
7#include <TFile.h>
8#include <TKey.h>
9
10#include "TRestAnalysisTree.h"
11#include "TRestEvent.h"
12#include "TRestMetadata.h"
13#include "TRestTools.h"
14
16
18class TRestRun : public TRestMetadata {
19 protected:
20 // run info
21 Int_t fRunNumber; //< first identification number
24 TString fRunClassName;
25 TString fRunType; //< Stores bit by bit the type of run. e.g. calibration, background, pedestal,
26 // simulation, data taking
27 TString fRunUser; //< To identify the author it has created the run. It might be also a word describing
28 // the origin of the run (I.e. REST_Prototype, T-REX, etc)
29 TString fRunTag; //< A tag to be written to the output file
30 TString fRunDescription; //< A word or sentence describing the run (I.e. Fe55 calibration, cosmics, etc)
31 TString fExperimentName;
32
33 // program data
34 TString fInputFileName;
35 TString fOutputFileName;
36 Double_t fStartTime;
37 Double_t fEndTime;
38 Int_t fEntriesSaved;
39 Int_t fNFilesSplit; // Number of files being split. Used when retrieving
40
41 // data-like metadata objects
42 std::vector<TRestMetadata*> fMetadata;
43 std::vector<TRestMetadata*> fInputMetadata;
44
45 // temp data members
46 std::vector<TString> fInputFileNames;
47 TRestRootFileHandle fInputFileOwner;
48 TRestRootFileHandle fOutputFileOwner;
49 TFile* fInputFile;
50 TFile* fOutputFile;
51 TRestEvent* fInputEvent;
52 TTree* fEventTree;
53 TRestAnalysisTree* fAnalysisTree;
54 bool fOverwrite;
55 bool fSaveHistoricData;
56 TRestEventProcess* fFileProcess;
57 int fCurrentEvent;
58 Long64_t fBytesRead;
59 Long64_t fTotalBytes;
60 int fEventBranchLoc;
61 int fEventIndexCounter = 0;
62 bool fHangUpEndFile = false;
63 bool fFromRML = false;
64
65 Long64_t fFeminosDaqTotalEvents = 0;
66
67 void InitFromConfigFile() override;
68
69 private:
70 std::string ReplaceMetadataMember(const std::string& instr, Int_t precision = 0);
71
72 public:
74 void Initialize() override;
75
76 // file operation
77 void OpenInputFile(int i);
78 void OpenInputFile(const TString& filename, const std::string& mode = "");
79 void AddInputFileExternal(const std::string& file);
80 void ReadFileInfo(const std::string& filename);
81 void ReadInputFileMetadata();
82 void ReadInputFileTrees();
83
84 void ResetEntry();
85
86 Int_t GetNextEvent(TRestEvent* targetEvent, TRestAnalysisTree* targetTree);
87
88 void GetEntry(Long64_t entry);
89
90 void GetNextEntry() {
91 if (fCurrentEvent + 1 >= GetEntries()) {
92 fCurrentEvent = -1;
93 }
94 GetEntry(fCurrentEvent + 1);
95 }
96
97 TString FormFormat(const TString& filenameFormat);
98 TFile* MergeToOutputFile(std::vector<std::string> fileFullNames, std::string outputFileName = "");
99 TFile* FormOutputFile();
100 TFile* UpdateOutputFile();
101
102 void PassOutputFile() {
103 if (!fOutputFileOwner.Close())
104 RESTError << "Failed to close the previously owned output file: " << fOutputFileOwner.Error()
105 << RESTendl;
106 fOutputFile = fInputFile;
107 fOutputFileName = fOutputFile->GetName();
108 }
109
110 void WriteWithDataBase();
111
112 void CloseFile();
113
114 void ImportMetadata(const TString& rootFile, const TString& name, const TString& type, Bool_t store);
115
117 inline void AddMetadata(TRestMetadata* meta) {
118 if (meta != nullptr) {
119 fMetadata.push_back(meta);
120 } else {
121 RESTWarning << "REST Warning! A null metadata wants to be added in TRestRun!" << RESTendl;
122 }
123 }
124 void AddEventBranch(TRestEvent* event);
125 void SkipEventTree() {}
126
127 void cd() {
128 if (fInputFile != nullptr) fInputFile->cd();
129 }
130
131 // Getters
132 inline Int_t GetParentRunNumber() const { return fParentRunNumber; }
133 inline Int_t GetSubRunNumber() const { return fParentRunNumber; }
134 inline Int_t GetRunNumber() const { return fRunNumber; }
135 inline TString GetRunType() const { return fRunType; }
136 inline TString GetRunUser() const { return fRunUser; }
137 inline TString GetRunTag() const { return fRunTag; }
138 inline TString GetRunDescription() const { return fRunDescription; }
139 Double_t GetRunLength() const;
140 inline Double_t GetStartTimestamp() const { return fStartTime; }
141 inline Double_t GetEndTimestamp() const { return fEndTime; }
142 inline TString GetExperimentName() const { return fExperimentName; }
143
144 inline std::vector<TString> GetInputFileNames() const { return fInputFileNames; }
145 inline std::string GetInputFileName(unsigned int i) const {
146 return i < fInputFileNames.size() ? (std::string)fInputFileNames[i] : "";
147 }
148 inline std::string GetInputFileNamePattern() const { return fInputFileName.Data(); }
149 inline TString GetOutputFileName() const { return fOutputFileName; }
150 inline TFile* GetInputFile() const { return fInputFile; }
151 inline TFile* GetOutputFile() const { return fOutputFile; }
152 inline int GetCurrentEntry() const { return fCurrentEvent; }
153 inline Long64_t GetBytesRead() const { return fBytesRead; }
154 Long64_t GetTotalBytes();
155 Long64_t GetEntries() const;
156
158 inline TRestEvent* GetInputEvent() const { return fInputEvent; }
160 template <class T>
161 inline T* GetInputEvent() {
162 return static_cast<T*>(fInputEvent);
163 }
164
165 TRestEvent* GetEventWithID(Int_t eventID, Int_t subEventID = -1, const TString& tag = "");
166 std::vector<int> GetEventEntriesWithConditions(const std::string&, int startingIndex = 0,
167 int maxNumber = -1);
168 std::vector<int> GetEventIdsWithConditions(const std::string&, int startingIndex = 0, int maxNumber = -1);
169 TRestEvent* GetNextEventWithConditions(const std::string&);
170 inline TRestEventProcess* GetFileProcess() const { return fFileProcess; }
171 std::string GetRunInformation(const std::string& info);
172 inline Int_t GetObservableID(const std::string& name) { return fAnalysisTree->GetObservableID(name); }
173 inline Bool_t ObservableExists(const std::string& name) { return fAnalysisTree->ObservableExists(name); }
174 inline TString GetInputEventName() const { return fInputEvent->ClassName(); }
175 inline TRestAnalysisTree* GetAnalysisTree() const { return fAnalysisTree; }
176 inline TTree* GetEventTree() const { return fEventTree; }
177 inline Int_t GetInputFileNumber() const { return fFileProcess == nullptr ? fInputFileNames.size() : 1; }
178
179 std::vector<std::string> GetEventTypesList();
180
181 TRestMetadata* GetMetadata(const TString& name, TFile* file = nullptr);
182 TRestMetadata* GetMetadataClass(const TString& type, TFile* file = nullptr);
183 std::vector<std::string> GetMetadataNames();
184 std::vector<std::string> GetMetadataTitles();
185 inline int GetNumberOfMetadata() const { return fMetadata.size(); }
186 Double_t GetElapsedTimeSeconds() const { return fEndTime - fStartTime; }
187
188 inline std::string GetMetadataMember(const std::string& instr) { return ReplaceMetadataMember(instr); }
189 std::string ReplaceMetadataMembers(const std::string& instr, Int_t precision = 8);
190
191 Bool_t EvaluateMetadataMember(const std::string& instr);
192
193 // Setters
194 inline void SetInputFileName(const std::string& s) {
195 fInputFileName = s;
196 fInputFileNames = Vector_cast<std::string, TString>(
197 TRestTools::GetFilesMatchingPattern((std::string)fInputFileName));
198 }
199 inline void SetOutputFileName(const std::string& s) { fOutputFileName = s; }
201 inline void SetCurrentEntry(int i) { fCurrentEvent = i; }
202 void SetInputEvent(TRestEvent* event);
203 inline void SetRunNumber(Int_t number) { fRunNumber = number; }
204 inline void SetParentRunNumber(Int_t number) { fParentRunNumber = number; }
205 inline void SetRunType(const TString& type) {
206 std::string cleanType = RemoveWhiteSpaces((std::string)type);
207 fRunType = (TString)cleanType;
208 }
209 inline void SetRunTag(const TString& tag) {
210 std::string cleanTag = RemoveWhiteSpaces((std::string)tag);
211 fRunTag = (TString)cleanTag;
212 }
213 inline void SetRunUser(const TString& user) {
214 std::string cleanUser = RemoveWhiteSpaces((std::string)user);
215 fRunUser = (TString)cleanUser;
216 }
217 inline void SetRunDescription(const TString& description) { fRunDescription = description; }
218 inline void SetStartTimeStamp(Double_t timestamp) { fStartTime = timestamp; }
219 inline void SetEndTimeStamp(Double_t timestamp) { fEndTime = timestamp; }
220 inline void SetTotalBytes(Long64_t totalBytes) { fTotalBytes = totalBytes; }
221 inline void SetHistoricMetadataSaving(bool save) { fSaveHistoricData = save; }
222 inline void SetNFilesSplit(int n) { fNFilesSplit = n; }
223 inline void HangUpEndFile() { fHangUpEndFile = true; }
224 inline void ReleaseEndFile() { fHangUpEndFile = false; }
225
226 inline void SetFeminosDaqTotalEvents(Long64_t n) { fFeminosDaqTotalEvents = n; }
227 inline Long64_t GetFeminosDaqTotalEvents() const { return fFeminosDaqTotalEvents; }
228
229 // Printers
230 void PrintStartDate();
231 void PrintEndDate();
232
233 void PrintMetadata() override;
234 inline void PrintAllMetadata() {
236 for (unsigned int i = 0; i < fMetadata.size(); i++) {
237 fMetadata[i]->PrintMetadata();
238 }
239 }
240 inline void PrintTrees() const {
241 if (fEventTree != nullptr) {
242 RESTcout << RESTendl;
243 RESTcout << "=====EventTree=====" << RESTendl;
244 fEventTree->Print();
245 }
246 if (fAnalysisTree != nullptr) {
247 RESTcout << RESTendl;
248 RESTcout << "=====AnalysisTree=====" << RESTendl;
249 fAnalysisTree->Print();
250 }
251 }
252 void PrintObservables() {
253 if (fAnalysisTree != nullptr) {
254 fAnalysisTree->PrintObservables();
255 }
256 }
257
258 inline void PrintEvent() const { fInputEvent->PrintEvent(); }
259 void PrintErrors();
260 void PrintWarnings();
261
262 Int_t Write(const char* name = nullptr, Int_t option = 0, Int_t bufsize = 0) override;
263
264 // Constructor & Destructor
265 TRestRun();
266 explicit TRestRun(const std::string& filename);
267 TRestRun(const TRestRun&) = delete;
268 TRestRun& operator=(const TRestRun&) = delete;
269 ~TRestRun();
270
271 ClassDefOverride(TRestRun, 6);
272};
273
274#endif
REST core data-saving helper based on TTree.
Int_t GetObservableID(const std::string &obsName)
Get the index of the specified observable.
Bool_t ObservableExists(const std::string &obsName)
Get if the specified observable exists.
A base class for any REST event process.
A base class for any REST event.
Definition: TRestEvent.h:38
virtual void PrintEvent() const
Definition: TRestEvent.cxx:187
A base class for any REST metadata class.
Definition: TRestMetadata.h:74
endl_t RESTendl
Termination flag object for TRestStringOutput.
bool Close() noexcept
Definition: TRestTools.cxx:662
Data provider and manager in REST.
Definition: TRestRun.h:18
void PrintWarnings()
Prints out all the warnings registered by metadata classes accessible to TRestRun,...
Definition: TRestRun.cxx:2036
void AddEventBranch(TRestEvent *event)
Add an event branch in output EventTree.
Definition: TRestRun.cxx:1390
void OpenInputFile(int i)
Open the i th file in the file list.
Definition: TRestRun.cxx:358
void CloseFile()
Close both input file and output file, setting trees to nullptr also.
Definition: TRestRun.cxx:1251
TFile * FormOutputFile()
Create a new TFile as REST output file. Writing metadata objects into it.
Definition: TRestRun.cxx:1118
void AddMetadata(TRestMetadata *meta)
add metadata object to the metadata list
Definition: TRestRun.h:117
void ResetEntry()
Reset file reading progress.
Definition: TRestRun.cxx:853
TString FormFormat(const TString &filenameFormat)
Form output file name according to file info list, proc info list and run data.
Definition: TRestRun.cxx:1012
TRestEvent * GetInputEvent() const
Calling GetInputEvent() will return a basic TRestEvent*
Definition: TRestRun.h:158
Int_t fParentRunNumber
It can be used as parent number of subrun number.
Definition: TRestRun.h:23
T * GetInputEvent()
Calling GetInputEvent<TRestGeant4Event>() will return a TRestGeant4Event*
Definition: TRestRun.h:161
void SetInputEvent(TRestEvent *event)
Retarget input event in the tree.
Definition: TRestRun.cxx:1353
std::string ReplaceMetadataMember(const std::string &instr, Int_t precision=0)
It will replace the data member from the corresponding metadata class type or name defined in the inp...
Definition: TRestRun.cxx:1811
void GetEntry(Long64_t entry)
Calls GetEntry() for both AnalysisTree and EventTree.
Definition: TRestRun.cxx:980
void InitFromConfigFile() override
Initialize logic of TRestRun.
Definition: TRestRun.cxx:166
void Initialize() override
REST run class.
Definition: TRestRun.cxx:115
void PrintErrors()
Prints out all the warnings registered by metadata classes accessible to TRestRun,...
Definition: TRestRun.cxx:2010
std::string ReplaceMetadataMembers(const std::string &instr, Int_t precision=8)
It will replace the data members contained inside the string given as input. The data members in the ...
Definition: TRestRun.cxx:1760
void PrintMetadata() override
Prints the basic run information.
Definition: TRestRun.cxx:1929
TFile * MergeToOutputFile(std::vector< std::string > fileFullNames, std::string outputFileName="")
Form REST output file by merging a list of files together.
Definition: TRestRun.cxx:1058
std::vector< std::string > GetEventTypesList()
It returns a list of available event types inside the file.
Definition: TRestRun.cxx:964
void ImportMetadata(const TString &rootFile, const TString &name, const TString &type, Bool_t store)
Open the root file and import the metadata of the given name.
Definition: TRestRun.cxx:1406
Bool_t EvaluateMetadataMember(const std::string &instr)
It will evaluate the expression given including the data member from the corresponding metadata class...
Definition: TRestRun.cxx:1876
void SetExtProcess(TRestEventProcess *p)
Set external file process.
Definition: TRestRun.cxx:1310
void PrintStartDate()
Prints the run start date and time in human format.
Definition: TRestRun.cxx:1968
Double_t fStartTime
Event absolute starting time/date (unix timestamp)
Definition: TRestRun.h:36
void WriteWithDataBase()
Write this object into TFile and add a new entry in database.
Definition: TRestRun.cxx:1188
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
overwriting the write() method with fStore considered
Definition: TRestRun.cxx:1455
void ReadFileInfo(const std::string &filename)
Extract file info from a file, and save it in the file info list.
Definition: TRestRun.cxx:724
Double_t fEndTime
Event absolute ending time/date (unix timestamp)
Definition: TRestRun.h:37
Int_t GetNextEvent(TRestEvent *targetEvent, TRestAnalysisTree *targetTree)
Get next event by writing event data into target event and target tree.
Definition: TRestRun.cxx:868
TRestEvent * GetNextEventWithConditions(const std::string &)
Load the next event that satisfies the conditions specified by a string.
Definition: TRestRun.cxx:1633
static std::vector< std::string > GetFilesMatchingPattern(std::string pattern, bool unlimited=false)
Returns a list of files whose name match the pattern string. Key word is "*". e.g....
std::string RemoveWhiteSpaces(std::string in)
Returns the input string removing all white spaces.