REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestTools.cxx
1/******************** REST disclaimer ***********************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5 * For more information see http://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see http://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
44#include "TRestTools.h"
45
46#include <TArrayC.h>
47#include <TClass.h>
48#include <TClassEdit.h>
49#include <TDirectory.h>
50#include <TFile.h>
51#include <TFileMerger.h>
52#include <TKey.h>
53#include <TList.h>
54#include <TObjString.h>
55#include <TROOT.h>
56#include <TSchemaRule.h>
57#include <TSchemaRuleSet.h>
58#include <TStreamerInfo.h>
59#include <TSystem.h>
60#include <TTree.h>
61#include <TUUID.h>
62#include <TUrl.h>
63
64#include <regex>
65
66#ifdef USE_Curl
67#include <curl/curl.h>
68#endif
69
70#ifdef WIN32
71#include <io.h>
72#include <windows.h>
73#else
74#include <sys/stat.h>
75
76#include "unistd.h"
77#endif // !WIN32
78
79#ifdef __APPLE__
80#include <array>
81#endif
82
83#include <atomic>
84#include <cerrno>
85#include <chrono>
86#include <cstring>
87#include <filesystem>
88#include <iostream>
89#include <limits>
90#include <memory>
91#include <optional>
92#include <stdexcept>
93#include <thread>
94#include <tuple>
95
96#include "TRestStringHelper.h"
97#include "TRestStringOutput.h"
98
99using namespace std;
100
101namespace {
102#ifdef REST_TESTING_ENABLED
103std::atomic_bool gForceNextRootUpdatePreflightFailure{false};
104std::atomic_bool gForceNextMergeValidationFailure{false};
105#endif
106
107struct StreamerInfoIdentity {
108 std::string name;
109 Int_t version;
110 UInt_t checksum;
111
112 bool operator<(const StreamerInfoIdentity& other) const {
113 return std::tie(name, version, checksum) < std::tie(other.name, other.version, other.checksum);
114 }
115};
116
117struct RootSchemaSnapshot {
118 std::set<StreamerInfoIdentity> streamerInfos;
119 std::set<std::string> schemaRules;
120 std::set<Int_t> numbers;
121};
122
125bool IsImplementationSchema(const std::string& name) {
126 return TClassEdit::IsStdClass(name.c_str()) || TClassEdit::IsStdPairBase(name.c_str());
127}
128
129struct RootContentEntry {
130 std::string className;
131 std::optional<Long64_t> treeEntries;
132};
133
134using RootContentManifest = std::map<std::string, RootContentEntry>;
135
136struct LocalFileIdentity {
137 std::uintmax_t size;
138 std::filesystem::file_time_type modificationTime;
139#ifndef WIN32
140 std::uintmax_t device;
141 std::uintmax_t inode;
142#endif
143
144 bool operator==(const LocalFileIdentity& other) const {
145 return size == other.size && modificationTime == other.modificationTime
146#ifndef WIN32
147 && device == other.device && inode == other.inode
148#endif
149 ;
150 }
151};
152
153std::optional<std::filesystem::path> LocalRootPath(const std::string& filename) {
154 TUrl url(filename.c_str(), kTRUE);
155 if (!url.IsValid() || std::string(url.GetProtocol()) != "file") return std::nullopt;
156 TString expanded(url.GetFile());
157 if (gSystem->ExpandPathName(expanded)) return std::nullopt;
158 return std::filesystem::path(expanded.Data());
159}
160
164std::optional<LocalFileIdentity> CaptureLocalFileIdentity(const std::filesystem::path& path,
165 std::string& error) {
166 std::error_code ec;
167 LocalFileIdentity identity;
168 identity.size = std::filesystem::file_size(path, ec);
169 if (ec) {
170 error = "Cannot inspect size of " + path.string() + ": " + ec.message();
171 return std::nullopt;
172 }
173 identity.modificationTime = std::filesystem::last_write_time(path, ec);
174 if (ec) {
175 error = "Cannot inspect modification time of " + path.string() + ": " + ec.message();
176 return std::nullopt;
177 }
178#ifndef WIN32
179 struct stat status {};
180 if (::stat(path.c_str(), &status) != 0) {
181 error = "Cannot inspect filesystem identity of " + path.string() + ": " + std::strerror(errno);
182 return std::nullopt;
183 }
184 identity.device = static_cast<std::uintmax_t>(status.st_dev);
185 identity.inode = static_cast<std::uintmax_t>(status.st_ino);
186#endif
187 return identity;
188}
189
192std::string NormalizeSchemaRule(const std::string& rule) {
193 ROOT::TSchemaRule parsed;
194 if (!parsed.SetFromRule(rule.c_str())) return "";
195 TString normalized;
196 parsed.AsString(normalized);
197 return normalized.Data();
198}
199
210bool ReadSchemaSnapshot(TFile& file, RootSchemaSnapshot& snapshot, std::string& error, bool resolve = false) {
211 if (!file.IsOpen() || file.IsZombie()) {
212 error = "Cannot inspect StreamerInfos in a closed or invalid ROOT file";
213 return false;
214 }
215 if (file.GetSeekInfo() == 0) return true;
216
217 std::unique_ptr<TList> infos(file.GetStreamerInfoList());
218 if (infos == nullptr) {
219 error = "ROOT could not read the existing StreamerInfo record";
220 return false;
221 }
222 infos->SetOwner(kTRUE);
223
224 TIter next(infos.get());
225 while (TObject* object = next()) {
226 if (auto* info = dynamic_cast<TStreamerInfo*>(object)) {
227 snapshot.streamerInfos.insert({info->GetName(), info->GetClassVersion(), info->GetCheckSum()});
228 continue;
229 }
230
231 auto* rules = dynamic_cast<TList*>(object);
232 if (rules == nullptr || std::string(rules->GetName()) != "listOfRules") continue;
233 TIter nextRule(rules);
234 while (TObject* ruleObject = nextRule()) {
235 auto* rule = dynamic_cast<TObjString*>(ruleObject);
236 if (rule == nullptr) continue;
237 const std::string normalized = NormalizeSchemaRule(rule->GetString().Data());
238 if (normalized.empty()) {
239 error = "The on-disk StreamerInfo contains an invalid schema rule";
240 return false;
241 }
242 snapshot.schemaRules.insert(normalized);
243 }
244 }
245 if (!resolve) return true;
246
247 // BuildCheck transfers surviving descriptors into ROOT's global registry.
248 // Aliases and normalized implementation descriptors instead get kCanDelete,
249 // exactly as in TFile::ReadStreamerInfo. Inventory before this mutation.
250 infos->SetOwner(kFALSE);
251 bool resolved = true;
252 next.Reset();
253 while (TObject* object = next()) {
254 auto* info = dynamic_cast<TStreamerInfo*>(object);
255 if (info == nullptr) {
256 object->SetBit(TObject::kCanDelete);
257 continue;
258 }
259 const StreamerInfoIdentity identity{info->GetName(), info->GetClassVersion(), info->GetCheckSum()};
260 info->BuildCheck(&file);
261 if (info->GetNumber() > 0) {
262 snapshot.numbers.insert(info->GetNumber());
263 const auto* registered =
264 dynamic_cast<TStreamerInfo*>(gROOT->GetListOfStreamerInfo()->At(info->GetNumber()));
265 if (!IsImplementationSchema(identity.name) &&
266 (!registered || identity.name != registered->GetName() ||
267 // Streamer writes the absolute value of ROOT's internal version
268 // (for example ROOT::TIOFeatures uses a negative version).
269 identity.version != std::abs(registered->GetClassVersion()) ||
270 identity.checksum != registered->GetCheckSum())) {
271 error = "Conflicting StreamerInfo for " + identity.name + " version " +
272 std::to_string(identity.version) + " checksum " + std::to_string(identity.checksum);
273 if (registered)
274 error += "; resolved to version " + std::to_string(registered->GetClassVersion()) +
275 " checksum " + std::to_string(registered->GetCheckSum());
276 error +=
277 ". Cannot safely preserve this schema; update or merge refused before "
278 "writing. Input files are unchanged. Check class versioning and schema-evolution "
279 "rules; an explicit migration may be needed.";
280 resolved = false;
281 }
282 } else if (!info->TestBit(TObject::kCanDelete)) {
283 error = "Cannot preserve StreamerInfo " + std::string(info->GetName()) + " version " +
284 std::to_string(info->GetClassVersion()) + " checksum " +
285 std::to_string(info->GetCheckSum());
286 resolved = false;
287 }
288 }
289 infos->Clear();
290 return resolved;
291}
292
297bool IsSubset(const RootSchemaSnapshot& expected, const RootSchemaSnapshot& actual, std::string& error) {
298 for (const auto& info : expected.streamerInfos) {
299 if (actual.streamerInfos.count(info) == 0) {
300 // ROOT deliberately normalizes or omits implementation metadata for
301 // standard-library types when it rewrites StreamerInfo. These are
302 // not user schemas; exact historical identities remain mandatory
303 // for every other class.
304 if (IsImplementationSchema(info.name)) continue;
305 error = "Merged file lost StreamerInfo " + info.name + " version " +
306 std::to_string(info.version) + " checksum " + std::to_string(info.checksum);
307 return false;
308 }
309 }
310 for (const auto& rule : expected.schemaRules) {
311 if (actual.schemaRules.count(rule) == 0) {
312 error = "Merged file lost an on-disk schema-evolution rule";
313 return false;
314 }
315 }
316 return true;
317}
318
323bool ReadContentManifest(TDirectory& directory, const std::string& prefix, RootContentManifest& manifest,
324 std::string& error) {
325 TDirectory::TContext directoryContext(&directory);
326 std::map<std::string, TKey*> newestKeys;
327 TIter nextKey(directory.GetListOfKeys());
328 while (auto* key = dynamic_cast<TKey*>(nextKey())) {
329 auto existing = newestKeys.find(key->GetName());
330 if (existing == newestKeys.end() || key->GetCycle() > existing->second->GetCycle())
331 newestKeys[key->GetName()] = key;
332 }
333
334 for (const auto& [name, key] : newestKeys) {
335 const std::string path = prefix.empty() ? name : prefix + "/" + name;
336 RootContentEntry entry{key->GetClassName(), std::nullopt};
337 TClass* keyClass = TClass::GetClass(key->GetClassName(), kFALSE);
338 const bool isDirectory = keyClass != nullptr && keyClass->InheritsFrom(TDirectory::Class());
339 const bool isTree = keyClass != nullptr && keyClass->InheritsFrom(TTree::Class());
340
341 if (isDirectory || isTree) {
342 std::unique_ptr<TObject> object(key->ReadObj());
343 if (object == nullptr) {
344 error =
345 "Cannot read " + std::string(isDirectory ? "directory" : "tree") + " key '" + path + "'";
346 return false;
347 }
348 if (isDirectory) {
349 auto* child = dynamic_cast<TDirectory*>(object.get());
350 if (child == nullptr) {
351 error = "Key '" + path + "' claims directory class '" + entry.className +
352 "' but ROOT read a different object";
353 return false;
354 }
355 manifest[path] = entry;
356 if (!ReadContentManifest(*child, path, manifest, error)) return false;
357 continue;
358 }
359
360 auto* tree = dynamic_cast<TTree*>(object.get());
361 if (tree == nullptr) {
362 error = "Key '" + path + "' claims tree class '" + entry.className +
363 "' but ROOT read a different object";
364 return false;
365 }
366 entry.treeEntries = tree->GetEntries();
367 }
368 manifest[path] = entry;
369 }
370 return true;
371}
372
377bool AddContentManifest(const RootContentManifest& source, RootContentManifest& expected,
378 std::string& error) {
379 for (const auto& [path, sourceEntry] : source) {
380 const auto existing = expected.find(path);
381 if (existing == expected.end()) {
382 expected[path] = sourceEntry;
383 continue;
384 }
385 if (existing->second.className != sourceEntry.className) {
386 error = "ROOT merge input key '" + path + "' has incompatible classes '" +
387 existing->second.className + "' and '" + sourceEntry.className + "'";
388 return false;
389 }
390 if (existing->second.treeEntries.has_value() != sourceEntry.treeEntries.has_value()) {
391 error = "ROOT merge input key '" + path + "' is inconsistently identified as a TTree";
392 return false;
393 }
394 if (sourceEntry.treeEntries.has_value()) {
395 if (*sourceEntry.treeEntries >
396 std::numeric_limits<Long64_t>::max() - *existing->second.treeEntries) {
397 error = "Merged TTree entry count overflows for key '" + path + "'";
398 return false;
399 }
400 *existing->second.treeEntries += *sourceEntry.treeEntries;
401 }
402 }
403 return true;
404}
405
409bool ValidateContentManifest(const RootContentManifest& expected, const RootContentManifest& actual,
410 std::string& error) {
411 for (const auto& [path, expectedEntry] : expected) {
412 const auto found = actual.find(path);
413 if (found == actual.end()) {
414 error = "Merged file lost key '" + path + "'";
415 return false;
416 }
417 if (found->second.className != expectedEntry.className) {
418 error = "Merged key '" + path + "' changed class from '" + expectedEntry.className + "' to '" +
419 found->second.className + "'";
420 return false;
421 }
422 if (expectedEntry.treeEntries.has_value()) {
423 if (!found->second.treeEntries.has_value()) {
424 error = "Merged key '" + path + "' is no longer readable as a TTree";
425 return false;
426 }
427 if (*found->second.treeEntries != *expectedEntry.treeEntries) {
428 error = "Merged TTree '" + path + "' has " + std::to_string(*found->second.treeEntries) +
429 " entries; expected " + std::to_string(*expectedEntry.treeEntries);
430 return false;
431 }
432 }
433 }
434 return true;
435}
436
443bool LoadOnDiskSchemaRules(const RootSchemaSnapshot& snapshot, std::string& error) {
444 for (const auto& text : snapshot.schemaRules) {
445 auto rule = std::make_unique<ROOT::TSchemaRule>();
446 if (!rule->SetFromRule(text.c_str())) {
447 error = "Cannot parse an on-disk schema-evolution rule";
448 return false;
449 }
450 const std::string target = rule->GetTargetClass();
451 TClass* cl = TClass::GetClass(target.c_str(), kFALSE);
452 if (!cl) {
453 error = "Cannot register schema rules for unloaded class " + target;
454 return false;
455 }
456 auto registered = [&]() {
457 if (!cl->GetSchemaRules()) return false;
458 TIter next(cl->GetSchemaRules()->GetRules());
459 while (auto* candidate = dynamic_cast<ROOT::TSchemaRule*>(next())) {
460 TString serialized;
461 candidate->AsString(serialized);
462 if (text == serialized.Data()) return true;
463 }
464 return false;
465 };
466 if (registered()) continue;
467 bool added;
468 if (cl->GetState() <= TClass::kEmulated) {
469 auto* rules = cl->GetSchemaRules(kTRUE);
470 added = rules && rules->AddRule(rule.get(), ROOT::Detail::TSchemaRuleSet::kNoCheck);
471 if (added) rule.release();
472 } else {
473 added = TClass::AddRule(text.c_str());
474 }
475 if (!added || !registered()) {
476 error = "Cannot preserve an on-disk schema rule for class " + target;
477 return false;
478 }
479 }
480 return true;
481}
482
489bool ApplySchemaUpdate(TFile& file, const std::set<Int_t>& numbers, std::string& error) {
490 TArrayC* classIndex = file.GetClassIndex();
491 if (classIndex == nullptr) {
492 error = "ROOT file has no StreamerInfo class index";
493 return false;
494 }
495 if (!numbers.empty() && *numbers.rbegin() >= classIndex->GetSize())
496 classIndex->Set(*numbers.rbegin() + 1);
497 for (const Int_t number : numbers) classIndex->fArray[number] = 1;
498 classIndex->fArray[0] = 1;
499 return true;
500}
501
502// Internal operations throw to unwind file owners before cleaning up a failed
503// transaction. The public API translates failures back to bool plus Error().
504void Require(bool success, const std::string& error) {
505 if (!success) throw std::runtime_error(error);
506}
507
508std::filesystem::path UniqueSiblingPath(const std::filesystem::path& target, const std::string& label) {
509 for (int attempt = 0; attempt < 20; ++attempt) {
510 TString uuid = TUUID().AsString();
511 uuid.ReplaceAll("-", "");
512 auto candidate =
513 target.parent_path() / ("." + target.filename().string() + ".rest-" + label + "-" + uuid.Data());
514 if (!std::filesystem::exists(candidate)) return candidate;
515 }
516 return {};
517}
518
523bool ReplaceLocalFile(const std::filesystem::path& temporary, const std::filesystem::path& target,
524 const std::filesystem::path& backup, std::string& error) {
525 const bool targetExists = std::filesystem::exists(target);
526#ifdef WIN32
527 if (targetExists) {
528 if (!ReplaceFileW(target.wstring().c_str(), temporary.wstring().c_str(), backup.wstring().c_str(),
529 REPLACEFILE_WRITE_THROUGH, nullptr, nullptr)) {
530 error = "Windows ReplaceFile failed with error " + std::to_string(GetLastError());
531 return false;
532 }
533 } else if (!MoveFileExW(temporary.wstring().c_str(), target.wstring().c_str(), MOVEFILE_WRITE_THROUGH)) {
534 error = "Windows MoveFileEx failed with error " + std::to_string(GetLastError());
535 return false;
536 }
537#else
538 if (targetExists) {
539 std::error_code ec;
540 std::filesystem::create_hard_link(target, backup, ec);
541 if (ec) {
542 ec.clear();
543 std::filesystem::copy_file(target, backup, std::filesystem::copy_options::none, ec);
544 if (ec) {
545 error = "Cannot create rollback backup " + backup.string() + ": " + ec.message();
546 return false;
547 }
548 }
549 }
550 if (::rename(temporary.c_str(), target.c_str()) != 0) {
551 error = "Atomic rename to " + target.string() + " failed: " + std::strerror(errno);
552 if (targetExists) {
553 std::error_code cleanupError;
554 std::filesystem::remove(backup, cleanupError);
555 if (cleanupError)
556 error += ". REST also could not remove rollback backup '" + backup.string() +
557 "': " + cleanupError.message();
558 }
559 return false;
560 }
561#endif
562 return true;
563}
564
568bool RollBackLocalFile(const std::filesystem::path& target, const std::filesystem::path& backup,
569 bool targetPreviouslyExisted, std::string& error) {
570 if (!targetPreviouslyExisted) {
571 std::error_code ec;
572 std::filesystem::remove(target, ec);
573 if (ec) error = "Cannot remove invalid replacement " + target.string() + ": " + ec.message();
574 return !ec;
575 }
576#ifdef WIN32
577 if (!ReplaceFileW(target.wstring().c_str(), backup.wstring().c_str(), nullptr, REPLACEFILE_WRITE_THROUGH,
578 nullptr, nullptr)) {
579 error = "Cannot roll back " + target.string() + "; Windows error " + std::to_string(GetLastError());
580 return false;
581 }
582#else
583 if (::rename(backup.c_str(), target.c_str()) != 0) {
584 error = "Cannot roll back " + target.string() + ": " + std::strerror(errno);
585 return false;
586 }
587#endif
588 return true;
589}
590} // namespace
591
597TRestRootFileHandle TRestRootFileHandle::Open(const std::string& filename, TRestRootFileMode mode) {
598 TRestRootFileHandle result;
599 if (mode != TRestRootFileMode::Read && TRestTools::IsRemoteRootPath(filename)) {
600 result.fError = "REST refuses to mutate remote ROOT destination '" + filename +
601 "'. Choose an explicit local output file.";
602 return result;
603 }
604
605 const char* option = mode == TRestRootFileMode::Recreate ? "RECREATE" : "READ";
606 bool createForUpdate = false;
607 if (mode == TRestRootFileMode::Update) {
608 std::error_code ec;
609 const auto path = LocalRootPath(filename);
610 if (!path) {
611 result.fError = "Cannot resolve local UPDATE destination '" + filename + "'";
612 return result;
613 }
614 createForUpdate = !std::filesystem::exists(*path, ec);
615 if (ec) {
616 result.fError = "Cannot inspect ROOT destination '" + filename + "': " + ec.message();
617 return result;
618 }
619 // ROOT UPDATE creates missing files. CREATE preserves that contract
620 // without overwriting a file appearing between this check and Open.
621 if (createForUpdate) option = "CREATE";
622 }
623 result.fFile.reset(TFile::Open(filename.c_str(), option));
624 if (result.fFile == nullptr || !result.fFile->IsOpen() || result.fFile->IsZombie()) {
625 result.fError =
626 "Cannot open ROOT file '" + filename +
627 (mode == TRestRootFileMode::Update && !createForUpdate ? "' for read-only UPDATE preflight"
628 : "' in mode " + std::string(option));
629 result.fFile.reset();
630 return result;
631 }
632 if ((mode == TRestRootFileMode::Recreate || createForUpdate) && !result.fFile->IsWritable()) {
633 result.fError = "ROOT file '" + filename + "' is not writable";
634 result.fFile.reset();
635 return result;
636 }
637
638 if (mode == TRestRootFileMode::Update && !createForUpdate &&
639 !PrepareBorrowedUpdate(*result.fFile, &result.fError)) {
640 result.fFile.reset();
641 }
642 return result;
643}
644
645TRestRootFileHandle::TRestRootFileHandle(TRestRootFileHandle&& other) noexcept
646 : fFile(std::move(other.fFile)), fError(std::move(other.fError)) {}
647
648TRestRootFileHandle& TRestRootFileHandle::operator=(TRestRootFileHandle&& other) noexcept {
649 if (this == &other) return *this;
650 Close();
651 fFile = std::move(other.fFile);
652 fError = std::move(other.fError);
653 return *this;
654}
655
656TRestRootFileHandle::~TRestRootFileHandle() { Close(); }
657
663 if (fFile == nullptr) return fError.empty();
664 if (fFile->IsOpen()) fFile->Close();
665 const bool success = !fFile->IsZombie() && !fFile->TestBit(TFile::kWriteError);
666 if (!success && fError.empty())
667 fError = "ROOT reported a write error while closing " + std::string(fFile->GetName());
668 fFile.reset();
669 return success;
670}
671
678bool TRestRootFileHandle::PrepareBorrowedUpdate(TFile& file, std::string* error) {
679 bool transitionAttempted = false;
680 try {
681 Require(file.IsOpen() && !file.IsZombie() && !file.IsWritable(),
682 "UPDATE preparation requires a valid TFile that is currently open in READ mode");
683 const std::string filename = file.GetName();
684 const auto path = LocalRootPath(filename);
685 Require(path.has_value(), "REST refuses to mutate remote ROOT destination '" + filename +
686 "'. Choose an explicit local output file.");
687 std::string detail;
688 const auto before = CaptureLocalFileIdentity(*path, detail);
689 Require(before.has_value(), detail);
690 RootSchemaSnapshot schema;
691 Require(ReadSchemaSnapshot(file, schema, detail, true) && LoadOnDiskSchemaRules(schema, detail),
692 detail);
693#ifdef REST_TESTING_ENABLED
694 Require(!gForceNextRootUpdatePreflightFailure.exchange(false),
695 "Forced writable ROOT preflight failure for testing");
696#endif
697 transitionAttempted = true;
698 Require(file.ReOpen("UPDATE") == 0 && file.IsWritable(),
699 "ROOT could not transition '" + filename + "' from validated READ to UPDATE");
700 const auto after = CaptureLocalFileIdentity(*path, detail);
701 Require(after.has_value(), detail);
702 Require(*before == *after,
703 "ROOT file '" + filename + "' changed during the READ-to-UPDATE transition");
704 Require(ApplySchemaUpdate(file, schema.numbers, detail), detail);
705 if (error) error->clear();
706 return true;
707 } catch (const std::exception& failure) {
708 if (transitionAttempted) file.SetWritable(kFALSE);
709 if (error) *error = failure.what();
710 return false;
711 }
712}
713
720std::vector<string> TRestTools::GetOptions(string optionsStr) { return Split(optionsStr, ":"); }
721
730 const set<string> libraryExtension{".so", ".dylib", ".dll"};
731 const set<string> excludedLibraries{
732 "RestG4"}; // Ignoring package libraries if they exist. TODO: do not hardcode this
733
734 vector<string> ldPaths;
735#ifdef WIN32
736 ldPaths.push_back(REST_PATH + "/bin/");
737#else
738 // On Linux and macOS the REST libraries are installed at lib/ (see MacroRootDict.cmake)
739 ldPaths.push_back(REST_PATH + "/lib/");
740#endif // WIN32
741 ldPaths.push_back(REST_USER_PATH + "/userlib/");
742
743 vector<string> fileList;
744 for (const std::filesystem::path path : ldPaths) {
745 if (!exists(path)) {
746 // RESTWarning << "Directory " << string(path) << " for library loading not exist" << RESTendl;
747 continue;
748 }
749 for (const auto& it : std::filesystem::directory_iterator(path)) {
750 if (!it.is_regular_file()) {
751 continue;
752 }
753 if (libraryExtension.count(it.path().extension().string()) == 0) {
754 // needs correct extension e.g. ".so"
755 continue;
756 }
757 const TString pathRootString = it.path().string();
758 TString libName = TRestTools::SeparatePathAndName((std::string)pathRootString).second;
759 if (!libName.Contains("Rest", TString::kExact)) {
760 // e.g. "libRestFramework.so"
761 continue;
762 }
763 // Check if library is excluded from loading e.g. is from a package
764 bool excluded = false;
765 for (const TString excludedLibrary : excludedLibraries) {
766 if (libName.Contains(excludedLibrary, TString::kExact)) {
767 excluded = true;
768 // RESTWarning << "Library '" << pathRootString << "' excluded from loading" << RESTendl;
769 break;
770 }
771 }
772 if (excluded) {
773 continue;
774 }
775 fileList.emplace_back(it.path().string());
776 }
777 }
778
779 // load the found REST libraries
780 if (!silent) cout << "= Loading libraries ..." << endl;
781 for (const auto& library : fileList) {
782 if (!silent) cout << " - " << library << endl;
783 gSystem->Load(library.c_str());
784 }
785 if (!silent) cout << endl;
786}
787
795template <typename T>
796int TRestTools::PrintTable(std::vector<std::vector<T>> data, Int_t start, Int_t end) {
797 Int_t size = data.size();
798 if (end > 0 && size > end) size = end;
799 for (int n = start; n < size; n++) {
800 for (unsigned int m = 0; m < data[n].size(); m++) cout << data[n][m] << "\t";
801 cout << endl;
802 }
803 return 0;
804}
805
806template int TRestTools::PrintTable<Int_t>(std::vector<std::vector<Int_t>> data, Int_t start, Int_t end);
807template int TRestTools::PrintTable<Float_t>(std::vector<std::vector<Float_t>> data, Int_t start, Int_t end);
808template int TRestTools::PrintTable<Double_t>(std::vector<std::vector<Double_t>> data, Int_t start,
809 Int_t end);
810template int TRestTools::PrintTable<std::string>(std::vector<std::vector<std::string>> data, Int_t start,
811 Int_t end);
812
817template <typename T>
818int TRestTools::ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data) {
819 ofstream file(fname);
820 if (!file.is_open()) {
821 RESTError << "Unable to open file for writing : " << fname << RESTendl;
822 return 1;
823 }
824
825 for (unsigned int n = 0; n < data.size(); n++)
826 for (unsigned int m = 0; m < data[n].size(); m++) {
827 file << data[n][m];
828 if (m + 1 < data[n].size()) file << "\t";
829 if (m + 1 == data[n].size()) file << "\n";
830 }
831 file.close();
832
833 return 0;
834}
835
836template int TRestTools::ExportASCIITable<Int_t>(std::string fname, std::vector<std::vector<Int_t>>& data);
837template int TRestTools::ExportASCIITable<Float_t>(std::string fname,
838 std::vector<std::vector<Float_t>>& data);
839template int TRestTools::ExportASCIITable<Double_t>(std::string fname,
840 std::vector<std::vector<Double_t>>& data);
841
846template <typename T>
847int TRestTools::ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data) {
848 ofstream file(fname, ios::out | ios::binary);
849 if (!file.is_open()) {
850 RESTError << "Unable to open file for writing : " << fname << RESTendl;
851 return 1;
852 }
853
854 for (unsigned int n = 0; n < data.size(); n++)
855 for (unsigned int m = 0; m < data[n].size(); m++) {
856 file.write((char*)&data[n][m], sizeof(T));
857 }
858 file.close();
859
860 return 0;
861}
862
863template int TRestTools::ExportBinaryTable<Int_t>(std::string fname, std::vector<std::vector<Int_t>>& data);
864template int TRestTools::ExportBinaryTable<Float_t>(std::string fname,
865 std::vector<std::vector<Float_t>>& data);
866template int TRestTools::ExportBinaryTable<Double_t>(std::string fname,
867 std::vector<std::vector<Double_t>>& data);
868
885template <typename T>
886int TRestTools::ReadBinaryTable(string fName, std::vector<std::vector<T>>& data, Int_t columns) {
887 if (!TRestTools::isValidFile((string)fName)) {
888 RESTError << "TRestTools::ReadBinaryTable. Error." << RESTendl;
889 RESTError << "Cannot open file : " << fName << RESTendl;
890 return 0;
891 }
892
893 if (columns == -1) {
894 columns = GetBinaryFileColumns(fName);
895 if (columns == -1) {
896 RESTError << "TRestTools::ReadBinaryTable. Format extension error." << RESTendl;
897 RESTError << "Please, specify the number of columns at the method 3rd argument" << RESTendl;
898 return 0;
899 }
900 }
901
902 std::ifstream fin(fName, std::ios::binary);
903 fin.seekg(0, std::ios::end);
904 const size_t num_elements = fin.tellg() / sizeof(T);
905 fin.seekg(0, std::ios::beg);
906
907 if (num_elements % columns != 0) {
908 cout << "TRestTools::ReadBinaryTable. Error." << endl;
909 cout << "Number of elements : " << num_elements
910 << " is not compatible with the number of columns : " << columns << endl;
911 fin.close();
912 return 0;
913 }
914
915 std::vector<T> dataArray(columns);
916 fin.read(reinterpret_cast<char*>(&dataArray[0]), columns * sizeof(T));
917 while (fin.good()) {
918 data.push_back(dataArray);
919 fin.read(reinterpret_cast<char*>(&dataArray[0]), columns * sizeof(T));
920 }
921 return 1;
922}
923
924template int TRestTools::ReadBinaryTable<Int_t>(string fName, std::vector<std::vector<Int_t>>& data,
925 Int_t columns);
926template int TRestTools::ReadBinaryTable<Float_t>(string fName, std::vector<std::vector<Float_t>>& data,
927 Int_t columns);
928template int TRestTools::ReadBinaryTable<Double_t>(string fName, std::vector<std::vector<Double_t>>& data,
929 Int_t columns);
930
936Bool_t TRestTools::IsBinaryFile(string fname) {
937 if (GetBinaryFileColumns(fname) > 0) return true;
938 return false;
939}
940
949 string extension = GetFileNameExtension(fname);
950 if (extension.find("N") != 0) {
951 return -1;
952 }
953
954 size_t pos = extension.find("i");
955 if (pos != string::npos) {
956 return StringToInteger(extension.substr(1, pos - 1));
957 }
958
959 pos = extension.find("f");
960 if (pos != string::npos) {
961 return StringToInteger(extension.substr(1, pos - 1));
962 }
963
964 pos = extension.find("d");
965 if (pos != string::npos) {
966 return StringToInteger(extension.substr(1, pos - 1));
967 }
968
969 RESTError << "Format " << ToUpper(extension) << " not recognized" << RESTendl;
970 return -1;
971}
972
977template <typename T>
978void TRestTools::TransposeTable(std::vector<std::vector<T>>& data) {
979 if (data.size() == 0) return;
980
981 std::vector<std::vector<T>> trans_vec(data[0].size(), std::vector<T>());
982
983 for (unsigned int i = 0; i < data.size(); i++)
984 for (unsigned int j = 0; j < data[i].size(); j++) trans_vec[j].push_back(data[i][j]);
985
986 data = trans_vec;
987}
988
989template void TRestTools::TransposeTable<Double_t>(std::vector<std::vector<Double_t>>& data);
990
991template void TRestTools::TransposeTable<Float_t>(std::vector<std::vector<Float_t>>& data);
992
993template void TRestTools::TransposeTable<Int_t>(std::vector<std::vector<Int_t>>& data);
994
1002template <typename T>
1003T TRestTools::GetMaxValueFromTable(const std::vector<std::vector<T>>& data, Int_t column) {
1004 if (data.size() == 0) return 0;
1005 if (column > -1 && data[0].size() <= (unsigned int)column) return 0;
1006
1007 T maxValue = data[0][0];
1008 if (column == -1) {
1009 for (unsigned int n = 0; n < data.size(); n++)
1010 for (unsigned int c = 0; c < data[n].size(); c++)
1011 if (maxValue < data[n][c]) maxValue = data[n][c];
1012 } else {
1013 maxValue = data[0][column];
1014 for (unsigned int n = 0; n < data.size(); n++)
1015 if (maxValue < data[n][column]) maxValue = data[n][column];
1016 }
1017
1018 return maxValue;
1019}
1020
1021template Int_t TRestTools::GetMaxValueFromTable<Int_t>(const std::vector<std::vector<Int_t>>& data,
1022 Int_t column);
1023
1024template Float_t TRestTools::GetMaxValueFromTable<Float_t>(const std::vector<std::vector<Float_t>>& data,
1025 Int_t column);
1026
1027template Double_t TRestTools::GetMaxValueFromTable<Double_t>(const std::vector<std::vector<Double_t>>& data,
1028 Int_t column);
1029
1037template <typename T>
1038T TRestTools::GetMinValueFromTable(const std::vector<std::vector<T>>& data, Int_t column) {
1039 if (data.empty()) return 0;
1040 if (column != -1 && data[0].size() <= (unsigned int)column) return 0;
1041
1042 T minValue = data[0][0];
1043 if (column == -1) {
1044 for (unsigned int n = 0; n < data.size(); n++)
1045 for (unsigned int c = 0; c < data[n].size(); c++)
1046 if (minValue > data[n][c]) minValue = data[n][c];
1047 } else {
1048 minValue = data[0][column];
1049 for (unsigned int n = 0; n < data.size(); n++)
1050 if (minValue > data[n][column]) minValue = data[n][column];
1051 }
1052
1053 return minValue;
1054}
1055
1056template Int_t TRestTools::GetMinValueFromTable<Int_t>(const std::vector<std::vector<Int_t>>& data,
1057 Int_t column);
1058
1059template Float_t TRestTools::GetMinValueFromTable<Float_t>(const std::vector<std::vector<Float_t>>& data,
1060 Int_t column);
1061
1062template Double_t TRestTools::GetMinValueFromTable<Double_t>(const std::vector<std::vector<Double_t>>& data,
1063 Int_t column);
1064
1074template <typename T>
1075T TRestTools::GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column) {
1076 if (data.size() == 0 || data[0].size() <= (unsigned int)column) return 0;
1077 T lowestIncrease = abs(data[0][column] - data[1][column]);
1078 for (unsigned int n = 1; n < data.size(); n++) {
1079 T value = abs(data[n - 1][column] - data[n][column]);
1080 if (lowestIncrease == 0) lowestIncrease = value;
1081 if (value > 0 && value < lowestIncrease) lowestIncrease = value;
1082 }
1083 return lowestIncrease;
1084}
1085
1086template Int_t TRestTools::GetLowestIncreaseFromTable<Int_t>(std::vector<std::vector<Int_t>> data,
1087 Int_t column);
1088
1089template Float_t TRestTools::GetLowestIncreaseFromTable<Float_t>(std::vector<std::vector<Float_t>> data,
1090 Int_t column);
1091
1092template Double_t TRestTools::GetLowestIncreaseFromTable<Double_t>(std::vector<std::vector<Double_t>> data,
1093 Int_t column);
1094
1104template <typename T>
1105T TRestTools::GetIntegralFromTable(const std::vector<std::vector<T>>& data) {
1106 if (data.size() == 0) return 0;
1107 T sum = 0;
1108 for (unsigned int n = 0; n < data.size(); n++) {
1109 for (unsigned int m = 0; m < data[n].size(); m++) sum += data[n][m];
1110 }
1111 return sum;
1112}
1113
1114template Int_t TRestTools::GetIntegralFromTable<Int_t>(const std::vector<std::vector<Int_t>>& data);
1115
1116template Float_t TRestTools::GetIntegralFromTable<Float_t>(const std::vector<std::vector<Float_t>>& data);
1117
1118template Double_t TRestTools::GetIntegralFromTable<Double_t>(const std::vector<std::vector<Double_t>>& data);
1119
1126template <typename T>
1127std::vector<T> TRestTools::GetColumnFromTable(const std::vector<std::vector<T>>& data, unsigned int column) {
1128 std::vector<T> columnData;
1129 if (data.size() == 0 || data[0].size() <= column) return columnData;
1130
1131 for (unsigned int n = 0; n < data.size(); n++) columnData.push_back(data[n][column]);
1132
1133 return columnData;
1134}
1135
1136template std::vector<Int_t> TRestTools::GetColumnFromTable<Int_t>(const std::vector<std::vector<Int_t>>& data,
1137 unsigned int column);
1138
1139template std::vector<Float_t> TRestTools::GetColumnFromTable<Float_t>(
1140 const std::vector<std::vector<Float_t>>& data, unsigned int column);
1141
1142template std::vector<Double_t> TRestTools::GetColumnFromTable<Double_t>(
1143 const std::vector<std::vector<Double_t>>& data, unsigned int column);
1144
1145template std::vector<std::string> TRestTools::GetColumnFromTable<std::string>(
1146 const std::vector<std::vector<std::string>>& data, unsigned int column);
1147
1161int TRestTools::ReadASCIITable(string fName, std::vector<std::vector<std::string>>& data, Int_t skipLines,
1162 std::string separator) {
1163 if (!TRestTools::isValidFile((string)fName)) {
1164 cout << "TRestTools::ReadASCIITable. Error" << endl;
1165 cout << "Cannot open file : " << fName << endl;
1166 return 0;
1167 }
1168
1169 data.clear();
1170
1171 std::ifstream fin(fName);
1172
1173 // First we create a table with string values
1174 std::vector<std::vector<string>> values;
1175
1176 for (string line; std::getline(fin, line);) {
1177 if (skipLines > 0) {
1178 skipLines--;
1179 continue;
1180 }
1181
1182 if (line.find("#") == string::npos) {
1183 std::istringstream in(line);
1184
1185 std::string token;
1186 std::vector<std::string> tokens;
1187 while (std::getline(in, token, (char)separator[0])) {
1188 tokens.push_back(token);
1189 }
1190 data.push_back(tokens);
1191 }
1192 }
1193
1194 return 1;
1195}
1196
1210int TRestTools::ReadASCIITable(string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines,
1211 std::string separator) {
1212 if (!TRestTools::isValidFile((string)fName)) {
1213 cout << "TRestTools::ReadASCIITable. Error" << endl;
1214 cout << "Cannot open file : " << fName << endl;
1215 return 0;
1216 }
1217
1218 data.clear();
1219
1220 std::ifstream fin(fName);
1221
1222 // First we create a table with string values
1223 std::vector<std::vector<string>> values;
1224
1225 for (string line; std::getline(fin, line);) {
1226 if (skipLines > 0) {
1227 skipLines--;
1228 continue;
1229 }
1230
1231 if (line.find("#") == string::npos) {
1232 std::istringstream in(line);
1233
1234 std::string token;
1235 std::vector<std::string> tokens;
1236 while (std::getline(in, token, (char)separator[0])) {
1237 tokens.push_back(token);
1238 }
1239 values.push_back(tokens);
1240 }
1241 }
1242
1243 // Filling the double values table (TODO error handling in case ToDouble conversion fails)
1244 for (unsigned int n = 0; n < values.size(); n++) {
1245 std::vector<Double_t> dblTmp;
1246 dblTmp.clear();
1247
1248 for (unsigned int m = 0; m < values[n].size(); m++) dblTmp.push_back(StringToDouble(values[n][m]));
1249
1250 data.push_back(dblTmp);
1251 }
1252
1253 return 1;
1254}
1255
1269int TRestTools::ReadASCIITable(string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines,
1270 std::string separator) {
1271 if (!TRestTools::isValidFile((string)fName)) {
1272 cout << "TRestTools::ReadASCIITable. Error" << endl;
1273 cout << "Cannot open file : " << fName << endl;
1274 return 0;
1275 }
1276
1277 data.clear();
1278
1279 std::ifstream fin(fName);
1280
1281 // First we create a table with string values
1282 std::vector<std::vector<string>> values;
1283
1284 for (string line; std::getline(fin, line);) {
1285 if (skipLines > 0) {
1286 skipLines--;
1287 continue;
1288 }
1289
1290 if (line.find("#") == string::npos) {
1291 std::istringstream in(line);
1292
1293 std::string token;
1294 std::vector<std::string> tokens;
1295 while (std::getline(in, token, (char)separator[0])) {
1296 tokens.push_back(token);
1297 }
1298 values.push_back(tokens);
1299 }
1300 }
1301
1302 // Filling the float values table (TODO error handling in case ToFloat conversion fails)
1303 for (unsigned int n = 0; n < values.size(); n++) {
1304 std::vector<Float_t> dblTmp;
1305 dblTmp.clear();
1306
1307 for (unsigned int m = 0; m < values[n].size(); m++) dblTmp.push_back(StringToFloat(values[n][m]));
1308
1309 data.push_back(dblTmp);
1310 }
1311
1312 return 1;
1313}
1314
1328int TRestTools::ReadCSVFile(std::string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines) {
1329 return ReadASCIITable(fName, data, skipLines, ",");
1330}
1331
1345int TRestTools::ReadCSVFile(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines) {
1346 return ReadASCIITable(fName, data, skipLines, ",");
1347}
1348
1352Int_t TRestTools::isValidFile(const string& path) { return std::filesystem::is_regular_file(path); }
1353
1361bool TRestTools::fileExists(const string& filename) { return std::filesystem::exists(filename); }
1362
1366bool TRestTools::isRootFile(const string& filename) { return GetFileNameExtension(filename) == "root"; }
1367
1371bool TRestTools::isRunFile(const std::string& filename) {
1372 if (!isRootFile(filename)) return false;
1373
1374 TFile* f = TFile::Open((TString)filename);
1375
1376 TIter nextkey(f->GetListOfKeys());
1377 TKey* key;
1378 while ((key = (TKey*)nextkey())) {
1379 if ((std::string)key->GetClassName() == "TRestRun") return true;
1380 }
1381 return false;
1382}
1383
1387bool TRestTools::isDataSet(const std::string& filename) {
1388 if (!isRootFile(filename)) return false;
1389
1390 if (!TRestTools::fileExists(filename)) return false;
1391
1392 TFile* f = TFile::Open((TString)filename);
1393
1394 TIter nextkey(f->GetListOfKeys());
1395 TKey* key;
1396 while ((key = (TKey*)nextkey())) {
1397 if ((std::string)key->GetClassName() == "TRestDataSet") return true;
1398 }
1399 return false;
1400}
1401
1405bool TRestTools::isURL(const string& s) {
1406 std::regex pattern("^https?://(.+)");
1407 return std::regex_match(s, pattern);
1408}
1409
1413bool TRestTools::IsRemoteRootPath(const std::string& filename) {
1414 TUrl url(filename.c_str(), kTRUE);
1415 return url.IsValid() && std::string(url.GetProtocol()) != "file";
1416}
1417
1418#ifdef REST_TESTING_ENABLED
1422void TRestTools::ForceNextRootUpdatePreflightFailureForTesting() {
1423 gForceNextRootUpdatePreflightFailure.store(true);
1424}
1425
1429void TRestTools::ForceNextTransactionalMergeValidationFailureForTesting() {
1430 gForceNextMergeValidationFailure.store(true);
1431}
1432#endif
1433
1451bool TRestTools::MergeRootFilesTransactionally(const std::string& outputFile,
1452 const std::vector<std::string>& inputFiles,
1453 const std::string& existingTarget, bool removeInputsOnSuccess,
1454 std::string* error) {
1455 namespace fs = std::filesystem;
1456 fs::path temporary;
1457 try {
1458 const auto localOutput = LocalRootPath(outputFile);
1459 Require(localOutput.has_value(), "REST refuses to replace remote ROOT destination '" + outputFile +
1460 "'. Choose an explicit local output file.");
1461 const fs::path target = fs::weakly_canonical(fs::absolute(*localOutput));
1462 Require(fs::is_directory(target.parent_path()),
1463 "Output directory does not exist for '" + target.string() + "'");
1464
1465 std::vector<std::string> sources;
1466 if (!existingTarget.empty()) sources.push_back(existingTarget);
1467 sources.insert(sources.end(), inputFiles.begin(), inputFiles.end());
1468 Require(!sources.empty(), "No ROOT input files were provided for transactional merge");
1469
1470 RootSchemaSnapshot schema;
1471 RootContentManifest content, workers;
1472 std::string detail;
1473 for (std::size_t index = 0; index < sources.size(); ++index) {
1474 auto source = TRestRootFileHandle::Open(sources[index], TRestRootFileMode::Read);
1475 Require(bool(source), source.Error());
1476 RootContentManifest incoming;
1477 if (!ReadSchemaSnapshot(*source, schema, detail, true) ||
1478 !LoadOnDiskSchemaRules(schema, detail) ||
1479 !ReadContentManifest(*source, "", incoming, detail) ||
1480 !AddContentManifest(incoming, !existingTarget.empty() && index == 0 ? content : workers,
1481 detail))
1482 throw std::runtime_error("Cannot prepare merge input '" + sources[index] + "': " + detail);
1483 }
1484 // ROOT UPDATE merges workers together, replacing same-named target
1485 // objects; target-only objects stay in the byte-for-byte seed untouched.
1486 for (const auto& [path, incoming] : workers) {
1487 const auto found = content.find(path);
1488 Require(found == content.end() || found->second.className == incoming.className,
1489 "ROOT merge input key '" + path + "' has incompatible classes");
1490 content[path] = incoming;
1491 }
1492
1493 temporary = UniqueSiblingPath(target, "merge");
1494 const auto backup = UniqueSiblingPath(target, "backup");
1495 Require(!temporary.empty() && !backup.empty(),
1496 "Cannot allocate temporary paths next to '" + target.string() + "'");
1497 if (!existingTarget.empty()) {
1498 const auto localExisting = LocalRootPath(existingTarget);
1499 if (localExisting)
1500 fs::copy_file(*localExisting, temporary);
1501 else
1502 Require(TFile::Cp(existingTarget.c_str(), temporary.c_str(), kFALSE),
1503 "ROOT could not seed temporary merge output from '" + existingTarget + "'");
1504 }
1505 {
1506 auto output = TRestRootFileHandle::Open(temporary.string(), existingTarget.empty()
1507 ? TRestRootFileMode::Recreate
1508 : TRestRootFileMode::Update);
1509 Require(bool(output), output.Error());
1510 Require(ApplySchemaUpdate(*output, schema.numbers, detail), detail);
1511 if (inputFiles.empty()) {
1512 Require(output.Close(), output.Error());
1513 } else {
1514 TFileMerger merger(kFALSE);
1515 merger.SetPrintLevel(0);
1516 Require(merger.OutputFile(std::move(output.fFile)),
1517 "ROOT could not adopt temporary merge output '" + temporary.string() + "'");
1518 for (const auto& input : inputFiles)
1519 Require(merger.AddFile(input.c_str(), kFALSE),
1520 "ROOT could not add merge input '" + input + "'");
1521 Require(merger.Merge(), "ROOT failed while merging into '" + temporary.string() + "'");
1522 }
1523 }
1524
1525 auto validate = [&](const fs::path& path) {
1526 auto file = TRestRootFileHandle::Open(path.string(), TRestRootFileMode::Read);
1527 Require(bool(file), file.Error());
1528 RootSchemaSnapshot actualSchema;
1529 RootContentManifest actualContent;
1530 Require(ReadSchemaSnapshot(*file, actualSchema, detail) &&
1531 IsSubset(schema, actualSchema, detail) &&
1532 ReadContentManifest(*file, "", actualContent, detail) &&
1533 ValidateContentManifest(content, actualContent, detail),
1534 detail);
1535 };
1536 validate(temporary);
1537 const bool targetExisted = fs::exists(target);
1538 if (targetExisted) fs::permissions(temporary, fs::status(target).permissions());
1539 Require(ReplaceLocalFile(temporary, target, backup, detail), detail);
1540 try {
1541 validate(target);
1542#ifdef REST_TESTING_ENABLED
1543 Require(!gForceNextMergeValidationFailure.exchange(false),
1544 "Forced post-replacement ROOT validation failure for testing");
1545#endif
1546 } catch (const std::exception& failure) {
1547 std::string rollbackError;
1548 detail = failure.what();
1549 if (!RollBackLocalFile(target, backup, targetExisted, rollbackError))
1550 detail += ". Rollback also failed: " + rollbackError + ". The backup is at '" +
1551 backup.string() + "'";
1552 throw std::runtime_error(detail);
1553 }
1554
1555 if (targetExisted) {
1556 std::error_code ec;
1557 fs::remove(backup, ec);
1558 Require(!ec, "Merged output is valid, but REST could not remove rollback backup '" +
1559 backup.string() + "': " + ec.message());
1560 }
1561 if (removeInputsOnSuccess) {
1562 std::string failures;
1563 for (const auto& input : inputFiles) {
1564 const auto localInput = LocalRootPath(input);
1565 if (!localInput) continue;
1566 std::error_code ec;
1567 const auto absoluteInput = fs::weakly_canonical(fs::absolute(*localInput), ec);
1568 if (!ec && absoluteInput == target) continue;
1569 if (!ec) fs::remove(*localInput, ec);
1570 if (ec) failures += "'" + input + "': " + ec.message() + "; ";
1571 }
1572 Require(failures.empty(),
1573 "Merged output is valid, but REST could not remove input file(s): " + failures);
1574 }
1575 if (error) error->clear();
1576 return true;
1577 } catch (const std::exception& failure) {
1578 // All ROOT owners have unwound before unlinking the candidate, including
1579 // failures in TFileMerger adoption, schema registration or filesystem APIs.
1580 std::string detail = failure.what();
1581 if (!temporary.empty()) {
1582 std::error_code ec;
1583 std::filesystem::remove(temporary, ec);
1584 if (ec) detail += ". Cannot remove temporary file '" + temporary.string() + "': " + ec.message();
1585 }
1586 if (error) *error = detail;
1587 return false;
1588 }
1589}
1590
1594bool TRestTools::isPathWritable(const string& path) {
1595 int result = 0;
1596#ifdef WIN32
1597 result = _access(path.c_str(), 2);
1598#else
1599 result = access(path.c_str(), 2);
1600#endif
1601
1602 if (result == 0)
1603 return true;
1604 else
1605 return false;
1606}
1607
1611bool TRestTools::isAbsolutePath(const string& path) {
1612 if (path[0] == '/' || path[0] == '~' || path.find(':') != string::npos) {
1613 return true;
1614 }
1615 return false;
1616}
1617
1629std::pair<string, string> TRestTools::SeparatePathAndName(const string& fullname) {
1630 filesystem::path path(fullname);
1631 return {path.parent_path().string(), path.filename().string()};
1632}
1633
1639string TRestTools::GetFileNameExtension(const string& fullname) {
1640 string extension = filesystem::path(fullname).extension().string();
1641 if (extension.size() > 1) return extension.substr(1);
1642 return extension;
1643}
1644
1650string TRestTools::GetFileNameRoot(const string& fullname) {
1651 return filesystem::path(fullname).stem().string();
1652}
1653
1659// Output: {"hitsAna_xMean", "hitsAna_yMean"}.
1663std::vector<std::string> TRestTools::GetObservablesInString(const std::string& observablesStr,
1664 bool removeDuplicates) {
1665 std::vector<std::string> obsList;
1666 std::string text = observablesStr;
1667 while (text.find("_") != std::string::npos) {
1668 size_t pos_ = text.find("_");
1669 size_t beginning = text.find_last_of(" -+*/)(^%<>", pos_) + 1;
1670 size_t end = text.find_first_of(" -+*/)(^%<>", pos_);
1671 std::string obs = text.substr(beginning, end - beginning);
1672 text = Replace(text, obs, "1", 0, removeDuplicates ? 0 : 1);
1673 obsList.push_back(obs);
1674 }
1675 return obsList;
1676}
1677
1689std::set<std::string> TRestTools::GetMatchingStrings(const std::vector<std::string>& stack,
1690 const std::vector<std::string>& wantedStrings) {
1691 std::set<std::string> result;
1692 for (auto& ws : wantedStrings) {
1693 if (ws.find("*") != std::string::npos || ws.find("?") != std::string::npos) {
1694 for (auto& c : stack)
1695 if (MatchString(c, ws)) result.insert(c);
1696 } else if (std::find(stack.begin(), stack.end(), ws) != stack.end())
1697 result.insert(ws);
1698 }
1699 // return std::vector<std::string>(result.begin(), result.end()); // convert to vector
1700 return result;
1701}
1702
1710 // we replace multiple appearances of "/" (slash) by a single "/"
1711 string to_replace = "//";
1712 size_t start_pos = str.find(to_replace);
1713 while (start_pos != string::npos) {
1714 str.replace(start_pos, to_replace.length(), "/");
1715 start_pos = str.find(to_replace);
1716 }
1717 return str;
1718}
1719
1728string TRestTools::GetPureFileName(const string& path) { return filesystem::path(path).filename().string(); }
1729
1733string TRestTools::ToAbsoluteName(const string& filename) {
1734 filesystem::path path;
1735 for (const auto& directory : filesystem::path(filename)) {
1736 if (path.empty() && directory == "~") {
1737 // path starts with ~
1738 const auto envVariableHome = getenv("HOME");
1739 if (envVariableHome == nullptr) {
1740 cout << "TRestTools::ToAbsoluteName - ERROR - "
1741 "cannot resolve ~ because 'HOME' env variable does not exist"
1742 << endl;
1743 exit(1);
1744 }
1745 const auto userHomePath = filesystem::path(envVariableHome);
1746 if (userHomePath.empty()) {
1747 cout << "TRestTools::ToAbsoluteName - ERROR - "
1748 "cannot resolve ~ because 'HOME' env variable is not set to a valid value"
1749 << endl;
1750 exit(1);
1751 }
1752 path /= userHomePath;
1753 } else {
1754 path /= directory;
1755 }
1756 }
1757 return filesystem::weakly_canonical(path).string();
1758}
1759
1769vector<string> TRestTools::GetSubdirectories(const string& _path, int recursion) {
1770 vector<string> result;
1771
1772 std::filesystem::path path(_path);
1773 if (exists(path)) {
1774 std::filesystem::directory_iterator iter(path);
1775 for (auto& it : iter) {
1776 if (it.is_directory()) {
1777 result.push_back(it.path().string());
1778
1779 if (recursion != 0) {
1780 vector<string> subD = GetSubdirectories(it.path().string(), recursion - 1);
1781 result.insert(result.begin(), subD.begin(), subD.end());
1782 }
1783 }
1784 }
1785 }
1786
1787 return result;
1788}
1789
1794string TRestTools::SearchFileInPath(vector<string> paths, string filename) {
1795 if (fileExists(filename)) {
1796 return filename;
1797 } else {
1798 for (unsigned int i = 0; i < paths.size(); i++) {
1799 string path = paths[i];
1800 if (path[path.size() - 1] != '/') {
1801 path = path + "/";
1802 }
1803
1804 if (fileExists(path + filename)) {
1805 return path + filename;
1806 }
1807
1808 // search also in subdirectory, but only 5 times of recursion
1809 vector<string> pathsExpanded = GetSubdirectories(paths[i], 5);
1810 for (unsigned int j = 0; j < pathsExpanded.size(); j++)
1811 if (fileExists(pathsExpanded[j] + "/" + filename)) return pathsExpanded[j] + "/" + filename;
1812 }
1813 }
1814 return "";
1815}
1816
1821bool TRestTools::CheckFileIsAccessible(const std::string& filename) {
1822 ifstream ifs;
1823 ifs.open(filename.c_str());
1824
1825 if (!ifs) {
1826 return false;
1827 } else {
1828 ifs.close();
1829 }
1830 return true;
1831}
1832
1841vector<string> TRestTools::GetFilesMatchingPattern(string pattern, bool unlimited) {
1842 std::vector<string> outputFileNames;
1843 if (pattern != "") {
1844 vector<string> items = Split(pattern, "\n");
1845 for (auto item : items) {
1846 if (item.find_first_of("*?") != string::npos) {
1847#ifdef WIN32
1848 item = Replace(item, "/", "\\");
1849 string item_trim =
1850 item.substr(0, item.find_first_of("*?")); // trim string to before wildcard character
1851 auto path_name = SeparatePathAndName(item_trim);
1852 string _path = path_name.first;
1853 if (!std::filesystem::exists(_path)) {
1854 RESTError << "TRestTools::GetFilesMatchingPattern(): path " << _path << " does not exist!"
1855 << RESTendl;
1856 return outputFileNames;
1857 }
1858
1859 std::filesystem::path path(_path);
1860 std::filesystem::recursive_directory_iterator iter(path);
1861 for (auto& it : iter) {
1862 if (it.is_regular_file()) {
1863 string filename = it.path().string();
1864 if (MatchString(filename, item)) {
1865 outputFileNames.push_back(it.path().string());
1866 }
1867 }
1868 }
1869#else
1870 auto path_name = SeparatePathAndName(item);
1871 if (unlimited) {
1872 std::string currentDir = filesystem::current_path();
1873 ChangeDirectory(path_name.first);
1874 string a = Execute("find -type f -name \'" + path_name.second + "\'");
1875 ChangeDirectory(currentDir);
1876 auto b = Split(a, "\n");
1877
1878 for (unsigned int i = 0; i < b.size(); i++) {
1879 outputFileNames.push_back(path_name.first + "/" + b[i]);
1880 }
1881
1882 } else {
1883 string a = Execute("find " + item);
1884 auto b = Split(a, "\n");
1885
1886 for (unsigned int i = 0; i < b.size(); i++) {
1887 outputFileNames.push_back(b[i]);
1888 }
1889 }
1890#endif
1891
1892 } else {
1893 if (fileExists(item)) outputFileNames.push_back(item);
1894 }
1895 }
1896 }
1897 return outputFileNames;
1898}
1899
1904#ifndef REST_Version
1905#define REST_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
1906#endif
1907 vector<string> ver = Split(in, ".");
1908 if (ver.size() == 3) {
1909 vector<int> verint;
1910 for (auto v : ver) {
1911 int n = StringToInteger(v.substr(0, v.find_first_not_of("0123456789")));
1912 if (n != -1) {
1913 verint.push_back(n);
1914 } else {
1915 return -1;
1916 }
1917 }
1918 return REST_VERSION(verint[0], verint[1], verint[2]);
1919 }
1920 return -1;
1921}
1922
1926string TRestTools::Execute(string cmd) {
1927 std::array<char, 128> buffer;
1928 string result;
1929#ifdef WIN32
1930 std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(("powershell.exe " + cmd).c_str(), "r"), _pclose);
1931#else
1932 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
1933#endif // WIN32
1934
1935 if (!pipe) {
1936 throw std::runtime_error("popen() failed!");
1937 }
1938 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
1939 result += buffer.data();
1940 }
1941
1942 if (result.size() > 0 && result[result.size() - 1] == '\n')
1943 result = result.substr(0, result.size() - 1); // remove last "\n"
1944
1945 return result;
1946}
1947
1952std::istream& TRestTools::GetLine(std::istream& is, std::string& t) {
1953 t.clear();
1954
1955 // The characters in the stream are read one-by-one using a std::streambuf.
1956 // That is faster than reading them one-by-one using the std::istream.
1957 // Code that uses streambuf this way must be guarded by a sentry object.
1958 // The sentry object performs various tasks,
1959 // such as thread synchronization and updating the stream state.
1960
1961 std::istream::sentry se(is, true);
1962 std::streambuf* sb = is.rdbuf();
1963
1964 for (;;) {
1965 int c = sb->sbumpc();
1966 switch (c) {
1967 case '\n':
1968 return is;
1969 case '\r':
1970 if (sb->sgetc() == '\n') sb->sbumpc();
1971 return is;
1972 case std::streambuf::traits_type::eof():
1973 // Also handle the case when the last line has no line ending
1974 if (t.empty()) is.setstate(std::ios::eofbit);
1975 return is;
1976 default:
1977 t += (char)c;
1978 }
1979 }
1980}
1981
1989std::string TRestTools::DownloadRemoteFile(const string& url, bool pidPrefix) {
1990 string pureName = TRestTools::GetPureFileName(url);
1991 if (pureName.empty()) {
1992 RESTWarning << "error! (TRestTools::DownloadRemoteFile): url is not a file!" << RESTendl;
1993 RESTWarning << "please specify a concrete file name in this url" << RESTendl;
1994 RESTWarning << "url: " << url << RESTendl;
1995 return "";
1996 }
1997
1998 if (url.find("local:") == 0) {
1999 return Replace(url, "local:", "");
2000 } else {
2001 string fullpath =
2002 REST_USER_PATH + "/download/" + (pidPrefix ? "PID_" + ToString(getpid()) + "_" : "") + pureName;
2003 int out;
2004 int attempts = 10;
2005 do {
2006 out = TRestTools::DownloadRemoteFile(url, fullpath);
2007 if (out == 1024) {
2008 RESTWarning << "Retrying download in 5 seconds" << RESTendl;
2009 std::this_thread::sleep_for(std::chrono::seconds(5));
2010 } else if (attempts < 10) {
2011 RESTSuccess << "Download succeeded after " << 10 - attempts << " attempts" << RESTendl;
2012 }
2013 attempts--;
2014 } while (out == 1024 && attempts > 0);
2015
2016 if (out == 0 || TRestTools::fileExists(fullpath)) {
2017 return fullpath;
2018 } else {
2019 return "";
2020 }
2021 }
2022}
2023
2030int TRestTools::DownloadRemoteFile(string remoteFile, string localFile) {
2031 TUrl url(remoteFile.c_str());
2032
2033 RESTInfo << "Downloading remote file : " << remoteFile << RESTendl;
2034 RESTInfo << "To local file : " << localFile << RESTendl;
2035
2036 string localFiletmp = localFile + ".restdownload";
2037 if ((string)url.GetProtocol() == "https" || (string)url.GetProtocol() == "http") {
2038 string path = TRestTools::SeparatePathAndName(localFiletmp).first;
2039 if (!TRestTools::fileExists(path)) {
2040 if (!filesystem::create_directories(path)) {
2041 std::cerr << "mkdir failed to create directory: " << path << std::endl;
2042 return -1;
2043 }
2044 }
2045
2046 string cmd = "wget --no-check-certificate " + EscapeSpecialLetters(remoteFile) + " -O " +
2047 EscapeSpecialLetters(localFiletmp) + " -q";
2048 RESTDebug << cmd << RESTendl;
2049 int a = system(cmd.c_str());
2050
2051 if (a == 0) {
2052 rename(localFiletmp.c_str(), localFile.c_str());
2053 return 0;
2054 } else {
2055 RESTError << "download failed! (" << remoteFile << ")" << RESTendl;
2056 if (a == 1024) {
2057 RESTError << "Network connection problem?" << RESTendl;
2058 return 1024;
2059 }
2060 if (a == 2048) {
2061 RESTError << "File does NOT exist in remotely?" << RESTendl;
2062 return 2048;
2063 }
2064 }
2065 } else if ((string)url.GetProtocol() == "ssh") {
2066 string cmd = "scp -P " + ToString(url.GetPort() == 0 ? 22 : url.GetPort()) + " " + url.GetUser() +
2067 "@" + url.GetHost() + ":" + EscapeSpecialLetters(url.GetFile()) + " " + localFiletmp;
2068 cout << cmd << endl;
2069 int a = system(cmd.c_str());
2070 if (a == 0) {
2071 rename(localFiletmp.c_str(), localFile.c_str());
2072 return 0;
2073 }
2074 } else {
2075 if (!TRestTools::fileExists(remoteFile)) {
2076 RESTWarning << "Trying to download: " << remoteFile << RESTendl;
2077 RESTWarning << "Unknown protocol!" << RESTendl;
2078 }
2079 }
2080
2081 return -1;
2082}
2083
2088std::string TRestTools::POSTRequest(const std::string& url, const std::map<std::string, std::string>& keys) {
2089 std::string file_content = "";
2090#ifdef USE_Curl
2091 CURL* curl;
2092 CURLcode res;
2093
2094 string filename = REST_USER_PATH + "/download/curl.out";
2095
2096 /* In windows, this will init the winsock stuff */
2097 curl_global_init(CURL_GLOBAL_ALL);
2098
2099 FILE* f = fopen(filename.c_str(), "wt");
2100
2101 std::string request = "";
2102 int n = 0;
2103 for (auto const& x : keys) {
2104 if (n > 0) request += "&";
2105 request += x.first + "=" + x.second;
2106 n++;
2107 }
2108 /* get a curl handle */
2109 curl = curl_easy_init();
2110 if (curl) {
2111 /* First set the URL that is about to receive our POST. This URL can
2112 just as well be a https:// URL if that is what should receive the
2113 data. */
2114 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
2115 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)f);
2116 /* Now specify the POST data */
2117 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.c_str());
2118
2119 /* Perform the request, res will get the return code */
2120 res = curl_easy_perform(curl);
2121 /* Check for errors */
2122 if (res != CURLE_OK)
2123 RESTError << "curl_easy_perform() failed: " << curl_easy_strerror(res) << RESTendl;
2124
2125 /* always cleanup */
2126 curl_easy_cleanup(curl);
2127 }
2128 fclose(f);
2129 curl_global_cleanup();
2130
2131 std::getline(std::ifstream(filename), file_content, '\0');
2132#else
2133 RESTError << "TRestTools::POSTRequest. REST framework was compiled without CURL support" << RESTendl;
2134 RESTError << "Please recompile REST after installing curl development libraries." << RESTendl;
2135 RESTError << "Depending on your system this might be: curl-dev, curl-devel or libcurl-openssl-dev. "
2136 << RESTendl;
2137 RESTError << "No file will be downloaded" << RESTendl;
2138#endif
2139
2140 return file_content;
2141}
2142
2150int TRestTools::UploadToServer(string localFile, string remoteFile, string methodUrl) {
2151 if (!TRestTools::fileExists(localFile)) {
2152 cout << "error! local file not exist!" << endl;
2153 return -1;
2154 }
2155 // construct path
2156 // [proto://][user[:passwd]@]host[:port]/file.ext[#anchor][?options]
2157 TUrl url(remoteFile.c_str());
2158 TUrl method(methodUrl.c_str());
2159 if (method.GetProtocol() != (string) "") url.SetProtocol(method.GetProtocol());
2160 if (method.GetPort() != 0) url.SetPort(method.GetPort());
2161 if (method.GetUser() != (string) "") url.SetUser(method.GetUser());
2162 if (method.GetPasswd() != (string) "") url.SetPasswd(method.GetPasswd());
2163
2164 if ((string)url.GetProtocol() == "https" || (string)url.GetProtocol() == "http") {
2165 // maybe we use curl to upload to http in future
2166 } else if ((string)url.GetProtocol() == "ssh") {
2167 string cmd = "scp -P " + ToString(url.GetPort() == 0 ? 22 : url.GetPort()) + " " +
2168 EscapeSpecialLetters(localFile) + " " + url.GetUser() + "@" + url.GetHost() + ":" +
2169 EscapeSpecialLetters(url.GetFile());
2170 cout << cmd << endl;
2171 int a = system(cmd.c_str());
2172
2173 if (a != 0) {
2174 RESTError << __PRETTY_FUNCTION__ << RESTendl;
2175 RESTError << "problem copying gases definitions to remote server" << RESTendl;
2176 RESTError << "Please report this problem at "
2177 "http://gifna.unizar.es/rest-forum/"
2178 << RESTendl;
2179 return -1;
2180 }
2181
2182 return 0;
2183 }
2184 return 0;
2185}
2186
2187void TRestTools::ChangeDirectory(const string& toDirectory) { filesystem::current_path(toDirectory); }
2188
2197std::vector<int> TRestTools::CanvasDivisions(int n) {
2198 std::vector<int> r;
2199 for (int i = 2; i * i <= n; i += 1 + (i > 2)) {
2200 while ((n % i) == 0) {
2201 r.push_back(i);
2202 n /= i;
2203 }
2204 }
2205 if (n != 1) r.push_back(n);
2206
2207 while (r.size() > 2) {
2208 // We multiply the 2 lowest elements and
2209 // replace the elements in the vector by the result
2210 auto min1 = std::min_element(r.begin(), r.end());
2211 int low1 = *min1;
2212
2213 // Remove the first element equal to min1 (efficient way)
2214 auto it = std::find(r.begin(), r.end(), low1);
2215 if (it != r.end()) {
2216 std::iter_swap(it, r.end() - 1);
2217 r.erase(r.end() - 1);
2218 }
2219
2220 auto min2 = std::min_element(r.begin(), r.end());
2221 int low2 = *min2;
2222
2223 // Remove the first element equal to min2 (efficient way)
2224 it = std::find(r.begin(), r.end(), low2);
2225 if (it != r.end()) {
2226 std::iter_swap(it, r.end() - 1);
2227 r.erase(r.end() - 1);
2228 }
2229
2230 int resultado = low1 * low2;
2231 r.push_back(resultado);
2232 }
2233
2234 std::sort(r.begin(), r.end());
2235
2236 if (r.size() == 1) r.push_back(1);
2237
2238 return r;
2239}
2240
2241string ValueWithQuantity::ToString() const {
2242 string unit;
2243 auto value = fValue;
2244 if (fQuantity == ENERGY) {
2245 unit = "eV";
2246 value *= 1E3; // since we store energy in keV, not in eV
2247 } else if (fQuantity == TIME) {
2248 unit = "s"; // time is stored in microseconds
2249 value *= 1E-6;
2250 } else if (fQuantity == LENGTH) {
2251 unit = "m";
2252 value *= 1E-3; // since we store length in mm, not in m
2253 } else {
2254 return "";
2255 }
2256
2257 const auto abs = TMath::Abs(value);
2258 if (abs == 0) {
2259 return TString::Format("%d", 0).Data();
2260 } else if (abs < 1E-6) {
2261 return TString::Format("%.2f %s%s", value * 1E9, "n", unit.c_str()).Data();
2262 } else if (abs < 1E-3) {
2263 return TString::Format("%.2f %s%s", value * 1E6, "u", unit.c_str()).Data();
2264 } else if (abs < 1E0) {
2265 return TString::Format("%.2f %s%s", value * 1E3, "m", unit.c_str()).Data();
2266 } else if (abs < 1E3) {
2267 return TString::Format("%.2f %s%s", value * 1E0, "", unit.c_str()).Data();
2268 } else if (abs < 1E6) {
2269 return TString::Format("%.2f %s%s", value * 1E-3, "k", unit.c_str()).Data();
2270 } else if (abs < 1E9) {
2271 return TString::Format("%.2f %s%s", value * 1E-6, "M", unit.c_str()).Data();
2272 } else if (abs < 1E12) {
2273 return TString::Format("%.2f %s%s", value * 1E-9, "G", unit.c_str()).Data();
2274 } else {
2275 return TString::Format("%.2f %s%s", value * 1E-12, "T", unit.c_str()).Data();
2276 }
2277}
2278
2279string ToTimeStringLong(double seconds) {
2280 const auto abs = TMath::Abs(seconds);
2281 if (abs < 60) {
2282 return TString::Format("%.2f %s", seconds, "seconds").Data();
2283 } else if (abs < 60 * 60) {
2284 return TString::Format("%.2f %s", seconds / 60.0, "minutes").Data();
2285 } else if (abs < 60 * 60 * 24) {
2286 return TString::Format("%.2f %s", seconds / (60.0 * 60.0), "hours").Data();
2287 } else {
2288 return TString::Format("%.2f %s", seconds / (60.0 * 60.0 * 24.0), "days").Data();
2289 }
2290}
bool Close() noexcept
Definition: TRestTools.cxx:662
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
Definition: TRestTools.cxx:597
static bool PrepareBorrowedUpdate(TFile &file, std::string *error=nullptr)
Definition: TRestTools.cxx:678
static std::string Execute(std::string cmd)
Executes a shell command and returns its output in 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 Int_t isValidFile(const std::string &path)
Returns true if the file with path filename exists.
static bool IsRemoteRootPath(const std::string &filename)
Returns true when ROOT resolves filename to a non-local protocol.
static std::string GetFileNameExtension(const std::string &fullname)
Gets the file extension as the substring found after the latest ".".
static std::string POSTRequest(const std::string &url, const std::map< std::string, std::string > &keys)
It performs a POST web protocol request using a set of keys and values given by argument,...
static std::vector< std::string > GetSubdirectories(const std::string &path, int recursion=-1)
It lists all the subdirectories inside path and adds them to the result vector.
static int ReadASCIITable(std::string fName, std::vector< std::vector< Double_t > > &data, Int_t skipLines=0, std::string separator="\t")
Reads an ASCII file containing a table with values.
static bool CheckFileIsAccessible(const std::string &)
Checks if the config file can be opened (and thus exists). It returns true in case of success,...
static std::string GetPureFileName(const std::string &fullPathFileName)
Removes all directories in the full path filename description given in the argument.
static int ReadBinaryTable(std::string fName, std::vector< std::vector< T > > &data, Int_t columns=-1)
Reads a binary file containing a fixed-columns table with values.
Definition: TRestTools.cxx:886
static int GetBinaryFileColumns(std::string fname)
It extracts the number of columns from the filename extension given by argument. The file should cont...
Definition: TRestTools.cxx:948
static std::vector< std::string > GetOptions(std::string optionsStr)
Returns all the options in an option string.
Definition: TRestTools.cxx:720
static int PrintTable(std::vector< std::vector< T > > data, Int_t start=0, Int_t end=0)
Prints the contents of the vector table given as argument in screen. Allowed types are Int_t,...
Definition: TRestTools.cxx:796
static std::string GetFileNameRoot(const std::string &fullname)
Gets the filename root as the substring found before the latest ".".
static T GetMaxValueFromTable(const std::vector< std::vector< T > > &data, Int_t column=-1)
It returns the maximum value for a particular column from the table given by argument....
static T GetLowestIncreaseFromTable(std::vector< std::vector< T > > data, Int_t column)
It returns the lowest increase, different from zero, between the elements of a particular column from...
static void LoadRESTLibrary(bool silent=false)
Calls gSystem to load REST library.
Definition: TRestTools.cxx:729
static int ReadCSVFile(std::string fName, std::vector< std::vector< Double_t > > &data, Int_t skipLines=0)
Reads a CSV file containing a table with comma separated values.
static bool isRunFile(const std::string &filename)
It checks if the file has been processed using a REST event processing chain.
static int ExportASCIITable(std::string fname, std::vector< std::vector< T > > &data)
Writes the contents of the vector table given as argument to fname. Allowed types are Int_t,...
Definition: TRestTools.cxx:818
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
static std::set< std::string > GetMatchingStrings(const std::vector< std::string > &stack, const std::vector< std::string > &wantedStrings)
Returns a set of strings that match the wanted strings from the stack. The wanted strings can contain...
static std::vector< int > CanvasDivisions(int n)
It returns a vector with 2 components {a,b}, the components satisfy that a x b = n,...
static std::string DownloadRemoteFile(const std::string &remoteFile, bool pidPrefix=false)
download the remote file automatically, returns the downloaded file name.
static T GetMinValueFromTable(const std::vector< std::vector< T > > &data, Int_t column=-1)
It returns the minimum value for a particular column from the table given by argument....
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 T GetIntegralFromTable(const std::vector< std::vector< T > > &data)
It returns the lowest increase, different from zero, between the elements of a particular column from...
static bool isAbsolutePath(const std::string &path)
Check if the path is absolute path or not.
static int ExportBinaryTable(std::string fname, std::vector< std::vector< T > > &data)
Writes the contents of the vector table given as argument to fname as a binary file....
Definition: TRestTools.cxx:847
static Bool_t IsBinaryFile(std::string fname)
It identifies if the filename extension follows the formatting ".Nxyzf", where the number of columns ...
Definition: TRestTools.cxx:936
static std::string RemoveMultipleSlash(std::string)
Returns the input string but without multiple slashes ("/")
static bool isDataSet(const std::string &filename)
It checks if the file contains a dataset object.
static bool isURL(const std::string &filename)
Returns true if filename is an http address.
static std::istream & GetLine(std::istream &is, std::string &t)
It reads the next line from the incoming istream and puts it in the string argument t....
static std::vector< std::string > GetObservablesInString(const std::string &observablesStr, bool removeDuplicates=true)
Returns a vector with the observables names found in the input string. The observables names must con...
static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl="")
static bool isRootFile(const std::string &filename)
Returns true if the filename has *.root* extension.
static std::string SearchFileInPath(std::vector< std::string > path, std::string filename)
Search file in the given vector of path strings, return a full name if found, return "" if not.
static void TransposeTable(std::vector< std::vector< T > > &data)
It transposes the std::vector<std::vector> table given in the argument. It will transform rows in col...
Definition: TRestTools.cxx:978
static std::vector< T > GetColumnFromTable(const std::vector< std::vector< T > > &data, unsigned int column)
It returns a vector with the values extracted from the particular column inside the data table given ...
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.
static std::string ToAbsoluteName(const std::string &filename)
It takes a path and returns its absolute path.
Float_t StringToFloat(std::string in)
Gets a float from a string.
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.
Bool_t MatchString(std::string str, std::string matcher)
This method matches a string with certain matcher. Returns true if matched. Supports wildcard charact...
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.
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.