CbmRoot
Loading...
Searching...
No Matches
CbmQaReportBuilder.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#include "CbmQaReportBuilder.h"
11
12#include "CbmQaReportFigure.h"
13#include "Logger.h"
14#include "TCanvas.h"
15
16#include <fstream>
17
18
24
25namespace fs = cbm::qa::report::fs;
26
27// ---------------------------------------------------------------------------------------------------------------------
28//
29Builder::Builder(std::string_view title, fs::path outPath)
30 : fpHeader(std::make_shared<Header>())
31 , fpTail(std::make_shared<Tail>())
32{
33 fScriptsPath = fs::weakly_canonical(outPath);
34 fFiguresPath = fs::weakly_canonical(fScriptsPath / "figures");
35 fs::create_directories(fScriptsPath);
36 fs::create_directories(fFiguresPath);
37
38 LOG(info) << "Initializing report builder for a document " << title << ". The scripts path is "
39 << fScriptsPath.string();
40 fpHeader->SetTitle(title);
41}
42
43// ---------------------------------------------------------------------------------------------------------------------
44//
45void Builder::CreateScript(Engine& engine, bool bCompile)
46{
47 std::string outName = (fScriptsPath / (fmt::format("{}.{}", ksSourceName, engine.ScriptExtention()))).string();
48 LOG(info) << "\nCreating source script \"" << outName << "\" using " << engine.MyName();
49
50 std::ofstream script(outName);
51
52 script << fpHeader->GetBody(engine);
53 for (const auto& pSection : fvpSections) {
54 script << pSection->GetBody(engine);
55 }
56 script << fpTail->GetBody(engine);
57 script.close();
58
59 if (bCompile) {
60 engine.Compile(outName);
61 }
62}
63
64// ---------------------------------------------------------------------------------------------------------------------
65//
66std::string Builder::SaveCanvas(const TCanvas* canv, const std::string& relPath) const
67{
68 if (!canv) {
69 LOG(fatal) << "cbm::qa::report::Builder::SaveCanvas(): a nullptr canvas is passed to the function. The provided "
70 << "relative path is \"" << relPath << "\"";
71 }
72 fs::path absPath = this->AbsFigurePath(relPath + "." + fsFigureExtention);
73 fs::create_directories(absPath.parent_path());
74 canv->SaveAs(absPath.string().c_str());
75 return this->PathInSource(absPath);
76}
Base class for the report builder (implementation)
Base class for the report figure (header)
std::string fsFigureExtention
Figure extention.
void CreateScript(Engine &engine, bool bCompile=true)
Saves script.
std::string SaveCanvas(const TCanvas *canv, const std::string &relPath) const
Saves canvas.
std::string AbsFigurePath(fs::path relPath) const
Returns absolute path to the figure.
fs::path fFiguresPath
Figures path.
std::shared_ptr< Tail > fpTail
Tail of the report.
fs::path fScriptsPath
Scripts path.
static constexpr std::string_view ksSourceName
Name of source.
std::vector< std::shared_ptr< Section > > fvpSections
List of report sections.
std::shared_ptr< Header > fpHeader
Header of the report.
Builder(std::string_view title, fs::path outPath)
Constructor.
std::string PathInSource(fs::path path) const
Converts path of the object in the code to one in the document source.
A base abstract class to provide an interface for element body (a visitor in the Visitor pattern)
virtual std::string ScriptExtention() const =0
Returns script extention.
virtual void Compile(const std::string &) const
Defines the compilation rule (can be omitted)
virtual std::string MyName() const =0
Returns engine name.
Figure in the report.
Header of the report.
Tail of the report.
Hash for CbmL1LinkKey.