User Tools

Site Tools


root:pyroot_tfile

Differences

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

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
root:pyroot_tfile [2017/06/13 09:37] – created iwnroot:pyroot_tfile [2017/09/05 10:35] – [Handling TFile files] iwn
Line 1: Line 1:
-====== Handling TH1 histograms ======+[[root:root|Getting started with ROOT]] -> [[root:pyroot|Useful pyROOT snippets]] → [[root:pyroot_tfile|Handling TFile files]]
  
-===== Filling a histogram =====+====== Handling TFile files ======
  
-Simple enough:+Class references for: 
 +  * ''[[https://root.cern.ch/doc/master/classTFile.html|TDirectory]]'', 
 +  * ''[[https://root.cern.ch/doc/master/classTFile.html|TFile]]''.
  
-<code python> +===== Adding a directory to a TFile =====
-hist TH1D("hist_name","hist title",100,0,100) +
-for i in xrange(10000): +
-    hist.Fill(gRandom.Gauss()) +
-</code>+
  
-With some weight: +Here is a basic example creating a file with on directoryIf you want to create and save an object in this directorye.ga tree or histogram, use ''dir.cd()''. Pay attention to the order of creating an objectchanging directory (''cd()''and writing the object!
-<code python> +
-weight = gRandom.Gauss(1,0.1) +
-hist.Fill(gRandom.Gauss(),weight) +
-</code>+
  
-===== Filling a histogram from a tree =====+<file python addDirectory.py> 
 +from ROOT import TFile
  
-The classic way would be +file TFile("tree.root",'update'
-<code python> +  
-hist TH1D("hist_name","hist title",100,0,100+# make directory if it does not exist 
-for event in tree+dir = file.GetDirectory("dir") 
-    hist.Fill(tree.px+if not dir
-</code>+    print ">>> created dir" 
 +    dir = file.mkdir("dir"
 +  
 +# make this directory the current one: everything you write will be saved here 
 +dir.cd()
  
-But the simplest and most powerful way to draw some variable into a histogram, is with the [[https://root.cern.ch/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45|Draw]] function: +# make tree, histogram, ... 
-<code python> +         
-hist = TH1D("hist_name","hist title",100,0,100+file.Write("",TFile.kOverwrite
-tree.Draw("px >> hist_name"+file.Close() 
-</code> +</file>
-Note that the histogram has been saved into ''ROOT'''s working memory and is therefore accessible via its name in the ''Draw'' function, ''"hist_name"'' in this example.+
  
-You can also use mathematical expressions: 
-<code python> 
-tree.Draw("2*sqrt(px*px+py*py) >> hist_name") 
-tree.Draw("abs(eta) >> hist_name") 
-</code> 
  
-You can apply some selections using variable, available in the tree's branches: +===== gDirectory =====
-<code python> +
-tree.Draw("px >> hist_name","pt>20 && E>20"+
-</code>+
  
-You can apply some selections and/or a weight, if a weight variable is available in the tree: +Note that ''gDirectory'' is the current directory containing all newly created saved objects, or those saved to this directoryIf you just opened a filethis will be your current directory until you close it or use ''cd()'' on some ''TDirectory'' or other ''TFile''Test the behavior with this macro:
-<code python> +
-tree.Draw("px >> hist_name","(pt>20 && E>20)*weight"+
-tree.Draw("px >> hist_name","weight"+
-</code>+
  
-An equivalent method is creating the histogram in the ''Draw'' method. If you need the ''TH1F'' object, you can still get it with ''gDirectory'' (see [[root:pyroot#gDirectory|below]]): +<file python gDirectory.py
-<code python> +from ROOT import TFileTTree, TH1F, gDirectory
-from ROOT import gDirectory +
-... +
-tree.Draw("pt >> h(100,0,100)"+
-hist = gDirectory.Get("h") # will be a TH1F object +
-</code>+
  
-It's also possible to pass some option you would normally use in "TH1::Draw()". It is passed as the third string. +def printGDirectory(): 
-<code python> +    print ">>> gDirectory.GetName()\n%s" % gDirectory.GetName() 
-tree.Draw("px >> h1(100,0,100)","","E2") +    print ">>> gDirectory.pwd()" 
-tree.Draw("py >> h2(100,0,100)","","E2 SAME")+    gDirectory.pwd(
 +    print ">>> gDirectory.ls()" 
 +    gDirectory.ls() 
 +    print
  
-tree.Draw("px >> h1(100,0,100)","abs(eta)>2","E2") +print "\ndefault" 
-tree.Draw("px >> h2(100,0,100)","abs(eta)<2","E2 SAME") +printGDirectory(
-</code>+ 
 +print ">>> creating a file with some contents" 
 +file = TFile("test.root","recreate"
 +tree = TTree("tree","tree"
 +hist = TH1F("hist","hist",100,0,100) 
 +dir1 = file.mkdir("dir1"
 +printGDirectory() 
 + 
 +print ">>> gDirectory.Delete(\"hist\")" 
 +gDirectory.Delete("hist"
 +printGDirectory() 
 + 
 +print ">>> dir1.cd()" 
 +dir1.cd() 
 +printGDirectory() 
 + 
 +print ">>> file.Close()" 
 +file.Close() 
 +printGDirectory(
 +</file>
  
-__Protip__: This is very useful for quick drawing and comparing, for example in the ''ROOT'' command line. 
-<code C++> 
-$ root -l tree.root 
-[1] tree->Draw("px >> h1(100,0,100)","","E2") 
-[2] tree->Draw("py >> h2(100,0,100)","","E2 SAME") 
-</code> 
root/pyroot_tfile.txt · Last modified: 2017/09/05 10:37 by iwn