[CEA 46521][ROOT 6.36][Python] Serialisation of TMatrixD with TBufferJSON Issue


ROOT Version: 6.32.14: Passed / 6.36.02: Failed
Platform: linuxx8664gcc
Compiler: c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0


Hello,

Summary

Serialisation of a TMatrixD with TBufferJSON::ToJSON() triggers a Failed to instantiate error in Python with ROOT 6.36.

Packages

I tested your packages for Ubuntu 24:

Python Tests

ROOT 6.32

$ root -q
   ------------------------------------------------------------------
  | Welcome to ROOT 6.32.14                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jun 05 2025, 07:08:58                 |
  | From tags/v6-32-14@v6-32-14                                      |
  | With c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0                   |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------
$ python3 -c "import ROOT; print(ROOT.TBufferJSON.ToJSON(ROOT.TMatrixD()))"
{
  "_typename" : "TMatrixT<double>",
  "fUniqueID" : 0,
  "fBits" : 0,
  "fNrows" : 0,
  "fNcols" : 0,
  "fRowLwb" : 0,
  "fColLwb" : 0,
  "fNelems" : 0,
  "fNrowIndex" : 0,
  "fTol" : 0,
  "fElements" : []
}

ROOT 6.36

$ root -q
   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.02                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jul 08 2025, 22:04:21                 |
  | From tags/v6-36-02@v6-36-02                                      |
  | With c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0                   |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------
$ python3 -c "import ROOT; print(ROOT.TBufferJSON.ToJSON(ROOT.TMatrixD()))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: Template method resolution failed:
  Failed to instantiate "ToJSON()"

Discussions

Note

C++ test passed:

$ root -q -e "TMatrixD m; TBufferJSON::ToJSON(&m)"
   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.02                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jul 08 2025, 22:04:21                 |
  | From tags/v6-36-02@v6-36-02                                      |
  | With c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0                   |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------


(TString) "{
  "_typename" : "TMatrixT<double>",
  "fUniqueID" : 0,
  "fBits" : 0,
  "fNrows" : 0,
  "fNcols" : 0,
  "fRowLwb" : 0,
  "fColLwb" : 0,
  "fNelems" : 0,
  "fNrowIndex" : 0,
  "fTol" : 0,
  "fElements" : []
}"[209]

Best regards,

I guess @jonas can help

Hi,

TBuffer::ToJSON has template parameter and need special signature to correctly call it in python.
But probably you can use TBufferJSON::ConvertToJSON() method. It should work for TMatrixD.
Like:

 python3 -c "import ROOT; print(ROOT.TBufferJSON.ConvertToJSON(ROOT.TMatrixD()))" 

Regards,
Sergey

1 Like

Thank you very much!

Your solution works:

$ root -q
   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.02                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jul 08 2025, 22:04:21                 |
  | From tags/v6-36-02@v6-36-02                                      |
  | With c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0                   |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------
$ python3 -c "import ROOT; print(ROOT.TBufferJSON.ConvertToJSON(ROOT.TMatrixD()))" 
{
  "_typename" : "TMatrixT<double>",
  "fUniqueID" : 0,
  "fBits" : 0,
  "fNrows" : 0,
  "fNcols" : 0,
  "fRowLwb" : 0,
  "fColLwb" : 0,
  "fNelems" : 0,
  "fNrowIndex" : 0,
  "fTol" : 0,
  "fElements" : []
}

What is the difference between ToJSON() and ConvertToJSON()?

Hi,

With ToJSON one can use any object type - but method uses object type as template argument.
On C++ object type detected automatically, in python such feature not always works.

With ConvertJSON one normally converts derived from TObject classes - which is a case for TMatrixD. And python has no problem to use it.

Regards,
Sergey

1 Like