<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=MatthieuHentz</id>
	<title>PBTWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=MatthieuHentz"/>
	<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/wiki/Special:Contributions/MatthieuHentz"/>
	<updated>2026-04-05T20:13:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2298</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2298"/>
		<updated>2019-02-21T09:49:12Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Show loaded modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
=== Set path to modules ===&lt;br /&gt;
Before modules can be accessed, the module tool needs to be given the path to the desired set of modules. For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== List available modules ===&lt;br /&gt;
You can then list all modules available with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or check what versions of a given module are available using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail [modulename]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail Geant4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
prints out the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------------------------------------------------- /unix/pbt/software/eb/CentOS7/modules/phys --------------------------------------------------------------------&lt;br /&gt;
Geant4/10.00.p04-GCC-4.9.3    Geant4/10.01.p03-GCC-4.9.3    Geant4/10.02.p03-GCC-4.9.3    Geant4/10.03.p03-GCC-4.9.3    Geant4/10.04.p02-GCC-4.9.3    Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------- /unix/pbt/software/eb/CentOS7/modules/all ---------------------------------------------------------------------&lt;br /&gt;
Geant4/10.00.p04-GCC-4.9.3    Geant4/10.01.p03-GCC-4.9.3    Geant4/10.02.p03-GCC-4.9.3    Geant4/10.03.p03-GCC-4.9.3    Geant4/10.04.p02-GCC-4.9.3    Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Load modules ===&lt;br /&gt;
Load a given module using the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module load&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load the specified module along with all its dependencies. For instance,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loads Geant4 and all of the following&lt;br /&gt;
&lt;br /&gt;
  1) GCC/4.9.3                            7) libxml2/2.9.3-GCC-4.9.3             13) libdrm/2.4.68-GCC-4.9.3             19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
  2) expat/2.1.1-GCC-4.9.3                8) libpng/1.6.21-GCC-4.9.3             14) ncurses/6.0-GCC-4.9.3               20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
  3) CLHEP/2.3.3.0-GCC-4.9.3              9) bzip2/1.0.6-GCC-4.9.3               15) LLVM/3.8.0-GCC-4.9.3                21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
  4) cURL/7.44.0-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            16) eudev/3.1.5-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
  5) Xerces-C++/3.1.2-GCC-4.9.3          11) fontconfig/2.11.95-GCC-4.9.3        17) Mesa/11.2.1-GCC-4.9.3               23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
  6) zlib/1.2.8-GCC-4.9.3                12) X11/20160819-GCC-4.9.3              18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1  24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Don&#039;t forget to source the relevant environment variables where required, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Show loaded modules ===&lt;br /&gt;
To see what&#039;s loaded use&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module list&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Remove loaded modules ===&lt;br /&gt;
To get rid of all loaded modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module purge&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
=== Installing modules ===&lt;br /&gt;
&lt;br /&gt;
Example command to install module:&lt;br /&gt;
&lt;br /&gt;
eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
Two types of the same version (i.e. one single-threaded and one multi-threaded) can be installed side-by-side. To get EasyBuild to not replace a current version, the module can be renamed in the EasyBuild file. When a module is built, EasyBuild checks for modules of the same name and version and if one already exists, the build won&#039;t complete. If the options are updated, the build will be updated.&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the `module use` command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
=== The question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I&#039;m trying to compile a multi-threaded version of Geant4 using EasyBuild but would like to make sure I don&#039;t break everything.&lt;br /&gt;
&lt;br /&gt;
Looking at the easyconfig file for the most recent Geant4 version installed on our system and the directory structure of the `eb/CentOS7/software` directory, I can see that the location of the installation depends on the name and the version of the package and on the toolchain used to compile it. This leads me to believe that if I were to set `GEANT4_BUILD_MULTITHREADED=ON` in the easyconfig file and run the following command,&lt;br /&gt;
&lt;br /&gt;
	eb Geant4-10.04.p02-GCC-4.9.3.eb --robot&lt;br /&gt;
&lt;br /&gt;
it would overwrite the current (non-mutli-threaded) version of Geant4. I have two questions relating to this:&lt;br /&gt;
&lt;br /&gt;
1) Am I correct in thinking it would overwrite it?&lt;br /&gt;
2) If so, would you mind showing me how I can compile it alongside the existing version?&lt;br /&gt;
&lt;br /&gt;
The best solution I could come up with would be to install it in a different location using the command line option `--installpath-software=INSTALLPATH-SOFTWARE` but I&#039;m unsure if that breaks compatibility with the modules tool and I&#039;d also like to avoid overwriting the existing easyconfig file, which I don&#039;t know how to ensure.&lt;br /&gt;
&lt;br /&gt;
= Note on how to install Geant4 on Mac =&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ah we tried it briefly on mac, but it was a little frustrating so we stuck with our usual way, which is macports.  For your mac, I&#039;d recommend that.  I know it&#039;s different than EasyBuild, but it&#039;s generally easier for mac and for scientific software.&lt;br /&gt;
&lt;br /&gt;
https://www.macports.org&lt;br /&gt;
&lt;br /&gt;
Before getting it you should xcode + the command line tools (from app store) installed (test is you can run &#039;make&#039; on the terminal).  You should also have XQuartz (https://www.xquartz.org) installed for X11 support.  Then download and install the macports software (small, little mac installer).  Gives you a command on the terminal &#039;port&#039;.  Everything is by default installed in /opt/local so you can nuke that directory if you don&#039;t want it anymore.  It works by adding a line to your profile that adds that directory to your paths.&lt;br /&gt;
&lt;br /&gt;
port search packagename&lt;br /&gt;
&lt;br /&gt;
sudo port install py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I&#039;d install everything up to CLHEP from macports, then make my own geant4 installation.  I think there is a geant4 port on there, but I haven&#039;t used it.  For the geant4 installation you download hte source somewhere, make a build directory (outside the source) and then an install directory.  Usual CMake pattern.&lt;br /&gt;
&lt;br /&gt;
I&#039;d start with a high level package in macports and it&#039;ll work out all the requirements.  Like...&lt;br /&gt;
&lt;br /&gt;
sudo port install clhep qt5 py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I do everything apart from geant4 and bdsim through macports.&lt;br /&gt;
&lt;br /&gt;
Hope that helps,&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Summary from Raffy about EasyBuild, Environment Modules and GEANT4 =&lt;br /&gt;
== Geant4 ==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/gettingstarted.html&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
===GEANT4 OS/Software Prerequisites===&lt;br /&gt;
the following source/software must be present to build Geant4:&lt;br /&gt;
* Geant4 Toolkit Source Code.&lt;br /&gt;
* C++ Compiler and Standard Library supporting the C++11 Standard&lt;br /&gt;
* Linux (Scientific Linux CERN 6 and Linux CentOS 7 ): GNU Compiler Collection 4.8.5 or higher ( recommended to use the GCC compiler supplied by the package management system of your distribution unless this does not meet the minimum version requirement. The minimum required versions of GCC may be installed on SLC6 systems via the freeDeveloper Toolset)&lt;br /&gt;
* CMake 3.3 or higher (as provided through the package management system of your distribution, unless it does not meet the minimum version requirement.)&lt;br /&gt;
The main requirement is that the system has a GCC of sufficient version to support C++11 installed. &lt;br /&gt;
&lt;br /&gt;
===User Interface and Visualization Drivers===&lt;br /&gt;
* Qt User Interface and Visualization&lt;br /&gt;
** Qt4 (&amp;gt;=4.6) or Qt5 headers and libraries&lt;br /&gt;
* OpenGL or MesaGL headers and libraries.&lt;br /&gt;
** X11 OpenGL Visualization (Linux and macOS)&lt;br /&gt;
* Open Inventor Visualization&lt;br /&gt;
* Coin3D libraries and headers with SoXt(SoWin) graphics binding on Linux/macOS(Windows), compiled against the C++11 standard.&lt;br /&gt;
* Motif User Interface and Visualization (Linux and macOS)&lt;br /&gt;
&lt;br /&gt;
===Standard GEANT4 Building and Installation===&lt;br /&gt;
* Unpack the Geant4 source package geant4.10.05.tar.gz to a location of your choice: source directory /path/to/geant4.10.05&lt;br /&gt;
* create a directory in which to configure and run the build and store the build products /path/to/geant4.10.05-build&lt;br /&gt;
* $ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4.10.05-install /path/to/geant4.10.05&lt;br /&gt;
* CMake Variable CMAKE_INSTALL_PREFIX is used to set the install directory, the directory under which the Geant4 libraries, headers and support files will be installed. It must be supplied as an absolute path. The second argument to CMake is the path to the source directory.&lt;br /&gt;
* Additional arguments may be passed to CMake to activate optional components of Geant4, such as visualization drivers, or tune the build and install parameters.&lt;br /&gt;
*  If you run CMake and decide afterwards you want to activate additional options, simply rerun CMake in the build directory, passing it the extra options plus the build directory path. &lt;br /&gt;
* Datasets are not required to be present to build Geant4, but may be required to run your application, depending on the physics models you use. If you wish to download and install the datasets automatically as part of your build of Geant4, simply add the option-DGEANT4_INSTALL_DATA=ON to the arguments passed to CMake.&lt;br /&gt;
* After the configuration has run, CMake will have generated Unix Makefiles for building Geant4. To run the build, simply execute make in the build directory&lt;br /&gt;
* Once the build has completed, you can install Geant4 to the directory you specified earlier in CMAKE_INSTALL_PREFIX &lt;br /&gt;
* additional options can be passed to CMake to adjust the way Geant4 is built and installed and to enable optional components. Some options enable components of Geant4 which require external software.If these options are enabled, the required software will be searched for, and hence there are also options which control where CMake should look for these packages.These options may be set by passing their name and value to the cmake command via -D flags.&lt;br /&gt;
&lt;br /&gt;
===Standard Options===&lt;br /&gt;
* CMAKE_INSTALL_PREFIX installation prefix for Geant4. (default Unix: /usr/local)&lt;br /&gt;
* CMAKE_BUILD_TYPE : (DEFAULT : Release) Sets the type of build. It defaults to Release which gives a fully optimized build without debugging symbols.&lt;br /&gt;
* GEANT4_BUILD_MULTITHREADED : (DEFAULT : OFF) If set to ON, build Geant4 libraries with support for multithreading. At present, this is only supported on Unix systems. (Requires: Compiler/C++ Standard Library with support for C++ Threading Support)&lt;br /&gt;
* GEANT4_INSTALL_DATA : (DEFAULT : OFF)  to ON, download and install any Geant4 datasets missing from GEANT4_INSTALL_DATADIR.&lt;br /&gt;
* GEANT4_INSTALL_DATADIR : (DEFAULT : CMAKE_INSTALL_DATAROOTDIR) Installation directory for Geant4 datasets.&lt;br /&gt;
* GEANT4_USE_G3TOG4 : (DEFAULT : OFF) If set to ON, build the G3ToG4 library for reading ASCII call list files generated from Geant3 geometries.&lt;br /&gt;
* GEANT4_USE_GDML : (DEFAULT : OFF) If set to ON, build the G4persistency library with support for GDML.&lt;br /&gt;
* GEANT4_USE_QT (DEFAULT : OFF) If set to ON, build Qt4/5 User Interface and Visualization drivers. Requires: Qt4 or Qt5 and OpenGL libraries and headers. Qt5 will be searched for first. If it is not found Qt4 will be searched for. This behaviour can be adjusted through the advanced option GEANT4_FORCE_QT4.&lt;br /&gt;
* GEANT4_USE_OPENGL_X11 (DEFAULT : OFF, Unix Only)If set to ON, build the X11 OpenGL visualization driver.&lt;br /&gt;
* GEANT4_USE_RAYTRACER_X11 (DEFAULT : OFF, Unix only) If set to ON, build RayTracer visualization driver with X11 support.&lt;br /&gt;
* GEANT4_USE_INVENTOR (DEFAULT : OFF) If set to ON, build the OpenInventor visualization driver.&lt;br /&gt;
* GEANT4_USE_XM (DEFAULT : OFF, Unix Only) If set to ON, build Motif User Interface and Visualization drivers.&lt;br /&gt;
* GEANT4_USE_SYSTEM_CLHEP (DEFAULT : OFF) If set to ON, build Geant4 with an external install of CLHEP. You should not set this unless your usage of Geant4 mandates a specific external CLHEP installation (e.g. if your project’s software uses CLHEP in other tools and requires consistent use of the same CLHEP across the software stack).Requires: CLHEP libraries and headers. See the CLHEP_XXX and CMAKE_PREFIX_PATHAdvanced Options if CMake has trouble locating CLHEP.&lt;br /&gt;
* GEANT4_USE_SYSTEM_EXPAT (DEFAULT : ON) If set to ON, build Geant4 with an external install of Expat.&lt;br /&gt;
* GEANT4_USE_SYSTEM_ZLIB (DEFAULT : OFF) If set to ON, build Geant4 with an external install of zlib.&lt;br /&gt;
&lt;br /&gt;
===Advanced Options===&lt;br /&gt;
* http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
*Among the advanced, I list only the one used in the easy build recipe file https://bitbucket.org/jairhul/accsoft-eb/src/5f285502686e28d656dfa4d3ce1408d2551ee5d1/Geant4-10.00.p04-GCC-4.9.3.eb?at=master&amp;amp;fileviewer=file-view-default&lt;br /&gt;
** EXPAT_LIBRARY If CMake cannot locate your external EXPAT installation, set this to the full path to the EXPAT library, e.g. /usr/lib/libexpat.so&lt;br /&gt;
** EXPAT_INCLUDE_DIR If CMake cannot locate your external EXPAT installation, set this to the directory containing the EXPAT headers (e.g. if you have /foobar/expat.h, then set this to /foobar).&lt;br /&gt;
&lt;br /&gt;
== EasyBuild ==&lt;br /&gt;
===Reference===&lt;br /&gt;
https://media.readthedocs.org/pdf/caylo-easybuild/docfix/caylo-easybuild.pdf&lt;br /&gt;
EasyBuild is a flexible software build and installation framework that allows you to manage (scientific) software on High Performance Computing (HPC) systems in an efficient way.  Divert from the standard configure / make / make install with custom procedures. Allows for easily reproducing previous builds.&lt;br /&gt;
&lt;br /&gt;
===EasyBuild===&lt;br /&gt;
EasyBuild consists of a collection of Python modules and packages that interact with each other, dynamically picking up additional Python modules as needed for building and installing a (stack of) software package(s) specified via simple specification files&lt;br /&gt;
&lt;br /&gt;
In EasyBuild terminology: the EasyBuild framework leverages easyblocks to automatically build and install software using particular compiler toolchains, as specified by one or multiple easyconfig files.&lt;br /&gt;
&lt;br /&gt;
The EasyBuild framework provides functionality commonly needed when installing scientific software on HPC systems. For example, it deals with downloading, unpacking and patching of sources, loading module files for dependencies, setting up the build environment, autonomously running (interactive) shell commands, creating module files that match the specification files, etc.&lt;br /&gt;
The implementation of a particular software build and install procedure is done in a Python module, which is aptly referred to as an easyblock.&lt;br /&gt;
For each software package being built, the EasyBuild framework will determine which easyblock should be used, based on the name of the software package or the value of the easyblock specification parameter (see Easyblock specification). Since EasyBuild v2.0, an easyblock must be specified in case no matching easyblock is found based on the software name (cfr. Automagic fallback to ConfigureMake).&lt;br /&gt;
The specification files that are supplied to EasyBuild are referred to as easyconfig files (or simply easyconfigs .eb), which are basically plain text files containing (mostly) key-value assignments for build parameters supported by the framework, also referred to as easyconfig parameters (see Writing easyconfig files: the basics for more information).&lt;br /&gt;
An easyconfig file serves as a build specification for EasyBuild.&lt;br /&gt;
Easyconfigs typically follow a (fixed) strict naming scheme, i.e. &amp;lt;name&amp;gt;-&amp;lt;version&amp;gt;[-&amp;lt;toolchain&amp;gt;][&amp;lt;versionsuffix&amp;gt;&lt;br /&gt;
A handful of easyconfig parameters are mandatory:&lt;br /&gt;
* name, version: specify what software (version) to build&lt;br /&gt;
* homepage, description: metadata (used for module help)&lt;br /&gt;
* toolchain: specifies name and version of compiler toolchain to use&lt;br /&gt;
* Using external modules as dependencies&lt;br /&gt;
* Configure/build/install command options: &lt;br /&gt;
** configopts: options for configure command&lt;br /&gt;
** preconfigopts: options used as prefix for configure command&lt;br /&gt;
** buildopts, prebuildopts: options for build command&lt;br /&gt;
** installopts, preinstallopts: options for install &lt;br /&gt;
&lt;br /&gt;
EasyBuild allows to easily reproduce software builds/installations and install multiple versions.&lt;br /&gt;
&lt;br /&gt;
==Environment Modules==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://modules.sourceforge.net/man/module.html&lt;br /&gt;
===Environment Modules===&lt;br /&gt;
Typically users initialize their environment when they log in by setting environment information for every application they will reference during the session. &lt;br /&gt;
The Environment Modules system is a tool to help users manage their Unix or Linux shell environment, by allowing groups of related environment-variable settings to be made or removed dynamically. The modules system is based on modulefiles,[6] which specify groups of environment settings that need to be made together. Modulefiles can be installed in a central location for general use, or in a user directory for personal use. Environment Modules modulefiles are written in the Tcl (Tool Command Language) and are interpreted by the modulecmd program via the module[7] user interface. The key advantage of Environment Modules is that it is shell independent and supports all major shells such as bash, ksh, zsh, sh, tcsh, and csh. The second key advantage is that it allows to use multiple versions of the program or package from the same account by just loading proper module. modulefiles are created on per application per version basis. They can be dynamically loaded, unloaded, or switched. Along with the capability of using multiple versions of the same software it also can be used to implement site policies regarding the access and use of applications.&lt;br /&gt;
https://modules.readthedocs.io/en/latest/FAQ.html&lt;br /&gt;
&lt;br /&gt;
===New commands for centOS 7 ===&lt;br /&gt;
&lt;br /&gt;
# Tell module tool where the modules are&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
# Load Geant4&lt;br /&gt;
# This loads all the necessary libraries like CLHEP, Qt5, etc.&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
module load CMake/3.5.2-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;what does this mean&#039;&#039;&#039;&lt;br /&gt;
* module use [-a|--append] directory…&lt;br /&gt;
Prepend one or more directories to the MODULEPATH environment variable.&lt;br /&gt;
* load   modulefile...&lt;br /&gt;
Load modulefile(s) into the shell environment.&lt;br /&gt;
&lt;br /&gt;
when Geant4 is installed as a module&lt;br /&gt;
To see which versions are available, execute: module avail geant4&lt;br /&gt;
To then use a particular version, execute for example&lt;br /&gt;
module load geant4/10.02.p03&lt;br /&gt;
After you load the module, you can see where the software is installed by executing&lt;br /&gt;
echo $EBROOTGEANT4&lt;br /&gt;
&lt;br /&gt;
Set GEANT environment variables&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2297</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2297"/>
		<updated>2019-02-21T09:48:55Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Load modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
=== Set path to modules ===&lt;br /&gt;
Before modules can be accessed, the module tool needs to be given the path to the desired set of modules. For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== List available modules ===&lt;br /&gt;
You can then list all modules available with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or check what versions of a given module are available using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail [modulename]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail Geant4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
prints out the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------------------------------------------------- /unix/pbt/software/eb/CentOS7/modules/phys --------------------------------------------------------------------&lt;br /&gt;
Geant4/10.00.p04-GCC-4.9.3    Geant4/10.01.p03-GCC-4.9.3    Geant4/10.02.p03-GCC-4.9.3    Geant4/10.03.p03-GCC-4.9.3    Geant4/10.04.p02-GCC-4.9.3    Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------- /unix/pbt/software/eb/CentOS7/modules/all ---------------------------------------------------------------------&lt;br /&gt;
Geant4/10.00.p04-GCC-4.9.3    Geant4/10.01.p03-GCC-4.9.3    Geant4/10.02.p03-GCC-4.9.3    Geant4/10.03.p03-GCC-4.9.3    Geant4/10.04.p02-GCC-4.9.3    Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Load modules ===&lt;br /&gt;
Load a given module using the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module load&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load the specified module along with all its dependencies. For instance,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loads Geant4 and all of the following&lt;br /&gt;
&lt;br /&gt;
  1) GCC/4.9.3                            7) libxml2/2.9.3-GCC-4.9.3             13) libdrm/2.4.68-GCC-4.9.3             19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
  2) expat/2.1.1-GCC-4.9.3                8) libpng/1.6.21-GCC-4.9.3             14) ncurses/6.0-GCC-4.9.3               20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
  3) CLHEP/2.3.3.0-GCC-4.9.3              9) bzip2/1.0.6-GCC-4.9.3               15) LLVM/3.8.0-GCC-4.9.3                21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
  4) cURL/7.44.0-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            16) eudev/3.1.5-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
  5) Xerces-C++/3.1.2-GCC-4.9.3          11) fontconfig/2.11.95-GCC-4.9.3        17) Mesa/11.2.1-GCC-4.9.3               23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
  6) zlib/1.2.8-GCC-4.9.3                12) X11/20160819-GCC-4.9.3              18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1  24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Don&#039;t forget to source the relevant environment variables where required, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Show loaded modules ===&lt;br /&gt;
To see what&#039;s loaded, use&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module list&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which prints, for example,&lt;br /&gt;
&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Remove loaded modules ===&lt;br /&gt;
To get rid of all loaded modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module purge&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
=== Installing modules ===&lt;br /&gt;
&lt;br /&gt;
Example command to install module:&lt;br /&gt;
&lt;br /&gt;
eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
Two types of the same version (i.e. one single-threaded and one multi-threaded) can be installed side-by-side. To get EasyBuild to not replace a current version, the module can be renamed in the EasyBuild file. When a module is built, EasyBuild checks for modules of the same name and version and if one already exists, the build won&#039;t complete. If the options are updated, the build will be updated.&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the `module use` command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
=== The question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I&#039;m trying to compile a multi-threaded version of Geant4 using EasyBuild but would like to make sure I don&#039;t break everything.&lt;br /&gt;
&lt;br /&gt;
Looking at the easyconfig file for the most recent Geant4 version installed on our system and the directory structure of the `eb/CentOS7/software` directory, I can see that the location of the installation depends on the name and the version of the package and on the toolchain used to compile it. This leads me to believe that if I were to set `GEANT4_BUILD_MULTITHREADED=ON` in the easyconfig file and run the following command,&lt;br /&gt;
&lt;br /&gt;
	eb Geant4-10.04.p02-GCC-4.9.3.eb --robot&lt;br /&gt;
&lt;br /&gt;
it would overwrite the current (non-mutli-threaded) version of Geant4. I have two questions relating to this:&lt;br /&gt;
&lt;br /&gt;
1) Am I correct in thinking it would overwrite it?&lt;br /&gt;
2) If so, would you mind showing me how I can compile it alongside the existing version?&lt;br /&gt;
&lt;br /&gt;
The best solution I could come up with would be to install it in a different location using the command line option `--installpath-software=INSTALLPATH-SOFTWARE` but I&#039;m unsure if that breaks compatibility with the modules tool and I&#039;d also like to avoid overwriting the existing easyconfig file, which I don&#039;t know how to ensure.&lt;br /&gt;
&lt;br /&gt;
= Note on how to install Geant4 on Mac =&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ah we tried it briefly on mac, but it was a little frustrating so we stuck with our usual way, which is macports.  For your mac, I&#039;d recommend that.  I know it&#039;s different than EasyBuild, but it&#039;s generally easier for mac and for scientific software.&lt;br /&gt;
&lt;br /&gt;
https://www.macports.org&lt;br /&gt;
&lt;br /&gt;
Before getting it you should xcode + the command line tools (from app store) installed (test is you can run &#039;make&#039; on the terminal).  You should also have XQuartz (https://www.xquartz.org) installed for X11 support.  Then download and install the macports software (small, little mac installer).  Gives you a command on the terminal &#039;port&#039;.  Everything is by default installed in /opt/local so you can nuke that directory if you don&#039;t want it anymore.  It works by adding a line to your profile that adds that directory to your paths.&lt;br /&gt;
&lt;br /&gt;
port search packagename&lt;br /&gt;
&lt;br /&gt;
sudo port install py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I&#039;d install everything up to CLHEP from macports, then make my own geant4 installation.  I think there is a geant4 port on there, but I haven&#039;t used it.  For the geant4 installation you download hte source somewhere, make a build directory (outside the source) and then an install directory.  Usual CMake pattern.&lt;br /&gt;
&lt;br /&gt;
I&#039;d start with a high level package in macports and it&#039;ll work out all the requirements.  Like...&lt;br /&gt;
&lt;br /&gt;
sudo port install clhep qt5 py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I do everything apart from geant4 and bdsim through macports.&lt;br /&gt;
&lt;br /&gt;
Hope that helps,&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Summary from Raffy about EasyBuild, Environment Modules and GEANT4 =&lt;br /&gt;
== Geant4 ==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/gettingstarted.html&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
===GEANT4 OS/Software Prerequisites===&lt;br /&gt;
the following source/software must be present to build Geant4:&lt;br /&gt;
* Geant4 Toolkit Source Code.&lt;br /&gt;
* C++ Compiler and Standard Library supporting the C++11 Standard&lt;br /&gt;
* Linux (Scientific Linux CERN 6 and Linux CentOS 7 ): GNU Compiler Collection 4.8.5 or higher ( recommended to use the GCC compiler supplied by the package management system of your distribution unless this does not meet the minimum version requirement. The minimum required versions of GCC may be installed on SLC6 systems via the freeDeveloper Toolset)&lt;br /&gt;
* CMake 3.3 or higher (as provided through the package management system of your distribution, unless it does not meet the minimum version requirement.)&lt;br /&gt;
The main requirement is that the system has a GCC of sufficient version to support C++11 installed. &lt;br /&gt;
&lt;br /&gt;
===User Interface and Visualization Drivers===&lt;br /&gt;
* Qt User Interface and Visualization&lt;br /&gt;
** Qt4 (&amp;gt;=4.6) or Qt5 headers and libraries&lt;br /&gt;
* OpenGL or MesaGL headers and libraries.&lt;br /&gt;
** X11 OpenGL Visualization (Linux and macOS)&lt;br /&gt;
* Open Inventor Visualization&lt;br /&gt;
* Coin3D libraries and headers with SoXt(SoWin) graphics binding on Linux/macOS(Windows), compiled against the C++11 standard.&lt;br /&gt;
* Motif User Interface and Visualization (Linux and macOS)&lt;br /&gt;
&lt;br /&gt;
===Standard GEANT4 Building and Installation===&lt;br /&gt;
* Unpack the Geant4 source package geant4.10.05.tar.gz to a location of your choice: source directory /path/to/geant4.10.05&lt;br /&gt;
* create a directory in which to configure and run the build and store the build products /path/to/geant4.10.05-build&lt;br /&gt;
* $ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4.10.05-install /path/to/geant4.10.05&lt;br /&gt;
* CMake Variable CMAKE_INSTALL_PREFIX is used to set the install directory, the directory under which the Geant4 libraries, headers and support files will be installed. It must be supplied as an absolute path. The second argument to CMake is the path to the source directory.&lt;br /&gt;
* Additional arguments may be passed to CMake to activate optional components of Geant4, such as visualization drivers, or tune the build and install parameters.&lt;br /&gt;
*  If you run CMake and decide afterwards you want to activate additional options, simply rerun CMake in the build directory, passing it the extra options plus the build directory path. &lt;br /&gt;
* Datasets are not required to be present to build Geant4, but may be required to run your application, depending on the physics models you use. If you wish to download and install the datasets automatically as part of your build of Geant4, simply add the option-DGEANT4_INSTALL_DATA=ON to the arguments passed to CMake.&lt;br /&gt;
* After the configuration has run, CMake will have generated Unix Makefiles for building Geant4. To run the build, simply execute make in the build directory&lt;br /&gt;
* Once the build has completed, you can install Geant4 to the directory you specified earlier in CMAKE_INSTALL_PREFIX &lt;br /&gt;
* additional options can be passed to CMake to adjust the way Geant4 is built and installed and to enable optional components. Some options enable components of Geant4 which require external software.If these options are enabled, the required software will be searched for, and hence there are also options which control where CMake should look for these packages.These options may be set by passing their name and value to the cmake command via -D flags.&lt;br /&gt;
&lt;br /&gt;
===Standard Options===&lt;br /&gt;
* CMAKE_INSTALL_PREFIX installation prefix for Geant4. (default Unix: /usr/local)&lt;br /&gt;
* CMAKE_BUILD_TYPE : (DEFAULT : Release) Sets the type of build. It defaults to Release which gives a fully optimized build without debugging symbols.&lt;br /&gt;
* GEANT4_BUILD_MULTITHREADED : (DEFAULT : OFF) If set to ON, build Geant4 libraries with support for multithreading. At present, this is only supported on Unix systems. (Requires: Compiler/C++ Standard Library with support for C++ Threading Support)&lt;br /&gt;
* GEANT4_INSTALL_DATA : (DEFAULT : OFF)  to ON, download and install any Geant4 datasets missing from GEANT4_INSTALL_DATADIR.&lt;br /&gt;
* GEANT4_INSTALL_DATADIR : (DEFAULT : CMAKE_INSTALL_DATAROOTDIR) Installation directory for Geant4 datasets.&lt;br /&gt;
* GEANT4_USE_G3TOG4 : (DEFAULT : OFF) If set to ON, build the G3ToG4 library for reading ASCII call list files generated from Geant3 geometries.&lt;br /&gt;
* GEANT4_USE_GDML : (DEFAULT : OFF) If set to ON, build the G4persistency library with support for GDML.&lt;br /&gt;
* GEANT4_USE_QT (DEFAULT : OFF) If set to ON, build Qt4/5 User Interface and Visualization drivers. Requires: Qt4 or Qt5 and OpenGL libraries and headers. Qt5 will be searched for first. If it is not found Qt4 will be searched for. This behaviour can be adjusted through the advanced option GEANT4_FORCE_QT4.&lt;br /&gt;
* GEANT4_USE_OPENGL_X11 (DEFAULT : OFF, Unix Only)If set to ON, build the X11 OpenGL visualization driver.&lt;br /&gt;
* GEANT4_USE_RAYTRACER_X11 (DEFAULT : OFF, Unix only) If set to ON, build RayTracer visualization driver with X11 support.&lt;br /&gt;
* GEANT4_USE_INVENTOR (DEFAULT : OFF) If set to ON, build the OpenInventor visualization driver.&lt;br /&gt;
* GEANT4_USE_XM (DEFAULT : OFF, Unix Only) If set to ON, build Motif User Interface and Visualization drivers.&lt;br /&gt;
* GEANT4_USE_SYSTEM_CLHEP (DEFAULT : OFF) If set to ON, build Geant4 with an external install of CLHEP. You should not set this unless your usage of Geant4 mandates a specific external CLHEP installation (e.g. if your project’s software uses CLHEP in other tools and requires consistent use of the same CLHEP across the software stack).Requires: CLHEP libraries and headers. See the CLHEP_XXX and CMAKE_PREFIX_PATHAdvanced Options if CMake has trouble locating CLHEP.&lt;br /&gt;
* GEANT4_USE_SYSTEM_EXPAT (DEFAULT : ON) If set to ON, build Geant4 with an external install of Expat.&lt;br /&gt;
* GEANT4_USE_SYSTEM_ZLIB (DEFAULT : OFF) If set to ON, build Geant4 with an external install of zlib.&lt;br /&gt;
&lt;br /&gt;
===Advanced Options===&lt;br /&gt;
* http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
*Among the advanced, I list only the one used in the easy build recipe file https://bitbucket.org/jairhul/accsoft-eb/src/5f285502686e28d656dfa4d3ce1408d2551ee5d1/Geant4-10.00.p04-GCC-4.9.3.eb?at=master&amp;amp;fileviewer=file-view-default&lt;br /&gt;
** EXPAT_LIBRARY If CMake cannot locate your external EXPAT installation, set this to the full path to the EXPAT library, e.g. /usr/lib/libexpat.so&lt;br /&gt;
** EXPAT_INCLUDE_DIR If CMake cannot locate your external EXPAT installation, set this to the directory containing the EXPAT headers (e.g. if you have /foobar/expat.h, then set this to /foobar).&lt;br /&gt;
&lt;br /&gt;
== EasyBuild ==&lt;br /&gt;
===Reference===&lt;br /&gt;
https://media.readthedocs.org/pdf/caylo-easybuild/docfix/caylo-easybuild.pdf&lt;br /&gt;
EasyBuild is a flexible software build and installation framework that allows you to manage (scientific) software on High Performance Computing (HPC) systems in an efficient way.  Divert from the standard configure / make / make install with custom procedures. Allows for easily reproducing previous builds.&lt;br /&gt;
&lt;br /&gt;
===EasyBuild===&lt;br /&gt;
EasyBuild consists of a collection of Python modules and packages that interact with each other, dynamically picking up additional Python modules as needed for building and installing a (stack of) software package(s) specified via simple specification files&lt;br /&gt;
&lt;br /&gt;
In EasyBuild terminology: the EasyBuild framework leverages easyblocks to automatically build and install software using particular compiler toolchains, as specified by one or multiple easyconfig files.&lt;br /&gt;
&lt;br /&gt;
The EasyBuild framework provides functionality commonly needed when installing scientific software on HPC systems. For example, it deals with downloading, unpacking and patching of sources, loading module files for dependencies, setting up the build environment, autonomously running (interactive) shell commands, creating module files that match the specification files, etc.&lt;br /&gt;
The implementation of a particular software build and install procedure is done in a Python module, which is aptly referred to as an easyblock.&lt;br /&gt;
For each software package being built, the EasyBuild framework will determine which easyblock should be used, based on the name of the software package or the value of the easyblock specification parameter (see Easyblock specification). Since EasyBuild v2.0, an easyblock must be specified in case no matching easyblock is found based on the software name (cfr. Automagic fallback to ConfigureMake).&lt;br /&gt;
The specification files that are supplied to EasyBuild are referred to as easyconfig files (or simply easyconfigs .eb), which are basically plain text files containing (mostly) key-value assignments for build parameters supported by the framework, also referred to as easyconfig parameters (see Writing easyconfig files: the basics for more information).&lt;br /&gt;
An easyconfig file serves as a build specification for EasyBuild.&lt;br /&gt;
Easyconfigs typically follow a (fixed) strict naming scheme, i.e. &amp;lt;name&amp;gt;-&amp;lt;version&amp;gt;[-&amp;lt;toolchain&amp;gt;][&amp;lt;versionsuffix&amp;gt;&lt;br /&gt;
A handful of easyconfig parameters are mandatory:&lt;br /&gt;
* name, version: specify what software (version) to build&lt;br /&gt;
* homepage, description: metadata (used for module help)&lt;br /&gt;
* toolchain: specifies name and version of compiler toolchain to use&lt;br /&gt;
* Using external modules as dependencies&lt;br /&gt;
* Configure/build/install command options: &lt;br /&gt;
** configopts: options for configure command&lt;br /&gt;
** preconfigopts: options used as prefix for configure command&lt;br /&gt;
** buildopts, prebuildopts: options for build command&lt;br /&gt;
** installopts, preinstallopts: options for install &lt;br /&gt;
&lt;br /&gt;
EasyBuild allows to easily reproduce software builds/installations and install multiple versions.&lt;br /&gt;
&lt;br /&gt;
==Environment Modules==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://modules.sourceforge.net/man/module.html&lt;br /&gt;
===Environment Modules===&lt;br /&gt;
Typically users initialize their environment when they log in by setting environment information for every application they will reference during the session. &lt;br /&gt;
The Environment Modules system is a tool to help users manage their Unix or Linux shell environment, by allowing groups of related environment-variable settings to be made or removed dynamically. The modules system is based on modulefiles,[6] which specify groups of environment settings that need to be made together. Modulefiles can be installed in a central location for general use, or in a user directory for personal use. Environment Modules modulefiles are written in the Tcl (Tool Command Language) and are interpreted by the modulecmd program via the module[7] user interface. The key advantage of Environment Modules is that it is shell independent and supports all major shells such as bash, ksh, zsh, sh, tcsh, and csh. The second key advantage is that it allows to use multiple versions of the program or package from the same account by just loading proper module. modulefiles are created on per application per version basis. They can be dynamically loaded, unloaded, or switched. Along with the capability of using multiple versions of the same software it also can be used to implement site policies regarding the access and use of applications.&lt;br /&gt;
https://modules.readthedocs.io/en/latest/FAQ.html&lt;br /&gt;
&lt;br /&gt;
===New commands for centOS 7 ===&lt;br /&gt;
&lt;br /&gt;
# Tell module tool where the modules are&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
# Load Geant4&lt;br /&gt;
# This loads all the necessary libraries like CLHEP, Qt5, etc.&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
module load CMake/3.5.2-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;what does this mean&#039;&#039;&#039;&lt;br /&gt;
* module use [-a|--append] directory…&lt;br /&gt;
Prepend one or more directories to the MODULEPATH environment variable.&lt;br /&gt;
* load   modulefile...&lt;br /&gt;
Load modulefile(s) into the shell environment.&lt;br /&gt;
&lt;br /&gt;
when Geant4 is installed as a module&lt;br /&gt;
To see which versions are available, execute: module avail geant4&lt;br /&gt;
To then use a particular version, execute for example&lt;br /&gt;
module load geant4/10.02.p03&lt;br /&gt;
After you load the module, you can see where the software is installed by executing&lt;br /&gt;
echo $EBROOTGEANT4&lt;br /&gt;
&lt;br /&gt;
Set GEANT environment variables&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2296</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2296"/>
		<updated>2019-02-21T09:44:57Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Use modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
=== Set path to modules ===&lt;br /&gt;
Before modules can be accessed, the module tool needs to be given the path to the desired set of modules. For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== List available modules ===&lt;br /&gt;
You can then list all modules available with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or check what versions of a given module are available using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail [modulename]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;module avail Geant4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
prints out the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------------------------------------------------- /unix/pbt/software/eb/CentOS7/modules/phys --------------------------------------------------------------------&lt;br /&gt;
Geant4/10.00.p04-GCC-4.9.3    Geant4/10.01.p03-GCC-4.9.3    Geant4/10.02.p03-GCC-4.9.3    Geant4/10.03.p03-GCC-4.9.3    Geant4/10.04.p02-GCC-4.9.3    Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------- /unix/pbt/software/eb/CentOS7/modules/all ---------------------------------------------------------------------&lt;br /&gt;
Geant4/10.00.p04-GCC-4.9.3    Geant4/10.01.p03-GCC-4.9.3    Geant4/10.02.p03-GCC-4.9.3    Geant4/10.03.p03-GCC-4.9.3    Geant4/10.04.p02-GCC-4.9.3    Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Load modules ===&lt;br /&gt;
Load a given module using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and where required source the relevant environment variables, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Show loaded modules ===&lt;br /&gt;
To see what&#039;s loaded, use&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module list&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which prints, for example,&lt;br /&gt;
&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Remove loaded modules ===&lt;br /&gt;
To get rid of all loaded modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module purge&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
=== Installing modules ===&lt;br /&gt;
&lt;br /&gt;
Example command to install module:&lt;br /&gt;
&lt;br /&gt;
eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
Two types of the same version (i.e. one single-threaded and one multi-threaded) can be installed side-by-side. To get EasyBuild to not replace a current version, the module can be renamed in the EasyBuild file. When a module is built, EasyBuild checks for modules of the same name and version and if one already exists, the build won&#039;t complete. If the options are updated, the build will be updated.&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the `module use` command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
=== The question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I&#039;m trying to compile a multi-threaded version of Geant4 using EasyBuild but would like to make sure I don&#039;t break everything.&lt;br /&gt;
&lt;br /&gt;
Looking at the easyconfig file for the most recent Geant4 version installed on our system and the directory structure of the `eb/CentOS7/software` directory, I can see that the location of the installation depends on the name and the version of the package and on the toolchain used to compile it. This leads me to believe that if I were to set `GEANT4_BUILD_MULTITHREADED=ON` in the easyconfig file and run the following command,&lt;br /&gt;
&lt;br /&gt;
	eb Geant4-10.04.p02-GCC-4.9.3.eb --robot&lt;br /&gt;
&lt;br /&gt;
it would overwrite the current (non-mutli-threaded) version of Geant4. I have two questions relating to this:&lt;br /&gt;
&lt;br /&gt;
1) Am I correct in thinking it would overwrite it?&lt;br /&gt;
2) If so, would you mind showing me how I can compile it alongside the existing version?&lt;br /&gt;
&lt;br /&gt;
The best solution I could come up with would be to install it in a different location using the command line option `--installpath-software=INSTALLPATH-SOFTWARE` but I&#039;m unsure if that breaks compatibility with the modules tool and I&#039;d also like to avoid overwriting the existing easyconfig file, which I don&#039;t know how to ensure.&lt;br /&gt;
&lt;br /&gt;
= Note on how to install Geant4 on Mac =&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ah we tried it briefly on mac, but it was a little frustrating so we stuck with our usual way, which is macports.  For your mac, I&#039;d recommend that.  I know it&#039;s different than EasyBuild, but it&#039;s generally easier for mac and for scientific software.&lt;br /&gt;
&lt;br /&gt;
https://www.macports.org&lt;br /&gt;
&lt;br /&gt;
Before getting it you should xcode + the command line tools (from app store) installed (test is you can run &#039;make&#039; on the terminal).  You should also have XQuartz (https://www.xquartz.org) installed for X11 support.  Then download and install the macports software (small, little mac installer).  Gives you a command on the terminal &#039;port&#039;.  Everything is by default installed in /opt/local so you can nuke that directory if you don&#039;t want it anymore.  It works by adding a line to your profile that adds that directory to your paths.&lt;br /&gt;
&lt;br /&gt;
port search packagename&lt;br /&gt;
&lt;br /&gt;
sudo port install py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I&#039;d install everything up to CLHEP from macports, then make my own geant4 installation.  I think there is a geant4 port on there, but I haven&#039;t used it.  For the geant4 installation you download hte source somewhere, make a build directory (outside the source) and then an install directory.  Usual CMake pattern.&lt;br /&gt;
&lt;br /&gt;
I&#039;d start with a high level package in macports and it&#039;ll work out all the requirements.  Like...&lt;br /&gt;
&lt;br /&gt;
sudo port install clhep qt5 py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I do everything apart from geant4 and bdsim through macports.&lt;br /&gt;
&lt;br /&gt;
Hope that helps,&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Summary from Raffy about EasyBuild, Environment Modules and GEANT4 =&lt;br /&gt;
== Geant4 ==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/gettingstarted.html&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
===GEANT4 OS/Software Prerequisites===&lt;br /&gt;
the following source/software must be present to build Geant4:&lt;br /&gt;
* Geant4 Toolkit Source Code.&lt;br /&gt;
* C++ Compiler and Standard Library supporting the C++11 Standard&lt;br /&gt;
* Linux (Scientific Linux CERN 6 and Linux CentOS 7 ): GNU Compiler Collection 4.8.5 or higher ( recommended to use the GCC compiler supplied by the package management system of your distribution unless this does not meet the minimum version requirement. The minimum required versions of GCC may be installed on SLC6 systems via the freeDeveloper Toolset)&lt;br /&gt;
* CMake 3.3 or higher (as provided through the package management system of your distribution, unless it does not meet the minimum version requirement.)&lt;br /&gt;
The main requirement is that the system has a GCC of sufficient version to support C++11 installed. &lt;br /&gt;
&lt;br /&gt;
===User Interface and Visualization Drivers===&lt;br /&gt;
* Qt User Interface and Visualization&lt;br /&gt;
** Qt4 (&amp;gt;=4.6) or Qt5 headers and libraries&lt;br /&gt;
* OpenGL or MesaGL headers and libraries.&lt;br /&gt;
** X11 OpenGL Visualization (Linux and macOS)&lt;br /&gt;
* Open Inventor Visualization&lt;br /&gt;
* Coin3D libraries and headers with SoXt(SoWin) graphics binding on Linux/macOS(Windows), compiled against the C++11 standard.&lt;br /&gt;
* Motif User Interface and Visualization (Linux and macOS)&lt;br /&gt;
&lt;br /&gt;
===Standard GEANT4 Building and Installation===&lt;br /&gt;
* Unpack the Geant4 source package geant4.10.05.tar.gz to a location of your choice: source directory /path/to/geant4.10.05&lt;br /&gt;
* create a directory in which to configure and run the build and store the build products /path/to/geant4.10.05-build&lt;br /&gt;
* $ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4.10.05-install /path/to/geant4.10.05&lt;br /&gt;
* CMake Variable CMAKE_INSTALL_PREFIX is used to set the install directory, the directory under which the Geant4 libraries, headers and support files will be installed. It must be supplied as an absolute path. The second argument to CMake is the path to the source directory.&lt;br /&gt;
* Additional arguments may be passed to CMake to activate optional components of Geant4, such as visualization drivers, or tune the build and install parameters.&lt;br /&gt;
*  If you run CMake and decide afterwards you want to activate additional options, simply rerun CMake in the build directory, passing it the extra options plus the build directory path. &lt;br /&gt;
* Datasets are not required to be present to build Geant4, but may be required to run your application, depending on the physics models you use. If you wish to download and install the datasets automatically as part of your build of Geant4, simply add the option-DGEANT4_INSTALL_DATA=ON to the arguments passed to CMake.&lt;br /&gt;
* After the configuration has run, CMake will have generated Unix Makefiles for building Geant4. To run the build, simply execute make in the build directory&lt;br /&gt;
* Once the build has completed, you can install Geant4 to the directory you specified earlier in CMAKE_INSTALL_PREFIX &lt;br /&gt;
* additional options can be passed to CMake to adjust the way Geant4 is built and installed and to enable optional components. Some options enable components of Geant4 which require external software.If these options are enabled, the required software will be searched for, and hence there are also options which control where CMake should look for these packages.These options may be set by passing their name and value to the cmake command via -D flags.&lt;br /&gt;
&lt;br /&gt;
===Standard Options===&lt;br /&gt;
* CMAKE_INSTALL_PREFIX installation prefix for Geant4. (default Unix: /usr/local)&lt;br /&gt;
* CMAKE_BUILD_TYPE : (DEFAULT : Release) Sets the type of build. It defaults to Release which gives a fully optimized build without debugging symbols.&lt;br /&gt;
* GEANT4_BUILD_MULTITHREADED : (DEFAULT : OFF) If set to ON, build Geant4 libraries with support for multithreading. At present, this is only supported on Unix systems. (Requires: Compiler/C++ Standard Library with support for C++ Threading Support)&lt;br /&gt;
* GEANT4_INSTALL_DATA : (DEFAULT : OFF)  to ON, download and install any Geant4 datasets missing from GEANT4_INSTALL_DATADIR.&lt;br /&gt;
* GEANT4_INSTALL_DATADIR : (DEFAULT : CMAKE_INSTALL_DATAROOTDIR) Installation directory for Geant4 datasets.&lt;br /&gt;
* GEANT4_USE_G3TOG4 : (DEFAULT : OFF) If set to ON, build the G3ToG4 library for reading ASCII call list files generated from Geant3 geometries.&lt;br /&gt;
* GEANT4_USE_GDML : (DEFAULT : OFF) If set to ON, build the G4persistency library with support for GDML.&lt;br /&gt;
* GEANT4_USE_QT (DEFAULT : OFF) If set to ON, build Qt4/5 User Interface and Visualization drivers. Requires: Qt4 or Qt5 and OpenGL libraries and headers. Qt5 will be searched for first. If it is not found Qt4 will be searched for. This behaviour can be adjusted through the advanced option GEANT4_FORCE_QT4.&lt;br /&gt;
* GEANT4_USE_OPENGL_X11 (DEFAULT : OFF, Unix Only)If set to ON, build the X11 OpenGL visualization driver.&lt;br /&gt;
* GEANT4_USE_RAYTRACER_X11 (DEFAULT : OFF, Unix only) If set to ON, build RayTracer visualization driver with X11 support.&lt;br /&gt;
* GEANT4_USE_INVENTOR (DEFAULT : OFF) If set to ON, build the OpenInventor visualization driver.&lt;br /&gt;
* GEANT4_USE_XM (DEFAULT : OFF, Unix Only) If set to ON, build Motif User Interface and Visualization drivers.&lt;br /&gt;
* GEANT4_USE_SYSTEM_CLHEP (DEFAULT : OFF) If set to ON, build Geant4 with an external install of CLHEP. You should not set this unless your usage of Geant4 mandates a specific external CLHEP installation (e.g. if your project’s software uses CLHEP in other tools and requires consistent use of the same CLHEP across the software stack).Requires: CLHEP libraries and headers. See the CLHEP_XXX and CMAKE_PREFIX_PATHAdvanced Options if CMake has trouble locating CLHEP.&lt;br /&gt;
* GEANT4_USE_SYSTEM_EXPAT (DEFAULT : ON) If set to ON, build Geant4 with an external install of Expat.&lt;br /&gt;
* GEANT4_USE_SYSTEM_ZLIB (DEFAULT : OFF) If set to ON, build Geant4 with an external install of zlib.&lt;br /&gt;
&lt;br /&gt;
===Advanced Options===&lt;br /&gt;
* http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
*Among the advanced, I list only the one used in the easy build recipe file https://bitbucket.org/jairhul/accsoft-eb/src/5f285502686e28d656dfa4d3ce1408d2551ee5d1/Geant4-10.00.p04-GCC-4.9.3.eb?at=master&amp;amp;fileviewer=file-view-default&lt;br /&gt;
** EXPAT_LIBRARY If CMake cannot locate your external EXPAT installation, set this to the full path to the EXPAT library, e.g. /usr/lib/libexpat.so&lt;br /&gt;
** EXPAT_INCLUDE_DIR If CMake cannot locate your external EXPAT installation, set this to the directory containing the EXPAT headers (e.g. if you have /foobar/expat.h, then set this to /foobar).&lt;br /&gt;
&lt;br /&gt;
== EasyBuild ==&lt;br /&gt;
===Reference===&lt;br /&gt;
https://media.readthedocs.org/pdf/caylo-easybuild/docfix/caylo-easybuild.pdf&lt;br /&gt;
EasyBuild is a flexible software build and installation framework that allows you to manage (scientific) software on High Performance Computing (HPC) systems in an efficient way.  Divert from the standard configure / make / make install with custom procedures. Allows for easily reproducing previous builds.&lt;br /&gt;
&lt;br /&gt;
===EasyBuild===&lt;br /&gt;
EasyBuild consists of a collection of Python modules and packages that interact with each other, dynamically picking up additional Python modules as needed for building and installing a (stack of) software package(s) specified via simple specification files&lt;br /&gt;
&lt;br /&gt;
In EasyBuild terminology: the EasyBuild framework leverages easyblocks to automatically build and install software using particular compiler toolchains, as specified by one or multiple easyconfig files.&lt;br /&gt;
&lt;br /&gt;
The EasyBuild framework provides functionality commonly needed when installing scientific software on HPC systems. For example, it deals with downloading, unpacking and patching of sources, loading module files for dependencies, setting up the build environment, autonomously running (interactive) shell commands, creating module files that match the specification files, etc.&lt;br /&gt;
The implementation of a particular software build and install procedure is done in a Python module, which is aptly referred to as an easyblock.&lt;br /&gt;
For each software package being built, the EasyBuild framework will determine which easyblock should be used, based on the name of the software package or the value of the easyblock specification parameter (see Easyblock specification). Since EasyBuild v2.0, an easyblock must be specified in case no matching easyblock is found based on the software name (cfr. Automagic fallback to ConfigureMake).&lt;br /&gt;
The specification files that are supplied to EasyBuild are referred to as easyconfig files (or simply easyconfigs .eb), which are basically plain text files containing (mostly) key-value assignments for build parameters supported by the framework, also referred to as easyconfig parameters (see Writing easyconfig files: the basics for more information).&lt;br /&gt;
An easyconfig file serves as a build specification for EasyBuild.&lt;br /&gt;
Easyconfigs typically follow a (fixed) strict naming scheme, i.e. &amp;lt;name&amp;gt;-&amp;lt;version&amp;gt;[-&amp;lt;toolchain&amp;gt;][&amp;lt;versionsuffix&amp;gt;&lt;br /&gt;
A handful of easyconfig parameters are mandatory:&lt;br /&gt;
* name, version: specify what software (version) to build&lt;br /&gt;
* homepage, description: metadata (used for module help)&lt;br /&gt;
* toolchain: specifies name and version of compiler toolchain to use&lt;br /&gt;
* Using external modules as dependencies&lt;br /&gt;
* Configure/build/install command options: &lt;br /&gt;
** configopts: options for configure command&lt;br /&gt;
** preconfigopts: options used as prefix for configure command&lt;br /&gt;
** buildopts, prebuildopts: options for build command&lt;br /&gt;
** installopts, preinstallopts: options for install &lt;br /&gt;
&lt;br /&gt;
EasyBuild allows to easily reproduce software builds/installations and install multiple versions.&lt;br /&gt;
&lt;br /&gt;
==Environment Modules==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://modules.sourceforge.net/man/module.html&lt;br /&gt;
===Environment Modules===&lt;br /&gt;
Typically users initialize their environment when they log in by setting environment information for every application they will reference during the session. &lt;br /&gt;
The Environment Modules system is a tool to help users manage their Unix or Linux shell environment, by allowing groups of related environment-variable settings to be made or removed dynamically. The modules system is based on modulefiles,[6] which specify groups of environment settings that need to be made together. Modulefiles can be installed in a central location for general use, or in a user directory for personal use. Environment Modules modulefiles are written in the Tcl (Tool Command Language) and are interpreted by the modulecmd program via the module[7] user interface. The key advantage of Environment Modules is that it is shell independent and supports all major shells such as bash, ksh, zsh, sh, tcsh, and csh. The second key advantage is that it allows to use multiple versions of the program or package from the same account by just loading proper module. modulefiles are created on per application per version basis. They can be dynamically loaded, unloaded, or switched. Along with the capability of using multiple versions of the same software it also can be used to implement site policies regarding the access and use of applications.&lt;br /&gt;
https://modules.readthedocs.io/en/latest/FAQ.html&lt;br /&gt;
&lt;br /&gt;
===New commands for centOS 7 ===&lt;br /&gt;
&lt;br /&gt;
# Tell module tool where the modules are&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
# Load Geant4&lt;br /&gt;
# This loads all the necessary libraries like CLHEP, Qt5, etc.&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
module load CMake/3.5.2-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;what does this mean&#039;&#039;&#039;&lt;br /&gt;
* module use [-a|--append] directory…&lt;br /&gt;
Prepend one or more directories to the MODULEPATH environment variable.&lt;br /&gt;
* load   modulefile...&lt;br /&gt;
Load modulefile(s) into the shell environment.&lt;br /&gt;
&lt;br /&gt;
when Geant4 is installed as a module&lt;br /&gt;
To see which versions are available, execute: module avail geant4&lt;br /&gt;
To then use a particular version, execute for example&lt;br /&gt;
module load geant4/10.02.p03&lt;br /&gt;
After you load the module, you can see where the software is installed by executing&lt;br /&gt;
echo $EBROOTGEANT4&lt;br /&gt;
&lt;br /&gt;
Set GEANT environment variables&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2291</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2291"/>
		<updated>2019-02-20T19:38:13Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Install modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
As Simon says, I&#039;ve made modules (&#039;environment modules for unix&#039;) on the system using EasyBuild.  I&#039;ve installed the latest Geant4 for BDSIM (our application) but I&#039;ve also now put on the latest patch version of all recent Geant4 versions (was quite easy).  You can access them from a CentOS7 node (I&#039;m presuming Simon&#039;s here):&lt;br /&gt;
&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
module avail Geant4&lt;br /&gt;
&lt;br /&gt;
.. this prints out the following&lt;br /&gt;
&lt;br /&gt;
 ------------------------------------------------ /unix/pbt/software/eb/CentOS7/modules/all -------------------------------------------------&lt;br /&gt;
 Geant4/10.00.p04-GCC-4.9.3 Geant4/10.01.p03-GCC-4.9.3 Geant4/10.02.p03-GCC-4.9.3 Geant4/10.03.p03-GCC-4.9.3 Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
The last line sources the required environmental variable for Geant4 to be found.  This will load the corresponding CLHEP version required (multiple available).  &lt;br /&gt;
&lt;br /&gt;
To see what&#039;s loaded:&lt;br /&gt;
&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
Currently Loaded Modulefiles:&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
To get rid of these loaded modules:&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
&lt;br /&gt;
To see all modules:&lt;br /&gt;
&lt;br /&gt;
module avail&lt;br /&gt;
&lt;br /&gt;
There&#039;s the latest ROOT 6 on there too as well as a few versions of CLHEP (note Geant4 versions use different ones).&lt;br /&gt;
&lt;br /&gt;
The module use and module load should be done in each new environment / terminal.  &lt;br /&gt;
&lt;br /&gt;
I tried making an easybuild recipe for GATE, but it fails at the end (I even tried a patch to remove their hard-coded make files for gjm) but still no success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
=== Installing modules ===&lt;br /&gt;
&lt;br /&gt;
Example command to install module:&lt;br /&gt;
&lt;br /&gt;
eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
Two types of the same version (i.e. one single-threaded and one multi-threaded) can be installed side-by-side. To get EasyBuild to not replace a current version, the module can be renamed in the EasyBuild file. When a module is built, EasyBuild checks for modules of the same name and version and if one already exists, the build won&#039;t complete. If the options are updated, the build will be updated.&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the `module use` command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
=== The question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I&#039;m trying to compile a multi-threaded version of Geant4 using EasyBuild but would like to make sure I don&#039;t break everything.&lt;br /&gt;
&lt;br /&gt;
Looking at the easyconfig file for the most recent Geant4 version installed on our system and the directory structure of the `eb/CentOS7/software` directory, I can see that the location of the installation depends on the name and the version of the package and on the toolchain used to compile it. This leads me to believe that if I were to set `GEANT4_BUILD_MULTITHREADED=ON` in the easyconfig file and run the following command,&lt;br /&gt;
&lt;br /&gt;
	eb Geant4-10.04.p02-GCC-4.9.3.eb --robot&lt;br /&gt;
&lt;br /&gt;
it would overwrite the current (non-mutli-threaded) version of Geant4. I have two questions relating to this:&lt;br /&gt;
&lt;br /&gt;
1) Am I correct in thinking it would overwrite it?&lt;br /&gt;
2) If so, would you mind showing me how I can compile it alongside the existing version?&lt;br /&gt;
&lt;br /&gt;
The best solution I could come up with would be to install it in a different location using the command line option `--installpath-software=INSTALLPATH-SOFTWARE` but I&#039;m unsure if that breaks compatibility with the modules tool and I&#039;d also like to avoid overwriting the existing easyconfig file, which I don&#039;t know how to ensure.&lt;br /&gt;
&lt;br /&gt;
= Note on how to install Geant4 on Mac =&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ah we tried it briefly on mac, but it was a little frustrating so we stuck with our usual way, which is macports.  For your mac, I&#039;d recommend that.  I know it&#039;s different than EasyBuild, but it&#039;s generally easier for mac and for scientific software.&lt;br /&gt;
&lt;br /&gt;
https://www.macports.org&lt;br /&gt;
&lt;br /&gt;
Before getting it you should xcode + the command line tools (from app store) installed (test is you can run &#039;make&#039; on the terminal).  You should also have XQuartz (https://www.xquartz.org) installed for X11 support.  Then download and install the macports software (small, little mac installer).  Gives you a command on the terminal &#039;port&#039;.  Everything is by default installed in /opt/local so you can nuke that directory if you don&#039;t want it anymore.  It works by adding a line to your profile that adds that directory to your paths.&lt;br /&gt;
&lt;br /&gt;
port search packagename&lt;br /&gt;
&lt;br /&gt;
sudo port install py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I&#039;d install everything up to CLHEP from macports, then make my own geant4 installation.  I think there is a geant4 port on there, but I haven&#039;t used it.  For the geant4 installation you download hte source somewhere, make a build directory (outside the source) and then an install directory.  Usual CMake pattern.&lt;br /&gt;
&lt;br /&gt;
I&#039;d start with a high level package in macports and it&#039;ll work out all the requirements.  Like...&lt;br /&gt;
&lt;br /&gt;
sudo port install clhep qt5 py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I do everything apart from geant4 and bdsim through macports.&lt;br /&gt;
&lt;br /&gt;
Hope that helps,&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Summary from Raffy about EasyBuild, Environment Modules and GEANT4 =&lt;br /&gt;
== Geant4 ==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/gettingstarted.html&lt;br /&gt;
http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
===GEANT4 OS/Software Prerequisites===&lt;br /&gt;
the following source/software must be present to build Geant4:&lt;br /&gt;
* Geant4 Toolkit Source Code.&lt;br /&gt;
* C++ Compiler and Standard Library supporting the C++11 Standard&lt;br /&gt;
* Linux (Scientific Linux CERN 6 and Linux CentOS 7 ): GNU Compiler Collection 4.8.5 or higher ( recommended to use the GCC compiler supplied by the package management system of your distribution unless this does not meet the minimum version requirement. The minimum required versions of GCC may be installed on SLC6 systems via the freeDeveloper Toolset)&lt;br /&gt;
* CMake 3.3 or higher (as provided through the package management system of your distribution, unless it does not meet the minimum version requirement.)&lt;br /&gt;
The main requirement is that the system has a GCC of sufficient version to support C++11 installed. &lt;br /&gt;
&lt;br /&gt;
===User Interface and Visualization Drivers===&lt;br /&gt;
* Qt User Interface and Visualization&lt;br /&gt;
** Qt4 (&amp;gt;=4.6) or Qt5 headers and libraries&lt;br /&gt;
* OpenGL or MesaGL headers and libraries.&lt;br /&gt;
** X11 OpenGL Visualization (Linux and macOS)&lt;br /&gt;
* Open Inventor Visualization&lt;br /&gt;
* Coin3D libraries and headers with SoXt(SoWin) graphics binding on Linux/macOS(Windows), compiled against the C++11 standard.&lt;br /&gt;
* Motif User Interface and Visualization (Linux and macOS)&lt;br /&gt;
&lt;br /&gt;
===Standard GEANT4 Building and Installation===&lt;br /&gt;
* Unpack the Geant4 source package geant4.10.05.tar.gz to a location of your choice: source directory /path/to/geant4.10.05&lt;br /&gt;
* create a directory in which to configure and run the build and store the build products /path/to/geant4.10.05-build&lt;br /&gt;
* $ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4.10.05-install /path/to/geant4.10.05&lt;br /&gt;
* CMake Variable CMAKE_INSTALL_PREFIX is used to set the install directory, the directory under which the Geant4 libraries, headers and support files will be installed. It must be supplied as an absolute path. The second argument to CMake is the path to the source directory.&lt;br /&gt;
* Additional arguments may be passed to CMake to activate optional components of Geant4, such as visualization drivers, or tune the build and install parameters.&lt;br /&gt;
*  If you run CMake and decide afterwards you want to activate additional options, simply rerun CMake in the build directory, passing it the extra options plus the build directory path. &lt;br /&gt;
* Datasets are not required to be present to build Geant4, but may be required to run your application, depending on the physics models you use. If you wish to download and install the datasets automatically as part of your build of Geant4, simply add the option-DGEANT4_INSTALL_DATA=ON to the arguments passed to CMake.&lt;br /&gt;
* After the configuration has run, CMake will have generated Unix Makefiles for building Geant4. To run the build, simply execute make in the build directory&lt;br /&gt;
* Once the build has completed, you can install Geant4 to the directory you specified earlier in CMAKE_INSTALL_PREFIX &lt;br /&gt;
* additional options can be passed to CMake to adjust the way Geant4 is built and installed and to enable optional components. Some options enable components of Geant4 which require external software.If these options are enabled, the required software will be searched for, and hence there are also options which control where CMake should look for these packages.These options may be set by passing their name and value to the cmake command via -D flags.&lt;br /&gt;
&lt;br /&gt;
===Standard Options===&lt;br /&gt;
* CMAKE_INSTALL_PREFIX installation prefix for Geant4. (default Unix: /usr/local)&lt;br /&gt;
* CMAKE_BUILD_TYPE : (DEFAULT : Release) Sets the type of build. It defaults to Release which gives a fully optimized build without debugging symbols.&lt;br /&gt;
* GEANT4_BUILD_MULTITHREADED : (DEFAULT : OFF) If set to ON, build Geant4 libraries with support for multithreading. At present, this is only supported on Unix systems. (Requires: Compiler/C++ Standard Library with support for C++ Threading Support)&lt;br /&gt;
* GEANT4_INSTALL_DATA : (DEFAULT : OFF)  to ON, download and install any Geant4 datasets missing from GEANT4_INSTALL_DATADIR.&lt;br /&gt;
* GEANT4_INSTALL_DATADIR : (DEFAULT : CMAKE_INSTALL_DATAROOTDIR) Installation directory for Geant4 datasets.&lt;br /&gt;
* GEANT4_USE_G3TOG4 : (DEFAULT : OFF) If set to ON, build the G3ToG4 library for reading ASCII call list files generated from Geant3 geometries.&lt;br /&gt;
* GEANT4_USE_GDML : (DEFAULT : OFF) If set to ON, build the G4persistency library with support for GDML.&lt;br /&gt;
* GEANT4_USE_QT (DEFAULT : OFF) If set to ON, build Qt4/5 User Interface and Visualization drivers. Requires: Qt4 or Qt5 and OpenGL libraries and headers. Qt5 will be searched for first. If it is not found Qt4 will be searched for. This behaviour can be adjusted through the advanced option GEANT4_FORCE_QT4.&lt;br /&gt;
* GEANT4_USE_OPENGL_X11 (DEFAULT : OFF, Unix Only)If set to ON, build the X11 OpenGL visualization driver.&lt;br /&gt;
* GEANT4_USE_RAYTRACER_X11 (DEFAULT : OFF, Unix only) If set to ON, build RayTracer visualization driver with X11 support.&lt;br /&gt;
* GEANT4_USE_INVENTOR (DEFAULT : OFF) If set to ON, build the OpenInventor visualization driver.&lt;br /&gt;
* GEANT4_USE_XM (DEFAULT : OFF, Unix Only) If set to ON, build Motif User Interface and Visualization drivers.&lt;br /&gt;
* GEANT4_USE_SYSTEM_CLHEP (DEFAULT : OFF) If set to ON, build Geant4 with an external install of CLHEP. You should not set this unless your usage of Geant4 mandates a specific external CLHEP installation (e.g. if your project’s software uses CLHEP in other tools and requires consistent use of the same CLHEP across the software stack).Requires: CLHEP libraries and headers. See the CLHEP_XXX and CMAKE_PREFIX_PATHAdvanced Options if CMake has trouble locating CLHEP.&lt;br /&gt;
* GEANT4_USE_SYSTEM_EXPAT (DEFAULT : ON) If set to ON, build Geant4 with an external install of Expat.&lt;br /&gt;
* GEANT4_USE_SYSTEM_ZLIB (DEFAULT : OFF) If set to ON, build Geant4 with an external install of zlib.&lt;br /&gt;
&lt;br /&gt;
===Advanced Options===&lt;br /&gt;
* http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/InstallationGuide/html/installguide.html&lt;br /&gt;
*Among the advanced, I list only the one used in the easy build recipe file https://bitbucket.org/jairhul/accsoft-eb/src/5f285502686e28d656dfa4d3ce1408d2551ee5d1/Geant4-10.00.p04-GCC-4.9.3.eb?at=master&amp;amp;fileviewer=file-view-default&lt;br /&gt;
** EXPAT_LIBRARY If CMake cannot locate your external EXPAT installation, set this to the full path to the EXPAT library, e.g. /usr/lib/libexpat.so&lt;br /&gt;
** EXPAT_INCLUDE_DIR If CMake cannot locate your external EXPAT installation, set this to the directory containing the EXPAT headers (e.g. if you have /foobar/expat.h, then set this to /foobar).&lt;br /&gt;
&lt;br /&gt;
== EasyBuild ==&lt;br /&gt;
===Reference===&lt;br /&gt;
https://media.readthedocs.org/pdf/caylo-easybuild/docfix/caylo-easybuild.pdf&lt;br /&gt;
EasyBuild is a flexible software build and installation framework that allows you to manage (scientific) software on High Performance Computing (HPC) systems in an efficient way.  Divert from the standard configure / make / make install with custom procedures. Allows for easily reproducing previous builds.&lt;br /&gt;
&lt;br /&gt;
===EasyBuild===&lt;br /&gt;
EasyBuild consists of a collection of Python modules and packages that interact with each other, dynamically picking up additional Python modules as needed for building and installing a (stack of) software package(s) specified via simple specification files&lt;br /&gt;
&lt;br /&gt;
In EasyBuild terminology: the EasyBuild framework leverages easyblocks to automatically build and install software using particular compiler toolchains, as specified by one or multiple easyconfig files.&lt;br /&gt;
&lt;br /&gt;
The EasyBuild framework provides functionality commonly needed when installing scientific software on HPC systems. For example, it deals with downloading, unpacking and patching of sources, loading module files for dependencies, setting up the build environment, autonomously running (interactive) shell commands, creating module files that match the specification files, etc.&lt;br /&gt;
The implementation of a particular software build and install procedure is done in a Python module, which is aptly referred to as an easyblock.&lt;br /&gt;
For each software package being built, the EasyBuild framework will determine which easyblock should be used, based on the name of the software package or the value of the easyblock specification parameter (see Easyblock specification). Since EasyBuild v2.0, an easyblock must be specified in case no matching easyblock is found based on the software name (cfr. Automagic fallback to ConfigureMake).&lt;br /&gt;
The specification files that are supplied to EasyBuild are referred to as easyconfig files (or simply easyconfigs .eb), which are basically plain text files containing (mostly) key-value assignments for build parameters supported by the framework, also referred to as easyconfig parameters (see Writing easyconfig files: the basics for more information).&lt;br /&gt;
An easyconfig file serves as a build specification for EasyBuild.&lt;br /&gt;
Easyconfigs typically follow a (fixed) strict naming scheme, i.e. &amp;lt;name&amp;gt;-&amp;lt;version&amp;gt;[-&amp;lt;toolchain&amp;gt;][&amp;lt;versionsuffix&amp;gt;&lt;br /&gt;
A handful of easyconfig parameters are mandatory:&lt;br /&gt;
* name, version: specify what software (version) to build&lt;br /&gt;
* homepage, description: metadata (used for module help)&lt;br /&gt;
* toolchain: specifies name and version of compiler toolchain to use&lt;br /&gt;
* Using external modules as dependencies&lt;br /&gt;
* Configure/build/install command options: &lt;br /&gt;
** configopts: options for configure command&lt;br /&gt;
** preconfigopts: options used as prefix for configure command&lt;br /&gt;
** buildopts, prebuildopts: options for build command&lt;br /&gt;
** installopts, preinstallopts: options for install &lt;br /&gt;
&lt;br /&gt;
EasyBuild allows to easily reproduce software builds/installations and install multiple versions.&lt;br /&gt;
&lt;br /&gt;
==Environment Modules==&lt;br /&gt;
===Reference===&lt;br /&gt;
http://modules.sourceforge.net/man/module.html&lt;br /&gt;
===Environment Modules===&lt;br /&gt;
Typically users initialize their environment when they log in by setting environment information for every application they will reference during the session. &lt;br /&gt;
The Environment Modules system is a tool to help users manage their Unix or Linux shell environment, by allowing groups of related environment-variable settings to be made or removed dynamically. The modules system is based on modulefiles,[6] which specify groups of environment settings that need to be made together. Modulefiles can be installed in a central location for general use, or in a user directory for personal use. Environment Modules modulefiles are written in the Tcl (Tool Command Language) and are interpreted by the modulecmd program via the module[7] user interface. The key advantage of Environment Modules is that it is shell independent and supports all major shells such as bash, ksh, zsh, sh, tcsh, and csh. The second key advantage is that it allows to use multiple versions of the program or package from the same account by just loading proper module. modulefiles are created on per application per version basis. They can be dynamically loaded, unloaded, or switched. Along with the capability of using multiple versions of the same software it also can be used to implement site policies regarding the access and use of applications.&lt;br /&gt;
https://modules.readthedocs.io/en/latest/FAQ.html&lt;br /&gt;
&lt;br /&gt;
===New commands for centOS 7 ===&lt;br /&gt;
&lt;br /&gt;
# Tell module tool where the modules are&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
# Load Geant4&lt;br /&gt;
# This loads all the necessary libraries like CLHEP, Qt5, etc.&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
module load CMake/3.5.2-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;what does this mean&#039;&#039;&#039;&lt;br /&gt;
* module use [-a|--append] directory…&lt;br /&gt;
Prepend one or more directories to the MODULEPATH environment variable.&lt;br /&gt;
* load   modulefile...&lt;br /&gt;
Load modulefile(s) into the shell environment.&lt;br /&gt;
&lt;br /&gt;
when Geant4 is installed as a module&lt;br /&gt;
To see which versions are available, execute: module avail geant4&lt;br /&gt;
To then use a particular version, execute for example&lt;br /&gt;
module load geant4/10.02.p03&lt;br /&gt;
After you load the module, you can see where the software is installed by executing&lt;br /&gt;
echo $EBROOTGEANT4&lt;br /&gt;
&lt;br /&gt;
Set GEANT environment variables&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2289</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2289"/>
		<updated>2019-02-19T12:19:11Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Lower priority */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Write code for benchmarking metrics analysis: CDF, noise and relative error&lt;br /&gt;
** Design more appropriate phantom to evaluate MTF (cf. Plautz et al., 2016 and Mori and Machida, 2009)&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms (Update 18/02/2019: Ask Robert Johnson (rjohnson@ucsc.edu) for access to GitHub repo.)&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
* Write script to move the required number of projection files into the current directory&lt;br /&gt;
* Edit script &#039;backproject&#039; to take an argument for the number of projections used&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;br /&gt;
* (01/02/2019) Implement phantoms to be used to determine MTF and CDF in Geant4: one uniform phantom with a single central non-uniform pixel and another truly uniform phantom&lt;br /&gt;
* (14/02/2019) Write code for benchmarking metrics analysis: MTF&lt;br /&gt;
&lt;br /&gt;
== Current Reading List ==&lt;br /&gt;
&lt;br /&gt;
=== Image quality assessment ===&lt;br /&gt;
* Plautz et al. (including Schulte) 2016 An evaluation of spatial resolution of a prototype proton CT scanner &#039;&#039;Med. Phys.&#039;&#039; &#039;&#039;&#039;43&#039;&#039;&#039; (12)&lt;br /&gt;
&lt;br /&gt;
=== Proton CT ===&lt;br /&gt;
* Paganetti H 2012 Range uncertainties in proton therapy and the role of Monte Carlo simulations &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;57&#039;&#039;&#039;&lt;br /&gt;
* Johnson R P 2018 Review of medical radiography and tomography with proton beams &#039;&#039;Rep. Prog. Phys.&#039;&#039; &#039;&#039;&#039;81&#039;&#039;&#039;&lt;br /&gt;
* Arbor N &#039;&#039;et al.&#039;&#039; 2015 Monte Carlo comparison of x-ray and proton CT for range calculations of proton therapy beams &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;60&#039;&#039;&#039;&lt;br /&gt;
* Rädler M 2018 Two-dimensional noise reconstruction in proton computed tomography using distance-driven filtered back-projection of simulated projections &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;63&#039;&#039;&#039;&lt;br /&gt;
* Brooke M and Penfold S 2018 An inhomogeneous most likely path formalism for proton computed tomography &#039;&#039;arXiv:1808.00122v1&#039;&#039;&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2288</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2288"/>
		<updated>2019-02-19T10:28:07Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Write code for benchmarking metrics analysis: CDF, noise and relative error&lt;br /&gt;
** Design more appropriate phantom to evaluate MTF (cf. Plautz et al., 2016 and Mori and Machida, 2009)&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms (Update 18/02/2019: Ask Robert Johnson (rjohnson@ucsc.edu) for access to GitHub repo.)&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;br /&gt;
* (01/02/2019) Implement phantoms to be used to determine MTF and CDF in Geant4: one uniform phantom with a single central non-uniform pixel and another truly uniform phantom&lt;br /&gt;
* (14/02/2019) Write code for benchmarking metrics analysis: MTF&lt;br /&gt;
&lt;br /&gt;
== Current Reading List ==&lt;br /&gt;
&lt;br /&gt;
=== Image quality assessment ===&lt;br /&gt;
* Plautz et al. (including Schulte) 2016 An evaluation of spatial resolution of a prototype proton CT scanner &#039;&#039;Med. Phys.&#039;&#039; &#039;&#039;&#039;43&#039;&#039;&#039; (12)&lt;br /&gt;
&lt;br /&gt;
=== Proton CT ===&lt;br /&gt;
* Paganetti H 2012 Range uncertainties in proton therapy and the role of Monte Carlo simulations &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;57&#039;&#039;&#039;&lt;br /&gt;
* Johnson R P 2018 Review of medical radiography and tomography with proton beams &#039;&#039;Rep. Prog. Phys.&#039;&#039; &#039;&#039;&#039;81&#039;&#039;&#039;&lt;br /&gt;
* Arbor N &#039;&#039;et al.&#039;&#039; 2015 Monte Carlo comparison of x-ray and proton CT for range calculations of proton therapy beams &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;60&#039;&#039;&#039;&lt;br /&gt;
* Rädler M 2018 Two-dimensional noise reconstruction in proton computed tomography using distance-driven filtered back-projection of simulated projections &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;63&#039;&#039;&#039;&lt;br /&gt;
* Brooke M and Penfold S 2018 An inhomogeneous most likely path formalism for proton computed tomography &#039;&#039;arXiv:1808.00122v1&#039;&#039;&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2229</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2229"/>
		<updated>2019-02-12T14:11:22Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
As Simon says, I&#039;ve made modules (&#039;environment modules for unix&#039;) on the system using EasyBuild.  I&#039;ve installed the latest Geant4 for BDSIM (our application) but I&#039;ve also now put on the latest patch version of all recent Geant4 versions (was quite easy).  You can access them from a CentOS7 node (I&#039;m presuming Simon&#039;s here):&lt;br /&gt;
&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
module avail Geant4&lt;br /&gt;
&lt;br /&gt;
.. this prints out the following&lt;br /&gt;
&lt;br /&gt;
 ------------------------------------------------ /unix/pbt/software/eb/CentOS7/modules/all -------------------------------------------------&lt;br /&gt;
 Geant4/10.00.p04-GCC-4.9.3 Geant4/10.01.p03-GCC-4.9.3 Geant4/10.02.p03-GCC-4.9.3 Geant4/10.03.p03-GCC-4.9.3 Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
The last line sources the required environmental variable for Geant4 to be found.  This will load the corresponding CLHEP version required (multiple available).  &lt;br /&gt;
&lt;br /&gt;
To see what&#039;s loaded:&lt;br /&gt;
&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
Currently Loaded Modulefiles:&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
To get rid of these loaded modules:&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
&lt;br /&gt;
To see all modules:&lt;br /&gt;
&lt;br /&gt;
module avail&lt;br /&gt;
&lt;br /&gt;
There&#039;s the latest ROOT 6 on there too as well as a few versions of CLHEP (note Geant4 versions use different ones).&lt;br /&gt;
&lt;br /&gt;
The module use and module load should be done in each new environment / terminal.  &lt;br /&gt;
&lt;br /&gt;
I tried making an easybuild recipe for GATE, but it fails at the end (I even tried a patch to remove their hard-coded make files for gjm) but still no success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
=== Installing modules ===&lt;br /&gt;
Hello all,&lt;br /&gt;
&lt;br /&gt;
- BDSIM has to use the single threaded version -&amp;gt; &amp;quot;high throughput&amp;quot; only vs &amp;quot;high performance&amp;quot;&lt;br /&gt;
- BDSIM requires external CLHEP so Geant4&#039;s internal one isn&#039;t mixed in -&amp;gt; otherwise, whilst BDSIM would work, the random number generator found is unclear and the results are not strongly reproducible.&lt;br /&gt;
&lt;br /&gt;
However, if you just want a multithreaded Geant4 build for your own application... yes you can make a separate easybuild file.  In fact the full command I&#039;ve used to construct a module is (for example):&lt;br /&gt;
&lt;br /&gt;
eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
The trick to get it to not replace a current version is to rename the module in the easybuild file.  When you make one, easybuild checks for ones of the same name and version and if one with the same name and recipe is built it won&#039;t build it.  If the options are updated, I believe it will update the build.  Anyway, if you change the name it will create a new one.  We used the name also as part of the source URL, so you should just hard code that.  I&#039;ve made an example here:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/accsoft-eb-nevay/Geant4-10.04.p02-mt-GCC-4.9.3.eb&lt;br /&gt;
(copied from the regular geant4 one, turned multithreading on, changed version to have &amp;quot;-mt&amp;quot; and hardcoded source url)&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the module use command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
=== The question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I&#039;m trying to compile a multi-threaded version of Geant4 using EasyBuild but would like to make sure I don&#039;t break everything.&lt;br /&gt;
&lt;br /&gt;
Looking at the easyconfig file for the most recent Geant4 version installed on our system and the directory structure of the `eb/CentOS7/software` directory, I can see that the location of the installation depends on the name and the version of the package and on the toolchain used to compile it. This leads me to believe that if I were to set `GEANT4_BUILD_MULTITHREADED=ON` in the easyconfig file and run the following command,&lt;br /&gt;
&lt;br /&gt;
	eb Geant4-10.04.p02-GCC-4.9.3.eb --robot&lt;br /&gt;
&lt;br /&gt;
it would overwrite the current (non-mutli-threaded) version of Geant4. I have two questions relating to this:&lt;br /&gt;
&lt;br /&gt;
1) Am I correct in thinking it would overwrite it?&lt;br /&gt;
2) If so, would you mind showing me how I can compile it alongside the existing version?&lt;br /&gt;
&lt;br /&gt;
The best solution I could come up with would be to install it in a different location using the command line option `--installpath-software=INSTALLPATH-SOFTWARE` but I&#039;m unsure if that breaks compatibility with the modules tool and I&#039;d also like to avoid overwriting the existing easyconfig file, which I don&#039;t know how to ensure.&lt;br /&gt;
&lt;br /&gt;
= Note on how to install Geant4 on Mac =&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ah we tried it briefly on mac, but it was a little frustrating so we stuck with our usual way, which is macports.  For your mac, I&#039;d recommend that.  I know it&#039;s different than EasyBuild, but it&#039;s generally easier for mac and for scientific software.&lt;br /&gt;
&lt;br /&gt;
https://www.macports.org&lt;br /&gt;
&lt;br /&gt;
Before getting it you should xcode + the command line tools (from app store) installed (test is you can run &#039;make&#039; on the terminal).  You should also have XQuartz (https://www.xquartz.org) installed for X11 support.  Then download and install the macports software (small, little mac installer).  Gives you a command on the terminal &#039;port&#039;.  Everything is by default installed in /opt/local so you can nuke that directory if you don&#039;t want it anymore.  It works by adding a line to your profile that adds that directory to your paths.&lt;br /&gt;
&lt;br /&gt;
port search packagename&lt;br /&gt;
&lt;br /&gt;
sudo port install py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I&#039;d install everything up to CLHEP from macports, then make my own geant4 installation.  I think there is a geant4 port on there, but I haven&#039;t used it.  For the geant4 installation you download hte source somewhere, make a build directory (outside the source) and then an install directory.  Usual CMake pattern.&lt;br /&gt;
&lt;br /&gt;
I&#039;d start with a high level package in macports and it&#039;ll work out all the requirements.  Like...&lt;br /&gt;
&lt;br /&gt;
sudo port install clhep qt5 py27-matplotlib&lt;br /&gt;
&lt;br /&gt;
I do everything apart from geant4 and bdsim through macports.&lt;br /&gt;
&lt;br /&gt;
Hope that helps,&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2228</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2228"/>
		<updated>2019-02-12T14:10:44Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Install modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
As Simon says, I&#039;ve made modules (&#039;environment modules for unix&#039;) on the system using EasyBuild.  I&#039;ve installed the latest Geant4 for BDSIM (our application) but I&#039;ve also now put on the latest patch version of all recent Geant4 versions (was quite easy).  You can access them from a CentOS7 node (I&#039;m presuming Simon&#039;s here):&lt;br /&gt;
&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
module avail Geant4&lt;br /&gt;
&lt;br /&gt;
.. this prints out the following&lt;br /&gt;
&lt;br /&gt;
 ------------------------------------------------ /unix/pbt/software/eb/CentOS7/modules/all -------------------------------------------------&lt;br /&gt;
 Geant4/10.00.p04-GCC-4.9.3 Geant4/10.01.p03-GCC-4.9.3 Geant4/10.02.p03-GCC-4.9.3 Geant4/10.03.p03-GCC-4.9.3 Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
The last line sources the required environmental variable for Geant4 to be found.  This will load the corresponding CLHEP version required (multiple available).  &lt;br /&gt;
&lt;br /&gt;
To see what&#039;s loaded:&lt;br /&gt;
&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
Currently Loaded Modulefiles:&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
To get rid of these loaded modules:&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
&lt;br /&gt;
To see all modules:&lt;br /&gt;
&lt;br /&gt;
module avail&lt;br /&gt;
&lt;br /&gt;
There&#039;s the latest ROOT 6 on there too as well as a few versions of CLHEP (note Geant4 versions use different ones).&lt;br /&gt;
&lt;br /&gt;
The module use and module load should be done in each new environment / terminal.  &lt;br /&gt;
&lt;br /&gt;
I tried making an easybuild recipe for GATE, but it fails at the end (I even tried a patch to remove their hard-coded make files for gjm) but still no success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
=== Installing modules ===&lt;br /&gt;
Hello all,&lt;br /&gt;
&lt;br /&gt;
- BDSIM has to use the single threaded version -&amp;gt; &amp;quot;high throughput&amp;quot; only vs &amp;quot;high performance&amp;quot;&lt;br /&gt;
- BDSIM requires external CLHEP so Geant4&#039;s internal one isn&#039;t mixed in -&amp;gt; otherwise, whilst BDSIM would work, the random number generator found is unclear and the results are not strongly reproducible.&lt;br /&gt;
&lt;br /&gt;
However, if you just want a multithreaded Geant4 build for your own application... yes you can make a separate easybuild file.  In fact the full command I&#039;ve used to construct a module is (for example):&lt;br /&gt;
&lt;br /&gt;
eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
The trick to get it to not replace a current version is to rename the module in the easybuild file.  When you make one, easybuild checks for ones of the same name and version and if one with the same name and recipe is built it won&#039;t build it.  If the options are updated, I believe it will update the build.  Anyway, if you change the name it will create a new one.  We used the name also as part of the source URL, so you should just hard code that.  I&#039;ve made an example here:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/accsoft-eb-nevay/Geant4-10.04.p02-mt-GCC-4.9.3.eb&lt;br /&gt;
(copied from the regular geant4 one, turned multithreading on, changed version to have &amp;quot;-mt&amp;quot; and hardcoded source url)&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the module use command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
=== The question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I&#039;m trying to compile a multi-threaded version of Geant4 using EasyBuild but would like to make sure I don&#039;t break everything.&lt;br /&gt;
&lt;br /&gt;
Looking at the easyconfig file for the most recent Geant4 version installed on our system and the directory structure of the `eb/CentOS7/software` directory, I can see that the location of the installation depends on the name and the version of the package and on the toolchain used to compile it. This leads me to believe that if I were to set `GEANT4_BUILD_MULTITHREADED=ON` in the easyconfig file and run the following command,&lt;br /&gt;
&lt;br /&gt;
	eb Geant4-10.04.p02-GCC-4.9.3.eb --robot&lt;br /&gt;
&lt;br /&gt;
it would overwrite the current (non-mutli-threaded) version of Geant4. I have two questions relating to this:&lt;br /&gt;
&lt;br /&gt;
1) Am I correct in thinking it would overwrite it?&lt;br /&gt;
2) If so, would you mind showing me how I can compile it alongside the existing version?&lt;br /&gt;
&lt;br /&gt;
The best solution I could come up with would be to install it in a different location using the command line option `--installpath-software=INSTALLPATH-SOFTWARE` but I&#039;m unsure if that breaks compatibility with the modules tool and I&#039;d also like to avoid overwriting the existing easyconfig file, which I don&#039;t know how to ensure.&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2227</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2227"/>
		<updated>2019-02-12T14:08:53Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Compile BDSIM */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
=== James Chappell&#039;s question ===&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
=== The answer ===&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
=== Additional info ===&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
As Simon says, I&#039;ve made modules (&#039;environment modules for unix&#039;) on the system using EasyBuild.  I&#039;ve installed the latest Geant4 for BDSIM (our application) but I&#039;ve also now put on the latest patch version of all recent Geant4 versions (was quite easy).  You can access them from a CentOS7 node (I&#039;m presuming Simon&#039;s here):&lt;br /&gt;
&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
module avail Geant4&lt;br /&gt;
&lt;br /&gt;
.. this prints out the following&lt;br /&gt;
&lt;br /&gt;
 ------------------------------------------------ /unix/pbt/software/eb/CentOS7/modules/all -------------------------------------------------&lt;br /&gt;
 Geant4/10.00.p04-GCC-4.9.3 Geant4/10.01.p03-GCC-4.9.3 Geant4/10.02.p03-GCC-4.9.3 Geant4/10.03.p03-GCC-4.9.3 Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
The last line sources the required environmental variable for Geant4 to be found.  This will load the corresponding CLHEP version required (multiple available).  &lt;br /&gt;
&lt;br /&gt;
To see what&#039;s loaded:&lt;br /&gt;
&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
Currently Loaded Modulefiles:&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
To get rid of these loaded modules:&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
&lt;br /&gt;
To see all modules:&lt;br /&gt;
&lt;br /&gt;
module avail&lt;br /&gt;
&lt;br /&gt;
There&#039;s the latest ROOT 6 on there too as well as a few versions of CLHEP (note Geant4 versions use different ones).&lt;br /&gt;
&lt;br /&gt;
The module use and module load should be done in each new environment / terminal.  &lt;br /&gt;
&lt;br /&gt;
I tried making an easybuild recipe for GATE, but it fails at the end (I even tried a patch to remove their hard-coded make files for gjm) but still no success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
* BDSIM has to use the single threaded version -&amp;gt; &amp;quot;high throughput&amp;quot; only vs &amp;quot;high performance&amp;quot;&lt;br /&gt;
* BDSIM requires external CLHEP so Geant4&#039;s internal one isn&#039;t mixed in -&amp;gt; otherwise, whilst BDSIM would work, the random number generator found is unclear and the results are not strongly reproducible.&lt;br /&gt;
&lt;br /&gt;
Example command: eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
The trick to get it to not replace a current version is to rename the module in the easybuild file.  When you make one, easybuild checks for ones of the same name and version and if one with the same name and recipe is built it won&#039;t build it.  If the options are updated, I believe it will update the build.  Anyway, if you change the name it will create a new one.  We used the name also as part of the source URL, so you should just hard code that.  I&#039;ve made an example here:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/accsoft-eb-nevay/Geant4-10.04.p02-mt-GCC-4.9.3.eb&lt;br /&gt;
(copied from the regular geant4 one, turned multithreading on, changed version to have &amp;quot;-mt&amp;quot; and hardcoded source url)&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the module use command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2226</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2226"/>
		<updated>2019-02-12T14:08:25Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Compile BDSIM */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
== James Chappell&#039;s question ==&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
== The answer ==&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
== Additional info ==&lt;br /&gt;
Hello,&lt;br /&gt;
&lt;br /&gt;
Ok, I&#039;ve updated the general BDSIM build in easy build now.  It should have the AWAKE module turned on.  Note, this is the code of v1.1 and not the latest develop version.&lt;br /&gt;
&lt;br /&gt;
I made an example environment only script in:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/bdsim-own-build.sh&lt;br /&gt;
&lt;br /&gt;
I tried making my own build and it worked just fine.  I only needed to do also:&lt;br /&gt;
&lt;br /&gt;
module load CMake&lt;br /&gt;
&lt;br /&gt;
to get a more up to date version. From my home dir:&lt;br /&gt;
&lt;br /&gt;
mkdir build-custom-build&lt;br /&gt;
cd build-custom-build&lt;br /&gt;
cmake ../bdsim&lt;br /&gt;
# check compiler is GCC4.9.3 form easy build, and ROOT and Geant4 and CLHEP -&amp;gt; all ok&lt;br /&gt;
ccmake .&lt;br /&gt;
turn on USE_AWAKE&lt;br /&gt;
&amp;lt;c&amp;gt;&lt;br /&gt;
&amp;lt;g&amp;gt;&lt;br /&gt;
make -j15&lt;br /&gt;
works!&lt;br /&gt;
&lt;br /&gt;
It&#039;s in /home/lnevay/bdsim-custom-build&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
As Simon says, I&#039;ve made modules (&#039;environment modules for unix&#039;) on the system using EasyBuild.  I&#039;ve installed the latest Geant4 for BDSIM (our application) but I&#039;ve also now put on the latest patch version of all recent Geant4 versions (was quite easy).  You can access them from a CentOS7 node (I&#039;m presuming Simon&#039;s here):&lt;br /&gt;
&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
module avail Geant4&lt;br /&gt;
&lt;br /&gt;
.. this prints out the following&lt;br /&gt;
&lt;br /&gt;
 ------------------------------------------------ /unix/pbt/software/eb/CentOS7/modules/all -------------------------------------------------&lt;br /&gt;
 Geant4/10.00.p04-GCC-4.9.3 Geant4/10.01.p03-GCC-4.9.3 Geant4/10.02.p03-GCC-4.9.3 Geant4/10.03.p03-GCC-4.9.3 Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
The last line sources the required environmental variable for Geant4 to be found.  This will load the corresponding CLHEP version required (multiple available).  &lt;br /&gt;
&lt;br /&gt;
To see what&#039;s loaded:&lt;br /&gt;
&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
Currently Loaded Modulefiles:&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
To get rid of these loaded modules:&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
&lt;br /&gt;
To see all modules:&lt;br /&gt;
&lt;br /&gt;
module avail&lt;br /&gt;
&lt;br /&gt;
There&#039;s the latest ROOT 6 on there too as well as a few versions of CLHEP (note Geant4 versions use different ones).&lt;br /&gt;
&lt;br /&gt;
The module use and module load should be done in each new environment / terminal.  &lt;br /&gt;
&lt;br /&gt;
I tried making an easybuild recipe for GATE, but it fails at the end (I even tried a patch to remove their hard-coded make files for gjm) but still no success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
* BDSIM has to use the single threaded version -&amp;gt; &amp;quot;high throughput&amp;quot; only vs &amp;quot;high performance&amp;quot;&lt;br /&gt;
* BDSIM requires external CLHEP so Geant4&#039;s internal one isn&#039;t mixed in -&amp;gt; otherwise, whilst BDSIM would work, the random number generator found is unclear and the results are not strongly reproducible.&lt;br /&gt;
&lt;br /&gt;
Example command: eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
The trick to get it to not replace a current version is to rename the module in the easybuild file.  When you make one, easybuild checks for ones of the same name and version and if one with the same name and recipe is built it won&#039;t build it.  If the options are updated, I believe it will update the build.  Anyway, if you change the name it will create a new one.  We used the name also as part of the source URL, so you should just hard code that.  I&#039;ve made an example here:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/accsoft-eb-nevay/Geant4-10.04.p02-mt-GCC-4.9.3.eb&lt;br /&gt;
(copied from the regular geant4 one, turned multithreading on, changed version to have &amp;quot;-mt&amp;quot; and hardcoded source url)&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the module use command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2225</id>
		<title>Software/EasyBuild</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Software/EasyBuild&amp;diff=2225"/>
		<updated>2019-02-12T14:07:39Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Instructions for using software with EasyBuild.=&lt;br /&gt;
&lt;br /&gt;
= Compile BDSIM =&lt;br /&gt;
== James Chappell&#039;s question ==&lt;br /&gt;
Dear Laurie,&lt;br /&gt;
&lt;br /&gt;
I’m trying to learn how to use the version of easy build that you setup to run some BDSIM simulations for AWAKE. I have a few questions for you.&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
2) Having looked through your eb files in /unix/pbt/software/eb/accsoft-eb-nevay, I can see that you define sources using a url and I’m assuming that in the process of compiling all the software, this source code is downloaded and then compiled. Is there a way to define the source from within the local system directories and then compile this version? Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
Sorry for all the questions, I’m just trying to get my head around how it all works! Thanks for your help,&lt;br /&gt;
James.&lt;br /&gt;
&lt;br /&gt;
== The answer ==&lt;br /&gt;
Dear James,&lt;br /&gt;
&lt;br /&gt;
1) The version of bdsim that you wrote the shell script for within /unix/pbt/software/eb/bdsim.sh does not have the AWAKE module compiled. Do you know if there is another version that I can link to which does for testing purposes for now? &lt;br /&gt;
&lt;br /&gt;
My bad -&amp;gt; I&#039;ll recompile it with awake on.  I need a time slot when you won&#039;t be using it as it&#039;ll disappear during the rebuilding - pretty bad if someone&#039;s running jobs when I do it!  Let me know when I can do this - only takes  about 15 mins.  &lt;br /&gt;
&lt;br /&gt;
Is there a way to define the source from within the local system directories and then compile this version? &lt;br /&gt;
It looks like the system really only is designed to download things from what I can read online...  Despite the url, it caches them in a sources directory, so if it was already there it would use that (a fudge though).&lt;br /&gt;
&lt;br /&gt;
Furthermore, to compile the AWAKE module, normally I use “ccmake .” to edit the compilation options - is the equivalent using the “configopts” command within an *.eb script? I’m assuming the command will be something like: “-USE_AWAKE=on”? &lt;br /&gt;
&lt;br /&gt;
We have so far in the Bdsim-1.1-GCC-4.9.4.eb easy build file:&lt;br /&gt;
&lt;br /&gt;
configopts = &#039; -DCMAKE_CXX_FLAGS=&amp;quot;-std=c++11&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
We can add to this:&lt;br /&gt;
&lt;br /&gt;
configopts += &#039;-DUSE_AWAKE=ON&#039;&lt;br /&gt;
&lt;br /&gt;
See ROOT-v6.08.06-GCC-4.9.3-Python-2.7.12.eb for lots of examples.&lt;br /&gt;
&lt;br /&gt;
3) For our simulations, we edit the source code of the AWAKE module to make changes to the spectrometer and then recompile BDSIM before using it. How will this setup need to change when using easy build? Will it require recompiling the whole system of dependencies every time (I can see from your email to Simon previously that this took 5 hours)? Is it the case that we can compile everything up to BDSIM in one place (i.e. Geant4 and all its dependencies), and then just load our own version of BDSIM (or even just the AWAKE module) on top of this?&lt;br /&gt;
&lt;br /&gt;
If you&#039;re modifying the C++ often I&#039;d make your own build of BDSIM + AWAKE separately from the EasyBuild system and not bother trying to package it into a module. The module system is really for things that won&#039;t change.&lt;br /&gt;
&lt;br /&gt;
You can get all the dependencies from EasyBuild and then make your own independent build of whatever you want.  I think this was done for Gate for example and we do this just now for our nightly testing of BDSIM.  I load the BDSIM module, then unload it - this leaves all the dependencies loaded.&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
module load Bdsim&lt;br /&gt;
module unload Bdsim&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
# still has all libraries loaded including Geant4 etc..&lt;br /&gt;
# somewhere else, make a build dir and build from your source.&lt;br /&gt;
&lt;br /&gt;
When you load Geant4 and ROOT, note you should really source their setup scripts:&lt;br /&gt;
&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
source $EBROOTROOT/bin/thisroot.sh&lt;br /&gt;
&lt;br /&gt;
---&amp;gt;&amp;gt;&amp;gt; You should make a copy of the bdsim.sh script that does most of this and just add the unload to the end.&lt;br /&gt;
&lt;br /&gt;
then...&lt;br /&gt;
&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
cmake ../relative/path/to/bdsim -DCMAKE_CXX_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/g++&lt;br /&gt;
 -DCMAKE_C_COMPILER=/home/accsoft/SL68/software/GCC/4.9.3/bin/gcc&lt;br /&gt;
 -DCLHEP_LIBRARY_DIR=/home/accsoft/SL68/software/CLHEP/2.3.1.1-GCC-4.9.3/lib/&lt;br /&gt;
make -j10&lt;br /&gt;
&lt;br /&gt;
* paths are example on my system at RHUL&lt;br /&gt;
&lt;br /&gt;
Normally you wouldn&#039;t have to specify these options as CMake would find these on its own. However, practice shows it often finds a system compiler before it finds the one in easybuild (this is required to be the same for all the software to be consistent), so we explicitly specify them.&lt;br /&gt;
&lt;br /&gt;
Note once a build is make and it finds a compiler, the compiler can never be changed in CMake.  You must wipe the build directory contents to try again.&lt;br /&gt;
&lt;br /&gt;
How do you use this new build?  Simple, you must have the same environment loaded as when it was built (so hence the modified bdsim.sh) then execute from that directory.  This could easily be done in your shell script for the simulation or via an alias (fictional paths here as an example)...&lt;br /&gt;
&lt;br /&gt;
alias bdsim-awake-devel=&amp;quot;/home/jchappell/my-builds/build123/bdsim&amp;quot;&lt;br /&gt;
bdsim-awake-devel --help&lt;br /&gt;
&lt;br /&gt;
Note if you haven&#039;t changed the output or the analysis you can just use the regular bdsim from easybuild if you do any analysis later on the output.  If you have changed it, you&#039;ll need to use your build and modify your bdsim.sh - pretty clear where to change for the analysis stuff.&lt;br /&gt;
&lt;br /&gt;
Ok, hopefully that should help you both get the build working and help you understand.&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Use modules =&lt;br /&gt;
As Simon says, I&#039;ve made modules (&#039;environment modules for unix&#039;) on the system using EasyBuild.  I&#039;ve installed the latest Geant4 for BDSIM (our application) but I&#039;ve also now put on the latest patch version of all recent Geant4 versions (was quite easy).  You can access them from a CentOS7 node (I&#039;m presuming Simon&#039;s here):&lt;br /&gt;
&lt;br /&gt;
module use /unix/pbt/software/eb/CentOS7/modules/*&lt;br /&gt;
module avail Geant4&lt;br /&gt;
&lt;br /&gt;
.. this prints out the following&lt;br /&gt;
&lt;br /&gt;
 ------------------------------------------------ /unix/pbt/software/eb/CentOS7/modules/all -------------------------------------------------&lt;br /&gt;
 Geant4/10.00.p04-GCC-4.9.3 Geant4/10.01.p03-GCC-4.9.3 Geant4/10.02.p03-GCC-4.9.3 Geant4/10.03.p03-GCC-4.9.3 Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;br /&gt;
&lt;br /&gt;
The last line sources the required environmental variable for Geant4 to be found.  This will load the corresponding CLHEP version required (multiple available).  &lt;br /&gt;
&lt;br /&gt;
To see what&#039;s loaded:&lt;br /&gt;
&lt;br /&gt;
module list&lt;br /&gt;
&lt;br /&gt;
Currently Loaded Modulefiles:&lt;br /&gt;
 1) GCC/4.9.3                            9) bzip2/1.0.6-GCC-4.9.3               17) Mesa/11.2.1-GCC-4.9.3&lt;br /&gt;
 2) expat/2.1.1-GCC-4.9.3               10) freetype/2.6.3-GCC-4.9.3            18) libGLU/9.0.0-GCC-4.9.3-Mesa-11.2.1&lt;br /&gt;
 3) CLHEP/2.3.3.0-GCC-4.9.3             11) fontconfig/2.11.95-GCC-4.9.3        19) libffi/3.2.1-GCC-4.9.3&lt;br /&gt;
 4) cURL/7.44.0-GCC-4.9.3               12) X11/20160819-GCC-4.9.3              20) gettext/0.19.6-GCC-4.9.3&lt;br /&gt;
 5) Xerces-C++/3.1.2-GCC-4.9.3          13) libdrm/2.4.68-GCC-4.9.3             21) PCRE/8.38-GCC-4.9.3&lt;br /&gt;
 6) zlib/1.2.8-GCC-4.9.3                14) ncurses/6.0-GCC-4.9.3               22) GLib/2.49.5-GCC-4.9.3&lt;br /&gt;
 7) libxml2/2.9.3-GCC-4.9.3             15) LLVM/3.8.0-GCC-4.9.3                23) Qt5/5.7.0-GCC-4.9.3&lt;br /&gt;
 8) libpng/1.6.21-GCC-4.9.3             16) eudev/3.1.5-GCC-4.9.3               24) Geant4/10.04.p02-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
To get rid of these loaded modules:&lt;br /&gt;
&lt;br /&gt;
module purge&lt;br /&gt;
&lt;br /&gt;
To see all modules:&lt;br /&gt;
&lt;br /&gt;
module avail&lt;br /&gt;
&lt;br /&gt;
There&#039;s the latest ROOT 6 on there too as well as a few versions of CLHEP (note Geant4 versions use different ones).&lt;br /&gt;
&lt;br /&gt;
The module use and module load should be done in each new environment / terminal.  &lt;br /&gt;
&lt;br /&gt;
I tried making an easybuild recipe for GATE, but it fails at the end (I even tried a patch to remove their hard-coded make files for gjm) but still no success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Install modules =&lt;br /&gt;
&lt;br /&gt;
* BDSIM has to use the single threaded version -&amp;gt; &amp;quot;high throughput&amp;quot; only vs &amp;quot;high performance&amp;quot;&lt;br /&gt;
* BDSIM requires external CLHEP so Geant4&#039;s internal one isn&#039;t mixed in -&amp;gt; otherwise, whilst BDSIM would work, the random number generator found is unclear and the results are not strongly reproducible.&lt;br /&gt;
&lt;br /&gt;
Example command: eb Bdsim-1.2.1-GCC-4.9.3.eb --prefix=/unix/pbt/software/eb/CentOS7 -robot --robot-path=./: --modules-tool=EnvironmentModulesC --module-syntax=Tcl --allow-modules-tool-mismatch --optarch=GENERIC --buildpath=/tmp/easybuild/&lt;br /&gt;
&lt;br /&gt;
The trick to get it to not replace a current version is to rename the module in the easybuild file.  When you make one, easybuild checks for ones of the same name and version and if one with the same name and recipe is built it won&#039;t build it.  If the options are updated, I believe it will update the build.  Anyway, if you change the name it will create a new one.  We used the name also as part of the source URL, so you should just hard code that.  I&#039;ve made an example here:&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/accsoft-eb-nevay/Geant4-10.04.p02-mt-GCC-4.9.3.eb&lt;br /&gt;
(copied from the regular geant4 one, turned multithreading on, changed version to have &amp;quot;-mt&amp;quot; and hardcoded source url)&lt;br /&gt;
&lt;br /&gt;
When you build a module, you should do the module use command so it can find the others already available (it checks the requirements of the one you&#039;re building) and also the EasyBuild module itself.  You should have a clean environment with no other modules loaded.&lt;br /&gt;
&lt;br /&gt;
/unix/pbt/software/eb/easybuildonly.sh&lt;br /&gt;
&lt;br /&gt;
seems to provide this.&lt;br /&gt;
&lt;br /&gt;
I tried my example and found you should edit the version rather than the name for it to work.  I&#039;ve done this and it&#039;s setup now.  You can import it as.&lt;br /&gt;
&lt;br /&gt;
module load Geant4/10.04.p02-mt-GCC-4.9.3&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
Laurie&lt;br /&gt;
&lt;br /&gt;
PS we keep a note of the big incantation.  You could get around most of these with some environmental variables, but we got with this so far.&lt;br /&gt;
&lt;br /&gt;
PPS, remember you should source the geant4.sh from that installation before use.&lt;br /&gt;
source $EBROOTGEANT4/bin/geant4.sh&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2195</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2195"/>
		<updated>2019-02-06T14:50:15Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Completed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Write code for benchmarking metrics analysis: MTF, CDF, noise and relative error&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;br /&gt;
* (01/02/2018) Implement phantoms to be used to determine MTF and CDF in Geant4: one uniform phantom with a single central non-uniform pixel and another truly uniform phantom&lt;br /&gt;
&lt;br /&gt;
== Current Reading List ==&lt;br /&gt;
* Paganetti H 2012 Range uncertainties in proton therapy and the role of Monte Carlo simulations &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;57&#039;&#039;&#039;&lt;br /&gt;
* Johnson R P 2018 Review of medical radiography and tomography with proton beams &#039;&#039;Rep. Prog. Phys.&#039;&#039; &#039;&#039;&#039;81&#039;&#039;&#039;&lt;br /&gt;
* Arbor N &#039;&#039;et al.&#039;&#039; 2015 Monte Carlo comparison of x-ray and proton CT for range calculations of proton therapy beams &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;60&#039;&#039;&#039;&lt;br /&gt;
* Rädler M 2018 Two-dimensional noise reconstruction in proton computed tomography using distance-driven filtered back-projection of simulated projections &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;63&#039;&#039;&#039;&lt;br /&gt;
* Brooke M and Penfold S 2018 An inhomogeneous most likely path formalism for proton computed tomography &#039;&#039;arXiv:1808.00122v1&#039;&#039;&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2194</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2194"/>
		<updated>2019-02-06T14:50:01Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Completed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Write code for benchmarking metrics analysis: MTF, CDF, noise and relative error&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;br /&gt;
* Implement phantoms to be used to determine MTF and CDF in Geant4: one uniform phantom with a single central non-uniform pixel and another truly uniform phantom (01/02/2018)&lt;br /&gt;
&lt;br /&gt;
== Current Reading List ==&lt;br /&gt;
* Paganetti H 2012 Range uncertainties in proton therapy and the role of Monte Carlo simulations &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;57&#039;&#039;&#039;&lt;br /&gt;
* Johnson R P 2018 Review of medical radiography and tomography with proton beams &#039;&#039;Rep. Prog. Phys.&#039;&#039; &#039;&#039;&#039;81&#039;&#039;&#039;&lt;br /&gt;
* Arbor N &#039;&#039;et al.&#039;&#039; 2015 Monte Carlo comparison of x-ray and proton CT for range calculations of proton therapy beams &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;60&#039;&#039;&#039;&lt;br /&gt;
* Rädler M 2018 Two-dimensional noise reconstruction in proton computed tomography using distance-driven filtered back-projection of simulated projections &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;63&#039;&#039;&#039;&lt;br /&gt;
* Brooke M and Penfold S 2018 An inhomogeneous most likely path formalism for proton computed tomography &#039;&#039;arXiv:1808.00122v1&#039;&#039;&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2193</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2193"/>
		<updated>2019-02-06T14:49:27Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* To Do */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Write code for benchmarking metrics analysis: MTF, CDF, noise and relative error&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;br /&gt;
&lt;br /&gt;
== Current Reading List ==&lt;br /&gt;
* Paganetti H 2012 Range uncertainties in proton therapy and the role of Monte Carlo simulations &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;57&#039;&#039;&#039;&lt;br /&gt;
* Johnson R P 2018 Review of medical radiography and tomography with proton beams &#039;&#039;Rep. Prog. Phys.&#039;&#039; &#039;&#039;&#039;81&#039;&#039;&#039;&lt;br /&gt;
* Arbor N &#039;&#039;et al.&#039;&#039; 2015 Monte Carlo comparison of x-ray and proton CT for range calculations of proton therapy beams &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;60&#039;&#039;&#039;&lt;br /&gt;
* Rädler M 2018 Two-dimensional noise reconstruction in proton computed tomography using distance-driven filtered back-projection of simulated projections &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;63&#039;&#039;&#039;&lt;br /&gt;
* Brooke M and Penfold S 2018 An inhomogeneous most likely path formalism for proton computed tomography &#039;&#039;arXiv:1808.00122v1&#039;&#039;&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2105</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2105"/>
		<updated>2019-01-24T11:06:03Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Implement phantoms to be used to determine MTF and CDF in Geant4: one uniform phantom with a single central non-uniform pixel and another truly uniform phantom&lt;br /&gt;
* Write code for benchmarking metrics analysis: MTF, CDF, noise and relative error&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;br /&gt;
&lt;br /&gt;
== Current Reading List ==&lt;br /&gt;
* Paganetti H 2012 Range uncertainties in proton therapy and the role of Monte Carlo simulations &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;57&#039;&#039;&#039;&lt;br /&gt;
* Johnson R P 2018 Review of medical radiography and tomography with proton beams &#039;&#039;Rep. Prog. Phys.&#039;&#039; &#039;&#039;&#039;81&#039;&#039;&#039;&lt;br /&gt;
* Arbor N &#039;&#039;et al.&#039;&#039; 2015 Monte Carlo comparison of x-ray and proton CT for range calculations of proton therapy beams &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;60&#039;&#039;&#039;&lt;br /&gt;
* Rädler M 2018 Two-dimensional noise reconstruction in proton computed tomography using distance-driven filtered back-projection of simulated projections &#039;&#039;Phys. Med. Biol.&#039;&#039; &#039;&#039;&#039;63&#039;&#039;&#039;&lt;br /&gt;
* Brooke M and Penfold S 2018 An inhomogeneous most likely path formalism for proton computed tomography &#039;&#039;arXiv:1808.00122v1&#039;&#039;&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2104</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=2104"/>
		<updated>2019-01-23T19:38:59Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* To Do */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
=== Urgent ===&lt;br /&gt;
* Nada&lt;br /&gt;
&lt;br /&gt;
=== Higher priority ===&lt;br /&gt;
* Work on progress report&lt;br /&gt;
* Implement phantoms to be used to determine MTF and CDF in Geant4: one uniform phantom with a single central non-uniform pixel and another truly uniform phantom&lt;br /&gt;
* Write code for benchmarking metrics analysis: MTF, CDF, noise and relative error&lt;br /&gt;
* Get in touch with R Schulte to get access to DROP and TVS-DROP algorithms&lt;br /&gt;
* Run preliminary simulations to test the metrics&lt;br /&gt;
&lt;br /&gt;
=== Lower priority ===&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=1934</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=1934"/>
		<updated>2019-01-14T11:48:00Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;br /&gt;
* Write progress report&lt;br /&gt;
* Do reconstruction of simulation with &amp;gt;1 billion protons&lt;br /&gt;
* Convert ITK images to DICOM&lt;br /&gt;
* Assign ID to each proton to simplify matching&lt;br /&gt;
** Concatenating run ID and event ID to a long string does not work as the string is truncated for some reason.&lt;br /&gt;
** Alternative: Use a short hash? -&amp;gt; Need a hash function&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Completed ==&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=1933</id>
		<title>ELogs/MatthieuHentz</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=ELogs/MatthieuHentz&amp;diff=1933"/>
		<updated>2019-01-14T11:30:48Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Electronic Log for Matthieu Hentz&lt;br /&gt;
&lt;br /&gt;
== To Do ==&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1602</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1602"/>
		<updated>2018-03-28T16:39:35Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Building the simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Building the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir Clatterbridge_sim&lt;br /&gt;
[username@pc1XX ~]$ cd Clatterbridge_sim&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ mkdir build&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, the simulation can be cloned from the [https://github.com/mhentz/Clatterbridge_sim.git GitHub repository] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ git clone https://github.com/mhentz/Clatterbridge_sim.git&lt;br /&gt;
[username@pc1XX ~]$ cd Clatterbridge_sim&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ mkdir build&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When cloning the repository, the executable scripts will be created without execute permissions. These must be given using&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ chmod 755 *.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
[[Clatterbridge files|List of source files.]]&lt;br /&gt;
&lt;br /&gt;
Click here to download source as a zip: [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim.zip download]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Proton_Calorimetry/Meetings/2018/03/14&amp;diff=1601</id>
		<title>Proton Calorimetry/Meetings/2018/03/14</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Proton_Calorimetry/Meetings/2018/03/14&amp;diff=1601"/>
		<updated>2018-03-28T16:33:20Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Present */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Minutes for UCL Proton Calorimetry Meeting, 14th March 2018 (D17, Physics &amp;amp; Astronomy, UCL) ==&lt;br /&gt;
&lt;br /&gt;
=== Present ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Anastasia Basharina-Freshville&#039;&#039;&#039;, &#039;&#039;&#039;Dan Walker&#039;&#039;&#039;, &#039;&#039;&#039;Jordan Silverman&#039;&#039;&#039;, &#039;&#039;&#039;Laurent Kelleter&#039;&#039;&#039;, &#039;&#039;&#039;Matthieu Hentz&#039;&#039;&#039;, &#039;&#039;&#039;Simon Jolly&#039;&#039;&#039;, &#039;&#039;&#039;Ruben Saakyan&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;RS&#039;&#039;&#039;:&lt;br /&gt;
* Interested in seeing dose deposition vs depth, even if not calibrated, dump into histogram and check what we see from both sensors.&lt;br /&gt;
* Seeing huge variations in current. Is this problem for us?&lt;br /&gt;
** If variation of current affects relative variation in sheets then it&#039;s a problem.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; suggest you can see variation in current but can wash it out if picking a 5 ms exposure on dlsr.&lt;br /&gt;
* Consider 300 um scintillator sheets for very fine resolution around Bragg peak.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; and &#039;&#039;&#039;LK&#039;&#039;&#039; in perfect unison: this is fine for single energy but problematic when scanning different energies.&lt;br /&gt;
* Good opportunity to check 5-stage tube if time allows.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;MH&#039;&#039;&#039;:&lt;br /&gt;
* Finished writing documentation for simulation.&lt;br /&gt;
* Working on next coursework and CDT group project.&lt;br /&gt;
* Jacinta Jap would like to know:&lt;br /&gt;
**Where are the beam parameters used in the Clatterbridge simulation from?&lt;br /&gt;
*** &#039;&#039;&#039;SJ&#039;&#039;&#039; obtained Excel spreadsheet of Twiss parameters from Hywel Owen. As the scattering perturbs the beam a lot the shape should not be crucial. Making sure the energy is right is more important.&lt;br /&gt;
** Who wrote the MCNPX simulation?&lt;br /&gt;
*** &#039;&#039;&#039;SJ&#039;&#039;&#039; only recalls Colin Baker using MCNPX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;JS&#039;&#039;&#039;:&lt;br /&gt;
* Sucessfully converted CAD model to a GDML file.&lt;br /&gt;
* Asks if he should import it into the simulation before the report is due (deadline is 26 March)?&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; suggests to work on it until Monday 19 March and focus on writing report from then onwards.&lt;br /&gt;
** &#039;&#039;&#039;JS&#039;&#039;&#039; knows what steps are required, only needs to implement them.&lt;br /&gt;
** &#039;&#039;&#039;MH&#039;&#039;&#039; suggests getting rid of beamline definition in DetectorConstruction, import GDML file and reintroduce any missing elements one by one for easier debugging.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DW&#039;&#039;&#039;:&lt;br /&gt;
* Will be around next year as he received offer from CDT in Quantum Technologies (woo woo).&lt;br /&gt;
* Comments on Medaustron run:&lt;br /&gt;
** Timestamps are separated by 3 minutes 38 seconds, i.e. our data taken after theirs. There are no sensible matches between datasets.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; explains that absence of discrete trigger means no concrete correlation between the two data sets. Adjusting trigger levels throws away events so may end up with different numbers between calorimeter and tracker.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; suggest that it won&#039;t matter in write up for project. DW can show spectra and should also focus on writing after Monday evening (19 March).&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Proton_Calorimetry/Meetings/2018/03/14&amp;diff=1575</id>
		<title>Proton Calorimetry/Meetings/2018/03/14</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Proton_Calorimetry/Meetings/2018/03/14&amp;diff=1575"/>
		<updated>2018-03-28T10:33:41Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Present */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Minutes for UCL Proton Calorimetry Meeting, 14th March 2018 (D17, Physics &amp;amp; Astronomy, UCL) ==&lt;br /&gt;
&lt;br /&gt;
=== Present ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Anastasia Basharina-Freshville&#039;&#039;&#039;, &#039;&#039;&#039;Dan Walker&#039;&#039;&#039;, &#039;&#039;&#039;Jordan Silverman&#039;&#039;&#039;, &#039;&#039;&#039;Laurent Kelleter&#039;&#039;&#039;, &#039;&#039;&#039;Matthieu Hentz&#039;&#039;&#039;, &#039;&#039;&#039;Simon Jolly&#039;&#039;&#039;, &#039;&#039;&#039;Ruben Saakyan&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;RS&#039;&#039;&#039;:&lt;br /&gt;
* Interested in seeing dose deposition vs depth, even if not calibrated, dump into histogram and check what we see from both sensors.&lt;br /&gt;
* Seeing huge variations in current. Is this problem for us?&lt;br /&gt;
** If variation of current affects relative variation in sheets then it&#039;s a problem.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; suggest you can see variation in current but can wash it out if picking a 5 ms exposure on dlsr.&lt;br /&gt;
* Consider 300 um scintillator sheets for very fine resolution around Bragg peak.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; and &#039;&#039;&#039;LK&#039;&#039;&#039; in perfect unison: this is fine for single energy but problematic when scanning different energies.&lt;br /&gt;
* Good opportunity to check 5-stage tube if time allows.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;MH&#039;&#039;&#039;:&lt;br /&gt;
* Finished writing documentation for simulation.&lt;br /&gt;
* Working on next coursework and CDT group project.&lt;br /&gt;
* Jacinta Jap would like to know:&lt;br /&gt;
** Where are the beam parameters used in the Clatterbridge simulation from?&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; obtained Excel spreadsheet of Twiss parameters from Hywel Owen. As the scattering perturbs the beam a lot the shape should not be crucial. Making sure the energy is right is more important.&lt;br /&gt;
* Who wrote the MCNPX simulation?&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; only recalls Colin Baker using MCNPX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;JS&#039;&#039;&#039;:&lt;br /&gt;
* Sucessfully converted CAD model to a GDML file.&lt;br /&gt;
* Asks if he should import it into the simulation before the report is due (deadline is 26 March)?&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; suggests to work on it until Monday 19 March and focus on writing report from then onwards.&lt;br /&gt;
** &#039;&#039;&#039;JS&#039;&#039;&#039; knows what steps are required, only needs to implement them.&lt;br /&gt;
** &#039;&#039;&#039;MH&#039;&#039;&#039; suggests getting rid of beamline definition in DetectorConstruction, import GDML file and reintroduce any missing elements one by one for easier debugging.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DW&#039;&#039;&#039;:&lt;br /&gt;
* Will be around next year as he received offer from CDT in Quantum Technologies (woo woo).&lt;br /&gt;
* Comments on Medaustron run:&lt;br /&gt;
** Timestamps are separated by 3 minutes 38 seconds, i.e. our data taken after theirs. There are no sensible matches between datasets.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; explains that absence of discrete trigger means no concrete correlation between the two data sets. Adjusting trigger levels throws away events so may end up with different numbers between calorimeter and tracker.&lt;br /&gt;
** &#039;&#039;&#039;SJ&#039;&#039;&#039; suggest that it won&#039;t matter in write up for project. DW can show spectra and should also focus on writing after Monday evening (19 March).&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1534</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1534"/>
		<updated>2018-03-18T09:37:46Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Data analysis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge Wiki page]&lt;br /&gt;
&lt;br /&gt;
== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
=== Parallel Simulations ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
=== Data analysis ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/analysis.py analysis.py]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/bragg.py bragg.py]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/lateral.py lateral.py]&lt;br /&gt;
&lt;br /&gt;
== Header Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/FileReader.hh FileReader.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldConstruction.hh ParallelWorldConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldMessenger.hh ParallelWorldMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldParam.hh ParallelWorldParam.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceHit.hh PhaseSpaceHit.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceSD.hh PhaseSpaceSD.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorMessenger.hh PrimaryGeneratorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAccumulator.hh RunAccumulator.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunActionMessenger.hh RunActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingActionMessenger.hh SteppingActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
== Source Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc FileReader.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc ParallelWorldConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldMessenger.cc ParallelWorldMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc ParallelWorldParam.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc PhaseSpaceHit.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc PhaseSpaceSD.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorMessenger.cc PrimaryGeneratorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc RunAccumulator.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunActionMessenger.cc RunActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingActionMessenger.cc SteppingActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1530</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1530"/>
		<updated>2018-03-13T15:54:29Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Build the simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Building the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir Clatterbridge_sim&lt;br /&gt;
[username@pc1XX ~]$ cd Clatterbridge_sim&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ mkdir build&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, the simulation can be cloned from the [https://github.com/mhentz/Clatterbridge_sim.git GitHub repository] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ git clone https://github.com/mhentz/Clatterbridge_sim.git&lt;br /&gt;
[username@pc1XX ~]$ cd Clatterbridge_sim&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ mkdir build&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
[[Clatterbridge files|List of source files.]]&lt;br /&gt;
&lt;br /&gt;
Click here to download source as a zip: [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim.zip download]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1529</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1529"/>
		<updated>2018-03-13T15:54:08Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Build the simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir Clatterbridge_sim&lt;br /&gt;
[username@pc1XX ~]$ cd Clatterbridge_sim&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ mkdir build&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, the simulation can be cloned from the [https://github.com/mhentz/Clatterbridge_sim.git GitHub repository] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ git clone https://github.com/mhentz/Clatterbridge_sim.git&lt;br /&gt;
[username@pc1XX ~]$ cd Clatterbridge_sim&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ mkdir build&lt;br /&gt;
[username@pc1XX Clatterbridge_sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
[[Clatterbridge files|List of source files.]]&lt;br /&gt;
&lt;br /&gt;
Click here to download source as a zip: [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim.zip download]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1528</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1528"/>
		<updated>2018-03-13T12:27:43Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge Wiki page]&lt;br /&gt;
&lt;br /&gt;
== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
=== Parallel Simulations ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
=== Data analysis ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/analysis.py analysis.py]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/bragg.py bragg.py]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/lateral.py lateral.py]&lt;br /&gt;
&lt;br /&gt;
== Header Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/FileReader.hh FileReader.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldConstruction.hh ParallelWorldConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldMessenger.hh ParallelWorldMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldParam.hh ParallelWorldParam.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceHit.hh PhaseSpaceHit.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceSD.hh PhaseSpaceSD.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorMessenger.hh PrimaryGeneratorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAccumulator.hh RunAccumulator.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunActionMessenger.hh RunActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingActionMessenger.hh SteppingActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
== Source Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc FileReader.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc ParallelWorldConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldMessenger.cc ParallelWorldMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc ParallelWorldParam.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc PhaseSpaceHit.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc PhaseSpaceSD.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorMessenger.cc PrimaryGeneratorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc RunAccumulator.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunActionMessenger.cc RunActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingActionMessenger.cc SteppingActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1527</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1527"/>
		<updated>2018-03-13T12:25:12Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Data analysis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
=== Parallel Simulations ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
=== Data analysis ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/analysis.py analysis.py]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/bragg.py bragg.py]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/lateral.py lateral.py]&lt;br /&gt;
&lt;br /&gt;
== Header Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/FileReader.hh FileReader.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldConstruction.hh ParallelWorldConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldMessenger.hh ParallelWorldMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldParam.hh ParallelWorldParam.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceHit.hh PhaseSpaceHit.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceSD.hh PhaseSpaceSD.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorMessenger.hh PrimaryGeneratorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAccumulator.hh RunAccumulator.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunActionMessenger.hh RunActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingActionMessenger.hh SteppingActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
== Source Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc FileReader.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc ParallelWorldConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldMessenger.cc ParallelWorldMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc ParallelWorldParam.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc PhaseSpaceHit.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc PhaseSpaceSD.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorMessenger.cc PrimaryGeneratorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc RunAccumulator.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunActionMessenger.cc RunActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingActionMessenger.cc SteppingActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1526</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1526"/>
		<updated>2018-03-13T12:24:27Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Simulation Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
=== Parallel Simulations ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
=== Data analysis ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/simulation_analysis.C simulation_analysis.C]&lt;br /&gt;
&lt;br /&gt;
== Header Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/FileReader.hh FileReader.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldConstruction.hh ParallelWorldConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldMessenger.hh ParallelWorldMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/ParallelWorldParam.hh ParallelWorldParam.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceHit.hh PhaseSpaceHit.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhaseSpaceSD.hh PhaseSpaceSD.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorMessenger.hh PrimaryGeneratorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAccumulator.hh RunAccumulator.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunActionMessenger.hh RunActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingActionMessenger.hh SteppingActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
== Source Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc FileReader.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc ParallelWorldConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldMessenger.cc ParallelWorldMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc ParallelWorldParam.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc PhaseSpaceHit.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc PhaseSpaceSD.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorMessenger.cc PrimaryGeneratorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc RunAccumulator.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunActionMessenger.cc RunActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingActionMessenger.cc SteppingActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1525</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1525"/>
		<updated>2018-03-13T12:15:56Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
[[Clatterbridge files|List of source files.]]&lt;br /&gt;
&lt;br /&gt;
Click here to download source as a zip: [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim.zip download]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1524</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1524"/>
		<updated>2018-03-13T12:15:40Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
[[Clatterbridge files|List of source files.]]&lt;br /&gt;
&lt;br /&gt;
Click here to download source files as a zip: [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim.zip download]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1523</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1523"/>
		<updated>2018-03-13T12:14:59Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
=== Parallel Simulations ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
=== Data analysis ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/simulation_analysis.C simulation_analysis.C]&lt;br /&gt;
&lt;br /&gt;
=== Header Files ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh include/DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh include/DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh include/EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh include/EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh include/PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh include/PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh include/PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh include/RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh include/StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh include/StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh include/SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh include/SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
=== Source Files ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  src/DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  src/DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  src/EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc src/EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc src/PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc src/PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc src/PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc src/RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc src/StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc src/StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc src/SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc src/SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1522</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1522"/>
		<updated>2018-03-13T12:14:40Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Simulation Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
=== Parallel Simulations ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
=== Data analysis ===&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/simulation_analysis.C simulation_analysis.C]&lt;br /&gt;
&lt;br /&gt;
== Header Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh include/DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh include/DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh include/EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh include/EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh include/PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh include/PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh include/PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh include/RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh include/StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh include/StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh include/SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh include/SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
== Source Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  src/DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  src/DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  src/EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc src/EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc src/PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc src/PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc src/PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc src/RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc src/StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc src/StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc src/SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc src/SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1521</id>
		<title>Clatterbridge files</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge_files&amp;diff=1521"/>
		<updated>2018-03-13T12:14:25Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Simulation Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Simulation Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc clatterbridgeSim.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac proton.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac score_init.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac score_dump.mac]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/visualisation.mac visualisation.mac]&lt;br /&gt;
&lt;br /&gt;
==== Parallel Simulations ====&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh cb_parallel.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh run.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh submit.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh combine.sh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh combine_all.sh]&lt;br /&gt;
&lt;br /&gt;
==== Data analysis ====&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_rstephens/ProtonPB_source/simulation_analysis.C simulation_analysis.C]&lt;br /&gt;
&lt;br /&gt;
== Header Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorConstruction.hh include/DetectorConstruction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/DetectorMessenger.hh include/DetectorMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventAction.hh include/EventAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/EventActionMessenger.hh include/EventActionMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsList.hh include/PhysicsList.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PhysicsListMessenger.hh include/PhysicsListMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/PrimaryGeneratorAction.hh include/PrimaryGeneratorAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/RunAction.hh include/RunAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMax.hh include/StepMax.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/StepMaxMessenger.hh include/StepMaxMessenger.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingAction.hh include/SteppingAction.hh]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/include/SteppingVerbose.hh include/SteppingVerbose.hh]&lt;br /&gt;
&lt;br /&gt;
== Source Files ==&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  src/DetectorConstruction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorMessenger.cc  src/DetectorMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventAction.cc  src/EventAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/EventActionMessenger.cc src/EventActionMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc src/PhysicsList.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsListMessenger.cc src/PhysicsListMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc src/PrimaryGeneratorAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAction.cc src/RunAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMax.cc src/StepMax.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/StepMaxMessenger.cc src/StepMaxMessenger.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc src/SteppingAction.cc]&lt;br /&gt;
&lt;br /&gt;
[http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingVerbose.cc src/SteppingVerbose.cc]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1520</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1520"/>
		<updated>2018-03-13T12:12:52Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;br /&gt;
&lt;br /&gt;
Click here to download source files as a zip: [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim.zip download]&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1519</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1519"/>
		<updated>2018-03-13T12:06:34Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Changing parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1518</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1518"/>
		<updated>2018-03-13T12:04:19Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Phase space file */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt;] accordingly. The z-coordinates are given relative to the position of the source and the z-axis lies along the beamline with its positive end pointing towards the centre of the room. An example file is shown below (&amp;lt;code&amp;gt;psf_z0.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1517</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1517"/>
		<updated>2018-03-13T12:02:19Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Output files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Phase space file ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;psf_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator::Dump&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the particles were recorded). Currently the recorded quantities are the parent ID indicating whether a particle is a primary or secondary, the particle name, the position vector in mm, the momentum direction vector, and the kinetic energy in MeV. Other quantities can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceHit.cc &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# parentID, name, x [mm], y [mm], z [mm], mom_x, mom_y, mom_z, ke [MeV]&lt;br /&gt;
0,proton,1.862704,-8.483117,0.0,0.003526,0.001614,0.999992,62.677102&lt;br /&gt;
0,proton,0.087498,3.564388,0.0,0.002226,-0.000444,0.999997,62.506999&lt;br /&gt;
0,proton,2.636636,5.235856,0.0,0.000542,0.000050,1.000000,62.569683&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1516</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1516"/>
		<updated>2018-03-13T11:55:35Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Scoring */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Note: scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1515</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1515"/>
		<updated>2018-03-13T11:53:22Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Gathering data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
&#039;&#039;&#039;Scorers are not currently used in the simulation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1514</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1514"/>
		<updated>2018-03-13T11:52:30Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Monitoring job status */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining data ===&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine.sh &amp;lt;code&amp;gt;combine.sh&amp;lt;/code&amp;gt;] [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_all.sh &amp;lt;code&amp;gt;combine_all.sh&amp;lt;/code&amp;gt;]. If only combining a file corresponding to one position:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The user will be prompted to provide the position in mm of the file that should be combined.&lt;br /&gt;
&lt;br /&gt;
If combining all files:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_all.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1513</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1513"/>
		<updated>2018-03-13T11:18:22Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Monitoring job status */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command to refresh periodically):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1512</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1512"/>
		<updated>2018-03-13T11:13:50Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Running simulations in parallel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag must be supplied as an argument to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] and since the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be adjusted by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;. Other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files. The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;cb_parallel_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. Besides setting parameters and navigating to the required directory, the job submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;protonX.mac&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Building_the_simulation Building the simulation] &amp;quot; unless you have already done so. The script &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; can be supplied with a note when being called (e.g. note of number of primaries). Parallel simulations are then run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh &amp;quot;This is a note that will be written to notes.txt&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1511</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1511"/>
		<updated>2018-03-12T20:44:08Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Running simulations in parallel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] creates a timestamped directory and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the short queue using the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by changing the arguments supplied to &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt; (other queues available are &amp;lt;code&amp;gt;medium&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag can be used to set parallel simulations to read from input files):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Submit a given number of simulations to a given queue&lt;br /&gt;
# If reading primaries from a file, set the split option&lt;br /&gt;
../submit.sh short 100&lt;br /&gt;
#../submit.sh short 100 -r&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1510</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1510"/>
		<updated>2018-03-12T20:13:38Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Running simulations in parallel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, simulations can be run in parallel while reading primaries from an input file. This file should be split and distributed evenly across the simulations using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] in order to make use of the parallelism. The script calculates the number of lines each simulation should receive given a number of simulations and then writes chunks of the initial file to separate files which will then be used as input files for the parallel simulation. It should be called in the simulation&#039;s base directory as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./split_input.sh /path/to/file nsimulations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Monitoring job status ===&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1509</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1509"/>
		<updated>2018-03-12T20:03:19Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Running simulations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel ===&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1508</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1508"/>
		<updated>2018-03-12T20:02:43Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Running simulations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
== Build the simulation ==&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running the simulation in batch mode with macro ==&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running simulations in parallel ==&lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1507</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1507"/>
		<updated>2018-03-12T20:02:25Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel === &lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For parallel simulations, some parameters need to be set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
*When running simulations in parallel, each simulation is executed two directories further down from where the parallel run is initialised. Hence, the correct path to the &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt; macro should be set in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/control/execute ../../gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Separate simulations should have different random number generator seeds so that they are independent but in order for simulations to be reproducible, the same sequence of seeds should be assigned for each run. To achieve this, the seeds in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; are set by the job submission script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/cb_parallel.sh &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt;] using the index of the current simulation. For the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro to use the seeds set by the submission script, the following line should be set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set seeds for randon number generators&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If generating primaries from a phase space file that was split using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/split_input.sh &amp;lt;code&amp;gt;split_input.sh&amp;lt;/code&amp;gt;] as described below, the number of events is set by &amp;lt;code&amp;gt;cb_parallel.sh&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the following line should be uncommented:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Run simulation&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Parallel version with split input file&lt;br /&gt;
/run/beamOn ${nevents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1506</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1506"/>
		<updated>2018-03-12T17:38:47Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Recording in a single position for staged simulations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position (for staged simulations) ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel === &lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For split simulations, the parameters are set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;]. The main difference to [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] is the lower number of events and the variable seeds &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which are set in the PBS job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; described below (X is an integer). This ensures that all simulations are independent (since they have different seeds) but as the same set of seeds is used each time, the simulations remain repeatable.&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1505</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1505"/>
		<updated>2018-03-12T17:38:24Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Recording at regular intervals using parallel world */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using a parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position for staged simulations ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel === &lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For split simulations, the parameters are set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;]. The main difference to [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] is the lower number of events and the variable seeds &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which are set in the PBS job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; described below (X is an integer). This ensures that all simulations are independent (since they have different seeds) but as the same set of seeds is used each time, the simulations remain repeatable.&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1504</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1504"/>
		<updated>2018-03-12T17:38:01Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Reading primaries from phase space file */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from a phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position for staged simulations ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel === &lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For split simulations, the parameters are set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;]. The main difference to [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] is the lower number of events and the variable seeds &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which are set in the PBS job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; described below (X is an integer). This ensures that all simulations are independent (since they have different seeds) but as the same set of seeds is used each time, the simulations remain repeatable.&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
	<entry>
		<id>https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1503</id>
		<title>Clatterbridge</title>
		<link rel="alternate" type="text/html" href="https://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;diff=1503"/>
		<updated>2018-03-12T17:37:41Z</updated>

		<summary type="html">&lt;p&gt;MatthieuHentz: /* Using the /gps/ commands */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This simulation is a model of the monoenergetic 62.5 MeV proton beam at the [http://www.clatterbridgecc.nhs.uk/patients/treatment-and-support/proton-therapy Clatterbridge Cancer Centre] as it traverses the components of the beamline and finally hits a volume of water. The simulation was built on the example in &amp;lt;code&amp;gt;examples/extended/electromagnetic/TestEm7&amp;lt;/code&amp;gt; supplied with the Geant4 package and detailed [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/Tutorials/Basic/Monoenergetic_Proton_Pencil_Beam here]. The physics list used is QGSP_BIC_HP, the standard for simulating clinical proton beams.&lt;br /&gt;
&lt;br /&gt;
= Geometry =&lt;br /&gt;
=== Treatment room === &lt;br /&gt;
The schematic below shows the Clatterbridge treatment room, illustrating the layout of the geometries defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;]. The whole beamline is contained within the &amp;quot;inner room&amp;quot; volume, which is placed in the &amp;quot;outer room&amp;quot; volume. As the outer volume is a solid concrete box and the inner volume is a box filled with air, the small overlap models the concrete walls of the treatment room. The proton source is placed against the wall in the inner room (-4200 mm from the origin), and all beamline components are placed relative to this reference point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:treatmentRoomSchematic.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Beamline ===&lt;br /&gt;
[[Media:Beamline_doc.pdf|The positions of the components relative to the source are shown here (annotated PDF)]]. The beamline is shown below. The top figure is a perspective view. The second figure is a top-down view which also shows the borated plastic shielding (f) that was previously hidden to allow the full beamline to be visible.&lt;br /&gt;
&lt;br /&gt;
[[File:beamline_perspective.png|border|right|600px]]&lt;br /&gt;
[[File:beamline_top_down.png|border|right|600px]]&lt;br /&gt;
&lt;br /&gt;
The beamline consists of the following components:&lt;br /&gt;
&lt;br /&gt;
*an aluminium tube (a) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**a tungsten scattering foil&lt;br /&gt;
**a brass beam stopper&lt;br /&gt;
**another tungsten scattering foil&lt;br /&gt;
**a mylar window&lt;br /&gt;
&lt;br /&gt;
*an empty aluminium box (b)&lt;br /&gt;
*an iron block (c)&lt;br /&gt;
*a second aluminium tube (d)&lt;br /&gt;
*a second aluminium box (e) containing from left to right;&lt;br /&gt;
**a brass collimator&lt;br /&gt;
**two dose monitors&lt;br /&gt;
**cross wires&lt;br /&gt;
&lt;br /&gt;
*a brass nozzle (g)&lt;br /&gt;
&lt;br /&gt;
The water phantom (h) is also shown for completeness.&lt;br /&gt;
&lt;br /&gt;
=== Dual scattering tube ===&lt;br /&gt;
[[Media:Tube_doc.pdf|The positions of the components in the tube are shown here (annotated PDF)]]. The proton beam at Clatterbridge is delivered using passive spreading. That is, the beam is spread out using dual scattering where the proton beam is scattered through a first scattering foil followed by a beam stopper and a second scattering foil. The beam stopper is used to reduce the intensity of the beam at its centre and a high-Z material such as tungsten is chosen for the foils as it is highly scattering.&lt;br /&gt;
&lt;br /&gt;
A visualisation of the first tube with 500 primary protons represented by blue tracks is shown below. The protons first travel through the collimator, followed by the first scattering foil which spreads out the beam. The brass stopper then blocks out approximately half of the remaining protons before the beam is again spread out through the second scattering foil. The intention is to produce a wide, homogeneous beam. The particles exit the first tube through a Kapton window which is used to keep the tube under vacuum to reduce random scattering off air molecules.&lt;br /&gt;
&lt;br /&gt;
[[File:First_tube_500_sim.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dosimetry box ===&lt;br /&gt;
[[Media:Dosimetry_box.pdf|The positions of the components in the dosimetry box are shown here (annotated PDF)]]. The aluminium box shown below contains the dosimetry equipment. The beam is first collimated using a brass collimator to remove any stray protons. The beam then travels through the dose monitors, the cross wires and finally exits the box through the brass nozzle.&lt;br /&gt;
&lt;br /&gt;
[[File:Second_box.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Dose monitor ===&lt;br /&gt;
[[Media:Dose_monitor_doc.pdf|The positions of the components in the dose monitors are shown here (annotated PDF)]]. The dose monitors are drift chambers used to measure the dose deposited by the proton beam. An exploded view of a dose monitor as used in the dosimetry box is shown below. It consists of a set of aluminised mylar foils wedged between layers of perspex to hold them in place. The guard ring is used to create a sealed volume of air between the foils. The aluminium layers face towards the centre of the dose monitor such that the assembly acts as a drift chamber when a potential difference is applied.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Dose_monitor_exploded.png|centre|700px]]&lt;br /&gt;
&lt;br /&gt;
=== Visualisation ===&lt;br /&gt;
The geometry and particle tracks in Geant4 [http://geant4.slac.stanford.edu/Presentations/vis/G4DAWNTutorial/G4DAWNTutorial.html can be visualised using the DAWN visualiser]. Other, perhaps more suitable, visualisers are available (see [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08.html]) but DAWN is useful in that it produces very high-resolution visualisations. The main drawback of DAWN is that it does not allow for click-and-drag functionality. Instead, parameters must be set before each visualisation is drawn which tends to require a fair amount of trial-and-error to obtain good images. All images of the geometry on this page were created using DAWN and an example visualisation of the beamline with all particle tracks drawn can be seen below. The first tube and second box are visualised using the wireframe setting so that their inner components are visible. This example contains 500 primary protons. Protons are shown in blue, electrons are red, positrons are cyan, gamma rays are green, and neutrons are yellow.&lt;br /&gt;
&lt;br /&gt;
[[File:Beamline_500_sim_perspective.png|border|centre|800px]]&lt;br /&gt;
&lt;br /&gt;
= Generating primary protons =&lt;br /&gt;
===  Using the &amp;lt;code&amp;gt;/gps/&amp;lt;/code&amp;gt; commands ===&lt;br /&gt;
The protons are generated using the [http://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s07.html &amp;lt;code&amp;gt;G4GeneralParticleSource&amp;lt;/code&amp;gt;] (GPS) class which allows for a range of properties of the primary protons to be set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] file (primaries are the initial particles generated by the GPS). First, the particle source is positioned against the wall on one side of the room which is at -4200 mm from the centre. In an attempt to achieve a more realistic beam, the primary protons are distributed normally in the x and y directions centred at 0 with standard deviations of 4.0 mm and 4.5 mm respectively. This gives the beam width and results in a nearly circular profile. The primaries are generated with initial energies following a Gaussian distribution with mean 62.5 MeV and standard deviation 0.082 MeV, and Gaussian radial distributions with respect to the z-axis with standard deviations of 2.3 mrad and 1.2 mrad in the x and y directions respectively.&lt;br /&gt;
&lt;br /&gt;
The parameters are set in the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/gps.mac &amp;lt;code&amp;gt;gps.mac&amp;lt;/code&amp;gt;] macro as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Set particle gun settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
/gps/particle proton&lt;br /&gt;
/gps/number 1 			# per event&lt;br /&gt;
&lt;br /&gt;
# Energy&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&lt;br /&gt;
# Source position&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&lt;br /&gt;
# Beam width&lt;br /&gt;
/gps/pos/type Beam&lt;br /&gt;
/gps/pos/sigma_x 4.0 mm&lt;br /&gt;
/gps/pos/sigma_y 4.5 mm&lt;br /&gt;
&lt;br /&gt;
# Angular distribution of primaries for realistic emittance&lt;br /&gt;
/gps/ang/type beam2d&lt;br /&gt;
/gps/ang/sigma_x 2.3 mrad&lt;br /&gt;
/gps/ang/sigma_y 1.2 mrad&lt;br /&gt;
/gps/ang/rot1 -1 0 0		# aligns gps with positive z-axis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro is called in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Use macro to set properties of primaries&lt;br /&gt;
/control/execute gps.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  Reading primaries from phase space file ===&lt;br /&gt;
Primaries can be generated from an input phase space file using [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/FileReader.cc &amp;lt;code&amp;gt;FileReader&amp;lt;/code&amp;gt;] in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PrimaryGeneratorAction.cc &amp;lt;code&amp;gt;PrimaryGeneratorAction&amp;lt;/code&amp;gt;]. The input must be of the same format as the output files generated by the simulation and its first line is assumed to be a header. The path to the input file should be given in &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; and the position at which the particles should be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Primary generator settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Reading from phase space file&lt;br /&gt;
/primarygenerator/input path/to/file&lt;br /&gt;
&lt;br /&gt;
# Set position in z particles are generated at&lt;br /&gt;
/primarygenerator/generateAt 0&lt;br /&gt;
#/primarygenerator/generateAt 81&lt;br /&gt;
#/primarygenerator/generateAt 357&lt;br /&gt;
#/primarygenerator/generateAt 1700&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Specifying where particles should be generated in useful when running staged simulations as it allows particles to be generated at the same position they were initially recorded at.&lt;br /&gt;
&lt;br /&gt;
= Gathering data =&lt;br /&gt;
== Tracking ==&lt;br /&gt;
=== Accumulating hits over a run ===&lt;br /&gt;
Several quantities can be monitored at different stages in the beamline using the &amp;lt;code&amp;gt;SensitiveDetector&amp;lt;/code&amp;gt; defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhaseSpaceSD.cc &amp;lt;code&amp;gt;PhaseSpaceSD.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; extends [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VSensitiveDetector.html &amp;lt;code&amp;gt;G4VSensitiveDetector&amp;lt;/code&amp;gt;]). When a particle hits a boundary on which a &amp;lt;code&amp;gt;PhaseSpaceSD&amp;lt;/code&amp;gt; is defined, &amp;lt;code&amp;gt;PhaseSpaceSD::ProcessHits&amp;lt;/code&amp;gt; is called and generates a &amp;lt;code&amp;gt;PhaseSpaceHit&amp;lt;/code&amp;gt; (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VHit.html &amp;lt;code&amp;gt;G4VHit&amp;lt;/code&amp;gt;]) that is then stored in the hits collection of the sensitive detector. A unique ID belonging to the volume the hit was registered on is passed to the hit along with any required quantity. Hits are accumulated in the sensitive detector&#039;s hits collection over an event (an event corresponds to the full simulation of one primary).&lt;br /&gt;
&lt;br /&gt;
The [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/RunAccumulator.cc &amp;lt;code&amp;gt;RunAccumulator&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4Run.html &amp;lt;code&amp;gt;G4Run&amp;lt;/code&amp;gt;]) accumulates hits over a run (the collection of all events). It is constructed by &amp;lt;code&amp;gt;RunAction::GenerateRun&amp;lt;/code&amp;gt; at the start of a run. At the end of an event, &amp;lt;code&amp;gt;RunAccumulator::RecordEvent&amp;lt;/code&amp;gt; stores all hits that were registered during the event in the run hits vector and increments the event counter. Once that counter reaches the buffer, the hits are written to their corresponding output file. The buffer size can be set using the following line in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
# Set buffer size&lt;br /&gt;
/user/run/buffer 1000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording at regular intervals using parallel world ===&lt;br /&gt;
To characterise the beam, the evolution of several properties (energy spectrum, emittance, spatial profile, etc.) must be monitored at regular intervals, or at least at several positions, along the beamline. To achieve this, many volumes would need to be defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc &amp;lt;code&amp;gt;DetectorConstruction&amp;lt;/code&amp;gt;] to be used as sensitive detectors. However, volumes overlapping or sharing boundaries can lead to errors in the simulation and must therefore be avoided. One approach in achieving this would be to carefully construct each detector volume such that it fits in, or around, a given region. An example illustrating why this may be challenging is given by the following; suppose detection was required inside and outside of the first aluminium box on a plane transverse to the direction of the beam. Separate detector volumes would be required on the inside and outside of the box just to detect particles incident on the same plane. This does not yet address the issue of how to handle the boundaries between the box and the detectors as particles would be likely to pass through the gaps and hence go undetected. Even for this simple example, the geometry of the detector volumes would be rather complex and hence not easily reusable.&lt;br /&gt;
&lt;br /&gt;
To avoid these problems, the main geometry (called the &#039;&#039;mass geometry&#039;&#039;) can be overlaid with another parallel geometry that does not interact physically with particles in the mass geometry, as described [http://geant4.web.cern.ch/geant4/workAreaUserDocKA/Backup/Docbook_UsersGuides_beta/ForApplicationDeveloper/html/ch04s07.html here]. Different volumes can then be defined at arbitrary positions and have sensitive detectors assigned to them in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldConstruction.cc &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;]. To track particles at regular intervals along the beamline, an array of thin boxes is defined by passing the parametrisation [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/ParallelWorldParam.cc &amp;lt;code&amp;gt;ParallelWorldParam&amp;lt;/code&amp;gt;] (extension of [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPVParameterisation.html &amp;lt;code&amp;gt;G4VPVParametrisation&amp;lt;/code&amp;gt;]) to the parametrised volume constructor [http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4PVParameterised.html &amp;lt;code&amp;gt;G4PVParametrised&amp;lt;/code&amp;gt;]. This creates boxes of a given size at given intervals spanning a given distance from the source. The size of the box can be adjusted in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;, while the spacing and total distance spanned can be set in the &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt; macro using the following settings:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## If using &#039;all&#039;&lt;br /&gt;
/parallel/detector/spacing 25 mm      # default is 25 mm&lt;br /&gt;
/parallel/detector/distance 1900 mm   # default is 1900 mm&lt;br /&gt;
&lt;br /&gt;
# Pass spacing and distance to run action and set buffer size&lt;br /&gt;
/user/run/detector/spacing 25&lt;br /&gt;
/user/run/detector/distance 1900&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording in a single position for staged simulations ===&lt;br /&gt;
Alternatively, particles can be recorded in a single position by choosing one of the components and uncommenting accordingly. The available components are&lt;br /&gt;
* scatterfoil1 at 81 mm&lt;br /&gt;
*tube1 at 357 mm&lt;br /&gt;
*nozzle at 1700 mm&lt;br /&gt;
*outside at 1768 m&lt;br /&gt;
Components can be added to the components map in &amp;lt;code&amp;gt;ParallelWorldConstruction&amp;lt;/code&amp;gt;. Only recording in one position can be useful when the beam has been characterised and the simulation is know to be running as intended. Writing to output less often improves the simulation&#039;s performance significantly. This can be used to speed up the simulation further by performing simulations in a staged manner. Sections upstream, where no changes to the geometry are made (e.g. from the source to after the first collimator where the particle count reduces by about 90%), then only need to be simulated once and the output from that simulation can be used as the input to the rest of the simulation.&lt;br /&gt;
&lt;br /&gt;
For example, if the simulation should only run up to the first scatter foil (situated after the first collimator), the commands would look as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Detector settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Choose where particles are recorded&lt;br /&gt;
#/parallel/detector all&lt;br /&gt;
/parallel/detector scatterfoil1&lt;br /&gt;
#/parallel/detector tube1&lt;br /&gt;
#/parallel/detector nozzle&lt;br /&gt;
#/parallel/detector outside&lt;br /&gt;
...&lt;br /&gt;
## If using a component, set dump mode and detector position&lt;br /&gt;
/user/run/dump/single true&lt;br /&gt;
/user/run/detector/position 81&lt;br /&gt;
#/user/run/detector/position 357&lt;br /&gt;
#/user/run/detector/position 1700&lt;br /&gt;
#/user/run/detector/position 1768&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
# Stepping action settings&lt;br /&gt;
#-------------------------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
## Kill particles 1 mm after being recorded&lt;br /&gt;
/steppingAction/kill 82&lt;br /&gt;
#/steppingAction/kill 358&lt;br /&gt;
#/steppingAction/kill 1701&lt;br /&gt;
#/steppingAction/kill 1769&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command &amp;lt;code&amp;gt;/user/run/dump/single&amp;lt;/code&amp;gt; sets a flag used in &amp;lt;code&amp;gt;RunAccumulator::InitialiseOutputFiles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Scoring ==&lt;br /&gt;
Scorers are defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;] and the quantities they record are dumped to files as defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal scoring mesh ===&lt;br /&gt;
A scoring mesh is &#039;&#039;longitudinal&#039;&#039; in the sense that it records data along the direction of the beamline, e.g. the energy deposited at different positions in z. Hence, for a mesh to be longitudinal it must have bins defined along its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh detEnergyLon&lt;br /&gt;
/score/mesh/boxSize 20. 20. 20. mm&lt;br /&gt;
/score/mesh/nBin 1 1 200&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2340 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the third line in the snippet above you can see that there is only a single bin in both the x and y directions but 200 in the z-direction.&lt;br /&gt;
&lt;br /&gt;
=== Lateral scoring mesh ===&lt;br /&gt;
Similarly to the longitudinal meshes, a mesh is said to be &#039;&#039;lateral&#039;&#039; when it records data transverse to the direction of travel of the protons. For a mesh to be lateral it must have bins defined perpendicularly to its z-axis. The scorer defined on the mesh &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; is an example of such a mesh. It is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh braggEnergyLat&lt;br /&gt;
/score/mesh/boxSize 20. 20. 0.5 mm&lt;br /&gt;
/score/mesh/nBin 200 1 1&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -2329.2 mm&lt;br /&gt;
/score/quantity/energyDeposit energyDeposit&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again, you can see that there is only a single bin in the y and z directions but 200 in the x-direction.&lt;br /&gt;
&lt;br /&gt;
= Running simulations =&lt;br /&gt;
&lt;br /&gt;
=== Build the simulation ===&lt;br /&gt;
To access a Linux desktop PC running Scientific Linux 6, [http://www.hep.ucl.ac.uk/pbt/wiki/Software/Geant4/UCL_HEP_Cluster follow the instructions here]. Once access has been established, follow the instructions below to copy the simulation files to your directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX ~]$ mkdir sim&lt;br /&gt;
[username@pc1XX ~]$ cd sim&lt;br /&gt;
[username@pc1XX sim]$ cp -r /unix/pbt/users/mhentz/Clatterbridge_sim/source .&lt;br /&gt;
[username@pc1XX sim]$ mkdir build&lt;br /&gt;
[username@pc1XX sim]$ cd build&lt;br /&gt;
[username@pc1XX build]$ /unix/pbt/software/scripts/pbt.sh&lt;br /&gt;
[username@pc1XX build]$ cmake -DGeant4_DIR=/unix/pbt/software/dev ../source&lt;br /&gt;
[username@pc1XX build]$ make -j4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running the simulation in batch mode with macro ===&lt;br /&gt;
&lt;br /&gt;
This will run the simulation and produce the required output files in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory created. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ mkdir data&lt;br /&gt;
[username@pc1XX build]$ ./clatterbridgeSim proton.mac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running simulations in parallel === &lt;br /&gt;
A simulation comprising of a large number of particles can take a long time to run as events run sequentially (about 20 hours for 1e6 events). This can be reduced considerably by running a number of independent simulations with fewer events &amp;quot;in parallel&amp;quot; and then merging the output files once they have completed.  The main limitation lies in the maximum number of simulations that can be run simultaneously–which is 120. Splitting 1e6 events into 100 simulations of 10,000 events reduces the runtime to a much more bearable 20 minutes.&lt;br /&gt;
&lt;br /&gt;
For split simulations, the parameters are set in the macro [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;]. The main difference to [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] is the lower number of events and the variable seeds &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/random/setSeeds ${seed1} ${seed2}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which are set in the PBS job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; described below (X is an integer). This ensures that all simulations are independent (since they have different seeds) but as the same set of seeds is used each time, the simulations remain repeatable.&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/run.sh &amp;lt;code&amp;gt;run.sh&amp;lt;/code&amp;gt;] (located in the &amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt; directory but should automatically be copied to &amp;lt;code&amp;gt;build&amp;lt;/code&amp;gt; by cmake) creates a timestamped directory along with a few useful subdirectories and submits a specified number of simulations to be executed there. It is currently set to submit 100 jobs to the medium queue through the [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] script. This can be changed by supplying different arguments on line 22 (other queues available are &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
../submit.sh 100 medium&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/submit.sh &amp;lt;code&amp;gt;submit.sh&amp;lt;/code&amp;gt;] assigns values to the variables in the template [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridge_split.sh &amp;lt;code&amp;gt;clatterbridge_split.sh&amp;lt;/code&amp;gt;] and writes the result to a job submission script &amp;lt;code&amp;gt;clatterbridge_X.sh&amp;lt;/code&amp;gt; for all 100 simulations. This is done on line 31:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat ../clatterbridge_split.sh | sed -e &amp;quot;s/\${i}/$i/&amp;quot; &amp;gt;&amp;gt; clatterbridge_$i.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Besides setting vital parameters and navigating to the correct folder, the submission script sets the seeds in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac_split &amp;lt;code&amp;gt;proton.mac_split&amp;lt;/code&amp;gt;] and writes the result to a new macro &amp;lt;code&amp;gt;proton.mac_simX&amp;lt;/code&amp;gt;. It then runs the simulation with the corresponding macro.&lt;br /&gt;
&lt;br /&gt;
Follow the instructions in &amp;quot;[http://www.hep.ucl.ac.uk/pbt/pbtWiki/index.php?title=Clatterbridge&amp;amp;action=submit#Make_the_simulation Make the simulation] &amp;quot; unless you have already done so. Split simulations are run as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ ./run.sh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the simulations can be tracked with the following command (can be used in conjunction with the &amp;lt;code&amp;gt;watch&amp;lt;/code&amp;gt; command):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qstat -u &amp;lt;username&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If something goes wrong and you wish to stop all the jobs simultaneously you can use the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ qselect -u &amp;lt;username&amp;gt; | xargs qdel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting data can be combined by running the scripts [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_tracking.py &amp;lt;code&amp;gt;combine_tracking.py&amp;lt;/code&amp;gt;] and [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/combine_scorers.py &amp;lt;code&amp;gt;combine_scorers.py&amp;lt;/code&amp;gt;] as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ cd timestamp-dir&lt;br /&gt;
[username@pc1XX timestamp-dir]$ cp ../../source/combine_*.py .&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_tracking.py&lt;br /&gt;
[username@pc1XX timestamp-dir]$ ./combine_scorers.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Output files =&lt;br /&gt;
=== Output used for beam emittance and energy spectra ===&lt;br /&gt;
The output files &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; are written to the subdirectory &amp;lt;code&amp;gt;data/&amp;lt;/code&amp;gt; in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; is the position in z where the protons were recorded). Other particles can be recorded by changing [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;] accordingly. The output files contain the parent ID which is 0 for a primary particle and non-zero for all other particles, the xyz-coordinates in mm, the angle subtended by the projection of the momentum vector onto the x-axis and the z-axis in mrad (used to plot the beam emittance) and the kinetic energy in MeV. The z-coordinates are given relative to the position of the particle source and the z-axis lies along the beamline with its positive end pointing towards the origin. An example is shown below (&amp;lt;code&amp;gt;output_z1759.txt&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# zposition: 1759&lt;br /&gt;
# parentID, x, y, z [mm], theta [mrad], ke [MeV]&lt;br /&gt;
0 -9.78358 4.39357 1759.0 -6.01461 60.32370&lt;br /&gt;
0 -7.12684 -10.49640 1758.9 -4.95641 60.12080&lt;br /&gt;
0 8.83137 -2.33413 1759.0 10.60610 60.07960&lt;br /&gt;
0 5.92333 -3.29109 1758.9 7.89279 60.20930&lt;br /&gt;
0 -2.21463 6.24370 1759.0 -6.14550 60.04930&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scorer outputs ===&lt;br /&gt;
The files described below are all written using the &amp;lt;code&amp;gt;/score/dumpQuantityToFile&amp;lt;/code&amp;gt; commands in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_dump.mac &amp;lt;code&amp;gt;score_dump.mac&amp;lt;/code&amp;gt;]. The data are recorded by the scorers defined in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
==== Lateral energy deposition at the Bragg peak in the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;braggEnergyLat&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition at the Bragg peak in the plane perpendicular to the direction of incidence of the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV (as the mesh is only binned in x, it consists of strips parallel to the y-axis):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: braggEnergyLat&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,29.931796279684590&lt;br /&gt;
1,0,0,38.256791245912446&lt;br /&gt;
2,0,0,115.051837974658625&lt;br /&gt;
3,0,0,185.831154261095321&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the lateral energy deposition profile at the Bragg peak.&lt;br /&gt;
&lt;br /&gt;
==== Longitudinal energy deposition throughout the detector ====&lt;br /&gt;
The file &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt; contains data recorded by the &amp;lt;code&amp;gt;energyDeposit&amp;lt;/code&amp;gt; scorer defined on the &amp;lt;code&amp;gt;detEnergyLon&amp;lt;/code&amp;gt; mesh in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/score_init.mac &amp;lt;code&amp;gt;score_init.mac&amp;lt;/code&amp;gt;]. It records the energy deposition in the detector in bins along the beam. The columns represent the bin number in the x, y and z directions and the energy deposition in MeV:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mesh name: detEnergyLon&lt;br /&gt;
# primitive scorer name: energyDeposit&lt;br /&gt;
# iX, iY, iZ, value [MeV]&lt;br /&gt;
0,0,0,59390.147050145809771&lt;br /&gt;
0,0,1,59016.142052385039278&lt;br /&gt;
0,0,2,59209.373600437116693&lt;br /&gt;
0,0,3,59578.920521325460868&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This file can be used to plot the energy deposition profile through the detector. The Bragg peak can be identified from such a plot.&lt;br /&gt;
&lt;br /&gt;
= Data Analysis =&lt;br /&gt;
=== Beam profile, emittance and energy spectrum ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;analysis.py&amp;lt;/code&amp;gt; produces several different plots from the &amp;lt;code&amp;gt;output_zX.txt&amp;lt;/code&amp;gt; files. It plots the beam profile, the projection of the beam profile onto the x-axis, the beam emittance and the energy spectrum at the position in z where the protons were recorded. This script requires the &amp;lt;code&amp;gt;SciPy&amp;lt;/code&amp;gt; module which is not available on the Linux SL6 PCs. Hence, it must currently be run locally. It is run for a given position along the beamline as follows (400 mm as an example, the script must be run in the directory containing the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; subdirectory):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./analysis.py 400&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This runs the script using the file &amp;lt;code&amp;gt;output_z400.txt&amp;lt;/code&amp;gt; and produces the corresponding plots shown below. At this stage, the beam has undergone dual scattering and is starting to spread out again to produce a uniform beam.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Tiles_400.jpg|centre|750px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Longitudinal energy deposition profile and Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;bragg.py&amp;lt;/code&amp;gt; plots the longitudinal energy deposition profile through the detector from &amp;lt;code&amp;gt;DetEnergyDepLon.txt&amp;lt;/code&amp;gt;. It is executed using Python 2.7 in the same directory as the data file as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./bragg.py&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lon_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Lateral energy deposition profile at the Bragg peak ===&lt;br /&gt;
The script &amp;lt;code&amp;gt;lateral.py&amp;lt;/code&amp;gt; plots the lateral energy deposition at the Bragg peak from &amp;lt;code&amp;gt;BraggEnergyDepLat.txt&amp;lt;/code&amp;gt; described above. It is executed on Python 2.7 as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[user@hostname ~]$ ./lateral.py BraggEnergyDepLat.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces the following plot;&lt;br /&gt;
[[File:Lat_energy_deposition_bragg.png|centre|600px]]&lt;br /&gt;
&lt;br /&gt;
== Changing parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Beam parameters ===&lt;br /&gt;
These can be modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/proton.mac &amp;lt;code&amp;gt;proton.mac&amp;lt;/code&amp;gt;] under the header &amp;quot;particle gun settings&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Energy ====&lt;br /&gt;
The GPS gives the initial protons a range of energies following a Gaussian distribution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/ene/type Gauss&lt;br /&gt;
/gps/ene/mono 62.5 MeV&lt;br /&gt;
/gps/ene/sigma 0.082 MeV&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Source position ====&lt;br /&gt;
The proton source is positioned at &amp;lt;code&amp;gt;z = -4200 mm&amp;lt;/code&amp;gt; relative to the centre of the inner room, which corresponds to a position against the wall of the inner room.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/gps/position 0.0 0.0 -4200 mm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scoring meshes ===&lt;br /&gt;
Filters can be applied to scoring meshes so as to only record a given type of particle using the command &amp;lt;code&amp;gt;/score/filter/particle protonFilter proton&amp;lt;/code&amp;gt; in the definition of the scoring mesh. This can be useful as, for example, one may want to monitor the flux of different particles along the beamline. This would require a large mesh positioned over the whole beamline as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/score/create/boxMesh beamlineLongitudinal&lt;br /&gt;
/score/mesh/boxSize 50. 50. 1000. mm&lt;br /&gt;
/score/mesh/nBin 1 1 400&lt;br /&gt;
/score/mesh/translate/xyz 0. 0. -3200 mm&lt;br /&gt;
/score/quantity/flatSurfaceFlux protonFlux&lt;br /&gt;
/score/filter/particle protonFilter proton&lt;br /&gt;
/score/quantity/flatSurfaceFlux neutronFlux&lt;br /&gt;
/score/filter/particle neutronFilter neutron&lt;br /&gt;
/score/close&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mesh records two separate quantities, the proton flux and the neutron flux, which can then be written to separate output files.&lt;br /&gt;
&lt;br /&gt;
=== Beamline components ===&lt;br /&gt;
Components of the beamline can be added, removed or modified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/DetectorConstruction.cc  &amp;lt;code&amp;gt;DetectorConstruction.cc&amp;lt;/code&amp;gt;]. For example, the position of the second dose monitor can be changed by changing the z-value of the &amp;lt;code&amp;gt;G4ThreeVector&amp;lt;/code&amp;gt; on the line below. As the dose monitor is placed within the dosimetry box &amp;lt;code&amp;gt;lAlBox2&amp;lt;/code&amp;gt; it&#039;s position is given relative to the centre of that box.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pDoseMonitor2 = new G4PVPlacement(0, G4ThreeVector(0, 0, -169.5*CLHEP::mm), lDoseMonitor1, &amp;quot;DoseMonitor2&amp;quot;, lAlBox2, false, 0, checkOverlaps);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;After any changes made to the source or header files (ending in &amp;lt;code&amp;gt;.cc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;.hh&amp;lt;/code&amp;gt;), the code will need to be recompiled. In the build directory, write:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[username@pc1XX build]$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Physics list ===&lt;br /&gt;
A user-defined physics list can be specified in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/PhysicsList.cc &amp;lt;code&amp;gt;PhysicsList.cc&amp;lt;/code&amp;gt;] and then selected in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/clatterbridgeSim.cc &amp;lt;code&amp;gt;clatterbridgeSim.cc&amp;lt;/code&amp;gt;] using the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
runManager-&amp;gt;SetUserInitialization(new  PhysicsList);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tracking positions ===&lt;br /&gt;
As described in the section &amp;quot;[http://www.hep.ucl.ac.uk/pbt/wiki/Clatterbridge#Tracking Tracking] &amp;quot;, protons are tracked in [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.cc &amp;lt;code&amp;gt;SteppingAction.cc&amp;lt;/code&amp;gt;]. This class contains a vector &amp;lt;code&amp;gt;trackingPosVec&amp;lt;/code&amp;gt; which contains the positions of interest where protons are to be tracked. Positions can be changed by changing &amp;lt;code&amp;gt;trackingPosFiller&amp;lt;/code&amp;gt; in the header file [http://www.hep.ucl.ac.uk/pbt/wikiData/code/Clatterbridge_sim/source/src/SteppingAction.hh &amp;lt;code&amp;gt;SteppingAction.hh&amp;lt;/code&amp;gt;] on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trackingPosFiller = { 0, 45, 79, 81, 100, 150, ..., 1759, 1800, 1839, 1881 };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Don&#039;t forget to recompile after any changes made.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Files =&lt;br /&gt;
&lt;br /&gt;
[[Clatterbridge files|List of files for Clatterbridge simulation]].&lt;/div&gt;</summary>
		<author><name>MatthieuHentz</name></author>
	</entry>
</feed>