ubuntu20.4安装geant4和root

发布时间 2023-06-14 18:01:30作者: endcase

1.下载需要的软件包和数据包

下载root

https://root.cern/install/all_releases

下载geant4

https://geant4.web.cern.ch/download/all

 

2.准备安装文件

在home目录下面创建一个geant4的文件夹

打开geant4文件夹,在里面创建一个file的文件夹

将下载好的14个文件拖动到这个文件夹(home目录下的/geant4/file/)下

右击终端打开

cd ~

 

将刚刚传入的文件移动到opt目录下

sudo mv geant4/ /opt/

 

进入下载的文件的文件夹目录

cd /opt/geant4/file/

 

移动下载的geant4-v11.1.1.tar.gz到上级目录

sudo mv /opt/geant4/file/geant4-v11.1.1.tar.gz ..

 

返回上一级

cd ..

 

解压

sudo tar -zxvf geant4-v11.1.1.tar.gz

 

删除安装包

sudo rm -rf geant4-v11.1.1.tar.gz

 

3.安装依赖

安装依赖工具

sudo apt install curl g++ libgl1-mesa-dev cmake libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev libhdf5-serial-dev hdf5-tools libexpat1 libexpat1-dev build-essential -y

 

安装qt5

sudo apt install qt5* -y

 

创建两个文件夹

mkdir geant4-install
mkdir geant4-build && cd geant4-build

 

4.安装geant4

sudo cmake -DCMAKE_INSTALL_PREFIX=/opt/geant4/geant4-install \
-DGEANT4_USE_OPENGL_X11=ON \
-DGEANT4_USE_RAYTRACER_X11=ON \
-DGEANT4_USE_QT=ON \
GEANT4_BUILD_MULTITHREADED=ON \
/opt/geant4/geant4-v11.1.1

 

ps:-jN表示应用的电脑逻辑处理器数

sudo make -j6

 

进行安装

sudo make install -j6

 

设置环境变量

echo "source /opt/geant4/geant4-install/bin/geant4.sh" >> ~/.bashrc

 


5.安装geant4的数据包

进入下载的文件的文件夹目录

cd /opt/geant4/file/

 

创建data文件夹

mkdir -p /opt/geant4/geant4-install/share/Geant4/data

 

移动数据包到geant4的安装目录下

sudo mv G4*.tar.gz /opt/geant4/geant4-install/share/Geant4/data

 

然后在data文件夹下批量解压

cd /opt/geant4/geant4-install/share/Geant4/data
sudo ls *.tar.gz | sudo xargs -n1 tar xzvf

 

geant4的数据包已经安装完成,删除数据包

sudo rm -rf G4*.tar.gz

 

使环境变量生效

source ~/.bashrc

 

6.安装root

右击终端打开

进入下载的文件的文件夹目录

cd /opt/geant4/file/

 

解压即可使用 - 在Ubantu系统中解压的命令

sudo tar -zxvf root_v6.28.04.Linux-ubuntu20-x86_64-gcc9.4.tar.gz

 

移动到opt目录下

sudo mv root /opt/

 

删除安装包

sudo rm -rf root_v6.28.04.Linux-ubuntu20-x86_64-gcc9.4.tar.gz

 

配置环境变量

echo "source /opt/root/bin/thisroot.sh" >> ~/.bashrc

 

使环境变量生效

source ~/.bashrc

 

ps之后每次想要用root的时候可以直接使用,更加方便。

验证root

root

 


7.验证安装

右击终端打开

建立工作路径

mkdir ~/geant4_ws && cd ~/geant4_ws

 

将官方例子复制到工作路径里面

cp -r /opt/geant4/geant4-install/share/Geant4/examples ~/geant4_ws
cp -r ~/geant4_ws/examples/basic/B1 ~/geant4_ws
cd B1
mkdir build && cd build

 

下面的命令cmake后面是空格加两个点(表示用上级目录进行cmkae)

cmake ..

 

出错的话执行 source ~/.bashrc

编译运行

make -j6

 

./exampleB1

 

 8.vscode中配置geant4和root

打开vscode

安装插件Chinese、C/C++、CMake Tools

在geant4_ws目录下新建一个文件夹G4_First

在vscode中打开文件夹,新建include,src,build文件夹

按住Ctrl+Shift+P,搜索框中输入c++,点击"编辑配置(JSON)"

修改json文件内容如下

{
    "configurations": [
        {
            "name": "Linux",
            "browse": {
                "path": [
                    "${workspaceFolder}/src",                       //链接source
                    "${workspaceFolder}/include",                   //链接include
                    "/opt/root/include",                            //链接root
                    "/opt/geant4/geant4-install/include/Geant4"     //链接Geant4头文件
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "${workspaceFolder}/src",                       //链接source
                "${workspaceFolder}/include",                   //链接include
                "/opt/root/include",                            //链接root
                "/opt/geant4/geant4-install/include/Geant4"     //链接Geant4头文件
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

 

在src下面新建文件Hello.cc

int main(void){
   return 0;
}

 

在src下新建文件CMakeLists.txt

在文件中填入以下内容保存

#----------------------------------------------------------------------------
# Setup the project
cmake_minimum_required(VERSION 3.16...3.21)
# 工程名称G4_First project(G4_First) #---------------------------------------------------------------------------- # Find Geant4 package, activating all available UI and Vis drivers by default # You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui # to build a batch mode only executable # option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON) if(WITH_GEANT4_UIVIS) find_package(Geant4 REQUIRED ui_all vis_all) else() find_package(Geant4 REQUIRED) endif() #---------------------------------------------------------------------------- # Setup Geant4 include directories and compile definitions # Setup include directory for this project # include(${Geant4_USE_FILE}) include_directories(${PROJECT_SOURCE_DIR}/include) #---------------------------------------------------------------------------- # Locate sources and headers for this project # NB: headers are included so they will show up in IDEs # file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc) file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)

#----------------------------------------------------------------------------
# Add the executable, and link it to the Geant4 libraries
#工程名G4_First,主文件Hello.cc
add_executable(G4_First Hello.cc ${sources} ${headers})
target_link_libraries(G4_First ${Geant4_LIBRARIES})


 

打开终端

cd build && cmake ..
make -j6
./G4_First