source: trunk/documents/UserDoc/DocBookUsersGuides/ForApplicationDeveloper/xml/Appendix/pythonInterface.xml @ 1222

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

CVS update

File size: 14.6 KB
Line 
1<!-- ******************************************************** -->
2<!--                                                          -->
3<!--  [History]                                               -->
4<!--    Created by Koichi Murakami, Dec-2008                  -->
5<!--    Correct typo by KM Dec-2009                           -->
6<!--                                                          -->
7<!-- ******************************************************** -->
8
9<!-- ******************* Section (Level#1) ****************** -->
10<sect1 id="sect.PythonInterface">
11<title>
12Python Interface
13</title>
14
15<para>
16Python is a popular scripting language with an interactive interpreter.
17Geant4Py, a Geant4-Python bridge, provides a bridge for Geant4 classes.
18This enables to directly access Geant4 classes from Python scripting.
19User applications can be easily configured with many Python
20third-party modules, such as PyROOT, on the Python software bus.
21</para>
22
23<para>
24Geant4Py is located in the directory <literal>environments/g4py/</literal>.
25</para>
26
27<!-- ******************* Section (Level#2) ****************** -->
28<sect2 id="sect.Installation">
29<title>
30Installation
31</title>
32
33<sect3 id="sect.SoftwareRequirements">
34<title>
35Software Requirements
36</title>
37
38<para>
39Geant4Py requires
40<ulink url="http://www.boost.org/">Boost-C++ external library</ulink>,
41which helps Python binding of C++ codes.
42A Precompiled package is available for many Linux distributions
43(SL, SuSE, Ubuntu, etc) and Mac OSX.
44</para>
45
46<para>
47Geant4 libraries can be built as "static" and "granular",
48where library components (variables, functions, ...)
49used in the application are linked statically and shipped
50with the application.
51On the other hands, in dynamic binding, the library components
52are not included in the application, and their binding is
53carried out at run time.
54The modular approach of Python is based on dynamic binding mechanism,
55so you have Geant4 shared libraries are required instead.
56</para>
57
58<para>
59For generic information about building Geant4 libraries,
60please refer to the <ulink url="http://cern.ch/geant4/G4UsersDocuments/UsersGuides/InstallationGuide/html/">Installation Guide</ulink>.
61</para>
62
63<para>
64Here are some tips for manually building "global" and "shared" libraries
65from an already existing "static + granular" installation.
66After setting additional environment variables like:
67<table id="table.additionalEnvs">
68<title>
69Additional environment variables for building global and shared library
70</title>
71<tgroup cols="3">
72  <tbody>
73  <row>
74    <entry>
75      <emphasis role="bold">Environment Variable</emphasis>
76    </entry>
77    <entry>
78      <emphasis role="bold">Description</emphasis>
79    </entry>
80    <entry>
81      <emphasis role="bold">Value (example)</emphasis>
82    </entry>
83  </row>
84  <row>
85    <entry>
86      <emphasis role="bold">G4LIB</emphasis>
87    </entry>
88    <entry>
89      Path where the Geant4 libraries are installed
90    </entry>
91    <entry>
92      $G4INSTALL/slib
93    </entry>
94  </row>
95  <row>
96    <entry>
97      <emphasis role="bold">G4TMP</emphasis>
98    </entry>
99    <entry>
100      Path where temporary files are placed
101    </entry>
102    <entry>
103      $G4INSTALL/tmp-slib
104    </entry>
105  </row>
106  <row>
107    <entry>
108      <emphasis role="bold">G4LIB_BUILD_SHARED</emphasis>
109    </entry>
110    <entry>
111      Flag for building shared libraries
112    </entry>
113    <entry>
114      1
115    </entry>
116  </row>
117  </tbody>
118</tgroup>
119</table>
120
121execute the following:
122
123<informalexample>
124<programlisting>
125# cd $G4INSTALL/source
126# make
127# make global
128</programlisting>
129</informalexample>
130
131In addition, it is required that all header files are installed
132in a single directory.
133
134<informalexample>
135<programlisting>
136# cd $G4INSTALL/source
137# make includes
138</programlisting>
139</informalexample>
140
141This will collect all relevant header files
142in <literal>$G4INSTALL/include</literal> (or <literal>$G4INCLUDE</literal>).
143</para>
144
145<para>
146There are additional tools for helping building a Geant4 library in
147the directory <literal>g4py/tools/</literal>.
148</para>
149</sect3>
150
151<sect3 id="sect.BuildingGeant4Py">
152<title>
153Building Geant4Py module
154</title>
155
156<para>
157Geant4Py provides a configure scrtipt for building modules.
158
159<informalexample>
160<programlisting>
161# ./configure --help
162`configure' configures Geant4Py to adapt to many kinds of systems.
163
164Usage:  ./configure SYSTEM [OPTION]... [VAR=VALUE]...
165
166SYSTEM: System type (see Supported Arhitectures)
167
168Options:
169  -h, --help                Display this help and exit
170
171Installation directories:
172  --prefix=PREFIX           Installation prefix  [./]
173  --libdir=DIR              Python modules dir [PREFIX/lib]
174
175Fine tuning of the library path:
176  --with-g4-incdir=DIR      Geant4 header dir [$G4INCLUDE]
177  --with-g4-libdir=DIR      Geant4 library dir [$G4LIB/$G4SYSTEM]
178  --with-clhep-incdir=DIR   CLHEP header dir [$CLHEP_INCLUDE_DIR]
179  --with-clhep-libdir=DIR   CLHEP library dir [$CLHEP_LIB_DIR]
180  --with-clhep-lib=LIB      library name of libCLHEP.so [CLHEP|$CLHEP_LIB]
181
182  --with-python-incdir=DIR  Python header dir [/usr/include/python(2.#)],
183                            (location of pyconfig.h)
184  --with-python-libdir=DIR  Python library dir [/usr/lib(64)]
185
186  --with-boost-incdir=DIR   BOOST-C++ header dir [/usr/include],
187                            (location of boost/)
188  --with-boost-libdir=DIR   BOOST-C++ library dir [/usr/lib]
189  --with-boost-python-lib=LIB library name of libboost_python.so [boost_python]
190
191  --with-package-dir=DIR    Geant4 Package dir
192
193  --with-extra-dir=DIR      Install path for extra packages [/usr/local]
194
195  --with-xercesc-incdir=DIR Xerces-C header dir [/usr/include]
196  --with-xercesc-libdir=DIR Xerces-C library dir [/usr/lib(64)]
197
198Enable/disable options: prefix with either --enable- or --disable-
199  openglx      OpenGLX support    [auto]
200  openglxm     OpenGLXm support   [disable, $G4VIS_USE_OPENGLXM]
201  raytracerx   RayTracerX support [disable, $G4VIS_USE_RAYTRACERX]
202
203Supported Architectures:
204  linux           for Linux gcc 3.x and 4.x (32bit)
205  linux64         for Linux gcc 3.x and 4.x (64bit, alias to linuxx8664gcc)
206  linuxx8664gcc   for AMD Opteron and Intel EM64T Linux gcc 3.x and 4.x
207  macosx          for MacOSX with gcc (Tiger/Leopard and Xcode)
208</programlisting>
209</informalexample>
210
211For example, you run it like
212<informalexample>
213<programlisting>
214# ./configure linux64
215  --with-g4-incdir=/opt/heplib/Geant4/geant4.9.3/include
216  --with-g4-libdir=/opt/heplib/Geant4/geant4.9.3/slib/Linux-g++
217  --with-clhep-incdir=/opt/heplib/CLHEP/2.0.4.5/include
218  --with-clhep-libdir=/opt/heplib/CLHEP/2.0.4.5/lib
219  --with-clhep-lib=CLHEP-2.0.4.5
220</programlisting>
221</informalexample>
222
223The configure script automatically check your environment,
224and create <literal>config/config.gmk</literal>,
225which describes your envrionment.
226
227After executing the configure script successfully, then
228
229<informalexample>
230<programlisting>
231# make
232# make install
233</programlisting>
234</informalexample>
235</para>
236</sect3>
237
238</sect2>
239
240
241<!-- ******************* Section (Level#2) ****************** -->
242<sect2 id="sect.UsingGeant4Py">
243<title>
244Using Geant4Py
245</title>
246
247<para>
248<emphasis role="bold">PYTHONPATH</emphasis> environment variable
249should be set at tun time.
250<emphasis role="bold">PYTHONPATH</emphasis> environment variable indicates
251Python module search directories, given by a colon-separated list
252of directories. Practically, the variable is
253<literal>(your g4py directory)/lib</literal>.
254</para>
255
256<sect3 id="sect.ImportGeant4">
257<title>
258Import Geant4
259</title>
260
261<para>
262To use Geant4Py, you start with importing the module called "Geant4".
263<informalexample>
264<programlisting>
265# python
266Python 2.6.2 (r262:71600, Oct 24 2009, 03:15:21)
267[GCC 4.4.1 [gcc-4_4-branch revision 150839]] on linux2
268Type "help", "copyright", "credits" or "license" for more information.
269&gt;&gt;&gt; from Geant4 import *
270
271*************************************************************
272 Geant4 version Name: geant4-09-03         (18-December-2009)
273                      Copyright : Geant4 Collaboration
274                      Reference : NIM A 506 (2003), 250-303
275                            WWW : http://cern.ch/geant4
276*************************************************************
277
278Visualization Manager instantiating...
279&gt;&gt;&gt;
280</programlisting>
281</informalexample>
282</para>
283</sect3>
284
285<sect3 id="sect.AccessToG4Globals">
286<title>
287Access to Geant4 Globals
288</title>
289
290<para>
291When importing the Geant4 module, the <literal>G4RunManager</literal>
292object will be automatically instantiated. Geant4 singleton objects are also
293automatically instantiated. These singleton objects can be accessed
294by "gXXXX" variables, like "gRunManager".
295
296<informalexample>
297<programlisting>
298                        gLossTableManager       gTerminate
299gApplyUICommand         gMaterialTable          gTrackingManager
300gControlExecute         gNistManager            gTransportationManager
301gElementTable           gParticleIterator       gUImanager
302gEmCalculator           gParticleTable          gVisManager
303gEventManager           gProcessTable
304gExceptionHandler       gProductionCutsTable
305gG4Date                 gRunManager
306gG4VERSION_NUMBER       gRunManagerKernel
307gG4Version              gStackManager
308gGeometryManager        gStartUISession
309gGetCurrentValues       gStateManager
310</programlisting>
311</informalexample>
312</para>
313</sect3>
314
315<sect3 id="sect.CallGeant4Methods">
316<title>
317Call Geant4 Methods
318</title>
319
320<para>
321Once a Python object of a Geant4 class instantiated,
322Geant4 methods can be directly called the same way as in C++.
323
324<informalexample>
325<programlisting>
326&gt;&gt;&gt; from Geant4 import *
327
328*************************************************************
329 Geant4 version Name: geant4-09-03         (18-December-2009)
330                      Copyright : Geant4 Collaboration
331                      Reference : NIM A 506 (2003), 250-303
332                            WWW : http://cern.ch/geant4
333*************************************************************
334
335Visualization Manager instantiating...
336
337&gt;&gt;&gt; print gRunManager.GetVersionString()
338 Geant4 version Name: geant4-09-03    (18-December-2009)
339</programlisting>
340</informalexample>
341</para>
342</sect3>
343
344</sect2>
345
346
347<!-- ******************* Section (Level#2) ****************** -->
348<sect2 id="sect.Site-modules">
349<title>
350Site-modules
351</title>
352
353<para>
354Geant4Py provides additional utility modules called "g4py" in the directory
355<literal>site-modules</literal>. It consists of predifined geometries,
356materials, physics lists, primary generator actions, and so on.
357</para>
358
359<sect3 id="sect.ezgeomModule">
360<title>
361<emphasis>ezgeom</emphasis> module
362</title>
363
364<para>
365The <emphasis>ezgeom</emphasis> module provides an alternative way
366of defining simple geometry.
367An example code for defining a simple geometry is shown here:
368</para>
369
370<informalexample>
371<programlisting>
372import g4py.ezgeom
373from g4py.ezgeom import G4EzVolume
374
375def ConstructGeom():
376  print "* Constructing geometry..."
377  # reset world material
378  air= G4Material.GetMaterial("G4_AIR")
379  g4py.ezgeom.SetWorldMaterial(air)
380
381  # a target box is placed
382  global target
383  target= G4EzVolume("Target")
384  au= G4Material.GetMaterial("G4_Au")
385  target.CreateTubeVolume(au, 0., 1.*cm, 1.*mm)
386  target.PlaceIt(G4ThreeVector(0.,0.,-10.*cm))
387</programlisting>
388</informalexample>
389</sect3>
390
391
392<sect3 id="sect.NISTmaterialsModule">
393<title>
394<emphasis>NISTmaterials</emphasis> module
395</title>
396
397<para>
398The <emphasis>NISTmaterials</emphasis> module provides an instant
399use of Geant4 NIST materials.
400An example code for creating NIST materials:
401</para>
402
403<informalexample>
404<programlisting>
405from Geant4 import *
406import g4py.NISTmaterials
407
408g4py.NISTmaterials.Construct()
409print Geant4.gMaterialTable
410</programlisting>
411</informalexample>
412</sect3>
413
414<sect3 id="sect.ParticleGunModule">
415<title>
416<emphasis>ParticleGun</emphasis> module
417</title>
418
419<para>
420The <emphasis>ParticleGun</emphasis> module provides
421a primary generator action with <literal>G4ParticleGun</literal>.
422An example code is shown here:
423</para>
424
425<informalexample>
426<programlisting>
427import g4py.ParticleGun
428
429# normal way for constructing user primary generator action
430#pgPGA= g4py.ParticleGun.ParticleGunAction()
431#gRunManager.SetUserAction(pgPGA)
432#pg= pgPGA.GetParticleGun()
433
434# 2nd way, short-cut way
435pg= g4py.ParticleGun.Construct()
436
437# set parameters of particle gun
438pg.SetParticleByName("e-")
439pg.SetParticleEnergy(300.*MeV)
440primary_position= G4ThreeVector(0.,0., -14.9*cm)
441primary_direction= G4ThreeVector(0.2, 0., 1.)
442pg.SetParticlePosition(primary_position)
443pg.SetParticleMomentumDirection(primary_direction)
444</programlisting>
445</informalexample>
446</sect3>
447
448</sect2>
449
450
451<!-- ******************* Section (Level#2) ****************** -->
452<sect2 id="sect.Examples">
453<title>
454Examples
455</title>
456
457<para>
458There are some examples of Geant4Py in the directories
459<literal>"tests/"</literal> and <literal>"examples/"</literal>.
460</para>
461
462In the <literal>"tests/"</literal> directory,
463
464<informalexample>
465<programlisting>
466gtest01 : exposes a user application
467gtest02 : test for using site-module packages
468gtest03 : test for ezgeom package
469gtest04 : test for getting command tree and command information
470gtest05 : test for constructing CSG geometries in Python
471gtest06 : test for constructing/visualizing boolean geoemtries
472gtest07 : test for checking overlapped geometries
473</programlisting>
474</informalexample>
475
476The <literal>"examples/"</literal> directory contains
477a set of examples of Geant4Py.
478
479<variablelist>
480<varlistentry>
481<term>demos/water_phantom</term>
482<listitem>
483<para>
484An example of "water phantom dosimetry".
485This demo program shows that a Geant4 application well coworks with Root
486on Python front end. VisManager, PrimaryGeneratorAction, UserAction-s,
487histogramming with Root are implemented in Python;
488<itemizedlist>
489<listitem>dose calculation in a water phantom</listitem>
490<listitem>Python overloading of user actions</listitem>
491<listitem>on-line histogramming with Root</listitem>
492<listitem>visualization</listitem>
493</itemizedlist>
494</para>
495</listitem>
496</varlistentry>
497</variablelist>
498
499<variablelist>
500<varlistentry>
501<term>education</term>
502
503<listitem>
504<para>
505Educational examples with Graphical User Interface using
506<literal>TKinter</literal>
507</para>
508
509<para>
510* lesson1
511</para>
512
513<para>
514The first version of the courseware of the mass attenuation coefficient.
515</para>
516
517<para>
518* lesson2
519</para>
520
521<para>
522GUI interface of ExN03, which can control geometry configuration,
523intial particle condition, physics processes, cut value,
524magnetic field and visualization outputs.
525</para>
526
527</listitem>
528</varlistentry>
529</variablelist>
530
531
532<variablelist>
533<varlistentry>
534<term>emplot</term>
535<listitem>
536<para>
537Examples of plotting photon cross sections and stopping powers with Root.
538</para>
539</listitem>
540</varlistentry>
541</variablelist>
542
543
544<variablelist>
545<varlistentry>
546<term>gdml</term>
547<listitem>
548<para>
549Examples of writing/reading user's geometry to/from a GDML file
550</para>
551</listitem>
552</varlistentry>
553</variablelist>
554
555</sect2>
556</sect1>
557
Note: See TracBrowser for help on using the repository browser.