-
Notifications
You must be signed in to change notification settings - Fork 67
How to create a root tree? #543
Comments
I need a hand here:
The problem is here: with this error: I tried to use append instead of extend and also give errors. The values need to be written as they are being read..... |
You're mixing Uproot 3 and 4 (which I think you eventually fixed, in favor of Uproot 3). Since you seem to be developing a new script, rather than keeping an old one running, you should start with the latest version. (Uproot 3 is only being kept around for analysis code that was developed using it and now needs to keep working so that somebody can graduate or otherwise finish up a one-time project.) Uproot 4 became the default "uproot" (as in Here's where the documentation on Uproot 4 writing starts: https://uproot.readthedocs.io/en/latest/basic.html#opening-a-file-for-writing The following line isn't responsible for the error message, but:
outside of this The reason you're getting an error is because f["t"].extend({"voltage": voltages, "current": currents}) where Looking at your code some more, I see now that that's how Since the function def current_response(voltage):
self.source.source_voltage = voltage
time.sleep(self.delay)
return self.meter.current goes through Python code—it's not purely NumPy—then you'll have to write a Python However, Uproot just doesn't work that way. The So what you'll have to do is iterate over the data in a Python def execute(self):
voltages = np.arange(
self.min_voltage,
self.max_voltage + self.voltage_step,
self.voltage_step,
dtype=np.float64,
)
currents = np.zeros(len(voltages), dtype=np.float64)
for i, voltage in enumerate(voltages):
self.source.source_voltage = voltage
time.sleep(self.delay)
currents[i] = self.meter.current
with uproot.recreate("IVcurve.root") as f:
f["t"] = {"voltage": voltages, "current": currents} A single with uproot.recreate("IVcurve.root") as f:
t = f.mktree("t", {"voltage": np.float64, "current": np.float64})
t.extend({"voltage": voltages, "current": currents}) Also, it looks to me like you're just making this ROOT file in order to plot the data. There are much easier ways to do that, like Matplotlib's plt.plot: import matplotlib.pyplot as plt
plt.plot(voltages, currents) And if you need to save the NumPy arrays in some form, you could just pickle them: import pickle
# write
pickle.dump({"voltage": voltages, "current": currents}, open("IVcurve.pkl", "wb"))
# read
voltages_and_currents = pickle.load(open("IVcurve.pkl", "rb"))
voltages = voltages_and_currents["voltage"]
currents = voltages_and_currents["current"] or use NumPy's own file format: # write
np.savez(open("IVcurve.npy", "wb"), voltage=voltages, current=currents)
# read
voltages_and_currents = np.load(open("IVcurve.npy", "rb"))
voltages = voltages_and_currents["voltage"]
currents = voltages_and_currents["current"] Or HDF5, etc. If the point is to get it into ROOT to use ROOT's plotting features, then yes, write a ROOT file. Otherwise, if you're going to be using Matplotlib (because you're in Python already), data with simple structure like this—just two columns of numbers—can be saved in a variety of ways, most of which are simpler and recognized by more data analysis tools. If, on the other hand, you ever need to write jagged arrays or more complex data structures, then you'll want to seriously consider ROOT (or Parquet). |
@jpivarski many thanks indeed now many things are clearer and I have introduced all these changes. However, only tomorrow I can test it when getting access to the device. |
Could you add an example of how for this tree and I can add new baskets? I mean I would like to add more data to the same branches, but tried different ways and none of them works. |
If you have created a new TTree like with uproot.recreate("IVcurve.root") as f:
f["t"] = {"voltage": voltages, "current": currents} # voltages and currents are NumPy arrays and have not yet closed the file (i.e. you're still in the "with" block above), you can add a second TBasket to it by f["t"].extend({"voltage": voltages, "current": currents}) # voltages, currents may be new arrays (Note the indentation: we're still in the "with" block; the file has not closed.) You can't, however, open a file and change an existing TTree. That's not in principle impossible, but it's code that has not been written; it's not a feature of Uproot yet. The reason uproot.WritableTree has an It's possible (though inadvisable) to write each entry as a separate TBasket. (Seriously inadvisable! I'm not going to write out the code!) |
Hi,
Trying to create a root tree and stuck at the beggining with:
AttributeError: module 'uproot' has no attribute 'newtree'
I need to create two branches: voltage int32, current float64
If you could put an example will be great
Using python3.9 and uproot3
Thanks in advance!
The text was updated successfully, but these errors were encountered: