Geant4-11
G4PlotManager.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 02/06/2015 (ivana@ipno.in2p3.fr)
28
29#include "G4HnInformation.hh"
30#include "G4PlotManager.hh"
32#include "G4ios.hh"
33
34#if defined(TOOLS_USE_FREETYPE)
35#include "tools/sg/text_freetype"
36#include "tools/xml/xml_style"
37#include "tools/xml/wrap_viewplot_fonts_google_style"
38 //inlib/xml/viewplot.style file embeded in an inline function.
39#include "tools/font/lato_regular_ttf"
40#include "tools/font/roboto_bold_ttf"
41
42namespace {
43
44// from g4tools/test/viewplot.cpp
45//_____________________________________________________________________________
46void HD_style(tools::sg::plots& a_plots,float a_line_width) {
47 std::vector<tools::sg::plotter*> plotters;
48 a_plots.plotters(plotters);
49 tools_vforcit(tools::sg::plotter*,plotters,it) {
50 tools::sg::plotter* _plotter = *it;
51 _plotter->bins_style(0).line_width = a_line_width;
52 _plotter->inner_frame_style().line_width = a_line_width;
53 _plotter->grid_style().line_width = a_line_width;
54 _plotter->x_axis().line_style().width = a_line_width;
55 _plotter->x_axis().ticks_style().width = a_line_width;
56 _plotter->y_axis().line_style().width = a_line_width;
57 _plotter->y_axis().ticks_style().width = a_line_width;
58 _plotter->z_axis().line_style().width = a_line_width;
59 _plotter->z_axis().ticks_style().width = a_line_width;
60
61 // needed if font is hershey :
62 _plotter->title_style().line_width = a_line_width;
63 _plotter->infos_style().line_width = a_line_width;
64 _plotter->title_box_style().line_width = a_line_width;
65
66 _plotter->x_axis().labels_style().line_width = a_line_width;
67 _plotter->x_axis().mag_style().line_width = a_line_width;
68 _plotter->x_axis().title_style().line_width = a_line_width;
69
70 _plotter->y_axis().labels_style().line_width = a_line_width;
71 _plotter->y_axis().mag_style().line_width = a_line_width;
72 _plotter->y_axis().title_style().line_width = a_line_width;
73
74 _plotter->z_axis().labels_style().line_width = a_line_width;
75 _plotter->z_axis().mag_style().line_width = a_line_width;
76 _plotter->z_axis().title_style().line_width = a_line_width;
77 }
78}
79
80// from g4tools/test/viewplot.cpp
81//_____________________________________________________________________________
82void regions_style(tools::sg::plots& a_plots,float a_plotter_scale = 1) {
83 // Rescale some plotter parameters (for example margins) according to the number of regions.
84 // We assume that these parameters had been set previously according to one plot per page.
85 // Then this function must be applied after all the styles had been applied (because
86 // a plotting style may set these parameters).
87
88 float ww_wc = a_plots.width;
89 float wh_wc = a_plots.height;
90 float rw_wc = ww_wc/a_plots.cols;
91 float rh_wc = wh_wc/a_plots.rows;
92
93 float cooking = 1.2f; //if increased the data area is diminished.
94
95 float wfac = (rw_wc/ww_wc)*cooking;
96 float hfac = (rh_wc/wh_wc)*cooking;
97
98 float label_cooking = 1.6f; //if increased the labels are bigger.
99
100 if((a_plots.cols.value()>=4)&&(a_plots.cols.value()>a_plots.rows.value())) label_cooking = 0.9f;
101
102 float title_cooking = 1.1f; //extra title cooking.
103
104 a_plots.plotter_scale = a_plotter_scale;
105
106 std::vector<tools::sg::plotter*> plotters;
107 a_plots.plotters(plotters);
108 tools_vforcit(tools::sg::plotter*,plotters,it) {
109 tools::sg::plotter* _plotter = *it;
110
111 _plotter->left_margin = _plotter->left_margin * wfac;
112 _plotter->right_margin = _plotter->right_margin * wfac;
113 _plotter->bottom_margin = _plotter->bottom_margin * hfac;
114 _plotter->top_margin = _plotter->top_margin * hfac;
115
116 _plotter->x_axis().tick_length = _plotter->x_axis().tick_length * wfac;
117 _plotter->y_axis().tick_length = _plotter->y_axis().tick_length * hfac;
118
119 _plotter->title_to_axis = _plotter->title_to_axis * hfac;
120 _plotter->title_height = _plotter->title_height * hfac * title_cooking;
121
122 _plotter->x_axis().label_height = _plotter->x_axis().label_height * hfac * label_cooking;
123 _plotter->y_axis().label_height = _plotter->y_axis().label_height * hfac * label_cooking;
124
125 }
126}
127
128// from g4tools/test/viewplot.cpp
129//_____________________________________________________________________________
130bool load_embeded_styles(tools::xml::styles& a_styles) {
131 std::string ss;
132 unsigned int linen;
133 const char** lines = viewplot_fonts_google_style(linen);
134 for(unsigned int index=0;index<linen;index++) {
135 std::string s = lines[index];
136 tools::replace(s,"@@double_quote@@","\"");
137 tools::replace(s,"@@back_slash@@","\\");
138 ss += s + "\n";
139 }
140 return tools::xml::load_style_string(a_styles,ss);
141}
142
143}
144#endif
145
146using namespace G4Analysis;
147
148//
149// ctors, dtor
150//
151
152//_____________________________________________________________________________
154 : fState(state)
155{
156#if defined(TOOLS_USE_FREETYPE)
160 fState.Message(kVL1, "... using high resolution with Freetype fonts", "");
161 //Have vertical A4 :
162 // unsigned int ww = 2000; //to have better antialising on freetype fonts.
163 // float A4 = 29.7f/21.0f;
164 // unsigned int wh = (unsigned int)(float(ww)*A4*0.80);
165 static tools::sg::text_freetype ttf;
166 ttf.add_embedded_font(tools::sg::font_lato_regular_ttf(),tools::font::lato_regular_ttf);
167 ttf.add_embedded_font(tools::sg::font_roboto_bold_ttf(),tools::font::roboto_bold_ttf);
168 fViewer = std::make_unique<tools::viewplot>(G4cout, ttf,
173 fViewer->plots().view_border = false;
174 load_embeded_styles(fViewer->styles());
175 fViewer->styles().add_colormap("default",tools::sg::style_default_colormap());
176 fViewer->styles().add_colormap("ROOT",tools::sg::style_ROOT_colormap());
177#else
178 // cretae a viewer with default parameters
179 fState.Message(kVL1, "... using low resolution with Hershey fonts", "");
180 fViewer = std::make_unique<tools::viewplot>(G4cout,
185 fViewer->plots().view_border = false;
186#endif
187}
188
189//
190// private methods
191//
192
193//_____________________________________________________________________________
195{
196 fState.Message(kVL4, "write a page in", "plot file", fFileName);
197
198#if defined(TOOLS_USE_FREETYPE)
199 HD_style(fViewer->plots(), 5);
200 regions_style(fViewer->plots(), fPlotParameters.GetScale());
201#endif
202
203 G4bool result = fViewer->write_page();
204 if ( ! result ) {
205 Warn("Cannot write a page in the plot file " + fFileName,
206 fkClass, "WritePage");
207 }
208
209 // clear viewers plots
210 fViewer->plots().init_sg();
211 //it will recreate the sg::plotters and then reset the styles on new ones.
212
213 fState.Message(kVL3, "write a page in", "plot file", fFileName);
214
215 return result;
216}
217
218//
219// public methods
220//
221
222//_____________________________________________________________________________
224{
225 fState.Message(kVL4, "open", "plot file", fileName);
226
227 // Keep filename for logging
228 fFileName = fileName;
229
230 G4bool result = fViewer->open_file(fileName);
231 if ( ! result ) {
232 Warn("Cannot open plot file " + fileName, fkClass, "OpenFile");
233 }
234
235 fState.Message(kVL1, "open", "plot file", fileName);
236
237 return result;
238}
239
240//_____________________________________________________________________________
242{
243 fState.Message(kVL4, "close", "plot file", fFileName);
244
245 G4bool result = fViewer->close_file();
246 if ( ! result ) {
247 Warn("Cannot close the plot file", fkClass, "CloseFile");
248 }
249
250 fState.Message(kVL1, "close", "plot file", fFileName);
251
252 return result;
253}
static constexpr double s
Definition: G4SIunits.hh:154
bool G4bool
Definition: G4Types.hh:86
G4GLOB_DLL std::ostream G4cout
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4bool WritePage()
const G4AnalysisManagerState & fState
std::unique_ptr< tools::viewplot > fViewer
G4bool OpenFile(const G4String &fileName)
G4PlotParameters fPlotParameters
G4PlotManager()=delete
G4bool CloseFile()
G4String fFileName
static constexpr std::string_view fkClass
G4int GetWidth() const
G4int GetColumns() const
G4float GetScale() const
G4int GetHeight() const
G4int GetRows() const
constexpr G4int kVL1
constexpr G4int kVL3
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)