This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| root:pyroot_tfile [2017/06/13 09:37] – created iwn | root:pyroot_tfile [2017/09/05 10:37] (current) – [Handling TFile files] iwn | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | [[root: |
| - | ===== Filling a histogram | + | ====== Handling TFile files ====== |
| - | Simple enough: | + | Class references for: |
| + | * '' | ||
| + | * '' | ||
| + | ===== Adding a directory to a TFile ===== | ||
| - | <code python> | + | Here is a basic example creating a file with on directory. If you want to create and save an object in this directory, e.g. a tree or histogram, use '' |
| - | hist = TH1D(" | + | |
| - | for i in xrange(10000): | + | |
| - | hist.Fill(gRandom.Gauss()) | + | |
| - | </ | + | |
| - | With some weight: | + | <file python |
| - | <code python> | + | from ROOT import TFile |
| - | weight = gRandom.Gauss(1, | + | |
| - | hist.Fill(gRandom.Gauss(), | + | |
| - | </ | + | |
| - | ===== Filling a histogram from a tree ===== | + | file = TFile(" |
| + | |||
| + | # make directory if it does not exist | ||
| + | dir = file.GetDirectory(" | ||
| + | if not dir: | ||
| + | print ">>> | ||
| + | dir = file.mkdir(" | ||
| + | |||
| + | # make this directory the current one: everything you write will be saved here | ||
| + | dir.cd() | ||
| - | The classic way would be | + | # make tree, histogram, ... |
| - | <code python> | + | |
| - | hist = TH1D("hist_name","hist title", | + | file.Write("", |
| - | for event in tree: | + | file.Close() |
| - | hist.Fill(tree.px) | + | </file> |
| - | </code> | + | |
| - | But the simplest and most powerful way to draw some variable into a histogram, is with the [[https:// | ||
| - | <code python> | ||
| - | hist = TH1D(" | ||
| - | tree.Draw(" | ||
| - | </ | ||
| - | Note that the histogram has been saved into '' | ||
| - | You can also use mathematical expressions: | + | ===== gDirectory ===== |
| - | <code python> | + | |
| - | tree.Draw(" | + | |
| - | tree.Draw(" | + | |
| - | </ | + | |
| - | You can apply some selections using variable, available in the tree's branches: | + | Note that '' |
| - | <code python> | + | |
| - | tree.Draw(" | + | |
| - | </ | + | |
| - | You can apply some selections and/or a weight, if a weight variable is available in the tree: | + | <file python |
| - | <code python> | + | from ROOT import TFile, TTree, TH1F, gDirectory |
| - | tree.Draw(" | + | |
| - | tree.Draw(" | + | |
| - | </ | + | |
| - | An equivalent method is creating the histogram in the '' | + | def printGDirectory(): |
| - | <code python> | + | print ">> |
| - | from ROOT import | + | print ">>> |
| - | ... | + | |
| - | tree.Draw("pt >> | + | print ">> |
| - | hist = gDirectory.Get(" | + | gDirectory.ls() |
| - | </ | + | |
| - | It's also possible to pass some option you would normally use in "TH1::Draw()". It is passed as the third string. | + | print "\ndefault" |
| - | <code python> | + | printGDirectory() |
| - | tree.Draw("px >> h1(100, | + | |
| - | tree.Draw(" | + | |
| - | tree.Draw("px >> | + | print ">> |
| - | tree.Draw("px >> | + | file = TFile(" |
| - | </code> | + | tree = TTree(" |
| + | hist = TH1F(" | ||
| + | dir1 = file.mkdir("dir1") | ||
| + | printGDirectory() | ||
| + | |||
| + | print ">>> | ||
| + | gDirectory.Delete("hist" | ||
| + | printGDirectory() | ||
| + | |||
| + | print ">>> dir1.cd()" | ||
| + | dir1.cd() | ||
| + | printGDirectory() | ||
| + | |||
| + | print ">>> | ||
| + | file.Close() | ||
| + | printGDirectory() | ||
| + | </file> | ||
| - | __Protip__: This is very useful for quick drawing and comparing, for example in the '' | ||
| - | <code C++> | ||
| - | $ root -l tree.root | ||
| - | [1] tree-> | ||
| - | [2] tree-> | ||
| - | </ | ||