Re: [ROOT] conditional object declaration

From: Robert Feuerbach (feuerbac@jlab.org)
Date: Wed May 21 2003 - 14:10:29 MEST


Hi Michael,

The problem is that you declare and define histo inside the (implied)
block of the 'if' statement, so it never makes it to the outside. 

What you want to do can be accomplished by using the fact that all
histograms inherit from TH1. Something that would work is instead: 

Int_t Macro(Int_t Dimension)
{
   TH1* histo=0;
   if (Dimension ==2) histo = new TH2F("histo"...)
   else if (Dimension ==3) histo = new TH3F("histo"...)

and then later
  
  if (histo->GetDimension() == 2) ((TH2*)histo)->Fill(...)
  else if (histo->GetDimension() == 3) ((TH3*)histo)->Fill(...)


You have to cast to TH2/TH3 (or TH2F/TH3F) since there is only a 1-D Fill
method in TH1.


Good luck,
     Rob Feuerbach

On Wed, 21 May 2003, K.-Michael Aye wrote:

> Date: Wed, 21 May 2003 11:35:54 +0100
> From: K.-Michael Aye <klaus-michael.aye@durham.ac.uk>
> To: roottalk@cern.ch
> Subject: [ROOT] conditional object declaration
> 
> Hi all,
> 
> I tried something like this
> 
> 
> #includes...
> 
> Int_t Macro(Int_t Dimension)
> {
>    if (Dimension ==2) TH2F* histo = new TH2F("histo"...)
>    else if (Dimension ==3) TH3F* histo = new TH3F("histo"...)
> 
> 
> But later in the code I want to fill histo
> 
>   if (Dimension == 2) histo->Fill(...)
>   else if (Dimension == 3) histo->Fill(..)
> 
> But during compilation with Aclic, it complains at fill-time that histo is 
> undeclared. So is conditional declaration not allowed then? Or how could I 
> achieve what I want to do?
> 
> Greetings,
> Michael
> 
> PS.: root 3.05/03, gcc 3.2 under Linux
> 

-- 
/***************************************************
 * Robert Feuerbach              feuerbac@jlab.org *
 * Jefferson Lab                CEBAF Center  A120 *
 * 12000 Jefferson Avenue   Office: (757) 269-7254 *
 * Mail Stop 12H              Page:       584-7254 *
 * Newport News, VA 23606      Fax: (757) 249-1965 *
 ***************************************************/



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:11 MET