# SetRangeUser and SetTitle working with THStacks and TH1F obj

**URL:** <https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114>\
**Category:** ROOT\
**Created:** [March 22, 2011, 5:00pm UTC](https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114 "2011-03-22T17:00:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernando\_Quinonez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@Fernando\_Quinonez](https://root-forum.cern.ch/u/Fernando_Quinonez)\
**Post date:** [March 22, 2011, 5:00pm UTC](https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114/1 "2011-03-22T17:00:12Z")

</div>

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:

```auto
{
  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.

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [March 22, 2011, 7:24pm UTC](https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114/2 "2011-03-22T19:24:20Z")

</div>

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

```cpp
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"

```

```cpp
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");

```

---

<div class="post-metadata">

**Author:** ![Fernando\_Quinonez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@Fernando\_Quinonez](https://root-forum.cern.ch/u/Fernando_Quinonez)\
**Post date:** [March 22, 2011, 8:17pm UTC](https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114/3 "2011-03-22T20:17:05Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [March 22, 2011, 9:19pm UTC](https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114/4 "2011-03-22T21:19:11Z")

</div>

```auto
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");
}
```
