Script behaves differently when interpreted than compiled

Greetings all,

Following on from Rene’s advice I have been using ACLiC to hunt down coding problems with various scripts that I am using.

I have a series of scripts for sorting data from our experiments. For a long data run this sort is the most time consuming part of data analysis and so running it in compiled form is likely to present substantial time savings.

The script runs without problems in interpreted mode but it fails during compilation.

I have produced a short test script which illustrates the problem and the smallest available data file that I could make to work with it. The TTree in the attached file is unimaginatively named “TREE”.

[code]#include “TCanvas.h”
#include “TF1.h”
#include “TH1F.h”
#include “TFile.h”
#include “TTree.h”

void TestSort(){
TFile *f1=TFile::Open( “Test.root”, “READ” );
TTree T = (TTree)f1->Get(“TREE”); //EDITED AFTER UPLOAD TO REPLACE UNDECLARED IDENTIFIER “tree_name”
TCanvas *c1 = new TCanvas(“c1”,“Sorting canvas”,10,10,900,700);
TH1F *spectrum = new TH1F(“spectrum”, “”,4000,10,4010);
c1->cd(1);
TREE->Draw(“X.GE4>>spectrum”); //Error is generated for this line
spectrum->SetLineColor(2);
}[/code]

Any guidance you can give me on what I am doing wrong would be greatly appreciated.

Problem found with

ROOT 5.24/00 (trunk@29257, Jun 30 2009, 09:23:51 on win32)
CINT/ROOT C/C++ Interpreter version 5.17.00, Dec 21, 2008

and duplicated in

ROOT 5.18/00b (branches/v5-18-00-patches@22563, Mar 20 2009, 00:45:00 on linuxx8664gcc)
CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008
TestSort.C (444 Bytes)
Test.root (15.4 KB)

Hi,

See your modified (and commented) code:

[code]#include “TCanvas.h”
#include “TF1.h”
#include “TH1F.h”
#include “TFile.h”
#include “TTree.h”

void TestSort() {
TFile *f1=TFile::Open( “Test.root”, “READ” );
TTree T = (TTree)f1->Get(“TREE”); // <---- TREE is the tree name
TCanvas *c1 = new TCanvas(“c1”,“Sorting canvas”,10,10,900,700);
TH1F *spectrum = new TH1F(“spectrum”, “”,4000,10,4010);
c1->cd(1);
T->Draw(“X.GE4>>spectrum”); // <---- use T (pointer on the TTree) instead of TREE (which is its name)…
spectrum->SetLineColor(2);
}[/code]
Hope this helps.

Cheers, Bertrand.

Dear Bertrand,

Thank you for the advice.

Your solution does work for this short form script - but when I tried the same solution on my longer form script (which does this 20 times) it fails to compile.

I’ll see if I can come up with a longer script to illustrate the broader problem.

Regards

[quote] but when I tried the same solution on my longer form script (which does this 20 times) it fails to compile. [/quote]What is the compilation error? What is the line of code mentioned in the error?

Cheers,
Philippe

Dear Bertrand and Philippe,

Thank you for your help - my problem is now solved.

The errors I was getting were variations on the following

[quote]TestSort_C_ACLiC_dict.cxx
C:\Fit_test_folder_ru98\TestSort.C(13) : error C2065: ‘TREE’ : undeclared identifier
C:\Fit_test_folder_ru98\TestSort.C(13) : error C2227: left of ‘->Draw’ must point to class/struct/union/generic type
type is '‘unknown-type’'
Error in : Compilation failed![/quote]

The problem would actually occur 60 times in the larger script (my estimate of 20 was way out).

The issue did not arise because of the snippet of code that I posted - though the included the lines that the compiler tripped over.

While trying to produce a longer running code snippet that illustrated the issue I found that the value “TREE” had been assigned to a TString at the beginning of the code. When that assignment was removed the code behaved the same in interpreted or compiled versions.

Since no problems arise when I remove the TString assignment I now have a running script that behaves the same whether it is interpreted or compiled.

Regards