Hi Ben, I do not know if the following are typos or actual slight errors, so I'll just go over them to make sure. Your original code says: TFile file("adc.root"); TTree *tree = new TTree("tree"); This does NOT load the tree in the file. What this 2 lines of code do is to open the file "adc.root" and create a new empty TTree name tree. So your second attempt is better TFile file("adc.root"); TTree *tree = (TTree*)file->Get("tree"); Except that in compiled C++ (as opposed to interpreted CINT), you have to pay attention to whether your variable are object or pointer to objects. When a variable is an object (as in your line: TFile file("adc.root");) to access the object you need to use the dot ('.') notation. Thus the proper syntax is: TFile file("adc.root"); TTree *tree = (TTree*)file.Get("tree"); When a variable is a pointer to an object, to access the object you need to use the arrow ('->') notation. Thus a proper alternative is: TFile *file = new TFile("adc.root"); TTree *tree = (TTree*)file->Get("tree"); Note that declaring your file object as TFile file("adc.root"); or TFile *file = new TFile("adc.root"); as strong implication on the lifetime of the object. In the first case, the object will be deleted as soon as the function it is declared in ends. In the second case, the object will be deleted only when you explicitly call: "delete file". Cheers, Philippe. -----Original Message----- From: Ben Morgan [mailto:B.Morgan@sheffield.ac.uk] Sent: Tuesday, November 13, 2001 9:59 AM To: Philippe Canal Cc: roottalk Subject: RE: [ROOT] Reading 2D arrays from a Tree Thanks Philippe, I should have remembered, I'm using ROOT v3.01/06 on an Intel PC running Red Hat 6.2. Yes, Glen's codes run fine when run as macros in an interactive session, but simply cutting and pasting them into a very simplistic C++ code like: #include<iostream.h> #include<iomanip.h> #include<fstream.h> #include<stdio.h> #include <stdlib.h> #include<math.h> //ROOT Headers #include "TROOT.h" #include "TFile.h" #include "TNetFile.h" #include "TRandom.h" #include "TTree.h" #include "TBranch.h" #include "TClonesArray.h" #include "TStopwatch.h" int main(int argc, char **argv) { Float_t na[10][10]; TFile file("adc.root"); TTree *tree = new TTree("tree"); TBranch *b = tree->GetBranch("adc"); TLeaf *l = b->GetLeaf("adc"); tree->SetBranchAddress("adc",&na); for (Int_t i=0; i<150; i++) { b->GetEvent(i); printf("na[1][1] = %f, ",na[1][1]); } return 0; } This doesn't compile. The problem seems to be with the line: TTree *tree = new TTree("tree"); This doesn't appear in Glen's code, but something like this appears to be neccessary in a C++ program otherwise the compiler complains about 'tree' being undefined in the next line, i.e. leaving this line out produces: twodread.C: In function `int main(int, char **)': twodread.C:25: `tree' undeclared (first use this function) on compilation. Equally, using a line like TTree *tree = (TTree*)file->Get("tree") as used in the examples of reading trees in the manual doesn't compile properly either. Hope this makes things clearer, Thanks, Ben Morgan. -- ------------------------------------------------------------------------------- Mr. Ben Morgan Postgraduate Student University of Sheffield Department of Physics & Astronomy Hicks Building Hounsfield Road Sheffield S3 7RH -------------------------------------------------------------------------------
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:08 MET