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.