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
root:pyroot_tfile [2017/06/13 09:37] – created iwnroot:pyroot_tfile [2017/09/05 10:37] (current) – [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/classTDirectory.html|TDirectory]]'', 
 +  * ''[[https://root.cern.ch/doc/master/classTFile.html|TFile]]''
 +===== 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 directorye.g. a tree or histogramuse ''dir.cd()''Pay attention to the order of creating an object, changing directory (''cd()''and writing the object!
-hist = TH1D("hist_name","hist title",100,0,100) +
-for i in xrange(10000)+
-    hist.Fill(gRandom.Gauss()) +
-</code>+
  
-With some weight: +<file python addDirectory.py
-<code python> +from ROOT import TFile
-weight = gRandom.Gauss(1,0.1) +
-hist.Fill(gRandom.Gauss(),weight) +
-</code>+
  
-===== Filling a histogram from a tree =====+file TFile("tree.root",'update'
 +  
 +# make directory if it does not exist 
 +dir file.GetDirectory("dir"
 +if not dir: 
 +    print ">>> created dir" 
 +    dir file.mkdir("dir"
 +  
 +# 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",100,0,100+file.Write("",TFile.kOverwrite
-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://root.cern.ch/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45|Draw]] function: 
-<code python> 
-hist = TH1D("hist_name","hist title",100,0,100) 
-tree.Draw("px >> hist_name") 
-</code> 
-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: +===== gDirectory =====
-<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: +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") +
-</code>+
  
-You can apply some selections and/or a weight, if a weight variable is available in the tree: +<file python gDirectory.py
-<code python+from ROOT import TFileTTree, TH1F, gDirectory
-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]]): +def printGDirectory(): 
-<code python> +    print ">>> gDirectory.GetName()\n%s" % gDirectory.GetName() 
-from ROOT import gDirectory +    print ">>> gDirectory.pwd()" 
-... +    gDirectory.pwd(
-tree.Draw("pt >> h(100,0,100)") +    print ">>> gDirectory.ls()" 
-hist = gDirectory.Get("h"# will be a TH1F object +    gDirectory.ls() 
-</code>+    print
  
-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,0,100)","","E2"+
-tree.Draw("py >> h2(100,0,100)","","E2 SAME")+
  
-tree.Draw("px >> h1(100,0,100)","abs(eta)>2","E2") +print ">>> creating a file with some contents" 
-tree.Draw("px >> h2(100,0,100)","abs(eta)<2","E2 SAME") +file = TFile("test.root","recreate"
-</code>+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.1497339438.txt.gz · Last modified: 2017/06/13 09:37 by iwn