# Filling Histograms with a text file

**URL:** <https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190>\
**Category:** Newbie\
**Created:** [May 22, 2019, 7:34am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190 "2019-05-22T07:34:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 22, 2019, 7:34am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/1 "2019-05-22T07:34:44Z")

</div>

Hi, I am new to ROOT and I am trying to create a macro for a 1-D histogram which is filled with a .txt file, but I don’t really know how to do it.  
I’ve read some recommendations on other forums on how to do this, but whenever I try I get an empty canvas, meaning that it is not reading the text file nor filling it.

Here is the macro (which is most likely very wrong since I am new to ROOT… pls help)  
[testalphacount.C](https://root-forum.cern.ch/uploads/default/original/3X/9/6/967b0ac46aef9512d9a794b69ab0a60116829e2a.C) (268 Bytes)

Here’s the text file in question (it has about 4000+ entries):  
[Am241.txt](https://root-forum.cern.ch/uploads/default/original/3X/8/b/8b3d13b80d3d6040edf752bf076e82330fcbfffb.txt) (8.2 KB)

now, I also have a couple questions:  
Does it matter where I save the text file for ROOT to read it into the histogram? if so, where should I save it? How do I read the text file on the macro to fill the histogram?

---

<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:** [May 22, 2019, 8:11am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/2 "2019-05-22T08:11:03Z")

</div>

Try:

```auto
/*
  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;
  ifstream inp(file);
  while (inp >> y) g->SetPoint(g->GetN(), g->GetN(), y); // note: x = g->GetN()
  g->Draw("AL");
}

```

---

<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 22, 2019, 8:11am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/3 "2019-05-22T08:11:40Z")

</div>

1. In your file you have 4095 lines.
2. You create your histogram with 100 bins
3. You do the loop over SetBinContent from 1 to 6000

This means that all the bin numbers above 100 will be outside the histogram range and all the bins between 4095 and 6000 will be the same but in addition the value x will be undefined.

You should first clarify what you want to do exactly. Does one line in the .txt file correspond to one bin of your histogram ?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 22, 2019, 8:21am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/4 "2019-05-22T08:21:32Z")

</div>

Hi,

Add a name to the column in your `Am241.txt` file (e.g. `Col0`) and try:

```auto
root [0] auto tdf = ROOT::RDF::MakeCsvDataFrame("Am241.txt");
root [1] auto h = tdf.Histo1D("Col0");
root [2] h->Draw()

```

Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 22, 2019, 8:42am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/5 "2019-05-22T08:42:11Z")

</div>

Or even better, without touching your `Am241.txt` file (thanks @Danilo):

```auto
root [0] auto tdf = ROOT::RDF::MakeCsvDataFrame("Am241.txt", false);
root [1] auto h = tdf.Histo1D("Col0");
root [2] h->Draw()

```

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 22, 2019, 5:17pm UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/6 "2019-05-22T17:17:12Z")

</div>

Hi!  
Sorry for any bad wording.  
Overall I wanted big bins in my histogram (hence why I chose it to have just 100 bins), and now I see what I did wrong on the SetBinContent loop.  
Anyhow, if I wanted each line in the .txt to correspond to one bin, what should I do then?  
Thank you for your response!

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 22, 2019, 5:21pm UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/7 "2019-05-22T17:21:15Z")

</div>

Hi!  
I keep getting a blank canvas and an error saying “Error in TGraphPainter::PaintGraph: illegal number of points (0)”

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 22, 2019, 5:33pm UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/8 "2019-05-22T17:33:56Z")

</div>

Hi!  
I added a name to the column in the .txt file and put your code into a macro, but It gives me this error:

```auto
Error in <TRint::HandleTermInput()>: std::runtime_error caught: Unknown column: Col0

```

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 22, 2019, 6:53pm UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/9 "2019-05-22T18:53:00Z")

</div>

OK, so remove the name and try the second method:

```auto
auto tdf = ROOT::RDF::MakeCsvDataFrame("Am241.txt", false);
auto h = tdf.Histo1D("Col0");
h->Draw();

```

---

<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 23, 2019, 8:21am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/10 "2019-05-23T08:21:10Z")

</div>

> [@JorgeM](#):
>
> if I wanted each line in the .txt to correspond to one bin, what should I do then?

your macro should be:

```auto
{
  auto am241=new TH1F("am241",
           "Count Rate;N_{counts};# occurencies",
           4095,
           -0.5,
           6000);
  ifstream inp; double x;
  inp.open("Am241.txt");
  for (int i=1; i<=4095; i++) {
    inp >> x;
    am241->SetBinContent(i,x);
  }
  am241->Draw();
}

```

And you will get:

 ![50](https://root-forum.cern.ch/uploads/default/original/3X/d/a/da68546df258a3d46e6c28267e9f8e79f06a6a06.jpeg)

---

<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 23, 2019, 8:24am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/11 "2019-05-23T08:24:03Z")

</div>

I left 6000 as maximum value for the X axis. It was intentional to show that the number of bins and the axis values are decoupled. In that particular case it might be more logical to replace 6000 by 4095… that’s up to you to decide.

---

<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 23, 2019, 8:25am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/12 "2019-05-23T08:25:19Z")

</div>

Also, a log scale along the Y axis might be something to consider:

 ![36](https://root-forum.cern.ch/uploads/default/original/3X/f/c/fc72b210ecfaecba218ed037bf201d8c3734a5a8.jpeg)

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 23, 2019, 9:01am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/13 "2019-05-23T09:01:34Z")

</div>

Hi Couet!  
Thank you for your input!  
However, I’m still having issues with reading the text file.  
Here’s the code to my macro:

```auto
{
  auto am241=new TH1F("am241",
		     "am241;N_{counts};# occurencies",
		      4095, //number of bins
		      -0.5, //x-axis min
		      6000); //x-axis max
  ifstream inp; double x;
  inp.open("Am241.txt");
  for (int i=1; i<4095; i++) {
    inp >> x;
    am241->SetBinContent(i,x);
  }
  c1 = new TCanvas("c1","Bin Size", 1200,400);
  c1->cd(1);
  am241->Draw();
  
  
}

```

and the histogram gets drawn, however it is still empty:

 ![Screenshot%20from%202019-05-23%2003-58-04](https://root-forum.cern.ch/uploads/default/original/3X/d/5/d5efd9fc8559186728302eb906ea81ed95c4381d.png)

My text file is located in the same directory as my macros, but its not reading it ☹

 ![Screenshot%20from%202019-05-23%2004-00-53](https://root-forum.cern.ch/uploads/default/original/3X/9/4/9418469f4e4543b9ef013e6b49f8b643d116dc04.png)  
Is there a way to call the file or something of sorts?

Thank you for everything else!

---

<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 23, 2019, 9:16am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/14 "2019-05-23T09:16:45Z")

</div>

When I run your macro I get this plot:

 ![12](https://root-forum.cern.ch/uploads/default/original/3X/1/1/11ce39eedc1159798b87e181f80cd0c78ac4c057.jpeg)  
How do you run the macro ?  
Do you have some messages when you it ?  
I have the text file `Am241.txt` and the macro `testalphacount.C` in the same folder. I start root at the shell prompt from that folder and I do:

```auto
$ root
   ------------------------------------------------------------
  | Welcome to ROOT 6.17/01 https://root.cern |
  | (c) 1995-2019, The ROOT Team |
  | Built for macosx64 on May 22 2019, 06:34:58 |
  | From heads/master@v6-16-00-rc1-2164-g789b8e6cd5 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] .x testalphacount.C
root [1] 

```

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 23, 2019, 9:34am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/15 "2019-05-23T09:34:55Z")

</div>

I usually just open the terminal and open root from the predetermined prompt shell, then I execute the file just how you do it, but I don’t get any messages:

 ![Screenshot%20from%202019-05-23%2004-22-24](https://root-forum.cern.ch/uploads/default/original/3X/7/b/7b6cf372ca60afb9f966f11f470c36463187719c.png)

I’ve also just checked from where I’m opening root and I get this:

 ![Screenshot%20from%202019-05-23%2004-29-07](https://root-forum.cern.ch/uploads/default/original/3X/3/5/350eb00f6288d9a29fb708c0407825c16139d350.png)

Now, when I try to open root from the macros folder, it just boots root from the same directory as before:

![Screenshot%20from%202019-05-23%2004-33-30](https://root-forum.cern.ch/uploads/default/original/3X/4/7/47613da9357866911c21dae4d8b701408a570824.png)

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 23, 2019, 9:37am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/16 "2019-05-23T09:37:16Z")

</div>

Could it be the way I built root?  
should I uninstall and then rebuild it?

---

<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:** [May 23, 2019, 9:39am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/17 "2019-05-23T09:39:37Z")

</div>

The “Am241.txt” file must be in the same subdirectory in which you start “root”.

---

<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 23, 2019, 9:45am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/18 "2019-05-23T09:45:23Z")

</div>

In `/home/jorge` (where you are starting root) if you do:

```auto
$ ls testalphacount.C Am241.txt 

```

you should get:

```auto
Am241.txt testalphacount.C

```

---

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [May 23, 2019, 10:02am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/19 "2019-05-23T10:02:11Z")

</div>

I moved the file to the home directory and it works now!  
However, is there a better way to do this? since I don’t want to flood my home directory with text files for root to read it.

@couet @Wile_E_Coyote

Thank you for your responses!

---

<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:** [May 23, 2019, 10:16am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/20 "2019-05-23T10:16:43Z")

</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;…

[Next page](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190.md?page=2)
