When opening TFile in UPDATE mode, the TObject::kOverwrite doesn't work if Ctrl+C is received

Hi, all

I want my code to open a root file to update it. i open in “UPDATE” mode, get the pointer of the histogram in the file, and fill the histogram. After each filling i call hpos->Write("",TObject::kOverwrite);. And when i finish filling with many points i close the file.

i was expecting that the “hpos->Write(”",TObject::kOverwrite);" will update the object “hpos” in disk every time i call it. And is doing it if i close the TFile, but when i send a Ctrl+c to the program without closing the Tfile, the “hpos” object do not update on disk. This doesn’t happen if i opened the TFile in “RECREATE” mode, meaning i could send a Ctrl+c and the file gets updated. This behavior only happen when use “kOverwrite”, but i don’t want copies of my objects.

Is it the normal working? because i want that even when a Ctrl+c is send, my data was written (on disk) every time i called “hpos->Write(”",TObject::kOverwrite);".

I hope is clear enough. Thank you in advance for your time.


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Maybe @pcanal can give some hints

1 Like

Hi,

To make the file resilient to abrupt process end, you need to also store the ‘directory’ information. One way is:

gDirectory->SaveSelf();

Another is to rely on the fact that the histogram is attached to the directory and instead of hpos->Write use

gDirectory->Write("", TObject::kOverwrite);

(gDirectory can also just be replaced with any pointer to the file or directory)

Cheers,
Philippe.

2 Likes

Thank you for your answer. I hope it was not so obvious. I tried

And is working fine.
Best,
Eduardo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.