Type object 'ROOT' has no attribute 'TFile'


Please read tips for efficient and successful posting and posting code

I’m trying to run a modified version of a previous script, and while the original script works fine, my new script has the following error:

Traceback (most recent call last):
  File "/Users/npolishetty/PycharmProjects/SandBoxROOT/macro.py", line 50, in <module>
    analyze_delphes_output(input_file, output_file)
  File "/Users/npolishetty/PycharmProjects/SandBoxROOT/macro.py", line 4, in analyze_delphes_output
    file = ROOT.TFile.Open(input_file)
AttributeError: <namespace cppyy.gbl.ROOT at 0x139008170> has no attribute 'TFile'. Full details:
  type object 'ROOT' has no attribute 'TFile'
  'ROOT::TFile' is not a known C++ class
  'TFile' is not a known C++ template
  'TFile' is not a known C++ enum

I haven’t changed anything in terms of a TFile class call except for the name of the files I wanted to interact with.

Does anyone know how to avoid this error? Thanks in advance.

ROOT Version: 6.26.10
Platform: M1 macOS 13.4.1
Compiler: python 3.9


Out of curiosity, I copy pasted the old code into the new script so that the only thing differing was the file name, but I still got the same error.

npolishetty@NitoBOT SandBoxROOT % python3.9 macro.py
Traceback (most recent call last):
  File "/Users/npolishetty/PycharmProjects/SandBoxROOT/macro.py", line 50, in <module>
    analyze_delphes_output(input_file, output_file)
  File "/Users/npolishetty/PycharmProjects/SandBoxROOT/macro.py", line 4, in analyze_delphes_output
    file = ROOT.TFile.Open(input_file)
AttributeError: <namespace cppyy.gbl.ROOT at 0x1377d2850> has no attribute 'TFile'. Full details:
  type object 'ROOT' has no attribute 'TFile'
  'ROOT::TFile' is not a known C++ class
  'TFile' is not a known C++ template
  'TFile' is not a known C++ enum
npolishetty@NitoBOT SandBoxROOT % pytho3.9 ROOTRunner3.py
zsh: command not found: pytho3.9
npolishetty@NitoBOT SandBoxROOT % python3.9 ROOTRunner3.py
Done

Hi @Revan,

Hmmm, interesting… Out of curiosity, could you share the code for your script? (FYI, @vpadulan)

Cheers,
J.

Yeah, I’ll share it. An update though, I just made a new python file with the exact same code and the error went away. Weird. Also I know there’s an error with how I used TBranch and stuff, I’m still trying to learn that.

from ROOT import *


class SimCol:
    def __init__(self):
        self.m1 = TLorentzVector()
        self.m2 = TLorentzVector()
        self.invMass = 0
        self.particle = None

    def calcInvMass(self):
        self.particle = self.m1 + self.m2
        self.invMass = self.particle.M()

    def __str__(self):
        return 'Invariant Mass: ' + str(self.invMass) + '\n'


collision = None
histo = TH1F("histo", "Invariant Mass Distribution; Invariant Mass (GeV); Number of Occurrences", 100, 240, 962)

file = TFile("/Users/npolishetty/PycharmProjects/Physics2/mg5/MG5_aMC_v2_9_15_New/ppTOz_zTOmumu_13TeV/Events/run_01/tag_1_delphes_events.root", "read")
tree = file.Get("Delphes")
branch = tree.GetBranch("Muon")

for entry in branch:
    collision = SimCol()
    collision.m1.SetPtEtaPhiM(entry.Muon.PT[0], entry.Muon.Eta[0], entry.Muon.Phi[0], 0.1)
    collision.m2.SetPtEtaPhiM(entry.Muon.PT[1], entry.Muon.Eta[1], entry.Muon.Phi[1], 0.1)
    collision.calcInvMass()
    histo.Fill(collision.invMass)

file.Close()

tf = TFile("delphes_macro.root", "recreate")
histo.Write()
tf.Close()

print("Done")

The error changed now from an OS Error dealing with the TFile constructor saying that it can’t open the delphes file, which is just bizarre because it can access the file in my duplicate script. Literally nothing is different.

Hi @Revan,

the problem that you report is, indeed, weird. However, I don’t think we can reproduce it reliably given that it seems to be related to something in your environment.

I’m CC’ing @vpadulan, @etejedor to see if they have any ideas.

Cheers,
J.

Dear @Revan ,

My guess is as good as @jalopezg’s , I think there must be something wrong in your environment. Make sure you have one and only one ROOT installation, and that you are properly activating the environment e.g.

conda activate my-env-with-root

Cheers,
Vincenzo