# How to create a dat file from a 2D histogram?

**URL:** <https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [May 21, 2022, 6:47am UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045 "2022-05-21T06:47:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviphy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aviphy](https://root-forum.cern.ch/u/aviphy)\
**Post date:** [May 21, 2022, 6:47am UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045/1 "2022-05-21T06:47:13Z")

</div>

Hi,  
I am using the following line of codes to generate an ASCII file from a 1D histogram:

```auto
{
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?

 ![2dHist](https://root-forum.cern.ch/uploads/default/original/3X/5/e/5ed395077c49ccb1813bc77a6309b7e179a5ae9c.png)

Thank you

\_ROOT Version: 6.18/04  
\_PLATFORM: Windows 10  
\_COMPILER: Visual Studio 2019

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 21, 2022, 6:52am UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045/2 "2022-05-21T06:52:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aviphy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aviphy](https://root-forum.cern.ch/u/aviphy)\
**Post date:** [May 21, 2022, 2:03pm UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045/3 "2022-05-21T14:03:11Z")

</div>

I’m doing the following:

```auto
{
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

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 21, 2022, 5:01pm UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045/4 "2022-05-21T17:01:55Z")

</div>

Try with:

```auto
tof2d->->GetXaxis()->GetBinCenter(i)

```

and

```auto
tof2d->->GetYaxis()->GetBinCenter(j)

```

and

```auto
tof2d->GetBinContent(i,j)

```

Otherwise, at some point there will be a function for this, see:

> <https://github.com/root-project/root/issues/10474>
>
> \### Explain what you would like to see improved
> I open this to keep track of a …suggestion by @lmoneta to save histograms as csv or txt files.
> https://github.com/root-project/root/issues/9491#issuecomment-1094786152
> 
> Right now, there are all sorts of workarounds to achieve this (see below), but they are easy to forget and periodically there are new questions on the forum wrt the same thing.
> A function doing
> \`TH1::SaveAs(".txt")\`
> would be more user-friendly.
> 
> \### Optional: share how it could be improved
> Move TGraph implementation in https://github.com/root-project/root/issues/9491 to a class THistSaveAs
> 
> \### Setup
> \`\`\`
> ------------------------------------------------------------------
> | Welcome to ROOT 6.24/06 https://root.cern |
> | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
> | Built for linuxx8664gcc on Sep 02 2021, 14:20:23 |
> | From tags/v6-24-06@v6-24-06 |
> | With |
> | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
> ------------------------------------------------------------------
> \`\`\`
> 
> \### Additional context
> https://root-forum.cern.ch/t/save-data-points-from-a-histogram-in-a-txt-file/49768/3
> https://root-forum.cern.ch/t/save-histogram-in-txt-file/43616/13
> https://stackoverflow.com/questions/28970124/cern-root-exporting-data-to-plain-text
> https://root-forum.cern.ch/t/save-the-contents-of-histogram-to-text-file/28616/2
> https://root-forum.cern.ch/t/histogram-to-ascii/14080/6

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [May 21, 2022, 5:17pm UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045/5 "2022-05-21T17:17:12Z")

</div>

[Go-HEP](https://go-hep.org) has a command (available for all major operating systems) that can convert a `TH{1,2}x` histogram into the [YODA](https://yoda.hepforge.org/) ASCII file format.

see:

- [root2yoda command - go-hep.org/x/hep/cmd/root2yoda - pkg.go.dev](https://pkg.go.dev/go-hep.org/x/hep/cmd/root2yoda)

you can download it for your own OS/arch from:

- [https://go-hep.org/dist/latest/](https://go-hep.org/dist/latest/)
- [https://go-hep.org/dist/latest/root2yoda-darwin\_amd64.exe](https://go-hep.org/dist/latest/root2yoda-darwin_amd64.exe)
- [https://go-hep.org/dist/latest/root2yoda-linux\_386.exe](https://go-hep.org/dist/latest/root2yoda-linux_386.exe)
- [https://go-hep.org/dist/latest/root2yoda-linux\_amd64.exe](https://go-hep.org/dist/latest/root2yoda-linux_amd64.exe)
- [https://go-hep.org/dist/latest/root2yoda-linux\_arm64.exe](https://go-hep.org/dist/latest/root2yoda-linux_arm64.exe)
- [https://go-hep.org/dist/latest/root2yoda-windows\_amd64.exe](https://go-hep.org/dist/latest/root2yoda-windows_amd64.exe)
- [https://go-hep.org/dist/latest/root2yoda-windows\_386.exe](https://go-hep.org/dist/latest/root2yoda-windows_386.exe)

FWIW, the code is there:

- [hep/main.go at v0.31.1 · go-hep/hep · GitHub](https://github.com/go-hep/hep/blob/v0.31.1/cmd/root2yoda/main.go)

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [May 21, 2022, 5:19pm UTC](https://root-forum.cern.ch/t/how-to-create-a-dat-file-from-a-2d-histogram/50045/6 "2022-05-21T17:19:16Z")

</div>

FYI, here is what the YODA format looks like:

- [hep/h1d\_gaps\_v1\_golden.yoda at main · go-hep/hep · GitHub](https://github.com/go-hep/hep/blob/main/hbook/testdata/h1d_gaps_v1_golden.yoda)
- [hep/h2d\_v2\_golden.yoda at main · go-hep/hep · GitHub](https://github.com/go-hep/hep/blob/main/hbook/testdata/h2d_v2_golden.yoda)
