Re: Creating and Filling Many Histogram

From: Rene BRUN <brun_at_mail.cern.ch>
Date: Sat, 12 Feb 2005 09:15:52 +0100 (MET)


Replace

   for(Int_t i = 1; i <= nb; i++)
by

   for(Int_t i = 0; i < nb; i++)

Rene Brun

On Sat, 12 Feb 2005, zaldy wrote:

>
> Hi,
>
> What is wrong with this piece of code
>
> //--------------------------------
> void test()
> {
> const int nb = 34;
> float a=-170.;
> float b=170.;
>
> TH1F* histo_xvsz_front[nb];
> for(Int_t i = 1; i <= nb; i++)
> {
> histo_xvsz_front[i] = new TH1F(Form("histo_xvsz_front_%03d",i),Form("Histogram xvsz_front #%d",i), nb,a,b);
> }
>
> }
>
> Thanks
>
> using RootVersion 4.02/00 29 January 2005
> on gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)
>
> //----------------------------
>
> > Hi Zaldy,
> >
> > zaldy <zaldy_at_neutrino.kek.jp> writes:
> >
> > > I have two questions.
> > > I would like to create many histogram simultaneously with names of the
> > > form: histo_1, histo_2, .... , histo_(i) where i=1,2,3,4,....,n.
> > >
> > > I figure out doing it like this:
> > >
> > > for(Int_t i=1; i<n; i++)
> > > {
> > > TH1F * histo_(i) = .....
> > > }
> >
> > First, you need square brackets for array dereferencing in C++.
> >
> > TH1F * histo_[i] = .....
> >
> > As for a sequence of similar histograms I often do things like:
> >
> >
> > const int n=100;
> > TH1F* histo[n];
> > for(Int_t i=1; i<n; i++)
> > {
> > TH1F * histo[i] = new TH1F(Form("name_%03d",i),Form("Histogram #%d",i),...);
> > }
> >
> > If you don't actually care about the name and will always refer to the
> > histogram via the pointer then you don't need a unique name but you
> > need to call SetDirectory(0):
> >
> > const int n=100;
> > TH1F* histo[n];
> > for(Int_t i=1; i<n; i++)
> > {
> > TH1F * histo[i] = new TH1F("blah","Blah Blah",...);
> > histo[i]->SetDirectory(0);
> > }
> >
> >
> > Luck,
> > -Brett.
> >
>
>
Received on Sat Feb 12 2005 - 09:15:57 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:04 MET