User Tools

Site Tools


root:pyroot_th1

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
Next revisionBoth sides next revision
root:pyroot_th1 [2017/06/22 12:05] – [Filling a histogram] iwnroot:pyroot_th1 [2017/09/01 16:26] iwn
Line 1: Line 1:
 +[[root:root|Getting started with ROOT]] -> [[root:pyroot|Useful pyROOT snippets]] → [[root:pyroot_th1|Handling TH1 histograms]]
 +
 ====== Handling TH1 histograms ====== ====== Handling TH1 histograms ======
  
-To use ''TH1D'' in ''python'', you can either do+===== Importing ROOT classes ===== 
 + 
 +To use ''TH1D'' in ''python'', you can either only import a ''TH1D'' class:
 <code python> <code python>
 from ROOT import TH1D from ROOT import TH1D
 hist = TH1D("hist_name","hist_title",100,0,100) hist = TH1D("hist_name","hist_title",100,0,100)
 </code> </code>
-or+or //all// classes:
 <code python> <code python>
 from ROOT import * # loads all classes, and thus takes longer from ROOT import * # loads all classes, and thus takes longer
 hist = TH1D("hist_name","hist_title",100,0,100) hist = TH1D("hist_name","hist_title",100,0,100)
 </code> </code>
-or+or only the ''ROOT'' module:
 <code python> <code python>
 import ROOT import ROOT
Line 17: Line 21:
 </code> </code>
 The examples below also make use of ''ROOT'''s ''gRandom'' to make some random variables. The examples below also make use of ''ROOT'''s ''gRandom'' to make some random variables.
- 
 ===== Filling a histogram ===== ===== Filling a histogram =====
  
Line 23: Line 26:
  
 <code python> <code python>
-from ROOT import TH1D, gRandom 
 hist = TH1D("hist_name","hist title",100,0,100) hist = TH1D("hist_name","hist title",100,0,100)
 for i in xrange(10000): for i in xrange(10000):
Line 37: Line 39:
 ===== Filling a histogram from a tree ===== ===== Filling a histogram from a tree =====
  
-The classic way would be+The classic way would be with a simple for loop
 <code python> <code python>
 hist = TH1D("hist_name","hist title",100,0,100) hist = TH1D("hist_name","hist title",100,0,100)
Line 51: Line 53:
 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. 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, including boolean expressions (which can take the value ''0.0'' or ''1.0''):+You can also use **mathematical expressions**, including **boolean expressions** (which can take the value ''0.0'' or ''1.0''):
 <code python> <code python>
 tree.Draw("2*sqrt(px*px+py*py) >> hist_name") tree.Draw("2*sqrt(px*px+py*py) >> hist_name")
 tree.Draw("abs(eta) >> hist_name") tree.Draw("abs(eta) >> hist_name")
 tree.Draw("pt1*(eta1<2.4) + pt2*(eta2<2.4) >> hist_name") # boolean expressions tree.Draw("pt1*(eta1<2.4) + pt2*(eta2<2.4) >> hist_name") # boolean expressions
 +</code>
 +This also includes custom functions, which you can load into ''ROOT'' with a ''C'' file:
 +<code python>
 +from ROOT import gROOT, ...
 +gROOT.Macro("myfunctions.C+")
 +...
 +tree.Draw("deltaR(eta1,phi1,eta2,phi2) >> hist_name"   # deltaR     function defined in "myfunctions.C+"
 +tree.Draw("pt1 >> hist_name","(pt>30)*weightEta1(eta1)") # weightEta1 function defined in "myfunctions.C+"
 </code> </code>
  
-You can apply some selections using variable, available in the tree's branches:+You can apply some **selections** using variable, available in the tree's branches:
 <code python> <code python>
 tree.Draw("px >> hist_name","pt>20 && E>20") tree.Draw("px >> hist_name","pt>20 && E>20")
 </code> </code>
  
-You can apply some selections and/or a weight, if a weight variable is available in the tree:+You can apply some selections and/or a **weight**, if a weight variable is available in the tree:
 <code python> <code python>
-tree.Draw("px >> hist_name","(pt>20 && E>20)*weight") 
 tree.Draw("px >> hist_name","weight") tree.Draw("px >> hist_name","weight")
 +tree.Draw("px >> hist_name","(pt>20 && E>20)*weight")
 </code> </code>
  
Line 77: Line 87:
 </code> </code>
  
-It's also possible to pass some option you would normally use in "TH1::Draw()". It is passed as the third string.+It's also possible to pass some **draw option** you would normally use in "TH1::Draw()". It is passed as the third string.
 <code python> <code python>
 tree.Draw("px >> h1(100,0,100)","","E2") tree.Draw("px >> h1(100,0,100)","","E2")
Line 92: Line 102:
 [2] tree->Draw("px >> h1(100,0,100)","pt>20") [2] tree->Draw("px >> h1(100,0,100)","pt>20")
 [3] tree->Draw("px >> h1(100,0,100)","","E2") [3] tree->Draw("px >> h1(100,0,100)","","E2")
-[4] tree->Draw("py >> h2(100,0,100)","","E2 SAME")+[4] tree->Draw("py >> h2(100,0,100)","","E2 SAME"// compare to px
 </code> </code>
root/pyroot_th1.txt · Last modified: 2017/09/05 10:35 by iwn