TLegend.Draw() cint / g++

using cint, if I do:

TCanvas *c1 = new TCanvas("c1","BKGCAT mio segnale",1200,600);
    c1->Divide(3,1);
    c1->cd(1);
    histoDSignalDsM_BKGCAT.SetLineColor(2);
    histoDsM_BKGCAT.DrawCopy();
    histoDSignalDsM_BKGCAT.DrawCopy("same");
    c1->cd(2);
    histoDSignalDsP_BKGCAT.SetLineColor(2);
    histoDsP_BKGCAT.DrawCopy();
    histoDSignalDsP_BKGCAT.DrawCopy("same");
    c1->cd(3);
    histoDSignalBs_BKGCAT.SetLineColor(2);
    histoBs_BKGCAT.DrawCopy();
    histoDSignalBs_BKGCAT.DrawCopy("same");
    TLegend *legenda1 = new TLegend(.75,.80,.95,.95);
    legenda1->AddEntry(&histoDSignalDsM_BKGCAT,"Signal D");
    legenda1->Draw();

the legend is drawn. But If I inizialize not dynamically legenda1

   TLegend legenda1(.75,.80,.95,.95);
    legenda1.AddEntry(&histoDSignalDsM_BKGCAT,"Signal D");
    legenda1.Draw();

the legend is not drawn. But if I compile the code, with g++ the legend is drawn!

This looks like a basic scoping problem. Could you send the shortest possible script that does not run with CINT, but runs correctly when compiled?

Rene

This works with g++, but not with CINT (the legend is not drawn).

#include <TROOT.h>
#include <TApplication.h>
#include <TCanvas.h>
#include <TPad.h>
#include <TStyle.h>
#include <TH1F.h>
#include <TLegend.h>


#ifdef __CINT__
int foo()
#else
int main(int argc, char **argv)
#endif
{
#ifdef __CINT__
#else
    TApplication app("app", &argc, argv);
#endif
    TH1F h("h","histogram",10,0,10);
    h.Fill(4); h.Fill(3); h.Fill(3);
    TCanvas *c1 = new TCanvas("c1","my canvas");
    h.DrawCopy();
    TLegend legend(.75,.80,.95,.95);
    legend.AddEntry(&h,"Signal");
    legend.Draw();
#ifdef __CINT__
#else
    app.Run();
#endif
    
    return 0;
}

I’m using this Makefile

CC	 = g++

MAIN 	= foo
MAINS	= $(MAIN).cpp
MAINO	= $(MAIN).o

CFLAGS = -Wall -O2 -g 
ROOTCFLAGS   := $(shell root-config --cflags)
ROOTLIBS     := $(shell root-config --libs)
ROOTGLIBS    := $(shell root-config --glibs)


$(MAIN)	: $(MAINS)
	@ echo "Compiling $@"
	$(CC) $(CFLAGS) $(ROOTCFLAGS) $(ROOTLIBS) $(ROOTGLIBS) $(MAINS) -o $(MAIN)

Hi,

in the code visible to CINT, legend goes out of scope at the end of the function and is thus destructed. Create it on the heap and delete it in the non-CINT case.

Cheers, Axel.

Maybe you can implement a DrawCopy method. I think it’s wrong initialiaze all object on heap only because with script when the code terminate all the variables are destroyed.

No need for a DrawCopy. Simply follow Axel’s advice, creating your object with new such that the TLegend object will not be destroyed when leaving the scope of your function.

Rene