簡體   English   中英

如何腌制從TLorentzVector繼承的類?

[英]How to pickle classes which inherit from TLorentzVector?

這使用PyROOT(ROOT)和酸洗。 下面是一個非常簡單的示例。 我嘗試使用如何腌制從A繼承的B類(具有許多變量)的對象作為示例,該對象定義了__setstate__和__getstate__

from ROOT import TLorentzVector
import cPickle as pickle
class MyVec(TLorentzVector):
  def __init__(self):
    TLorentzVector.__init__(self)
    self.a = 'testing'

  def __getstate__(self):
    return self.__dict__

  def __setstate__(self, state):
    self.__dict__ = state

a = MyVec()
b = pickle.loads(pickle.dumps(a))

print a.__class__
print b.__class__

print a.__dict__
print b.__dict__

這個輸出

<class '__main__.MyVec'>
<class 'ROOT.TLorentzVector'>
{'a': 'testing'}
{}

知道如何正確腌制對象嗎? 特別是,我不介意不繼承TLorentzVector並重載我正在使用的某些屬性。 但是我仍然不清楚為什么我不能使它完全起作用,因為它不保留對象屬性。

我認為這是一個可行的示例。 我不清楚它為什么起作用,但它必須與用本地python類包裝cython類並為其定義方法有關。

#!/usr/bin/env python
from ROOT import TLorentzVector
import cPickle as pickle


class MyVec(TLorentzVector):
    def __init__(self, *args, **kwargs):
        print "init"
        if len(args) == 2 and isinstance(args[0], pow.__class__):
          super(MyVec, self).__init__(args[0](*args[1]))
        else:
          args = args or (TLorentzVector())
          if isinstance(args[0], TLorentzVector):
            super(MyVec, self).__init__(args[0])
          else:
            raise ValueError("Unexpected value")
        self.a = kwargs.get('a', 'fake')
        self.b = kwargs.get('b', TLorentzVector())
        print "args", args
        print "kwargs", kwargs

    def __setstate__(self, state):
        print "setstate"
        self.__dict__ = state

    def __getstate__(self):
        print "getstate"
        return self.__dict__

    def __reduce__(self):
        print "reduce"
        return (self.__class__, super(MyVec, self).__reduce__(), self.__getstate__(), )

anotherVec = TLorentzVector()
anotherVec.SetPtEtaPhiM(50.0, 0.0, 0.0, 0.0)

evenMore = TLorentzVector()
evenMore.SetPtEtaPhiM(100.0, 0.0, 0.0, 0.0)

a = MyVec(anotherVec, a='testing', b=evenMore)

b = pickle.loads(pickle.dumps(a))

print a.__class__
print b.__class__

print a.__dict__
print b.__dict__

print a.Pt()
print b.Pt()

print a.b.Pt()
print b.b.Pt()

print a == b
print isinstance(a, MyVec)
print isinstance(b, MyVec)

這個輸出

init
args (<ROOT.TLorentzVector object ("TLorentzVector") at 0x7fa38bce26c0>,)
kwargs {'a': 'testing', 'b': <ROOT.TLorentzVector object ("TLorentzVector") at 0x7fa388f5c740>}
reduce
getstate
init
args (<built-in function _ObjectProxy__expand__>, ('@\x00\x00S\xff\xff\xff\xffTLorentzVector\x00@\x00\x00<\x00\x04\x00\x01\x00\x00\x00\x00\x03\x00\x00\x08@\x00\x00$\x00\x03\x00\x01\x00\x00\x00\x00\x03\x00\x00\x00@I\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@I\x00\x00\x00\x00\x00\x00', 'TLorentzVector'))
kwargs {}
setstate
<class '__main__.MyVec'>
<class '__main__.MyVec'>
{'a': 'testing', 'b': <ROOT.TLorentzVector object ("TLorentzVector") at 0x7fa388f5c740>}
{'a': 'testing', 'b': <ROOT.TLorentzVector object ("TLorentzVector") at 0x7fa38b448be0>}
50.0
50.0
100.0
100.0
True
True
True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM