# TGraph2D to TH2 segmentation violation

**URL:** <https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535>\
**Category:** Newbie\
**Created:** [July 20, 2020, 9:06pm UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535 "2020-07-20T21:06:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [July 20, 2020, 9:06pm UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535/1 "2020-07-20T21:06:16Z")

</div>

I have some root files with TGraph2D objects in it that I need to use for interpolation. I read in other thread that using Interpolation from TH2 is faster, so I tried to Get the objects as a TH2 but I get errors when using Interpolate():

```auto
root [11] TH2F *h2 = (TH2F*)_file0->Get("Efield_x")
(TH2F *) 0x1f18e90
root [12] Efield_x->Interpolate(0,0)
(Double_t) -177.869
root [13] h2->Interpolate(0,0)

 ***Break*** segmentation violation

```

If use `h2->Draw("COLZ")` it draws the expected map.  
What is going on and how can I fix this?

---

<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:** [July 21, 2020, 7:03am UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535/2 "2020-07-21T07:03:45Z")

</div>

What is `Efield_x` ? a TGraph2D ?  
If that’s the case you cannot do:

```auto
TH2F *h2 = (TH2F*)_file0->Get("Efield_x")

```

You can try:

```auto
TH2F *h2 = Efield_x->GetHistogram();

```

---

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [August 3, 2020, 3:45pm UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535/3 "2020-08-03T15:45:41Z")

</div>

This works and gives me a TH2F object that when I Draw it looks exactly like the TGraph2D. However when I use `Interpolate()` the result can be significantly different. For example:

```auto
h2->Interpolate(0.05,0)
(Double_t) -303134.
Efield_x_graph->Interpolate(0.05,0)
(Double_t) -459678.

```

I understand that the algorithm used for the interpolate function is different, but is this difference normal? Is there a way around this?

---

<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:** [August 3, 2020, 3:58pm UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535/4 "2020-08-03T15:58:01Z")

</div>

> [@Nyx](#):
>
> I understand that the algorithm used for the interpolate function is different, but is this difference normal? Is there a way around this?

It might be different depending on your data. For instance if your TGraph2D has a small number of points with a high gradian between them … also the number of bins of the underlying histogram can have an effect … they to increase the number of bins. It is difficult to tell without seeing both …

---

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [August 3, 2020, 4:00pm UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535/5 "2020-08-03T16:00:17Z")

</div>

The points in my TGraph2D are equidistant. Is it possible to have a bin per TGraph2D point?

---

<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:** [August 4, 2020, 7:05am UTC](https://root-forum.cern.ch/t/tgraph2d-to-th2-segmentation-violation/40535/6 "2020-08-04T07:05:27Z")

</div>

You can use [SetNpx](https://root.cern/doc/master/classTGraph2D.html#afa751502760cf6701e51bde3b1bfef72) and [SetNpy](https://root.cern/doc/master/classTGraph2D.html#a6af49e8ef38a3e0fa98ca5fdd522c3a4) to change le number of bins of the underlying histogram.
