# Problem constructing TH2F

**URL:** <https://root-forum.cern.ch/t/problem-constructing-th2f/8895>\
**Category:** ROOT\
**Created:** [September 2, 2009, 1:07pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895 "2009-09-02T13:07:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zlatusha](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/a9adbd/32.png) [@zlatusha](https://root-forum.cern.ch/u/zlatusha)\
**Post date:** [September 2, 2009, 1:07pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/1 "2009-09-02T13:07:55Z")

</div>

Dear experts,

I experience the following problem, I am trying to use the TH2F constructor:

TH2F(const char\* name, const char\* title, Int\_t nbinsx, Double\_t xlow, Double\_t xup, Int\_t nbinsy, const Double\_t\* ybins)

via the following commands in root interactively:

`root [0] const Float_t b_f[9]={0.,0.1,0.16,0.2,0.25,0.3,0.35,0.4,0.6};
root [1] TH2F*h=new TH2F("a","a",10,0,1,9,b_f)`

but I fail with the following message:

`Error: Can't call TH2F::TH2F("a","a",10,0,1,9,b_f) in current scope (tmpfile):1:
Possible candidates are...
(in TH2F)
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(void);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const char* name,const char* title,Int_t nbinsx,Double_t xlow,Double_t xup,Int_t nbinsy,Double_t ylow,Double_t yup);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const char* name,const char* title,Int_t nbinsx,const Double_t* xbins,Int_t nbinsy,Double_t ylow,Double_t yup);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const char* name,const char* title,Int_t nbinsx,Double_t xlow,Double_t xup,Int_t nbinsy,const Double_t* ybins);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const char* name,const char* title,Int_t nbinsx,const Double_t* xbins,Int_t nbinsy,const Double_t* ybins);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const char* name,const char* title,Int_t nbinsx,const Float_t* xbins,Int_t nbinsy,const Float_t* ybins);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const TMatrixFBase& m);
/opt/products/root/5.18.00/lib/libHist.so -1:-1 0 public: TH2F TH2F::TH2F(const TH2F& h2f);
(in TH2)
(in TH1)
***Interpreter error recovered*** `

and I really have no idea why!

root version is 5.18, any help or advice will he highly appreciated!!!

---

<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:** [September 2, 2009, 1:51pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/2 "2009-09-02T13:51:36Z")

</div>

The right way is:

```auto
Double_t b_f[9]={0.,0.1,0.16,0.2,0.25,0.3,0.35,0.4,0.6};
TH2F*h=new TH2F("a","a",10,0,1,8,b_f);
```

---

<div class="post-metadata">

**Author:** ![zlatusha](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/a9adbd/32.png) [@zlatusha](https://root-forum.cern.ch/u/zlatusha)\
**Post date:** [September 2, 2009, 2:51pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/3 "2009-09-02T14:51:20Z")

</div>

This is really strange, Floats were working ALWAYS for all the rest of the constructors of TH2F (TH1F respectively), this is why I didn’t try to use doubles at all… thanks however!

---

<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:** [September 2, 2009, 2:54pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/4 "2009-09-02T14:54:11Z")

</div>

That’s not only Float into Double, the number of bins should be 8… not 9

---

<div class="post-metadata">

**Author:** ![zlatusha](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/a9adbd/32.png) [@zlatusha](https://root-forum.cern.ch/u/zlatusha)\
**Post date:** [September 2, 2009, 3:13pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/5 "2009-09-02T15:13:43Z")

</div>

oh, no!

believe me it was correct, I was trying to compile and I couldn’t, and now when i changed the floats to double it all vanished like with wand…

---

<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:** [September 2, 2009, 3:19pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/6 "2009-09-02T15:19:17Z")

</div>

(Number of bins = array\_dimension) gives:

```auto
root [0] Double_t b_f[9]={0.,0.1,0.16,0.2,0.25,0.3,0.35,0.4,0.6};
root [1] TH2F*h=new TH2F("a","a",10,0,1,9,b_f)
Error in <TAxis::TAxis::Set>: bins must be in increasing order
root [2]
```

(Number of bins = array\_dimension-1) gives:

```auto
root [0] Double_t b_f[9]={0.,0.1,0.16,0.2,0.25,0.3,0.35,0.4,0.6};
root [1] TH2F*h=new TH2F("a","a",10,0,1,8,b_f)
root [2]
```

That’s logic, the array defines the bins’ boundaries.

---

<div class="post-metadata">

**Author:** ![tpochep](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/f05b48/32.png) [@tpochep](https://root-forum.cern.ch/u/tpochep)\
**Post date:** [September 2, 2009, 5:15pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/7 "2009-09-02T17:15:59Z")

</div>

[quote]believe me it was correct  
[/quote]  
No. I do not believe.

[quote]I was trying to compile and I couldn’t  
[/quote]  
I can see a little contradiction here: “it was correct” and at the same moment “I couldn’t” - so decide, if it is correct and you can compile it, or it’s incorrect and you can not compile it. Compiler usually knows C++ better, than you 🙂))

> [@](#):
>
> and now when i changed the floats to double it all vanished like with wand…

You know, float and double are two different types, pointer to float and pointer to double are different types, and pointer to float can not be implicitly converted into pointer to double (and that’s a very good and right thing about pointers). It’s a pity, that CINT can do this:

```auto
float arr[10];
double *p = arr;
```

which is illegal and very serious error in a code.

---

<div class="post-metadata">

**Author:** ![tpochep](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/f05b48/32.png) [@tpochep](https://root-forum.cern.ch/u/tpochep)\
**Post date:** [September 2, 2009, 5:20pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/8 "2009-09-02T17:20:28Z")

</div>

//duplicate was deleted

---

<div class="post-metadata">

**Author:** ![krisfrajer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a5b964/32.png) [@krisfrajer](https://root-forum.cern.ch/u/krisfrajer)\
**Post date:** [September 6, 2009, 4:11am UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/9 "2009-09-06T04:11:00Z")

</div>

> [@couet](#):
>
> That’s not only Float into Double, the number of bins should be 8… not 9

Hi  
This comment of yours solve a problem I had. Why is it 8 instead of 9? Could you please clarify this?

This small detail was not obvious in the ROOT documentation.  
Thanks.  
Kris

---

<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:** [September 7, 2009, 7:43am UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/10 "2009-09-07T07:43:10Z")

</div>

To define “n” bins you need “n+1” bins’ limits :

```auto

 v1 v2 v3 v3
 +---+---+---+
  b1 b2 b3

the 3 bins bi (i=1,3) need 4 values vi (i=1,4) to be define.

```

---

<div class="post-metadata">

**Author:** ![krisfrajer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a5b964/32.png) [@krisfrajer](https://root-forum.cern.ch/u/krisfrajer)\
**Post date:** [September 7, 2009, 3:04pm UTC](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/11 "2009-09-07T15:04:38Z")

</div>

Thanks a lot!
