# Weighting 2D histograms

**URL:** <https://root-forum.cern.ch/t/weighting-2d-histograms/49181>\
**Category:** Newbie\
**Created:** [March 14, 2022, 9:04pm UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181 "2022-03-14T21:04:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abunka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@abunka](https://root-forum.cern.ch/u/abunka)\
**Post date:** [March 14, 2022, 9:04pm UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/1 "2022-03-14T21:04:29Z")

</div>

Hello,  
I have some analysis code that outputs both 1 and 2D histograms that require weighting. For the 1D histograms it works fine to add the weight after the fill item:

```auto
hist("name")->Fill(fillItem, weight);

```

However, trying to do this for 2D histograms is failing:

```auto
hist("name2D")->Fill(fillItemA, fillItemB, weight);

```

It gives the following error at compilation:

```auto
error: no matching function for call to 'TH1::Fill(float&, float&, double&)'
                                         hist(histTitle)->Fill(logLike, mcResponse, mainWeight);

```

I don’t understand why it is interpreting the fill command as a TH1 and not a TH2. both TH1 and TH2 are included. What do I need to do to weight my 2D histograms?

---

<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:** [March 15, 2022, 7:19am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/2 "2022-03-15T07:19:58Z")

</div>

For TH2 [the Fill() signature is with 3 doubles](https://root.cern/doc/master/classTH2.html#a9d1a326ec5cef292722a357746c5a795).

---

<div class="post-metadata">

**Author:** ![abunka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@abunka](https://root-forum.cern.ch/u/abunka)\
**Post date:** [March 15, 2022, 6:49pm UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/3 "2022-03-15T18:49:31Z")

</div>

Interesting, the histogram is filling (without weights) when I feed it two floats, and is defined as a TH2F. Unfortunately, the problem persists having changed to doubles.

```auto
 error: no matching function for call to 'TH1::Fill(double&, double&, double&)'
                                         hist(histTitle)->Fill(logLike, mcResponse, mainWeight);
                                                                                              ^

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 16, 2022, 4:44am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/4 "2022-03-16T04:44:56Z")

</div>

It’s probably better / more useful if you show the code you are using, or at least a minimal but complete working example that reproduces your problem.

---

<div class="post-metadata">

**Author:** ![abunka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@abunka](https://root-forum.cern.ch/u/abunka)\
**Post date:** [March 16, 2022, 7:40am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/5 "2022-03-16T07:40:51Z")

</div>

Yes, I will work on this. The problem is occurring within ATLAS eventloop/analysis base, and so far I can not recreate the error in “pure” root.

---

<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:** [March 16, 2022, 8:53am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/6 "2022-03-16T08:53:33Z")

</div>

I tried to make a small reproducer:

```auto
#include "TH2F.h"
#include "TH2D.h"

void TH2Fill()
{
   float xf=0., yf=0., wf=1.;
   double xd=0., yd=0., wd=1.;

   auto h2d = new TH2D("h2d","h2d",10,-1,1.,10,-1,1.);
   auto h2f = new TH2F("h2f","h2f",10,-1,1.,10,-1,1.);

   h2d->Fill(xf,yf,wf);
   h2d->Fill(xd,yd,wd);
   h2d->Fill(xf,yf,wd);
   h2d->Fill(xd,yd,wf);

   h2f->Fill(xf,yf,wf);
   h2f->Fill(xd,yd,wd);
   h2f->Fill(xf,yf,wd);
   h2f->Fill(xd,yd,wf);
}

```

But it is working, I do not get any error message.

---

<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:** [March 16, 2022, 9:07am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/7 "2022-03-16T09:07:56Z")

</div>

The error in the first post explicitly mentiones “`TH1::Fill`”. So, it does not know that it is a “`TH2`”.  
@abunka Try: `((TH2*)hist("name2D"))->Fill(fillItemA, fillItemB, weight);`

---

<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:** [March 16, 2022, 10:06am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/8 "2022-03-16T10:06:32Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> ((TH2\*)hist(“name2D”))-\>Fill(fillItemA, fillItemB, weight)

I tried to transpose it in my reproducer but it still works.

---

<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:** [March 16, 2022, 10:17am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/9 "2022-03-16T10:17:18Z")

</div>

@couet In your code, try: `((TH1*)h2d)->Fill(xf,yf,wf);`

---

<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:** [March 16, 2022, 10:23am UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/10 "2022-03-16T10:23:48Z")

</div>

Yes that’s shows errors:

```auto
root [0] .x TH2Fill.C
In file included from input_line_12:1:
/Users/couet/roottest/TH2Fill.C:9:17: error: no matching member function for call to 'Fill'
   ((TH1*)h2d)->Fill(xf,yf,wf);
   ~~~~~~~~~~~~~^~~~
/Users/couet/git/roottrunk-bin/include/TH1.h:219:21: note: candidate function not viable: requires 2 arguments, but 3 were provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/Users/couet/git/roottrunk-bin/include/TH1.h:220:21: note: candidate function not viable: requires 2 arguments, but 3 were provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
/Users/couet/git/roottrunk-bin/include/TH1.h:218:21: note: candidate function not viable: requires single argument 'x', but 3 arguments were provided
   virtual Int_t Fill(Double_t x);
                    ^

```

```auto
#include "TH2D.h"

void TH2Fill()
{
   float xf=0., yf=0., wf=1.;

   auto h2d = new TH2D("h2d","h2d",10,-1,1.,10,-1,1.);

   ((TH1*)h2d)->Fill(xf,yf,wf);

}

```

---

<div class="post-metadata">

**Author:** ![abunka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@abunka](https://root-forum.cern.ch/u/abunka)\
**Post date:** [March 16, 2022, 6:41pm UTC](https://root-forum.cern.ch/t/weighting-2d-histograms/49181/11 "2022-03-16T18:41:43Z")

</div>

Thank you everyone for your help, adding the (TH2\*) in front of the fill command has solved the problem
