User Tools

Site Tools


root:loadtext

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
root:loadtext [2010/11/10 12:17] nchiaproot:loadtext [2010/11/15 17:17] (current) nchiap
Line 6: Line 6:
 </code> </code>
  
 +
 +=== Dynamic Branche Names ===
  
 If you do not give the structure, the first line of your file must contain the tree definition If you do not give the structure, the first line of your file must contain the tree definition
Line 15: Line 17:
  
 To use this tree you then need to get the branch names To use this tree you then need to get the branch names
-<code c++>+<code C++>
   TTree *myTree = new TTree("mytree","data from ascii file");   TTree *myTree = new TTree("mytree","data from ascii file");
   Long64_t nlines = myTree->ReadFile(filename);   Long64_t nlines = myTree->ReadFile(filename);
Line 27: Line 29:
   T->SetBranchAddress(col1Name.Data(),&hist1);   T->SetBranchAddress(col1Name.Data(),&hist1);
   T->SetBranchAddress(col2Name.Data(),&hist2);   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 creates a histogram for each of them.
 +
 +<code C++>
 +        /** Load Data into Tree **/
 +        TTree *T = new TTree("myT","data from ascii file");
 +        Long64_t nlines = T->ReadFile(filename);
 +
 +        /** Attache memory to tree **/
 +        Int_t nCols = T->GetListOfBranches()->GetEntries();
 +        std::vector<Float_t> entry(nCols, 0);
 +        for (Int_t i=0; i < nCols; i++) {
 +          TString colName = T->GetListOfBranches()->At(i)->GetName();
 +          T->SetBranchAddress(colName.Data(),&(entry[i]));
 +        }
 +
 +        /** Calculate binning **/
 +        Int_t nentries = (Int_t)T->GetEntries();
 +
 +        T->GetEntry(0);
 +        Float_t first = entry[0];
 +        T->GetEntry(1);
 +        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("hist%d",i);
 +          if (i == 1) {
 +            h = new TH1F(hName,histTitle,nlines,first,last);
 +          } else {
 +            h = new TH1F(hName,"",nlines,first,last);
 +          }
 +          histograms.AddLast(h);
 +        }
 +
 +        /** Fill Histograms **/
 +        for (Int_t i=0;i<nentries;) {  // increment inside loop
 +          T->GetEntry(i);
 +          i++;
 +          for (Int_t col = 1; col < nCols; col++) {
 +                  h = dynamic_cast<TH1F*>(histograms[col]);
 +                  h->SetBinContent(i, entry[col]);
 +          }
 +        }       
 </code> </code>
root/loadtext.1289387842.txt.gz · Last modified: 2010/11/10 12:17 by nchiap