Deposited energy counts and binning

Hello, I usually save my GEANT4 output in ROOT files to analyse them by ROOT. Unfortunately, my new boss can’t use ROOT then he asked me to save data also in txt files.

Then, I added this code in the

void B1EventAction::EndOfEventAction(const G4Event* event)
	auto analysisManager = G4AnalysisManager::Instance();  
  // accumulate statistics in run action
  analysisManager->FillNtupleDColumn(0,0, fEdep);
  //save on txt file
  static G4Mutex stuffMutex = G4MUTEX_INITIALIZER;
  G4AutoLock al(&stuffMutex);
static bool first = true;
static std::ofstream results ("DepositedEnergyData.txt", std::ofstream::out | std::ofstream::app);
  if (first) {
    first = false;
    results << "#,eDep/keV,..." << std::endl;
results<< fEdep<< G4endl;

to save data on a txt file. Here the EventAction and a result file example (4.2 KB)
DepositedEnergyData.txt (3.1 MB)

As I wrote, I use ROOT to analyse data and I wrote a macro ROOT to get data by the txt file and make a plot setting the binning etc.

This is the macro (2.2 KB)

But my boss needs the data previously binned.
Then he needs a files with two culomns:
FIRST CULOMN: The deposited energy values from 0 to maximum with binning 1keV (i.e. 0,1,2,3,4,5,…ecc)
SECOND CULOM: the number of counts

He sent me this example (notice that in the example there is also a third culomn that is a branching ratio)
exampledata.txt (84.8 KB)

Is there a way to directly save data in this way on GEANT? I mean…instead of saving the deposited energy for each event, saving binned data at the end of all the

I tried following the B4 example and this tutorial


  1. I copied the B4analysis.hh in my include directory and I enabled the cvs method
#ifndef B4Analysis_h
#define B4Analysis_h 1

//#include "g4root.hh"
#include "g4csv.hh"
//#include "g4xml.hh"

//#include "G4GenericAnalysisManager.hh"
//using G4AnalysisManager = G4GenericAnalysisManager;


B4Analysis.hh (1.9 KB)

Then I included B4analysis both in run action and event action

#include “B4Analysis.hh”

In Run

  1. In B1RunAction::B1RunAction() I added
G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance();
  G4cout << "Using " << analysisManagertxt->GetType() << G4endl;
  analysisManagertxt->CreateH1("Edep","Edep", 2500, 0., 2500*keV);
   // Creating ntuple for the txt file
  analysisManagertxt->CreateNtuple("B4", "Edep");
  1. In void B1RunAction::BeginOfRunAction(const G4Run*) I added
 // Get analysis manager
 G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance("cvs");

  // Open an output file
  G4String fileName = "Edepbinned";
  1. in void B1RunAction::EndOfRunAction(const G4Run* run) I added
 G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance();

In the Event file

  1. In void B1EventAction::EndOfEventAction(const G4Event* event) I added
 // get analysis manager
   G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance();

  // fill histograms
  analysisManagertxt->FillH1(0, fEdep);
  // fill ntuple
  analysisManagertxt->FillNtupleDColumn(0, fEdep);
  analysisManagertxt->AddNtupleRow(); (6.3 KB) (4.5 KB)

But I get this error

Error.txt (12.1 KB)

On the contray, if I don’t Include the B4analysis.hh and I write
G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance();
instead of
G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance("cvs");

I don’t get errors but at the end of the simulation:

  1. I get a root file instead of a cvs file
  2. The first root file (the one that I want) and the second root file (the one that should be a cvs file) looks like merged and data aren’t correctly written.