Skip to content

Commit

Permalink
reformat on DataMonitor and EventPropagator
Browse files Browse the repository at this point in the history
  • Loading branch information
YanzhaoW committed Aug 23, 2024
1 parent 5149c62 commit 99985c5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 68 deletions.
1 change: 0 additions & 1 deletion r3bbase/R3BEventHeader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
******************************************************************************/

#include "R3BEventHeader.h"
#include "R3BLogger.h"

ClassImp(R3BEventHeader)
12 changes: 4 additions & 8 deletions r3bbase/R3BEventHeaderPropagator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* or submit itself to any jurisdiction. *
******************************************************************************/

#include "FairLogger.h"
#include <FairRootManager.h>

#include "R3BEventHeaderPropagator.h"
Expand All @@ -23,11 +22,9 @@ R3BEventHeaderPropagator::R3BEventHeaderPropagator()
{
}

R3BEventHeaderPropagator::R3BEventHeaderPropagator(const TString& name, Int_t iVerbose, const TString& nameheader)
R3BEventHeaderPropagator::R3BEventHeaderPropagator(const TString& name, Int_t iVerbose, std::string_view nameheader)
: FairTask(name, iVerbose)
, fNameHeader(nameheader)
, fHeader(nullptr)
, fSource(nullptr)
{
}

Expand All @@ -39,21 +36,20 @@ InitStatus R3BEventHeaderPropagator::Init()
R3BLOG_IF(fatal, !fHeader, "EventHeader. not found.");
R3BLOG_IF(info, fHeader, "EventHeader. found.");

frm->Register(fNameHeader, "EventHeader", fHeader, kTRUE);
frm->Register(fNameHeader.data(), "EventHeader", fHeader, kTRUE);

fSource = frm->GetSource();
R3BLOG_IF(fatal, !fSource, "R3BFileSource not found.");

return kSUCCESS;
}

void R3BEventHeaderPropagator::Exec(Option_t*)
void R3BEventHeaderPropagator::Exec(Option_t* /*option*/)
{
if (fSource)
if (fSource != nullptr)
{
fHeader->SetRunId(fSource->GetRunId());
}
return;
}

ClassImp(R3BEventHeaderPropagator);
15 changes: 8 additions & 7 deletions r3bbase/R3BEventHeaderPropagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <Rtypes.h>

#include "R3BEventHeader.h"
class R3BFileSource;
class FairSource;

class R3BEventHeaderPropagator : public FairTask
Expand All @@ -36,7 +35,9 @@ class R3BEventHeaderPropagator : public FairTask
* @param name a name of the task.
* @param iVerbose a verbosity level.
*/
R3BEventHeaderPropagator(const TString& name, Int_t iVerbose = 1, const TString& nameheader = "EventHeader.");
explicit R3BEventHeaderPropagator(const TString& name,
Int_t iVerbose = 1,
std::string_view nameheader = "EventHeader.");

/**
* Method for task initialization.
Expand All @@ -51,15 +52,15 @@ class R3BEventHeaderPropagator : public FairTask
* Is called by the framework every time a new event is read.
* @param option an execution option.
*/
void Exec(Option_t*) override;
void Exec(Option_t* /*option*/) override;

private:
TString fNameHeader;
R3BEventHeader* fHeader;
FairSource* fSource;
std::string fNameHeader;
R3BEventHeader* fHeader = nullptr;
FairSource* fSource = nullptr;

public:
ClassDefOverride(R3BEventHeaderPropagator, 0)
ClassDefOverride(R3BEventHeaderPropagator, 1)
};

#endif // R3BEVENTHEADERPROPAGATOR_H
18 changes: 0 additions & 18 deletions r3bbase/data_monitor/R3BDataMonitor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@ namespace

namespace R3B
{
auto DataMonitor::get(const std::string& histName) -> TH1*
{
if (auto hist = histograms_.find(histName); hist != histograms_.end())
{
return hist->second.get();
}
throw R3B::logic_error(fmt::format("Histogram with the name {} doesn't exist!", histName));
}

auto DataMonitor::get_canvas(const std::string& histName) -> DataMonitorCanvas&
{
if (auto canvas = canvases_.find(histName); canvas != canvases_.end())
{
return canvas->second;
}
throw R3B::logic_error(fmt::format("Canvas with the name {} doesn't exist!", histName));
}

void DataMonitor::save_to_sink(std::string_view folderName, FairSink* sinkFile)
{
auto* hist_dir = get_hist_dir(sinkFile);
Expand Down
64 changes: 34 additions & 30 deletions r3bbase/data_monitor/R3BDataMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#pragma once

#include "R3BDataMonitorCanvas.h"
#include "R3BLogger.h"
#include "R3BShared.h"
#include <FairRun.h>
#include <R3BException.h>
Expand All @@ -32,13 +31,21 @@ namespace R3B
DataMonitor() = default;

template <typename... Args>
auto create_canvas(std::string_view canvas_name, std::string_view canvas_title, Args&&... args)
-> DataMonitorCanvas&;
[[nodiscard]] auto create_canvas(std::string_view canvas_name,
std::string_view canvas_title,
Args&&... args) -> DataMonitorCanvas&;

// Use R3B::make_hist to create unique_ptr<TH1>
auto add_hist(std::unique_ptr<TH1> hist) -> TH1* { return add_to_map(std::move(hist), histograms_); }
[[nodiscard]] auto add_hist(std::unique_ptr<TH1> hist) -> TH1*
{
return add_to_map(std::move(hist), histograms_);
}

template <typename Hist, typename... Args>
[[nodiscard]] auto add_hist(std::string_view histName, std::string_view histTitle, Args&&... args) -> Hist*;

template <typename GraphType>
auto add_graph(std::string_view graph_name, std::unique_ptr<GraphType> graph) -> GraphType*
[[nodiscard]] auto add_graph(std::string_view graph_name, std::unique_ptr<GraphType> graph) -> GraphType*
{
graph->SetName(graph_name.data());
return add_to_map(std::move(graph), graphs_);
Expand All @@ -50,24 +57,20 @@ namespace R3B
return add_graph(graph_name, std::make_unique<TGraph>(std::forward<Args>(args)...));
}

template <typename Hist, typename... Args>
auto add_hist(std::string_view histName, std::string_view histTitle, Args&&... args) -> Hist*;

// TODO: these methods should be removed
auto get(const std::string& histName) -> TH1*;
auto get_canvas(const std::string& histName) -> DataMonitorCanvas&;

// Save plots to FairSink
void save_to_sink(std::string_view folderName = "", FairSink* sinkFile = FairRun::Instance()->GetSink());
// Save plots to any root file
void save_to_file(std::string_view filename = "");

// Used for online monitoring
void draw_canvases();
void register_canvases(FairRun* run);
void reset_all_hists();
void save_to_file(std::string_view filename = "");

private:
std::string save_filename_{ "histograms_save" };
std::map<std::string, std::unique_ptr<TH1>> histograms_;
std::map<std::string, std::unique_ptr<TGraph>> graphs_;
// std::vector<std::unique_ptr<TGraph>> graphs_;
std::map<std::string, DataMonitorCanvas> canvases_;
static auto get_hist_dir(FairSink* sinkFile) -> TDirectory*;
void write_all(TDirectory* dir);
Expand All @@ -83,20 +86,6 @@ namespace R3B
}
};

template <typename... Args>
DataMonitorCanvas::DataMonitorCanvas(DataMonitor* monitor, Args&&... args)
: monitor_{ monitor }
, canvas_(std::make_unique<TCanvas>(std::forward<Args>(args)...))
{
}

template <int div_num, typename ElementType, typename... Args>
constexpr auto DataMonitorCanvas::add(Args&&... args) -> CanvasElement<ElementType>
{
static_assert(div_num > 0, "Division number of the histogram must be larger than 0!");
return add<ElementType>(div_num, std::forward<Args>(args)...);
}

template <typename ElementType, typename ContainerType>
auto DataMonitor::add_to_map(std::unique_ptr<ElementType> element, ContainerType& container) -> ElementType*
{
Expand All @@ -119,8 +108,9 @@ namespace R3B
}

template <typename... Args>
auto DataMonitor::create_canvas(std::string_view canvas_name, std::string_view canvas_title, Args&&... args)
-> DataMonitorCanvas&
auto DataMonitor::create_canvas(std::string_view canvas_name,
std::string_view canvas_title,
Args&&... args) -> DataMonitorCanvas&
{
if (canvases_.find(std::string{ canvas_name }) != canvases_.end())
{
Expand All @@ -132,6 +122,20 @@ namespace R3B
return it->second;
}

template <typename... Args>
DataMonitorCanvas::DataMonitorCanvas(DataMonitor* monitor, Args&&... args)
: monitor_{ monitor }
, canvas_(std::make_unique<TCanvas>(std::forward<Args>(args)...))
{
}

template <int div_num, typename ElementType, typename... Args>
constexpr auto DataMonitorCanvas::add(Args&&... args) -> CanvasElement<ElementType>
{
static_assert(div_num > 0, "Division number of the histogram must be larger than 0!");
return add<ElementType>(div_num, std::forward<Args>(args)...);
}

template <typename ElementType, typename... Args>
constexpr auto DataMonitorCanvas::add(int div_num, Args&&... args) -> CanvasElement<ElementType>
{
Expand Down
8 changes: 4 additions & 4 deletions r3bbase/data_monitor/R3BDataMonitorCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ namespace R3B
template <typename... Args>
explicit DataMonitorCanvas(DataMonitor* monitor, Args&&... args);

template <int div_num = 1, typename Hist, typename... Args>
[[nodiscard]] constexpr auto add(Args&&... args) -> CanvasElement<Hist>;
template <int div_num = 1, typename ElementType, typename... Args>
[[nodiscard]] constexpr auto add(Args&&... args) -> CanvasElement<ElementType>;

template <typename Hist, typename... Args>
[[nodiscard]] constexpr auto add(int div_num, Args&&... args) -> CanvasElement<Hist>;
template <typename ElementType, typename... Args>
[[nodiscard]] constexpr auto add(int div_num, Args&&... args) -> CanvasElement<ElementType>;

template <typename... Args>
void divide(Args&&... args)
Expand Down

0 comments on commit 99985c5

Please sign in to comment.