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 EventAction.cc

void B1EventAction::EndOfEventAction(const G4Event* event)
{ 
	auto analysisManager = G4AnalysisManager::Instance();  
  // accumulate statistics in run action
  fRunAction->AddEdep(fEdep);
  analysisManager->FillNtupleDColumn(0,0, fEdep);
  analysisManager->AddNtupleRow();  
  //save on txt file
  #ifdef G4MULTITHREADED
  static G4Mutex stuffMutex = G4MUTEX_INITIALIZER;
  G4AutoLock al(&stuffMutex);
#endif
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

B1EventAction.cc (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

depenetxt.cc (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 http://geant4.in2p3.fr/2013/resources/L14-Analysis.pdf tutorial

then

  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;

#endif

B4Analysis.hh (1.9 KB)

Then I included B4analysis both in run action and event action

#include “B4Analysis.hh”

In Run action.cc

  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");
  analysisManagertxt->CreateNtupleDColumn("Edep");
  analysisManagertxt->FinishNtuple();
  1. In void B1RunAction::BeginOfRunAction(const G4Run*) I added
 // Get analysis manager
 G4AnalysisManager* analysisManagertxt = G4AnalysisManager::Instance("cvs");

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

In the Event action.cc 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();

B1RunAction.cc (6.3 KB)
B1EventAction.cc (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.

I found this @anna’s reply Making more than one .csv file - #2 by anna
in which she says that it isn’t possible to save two different files at the same time. Then I can’t save a ROOT File and a txt file.

Is there a method , at the end of the simulation to get the data from the no-binned data file and create a binned one?