source: trunk/documents/UserDoc/DocBookUsersGuides/ForApplicationDeveloper/xml/Visualization/visexecutable.xml @ 905

Last change on this file since 905 was 905, checked in by garnier, 16 years ago

update

File size: 14.5 KB
Line 
1<!-- ******************************************************** -->
2<!--                                                          -->
3<!--  [History]                                               -->
4<!--    Converted to DocBook: Katsuya Amako, Aug-2006         -->
5<!--                                                          -->
6<!-- ******************************************************** -->
7
8
9<!-- ******************* Section (Level#1) ****************** -->
10<sect1 id="sect.VisAddExe">
11<title>
12Adding Visualization to Your Executable
13</title>
14
15<para>
16This section explains how to incorporate your selected
17visualization drivers into the <literal>main()</literal> function and create
18an executable for it. In order to perform visualization with your
19Geant4 executable, you must compile it with realized visualization
20driver(s). You may be dazzled by the number of choices of
21visualization driver, but you need not use all of them at one time.
22</para>
23
24<!-- ******************* Section (Level#2) ****************** -->
25<sect2 id="sect.VisAddExe.InstDrv">
26<title>
27Installing Visualization Drivers
28</title>
29
30<para>
31Depending on what has been installed on your system, several kinds
32of visualization driver are available. One or many drivers may be
33chosen for realization in compilation, depending on your
34visualization requirements. Features and notes on each driver are
35briefly described in <xref linkend="sect.VisDrv" />
36"<emphasis role="bold">Visualization Drivers</emphasis>",
37along with links to detailed web pages for the various drivers.
38</para>
39
40<para>
41Note that not all drivers can be installed on all systems;
42<xref linkend="table.VisDrv_1" /> in
43<xref linkend="sect.VisDrv" /> lists all the available drivers
44and the platforms on which they can be installed.
45For any of the visualization drivers to work, the corresponding
46graphics system must be installed beforehand.
47</para>
48
49<para>
50Unless the environment variable <literal>G4VIS_NONE</literal> is set to
51"1", visualization drivers that do not depend on external libraries
52are automatically incorporated into Geant4 libraries during their
53installation. (Here "installation of Geant4 libraries" means the
54generation of Geant4 libraries by compilation.) The automatically
55incorporated visualization drivers are: DAWNFILE, HepRepFile,
56HepRepXML, RayTracer, VRML1FILE, VRML2FILE and ATree and
57GAGTree.
58</para>
59
60<para>
61The OpenGL, Qt, OpenInventor and RayTracerX drivers are not
62incorporated by default. Nor are the DAWN-Network and VRML-Network
63drivers, because they require the network setting of the installed
64machine. In order to incorporate them, the environment variables
65"<literal>G4VIS_BUILD_DRIVERNAME_DRIVER</literal>" should be set to
66"<literal>1</literal>" before installing the Geant4 libraries:
67
68<informalexample>
69<programlisting>
70     setenv G4VIS_BUILD_OPENGLX_DRIVER      1  # OpenGL-Xlib driver
71     setenv G4VIS_BUILD_OPENGLXM_DRIVER     1  # OpenGL-Motif driver
72     setenv G4VIS_BUILD_OPENGLQT_DRIVER     1  # Qt driver
73     setenv G4VIS_BUILD_OIX_DRIVER          1  # OpenInventor-Xlib driver
74     setenv G4VIS_BUILD_RAYTRACERX_DRIVER   1  # RayTracer-XLib driver
75     setenv G4VIS_BUILD_DAWN_DRIVER         1  # DAWN-Network driver
76     setenv G4VIS_BUILD_VRML_DRIVER         1  # VRML-Network
77</programlisting>
78</informalexample>
79</para>
80
81<para>
82Unless the environment variable <literal>G4VIS_NONE</literal> is set to "1",
83setting any of the above variables sets a C-pre-processor flag of
84the same name. Also the C-pre-processor flag <literal>G4VIS_BUILD</literal>
85is set (see config/G4VIS_BUILD.gmk), which incorparates the
86selected driver into the Geant4 libraries.
87</para>
88
89</sect2>
90
91<!-- ******************* Section (Level#2) ****************** -->
92<sect2 id="sect.VisAddExe.RealVisDrv">
93<title>
94How to Realize Visualization Drivers in an Executable
95</title>
96
97<para>
98You can realize and use any of the visualization driver(s) you want
99in your Geant4 executable, provided they are among the set
100installed beforehand into the Geant4 libraries. A warning will
101appear if this is not the case.
102</para>
103
104<para>
105In order to realize visualization drivers, you must instantiate
106and initialize a subclass of <literal>G4VisManager</literal> that implements
107the pure virtual function <literal>RegisterGraphicsSystems()</literal>. This
108subclass must be compiled in the user's domain to force the loading
109of appropriate libraries in the right order. The easiest way to do
110this is to use <literal>G4VisExecutive</literal>, a provided class with
111included implementation. <literal>G4VisExecutive</literal> is sensitive to
112the <literal>G4VIS_USE...</literal> variables mentioned below.
113</para>
114
115<para>
116If you do wish to write your own subclass, you may do so. You
117will see how to do this by looking at <literal>G4VisExecutive.icc</literal>.
118A typical extract is:
119
120<informalexample>
121<programlisting>
122     ...
123       RegisterGraphicsSystem (new G4DAWNFILE);
124     ...
125     #ifdef G4VIS_USE_OPENGLX
126       RegisterGraphicsSystem (new G4OpenGLImmediateX);
127       RegisterGraphicsSystem (new G4OpenGLStoredX);
128     #endif
129     ...
130</programlisting>
131</informalexample>
132</para>
133
134<para>
135If you wish to use <literal>G4VisExecutive</literal> but register an
136additional graphics system, <literal>XXX</literal> say, you may do so either
137before or after initializing:
138
139<informalexample>
140<programlisting>
141     visManager-&gt;RegisterGraphicsSytem(new XXX);
142     visManager-&gt;Initialize();
143</programlisting>
144</informalexample>
145</para>
146
147<para>
148By default, you get the DAWNFILE, HepRepFile, RayTracer,
149VRML1FILE, VRML2FILE, ATree and GAGTree drivers. Additionally, you
150may choose from the OpenGL-Xlib, OpenGL-Motif, Qt, OpenInventor,
151RayTracerX, DAWN-Network and VRML-Network drivers, each of which
152can be selected by setting the proper environment variable:
153
154<informalexample>
155<programlisting>
156     setenv G4VIS_USE_OPENGLX      1
157     setenv G4VIS_USE_OPENGLXM     1
158     setenv G4VIS_USE_OPENGLQT     1
159     setenv G4VIS_USE_OIX          1
160     setenv G4VIS_USE_RAYTRACERX   1
161     setenv G4VIS_USE_DAWN         1
162     setenv G4VIS_USE_VRML         1
163</programlisting>
164</informalexample>
165</para>
166
167<para>
168(Of course, this has to be chosen from the set incorporated into
169the Geant4 libraries during their compilation.) Unless the
170environment variable <literal>G4VIS_NONE</literal> is set, these set
171C-pre-processor flags of the same name.
172</para>
173
174<para>
175Also, unless the environment variable <literal>G4VIS_NONE</literal> is
176set, the C-pre-processor flag <literal>G4VIS_USE</literal> is always set by
177default. This flag is available in describing the <literal>main()</literal>
178function.
179</para>
180
181<para>
182You may have to set additional environment variables for your
183selected visualization drivers and graphics systems. For example,
184the OpenGL driver may require the setting of <literal>OGLHOME</literal> which
185points to the location of the OpenGL libraries. For more details,
186see <xref linkend="sect.VisDrv" /> 
187"<emphasis role="bold">Visualization Drivers</emphasis>" and
188pages linked from there.
189</para>
190
191</sect2>
192
193
194<!-- ******************* Section (Level#2) ****************** -->
195<sect2 id="sect.VisAddExe.SampSetup">
196<title>
197A Sample Set-up File
198</title>
199
200<para>
201The following can be part of the user's <literal>.cshrc</literal> or
202<literal>.tcshrc</literal> file on a Linux platform to configure the
203environment for creating Geant4 executables available for Geant4
204visualization. This sample is shown only as an example; it may NOT
205correspond to a specific platform configuration and does NOT apply
206in general for any specific setup !
207
208<example id="programlist_VisAddExe_1">
209<title>
210Part of a sample <literal>.cshrc</literal> setup file for the Linux platform.
211</title>
212
213<programlisting>
214 ############################################################
215 # Main Environment Variables for GEANT4 with Visualization #
216 ############################################################
217
218 ### Platform
219 setenv G4SYSTEM Linux-g++
220
221 ### CLHEP base directory
222 setenv CLHEP_BASE_DIR /opt/local
223
224 ### OpenGL base directory
225 setenv OGLHOME /usr/X11R6
226 
227 ### G4VIS_BUILD
228 ###   Incorporation of OpenGL-Xlib and OpenGL-Motif drivers
229 ###   into Geant4 libraries.
230 setenv G4VIS_BUILD_OPENGLX_DRIVER 1
231 setenv G4VIS_BUILD_OPENGLXM_DRIVER 1
232
233 ### G4VIS_USE
234 ###   Incorporation of OpenGL-Xlib and OpenGL-Motif drivers
235 ###   into Geant4 executables.
236 setenv G4VIS_USE_OPENGLX       1
237 setenv G4VIS_USE_OPENGLXM      1
238
239 ### Viewer for DAWNFILE driver
240 ### Default value is "dawn".  You can change it to, say,
241 ### "david" for volume overlapping tests
242 # setenv G4DAWNFILE_VIEWER david
243
244 ### Viewer for VRMLFILE drivers
245 setenv G4VRMLFILE_VIEWER vrmlview
246
247 ########## end
248</programlisting>
249</example>
250</para>
251
252</sect2>
253
254
255<!-- ******************* Section (Level#2) ****************** -->
256<sect2 id="sect.VisAddExe.VisMan">
257<title>
258Visualization Manager
259</title>
260
261<para>
262Visualization procedures are controlled by the "Visualization
263Manager", a class which must inherit from <emphasis>G4VisManager</emphasis>
264defined in the visualization category. Most users will find that
265they can just use the default visualization manager,
266<emphasis>G4VisExecutive</emphasis>. The Visualization Manager accepts users'
267requests for visualization, processes them, and passes the
268processed requirements to the abstract interface, i.e., to the
269currently selected visualization driver.
270</para>
271
272</sect2>
273
274
275<!-- ******************* Section (Level#2) ****************** -->
276<sect2 id="sect.VisAddExe.WrtMain">
277<title>
278How to Write the <literal>main()</literal> Function
279</title>
280
281<para>
282In order for your Geant4 executable to perform visualization, you
283must instantiate and initialize "your" Visualization Manager in the
284<literal>main()</literal> function. The core of the Visualization Manager is
285the class <literal>G4VisManager</literal>, defined in the visualization
286category. This class requires that one pure virtual function be
287implemented, namely, <literal>void RegisterGraphicsSystems()</literal>. The
288easiest way to do this is to use <literal>G4VisExecutive</literal>, as
289described above (but you may write your own class - see above).
290</para>
291
292<para>
293<xref linkend="programlist_VisAddExe_2" /> shows the form of the
294<literal>main()</literal> function.
295
296<example id="programlist_VisAddExe_2">
297<title>
298The form of the <literal>main()</literal> function.
299</title>
300
301<programlisting>
302 //----- C++ source codes: Instantiation and initialization of G4VisManager
303
304 .....
305 // Your Visualization Manager
306 #include "G4VisExecutive.hh"
307 .....
308
309 // Instantiation and initialization of the Visualization Manager
310 #ifdef G4VIS_USE
311 G4VisManager* visManager = new G4VisExecutive;
312 visManager -&gt; initialize ();
313 #endif
314
315 .....
316 #ifdef G4VIS_USE
317 delete visManager;
318 #endif
319
320 //----- end of C++
321</programlisting>
322</example>
323</para>
324
325<para>
326Alternatively, you can implement an empty
327<literal>RegisterGraphicsSystems()</literal> function, and register
328visualization drivers you want directly in your <literal>main()</literal>
329function. See <xref linkend="programlist_VisAddExe_3" />.
330
331<example id="programlist_VisAddExe_3">
332<title>
333An alternative style for the <literal>main()</literal> function.
334</title>
335
336<programlisting>
337 //----- C++ source codes: How to register a visualization driver directly
338 //                        in main() function
339
340 .....
341 G4VisManager* visManager = new G4VisExecutive;
342 visManager -&gt; RegisterGraphicsSystem (new MyGraphicsSystem);
343 .....
344 delete visManager
345
346 //----- end of C++
347</programlisting>
348</example>
349</para>
350
351<para>
352<emphasis>Do not forget</emphasis> to delete the instantiated Visualization
353Manager by yourself. Note that a graphics system for Geant4 Visualization
354may run as a different process. In that case, the destructor of
355<literal>G4VisManager</literal> might have to terminate the graphics system
356and/or close the connection.
357</para>
358
359<para>
360We recommend that the instantiation, initialization, and
361deletion of the Visualization Manager be protected by
362C-pre-processor commands, as in the novice examples. The
363C-pre-processor macro <literal>G4VIS_USE</literal> is automatically defined
364unless the environment variable <literal>G4VIS_NONE</literal> is set. This
365assumes that you are compiling your Geant4 executable with the
366standard version of GNUmakefile found in the <literal>config</literal>
367directory.
368</para>
369
370<para>
371<xref linkend="programlist_VisAddExe_4" /> shows an example of the
372<literal>main()</literal> function available for Geant4 Visualization.
373
374<example id="programlist_VisAddExe_4">
375<title>
376An example of the <literal>main()</literal> function available for
377Geant4 Visualization.
378</title>
379
380<programlisting>
381 //----- C++ source codes: An example of main() for visualization
382 .....
383 #include "G4VisExecutive.hh"
384 .....
385
386 int main()
387 {
388      // Run Manager
389      G4RunManager * runManager = new G4RunManager;
390
391      // Detector components
392      runManager-&gt;set_userInitialization(new MyDetectorConstruction);
393      runManager-&gt;set_userInitialization(new MyPhysicsList);
394
395      // UserAction classes.
396      runManager-&gt;set_userAction(new MyRunAction);
397      runManager-&gt;set_userAction(new MyPrimaryGeneratorAction);
398      runManager-&gt;set_userAction(new MyEventAction);
399      runManager-&gt;set_userAction(new MySteppingAction);
400
401 #ifdef G4VIS_USE
402      G4VisManager* visManager = new G4VisExecutive;
403      visManager -&gt; initialize ();
404 #endif
405
406      // Event loop
407      // Define (G)UI terminal
408      G4UIsession * session = new G4UIterminal
409      session-&gt;sessionStart();
410
411      delete session;
412      delete runManager;
413
414 #ifdef G4VIS_USE
415      delete visManager;
416 #endif
417
418      return 0;
419 }
420
421 //----- end of C++
422</programlisting>
423</example>
424</para>
425
426<para>
427Useful information on incorporated visualization drivers can be
428displayed in initializing the Visualization Manager. This is done
429by setting the verbosity flag to an appropriate string:
430
431<informalexample>
432<programlisting>
433   Simple graded message scheme - give first letter or a digit:
434    0) quiet,         // Nothing is printed.
435    1) startup,       // Startup and endup messages are printed...
436    2) errors,        // ...and errors...
437    3) warnings,      // ...and warnings...
438    4) confirmations, // ...and confirming messages...
439    5) parameters,    // ...and parameters of scenes and views...
440    6) all            // ...and everything available.
441</programlisting>
442</informalexample>
443</para>
444
445<para>
446For example, in your <literal>main()</literal> function, write the following
447code:
448
449<informalexample>
450<programlisting>
451  ...
452  G4VisManager* visManager = new G4VisExecutive ();
453  visManager -&gt; SetVerboseLevel (1);
454  visManager -&gt; Initialize ();
455  ...
456</programlisting>
457</informalexample>
458</para>
459
460<para>
461(This can also be set with the <literal>/vis/verbose</literal> command.)
462</para>
463
464
465</sect2>
466</sect1>
Note: See TracBrowser for help on using the repository browser.