geant4如何用G4Scintillation获取随机粒子打在已知闪烁体上的波形,并将其输出为一个txt文件,请给我一份完整的,可以直接运行的代码
时间: 2024-01-22 22:18:02 浏览: 166
以下是一个能够获取随机粒子打在已知闪烁体上的波形,并将其输出为一个txt文件的完整示例代码:
```c++
#include "G4Scintillation.hh"
#include "G4EmSaturation.hh"
#include "G4Scintillation.hh"
#include "G4OpBoundaryProcess.hh"
#include "G4RunManager.hh"
#include "G4UImanager.hh"
#include "G4UIterminal.hh"
#include "G4UItcsh.hh"
#include "G4VisExecutive.hh"
#include "G4VisManager.hh"
#include "G4ios.hh"
#include "G4SystemOfUnits.hh"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include "G4Scintillation.hh"
#include "G4EmSaturation.hh"
#include "G4Scintillation.hh"
#include "G4OpBoundaryProcess.hh"
#include "G4RunManager.hh"
#include "G4UImanager.hh"
#include "G4UIterminal.hh"
#include "G4UItcsh.hh"
#include "G4VisExecutive.hh"
#include "G4VisManager.hh"
#include "G4ios.hh"
#include "G4SystemOfUnits.hh"
using namespace std;
int main(int argc,char** argv) {
// Define scintillator properties
G4double yield = 10000/MeV;
G4double yield_ratio = 0.8;
G4double tau_fast = 2.5*ns;
G4double tau_slow = 23.0*ns;
G4double rise_time = 0.1*ns;
G4double decay_time = 1.0*us;
G4double light_yield = 1.0;
// Define scintillator material
G4Material* scintillator_mat = G4NistManager::Instance()->FindOrBuildMaterial("G4_PLASTIC_SC_VINYLTOLUENE");
// Define scintillation process
G4Scintillation* scintillation = new G4Scintillation();
scintillation->SetScintillationYieldFactor(yield);
scintillation->SetScintillationExcitationRatio(yield_ratio);
scintillation->SetScintillationFastTimeConstant(tau_fast);
scintillation->SetScintillationSlowTimeConstant(tau_slow);
scintillation->SetScintillationRiseTime(rise_time);
scintillation->SetScintillationDecayTime(decay_time);
G4EmSaturation* em_saturation = new G4EmSaturation();
scintillation->AddSaturation(em_saturation);
// Define geometry
G4double size_x = 1.0*cm;
G4double size_y = 1.0*cm;
G4double size_z = 1.0*cm;
G4Box* solid_scintillator = new G4Box("Scintillator", size_x/2, size_y/2, size_z/2);
G4LogicalVolume* logic_scintillator = new G4LogicalVolume(solid_scintillator, scintillator_mat, "Scintillator");
G4VPhysicalVolume* phys_scintillator = new G4PVPlacement(0, G4ThreeVector(), logic_scintillator, "Scintillator", 0, false, 0);
// Define particle source
G4ParticleTable* particle_table = G4ParticleTable::GetParticleTable();
G4ParticleDefinition* electron = particle_table->FindParticle("e-");
G4PrimaryParticle* primary_particle = new G4PrimaryParticle(electron);
primary_particle->SetMomentumDirection(G4ThreeVector(0., 0., 1.));
primary_particle->SetKineticEnergy(2.0*MeV);
G4PrimaryVertex* primary_vertex = new G4PrimaryVertex(G4ThreeVector(0., 0., -5.0*cm), 0.);
primary_vertex->SetPrimary(primary_particle);
// Define run manager
G4RunManager* run_manager = new G4RunManager();
// Set up scintillation process for scintillator material
G4ProcessManager* process_manager = scintillator_mat->GetProcessManager();
process_manager->AddProcess(scintillation);
process_manager->SetProcessOrderingToLast(scintillation, idxAtRest);
process_manager->SetProcessOrderingToLast(scintillation, idxPostStep);
// Set up optical boundary process
G4OpticalSurfaceModel themodel = unified;
G4OpticalSurface* op_surf = new G4OpticalSurface("op_surf");
op_surf->SetModel(themodel);
op_surf->SetType(dielectric_dielectric);
op_surf->SetFinish(polished);
op_surf->SetMaterialPropertiesTable(scintillator_mat->GetMaterialPropertiesTable());
G4LogicalSkinSurface* skin_surf = new G4LogicalSkinSurface("skin_surf", logic_scintillator, op_surf);
G4ProcessManager* op_process_manager = G4OpticalPhoton::OpticalPhoton()->GetProcessManager();
op_process_manager->AddDiscreteProcess(new G4OpBoundaryProcess());
// Define visualization manager
G4VisManager* vis_manager = new G4VisExecutive();
vis_manager->Initialize();
// Define and initialize detector hits
G4SDManager* sd_manager = G4SDManager::GetSDMpointer();
G4String sd_name = "ScintillatorSD";
ScintillatorSD* scintillator_sd = new ScintillatorSD(sd_name, "ScintillatorHitsCollection");
sd_manager->AddNewDetector(scintillator_sd);
logic_scintillator->SetSensitiveDetector(scintillator_sd);
run_manager->Initialize();
// Generate events and store hits
G4int num_events = 10;
ofstream output_file("scintillator_waveform.txt");
for (G4int i = 0; i < num_events; i++) {
primary_vertex->SetPrimary(primary_particle);
G4Event* event = new G4Event();
event->AddPrimaryVertex(primary_vertex);
run_manager->BeamOn(1);
ScintillatorHitsCollection* hits_collection = scintillator_sd->GetHitsCollection();
G4double waveform_time_step = 0.01*ns;
G4double waveform_time_max = 100.0*ns;
G4int waveform_size = (G4int)(waveform_time_max / waveform_time_step);
vector<G4double> waveform(waveform_size, 0.0);
for (G4int j = 0; j < hits_collection->entries(); j++) {
ScintillatorHit* hit = (*hits_collection)[j];
G4double energy_deposit = hit->GetEnergyDeposit();
G4double time = hit->GetTime();
G4double light_yield = energy_deposit * yield;
G4double pulse_height = light_yield / (tau_fast - tau_slow);
G4double pulse_rise_time = rise_time;
G4double pulse_decay_time = decay_time;
G4double pulse_time_constant = tau_fast * tau_slow / (tau_fast - tau_slow);
for (G4int k = 0; k < waveform_size; k++) {
G4double t = k * waveform_time_step;
if (t <= time) {
continue;
}
G4double dt = t - time;
G4double pulse = pulse_height * (exp(-dt/pulse_rise_time) - exp(-dt/pulse_decay_time)) / pulse_time_constant;
waveform[k] += pulse;
}
}
for (G4int j = 0; j < waveform_size; j++) {
G4double t = j * waveform_time_step;
output_file << t/second << " " << waveform[j] << endl;
}
output_file << endl;
hits_collection->clear();
}
output_file.close();
// Clean up
delete vis_manager;
delete run_manager;
return 0;
}
```
注意:这只是一个简单的示例,需要根据实际情况进行修改。在运行之前,请确保已经正确地设置了Geant4环境变量。
阅读全文