I have analysis output_root files in which results stored in form of tree, branches and leaves (all variables are 1d vectors) and all have the same tree name. I need to select some variables, plot them in histos and then add all to one stacked histo.
I have created a python script for doing so, but actually the stacked histo has several issues (logy is not applied, legend is not added, the histos are not filled, but rather represented by points and error bars) despite I defined the required conditions for doing so including HIST draw option.
I tried to figure out what might be the issue with my code, but couldn’t, its my first steps towards pyroot.
Any help will be much appreciated, I have attached the code here.
It is difficult to run your example as we do not have the data file. I can suggest you to apply the HIST option on the individual histograms instead of globally:
I have done what you kindly mentioned, the error bars issue solved, but others still, also styles I defined in rootlogon.py seems to be not applied on the plot, you could kindly find the plot as stackedhisto.png in the same path given.
I see your main macro calls setStyle in rootlogon.py . In theory that should work. I am not a python expert mays be that’s a python issue (@etejedor might know). I took the .txt file from your afs repository. I did not take te big root files as they do not seem to be used by your example:
% grep root readingsamples2.py
import rootlogon
rootlogon.setStyle()
help = "define output root file for saving results",
out_stck_histo = ru.TFile("stacked_histo.root", "RECREATE")
if (os.path.exists("stacked_histo.root")):
print("stacked histo is created successfully in: stacked_histo.root")
%
The only root file mentioned in your macro is created. So in principal it should be fine. But I get:
% python readingsamples2.py -o newfile.root -t "output_demo" -n 100 --xmin 100 --xmax 180 -x "m_{4mu}(GeV)" --logY
...working with signal...
Error in <TFile::TFile>: file /Users/couet/Downloads/output_demo_Hhhbb4M_BMP1.root does not exist
Traceback (most recent call last):
File "readingsamples2.py", line 84, in <module>
input_file = ru.TFile(sample_path, "READ")
File "/Users/couet/git/couet-root-bin/lib/ROOT/pythonization/_tfile.py", line 55, in _TFileConstructor
raise OSError('Failed to open file {}'.format(args[0]))
OSError: Failed to open file output_demo_Hhhbb4M_BMP1.root
%
The fact you do not see the legend is not clear to me. One thing I see is the legend is quite big (.2,.2,.8,.8) … it might cover a large part of your plot… but ok … It should be visible. It could be that the AddEntry method is not happy with the pointers to the histograms (not sure how it works in Python) . Instead of passing the pointers gives the name of the histograms:
The signal file may be not downloaded correctly in /Users/couet/Downloads/output_demo_Hhhbb4M_BMP1.root I have tested the code on lxplus public area and it works fine.
I have tried with histos names instead for legend, but it is still not shown! also logY scale is not applied.
The settings you do should be applied even if you do them in a separate module. Can you try to do them in readingsamples2.py to see if that changes anything? Perhaps pick one and do it alongside the histogram creation. But it should not really make a difference.
I have also applied Integral() and Scale() to histos in readingsamples.py after block # adjust filling cosmetics as follows, to adjust plot, but not such great difference,
# get integral of histos bins, which is the summation of bin contents in x-axis range (helps in getting continous plot instead of discrete one)
h0.Integral()
h1.Integral()
h2.Integral()
h3.Integral()
# add scale factor to histos
sf0 = 1/h0.Integral()
sf1 = 1/h1.Integral()
sf2 = 1/h2.Integral()
sf3 = 1/h3.Integral()
h0.Scale(sf0)
h1.Scale(sf1)
h2.Scale(sf2)
h3.Scale(sf3)