About visualization and root file making

Hi everyone,

I wanted to make a simple particle gun and detector for neutrinos, I will beam electron neutrino then measure the flux at the detector. I will mostly use the geants own mechanics. It is for myself training me. I wanted to show myself that it will not interact with biological tissue. However, it doesn’t save the root file. It does compile. It creates the executable as it should do (also UI doesn’t work. I tried OpenGL and RayTracer both gave error.)

Here is the code excerpt both from RootFile write and where I try to save it.

/ Save data to ROOT file, get the event action and call WriteDataToRootFile
const MyEventAction* myEventAction = dynamic_cast<const MyEventAction*>(runManager->GetUserEventAction());
if (myEventAction) {
} else {
if (myEventAction) {
// A different event action is set, but it’s not of type MyEventAction
// Handle the case when a different event action is set
// G4String actionClassName = myEventAction->GetUserActionName();
G4String expectedClassName = typeid(MyEventAction).name();
G4cerr << "Error: Unexpected event action type. Expected: " << expectedClassName << “, Found: Undefined” << G4endl;
} else {
// No event action is set
// Handle the case when no event action is set
G4cerr << “Error: No event action is set.” << G4endl;

void WriteDataToRootFile(const std::string& filename) const {
std::string outputPath = “/users/burkanbereketoglu/downloads/”; // specify the desired output

// Construct the full path including the output directory
std::string fullFilePath = outputPath + filename;
TFile* file = new TFile(fullFilePath.c_str(), "RECREATE");
TTree* tree = new TTree("flux_tree", "Flux Data");

// Create branches in the ROOT tree
std::vector<G4double> fluxXArray;
std::vector<G4double> fluxYArray;
std::vector<G4double> fluxZArray;
tree->Branch("fluxX", &fluxXArray);
tree->Branch("fluxY", &fluxYArray);
tree->Branch("fluxZ", &fluxZArray);

// Fill the branches with the flux data
fluxXArray = fluxX;
fluxYArray = fluxY;
fluxZArray = fluxZ;

// Write the ROOT tree to the file

delete file;


std::vector fluxX;
std::vector fluxY;
std::vector fluxZ;

hope this helps for better understanding.


There is also the geant4 way of scoring + writing to file: Command-based scoring — Book For Application Developers 11.1 documentation

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.