在Mac上面安装geant4,主要是写代码,看看探测器的结构,粒子发射的位置,产生的粒子等。一般情况都会在Linux主机上面,或者是Cluster上面,非图像界面跑数据的。但是就是想在Mac上面装,我愿意呀。
然而安装的时候一定要按照安装手册进行配置。首先要有brew,这个如果你不会,你在网上搜,一搜一大把。推荐官网:https://brew.sh,免得被骗掉坑里。
如果要运行geant4,而且使用Qt界面显示(好用,强烈推荐),你要装Qt。
1 首先要装OpenGL的库,到官网 https://www.xquartz.org,下载最新版(今年是2020年了,7月底了)的XQuartz-2.7.11.dmg,最新版的。安装,对应的X11库因该是在/usr/X11R6。
2 要安装Xcode,这个在Apple Store里面找,好大,1.8G左右吧,不记得了。然后在终端,就是那个黑框,输入 xcode-select --install 安装Xcode command line tools,对应的他的GL库因该就是/usr/X11那个目录。
3 要安装Cmake,这个是生成Makefile文件的工具。brew install cmake,可能在上面的步骤,或者这个步骤要求你同意一个license。
You have not agreed to the Xcode license.
Before running the installer again please agree to the license by opening
Xcode.app or running:
sudo xcodebuild -license
相信大家都是知识分子,按照提示做就是了。
4 我是要用VTK所以,brew install vtk的时候给我装了qt5.15.0。这一步就是要安装QT,你只要brew install qt就行了,就可以安装最新的qt,现在是5.15.0这个版本。
我安装完VTK,发现连接的库出现了问题,
/usr/local/Cellar/vtk/8.2.0_10/include/vtk-8.2/vtkGenericDataArrayLookupHelper.h:56:6: error: expected ')'
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/math.h:165:7: note: expanded from macro 'isnan'
( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \
^
/usr/local/Cellar/vtk/8.2.0_10/include/vtk-8.2/vtkGenericDataArrayLookupHelper.h:56:6: note: to match this '('
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/math.h:165:5: note: expanded from macro 'isnan'
( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
是C++库的问题。Catalina版的C++的库很诡异,要将Xcode command line里面的库设置成使用的库。操作如下:
% xcrun --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
% sudo xcode-select -s /Library/Developer/CommandLineTools
Password:
这里告诉大家,如果安装OpenCV,直接安装:brew install opencv就行了,会安装最新版opencv4,如果你先看brew search opencv 会发现有opencv opencv@2 opencv@3,如果安装时使用brew install opencv@3后,你就会发现写的代码编译找不到opencv呀。还要再安装,brew install opencv才行,会再装一个protobuf包。
经过一番折腾,我的VTK和OpenCV运行良好。
5 安装xerces-c,这个是用来读取gdml文件的
brew install xerces-c
6 现在可以安装geant4了。在里面建一个build文件夹,所有操作都在这个文件夹里。这里我是从我的Linux主机上面考来的,所以有数据了,我就直接利用了,注意安装完之后,不要将安装文件删掉,至少也包保存build下面的data目录。或者,是-DGEANT_INSTALL_DATA=ON,我估计cmake检查到数据后,就不会去下载了。编译比较耗时的部分就是下载数据,最近连欧洲的网络比较慢。
% cmake -DCMAKE_INSTALL_PREFIX=/usr/local/Geant4 -DGEANT4_USE_GDML=ON -DGEANT_INSTALL_DATADIR=/Users/我的名字/Downloads/geant4.10.6.p01/build/data -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_QT=ON ..
CMake Error at /usr/local/Cellar/cmake/3.17.3/share/cmake/Modules/FindQt4.cmake:1314 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
cmake/Modules/Geant4InterfaceOptions.cmake:117 (find_package)
cmake/Modules/G4CMakeMain.cmake:64 (include)
CMakeLists.txt:50 (include)
这个是没有找到QT呀。我们就告诉cmake,qt在哪里
export CMAKE_PREFIX_PATH=/usr/local/Cellar/qt/5.15.0/lib/cmake
这个是我的qt所在的位置,然后再运行cmake -DCMAKE_INSTALL_PREFIX=/usr/local/Geant4 -DGEANT4_USE_GDML=ON -DGEANT_INSTALL_DATADIR=/Users/我的名字/Downloads/geant4.10.6.p01/build/data -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_QT=ON ..
结果是:
-- Reusing dataset G4NDL (4.6)
-- Reusing dataset G4EMLOW (7.9.1)
-- Reusing dataset PhotonEvaporation (5.5)
-- Reusing dataset RadioactiveDecay (5.4)
-- Reusing dataset G4PARTICLEXS (2.1)
-- Reusing dataset G4PII (1.3)
-- Reusing dataset RealSurface (2.1.1)
-- Reusing dataset G4SAIDDATA (2.0)
-- Reusing dataset G4ABLA (3.1)
-- Reusing dataset G4INCL (1.0)
-- Reusing dataset G4ENSDFSTATE (2.2)
-- The following Geant4 features are enabled:
GEANT4_BUILD_CXXSTD: Compiling against C++ Standard '11'
GEANT4_USE_SYSTEM_EXPAT: Using system EXPAT library
GEANT4_USE_GDML: Building Geant4 with GDML support
GEANT4_USE_QT: Build Geant4 with Qt support
GEANT4_USE_RAYTRACER_X11: Build RayTracer driver with X11 support
GEANT4_USE_OPENGL_X11: Build Geant4 OpenGL driver with X11 support
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/xxxxx/Downloads/geant4.10.06.p01/build
% make -j4
编译好之后,sudo make install,记住只要这个安装文件在,将来就可以 sudo make uninstall 进行卸载。
7 然后要到你的安装目录下面,在bin目录下面source geant4.sh
你可以看变量 % env
如果有:
G4NEUTRONHPDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4NDL4.6
G4LEDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4EMLOW7.9.1
G4LEVELGAMMADATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/PhotonEvaporation5.5
G4RADIOACTIVEDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/RadioactiveDecay5.4
G4PARTICLEXSDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4PARTICLEXS2.1
G4PIIDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4PII1.3
G4REALSURFACEDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/RealSurface2.1.1
G4SAIDXSDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4SAIDDATA2.0
G4ABLADATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4ABLA3.1
G4INCLDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4INCL1.0
G4ENSDFSTATEDATA=/Users/kangdehua/Downloads/geant4.10.06.p01/build/data/G4ENSDFSTATE2.2
就代表成功了。
8 测试,拷贝B1到你的g4works目录,在B1下面建一个build目录,进入然后
cmake ..
make
:::::
-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found EXPAT: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib/libexpat.tbd (found suitable version "2.2.8", minimum required is "2.2.8")
-- Found XercesC: /usr/local/lib/libxerces-c.dylib (found suitable version "3.2.3", minimum required is "3.2.3")
-- Found X11: /usr/X11R6/include
-- Looking for XOpenDisplay in /usr/X11R6/lib/libX11.dylib;/usr/X11R6/lib/libXext.dylib
-- Looking for XOpenDisplay in /usr/X11R6/lib/libX11.dylib;/usr/X11R6/lib/libXext.dylib - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Looking for IceConnectionNumber in ICE
-- Looking for IceConnectionNumber in ICE - found
-- Found OpenGL: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework
-- Found XQuartzGL: /usr/X11R6/include
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/kangdehua/Documents/Study/g4works/B1/build
% make -j4
Scanning dependencies of target exampleB1
[ 37%] Building CXX object CMakeFiles/exampleB1.dir/exampleB1.cc.o
[ 50%] Building CXX object CMakeFiles/exampleB1.dir/src/B1DetectorConstruction.cc.o
[ 50%] Building CXX object CMakeFiles/exampleB1.dir/src/B1EventAction.cc.o
[ 50%] Building CXX object CMakeFiles/exampleB1.dir/src/B1ActionInitialization.cc.o
[ 62%] Building CXX object CMakeFiles/exampleB1.dir/src/B1PrimaryGeneratorAction.cc.o
[ 75%] Building CXX object CMakeFiles/exampleB1.dir/src/B1RunAction.cc.o
[ 87%] Building CXX object CMakeFiles/exampleB1.dir/src/B1SteppingAction.cc.o
[100%] Linking CXX executable exampleB1
[100%] Built target exampleB1
然后,运行试试如下图: