# Converting single column text files into TH1F for fitting

**URL:** <https://root-forum.cern.ch/t/converting-single-column-text-files-into-th1f-for-fitting/38037>\
**Category:** ROOT\
**Created:** [February 18, 2020, 6:57pm UTC](https://root-forum.cern.ch/t/converting-single-column-text-files-into-th1f-for-fitting/38037 "2020-02-18T18:57:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [February 18, 2020, 6:57pm UTC](https://root-forum.cern.ch/t/converting-single-column-text-files-into-th1f-for-fitting/38037/1 "2020-02-18T18:57:16Z")

</div>

Hi!

I’m not very well-versed in C/C++ and I was hoping someone could point me in the right direction:

I’ve got a .dat file which contains only a column of counts, each coming from a specific channel. I intend to read this .dat file and save it as a TH1F .root file, which each row corresponding to a TH1F bin.

I have read Rene Brun’s `basic.c` and `basic2.c`, but I can’t seem to get it to work. I have also heard that TGraphs can read text files natively and am not adverse to first reading them in as TGraphs and then converting it into a TH1D (for future fitting).

Thanks in advance for any pointers!

* * *

_ROOT Version:_ 6.09/01  
_Platform:_ Linux Mint 19.1  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 18, 2020, 8:59pm UTC](https://root-forum.cern.ch/t/converting-single-column-text-files-into-th1f-for-fitting/38037/2 "2020-02-18T20:59:26Z")

</div>

> [@Filling Histograms with a text file](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/2):
>
> Try: /\* Usage examples: root [0] .L Am241.cxx root [1] Am241(); // expects "Am241.txt" in the current directory root [1] Am241("/full/path/to/some/Am241.txt"); or simply: root [0] .x Am241.cxx root [0] .x Am241.cxx("/full/path/to/some/Am241.txt") \*/ #include "TGraph.h" #include \<fstream\> void Am241(const char \*file = "Am241.txt") { if ((!file) || (!file[0])) return; // just a precaution TGraph \*g = new TGraph(); g-\>SetTitle("Count Rate;N\_{counts};# occurencies"); double y;…

BTW. There exists the [TGraph::Fit](https://root.cern.ch/doc/master/classTGraph.html#aa978c8ee0162e661eae795f6f3a35589) method.

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [February 19, 2020, 6:51pm UTC](https://root-forum.cern.ch/t/converting-single-column-text-files-into-th1f-for-fitting/38037/3 "2020-02-19T18:51:11Z")

</div>

Thanks! It worked!

And thank you for the tip about the TGraph::Fit method!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 4, 2020, 6:56pm UTC](https://root-forum.cern.ch/t/converting-single-column-text-files-into-th1f-for-fitting/38037/4 "2020-03-04T18:56:55Z")

</div>

Closed on March 4, 2020, 6:56pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
