Get an histogram from a canvas

Hello

I have the following problem: I wrote a canvas in a file and now I’d like to get back the histogram from the canvas and do some stuff…
This is the 1st script:

TFile miofile(“Hfra40.root”,“read”);
miofile->ls();
TCanvas c1 = (TCanvas)miofile.Get(“c1”);
c1->Draw();

the c1 canvas is drawn and this is the output:
TFile** Hfra40.root
TFile* Hfra40.root
KEY: TCanvas c1;1 c1

For now OK!

This is the 2nd script:

TFile miofile(“Hfra40.root”,“read”);
TH1F hFra40;
TCanvas c1 = (TCanvas)miofile.Get(“c1”);
hFra40 = (TH1F
)c1->GetPrimitive(“hFracEtcorr2Part”);
hFra40->Draw();

No error but no drawing -> not good (the name of the Histo is right!)

3th script:

TFile miofile(“Hfra40.root”,“read”);
TCanvas c1 = (TCanvas)miofile.Get(“c1”);
TObject *obj;
TIter next(c1->GetListOfPrimitives());
while ((obj=next())) {
cout << "Reading: " << obj->GetName() << endl;
if (obj->InheritsFrom(“TH1”)) {
cout << "histo: " << obj->GetName() << endl;
}
}

the ouput of the script is:

Reading: TFrame
Reading: hFracEtcorr2Part
histo: hFracEtcorr2Part
Reading: title

No error -> everything seems OK!
but if I add the line
obj->Draw();
whitin the if statement… no drawing…

Last script:

TFile miofile(“Hfra40.root”,“read”);
TCanvas c1 = (TCanvas)miofile.Get(“c1”);
TObject *obj;
TH1F hFra40;
TIter next(c1->GetListOfPrimitives());
while ((obj=next())) {
cout << "Reading: " << obj->GetName() << endl;
if (obj->InheritsFrom(“TH1”)) {
cout << "histo: " << obj->GetName() << endl;
hFra40 = (TH1F
)obj->Clone();
}
}
hFra40->Draw();

again no error… but if I open a Tbroswser and get to the file (maybe this is the problem?) I see now the
histogram with name hFracEtcorr2Part: try to double click on it to draw it and get this bunch of error:
*** Break *** segmentation violation
Generating stack trace…
0x40fb72f4 in TPad::PaintBox(double, double, double, double, char const*) + 0x37e from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGpad.so
0x40fb637f in TPad::PaintBorder(short, bool) + 0xa1 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGpad.so
0x40fb6ba0 in TPad::PaintModified() + 0xd0 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGpad.so
0x40f9be04 in TCanvas::Update() + 0x78 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGpad.so
0x408152e3 in TH1::Browse(TBrowser*) + 0x3b from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libHist.so
0x412acf37 in TRootBrowser::IconBoxAction(TObject*) + 0x343 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x412ac497 in TRootBrowser::ProcessMessage(long, long, long) + 0xe39 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x4124f4b6 in TGFrame::HandleClientMessage(Event_t*) + 0x4a from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x41251a58 in TGMainFrame::HandleClientMessage(Event_t*) + 0x2c from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x4124f1ea in TGFrame::HandleEvent(Event_t*) + 0x1d0 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x41232f7b in TGClient::HandleEvent(Event_t*) + 0xd7 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x41232c74 in TGClient::ProcessOneEvent() + 0x6e from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x41232cfe in TGClient::HandleInput() + 0x28 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x4123187a in TGInputHandler::Notify() + 0x22 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libGui.so
0x401c643e in TUnixSystem::DispatchOneEvent(bool) + 0x3a from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libCore.so
0x401313b3 in TSystem::InnerLoop() + 0x25 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libCore.so
0x40131341 in TSystem::Run() + 0x7b from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libCore.so
0x400e19f6 in TApplication::Run(bool) + 0x30 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libCore.so
0x40e12bdb in TRint::Run(bool) + 0x311 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/lib/libRint.so
0x08048890 in main + 0x90 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/bin/root.exe
0x42017589 in __libc_start_main + 0x95 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/bin/root.exe
0x08048711 in _Unwind_Resume + 0x35 from /cms/external/lcg/external/root/3.10.01/rh73_gcc32/bin/root.exe

Any suggestion on how to do?

Thanks

Attilio

I already replied to the same message to roottalk and the Forum (double work for us). My answer was:
Attilio,

In your 2nd script, do;
{
TFile miofile(“Hfra40.root”,“read”);
TH1F hFra40;
TCanvas c1 = (TCanvas)miofile.Get(“c1”);
hFra40 = (TH1F
)c1->GetPrimitive(“hFracEtcorr2Part”);
c1->Clear();
c1->Draw();
hFra40->Draw();
}
or
{
TFile miofile(“Hfra40.root”,“read”);
TH1F hFra40;
TCanvas c1 = (TCanvas)miofile.Get(“c1”);
hFra40 = (TH1F
)c1->GetPrimitive(“hFracEtcorr2Part”);
TCanvas c2;
hFra40->Draw();
}

In your original script, the current pad/canvas is c1.
So you draw the histogram in this canvas.

Rene Brun

Sorry for the double post… I thought that the old forum wasn’t working anymore…
any way thank you! Now it’s ok.

Cheers

Attilio

I use these commands inside of a loop, and get a memory leak (from the TCanvas, as the leak goes away when I remove it), I have tried to explicitly delete the TCanvas with:

delete c1;

or

c1->Delete();

but it doesn’t seem to help. Is there a way around this?

Send a concrete running example that we can execute.

Rene

Hello,
I have a .root file with several canvases and in each of them a histogram but I can’t extract them. My code is as follows:

TFile *file = new TFile(“Root-file_new.root”)
name_new = file->GetListOfKeys()->At(0)->GetName()
TCanvas canvas = (TCanvas)file->Get(name_new)

Until here it works fine, I have acces to the canvas. I searched other answers in the forum and tried this line:

TH1D hist = (TH1D)canvas->GetPrimitive(“histogram-name”)

But it returns:

(TH1D *) nullptr

Besides, to find the histogram’s name I had to open the canvas in the TBrowser and save it as canvas.C and search the name by reading the file myself. Is there a function that will alloud me to get the histogram’s name? Thanks

Jose

Try: canvas->ls();