39#include <TBranchElement.h>
40#include <TStreamerInfo.h>
45#include "TRestDataBase.h"
46#include "TRestEventProcess.h"
47#include "TRestManager.h"
48#include "TRestTools.h"
49#include "TRestVersion.h"
56Int_t GetDetectorSignalVersion(TBranch* branch) {
59 const auto visit = [&](
auto&& self, TBranch* current) ->
void {
60 if (
auto* element =
dynamic_cast<TBranchElement*
>(current)) {
61 const std::string name = current->GetName();
62 if (name ==
"fSignal.fSignalTime") time = element->GetClassVersion();
63 if (name ==
"fSignal.fSignalCharge") charge = element->GetClassVersion();
65 auto* children = current->GetListOfBranches();
66 for (
int index = 0; index <= children->GetLast(); ++index)
67 self(self,
static_cast<TBranch*
>(children->At(index)));
69 if (branch !=
nullptr) visit(visit, branch);
70 return time == charge ? time : -1;
73bool RequiresLegacySignalRecovery(TFile& file) {
74 auto* currentSignal = TClass::GetClass(
"TRestDetectorSignal");
75 auto* tree =
dynamic_cast<TTree*
>(file.Get(
"EventTree"));
76 auto* branch = tree ==
nullptr ? nullptr : tree->GetBranch(
"TRestDetectorSignalEventBranch");
77 const Int_t version = GetDetectorSignalVersion(branch);
78 if (currentSignal ==
nullptr || currentSignal->GetClassVersion() < 4 || version < 1 || version >= 4)
81 std::unique_ptr<TList> infos(file.GetStreamerInfoList());
82 if (infos ==
nullptr)
return true;
83 TIter next(infos.get());
84 while (
auto*
object = next()) {
85 auto* info =
dynamic_cast<TStreamerInfo*
>(object);
86 if (info !=
nullptr && std::string(info->GetName()) ==
"TRestDetectorSignal" &&
87 info->GetClassVersion() == version)
98TRestRun::TRestRun(
const string& filename) {
99 if (filename.find(
".root") != string::npos) {
102 }
else if (filename.find(
".rml") != string::npos) {
106 RESTError <<
"TRestRun::TRestRun(): input file error!" <<
RESTendl;
123 fRunUser = REST_USER;
128 fExperimentName =
"Null";
130 fRunDescription =
"Null";
132 fInputFileName =
"null";
133 fOutputFileName =
"rest_default.root";
141 fInputMetadata.clear();
143 fInputFileNames.clear();
144 fInputFile =
nullptr;
145 fOutputFile =
nullptr;
146 fInputEvent =
nullptr;
147 fAnalysisTree =
nullptr;
148 fEventTree =
nullptr;
150 fEventBranchLoc = -1;
151 fFileProcess =
nullptr;
152 fSaveHistoricData =
true;
167 RESTDebug <<
"Initializing TRestRun from config file, version: " << REST_RELEASE <<
RESTendl;
172 fRunUser = REST_USER;
185 string inputNameOld =
GetParameter(
"inputFile",
"default");
186 if (inputNameOld !=
"default") {
187 RESTWarning <<
"Parameter \"inputFile\" in rml is obsolete! Please update it to \"inputFileName\""
189 if (inputName.empty()) {
190 inputName = inputNameOld;
194 RESTError <<
"TRestRun: run number and input file name cannot both be "
200 if (
ToUpper(inputName) !=
"AUTO") {
201 fInputFileName = inputName;
205 if (
ToUpper(runNstr) !=
"AUTO") {
206 fRunNumber = atoi(runNstr.c_str());
209 if (
ToUpper(inputName) ==
"AUTO") {
212 fInputFileName = db->
query_run(fRunNumber).value;
213 fInputFileNames = Vector_cast<DBFile, TString>(files);
216 if (
ToUpper(runNstr) ==
"AUTO") {
220 fRunNumber = runs[0];
226 if (fRunNumber == 0) {
229 entry.runNr = fRunNumber;
230 entry.description = fRunDescription;
232 entry.type = fRunType;
233 entry.version = REST_RELEASE;
239 if (fRunNumber != -1) {
240 DBEntry entry = gDataBase->query_run(fRunNumber);
241 if (!entry.IsZombie()) {
244 fRunDescription = entry.description;
246 fRunType = entry.type;
250 if (fInputFileNames.empty()) {
251 if (fInputFileName !=
"") {
252 RESTError <<
"cannot find the input file!" <<
RESTendl;
255 RESTWarning <<
"no input file added" <<
RESTendl;
262 string outputName =
GetParameter(
"outputFileName",
"default");
263 string outputNameOld =
GetParameter(
"outputFile",
"default");
264 if (outputNameOld !=
"default") {
265 RESTWarning <<
"Parameter \"outputFile\" in rml is obsolete! Please update it to \"outputFileName\""
267 if (outputName ==
"default") {
268 outputName = outputNameOld;
271 if (
ToUpper(outputName) ==
"DEFAULT") {
274 char runParentStr[256];
276 char runNumberStr[256];
277 sprintf(runNumberStr,
"%05d", fRunNumber);
279 fOutputFileName = outputDir +
"Run_" + expName +
"_" + fRunUser +
"_" + runType +
"_" + fRunTag +
280 "_" + (TString)runNumberStr +
"_" + (TString)runParentStr +
"_V" + REST_RELEASE +
287 fOutputFileName = outputDir +
"Run_" + expName +
"_" + fRunUser +
"_" + runType +
"_" + fRunTag +
288 "_" + (TString)runNumberStr +
"_" + (TString)runParentStr +
"_V" +
289 REST_RELEASE +
".root";
291 }
else if (
ToUpper(outputName) ==
"NULL" || outputName ==
"/dev/null") {
292 fOutputFileName =
"/dev/null";
294 fOutputFileName = outputName;
297 fOutputFileName = outputDir +
"/" + outputName;
303 int z = system((TString)
"mkdir -p " + outputDir);
304 if (z != 0) RESTError <<
"Problem creating directory : " << outputDir <<
RESTendl;
307 RESTWarning <<
"TRestRun: Output path '" << outputDir <<
"' does not exist or it is not writable."
312 TiXmlElement* e =
fElement->FirstChildElement();
313 while (e !=
nullptr) {
314 string key = e->Value();
315 if (key ==
"addMetadata") {
316 if (e->Attribute(
"file") !=
nullptr) {
317 ImportMetadata(e->Attribute(
"file"), e->Attribute(
"name"), e->Attribute(
"type"),
true);
319 RESTWarning <<
"Wrong definition of addMetadata! Metadata name or file name "
323 }
else if (
Count(key,
"TRest") > 0) {
325 RESTWarning <<
"TRestRun: A root file is being included in section <" << key
326 <<
" ! To import metadata from this file, use <addMetadata" <<
RESTendl;
327 RESTWarning <<
"Skipping..." <<
RESTendl;
331 if (meta ==
nullptr) {
332 RESTWarning <<
"failed to add metadata \"" << key <<
"\"" <<
RESTendl;
333 e = e->NextSiblingElement();
338 fMetadata.push_back(meta);
341 e = e->NextSiblingElement();
347 RESTDebug <<
"TRestRun::EndOfInit. InputFile pattern: \"" << fInputFileName <<
"\"" <<
RESTendl;
348 RESTInfo <<
"which matches :" <<
RESTendl;
349 for (
const auto& inputFileName : fInputFileNames) {
350 RESTInfo << inputFileName <<
RESTendl;
352 RESTEssential <<
"(" << fInputFileNames.size() <<
" added files)" <<
RESTendl;
359 if (fInputFileNames.size() > (
unsigned int)i) {
360 TString Filename = fInputFileNames[i];
361 RESTInfo <<
"opening... " << Filename <<
RESTendl;
379 RESTError <<
"input file \"" << filename <<
"\" does not exist!" <<
RESTendl;
387 for (
const auto& inputFileName : fInputFileNames) {
388 if (inputFileName == filename) {
395 fInputFileNames.push_back(filename);
399 TString normalizedMode(mode);
400 normalizedMode.ToUpper();
401 TRestRootFileMode fileMode;
402 if (normalizedMode.IsNull() || normalizedMode ==
"READ" || normalizedMode ==
"OPEN") {
403 fileMode = TRestRootFileMode::Read;
404 }
else if (normalizedMode ==
"UPDATE") {
405 fileMode = TRestRootFileMode::Update;
407 RESTError <<
"TRestRun::OpenInputFile(): unsupported ROOT mode '" << mode
408 <<
"'. Supported modes are READ/OPEN and UPDATE." <<
RESTendl;
413 RESTError << inputFile.Error() <<
RESTendl;
416 fInputFileOwner = std::move(inputFile);
417 fInputFile = fInputFileOwner.Get();
419 if (RequiresLegacySignalRecovery(*fInputFile)) {
420 RESTError <<
"This file contains a legacy detector-signal schema that cannot be read safely "
421 "with the loaded REST version."
423 RESTError <<
"Recover it first with:" <<
RESTendl;
424 RESTError <<
" restRoot --recover-legacy-signals " << filename <<
RESTendl;
428 if (GetMetadataClass(
"TRestRun", fInputFile)) {
430 TString runTypeTmp = fRunType;
432 TString runTagTmp = fRunTag;
433 TString runDescriptionTmp = fRunDescription;
434 TString experimentNameTmp = fExperimentName;
435 TString outputFileNameTmp = fOutputFileName;
436 TString inputFileNameTmp = fInputFileName;
446 this->Read(GetMetadataClass(
"TRestRun", fInputFile)->GetName());
448 if (inputFileNameTmp !=
"null") fInputFileName = inputFileNameTmp;
449 if (outputFileNameTmp !=
"rest_default.root") fOutputFileName = outputFileNameTmp;
455 if (runTypeTmp !=
"Null" && runTypeTmp !=
"preserve") fRunType = runTypeTmp;
462 if (runTagTmp !=
"Null" && runTagTmp !=
"preserve") fRunTag = runTagTmp;
463 if (runDescriptionTmp !=
"Null" && runDescriptionTmp !=
"preserve")
464 fRunDescription = runDescriptionTmp;
465 if (experimentNameTmp !=
"Null" && experimentNameTmp !=
"preserve")
466 fExperimentName = experimentNameTmp;
473 if (fSaveHistoricData) {
474 if (this->GetVersionCode() >= REST_VERSION(2, 2, 1) ||
475 this->GetVersionCode() <= REST_VERSION(2, 1, 8)) {
476 ReadInputFileMetadata();
478 RESTWarning <<
"-- W : The metadata version found on input file is lower "
481 RESTWarning <<
"-- W : metadata from input file will not be read" <<
RESTendl;
485 RESTDebug <<
"Initializing input file : version code : " << this->GetVersionCode() <<
RESTendl;
486 RESTDebug <<
"Input file version : " << this->
GetVersion() << RESTendl;
487 ReadInputFileTrees();
490 RESTWarning <<
"TRestRun object not found in file! The input file may be incomplete!" <<
RESTendl;
491 ReadInputFileTrees();
494 fInputFile =
nullptr;
495 fAnalysisTree =
nullptr;
496 if (fFileProcess !=
nullptr) {
497 fFileProcess->OpenInputFiles(Vector_cast<TString, string>(fInputFileNames));
498 fFileProcess->InitProcess();
502 if (fAnalysisTree ==
nullptr && fFileProcess ==
nullptr)
503 RESTInfo <<
"Input file is not REST root file, an external process is needed!" <<
RESTendl;
506void TRestRun::AddInputFileExternal(
const string& file) {
508 if (fFileProcess !=
nullptr) {
509 bool add = fFileProcess->AddInputFile(file);
511 RESTError <<
"failed to add input file!" << RESTendl;
513 fInputFileNames.emplace_back(file);
518void TRestRun::ReadInputFileMetadata() {
519 TFile* f = fInputFile;
521 fInputMetadata.clear();
523 TIter nextkey(f->GetListOfKeys());
526 set<string> addedNames;
527 while ((key = (TKey*)nextkey())) {
528 RESTDebug <<
"Reading key with name : " << key->GetName() <<
RESTendl;
529 RESTDebug <<
"Key type (class) : " << key->GetClassName() <<
RESTendl;
531 if (!TClass::GetClass(key->GetClassName()) ||
532 !TClass::GetClass(key->GetClassName())->IsLoaded()) {
533 RESTError <<
"-- Class " << key->GetClassName() <<
" has no dictionary!" <<
RESTendl;
534 RESTError <<
"- Any relevant REST library missing? " <<
RESTendl;
535 RESTError <<
"- File reading will continue without loading key: " << key->GetName()
540 if (addedNames.count(key->GetName()) != 0) {
545 RESTDebug <<
"Key of type : " << a->ClassName() <<
"(" << a <<
")" <<
RESTendl;
548 RESTError <<
"TRestRun::ReadInputFileMetadata." <<
RESTendl;
549 RESTError <<
"Key name : " << key->GetName() <<
RESTendl;
550 RESTError <<
"Hidden key? Please, report this problem." <<
RESTendl;
551 }
else if (a->InheritsFrom(
"TRestMetadata") && a->ClassName() != (TString)
"TRestRun") {
571 fInputMetadata.push_back(a);
572 fMetadata.push_back(a);
573 addedNames.insert(key->GetName());
579void TRestRun::ReadInputFileTrees() {
580 if (fInputFile !=
nullptr) {
581 RESTDebug <<
"Finding TRestAnalysisTree.." <<
RESTendl;
582 TTree* _eventTree =
nullptr;
583 string filename = fInputFile->GetName();
585 if (fInputFile->Get(
"AnalysisTree") !=
nullptr) {
586 fAnalysisTree = (TRestAnalysisTree*)fInputFile->Get(
"AnalysisTree");
588 if (fNFilesSplit > 0) {
589 RESTEssential <<
"Linking analysis tree from split data files" <<
RESTendl;
590 fAnalysisTree = (TRestAnalysisTree*)fAnalysisTree->Clone();
592 for (
int i = 1; i <= fNFilesSplit; i++) {
593 string filename = fInputFile->GetName() + (string)
"." + ToString(i);
594 RESTInfo << filename <<
" --> ";
595 RESTInfo << (fAnalysisTree->AddChainFile(filename) ?
"success" :
"failed") <<
RESTendl;
597 if (fAnalysisTree->GetChain() ==
nullptr ||
598 fAnalysisTree->GetChain()->GetNtrees() != fNFilesSplit + 1) {
599 RESTError <<
"Error adding split files, files missing?" <<
RESTendl;
600 RESTError <<
"Your data could be incomplete!" <<
RESTendl;
607 fAnalysisTree->GetEntries();
609 fAnalysisTree->GetEntry(0);
611 _eventTree = (TTree*)fInputFile->Get(
"EventTree");
612 }
else if (fInputFile->FindKey(
"TRestAnalysisTree") !=
nullptr) {
616 RESTWarning <<
"Loading root file from old version REST!" <<
RESTendl;
617 fAnalysisTree = (TRestAnalysisTree*)fInputFile->Get(
"TRestAnalysisTree");
618 fAnalysisTree->GetEntry(0);
620 TIter nextkey(fInputFile->GetListOfKeys());
622 while ((key = (TKey*)nextkey())) {
624 if (((
string)key->GetName()).find(
"EventTree") != string::npos) {
625 _eventTree = (TTree*)fInputFile->Get(key->GetName());
626 string eventName =
Replace(key->GetName(),
"Tree",
"", 0);
627 TBranch* br = _eventTree->GetBranch(
"eventBranch");
628 br->SetName((eventName +
"Branch").c_str());
629 br->SetTitle((eventName +
"Branch").c_str());
633 RESTDebug <<
"Old REST file successfully recovered!" <<
RESTendl;
635 RESTWarning <<
"(OpenInputFile) : AnalysisTree was not found" <<
RESTendl;
636 RESTWarning <<
"Inside file : " << filename <<
RESTendl;
637 RESTWarning <<
"This may not be a REST output file!" <<
RESTendl;
640 if (_eventTree !=
nullptr) {
641 if (fNFilesSplit > 0) {
644 RESTEssential <<
"Linking event tree from split data files" <<
RESTendl;
645 TChain* _fEventTree =
new TChain(
"EventTree");
646 RESTInfo << fInputFile->GetName() <<
" --> ";
647 RESTInfo << (_fEventTree->Add(fInputFile->GetName()) ?
"success" :
"failed") <<
RESTendl;
649 for (
int i = 1; i <= fNFilesSplit; i++) {
650 string filename = fInputFile->GetName() + (string)
"." + ToString(i);
651 RESTInfo << filename <<
" --> ";
652 RESTInfo << (_fEventTree->Add(filename.c_str()) ?
"success" :
"failed") <<
RESTendl;
654 fEventTree = _fEventTree;
656 fEventTree = _eventTree;
659 RESTDebug <<
"Finding event branch.." <<
RESTendl;
660 if (fInputEvent ==
nullptr) {
661 TObjArray* branches = fEventTree->GetListOfBranches();
663 if (branches->GetLast() > -1) {
664 TBranch* br = (TBranch*)branches->At(branches->GetLast());
666 if (br ==
nullptr ||
Count(br->GetName(),
"EventBranch") == 0) {
667 RESTInfo <<
"No event branch inside file : " << filename <<
RESTendl;
668 RESTInfo <<
"This file may be a pure analysis file" <<
RESTendl;
670 string type =
Replace(br->GetName(),
"Branch",
"", 0);
671 TClass* cl = TClass::GetClass(type.c_str());
672 if (cl->HasDictionary()) {
674 }
else if (fInputEvent !=
nullptr) {
676 fInputEvent =
nullptr;
679 if (fInputEvent ==
nullptr) {
680 RESTError <<
"TRestRun:OpenInputFile. Cannot initialize input event, event "
684 <<
"Please install corresponding libraries to provide root dictionaries for "
690 fInputEvent->InitializeWithMetadata(
this);
691 fEventTree->SetBranchAddress(br->GetName(), &fInputEvent);
692 fEventBranchLoc = branches->GetLast();
693 RESTDebug <<
"found event branch of event type: " << fInputEvent->ClassName()
698 string brname = (string)fInputEvent->ClassName() +
"Branch";
699 if (fEventTree->GetBranch(brname.c_str()) ==
nullptr) {
700 RESTWarning <<
"REST WARNING (OpenInputFile) : No matched event branch "
703 RESTWarning <<
"Branch required: " << brname <<
RESTendl;
705 fEventTree->SetBranchAddress(brname.c_str(), &fInputEvent);
706 RESTDebug << brname <<
" is found and set!" <<
RESTendl;
710 RESTDebug <<
"TRestRun:OpenInputFile. EventTree was not found" <<
RESTendl;
711 RESTDebug <<
"This is a pure analysis file!" <<
RESTendl;
712 fInputEvent =
nullptr;
725 RESTDebug <<
"begin collecting basic file info..." << filename <<
RESTendl;
729 FILE* fp = fopen(filename.c_str(),
"rb");
731 RESTError <<
"TRestRun::ReadFileInfo. Something went wrong with fopen()!" << strerror(errno)
743 fTotalBytes = buf.st_size;
746 RESTDebug <<
"begin matching file name pattern for more file info..." <<
RESTendl;
754 vector<string> formatSectionList;
755 vector<string> formatPrefixList;
761 pos1 = format.find(
"[", pos + 1);
762 pos2 = format.find(
"]", pos1);
763 if (pos1 == -1 || pos2 == -1) {
764 formatPrefixList.push_back(format.substr(pos + 1, -1));
768 formatSectionList.push_back(format.substr(pos1 + 1, pos2 - pos1 - 1));
769 formatPrefixList.push_back(format.substr(pos + 1, pos1 - pos - 1));
775 for (
unsigned int i = 0; i < formatSectionList.size() && i < formatPrefixList.size() - 1; i++) {
776 if (i != 0 && formatPrefixList[i].empty()) {
777 RESTWarning <<
"file format reference contains error!" <<
RESTendl;
780 int pos1 = name.find(formatPrefixList[i], pos + 1) + formatPrefixList[i].size();
781 if (formatPrefixList[i].empty()) pos1 = 0;
782 int pos2 = name.find(formatPrefixList[i + 1], pos1);
783 if (formatPrefixList[i + 1].empty()) pos2 = name.length();
784 if (pos1 == -1 || pos2 == -1) {
785 RESTWarning <<
"File pattern matching: file format mismatch!" <<
RESTendl;
789 string infoFromFileName = name.substr(pos1, pos2 - pos1);
791 RESTDebug <<
"File pattern matching. key: " << formatSectionList[i] <<
" (between the mark \""
792 << formatPrefixList[i] <<
"\" and \"" << formatPrefixList[i + 1]
793 <<
"\"), value: " << infoFromFileName <<
RESTendl;
796 bool inforead =
false;
800 RESTValue member = RESTValue(
this, this->ClassName()).GetDataMember(formatSectionList[i]);
805 RESTWarning <<
"TRestRun: file name format field \"" << formatSectionList[i]
806 <<
"\"(value = " << infoFromFileName
807 <<
") not registered, data member does not exist in TRestRun!" <<
RESTendl;
815 vector<string> classDataMember =
Split(formatSectionList[i],
"::");
816 if (classDataMember.size() > 1) {
818 if (meta !=
nullptr) {
819 RESTValue member = RESTValue(meta, meta->ClassName()).GetDataMember(classDataMember[1]);
825 RESTWarning <<
"TRestRun: file name format field \"" << formatSectionList[i]
826 <<
"\"(value = " << infoFromFileName
827 <<
") not registered, metadata exist but without such datamember field!"
831 RESTWarning <<
"TRestRun: file name format field \"" << formatSectionList[i]
832 <<
"\"(value = " << infoFromFileName
833 <<
") not registered, metadata does not exist!" <<
RESTendl;
855 if (fFileProcess !=
nullptr) {
856 fFileProcess->ResetEntry();
869 bool messageShown =
false;
872 if (fFileProcess !=
nullptr) {
873 RESTDebug <<
"TRestRun: getting next event from external process" <<
RESTendl;
876 fFileProcess->BeginOfEventProcess();
877 eve = fFileProcess->ProcessEvent(
nullptr);
878 fFileProcess->EndOfEventProcess();
880 fBytesRead = fFileProcess->GetTotalBytesRead();
887 if (eve ==
nullptr) {
888 RESTDebug <<
"TRestRun::GetNextEvent(): input event has not been initialized!" <<
RESTendl;
890 RESTDebug <<
"TRestRun: getting next event from root file" <<
RESTendl;
891 if (fAnalysisTree ==
nullptr) {
892 RESTWarning <<
"error to get event from input file, missing analysis tree from input file"
896 if (fCurrentEvent >= fAnalysisTree->GetTree()->GetEntriesFast()) {
899 if (targetTree !=
nullptr) {
902 fBytesRead += fAnalysisTree->GetEntry(fCurrentEvent);
903 targetTree->SetEventInfo(fAnalysisTree);
904 for (
int n = 0; n < fAnalysisTree->GetNumberOfObservables(); n++)
905 targetTree->
SetObservable(n, fAnalysisTree->GetObservable(n));
907 if (fEventTree !=
nullptr) {
908 if (fEventTree->IsA() == TChain::Class()) {
909 Long64_t entry = fEventTree->LoadTree(fCurrentEvent);
910 fBytesRead += ((TBranch*)fEventTree->GetTree()->GetListOfBranches()->UncheckedAt(
915 ((TBranch*)fEventTree->GetListOfBranches()->UncheckedAt(fEventBranchLoc))
916 ->GetEntry(fCurrentEvent);
927 if (eve ==
nullptr) {
928 if (fHangUpEndFile && fFileProcess !=
nullptr) {
932 RESTEssential <<
"external process file reading reaches end, waiting for more files"
947 if (fInputEvent->GetID() == 0 && fInputEvent->GetSubID() == 0) {
948 fInputEvent->SetID(fCurrentEvent - 1);
951 if (fInputEvent->GetRunOrigin() == 0) {
952 fInputEvent->SetRunOrigin(fRunNumber);
956 fInputEvent->CloneTo(targetEvent);
965 std::vector<std::string> list;
966 TObjArray* branches = GetEventTree()->GetListOfBranches();
967 for (
int i = 0; i <= branches->GetLast(); i++) {
968 auto branch = (TBranch*)branches->At(i);
969 if (((std::string)branch->GetName()).find(
"EventBranch") != string::npos) {
970 std::string eventType =
Replace((
string)branch->GetName(),
"Branch",
"");
971 list.emplace_back(eventType);
981 if (entry >= GetEntries()) {
982 RESTWarning <<
"TRestRun::GetEntry. Entry requested out of limits" <<
RESTendl;
983 RESTWarning <<
"Total number of entries is : " << GetEntries() <<
RESTendl;
986 if (fAnalysisTree !=
nullptr) {
987 fAnalysisTree->GetEntry(entry);
989 if (fEventTree !=
nullptr) {
990 fEventTree->GetEntry(entry);
993 if (fInputEvent !=
nullptr) {
994 fInputEvent->InitializeReferences(
this);
997 fCurrentEvent = entry;
1013 string inString = (string)FilenameFormat;
1014 string outString = (string)FilenameFormat;
1016 RESTDebug <<
"TRestRun::FormFormat. In string : " << inString <<
RESTendl;
1020 int pos1 = inString.find(
"[", pos);
1021 int pos2 = inString.find(
"]", pos1);
1022 if (pos1 == -1 || pos2 == -1)
break;
1024 string targetstr = inString.substr(pos1, pos2 - pos1 + 1);
1025 string target = inString.substr(pos1 + 1, pos2 - pos1 - 1);
1026 string replacestr = GetRunInformation(target);
1028 RESTDebug <<
"TRestRun::FormFormat. target : " << target <<
RESTendl;
1029 RESTDebug <<
"TRestRun::FormFormat. replacestr : " << replacestr <<
RESTendl;
1034 if (target ==
"fVersion") replacestr = (string)REST_RELEASE;
1035 if (target ==
"fCommit") replacestr = (string)REST_COMMIT;
1037 if (replacestr != target) {
1038 if (target ==
"fRunNumber" || target ==
"fParentRunNumber") {
1041 outString =
Replace(outString, targetstr, replacestr, 0);
1059 RESTDebug <<
"TRestRun::FormOutputFile. target : " << outputfilename <<
RESTendl;
1060 const string filename = outputfilename.empty() ? string(fOutputFileName.Data()) : outputfilename;
1062 const string finalFilename = fOutputFileName.Data();
1063 RESTInfo << (outputfilename.empty() ?
"Creating file : " :
"Updating file : ") << finalFilename
1066 std::error_code targetStatusError;
1067 const bool targetExists = !outputfilename.empty() && std::filesystem::exists(filename, targetStatusError);
1068 if (targetStatusError) {
1069 fOutputFileName =
"";
1070 RESTError <<
"Cannot inspect merge target '" << filename <<
"': " << targetStatusError.message()
1074 const string existingTarget = targetExists ? filename :
"";
1075 std::string mergeError;
1078 fOutputFileName =
"";
1079 RESTError <<
"(Merge files) " << mergeError <<
RESTendl;
1084 std::error_code removalError;
1085 std::filesystem::remove(filename, removalError);
1087 RESTWarning <<
"Merged output is valid, but the pre-format target '" << filename
1088 <<
"' could not be removed: " << removalError.message() <<
RESTendl;
1094 RESTError << outputFile.Error() <<
RESTendl;
1095 fOutputFileName =
"";
1098 if (fOutputFileOwner && !fOutputFileOwner.Close()) {
1099 RESTError << fOutputFileOwner.Error() <<
RESTendl;
1100 fOutputFileName =
"";
1103 fOutputFileOwner = std::move(outputFile);
1104 fOutputFile = fOutputFileOwner.Get();
1105 RESTDebug <<
"TRestRun::FormOutputFile. Calling WriteWithDataBase()" <<
RESTendl;
1108 RESTcout << this->ClassName() <<
" Created ..." <<
RESTendl;
1121 fOutputFileName =
FormFormat(fOutputFileName);
1123 fOutputFileName = std::filesystem::weakly_canonical(fOutputFileName.Data());
1127 RESTError << outputFile.Error() <<
RESTendl;
1130 fOutputFileOwner = std::move(outputFile);
1131 fOutputFile = fOutputFileOwner.Get();
1133 fEventTree =
new TTree(
"EventTree",
"EventTree");
1135 fAnalysisTree->Write(
nullptr, TObject::kOverwrite);
1136 fEventTree->Write(
nullptr, TObject::kOverwrite);
1139 RESTcout <<
"TRestRun: Output File Created." <<
RESTendl;
1146TFile* TRestRun::UpdateOutputFile() {
1147 if (fOutputFile !=
nullptr) {
1148 if (!fOutputFile->IsOpen()) {
1149 RESTError <<
"TRestRun::UpdateOutputFile(): output file is closed" << RESTendl;
1152 if (!fOutputFile->IsWritable()) {
1153 std::string updateError;
1155 RESTError << updateError <<
RESTendl;
1162 fAnalysisTree->Write(
nullptr, kOverwrite);
1163 fEventTree->Write(
nullptr, kOverwrite);
1167 RESTcout <<
"TRestRun: Output File Updated." <<
RESTendl;
1174 RESTError <<
"TRestRun::UpdateOutputFile(): output file is closed" <<
RESTendl;
1191 RESTDebug <<
"TRestRun::WriteWithDataBase. Getting entries in analysisTree" <<
RESTendl;
1195 if (fOutputFile !=
nullptr) {
1197 if (tree !=
nullptr) {
1198 fEntriesSaved = tree->GetEntries();
1201 RESTDebug <<
"TRestRun::WriteWithDataBase. Entries found : " << fEntriesSaved <<
RESTendl;
1210 fRunUser = REST_USER;
1214 RESTDebug <<
"TRestRun::WriteWithDataBase. Calling this->Write(0, kOverwrite)" <<
RESTendl;
1215 this->
Write(
nullptr, kOverwrite);
1217 RESTDebug <<
"TRestRun::WriteWithDataBase. Succeed" <<
RESTendl;
1218 RESTDebug <<
"TRestRun::WriteWithDataBase. fMetadata.size() == " << fMetadata.size() <<
RESTendl;
1219 for (
auto& metadata : fMetadata) {
1220 bool historic =
false;
1221 RESTDebug <<
"TRestRun::WriteWithDataBase. fInputMetadata.size() == " << fInputMetadata.size()
1223 for (
const auto& inputMetadata : fInputMetadata) {
1224 RESTDebug << metadata->GetName() <<
" == " << inputMetadata->GetName() <<
RESTendl;
1225 if (metadata == inputMetadata) {
1232 RESTDebug <<
"NO historic" <<
RESTendl;
1233 metadata->Write(metadata->GetName(), kOverwrite);
1235 RESTDebug <<
"IS historic" <<
RESTendl;
1236 if (fSaveHistoricData) metadata->Write(metadata->GetName(), kOverwrite);
1241 RESTDebug <<
"TResRun::WriteWithDataBase. Run number is : " << fRunNumber <<
RESTendl;
1242 if (fRunNumber != -1) {
1243 int fileid = gDataBase->set_runfile(fRunNumber, (
string)fOutputFileName);
1244 RESTcout <<
"DataBase Entry Added! Run Number: " << fRunNumber <<
", File ID: " << fileid <<
RESTendl;
1253 if (fAnalysisTree !=
nullptr) {
1254 fEntriesSaved = fAnalysisTree->GetEntries();
1255 if (fAnalysisTree->GetEntries() > 0 && fInputFile ==
nullptr) {
1256 if (fOutputFile !=
nullptr) {
1258 fAnalysisTree->Write(
nullptr, kOverwrite);
1259 this->
Write(
nullptr, kOverwrite);
1262 delete fAnalysisTree;
1263 fAnalysisTree =
nullptr;
1266 if (fEventTree !=
nullptr) {
1267 if (fEventTree->GetEntries() > 0 && fInputFile ==
nullptr) fEventTree->Write(
nullptr, kOverwrite);
1269 fEventTree =
nullptr;
1272 for (
unsigned int i = 0; i < fMetadata.size(); i++) {
1273 for (
unsigned int j = 0; j < fInputMetadata.size(); j++) {
1274 if (fMetadata[i] == fInputMetadata[j]) {
1275 delete fMetadata[i];
1276 fMetadata.erase(fMetadata.begin() + i);
1278 fInputMetadata.erase(fInputMetadata.begin() + j);
1284 fInputEvent =
nullptr;
1285 if (fOutputFile !=
nullptr) {
1286 fOutputFile->Write(0, TObject::kOverwrite);
1287 if (fOutputFileOwner.Get() == fOutputFile) {
1288 if (!fOutputFileOwner.Close()) RESTError << fOutputFileOwner.Error() <<
RESTendl;
1289 }
else if (fOutputFile != fInputFile) {
1290 fOutputFile->Close();
1293 fOutputFile =
nullptr;
1295 if (fInputFile !=
nullptr) {
1296 if (fInputFileOwner.Get() == fInputFile) {
1297 if (!fInputFileOwner.Close()) RESTError << fInputFileOwner.Error() <<
RESTendl;
1299 fInputFile->Close();
1301 fInputFile =
nullptr;
1303 if (fOutputFileOwner && !fOutputFileOwner.Close()) RESTError << fOutputFileOwner.Error() <<
RESTendl;
1304 if (fInputFileOwner && !fInputFileOwner.Close()) RESTError << fInputFileOwner.Error() <<
RESTendl;
1311 if (fFileProcess ==
nullptr && p !=
nullptr) {
1314 fFileProcess->
OpenInputFiles(Vector_cast<TString, string>(fInputFileNames));
1315 fFileProcess->InitProcess();
1316 fInputEvent = fFileProcess->GetOutputEvent();
1317 if (fInputEvent ==
nullptr) {
1318 RESTError <<
"The external process \"" << p->GetName() <<
"\" doesn't yield any output event!"
1322 fInputEvent->SetRunOrigin(fRunNumber);
1326 if (fInputFileOwner && !fInputFileOwner.Close()) RESTError << fInputFileOwner.Error() <<
RESTendl;
1327 fInputFile =
nullptr;
1329 fAnalysisTree =
new TRestAnalysisTree(
"externalProcessAna",
"externalProcessAna");
1335 RESTInfo <<
"The external file process has been set! Name : " << fFileProcess->GetName() <<
RESTendl;
1337 if (fFileProcess !=
nullptr) {
1338 RESTError <<
"There can only be one file process!" <<
RESTendl;
1342 RESTWarning <<
"Given file process is null, skipping..." <<
RESTendl;
1354 if (event !=
nullptr) {
1355 if (fEventTree !=
nullptr) {
1356 if (fInputEvent !=
nullptr) {
1357 fEventTree->SetBranchAddress((TString)fInputEvent->ClassName() +
"Branch",
nullptr);
1358 fEventTree->SetBranchStatus((TString)fInputEvent->ClassName() +
"Branch",
false);
1360 TObjArray* branches = fEventTree->GetListOfBranches();
1361 string branchName = (string)event->ClassName() +
"Branch";
1362 for (
int i = 0; i <= branches->GetLast(); i++) {
1363 auto branch = (TBranch*)branches->At(i);
1364 if ((
string)branch->GetName() == branchName) {
1365 RESTDebug <<
"Setting input event.. Type: " <<
event->ClassName() <<
" Address: " <<
event
1367 fInputEvent = event;
1368 fEventTree->SetBranchAddress(branchName.c_str(), &fInputEvent);
1369 fEventTree->SetBranchStatus(branchName.c_str(),
false);
1370 fEventBranchLoc = i;
1372 }
else if (i == branches->GetLast()) {
1373 RESTWarning <<
"REST Warning : (TRestRun) cannot find corresponding "
1374 "branch in event tree!"
1376 RESTWarning <<
"Event Type : " <<
event->ClassName() <<
RESTendl;
1377 RESTWarning <<
"Input event not set!" <<
RESTendl;
1381 fInputEvent = event;
1391 if (event !=
nullptr) {
1392 if (fEventTree !=
nullptr) {
1393 string eventName = (string)event->ClassName();
1394 string branchName = eventName +
"Branch";
1395 fEventTree->Branch(branchName.c_str(), event);
1396 fEventTree->SetTitle((eventName +
"Tree").c_str());
1407 const TString thisFile =
SearchFile(File.Data());
1408 if (thisFile ==
"") {
1409 RESTError <<
"(ImportMetadata): The file " << thisFile <<
" does not exist!" <<
RESTendl;
1414 RESTError <<
"(ImportMetadata) : The file " << thisFile <<
" is not root file!" <<
RESTendl;
1415 RESTError <<
"If you want to initialize metadata from rml file, use <TRest section!" <<
RESTendl;
1419 TFile* file = TFile::Open(thisFile);
1421 if (type ==
"" && name ==
"") {
1422 RESTError <<
"(ImportMetadata) : metadata type and name is not "
1430 meta = GetMetadata(name, file);
1431 }
else if (type !=
"") {
1432 meta = GetMetadataClass(type, file);
1435 if (meta ==
nullptr) {
1436 cout <<
"REST ERROR (ImportMetadata) : " << name <<
" does not exist." << endl;
1437 cout <<
"Inside root file : " << File << endl;
1449 fMetadata.push_back(meta);
1460Double_t TRestRun::GetRunLength()
const {
1462 cout <<
"Run time is not set\n";
1467Long64_t TRestRun::GetTotalBytes() {
1468 if (fFileProcess !=
nullptr) {
1469 fTotalBytes = fFileProcess->GetTotalBytes();
1474Long64_t TRestRun::GetEntries()
const {
1475 if (fAnalysisTree !=
nullptr) {
1476 return fAnalysisTree->GetEntries();
1478 return fEntriesSaved;
1483TRestEvent* TRestRun::GetEventWithID(Int_t eventID, Int_t subEventID,
const TString& tag) {
1484 if (fAnalysisTree !=
nullptr) {
1485 int nEntries = fAnalysisTree->GetEntries();
1488 fAnalysisTree->SetBranchStatus(
"*",
false);
1489 fAnalysisTree->SetBranchStatus(
"eventID",
true);
1490 fAnalysisTree->SetBranchStatus(
"subEventID",
true);
1491 fAnalysisTree->SetBranchStatus(
"subEventTag",
true);
1495 for (
int i = 0; i < nEntries; i++) {
1496 fAnalysisTree->GetEntry(i);
1497 if (fAnalysisTree->GetEventID() == eventID) {
1498 if (subEventID != -1 && fAnalysisTree->GetSubEventID() != subEventID)
continue;
1499 if (tag !=
"" && fAnalysisTree->GetSubEventTag() != tag)
continue;
1500 if (fEventTree !=
nullptr) fEventTree->GetEntry(i);
1501 fAnalysisTree->SetBranchStatus(
"*",
true);
1502 fAnalysisTree->GetEntry(i);
1508 fAnalysisTree->SetBranchStatus(
"*",
true);
1513std::vector<int> TRestRun::GetEventEntriesWithConditions(
const string& cuts,
int startingIndex,
1515 int max = maxNumber;
1516 if (max < 0) max = GetEntries();
1518 std::vector<int> eventIds;
1520 std::vector<string> observables;
1521 std::vector<string> operators;
1522 std::vector<Double_t> values;
1524 const std::vector<string> validOperators = {
"==",
"<=",
">=",
"=",
">",
"<"};
1526 vector<string> cutsVector =
Split(cuts,
"&&",
false,
true);
1528 for (
unsigned int i = 0; i < cutsVector.size(); i++) {
1529 string cut = cutsVector[i];
1530 for (
unsigned int j = 0; j < validOperators.size(); j++) {
1531 if (cut.find(validOperators[j]) != string::npos) {
1532 operators.push_back(validOperators[j]);
1533 observables.push_back((
string)cut.substr(0, cut.find(validOperators[j])));
1534 values.push_back(std::stod((
string)cut.substr(
1535 cut.find(validOperators[j]) + validOperators[j].length(), string::npos)));
1542 if (fAnalysisTree ==
nullptr) {
1545 Int_t nEntries = fAnalysisTree->GetEntries();
1546 auto branches = fAnalysisTree->GetListOfBranches();
1547 std::set<string> branchNames;
1548 for (
int i = 0; i < branches->GetEntries(); i++) {
1549 branchNames.insert((
string)branches->At(i)->GetName());
1552 for (
unsigned int i = 0; i < observables.size(); i++) {
1554 if (std::find(validOperators.begin(), validOperators.end(), operators[i]) == validOperators.end()) {
1556 cout <<
"invalid operation '" << operators[i] <<
"' for 'TRestRun::GetEventIdsWithConditions'"
1561 if (branchNames.count(observables[i]) == 0) {
1563 cout <<
"invalid observable '" << observables[i] <<
"' for 'TRestRun::GetEventIdsWithConditions'"
1565 cout <<
"valid branch names: ";
1566 for (
auto branchName : branchNames) {
1567 cout << branchName <<
" ";
1574 fAnalysisTree->SetBranchStatus(
"*",
false);
1575 for (
unsigned int i = 0; i < observables.size(); i++) {
1576 fAnalysisTree->SetBranchStatus(observables[i].c_str(),
true);
1579 Double_t valueToCompareFrom;
1580 bool comparisonResult;
1582 for (
int iNoOffset = 0; iNoOffset < nEntries; iNoOffset++) {
1583 i = (iNoOffset + startingIndex) % nEntries;
1584 fAnalysisTree->GetEntry(i);
1585 comparisonResult =
true;
1586 for (
unsigned int j = 0; j < observables.size(); j++) {
1587 valueToCompareFrom = fAnalysisTree->GetDblObservableValue(observables[j].c_str());
1588 if (operators[j] ==
"=" || operators[j] ==
"==") {
1589 comparisonResult = comparisonResult && (valueToCompareFrom == values[j]);
1590 }
else if (operators[j] ==
"<") {
1591 comparisonResult = comparisonResult && (valueToCompareFrom < values[j]);
1592 }
else if (operators[j] ==
"<=") {
1593 comparisonResult = comparisonResult && (valueToCompareFrom <= values[j]);
1594 }
else if (operators[j] ==
">") {
1595 comparisonResult = comparisonResult && (valueToCompareFrom > values[j]);
1596 }
else if (operators[j] ==
">=") {
1597 comparisonResult = comparisonResult && (valueToCompareFrom >= values[j]);
1601 if (comparisonResult) {
1602 if ((
int)eventIds.size() < max) {
1603 eventIds.push_back(i);
1610 fAnalysisTree->SetBranchStatus(
"*",
true);
1614std::vector<int> TRestRun::GetEventIdsWithConditions(
const string& cuts,
int startingIndex,
int maxNumber) {
1615 int max = maxNumber;
1616 if (max < 0) max = GetEntries();
1618 auto indices = GetEventEntriesWithConditions(cuts, startingIndex, max);
1619 std::vector<int> ids;
1620 for (
unsigned int i = 0; i < indices.size(); i++) {
1622 ids.push_back(fAnalysisTree->GetEventID());
1635 if (fEventIndexCounter >= GetEntries()) {
1636 fEventIndexCounter = 0;
1638 auto indices = GetEventEntriesWithConditions(cuts, fEventIndexCounter++, 1);
1639 if (indices.size() == 0) {
1643 fAnalysisTree->GetEntry(indices[0]);
1644 fEventTree->GetEntry(indices[0]);
1645 fCurrentEvent = indices[0];
1650string TRestRun::GetRunInformation(
const string& info) {
1667 result =
fHostmgr->GetProcessRunner()->GetProcInfo(info);
1676TRestMetadata* TRestRun::GetMetadataClass(
const TString& type, TFile* file) {
1677 if (file !=
nullptr) {
1678 TIter nextkey(file->GetListOfKeys());
1680 while ((key = (TKey*)nextkey())) {
1681 string kName = key->GetClassName();
1687 if (metadata !=
nullptr && metadata->InheritsFrom(
"TRestMetadata")) {
1690 RESTWarning <<
"TRestRun::GetMetadataClass() : The object to import is "
1691 "not inherited from TRestMetadata"
1697 for (
unsigned int i = 0; i < fMetadata.size(); i++)
1698 if (fMetadata[i]->InheritsFrom(type))
return fMetadata[i];
1701 return GetMetadataClass(type, fInputFile);
1708TRestMetadata* TRestRun::GetMetadata(
const TString& name, TFile* file) {
1709 if (file !=
nullptr) {
1710 TIter nextkey(file->GetListOfKeys());
1712 while ((key = (TKey*)nextkey())) {
1713 string kName = key->GetName();
1715 if (kName == name) {
1718 if (metadata->InheritsFrom(
"TRestMetadata")) {
1721 RESTWarning <<
"TRestRun::GetMetadata() : The object to import is not "
1722 "inherited from TRestMetadata"
1728 for (
unsigned int i = 0; i < fMetadata.size(); i++) {
1729 if (fMetadata[i]->GetName() == name) {
1730 return fMetadata[i];
1738std::vector<std::string> TRestRun::GetMetadataNames() {
1739 std::vector<std::string> strings;
1741 for (
int n = 0; n < GetNumberOfMetadata(); n++) strings.push_back(fMetadata[n]->GetName());
1746std::vector<std::string> TRestRun::GetMetadataTitles() {
1747 std::vector<std::string> strings;
1749 for (
int n = 0; n < GetNumberOfMetadata(); n++) strings.push_back(fMetadata[n]->GetTitle());
1761 if (instr.find(
"[", 0) == string::npos)
return instr;
1762 string outstring = instr;
1764 int startPosition = 0;
1765 int endPosition = 0;
1766 while ((startPosition = outstring.find(
"[", endPosition)) != (
int)string::npos) {
1767 endPosition = outstring.find(
"]", startPosition + 1);
1768 string s = outstring.substr(startPosition + 1, endPosition - startPosition - 1);
1769 int cont = count(s.begin(), s.end(),
'[') - count(s.begin(), s.end(),
']');
1771 if (cont < 0) RESTError <<
"This is a coding error at ReplaceMetadataMembers!" <<
RESTendl;
1775 endPosition = outstring.find(
"]", endPosition + 1);
1776 s = outstring.substr(startPosition + 1, endPosition - startPosition - 1);
1777 cont = count(s.begin(), s.end(),
'[') - count(s.begin(), s.end(),
']');
1778 if (endPosition == (
int)string::npos)
break;
1780 if (endPosition == (
int)string::npos)
break;
1782 string expressionToReplace = outstring.substr(startPosition + 1, endPosition - startPosition - 1);
1785 outstring.replace(startPosition, endPosition - startPosition + 1, value);
1789 outstring =
Replace(outstring,
"{{",
"[");
1790 outstring =
Replace(outstring,
"}}",
"]");
1812 if (instr.find(
"::") == string::npos && instr.find(
"->") == string::npos) {
1813 return "{{" + instr +
"}}";
1815 vector<string> results =
Split(instr,
"::",
false,
true);
1816 if (results.size() == 1) results =
Split(instr,
"->",
false,
true);
1818 if (results.size() == 2) {
1820 int pos1 = results[1].find(
"[", 0);
1821 int pos2 = results[1].find(
"]", pos1);
1822 if (pos1 > 0 && pos2 > 0) {
1823 string indexStr = results[1].substr(pos1 + 1, pos2 - pos1 - 1);
1826 if (index < 0) index = 0;
1828 results[1] = results[1].substr(0, pos1);
1831 if (GetMetadata(results[0])) {
1832 if (index >= this->GetMetadata(results[0])->
GetDataMemberValues(results[1], precision).size()) {
1833 RESTWarning <<
"TRestRun::ReplaceMetadataMember. Index out of range!" <<
RESTendl;
1834 RESTWarning <<
"Returning the first element" <<
RESTendl;
1837 return this->GetMetadata(results[0])->GetDataMemberValues(results[1], precision)[index];
1840 if (GetMetadataClass(results[0])) {
1843 RESTWarning <<
"TRestRun::ReplaceMetadataMember. Index out of range!" <<
RESTendl;
1844 RESTWarning <<
"Returning the first element" <<
RESTendl;
1847 return this->GetMetadataClass(results[0])->GetDataMemberValues(results[1], precision)[index];
1851 RESTError <<
"TRestRun::ReplaceMetadataMember. Wrong number of elements found" <<
RESTendl;
1853 RESTWarning <<
"TRestRun::ReplaceMetadataMember. " << instr <<
" not found!" <<
RESTendl;
1877 if (instr ==
"")
return true;
1879 std::vector<string> oper = {
"=",
"==",
"<=",
"<",
">=",
">",
"!="};
1882 std::vector<string> results;
1883 for (
unsigned int n = 0; n < oper.size(); n++) {
1885 if (instr.find(
"->") != string::npos) pos = instr.find(
"->") + 2;
1887 if (instr.find(oper[n], pos) != string::npos) {
1889 results =
Split(instr, oper[n],
false,
true, pos);
1895 RESTWarning <<
"TRestRun::EvaluateMetadataMember. Not valid operator found in expression : " << instr
1900 if (results.size() != 2) {
1901 RESTWarning <<
"TRestRun::EvaluateMetadataMember. Not valid expression : " << instr <<
RESTendl;
1915 if (expOp ==
"=" && lvalue == rvalue)
return true;
1916 if (expOp ==
"==" && lvalue == rvalue)
return true;
1917 if (expOp ==
"<=" && lvalue <= rvalue)
return true;
1918 if (expOp ==
"<" && lvalue < rvalue)
return true;
1919 if (expOp ==
">=" && lvalue >= rvalue)
return true;
1920 if (expOp ==
">" && lvalue > rvalue)
return true;
1921 if (expOp ==
"!=" && lvalue != rvalue)
return true;
1933 RESTMetadata <<
"Version : " << this->
GetVersion() << RESTendl;
1934 RESTMetadata <<
"Parent run number : " << GetParentRunNumber() <<
RESTendl;
1935 RESTMetadata <<
"Run number : " << GetRunNumber() <<
RESTendl;
1936 RESTMetadata <<
"Experiment/project : " << GetExperimentName() <<
RESTendl;
1937 RESTMetadata <<
"Run type : " << GetRunType() <<
RESTendl;
1938 RESTMetadata <<
"Run tag : " << GetRunTag() <<
RESTendl;
1939 RESTMetadata <<
"Run user : " << GetRunUser() <<
RESTendl;
1940 RESTMetadata <<
"Run description : " << GetRunDescription() <<
RESTendl;
1941 RESTMetadata <<
"Start Date/Time : " <<
ToDateTimeString(GetStartTimestamp()) <<
" ("
1942 << GetStartTimestamp() <<
")" <<
RESTendl;
1943 RESTMetadata <<
"End Date/Time : " <<
ToDateTimeString(GetEndTimestamp()) <<
" (" << GetEndTimestamp()
1948 if (fInputFile !=
nullptr) {
1949 RESTMetadata <<
"Data file : " << fInputFile->GetName();
1950 if (fNFilesSplit > 0) {
1951 RESTMetadata <<
" (Splitted into " << fNFilesSplit + 1 <<
" files)" <<
RESTendl;
1956 RESTMetadata <<
"Number of events : " << fEntriesSaved <<
RESTendl;
1961 RESTMetadata <<
"---------------------------------------" <<
RESTendl;
1971 cout <<
"------------------------" << endl;
1972 cout <<
"---- Run start date ----" << endl;
1973 cout <<
"------------------------" << endl;
1974 cout <<
"Unix time : " <<
fStartTime << endl;
1976 struct tm* tm = localtime(&tt);
1979 strftime(date,
sizeof(date),
"%Y-%m-%d", tm);
1980 cout <<
"Date : " << date << endl;
1983 strftime(time,
sizeof(time),
"%H:%M:%S", tm);
1984 cout <<
"Time : " << time << endl;
1985 cout <<
"++++++++++++++++++++++++" << endl;
1988void TRestRun::PrintEndDate() {
1989 cout <<
"----------------------" << endl;
1990 cout <<
"---- Run end date ----" << endl;
1991 cout <<
"----------------------" << endl;
1992 cout <<
"Unix time : " <<
fEndTime << endl;
1994 struct tm* tm = localtime(&tt);
1997 strftime(date,
sizeof(date),
"%Y-%m-%d", tm);
1998 cout <<
"Date : " << date << endl;
2001 strftime(time,
sizeof(time),
"%H:%M:%S", tm);
2002 cout <<
"Time : " << time << endl;
2003 cout <<
"++++++++++++++++++++++++" << endl;
2012 for (
unsigned int n = 0; n < fMetadata.size(); n++)
2013 if (fMetadata[n]->
GetError()) nErrors++;
2017 RESTError <<
"Found a total of " << nErrors <<
" metadata errors" <<
RESTendl;
2018 for (
unsigned int n = 0; n < fMetadata.size(); n++)
2021 RESTError <<
"Class: " << fMetadata[n]->ClassName() <<
" Name: " << fMetadata[n]->GetName()
2023 RESTError <<
"Number of errors: " << fMetadata[n]->GetNumberOfErrors() <<
RESTendl;
2024 RESTError <<
"Message: " << fMetadata[n]->GetErrorMessage() <<
RESTendl;
2028 cout <<
"No errors found!" << endl;
2037 Int_t nWarnings = 0;
2038 for (
unsigned int n = 0; n < fMetadata.size(); n++)
2043 RESTWarning <<
"Found a total of " << nWarnings <<
" metadata warnings" <<
RESTendl;
2044 for (
unsigned int n = 0; n < fMetadata.size(); n++)
2047 RESTWarning <<
"Class: " << fMetadata[n]->ClassName() <<
" Name: " << fMetadata[n]->GetName()
2049 RESTWarning <<
"Number of warnings: " << fMetadata[n]->GetNumberOfWarnings() <<
RESTendl;
2050 RESTWarning <<
"Message: " << fMetadata[n]->GetWarningMessage() <<
RESTendl;
2054 cout <<
"No warnings found!" << endl;
bool IsZombie() const
If this object type wrapper is invalid.
void ParseString(const std::string &str) const
Set the value of the wrapped object from std::string.
REST core data-saving helper based on TTree.
void SetObservable(Int_t id, RESTValue obs)
Set the value of observable whose id is as specified.
virtual std::vector< int > search_run_with_file(std::string filepattern)
search runs according to the file name. return a list of run numbers
virtual int get_lastrun()
get the latest run id in database
virtual int set_run(DBEntry info, bool overwrite=true)
add/update a run, with run info as struct DBEntry. returns the added run id
virtual DBEntry query_run(int runnumber)
virtual std::vector< DBFile > query_run_files(int runnumber)
return all the files of the run
A base class for any REST event process.
virtual Bool_t OpenInputFiles(const std::vector< std::string > &files)
Interface to external file reading, open input file. To be implemented in external processes.
void SetAnalysisTree(TRestAnalysisTree *tree)
Set analysis tree of this process, then add observables to it.
virtual Long64_t GetTotalBytes() const
A base class for any REST event.
virtual void Initialize()=0
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
static bool PrepareBorrowedUpdate(TFile &file, std::string *error=nullptr)
Data provider and manager in REST.
void PrintWarnings()
Prints out all the warnings registered by metadata classes accessible to TRestRun,...
void AddEventBranch(TRestEvent *event)
Add an event branch in output EventTree.
void OpenInputFile(int i)
Open the i th file in the file list.
void CloseFile()
Close both input file and output file, setting trees to nullptr also.
TFile * FormOutputFile()
Create a new TFile as REST output file. Writing metadata objects into it.
void ResetEntry()
Reset file reading progress.
TString FormFormat(const TString &filenameFormat)
Form output file name according to file info list, proc info list and run data.
Int_t fParentRunNumber
It can be used as parent number of subrun number.
void SetInputEvent(TRestEvent *event)
Retarget input event in the tree.
std::string ReplaceMetadataMember(const std::string &instr, Int_t precision=0)
It will replace the data member from the corresponding metadata class type or name defined in the inp...
void GetEntry(Long64_t entry)
Calls GetEntry() for both AnalysisTree and EventTree.
void InitFromConfigFile() override
Initialize logic of TRestRun.
void Initialize() override
REST run class.
void PrintErrors()
Prints out all the warnings registered by metadata classes accessible to TRestRun,...
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 ...
void PrintMetadata() override
Prints the basic run information.
TFile * MergeToOutputFile(std::vector< std::string > fileFullNames, std::string outputFileName="")
Form REST output file by merging a list of files together.
std::vector< std::string > GetEventTypesList()
It returns a list of available event types inside the file.
void ImportMetadata(const TString &rootFile, const TString &name, const TString &type, Bool_t store)
Open the root file and import the metadata of the given name.
Bool_t EvaluateMetadataMember(const std::string &instr)
It will evaluate the expression given including the data member from the corresponding metadata class...
void SetExtProcess(TRestEventProcess *p)
Set external file process.
void PrintStartDate()
Prints the run start date and time in human format.
Double_t fStartTime
Event absolute starting time/date (unix timestamp).
void WriteWithDataBase()
Write this object into TFile and add a new entry in database.
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
overwriting the write() method with fStore considered
void ReadFileInfo(const std::string &filename)
Extract file info from a file, and save it in the file info list.
Double_t fEndTime
Event absolute ending time/date (unix timestamp).
Int_t GetNextEvent(TRestEvent *targetEvent, TRestAnalysisTree *targetTree)
Get next event by writing event data into target event and target tree.
TRestEvent * GetNextEventWithConditions(const std::string &)
Load the next event that satisfies the conditions specified by a string.
TClass * GetClassQuick()
Get the type of a "class" object, returning the wrapped type identifier "TClass".
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.
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.
Double_t StringToDouble(std::string in)
Gets a double from a string.
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.
Int_t Count(std::string s, std::string sbstring)
Counts the number of occurences of substring inside the input string in.
std::string ParameterNameToDataMemberName(std::string name)
Convert parameter name to datamember name, following REST parameter naming convention.
std::string DataMemberNameToParameterName(std::string name)
Convert data member name to parameter name, following REST parameter naming convention.
Int_t isANumber(std::string in)
Returns 1 only if a valid number is found in the string in. If not it returns 0.
std::string RemoveWhiteSpaces(std::string in)
Returns the input string removing all white spaces.
std::string ToDateTimeString(time_t time)
Format time_t into string.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.
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.