Plotting histograms from macro created with TTree::MakeClass

Hi Rooters

I have created an analysis class for a Tree using TTree::MakeClass. I fill 2 histograms in the loop. After the loop I would like to plot these histograms and to be able to interact with canvas and other objects. I use the following code:

Canvas = new TCanvas(“Canvas”,“Plotting Canvas”);

and add:

Int_t a;

to the end in order to see the output (since I don’t know a better way.)

Perhaps the following output when creating an object of the class is relevant:

root[ ] .L MyClass.C
root[ ] MyClass m
Warning in TClass::TClass: no dictionary for class T3Vector is available

The problems are:

  1. The output is erratic. Sometimes the canvas comes up blank and other times the histograms are plotted correctly. i.e. I can keep running the command m.Loop() until it works.

  2. I am unable to interact with the canvas, and if I leave off the “Int_t a; cin>>a;” it all just disappears.

Please suggest a fix for this.


I do not understand this problem. It looks like a scope problem when objects are created in the stack instead of with new.
Could you send me your .C,.h and .root file
reproducing this problem?


Hi Rene

I am running Red Hat 8.0 kernel 2.4.18-18.8.0 with root version 3.05/06.

I have attached the .C, .h, and .root files.

MyClass.h (5.5 KB)
MyClass.C (3.88 KB)
Polarfile.root (12.1 KB)


My guess was right. You have a scope problem. You create your two histograms in the stack instead of using new.
Your histograms are automatically deleted when leaving the Loop function. Change your two lines creating the histograms to:

TH1F *Neutrino_Energy = new TH1F(“Neutrino_Energy”, “Neutrino Energy”, 100,0,1);
TH1F *Neutrino_Mass = new TH1F(“Neutrino_Mass”, “Neutrino Mass”, 100,0,1);


hi ,
I am having a similar problem as posted earlier in this topic.
I use exactly the same structure (i,e MakeClass).
I create histograms on the stack . However after the Loop execution is done and when i want to, say, Divide two histograms, I get an out of scope error message for the histograms that I want to divide. Sometimes
it makes the division only once . A repeated h3->Divide(h1,h2,c1,c2)
yields out scope error message.

.ls shows that histograms are there and i can Draw them one by one. It is quite confusing.

I tried creating histos before Loop(). In this case the
h3->Divide(h1,h2,c1,c2) always
works but the errors are not calculated properly eventhough I do Sumw2() just after I create histos…

Any ideas about this problem? Or is it me that I am doing something fundamentally wrong here?

(I use latest root version 3.10 on RH9.)

thansk a lot in advance


Just a correction to my previous post.
I create histograms as
TH1F *h1 = new TH1F(“h1”,"",40,4,30))

NOT on the stack as I wrote in…
sorry about this mistake…

It is hard to understand what you are talking about without having a concrete piece of code. Please send a running script showing the problem


I attached s.C and s.h. the data file size is about 40MB. so i did not include it…

after execution is finished, when i make the division again as shown below, i can the error messages…

thank much again

root[0] .L s.C
root [1] s t
t.root [2] t.Loop()
root [3] ht3all->Draw()
TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [4] ht3all->Divide(ht1all,ht2all,1,1);
Error: Symbol ht1all is not defined in current scope FILE:(tmpfile) LINE:1
Error: Symbol ht2all is not defined in current scope FILE:(tmpfile) LINE:1
Error in TH1D::Divide: Attempt to divide by a non-existing histogram
*** Interpreter error recovered ***
root [5] .q
s.h (77.9 KB)
s.C (2.48 KB)

Your file s.C does not compile. You have several missing declarations
in s.h. I suggest you try
root > .L s.C++

to see thye list of compilation errors