# Cloning 2 histos leads to segmentation fault

**URL:** <https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432>\
**Category:** Newbie\
**Created:** [August 6, 2024, 12:51pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432 "2024-08-06T12:51:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![archiron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@archiron](https://root-forum.cern.ch/u/archiron)\
**Post date:** [August 6, 2024, 12:51pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/1 "2024-08-06T12:51:03Z")

</div>

* * *

Hi,  
I have a list of some histos called with file.Get(path).Get(histo\_name) for a given release (for example CMSSW\_12\_5\_0\_pre5).  
I would like to compare each histo (named as h2) with the same from another release (named as h1).  
Do to this, I use a function named createPicture2(self, histo1, histo2, filename) in which I try to clone the 2 histos with :  
self.cnv = TCanvas(str(id), “canvas”)  
print(‘call to histo2 Clone’)  
histo2c = histo2.Clone()  
print(‘call to histo1 Clone’)  
histo3 = histo1.Clone()  
print(‘end Cloning’)

```
    self.cnv.Draw()
    self.cnv.Update()
    self.cnv.SaveAs(filename)
    self.cnv.Close()

```

with the 2 cases histo1.clone & histo2.clone I have a segmentation fault.  
with histo2 only, all run correctly.  
with histo1 only, I have a segmentation fault.  
Into the most of cases, the segmentation appears when h2 is updated into the main loop

adding a canvas/pad/histo.draw option show that without cloning anything, the graphs of h2 or h1 are correct.

So it seems that I cannot have 2 clones at the same time. Is it correct ?  
thanks.  
Arnaud

_ROOT Version:_ 6.24.04  
_Platform:_ Not lxplus  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 6, 2024, 1:33pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/2 "2024-08-06T13:33:17Z")

</div>

Hello @archiron, thank you for your question.  
Do you have a more complete example of what you’re trying to do? In principle you should be able to clone however many objects you want.  
Are you able to try your use case with a more recent version of ROOT to see if the problem persists?

---

<div class="post-metadata">

**Author:** ![archiron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@archiron](https://root-forum.cern.ch/u/archiron)\
**Post date:** [August 6, 2024, 2:18pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/3 "2024-08-06T14:18:02Z")

</div>

Hi, thank you for your answer.  
Unfortunatly I cannot now try a more recent version of ROOT. I’m working on a T3 at LLR and the only working version is 6.24.04.

to be more concrete, I have a python file (sorry it’s not C++) with a primary loop (see pgm at end) in which I read the ROOT file.  
in this main loop I have another secondary loop onto a list of histo names. Into this loop I get the 2 histos I need for comparison and then I send them to a function for a picture creation.  
The function is showed at the bottom. There is nothing inside than the cloning.  
Arnaud

```auto
for elem in sortedRels:
    print('=== release : {}'.format(elem))
    f_rel = ROOT.TFile(pathDATA + file) # --> read the file
    h1 = getHisto(f_rel, tp_1) # --> get the histo
    for i in range(0, len(histoArray)): # --> histoArray is a list of histo names
        histo_2 = h2.Get(short_histo_names[0])
        histo_1 = h1.Get(short_histo_names[0]) 
        gif_name = pathDBox + short_histo_names[0] + '_' + rel + ".gif"
        gr.createPicture2b(histo_1, histo_2, gif_name, 0)       
    ROOT.TFile.Close(f_rel)
print("END !\n")

```

=======

```auto
    def createPicture2b(self, histo1, histo2, filename, id):
        self.cnv = TCanvas(str(id), "canvas")

        histo2c = histo2.Clone()
        histo1c = histo1.Clone()

        self.cnv.SetCanvasSize(960, 900)
        self.cnv.Clear()
        self.cnv.SetFillColor(10)

        pad1 = ROOT.TPad(str(id), "pad1", 0, 0.25, 1.0, 1.0) # ,0,0,0
        pad1.SetBottomMargin(0.05)
        pad1.Draw()
        pad1.cd()
        
        newDrawOptions = "hist"
        histo2.SetStats(1)
        histo2.Draw(newDrawOptions)
        
        self.cnv.Draw()
        self.cnv.Update()

        self.cnv.SaveAs(filename)
        self.cnv.Close()
        
        return

```

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 6, 2024, 2:28pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/4 "2024-08-06T14:28:07Z")

</div>

Thanks for posting the code sample.  
I don’t see `h2` defined anywhere: is it coming from outside the main loop? Also, isn’t the inner loop completely invariant with respect to its iteration index? In other words, are you not using `i` on purpose?

---

<div class="post-metadata">

**Author:** ![archiron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@archiron](https://root-forum.cern.ch/u/archiron)\
**Post date:** [August 6, 2024, 2:34pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/5 "2024-08-06T14:34:58Z")

</div>

yes, (sorry I forget it), before the main loop with :

```auto
f2 = ROOT.TFile(pathDATA + input_ref_file)
h2 = getHisto(f2, tp_1)

```

tp\_1 is : tp\_1 = ‘ElectronMcSignalValidator’  
and the GetHisto function is defined as :

```auto
def getHisto(file, tp):
    path = 'DQMData/Run 1/EgammaV/Run summary/' + tp
    t_path = file.Get(path)
    return t_path

```

which gives for the path : `DQMData/Run 1/EgammaV/Run summary/ElectronMcSignalValidator`

Arnaud

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 6, 2024, 2:40pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/6 "2024-08-06T14:40:56Z")

</div>

Thanks for the clarification.  
However, I still don’t get what is your inner loop supposed to do: aren’t you simply recreating the same picture over and over, since the histogram names you’re using are always the same?  
(maybe you wanted to use `short_histo_names[i]` instead?)

---

<div class="post-metadata">

**Author:** ![archiron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@archiron](https://root-forum.cern.ch/u/archiron)\
**Post date:** [August 6, 2024, 2:47pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/7 "2024-08-06T14:47:35Z")

</div>

you’re right ! in fact the method I use is a little more complicated so I made a simplification and forgot to replace _ **0** _ with _ **i** _ !

the simplified code is :

```auto
f2 = ROOT.TFile(pathDATA + input_ref_file)
h2 = getHisto(f2, tp_1)

for elem in sortedRels:
    print('=== release : {}'.format(elem))
    f_rel = ROOT.TFile(pathDATA + file) # --> read the file
    h1 = getHisto(f_rel, tp_1) # --> get the histo
    for i in range(0, len(histoArray)): # --> histoArray is a list of histo names
        histo_2 = h2.Get(histoArray[i])
        histo_1 = h1.Get(histoArray[i]) 
        gif_name = pathDBox + histoArray[i] + '_' + rel + ".gif"
        gr.createPicture2b(histo_1, histo_2, gif_name, 0)       
    ROOT.TFile.Close(f_rel)
print("END !\n")

```

To be more precise, I work with 1 release for H2 and 18 others releases for h1. size of the histoArray is actually of 9 but it will be about 240 at the end.

---

<div class="post-metadata">

**Author:** ![archiron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@archiron](https://root-forum.cern.ch/u/archiron)\
**Post date:** [August 6, 2024, 4:29pm UTC](https://root-forum.cern.ch/t/cloning-2-histos-leads-to-segmentation-fault/60432/8 "2024-08-06T16:29:07Z")

</div>

Hi, I made a test onto a lxplus computer, porting my version on.  
With the last ROOT version (6.32) all seems OK on interactive mode.  
I remade the test with all the graphic parts and all is OK.

 ![h_recTrackNum_14_1_0_pre2](https://root-forum.cern.ch/uploads/default/original/3X/7/f/7f507cafbab1d0b966ac645dcfc3d7509cf0f98d.gif)

I think you were right with the version of ROOT into your fisrst answer ; The version I used and/or the environment I used (T3 at LLR) must have some limitations.

Many thanks to you.  
best regards

Arnaud
