Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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 "toolx/sg/text_freetype"
36#include "toolx/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 for (auto* _plotter : plotters) {
50 _plotter->bins_style(0).line_width = a_line_width;
51 _plotter->inner_frame_style().line_width = a_line_width;
52 _plotter->grid_style().line_width = a_line_width;
53 _plotter->x_axis().line_style().width = a_line_width;
54 _plotter->x_axis().ticks_style().width = a_line_width;
55 _plotter->y_axis().line_style().width = a_line_width;
56 _plotter->y_axis().ticks_style().width = a_line_width;
57 _plotter->z_axis().line_style().width = a_line_width;
58 _plotter->z_axis().ticks_style().width = a_line_width;
59
60 // needed if font is hershey :
61 _plotter->title_style().line_width = a_line_width;
62 _plotter->infos_style().line_width = a_line_width;
63 _plotter->title_box_style().line_width = a_line_width;
64
65 _plotter->x_axis().labels_style().line_width = a_line_width;
66 _plotter->x_axis().mag_style().line_width = a_line_width;
67 _plotter->x_axis().title_style().line_width = a_line_width;
68
69 _plotter->y_axis().labels_style().line_width = a_line_width;
70 _plotter->y_axis().mag_style().line_width = a_line_width;
71 _plotter->y_axis().title_style().line_width = a_line_width;
72
73 _plotter->z_axis().labels_style().line_width = a_line_width;
74 _plotter->z_axis().mag_style().line_width = a_line_width;
75 _plotter->z_axis().title_style().line_width = a_line_width;
76 }
77}
78
79// from g4tools/test/viewplot.cpp
80//_____________________________________________________________________________
81void regions_style(tools::sg::plots& a_plots,float a_plotter_scale = 1) {
82 // Rescale some plotter parameters (for example margins) according to the number of regions.
83 // We assume that these parameters had been set previously according to one plot per page.
84 // Then this function must be applied after all the styles had been applied (because
85 // a plotting style may set these parameters).
86
87 float ww_wc = a_plots.width;
88 float wh_wc = a_plots.height;
89 float rw_wc = ww_wc/a_plots.cols;
90 float rh_wc = wh_wc/a_plots.rows;
91
92 float cooking = 1.2f; //if increased the data area is diminished.
93
94 float wfac = (rw_wc/ww_wc)*cooking;
95 float hfac = (rh_wc/wh_wc)*cooking;
96
97 float label_cooking = 1.6f; //if increased the labels are bigger.
98
99 if((a_plots.cols.value()>=4)&&(a_plots.cols.value()>a_plots.rows.value())) label_cooking = 0.9f;
100
101 float title_cooking = 1.1f; //extra title cooking.
102
103 a_plots.plotter_scale = a_plotter_scale;
104
105 std::vector<tools::sg::plotter*> plotters;
106 a_plots.plotters(plotters);
107 for (auto* _plotter : plotters) {
108 _plotter->left_margin = _plotter->left_margin * wfac;
109 _plotter->right_margin = _plotter->right_margin * wfac;
110 _plotter->bottom_margin = _plotter->bottom_margin * hfac;
111 _plotter->top_margin = _plotter->top_margin * hfac;
112
113 _plotter->x_axis().tick_length = _plotter->x_axis().tick_length * wfac;
114 _plotter->y_axis().tick_length = _plotter->y_axis().tick_length * hfac;
115
116 _plotter->title_to_axis = _plotter->title_to_axis * hfac;
117 _plotter->title_height = _plotter->title_height * hfac * title_cooking;
118
119 _plotter->x_axis().label_height = _plotter->x_axis().label_height * hfac * label_cooking;
120 _plotter->y_axis().label_height = _plotter->y_axis().label_height * hfac * label_cooking;
121 }
122}
123
124// from g4tools/test/viewplot.cpp
125//_____________________________________________________________________________
126bool load_embeded_styles(tools::xml::styles& a_styles) {
127 std::string ss;
128 unsigned int linen;
129 const char** lines = viewplot_fonts_google_style(linen);
130 for(unsigned int index=0;index<linen;index++) {
131 std::string s = lines[index];
132 tools::replace(s,"@@double_quote@@","\"");
133 tools::replace(s,"@@back_slash@@","\\");
134 ss += s + "\n";
135 }
136 return toolx::xml::load_style_string(a_styles,ss);
137}
138
139}
140#endif
141
142using namespace G4Analysis;
143
144//
145// ctors, dtor
146//
147
148//_____________________________________________________________________________
150 : fState(state)
151{
152#if defined(TOOLS_USE_FREETYPE)
153 //////////////////////////////////////////////////////////////////////////////
154 /// plotting, high resolution with freetype fonts and by using styles : //////
155 //////////////////////////////////////////////////////////////////////////////
156 fState.Message(kVL1, "... using high resolution with Freetype fonts", "");
157 //Have vertical A4 :
158 // unsigned int ww = 2000; //to have better antialising on freetype fonts.
159 // float A4 = 29.7f/21.0f;
160 // unsigned int wh = (unsigned int)(float(ww)*A4*0.80);
161 static toolx::sg::text_freetype ttf;
162 ttf.add_embedded_font(tools::sg::font_lato_regular_ttf(),tools::font::lato_regular_ttf);
163 ttf.add_embedded_font(tools::sg::font_roboto_bold_ttf(),tools::font::roboto_bold_ttf);
164 fViewer = std::make_unique<tools::viewplot>(G4cout, ttf,
165 fPlotParameters.GetColumns(),
166 fPlotParameters.GetRows(),
167 fPlotParameters.GetWidth(),
168 fPlotParameters.GetHeight());
169 fViewer->plots().view_border = false;
170 load_embeded_styles(fViewer->styles());
171 fViewer->styles().add_colormap("default",tools::sg::style_default_colormap());
172 fViewer->styles().add_colormap("ROOT",tools::sg::style_ROOT_colormap());
173#else
174 // cretae a viewer with default parameters
175 fState.Message(kVL1, "... using low resolution with Hershey fonts", "");
176 fViewer = std::make_unique<tools::viewplot>(G4cout,
177 fPlotParameters.GetColumns(),
178 fPlotParameters.GetRows(),
179 fPlotParameters.GetWidth(),
180 fPlotParameters.GetHeight());
181 fViewer->plots().view_border = false;
182#endif
183}
184
185//
186// private methods
187//
188
189//_____________________________________________________________________________
190G4bool G4PlotManager::WritePage()
191{
192 fState.Message(kVL4, "write a page in", "plot file", fFileName);
193
194#if defined(TOOLS_USE_FREETYPE)
195 HD_style(fViewer->plots(), 5);
196 regions_style(fViewer->plots(), fPlotParameters.GetScale());
197#endif
198
199 G4bool result = fViewer->write_page();
200 if ( ! result ) {
201 Warn("Cannot write a page in the plot file " + fFileName,
202 fkClass, "WritePage");
203 }
204
205 // clear viewers plots
206 fViewer->plots().init_sg();
207 //it will recreate the sg::plotters and then reset the styles on new ones.
208
209 fState.Message(kVL3, "write a page in", "plot file", fFileName);
210
211 return result;
212}
213
214//
215// public methods
216//
217
218//_____________________________________________________________________________
220{
221 fState.Message(kVL4, "open", "plot file", fileName);
222
223 // Keep filename for logging
224 fFileName = fileName;
225
226 G4bool result = fViewer->open_file(fileName);
227 if ( ! result ) {
228 Warn("Cannot open plot file " + fileName, fkClass, "OpenFile");
229 }
230
231 fState.Message(kVL1, "open", "plot file", fileName);
232
233 return result;
234}
235
236//_____________________________________________________________________________
238{
239 fState.Message(kVL4, "close", "plot file", fFileName);
240
241 G4bool result = fViewer->close_file();
242 if ( ! result ) {
243 Warn("Cannot close the plot file", fkClass, "CloseFile");
244 }
245
246 fState.Message(kVL1, "close", "plot file", fFileName);
247
248 return result;
249}
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 OpenFile(const G4String &fileName)
G4PlotManager()=delete
G4float GetScale() 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)