REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestEventProcess.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
52
53#include "TRestEventProcess.h"
54
55#include <TBuffer.h>
56#include <TClass.h>
57#include <TDataMember.h>
58
59#include "TRestManager.h"
60#include "TRestRun.h"
61
62using namespace std;
63
64ClassImp(TRestEventProcess);
65
70 fObservablesDefined.clear();
71 fObservablesUpdated.clear();
72 fSingleThreadOnly = false;
73 fCanvasSize = TVector2(800, 600);
74}
75
80
99vector<string> TRestEventProcess::ReadObservables() {
100 std::cout << "Entering ReadObservables" << std::endl;
101 TiXmlElement* e = GetElement("observable");
102 vector<string> obsNames;
103 vector<string> obsTypes;
104 vector<string> obsDesc;
105
106 while (e != nullptr) {
107 const char* obschr = e->Attribute("name");
108 const char* _value = e->Attribute("value");
109 const char* _type = e->Attribute("type");
110 const char* _desc = e->Attribute("description");
111
112 std::cout << "Name: " << obschr << std::endl;
113
114 string value;
115 if (_value == nullptr)
116 value = "ON";
117 else
118 value = _value;
119 string type;
120 if (_type == nullptr)
121 type = "double";
122 else
123 type = _type;
124 string description;
125 if (_desc == nullptr)
126 description = "";
127 else
128 description = _desc;
129
130 if (ToUpper(value) == "ON") {
131 if (obschr != nullptr) {
132 RESTDebug << this->ClassName() << " : setting observable \"" << obschr << "\"" << RESTendl;
133 // vector<string> tmp = Split(obsstring, ":");
134 obsNames.emplace_back(obschr);
135 obsTypes.push_back(type);
136 obsDesc.push_back(description);
137 }
138 }
139
140 e = e->NextSiblingElement("observable");
141
142 } // now we get a list of all observal names, we add them into fAnalysisTree and fObservableInfo
143
144 for (unsigned int i = 0; i < obsNames.size(); i++) {
145 string obsName = this->GetName() + (string) "_" + obsNames[i];
146 std::cout << "ADDING OBSERVABLE : " << obsName << std::endl;
147 fAnalysisTree->AddObservable(obsName, obsTypes[i], obsDesc[i]);
148 int id = fAnalysisTree->GetObservableID(obsName);
149 if (id != -1) {
150 fObservablesDefined[(string)GetName() + "_" + obsNames[i]] = id;
151 }
152 }
153
154 return obsNames;
155}
156
158 fAnalysisTree = tree;
159 if (fAnalysisTree == nullptr) return;
160 ReadObservables();
161}
162
168 if (p == nullptr) return;
169 for (const auto& fFriendlyProcess : fFriendlyProcesses) {
170 if (fFriendlyProcess->GetName() == p->GetName()) {
171 return;
172 }
173 }
174 fFriendlyProcesses.push_back(p);
175}
176
179 if (p == nullptr) return;
180 for (const auto& fParallelProcess : fParallelProcesses) {
181 if (fParallelProcess == p || this == p) {
182 return;
183 }
184 }
185 fParallelProcesses.push_back(p);
186}
187
190Bool_t TRestEventProcess::OpenInputFiles(const vector<string>& files) { return false; }
191
198
199 if (ToUpper(GetParameter("observable", "")) == "ALL" ||
200 ToUpper(GetParameter("observables", "")) == "ALL") {
201 fDynamicObs = true;
202 }
203
204 // load cuts
205 fCuts.clear();
206 if (ToUpper(GetParameter("cutsEnabled", "false")) == "TRUE") {
207 TiXmlElement* ele = fElement->FirstChildElement();
208 while (ele != nullptr) {
209 if (ele->Value() != nullptr && (string)ele->Value() == "cut") {
210 if (ele->Attribute("name") != nullptr && ele->Attribute("value") != nullptr) {
211 string name = ele->Attribute("name");
212 name = (string)this->GetName() + "_" + name;
213 TVector2 value = StringTo2DVector(ele->Attribute("value"));
214 if (value.X() != value.Y()) fCuts.push_back(pair<string, TVector2>(name, value));
215 }
216 }
217
218 else if (ele->Value() != nullptr && (string)ele->Value() == "parameter") {
219 if (ele->Attribute("name") != nullptr && ele->Attribute("value") != nullptr) {
220 string name = ele->Attribute("name");
221 if (name.find("Cut") == name.size() - 3 || name.find("CutRange") == name.size() - 8) {
222 name = name.substr(0, name.find("Cut") + 3);
223 TVector2 value = StringTo2DVector(ele->Attribute("value"));
224 if (value.X() != value.Y()) fCuts.push_back(pair<string, TVector2>(name, value));
225 }
226 }
227 }
228
229 ele = ele->NextSiblingElement();
230 }
231 }
232
233 return 0;
234}
235
242 TRestMetadata* m = nullptr;
243 if (fRunInfo != nullptr) {
244 m = fRunInfo->GetMetadata(name);
245 if (m == nullptr) m = fRunInfo->GetMetadataClass(name);
246 }
247 if (fHostmgr != nullptr) {
248 if (m == nullptr) m = fHostmgr->GetMetadata(name);
249 if (m == nullptr) m = fHostmgr->GetMetadataClass(name);
250 }
251 return m;
252}
253
266TRestEventProcess* TRestEventProcess::GetFriend(const string& nameOrType) {
267 TRestEventProcess* proc = GetFriendLive(nameOrType);
268 if (proc == nullptr) {
269 TRestMetadata* friendfromfile = GetMetadata(nameOrType);
270 if (friendfromfile != nullptr && friendfromfile->InheritsFrom("TRestEventProcess")) {
271 return (TRestEventProcess*)friendfromfile;
272 }
273 return nullptr;
274 } else {
275 return proc;
276 }
277}
278
283TRestEventProcess* TRestEventProcess::GetFriendLive(const string& nameOrType) {
284 for (const auto& fFriendlyProcess : fFriendlyProcesses) {
285 if ((string)fFriendlyProcess->GetName() == nameOrType ||
286 (string)fFriendlyProcess->ClassName() == nameOrType) {
287 return fFriendlyProcess;
288 }
289 }
290 return nullptr;
291}
292
302TRestEventProcess* TRestEventProcess::GetParallel(int i) {
303 if (i >= 0 && i < (int)fParallelProcesses.size()) {
304 return fParallelProcesses[i];
305 }
306 return nullptr;
307}
308
313bool TRestEventProcess::ApplyCut() {
314 for (auto cut : fCuts) {
315 string type = (string)fAnalysisTree->GetObservableType(cut.first);
316 if (fAnalysisTree != nullptr && type == "double") {
317 double val = fAnalysisTree->GetObservableValue<double>(cut.first);
318 if (val > cut.second.Y() || val < cut.second.X()) {
319 return true;
320 }
321 }
322 if (fAnalysisTree != nullptr && type == "int") {
323 int val = fAnalysisTree->GetObservableValue<int>(cut.first);
324 if (val > cut.second.Y() || val < cut.second.X()) {
325 return true;
326 }
327 }
328 }
329 return false;
330}
331
332/*
333
334void TRestEventProcess::InitProcess()
335{
336// virtual function to be executed once at the beginning of process
337// (before starting the process of the events)
338cout << GetName() << ": Process initialization..." << endl;
339}
340*/
341
351 RESTDebug << "Entering " << ClassName() << "::BeginOfEventProcess, Initializing output event..."
352 << RESTendl;
353 if (inEv != nullptr && GetOutputEvent().address != nullptr && (TRestEvent*)GetOutputEvent() != inEv) {
354 TRestEvent* outEv = GetOutputEvent();
355 outEv->Initialize();
356
357 outEv->SetID(inEv->GetID());
358 outEv->SetSubID(inEv->GetSubID());
359 outEv->SetSubEventTag(inEv->GetSubEventTag());
360
361 outEv->SetRunOrigin(inEv->GetRunOrigin());
362 outEv->SetSubRunOrigin(inEv->GetSubRunOrigin());
363
364 outEv->SetTime(inEv->GetTime());
365 }
366
367 fObservablesUpdated.clear();
368
369 // TODO if fIsExternal and we already have defined the fAnalysisTree run#,
370 // evId#, timestamp, etc at the analysisTree we could stamp the output event
371 // here.
372}
373
374/*
375
376void TRestEventProcess::ProcessEvent( TRestEvent *inputEvent )
377{
378// virtual function to be executed for every event to be processed
379}
380*/
381
386 RESTDebug << "Entering TRestEventProcess::EndOfEventProcess (" << ClassName() << ")" << RESTendl;
388 if (fObservablesDefined.size() != fObservablesUpdated.size()) {
389 for (auto x : fObservablesDefined) {
390 if (fObservablesUpdated.count(x.first) == 0) {
391 // the observable is added through <observable section but not set in the process
392 RESTWarning
393 << "The observable '" << x.first << "' is defined but not set by "
394 << this->ClassName()
395 << ", the event is empty? Makesure all observables are set through ProcessEvent()"
396 << RESTendl;
397 }
398 }
399 }
400 }
401}
402
403/*
404
405void TRestEventProcess::EndProcess()
406{
407// virtual function to be executed once at the end of the process
408// (after all events have been processed)
409cout << GetName() << ": Process ending..." << endl;
410}
411*/
412
420 RESTMetadata.setcolor(COLOR_BOLDGREEN);
421 RESTMetadata.setborder("||");
422 RESTMetadata.setlength(100);
423 // metadata << " " << endl;
424 cout << endl;
425 RESTMetadata << "=" << RESTendl;
426 RESTMetadata << "Process : " << ClassName() << RESTendl;
427 RESTMetadata << "Name: " << GetName() << " Title: " << GetTitle()
428 << " VerboseLevel: " << GetVerboseLevelString() << RESTendl;
429 RESTMetadata << " ----------------------------------------------- " << RESTendl;
430 RESTMetadata << " " << RESTendl;
431
432 if (!fObservablesDefined.empty()) {
433 RESTMetadata << " Analysis tree observables added by this process " << RESTendl;
434 RESTMetadata << " +++++++++++++++++++++++++++++++++++++++++++++++ " << RESTendl;
435 }
436
437 auto iter = fObservablesDefined.begin();
438 while (iter != fObservablesDefined.end()) {
439 RESTMetadata << " ++ " << iter->first << RESTendl;
440 iter++;
441 }
442
443 if (!fObservablesDefined.empty()) {
444 RESTMetadata << " +++++++++++++++++++++++++++++++++++++++++++++++ " << RESTendl;
445 RESTMetadata << " " << RESTendl;
446 }
447}
448
454void TRestEventProcess::EndPrintProcess() {
455 if (fCuts.size() > 0) {
456 RESTMetadata << "Cuts enabled" << RESTendl;
457 RESTMetadata << "------------" << RESTendl;
458
459 auto iter = fCuts.begin();
460 while (iter != fCuts.end()) {
461 if (iter->second.X() != iter->second.Y())
462 RESTMetadata << iter->first << ", range : ( " << iter->second.X() << " , " << iter->second.Y()
463 << " ) " << RESTendl;
464 iter++;
465 }
466 }
467
468 RESTMetadata << " " << RESTendl;
469 RESTMetadata << "=" << RESTendl;
470 RESTMetadata << RESTendl;
471 RESTMetadata.resetcolor();
472 RESTMetadata.setborder("");
473 RESTMetadata.setlength(10000);
474}
475
483// Double_t TRestEventProcess::GetDoubleParameterFromFriends(string className, string parName) {
484// for (size_t i = 0; i < fFriendlyProcesses.size(); i++)
485// if ((string)fFriendlyProcesses[i]->ClassName() == (string)className)
486// return StringToDouble(fFriendlyProcesses[i]->GetParameter((string)parName));
487//
488// return PARAMETER_NOT_FOUND_DBL;
489//}
490//
498// Double_t TRestEventProcess::GetDoubleParameterFromFriendsWithUnits(string className, string parName) {
499// for (size_t i = 0; i < fFriendlyProcesses.size(); i++)
500// if ((string)fFriendlyProcesses[i]->ClassName() == (string)className)
501// return fFriendlyProcesses[i]->GetDblParameterWithUnits((string)parName);
502//
503// return PARAMETER_NOT_FOUND_DBL;
504//}
505
508TRestAnalysisTree* TRestEventProcess::GetFullAnalysisTree() {
509 if (fHostmgr != nullptr && fHostmgr->GetProcessRunner() != nullptr)
510 return fHostmgr->GetProcessRunner()->GetOutputAnalysisTree();
511 return nullptr;
512}
513
516std::vector<string> TRestEventProcess::GetListOfAddedObservables() {
517 vector<string> list;
518 auto iter = fObservablesDefined.begin();
519 while (iter != fObservablesDefined.end()) {
520 list.push_back(iter->first);
521 iter++;
522 }
523 return list;
524}
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.
TString GetObservableType(Int_t n)
Get the observable type according to id.
A base class for any REST event process.
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
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 fValidateObservables
It defines if observable names should be added to the validation list.
virtual void BeginOfEventProcess(TRestEvent *inputEvent=nullptr)
Begin of event process, preparation work. Called right before ProcessEvent()
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.
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< 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.
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.
A base class for any REST event.
Definition: TRestEvent.h:38
void SetTime(Double_t time)
Definition: TRestEvent.cxx:83
virtual void Initialize()=0
Definition: TRestEvent.cxx:71
TRestMetadata * GetMetadataClass(std::string type)
Get the application metadata class, according to the type.
TRestMetadata * GetMetadata(std::string name)
Get the application metadata class, according to the name.
A base class for any REST metadata class.
Definition: TRestMetadata.h:70
endl_t RESTendl
Termination flag object for TRestStringOutput.
TiXmlElement * GetElement(std::string eleDeclare, TiXmlElement *e=nullptr)
Get an xml element from a given parent element, according to its declaration.
TString GetVerboseLevelString()
returns the verbose level in type of TString
TRestManager * fHostmgr
All metadata classes can be initialized and managed by TRestManager.
virtual Int_t LoadSectionMetadata()
This method does some preparation of xml section.
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.
TiXmlElement * fElement
Saving the sectional element together with global element.
std::string ToUpper(std::string in)
Convert string to its upper case. Alternative of TString::ToUpper.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.