File is created but nothing is written on it


_ROOT Version: 6.19/01
_Platform: OS Mojave
_Compiler: Cling


I have a bunch of histograms I want to save. I create a file then loop through the histograms to save them, but for some reason I get this message:

Error in TROOT::WriteTObject: The current directory (PyROOT) is not associated with a file. The object (FLMT034741_16-17-44-413) has not been written.

I get it once for every histogram that I want to save.

The weird part is that the file is indeed created and it is in the directory it should be, and yet I cannot get the histograms to be written on it.

The relevant part of the code is this:

file = root.TFile('OceanView.root','recreate') #create place to save histograms
for file in ourFiles:
    name = file[0:len(file)-4] #the name has the .txt extension, so I remove it
    [v1, v2] = aTv.getVectors(directory+'/'+file) #this function extracts the data from the .txt file
    histogram = vTh.histo(v1,v2,name) #this function takes the data from the .txt file and creates an object that, among other things contains the histogram
    histogram.hist.Write() #I want to save the histogram 

The functions I use work as intended, I have tried them individually and I hadn’t had any problems

Try:

outfile = root.TFile(...) # use a "distinct" variable name here
# ... your "for file in ourFiles:" loop comes here
outfile.Close() # we are done

thank you so much, you and Philippe are amazing

could you explain me what happened?, was it just that the name “file” was a bad choice of name?

google -> python object lifetime

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.