26#include "TRestThread.h"
30#ifdef TIME_MEASUREMENT
32using namespace chrono;
39 fOutputEvent =
nullptr;
40 fInputEvent =
nullptr;
42 fAnalysisTree =
nullptr;
45 fOutputFile =
nullptr;
47 fProcessChain.clear();
51 fCompressionLevel = 1;
66 RESTInfo <<
"thread " << fThreadId <<
": validating process chain" << RESTendl;
69 vector<TRestEventProcess*> processes;
70 for (
unsigned int i = 0; i < fProcessChain.size(); i++) {
71 RESTValue inEvent = fProcessChain[i]->GetInputEvent();
72 RESTValue outEvent = fProcessChain[i]->GetOutputEvent();
74 if (outEvent.
type ==
"TRestEvent" && inEvent.
type ==
"TRestEvent") {
75 RESTInfo <<
"general process: " << fProcessChain[i]->GetName() << RESTendl;
77 }
else if (outEvent.
cl && outEvent.
cl->InheritsFrom(
"TRestEvent") && inEvent.
cl &&
78 inEvent.
cl->InheritsFrom(
"TRestEvent")) {
79 processes.push_back(fProcessChain[i]);
81 RESTError <<
"Process: " << fProcessChain[i]->ClassName()
82 <<
" not properly written, the input/output event is illegal!" << RESTendl;
83 RESTError <<
"Hint: they must be inherited from TRestEvent" << RESTendl;
88 if (processes.size() > 0) {
90 if (input !=
nullptr) {
91 if ((
string)input->ClassName() != processes[0]->GetInputEvent().type) {
92 RESTError <<
"(ValidateChain): Input event type does not match!" << RESTendl;
93 cout <<
"Input type of the first non-external process in chain: "
94 << processes[0]->GetInputEvent().type << endl;
95 cout <<
"The event type from file: " << input->ClassName() << endl;
96 cout <<
"No events will be processed. Please correct event process "
103 for (
unsigned int i = 0; i < processes.size() - 1; i++) {
104 string outEventType = processes[i]->GetOutputEvent().type;
105 string nextinEventType = processes[i + 1]->GetInputEvent().type;
106 if (outEventType != nextinEventType && outEventType !=
"TRestEvent" &&
107 nextinEventType !=
"TRestEvent") {
108 RESTError <<
"(ValidateChain): Event process input/output does not match" << RESTendl;
109 RESTError <<
"The event output for process " << processes[i]->GetName() <<
" is "
110 << outEventType << RESTendl;
111 RESTError <<
"The event input for process " << processes[i + 1]->GetName() <<
" is "
112 << nextinEventType << RESTendl;
113 RESTError <<
"No events will be processed. Please correctly connect the process chain!"
123void TRestThread::SetThreadId(Int_t
id) {
152 RESTDebug <<
"Processing ..." << RESTendl;
153 for (
int i = 0; i < 5; i++) {
155 RESTDebug <<
"Test run " << i <<
" : Input Event ---- " << fInputEvent->ClassName() <<
"("
156 << fInputEvent <<
")" << RESTendl;
157 for (
unsigned int j = 0; j < fProcessChain.size(); j++) {
158 RESTDebug <<
"t" << fThreadId <<
"p" << j <<
": " << fProcessChain[j]->ClassName() << RESTendl;
160 fProcessChain[j]->SetObservableValidation(
true);
162 fProcessChain[j]->BeginOfEventProcess(ProcessedEvent);
163 ProcessedEvent = fProcessChain[j]->ProcessEvent(ProcessedEvent);
164 if (fProcessChain[j]->ApplyCut()) ProcessedEvent =
nullptr;
167 if (ProcessedEvent ==
nullptr) {
168 ProcessedEvent = fProcessChain[j]->GetOutputEvent();
171 if (ProcessedEvent ==
nullptr) {
172 RESTDebug <<
" ---- NULL" << RESTendl;
176 TRestEvent* outputevent = fProcessChain[j]->GetOutputEvent();
177 if (outputevent != ProcessedEvent) {
179 <<
"Test run, in " << fProcessChain[j]->ClassName()
180 <<
" : output event is different with process returned event! Please check to assign "
181 "the TRestEvent datamember as inputEvent in ProcessEvent() method"
185 fProcessChain[j]->EndOfEventProcess();
187 fProcessChain[j]->SetObservableValidation(
false);
189 RESTDebug <<
" .... " << ProcessedEvent->ClassName() <<
"(" << ProcessedEvent <<
")" << RESTendl;
192 fOutputEvent = ProcessedEvent;
193 if (fOutputEvent !=
nullptr) {
194 RESTDebug <<
"Output Event ---- " << fOutputEvent->ClassName() <<
"(" << fOutputEvent <<
")"
198 RESTDebug <<
"Null output, trying again" << RESTendl;
202 if (fOutputEvent ==
nullptr) {
224 RESTDebug <<
"Entering TRestThread::PrepareToProcess" << RESTendl;
226 string threadFileName;
227 if (fHostRunner->GetOutputDataFile()->GetName() == (
string)
"/dev/null") {
228 threadFileName =
"/dev/null";
230 threadFileName = REST_TMP_PATH +
"rest_thread_tmp" + ToString(
this) +
".root";
233 bool outputConfigToDel =
false;
234 if (outputConfig ==
nullptr) {
235 outputConfigToDel =
true;
236 outputConfig =
new bool[4];
237 for (
int i = 0; i < 4; i++) {
238 outputConfig[i] =
true;
241 if (outputConfig[3] ==
false) {
242 cout <<
"Error! output analysis must be on!" << endl;
246 if (fProcessChain.size() > 0) {
247 RESTDebug <<
"TRestThread: Creating file : " << threadFileName << RESTendl;
250 RESTError << outputFile.Error() << RESTendl;
253 if (fOutputFileOwner && !fOutputFileOwner.
Close()) {
254 RESTError << fOutputFileOwner.Error() << RESTendl;
257 fOutputFileOwner = std::move(outputFile);
258 fOutputFile = fOutputFileOwner.
Get();
259 fOutputFile->SetCompressionLevel(fCompressionLevel);
260 fAnalysisTree =
new TRestAnalysisTree(
"AnalysisTree_" + ToString(fThreadId),
"dummyTree");
263 RESTDebug <<
"TRestThread: Finding first input event of process chain..." << RESTendl;
264 if (fHostRunner->GetInputEvent() ==
nullptr) {
266 <<
"Input event is not initialized from TRestRun! Please check your input file and file "
271 fInputEvent = (
TRestEvent*)fHostRunner->GetInputEvent()->Clone();
272 string chainInputType = fProcessChain[0]->GetInputEvent().type;
274 if (chainInputType !=
"TRestEvent" && chainInputType != (
string)fInputEvent->ClassName()) {
275 cout <<
"REST ERROR: Input event type does not match!" << endl;
276 cout <<
"Process input type : " << chainInputType
277 <<
", File input type : " << fInputEvent->ClassName() << endl;
281 RESTDebug <<
"TRestThread: Reading input event and input observable..." << RESTendl;
282 if (fHostRunner->
GetNextevtFunc(fInputEvent, fAnalysisTree) != 0) {
283 RESTError <<
"In thread " << fThreadId <<
")::Failed to read input event, process cannot start!"
288 RESTDebug <<
"TRestThread: Init process..." << RESTendl;
289 for (
unsigned int i = 0; i < fProcessChain.size(); i++) {
290 fProcessChain[i]->SetAnalysisTree(fAnalysisTree);
291 for (
unsigned int j = 0; j < fProcessChain.size(); j++) {
292 fProcessChain[i]->SetFriendProcess(fProcessChain[j]);
294 RESTDebug <<
"InitProcess() process for " << fProcessChain[i]->ClassName() << RESTendl;
295 fProcessChain[i]->InitProcess();
299 if (fHostRunner->UseTestRun()) {
300 RESTDebug <<
"Test Run..." << RESTendl;
302 RESTError <<
"In thread " << fThreadId <<
")::test run failed!" << RESTendl;
303 RESTError <<
"One of the processes has NULL pointer fOutputEvent!" << RESTendl;
305 RESTError <<
"To see more detail, turn on debug mode for "
306 "TRestProcessRunner!"
310 RESTDebug <<
"Test Run complete!" << RESTendl;
312 RESTDebug <<
"Initializing output event" << RESTendl;
313 string chainOutputType = fProcessChain[fProcessChain.size() - 1]->GetOutputEvent().type;
315 if (fOutputEvent ==
nullptr) {
322 fEventTree =
new TTree((TString)
"EventTree_" + ToString(fThreadId),
"dummyTree");
323 vector<pair<TString, TRestEvent*>> branchesToAdd;
327 if (outputConfig[1] ==
true) {
333 TString BranchName = (TString)evt->GetName() +
"Branch";
334 if (branchesToAdd.size() == 0)
335 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
337 for (
unsigned int j = 0; j < branchesToAdd.size(); j++) {
338 if (branchesToAdd[j].first == BranchName)
339 branchesToAdd[j].second = evt;
340 else if (j == branchesToAdd.size() - 1)
341 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
345 for (
unsigned int i = 0; i < fProcessChain.size(); i++) {
346 TRestEvent* evt = fProcessChain[i]->GetOutputEvent();
347 if (evt !=
nullptr) {
348 TString BranchName = (TString)evt->GetName() +
"Branch";
349 if (branchesToAdd.size() == 0)
350 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
352 for (
unsigned int j = 0; j < branchesToAdd.size(); j++) {
353 if (branchesToAdd[j].first == BranchName)
354 branchesToAdd[j].second = evt;
355 else if (j == branchesToAdd.size() - 1)
356 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
362 if (outputConfig[2] ==
true) {
366 TString BranchName = (TString)evt->GetName() +
"Branch";
367 if (branchesToAdd.size() == 0)
368 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
370 for (
unsigned int j = 0; j < branchesToAdd.size(); j++) {
371 if (branchesToAdd[j].first == BranchName)
372 branchesToAdd[j].second = evt;
373 else if (j == branchesToAdd.size() - 1)
374 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
379 auto iter = branchesToAdd.begin();
380 while (iter != branchesToAdd.end()) {
381 fEventTree->Branch(iter->first, iter->second->ClassName(), iter->second);
385 if (fEventTree->GetListOfBranches()->GetLast() < 0)
388 fEventTree =
nullptr;
396 fOutputFile->Clear();
397 for (
unsigned int i = 0; i < fProcessChain.size(); i++) {
398 fProcessChain[i]->InitProcess();
401 RESTDebug <<
"Thread " << fThreadId <<
" Ready!" << RESTendl;
403 string tmp = fHostRunner->GetInputEvent()->ClassName();
405 fOutputEvent = fInputEvent;
408 RESTError << outputFile.Error() << RESTendl;
411 if (fOutputFileOwner && !fOutputFileOwner.
Close()) {
412 RESTError << fOutputFileOwner.Error() << RESTendl;
415 fOutputFileOwner = std::move(outputFile);
416 fOutputFile = fOutputFileOwner.
Get();
417 fOutputFile->SetCompressionLevel(fCompressionLevel);
420 RESTDebug <<
"Creating Analysis Tree..." << RESTendl;
421 fAnalysisTree =
new TRestAnalysisTree(
"AnalysisTree_" + ToString(fThreadId),
"dummyTree");
423 fEventTree =
new TTree((TString)
"EventTree_" + ToString(fThreadId),
"dummyTree");
426 if (outputConfig[2]) {
427 TString BranchName = (TString)fInputEvent->GetName() +
"Branch";
428 if (fEventTree->GetBranch(BranchName) ==
nullptr)
430 fEventTree->Branch(BranchName, fInputEvent->ClassName(), fInputEvent);
435 if (outputConfigToDel)
delete outputConfig;
456 while (fHostRunner->
GetNextevtFunc(fInputEvent, fAnalysisTree) == 0) {
483 RESTDebug <<
"Entering TRestThread::AddProcess" << RESTendl;
485 fProcessChain.push_back(process);
505 fProcessNullReturned =
false;
508#ifdef TIME_MEASUREMENT
509 vector<int> processtime(fProcessChain.size());
512 for (
unsigned int j = 0; j < fProcessChain.size(); j++) {
513 cout <<
"------- Starting process " + (string)fProcessChain[j]->GetName() +
" -------" << endl;
515#ifdef TIME_MEASUREMENT
516 high_resolution_clock::time_point t1 = high_resolution_clock::now();
519 fProcessChain[j]->BeginOfEventProcess(ProcessedEvent);
520 ProcessedEvent = fProcessChain[j]->ProcessEvent(ProcessedEvent);
521 if (fProcessChain[j]->ApplyCut()) ProcessedEvent =
nullptr;
522 fProcessChain[j]->EndOfEventProcess();
524#ifdef TIME_MEASUREMENT
525 high_resolution_clock::time_point t2 = high_resolution_clock::now();
526 processtime[j] = (int)duration_cast<microseconds>(t2 - t1).count();
529 if (ProcessedEvent ==
nullptr) {
530 cout <<
"------- End of process " + (string)fProcessChain[j]->GetName() +
531 " (NULL returned) -------"
533 fProcessNullReturned =
true;
536 cout <<
"------- End of process " + (string)fProcessChain[j]->GetName() +
" -------" << endl;
540 j < fProcessChain.size() - 1) {
545#ifdef TIME_MEASUREMENT
546 cout <<
"Process timing summary : " << endl;
547 for (
unsigned int j = 0; j < fProcessChain.size(); j++) {
548 cout << fProcessChain[j]->ClassName() <<
"(" << fProcessChain[j]->GetName()
549 <<
") : " << processtime[j] / (double)1000 <<
" ms" << endl;
553 if (fHostRunner->UseTestRun()) {
554 fOutputEvent = ProcessedEvent;
556 cout <<
"Transferring output event..." << endl;
557 if (ProcessedEvent !=
nullptr) {
558 ProcessedEvent->
CloneTo(fOutputEvent);
563 "======= End of Event " +
564 ((fOutputEvent ==
nullptr) ? ToString(fInputEvent->GetID()) : ToString(fOutputEvent->GetID())) +
567 for (
unsigned int j = 0; j < fProcessChain.size(); j++) {
568 fProcessChain[j]->BeginOfEventProcess(ProcessedEvent);
569 ProcessedEvent = fProcessChain[j]->ProcessEvent(ProcessedEvent);
570 if (fProcessChain[j]->ApplyCut()) ProcessedEvent =
nullptr;
571 fProcessChain[j]->EndOfEventProcess();
572 if (ProcessedEvent ==
nullptr) {
573 fProcessNullReturned =
true;
578 if (fHostRunner->UseTestRun()) {
579 fOutputEvent = ProcessedEvent;
581 if (ProcessedEvent !=
nullptr) {
582 ProcessedEvent->
CloneTo(fOutputEvent);
594 RESTInfo <<
"TRestThread : Writing temp file. Thread id : " << fThreadId << RESTendl;
596 if (fOutputFile ==
nullptr)
return;
601 for (
unsigned int i = 0; i < fProcessChain.size(); i++) {
603 fProcessChain[i]->EndProcess();
604 if (fProcessChain[i]->GetError()) nErrors++;
605 if (fProcessChain[i]->GetWarning()) nWarnings++;
609 RESTWarning << nWarnings
610 <<
" process warnings were found! Use run0->PrintWarnings(); to get additional info."
613 RESTError << nErrors <<
" process errors were found! Use run0->PrintErrors(); to get additional info."
616 delete fAnalysisTree;
std::string type
Type of the wrapped object.
TClass * cl
Pointer to the corresponding TClass helper, if the wrapped object is in class type.
REST core data-saving helper based on TTree.
void DisableQuickObservableValueSetting()
It will disable quick observable value setting.
A base class for any REST event process.
A base class for any REST event.
virtual void CloneTo(TRestEvent *target)
Clone the content of this TRestEvent object to another.
Int_t GetNextevtFunc(TRestEvent *targetevt, TRestAnalysisTree *targettree)
Get next event and copy it to the address of targetevt.
void FillThreadEventFunc(TRestThread *t)
Calling back the FillEvent() method in TRestThread.
TFile * Get() const noexcept
Borrow the file without transferring ownership; do not delete or outlive the handle.
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
@ REST_Essential
+show some essential information, as well as warnings
@ REST_Extreme
show everything
@ REST_Debug
+show the defined debug messages
void AddProcess(TRestEventProcess *process)
Add a process.
bool TestRun()
Make a test run of our process chain.
void Initialize()
Set variables by default during initialization.
void StartProcess()
The main function of this class. Thread will run this function until the end.
void EndProcess()
Write and close the output file.
void ProcessEvent()
Process a single event.
void PrepareToProcess(bool *outputConfig=nullptr)
Prepare some thing before we can start process.
Int_t ValidateChain(TRestEvent *input)
Check if the input/output of each process in the process chain matches.
void StartThread()
Create a thread with the method StartProcess().
TRestReflector Assembly(const std::string &typeName)
Assembly an object of type: typeName, returning the allocated memory address and size.
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.