# Normalizing histograms inside .root file

Hello,

I am want to write a script so i can normalize the histograms in a .root file. i wrote the following script below, but the histograms inside the .root file are the same after i run the scripts, although the ones a plot are normalized.

the ster[] array is where i keep my histograms, and teste[] is where the name of the histograms go.

for i in range(nHistograms):
ster[i] = histFile.Get(teste[i])

f = root.TFile("mc16e-LH-CVetoBVeto.root")
w = f.Get("sumOfWeights")
ws = w.GetBinContent(1)
F = (1.01*0.85)/ws

for i in range(nHistograms):
ster[i].Scale(F/(ster[i].Integral()+1))

for i in range(nHistograms):
ster[i].SetDirectory (histFile)
ster[i].Write()

So i would appreciate any help or tips.

Best Regards,
Caio Cesar.

Hi,

You should opened the TFile in writable mode. I would copy my original fills, in order not to loose it in case of a mistake and then do:
f = root.TFile("mc16e-LH-CVetoBVeto.root","UPDATE")

Lorenzo

1 Like

It worked, thank you!