Creating .txt file

Hi I am new to Grant4 and I am looking to create a data file. I have looked at the examples B4 and B5 but for now I only need a text file. How can I change the code from either of those two examples to get a text file??

In the aplication I am creating right now I am generating fotons in a volume and recording the hits on a cilindrical Ge detector. I am using the code from the B1 example and changing a few things. Does Geant4 have a command like the one in examples B4 and B5 that allows me to create the .txt file or do I create it as a normal c++ code with ofstream MyFile("filename.txt");.

And where do I implement the code to create the .txt file? Do I have to creat a whole new .cc file or can I introduce it in one of the existing ones like

1 Like

You can write your own data file wherever you want - only be careful to protect it in the case of multithreading. So typically, you would obtain, say, the energy deposited in a step in the user stepping action:

// collect energy deposited in this step
G4double eDep = step->GetTotalEnergyDeposit();

static G4Mutex stuffMutex = G4MUTEX_INITIALIZER;
G4AutoLock al(&stuffMutex);
static std::ofstream stuff(“stuff.csv”);
static bool first = true;
if (first) {
first = false;
stuff << “#,eDep/keV,…” << std::endl;
stuff << ‘,’ << eDep/keV /* … */ << std::endl;

Generally I’d write a csv (comma-separated value) file, but you can call it what you like and write anything you want.

If you want to range across more than one function or class you have to be a bit smarter.

Alternatively, you can use the G4 Analysis system - see basic example B3.

1 Like

Thank you very much for the reply. This works fine for me for now.