Unsupported Operand Type

Hi, I’ve got this code which takes a number of .root files, each with 3 different TH1F histograms. It adds up all the root files, and outputs a .root file with the 3 different summed histograms. Here is the type error:

      2     for filename in files[1:]:
      3         f = TFile(filename)
----> 4         p_loss += TH1F(f.Get("PlossHisto"))
      5         e_loss += TH1F(f.Get("ElossHisto"))
      6         p_hits += TH1F(f.Get("PhitsHisto"))

TypeError: unsupported operand type(s) for +=: 'PyROOT_NoneType' and 'TH1F'

Now this is fair enough, except as shown below I’ve defined p_loss, e_loss and p_hits as type TH1F:

[code] #create clones of first histograms
#these are now the total histograms we add to
p_loss = TH1F.Clone(plosshis)
e_loss = TH1F.Clone(ehitshis)
p_hits = TH1F.Clone(phitshis)

if len(files) > 1:
    for filename in files[1:]:
        f = TFile(filename)
        p_loss += TH1F(f.Get("PlossHisto"))
        e_loss += TH1F(f.Get("ElossHisto"))
        p_hits += TH1F(f.Get("PhitsHisto"))


The operator ‘+=’ will work for two TH1F, but it appears the p_loss, e_loss and p_hits histograms lose their TH1F type and I don’t know why. Any insight offered would be greatly appreciated.

Can you explicitly check that the histograms you are Cloning from are valid, and that the histograms you get from the files are also valid?

I think if you TFile::Get an object with a name that doesn’t exist, you get a NoneType, so maybe your histogram names are incorrect, or maybe your filenames are incorrect (and thus opened as zombies).



what Jean-François said, but with a small addition. You get NoneType if an object does not exist, but you get PyROOT_NoneType if an object initially existed, and then got deleted on the C++ side. Likely your histograms have the same name?