最近開始為訂閱號“Geant4不完全學習指南”寫一些文章。

正文開始。
四步輕松安裝Geant4 (Linux篇) | 入門
軟件版本:Ubuntu16.04,Geant4.10.04
預備知識:Linux命令行終端的簡單使用
Geant4官方已經適配主流的計算機平台 (Windows, Linux, macOS), 其中在用戶數量上以Linux居多。所以我們先從Linux平台上的安裝講起。當然Linux不止有一個系統,而是有不同廠家發行的各種發行版(Ubuntu, CentOS, Fedora, RedHat, Scientific Linux, Arch Linux 等等)。這些系統之間的差異有時會給Geant4造成問題,所以我們還是推薦初學者只使用官方測試過的系統.以Geant 4.10.04版本來說,CentOS7, Scientific Linux CERN 6和 Ubuntu 16都是不錯的選擇。其中筆者尤其建議初學者從Ubuntu系統上手,配合CMake自動化配置工具,Geant4的安裝可以很簡單。使用者甚至不需要明白CMake的技術細節,按照步驟輸入命令終端即可。雖然最新的Ubuntu系統是17.10,但是因為Geant 4.10.04只對主版本號16的Ubuntu做過測試,我們還是建議大家使用Ubuntu 16.04。如果想要安裝其他版本的Geant4,也可以查一下Release Note,使用官方測試過的系統可以避免很多問題。
1. 系統准備
sudo apt-get update #保持系統更新
sudo apt-get install build-essential #安裝gcc等編譯程序
sudo apt-get install cmake #安裝cmake
sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev libxt-dev libxmu-dev libxi-dev zlib1g-dev libgl2ps-dev libexpat1-dev libxerces-c-dev # 安裝必需的庫文件
注意:libXmu-dev libXi-dev在新版本的ubuntu下已經更名為libxmu-dev libxi-dev
2. 下載Geant4
在Geant4的官網下載最新版本geant4.10.04.tar.gz或者geant4.10.04.zip.

將文件解壓到制定位置,比如/home/handbook/,
tar -xzvf geant4.10.04.tar.gz
現在就有了目錄/home/handbook/geant4.10.04
3. 使用CMake生成Makefile
在/home/handbook/geant4.10.04/目錄下建立build/文件夾,並進入
cd /home/handbook/geant4.10.04
mkdir build
cd buiid/
我們要在 build/ 文件夾下使用CMake工具來生成Makefile,同時設置 /home/handbook/geant4.10.04 為安裝目錄,為Geant4安裝一些必要的軟件支持 (OpenGL, RayTracer和GDML),並且自動下載物理模擬必須的數據庫文件。
cmake -DCMAKE_INSTALL_PREFIX=/home/handbook/geant4.10.04 -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_GDML=ON -DGEANT4_INSTALL_DATA=ON ..
如果命令正常運行的話,大概會這樣結束:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/handbook/geant4.10.04/build
4. 使用make命令進行編譯
如果你的電腦有N個處理器,可以使用如下命令
make –jN 否則只用 make
如果命令正常運行的話,大概會這樣結束:
[100%] Building CXX object source /visualization/OpenGL/CMakeFiles/G4OpenGL.dir/src/G4OpenGLXViewer.cc.o
[100%] Linking CXX shared library ../../../BuildProducts/lib/libG4OpenGL.so
[100%] Built target G4OpenGL
最后安裝編譯好的文件到我們剛才指定的文件夾
make install
如果命令正常運行的話,大概會這樣結束:
--Installing: /home/handbook/geant4.10.04/include/Geant4/G4OpenGLImmediateXViewer.hh
-- Installing: /home/handbook/geant4.10.04/include/Geant4/G4OpenGLStoredX.hh
-- Installing: /home/handbook/geant4.10.04/include/Geant4/G4OpenGLStoredXViewer.hh
-- Installing: /home/handbook/geant4.10.04/include/Geant4/G4OpenGLXViewer.hh
好了,如果沒有報錯的話,你的安裝就已經完成了!
我們來簡單驗證一下Geant4是否正常工作。 geant4.10.04/examples 下面有很多的示例可以測試。我們選一個最簡單的示例 examples/basic/B1
首先,我們要加載Geant4運行時的環境
source ~/geant4.10.04/bin/geant4.sh
然后,我們拷貝一份代碼,並且編譯。
cd /home/handbook
cp –r geant4.10.04/examples/basic/B1 .
cd B1
mkdir build
cd build
cmake ..
make
如果命令正常運行的話,大概會是這樣:
handbook@handbook-VirtualBox:~/B1/build$ make
Scanning dependencies of target exampleB1
[ 12%] Building CXX object CMakeFiles/exampleB1.dir/exampleB1.cc.o
[ 25%] Building CXX object CMakeFiles/exampleB1.dir/src/B1EventAction.cc.o
[ 37%] Building CXX object CMakeFiles/exampleB1.dir/src/B1DetectorConstruction.cc.o
[ 50%] Building CXX object CMakeFiles/exampleB1.dir/src/B1SteppingAction.cc.o
[ 62%] Building CXX object CMakeFiles/exampleB1.dir/src/B1ActionInitialization.cc.o
[ 75%] Building CXX object CMakeFiles/exampleB1.dir/src/B1PrimaryGeneratorAction.cc.o
[ 87%] Building CXX object CMakeFiles/exampleB1.dir/src/B1RunAction.cc.o
[100%] Linking CXX executable exampleB1
[100%] Built target exampleB1
編譯完后,我們在build/目錄下就得到了一個可執行程序exampleB1。運行后會打開一個可視化的界面,那就說明正常運行了。
./exampleB1

