This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| root:pyroot_ttree [2017/06/22 17:49] – iwn | root:pyroot_ttree [2018/02/28 14:03] (current) – [TChain] iwn | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | [[root: | ||
| + | |||
| ====== Handling TTree trees ====== | ====== Handling TTree trees ====== | ||
| + | |||
| + | [[https:// | ||
| [[root: | [[root: | ||
| Line 23: | Line 27: | ||
| tree = TTree(" | tree = TTree(" | ||
| - | # 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 |
| px = array(' | px = array(' | ||
| phi = array(' | phi = array(' | ||
| Line 52: | Line 56: | ||
| </ | </ | ||
| - | Note that '' | + | Note that '' |
| <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 |
| </ | </ | ||
| If you also need the event' | If you also need the event' | ||
| <code python> | <code python> | ||
| for i, event in enumerate(tree): | for i, event in enumerate(tree): | ||
| - | print i, tree.px | + | print i, event.px |
| </ | </ | ||
| - | + | ===== Making | |
| - | ===== Filling | + | |
| See [[root: | See [[root: | ||
| + | |||
| + | |||
| ===== Adding and saving a branch to a TTree in a TFile ===== | ===== Adding and saving a branch to a TTree in a TFile ===== | ||
| Line 149: | Line 154: | ||
| branch.Fill() | branch.Fill() | ||
| </ | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Scanning the values of a TTree ===== | ||
| + | |||
| + | Print the values of some branches in columns with '' | ||
| + | <code python> | ||
| + | tree.Scan(" | ||
| + | tree.Scan(" | ||
| + | tree.Scan(" | ||
| + | </ | ||
| + | |||
| + | The [[https:// | ||
| + | |||
| + | To write the output directly to a file (which gets overwritten every time): | ||
| + | <code python> | ||
| + | tree.GetPlayer().SetScanRedirect(True) | ||
| + | tree.GetPlayer().SetScanFileName(" | ||
| + | tree.Scan(" | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== 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 '' | ||
| + | |||
| + | With '' | ||
| + | <code bash> | ||
| + | hadd sample.root sample_1.root sample_2.root sample_3.root | ||
| + | </ | ||
| + | |||
| + | With '' | ||
| + | <code python> | ||
| + | chain = TChain(" | ||
| + | chain.Add(" | ||
| + | chain.Add(" | ||
| + | chain.Add(" | ||
| + | for event in chain: | ||
| + | print event.px | ||
| + | </ | ||
| + | Looping over the events in a chain is similar as for [[root: | ||