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

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.

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

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

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

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

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

I believe it’s

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

2 Likes

Thanks @yus . It works :slight_smile: