Problem 2570

Summary: G4AnalysisManager creates ROOT files that are not updateable
Product: Geant4 Reporter: seligman
Component: analysisAssignee: Ivana Hrivnacova <ivana>
Status: ASSIGNED ---    
Severity: normal    
Priority: P4    
Version: 11.1   
Hardware: All   
OS: All   
Attachments: A file output from a Geant4 simulation using G4AnalysisManager
A file after the described "open with update", followed by an immediate close
Tar file with a fix

Description seligman 2023-10-24 00:28:24 CEST
Created attachment 829 [details]
A file output from a Geant4 simulation using G4AnalysisManager

According to problem ID 1534, this was problem was fixed in Geant4.9.6. However, it still exists in Geant4.11.

Here's is my description of the problem in the ROOT forums:

https://root-forum.cern.ch/t/appending-to-file-opened-in-update-mode-corrupts-file/56801

I was finally pointed to the problem itself here:

https://root-forum.cern.ch/t/opening-a-file-in-update-mode-causes-error-in-tbasket-streamer-the-value-of-fnbytes-is-incorrect/43340/15

To duplicate the problem: Create any ROOT file using Geant4's G4AnalysisManager; I've attached an example file. Then open the file in ROOT:

# root
root [0] auto input=TFile::Open("example.root","UPDATE");
root [1] input->Close();
root [2] .q

From that point forward, the state of the file will be problematic. I've
Comment 1 seligman 2023-10-24 00:30:22 CEST
Created attachment 830 [details]
A file after the described "open with update", followed by an immediate close
Comment 2 seligman 2023-10-24 00:33:24 CEST
Hit the wrong button...

I've attached copies of the file from the same run of the Geant4 simulation that used G4AnalysisManager. The "works" file is before the example ROOT lines were executed, the "broken" file is after.

To compare the files, load them in ROOT and examine the result of scanning the LArHits ntuple:

# root gramsg4-works.root
root [0] LArHits->Scan()
root [1] .q

# root gramsg4-broken.root
root [0] LArHits->Scan()
root [1] .q
Comment 3 Ivana Hrivnacova 2023-11-08 12:05:07 CET
Hello,

Thank you for reporting this problem. We have found that it was already reported in ROOT forum two years ago (https://root-forum.cern.ch/t/opening-a-file-in-update-mode-causes-error-in-tbasket-streamer-the-value-of-fnbytes-is-incorrect/43340), however we were not notify about it at that time.

There is also suggested the following work-around that can be used until the problem is fixed in Geant4:
"The geant4 reimplementation has a flaw that makes the file not update-able. Instead you need to first copy the file with ROOT (hadd -f output_updatable.root output_good.root) then update the copy."

Best regards,
Comment 4 seligman 2023-11-08 12:11:57 CET
Hi,

That work-around was discussed in the ROOT page I referenced in my original report:

https://root-forum.cern.ch/t/appending-to-file-opened-in-update-mode-corrupts-file/56801

I implemented it within a C++ routine within my Geant4 simulation:

https://github.com/wgseligman/GramsSim/blob/develop/GramsG4/g4util/src/FixAnalysis.cc

However, this does not solve the problem within Geant4. It also wastes disk space. Is there any timescale for a G4 fix? Or will it be left to the future GeantV + ROOT7 integration?
Comment 5 Ivana Hrivnacova 2023-11-08 12:37:16 CET
Hi,

You wrote: "According to problem ID 1534, this was problem was fixed in Geant4.9.6. However, it still exists in Geant4.11."

Where was this problem reported ? The problem with this ID in Geant4 bugzilla is not related to analysis.

The 'g4tools' developer is working on the fix, which will be integrated in Geant4 as soon as possible, we hope it will be still in the coming release, 11.2.

Best regards,
Comment 6 Ivana Hrivnacova 2023-11-13 17:44:24 CET
Created attachment 835 [details]
Tar file with a fix
Comment 7 Ivana Hrivnacova 2023-11-13 17:45:15 CET
Hello,

The problem is now fixed in Geant4 development version; I attach a tar file that can be unpacked in the geant4 top directory; it contains only one file:

source/externals/g4tools/include/tools/wroot/file

It fixes the problem with updating a Root file in our tests (with the Root file produced with example B4); but it would be useful if you can check that it works also in your application.

With best regards,