REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
TRestThread.cxx
1
25
26#include "TRestThread.h"
27
28using namespace std;
29
30#ifdef TIME_MEASUREMENT
31#include <chrono>
32using namespace chrono;
33#endif
34
39 fOutputEvent = nullptr;
40 fInputEvent = nullptr;
41
42 fAnalysisTree = nullptr;
43 fEventTree = nullptr;
44
45 fOutputFile = nullptr;
46
47 fProcessChain.clear();
48
49 isFinished = false;
50
51 fCompressionLevel = 1;
53}
54
66 RESTInfo << "thread " << fThreadId << ": validating process chain" << RESTendl;
67
68 // add the non-general processes to list for validation
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();
73
74 if (outEvent.type == "TRestEvent" && inEvent.type == "TRestEvent") {
75 RESTInfo << "general process: " << fProcessChain[i]->GetName() << RESTendl;
76 continue;
77 } else if (outEvent.cl && outEvent.cl->InheritsFrom("TRestEvent") && inEvent.cl &&
78 inEvent.cl->InheritsFrom("TRestEvent")) {
79 processes.push_back(fProcessChain[i]);
80 } else {
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;
84 abort();
85 }
86 }
87
88 if (processes.size() > 0) {
89 // verify that the input event of first process is OK
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 "
97 "input/output."
98 << endl;
99 }
100 }
101
102 // verify that the output event type is good to be the input event of the next 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!"
114 << RESTendl;
115 GetChar();
116 return -1;
117 }
118 }
119 }
120 return 0;
121}
122
123void TRestThread::SetThreadId(Int_t id) {
124 fThreadId = id;
125 if (fThreadId != 0 && fVerboseLevel > TRestStringOutput::REST_Verbose_Level::REST_Essential)
127}
128
152 RESTDebug << "Processing ..." << RESTendl;
153 for (int i = 0; i < 5; i++) {
154 TRestEvent* ProcessedEvent = fInputEvent;
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;
159
160 fProcessChain[j]->SetObservableValidation(true);
161
162 fProcessChain[j]->BeginOfEventProcess(ProcessedEvent);
163 ProcessedEvent = fProcessChain[j]->ProcessEvent(ProcessedEvent);
164 if (fProcessChain[j]->ApplyCut()) ProcessedEvent = nullptr;
165 // if the output of ProcessEvent() is NULL we assume the event is cut.
166 // we try to use GetOutputEvent()
167 if (ProcessedEvent == nullptr) {
168 ProcessedEvent = fProcessChain[j]->GetOutputEvent();
169 }
170 // if still null we perform another try
171 if (ProcessedEvent == nullptr) {
172 RESTDebug << " ---- NULL" << RESTendl;
173 break;
174 }
175 // check if the output event is same as the processed event
176 TRestEvent* outputevent = fProcessChain[j]->GetOutputEvent();
177 if (outputevent != ProcessedEvent) {
178 RESTWarning
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"
182 << RESTendl;
183 }
184
185 fProcessChain[j]->EndOfEventProcess();
186
187 fProcessChain[j]->SetObservableValidation(false);
188
189 RESTDebug << " .... " << ProcessedEvent->ClassName() << "(" << ProcessedEvent << ")" << RESTendl;
190 }
191
192 fOutputEvent = ProcessedEvent;
193 if (fOutputEvent != nullptr) {
194 RESTDebug << "Output Event ---- " << fOutputEvent->ClassName() << "(" << fOutputEvent << ")"
195 << RESTendl;
196 break;
197 } else {
198 RESTDebug << "Null output, trying again" << RESTendl;
199 }
200 fHostRunner->GetNextevtFunc(fInputEvent, fAnalysisTree);
201 }
202 if (fOutputEvent == nullptr) {
203 // fOutputEvent = fProcessChain[fProcessChain.size() - 1]->GetOutputEvent();
204 return false;
205 }
206 return true;
207}
208
223void TRestThread::PrepareToProcess(bool* outputConfig) {
224 RESTDebug << "Entering TRestThread::PrepareToProcess" << RESTendl;
225
226 string threadFileName;
227 if (fHostRunner->GetOutputDataFile()->GetName() == (string) "/dev/null") {
228 threadFileName = "/dev/null";
229 } else {
230 threadFileName = REST_TMP_PATH + "rest_thread_tmp" + ToString(this) + ".root";
231 }
232
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;
239 }
240 }
241 if (outputConfig[3] == false) {
242 cout << "Error! output analysis must be on!" << endl;
243 exit(1);
244 }
245
246 if (fProcessChain.size() > 0) {
247 RESTDebug << "TRestThread: Creating file : " << threadFileName << RESTendl;
248 auto outputFile = TRestRootFileHandle::Open(threadFileName, TRestRootFileMode::Recreate);
249 if (!outputFile) {
250 RESTError << outputFile.Error() << RESTendl;
251 exit(1);
252 }
253 if (fOutputFileOwner && !fOutputFileOwner.Close()) {
254 RESTError << fOutputFileOwner.Error() << RESTendl;
255 exit(1);
256 }
257 fOutputFileOwner = std::move(outputFile);
258 fOutputFile = fOutputFileOwner.Get();
259 fOutputFile->SetCompressionLevel(fCompressionLevel);
260 fAnalysisTree = new TRestAnalysisTree("AnalysisTree_" + ToString(fThreadId), "dummyTree");
261 fAnalysisTree->DisableQuickObservableValueSetting();
262
263 RESTDebug << "TRestThread: Finding first input event of process chain..." << RESTendl;
264 if (fHostRunner->GetInputEvent() == nullptr) {
265 RESTError
266 << "Input event is not initialized from TRestRun! Please check your input file and file "
267 "reading process!"
268 << RESTendl;
269 exit(1);
270 }
271 fInputEvent = (TRestEvent*)fHostRunner->GetInputEvent()->Clone();
272 string chainInputType = fProcessChain[0]->GetInputEvent().type;
273 // we check if the process chain input type matches fInputEvent
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;
278 exit(1);
279 }
280
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!"
284 << RESTendl;
285 exit(1);
286 }
287
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]);
293 }
294 RESTDebug << "InitProcess() process for " << fProcessChain[i]->ClassName() << RESTendl;
295 fProcessChain[i]->InitProcess();
296 }
297
298 // test run
299 if (fHostRunner->UseTestRun()) {
300 RESTDebug << "Test Run..." << RESTendl;
301 if (!TestRun()) {
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!"
307 << RESTendl;
308 exit(1);
309 }
310 RESTDebug << "Test Run complete!" << RESTendl;
311 } else {
312 RESTDebug << "Initializing output event" << RESTendl;
313 string chainOutputType = fProcessChain[fProcessChain.size() - 1]->GetOutputEvent().type;
314 fOutputEvent = REST_Reflection::Assembly(chainOutputType);
315 if (fOutputEvent == nullptr) {
316 exit(1);
317 }
318 }
319
321 // create dummy tree to store events
322 fEventTree = new TTree((TString) "EventTree_" + ToString(fThreadId), "dummyTree");
323 vector<pair<TString, TRestEvent*>> branchesToAdd;
324 // avoid duplicated branch
325 // if event type is same, we only create branch for the last of this type
326 // event
327 if (outputConfig[1] == true) {
328 // outputConfig[1]: the input events of each processes in the process chain(can be many)
329
330 // the input event of process chain
331 TRestEvent* evt = fInputEvent;
332 {
333 TString BranchName = (TString)evt->GetName() + "Branch";
334 if (branchesToAdd.size() == 0)
335 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
336 else
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));
342 }
343 }
344 // the input event of other processes
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));
351 else
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));
357 }
358 }
359 }
360 }
361
362 if (outputConfig[2] == true) {
363 // outputConfig[2]: output event: the output event of the last process in the process chain
364 TRestEvent* evt = fOutputEvent;
365 {
366 TString BranchName = (TString)evt->GetName() + "Branch";
367 if (branchesToAdd.size() == 0)
368 branchesToAdd.push_back(pair<TString, TRestEvent*>(BranchName, evt));
369 else
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));
375 }
376 }
377 }
378
379 auto iter = branchesToAdd.begin();
380 while (iter != branchesToAdd.end()) {
381 fEventTree->Branch(iter->first, iter->second->ClassName(), iter->second);
382 iter++;
383 }
384
385 if (fEventTree->GetListOfBranches()->GetLast() < 0) // if no event branches are created
386 {
387 delete fEventTree;
388 fEventTree = nullptr;
389 }
390
391 // fAnalysisTree->CreateBranches();
392
393 // create output temp file for process-defined output object
394
395 fOutputFile->cd();
396 fOutputFile->Clear();
397 for (unsigned int i = 0; i < fProcessChain.size(); i++) {
398 fProcessChain[i]->InitProcess();
399 }
400
401 RESTDebug << "Thread " << fThreadId << " Ready!" << RESTendl;
402 } else {
403 string tmp = fHostRunner->GetInputEvent()->ClassName();
404 fInputEvent = REST_Reflection::Assembly(tmp);
405 fOutputEvent = fInputEvent;
406 auto outputFile = TRestRootFileHandle::Open(threadFileName, TRestRootFileMode::Recreate);
407 if (!outputFile) {
408 RESTError << outputFile.Error() << RESTendl;
409 exit(1);
410 }
411 if (fOutputFileOwner && !fOutputFileOwner.Close()) {
412 RESTError << fOutputFileOwner.Error() << RESTendl;
413 exit(1);
414 }
415 fOutputFileOwner = std::move(outputFile);
416 fOutputFile = fOutputFileOwner.Get();
417 fOutputFile->SetCompressionLevel(fCompressionLevel);
418 fOutputFile->cd();
419
420 RESTDebug << "Creating Analysis Tree..." << RESTendl;
421 fAnalysisTree = new TRestAnalysisTree("AnalysisTree_" + ToString(fThreadId), "dummyTree");
422 fAnalysisTree->DisableQuickObservableValueSetting();
423 fEventTree = new TTree((TString) "EventTree_" + ToString(fThreadId), "dummyTree");
424 // fEventTree->CreateEventBranches();
425
426 if (outputConfig[2]) {
427 TString BranchName = (TString)fInputEvent->GetName() + "Branch";
428 if (fEventTree->GetBranch(BranchName) == nullptr) // avoid duplicated branch
429 {
430 fEventTree->Branch(BranchName, fInputEvent->ClassName(), fInputEvent);
431 }
432 }
433 // currently, external process analysis is not supported!
434 }
435 if (outputConfigToDel) delete outputConfig;
436}
437
454 isFinished = false;
455
456 while (fHostRunner->GetNextevtFunc(fInputEvent, fAnalysisTree) == 0) {
457 ProcessEvent();
458 /*if (fOutputEvent != nullptr) */ fHostRunner->FillThreadEventFunc(this);
459 }
460
461 // fHostRunner->WriteThreadFileFunc(this);
462 isFinished = true;
463}
464
470 t = thread(&TRestThread::StartProcess, this);
471 t.detach();
472 // t.join();
473}
474
483 RESTDebug << "Entering TRestThread::AddProcess" << RESTendl;
484
485 fProcessChain.push_back(process);
486 // if (Console::CompatibilityMode && process->GetVerboseLevel() >= REST_Debug) {
487 // warning << "REST WARNING! Cannot use \"debug\" output level for process " << process->GetName()
488 // << endl;
489 // process->SetVerboseLevel(REST_Info);
490 //}
491 if (process->GetVerboseLevel() > fVerboseLevel) {
492 SetVerboseLevel(process->GetVerboseLevel());
493 }
494}
495
504 TRestEvent* ProcessedEvent = fInputEvent;
505 fProcessNullReturned = false;
506
508#ifdef TIME_MEASUREMENT
509 vector<int> processtime(fProcessChain.size());
510#endif
511
512 for (unsigned int j = 0; j < fProcessChain.size(); j++) {
513 cout << "------- Starting process " + (string)fProcessChain[j]->GetName() + " -------" << endl;
514
515#ifdef TIME_MEASUREMENT
516 high_resolution_clock::time_point t1 = high_resolution_clock::now();
517#endif
518
519 fProcessChain[j]->BeginOfEventProcess(ProcessedEvent);
520 ProcessedEvent = fProcessChain[j]->ProcessEvent(ProcessedEvent);
521 if (fProcessChain[j]->ApplyCut()) ProcessedEvent = nullptr;
522 fProcessChain[j]->EndOfEventProcess();
523
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();
527#endif
528
529 if (ProcessedEvent == nullptr) {
530 cout << "------- End of process " + (string)fProcessChain[j]->GetName() +
531 " (NULL returned) -------"
532 << endl;
533 fProcessNullReturned = true;
534 break;
535 } else {
536 cout << "------- End of process " + (string)fProcessChain[j]->GetName() + " -------" << endl;
537 }
538
539 if (fProcessChain[j]->GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Extreme &&
540 j < fProcessChain.size() - 1) {
541 GetChar();
542 }
543 }
544
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;
550 }
551#endif
552
553 if (fHostRunner->UseTestRun()) {
554 fOutputEvent = ProcessedEvent;
555 } else {
556 cout << "Transferring output event..." << endl;
557 if (ProcessedEvent != nullptr) {
558 ProcessedEvent->CloneTo(fOutputEvent);
559 }
560 }
561
562 GetChar(
563 "======= End of Event " +
564 ((fOutputEvent == nullptr) ? ToString(fInputEvent->GetID()) : ToString(fOutputEvent->GetID())) +
565 " =======");
566 } else {
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;
574 break;
575 }
576 }
577
578 if (fHostRunner->UseTestRun()) {
579 fOutputEvent = ProcessedEvent;
580 } else {
581 if (ProcessedEvent != nullptr) {
582 ProcessedEvent->CloneTo(fOutputEvent);
583 }
584 }
585 }
586}
587
594 RESTInfo << "TRestThread : Writing temp file. Thread id : " << fThreadId << RESTendl;
595
596 if (fOutputFile == nullptr) return;
597
598 fOutputFile->cd();
599 Int_t nErrors = 0;
600 Int_t nWarnings = 0;
601 for (unsigned int i = 0; i < fProcessChain.size(); i++) {
602 // The processes must call object->Write in this method
603 fProcessChain[i]->EndProcess();
604 if (fProcessChain[i]->GetError()) nErrors++;
605 if (fProcessChain[i]->GetWarning()) nWarnings++;
606 }
607
608 if (nWarnings)
609 RESTWarning << nWarnings
610 << " process warnings were found! Use run0->PrintWarnings(); to get additional info."
611 << RESTendl;
612 if (nErrors)
613 RESTError << nErrors << " process errors were found! Use run0->PrintErrors(); to get additional info."
614 << RESTendl;
615
616 delete fAnalysisTree;
617}
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.
A base class for any REST event process.
A base class for any REST event.
Definition TRestEvent.h:38
virtual void CloneTo(TRestEvent *target)
Clone the content of this TRestEvent object to another.
TRestStringOutput::REST_Verbose_Level GetVerboseLevel()
returns the verboselevel in type of REST_Verbose_Level enumerator
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
@ REST_Essential
+show some essential information, as well as warnings
@ 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.