ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4PlotManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4PlotManager.cc
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"
31 #include "G4ios.hh"
32 
33 #if defined(TOOLS_USE_FREETYPE)
34 #include <tools/sg/text_freetype>
35 #include <tools/xml/xml_style>
36 #include <tools/xml/wrap_viewplot_fonts_google_style>
37  //inlib/xml/viewplot.style file embeded in an inline function.
38 
39 namespace {
40 
41 // from g4tools/test/viewplot.cpp
42 //_____________________________________________________________________________
43 void HD_style(tools::sg::plots& a_plots,float a_line_width) {
44  std::vector<tools::sg::plotter*> plotters;
45  a_plots.plotters(plotters);
46  tools_vforcit(tools::sg::plotter*,plotters,it) {
47  tools::sg::plotter* _plotter = *it;
48  _plotter->bins_style(0).line_width = a_line_width;
49  _plotter->inner_frame_style().line_width = a_line_width;
50  _plotter->grid_style().line_width = a_line_width;
51  _plotter->x_axis().line_style().width = a_line_width;
52  _plotter->x_axis().ticks_style().width = a_line_width;
53  _plotter->y_axis().line_style().width = a_line_width;
54  _plotter->y_axis().ticks_style().width = a_line_width;
55  _plotter->z_axis().line_style().width = a_line_width;
56  _plotter->z_axis().ticks_style().width = a_line_width;
57 
58  // needed if font is hershey :
59  _plotter->title_style().line_width = a_line_width;
60  _plotter->infos_style().line_width = a_line_width;
61  _plotter->title_box_style().line_width = a_line_width;
62 
63  _plotter->x_axis().labels_style().line_width = a_line_width;
64  _plotter->x_axis().mag_style().line_width = a_line_width;
65  _plotter->x_axis().title_style().line_width = a_line_width;
66 
67  _plotter->y_axis().labels_style().line_width = a_line_width;
68  _plotter->y_axis().mag_style().line_width = a_line_width;
69  _plotter->y_axis().title_style().line_width = a_line_width;
70 
71  _plotter->z_axis().labels_style().line_width = a_line_width;
72  _plotter->z_axis().mag_style().line_width = a_line_width;
73  _plotter->z_axis().title_style().line_width = a_line_width;
74  }
75 }
76 
77 // from g4tools/test/viewplot.cpp
78 //_____________________________________________________________________________
79 void regions_style(tools::sg::plots& a_plots,float a_plotter_scale = 1) {
80  // Rescale some plotter parameters (for example margins) according to the number of regions.
81  // We assume that these parameters had been set previously according to one plot per page.
82  // Then this function must be applied after all the styles had been applied (because
83  // a plotting style may set these parameters).
84 
85  float ww_wc = a_plots.width;
86  float wh_wc = a_plots.height;
87  float rw_wc = ww_wc/a_plots.cols;
88  float rh_wc = wh_wc/a_plots.rows;
89 
90  float cooking = 1.2f; //if increased the data area is diminished.
91 
92  float wfac = (rw_wc/ww_wc)*cooking;
93  float hfac = (rh_wc/wh_wc)*cooking;
94 
95  float label_cooking = 1.6f; //if increased the labels are bigger.
96 
97  if((a_plots.cols.value()>=4)&&(a_plots.cols.value()>a_plots.rows.value())) label_cooking = 0.9f;
98 
99  float title_cooking = 1.1f; //extra title cooking.
100 
101  a_plots.plotter_scale = a_plotter_scale;
102 
103  std::vector<tools::sg::plotter*> plotters;
104  a_plots.plotters(plotters);
105  tools_vforcit(tools::sg::plotter*,plotters,it) {
106  tools::sg::plotter* _plotter = *it;
107 
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 
125 // from g4tools/test/viewplot.cpp
126 //_____________________________________________________________________________
127 bool load_embeded_styles(tools::xml::styles& a_styles) {
128  std::string ss;
129  unsigned int linen;
130  const char** lines = viewplot_fonts_google_style(linen);
131  for(unsigned int index=0;index<linen;index++) {
132  std::string s = lines[index];
133  tools::replace(s,"@@double_quote@@","\"");
134  tools::replace(s,"@@back_slash@@","\\");
135  ss += s + "\n";
136  }
137  return tools::xml::load_style_string(a_styles,ss);
138 }
139 
140 }
141 #endif
142 
143 //
144 // static data
145 //
146 
147 //
148 // ctors, dtor
149 //
150 
151 //_____________________________________________________________________________
153  : fState(state),
154  fPlotParameters(),
155  fViewer(nullptr),
156  fFileName()
157 {
158 #if defined(TOOLS_USE_FREETYPE)
159 
160 
161 
162 #ifdef G4VERBOSE
163  if ( fState.GetVerboseL1() )
164  G4cout << "... using high resolution with Freetype fonts" << G4endl;
165 #endif
166  //Have vertical A4 :
167  // unsigned int ww = 2000; //to have better antialising on freetype fonts.
168  // float A4 = 29.7f/21.0f;
169  // unsigned int wh = (unsigned int)(float(ww)*A4*0.80);
170  static tools::sg::text_freetype ttf;
171  fViewer.reset(new tools::viewplot(G4cout, ttf,
176  fViewer->plots().view_border = false;
177  load_embeded_styles(fViewer->styles());
178  fViewer->styles().add_colormap("default",tools::sg::style_default_colormap());
179  fViewer->styles().add_colormap("ROOT",tools::sg::style_ROOT_colormap());
180 #else
181  // cretae a viewer with default parameters
182 #ifdef G4VERBOSE
183  if ( fState.GetVerboseL1() )
184  G4cout << "... using low resolution with Hershey fonts" << G4endl;
185 #endif
186  fViewer.reset(new tools::viewplot(G4cout,
191  fViewer->plots().view_border = false;
192 #endif
193 }
194 
195 //_____________________________________________________________________________
197 {}
198 
199 //
200 // private methods
201 //
202 
203 //_____________________________________________________________________________
205 {
206 #ifdef G4VERBOSE
207  if ( fState.GetVerboseL4() )
208  fState.GetVerboseL4()->Message("write a page in", "plot file", fFileName);
209 #endif
210 
211 #if defined(TOOLS_USE_FREETYPE)
212  HD_style(fViewer->plots(), 5);
213  regions_style(fViewer->plots(), fPlotParameters.GetScale());
214 #endif
215 
216  G4bool result = fViewer->write_page();
217  if ( ! result ) {
218  G4ExceptionDescription description;
219  description << " " << "Cannot write a page in the plot file " << fFileName;
220  G4Exception("G4PlotManager::WritePage()",
221  "Analysis_W022", JustWarning, description);
222  }
223 
224  // clear viewers plots
225  fViewer->plots().init_sg();
226  //it will recreate the sg::plotters and then reset the styles on new ones.
227 
228 #ifdef G4VERBOSE
229  if ( fState.GetVerboseL3() )
230  fState.GetVerboseL3()->Message("write a page in", "plot file", fFileName);
231 #endif
232 
233  return result;
234 }
235 
236 //
237 // public methods
238 //
239 
240 //_____________________________________________________________________________
242 {
243 #ifdef G4VERBOSE
244  if ( fState.GetVerboseL4() )
245  fState.GetVerboseL4()->Message("open", "plot file", fileName);
246 #endif
247 
248  // Keep filename for logging
249  fFileName = fileName;
250 
251  G4bool result = fViewer->open_file(fileName);
252  if ( ! result ) {
253  G4ExceptionDescription description;
254  description << " " << "Cannot open plot file " << fileName;
255  G4Exception("G4PlotManager::OpenFile()",
256  "Analysis_W001", JustWarning, description);
257  }
258 
259 #ifdef G4VERBOSE
260  if ( fState.GetVerboseL1() )
261  fState.GetVerboseL1()->Message("open", "plot file", fileName);
262 #endif
263 
264  return result;
265 }
266 
267 //_____________________________________________________________________________
269 {
270 #ifdef G4VERBOSE
271  if ( fState.GetVerboseL4() )
272  fState.GetVerboseL4()->Message("close", "plot file", fFileName);
273 #endif
274 
275  G4bool result = fViewer->close_file();
276  if ( ! result ) {
277  G4ExceptionDescription description;
278  description << " " << "Cannot close the plot file.";
279  G4Exception("G4PlotManager::CloseFile()",
280  "Analysis_W021", JustWarning, description);
281  }
282 
283 #ifdef G4VERBOSE
284  if ( fState.GetVerboseL1() )
285  fState.GetVerboseL1()->Message("close", "plot file", fFileName);
286 #endif
287 
288  return result;
289 }