Export histogram in ascii - Print("all") loses precision

Dear co-rooters,

I have a TH1F histogram and while on a root session I type the the following

When I open the file with an editor, surprisingly I see the following

fSumw[3608466]=1453, x=0.00400941 fSumw[3608467]=1450, x=0.00400941 fSumw[3608468]=1450, x=0.00400941 fSumw[3608469]=1448, x=0.00400941 fSumw[3608470]=1453, x=0.00400941 fSumw[3608471]=1455, x=0.00400941 fSumw[3608472]=1449, x=0.00400941 fSumw[3608473]=1459, x=0.00400941 fSumw[3608474]=1449, x=0.00400941 fSumw[3608475]=1452, x=0.00400942 fSumw[3608476]=1448, x=0.00400942 fSumw[3608477]=1456, x=0.00400942 fSumw[3608478]=1446, x=0.00400942 fSumw[3608479]=1457, x=0.00400942 fSumw[3608480]=1455, x=0.00400942 fSumw[3608481]=1455, x=0.00400942 fSumw[3608482]=1451, x=0.00400942 fSumw[3608483]=1455, x=0.00400942 fSumw[3608484]=1448, x=0.00400943 fSumw[3608485]=1455, x=0.00400943 fSumw[3608486]=1449, x=0.00400943 fSumw[3608487]=1454, x=0.00400943 fSumw[3608488]=1453, x=0.00400943 fSumw[3608489]=1451, x=0.00400943 fSumw[3608490]=1452, x=0.00400943 fSumw[3608491]=1453, x=0.00400943 fSumw[3608492]=1451, x=0.00400943

It’s like it can’t print with a good precision. Is there a way to print with a better precision?

Thanks a lot in advance!

I think you need to manually loop over all bins and print them using something like “%.17g” for doubles and “%.7g” for floats.

Thanks a lot for your post!
How can I print bins?
I am not sure…

See the source code of TH1::Print.

Wow!
That looks odd…
It’s like rewriting the function…

I’ve written the following code, but I still seem to have the same problem…

for (int i=1; i<=hist->GetNbinsX(); i++){ if(hist->GetBinCenter(i)>5.e-3 && hist->GetBinCenter(i)<7.e-3){ myfile << (double) hist->GetBinCenter(i) << "\t" << hist->GetBinContent(i) << endl; } }

Any idea on why this might be happening?

Finally the solution is to use std::setprecision(10). The full code is the following

[code]#include “TH1.h”
#include “TH1F.h”

#include
#include
#include
using namespace std;

void histo2ascii(TH1* hist){

ofstream myfile;
myfile.open ("movie_small.txt");

for (int i=1; i<=hist->GetNbinsX(); i++){
	if(hist->GetBinCenter(i)>5.e-3 && hist->GetBinCenter(i)<7.e-3){
		myfile << std::setprecision(10) << hist->GetBinCenter(i) << "\t" << hist->GetBinContent(i) << endl;
		
	}
}

myfile.close();

}[/code]

Yes you found the solution. There is many example on the web like:
stackoverflow.com/questions/5540 … using-cout