Skip to content

Save THStack in root file #20476

@BethLong06

Description

@BethLong06

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:

Image Currently I get this: Image Where even clicking on h1/h2 doesn’t show anything

Alternatives considered

No response

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions