Save THStack in root file
Feature description
I would like to be able to save an identical object to what’s produced interactively when making a stack plot, ie with this MWE:
void TRatioPlotStack()
{
gStyle->SetHistLineColor(kBlack);
gStyle->SetMarkerStyle(8);
gStyle->SetMarkerSize(0.7);
TGaxis::SetMaxDigits(3);
gROOT->ForceStyle();
gStyle->SetOptStat(0);
auto C = new TCanvas("C", "A ratio example");
auto h1 = new TH1D("h1", "TRatioPlot Example; x; y", 50, 0, 10);
auto h2 = new TH1D("h2", "h2", 50, 0, 10);
auto h3 = new TH1D("h3", "h3", 50, 0, 10);
auto f1 = new TF1("f1", "exp(- x/[0] )");
f1->SetParameter(0, 3);
for(int ii=0; ii<h1->GetXaxis()->GetNbins(); ii++) h1->SetBinContent(ii+1,150);
h2->FillRandom("f1", 2000);
h3->FillRandom("f1", 2000);
h1->Sumw2();
h1->SetOption("E");
h2->Scale(1. / 2.);
h2->SetFillColor(kRed);
h3->SetFillColor(kBlue);
THStack* st = new THStack("st","");
st->Add(h2);
st->Add(h3);
auto rp = new TRatioPlot(h1, st);
C->SetTicks(0, 1);
rp->Draw();
rp->GetLowYaxis()->SetNdivisions(505);
TFile* fFile = TFile::Open("~/Desktop/test.root","recreate");
// fFile->cd();
rp->Write();
fFile->Close();
}
to save this:
Alternatives considered
No response
Additional context
No response
As workaround, you could call C->Write() which saves everything on the canvas.
Then, double clicking would work. To retrieve the objects, just:
C->ls();
C->FindObject("TRatioPlot")
Yes, store of TCanvas is the only working way to store TRatioPlot object
I think what could lead to confusion is the fact that TRatioPlot, as the name suggests, is a helper to draw ratios, and not an object thought to be persistified.
Indeed, it does not really make sense to save a TRatioPlot, as it is not a self-contained object like a TH1 or a TGraph. TRatioPlot is a class designed to lay out pads, and you can clearly see this if you inspect the pad contents (via gPad->ls()): several pads and axes are generated.
In addition, TRatioPlot is not a named class and therefore cannot be accessed via ratioplot->Draw() from the command line. As mentioned before, you should save the entire canvas or use another method to save it (PDF, SVG, PNG, etc.).
We will improve the TRatioPlot doc.