Reading & Analysing ascii data in TTree

Dear Root Gurus,

I’m trying to do some analysis on 4 ascii datafiles, each with 25 columns of data. Each column represents a variable. The variables in all 4 data files have identical names but differ only in value. I tried to read the data into a Tree using

TNtuple *ntuple = new TNtuple("name","title", v1:v2:v3:....v25); ntuple->Fill(v1,v2,v3,..v25);

as described in the tutorial basic.C. I get an error message which I traced to

i.e. it seems that the Fill function cannot read in more that 15 variables at a time. Is there any way around this?

my second question is that I decided to spit each datafile into 2 and read them all into a Tree, so that opening the Tree gives:

TFile** sbrt.root TFile* sbrt.root Key: TNtuple pbcl1;1 data from ascii file Key: TNtuple pbcl2;1 data from ascii file Key: TNtuple pben1;1 data from ascii file Key: TNtuple pben2;1 data from ascii file Key: TNtuple cccl1;1 data from ascii file Key: TNtuple cccl2;1 data from ascii file Key: TNtuple ccen1;1 data from ascii file Key: TNtuple ccen2;1 data from ascii file

but I want to carry out operations that involve simultaneous access to identical branches in different objects, for example, plotting a histogram of h->Draw("x"); where x = pbcl1.variable3/cccl1.variable3

I’ve tried using the AddFriend tutorial with no success. :frowning: Does anyone know how I can solve this problem?

Thanks.

You do

TNtuple *ntuple = new TNtuple("name","title", v1:v2:v3:....v25); //loop on each row of the ascii file, filling a vector v[25] and calling //for each row ntuple->Fill(v); or simply

TNtuple *ntuple = new TNtuple("name","title", v1:v2:v3:....v25); ntuple->ReadFile("file.dat");
Rene

Hi,

As Rene mentioned, you should not have to split into 2 TNtuple; either use the array interface or better yet use TTree::ReadFile.

Also, when you had 2 trees, the ‘right’ way to work with both was to use AddFriend. How did it fail for you? I am guessing that you may have had to disambiguate the leaf names.

Cheers,
Philippe.

Philippe,

Why do you recommend AddFriend when there is a trivial solution?

Rene

Hi Rene,

See additional comment in my previous post.

Cheers,
Philippe.

Thanks Rene,

I tried your suggestion w.r.t. filling the TNtuples and it works fine. Regarding the Addfriends…I’ve also got it to work. I think where I went wrong was in repeating the “Get” command

for both parent and friend, rather than just calling it once for the parent and then using AddFriend to get the friend.

Phillipe, I’m curious, you mentioned to Rene that there was a more trivial solution than using AddFriend. Were you referring to the hadd functionality?

cheers. :laughing:

[quote]Phillipe, I’m curious, you mentioned to Rene that there was a more trivial solution than using AddFriend. Were you referring to the hadd functionality?[/quote]Both Rene and I were thinking of TTree::ReadFile or filling the TNtuple with an array.

Cheers,
Philippe.

[quote=“pcanal”][quote]Both Rene and I were thinking of TTree::ReadFile or filling the TNtuple with an array.

Cheers,
Philippe.[/quote][/quote]
All right then. Thanks a lot.

Hi Rene, Phillipe,

I’m trying to take my code a step further to read in my data file with an arbitrary number of columns (in this example the data file has 100 columns). The code looks like this:

[quote] TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“test1.C”,"");
dir.ReplaceAll("/./","/");

//pencil beam classic
Float_t pc_bin,pc_Body_d,pc_Body_v,pc_Spine_d,pc_Spine_v;
Float_t pc_LungL_d,pc_LungL_v,pc_LungL2_d,pc_LungL2_v;
Float_t pc_LungR_d,pc_LungR_v;
Float_t pc_LungR2_d,pc_LungR2_v,pc_Heart_d,pc_Heart_v;
Float_t pc_Oesophagus_d,pc_Oesophagus_v,pc_RLungGTV_d,pc_RLungGTV_v;
Float_t pc_RLungITV_d,pc_RLungITV_v,pc_RLungPTV_d,pc_RLungPTV_v;
Float_t pc_RLungPTV2cm_d,pc_RLungPTV2cm_v;
//pencil beam enhanced
Float_t pe_bin,pe_Body_d,pe_Body_v,pe_Spine_d,pe_Spine_v;
Float_t pe_LungL_d,pe_LungL_v,pe_LungL2_d,pe_LungL2_v;
Float_t pe_LungR_d,pe_LungR_v;
Float_t pe_LungR2_d,pe_LungR2_v,pe_Heart_d,pe_Heart_v;
Float_t pe_Oesophagus_d,pe_Oesophagus_v,pe_RLungGTV_d,pe_RLungGTV_v;
Float_t pe_RLungITV_d,pe_RLungITV_v,pe_RLungPTV_d,pe_RLungPTV_v;
Float_t pe_RLungPTV2cm_d,pe_RLungPTV2cm_v;
//collapsed cone classic
Float_t cc_bin,cc_Body_d,cc_Body_v,cc_Spine_d,cc_Spine_v;
Float_t cc_LungL_d,cc_LungL_v,cc_LungL2_d,cc_LungL2_v;
Float_t cc_LungR_d,cc_LungR_v;
Float_t cc_LungR2_d,cc_LungR2_v,cc_Heart_d,cc_Heart_v;
Float_t cc_Oesophagus_d,cc_Oesophagus_v,cc_RLungGTV_d,cc_RLungGTV_v;
Float_t cc_RLungITV_d,cc_RLungITV_v,cc_RLungPTV_d,cc_RLungPTV_v;
Float_t cc_RLungPTV2cm_d,cc_RLungPTV2cm_v;
//collapsed cone enhanced
Float_t ce_bin,ce_Body_d,ce_Body_v,ce_Spine_d,ce_Spine_v;
Float_t ce_LungL_d,ce_LungL_v,ce_LungL2_d,ce_LungL2_v;
Float_t ce_LungR_d,ce_LungR_v;
Float_t ce_LungR2_d,ce_LungR2_v,ce_Heart_d,ce_Heart_v;
Float_t ce_Oesophagus_d,ce_Oesophagus_v,ce_RLungGTV_d,ce_RLungGTV_v;
Float_t ce_RLungITV_d,ce_RLungITV_v,ce_RLungPTV_d,ce_RLungPTV_v;
Float_t ce_RLungPTV2cm_d,ce_RLungPTV2cm_v;

// Int_t nlines = 0;
TFile *f = new TFile(“sbrt.root”,“RECREATE”);
// TH1F *h1 = new TH1F(“h1”,“x distribution”,100,0,400);
TNtuple *ntuple = new TNtuple(“sbrt”,“sbrt dose data”,
“pc_bin:pc_Body_d:pc_Body_v:pc_Spine_d:pc_Spine_v:
pc_LungL_d:pc_LungL_v:pc_LungL2_d:pc_LungL2_v:pc_LungR_d:
pc_LungR_v:pc_LungR2_d:pc_LungR2_v:pc_Heart_d:pc_Heart_v:
pc_Oesophagus_d:pc_Oesophagus_v:pc_RLungGTV_d:pc_RLungGTV_v:
pc_RLungITV_d:pc_RLungITV_v:pc_RLungPTV_d:pc_RLungPTV_v:
pc_RLungPTV2cm_d:pc_RLungPTV2cm_v:pe_bin:pe_Body_d:pe_Body_v:
pe_Spine_d:pe_Spine_v:pe_LungL_d:pe_LungL_v:pe_LungL2_d:
pe_LungL2_v:pe_LungR_d:pe_LungR_v:pe_LungR2_d:pe_LungR2_v:
pe_Heart_d:pe_Heart_v:pe_Oesophagus_d:pe_Oesophagus_v:
pe_RLungGTV_d:pe_RLungGTV_v:pe_RLungITV_d:pe_RLungITV_v:
pe_RLungPTV_d:pe_RLungPTV_v:pe_RLungPTV2cm_d:pe_RLungPTV2cm_v:
cc_bin:cc_Body_d:cc_Body_v:cc_Spine_d:cc_Spine_v:cc_LungL_d:
cc_LungL_v:cc_LungL2_d:cc_LungL2_v:cc_LungR_d:cc_LungR_v:
cc_LungR2_d:cc_LungR2_v:cc_Heart_d:cc_Heart_v:cc_Oesophagus_d:
cc_Oesophagus_v:cc_RLungGTV_d:cc_RLungGTV_v:cc_RLungITV_d:
cc_RLungITV_v:cc_RLungPTV_d:cc_RLungPTV_v:cc_RLungPTV2cm_d:
cc_RLungPTV2cm_v:ce_bin:ce_Body_d:ce_Body_v:ce_Spine_d:
ce_Spine_v:ce_LungL_d:ce_LungL_v:ce_LungL2_d:ce_LungL2_v:
ce_LungR_d:ce_LungR_v:ce_LungR2_d:ce_LungR2_v:ce_Heart_d:
ce_Heart_v:ce_Oesophagus_d:ce_Oesophagus_v:ce_RLungGTV_d:
ce_RLungGTV_v:ce_RLungITV_d:ce_RLungITV_v:ce_RLungPTV_d:
ce_RLungPTV_v:ce_RLungPTV2cm_d:ce_RLungPTV2cm_v”);

ntuple->ReadFile(“sbrt.dat”);

f->Write();
}[/quote]

but when I try to compile it, I get the error message;

[quote]Limitation: Statement is too long… (Statement being TNtuple *ntuple = new TNtuple(“name”,“title”, “pc_bin: … :ce_RLungPTV2cm_v”);)
cint: Security mode 0x7:0x2 *** Fatal error in interpreter… restarting interpreter ***[/quote]
How can I solve this problem?

thanks.

Hi,

You have 2 solutions, you could either compile your code via ACLiC (do .L myscript.C+) or create your string part by part: TString str = "...."; str.Append("...."); etc.

Cheers,
Philippe.

[quote=“pcanal”]Hi,

You have 2 solutions, you could either compile your code via ACLiC (do .L myscript.C+) or create your string part by part: TString str = "...."; str.Append("...."); etc.

Cheers,
Philippe.[/quote]

hi,

Thanks for the suggestions. The string approach worked :laughing:. the first approach - compiling with ACLiC did not work :cry: . I kept getting the error message

'cl' is not recognized as an internal or external command,operable program or batch file. Error: external preprocessing failed. (0) !!! removing pathname\filename_c_ACLiC_dict.cxx pathname\filename_c_ACLiC_dict.h !!! Error: c:\root\bin\rootcint: error loading headers... Error in <ACLiC>: Dictionary generation failed! Info in <ACLiC>: Invoking compiler to check macro's validity 'cl' is not recognized as an internal or external command,operable program or batch file.

Out of curiosity how can I solve this? I’m running root on window, could this be reason?

cheers

Hi,

You will need to install and setup the microsoft compiler (That is what ‘cl.exe’ is :slight_smile:). Microsoft offers a ‘free’ version of the C++ compiler on its web site.

Cheers,
Philippe.

[quote=“pcanal”]Hi,

You will need to install and setup the microsoft compiler (That is what ‘cl.exe’ is :slight_smile:). Microsoft offers a ‘free’ version of the C++ compiler on its web site.

Cheers,
Philippe.[/quote]

I run root from a cygwin module I installed along with c++ modules (in my c directory). so in my /usr/bin/ directory I have c++.exe, cpp.exe, g++.exe, gcc.exe compilers. typing “c++ -v” gives me the version details etc everything is current. So I’m not sure what the problem is.

Hello,

I am wondering if the TTree:ReadFile method is faster than manually looping over the lines in the ASCII file and filling the tree using an event class?

I am currently trying to read data from a text file into a TTree using the latter method, but am reading a very large file (1 million lines- i.e. 1 million events). Seems to be working okay but is taking several hours to complete on my local machine.

Would TTree:ReadFile be quicker?

Are you running your macro as (ACLiC) precompiled or as interpreted?
If “interpreted” … try to use it as “precompiled” (i.e. use ACLiC).