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> |
---|
12 | Python Interface |
---|
13 | </title> |
---|
14 | |
---|
15 | <para> |
---|
16 | Python is a popular scripting language with an interactive interpreter. |
---|
17 | Geant4Py, a Geant4-Python bridge, provides a bridge for Geant4 classes. |
---|
18 | This enables to directly access Geant4 classes from Python scripting. |
---|
19 | User applications can be easily configured with many Python |
---|
20 | third-party modules, such as PyROOT, on the Python software bus. |
---|
21 | </para> |
---|
22 | |
---|
23 | <para> |
---|
24 | Geant4Py is located in the directory <literal>environments/g4py/</literal>. |
---|
25 | </para> |
---|
26 | |
---|
27 | <!-- ******************* Section (Level#2) ****************** --> |
---|
28 | <sect2 id="sect.Installation"> |
---|
29 | <title> |
---|
30 | Installation |
---|
31 | </title> |
---|
32 | |
---|
33 | <sect3 id="sect.SoftwareRequirements"> |
---|
34 | <title> |
---|
35 | Software Requirements |
---|
36 | </title> |
---|
37 | |
---|
38 | <para> |
---|
39 | Geant4Py requires |
---|
40 | <ulink url="http://www.boost.org/">Boost-C++ external library</ulink>, |
---|
41 | which helps Python binding of C++ codes. |
---|
42 | A Precompiled package is available for many Linux distributions |
---|
43 | (SL, SuSE, Ubuntu, etc) and Mac OSX. |
---|
44 | </para> |
---|
45 | |
---|
46 | <para> |
---|
47 | Geant4 libraries can be built as "static" and "granular", |
---|
48 | where library components (variables, functions, ...) |
---|
49 | used in the application are linked statically and shipped |
---|
50 | with the application. |
---|
51 | On the other hands, in dynamic binding, the library components |
---|
52 | are not included in the application, and their binding is |
---|
53 | carried out at run time. |
---|
54 | The modular approach of Python is based on dynamic binding mechanism, |
---|
55 | so you have Geant4 shared libraries are required instead. |
---|
56 | </para> |
---|
57 | |
---|
58 | <para> |
---|
59 | For generic information about building Geant4 libraries, |
---|
60 | please refer to the <ulink url="http://cern.ch/geant4/G4UsersDocuments/UsersGuides/InstallationGuide/html/">Installation Guide</ulink>. |
---|
61 | </para> |
---|
62 | |
---|
63 | <para> |
---|
64 | Here are some tips for manually building "global" and "shared" libraries |
---|
65 | from an already existing "static + granular" installation. |
---|
66 | After setting additional environment variables like: |
---|
67 | <table id="table.additionalEnvs"> |
---|
68 | <title> |
---|
69 | Additional 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 | |
---|
121 | execute the following: |
---|
122 | |
---|
123 | <informalexample> |
---|
124 | <programlisting> |
---|
125 | # cd $G4INSTALL/source |
---|
126 | # make |
---|
127 | # make global |
---|
128 | </programlisting> |
---|
129 | </informalexample> |
---|
130 | |
---|
131 | In addition, it is required that all header files are installed |
---|
132 | in a single directory. |
---|
133 | |
---|
134 | <informalexample> |
---|
135 | <programlisting> |
---|
136 | # cd $G4INSTALL/source |
---|
137 | # make includes |
---|
138 | </programlisting> |
---|
139 | </informalexample> |
---|
140 | |
---|
141 | This will collect all relevant header files |
---|
142 | in <literal>$G4INSTALL/include</literal> (or <literal>$G4INCLUDE</literal>). |
---|
143 | </para> |
---|
144 | |
---|
145 | <para> |
---|
146 | There are additional tools for helping building a Geant4 library in |
---|
147 | the directory <literal>g4py/tools/</literal>. |
---|
148 | </para> |
---|
149 | </sect3> |
---|
150 | |
---|
151 | <sect3 id="sect.BuildingGeant4Py"> |
---|
152 | <title> |
---|
153 | Building Geant4Py module |
---|
154 | </title> |
---|
155 | |
---|
156 | <para> |
---|
157 | Geant4Py 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 | |
---|
164 | Usage: ./configure SYSTEM [OPTION]... [VAR=VALUE]... |
---|
165 | |
---|
166 | SYSTEM: System type (see Supported Arhitectures) |
---|
167 | |
---|
168 | Options: |
---|
169 | -h, --help Display this help and exit |
---|
170 | |
---|
171 | Installation directories: |
---|
172 | --prefix=PREFIX Installation prefix [./] |
---|
173 | --libdir=DIR Python modules dir [PREFIX/lib] |
---|
174 | |
---|
175 | Fine 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 | |
---|
198 | Enable/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 | |
---|
203 | Supported 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 | |
---|
211 | For 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 | |
---|
223 | The configure script automatically check your environment, |
---|
224 | and create <literal>config/config.gmk</literal>, |
---|
225 | which describes your envrionment. |
---|
226 | |
---|
227 | After 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> |
---|
244 | Using Geant4Py |
---|
245 | </title> |
---|
246 | |
---|
247 | <para> |
---|
248 | <emphasis role="bold">PYTHONPATH</emphasis> environment variable |
---|
249 | should be set at tun time. |
---|
250 | <emphasis role="bold">PYTHONPATH</emphasis> environment variable indicates |
---|
251 | Python module search directories, given by a colon-separated list |
---|
252 | of directories. Practically, the variable is |
---|
253 | <literal>(your g4py directory)/lib</literal>. |
---|
254 | </para> |
---|
255 | |
---|
256 | <sect3 id="sect.ImportGeant4"> |
---|
257 | <title> |
---|
258 | Import Geant4 |
---|
259 | </title> |
---|
260 | |
---|
261 | <para> |
---|
262 | To use Geant4Py, you start with importing the module called "Geant4". |
---|
263 | <informalexample> |
---|
264 | <programlisting> |
---|
265 | # python |
---|
266 | Python 2.6.2 (r262:71600, Oct 24 2009, 03:15:21) |
---|
267 | [GCC 4.4.1 [gcc-4_4-branch revision 150839]] on linux2 |
---|
268 | Type "help", "copyright", "credits" or "license" for more information. |
---|
269 | >>> 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 | |
---|
278 | Visualization Manager instantiating... |
---|
279 | >>> |
---|
280 | </programlisting> |
---|
281 | </informalexample> |
---|
282 | </para> |
---|
283 | </sect3> |
---|
284 | |
---|
285 | <sect3 id="sect.AccessToG4Globals"> |
---|
286 | <title> |
---|
287 | Access to Geant4 Globals |
---|
288 | </title> |
---|
289 | |
---|
290 | <para> |
---|
291 | When importing the Geant4 module, the <literal>G4RunManager</literal> |
---|
292 | object will be automatically instantiated. Geant4 singleton objects are also |
---|
293 | automatically instantiated. These singleton objects can be accessed |
---|
294 | by "gXXXX" variables, like "gRunManager". |
---|
295 | |
---|
296 | <informalexample> |
---|
297 | <programlisting> |
---|
298 | gLossTableManager gTerminate |
---|
299 | gApplyUICommand gMaterialTable gTrackingManager |
---|
300 | gControlExecute gNistManager gTransportationManager |
---|
301 | gElementTable gParticleIterator gUImanager |
---|
302 | gEmCalculator gParticleTable gVisManager |
---|
303 | gEventManager gProcessTable |
---|
304 | gExceptionHandler gProductionCutsTable |
---|
305 | gG4Date gRunManager |
---|
306 | gG4VERSION_NUMBER gRunManagerKernel |
---|
307 | gG4Version gStackManager |
---|
308 | gGeometryManager gStartUISession |
---|
309 | gGetCurrentValues gStateManager |
---|
310 | </programlisting> |
---|
311 | </informalexample> |
---|
312 | </para> |
---|
313 | </sect3> |
---|
314 | |
---|
315 | <sect3 id="sect.CallGeant4Methods"> |
---|
316 | <title> |
---|
317 | Call Geant4 Methods |
---|
318 | </title> |
---|
319 | |
---|
320 | <para> |
---|
321 | Once a Python object of a Geant4 class instantiated, |
---|
322 | Geant4 methods can be directly called the same way as in C++. |
---|
323 | |
---|
324 | <informalexample> |
---|
325 | <programlisting> |
---|
326 | >>> 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 | |
---|
335 | Visualization Manager instantiating... |
---|
336 | |
---|
337 | >>> 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> |
---|
350 | Site-modules |
---|
351 | </title> |
---|
352 | |
---|
353 | <para> |
---|
354 | Geant4Py provides additional utility modules called "g4py" in the directory |
---|
355 | <literal>site-modules</literal>. It consists of predifined geometries, |
---|
356 | materials, 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> |
---|
365 | The <emphasis>ezgeom</emphasis> module provides an alternative way |
---|
366 | of defining simple geometry. |
---|
367 | An example code for defining a simple geometry is shown here: |
---|
368 | </para> |
---|
369 | |
---|
370 | <informalexample> |
---|
371 | <programlisting> |
---|
372 | import g4py.ezgeom |
---|
373 | from g4py.ezgeom import G4EzVolume |
---|
374 | |
---|
375 | def 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> |
---|
398 | The <emphasis>NISTmaterials</emphasis> module provides an instant |
---|
399 | use of Geant4 NIST materials. |
---|
400 | An example code for creating NIST materials: |
---|
401 | </para> |
---|
402 | |
---|
403 | <informalexample> |
---|
404 | <programlisting> |
---|
405 | from Geant4 import * |
---|
406 | import g4py.NISTmaterials |
---|
407 | |
---|
408 | g4py.NISTmaterials.Construct() |
---|
409 | print 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> |
---|
420 | The <emphasis>ParticleGun</emphasis> module provides |
---|
421 | a primary generator action with <literal>G4ParticleGun</literal>. |
---|
422 | An example code is shown here: |
---|
423 | </para> |
---|
424 | |
---|
425 | <informalexample> |
---|
426 | <programlisting> |
---|
427 | import 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 |
---|
435 | pg= g4py.ParticleGun.Construct() |
---|
436 | |
---|
437 | # set parameters of particle gun |
---|
438 | pg.SetParticleByName("e-") |
---|
439 | pg.SetParticleEnergy(300.*MeV) |
---|
440 | primary_position= G4ThreeVector(0.,0., -14.9*cm) |
---|
441 | primary_direction= G4ThreeVector(0.2, 0., 1.) |
---|
442 | pg.SetParticlePosition(primary_position) |
---|
443 | pg.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> |
---|
454 | Examples |
---|
455 | </title> |
---|
456 | |
---|
457 | <para> |
---|
458 | There are some examples of Geant4Py in the directories |
---|
459 | <literal>"tests/"</literal> and <literal>"examples/"</literal>. |
---|
460 | </para> |
---|
461 | |
---|
462 | In the <literal>"tests/"</literal> directory, |
---|
463 | |
---|
464 | <informalexample> |
---|
465 | <programlisting> |
---|
466 | gtest01 : exposes a user application |
---|
467 | gtest02 : test for using site-module packages |
---|
468 | gtest03 : test for ezgeom package |
---|
469 | gtest04 : test for getting command tree and command information |
---|
470 | gtest05 : test for constructing CSG geometries in Python |
---|
471 | gtest06 : test for constructing/visualizing boolean geoemtries |
---|
472 | gtest07 : test for checking overlapped geometries |
---|
473 | </programlisting> |
---|
474 | </informalexample> |
---|
475 | |
---|
476 | The <literal>"examples/"</literal> directory contains |
---|
477 | a set of examples of Geant4Py. |
---|
478 | |
---|
479 | <variablelist> |
---|
480 | <varlistentry> |
---|
481 | <term>demos/water_phantom</term> |
---|
482 | <listitem> |
---|
483 | <para> |
---|
484 | An example of "water phantom dosimetry". |
---|
485 | This demo program shows that a Geant4 application well coworks with Root |
---|
486 | on Python front end. VisManager, PrimaryGeneratorAction, UserAction-s, |
---|
487 | histogramming 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> |
---|
505 | Educational examples with Graphical User Interface using |
---|
506 | <literal>TKinter</literal> |
---|
507 | </para> |
---|
508 | |
---|
509 | <para> |
---|
510 | * lesson1 |
---|
511 | </para> |
---|
512 | |
---|
513 | <para> |
---|
514 | The first version of the courseware of the mass attenuation coefficient. |
---|
515 | </para> |
---|
516 | |
---|
517 | <para> |
---|
518 | * lesson2 |
---|
519 | </para> |
---|
520 | |
---|
521 | <para> |
---|
522 | GUI interface of ExN03, which can control geometry configuration, |
---|
523 | intial particle condition, physics processes, cut value, |
---|
524 | magnetic 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> |
---|
537 | Examples 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> |
---|
549 | Examples 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 | |
---|