VS Code 配置使用 Geant4 和 Root (一)

本文介绍了VisualStudioCode作为C++开发IDE的优秀特性,包括其丰富的插件、高效的代码提示和调试功能。作者分享了如何通过VSCode配置编译、运行和调试Geant4及Root,并指出VSCode的开源背景、高速性能和跨平台支持使其成为开发者的新宠。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


theme: orange

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情

一直以来,C++开发的IDE的选择,我一直首选Visual Studio,从08版本,到当前常用的19版本,很长时间都没变过。那还有其它IDE可以选择吗,当然有的!这篇文章手把手带领大家通过VS Code 配置编译、运行、调试 Geant4 和 Root。

VS Code 介绍

对于程序开发,如果你够强,使用上古神器vi也行,没有人会拦着你。可是,IDE不就是为了提高开发调试效率吗?满足软件工程的一些思想吗?对于C++程序,我们还可以选择CLion,这是Jetbrain家族的一个成员,如果你是Jetbrain的全套使用者,那在使用习惯上,你一定爱上它。但今天的主角不是它,而是万金油 Visual Studio Code,简称VS Code

Visual Studio Code 默认支持非常多的编程语言,包括 JavaScript、TypeScript、CSS 和 HTML;也可以通过下载扩展支持 Python、C/C++、Java 和 Go 在内的其他语言。支持功能包括语法高亮、括号补全、代码折叠和代码片段;对于部分语言,可以使用 IntelliSense。Visual Studio Code 也支持调试 Node.js 程序。和 GitHub 的 Atom一样,Visual Studio Code 也基于 Electron 框架构建。

image.png 以下是官网对其的定义,

Code editing Redefined.Free.
Built on open source. Runs everywhere.

可以看出来是何其的张狂,不过它有它自傲的底气,包括但不限于:
1. 微软出品,从云端编辑器Monaco进化而来,拥抱开源,粉丝众多,接受程度极高! 2. 丰富的插件,决定了它不是一个简单的文本编辑器,而是一个真正的IDE。 3. 它提出并实践的LSP协议让代码提示变得更加正规化。 4. 速度和性能兼之,优秀得令一众竞品头秃。 5. 多平台支持,包括Windows、Linux、Mac。

可以说它吸收了百家之长,去其糟粕,取其精华,同时又反哺了百家的发展。


未完待续!
没看够,订阅关注我,持续更新优秀好文!

Geant4中,获取粒子打在闪烁体上产生的脉冲波形可以通过使用G4Scintillation模拟闪烁体中的光发射过程。 首先,需要在你的代码中包含以下头文件: ```cpp #include "G4Scintillation.hh" #include "G4EmSaturation.hh" #include "G4OpAbsorption.hh" #include "G4OpBoundaryProcess.hh" #include "G4OpticalPhoton.hh" #include "G4SDManager.hh" #include "G4RunManager.hh" ``` 然后,在你的DetectorConstruction类的构造函数中添加以下代码: ```cpp // Create a scintillator material G4Material* scintillator = new G4Material("Scintillator", 1.032*g/cm3, 2); scintillator->AddElement(G4Element::GetElement("C"), 9); scintillator->AddElement(G4Element::GetElement("H"), 10); // Create a scintillator solid and logical volume G4Box* scintillatorSolid = new G4Box("ScintillatorSolid", 5*cm, 5*cm, 5*cm); G4LogicalVolume* scintillatorLogical = new G4LogicalVolume(scintillatorSolid, scintillator, "ScintillatorLogical"); // Set scintillation properties G4Scintillation* scintillationProcess = new G4Scintillation(); scintillationProcess->SetScintillationYieldFactor(1.0); scintillationProcess->SetTrackSecondariesFirst(true); // important for pulse shape simulation scintillationProcess->SetScintillationExcitationRatio(1.0); scintillationProcess->SetVerboseLevel(0); scintillatorLogical->SetUserLimits(new G4UserLimits(DBL_MAX,DBL_MAX,DBL_MAX,DBL_MAX,0))); // Set optical properties G4double photonEnergy[] = {1.9*eV, 4.3*eV}; G4double refractiveIndex[] = {1.58, 1.58}; G4double absorptionLength[] = {1*m, 1*m}; G4MaterialPropertiesTable* scintillatorMPT = new G4MaterialPropertiesTable(); scintillatorMPT->AddProperty("RINDEX", photonEnergy, refractiveIndex, 2); scintillatorMPT->AddProperty("ABSLENGTH", photonEnergy, absorptionLength, 2); scintillatorLogical->SetMaterialPropertiesTable(scintillatorMPT); ``` 在你的PhysicsList类中添加以下代码: ```cpp // Add scintillation process to optical photons G4Scintillation* theScintillationProcess = new G4Scintillation(); theScintillationProcess->SetScintillationYieldFactor(1.0); theScintillationProcess->SetTrackSecondariesFirst(true); // important for pulse shape simulation theScintillationProcess->SetScintillationExcitationRatio(1.0); theScintillationProcess->SetVerboseLevel(0); G4EmSaturation* emSaturation = new G4EmSaturation(); G4OpticalPhoton* opticalPhoton = G4OpticalPhoton::OpticalPhotonDefinition(); G4ProcessManager* opticalPhotonPM = opticalPhoton->GetProcessManager(); opticalPhotonPM->AddDiscreteProcess(theScintillationProcess); opticalPhotonPM->AddSaturation(emSaturation); ``` 最后,在你的EventAction类中添加以下代码: ```cpp // Get the sensitive detector manager G4SDManager* sdManager = G4SDManager::GetSDMpointer(); // Get the scintillator sensitive detector G4VSensitiveDetector* scintillatorSD = sdManager->FindSensitiveDetector("ScintillatorSD", false); if (!scintillatorSD) { G4ExceptionDescription exceptionDescription; exceptionDescription << "Cannot find ScintillatorSD"; G4Exception("EventAction::EndOfEventAction()", "MyCode0003", JustWarning, exceptionDescription); return; } // Get the hits collection for the scintillator G4HCofThisEvent* hcOfThisEvent = event->GetHCofThisEvent(); if (!hcOfThisEvent) { G4ExceptionDescription exceptionDescription; exceptionDescription << "Cannot get hits collection of this event"; G4Exception("EventAction::EndOfEventAction()", "MyCode0002", JustWarning, exceptionDescription); return; } ScintillatorHitsCollection* scintillatorHitsCollection = dynamic_cast<ScintillatorHitsCollection*>(hcOfThisEvent->GetHC(scintillatorSD->GetCollectionID())); if (!scintillatorHitsCollection) { G4ExceptionDescription exceptionDescription; exceptionDescription << "Cannot get hits collection for ScintillatorSD"; G4Exception("EventAction::EndOfEventAction()", "MyCode0004", JustWarning, exceptionDescription); return; } // Loop over the hits in the scintillator and add up the energy deposits G4double totalEnergyDeposited = 0.0; for (int i = 0; i < scintillatorHitsCollection->GetSize(); ++i) { ScintillatorHit* hit = dynamic_cast<ScintillatorHit*>(scintillatorHitsCollection->GetHit(i)); if (!hit) { G4ExceptionDescription exceptionDescription; exceptionDescription << "Cannot get hit " << i << " from ScintillatorHitsCollection"; G4Exception("EventAction::EndOfEventAction()", "MyCode0005", JustWarning, exceptionDescription); continue; } totalEnergyDeposited += hit->GetEdep(); } // Get the run manager G4RunManager* runManager = G4RunManager::GetRunManager(); // Get the scintillation process const G4Scintillation* scintillationProcess = dynamic_cast<const G4Scintillation*>(G4ProcessTable::GetProcessTable()->FindProcess("Scintillation", opticalPhoton)); if (!scintillationProcess) { G4ExceptionDescription exceptionDescription; exceptionDescription << "Cannot find Scintillation process"; G4Exception("EventAction::EndOfEventAction()", "MyCode0006", JustWarning, exceptionDescription); return; } // Set the scintillation process parameters const G4Material* scintillatorMaterial = runManager->GetCurrentEvent()->GetPrimaryVertex(0)->GetMaterial(); const G4MaterialPropertiesTable* scintillatorMPT = scintillatorMaterial->GetMaterialPropertiesTable(); if (!scintillatorMPT) { G4ExceptionDescription exceptionDescription; exceptionDescription << "Cannot get material properties table for Scintillator material"; G4Exception("EventAction::EndOfEventAction()", "MyCode0007", JustWarning, exceptionDescription); return; } G4MaterialPropertyVector* scintillationFastIntegral = scintillatorMPT->GetProperty("FASTCOMPONENT"); G4MaterialPropertyVector* scintillationSlowIntegral = scintillatorMPT->GetProperty("SLOWCOMPONENT"); G4double scintillationYield = scintillationProcess->GetScintillationYieldFactor(); G4double scintillationExcitationRatio = scintillationProcess->GetScintillationExcitationRatio(); G4double scintillationTimeConstant = scintillationProcess->GetScintillationTime(); G4double recombinationTimeConstant = scintillatorMPT->GetConstProperty("RESCALETIMECONSTANT"); G4double scintillationRiseTimeConstant = scintillatorMPT->GetConstProperty("RISERTIMECONSTANT"); G4double scintillationDecayTimeConstant = scintillationTimeConstant / (1.0 + scintillationExcitationRatio); G4double scintillationPeakTime = scintillationRiseTimeConstant * log(scintillationYield * totalEnergyDeposited / scintillationFastIntegral->GetMaxValue()); G4double scintillationPeakAmplitude = scintillationFastIntegral->GetMaxValue() / (1.0 + scintillationExcitationRatio) / exp(-scintillationPeakTime / scintillationRiseTimeConstant); G4double scintillationArea = scintillationYield * totalEnergyDeposited; // Simulate the pulse shape const G4double tMin = -10 * scintillationDecayTimeConstant; const G4double tMax = 100 * scintillationTimeConstant; const G4int nBins = 1000; TH1D* pulseShape = new TH1D("pulseShape", "Pulse Shape", nBins, tMin, tMax); pulseShape->SetDirectory(0); for (int iBin = 1; iBin <= nBins; ++iBin) { const G4double t = pulseShape->GetBinCenter(iBin); G4double scintillationIntegral = 0.0; for (int i = 0; i < scintillationFastIntegral->GetVectorLength(); ++i) { const G4double photonEnergy = scintillationFastIntegral->Energy(i); const G4double photonYield = scintillationFastIntegral->Value(i); const G4double photonProbability = scintillationProcess->GetPhotonProbability(photonEnergy, scintillatorMaterial); if (photonProbability > 0.0) { const G4double absorptionLength = scintillatorMPT->GetConstProperty("ABSLENGTH"); const G4double absorptionProbability = exp(-t / absorptionLength); const G4double emissionTimeConstant = scintillationFastIntegral->GetConstProperties().GetDoubleProperty("TIMECONSTANT"); const G4double emissionProbability = photonProbability * photonYield * exp((scintillationPeakTime - t) / emissionTimeConstant); if (emissionProbability > 0.0) { scintillationIntegral += emissionProbability * absorptionProbability; } } } pulseShape->SetBinContent(iBin, scintillationIntegral * scintillationPeakAmplitude); } // Save the pulse shape to a ROOT file TFile* outputFile = new TFile("output.root", "RECREATE"); pulseShape->Write(); outputFile->Close(); ``` 这段代码首先获取了ScintillatorSD中的光子能量沉积,然后计算了光子发射的脉冲波形。脉冲波形是通过在时间范围内积分每个光子的发射概率吸收概率来计算的。最后,脉冲波形被保存到ROOT文件中。 运行代码后,你将得到个名为output.rootROOT文件,其中包含了脉冲波形的图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值