# Converting TH1F into a TGraph with smooth line connecting all the points

**URL:** <https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967>\
**Category:** Newbie\
**Created:** [May 8, 2019, 10:25pm UTC](https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967 "2019-05-08T22:25:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [May 8, 2019, 10:25pm UTC](https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967/1 "2019-05-08T22:25:10Z")

</div>

Dear All,

I have plotted a TH1F histogram, I would like to now plot a TGraph from this histogram. But don’t know how to go about doing that, may I have some kindly.

```auto
Int_t n = 6000000;
Float_t norm = sigma/h_rapidity->Integral("width");// normalizing histogram to cross-section
 // hdndy->Scale(sigma/(binwidth *nevents)); 
hdndy = (TH1F*)h_rapidity->Clone();// making copy of rapidity histogram
hdndy->GetXaxis()->SetTitle(" Rapidity");
hdndy->GetYaxis()->SetTitle("d\sigma/dy");
hdndy->Scale(norm); //d\sigma/dy histogram.
hdndy->Draw("CL");

```

Thank you.

---

<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 9, 2019, 7:45am UTC](https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967/2 "2019-05-09T07:45:18Z")

</div>

Plotting the histogram with option `L` should draw the histogram as a line. But you may need in addition the option `HIST` if your histogram has errors:

```auto
hdndy->Draw("HIST L");

```

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [May 9, 2019, 9:33pm UTC](https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967/3 "2019-05-09T21:33:03Z")

</div>

Perhaps even the option `"C"` might be more appealing with a splined curve between the bins. More information on the 1D histogram plotting options: [https://root.cern.ch/doc/master/classTHistPainter.html#HP01b](https://root.cern.ch/doc/master/classTHistPainter.html#HP01b)

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [May 9, 2019, 11:29pm UTC](https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967/4 "2019-05-09T23:29:40Z")

</div>

Thank you

I am still not able to transform this TH1F into a TGraph, I added this portion to the above code but TGraph is blank.

Int\_t j = 100;// number of bins

Double\_t x[60000];  
Double\_t y[60000];  
for(int i = 0; i GetNbinsX; i++ )  
{  
x[i] = hdndy-\>GetBinCenter(i) ;  
y[i] = hdndy-\>GetBinContent(i);  
}

cout \<\< x[i] \<\< y[i] \<\< endl;  
TGraph \*gr = new TGraph(j, x,y);  
TCanvas \*a = new TCanvas(“Rapidity”," ",600,600);  
gr-\>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 10, 2019, 7:11am UTC](https://root-forum.cern.ch/t/converting-th1f-into-a-tgraph-with-smooth-line-connecting-all-the-points/33967/5 "2019-05-10T07:11:10Z")

</div>

```auto
void h2g() {
   auto h = new TH1F("h","h",100,-4,4);
   h->FillRandom("gaus",20000);
   auto g = new TGraph();
   for(int i = 1; i <= h->GetNbinsX(); i++ ) g->SetPoint(i-1, h->GetBinCenter(i), h->GetBinContent(i));
   g->Draw("AL");
}

```
