Hi,
I am using the following line of codes to generate an ASCII file from a 1D histogram:
{
using std::cout;
using std::endl;
std::ofstream myFile1;
myFile1.open("file.txt");
myFile1 << "BinNo" << "\t" << "Start" << "\t" << "End" << "\t" << "No" << endl;
for(Int_t i=1; i <= hist->GetNbinsX(); i++){
myFile1 << i << " " << ((double)(hist->GetBinCenter(i)) - (double)(hist->GetBinWidth(i) / 2.)) << " " << ((double)(hist->GetBinCenter(i)) + (double)(hist->GetBinWidth(i) / 2.)) << " " << (double)hist->GetBinContent(i) << endl;
}
myFile1.close();
}
However, I’m having trouble to generate an ASCII file from a 2D histogram (figure attached). Can somebody please suggest something?
Thank you
_ROOT Version: 6.18/04
_PLATFORM: Windows 10
_COMPILER: Visual Studio 2019
couet
2
You should do an extra loop on the Y bins inside the one you have on the X bins. I guess you did that? what problems do you have?
I’m doing the following:
{
using std::cout;
using std::endl;
std::ofstream myFile2;
myFile2.open("Ar8+_NO_TOF2D.txt");
//myFile1 << "BinNo" << "\t" << "Start" << "\t" << "End" << "\t" << "No" << endl;
myFile2 << "BinNo" << "\t" << "Start" << "\t" << "End" << "\t" << "NoX" << "\t" << "BinNo" << "\t" << "Start" << "\t" << "End" << "\t" << "NoY" << endl;
for(Int_t i=1; i <= tof2d->GetNbinsX(); i++)
{
//myFile1 << i << " " << ((double)(kerd->GetBinCenter(i)) - (double)(kerd->GetBinWidth(i) / 2.)) << " " << ((double)(kerd->GetBinCenter(i)) + (double)(kerd->GetBinWidth(i) / 2.)) << " " << (double)kerd->GetBinContent(i) << endl;
///*
for(Int_t j=1; j <= tof2d->GetNbinsY(); j++)
{
myFile2 << i << " " << ((double)(tof2d->GetBinCenter(i)) - (double)(tof2d->GetBinWidth(i) / 2.)) << " " << ((double)(tof2d->GetBinCenter(i)) + (double)(tof2d->GetBinWidth(i) / 2.)) << " " << (double)tof2d->GetBinContent(i) << j << " " << ((double)(tof2d->GetBinCenter(j)) - (double)(tof2d->GetBinWidth(j) / 2.)) << " " << ((double)(tof2d->GetBinCenter(j)) + (double)(tof2d->GetBinWidth(j) / 2.)) << " " << (double)tof2d->GetBinContent(j) << endl;
}
//*/
}
myFile2.close();
}
Getting error messages: TH2D:GetBinWidth/Center invalid in 2d histogram --return Nan
Try with:
tof2d->->GetXaxis()->GetBinCenter(i)
and
tof2d->->GetYaxis()->GetBinCenter(j)
and
tof2d->GetBinContent(i,j)
Otherwise, at some point there will be a function for this, see:
Go-HEP has a command (available for all major operating systems) that can convert a TH{1,2}x
histogram into the YODA ASCII file format.
see:
you can download it for your own OS/arch from:
FWIW, the code is there:
FYI, here is what the YODA format looks like: