R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BDataMonitor.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2023 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include "R3BDataMonitor.h"
15#include <FairRootFileSink.h>
16#include <FairRunOnline.h>
17#include <FairRuntimeDb.h>
18#include <fmt/chrono.h>
19#include <fmt/format.h>
20
21namespace
22{
23 auto create_datatime_rootfile(std::string_view base_filename) -> std::unique_ptr<TFile>
24 {
25 auto time = std::chrono::system_clock::now();
26 const auto file_name = fmt::format("{}-{:%Y-%m-%d-%Hh-%Mm-%Ss}.root", base_filename, time);
27 auto* root_file = TFile::Open(file_name.data(), "RECREATE");
28 return std::unique_ptr<TFile>{ root_file };
29 }
30
31} // namespace
32
33namespace R3B
34{
35 auto DataMonitor::get(const std::string& histName) -> TH1*
36 {
37 if (auto hist = histograms_.find(histName); hist != histograms_.end())
38 {
39 return hist->second.get();
40 }
41 throw R3B::logic_error(fmt::format("Histogram with the name {} doesn't exist!", histName));
42 }
43
44 auto DataMonitor::get_canvas(const std::string& histName) -> DataMonitorCanvas&
45 {
46 if (auto canvas = canvases_.find(histName); canvas != canvases_.end())
47 {
48 return canvas->second;
49 }
50 throw R3B::logic_error(fmt::format("Canvas with the name {} doesn't exist!", histName));
51 }
52
53 void DataMonitor::save_to_sink(std::string_view folderName, FairSink* sinkFile)
54 {
55 auto* hist_dir = get_hist_dir(sinkFile);
56 auto* new_dir = folderName.empty() ? hist_dir : hist_dir->mkdir(folderName.data(), "", true);
57 if (new_dir == nullptr)
58 {
60 fmt::format("Failed to create a sub directory {} for the histrogams!", folderName));
61 }
62 R3BLOG(info,
63 fmt::format("Saving figures to the directory {:?} in the root file {:?}",
64 folderName,
65 new_dir->GetFile()->GetName()));
66
67 write_all(new_dir);
68 // old_dir->cd();
69 }
70
71 auto DataMonitor::get_hist_dir(FairSink* sinkFile) -> TDirectory*
72 {
73 auto* rootSinkFile = dynamic_cast<FairRootFileSink*>(sinkFile);
74 if (rootSinkFile == nullptr)
75 {
76 throw R3B::logic_error("Cannot save the histograms as the output file is not a root file!");
77 }
78 auto* rootFile = rootSinkFile->GetRootFile();
79 auto* hist_dir = rootFile->mkdir(DEFAULT_HIST_MONITOR_DIR, "", true);
80 if (hist_dir == nullptr)
81 {
82 throw R3B::runtime_error("Cannot create a directory for the histrogams!");
83 }
84 return hist_dir;
85 }
86
88 {
89 for (auto& [canvas_name, canvas] : canvases_)
90 {
91 canvas.draw();
92 }
93 }
94
96 {
97 if (auto* run_online = dynamic_cast<FairRunOnline*>(run); run_online != nullptr)
98 {
99 for (auto& [name, canvas] : canvases_)
100 {
101 run_online->AddObject(canvas.get_canvas());
102 }
103 }
104 }
105
107 {
108 for (auto& [name, hist] : histograms_)
109 {
110 hist->Reset();
111 }
112 }
113
114 void DataMonitor::save_to_file(std::string_view filename)
115 {
116 if (filename.empty())
117 {
118 filename = save_filename_;
119 }
120 auto rootfile = create_datatime_rootfile(filename);
121 R3BLOG(info, fmt::format("Saving histograms to {}", rootfile->GetName()));
122 write_all(rootfile.get());
123 }
124
125 void DataMonitor::write_all(TDirectory* dir)
126 {
127 for (auto& [name, hist] : histograms_)
128 {
129 if (hist->GetEntries() == 0)
130 {
131 R3BLOG(warn, fmt::format("Histogram {} is empty while written to the file!", hist->GetName()));
132 }
133 dir->WriteObject(hist.get(), hist->GetName());
134 }
135 for (auto& [name, graph] : graphs_)
136 {
137 dir->WriteObject(graph.get(), graph->GetName(), "overwrite");
138 }
139 }
140} // namespace R3B
#define R3BLOG(severity, x)
Definition R3BLogger.h:35
void save_to_sink(std::string_view folderName="", FairSink *sinkFile=FairRun::Instance() ->GetSink())
void register_canvases(FairRun *run)
void save_to_file(std::string_view filename="")
auto get_canvas(const std::string &histName) -> DataMonitorCanvas &
auto get(const std::string &histName) -> TH1 *
constexpr auto DEFAULT_HIST_MONITOR_DIR