# Create histogram from textfile with several binning boundaries

**URL:** <https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912>\
**Category:** Newbie\
**Created:** [May 9, 2022, 2:49pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912 "2022-05-09T14:49:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [May 9, 2022, 2:49pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/1 "2022-05-09T14:49:19Z")

</div>

Dear all,

I want to create a histogram from a txt.file. The first 6 columns are the bin boundaries for the 3 parameter m, pt and y. The 7th column is the yield and the 8th one is the uncertainty. I have created a function to read the lines of this textfile.

```cpp
void DecodeLine(std::string line, double &m_low, double &m_high, double &y_low, double &y_high, double pt_low, double pt_high, double yield, double uncert){
  TString rs(line.c_str());
  printf("rs: %s\n", rs.Data());
  TObjArray *toks = rs.Tokenize(" ");
  if(toks->GetEntries()!=9) return;
  m_low = ((TObjString *)toks->At(0))->String().Atof();
  m_high = ((TObjString *)toks->At(1))->String().Atof();
  y_low = ((TObjString *)toks->At(2))->String().Atof();
  y_high = ((TObjString *)toks->At(3))->String().Atof();
  pt_low = ((TObjString *)toks->At(4))->String().Atof();
  pt_high = ((TObjString *)toks->At(5))->String().Atof();
  yield = ((TObjString *)toks->At(6))->String().Atof();
  uncert = ((TObjString *)toks->At(7))->String().Atof();
   printf("m_low %f, m_low %f, y_low %f, y_high %f, pt_low %f, pt_high %f, yield %f, uncert %f", m_low, m_high, y_low, y_high, pt_low, pt_high, yield, uncert);
}

```

In my main function , I did this

```cpp
ifstream in("results_DYPbPb_eppspdf_noresumbutkt_ptbins_mu1.txt");
  std::string str;
  double m_low, m_high, pt_low, pt_high, y_low, y_high, yield, uncert;
   
 while(getline(in,str)){
  if(str.find("#") < str.length()) continue; // Line commented out
  if(str.length() == 0) continue;
  DecodeLine(str, m_low, m_high, pt_low, pt_high, y_low, y_high, yield, uncert);
  if(y_low > 6) continue;

```

Im not sure how to create a histogram with these bin boundaries. At the end I want a TH2D histogram with m as X, pt as Y, the yield as dN/dm and the uncertainty.

I have attached the textfile.

Thanks

[results\_DYPbPb\_eppspdf\_noresumbutkt\_ptbins\_mu1.txt](https://root-forum.cern.ch/uploads/short-url/rBmdh1TRyAUWpWbcQMkIwwZID1D.txt) (607.7 KB)

---

<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 9, 2022, 8:23pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/2 "2022-05-09T20:23:59Z")

</div>

Maybe @moneta can give you some tips

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 10, 2022, 11:49am UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/3 "2022-05-10T11:49:42Z")

</div>

Hi,

You need to create an array (e.g. a std::vector) for the bin boundaries of the X (m) and Y (pt), and then use this constructor for TH2D:  
[https://root.cern.ch/doc/master/classTH2D.html#a075a0fbe5fa92d6ddac7cda6897e63a8](https://root.cern.ch/doc/master/classTH2D.html#a075a0fbe5fa92d6ddac7cda6897e63a8)  
where xbins and ybins are the arrays with the bin boundaries

Then you need to call `TH2::SetBinContent` passing the bin numbers and yield values

If you need I could provide you an example

Lorenzo

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [May 10, 2022, 12:26pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/4 "2022-05-10T12:26:17Z")

</div>

Hi thanks for the tips, yeah you can provide me an example to understand it better. What should I do with the parameters nbinsx and nbinsy?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 10, 2022, 12:29pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/5 "2022-05-10T12:29:52Z")

</div>

Ok, I will provide you an example but I could do probably only next week.  
`nbinsx` and `nbinsy` are the number of bins in the x and y axis. You should compute counting the boundaries when reading the file. Remember that the boundary array should have a dimension of `nbins+1`.

---

<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 10, 2022, 12:37pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/6 "2022-05-10T12:37:20Z")

</div>

[TH2::TH2](https://root.cern.ch/doc/master/classTH2.html#a5667b0486e50fd40cba6b640ca4ba67b)

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [May 10, 2022, 12:57pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/7 "2022-05-10T12:57:37Z")

</div>

so my xbins is the array of my low-edges for each bin. Is this the first column or second column of my textfile? I am not sure.

---

<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 10, 2022, 2:27pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/8 "2022-05-10T14:27:26Z")

</div>

You have two possibilities (which should return the same):

1. you take all “…lo” (unique) values and the last one from the “…hi”,
2. you take the first one from the “…lo” and then all “…hi” (unique) values.

BTW. You need to remove the last line from your “.txt” data file.

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [May 10, 2022, 3:33pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/9 "2022-05-10T15:33:29Z")

</div>

Hi thanks,

I did it, it worked finally!

One last question. I have the uncertainty in my txt file. Can I do something like that SetBinError(i, uncertainty) to have my uncertainty?

---

<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 10, 2022, 7:35pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/10 "2022-05-10T19:35:26Z")

</div>

You can use [SetBinContent](https://root.cern/doc/master/classTH2.html#a88d8a33f395f594543da88d2d1758bd2) and [SetBinError](https://root.cern/doc/master/classTH1.html#a97efc3b54a4408501266298c78f10a70).

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [May 12, 2022, 12:14pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/11 "2022-05-12T12:14:52Z")

</div>

Unfortunetly , I will get an eternal loop when I try to do a double loop over my nbinsx and nbinsy. What did I wrong?

```auto
Double_t m_low, m_high, y_low, y_high, pt_low, pt_high, yield, uncertainty;
    Int_t nbinsx = 25;
    Double_t binsmee[26] = {2, 2.25, 2.5, 2.75, 3., 3.25, 3.5, 3.75, 4., 4.25, 4.5, 4.75, 5., 5.25, 5.5, 5.75, 6, 6.25, 6.5, 6.75, 7., 7.25, 7.5, 7.75, 8., 20.};
    Int_t nbinsy = 41;
    Double_t binsptee[42] = {0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2., 2.25, 2.5, 2.75, 3., 3.25, 3.5, 3.75, 4., 4.25, 4.5, 4.75, 5., 5.25, 5.5, 5.75, 6., 6.25, 6.5, 6.75, 7., 7.25, 7.5, 7.75, 8., 8.25, 8.5, 8.75, 9., 9.25, 9.5, 9.75, 10., 20.};
    TH2D *drell = new TH2D("drell", "Drell Yann", nbinsx, binsmee, nbinsy, binsptee);
    ifstream in("results_DYPbPb_eppspdf_noresumbutkt_ptbins_mu1.txt");

    while (in) {
        in_pos = in.tellg();
        if (in >> m_low >> m_high >> y_low >> y_high >> pt_low >> pt_high >> yield >>
            uncertainty) {
		if(y_low > 6) continue;
	    yield = yield * 1e-15 * 2.632e-11;
	    printf("Found entry, yield is %f\n", yield);
		for (int i=1;i<=nbinsx;i++)
		{
			for(int j=1;j<=nbinsy;j++)
				
			{	printf("Found entry, yield is %f\n", yield);
				drell->SetBinContent(i,j,yield);
				}
       
		}
        } else {
            in.clear();
            in.seekg(in_pos);
            
            if (getline(in, s)) {
                if (s.empty() || s.find("#") != std::string::npos) {
                } else {
                    cout << "Error in line: " << s << "\n";
                }
            }
        }
    }
	  

```

---

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 12, 2022, 1:05pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/12 "2022-05-12T13:05:39Z")

</div>

Do `getline(in, s)` as well after `in_pos = in.tellg()`?

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [May 12, 2022, 1:12pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/13 "2022-05-12T13:12:23Z")

</div>

You mean while(getline) after in\_pos = in.tellg()?

---

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 12, 2022, 2:25pm UTC](https://root-forum.cern.ch/t/create-histogram-from-textfile-with-several-binning-boundaries/49912/15 "2022-05-12T14:25:54Z")

</div>

My last suggestion is wrong. Please ignore it. I tried you macro. After commenting the `printf`, the macro quits normally. Maybe the printout slowed down the execution?
