How to save std::unique_ptr into TTree?

Dear ROOT experts,

In a previous forum post [1], it was mentioned that saving unique_ptrs in a TTree is possible. I tried using the example code here [2], which writes and reads a TTree using a dictionary, but it doesn’t seem to work.

@pcanal, could you please help me understand how to do this, or point me to a working example? It seems that most TTree examples have been removed…

Thank you very much!

Best regards,
Alvaro

[1] Is shared_ptr supported in class member derived from TObject? - #5 by pcanal

[2] GitHub - atolosadelgado/customTTreeExample


ROOT Version: 6.36.000
Platform: Alma9
Compiler: gcc 14

Dear Alvaro,
Thank you for your question. You even identified the corresponding expert.
I just wanted to mention that due to today’s US holiday, the reply latency may be higher.
Best,
Lukas

Your code looks correct to me. The issue here is that it’s using a unique pointer to a POD (plain old datatype, int in this case). In the ROOT I/O, this is interpreted as array (@pcanal may correct me / give more details). This is typically not a critical limitation because there is anyway limited use in having a pointer to a POD.

If you change the code to store, e.g., a unique pointer to std::string, it works.

1 Like