R3BROOT
R3B analysis software
Toggle main menu visibility
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 "
R3BDataMonitorCanvas.h
"
16
#include "
R3BException.h
"
17
#include <FairRootFileSink.h>
18
#include <FairRunOnline.h>
19
#include <FairRuntimeDb.h>
20
#include <FairSink.h>
21
#include <TDirectory.h>
22
#include <TFile.h>
23
#include <TH1.h>
24
#include <chrono>
25
#include <fairlogger/Logger.h>
26
#include <fmt/chrono.h>
27
#include <fmt/core.h>
28
#include <fmt/format.h>
29
#include <memory>
30
#include <string>
31
#include <string_view>
32
33
namespace
34
{
35
auto
create_datatime_rootfile(std::string_view base_filename) -> std::unique_ptr<TFile>
36
{
37
auto
time = std::chrono::system_clock::now();
38
const
auto
file_name = fmt::format(
"{}-{:%Y-%m-%d-%Hh-%Mm-%Ss}.root"
, base_filename, time);
39
auto
* root_file = TFile::Open(file_name.data(),
"RECREATE"
);
40
return
std::unique_ptr<TFile>{ root_file };
41
}
42
43
}
// namespace
44
45
namespace
R3B
46
{
47
auto
DataMonitor::get
(
const
std::string& histName) -> TH1*
48
{
49
if
(
auto
hist =
histograms_
.find(histName); hist !=
histograms_
.end())
50
{
51
return
hist->second.get();
52
}
53
throw
R3B::logic_error
(fmt::format(
"Histogram with the name {} doesn't exist!"
, histName));
54
}
55
56
auto
DataMonitor::get_canvas
(
const
std::string& histName) ->
DataMonitorCanvas
&
57
{
58
if
(
auto
canvas =
canvases_
.find(histName); canvas !=
canvases_
.end())
59
{
60
return
canvas->second;
61
}
62
throw
R3B::logic_error
(fmt::format(
"Canvas with the name {} doesn't exist!"
, histName));
63
}
64
65
void
DataMonitor::save_to_sink
(std::string_view folderName, FairSink* sinkFile)
66
{
67
auto
* hist_dir =
get_hist_dir
(sinkFile);
68
auto
* new_dir = folderName.empty() ? hist_dir : hist_dir->mkdir(folderName.data(),
""
,
true
);
69
if
(new_dir ==
nullptr
)
70
{
71
throw
R3B::runtime_error
(
72
fmt::format(
"Failed to create a sub directory {} for the histograms!"
, folderName));
73
}
74
LOGP(info,
75
"Saving figures to the directory {:?} in the root file {:?}"
,
76
new_dir->GetName(),
77
new_dir->GetFile()->GetName());
78
79
write_all
(new_dir);
80
// old_dir->cd();
81
}
82
83
auto
DataMonitor::get_hist_dir
(FairSink* sinkFile) -> TDirectory*
84
{
85
auto
* rootSinkFile =
dynamic_cast<
FairRootFileSink*
>
(sinkFile);
86
if
(rootSinkFile ==
nullptr
)
87
{
88
throw
R3B::logic_error
(
"Cannot save the histograms as the output file is not a root file!"
);
89
}
90
auto
* rootFile = rootSinkFile->GetRootFile();
91
auto
* hist_dir = rootFile->mkdir(
DEFAULT_HIST_MONITOR_DIR
,
""
,
true
);
92
if
(hist_dir ==
nullptr
)
93
{
94
throw
R3B::runtime_error
(
"Cannot create a directory for the histograms!"
);
95
}
96
return
hist_dir;
97
}
98
99
void
DataMonitor::draw_canvases
()
100
{
101
for
(
auto
& [canvas_name, canvas] :
canvases_
)
102
{
103
canvas.draw();
104
}
105
}
106
107
void
DataMonitor::register_canvases
(FairRun* run)
108
{
109
if
(
auto
* run_online =
dynamic_cast<
FairRunOnline*
>
(run); run_online !=
nullptr
)
110
{
111
for
(
auto
& [name, canvas] :
canvases_
)
112
{
113
run_online->AddObject(canvas.get_canvas());
114
}
115
}
116
}
117
118
void
DataMonitor::reset_all_hists
()
119
{
120
for
(
auto
& [name, hist] :
histograms_
)
121
{
122
hist->Reset();
123
}
124
}
125
126
void
DataMonitor::save_to_file
(std::string_view filename)
127
{
128
if
(filename.empty())
129
{
130
filename =
save_filename_
;
131
}
132
auto
rootfile = create_datatime_rootfile(filename);
133
LOGP(info,
"Saving histograms to {}"
, rootfile->GetName());
134
write_all
(rootfile.get());
135
}
136
137
void
DataMonitor::write_all
(TDirectory* dir)
138
{
139
for
(
auto
& [name, hist] :
histograms_
)
140
{
141
if
(hist->GetEntries() == 0)
142
{
143
LOGP(warn,
"Histogram {} is empty while written to the file!"
, hist->GetName());
144
}
145
dir->WriteObject(hist.get(), hist->GetName(),
"update"
);
146
}
147
for
(
auto
& [name, graph] :
graphs_
)
148
{
149
dir->WriteObject(graph.get(), graph->GetName(),
"update"
);
150
}
151
}
152
}
// namespace R3B
R3BDataMonitor.h
R3BDataMonitorCanvas.h
R3BException.h
R3B::DataMonitorCanvas
Definition
R3BDataMonitorCanvas.h:47
R3B::DataMonitor::write_all
void write_all(TDirectory *dir)
Definition
R3BDataMonitor.cxx:137
R3B::DataMonitor::histograms_
std::map< std::string, std::unique_ptr< TH1 > > histograms_
Definition
R3BDataMonitor.h:81
R3B::DataMonitor::reset_all_hists
void reset_all_hists()
Definition
R3BDataMonitor.cxx:118
R3B::DataMonitor::save_filename_
std::string save_filename_
Definition
R3BDataMonitor.h:80
R3B::DataMonitor::save_to_sink
void save_to_sink(std::string_view folderName="", FairSink *sinkFile=FairRun::Instance() ->GetSink())
Definition
R3BDataMonitor.cxx:65
R3B::DataMonitor::register_canvases
void register_canvases(FairRun *run)
Definition
R3BDataMonitor.cxx:107
R3B::DataMonitor::draw_canvases
void draw_canvases()
Definition
R3BDataMonitor.cxx:99
R3B::DataMonitor::canvases_
std::map< std::string, DataMonitorCanvas > canvases_
Definition
R3BDataMonitor.h:83
R3B::DataMonitor::graphs_
std::map< std::string, std::unique_ptr< TGraph > > graphs_
Definition
R3BDataMonitor.h:82
R3B::DataMonitor::get_hist_dir
static auto get_hist_dir(FairSink *sinkFile) -> TDirectory *
Definition
R3BDataMonitor.cxx:83
R3B::DataMonitor::save_to_file
void save_to_file(std::string_view filename="")
Definition
R3BDataMonitor.cxx:126
R3B::DataMonitor::get_canvas
auto get_canvas(const std::string &histName) -> DataMonitorCanvas &
Definition
R3BDataMonitor.cxx:56
R3B::DataMonitor::get
auto get(const std::string &histName) -> TH1 *
Definition
R3BDataMonitor.cxx:47
R3B::logic_error
Definition
R3BException.h:32
R3B::runtime_error
Definition
R3BException.h:26
R3B
Definition
R3BNeulandAnalysisApp.cxx:44
R3B::DEFAULT_HIST_MONITOR_DIR
constexpr auto DEFAULT_HIST_MONITOR_DIR
Definition
R3BDataMonitor.h:36
r3bbase
data_monitor
R3BDataMonitor.cxx
Generated by
1.18.0