Articles

TGraph in pyROOT

In howto on Nov 22, 2015 by theoryl Tagged: , ,

pyROOT is a python binding of ROOT, which allows you to do a lot of pythonic things. The example below shows how to make a TGraph in pyROOT:

from ROOT import TGraph, gPad
from array import array
from math import exp

def func(x):
    y = 100*exp(-x)
    return y


# Method 1
if True:
    n = 100
    gr1 = TGraph(n)

    for i in range(n):
        x = 0.01*i
        gr1.SetPoint(i, x, func(x))

    gr1.SetMarkerStyle(20)
    gr1.SetMarkerSize(1)
    gr1.Draw("AP")


# Method 2
if False:
    n = 100
    xarr = []
    yarr = []

    for i in range(n):
        x = 0.01*i
        y = func(x)
        xarr.append(x)
        yarr.append(y)

    gr2 = TGraph(len(xarr), array('d', xarr), array('d', yarr))

    gr2.SetMarkerStyle(20)
    gr2.SetMarkerSize(1)
    gr2.Draw("AP")
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: