I’m using Geant4’s G4AnalysisManager
ROOT interface to save simulation output to a .root
file. I can fill an Ntuple with data at each step with no issues, and the output file is fine. However, I would also like to store the number of primary particles in each run. To do this, I store a vector of number of primaries and write this to a TVectorD
in the output .root
file after the simulation is done, e.g.:
RunAction::~RunAction() {
G4AnalysisManager *analysisManager = G4AnalysisManager::Instance();
analysisManager->Write();
analysisManager->CloseFile();
delete G4AnalysisManager::Instance();
WritePrimaries();
G4cout << "\n--> Wrote to " << file_name.str() << G4endl;
}
where my WritePrimaries
method is:
void RunAction::WritePrimaries() {
TFile *f = (TFile*) TFile::Open(TString(file_name.str()), "UPDATE");
f->cd();
size_t n = primaries.size();
TVectorD p(n);
for (size_t i = 0; i < n; ++i)
p[i] = primaries[i];
p.Write("primaries");
f->Close();
}
This seems to corrupt the .root
file. The data looks fine in a TBrowser
but in terminal I get:
root [0]
Attaching file output345.root as _file0...
(TFile *) 0x7f92dd832d70
root [1] TBrowser t
(TBrowser &) Name: Browser Title: ROOT Object Browser
root [2] Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:0, badread=1, nerrors=1, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:1, badread=1, nerrors=2, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:2, badread=1, nerrors=3, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:3, badread=1, nerrors=4, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:4, badread=1, nerrors=5, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:5, badread=1, nerrors=6, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:6, badread=1, nerrors=7, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:7, badread=1, nerrors=8, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:8, badread=1, nerrors=9, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
file probably overwritten: stopping reporting error messages
Error in <TBranch::GetBasket>: File: output345.root at byte:0, branch:edep, entry:9, badread=1, nerrors=10, basketnumber=0
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
Error in <TBasket::Streamer>: The value of fNbytes is incorrect (-66564769) ; trying to recover by setting it to zero
...<repeated for many lines>...
and similarly if I do photonEdep->Scan()
I can see that the first column is all zero instead of my test value of 1.23.
In fact, even if I don’t write anything at all, the file still gets corrupted! It seems that merely opening and closing the file in update mode is enough to corrupt it. In addition, this only happens on one branch—I was using this code without issue earlier and it has only started failing with the addition of a new branch.
Is there a safer way I can update this ROOT file?
ROOT Version: 6.18/00
Platform: Mac OSX 10.14.6
Compiler: Not Provided