Hello!
I would like to address this question to the experts, since i am really stuck with it sorry.
Ok, principally i have a Geant4 Application which in the EventLoop fills me a Tree. After the EventLoop is done, the Tree is written to a root-file -like usual.
unfortunately the Writing Fails, I dont really know the exact G4 type of error but without filling the TTree it works,
so i try to Reset the TTree and write it in the File again.
Unfortunately only the last Loop of the Filling which was executed was written. I tried to reproduce this error with a simple RootScript, as attached.
When executing this Script, the File is written with 4 copies of the Tree in it, but they are the same, and contain only the last entryFilling Loopā¦
Please, can someone say what i did wrong exactly with Fill, Write, and Reset() ???
Best Regards,
Frank
{
Double_t particle_energy;
Double_t particle_xvertex;
Double_t particle_yvertex;
Double_t particle_zvertex;
Int_t event;
TreePGunInfo = new TTree("TreePGunInfo","ParticleGunInfo");
TreePGunInfo->Branch("particleenergy",&particle_energy,"particle_energy/D");
TreePGunInfo->Branch("particlexvertex",&particle_xvertex,"particle_xvertex/D");
TreePGunInfo->Branch("particleyvertex",&particle_yvertex,"particle_yvertex/D");
TreePGunInfo->Branch("particlezvertex",&particle_zvertex,"particle_zvertex/D");
TreePGunInfo->Branch("event",&event,"event/I");
TFile* f= new TFile("f.root","RECREATE");
f->cd();
for (Int_t i =0; i< 300001; i++) {
event=i;
particle_energy=gRandom->Uniform(i,i+3);
particle_xvertex=gRandom->Uniform();
particle_yvertex=gRandom->Uniform();
particle_zvertex=gRandom->Uniform(i,i+1);
TreePGunInfo->Fill();
if (i%100000==0) {
TreePGunInfo->Print();
TreePGunInfo->Write();
TreePGunInfo->Reset();
}
}
The writing fails because the tree is not attached to any file. You have:
TreePGunInfo = new TTree("TreePGunInfo","ParticleGunInfo");
....
TFile* f= new TFile("f.root","RECREATE");
....
TreePGunInfo->Write();
but never attach the tree to the file and hence the system has no way to know where to store the file. The simplest solution is to use:TFile* f= new TFile("f.root","RECREATE");
TreePGunInfo = new TTree("TreePGunInfo","ParticleGunInfo");
....where the TTree is now attached to f.root (because it is the current ROOT directory at the time you create the TTree. See Userās guide for details).
Also you have if (i%100000==0) {
TreePGunInfo->Print();
TreePGunInfo->Write();
TreePGunInfo->Reset();
} . If you intend is to keep the last 100000 entries, you should look into the Circular Tree (TTree::SetCircular)
Even with Creation of the RootFile Befor and even setting TTreeDirectory to the file, the tree which is stored in the root file is still only the one of the last loop, at this 4 timesā¦
Again I will put the modified code.
{
TFile* f= new TFile("f.root","RECREATE");
Double_t particle_energy;
Double_t particle_xvertex;
Double_t particle_yvertex;
Double_t particle_zvertex;
Int_t event;
TreePGunInfo = new TTree("TreePGunInfo","ParticleGunInfo");
TreePGunInfo->SetDirectory(f);
TreePGunInfo->Branch("particleenergy",&particle_energy,"particle_energy/D");
TreePGunInfo->Branch("particlexvertex",&particle_xvertex,"particle_xvertex/D");
TreePGunInfo->Branch("particleyvertex",&particle_yvertex,"particle_yvertex/D");
TreePGunInfo->Branch("particlezvertex",&particle_zvertex,"particle_zvertex/D");
TreePGunInfo->Branch("event",&event,"event/I");
for (Int_t i =0; i< 301; i++) {
event=i;
particle_energy=gRandom->Uniform(i,i+3);
particle_xvertex=gRandom->Uniform();
particle_yvertex=gRandom->Uniform();
particle_zvertex=gRandom->Uniform(i,i+1);
TreePGunInfo->Fill();
if (i%100==0) {
TreePGunInfo->Print();
TreePGunInfo->Write();
TreePGunInfo->Reset();
}
}
f->Close();
}
So in principal I shoud get āTreePGunInfo;1ā, āTreePGunInfo;2ā and so on, but they are not different (if watched with TBrowserā¦).
[quote]So in principal I shoud get āTreePGunInfo;1ā, āTreePGunInfo;2ā and so on, but they are not different (if watched with TBrowserā¦).[/quote]For now the Tbrowser only brings one of the cycle. Cycles are usually assumed to be backup copy of earlier version of an object (and thus obsolete).
If you want to access a specific cycle, you can use:TTree *tree1,*tree2;
f->GetObject("TreePGunInfo;1",tree1);
f->GetObject("TreePGunInfo;2",tree2);
Now that said I do not understand your goal
Why are you reseting the TTree every once in a while?