Open jacanchaplais opened 9 months ago
Ah, so I've been poking around the test files, and found this: https://github.com/scikit-hep/pyhepmc/blob/main/tests/test_from_hepevt.py#L17 So I gather that it's not actually a constructor classmethod.
The naming here is a bit confusing, as .from_xxx()
always implies to me it's a classmethod, rather than an instance method which does some inplace operation.
Anyway, I'm sorted now, thank you! (I'll leave open in case you want to add some more docs for numpy stuff etc.)
Sorry for the late reply. I agree that it would be more pythonic if from_hepevt
was a classmethod. I am not sure if there is a way to fix this at this point, but if it is possible to have both a classmethod and a method with the same name then I am happy to fix this. The potential classmethod from_hepevt
then needs two additional optional arguments momentum_unit
and length_unit
.
Perhaps import_hepevt
would have been a better name?
The numpy-friendlyness is discussed here: https://scikit-hep.org/pyhepmc/examples/processing.html
For the record, this works
class Foo:
def from_bar(*args, **kwargs):
if isinstance(args[0], Foo):
print("method", args[1:], kwargs)
else:
print("classmethod", args, kwargs)
Sorry I missed this! I can see from the docs on the numpy stuff, it's mainly regarding computation rather than IO, which is a bit different to my use-case, but cool neverthless. :)
Also, fun example! Though, I think this would be closer to accessing it as a staticmethod? Decorated classmethods receive the class type constructor as the first argument, whereas if you were to do
Foo.from_bar("spam")
The first argument here would be "spam", not the Foo
constructor. So, it seems more like a staticmethod, to me. I guess you could still use it as though it were one, but as in your example, you would need to specifically point it to Foo
. Probably that's not an issue, unless you subclass it. :)
Hi there! I see that there is good coverage in the docs for reading data from HepMC files, but I can't see much information on how to write data to one. I also notice the docs say the library is "numpy-friendly", but I can't really see much in the way of numpy integration.
I have my simulation data in numpy arrays, and would love a way of writing out to file objects in the HepMC format. I tried using the
GenEvent.from_hepevt()
method, since it seemed to take in the numpy data and spit out aGenEvent
instance, but I kept getting confusing errorsAs far as I can tell I added everything correctly. I found this from the HepMC3 GitLab for converting directly from a
pythia8.Pythia
instance, but it's a bit hard to read, given there's no docs, type annotations, and the whole method for writing it is done in one monolithic chunk. I've managed to make it work, but will take a while unpacking what's going on to be able to just throw in numpy data.Can you offer any help? Examples or advice greatly appreciated.