Re: How to draw a list of histograms

Rene Brun (Rene.Brun@cern.ch)
Tue, 01 Jul 1997 17:12:19 +0200


> Hi,
> I need to draw on a Pad (using the Divide(..) method some of the TH1Fs
> of a big file (about 3 Mb).
> Well, my difficulty is that all my TH1Fs are stored in the form hxyzt
> KEY: TH1F h50418;1 PHI 4 PL 3 A
> KEY: TH1F h50419;1 PHI 4 PL 4 A
> KEY: TH1F h50420;1 PHI 4 PL 5 A
> KEY: TH1F h50421;1 PHI 5 PL 1 A
> KEY: TH1F h50422;1 PHI 5 PL 2 A
> KEY: TH1F h50423;1 PHI 5 PL 3 A
> KEY: TH1F h50424;1 PHI 5 PL 4 A
> KEY: TH1F h50425;1 PHI 5 PL 5 A
> KEY: TH1F h50426;1 PHI 6 PL 1 A
>
> So I can't use an array of TH1F like i read in the reply that Rene Brun
> wrote to Pasha Murat suggesting a prog like this:
>
> 1.{
> 2. gROOT->Reset();
> 3. TFile f("~/root/histos/test.root");
> 4. TCanvas *page = new TCanvas("page");
> 5. page->Divide(2,4);
> 6. TH1F *h[9];
> 7. char padname[20];
> 8. for(Int_t i=0;i<8;i++){
> 9. sprintf(padname,"page_%d",i+1);
> 10. TPad *pad = (TPad*) page->GetPrimitive(padname);
> 11. pad->cd();
> 12. h[i]->Draw();
> 13. }
> 14.}
> Anyway when I run this prog the CINT says:
> Error: illegal pointer to class object pad 0x0 77 FILE:divide.c LINE:11
> Error: illegal pointer to class object h[i] 0x0 155 FILE:divide.c
> LINE:12
>
> Can u tell me how to draw my TH1Fs?
> And the GetPrimitive(..) method does?
>
> Many thanks, Luca Sfarzo

The following macro illustrates how to loop on a list
of histograms (h50418 --> h50426)
{
gROOT->Reset();
TFile f("~/root/histos/test.root");
TCanvas *page = new TCanvas("page");
page->Divide(2,4);
TH1F *h;
char padname[20];
char hname[20];
for(Int_t i=0;i<8;i++){
sprintf(padname,"page_%d",i+1);
TPad *pad = (TPad*) page->GetPrimitive(padname);
pad->cd();
sprintf(hname,"h%d",i+50418);
h = (TH1F*)f.Get(hname);
h->Draw();
}
}

The following macro illustrates how to loop on the list
of keys in the current file using an iterator.
key name is the histogram name.
{
gROOT->Reset();
TFile f("~/root/histos/test.root");
TCanvas *page = new TCanvas("page");
page->Divide(2,4);
TH1F *h;
char padname[20];
TIter next(f.GetListOfKeys());
TKey *key;
i = 0;
while (key=(TKey*)next()) {
sprintf(padname,"page_%d",++i);
TPad *pad = (TPad*) page->GetPrimitive(padname);
pad->cd();
h = (TH1F*)f.Get(key->GetName());
h->Draw();
}
}

Rene Brun