Adding TLegend to a root file with GetListOfSpecials()

Hello!

I am fitting some plots and saving them to a root file with
TFile *fitDet;
fitDet = new TFile (“FitRate93.root”,“RECREATE”);
gROOT->GetListOfSpecials()->Add(result);
fitDet->WriteTObject(result);

But I also have a legend that shows the chi squared etc. In the macro that makes this root file, I declare this TLegend as
TLegend *leg71 = new TLegend(0.1,0.85,0.9,0.9);

So I want to save this TLegend also in the root file. For this I do :
gROOT->GetListOfSpecials()->Add(leg71);
fitDet->WriteTObject(leg71);

Looks like they both, the fitted histo and the legend, wrote to the root file FitRate93.root, but now how do I get the legend to draw?

If I open file FitRate93.root and double click graph with “ap” options it draws, now if I double click TPave with “same” option it doesn’t draw. Why is this?

Also it would be nice to give my legend a name. How do I give a TLegend a name so it would not appear as TPave in the root file and instead have a name?

Thank you!

  • Sujeewa

It is hard to imagine such a complex way of thinking how something works.
To write tour leg71 object to the file, simply do

leg71->Write();
In a next session, after having open the file, do

TLegend *l = (TLegend*)file->Get("legend name"); l->Draw();
Rene

Thanks Rene!

The problem was I wasn’t SetName-ing my TLegend, hence I couldn’t call it.

Regarding to the complexity issue you raised:

The fits take forever, so I don’t want to do them each time. But saving just the graphs isn’t good enough, as I also want the fit parameters etc. Thus the TLegend.

But if your reference of complexity is to GetListOfSpecials(), WriteTObject() methods, this is actually something I saw from one of the replies you and someone else had posted on how to write a TGraph to a root file thread in ROOT support.

Anyway, Thanks again!

  • Sujeewa