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

Last change on this file was 1211, checked in by garnier, 15 years ago

CVS update

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