_ROOT Version: 6.19
_Platform: OS 10.14.3
Edit: I thought I had solved it, but I haven’t
I don’t know how else to describe this, but I’ll try. I have many directories with the data of different runs, i want to go to each directory, take the data of that run, and overlap their histograms, there are hundreds of directories, so this had to be done by code.
First I thought that I would need the memory addresses of each histogram I want to overlap, so I did this:
def overlapDistances(directory,rootFile):
os.chdir(directory)
subDirectories = listdir(directory)
subDirectories.remove('.DS_Store')
c1 = root.TCanvas("canvas1", "canvas", 800, 800)
keys = []
for subDirectory in subDirectories:
os.chdir(directory)
if os.path.isdir(directory + '/' + subDirectory):
newDirectory = directory + '/' + subDirectory
os.chdir(newDirectory)
infile = TFile.Open(rootFile+'.root')
keyList = TIter(infile.GetListOfKeys()) # generate pointers to the histograms
keys.append(keyList.Begin())
print keys
overlapDistances('/Users/Fer/Desktop/2ndSesion/maylarCover','addedHistograms')
This works, I tried with 10 directories as an example:
[<ROOT.TIter object at 0x7fe5bcbfb9b0>, <ROOT.TIter object at 0x7fe5bda11a60>, <ROOT.TIter object at 0x7fe5bd922800>, <ROOT.TIter object at 0x7fe5bd9226d0>, <ROOT.TIter object at 0x7fe5bd921e60>, <ROOT.TIter object at 0x7fe5bcd65350>, <ROOT.TIter object at 0x7fe5bd922ad0>, <ROOT.TIter object at 0x7fe5bd922010>, <ROOT.TIter object at 0x7fe5bd922b60>,<ROOT.TIter object at 0x7fe5bd922da0>]
then I thought “wait a second, I can get the actual histograms in the same loop” and I changed just one thing: I changed keys.append(keyList.Begin()) for keys.append(keyList.Begin().ReadObj())
However the results are… baffling, again, using 10 directories as an example:
[None, None, None, None, None, None, None, None, None, <ROOT.TH1F object (“average”) at 0x7f9e691f8e70>]
It changed but consistently the first, the seventh, and the last objects are sometimes actually histograms, everything else in the list is just a nasty “None”. If I try with more directories there are a few elements in the list that are indeed histograms, but I don’t see any pattern, only that the first and last seem to be okay most of the time.
And before you ask, yes I checked that the directories in question had valid root files, and they do, that’s not the issue.
I have tried many different things, for example I tried to loop through the elements of TIter, I know there is only one histogram, so I did:
key01 = TKey()
for key in keyList:
key0 = key
h.append(key0.ReadObj())
But this too leaves me with a list of mostly “None” with a few histograms sprinkled in between.
I have narrowed it to 3 possibilities: the method ReadObj(), a problem with the first element of a TIter, or both.
At first I thought I had solved it with keys.append(keyList.Begin().ReadObj().Clone()) until i decided to actually see the properties of those histograms, just to find that they are blank histograms with one of the properties they should. And before you ask, the histograms are cloning are not blank.
In fact, check this out:
def overlapDistances(directory,rootFile):
os.chdir(directory)
subDirectories = listdir(directory)
subDirectories.remove('.DS_Store')
c1 = root.TCanvas("canvas1", "canvas", 800, 800)
h = [None]*len(subDirectories)
for n in range(0,len(subDirectories)):
h[n] = root.TH1F(str(n),'1',1,1,1)
n=0
for subDirectory in subDirectories:
os.chdir(directory)
if os.path.isdir(directory + '/' + subDirectory):
newDirectory = directory + '/' + subDirectory
os.chdir(newDirectory)
infile = TFile.Open(rootFile+'.root')
keyList = TIter(infile.GetListOfKeys())
h[n] = keyList.Begin().ReadObj().Clone()
print h[n].GetNbinsX()
n=n+1
for histogram in h:
print histogram.GetNbinsX()
overlapDistances('/Users/Fer/Desktop/2ndSesion/maylarCover','addedHistograms')
This code prints:
3647
3647
3647
3647
3647
3647
3647
3647
3647
3647
3647
1
1
1
1
1
1
1
1
1
1
1
In the first loop we see that in fact the histograms are being cloned and as proof they have the right number of bins, but then in the second loop we see they have reverted back to their original state, somehow.
By debugging I found out that apparently when the next histogram is clones the old one is reseted, though the mechanism through which this happens escapes me.
How is it possible that applying a method on one object affects another object which is not even mentioned on that iteration?