SetRangeUser and SetTitle working with THStacks and TH1F obj

Hello dear experts,

I was trying to set the range on a Canvas that has a THStack object and one or more TH1F objects outside the stack, also I was trying to set a title on the y-axis of it, but resulting on the pad without the titlte and the range does not coincide withe the requested.

This is an example:

{
  THStack* hs = new THStack("hs","hs");
  TH1F* h1 = new TH1F("h1","h1",50,-5,5);
  TH1F* h2 = new TH1F("h2","h2",50,-5,5);
  TH1F* h3 = new TH1F("h3","h3",50,-5,5);

  h1->FillRandom("gaus",13000);
  h2->FillRandom("gaus",20000);
  h3->FillRandom("gaus",35000);

  TH1F hsum = *h1 + *h2 + *h3;
  hsum.SetMarkerColor(kOrange);
  hsum.SetMarkerSize(1.5);
  hsum.SetMarkerStyle(20);

  h1->SetFillColor(kRed);
  h2->SetFillColor(kAzure);
  h3->SetFillColor(kYellow);

  TAxis* a = hsum->GetYaxis();
  a->SetTitle("Colombia?");
  a->SetRangeUser(0,5.e5);

  hs->Add(h1);
  hs->Add(h2);
  hs->Add(h3);


  hs->Draw();
  hsum->Draw("EPSAMES");

}

Well the root version is 5.28.00 for linuxx8664gcc
the gcc is
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50)

Any help is ubuntu.

I once had similar problems. Here’s what I’ve found to be working (don’t ask me why, no idea).

THStack *hs = (THStack *)0;
hs = new THStack(*hs_ORIG); // do not modify the "hs_ORIG" THStack, create a copy
hs->Draw("H NOSTACK");
hs->GetHistogram()->SetXTitle("My X Title");
hs->GetHistogram()->SetYTitle("My Y Title");
hs->GetXaxis()->SetTitleOffset(1.0);
hs->GetYaxis()->SetTitleOffset(1.5);
hs->GetYaxis()->CenterTitle(kTRUE);
hs->GetXaxis()->SetLimits(0, 10.001); // "xmin", "xmax"
hs->SetMinimum(1.95e8); // "ymin"
hs->SetMaximum(2.02e12); // "ymax"
TH1 *h = (TH1 *)0;
h = ((TH1 *)(hs_ORIG->GetStack()->Last()))->DrawCopy("H"); // copy of the THStack "sum"
h->SetTitle("My Title");
h->SetLineColor(2);
h->SetAxisRange(0, 9.999, "X"); // "xmin", "xmax"
h->SetAxisRange(1, 1600e9, "Y"); // "ymin", "ymax"
h->Draw("H");

Thank yoy pepe le pew!!
you did inspire me to do this:

void stacktest()
{
  gStyle->SetHistTopMargin(0);
  TCanvas c("c","c");
  THStack* hs = new THStack("hs","hs");
  TH1F* h1 = new TH1F("h1","h1",50,-5,5);
  TH1F* h2 = new TH1F("h2","h2",50,-5,5);
  TH1F* h3 = new TH1F("h3","h3",50,-5,5);

  h1->FillRandom("gaus",13000);
  h2->FillRandom("gaus",20000);
  h3->FillRandom("gaus",35000);

  TH1F hsum = *h1 + *h2 + *h3;
  hsum.SetMarkerColor(kOrange);
  hsum.SetMarkerSize(1.5);
  hsum.SetMarkerStyle(20);

  h1->SetFillColor(kRed);
  h2->SetFillColor(kAzure);
  h3->SetFillColor(kYellow);

  hs->SetMinimum(1.e-2);
  hs->SetMaximum(1.e4);

  hs->Add(h1);
  hs->Add(h2);
  hs->Add(h3);

  h1->GetYaxis()->SetTitleSize(0.05);
  h1->GetYaxis()->SetTitleOffset(1.85);
  h1->GetYaxis()->SetLabelSize(0.045);
  h1->GetYaxis()->SetTitle("Colombia");
  hs->SetHistogram(h1);

  gPad->SetLeftMargin(0.35);

  hs->Draw();
  hsum.Draw("EPSAMES");

  //c.Update();
  c.Draw();
  c.SaveAs("image.gif");

}

Now I can go on.

void stacktest()
{
  gStyle->SetHistTopMargin(0);
  TCanvas c("c","c");
  gPad->SetLeftMargin(0.35);
  
  TH1F* h1 = new TH1F("h1","h1",50,-5,5);
  TH1F* h2 = new TH1F("h2","h2",50,-5,5);
  TH1F* h3 = new TH1F("h3","h3",50,-5,5);
  
  h1->FillRandom("gaus",13000);
  h2->FillRandom("gaus",20000);
  h3->FillRandom("gaus",35000);
  
  h1->SetFillColor(kRed);
  h2->SetFillColor(kAzure);
  h3->SetFillColor(kYellow);
  
  THStack* hs = new THStack("hs","hs");
  
#if 1 /* 0 or 1 */
  hs->Add(h1);
  hs->Add(h2);
  hs->Add(h3);
  hs->Draw("");
#else /* 0 or 1 */
  hs->Add(h3);
  hs->Add(h2);
  hs->Add(h1);
  hs->Draw("NOSTACK");
#endif /* 0 or 1 */
  
  hs->GetYaxis()->SetTitleSize(0.05);
  hs->GetYaxis()->SetTitleOffset(1.85);
  hs->GetYaxis()->SetLabelSize(0.045);
  hs->GetYaxis()->SetTitle("Colombia");
  
  hs->SetMinimum(1.e-2);
  hs->SetMaximum(1.e4);
  
#if 1 /* 0 or 1 */
  TH1 *h = (TH1 *)(hs->GetStack()->Last()); // the "sum" of all histograms
#else /* 0 or 1 */
  TH1 *h = ((TH1 *)(hs->GetStack()->Last()))->DrawCopy("E P SAMES"); // copy of "sum"
  h->SetFillColor(0);
#endif /* 0 or 1 */
  
  h->SetMarkerColor(kOrange);
  h->SetMarkerSize(1.5);
  h->SetMarkerStyle(20);
  h->Draw("E P SAMES");
  
  c.Draw();
  c.SaveAs("image.gif");
}