Dear all,
I want to simulate 0vbb and record the coordinates and energy for each step of all events using bxdecay0, so my code is based on the example “bxdecay0_g4_ex01”
To manage the data for each step, I added SteppingAction.cc to get the informations and DataManager.cc to create a tree and fill it.
#include "SteppingAction.hh"
#include "G4Step.hh"
#include "G4Track.hh"
#include "G4ParticleDefinition.hh"
#include "G4SystemOfUnits.hh"
#include "EventAction.hh"
SteppingAction::SteppingAction(EventAction* eventAction)
: fEventAction(eventAction) {}
void SteppingAction::UserSteppingAction(const G4Step* step) {
G4double edep = step->GetTotalEnergyDeposit();
if (edep <= 0.) return;
G4StepPoint* prePoint = step->GetPreStepPoint();
G4ThreeVector pos = prePoint->GetPosition();
G4cout << "Step at x=" << pos.x()
<< ", y=" << pos.y()
<< ", z=" << pos.z()
<< ", edep=" << edep << G4endl;
fEventAction->AddStep(pos.x(), pos.y(), pos.z(), edep);
G4cout << "Step at x=" << pos.x() << ", edep=" << edep << G4endl;
}
DataManager* DataManager::fInstance = nullptr;
DataManager::DataManager():
fFile(0),fTree(0)
{}
void DataManager::Book() {
fFile = new TFile("event_tree.root", "RECREATE");
fTree = new TTree("T", "Step info per event");
fTree->Branch("x", &fX);
fTree->Branch("y", &fY);
fTree->Branch("z", &fZ);
fTree->Branch("edep", &fEdep);
}
DataManager::~DataManager() {
if (fTree)
delete fTree;
if (fFile){
fFile ->Close();
delete fFile;
}
}
DataManager* DataManager::GetInstance() {
if (!fInstance) fInstance = new DataManager();
return fInstance;
}
void DataManager::AddStep(double x, double y, double z, double edep) {
fX.push_back(x);
fY.push_back(y);
fZ.push_back(z);
fEdep.push_back(edep);
}
void DataManager::FillEvent() {
fTree->Fill();
fX.clear(); fY.clear(); fZ.clear(); fEdep.clear();
}
void DataManager::WriteEvent() {
if (fTree) fFile->Write();
}
And then fix the EventAction and RunAction
EventAction::EventAction()
: G4UserEventAction()
{
// Set default print level
G4RunManager::GetRunManager()->SetPrintProgress(500);
//return;
}
EventAction::~EventAction()
{
return;
}
void EventAction::BeginOfEventAction(const G4Event*)
{
return;
}
void EventAction::EndOfEventAction(const G4Event* evt)
{
G4int evtID = evt->GetEventID();
G4int printProgress = G4RunManager::GetRunManager()->GetPrintProgress();
// Printing survey:
if (evtID % printProgress == 0) {
G4cout << "End of event #" << evtID << "." << G4endl << G4endl;
}
DataManager::GetInstance()->FillEvent();
}
void EventAction::AddStep(G4double x, G4double y, G4double z, G4double edep) {
DataManager::GetInstance()->AddStep(x, y, z, edep);
}
void RunAction::BeginOfRunAction(const G4Run*)
{
auto dataManager = DataManager::GetInstance();
dataManager ->DataManager::Book();
return;
}
void RunAction::EndOfRunAction(const G4Run*)
{
auto dataManager = DataManager::GetInstance();
dataManager ->DataManager::WriteEvent();
return;
}
When running the simulation, the output of terminal is normal just like
======================== run summary ======================
The run was 1 geantino of 1 GeV
===========================================================
Nb of generated particles:
e-: 2 Emean = 1.499 MeV ( 658.5 keV --> 2.34 MeV)
So I’m sure the simulation works well.
But
sh: segmentation fault ./bxdecay0_g4_ex01 Se82_debug.mac
was showed finally and the root file I created was not filled.
Could anyone show some suggestions?
_Geant4 Version:_v11.2.0
_Operating System:_MacOS M1
_Compiler/Version:_clang version 15.0.0
_CMake Version:_3.29.3