# What is the correct way of THStack.GetXaxis().SetRangeUser() in for loop?

**URL:** <https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218>\
**Category:** Newbie\
**Tags:** python\
**Created:** [March 17, 2022, 6:38am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218 "2022-03-17T06:38:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 17, 2022, 6:38am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/1 "2022-03-17T06:38:35Z")

</div>

I was doing THstack plots in a `for` loop, where the histograms `h1,h2,h3` are drawn from RDataFrame. After the loop, I want to do some fine tuning on some plots.  
This is the working part of my code:

```auto
c1 = r.TCanvas("c1","c1 title",1500,1200)
c1.Divide(3,3)

h1 = df.Histo1D("var1")
h2 = df.Histo1D("var2")
h3 = df.Histo1D("var3")

hs1_list = [] # save objects in the for loop, to ease restyle later
for i in range(9):
    c1.cd(i+1)
    varname = varnames[i]
    hs = r.THStack("hs1{}".format(i+1),par_list[i][0])
    hs1_list.append(hs)
    hs.Add(h1.GetPtr())
    hs.Add(h2.GetPtr())
    hs.Add(h3.GetPtr())
    hs.DrawClone("nostack HIST E")

# fine tune some plots
hs1_list[0].GetXaxis().SetRangeUser(0,3100)

c1.Draw()

```

The `hs1_list[0].GetXaxis().SetRangeUser(0,3100)` line throw an error:  
`ReferenceError: attempt to access a null-pointer`  
sorry for not being able to include the full code.  
Could anyone help me on that ?

* * *

\_ROOT Version: 6.24 (PyROOT via conda)  
\_Platform:Centos7  
\_Compiler: gcc9

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2022, 6:48am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/2 "2022-03-17T06:48:40Z")

</div>

`hs1_list[0]` seems not initialised. I suppose `hs1_list.append(hs)` should have done it ? it does not seem to be the case.  
May be @etejedor can help.

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 17, 2022, 6:55am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/4 "2022-03-17T06:55:16Z")

</div>

I wrote in Python, so the `hs1_list` should be initialized automatically. I use `hs1_list` to store 9 (pointers? i think) of the THStack generated in the `for` loop. I tested to change the Xaxis range only for the 1st THStack (`hs1_list[0]`) before drawing the whole canvas.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2022, 6:59am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/5 "2022-03-17T06:59:51Z")

</div>

Try to add a `gPad->Update()` (that’s the C++ syntax, make it in Python) before the the `GetAxis()` line.

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 17, 2022, 7:07am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/6 "2022-03-17T07:07:00Z")

</div>

Sorry, it didnt work, the same error still being thrown.

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 17, 2022, 7:24am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/7 "2022-03-17T07:24:24Z")

</div>

I tried use `Draw()` in stead of `DrawClone()` in the `for `loop, and it worked.  
But in the previous similar plots, `DrawClone()` must be used otherwise it only plot the last plot in the ```for`` loop. Surprisingly, it worked in this case.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2022, 7:36am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-of-thstack-getxaxis-setrangeuser-in-for-loop/49218/8 "2022-03-17T07:36:49Z")

</div>

Yes Draw is better. DrawClone draws a copy of the stack so the original stack is not drawn.
