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:
42  Internal_Var,
45  };
46 
47  private:
48  // unused datamember, set as private
50  TRestEvent* fInputEvent = nullptr;
52  TRestEvent* fOutputEvent = nullptr;
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 
158  if (fValidateObservables) {
159  int id = fAnalysisTree->GetObservableID(obsName);
160  if (id != -1) {
161  fObservablesDefined[obsName] = id;
162  fObservablesUpdated[obsName] = id;
163  fAnalysisTree->SetObservable(obsName, value);
164  } else if (fDynamicObs) {
165  fAnalysisTree->SetObservable(obsName, value);
166  int n = fAnalysisTree->GetObservableID(obsName);
167  if (n != -1) {
168  fObservablesDefined[obsName] = id;
169  fObservablesUpdated[obsName] = id;
170  }
171  }
172  } else {
173  int id = fAnalysisTree->GetObservableID(obsName);
174  if (id != -1) {
175  fAnalysisTree->SetObservableValue(id, value);
176  } else if (fDynamicObs) {
177  fAnalysisTree->SetObservableValue(obsName, value);
178  }
179  }
180  }
181 
182  template <class T>
183  T GetObservableValue(const std::string& name) {
184  if (fAnalysisTree != nullptr) {
185  return fAnalysisTree->GetObservableValue<T>(name);
186  }
187  return std::numeric_limits<T>::quiet_NaN();
188  }
189 
191  inline void CreateCanvas() {
192  if (fCanvas != nullptr) {
193  return;
194  }
195  fCanvas = new TCanvas(this->GetName(), this->GetTitle(), fCanvasSize.X(), fCanvasSize.Y());
196  }
197 
198  public:
199  bool ApplyCut();
200 
201  virtual const char* GetProcessName() const = 0;
202  Int_t LoadSectionMetadata() override;
203  virtual void InitFromConfigFile() override {
204  std::map<std::string, std::string> parameters = GetParametersList();
205  for (auto& p : parameters) {
207  }
208  ReadParametersList(parameters);
209  }
210  std::vector<std::string> ReadObservables();
211  // open a list of input files to be processed, only used if is external process
212  virtual Bool_t OpenInputFiles(const std::vector<std::string>& files);
213  // add an input file during process
214  virtual Bool_t AddInputFile(const std::string& file) { return false; }
215  // reset the entry by moving file ptr to 0 with fseek
216  virtual Bool_t ResetEntry() { return false; }
217 
218  inline void SetObservableValidation(bool validate) { fValidateObservables = validate; }
219 
220  inline void RegisterAllObservables(Bool_t value = true) { fDynamicObs = value; }
221 
222  // process running methods
224  virtual void InitProcess() {}
226  virtual void BeginOfEventProcess(TRestEvent* inputEvent = nullptr);
228  virtual TRestEvent* ProcessEvent(TRestEvent* inputEvent) = 0;
230  virtual void EndOfEventProcess(TRestEvent* inputEvent = nullptr);
232  virtual void EndProcess() {}
233 
234  // setters
238  inline void SetRunInfo(TRestRun* r) { fRunInfo = r; }
240  inline void SetCanvasSize(Int_t x, Int_t y) { fCanvasSize = TVector2(x, y); }
246  virtual void NotifyAnalysisTreeReset() {}
247 
248  // getters
250  virtual RESTValue GetInputEvent() const = 0;
252  virtual RESTValue GetOutputEvent() const = 0;
255  virtual Long64_t GetTotalBytes() const { return -1; }
257  virtual Long64_t GetTotalBytesRead() const { return 0; }
259  inline Bool_t singleThreadOnly() const { return fSingleThreadOnly; }
261  inline Bool_t isExternal() const { return fIsExternal; }
263  inline TRestRun* GetRunInfo() const { return fRunInfo; }
265  inline TRestAnalysisTree* GetAnalysisTree() const { return fAnalysisTree; }
266  TRestAnalysisTree* GetFullAnalysisTree();
268  inline TCanvas* GetCanvas() const { return fCanvas; }
269  std::vector<std::string> GetListOfAddedObservables();
270 
271  // Constructor
273  // Destructor
275 
276  ClassDefOverride(TRestEventProcess, 3); // Base class for a REST process
277 };
278 
279 #define _ApplyCut(evt) \
280  if (ApplyCut()) return nullptr; \
281  return evt;
282 
283 #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.
TVector2 fCanvasSize
Canvas size.
void BeginPrintProcess()
[name, cut range]
TRestAnalysisTree * GetAnalysisTree() const
Return the local analysis tree (dummy)
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.
T * GetMetadata()
Get a metadata object from the host TRestRun.
Bool_t isExternal() const
Return whether this process is external process.
TCanvas * GetCanvas() const
Get canvas.
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.
bool fDynamicObs
It defines whether to use added observables only or all the observables appear in the code.
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 fReadOnly
not used, keep for compatibility
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.
virtual TRestEvent * ProcessEvent(TRestEvent *inputEvent)=0
Process one event.
void SetFriendProcess(TRestEventProcess *p)
Add friendly process to this process.
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
TRestRun * GetRunInfo() const
Return the pointer of the hosting TRestRun object.
A base class for any REST event.
Definition: TRestEvent.h:38
A base class for any REST metadata class.
Definition: TRestMetadata.h:74
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.