If statements in for loops

Some macros of mine are being plagued by this rather awkward problem of which I’ve just recently found the problem.

The following macro works fine

void test()
{
    
    if(0)
    {
        
        ntuple->get;     
    }
    else
    {
        cout << "Success" << endl;
    }
    
}

Run it and ROOT returns “Success”. Everything is good.

But if you imbed this same if statement in a loop,

void test()
{
    
    for(Int_t j = 0; j < 5; j++)
    {
        if(0)
        {
            
            ntuple->get;     
        }
        else
        {
            cout << "Success" << endl;
        }
    
    }
} 

ROOT returns

Error: non class,struct,union object $ntuple used with . or -> FILE:test.c LINE:9
*** Interpreter error recovered ***

Why does a for loop cause ROOT to look into the statements that shouldn’t be executed?

I know “ntuple->get” doesn’t make any sense, but if you use a more appropriate construction the same problem occurs.

I’m using 4.04/02 on OS X, but the same problem occurs on a PC running an older version of ROOT.

Thanks for any help!

Hi,

CINT has a byte-code compilation step that requires the parsing of all the code paths.

Philippe

So if CINT parses all of the paths, is there anyway to do something like

if(file exists)
{
load file
load ntuple data
project ntuple data into histogram
fit histogram to “gaus”, for example
retrieve fit parameters
}

without receiving an error? If the file doesn’t exist and the statements are not executed, ROOT complains that “gaus” (or any other fit function) isn’t a struct, class, etc, when I try guas->GetParameter(0).

Thanks

Hi,

You may have to replace some CINT shorcuts by their C++ equivalent. For example:if (file_exist) { TFile *file = TFile::Open("myfile.root"); TTree *tree; file->GetObject("mytree",tree); if (tree==0) { cerr << "tree not found\n"; return; } tree->Draw("myval>>myhisto"); TH1F *h; gDirectory->GetObject("myhisto",h); h->Fit("gaus"); // and since gaus is a builtin TF1: TF1 *g = (TF1*)gROOT->GetListOfFunctions()->FindObject("gaus"); }Cheers,
Philippe

I see. Being new to ROOT (and C++ for that matter) I didn’t really notice the difference between CINT and C++ itself. Thanks!