Hello,
I want to open two root files, one for data output and one for input, within a geant4 simulation.
I tried various methods to get this working, but my “best” result is with the following code:
G4bool hbarPrimGenAction::open_root_spectrum(G4String filename){
if(fileisopen) return true;
//std::cout << "READING FILE" << std::endl;
TFile *f = new TFile(filename.c_str(),"READ");
if(!f->IsOpen()) return false;
TH1D *velspectr2;
f->GetObject("velocity-z", velspectr2);
velspectr = TH1D(*velspectr2);
//if(velspectr == 0) return false;
f->Close();
//std::cout << "FILE CLOSED" << std::endl;
fileisopen = true;
return true; }
this works for reading the data, and using the spectrum within the root file as input for primary particle generation, but afterwards I cant write the results of the simulation to a root file… I always get error messages like this:
[quote][…]
Error in TROOT::WriteTObject: The current directory (root) is not associated with a file. The object (VelocityDist) has not been written.
Error in TROOT::WriteTObject: The current directory (root) is not associated with a file. The object (VelocityDistDet) has not been written.
[…][/quote]
but if I turn the input spectrum off and use a Maxwell distribution or something similar, I have no problem with getting the output… does anyone have an idea what I’m doing wrong?
Thanks,
Clemens