# Making histogram from text file

**URL:** <https://root-forum.cern.ch/t/making-histogram-from-text-file/53689>\
**Category:** Newbie\
**Created:** [February 20, 2023, 12:11pm UTC](https://root-forum.cern.ch/t/making-histogram-from-text-file/53689 "2023-02-20T12:11:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mona](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@Mona](https://root-forum.cern.ch/u/Mona)\
**Post date:** [February 20, 2023, 12:11pm UTC](https://root-forum.cern.ch/t/making-histogram-from-text-file/53689/1 "2023-02-20T12:11:31Z")

</div>

Hello everyone.  
I am trying to generate a histogram from my data file but the data files contains a lot colums and I want to read data one by one.

For example:  
2 4 5  
5 7 0  
6 1

And I want to read data and fill the histogram start 2,4,5,5,7 and so on…

I am trying this:

void Plotting(){

TH1F \*h = new TH1F(“h”, “histogram”,1000,5.,9.);  
ifstream inp; double x;

inp.open(“data.dat”);  
for (int i=1; i\<=1000; i++) {  
inp \>\> x;  
h-\>SetBinContent(i,x);  
}  
h-\>Draw();  
}

I am afraid that I am only reading the first column. Can someone help me how I can read all values one by one?

---

<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 20, 2023, 12:49pm UTC](https://root-forum.cern.ch/t/making-histogram-from-text-file/53689/3 "2023-02-20T12:49:28Z")

</div>

It looks like your “data file” contains variable length vectors, so you’d better contact the “author” of this file, who should be able to provide you with an appropriate “parser”.

---

<div class="post-metadata">

**Author:** ![Mona](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@Mona](https://root-forum.cern.ch/u/Mona)\
**Post date:** [February 20, 2023, 12:51pm UTC](https://root-forum.cern.ch/t/making-histogram-from-text-file/53689/4 "2023-02-20T12:51:05Z")

</div>

I am actually generating the data files using a monte carlo program.

---

<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 20, 2023, 12:52pm UTC](https://root-forum.cern.ch/t/making-histogram-from-text-file/53689/5 "2023-02-20T12:52:51Z")

</div>

So, talk to people familiar with this “monte carlo program” (e.g., your colleagues and/or supervisor).

---

<div class="post-metadata">

**Author:** ![Mona](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@Mona](https://root-forum.cern.ch/u/Mona)\
**Post date:** [February 20, 2023, 12:53pm UTC](https://root-forum.cern.ch/t/making-histogram-from-text-file/53689/6 "2023-02-20T12:53:55Z")

</div>

Okay. Thank you, I will do so.
