# OOP pyROOT histogram plotted from file disappears on click

**URL:** <https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303>\
**Category:** ROOT\
**Tags:** python, hist, root\
**Created:** [May 11, 2024, 9:17am UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303 "2024-05-11T09:17:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [May 11, 2024, 9:17am UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/1 "2024-05-11T09:17:07Z")

</div>

I have a lot of root files and I want to plot the same plots from each of them, so I’ve made a class to try to help me to do that efficiently. So far I have some code which works really well and does exactly what I want it to do except that once the plots are drawn as soon as I click on the canvas the histogram disappears, leaving just the title and an empty pad as shown below.  
Can someone help me fix this? Thanks!

```auto
import ROOT, os
from ROOT import TCanvas, gPad, kBlue, kRed, TFile

def histo_plotter(histo):
    ROOT.SetOwnership(histo,False)
    histo.Draw("same")
    return

class myclass:
    def __init__ (self,run_no):
        self.run_no = run_no

    @staticmethod
    def canvas_creator(canv_name):
        my_canvas = TCanvas(canv_name,canv_name,900,700)
        return my_canvas

    @staticmethod
    def histo_maker(names,colour):
        h1=ROOT.TH1F(names[0],names[0],64,0,4)
        h1.FillRandom('gaus')
        h2=ROOT.TH1F(names[1],names[1],64,0,4)
        h2.FillRandom('gaus')

        h1.SetLineColor(colour)
        h2.SetLineColor(colour)

        write_file = TFile("~/Downloads/file.root","RECREATE")
        h1.Write()
        h2.Write()
        write_file.Close()
        
        histo_list = [h1,h2]
        return histo_list
    
def main():
    instances = [myclass(x) for x in range(2)]
    colours = [kBlue, kRed]
    names = [["h1", "h2"], ["h3","h4"]]
       
    histos = [instance.histo_maker(names[i_inst], colours[i_inst]) for i_inst, instance in enumerate(instances)]
    canvas_x_signals = [instance.canvas_creator(f"canv_{no}") for no, instance in enumerate(instances)]

    file_path=os.path.join("/Users/bethlong/Downloads/", "file.root")
    root_file = ROOT.TFile(file_path,"read")
    root_file.ls()
    test_histo = root_file.Get("h3")

    test_canv = TCanvas("test_canv","test_canv",900,700)
    ROOT.SetOwnership(test_canv, False)
    test_histo.Draw()
    gPad.Update()
    
if __name__ == ' __main__':
    main()

```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/6/a6872ca8ea1cb50a41cd00465013dbc51ae495c3.jpeg)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [May 11, 2024, 9:24pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/2 "2024-05-11T21:24:48Z")

</div>

Hi,

From the code it seems you are writing the histograms to a file, which is then closed. The ownership of the histograms is of the file: this is why the canvas is empty.  
Can you refactor perhaps your code to avoid this?

Best,  
D

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [May 12, 2024, 1:05pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/3 "2024-05-12T13:05:04Z")

</div>

Hi, thanks for the answer. Actually, I observed the problem first in a program which just reads the histograms and plots them, it doesn’t create them. In fact if you comment the line

```auto
histos = [instance.histo_maker(names[i_inst], colours[i_inst]) for i_inst, instance in enumerate(instances)]

```

then the problem persists. I just included that to make the whole example self-contained, instead of assuming that someone would create their own file to test with!  
Also, when the canvas is created the histogram is shown, it only disappears when you click on it, and the fact that the title remains makes me think that maybe it isn’t a problem of the ownership of the histogram?

Any help would be very welcome!

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [May 13, 2024, 4:56pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/4 "2024-05-13T16:56:01Z")

</div>

Bumping this, is anyone else able to reproduce the problem?

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [May 22, 2024, 1:40pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/5 "2024-05-22T13:40:42Z")

</div>

Hi, I could really use a solution to this problem!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [May 24, 2024, 12:09pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/6 "2024-05-24T12:09:30Z")

</div>

Hi,

Have you tried to avoid closing the files to which the histograms are attached as written before? Maybe it could be helpful if you reduce your example to a minimal form that shows the problem?

Best,  
D

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [June 6, 2024, 1:34pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/7 "2024-06-06T13:34:30Z")

</div>

Hi Danilo,

sorry for being slow to reply and sorry if I wasn’t clear before, as I say, I only included the creation of the histo file to make the minimum working example completely self contained. If you already have some histos to plot and you plot them using the rest of the code then I hope you should be able to reproduce the problem:

```auto
import ROOT, os
from ROOT import TCanvas, gPad, TFile

def histo_plotter(histo):
    ROOT.SetOwnership(histo,False)
    histo.Draw("same")
    return

class myclass:
    def __init__ (self,run_no):
        self.run_no = run_no
        self.file_path=os.path.join("/Users/bethlong/Downloads/", "file.root")
        self.root_file = ROOT.TFile(self.file_path,"read")

    @staticmethod
    def canvas_creator(canv_name):
        my_canvas = TCanvas(canv_name,canv_name,900,700)
        return my_canvas

    def histo_getter(self):
        h1 = self.root_file.Get("h3")
        h2 = self.root_file.Get("h4")
        return h1,h2
    
def main():
    instances = [myclass(x) for x in range(2)]       
    canvas_x_signals = [instance.canvas_creator(f"canv_{no}") for no, instance in enumerate(instances)]

    file_path=os.path.join("/Users/bethlong/Downloads/", "file.root")
    root_file = ROOT.TFile(file_path,"read")
    test_histo = root_file.Get("h3")

    test_canv = TCanvas("test_canv","test_canv",900,700)
    ROOT.SetOwnership(test_canv, False)
    test_histo.Draw()
    gPad.Update()
    
if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [June 10, 2024, 12:27pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/8 "2024-06-10T12:27:12Z")

</div>

Update: I’ve reproduced the problem on an M1 Mac, still python3.12, still macOS Sonoma

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [June 14, 2024, 3:39pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/9 "2024-06-14T15:39:07Z")

</div>

Am I really the only person to use pyroot for OOP? Is it just not recommended? It’s a real shame that I can’t interact with the canvases since as soon as I click on one the plot disappears

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2024, 6:36am UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/10 "2024-06-20T06:36:23Z")

</div>

Hi,

As said before, the histogram is going out of scope, as everything else, given that the main function returns. To keep it drawn, this should be avoided. One way to achieve this could be to boot the interpreter with `python -i` and `test_histo.DrawClone()`.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [June 21, 2024, 12:45pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/11 "2024-06-21T12:45:36Z")

</div>

Hi, thanks for the reply. I already ran the code with `python -i`, I tried `DrawClone()` and the problem persisted but `DrawCopy()` seems to work! Thanks for the inspiration.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 5, 2024, 12:45pm UTC](https://root-forum.cern.ch/t/oop-pyroot-histogram-plotted-from-file-disappears-on-click/59303/12 "2024-07-05T12:45:55Z")

</div>

Closed on July 5, 2024, 12:45pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
