Writing TTree and Viewing Them on TBrowser

So, I have an original overall root file that contains all the raw data, and I am processing some of its values and writing the results into some new processed root files. My macro code looks something like the following:

void ProcessData() {
gROOT->Reset();

Double_t vars;    //placeholder

TFile *original = new TFile("path/file.root");
TTree *origTree = (TTree*) original->Get("data");
int nEntries = origTree->GetEntriesFast();
origTree->SetBranchAddress("vars", &vars);

TFile *result1 = new TFile("path/result1.root", "RECREATE");
TTree *res1Tree = new TTree("result1", "result1");
TBranch *res1vars = res1Tree->Branch("vars", &vars, "vars/D");

//code to process data
for (int i = 0; i < nEntries; ++i) {
    origTree->GetEntry(i);
    //codes to actually do some analyses on the values of vars read and manipulate vars
    res1vars->Fill();
}
result1->WriteTObject(res1Tree);
result1->Close();

//and the same thing happens with result2, result3, etc.

}

So, the code seems to run okay; when I put in some printing statements during the data processing code, vars does read values from the origTree and seems to be getting manipulated as I want.

When the macro is finished and I use TBrowser to access the files result1.root, result2.root, etc., the TTrees and the branches are there, but nothing shows up when I double-click them; I would expect the histogram for that branch, which is indeed what I get when I explore original.root in the same directory. The file sizes of the result.root files are also non-trivial; the sizes are approximately what I expect them to be, dependent on the sizes. I also get nothing when I explicitly read the files and try to draw some of the branches on a separate TCanvas, not on the automatic one in TBrowser.

Can someone tell me what is wrong with the code above, and what would be the reliable way to do this: importing a “master” raw data file (original.root), read some of the values, run some codes to analyze/manipulate the data, and save them into TTree(s) in a different result.root file? I have done similar things but instead adding new histograms to original.root or adding branches to TTrees from the original file and updating the file, and they seem to work fine. I’m guessing there is something peculiar about the way I should write files.

Remove the line:
gROOT->Reset();
then, instead of:
res1vars->Fill();
try:
res1Tree->Fill();
and instead of:
result1->WriteTObject(res1Tree);
result1->Close();
try:
res1Tree->Write();
delete result1; // automatically deletes “res1Tree”, too

[quote=“Pepe Le Pew”]Remove the line:
gROOT->Reset();
then, instead of:
res1vars->Fill();
try:
res1Tree->Fill();
and instead of:
result1->WriteTObject(res1Tree);
result1->Close();
try:
res1Tree->Write();
delete result1; // automatically deletes “res1Tree”, too[/quote]

Filling in the TTrees instead of the TBranches got the job done. Thank you!

File->WriteTObject(TTree);

seems to work fine, however. Is there any particular reason you suggested that, other than simplicity? Also, when I just use TTree->Write(); (or any other TObject, for that matter), how does root know which file to write the object onto, if I have more than one file open in the code? (in this case, I will have the master original.root file and result1.root both open, for example)

On a completely random note, just out of curiosity, sometimes when I am playing around with large data sets, the TTrees saved in a file will automatically split into two. Inside result1.root, for example, I have two TTrees: vars;1 and vars;2, and vars;1 is a smaller (first few hundred thousands) subset of the whole thing I intend to create, and vars;2 is exactly what I want. Some smaller files just have one copy vars;1 and that is the whole thing. Why does this happen (I am guessing some sort of memory limit issue or an automatic back-up kind of thing), and is there a way to suppress / get rid of vars;1?

ROOT User’s Guide -> Object Ownership -> Ownership by Current Directory (gDirectory)
ROOT User’s Guide -> Input/Output -> The Logical ROOT File: TFile and TKey -> Subdirectories and Navigation

That’s what TKey is about. Understood.

Thank you very much!