Plot 2 variables from ttree in histo

Dears rooters,

I have a probelm plotting 2 variables from a ttree in histogram… which I already did and it is simple, so I don’t understand why it doesn’t wors… probably is something stupid…
following the code, and the root file that I use in input is the same as here


canvas= TCanvas("canvas", "canvas", 600,600)
input_file = TFile.Open(name_input_file, "read")		
imported_tree= input_file.Get("step")		

histo_tot= TH1D("histo_tot","histo_tot",100,-4815, -4770)		


name_file_out= "edep_vs_Z_" + name_input_file		

the problem is that the canvas that I save is ok, but fot the histogram I received an error message:
AttributeError: ‘PyROOT_NoneType’ object has no attribute ‘SaveAs’

thanks a lot in advance for your help :slight_smile:

TH1D has no SaveAs method

thanks for the reply. Why it not has the method? In past I already saved a TH1D as root file

Yes … but it is the Write method which does that.

TFile outputFile ("outputFile.root","RECREATE");
TH1F h ("myHisto","Histogram Title",100, -2, 2);

Actually no… On the same root file i run another code on another branche, but i used SaveAs and works… Look:


input_file = TFile.Open(name_input_file, "read")	

histo= ROOT.TH1D("histo","histo",1000,-4792, -4786)	

imported_tree= input_file.Get("p_charge_GAP1")		

imported_tree.Draw("z_creation_GAP1>>histo","z_creation_GAP1>-4792 && z_creation_GAP1<-4786","")	

name_file_out= "betterZ_" + name_input_file

Sorry. My mistake. It comes from TObject. and works:

root [0] hpxpy->SaveAs("a.root") ;
Info in <TH2F::SaveAs>: ROOT file a.root has been created
root [1] 

It might be an issue with PyROOT … in your first example

but the code according to me should be correct no? how I could solve the problem?
thanks in advance :slight_smile:

histo is not defined in your macro.

yes you are right, this was my mistake to copy the macro, but in the code is corret: histo_tot…
what it is also strange is that if I plot only 1 variable, doing for example:

in this way it’s works, but it’s not what I want… any idea?

I tried also to translate from pyROOT to ROOT, but the same problem, if I plot only 1 variable it works, but not 2…

TFile *f=new TFile("14_07_2017_18_21_58_GIFpp.root");
TTree *tr=(TTree*)f->Get("step");
TH1F* histo_tot = new TH1F("histo_tot","histo_tot",100,-4815, -4770);	
return ;

thanks in advance :slight_smile:

Be careful that you are trying to fill a 1D histogram with two variables. I do not have your data file so I simulated your macro with the ntuple sitting in hsimple.root generated by $ROOTSYS/tutorials/hsimple.c.

I wrote:

   TFile *f=new TFile("hsimple.root");
   TTree *tr=(TTree*)f->Get("ntuple");
   TH1F* histo_tot = new TH1F("histo_tot","histo_tot",100,-4815, -4770);

and it works fine:

$ root -n boog3.C
  | Welcome to ROOT 6.11/01            |
  |                                    (c) 1995-2017, The ROOT Team |
  | Built for macosx64                                              |
  | From heads/master@v6-09-02-2009-gdcbfa1d, Aug 11 2017, 10:11:09 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'      |

root [0] 
Processing boog3.C...
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
Info in <TH1F::SaveAs>: ROOT file prova.root has been created
root [1] 

histo_tot must be a TH2F if you have two variables.

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