Re: Creating and Filling Many Histogram

From: Brett Viren <bv_at_bnl.gov>
Date: Thu, 03 Feb 2005 11:34:12 -0500


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 Thu Feb 03 2005 - 17:34:29 MET

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