Problem in put TLegend in Histogram TDF TResultProxy<TH1D>

Hi,

I’m using ROOT 6.13/03 Built for linuxx8664gcc heads/master@v6-13-02-109-g95bf30d, Mar 26 2018, 19:59:29

Let me explain:

I’m trying to draw the legends in the histogram using: auto legend
TLegend Class Reference

This example below work very well:

void Legend(){
    
    auto c1 = new TCanvas("c1","c1",600,500);
    gStyle->SetOptStat(0);
    auto h1 = new TH1F("h1","TLegend Example",200,-10,10);
    h1->FillRandom("gaus",30000);
    h1->SetFillColor(kGreen);
    h1->SetFillStyle(3003);
    h1->Draw();


    auto legend = new TLegend(0.1,0.7,0.48,0.9);
    legend->AddEntry(h1,"Histogram filled with random numbers","lep");
    legend->Draw();
   
}

histogram output with Legend:

In my code I just put the function auto legend

My code:

#include <ROOT/TDataFrame.hxx>
#include <TCanvas.h>
#include <TApplication.h>

using namespace std;
using namespace ROOT::Experimental;
using namespace ROOT::Experimental::VecOps;


    void analysis(){
        
        
        TDataFrame d("Particle","crmc_eposlhc_112108849_p_C_130000.root"); 
   
        // From EPOS LHC:
        auto hist1 = d.Histo1D({"hist1", "Multiplicity LAB", 100, -15, 15}, "px");
    
        // Drawing
        auto c1 = new TCanvas("c1", "c1", 10, 10, 700, 500);
        c1->SetGrid(1,1);
        c1->SetLogx(0); // 0 == scale without Log, 1 == scale with Log.
        c1->SetLogy(1);
        
        hist1->GetYaxis()->SetTitle("dN/d#eta");
        hist1->GetXaxis()->SetTitle("#eta");
        hist1->DrawClone();
        
        // Draw the Legend 
        auto legend = new TLegend(0.1,0.7,0.48,0.9);
        legend->AddEntry(hist1,"EPOS LHC","lep");
        legend->Draw();
        
        
    }
    
    int main(){
        TApplication app("app", nullptr, nullptr);
        analysis();
        app.Run();
    return 0;
    }

file: crmc_eposlhc_112108849_p_C_130000.root (304.2 KB)

Run:
root -l analysis.C

I get the following error:

/home/andre/Dropbox/Doutorado/CBJ/Analise Eventos C-jatos/analysis.C:38:17: 
error: no matching member function for call to 'AddEntry'
        legend->AddEntry(hist1,"EPOS LHC","lep");
        ~~~~~~~~^~~~~~~~
/opt/root6/include/TLegend.h:36:20: note: candidate function not viable: no known conversion from 
'ROOT::Experimental::TDF::TResultProxy<TH1D>' to 'const TObject *' for 1st argument
   TLegendEntry   *AddEntry(const TObject* obj, const char* label = "", Option_t* option = "lpf" );
                   ^
/opt/root6/include/TLegend.h:37:20: note: candidate function not viable: no known conversion from 
'ROOT::Experimental::TDF::TResultProxy<TH1D>' to 'const char *' for 1st argument
   TLegendEntry   *AddEntry(const char *name, const char* label = "", Option_t* option = "lpf" );

Someone know what I have to do to fix this?
Thanks,

Cheers, Andre

I got it, it’s working now!

Before:

legend->AddEntry(hist1,"EPOS LHC","lep");

After:

legend->AddEntry("hist1","EPOS LHC","lep");

only to be registered, maybe someone will fall into this mistake someday.

Cheers, Andre.

The object you put the legend can be added by name or as a pointer. It did not work as pointer because hist1 was not a pointer in your non working example. Also it might be good to add the TLegend.h include file as you are using the TLegend object,

Hi, Olivier

Thanks for the answer! I will take note of your suggestion.

Cheers, Andre

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