Error THStack multi histograms in one TCanvas with TDataFrame

ROOT 6.13/03 | Built for linuxx8664gcc heads/master@v6-13-02-120-g3c7fa4a, Mar 27 2018, 09:56:12

Hi, I’m trying to use the THStack Class Reference to created one
TCanvas with multi histograms. I’m using the TDataFrame.

In my code:

auto hs = new THStack("hs","Histograms");

auto hist1 = TDF.Histo1D({"hist1", "hist1", 100, -20, 20},"Eta");
hs->Add(hist1);
auto hist2 = TDF.Histo1D({"hist2", "hist2", 100, -20, 20},"Eta");
hs->Add(hist2);
auto hist3 = TDF.Histo1D({"hist3", "hist3", 100, -20, 20},"Eta");
hs->Add(hist3);
auto hist4 = TDF.Histo1D({"hist4", "hist4", 100, -20, 20},"Eta");
hs->Add(hist4);

auto c1 = new TCanvas("c1", "c1", 10, 10, 700, 700);
c1->Divide(2,2);

// The histograms:
c1->cd(1);hs->Draw();
c1->cd(1)->SetLogy(1);

c1->cd(2);hs->Draw("nostack");
c1->cd(2)->SetLogy(1);

c1->cd(3);hs->Draw("nostack");
c1->cd(3)->SetLogy(1);

c1->cd(4);hs->Draw("nostack");
c1->cd(4)->SetLogy(1);

Error:

no viable conversion from 'ROOT::Experimental::TDF::TResultProxy<TH1D>' to 'TH1 *'
   hs->Add(hist1);
...

The same error for hist2, hist3 and hist4.

and

/opt/root6/include/THStack.h:55:30: note: passing argument to parameter 'h' here
   virtual void     Add(TH1 *h, Option_t *option="");

I think that TDataFrame can’t call the class THStack or I’m put the
syntax wrong.

Someone have some tips or some place where I can find
more explanation, I’ve been trying to fix this for a while.

Cheers, Andre

Hi Andre,

the error says it all. The object returned by the Histo methods is a TResultProxy and not a TH1D. You should change your code to:

hs->Add(&hist1.GetValue());

Cheers,
D

Hi, Danilo,

Thanks for the reply. I had some progress, but the error message change:

error: cannot initialize a parameter of type 'TH1 *' with an rvalue of type 'const TH1D *'
        hs->Add(&hist1.GetValue());

/opt/root6/include/THStack.h:55:30: note: passing argument to parameter 'h' here
   virtual void     Add(TH1 *h, Option_t *option="");

Cheers, Andre

Hi Andre,

could you save the histogram pointer separately (in a TH1D pointer) and then add it to the stack?

Cheers,
D

Sorry, I understand a little, but I don’t know How I can
to do this?

What I understand so far:
hs->Add(&hist1.GetValue()); pass the dimension of the
histogram to stack,
right?

In the documentation THStack.cxx: Add

void THStack::Add(TH1 *h1, Option_t *option)
{
 if (!h1) return;
 if (h1->GetDimension() > 2) {
    Error("Add","THStack supports only 1-d and 2-d histograms");
    return;
 }
 if (!fHists) fHists = new TList();
 fHists->Add(h1,option);
 Modified(); //invalidate stack
}

Cheers, Andre

Hi Andre,

just thinking about saving in a temporary variable. For example:

auto hist1_ptr = (TH1D*)&hist1.GetValue();
hs->Add(hist1_ptr)

Cheers,
D

Hi Danilo,

Thanks for the support! It Worked.

Cheers, Andre.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.