Drawing histogram in a loop

Dear Rooters,

I am asking a pretty basic question but I couldn’t figure out how to do myself.
I have a tree and there are several branches in it like storage time, wavelength, time of flight etc.
I want to draw a histogram of branch “storage time” with filter on “wavelength” and this wavelength is a variable. So, in principle I want to draw array of histogram in a loop on same pad with changing wavelength (wavelength starts from 500 nm and changes in steps on 10nm.)

My trial after looking at few examples -

TH1D *hh[10] ;
	    char *histname = new char[10];
for(Int_t j=0;j<10;j++)
                sprintf(histname, "%s%d","hh",j);
	      tr->Draw(Form("tstor*1e-8>>hh(%d,%d,%d)",tbin,tmin,tmax),Form("(lambda == %d)",500 + j*10)*33e-3);
               TH1D *h =  (TH1D*)gDirectory->Get(histname);
	    if (h) h->Draw("same");


if (h) {
   if (j==0) h->Draw();
   else h->Draw("same");

(typo fixed).


I tried to implement your suggestions but unfortunately it says-

"Error: illegal pointer to class object h 0x0 1125 tree_2nov.cxx:247:
*** Interpreter error recovered *** "

You should have done it wrong because the text in “h” should protect that …

The lines I sent you should replace

       if (h) h->Draw("same");

in your original code.

Note I did a typo… it should be:

if (j==0)


if (j=0)

obviously :slight_smile:

Hello again,

Thank you for quick help. This time I checked my code carefully with the corrections of typos… #-o
and succeeded in drawing histogram but it draws only for j== 0. and same error message pops up.

Can you send a small running script showing the problem ?