# Segment Violation Error in PyROOT

**URL:** <https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107>\
**Category:** Newbie\
**Created:** [August 8, 2022, 2:25pm UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107 "2022-08-08T14:25:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 8, 2022, 2:25pm UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107/1 "2022-08-08T14:25:55Z")

</div>

I am trying to run the code below in PyROOT but it keeps on giving an error. I tried the same style code in ROOT and it worked fine.

My code:

```auto
 3 import ROOT
  4 import sys
  5 import array as arr
  6 
  7 file = ROOT.TFile("js2rt.root", "READ")
  8 tree = file.Get("0")
  9 
 10 chn0 = arr.array('i')
 11 tree.SetBranchAddress("chn0", chn0)
 13 
 14 hist = ROOT.TH2D("Chn_adc_hist", "Time ticks vs ADC; ADC; Time ticks", 1000, 1100, 1200 ,1000, 0, 750 )
 15         
 16 entries = tree.GetEntries()
 17         
 18 for i in range(entries):
 19 
 20 tree.GetEntry(i)
 21 ticks = i #timeticks
 22 hist.Fill(chn0, ticks)
 23         
 24 hist.Draw("COLZ2")

```

Error message:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/0/1/0189418d10fd66c3e7e9a13bda7a86cc06ebc27f.png)

Please help me fix it. Thank you.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 9, 2022, 7:02am UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107/2 "2022-08-09T07:02:00Z")

</div>

Hi,  
Have you trying reading the TTree as following:

```auto
ticks = 0
for row in tree : 
   hist.Fill(row.chn0, ticks)
   ticks += 1

```

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 9, 2022, 10:34am UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107/3 "2022-08-09T10:34:59Z")

</div>

I am still getting a similar error message.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/1/b1b8af13030248785f0cc0c9508cd123dd55cabb.png)

code:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/b/9b6d4c428b918ad559336ec63e0b8265881b735e.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 9, 2022, 10:42am UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107/4 "2022-08-09T10:42:55Z")

</div>

Post the output of: `tree.Print()`

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 9, 2022, 11:17am UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107/5 "2022-08-09T11:17:35Z")

</div>

tree.Print() output:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/5/a5bdb919ff7f9ebf54e846216b6d7abeeb5481f4.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 9, 2022, 11:24am UTC](https://root-forum.cern.ch/t/segment-violation-error-in-pyroot/51107/6 "2022-08-09T11:24:03Z")

</div>

I guess you need:  
`chn0 = arr.array('l', [0]) # one "Int_t"`  
and then:  
`hist.Fill(chn0[0], ticks)`

Please do read the links given in this post:

> [@Looping to create new Branches with different data](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/4):
>
> array('i', [0]) is a 2-bytes signed integer so you need "leafname/S" [array - Efficient arrays of numeric values](https://docs.python.org/3/library/array.html)[TTree → Add a column (“branch”) holding fundamental types and arrays thereof](https://root.cern/doc/master/classTTree.html#addcolumnoffundamentaltypes)
