User Tools

Site Tools


root:loadtext

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
root:loadtext [2010/11/09 16:33] – created nchiaproot:loadtext [2010/11/12 15:09] nchiap
Line 2: Line 2:
 To read data from a text file into a tree all you need are the following two lines To read data from a text file into a tree all you need are the following two lines
 <code C++> <code C++>
-  TTree *myTree = new TTree("ntuple","data from ascii file");+  TTree *myTree = new TTree("mytree","data from ascii file");
   Long64_t nlines = myTree->ReadFile(filename,"col1:col2:col3");   Long64_t nlines = myTree->ReadFile(filename,"col1:col2:col3");
 +</code>
 +
 +
 +=== Dynamic Branche Names ===
 +
 +If you do not give the structure, the first line of your file must contain the tree definition
 +<code text input.csv>
 +bin:hist1:hist2
 +1 12 13
 +[...]
 +</code>
 +
 +To use this tree you then need to get the branch names
 +<code C++>
 +  TTree *myTree = new TTree("mytree","data from ascii file");
 +  Long64_t nlines = myTree->ReadFile(filename);
 + 
 +  TString col0Name = T->GetListOfBranches()->At(0)->GetName();
 +  TString col1Name = T->GetListOfBranches()->At(1)->GetName();
 +  TString col2Name = T->GetListOfBranches()->At(2)->GetName();
 +
 +  Float_t bin,hist1,hist2;
 +  T->SetBranchAddress(col0Name.Data(),&bin);
 +  T->SetBranchAddress(col1Name.Data(),&hist1);
 +  T->SetBranchAddress(col2Name.Data(),&hist2);
 +</code>
 +
 +=== Dynamic Tree Structure ===
 +You can extend this idea to plot all histograms in an unknown tree.
 +The following code reads a tree form a datafile with a arbitrary number of columns
 +and creats a histogram for each of them.
 +
 +<code C++>
 </code> </code>
root/loadtext.txt · Last modified: 2010/11/15 17:17 by nchiap