Python Forum
Using ROOT and opening TCanvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using ROOT and opening TCanvas
#1
I'm creating a program to make some histograms from a datafile. I'm not sure how to use ROOT for this. This is my code

#! /usr/bin/env python3
"""
ntp.py: Example PyROOT program which stores data
in a ROOT TNtuple which is then plotted using
the ROOT plotting routines.
---
John Jacke
PHZ4151C
Florida State University
April 1, 2019
Updated: Sean Dobbs, March 30, 2020
"""
from ROOT import TH1F,TLorentzVector,TCanvas
import numpy as np
import ROOT


def func( aX ):
        """ calculates exp(-x) * sin^2(3x) """
        return np.exp( -aX ) * np.sin( 3.0 * aX )**2


# Open a ROOT TFile
rootFile = ROOT.TFile("n3pi.root", "RECREATE")
#Creating histograms below
Hnppm = TH1F("nppm","Neutron,PiPlus,PiPlus,PiMinus",150,0.8,2.3)
Hppm = TH1F("ppm","PiPlus,PiPlus,PiMinus",150,0.8,2.3)
Hp1m = TH1F("p1m","PiPlus1,PiMinus",150,0.8,2.3)
Hp2m = TH1F("p2m","PiPlus2,PiMinus",150,0.8,2.3)
Hpp = TH1F("pp","PiPlus1,PiPlus2",150,0.8,2.3)
Hnp1 = TH1F("np1","Neutron,PiPlus1",150,0.8,2.3)
Hnp2 = TH1F("np2","Neutron,PiPlus2",150,0.8,2.3)
Hnm = TH1F("nm","Neutron,PiMinus",150,0.8,2.3)

Photon,PiPlus = TLorentzVector(),2*[TLorentzVector()]
Proton = TLorentzVector(0.0,0.0,0.0,0.938)
PiMinus = TLorentzVector(0.0,0.0,0.0,0.0)
nEvents=0

with open("n3pi.dat", "r") as dataFile:
        for line in dataFile:
                word = line.split() # split line into a list of words
                value = int(word[0])
                if value == 4:
                        #print("New event Information")
                        nPiPlus = 0
                        nEvents+=1
                if value == 1:
                        #print("\tPhoton Beam with Energy: %f"%(float(word[5])))
                        Photon.SetPxPyPzE(float(word[2]),float(word[3]),float(word[4]),float(word[5]))
                if value == 8:
                        # piPlus meson
                        #print("\tPiPlus[%d] meson with Energy: %f"%(nPiPlus, float(word[5])))
                        PiPlus[nPiPlus].SetPxPyPzE(float(word[2]),float(word[3]),float(word[4]),float(word[5]))
                        nPiPlus += 1
                if value == 9:
                        #print("\tPiMinus meson with Energy: %f"%(float(word[5])))
                        PiMinus.SetPxPyPzE(float(word[2]),float(word[3]),float(word[4]),float(word[5]))
                Neutron = (Photon + Proton - (PiMinus+PiPlus[0]+PiPlus[1]))
#Filling the histograms with values obtained above
                Hnppm.Fill((Neutron + PiPlus[0] + PiPlus[1] + PiMinus).Mag())                                                                                                           
                Hppm.Fill((PiPlus[0] + PiPlus[1] + PiMinus).Mag())
                Hp1m.Fill((PiPlus[0] + PiMinus).Mag())
                Hp2m.Fill((PiPlus[1] + PiMinus).Mag())
                Hpp.Fill((PiPlus[0] + PiPlus[1]).Mag())
                Hnp1.Fill((Neutron + PiPlus[0]).Mag())
                Hnp2.Fill((Neutron+PiPlus[1]).Mag())
                Hnm.Fill((Neutron + PiMinus).Mag())
#Creating a TCanvas
Canvas = TCanvas("cc","invariant masses",10,10,800,600)
Canvas.Divide(2,4)
Canvas.cd(3)
Hnppm.Draw()
# Save the ROOT TFile containing ROOT objects
rootFile.Write()
....

Should I save this as a .root file? When trying to run it with root I type the following commands into the command prompt:
root -l
root "filename.py"

I am getting the error
Error:
"ROOT_prompt_0:1:5: error: expected ';' after expression root Problem1c.root"
Reply
#2
This is a python program that uses ROOT through pyroot. You run it by typing "python" (maybe "python3") followed by the program's filename.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to pass a input after changing the user from root to non root using python avinash 3 3,206 Apr-08-2019, 10:05 AM
Last Post: avinash
  opening a file from a windows desktop folder- error opening file Charan007 1 2,990 Dec-06-2018, 11:50 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020