User Tools

Site Tools


root:ttreedraw

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:ttreedraw [2012/07/13 11:09] burscheroot:ttreedraw [2013/06/14 11:41] (current) nchiap
Line 1: Line 1:
-To apply a function in TTree::Draw it must be known to CINT.+====== TTree::Draw() ====== 
 + 
 +===== Basic Usage ===== 
 + 
 +The ''Draw()''-Method of TTrees is very useful tool to quickly create histograms. 
 +The following code creates a histogram with the default settings. 
 +<code c++> 
 +TFile file("myfile.root"); 
 +TTree *tree = file.Get("mytree"); 
 +tree->Draw("myvar"); 
 +</code> 
 + 
 +Alternatively the name of the histogram to be created can be given or the data can be added to an existing histogram. 
 +<code c++> 
 +tree->Draw("myvar>>myhist(10,0,100)");     // create a new histogram *myhist* with 10 bins between 0 and 100 
 +TH1F *hist = gPad->GetPrimitive("myhist"); // create a pointer to the new histogram 
 +tree->Draw("myvar>>+myhist");              // add additional entries to *myhist* 
 +</code> 
 + 
 +For more information have a look at [[http://root.cern.ch/root/html/TTree#TTree:Draw@2|the official TTree::Draw documentation]]. 
 + 
 +===== Useful Tricks ===== 
 + 
 +==== Using a Custom Function in TTree::Draw ==== 
 +You can define a custom function and use it in the expression of TTree::Draw
 + 
 +To apply such a function it must be known to CINT.
 This can be done with ProcessLineSync This can be done with ProcessLineSync
  
Line 41: Line 67:
 } }
 </file> </file>
 +
 +===== Pitfalls =====
 +
 +==== Unable to add to existing histograms ====
 +Adding data to an existing histogram will fail, if any file was opened after creating the histogram!
 +TTree::Draw() searches for the named histogram in the active directory - and this is changed when opening a file.
 +
 +To avoid this make sure to load the input files before you create any histograms or
 +create a storage file and switch back there after opening any files:
 +<code c++>
 +TFile tmp("tmp.root", "recreate");
 +float bins[] = {0, 10, 20, 40, 80}; 
 +TH1F  myhist("myhist", "myhist", 4, bins)
 +
 +TFile file("myinput.root");
 +TTree *tree = file.Get("mytree");
 +
 +tmp.cd()
 +tree->Draw("myvar>>+myhist");
 +</code>
  
  
root/ttreedraw.1342170592.txt.gz · Last modified: 2012/07/13 11:09 by bursche