Output to ascii file

Hi,

I am trying to output some fit parameters to an ascii file. Therefore I am calling root with a given tree (e.g. root test.root), access histograms with TH1F* hist = (TH1F*)gROOT->FindObject(histname);
fit those and write the parameters to a file with
ofstream fout;
ostringstream ostr_fname("");
ostr_fname << “par_”<<runtime<<"_"<<smthits<<".txt";

  fout.open(ostr_fname.str().c_str());
  TFile *ofile = new TFile(ostr_fname.str().c_str(),"RECREATE");


fout<<"Parameter “<<i<<” “<<k<<”: "<<cstr_par1<Write();
That works fine if I try it only once, but I have the whole stuff in a large loop and the second time I call
TH1F* hist = (TH1F*)gROOT->FindObject(histname);
Root does not find the histogram anymore as the current directory was changed to my output ascii-file and I get an interpretor error.
I do not want to specify the input file name INSIDE my macro (so that I could use infile->cd()), so my question is, how I can get the name of the input file from Root at the beginning, when it is correct and set back to it at the end of the first loop run?

Thanks in advance

Petra

[quote]TH1F* hist = (TH1F*)gROOT->FindObject(histname);
Root does not find the histogram anymore as the current directory was changed to my output ascii-file and I get an interpretor error. [/quote]This is most likely because the current directory has changed between the first and second loop.

You should request the histogram directly from the TFile object:TH1F *hist; myfile->GetObject(histname,hist);

Cheers,
Philippe

This was exactly what I said. I know that the current directory was changed to the ascii file within the loop. But your solution does not help me as I need to know the name !within! Root to go back to the original file. This information I do not have as I am calling Root directly with the file name and do not know how to access the filename in Root afterwards. Any ideas?

Thanks, Petra

Instead of TFile *ofile = new TFile(ostr_fname.str().c_str(),"RECREATE"); use

TDirectory *sav= gDirectory; TFile *ofile = new TFile(ostr_fname.str().c_str(),"RECREATE"); sav->cd();

Cheers,
Philippe.

PS. If this is not helping :slight_smile: please send a full concrete example.