G4AnalysisReader appending to filenames

Hello everyone!

I wrote a little program that read/writes pandas dataframes into/from the formatted CSV files written by the G4AnalysisManager. While doing this I found that in the G4CsvAnalysisReader class, the SetFileName() and GetNtuple() methods append to the variable for the CSV file name a string containing the Ntuple name and (what I understand to be) its index. The issue with this is that Geant then can’t find the file. I haven’t been successful in finding what is causing this in the source code. The one clue I have is that the documentation vaguely mentions “While the histograms and profiles objects handled by the analysis reader are of the same type as those handled by the analysis manager, the reader’s ntuple type is different.”

###If this code is in a UserActionClass, say RunAction

#include "G4CsvAnalysisReader.hh"

using G4AnalysisReader = G4CsvAnalysisReader;

void RunAction::BeginRunAction(cont G4Run run){
	auto analysisReader = G4AnalysisReader::Instance();
	G4String FileNameInput = "test_file.csv";
	analysisReader->SetFileName(FileNameInput);
	analysisReader->GetNtuple("TUPLENAME");
	G4double ntupleIndex;
	analysisReader->SetNtupleDColumn("Energy",ntupleIndex);
}

###This will look for "test_file_nt_TUPLENAME_t13.csv" where 13 is a seemingly randominteger. 
###If you try to name your file "test_file_nt_TUPLENAME_t13.csv" in the first place, 
### you will get "test_file_nt_TUPLENAME_t13_nt_TUPLENAME_t9.csv"

Here is the error Geant4 spits out:

G4WT6 > 
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W001
      issued by : G4CsvRFileManager::OpenRFile
Cannot open file test_file_nt_index_t6.csv
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

G4WT6 > 
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W001
      issued by : G4TRNtupleManager<NT>::SetNtupleTColumn
ntuple -1 does not exist.
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

Any help would be appreciated, thank you :saluting_face:

Geant4 Version: geant4-11-03
Operating System: Arch Linux
Compiler/Version: gcc 14.2.1
CMake Version: 4.0.1


I’d create the Ntuples first using this prescription (this section covers it pretty well). You also need to open the file before you can start writing to it. You can be extra safe with:

if ( analysisReader->IsActive() ) {
  analysisReader->OpenFile(); 
 }

Hi jrellin, thank you for the reply! Maybe I’m misunderstanding the documentation or I wasn’t clear with what I am doing. I am using the AnalysisReader Class to read an Ntuple in a CSV file as described here . There doesn’t seem to be an OpenFile method for Analysis Reader unfortunately. Do I need to create an ‘empty’ Ntuple with the Analysis Manager first?

Hello,

The “tN” you’re seeing actually means “thread: N” where N is the thread that is being used. If it is t13 that would mean thread 13. Geant4 makes writing to files thread safe by having each thread have its own file, with a unique name using this “tN” suffix, and then merges them together in a single file at the end.

I would suggest trying to run in sequential mode (single threaded) first and see if your code behaves as expected.

Hello,

The call

analysisReader->SetFileName(FileNameInput);

imitates the behaviour of G4AnalysisManager and generates a filename for the input object in the same way what the analysis manager does when writing the object; in your use case, it appends the ntuple name and thread id.

If you want to avoid this behaviour, you can pass your filename as the 2nd argument in the GetNtuple function:

	auto analysisReader = G4AnalysisReader::Instance();
	G4String FileNameInput = "test_file.csv";
	// analysisReader->SetFileName(FileNameInput);
	analysisReader->GetNtuple("TUPLENAME", FileNameInput);
        ...

Best regards,