Same error keeps appearing even when macro is changed


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.24
Platform: macOS Monterey
Compiler: clang


Hi,

I wrote a macro (below) to add a branch to an existing tree

void makeAvgBranch() {
    TFile* inFile = new TFile("somefile.root", "update");
    TTree* tree = static_cast<TTree*>(inFile->Get("someTree"));
    tree->Print();
    Double_t newBranch;
    tree->Branch("avg", &newBranch);
    Double_t data_a, data_b;
    tree->SetBranchAddress("a", &data_a);
    tree->SetBranchAddress("b", &data_b);
    for (int i = 0; i < tree->GetEntries(); i++) {
        tree->GetEntry(i);
        newBranch->Fill(0.5*(data_a + data_b));
    }
    tree->Print();
}

This version produces the following error:

root [0] 
Processing makeAvgBranch.C...
In file included from input_line_8:1:
/path/to/makeAvgBranch.C:12:20: error: member reference type 'Double_t' (aka 'double') is not a pointer
        newBranch->Fill(0.5*(data_a + data_b));
        ~~~~~~~~~  ^
root [1] 

So, to fix this error, I changed to above macro to the following:

void makeAvgBranch() {
    TFile* inFile = new TFile("somefile.root", "update");
    TTree* tree = static_cast<TTree*>(inFile->Get("someTree"));
    tree->Print();
    Double_t newBranch;
    TBranch* avgBranch = tree->Branch("avg", &newBranch);
    Double_t data_a, data_b;
    tree->SetBranchAddress("a", &data_a);
    tree->SetBranchAddress("b", &data_b);
    for (int i = 0; i < tree->GetEntries(); i++) {
        tree->GetEntry(i);
        avgBranch->Fill(0.5*(data_a + data_b));
    }
    tree->Print();
}

However, the same error still shows up. I made some other changes like commenting out everything below the first tree->Print(); line, but no matter what I do, the same error keeps popping up. How do I fix this?

newBranch = 0.5 * (data_a + data_b); avgBranch->Fill();

with “newer” versions of ROOT (starting with v6.14), it’s recommended to use:
newBranch = 0.5 * (data_a + data_b); avgBranch->BackFill();

BTW. After the “for” loop, you need: tree->Write();

The same error still pops up like it didn’t register that I made changes to the macro file.

I deleted the old file and rewrote a new one. This seems to fix my issues. Thank you for your help!

1 Like

Note: the BackFill is available for ROOT v6.14 and newer.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.