# TTree::Draw to histogram but return a null-pointer \[PYROOT\]

**URL:** <https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493>\
**Category:** Newbie\
**Created:** [August 27, 2021, 10:34am UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493 "2021-08-27T10:34:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chayanit\_nan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@chayanit\_nan](https://root-forum.cern.ch/u/chayanit_nan)\
**Post date:** [August 27, 2021, 10:34am UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493/1 "2021-08-27T10:34:17Z")

</div>

Dear experts,

I’m trying to draw histogram from TTree using TTree::Draw() in python and running on multiple root files as shown in a loop below.

```auto
for i in range(1,100):
  filein = ROOT.TFile.Open("job%i.root"%i)
  testtree = filein.Get("Cut1/Events")

  denominator.SetDirectory(ROOT.gDirectory)
  num_120.SetDirectory(ROOT.gDirectory)

  testtree.Draw("min(MET_pt, MHT_pt)>>+denominator")

  if (testtree.GetBranchStatus("HLT_PFMET120_PFMHT120_IDTight")):
    testtree.Draw("min(MET_pt, MHT_pt)>>+num_120","HLT_PFMET120_PFMHT120_IDTight")

  denominator.SetDirectory(0)
  num_120.SetDirectory(0)

```

But then I got

```auto
Traceback (most recent call last):
  File "makeTEfficiency2018.py", line 47, in <module>
    testtree.Draw("min(MET_pt, MHT_pt)>>+denominator")
ReferenceError: attempt to access a null-pointer

```

Could you suggest how to fix this null-pointer issue?

ROOT Version: 6.14/09  
Python: 2.7.14  
Platform: CMSSW on lxplus

Thanks,  
Chayanit

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 27, 2021, 11:35am UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493/2 "2021-08-27T11:35:24Z")

</div>

Hi @chayanit_nan ,  
and welcome to the ROOT forum.

Could it be that one of the files does not contain the tree `Cut1/Events`? Some debugging prints of the filename, the file contents and the `testtree` variable might help.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![chayanit\_nan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@chayanit\_nan](https://root-forum.cern.ch/u/chayanit_nan)\
**Post date:** [August 30, 2021, 1:07pm UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493/3 "2021-08-30T13:07:50Z")

</div>

Thanks @eguiraud , you’re right that some files do not contain `Events`. How can I skip the files/tree which do not contain the branch I need?

Cheers,  
Chayanit

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2021, 2:42pm UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493/4 "2021-08-30T14:42:47Z")

</div>

You should be able to check whether `testtree` is a nullptr. @etejedor how does one do that in PyROOT?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 30, 2021, 2:52pm UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493/5 "2021-08-30T14:52:08Z")

</div>

I believe it’s

```python
    ...
    testtree = filein.Get("Cut1/Events")
    if testtree:
        denominator.SetDirectory(ROOT.gDirectory)
        num_120.SetDirectory(ROOT.gDirectory)
        ...

```

---

<div class="post-metadata">

**Author:** ![chayanit\_nan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@chayanit\_nan](https://root-forum.cern.ch/u/chayanit_nan)\
**Post date:** [September 2, 2021, 8:14am UTC](https://root-forum.cern.ch/t/ttree-draw-to-histogram-but-return-a-null-pointer-pyroot/46493/6 "2021-09-02T08:14:04Z")

</div>

Thanks @yus . It works 🙂
