Re: Q: how to get entry in a bin (TH1*)

From: daniel cussol <cussol_at_ccimap.in2p3.fr>
Date: Mon, 30 Apr 2007 14:28:06 +0200


Junichi Tanaka a écrit :
> Hi Mario,
>
> I know that we can get 0.3 in binx=3 in your example.
> My question is how to get 2 in binx=3.
> (How many did we call "Fill()" for a bin?)
>
> Junichi
>
> 2007/4/30, Mario Kadastik <mario.kadastik_at_cern.ch>:
>> Erm, I don't quite understand the problem. Here is a sample root
>> session:
>>
>> root [0] TH1D *t = new TH1D("t","tt",5,0,5);
>> root [1] t->Fill(1,0.1)
>> (Int_t)2
>> root [2] t->Fill(2,0.1);
>> root [3] t->Fill(2,0.2);
>> root [4] t->GetBinContent(1)
>> (const Double_t)0.00000000000000000e+00
>> root [5] t->GetBinContent(2)
>> (const Double_t)1.00000000000000006e-01
>> root [6] t->GetBinContent(3)
>> (const Double_t)3.00000000000000044e-01
>> root [7] t->GetBinContent(4)
>> (const Double_t)0.00000000000000000e+00
>>
>> Mario
>>
>> On Apr 30, 2007, at 2:18 PM, Junichi Tanaka wrote:
>>
>> > Dear all,
>> >
>> > I fill events with a weight, which depends on events, in a TH1D.
>> > In this case, how do I get entry of a bin?
>> > For example, 3 events are filled in a bin with 0.1, 0.1 and 0.2.
>> > By using GetBinContent(i), we can get 0.4 but I don't know how to
>> > get 3.
>> > Is it possible?
>> >
>> > best regards,
>> > Junichi Tanaka
>> >
>>
>>
>
>

Hello Junichi,
 The only way to do what you want is to define a clone of your 1D histogram and fill the clone with a weight 1. each time you fill your histogram. The bin 3 of your histogram will contain the sum of weights and the bin 3 of the clone will contain the number of entries for this bin. The code could be like this:

TH1F *hOri=new TH1F("MyHist","Weighted histogram",100,0,100); TH1F *hCount=(TH1F *)hOri->Clone("MyHistCounts"); for(Int_t i=0;i<10000;i++)
 {
 Double_t x=gRandom->Gaus(45.,3.);
 Double_t weight=gRandom->Rndm();
 hOri->Fill(x,weight);
 hCount->Fill(x,1.);
 }

Note that if you use a TProfile, the number of entries can be obtained directly by calling the TProfile::GetBinEntries(Int_t bin) method. I hope this helps.

Best regards.

-- 
Daniel CUSSOL

LPC Caen IN2P3/ENSICAEN/Universite de Caen
Boulevard du Marechal Juin
14050 CAEN CEDEX

e-mail : cussol_at_in2p3.fr
Tel    : +33-(0)2-31-45-29-73
FAX    : +33-(0)2-31-45-25-49
Received on Mon Apr 30 2007 - 14:27:43 CEST

This archive was generated by hypermail 2.2.0 : Mon Apr 30 2007 - 23:50:02 CEST