Re: extracting bin contents

Rene Brun (Rene.Brun@cern.ch)
Sat, 22 Mar 1997 09:25:18 +0100


Wolfgang Korsch wrote:
>
> Hi,
> does somebody know how to extract the contents of histogram bins?
> I need something comparable to /hist/get/content in PAW.
> I also would like to write the contents to an external
> ascii file. Any suggestions?
>

All histogram classes are derived from TH1. This class includes
several member functions to access the histogram info, such as
GetBinContent, GetBinError,etc

In an interactive macro, you can do for example:

Root > Int_t nbins = h->GetNbinsX();
Root > {for(Int_t i=0;i<nbins;i++) printf("bin[%d]=%g\n",i,h->GetBinContent(i));}

where h is a pointer to an histogram.

To write the contents to an ascii file, you can:
- Include these two statements (or more) in a macro (say test.C)
and do: Root > .x test.C > test.log
The file test.log will contain the output of the macro

- type any root command, for example second command above
and piping the result to a file. Note that in this case, the command
must be terminated by ;
Root > { for(..) printf(..);} > test.log

To dump the bin contents of an histogram to a file, you can also do:
Root > h->Print("all"); > test.log
To see the histogram data structure (or any object):
Root > h->Dump(); > test.log

Rene Brun