REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
TRestRun.cxx
1
9//
27
28#include "TRestRun.h"
29#ifdef WIN32
30#include <io.h>
31#include <process.h>
32#include <windows.h>
33#undef GetClassName
34#else
35#include <sys/stat.h>
36#include <unistd.h>
37#endif // !WIN32
38
39#include <TBranchElement.h>
40#include <TStreamerInfo.h>
41
42#include <filesystem>
43#include <memory>
44
45#include "TRestDataBase.h"
46#include "TRestEventProcess.h"
47#include "TRestManager.h"
48#include "TRestTools.h"
49#include "TRestVersion.h"
50
51using namespace std;
52
53std::mutex mutex_read;
54
55namespace {
56Int_t GetDetectorSignalVersion(TBranch* branch) {
57 Int_t time = -1;
58 Int_t charge = -1;
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();
64 }
65 auto* children = current->GetListOfBranches();
66 for (int index = 0; index <= children->GetLast(); ++index)
67 self(self, static_cast<TBranch*>(children->At(index)));
68 };
69 if (branch != nullptr) visit(visit, branch);
70 return time == charge ? time : -1;
71}
72
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)
79 return false;
80
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)
88 return false;
89 }
90 return true;
91}
92} // namespace
93
94ClassImp(TRestRun);
95
96TRestRun::TRestRun() { Initialize(); }
97
98TRestRun::TRestRun(const string& filename) {
99 if (filename.find(".root") != string::npos) {
100 Initialize();
101 OpenInputFile(filename);
102 } else if (filename.find(".rml") != string::npos) {
103 Initialize();
104 LoadConfigFromFile(filename);
105 } else {
106 RESTError << "TRestRun::TRestRun(): input file error!" << RESTendl;
107 }
108}
109
110TRestRun::~TRestRun() { CloseFile(); }
111
116 SetSectionName(this->ClassName());
117
118 time_t timev;
119 time(&timev);
120 fStartTime = (Double_t)timev;
121 fEndTime = 0; // We need a static value to avoid overwriting the time if another process sets the time
122
123 fRunUser = REST_USER;
124 fRunNumber = 0; // run number where input file is from and where output file
125 // will be saved
126 fParentRunNumber = 0; // subrun number where input file is from
127 fRunType = "Null";
128 fExperimentName = "Null";
129 fRunTag = "Null";
130 fRunDescription = "Null";
131
132 fInputFileName = "null";
133 fOutputFileName = "rest_default.root";
134
135 fBytesRead = 0;
136 fTotalBytes = -1;
137 fOverwrite = true;
138 fEntriesSaved = -1;
139 fNFilesSplit = 0;
140
141 fInputMetadata.clear();
142 fMetadata.clear();
143 fInputFileNames.clear();
144 fInputFile = nullptr;
145 fOutputFile = nullptr;
146 fInputEvent = nullptr;
147 fAnalysisTree = nullptr;
148 fEventTree = nullptr;
149 fCurrentEvent = 0;
150 fEventBranchLoc = -1;
151 fFileProcess = nullptr;
152 fSaveHistoricData = true;
153}
154
167 RESTDebug << "Initializing TRestRun from config file, version: " << REST_RELEASE << RESTendl;
168 fFromRML = true;
169 ReSetVersion();
170
171 // 1. Read basic parameter
172 fRunUser = REST_USER;
173 // fRunType = GetParameter("runType", "ANALYSIS").c_str();
174 // fRunDescription = GetParameter("runDescription", "").c_str();
175 // fExperimentName = GetParameter("experiment", "preserve").c_str();
176 // fRunTag = GetParameter("runTag", "noTag").c_str();
178
179 // 2. Initialize runnumber and input file name. They follow non-trivial logic
180 fRunNumber = -1;
182 string runNstr = GetParameter("runNumber", "-1");
183 string inputName = GetParameter("inputFileName", "");
184 inputName = TRestTools::RemoveMultipleSlash(inputName);
185 string inputNameOld = GetParameter("inputFile", "default");
186 if (inputNameOld != "default") {
187 RESTWarning << "Parameter \"inputFile\" in rml is obsolete! Please update it to \"inputFileName\""
188 << RESTendl;
189 if (inputName.empty()) {
190 inputName = inputNameOld;
191 }
192 }
193 if (ToUpper(runNstr) == "AUTO" && ToUpper(inputName) == "AUTO") {
194 RESTError << "TRestRun: run number and input file name cannot both be "
195 "\"AUTO\""
196 << RESTendl;
197 exit(1);
198 }
199
200 if (ToUpper(inputName) != "AUTO") {
201 fInputFileName = inputName;
202 fInputFileNames = Vector_cast<string, TString>(TRestTools::GetFilesMatchingPattern(inputName));
203 }
204
205 if (ToUpper(runNstr) != "AUTO") {
206 fRunNumber = atoi(runNstr.c_str());
207 }
208
209 if (ToUpper(inputName) == "AUTO") {
210 TRestDataBase* db = gDataBase;
211 auto files = db->query_run_files(fRunNumber);
212 fInputFileName = db->query_run(fRunNumber).value; // run entry value is file pattern
213 fInputFileNames = Vector_cast<DBFile, TString>(files);
214 }
215
216 if (ToUpper(runNstr) == "AUTO") {
217 TRestDataBase* db = gDataBase;
218 auto runs = db->search_run_with_file((string)fInputFileName);
219 if (!runs.empty()) {
220 fRunNumber = runs[0];
221 } else {
222 fRunNumber = -1;
223 }
224
225 // add a new run
226 if (fRunNumber == 0) {
227 fRunNumber = db->get_lastrun() + 1;
228 DBEntry entry;
229 entry.runNr = fRunNumber;
230 entry.description = fRunDescription;
231 entry.tag = fRunTag;
232 entry.type = fRunType;
233 entry.version = REST_RELEASE;
234 db->set_run(entry);
235 }
236 }
237
238 // get some run information
239 if (fRunNumber != -1) {
240 DBEntry entry = gDataBase->query_run(fRunNumber);
241 if (!entry.IsZombie()) {
242 fStartTime = entry.tstart;
243 fEndTime = entry.tend;
244 fRunDescription = entry.description;
245 fRunTag = entry.tag;
246 fRunType = entry.type;
247 }
248 }
249
250 if (fInputFileNames.empty()) {
251 if (fInputFileName != "") {
252 RESTError << "cannot find the input file!" << RESTendl;
253 exit(1);
254 } else {
255 RESTWarning << "no input file added" << RESTendl;
256 }
257 // throw;
258 }
259
260 // 3. Construct output file name
261 string outputDir = (string)GetDataPath();
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\""
266 << RESTendl;
267 if (outputName == "default") {
268 outputName = outputNameOld;
269 }
270 }
271 if (ToUpper(outputName) == "DEFAULT") {
272 string expName = RemoveWhiteSpaces((string)GetExperimentName());
273 string runType = RemoveWhiteSpaces((string)GetRunType());
274 char runParentStr[256];
275 sprintf(runParentStr, "%05d", fParentRunNumber);
276 char runNumberStr[256];
277 sprintf(runNumberStr, "%05d", fRunNumber);
278
279 fOutputFileName = outputDir + "Run_" + expName + "_" + fRunUser + "_" + runType + "_" + fRunTag +
280 "_" + (TString)runNumberStr + "_" + (TString)runParentStr + "_V" + REST_RELEASE +
281 ".root";
282
283 // fOverwrite = ToUpper(GetParameter("overwrite", "on")) != "OFF";
284 while (!fOverwrite && TRestTools::fileExists((string)fOutputFileName)) {
286 sprintf(runParentStr, "%05d", fParentRunNumber);
287 fOutputFileName = outputDir + "Run_" + expName + "_" + fRunUser + "_" + runType + "_" + fRunTag +
288 "_" + (TString)runNumberStr + "_" + (TString)runParentStr + "_V" +
289 REST_RELEASE + ".root";
290 }
291 } else if (ToUpper(outputName) == "NULL" || outputName == "/dev/null") {
292 fOutputFileName = "/dev/null";
293 } else if (TRestTools::isAbsolutePath(outputName)) {
294 fOutputFileName = outputName;
295 outputDir = TRestTools::SeparatePathAndName((string)fOutputFileName).first;
296 } else {
297 fOutputFileName = outputDir + "/" + outputName;
298 }
299 // remove multiple slashes from fOutputFileName
300 fOutputFileName = (TString)TRestTools::RemoveMultipleSlash((string)fOutputFileName);
301
302 if (!TRestTools::fileExists(outputDir)) {
303 int z = system((TString) "mkdir -p " + outputDir);
304 if (z != 0) RESTError << "Problem creating directory : " << outputDir << RESTendl;
305 }
306 if (!TRestTools::isPathWritable(outputDir)) {
307 RESTWarning << "TRestRun: Output path '" << outputDir << "' does not exist or it is not writable."
308 << RESTendl;
309 }
310
311 // 4. Loop over sections to initialize metadata
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);
318 } else {
319 RESTWarning << "Wrong definition of addMetadata! Metadata name or file name "
320 "is not given!"
321 << RESTendl;
322 }
323 } else if (Count(key, "TRest") > 0) {
324 if (e->Attribute("file") != nullptr && TRestTools::isRootFile(e->Attribute("file"))) {
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;
328 }
329
331 if (meta == nullptr) {
332 RESTWarning << "failed to add metadata \"" << key << "\"" << RESTendl;
333 e = e->NextSiblingElement();
334 continue;
335 }
337 meta->SetHostmgr(fHostmgr);
338 fMetadata.push_back(meta);
340 }
341 e = e->NextSiblingElement();
342 }
343
344 // 5. Open input file(s). We open input file at the last stage in case the file name pattern
345 // reading requires TRestDetector
346 OpenInputFile(0);
347 RESTDebug << "TRestRun::EndOfInit. InputFile pattern: \"" << fInputFileName << "\"" << RESTendl;
348 RESTInfo << "which matches :" << RESTendl;
349 for (const auto& inputFileName : fInputFileNames) {
350 RESTInfo << inputFileName << RESTendl;
351 }
352 RESTEssential << "(" << fInputFileNames.size() << " added files)" << RESTendl;
353}
354
359 if (fInputFileNames.size() > (unsigned int)i) {
360 TString Filename = fInputFileNames[i];
361 RESTInfo << "opening... " << Filename << RESTendl;
362 OpenInputFile((string)Filename);
363 }
364}
365
375void TRestRun::OpenInputFile(const TString& filename, const string& mode) {
376 CloseFile();
377 const bool isRemote = TRestTools::IsRemoteRootPath(filename.Data());
378 if (!isRemote && !TRestTools::fileExists((string)filename)) {
379 RESTError << "input file \"" << filename << "\" does not exist!" << RESTendl;
380 exit(1);
381 }
382
383 if (!isRemote) ReadFileInfo((string)filename);
384
385 // add to fInputFileNames in case it is opening a new file
386 bool inList = false;
387 for (const auto& inputFileName : fInputFileNames) {
388 if (inputFileName == filename) {
389 inList = true;
390 break;
391 }
392 }
393
394 if (!inList) {
395 fInputFileNames.push_back(filename);
396 }
397
398 if (TRestTools::isRootFile((string)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;
406 } else {
407 RESTError << "TRestRun::OpenInputFile(): unsupported ROOT mode '" << mode
408 << "'. Supported modes are READ/OPEN and UPDATE." << RESTendl;
409 exit(1);
410 }
411 auto inputFile = TRestRootFileHandle::Open(filename.Data(), fileMode);
412 if (!inputFile) {
413 RESTError << inputFile.Error() << RESTendl;
414 exit(1);
415 }
416 fInputFileOwner = std::move(inputFile);
417 fInputFile = fInputFileOwner.Get();
418
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."
422 << RESTendl;
423 RESTError << "Recover it first with:" << RESTendl;
424 RESTError << " restRoot --recover-legacy-signals " << filename << RESTendl;
425 exit(1);
426 }
427
428 if (GetMetadataClass("TRestRun", fInputFile)) {
429 // This should be the values in RML (if it was initialized using RML)
430 TString runTypeTmp = fRunType;
431 // TString runUserTmp = fRunUser;
432 TString runTagTmp = fRunTag;
433 TString runDescriptionTmp = fRunDescription;
434 TString experimentNameTmp = fExperimentName;
435 TString outputFileNameTmp = fOutputFileName;
436 TString inputFileNameTmp = fInputFileName;
437 TString cFileNameTmp = fConfigFileName;
438
439 // We define fVersion to -1 to identify old REST files that did not have yet
440 // versioning system
441 this->UnSetVersion();
442
443 // Now we load the values in the previous run file
444 // If successfully read the input file, the version code will be changed
445 // from -1 --> certain number
446 this->Read(GetMetadataClass("TRestRun", fInputFile)->GetName());
447
448 if (inputFileNameTmp != "null") fInputFileName = inputFileNameTmp;
449 if (outputFileNameTmp != "rest_default.root") fOutputFileName = outputFileNameTmp;
450 if (cFileNameTmp != "null") fConfigFileName = cFileNameTmp;
451
452 // If the value was initialized from RML and is not preserve, we recover
453 // back the value in RML
454 if (fFromRML) {
455 if (runTypeTmp != "Null" && runTypeTmp != "preserve") fRunType = runTypeTmp;
456
457 // We should not recover the user. Only when writing. If not when I open a file
458 // with restRoot just to read, and Print the run content from other user in my
459 // own account, it will say it was me!
460 // if (runUserTmp != "Null" && runTypeTmp != "preserve") fRunUser = runUserTmp;
461
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;
467 }
468
469 // If version is lower than 2.2.1 we do not read/transfer the metadata to
470 // output file?
471 // Actually g4 files from v2.1.x is all compatible with the v2.2.x version
472 // Only 2.2.0 is without auto schema evolution, whose metadata cannot be read
473 if (fSaveHistoricData) {
474 if (this->GetVersionCode() >= REST_VERSION(2, 2, 1) ||
475 this->GetVersionCode() <= REST_VERSION(2, 1, 8)) {
476 ReadInputFileMetadata();
477 } else {
478 RESTWarning << "-- W : The metadata version found on input file is lower "
479 "than 2.2.1!"
480 << RESTendl;
481 RESTWarning << "-- W : metadata from input file will not be read" << RESTendl;
482 }
483 }
484
485 RESTDebug << "Initializing input file : version code : " << this->GetVersionCode() << RESTendl;
486 RESTDebug << "Input file version : " << this->GetVersion() << RESTendl;
487 ReadInputFileTrees();
488 fCurrentEvent = 0;
489 } else {
490 RESTWarning << "TRestRun object not found in file! The input file may be incomplete!" << RESTendl;
491 ReadInputFileTrees();
492 }
493 } else {
494 fInputFile = nullptr;
495 fAnalysisTree = nullptr;
496 if (fFileProcess != nullptr) {
497 fFileProcess->OpenInputFiles(Vector_cast<TString, string>(fInputFileNames));
498 fFileProcess->InitProcess();
499 }
500 }
501
502 if (fAnalysisTree == nullptr && fFileProcess == nullptr)
503 RESTInfo << "Input file is not REST root file, an external process is needed!" << RESTendl;
504}
505
506void TRestRun::AddInputFileExternal(const string& file) {
507 mutex_read.lock();
508 if (fFileProcess != nullptr) {
509 bool add = fFileProcess->AddInputFile(file);
510 if (!add) {
511 RESTError << "failed to add input file!" << RESTendl;
512 }
513 fInputFileNames.emplace_back(file);
514 }
515 mutex_read.unlock();
516}
517
518void TRestRun::ReadInputFileMetadata() {
519 TFile* f = fInputFile;
520 if (f != nullptr) {
521 fInputMetadata.clear();
522
523 TIter nextkey(f->GetListOfKeys());
524 TKey* key;
525 // we should make sure the input metadata has unique names
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;
530
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()
536 << RESTendl;
537 continue;
538 }
539
540 if (addedNames.count(key->GetName()) != 0) {
541 continue;
542 }
543
544 TRestMetadata* a = (TRestMetadata*)f->Get(key->GetName());
545 RESTDebug << "Key of type : " << a->ClassName() << "(" << a << ")" << RESTendl;
546
547 if (!a) {
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") {
552 /*
553 //we make sure there is no repeated class added
554 // However, we might have two processes with the same class name
555 operating at different steps of the data chain
556 // We just avoid to write TRestRun from previous file to the list of
557 metadata structures
558
559 bool flag = false;
560 for (int i = 0; i < fInputMetadata.size(); i++) {
561 if (a->ClassName() == fInputMetadata[i]->ClassName()) {
562 flag = true;
563 break;
564 }
565 }
566 if (!flag) {
567 fInputMetadata.push_back(a);
568 }
569 */
571 fInputMetadata.push_back(a);
572 fMetadata.push_back(a);
573 addedNames.insert(key->GetName());
574 }
575 }
576 }
577}
578
579void TRestRun::ReadInputFileTrees() {
580 if (fInputFile != nullptr) {
581 RESTDebug << "Finding TRestAnalysisTree.." << RESTendl;
582 TTree* _eventTree = nullptr;
583 string filename = fInputFile->GetName();
584
585 if (fInputFile->Get("AnalysisTree") != nullptr) {
586 fAnalysisTree = (TRestAnalysisTree*)fInputFile->Get("AnalysisTree");
587
588 if (fNFilesSplit > 0) { // fNFilesSplit=1: split to 1 additional file
589 RESTEssential << "Linking analysis tree from split data files" << RESTendl;
590 fAnalysisTree = (TRestAnalysisTree*)fAnalysisTree->Clone(); // we must make a copy to have
591 // TBrowser correctly browsed.
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;
596 }
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;
601 }
602 }
603
604 // Note: we call GetEntries() to initialize total entry number
605 // Otherwise the child analysis tree's observables will be reset
606 // on the next call of GetEntries()
607 fAnalysisTree->GetEntries();
608 // Call GetEntry() to initialize observables and connect branches
609 fAnalysisTree->GetEntry(0);
610
611 _eventTree = (TTree*)fInputFile->Get("EventTree");
612 } else if (fInputFile->FindKey("TRestAnalysisTree") != nullptr) {
613 // This is v2.1.6- version of input file, we directly find EventTree and
614 // AnalysisTree. The old name pattern is "TRestXXXEventTree-eventBranch"
615 // and "TRestAnalysisTree"
616 RESTWarning << "Loading root file from old version REST!" << RESTendl;
617 fAnalysisTree = (TRestAnalysisTree*)fInputFile->Get("TRestAnalysisTree");
618 fAnalysisTree->GetEntry(0);
619
620 TIter nextkey(fInputFile->GetListOfKeys());
621 TKey* key;
622 while ((key = (TKey*)nextkey())) {
623 // cout << key->GetName() << endl;
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());
630 break;
631 }
632 }
633 RESTDebug << "Old REST file successfully recovered!" << RESTendl;
634 } else {
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;
638 }
639
640 if (_eventTree != nullptr) {
641 if (fNFilesSplit > 0) {
642 // eventTree shall be initialized as TChain
643 delete _eventTree;
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;
648
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;
653 }
654 fEventTree = _fEventTree;
655 } else {
656 fEventTree = _eventTree;
657 }
658
659 RESTDebug << "Finding event branch.." << RESTendl;
660 if (fInputEvent == nullptr) {
661 TObjArray* branches = fEventTree->GetListOfBranches();
662 // get the last event branch as input event branch
663 if (branches->GetLast() > -1) {
664 TBranch* br = (TBranch*)branches->At(branches->GetLast());
665
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;
669 } else {
670 string type = Replace(br->GetName(), "Branch", "", 0);
671 TClass* cl = TClass::GetClass(type.c_str());
672 if (cl->HasDictionary()) {
673 fInputEvent = REST_Reflection::Assembly(type);
674 } else if (fInputEvent != nullptr) {
675 delete fInputEvent;
676 fInputEvent = nullptr;
677 }
678
679 if (fInputEvent == nullptr) {
680 RESTError << "TRestRun:OpenInputFile. Cannot initialize input event, event "
681 "tree not read"
682 << RESTendl;
683 RESTError
684 << "Please install corresponding libraries to provide root dictionaries for "
685 "class reading."
686 << RESTendl;
687 return;
688 }
689
690 fInputEvent->InitializeWithMetadata(this);
691 fEventTree->SetBranchAddress(br->GetName(), &fInputEvent);
692 fEventBranchLoc = branches->GetLast();
693 RESTDebug << "found event branch of event type: " << fInputEvent->ClassName()
694 << RESTendl;
695 }
696 }
697 } else {
698 string brname = (string)fInputEvent->ClassName() + "Branch";
699 if (fEventTree->GetBranch(brname.c_str()) == nullptr) {
700 RESTWarning << "REST WARNING (OpenInputFile) : No matched event branch "
701 "inside file : "
702 << filename << RESTendl;
703 RESTWarning << "Branch required: " << brname << RESTendl;
704 } else {
705 fEventTree->SetBranchAddress(brname.c_str(), &fInputEvent);
706 RESTDebug << brname << " is found and set!" << RESTendl;
707 }
708 }
709 } else {
710 RESTDebug << "TRestRun:OpenInputFile. EventTree was not found" << RESTendl;
711 RESTDebug << "This is a pure analysis file!" << RESTendl;
712 fInputEvent = nullptr;
713 }
714 }
715}
716
724void TRestRun::ReadFileInfo(const string& filename) {
725 RESTDebug << "begin collecting basic file info..." << filename << RESTendl;
726
727 // basic file info: modify time, total bytes, etc.
728 struct stat buf;
729 FILE* fp = fopen(filename.c_str(), "rb");
730 if (!fp) {
731 RESTError << "TRestRun::ReadFileInfo. Something went wrong with fopen()!" << strerror(errno)
732 << RESTendl;
733 exit(1);
734 }
735 int fd = fileno(fp);
736 fstat(fd, &buf);
737 fclose(fp);
738 if (fEndTime == 0) {
739 fEndTime = buf.st_mtime;
740 }
741
742 if (TRestTools::isRootFile((string)filename)) {
743 fTotalBytes = buf.st_size;
744 }
745
746 RESTDebug << "begin matching file name pattern for more file info..." << RESTendl;
747 // format example:
748 // run[fRunNumber]_cobo[aaa]_frag[bbb]_Vm[TRestDetector::fAmplificationVoltage].graw
749 // we are going to match it with inputfile:
750 // run00042_cobo1_frag0000_Vm350.graw
751 string format = GetParameter("inputFormat", "");
752 string name = TRestTools::SeparatePathAndName(filename).second;
753
754 vector<string> formatSectionList;
755 vector<string> formatPrefixList;
756
757 int pos = -1;
758 int pos1 = 0;
759 int pos2 = 0;
760 while (true) {
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));
765 break;
766 }
767
768 formatSectionList.push_back(format.substr(pos1 + 1, pos2 - pos1 - 1));
769 formatPrefixList.push_back(format.substr(pos + 1, pos1 - pos - 1));
770
771 pos = pos2;
772 }
773
774 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;
778 return;
779 }
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;
786 return;
787 }
788
789 string infoFromFileName = name.substr(pos1, pos2 - pos1);
790
791 RESTDebug << "File pattern matching. key: " << formatSectionList[i] << " (between the mark \""
792 << formatPrefixList[i] << "\" and \"" << formatPrefixList[i + 1]
793 << "\"), value: " << infoFromFileName << RESTendl;
794
795 // run[fRunNumber]_cobo[aaa]_frag[bbb]_Vm[TRestDetector::fAmplificationVoltage].graw
796 bool inforead = false;
797 if (!inforead) {
798 // 1. store special file pattern parameters as TRestRun data member: fRunNumber
799 if (DataMemberNameToParameterName(formatSectionList[i]) != "") {
800 RESTValue member = RESTValue(this, this->ClassName()).GetDataMember(formatSectionList[i]);
801 if (!member.IsZombie()) {
802 member.ParseString(infoFromFileName);
803 inforead = true;
804 } else {
805 RESTWarning << "TRestRun: file name format field \"" << formatSectionList[i]
806 << "\"(value = " << infoFromFileName
807 << ") not registered, data member does not exist in TRestRun!" << RESTendl;
808 }
809 }
810 }
811
812 if (!inforead) {
813 // 2. store special file pattern parameters as data member of the loaded
814 // metadata class, e.g., TRestDetector::fAmplificationVoltage
815 vector<string> classDataMember = Split(formatSectionList[i], "::");
816 if (classDataMember.size() > 1) {
817 TRestMetadata* meta = GetMetadataClass(classDataMember[0]);
818 if (meta != nullptr) {
819 RESTValue member = RESTValue(meta, meta->ClassName()).GetDataMember(classDataMember[1]);
820 if (!member.IsZombie()) {
821 member.ParseString(infoFromFileName);
822 meta->UpdateMetadataMembers();
823 inforead = true;
824 } else {
825 RESTWarning << "TRestRun: file name format field \"" << formatSectionList[i]
826 << "\"(value = " << infoFromFileName
827 << ") not registered, metadata exist but without such datamember field!"
828 << RESTendl;
829 }
830 } else {
831 RESTWarning << "TRestRun: file name format field \"" << formatSectionList[i]
832 << "\"(value = " << infoFromFileName
833 << ") not registered, metadata does not exist!" << RESTendl;
834 }
835 }
836 }
837
838 // if (!inforead) {
839 // 3. store file format fields to REST_ARGS as global parameter: aaa, bbb
840 // REST_ARGS[formatsectionlist[i]] = infoFromFileName;
841 // }
842
843 pos = pos2 - 1;
844 }
845}
846
854 fCurrentEvent = 0;
855 if (fFileProcess != nullptr) {
856 fFileProcess->ResetEntry();
857 }
858}
859
868Int_t TRestRun::GetNextEvent(TRestEvent* targetEvent, TRestAnalysisTree* targetTree) {
869 bool messageShown = false;
870 TRestEvent* eve = fInputEvent;
871
872 if (fFileProcess != nullptr) {
873 RESTDebug << "TRestRun: getting next event from external process" << RESTendl;
874 GetEventExt:
875 mutex_read.lock();
876 fFileProcess->BeginOfEventProcess();
877 eve = fFileProcess->ProcessEvent(nullptr);
878 fFileProcess->EndOfEventProcess();
879 mutex_read.unlock();
880 fBytesRead = fFileProcess->GetTotalBytesRead();
881 // if (targettree != nullptr) {
882 // for (int n = 0; n < fAnalysisTree->GetNumberOfObservables(); n++)
883 // targettree->SetObservable(n, fAnalysisTree->GetObservable(n));
884 //}
885 fCurrentEvent++;
886 } else {
887 if (eve == nullptr) {
888 RESTDebug << "TRestRun::GetNextEvent(): input event has not been initialized!" << RESTendl;
889 } else {
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"
893 << RESTendl;
894 eve = nullptr;
895 } else {
896 if (fCurrentEvent >= fAnalysisTree->GetTree()->GetEntriesFast()) {
897 eve = nullptr;
898 } else {
899 if (targetTree != nullptr) {
900 // normal reading procedure
901 eve->Initialize();
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));
906 }
907 if (fEventTree != nullptr) {
908 if (fEventTree->IsA() == TChain::Class()) {
909 Long64_t entry = fEventTree->LoadTree(fCurrentEvent);
910 fBytesRead += ((TBranch*)fEventTree->GetTree()->GetListOfBranches()->UncheckedAt(
911 fEventBranchLoc))
912 ->GetEntry(entry);
913 } else {
914 fBytesRead +=
915 ((TBranch*)fEventTree->GetListOfBranches()->UncheckedAt(fEventBranchLoc))
916 ->GetEntry(fCurrentEvent);
917 }
918 }
919 fCurrentEvent++;
920 }
921 }
922 }
923 }
924
925 // cout << fHangUpEndFile << endl;
926
927 if (eve == nullptr) {
928 if (fHangUpEndFile && fFileProcess != nullptr) {
929 // if hangup is set, we continue calling ProcessEvent() of the
930 // external process, until there is non-null event yielded
931 if (!messageShown) {
932 RESTEssential << "external process file reading reaches end, waiting for more files"
933 << RESTendl;
934 }
935 usleep(1000000);
936 messageShown = true;
937 fCurrentEvent--;
938 goto GetEventExt;
939 }
940 fInputEvent = eve;
941 // if (fFileProcess != nullptr) fFileProcess->EndProcess();
942 return -1;
943 }
944
945 fInputEvent = eve;
946
947 if (fInputEvent->GetID() == 0 && fInputEvent->GetSubID() == 0) {
948 fInputEvent->SetID(fCurrentEvent - 1);
949 }
950
951 if (fInputEvent->GetRunOrigin() == 0) {
952 fInputEvent->SetRunOrigin(fRunNumber);
953 }
954
955 targetEvent->Initialize();
956 fInputEvent->CloneTo(targetEvent);
957
958 return 0;
959}
960
964std::vector<std::string> TRestRun::GetEventTypesList() {
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);
972 }
973 }
974 return list;
975}
976
980void TRestRun::GetEntry(Long64_t entry) {
981 if (entry >= GetEntries()) {
982 RESTWarning << "TRestRun::GetEntry. Entry requested out of limits" << RESTendl;
983 RESTWarning << "Total number of entries is : " << GetEntries() << RESTendl;
984 }
985
986 if (fAnalysisTree != nullptr) {
987 fAnalysisTree->GetEntry(entry);
988 }
989 if (fEventTree != nullptr) {
990 fEventTree->GetEntry(entry);
991 }
992
993 if (fInputEvent != nullptr) {
994 fInputEvent->InitializeReferences(this);
995 }
996
997 fCurrentEvent = entry;
998}
999
1012TString TRestRun::FormFormat(const TString& FilenameFormat) {
1013 string inString = (string)FilenameFormat;
1014 string outString = (string)FilenameFormat;
1015
1016 RESTDebug << "TRestRun::FormFormat. In string : " << inString << RESTendl;
1017
1018 int pos = 0;
1019 while (true) {
1020 int pos1 = inString.find("[", pos);
1021 int pos2 = inString.find("]", pos1);
1022 if (pos1 == -1 || pos2 == -1) break;
1023
1024 string targetstr = inString.substr(pos1, pos2 - pos1 + 1); // with []
1025 string target = inString.substr(pos1 + 1, pos2 - pos1 - 1); // without []
1026 string replacestr = GetRunInformation(target);
1027
1028 RESTDebug << "TRestRun::FormFormat. target : " << target << RESTendl;
1029 RESTDebug << "TRestRun::FormFormat. replacestr : " << replacestr << RESTendl;
1030
1031 // If we form an output file we are willing to form the output filename
1032 // using the latest version. But the version is set just before we
1033 // call Write.
1034 if (target == "fVersion") replacestr = (string)REST_RELEASE;
1035 if (target == "fCommit") replacestr = (string)REST_COMMIT;
1036
1037 if (replacestr != target) {
1038 if (target == "fRunNumber" || target == "fParentRunNumber") {
1039 replacestr = Form("%05d", StringToInteger(replacestr));
1040 }
1041 outString = Replace(outString, targetstr, replacestr, 0);
1042 }
1043 pos = pos2 + 1;
1044 }
1045
1046 return outString;
1047}
1048
1058TFile* TRestRun::MergeToOutputFile(vector<string> filenames, string outputfilename) {
1059 RESTDebug << "TRestRun::FormOutputFile. target : " << outputfilename << RESTendl;
1060 const string filename = outputfilename.empty() ? string(fOutputFileName.Data()) : outputfilename;
1061 fOutputFileName = FormFormat(filename);
1062 const string finalFilename = fOutputFileName.Data();
1063 RESTInfo << (outputfilename.empty() ? "Creating file : " : "Updating file : ") << finalFilename
1064 << RESTendl;
1065
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()
1071 << RESTendl;
1072 exit(1);
1073 }
1074 const string existingTarget = targetExists ? filename : "";
1075 std::string mergeError;
1076 if (!TRestTools::MergeRootFilesTransactionally(finalFilename, filenames, existingTarget, true,
1077 &mergeError)) {
1078 fOutputFileName = "";
1079 RESTError << "(Merge files) " << mergeError << RESTendl;
1080 exit(1);
1081 }
1082
1083 if (!outputfilename.empty() && filename != finalFilename && !TRestTools::IsRemoteRootPath(filename)) {
1084 std::error_code removalError;
1085 std::filesystem::remove(filename, removalError);
1086 if (removalError)
1087 RESTWarning << "Merged output is valid, but the pre-format target '" << filename
1088 << "' could not be removed: " << removalError.message() << RESTendl;
1089 }
1090
1091 // write metadata into the output file
1092 auto outputFile = TRestRootFileHandle::Open(finalFilename, TRestRootFileMode::Update);
1093 if (!outputFile) {
1094 RESTError << outputFile.Error() << RESTendl;
1095 fOutputFileName = "";
1096 exit(1);
1097 }
1098 if (fOutputFileOwner && !fOutputFileOwner.Close()) {
1099 RESTError << fOutputFileOwner.Error() << RESTendl;
1100 fOutputFileName = "";
1101 exit(1);
1102 }
1103 fOutputFileOwner = std::move(outputFile);
1104 fOutputFile = fOutputFileOwner.Get();
1105 RESTDebug << "TRestRun::FormOutputFile. Calling WriteWithDataBase()" << RESTendl;
1106 this->WriteWithDataBase();
1107
1108 RESTcout << this->ClassName() << " Created ..." << RESTendl;
1109 RESTcout << "- Path : " << TRestTools::SeparatePathAndName((string)fOutputFileName).first << RESTendl;
1110 RESTcout << "- Filename : " << TRestTools::SeparatePathAndName((string)fOutputFileName).second
1111 << RESTendl;
1112 return fOutputFile;
1113}
1114
1119 CloseFile();
1120
1121 fOutputFileName = FormFormat(fOutputFileName);
1122 // remove unwanted "./" etc. from the path while resolving them
1123 fOutputFileName = std::filesystem::weakly_canonical(fOutputFileName.Data());
1124
1125 auto outputFile = TRestRootFileHandle::Open(fOutputFileName.Data(), TRestRootFileMode::Recreate);
1126 if (!outputFile) {
1127 RESTError << outputFile.Error() << RESTendl;
1128 return nullptr;
1129 }
1130 fOutputFileOwner = std::move(outputFile);
1131 fOutputFile = fOutputFileOwner.Get();
1132 fAnalysisTree = new TRestAnalysisTree("AnalysisTree", "AnalysisTree");
1133 fEventTree = new TTree("EventTree", "EventTree");
1134
1135 fAnalysisTree->Write(nullptr, TObject::kOverwrite);
1136 fEventTree->Write(nullptr, TObject::kOverwrite);
1137 this->WriteWithDataBase();
1138
1139 RESTcout << "TRestRun: Output File Created." << RESTendl;
1140 RESTcout << "- Path : " << TRestTools::SeparatePathAndName((string)fOutputFileName).first << RESTendl;
1141 RESTcout << "- Filename : " << TRestTools::SeparatePathAndName((string)fOutputFileName).second
1142 << RESTendl;
1143 return fOutputFile;
1144}
1145
1146TFile* TRestRun::UpdateOutputFile() {
1147 if (fOutputFile != nullptr) {
1148 if (!fOutputFile->IsOpen()) {
1149 RESTError << "TRestRun::UpdateOutputFile(): output file is closed" << RESTendl;
1150 return nullptr;
1151 }
1152 if (!fOutputFile->IsWritable()) {
1153 std::string updateError;
1154 if (!TRestRootFileHandle::PrepareBorrowedUpdate(*fOutputFile, &updateError)) {
1155 RESTError << updateError << RESTendl;
1156 return nullptr;
1157 }
1158 }
1159
1160 fOutputFile->cd();
1161
1162 fAnalysisTree->Write(nullptr, kOverwrite);
1163 fEventTree->Write(nullptr, kOverwrite);
1164
1165 this->WriteWithDataBase();
1166
1167 RESTcout << "TRestRun: Output File Updated." << RESTendl;
1168 RESTcout << "- Path : " << TRestTools::SeparatePathAndName((string)fOutputFileName).first << RESTendl;
1169 RESTcout << "- Filename : " << TRestTools::SeparatePathAndName((string)fOutputFileName).second
1170 << RESTendl;
1171 return fOutputFile;
1172
1173 } else {
1174 RESTError << "TRestRun::UpdateOutputFile(): output file is closed" << RESTendl;
1175 }
1176 return nullptr;
1177}
1178
1189 TRestAnalysisTree* tree = nullptr;
1190
1191 RESTDebug << "TRestRun::WriteWithDataBase. Getting entries in analysisTree" << RESTendl;
1192
1193 // record the entries saved
1194 fEntriesSaved = -1;
1195 if (fOutputFile != nullptr) {
1196 tree = (TRestAnalysisTree*)fOutputFile->Get("AnalysisTree");
1197 if (tree != nullptr) {
1198 fEntriesSaved = tree->GetEntries();
1199 }
1200 }
1201 RESTDebug << "TRestRun::WriteWithDataBase. Entries found : " << fEntriesSaved << RESTendl;
1202
1203 // If time was not written by any other process we record the current time
1204 if (fEndTime == 0) {
1205 time_t timev;
1206 time(&timev);
1207 fEndTime = (Double_t)timev;
1208 }
1209
1210 fRunUser = REST_USER;
1211
1212 // save metadata objects in file
1213
1214 RESTDebug << "TRestRun::WriteWithDataBase. Calling this->Write(0, kOverwrite)" << RESTendl;
1215 this->Write(nullptr, kOverwrite);
1216
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()
1222 << RESTendl;
1223 for (const auto& inputMetadata : fInputMetadata) {
1224 RESTDebug << metadata->GetName() << " == " << inputMetadata->GetName() << RESTendl;
1225 if (metadata == inputMetadata) {
1226 historic = true;
1227 break;
1228 }
1229 }
1230
1231 if (!historic) {
1232 RESTDebug << "NO historic" << RESTendl;
1233 metadata->Write(metadata->GetName(), kOverwrite);
1234 } else {
1235 RESTDebug << "IS historic" << RESTendl;
1236 if (fSaveHistoricData) metadata->Write(metadata->GetName(), kOverwrite);
1237 }
1238 }
1239
1240 // write to database
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;
1245 }
1246}
1247
1252 fEntriesSaved = -1;
1253 if (fAnalysisTree != nullptr) {
1254 fEntriesSaved = fAnalysisTree->GetEntries();
1255 if (fAnalysisTree->GetEntries() > 0 && fInputFile == nullptr) {
1256 if (fOutputFile != nullptr) {
1257 fOutputFile->cd();
1258 fAnalysisTree->Write(nullptr, kOverwrite);
1259 this->Write(nullptr, kOverwrite);
1260 }
1261 }
1262 delete fAnalysisTree;
1263 fAnalysisTree = nullptr;
1264 }
1265
1266 if (fEventTree != nullptr) {
1267 if (fEventTree->GetEntries() > 0 && fInputFile == nullptr) fEventTree->Write(nullptr, kOverwrite);
1268 delete fEventTree;
1269 fEventTree = nullptr;
1270 }
1271
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);
1277 i--;
1278 fInputMetadata.erase(fInputMetadata.begin() + j);
1279 break;
1280 }
1281 }
1282 }
1283 delete fInputEvent;
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();
1291 delete fOutputFile;
1292 }
1293 fOutputFile = nullptr;
1294 }
1295 if (fInputFile != nullptr) {
1296 if (fInputFileOwner.Get() == fInputFile) {
1297 if (!fInputFileOwner.Close()) RESTError << fInputFileOwner.Error() << RESTendl;
1298 } else {
1299 fInputFile->Close();
1300 }
1301 fInputFile = nullptr;
1302 }
1303 if (fOutputFileOwner && !fOutputFileOwner.Close()) RESTError << fOutputFileOwner.Error() << RESTendl;
1304 if (fInputFileOwner && !fInputFileOwner.Close()) RESTError << fInputFileOwner.Error() << RESTendl;
1305}
1306
1311 if (fFileProcess == nullptr && p != nullptr) {
1312 fFileProcess = p;
1313
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!"
1319 << RESTendl;
1320 exit(1);
1321 } else {
1322 fInputEvent->SetRunOrigin(fRunNumber);
1323 fInputEvent->SetSubRunOrigin(fParentRunNumber);
1324 fInputEvent->SetTimeStamp(fStartTime);
1325 }
1326 if (fInputFileOwner && !fInputFileOwner.Close()) RESTError << fInputFileOwner.Error() << RESTendl;
1327 fInputFile = nullptr;
1328 // we make sure external processes can access to analysis tree
1329 fAnalysisTree = new TRestAnalysisTree("externalProcessAna", "externalProcessAna");
1330 p->SetAnalysisTree(fAnalysisTree);
1331 fTotalBytes = p->GetTotalBytes();
1332
1333 GetNextEvent(fInputEvent, nullptr);
1334 // fAnalysisTree->CreateBranches();
1335 RESTInfo << "The external file process has been set! Name : " << fFileProcess->GetName() << RESTendl;
1336 } else {
1337 if (fFileProcess != nullptr) {
1338 RESTError << "There can only be one file process!" << RESTendl;
1339 exit(1);
1340 }
1341 if (p == nullptr) {
1342 RESTWarning << "Given file process is null, skipping..." << RESTendl;
1343 }
1344 }
1345}
1346
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);
1359 }
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
1366 << RESTendl;
1367 fInputEvent = event;
1368 fEventTree->SetBranchAddress(branchName.c_str(), &fInputEvent);
1369 fEventTree->SetBranchStatus(branchName.c_str(), false);
1370 fEventBranchLoc = i;
1371 break;
1372 } else if (i == branches->GetLast()) {
1373 RESTWarning << "REST Warning : (TRestRun) cannot find corresponding "
1374 "branch in event tree!"
1375 << RESTendl;
1376 RESTWarning << "Event Type : " << event->ClassName() << RESTendl;
1377 RESTWarning << "Input event not set!" << RESTendl;
1378 }
1379 }
1380 } else {
1381 fInputEvent = event;
1382 }
1383 this->GetEntry(fCurrentEvent);
1384 }
1385}
1386
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());
1397 }
1398 }
1399}
1400
1406void TRestRun::ImportMetadata(const TString& File, const TString& name, const TString& type, Bool_t store) {
1407 const TString thisFile = SearchFile(File.Data());
1408 if (thisFile == "") {
1409 RESTError << "(ImportMetadata): The file " << thisFile << " does not exist!" << RESTendl;
1410 RESTError << RESTendl;
1411 return;
1412 }
1413 if (!TRestTools::isRootFile(thisFile.Data())) {
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;
1416 return;
1417 }
1418
1419 TFile* file = TFile::Open(thisFile);
1420 // TODO give error in case we try to obtain a class that is not TRestMetadata
1421 if (type == "" && name == "") {
1422 RESTError << "(ImportMetadata) : metadata type and name is not "
1423 "specified!"
1424 << RESTendl;
1425 return;
1426 }
1427
1428 TRestMetadata* meta = nullptr;
1429 if (name != "") {
1430 meta = GetMetadata(name, file);
1431 } else if (type != "") {
1432 meta = GetMetadataClass(type, file);
1433 }
1434
1435 if (meta == nullptr) {
1436 cout << "REST ERROR (ImportMetadata) : " << name << " does not exist." << endl;
1437 cout << "Inside root file : " << File << endl;
1438 GetChar();
1439 file->Close();
1440 delete file;
1441 return;
1442 }
1443
1444 if (store)
1445 meta->Store();
1446 else
1447 meta->DoNotStore();
1448
1449 fMetadata.push_back(meta);
1450 meta->LoadConfigFromBuffer();
1451 file->Close();
1452 delete file;
1453}
1454
1455Int_t TRestRun::Write(const char* name, Int_t option, Int_t bufsize) {
1456 ReSetVersion();
1457 return TRestMetadata::Write(name, option, bufsize);
1458}
1459
1460Double_t TRestRun::GetRunLength() const {
1461 if (fEndTime - fStartTime == -1) {
1462 cout << "Run time is not set\n";
1463 }
1464 return fEndTime - fStartTime;
1465}
1466
1467Long64_t TRestRun::GetTotalBytes() {
1468 if (fFileProcess != nullptr) {
1469 fTotalBytes = fFileProcess->GetTotalBytes();
1470 }
1471 return fTotalBytes;
1472}
1473
1474Long64_t TRestRun::GetEntries() const {
1475 if (fAnalysisTree != nullptr) {
1476 return fAnalysisTree->GetEntries();
1477 }
1478 return fEntriesSaved;
1479 // return REST_MAXIMUM_EVENTS;
1480}
1481
1482// Getters
1483TRestEvent* TRestRun::GetEventWithID(Int_t eventID, Int_t subEventID, const TString& tag) {
1484 if (fAnalysisTree != nullptr) {
1485 int nEntries = fAnalysisTree->GetEntries();
1486
1487 // set analysis tree to read only three branches
1488 fAnalysisTree->SetBranchStatus("*", false);
1489 fAnalysisTree->SetBranchStatus("eventID", true);
1490 fAnalysisTree->SetBranchStatus("subEventID", true);
1491 fAnalysisTree->SetBranchStatus("subEventTag", true);
1492
1493 // just look through the whole analysis tree and find the entry
1494 // this is not good!
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);
1503 fCurrentEvent = i;
1504 return fInputEvent;
1505 }
1506 }
1507 // reset the branch status
1508 fAnalysisTree->SetBranchStatus("*", true);
1509 }
1510 return nullptr;
1511}
1512
1513std::vector<int> TRestRun::GetEventEntriesWithConditions(const string& cuts, int startingIndex,
1514 int maxNumber) {
1515 int max = maxNumber;
1516 if (max < 0) max = GetEntries();
1517
1518 std::vector<int> eventIds;
1519 // parsing cuts
1520 std::vector<string> observables;
1521 std::vector<string> operators;
1522 std::vector<Double_t> values;
1523 // it is necessary that this vector is sorted from longest to shortest
1524 const std::vector<string> validOperators = {"==", "<=", ">=", "=", ">", "<"};
1525
1526 vector<string> cutsVector = Split(cuts, "&&", false, true);
1527
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)));
1536 break;
1537 }
1538 }
1539 }
1540
1541 // check if observable name corresponds to a valid observable on the tree
1542 if (fAnalysisTree == nullptr) {
1543 return eventIds;
1544 }
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());
1550 }
1551 // verify all observables in cuts are branch names
1552 for (unsigned int i = 0; i < observables.size(); i++) {
1553 // verify operators
1554 if (std::find(validOperators.begin(), validOperators.end(), operators[i]) == validOperators.end()) {
1555 // invalid operation
1556 cout << "invalid operation '" << operators[i] << "' for 'TRestRun::GetEventIdsWithConditions'"
1557 << endl;
1558 return eventIds;
1559 }
1560 // verify observables
1561 if (branchNames.count(observables[i]) == 0) {
1562 // invalid observable name
1563 cout << "invalid observable '" << observables[i] << "' for 'TRestRun::GetEventIdsWithConditions'"
1564 << endl;
1565 cout << "valid branch names: ";
1566 for (auto branchName : branchNames) {
1567 cout << branchName << " ";
1568 }
1569 cout << endl;
1570 return eventIds;
1571 }
1572 }
1573 // read only the necessary branches
1574 fAnalysisTree->SetBranchStatus("*", false);
1575 for (unsigned int i = 0; i < observables.size(); i++) {
1576 fAnalysisTree->SetBranchStatus(observables[i].c_str(), true);
1577 }
1578 // comparison code
1579 Double_t valueToCompareFrom;
1580 bool comparisonResult;
1581 int i;
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]);
1598 }
1599 }
1600
1601 if (comparisonResult) {
1602 if ((int)eventIds.size() < max) {
1603 eventIds.push_back(i);
1604 } else {
1605 break;
1606 }
1607 }
1608 }
1609 // reset branch status
1610 fAnalysisTree->SetBranchStatus("*", true);
1611 return eventIds;
1612}
1613
1614std::vector<int> TRestRun::GetEventIdsWithConditions(const string& cuts, int startingIndex, int maxNumber) {
1615 int max = maxNumber;
1616 if (max < 0) max = GetEntries();
1617
1618 auto indices = GetEventEntriesWithConditions(cuts, startingIndex, max);
1619 std::vector<int> ids;
1620 for (unsigned int i = 0; i < indices.size(); i++) {
1621 GetEntry(indices[i]);
1622 ids.push_back(fAnalysisTree->GetEventID());
1623 }
1624 return ids;
1625}
1626
1634 // we retrieve only one index starting from position set by the counter and increase by one
1635 if (fEventIndexCounter >= GetEntries()) {
1636 fEventIndexCounter = 0;
1637 }
1638 auto indices = GetEventEntriesWithConditions(cuts, fEventIndexCounter++, 1);
1639 if (indices.size() == 0) {
1640 // no events found
1641 return nullptr;
1642 } else {
1643 fAnalysisTree->GetEntry(indices[0]);
1644 fEventTree->GetEntry(indices[0]);
1645 fCurrentEvent = indices[0];
1646 return fInputEvent;
1647 }
1648}
1649
1650string TRestRun::GetRunInformation(const string& info) {
1651 string result = GetParameter(info, "");
1652 if (result != "") {
1653 return result;
1654 }
1655
1656 result = GetDataMemberValue(info);
1657 if (result != "") {
1658 return result;
1659 }
1660
1662 if (result != "") {
1663 return result;
1664 }
1665
1666 if (fHostmgr && fHostmgr->GetProcessRunner() != nullptr) {
1667 result = fHostmgr->GetProcessRunner()->GetProcInfo(info);
1668 if (result != "") {
1669 return result;
1670 }
1671 }
1672
1673 return info;
1674}
1675
1676TRestMetadata* TRestRun::GetMetadataClass(const TString& type, TFile* file) {
1677 if (file != nullptr) {
1678 TIter nextkey(file->GetListOfKeys());
1679 TKey* key;
1680 while ((key = (TKey*)nextkey())) {
1681 string kName = key->GetClassName();
1682
1683 if (REST_Reflection::GetClassQuick(kName.c_str()) != nullptr &&
1684 REST_Reflection::GetClassQuick(kName.c_str())->InheritsFrom(type)) {
1685 TRestMetadata* metadata = file->Get<TRestMetadata>(key->GetName());
1686
1687 if (metadata != nullptr && metadata->InheritsFrom("TRestMetadata")) {
1688 return metadata;
1689 } else {
1690 RESTWarning << "TRestRun::GetMetadataClass() : The object to import is "
1691 "not inherited from TRestMetadata"
1692 << RESTendl;
1693 }
1694 }
1695 }
1696 } else {
1697 for (unsigned int i = 0; i < fMetadata.size(); i++)
1698 if (fMetadata[i]->InheritsFrom(type)) return fMetadata[i];
1699
1700 if (fInputFile != nullptr && this->GetVersionCode() >= TRestTools::ConvertVersionCode("2.2.1")) {
1701 return GetMetadataClass(type, fInputFile);
1702 }
1703 }
1704
1705 return nullptr;
1706}
1707
1708TRestMetadata* TRestRun::GetMetadata(const TString& name, TFile* file) {
1709 if (file != nullptr) {
1710 TIter nextkey(file->GetListOfKeys());
1711 TKey* key;
1712 while ((key = (TKey*)nextkey())) {
1713 string kName = key->GetName();
1714
1715 if (kName == name) {
1716 TRestMetadata* metadata = file->Get<TRestMetadata>(name);
1717
1718 if (metadata->InheritsFrom("TRestMetadata")) {
1719 return metadata;
1720 } else {
1721 RESTWarning << "TRestRun::GetMetadata() : The object to import is not "
1722 "inherited from TRestMetadata"
1723 << RESTendl;
1724 }
1725 }
1726 }
1727 } else {
1728 for (unsigned int i = 0; i < fMetadata.size(); i++) {
1729 if (fMetadata[i]->GetName() == name) {
1730 return fMetadata[i];
1731 }
1732 }
1733 }
1734
1735 return nullptr;
1736}
1737
1738std::vector<std::string> TRestRun::GetMetadataNames() {
1739 std::vector<std::string> strings;
1740
1741 for (int n = 0; n < GetNumberOfMetadata(); n++) strings.push_back(fMetadata[n]->GetName());
1742
1743 return strings;
1744}
1745
1746std::vector<std::string> TRestRun::GetMetadataTitles() {
1747 std::vector<std::string> strings;
1748
1749 for (int n = 0; n < GetNumberOfMetadata(); n++) strings.push_back(fMetadata[n]->GetTitle());
1750
1751 return strings;
1752}
1753
1760string TRestRun::ReplaceMetadataMembers(const string& instr, Int_t precision) {
1761 if (instr.find("[", 0) == string::npos) return instr;
1762 string outstring = instr;
1763
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(), ']');
1770
1771 if (cont < 0) RESTError << "This is a coding error at ReplaceMetadataMembers!" << RESTendl;
1772
1773 // We search for the enclosing ']'. Since we might find a vector index inside.
1774 while (cont > 0) {
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;
1779 }
1780 if (endPosition == (int)string::npos) break;
1781
1782 string expressionToReplace = outstring.substr(startPosition + 1, endPosition - startPosition - 1);
1783 string value = ReplaceMetadataMember(expressionToReplace);
1784
1785 outstring.replace(startPosition, endPosition - startPosition + 1, value);
1786 endPosition = 0;
1787 }
1788
1789 outstring = Replace(outstring, "{{", "[");
1790 outstring = Replace(outstring, "}}", "]");
1791
1792 return REST_StringHelper::ReplaceMathematicalExpressions(outstring, precision);
1793}
1794
1798//
1811string TRestRun::ReplaceMetadataMember(const string& instr, Int_t precision) {
1812 if (instr.find("::") == string::npos && instr.find("->") == string::npos) {
1813 return "{{" + instr + "}}";
1814 }
1815 vector<string> results = Split(instr, "::", false, true);
1816 if (results.size() == 1) results = Split(instr, "->", false, true);
1817
1818 if (results.size() == 2) {
1819 size_t index = 0;
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); // without []
1824
1825 index = StringToInteger(indexStr);
1826 if (index < 0) index = 0;
1827
1828 results[1] = results[1].substr(0, pos1);
1829 }
1830
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;
1835 index = 0;
1836 }
1837 return this->GetMetadata(results[0])->GetDataMemberValues(results[1], precision)[index];
1838 }
1839
1840 if (GetMetadataClass(results[0])) {
1841 if (index >=
1842 this->GetMetadataClass(results[0])->GetDataMemberValues(results[1], precision).size()) {
1843 RESTWarning << "TRestRun::ReplaceMetadataMember. Index out of range!" << RESTendl;
1844 RESTWarning << "Returning the first element" << RESTendl;
1845 index = 0;
1846 }
1847 return this->GetMetadataClass(results[0])->GetDataMemberValues(results[1], precision)[index];
1848 }
1849
1850 } else
1851 RESTError << "TRestRun::ReplaceMetadataMember. Wrong number of elements found" << RESTendl;
1852
1853 RESTWarning << "TRestRun::ReplaceMetadataMember. " << instr << " not found!" << RESTendl;
1854 return "";
1855}
1856
1860//
1876Bool_t TRestRun::EvaluateMetadataMember(const string& instr) {
1877 if (instr == "") return true;
1878
1879 std::vector<string> oper = {"=", "==", "<=", "<", ">=", ">", "!="};
1880
1881 string expOp = "";
1882 std::vector<string> results;
1883 for (unsigned int n = 0; n < oper.size(); n++) {
1884 size_t pos = 0;
1885 if (instr.find("->") != string::npos) pos = instr.find("->") + 2;
1886
1887 if (instr.find(oper[n], pos) != string::npos) {
1888 expOp = oper[n];
1889 results = Split(instr, oper[n], false, true, pos);
1890 break;
1891 }
1892 }
1893
1894 if (expOp == "") {
1895 RESTWarning << "TRestRun::EvaluateMetadataMember. Not valid operator found in expression : " << instr
1896 << RESTendl;
1897 return false;
1898 }
1899
1900 if (results.size() != 2) {
1901 RESTWarning << "TRestRun::EvaluateMetadataMember. Not valid expression : " << instr << RESTendl;
1902 return false;
1903 }
1904
1905 if (!isANumber(results[1])) {
1906 if (ReplaceMetadataMember(results[0]).find(results[1]) != string::npos)
1907 return true;
1908 else
1909 return false;
1910 }
1911
1912 Double_t lvalue = StringToDouble(ReplaceMetadataMember(results[0]));
1913 Double_t rvalue = StringToDouble(results[1]);
1914
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;
1922
1923 return false;
1924}
1925
1930 // cout.precision(10);
1932
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()
1944 << ")" << RESTendl;
1945 RESTMetadata << "Input file : " << TRestTools::GetPureFileName((string)GetInputFileNamePattern())
1946 << RESTendl;
1947 RESTMetadata << "Output file : " << TRestTools::GetPureFileName((string)GetOutputFileName()) << RESTendl;
1948 if (fInputFile != nullptr) {
1949 RESTMetadata << "Data file : " << fInputFile->GetName();
1950 if (fNFilesSplit > 0) {
1951 RESTMetadata << " (Splitted into " << fNFilesSplit + 1 << " files)" << RESTendl;
1952 } else {
1953 RESTMetadata << RESTendl;
1954 }
1955 }
1956 RESTMetadata << "Number of events : " << fEntriesSaved << RESTendl;
1957 // metadata << "Input filename : " << fInputFilename << endl;
1958 // metadata << "Output filename : " << fOutputFilename << endl;
1959 // metadata << "Number of initial events : " << GetNumberOfEvents() << endl;
1960 // metadata << "Number of processed events : " << fProcessedEvents << endl;
1961 RESTMetadata << "---------------------------------------" << RESTendl;
1962 RESTMetadata << RESTendl;
1963}
1964
1969 cout.precision(10);
1970
1971 cout << "------------------------" << endl;
1972 cout << "---- Run start date ----" << endl;
1973 cout << "------------------------" << endl;
1974 cout << "Unix time : " << fStartTime << endl;
1975 time_t tt = (time_t)fStartTime;
1976 struct tm* tm = localtime(&tt);
1977
1978 char date[20];
1979 strftime(date, sizeof(date), "%Y-%m-%d", tm);
1980 cout << "Date : " << date << endl;
1981
1982 char time[20];
1983 strftime(time, sizeof(time), "%H:%M:%S", tm);
1984 cout << "Time : " << time << endl;
1985 cout << "++++++++++++++++++++++++" << endl;
1986}
1987
1988void TRestRun::PrintEndDate() {
1989 cout << "----------------------" << endl;
1990 cout << "---- Run end date ----" << endl;
1991 cout << "----------------------" << endl;
1992 cout << "Unix time : " << fEndTime << endl;
1993 time_t tt = (time_t)fEndTime;
1994 struct tm* tm = localtime(&tt);
1995
1996 char date[20];
1997 strftime(date, sizeof(date), "%Y-%m-%d", tm);
1998 cout << "Date : " << date << endl;
1999
2000 char time[20];
2001 strftime(time, sizeof(time), "%H:%M:%S", tm);
2002 cout << "Time : " << time << endl;
2003 cout << "++++++++++++++++++++++++" << endl;
2004}
2005
2011 Int_t nErrors = 0;
2012 for (unsigned int n = 0; n < fMetadata.size(); n++)
2013 if (fMetadata[n]->GetError()) nErrors++;
2014
2015 if (nErrors) {
2016 cout << endl;
2017 RESTError << "Found a total of " << nErrors << " metadata errors" << RESTendl;
2018 for (unsigned int n = 0; n < fMetadata.size(); n++)
2019 if (fMetadata[n]->GetError()) {
2020 cout << endl;
2021 RESTError << "Class: " << fMetadata[n]->ClassName() << " Name: " << fMetadata[n]->GetName()
2022 << RESTendl;
2023 RESTError << "Number of errors: " << fMetadata[n]->GetNumberOfErrors() << RESTendl;
2024 RESTError << "Message: " << fMetadata[n]->GetErrorMessage() << RESTendl;
2025 }
2026 cout << endl;
2027 } else {
2028 cout << "No errors found!" << endl;
2029 }
2030}
2031
2037 Int_t nWarnings = 0;
2038 for (unsigned int n = 0; n < fMetadata.size(); n++)
2039 if (fMetadata[n]->GetWarning()) nWarnings++;
2040
2041 if (nWarnings) {
2042 cout << endl;
2043 RESTWarning << "Found a total of " << nWarnings << " metadata warnings" << RESTendl;
2044 for (unsigned int n = 0; n < fMetadata.size(); n++)
2045 if (fMetadata[n]->GetWarning()) {
2046 cout << endl;
2047 RESTWarning << "Class: " << fMetadata[n]->ClassName() << " Name: " << fMetadata[n]->GetName()
2048 << RESTendl;
2049 RESTWarning << "Number of warnings: " << fMetadata[n]->GetNumberOfWarnings() << RESTendl;
2050 RESTWarning << "Message: " << fMetadata[n]->GetWarningMessage() << RESTendl;
2051 }
2052 cout << endl;
2053 } else {
2054 cout << "No warnings found!" << endl;
2055 }
2056}
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.
Definition TRestEvent.h:38
virtual void Initialize()=0
A base class for any REST metadata class.
void UnSetVersion()
Resets the version of TRestRun to -1, in case the file is old REST file. Only TRestRun is allowed to ...
virtual void PrintMetadata()
Implemented it in the derived metadata class to print out specific metadata information.
endl_t RESTendl
Termination flag object for TRestStringOutput.
void DoNotStore()
If this method is called the metadata information will not be stored in disk.
Int_t LoadConfigFromFile(const std::string &configFilename, const std::string &sectionName="")
Give the file name, find out the corresponding section. Then call the main starter.
TiXmlElement * fElementGlobal
Saving the global element, to be passed to the resident class, if necessary.
void SetHostmgr(TRestManager *m)
Set the host manager for this class.
Int_t LoadConfigFromBuffer()
Initialize data from a string element buffer.
TString GetDataPath()
Returns a std::string with the path used for data storage.
Bool_t GetError() const
It returns true if an error was identified by a derived metadata class.
Int_t LoadConfigFromElement(TiXmlElement *eSectional, TiXmlElement *eGlobal, std::map< std::string, std::string > envs={})
Main starter method.
void ReSetVersion()
Resets the version of TRestRun to REST_RELEASE. Only TRestRun is allowed to update version.
void SetConfigFile(std::string configFilename)
set config file path from external
void ReadAllParameters()
Reflection methods, Set value of a datamember in class according to TRestMetadata::fElement.
void SetSectionName(std::string sName)
set the section name, clear the section content
TString GetVersion()
Returns the REST version stored in fVersion.
std::string SearchFile(std::string filename)
Search files in current directory and directories specified in "searchPath" section.
std::vector< std::string > GetDataMemberValues(std::string memberName, Int_t precision=0)
Get the value of datamember as a vector of strings.
TRestManager * fHostmgr
All metadata classes can be initialized and managed by TRestManager.
std::string GetDataMemberValue(std::string memberName)
Get the value of data member as string.
std::string fConfigFileName
Full name of the rml file.
void Store()
If this method is called the metadata information will be stored in disk.
virtual void UpdateMetadataMembers()
Method to allow implementation of specific metadata members updates at inherited classes.
TRestMetadata()
TRestMetadata default constructor.
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
overwriting the write() method with fStore considered
Bool_t GetWarning() const
It returns true if an error was identified by a derived metadata class.
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.
TiXmlElement * fElement
Saving the sectional element together with global element.
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
static bool PrepareBorrowedUpdate(TFile &file, std::string *error=nullptr)
Data provider and manager in REST.
Definition TRestRun.h:18
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.
Definition TRestRun.cxx:358
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.
Definition TRestRun.cxx:853
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.
Definition TRestRun.h:23
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.
Definition TRestRun.cxx:980
void InitFromConfigFile() override
Initialize logic of TRestRun.
Definition TRestRun.cxx:166
void Initialize() override
REST run class.
Definition TRestRun.cxx:115
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.
Definition TRestRun.cxx:964
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).
Definition TRestRun.h:36
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.
Definition TRestRun.cxx:724
Double_t fEndTime
Event absolute ending time/date (unix timestamp).
Definition TRestRun.h:37
Int_t GetNextEvent(TRestEvent *targetEvent, TRestAnalysisTree *targetTree)
Get next event by writing event data into target event and target tree.
Definition TRestRun.cxx:868
TRestEvent * GetNextEventWithConditions(const std::string &)
Load the next event that satisfies the conditions specified by a string.
static std::pair< std::string, std::string > SeparatePathAndName(const std::string &fullname)
Separate path and filename in a full path+filename string, returns a pair of string.
static bool IsRemoteRootPath(const std::string &filename)
Returns true when ROOT resolves filename to a non-local protocol.
static std::string GetPureFileName(const std::string &fullPathFileName)
Removes all directories in the full path filename description given in the argument.
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
static std::vector< std::string > GetFilesMatchingPattern(std::string pattern, bool unlimited=false)
Returns a list of files whose name match the pattern string. Key word is "*". e.g....
static int ConvertVersionCode(std::string in)
Convert version to a unique string.
static bool isPathWritable(const std::string &path)
Returns true if the path given by argument is writable.
static bool isAbsolutePath(const std::string &path)
Check if the path is absolute path or not.
static std::string RemoveMultipleSlash(std::string)
Returns the input string but without multiple slashes ("/").
static bool isRootFile(const std::string &filename)
Returns true if the filename has .root extension.
static bool MergeRootFilesTransactionally(const std::string &outputFile, const std::vector< std::string > &inputFiles, const std::string &existingTarget="", bool removeInputsOnSuccess=true, std::string *error=nullptr)
Merge REST worker outputs using a validated candidate and rollback backup.
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.