Weighting 2D histograms

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:

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

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

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

It gives the following error at compilation:

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?

For TH2 the Fill() signature is with 3 doubles.

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.

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

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.

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.

I tried to make a small reproducer:

#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.

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);

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

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

Yes that’s shows errors:

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);
                    ^
#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);

}

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