Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // $Id: G4PlotManager.cc 85314 2014-10-27 14:33:08Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 02/06/2015 (ivana@ipno.in2p3.fr)
29 
30 #include "G4HnInformation.hh"
31 #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_style>
38  //inlib/xml/viewplot.style file embeded in an inline function.
39 
40 namespace {
41 
42 // from g4tools/test/viewplot.cpp
43 //_____________________________________________________________________________
44 void HD_style(tools::sg::plots& a_plots,float a_line_width) {
45  std::vector<tools::sg::plotter*> plotters;
46  a_plots.plotters(plotters);
47  tools_vforcit(tools::sg::plotter*,plotters,it) {
48  tools::sg::plotter* _plotter = *it;
49  _plotter->bins_style(0).line_width = a_line_width;
50  _plotter->inner_frame_style().line_width = a_line_width;
51  _plotter->grid_style().line_width = a_line_width;
52  _plotter->x_axis().line_style().width = a_line_width;
53  _plotter->x_axis().ticks_style().width = a_line_width;
54  _plotter->y_axis().line_style().width = a_line_width;
55  _plotter->y_axis().ticks_style().width = a_line_width;
56  _plotter->z_axis().line_style().width = a_line_width;
57  _plotter->z_axis().ticks_style().width = a_line_width;
58 
59  // needed if font is hershey :
60  _plotter->title_style().line_width = a_line_width;
61  _plotter->infos_style().line_width = a_line_width;
62  _plotter->title_box_style().line_width = a_line_width;
63 
64  _plotter->x_axis().labels_style().line_width = a_line_width;
65  _plotter->x_axis().mag_style().line_width = a_line_width;
66  _plotter->x_axis().title_style().line_width = a_line_width;
67 
68  _plotter->y_axis().labels_style().line_width = a_line_width;
69  _plotter->y_axis().mag_style().line_width = a_line_width;
70  _plotter->y_axis().title_style().line_width = a_line_width;
71 
72  _plotter->z_axis().labels_style().line_width = a_line_width;
73  _plotter->z_axis().mag_style().line_width = a_line_width;
74  _plotter->z_axis().title_style().line_width = a_line_width;
75  }
76 }
77 
78 // from g4tools/test/viewplot.cpp
79 //_____________________________________________________________________________
80 void regions_style(tools::sg::plots& a_plots,float a_plotter_scale = 1) {
81  // Rescale some plotter parameters (for example margins) according to the number of regions.
82  // We assume that these parameters had been set previously according to one plot per page.
83  // Then this function must be applied after all the styles had been applied (because
84  // a plotting style may set these parameters).
85 
86  float ww_wc = a_plots.width;
87  float wh_wc = a_plots.height;
88  float rw_wc = ww_wc/a_plots.cols;
89  float rh_wc = wh_wc/a_plots.rows;
90 
91  float cooking = 1.2f; //if increased the data area is diminished.
92 
93  float wfac = (rw_wc/ww_wc)*cooking;
94  float hfac = (rh_wc/wh_wc)*cooking;
95 
96  float label_cooking = 1.6f; //if increased the labels are bigger.
97 
98  if((a_plots.cols.value()>=4)&&(a_plots.cols.value()>a_plots.rows.value())) label_cooking = 0.9f;
99 
100  float title_cooking = 1.1f; //extra title cooking.
101 
102  a_plots.plotter_scale = a_plotter_scale;
103 
104  std::vector<tools::sg::plotter*> plotters;
105  a_plots.plotters(plotters);
106  tools_vforcit(tools::sg::plotter*,plotters,it) {
107  tools::sg::plotter* _plotter = *it;
108 
109  _plotter->left_margin = _plotter->left_margin * wfac;
110  _plotter->right_margin = _plotter->right_margin * wfac;
111  _plotter->bottom_margin = _plotter->bottom_margin * hfac;
112  _plotter->top_margin = _plotter->top_margin * hfac;
113 
114  _plotter->x_axis().tick_length = _plotter->x_axis().tick_length * wfac;
115  _plotter->y_axis().tick_length = _plotter->y_axis().tick_length * hfac;
116 
117  _plotter->title_to_axis = _plotter->title_to_axis * hfac;
118  _plotter->title_height = _plotter->title_height * hfac * title_cooking;
119 
120  _plotter->x_axis().label_height = _plotter->x_axis().label_height * hfac * label_cooking;
121  _plotter->y_axis().label_height = _plotter->y_axis().label_height * hfac * label_cooking;
122 
123  }
124 }
125 
126 // from g4tools/test/viewplot.cpp
127 //_____________________________________________________________________________
128 bool load_embeded_styles(tools::xml::styles& a_styles) {
129  std::string ss;
130  unsigned int linen;
131  const char** lines = viewplot_style(linen);
132  for(unsigned int index=0;index<linen;index++) {
133  std::string s = lines[index];
134  tools::replace(s,"@@double_quote@@","\"");
135  tools::replace(s,"@@back_slash@@","\\");
136  ss += s + "\n";
137  }
138  return tools::xml::load_style_string(a_styles,ss);
139 }
140 
141 }
142 #endif
143 
144 //
145 // static data
146 //
147 
148 //_____________________________________________________________________________
149 G4PlotParameters G4PlotManager::fgPlotParameters;
150 
151 //
152 // ctors, dtor
153 //
154 
155 //_____________________________________________________________________________
157  : fState(state),
158  fViewer(nullptr),
159  fFileName()
160 {
161 #if defined(TOOLS_USE_FREETYPE)
162 #ifdef G4VERBOSE
166  if ( fState.GetVerboseL1() )
167  G4cout << "... using high resolution with Freetype fonts" << G4endl;
168 #endif
169  //Have vertical A4 :
170  // unsigned int ww = 2000; //to have better antialising on freetype fonts.
171  // float A4 = 29.7f/21.0f;
172  // unsigned int wh = (unsigned int)(float(ww)*A4*0.80);
173  static tools::sg::text_freetype ttf;
174  fViewer.reset(new tools::viewplot(G4cout, ttf,
175  fgPlotParameters.GetColumns(),
176  fgPlotParameters.GetRows(),
177  fgPlotParameters.GetWidth(),
178  fgPlotParameters.GetHeight()));
179  fViewer->plots().view_border = false;
180  load_embeded_styles(fViewer->styles());
181  fViewer->styles().add_colormap("default",tools::sg::style_default_colormap());
182  fViewer->styles().add_colormap("ROOT",tools::sg::style_ROOT_colormap());
183 #else
184  // cretae a viewer with default parameters
185 #ifdef G4VERBOSE
186  if ( fState.GetVerboseL1() )
187  G4cout << "... using low resolution with Hershey fonts" << G4endl;
188 #endif
189  fViewer.reset(new tools::viewplot(G4cout,
190  fgPlotParameters.GetColumns(),
191  fgPlotParameters.GetRows(),
192  fgPlotParameters.GetWidth(),
193  fgPlotParameters.GetHeight()));
194  fViewer->plots().view_border = false;
195 #endif
196 }
197 
198 //_____________________________________________________________________________
200 {}
201 
202 //
203 // private methods
204 //
205 
206 //_____________________________________________________________________________
207 G4bool G4PlotManager::WritePage()
208 {
209 #ifdef G4VERBOSE
210  if ( fState.GetVerboseL4() )
211  fState.GetVerboseL4()->Message("write a page in", "plot file", fFileName);
212 #endif
213 
214 #if defined(TOOLS_USE_FREETYPE)
215  HD_style(fViewer->plots(), 5);
216  regions_style(fViewer->plots(), fgPlotParameters.GetScale());
217 #endif
218 
219  G4bool result = fViewer->write_page();
220  if ( ! result ) {
221  G4ExceptionDescription description;
222  description << " " << "Cannot write a page in the plot file " << fFileName;
223  G4Exception("G4PlotManager::WritePage()",
224  "Analysis_W022", JustWarning, description);
225  }
226 
227  // clear viewers plots
228  fViewer->plots().init_sg();
229  //it will recreate the sg::plotters and then reset the styles on new ones.
230 
231 #ifdef G4VERBOSE
232  if ( fState.GetVerboseL3() )
233  fState.GetVerboseL3()->Message("write a page in", "plot file", fFileName);
234 #endif
235 
236  return result;
237 }
238 
239 //
240 // public methods
241 //
242 
243 //_____________________________________________________________________________
245 {
246 #ifdef G4VERBOSE
247  if ( fState.GetVerboseL4() )
248  fState.GetVerboseL4()->Message("open", "plot file", fileName);
249 #endif
250 
251  // Keep filename for logging
252  fFileName = fileName;
253 
254  G4bool result = fViewer->open_file(fileName);
255  if ( ! result ) {
256  G4ExceptionDescription description;
257  description << " " << "Cannot open plot file " << fileName;
258  G4Exception("G4PlotManager::OpenFile()",
259  "Analysis_W001", JustWarning, description);
260  }
261 
262 #ifdef G4VERBOSE
263  if ( fState.GetVerboseL1() )
264  fState.GetVerboseL1()->Message("open", "plot file", fileName);
265 #endif
266 
267  return result;
268 }
269 
270 //_____________________________________________________________________________
272 {
273 #ifdef G4VERBOSE
274  if ( fState.GetVerboseL4() )
275  fState.GetVerboseL4()->Message("close", "plot file", fFileName);
276 #endif
277 
278  G4bool result = fViewer->close_file();
279  if ( ! result ) {
280  G4ExceptionDescription description;
281  description << " " << "Cannot close the plot file.";
282  G4Exception("G4PlotManager::CloseFile()",
283  "Analysis_W021", JustWarning, description);
284  }
285 
286 #ifdef G4VERBOSE
287  if ( fState.GetVerboseL1() )
288  fState.GetVerboseL1()->Message("close", "plot file", fFileName);
289 #endif
290 
291  return result;
292 }
G4double G4ParticleHPJENDLHEData::G4double result
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4int GetWidth() const
G4PlotManager(const G4AnalysisManagerState &state)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4int GetRows() const
const XML_Char * s
Definition: expat.h:262
const G4AnalysisVerbose * GetVerboseL3() const
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
G4float GetScale() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int GetColumns() const
#define G4endl
Definition: G4ios.hh:61
G4bool CloseFile()
G4bool OpenFile(const G4String &fileName)
const G4AnalysisVerbose * GetVerboseL1() const
G4int GetHeight() const