REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestEventProcess.h
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
23#ifndef RestCore_TRestEventProcess
24#define RestCore_TRestEventProcess
25
26#include <TCanvas.h>
27#include <TNamed.h>
28
29#include <limits>
30
31#include "TRestAnalysisTree.h"
32#include "TRestEvent.h"
33#include "TRestMetadata.h"
34#include "TRestRun.h"
35
38 protected:
45 };
46
47 private:
48 // unused datamember, set as private
55
59 std::vector<TRestEventProcess*> fFriendlyProcesses;
61 std::vector<TRestEventProcess*> fParallelProcesses;
62 protected:
64 TCanvas* fCanvas = nullptr;
66 TVector2 fCanvasSize;
74 TRestRun* fRunInfo = nullptr;
76 bool fIsExternal = false;
80 bool fSingleThreadOnly = false;
82 bool fReadOnly = false;
84 bool fDynamicObs = false;
86 bool fValidateObservables = false;
88 std::map<std::string, int> fObservablesUpdated;
90 std::map<std::string, int> fObservablesDefined;
92 std::vector<std::pair<std::string, TVector2>> fCuts;
93
94 // utils
96 void EndPrintProcess();
103 template <class T>
104 inline T* GetMetadata() {
105 std::string type = REST_Reflection::GetTypeName<T>();
106 return (T*)GetMetadata(type);
107 }
108 TRestMetadata* GetMetadata(const std::string& nameOrType);
109 TRestEventProcess* GetFriend(const std::string& nameOrType);
110 TRestEventProcess* GetFriendLive(const std::string& nameOrType);
111 inline size_t GetNumberOfParallelProcesses() const { return fParallelProcesses.size(); }
112 TRestEventProcess* GetParallel(int i);
127 template <class T>
128 std::vector<T> GetParallelDataMembers(T* member_of_process) {
129 std::vector<T> result;
130 int offset = (int)((char*)member_of_process - (char*)this);
131 if (offset <= 0 || offset > this->IsA()->GetClassSize()) {
132 std::cout << this->ClassName() << "::GetParallelDataMembers(): invalid object input!"
133 << std::endl;
134 return result;
135 }
136 for (int i = 0; i < GetNumberOfParallelProcesses(); i++) {
137 char* proc = (char*)GetParallel(i);
138 if (proc == (char*)this) continue;
139
140 result.push_back(*(T*)(proc + offset));
141 }
142 return result;
143 }
150 template <class T>
151 inline void SetObservableValue(const std::string& name, const T& value) {
152 if (fAnalysisTree == nullptr) {
153 return;
154 }
155
156 std::string obsName = std::string(this->GetName()) + "_" + name;
157 if (name == "t1t2") std::cout << "Observable: " << obsName << std::endl;
158
160 int id = fAnalysisTree->GetObservableID(obsName);
161 std::cout << "Id: " << id << std::endl;
162
163 if (id != -1) {
164 fObservablesDefined[obsName] = id;
165 fObservablesUpdated[obsName] = id;
166 fAnalysisTree->SetObservable(obsName, value);
167 } else if (fDynamicObs) {
168 fAnalysisTree->SetObservable(obsName, value);
169 int n = fAnalysisTree->GetObservableID(obsName);
170 if (n != -1) {
171 fObservablesDefined[obsName] = id;
172 fObservablesUpdated[obsName] = id;
173 }
174 }
175 } else {
176 int id = fAnalysisTree->GetObservableID(obsName);
177 if (id != -1) {
179 } else if (fDynamicObs) {
180 fAnalysisTree->SetObservableValue(obsName, value);
181 }
182 }
183 }
184
185 template <class T>
186 T GetObservableValue(const std::string& name) {
187 if (fAnalysisTree != nullptr) {
188 return fAnalysisTree->GetObservableValue<T>(name);
189 }
190 return std::numeric_limits<T>::quiet_NaN();
191 }
192
194 inline void CreateCanvas() {
195 if (fCanvas != nullptr) {
196 return;
197 }
198 fCanvas = new TCanvas(this->GetName(), this->GetTitle(), fCanvasSize.X(), fCanvasSize.Y());
199 }
200
201 public:
202 bool ApplyCut();
203
204 virtual const char* GetProcessName() const = 0;
205 Int_t LoadSectionMetadata() override;
206 virtual void InitFromConfigFile() override {
207 std::map<std::string, std::string> parameters = GetParametersList();
208 for (auto& p : parameters) {
210 }
211 ReadParametersList(parameters);
212 }
213 std::vector<std::string> ReadObservables();
214 // open a list of input files to be processed, only used if is external process
215 virtual Bool_t OpenInputFiles(const std::vector<std::string>& files);
216 // add an input file during process
217 virtual Bool_t AddInputFile(const std::string& file) { return false; }
218 // reset the entry by moving file ptr to 0 with fseek
219 virtual Bool_t ResetEntry() { return false; }
220
221 inline void SetObservableValidation(bool validate) { fValidateObservables = validate; }
222
223 inline void RegisterAllObservables(Bool_t value = true) { fDynamicObs = value; }
224
225 // process running methods
227 virtual void InitProcess() {}
229 virtual void BeginOfEventProcess(TRestEvent* inputEvent = nullptr);
231 virtual TRestEvent* ProcessEvent(TRestEvent* inputEvent) = 0;
233 virtual void EndOfEventProcess(TRestEvent* inputEvent = nullptr);
235 virtual void EndProcess() {}
236
237 // setters
241 inline void SetRunInfo(TRestRun* r) { fRunInfo = r; }
243 inline void SetCanvasSize(Int_t x, Int_t y) { fCanvasSize = TVector2(x, y); }
249 virtual void NotifyAnalysisTreeReset() {}
250
251 // getters
253 virtual RESTValue GetInputEvent() const = 0;
255 virtual RESTValue GetOutputEvent() const = 0;
258 virtual Long64_t GetTotalBytes() const { return -1; }
260 virtual Long64_t GetTotalBytesRead() const { return 0; }
262 inline Bool_t singleThreadOnly() const { return fSingleThreadOnly; }
264 inline Bool_t isExternal() const { return fIsExternal; }
266 inline TRestRun* GetRunInfo() const { return fRunInfo; }
269 TRestAnalysisTree* GetFullAnalysisTree();
271 inline TCanvas* GetCanvas() const { return fCanvas; }
272 std::vector<std::string> GetListOfAddedObservables();
273
274 // Constructor
276 // Destructor
278
279 ClassDefOverride(TRestEventProcess, 3); // Base class for a REST process
280};
281
282#define _ApplyCut(evt) \
283 if (ApplyCut()) return nullptr; \
284 return evt;
285
286#endif
REST core data-saving helper based on TTree.
T GetObservableValue(Int_t n)
Get observable in a given type, according to its id.
Int_t GetObservableID(const std::string &obsName)
Get the index of the specified observable.
void SetObservableValue(const Int_t &id, const T &value)
Set the value of observable whose index is id.
void SetObservable(Int_t id, RESTValue obs)
Set the value of observable whose id is as specified.
A base class for any REST event process.
virtual void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
TRestEvent * fOutputEvent
< not used, keep for compatibility
bool fIsExternal
It defines if the process reads event data from an external source.
void SetRunInfo(TRestRun *r)
Set TRestRun for this process.
virtual void EndProcess()
To be executed at the end of the run (outside event loop)
virtual void EndOfEventProcess(TRestEvent *inputEvent=nullptr)
End of event process. Nothing to do. Called directly after ProcessEvent()
std::vector< TRestEventProcess * > fFriendlyProcesses
/// not used
TRestAnalysisTree * fAnalysisTree
virtual void NotifyAnalysisTreeReset()
In case the analysis tree is reset(switched to new file), some process needs to have action.
TRestRun * GetRunInfo() const
Return the pointer of the hosting TRestRun object.
TVector2 fCanvasSize
Canvas size.
void BeginPrintProcess()
[name, cut range]
void SetAnalysisTree(TRestAnalysisTree *tree)
Set analysis tree of this process, then add observables to it.
Bool_t singleThreadOnly() const
Return whether this process is single std::thread only.
bool fValidateObservables
It defines if observable names should be added to the validation list.
TCanvas * GetCanvas() const
Get canvas.
Bool_t isExternal() const
Return whether this process is external process.
TRestAnalysisTree * GetAnalysisTree() const
Return the local analysis tree (dummy)
virtual Long64_t GetTotalBytes() const
@ No_Output
creates no branch in analysis tree, user can still manually save what he wants.
@ Observable
+saving observables as branches in root directory of the tree
@ Full_Output
+saving output event as another branch. branch name is process name+"_evtBranch"
virtual void BeginOfEventProcess(TRestEvent *inputEvent=nullptr)
Begin of event process, preparation work. Called right before ProcessEvent()
TCanvas * fCanvas
< Canvas for some viewer event
void SetParallelProcess(TRestEventProcess *p)
Add parallel process to this process.
std::vector< std::pair< std::string, TVector2 > > fCuts
Stores cut definitions. Any listed observables should be in the range.
TRestRun * fRunInfo
< Pointer to TRestRun object where to find metadata.
void CreateCanvas()
Create the canvas.
Int_t LoadSectionMetadata() override
This method does some preparation of xml section.
std::vector< T > GetParallelDataMembers(T *member_of_process)
Get a list of data members from parallel processes which is same to this process's certain data membe...
bool fDynamicObs
It defines whether to use added observables only or all the observables appear in the code.
bool fReadOnly
not used, keep for compatibility
virtual TRestEvent * ProcessEvent(TRestEvent *inputEvent)=0
Process one event.
std::vector< TRestEventProcess * > fParallelProcesses
Stores a list of parallel processes if multithreading is enabled.
virtual RESTValue GetOutputEvent() const =0
Get pointer to output event. Must be implemented in the derived class.
std::map< std::string, int > fObservablesUpdated
Stores the list of process observables updated when processing this event.
virtual void InitProcess()
To be executed at the beginning of the run (outside event loop)
TRestEvent * fInputEvent
< not used, keep for compatibility
virtual Long64_t GetTotalBytesRead() const
Interface to external file reading, get the read bytes. To be implemented in external processes.
void SetCanvasSize(Int_t x, Int_t y)
Set canvas size.
REST_Process_Output fOutputLevel
not used, keep for compatibility
virtual RESTValue GetInputEvent() const =0
Get pointer to input event. Must be implemented in the derived class.
std::map< std::string, int > fObservablesDefined
Stores the list of all the appeared process observables in the code.
T * GetMetadata()
Get a metadata object from the host TRestRun.
void SetFriendProcess(TRestEventProcess *p)
Add friendly process to this process.
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
Definition: TRestEvent.h:38
A base class for any REST metadata class.
Definition: TRestMetadata.h:70
std::map< std::string, std::string > GetParametersList()
It retrieves a map of all parameter:value found in the metadata class.
void ReadParametersList(std::map< std::string, std::string > &list)
It reads a parameter list and associates it to its corresponding metadata member. par0 --> fPar0.
Data provider and manager in REST.
Definition: TRestRun.h:18
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:1652
std::string ReplaceMathematicalExpressions(std::string buffer, Int_t precision=0, std::string errorMessage="")
Evaluates and replaces valid mathematical expressions found in the input string buffer.