Re: getting the contents of a root histogram

From: Valery Fine (fine@bnl.gov)
Date: Tue Dec 01 1998 - 19:38:12 MET


On  1 Dec 98 at 12:06, Craig Ogilvie wrote:


> In the spirit of asking novice questions, I'm trying
> to access the contents of a root histogram.,
> In paw I would hist/get_vec/cont, in root I've found 
> the possibility Int_t content= h1->GetBin(bin)
> but this seems geared to getting one bin at a time.
> Be4 I write a loop to fill an array, is
> there a function that gets all the values for
> me? 
>  Thanks
>   Craig Ogilvie
> ps I checked roottalk with the search option)
> and couldn;t find any discussion on this
> 

  Hello Craig,

  Good question and that is not as easy as one may think from the 
first glance.

  Let's solve it "step by step" using root.cern.ch Web site:

  1. I assume you are creating some TH class. Let's say
     TH1F - 1-dimensional histogram with "float" bins

  2. The description of this class is present on:

        http://root.cern.ch/root/html/TH1F.html

      Here we can find NO method to get what you want, but we 
      would suspect it is somewhere.

  3. Next step would be to get more broad view of the TH1F class.
     Let's try this by "clicking" 

                  "inheritance tree"

     on that page.

     This should bring us a Postscript diagram of the class.
     We should investigate it from the right to left looking for some 
    "public method" which we will suspect is Ok for us.

 4.  First we will investigate TH1F box. In this box there is only 
     method which may attract our attention. It is "GetBinContent"
     But you have evaluated this method and found it is not what you 
     want.

 5. Second. Look up TArrayF box below that TH1F (don't ask me why 
   the second one is very TArrayF, very likely you yourself would 
   choose TH1 box for the second step). There we can find an
   "attractive" method:

         GetArray()

 6. Now we should go back to the TH1F class description and "click" 
    TArrayF "blue line "to go to  
        http://root.cern.ch/root/html/TArrayF.html 
    with that TArrayF class definition.
    We will look for something returning us a pointer to Float_t *
    Here we can find methods:

             Float_t* GetArray()
and method
             Float_t& operator[](Int_t i)

   Since TH1F is derived from TArrayF all of the methdos above can be 
   applied not only to TArrayF but to TH1F as well.

   Now we may reply the question:

   To get ALL bins from some histogram one may try two of the methods 
above:

    1. First solution:

       TH1F myhist(...);
       Float_t *bins = new Float[myhist.GetSize()];

       for (Int_t i=0;i<myhist.GetSize();i++) bins[i] = myhist[i];
    
       From the first glance one may think it is the same as 
       GetBinContent(), namely we can draw the loop above as follows:

       for (Int_t i=0;i<myhist.GetNbinsX();i++) bins[i] = myhist.GetBinContent(i);

      But GetBinContent returns you Stat_t value. Alas it is not easy 
      to figure out what this "Stat_t" stands for, either this 
      "integer" of "float". If "Stat_t"  is "kind of integer 
      GetBinConten() may confuse you due rounding if your histogram 
      was filled with some weight.
         Clicking "Stat_t" brings you an explanation:

         http://root.cern.ch/root/html/ListOfTypes.html#Stat_t

            "Stat_t ............. Statistics type"
         

    2. Second solution:

       TH1F myhist(...);
       Float_t *bins = myhist.GetArray();

   The first solution creates a copy of the histogram, when the 
second one gives you a direct access to that histogram bins with no 
copy (which is NOT recommend approach)    

  Of course I am leaving for roottalk and for ROOT team the question 
how one can figure out that Float_t array is those bins. What about 
overflow and underflow bins. Are they present in this array or not, 
If yes where ?

  Does Int_t   TH1F::GetNbinsX()  returns the same number as 
       Int_t   TArray::GetSize();

  I am forwarding this reply to "roottalk" if someone from ROOT folk 
may provide us better solution.

  Anyway I hope this helps,
       With my best regards,
                                  Valery

  

 
=================================================================
Dr. Valeri Faine (Fine)
    -------------------          Phone: +1 516 344 7806
Brookhaven National Laboratory   FAX  : +1 516 344 4206
Bldg. 510A /STAR                 mailto:fine@bnl.gov
Upton, New York, 11973-5000      http://nicewww.cern.ch/~fine
USA
                                 
Dr. Valery Fine                  Telex : 911621 dubna su
    -----------
LCTA/Joint Inst.for Nuclear Res. Phone : +7 09621 6 40 80
141980 Dubna, Moscow region      Fax   : +7 09621 6 51 45
Russia                           mailto:fine@main1.jinr.dubna.su                              



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:40 MET