This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| root:loadtext [2010/11/09 16:33] – created nchiap | root:loadtext [2010/11/15 17:17] (current) – 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(" | + | TTree *myTree = new TTree(" |
| Long64_t nlines = myTree-> | Long64_t nlines = myTree-> | ||
| + | </ | ||
| + | |||
| + | |||
| + | === 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: | ||
| + | 1 12 13 | ||
| + | [...] | ||
| + | </ | ||
| + | |||
| + | To use this tree you then need to get the branch names | ||
| + | <code C++> | ||
| + | TTree *myTree = new TTree(" | ||
| + | Long64_t nlines = myTree-> | ||
| + | |||
| + | TString col0Name = T-> | ||
| + | TString col1Name = T-> | ||
| + | TString col2Name = T-> | ||
| + | |||
| + | Float_t bin, | ||
| + | T-> | ||
| + | T-> | ||
| + | T-> | ||
| + | </ | ||
| + | |||
| + | === 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 creates a histogram for each of them. | ||
| + | |||
| + | <code C++> | ||
| + | /** Load Data into Tree **/ | ||
| + | TTree *T = new TTree(" | ||
| + | Long64_t nlines = T-> | ||
| + | |||
| + | /** Attache memory to tree **/ | ||
| + | Int_t nCols = T-> | ||
| + | std:: | ||
| + | for (Int_t i=0; i < nCols; i++) { | ||
| + | TString colName = T-> | ||
| + | T-> | ||
| + | } | ||
| + | |||
| + | /** Calculate binning **/ | ||
| + | Int_t nentries = (Int_t)T-> | ||
| + | |||
| + | T-> | ||
| + | Float_t first = entry[0]; | ||
| + | T-> | ||
| + | Float_t second = entry[0]; | ||
| + | Float_t step = second-first; | ||
| + | first = first - step/2; | ||
| + | Float_t last = first+step*(nlines); | ||
| + | | ||
| + | /** Create Histograms **/ | ||
| + | TH1F *h; | ||
| + | TObjArray histograms; | ||
| + | for (Int_t i = 0; i < nCols; i++) { | ||
| + | TString hName = Form(" | ||
| + | if (i == 1) { | ||
| + | h = new TH1F(hName, | ||
| + | } else { | ||
| + | h = new TH1F(hName,"", | ||
| + | } | ||
| + | histograms.AddLast(h); | ||
| + | } | ||
| + | |||
| + | /** Fill Histograms **/ | ||
| + | for (Int_t i=0; | ||
| + | T-> | ||
| + | i++; | ||
| + | for (Int_t col = 1; col < nCols; col++) { | ||
| + | h = dynamic_cast< | ||
| + | h-> | ||
| + | } | ||
| + | } | ||
| </ | </ | ||