User Tools

Site Tools


root:pyroot_ttree

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:pyroot_ttree [2017/06/27 13:23] – [Scanning a TTree] iwnroot:pyroot_ttree [2018/02/28 14:03] (current) – [TChain] iwn
Line 1: Line 1:
 +[[root:root|Getting started with ROOT]] -> [[root:pyroot|Useful pyROOT snippets]] → [[root:pyroot_ttree|Handling TTree trees]]
 +
 ====== Handling TTree trees ====== ====== Handling TTree trees ======
 +
 +[[https://root.cern.ch/doc/master/classTTree.html|Here]] is the class reference for ''TTree''.
  
 [[root:example_TTree|Here]] is one file containing multiple examples. [[root:example_TTree|Here]] is one file containing multiple examples.
Line 23: Line 27:
 tree = TTree("tree_name", "tree title") tree = TTree("tree_name", "tree title")
  
-# create 1 dimensional float arrays as fill variables, in this way the float array serves +# create 1 dimensional float arrays as fill variables, in this way the float 
-# as a pointer which can be passed to the branch+array serves as a pointer which can be passed to the branch
 px  = array('d',[0]) px  = array('d',[0])
 phi = array('d',[0]) phi = array('d',[0])
Line 52: Line 56:
 </code> </code>
  
-Note that ''file.Write()'' will write all objects (trees, histograms, graphs, ...) that are still opened. Instead, you can write an object, in this case a ''TTree'', with the [[https://root.cern.ch/doc/master/classTTree.html#af6f2d9ae4048ad85fcae5d2afa05100f|Write() method]]:+Note that ''file.Write()'' will write all objects (trees, histograms, graphs, ...) that are still opened. Instead, you can write a individual object, in this case a ''TTree'', with the [[https://root.cern.ch/doc/master/classTTree.html#af6f2d9ae4048ad85fcae5d2afa05100f|Write() method]]:
 <code python> <code python>
 tree.Write() tree.Write()
Line 98: Line 102:
 <code python> <code python>
 for event in tree: for event in tree:
-    print tree.px+    print event.px
 </code> </code>
 If you also need the event's index: If you also need the event's index:
 <code python> <code python>
 for i, event in enumerate(tree): for i, event in enumerate(tree):
-    print i, tree.px+    print i, event.px
 </code> </code>
- 
  
 ===== Making a histogram with TTree ===== ===== Making a histogram with TTree =====
Line 154: Line 157:
  
  
-===== Scanning a TTree =====+===== Scanning the values of a TTree =====
  
 Print the values of some branches in columns with ''TTree::Scan'': Print the values of some branches in columns with ''TTree::Scan'':
Line 160: Line 163:
 tree.Scan("run:lumi:event") tree.Scan("run:lumi:event")
 tree.Scan("run:lumi:event","pt1>30 && pt2>30") # with some selections tree.Scan("run:lumi:event","pt1>30 && pt2>30") # with some selections
 +tree.Scan("run:lumi:event","pt1>30 && pt2>30","colsize=8") # with some option
 </code> </code>
  
-The [[https://root.cern.ch/doc/master/classTTreePlayer.html#aa0149b416e4b812a8762ec1e389ba2db|documentation of TTreePlayer::Scan()]] has more information on print options, formatting etc.+The [[https://root.cern.ch/doc/master/classTTreePlayer.html#aa0149b416e4b812a8762ec1e389ba2db|documentation of TTreePlayer::Scan()]] has more information on print options (column sizedecimal precision, ...) and formatting.
  
-To write the output directly to a file:+To write the output directly to a file (which gets overwritten every time):
 <code python> <code python>
 tree.GetPlayer().SetScanRedirect(True) tree.GetPlayer().SetScanRedirect(True)
Line 170: Line 174:
 tree.Scan("run:lum:evt","pt1>30 && pt2>30") tree.Scan("run:lum:evt","pt1>30 && pt2>30")
 </code> </code>
 +
 +
 +
 +
 +===== TChain =====
 +
 +If you have a samples split into many files, each containing the same tree, you can either add the files into one big one in the command line with ''hadd'' or load them in a ''TChain'' in you analysis code.
 +
 +With ''hadd'' in the command line:
 +<code bash>
 +hadd sample.root sample_1.root sample_2.root sample_3.root
 +</code>
 +
 +With ''TChain'':
 +<code python>
 +chain = TChain("tree_name")
 +chain.Add("sample_1.root")
 +chain.Add("sample_2.root")
 +chain.Add("sample_3.root")
 +for event in chain:
 +  print event.px
 +</code>
 +Looping over the events in a chain is similar as for [[root:pyroot_ttree#looping_over_a_ttree|trees]]. Note it's also possible to use a glob wildcard: ''chain.Add("sample_*.root")''
root/pyroot_ttree.1498562583.txt.gz · Last modified: 2017/06/27 13:23 by iwn