25#include <TGeoManager.h>
27#include "Math/MinimizerOptions.h"
28#include "TBranchRef.h"
29#include "TInterpreter.h"
30#include "TMinuitMinimizer.h"
33#include "TRestManager.h"
34#include "TRestThread.h"
35#include "TRestTools.h"
43std::mutex mutex_write;
46#ifdef TIME_MEASUREMENT
48using namespace std::chrono;
52high_resolution_clock::time_point tS, tE;
56Long64_t bytesReadLast = 0;
57Double_t progressLast = 0;
58Int_t progressLastPrinted = 0;
59vector<Long64_t> bytesAdded(ncalculated, 0);
60vector<Double_t> progAdded(ncalculated, 0);
61int positionCalculated = 0;
62int printInterval = 200000;
63int inputTreeEntries = 0;
67TRestProcessRunner::TRestProcessRunner() {
Initialize(); }
69TRestProcessRunner::~TRestProcessRunner() {}
76 fInputEvent =
nullptr;
77 fOutputEvent =
nullptr;
79 fAnalysisTree =
nullptr;
80 fOutputDataFile =
nullptr;
92 fProcStatus = kNormal;
93 fFileSplitSize = 10000000000LL;
98 fValidateObservables =
false;
99 fSortOutputEvents =
true;
100 fInputAnalysisStorage =
true;
101 fInputEventStorage =
true;
102 fOutputEventStorage =
true;
103 fOutputAnalysisStorage =
true;
119 if (fRunInfo ==
nullptr) {
120 RESTError <<
"File IO has not been specified, " <<
RESTendl;
121 RESTError <<
"please make sure the \"TRestFiles\" section is ahead of the "
122 "\"TRestProcessRunner\" section"
127 RESTError <<
"manager not initialized!" <<
RESTendl;
136 if (!fRunInfo->GetFileProcess() && fRunInfo->GetEntries() == 0) {
137 RESTError <<
"TRestProcessRunner::BeginOfInit. The input file is a valid REST file but entries are 0!"
147 if (lastEntry - fFirstEntry > 0 && fEventsToProcess == 0) {
148 fEventsToProcess = lastEntry - fFirstEntry;
149 }
else if (fEventsToProcess > 0 && lastEntry - fFirstEntry > 0 &&
150 lastEntry - fFirstEntry != fEventsToProcess) {
151 RESTWarning <<
"Conflict number of events to process!" <<
RESTendl;
152 }
else if (fEventsToProcess > 0 && lastEntry - fFirstEntry == 0) {
153 lastEntry = fFirstEntry + fEventsToProcess;
154 }
else if (fEventsToProcess == 0 && fFirstEntry == 0 && lastEntry == 0) {
155 fEventsToProcess = REST_MAXIMUM_EVENTS;
156 lastEntry = REST_MAXIMUM_EVENTS;
158 RESTWarning <<
"error setting of event number" <<
RESTendl;
159 fEventsToProcess = fEventsToProcess > 0 ? fEventsToProcess : REST_MAXIMUM_EVENTS;
160 fFirstEntry = fFirstEntry > 0 ? fFirstEntry : 0;
161 lastEntry = lastEntry == fFirstEntry + fEventsToProcess ? lastEntry : REST_MAXIMUM_EVENTS;
163 fRunInfo->SetCurrentEntry(fFirstEntry);
165 if (fFileSplitSize < 50000000LL || fFileSplitSize > 100000000000LL) {
166 RESTWarning <<
"automatic file splitting size cannot < 10MB or > 100GB, setting to default (10GB)."
168 fFileSplitSize = 10000000000LL;
174 fProcStatus = kIgnore;
175 if (!fOutputAnalysisStorage) {
176 RESTError <<
"output analysis must be turned on to process data!" <<
RESTendl;
189 if (fThreadNumber < 1) {
192 if (fThreadNumber > 15) {
196 for (
int i = 0; i < fThreadNumber; i++) {
198 t->SetProcessRunner(
this);
201 t->SetCompressionLevel(fFileCompression);
202 fThreads.push_back(t);
215 if (keydeclare ==
"addProcess") {
217 if (active !=
"" &&
ToUpper(active) !=
"ON")
return 0;
223 if (processType ==
"") {
224 RESTWarning <<
"Bad expression of addProcess" <<
RESTendl;
226 }
else if (processName ==
"") {
227 RESTWarning <<
"Event process " << processType <<
" has no name, it will be skipped" <<
RESTendl;
231 RESTInfo <<
"adding process " << processType <<
" \"" << processName <<
"\"" <<
RESTendl;
232 vector<TRestEventProcess*> processes;
233 for (
int i = 0; i < fThreadNumber; i++) {
237 fRunInfo->SetExtProcess(p);
242 fUsePauseMenu =
false;
243 fProcStatus = kIgnore;
244 if (fThreadNumber > 1) {
245 RESTInfo <<
"multi-threading is disabled due to process \"" << p->GetName() <<
"\""
247 RESTInfo <<
"This process is in debug mode or is single thread only" <<
RESTendl;
248 for (i = fThreadNumber; i > 1; i--) {
250 fThreads.erase(fThreads.end() - 1);
255 processes.push_back(p);
261 for (
int i = 0; i < fThreadNumber; i++) {
263 for (
int j = 0; j < fThreadNumber; j++) {
266 fThreads[i]->AddProcess(p);
270 RESTDebug <<
"process \"" << processType <<
"\" has been added!" <<
RESTendl;
284 RESTDebug <<
"Validating process chain..." <<
RESTendl;
286 if (fRunInfo->GetFileProcess() !=
nullptr) {
287 fInputEvent = fRunInfo->GetFileProcess()->GetOutputEvent();
289 if (fThreads[0]->GetProcessnum() > 0 &&
290 fThreads[0]->GetProcess(0)->GetInputEvent().address !=
nullptr) {
291 string name = fThreads[0]->GetProcess(0)->GetInputEvent().type;
294 fRunInfo->SetInputEvent(a);
296 fInputEvent = fRunInfo->GetInputEvent();
298 if (fInputEvent ==
nullptr) {
299 RESTError <<
"Cannot determine input event, validating process chain failed!" <<
RESTendl;
303 if (fProcessNumber > 0) {
304 if (fThreads[0]->ValidateChain(fInputEvent) == -1) exit(1);
316 if (fRunInfo->GetFileProcess() !=
nullptr) {
317 fProcessInfo[
"FirstProcess"] = fRunInfo->GetFileProcess()->GetName();
319 if (fProcessNumber > 0) fProcessInfo[
"FirstProcess"] = fThreads[0]->GetProcess(0)->GetName();
321 int n = fProcessNumber;
322 fProcessInfo[
"LastProcess"] =
323 (n == 0 ? fProcessInfo[
"FirstProcess"] : fThreads[0]->GetProcess(n - 1)->GetName());
324 fProcessInfo[
"ProcNumber"] = ToString(n);
344 RESTDebug <<
"Creating output File " << fRunInfo->GetOutputFileName() <<
RESTendl;
346 TString filename = fRunInfo->FormFormat(fRunInfo->GetOutputFileName());
350 RESTError << output.Error() <<
RESTendl;
353 if (fOutputDataFileOwner && !fOutputDataFileOwner.Close()) {
354 RESTError << fOutputDataFileOwner.Error() <<
RESTendl;
357 fOutputDataFileOwner = std::move(output);
358 fOutputDataFile = fOutputDataFileOwner.Get();
362 if (!fOutputDataFile->IsOpen()) {
363 RESTError <<
"Failed to create output file: " << fOutputDataFile->GetName() <<
RESTendl;
367 RESTInfo <<
"TRestProcessRunner : preparing threads..." <<
RESTendl;
368 fRunInfo->ResetEntry();
369 fRunInfo->SetCurrentEntry(fFirstEntry);
370 for (
int i = 0; i < fThreadNumber; i++) {
371 fThreads[i]->PrepareToProcess(&fInputAnalysisStorage);
375 if (fRunInfo->GetFileProcess() !=
nullptr) {
376 RESTEssential << this->ClassName() <<
": 1 + " << fProcessNumber <<
" processes loaded, "
377 << fThreadNumber <<
" threads prepared!" <<
RESTendl;
379 RESTEssential << this->ClassName() <<
": " << fProcessNumber <<
" processes loaded, " << fThreadNumber
380 <<
" threads prepared!" <<
RESTendl;
383 if (fRunInfo->GetFileProcess() !=
nullptr) fRunInfo->GetFileProcess()->PrintMetadata();
385 for (
int i = 0; i < fProcessNumber; i++) {
386 fThreads[0]->GetProcess(i)->PrintMetadata();
389 if (fRunInfo->GetFileProcess() !=
nullptr) {
390 RESTcout <<
"(external) " << fRunInfo->GetFileProcess()->ClassName() <<
" : "
391 << fRunInfo->GetFileProcess()->GetName() <<
RESTendl;
393 for (
int i = 0; i < fProcessNumber; i++) {
394 RESTcout <<
"++ " << fThreads[0]->GetProcess(i)->ClassName() <<
" : "
395 << fThreads[0]->GetProcess(i)->GetName() <<
RESTendl;
401 fOutputDataFile->cd();
402 TTree* tree = fThreads[0]->GetEventTree();
403 if (tree !=
nullptr) {
408 TTree::SetMaxTreeSize(100000000000LL > fFileSplitSize * 2
410 : fFileSplitSize * 2);
416 fAnalysisTree =
new TRestAnalysisTree(
"AnalysisTree",
"REST Process Analysis Tree");
417 fAnalysisTree->SetDirectory(fOutputDataFile);
418 TRestAnalysisTree::SetMaxTreeSize(100000000000LL > fFileSplitSize * 2 ? 100000000000LL
419 : fFileSplitSize * 2);
421 tree = fThreads[0]->GetAnalysisTree();
422 if (tree !=
nullptr) {
423 fNBranches = tree->GetNbranches();
425 RESTError <<
"Threads are not initialized! No AnalysisTree!" <<
RESTendl;
429 fOutputDataFile->cd();
433 if (fAnalysisTree !=
nullptr) {
434 fAnalysisTree->Write(
nullptr, kOverwrite);
439 fProcessedEvents = 0;
440 fRunInfo->ResetEntry();
441 fRunInfo->SetCurrentEntry(fFirstEntry);
442 inputTreeEntries = fRunInfo->GetEntries();
446 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit");
447 TMinuitMinimizer::UseStaticMinuit(
false);
448 if (gGlobalMutex ==
nullptr) {
449 gGlobalMutex =
new TMutex(
true);
450 gROOTMutex = gGlobalMutex;
451 gInterpreterMutex = gGlobalMutex;
454#ifdef TIME_MEASUREMENT
455 high_resolution_clock::time_point t3 = high_resolution_clock::now();
459 RESTcout << this->ClassName() <<
": Starting the Process.." <<
RESTendl;
460 for (
int i = 0; i < fThreadNumber; i++) {
461 fThreads[i]->StartThread();
464 while (fProcStatus == kPause ||
465 (fRunInfo->GetInputEvent() !=
nullptr && fEventsToProcess > fProcessedEvents)) {
468 if (fProcStatus == kNormal && Console::kbhit())
470 int a = Console::ReadKey();
473 fProcStatus = kPause;
476 "| |", TRestStringOutput::REST_Display_Orientation::kMiddle);
477 Console::ClearLinesAfterCursor();
480 RESTLog <<
"PROCESS PAUSED!" <<
RESTendl;
486 if (fProcStatus == kPause) {
489 if (fProcStatus == kStopping) {
493 usleep(printInterval);
500 if (fProcStatus != kIgnore && Console::kbhit())
501 while (getchar() !=
'\n')
504 RESTEssential <<
"Waiting for processes to finish ..." <<
RESTendl;
508 bool finish = fThreads[0]->Finished();
509 for (
int i = 1; i < fThreadNumber; i++) {
510 finish = finish && fThreads[i]->Finished();
516 fAnalysisTree->GetEntry(fAnalysisTree->GetEntries() - 1);
518 for (
int i = 0; i < fThreadNumber; i++) {
519 fThreads[i]->EndProcess();
521 if (fRunInfo->GetFileProcess()) {
522 fRunInfo->GetFileProcess()->EndProcess();
524 fProcStatus = kFinished;
526#ifdef TIME_MEASUREMENT
527 high_resolution_clock::time_point t4 = high_resolution_clock::now();
528 deltaTime = (int)duration_cast<microseconds>(t4 - t3).count();
533 gGlobalMutex =
nullptr;
534 gROOTMutex =
nullptr;
535 gInterpreterMutex =
nullptr;
537 RESTcout << this->ClassName() <<
": " << fProcessedEvents <<
" processed events" <<
RESTendl;
539#ifdef TIME_MEASUREMENT
540 RESTInfo <<
"Total processing time : " << ((Double_t)deltaTime) / 1000. <<
" ms" <<
RESTendl;
541 RESTInfo <<
"Average read time from disk (per event) : "
542 << ((Double_t)readTime) / fProcessedEvents / 1000. <<
" ms" <<
RESTendl;
543 RESTInfo <<
"Average process time (per event) : "
544 << ((Double_t)(deltaTime - readTime - writeTime)) / fProcessedEvents / 1000. <<
" ms"
546 RESTInfo <<
"Average write time to disk (per event) : "
547 << ((Double_t)writeTime) / fProcessedEvents / 1000. <<
" ms" <<
RESTendl;
551 if (fRunInfo->GetOutputFileName() !=
"/dev/null") {
567 TRestStringOutput::REST_Display_Orientation::kMiddle);
568 Console::ClearLinesAfterCursor();
570 RESTLog <<
"--------------MENU--------------" <<
RESTendl;
571 RESTLog <<
"\"v\" : change the verbose level" <<
RESTendl;
572 RESTLog <<
"\"n\" : push foward one event, then pause" <<
RESTendl;
573 RESTLog <<
"\"l\" : print the latest processed event" <<
RESTendl;
574 RESTLog <<
"\"d\" : detach the current process" <<
RESTendl;
575 RESTLog <<
"\"q\" : stop and quit the process" <<
RESTendl;
576 RESTLog <<
"press \"p\" to continue process..." <<
RESTendl;
583 Console::ClearCurrentLine();
584 int b = Console::ReadKey();
587 Console::CursorUp(infobar);
588 RESTLog.setcolor(COLOR_BOLDGREEN);
589 RESTLog <<
"Changing verbose level for all the processes..." <<
RESTendl;
590 RESTLog.setcolor(COLOR_BOLDWHITE);
591 Console::CursorDown(1);
592 Console::ClearLinesAfterCursor();
593 RESTLog <<
"type \"0\"/\"s\" to set level silent" <<
RESTendl;
594 RESTLog <<
"type \"1\"/\"e\" to set level essential" <<
RESTendl;
595 RESTLog <<
"type \"2\"/\"i\" to set level info" <<
RESTendl;
596 RESTLog <<
"type \"3\"/\"d\" to set level debug" <<
RESTendl;
597 RESTLog <<
"type \"4\"/\"x\" to set level extreme" <<
RESTendl;
598 RESTLog <<
"type other to return the pause menu" <<
RESTendl;
603 Console::CursorUp(1);
604 int c = Console::Read();
606 while (Console::Read() !=
'\n')
609 if (c ==
'0' || c ==
's') {
611 }
else if (c ==
'1' || c ==
'e') {
613 }
else if (c ==
'2' || c ==
'i') {
615 }
else if (c ==
'3' || c ==
'd') {
617 }
else if (c ==
'4' || c ==
'x') {
620 Console::CursorUp(infobar);
621 RESTLog.setcolor(COLOR_BOLDYELLOW);
622 RESTLog <<
"Verbose level not set!" <<
RESTendl;
623 RESTLog.setcolor(COLOR_BOLDWHITE);
629 for (
int i = 0; i < fThreadNumber; i++) {
630 fThreads[i]->SetVerboseLevel(l);
631 for (
int j = 0; j < fThreads[i]->GetProcessnum(); j++) {
632 fThreads[i]->GetProcess(j)->SetVerboseLevel(l);
635 Console::CursorUp(infobar);
636 RESTLog.setcolor(COLOR_BOLDGREEN);
637 RESTLog <<
"Verbose level has been set to " << ToString(
static_cast<int>(l)) <<
"!"
639 RESTLog.setcolor(COLOR_BOLDWHITE);
642 Console::ClearLinesAfterCursor();
644 }
else if (b ==
'd') {
645 Console::CursorUp(infobar);
646 RESTLog.setcolor(COLOR_BOLDGREEN);
647 RESTLog <<
"Detaching restManager to backend" <<
RESTendl;
648 RESTLog.setcolor(COLOR_BOLDWHITE);
649 Console::CursorDown(1);
650 Console::ClearLinesAfterCursor();
651 RESTLog <<
"type filename for output redirect" <<
RESTendl;
652 RESTLog <<
"leave blank to redirect to /dev/null" <<
RESTendl;
663 Console::CursorUp(1);
664 file = Console::ReadLine();
665 if (file ==
"") file =
"/dev/null";
668 Console::CursorUp(infobar);
669 RESTLog.setcolor(COLOR_BOLDYELLOW);
670 RESTLog <<
"file not writeable!" <<
RESTendl;
671 RESTLog.setcolor(COLOR_BOLDWHITE);
676 Console::CursorUp(infobar);
677 RESTLog.setcolor(COLOR_BOLDYELLOW);
678 RESTLog <<
"path not writeable!" <<
RESTendl;
679 RESTLog.setcolor(COLOR_BOLDWHITE);
685 RESTWarning <<
"fork not available on windows!" <<
RESTendl;
690 perror(
"fork error:");
695 RESTcout <<
"Child process created! pid: " << getpid() <<
RESTendl;
696 RESTInfo <<
"Restarting threads" <<
RESTendl;
697 mutex_write.unlock();
698 for (
int i = 0; i < fThreadNumber; i++) {
699 fThreads[i]->StartThread();
701 RESTInfo <<
"Re-directing output to " << file <<
RESTendl;
702 FILE* f = freopen(file.c_str(),
"w", stdout);
703 if (f ==
nullptr) RESTWarning <<
"Couldnt redirect output for file: " << file <<
RESTendl;
704 REST_Display_CompatibilityMode =
true;
710 fProcStatus = kNormal;
711 RESTInfo <<
"Continue processing..." <<
RESTendl;
716 }
else if (b ==
'n') {
719 }
else if (b ==
'l') {
721 fOutputEvent->PrintEvent();
723 }
else if (b ==
'q') {
724 fProcStatus = kStopping;
726 }
else if (b ==
'p') {
727 Console::CursorUp(menuupper);
728 Console::ClearLinesAfterCursor();
730 fProcStatus = kIgnore;
732 fProcStatus = kNormal;
735 }
else if (b ==
'\n') {
738 Console::CursorUp(infobar);
739 RESTLog.setcolor(COLOR_BOLDYELLOW);
740 RESTLog <<
"Invailed option \"" << (char)b <<
"\" (key value: " << b <<
") !" <<
RESTendl;
741 RESTLog.setcolor(COLOR_BOLDWHITE);
742 Console::CursorDown(infobar - 1);
772 while (fProcStatus == kPause) {
775#ifdef TIME_MEASUREMENT
776 high_resolution_clock::time_point t1 = high_resolution_clock::now();
779 if (fProcessedEvents >= fEventsToProcess || targetevt ==
nullptr || fProcStatus == kStopping) {
782 if (!fInputAnalysisStorage) {
783 n = fRunInfo->GetNextEvent(targetevt,
nullptr);
785 n = fRunInfo->GetNextEvent(targetevt, targettree);
789#ifdef TIME_MEASUREMENT
790 high_resolution_clock::time_point t2 = high_resolution_clock::now();
791 readTime += (int)duration_cast<microseconds>(t2 - t1).count();
793 mutex_write.unlock();
804 if (fSortOutputEvents) {
808 bool smallest =
true;
810 if (otherT->Finished()) {
813 if (t->GetThreadId() == otherT->GetThreadId()) {
816 int id1 = t->GetInputEvent()->GetID();
817 int id2 = otherT->GetInputEvent()->GetID();
820 }
else if (id1 == id2) {
821 cout <<
"warning! Two events with same event id! output events maybe dis-ordered!"
833#ifdef TIME_MEASUREMENT
834 high_resolution_clock::time_point t5 = high_resolution_clock::now();
836 if (t->GetOutputEvent() !=
nullptr) {
837 fOutputEvent = t->GetOutputEvent();
840 TObjArray* branchesT;
841 TObjArray* branchesL;
843 if (fAnalysisTree !=
nullptr) {
854 fAnalysisTree->SetEventInfo(fOutputEvent);
855 for (
int n = 0; n < remotetree->GetNumberOfObservables(); n++) {
856 fAnalysisTree->SetObservable(n, remotetree->GetObservable(n));
859 fAnalysisTree->Fill();
864 branchesT = t->GetEventTree()->GetListOfBranches();
866 for (
int i = 0; i < branchesT->GetLast() + 1; i++) {
867 TBranch* branchT = (TBranch*)branchesT->UncheckedAt(i);
868 TBranch* branchL = (TBranch*)branchesL->UncheckedAt(i);
869 branchL->SetAddress(branchT->GetAddress());
878 if (fOutputDataFile->GetEND() > fFileSplitSize) {
879 if (fAnalysisTree->GetDirectory() == (TDirectory*)fOutputDataFile) {
881 cout <<
"TRestProcessRunner: file size reaches limit (" << fFileSplitSize
882 <<
" bytes), switching to new file with index " << fNFilesSplit << endl;
886 for (
auto th : fThreads) {
887 for (
int j = 0; j < fProcessNumber; j++) {
888 auto proc = th->GetProcess(j);
889 proc->NotifyAnalysisTreeReset();
893 fAnalysisTree->AutoSave();
894 fAnalysisTree->Reset();
902 fRunInfo->SetNFilesSplit(fNFilesSplit);
907 RESTError << mainFile.Error() <<
RESTendl;
911 mainFile->Write(0, TObject::kOverwrite);
912 if (!mainFile.Close()) {
913 RESTError << mainFile.Error() <<
RESTendl;
920 const std::string splitFileName =
924 RESTError << splitFile.Error() <<
RESTendl;
927 TFile* newfile = splitFile.Get();
929 TBranch* branch =
nullptr;
930 fAnalysisTree->SetDirectory(newfile);
931 TIter nextb1(fAnalysisTree->GetListOfBranches());
932 while ((branch = (TBranch*)nextb1())) {
933 branch->SetFile(newfile);
935 if (fAnalysisTree->GetBranchRef()) {
936 fAnalysisTree->GetBranchRef()->SetFile(newfile);
941 TIter nextb2(
fEventTree->GetListOfBranches());
942 while ((branch = (TBranch*)nextb2())) {
943 branch->SetFile(newfile);
950 fOutputDataFile->Write(
nullptr, TObject::kOverwrite);
951 if (!fOutputDataFileOwner.Close()) {
952 RESTError << fOutputDataFileOwner.Error() <<
RESTendl;
955 fOutputDataFileOwner = std::move(splitFile);
956 fOutputDataFile = fOutputDataFileOwner.Get();
958 RESTError <<
"internal error!" <<
RESTendl;
962#ifdef TIME_MEASUREMENT
963 high_resolution_clock::time_point t6 = high_resolution_clock::now();
964 writeTime += (int)duration_cast<microseconds>(t6 - t5).count();
967 if (fProcStatus == kStep) {
968 fProcStatus = kPause;
969 cout <<
"Processed events:" << fProcessedEvents << endl;
972 mutex_write.unlock();
981 RESTEssential <<
"Configuring output file, writing metadata and tree objects" <<
RESTendl;
982#ifdef TIME_MEASUREMENT
983 fProcessInfo[
"ProcessTime"] = ToString(deltaTime) +
"ms";
986 fOutputDataFile->cd();
990 if (fAnalysisTree !=
nullptr) {
991 fAnalysisTree->Write(
nullptr, kOverwrite);
995 fOutputDataFile->Write();
996 if (!fOutputDataFileOwner.Close()) {
997 RESTError << fOutputDataFileOwner.Error() <<
RESTendl;
1000 fOutputDataFile =
nullptr;
1006 fRunInfo->SetNFilesSplit(fNFilesSplit);
1013 if (gGeoManager !=
nullptr) {
1014 gGeoManager->Write(
"Geometry", TObject::kOverwrite);
1022 TFile* output = destination !=
nullptr ? destination : fOutputDataFile;
1023 if (output ==
nullptr || !output->IsOpen() || !output->IsWritable()) {
1024 RESTError <<
"Cannot write process metadata without a writable destination" <<
RESTendl;
1029 this->
Write(
nullptr, TObject::kWriteDelete);
1031 if (fRunInfo !=
nullptr && fRunInfo->GetFileProcess() !=
nullptr) {
1032 fRunInfo->GetFileProcess()->Write(
nullptr, kOverwrite);
1034 for (
int i = 0; i < fProcessNumber && !fThreads.empty(); i++) {
1035 fThreads[0]->GetProcess(i)->Write(
nullptr, kOverwrite);
1044 RESTEssential <<
"Merging thread files together" <<
RESTendl;
1049 vector<string> files_to_merge;
1050 for (
int i = 0; i < fThreadNumber; i++) {
1051 TFile* f = fThreads[i]->GetOutputFile();
1053 const std::string threadFileName = f->GetName();
1054 f->Write(
nullptr, TObject::kOverwrite);
1055 if (!fThreads[i]->CloseOutputFile()) {
1056 RESTError <<
"Failed to close thread output file " << threadFileName <<
RESTendl;
1059 files_to_merge.push_back(threadFileName);
1064 fOutputDataFile = fRunInfo->MergeToOutputFile(files_to_merge, (
string)
fOutputDataFileName);
1075#ifdef TIME_MEASUREMENT
1080 time_t tt = time(
nullptr);
1092 if (pc ==
nullptr)
return nullptr;
1097 pc->SetObservableValidation(fValidateObservables);
1103double TRestProcessRunner::GetReadingSpeed() {
1105 for (
auto& n : bytesAdded) bytes += n;
1106 double speedbyte = bytes / (double)printInterval * (
double)1000000 / ncalculated;
1115 if (fProcStatus == kNormal || fProcStatus == kIgnore) {
1124 double speedbyte = GetReadingSpeed();
1127 for (
auto& n : progAdded) progsum += n;
1128 double progspeed = progsum / ncalculated / printInterval * 1000000;
1131 if (fRunInfo->GetFeminosDaqTotalEvents() > 0) {
1132 prog = fProcessedEvents / (double)fRunInfo->GetFeminosDaqTotalEvents() * 100;
1133 }
else if (fEventsToProcess == REST_MAXIMUM_EVENTS && fRunInfo->GetFileProcess() !=
nullptr)
1136 prog = fRunInfo->GetBytesRead() / (double)fRunInfo->GetTotalBytes() * 100;
1137 }
else if (fRunInfo->GetFileProcess() !=
nullptr)
1140 prog = fProcessedEvents / (double)fEventsToProcess * 100;
1141 }
else if (fEventsToProcess == REST_MAXIMUM_EVENTS)
1144 prog = fRunInfo->GetCurrentEntry() / (double)inputTreeEntries * 100;
1146 prog = fProcessedEvents / (double)fEventsToProcess * 100;
1149 char* buffer =
new char[500]();
1150 if (fRunInfo->GetFileProcess() !=
nullptr && speedbyte > 0) {
1151 sprintf(buffer,
"%d Events (%.1fMB/s), ", fProcessedEvents, speedbyte / 1024 / 1024);
1153 sprintf(buffer,
"%d Events, ", fProcessedEvents);
1158 if (fProcStatus == kNormal) {
1159 sprintf(buffer,
"%.1f min ETA, (Pause: \"p\") ", (100 - progressLast) / progspeed / 60);
1161 sprintf(buffer,
"%.1f min ETA, (Pause Disabled) ", (100 - progressLast) / progspeed / 60);
1166 if (REST_Display_CompatibilityMode) {
1169 barlength = Console::GetWidth() - s1.size() - s2.size() - 9;
1171 sprintf(buffer, (
"%.1f%[" +
MakeProgressBar(prog, barlength) +
"]").c_str(), prog);
1176 if (REST_Display_CompatibilityMode) {
1177 if (((
int)prog) != progressLastPrinted) {
1178 cout << s1 << s2 << s3 << endl;
1179 progressLastPrinted = (int)prog;
1182 printf(
"%s", (s1 + s2 + s3 +
"\r").c_str());
1186 bytesAdded[positionCalculated] = fRunInfo->GetBytesRead() - bytesReadLast;
1187 bytesReadLast = fRunInfo->GetBytesRead();
1188 progAdded[positionCalculated] = prog - progressLast;
1189 progressLast = prog;
1191 positionCalculated++;
1192 if (positionCalculated >= ncalculated) positionCalculated -= ncalculated;
1200 string progressbar(length,
'-');
1201 int n = (double)progress100 / 100 * length;
1203 if (n > length + 1) n = length + 1;
1204 for (
int i = 0; i < n; i++) {
1205 progressbar[i] =
'=';
1207 if (n < length + 1) progressbar[n] =
'>';
1213TRestAnalysisTree* TRestProcessRunner::GetInputAnalysisTree() {
return fRunInfo->GetAnalysisTree(); }
1219 if (fProcStatus == kNormal)
1221 else if (fProcStatus == kStopping)
1222 status =
"Terminated";
1226 RESTMetadata <<
"Status : " << status <<
RESTendl;
1227 RESTMetadata <<
"Processed events : " << fProcessedEvents <<
RESTendl;
1228 RESTMetadata <<
"Analysis tree branches : " << fNBranches <<
RESTendl;
1229 RESTMetadata <<
"Thread number : " << fThreadNumber <<
RESTendl;
1230 RESTMetadata <<
"Processes in each thread : " << fProcessNumber <<
RESTendl;
1231 RESTMetadata <<
"File auto split size: " << fFileSplitSize <<
RESTendl;
1232 RESTMetadata <<
"File compression level: " << fFileCompression <<
RESTendl;
1233 RESTMetadata <<
"******************************************" <<
RESTendl;
REST core data-saving helper based on TTree.
A base class for any REST event process.
void SetRunInfo(TRestRun *r)
Set TRestRun for this process.
Bool_t singleThreadOnly() const
Return whether this process is single std::thread only.
Bool_t isExternal() const
Return whether this process is external process.
void SetParallelProcess(TRestEventProcess *p)
Add parallel process to this process.
A base class for any REST event.
virtual void Initialize()=0
Running the processes efficiently with fantastic display.
TString fOutputDataFileName
the TFile pointer being used
void ReadProcInfo()
Create a process info list which used called by TRestRun::FormFormat().
void PrintProcessedEvents(Int_t rateE)
Print number of events processed, file read speed, ETA and a progress bar.
void ResetRunTimes()
Reset running time count to 0.
void BeginOfInit()
Reads information from rml config file.
Int_t ReadConfig(const std::string &keydeclare, TiXmlElement *e)
method to deal with iterated child elements
void MergeOutputFile()
Calls TRestRun::MergeOutputFile() to merge the main file with process's tmp file.
void RunProcess()
The main executer of event process.
void EndOfInit()
Ending of the startup procedure.
TRestEventProcess * InstantiateProcess(TString type, TiXmlElement *ele)
InstantiateProcess in sequential start up.
void PauseMenu()
A pause menu providing some functions during the process.
void Initialize() override
REST run class.
std::string MakeProgressBar(int progress100, int length=100)
Make a string of progress bar with given length and percentage.
void WriteProcessesMetadata(TFile *destination=nullptr)
Write process metadata to the requested file, or fOutputDataFile by default.
Int_t GetNextevtFunc(TRestEvent *targetevt, TRestAnalysisTree *targettree)
Get next event and copy it to the address of targetevt.
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
Bool_t fUseTestRun
Number of files being split.
void FillThreadEventFunc(TRestThread *t)
Calling back the FillEvent() method in TRestThread.
void ConfigOutputFile()
Forming an output file.
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
TRestEvent * GetInputEvent() const
Calling GetInputEvent() will return a basic TRestEvent*.
REST_Verbose_Level
Enumerate of verbose level, containing five levels.
@ REST_Essential
+show some essential information, as well as warnings
@ REST_Extreme
show everything
@ REST_Info
+show most of the information for each steps
@ REST_Debug
+show the defined debug messages
@ REST_Silent
show minimum information of the software, as well as error messages
Threaded worker of a process chain.
TRestReflector Assembly(const std::string &typeName)
Assembly an object of type: typeName, returning the allocated memory address and size.
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.
std::string ToUpper(std::string in)
Convert string to its upper case. Alternative of TString::ToUpper.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
std::string ToDateTimeString(time_t time)
Format time_t into string.