Re: arrays of histograms

Rene Brun (Rene.Brun@cern.ch)
Fri, 15 Aug 1997 19:09:32 +0200


Peter wrote:
>
> I'm new to this list, hope this hasn't been asked before but I could
> not find out how to do this from the documentation.
>
> I want to make an array of histograms. I will start with
>
> TH1S *qll = new TH1S[16];
>
> Now, I guess this calls the default constructor TH1S::TH1S(void) with
> no information about setting up the histogram limits, titles,
> etc. I've tried to do this by hand with something like
>
> for (short it1 = 0; it1 < 16; it1++) {
> qll[it1].SetBins(500, 300, 800);
> }
>
> but this does not seem to work (I get core dumps when I try to fill
> the histograms.) What would the correct way of doing this be?
>
> (From the documentation, I see that the other constructors call the
> TH1S::Build() method to create the histogram data structure, but this
> is a private method ??)
>
> Thanks a lot in advance, and thanks for what looks to be a great
> package.
>

This is a small example using pointers to histograms.

{
// example of program creating, filling, saving hsitograms in a file
gROOT->Reset();

TFile hfile("hfile.root","RECREATE");

// create histograms
TH1S *h[16];
char name[20];
char title[100];
for (Int_t i=0;i<16;i++) {
sprintf(name,"h%d",i);
sprintf(title,"title of h%d",i);
h[i] = new TH1S(name,title,100,500,600);
}

// fill histograms
for (Int_t i=0;i<16;i++) {
for (Int_t j=0;j<1000;j++) {
Float_t x= gRandom->Rndm();
h[i]->Fill(500+100*x);
}
}

// save histograms
hfile.Write();
}

You can also look at the tutorials section at:
http://root.cern.ch/root/Tutorials.html
and in particular at:
http://root.cern.ch/root/html/examples/hsimple.C.html
http://root.cern.ch/root/html/examples/hsum.C.html

Rene Brun