Accessing a friend TTree using pyROOT - python

I have a pyROOT script where I use TChain::AddFriend to combine two TTrees:
from ROOT import TFile, gDirectory
myfile = TFile("e.root")
mychain = gDirectory.Get("elec_gen")
entries = mychain.GetEntriesFast()
friendFile = TFile("mu.root")
friendChain = gDirectory.Get("muon_gen")
mychain.AddFriend("muon_gen")
elec_gen_evtNum = mychain.evtNum
muon_gen_evtNum = mychain.muon_gen.evtNum
When I run this I get:
$ python friendTest.py
Traceback (most recent call last):
File "friendTest.py", line 12, in <module>
muon_gen_evtNum = mychain.muon_gen.evtNum
AttributeError: 'TTree' object has no attribute 'muon_gen'
With the last line commented-out it runs fine. So it appears I am not accessing the leaves of the friend tree (muon_gen) correctly. How do I access them?
I also tried combining the TTrees using:
mychain.AddFriend("muon_gen","mu.root")
but this produces the same error.

I had a similar question, and found an answer (well, workaround) on the ROOT forum:
https://root-forum.cern.ch/t/accessing-a-friend-ttree-using-pyroot/25513
although no solution was presented using TFriend.
Instead, RobS found a workaround for his own question of just loading the two TFile and TTree separately and running LoadTree() and GetEvent() on each TChain

Related

2 monitors, pygame.display.Info won't work

I have two monitors (one is 1920x1080, the other is 1024x768) and I've just started a new game and ran into an issue with pg.display.Info:
import pygame as pg
pg.init()
x,y = pg.display.Info()
It gives me the following error:
Traceback (most recent call last):
File "C:\Users\marty\Desktop\New Text Document (2).py", line 3, in
x,y = pg.display.Info()
TypeError: 'VidInfo' object is not iterable
Even after isolating the code to those three lines, the same error gets raised. I couldn't find any info on 2 monitors in Python.
PS: pygame is updated, according to pip. My Python version is 3.6.5, and I'm running windows 10.
You can't unpack the VidInfo object which pg.display.Info() returns like that. Assign it to a single variable and then use the attributes that you need. For example:
info = pg.display.Info()
print(info)
print(info.current_w, info.current_h)

How to add a node with ruamel.yaml

I tried to add a new node following the example but:
myitems = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
myitems['abc'].append('test')
gives me an error:
Traceback (most recent call last):
File "item_updater.py", line 148, in <module>
myitems['wohnung'].append('test')
AttributeError: 'CommentedMap' object has no attribute 'append'
I am using ruamel.yaml v0.13.7
What am I doing wrong?
Your error doesn't come from the example you indicated, as in the inp of the example there is no wohnung that shows up in your error.
You probably forgot a - somewhere:
wohnung:
a: 1
instead of:
wohnung:
- a: 1
only on the latter you can append using myitems['wohnung'].append('test').
The example works, but without showing your real YAML input it is difficult to see what is the exact cause of your error.

comtypes: in call_with_inout, ctypes TypeError: 'c_double' object is not iterable

Im working with Agilent IVI drivers in Python 2.7.9 and can't seem to get 'proven' code to work on a particular Windows 7 machine. It executes successfully on other machines.
While this issue seems rather limited to one instrument, it appears to be a broader Python issue, so I turn to Stack Overflow for help. Any help or insight would be great.
The following code
# Import the TLB
from comtypes.client import GetModule, CreateObject
GetModule('AgInfiniium.dll')
# Pull in the coefficients and classes, we'll need those
from comtypes.gen.AgilentInfiniiumLib import *
# Instantiate the driver
OScope = CreateObject('AgilentInfiniium.AgilentInfiniium')
# Make constants of our settings
Address = "TCPIP0::10.254.0.222::INSTR"
resetOScope = False
# Open a connection to the scope
OScope.Initialize(Address,False,resetOScope,'')
# Make a measurement
print OScope.Measurements.Item("Channel1").ReadWaveformMeasurement(
MeasFunction=AgilentInfiniiumMeasurementAmplitude, MaxTime=10)
yields the following error:
Traceback (most recent call last):
File "P:\Aperture\Validation\WMI_BGA_Board\TestMatrixCode\scopeTest2.py", line 29, in <module>
print OScope.Measurements.Item("Channel1").ReadWaveformMeasurement(MeasFunction=AgilentInfiniiumMeasurementAmplitude ,MaxTime=10)
File "C:\Python27\lib\site-packages\comtypes-1.1.0-py2.7.egg\comtypes\__init__.py", line 656, in call_with_inout
rescode = list(rescode)
TypeError: 'c_double' object is not iterable
In my limited debugging attempts, I have seen that this call_with_inout
function tries to convert my Python arguments into arguments for the following C++ function:
public void ReadWaveformMeasurement(
AgilentInfiniiumMeasurementEnum MeasFunction,
AgilentInfiniiumTimeOutEnum MaxTime,
ref double pMeasurement)
It's creating some kind of variable for the pMeasurement pointer that ends up being type c_double, and then complains that it's not iterable.
At this point, this seems like it's local to this machine. I've gone to the extent of uninstalling Python, reinstalling the Agilent driver, and trying two versions of comtypes (1.1.0 and 1.1.1). Yet the problem persists. Any ideas? Thanks.

NameError: global name 'imshow' is not defined but Matplotlib is imported

I'm currently writing a python script which plots a numpy matrix containing some data (which I'm not having any difficulty computing). For complicated reasons having to do with how I'm creating that data, I have to go through terminal. I've done problems like this a million times in Spyder using imshow(). So, I thought I'd try to do the same in terminal. Here's my code:
from numpy import *
from matplotlib import *
def make_picture():
f = open("DATA2.txt")
arr = zeros((200, 200))
l = f.readlines()
for i in l:
j = i[:-1]
k = j.split(" ")
arr[int(k[0])][int(k[1])] = float(k[2])
f.close()
imshow(arr)
make_picture()
Suffice it to say, the array stuff works just fine. I've tested it, and it extracts the data perfectly well. So, I've got this 200 by 200 array of numbers floating around my RAM and I'd like to display it. When I run this code in Spyder, I get exactly what I expected. However, when I run this code in Terminal, I get an error message:
Traceback (most recent call last):
File "DATAmine.py", line 15, in <module>
make_picture()
File "DATAmine.py", line 13, in make_picture
imshow(arr)
NameError: global name 'imshow' is not defined
(My program's called DATAmine.py) What's the deal here? Is there something else I should be importing? I know I had to configure my Spyder paths, so I wonder if I don't have access to those paths or something. Any suggestions would be greatly appreciated. Thanks!
P.S. Perhaps I should mention I'm using Ubuntu. Don't know if that's relevant.
To make your life easier you can use
from pylab import *
This will import the full pylab package, which includes matplotlib and numpy.
Cheers

input syntax in paraview python shell

I currently am trying to use
paraview.simple.Histogram(Input, params)
as
paraview.simple.Histogram(q, BinCount = 30)
in the shell where q is a variable data set from my "out.e" ExodusII file. I'm getting the error
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'q' is not defined
I've tried to search the literature on python shell scripting in Paraview but it seems to be eluding me. I know this is a quick fix. Thanks
Try this instead:
Histogram(SelectInputArray="q", BinCount=30)
This assumes you currently have the reader as the active object in the Pipeline browser.
I was able to answer this problem using the following.
outset = Reader(FileName=['/dir/out.e'])
and for the Histogram
histogram1_1 = Histogram(Input=outset)
histogram1_1.SelectInputArray = ['CELLS', 'q']
histogram1_1.BinCount = 30
Note for anyone who comes into this issue, the TRACE option in the Python Shell will build a script for you when you do anything in the GUI.

Categories