Stacked histo is not plotted as expected

Dear ROOT experts,

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.

readingsamples2.py (6.1 KB)
rootlogon.py (2.0 KB)

_ROOT: Version: 6.18.04
Platform: Ubuntu18.04


Thanks,
Aya

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:

	hs.Add(h0,"HIST")
	hs.Add(h1,"HIST")
	hs.Add(h2,"HIST")
	hs.Add(h3,"HIST")
	hs.Draw()

Thanks for kind reply!

I have added the required files in

/afs/cern.ch/user/a/abeshr/public/CMSSW_8_0_21/src 

command for running script:

python readingsamples2.py -o newfile.root -t "output_demo" -n 100 --xmin 100 --xmax 180 -x "m_{4mu}(GeV)" --logY

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.

Thanks a lot!
Aya

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
%

Note you do not need the option SAME when you draw the legend. a simple Draw with option is enough.

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:

	leg.AddEntry("HHbb4Mu", "Hhhbb4Mu"    , "P")
	leg.AddEntry("TTbar", "TTbar"       , "F")
	leg.AddEntry("ZZ4Mu", "qq~->ZZ4Mu"  , "F")
	leg.AddEntry("ZZbb2Mu", "qq~->ZZbb2Mu", "F")

I see your main macro calls setStyle in rootlogon.py . In theory that should work

yes that’s fine (it’s just calling gStyle from another module).

@couet

Thanks a lot for your trial!

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.

Thanks,
Aya

@etejedor

Thanks for your kind reply!

My question is why the code can’t see the settings in rootlogon.py file and apply it to histos? I don’t know if I am missing something here.

Thanks,
Aya

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 tried, but nothing changed!

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)

Ok, then I’d say this is not Python-related, @couet may guide you through other things to try.

Thanks a lot!

I cannot try. The folder …

/afs/cern.ch/user/a/abeshr/public/CMSSW_8_0_21/src 

… does not exist anymore.

sorry, it was deleted by accident… I will upload the required folder again…

thanks,
aya

I uploaded them. If any additional files are required send me kindly, I will upload them, coz my quota is running out.

Thanks a lot for helping
Aya

I guess samples.txt is missing ?

When I run your macro I get:

% python readingsamples.py
Traceback (most recent call last):
  File "readingsamples.py", line 63, in <module>
    output_file = ru.TFile(outfile , "RECREATE")
  File "/Users/couet/git/couet-root-bin/lib/ROOT/pythonization/_tfile.py", line 52, in _TFileConstructor
    self._OriginalConstructor(*args)
TypeError: none of the 2 overloaded methods succeeded. Full details:
  TFile::TFile() =>
    TypeError: takes at most 0 arguments (2 given)
  TFile::TFile(const char* fname, const char* option = "", const char* ftitle = "", int compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault) =>
    TypeError: could not convert argument 1 (expected string or Unicode object, NoneType found)

Sorry for the late reply!

I uploaded it.

Thanks,
Aya

I got the new files. Stil it does not work:

 python readingsamples.py 
Traceback (most recent call last):
  File "readingsamples.py", line 63, in <module>
    output_file = ru.TFile(outfile , "RECREATE")
  File "/Users/couet/git/couet-root-bin/lib/ROOT/pythonization/_tfile.py", line 52, in _TFileConstructor
    self._OriginalConstructor(*args)
TypeError: none of the 2 overloaded methods succeeded. Full details:
  TFile::TFile() =>
    TypeError: takes at most 0 arguments (2 given)
  TFile::TFile(const char* fname, const char* option = "", const char* ftitle = "", int compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault) =>
    TypeError: could not convert argument 1 (expected string or Unicode object, NoneType found)

What should I do ? how do you run this script ?

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