Re: Selective filling of a histogram

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Fri, 31 Oct 2008 11:41:20 +0100


Roger,

I do not understand your problem (well, I just had a quick glance to it::) However, you do not need all this complexity and can divide your code by at least a factor 3.
Simply use the automatic binning feature of ROOT histograming. The limits that will be calculated will be much nicer than what you are trying to do with your algorithm. To get the auto-binning, simply define the lower limit equal to the upper limit, eg instead of

  TH1D *henergy = new TH1D("energy","energy(eV)",100,min-max/10,max+max/10);

Do:

  TH1D *henergy = new TH1D("energy","energy(eV)",100,0,0

Rene Brun

oger Mason wrote:
> Hello,
>
> I'm trying to fill a histogram selectively using the following code
> (generated using TTree::MakeClass the modified):
>
> #define MyClass_cxx
> #include "MyClass.h"
> #include <TH2.h>
> #include <TStyle.h>
> #include <TCanvas.h>
>
> void MyClass::Loop()
> {
> if (fChain == 0) return;
>
> Long64_t nentries = fChain->GetEntriesFast();
>
> Double_t min = 0;
> Double_t max = 0;
>
> Long64_t nbytes = 0, nb = 0;
> for (Long64_t jentry=0; jentry<nentries;jentry++) {
> Long64_t ientry = LoadTree(jentry);
> if (ientry < 0) break;
> nb = fChain->GetEntry(jentry); nbytes += nb;
> // if (Cut(ientry) < 0) continue;
> if ( ientry == 0)
> max = min = energy;
> if ( energy < min )
> min = energy;
> if ( energy > max )
> max = energy;
> }
> cout << " Min = " << min << " max = " << max << endl;
>
> TH1D *henergy = new TH1D("energy","energy(eV)",100,min-max/10,
> max+max/10);
> TH1D *h = new TH1D("selected","energy(eV)",100,min-max/10,
> max+max/10);
> TCanvas *canv = new TCanvas("canv","Canvas");
>
> for (Long64_t jentry=0; jentry<nentries;jentry++) {
> Long64_t ientry = LoadTree(jentry);
> if (ientry < 0) break;
> nb = fChain->GetEntry(jentry); nbytes += nb;
> // if (Cut(ientry) < 0) continue;
> henergy->Fill(energy);
>
> if ( energy > -2143.58 ) {
> // cout << "Energy = " << energy << endl;
> h->Fill(energy);
> }
> }
>
> h->SetFillColor(2);
>
> henergy->Draw();
> h->Draw("same");
> canv->Modified();
> canv->Update();
> }
>
> I run the code:
>
> root [0] .L MyClass.C
> root [1] MyClass m
> root [2] m.Loop()
> Min = -2143.61 max = -2143.55
>
> but histogram h is alway empty.
>
> Can someone point out what is wrong?
>
> Thanks,
> Roger
>
> ROOT 5.20/00 (trunk_at_24524, Sep 30 2008, 15:32:00 on linux)
>
>
Received on Fri Oct 31 2008 - 11:37:16 CET

This archive was generated by hypermail 2.2.0 : Fri Oct 31 2008 - 23:50:01 CET