I write a file to add a branch to an existing tree,
and save it in main function.
I declare all variable are global.
But when proceeding to wf->cd(); wf->close();
the program meet errors.
int main(){
add();
gv::rf->cd();
gv::trec->Write("", TObject::kOverwrite); // save only the new version of the tree
gv::rf->Close();
}
how to solve it? May I pass the file pointer to functionf add?
Thank you very much.
By the way, I find write root function in different .cc files, for example open a file in a .cc file and close in another .cc file, is very nuisance, I could not find examples in the website.
I do not quite understand. In your example (so far) there is no need to do the read/update and the write in 2 different cc files. Why do you have this separation?
My original code is quite long, so that reading it is boring.
Therefore to make my code clear, I would like to write various files to carry out different functions.
for example,
one is to open file to read and get TTrees and histograms inside
one is to open file to write and declare new TTrees and histograms
one is to perform the calculation using entries in TTrees and histograms
one is to close all the files
I do not want to write all of them in one file and even in a function.
Therefore I want both TTree and histogram, even TFile object, could be passed as parameters to the functions.
I find both TTree and histogram are attached to the currrent directory.
I read section Input/Output 1.2.8:
When a variable is declared on the stack in a function such as in the code below, it will be deleted when it goes out of scope.
Creating it on the heap instead of on the stack could prevent an object in a function from being deleted when it goes out of scope.
But I meet errors when I open a TFile in one file and close in main function.