Adding new branch to pre-existing TTree


I’m trying to add a new branch constructed from previous branches already stored in a pre-existing TTree to that same TTree. The new branch is just a “smeared” version of the old. I do this by sampling a point in phi-pt space from a pre-existing TH2F.

TString baseString = "location/to/my/file.root";
///read in TH2F histos
TFile *fSyst = new TFile("HistosForSystematics.root","read");
TFile *fIn = new TFile(baseString,"update");

TTree* tree = (TTree*)fIn->Get("tree");

float mpt,mptPhi,mptMod,mptPhiMod;

TBranch *b_nu_ptMod = tree->Branch("nu_ptMod",&mptMod,"nu_ptMod/F");
TBranch *b_nu_phiMod = tree->Branch("nu_phiMod",&mptPhiMod,"nu_phiMod/F");

tree->SetBranchAddress("nu_phi", &mptPhi);
tree->SetBranchAddress("nu_pt", &mpt);
tree->SetBranchStatus("*",0) ;
tree->SetBranchStatus("nu_pt", 1);
tree->SetBranchStatus("nu_phi", 1);

const int nEntries = tree->GetEntries();
for(int iev=0; iev<nEntries; ++iev){

       Double_t sampledDelPhi=0.0,sampledDelPt=0.0;     
       TString sHistName ="h2DHisto";
       TH2* hSyst = NULL;
       hSyst = (TH2*)fSyst->Get(sHistName);

  delete fIn;
  delete fSyst;

However, when printing the tree the number of entries in the new branches is always 0. Any ideas?

Problem solved. For those of you interested, in my macro I was calling

tree->SetBranchStatus("*",0) ;
tree->SetBranchStatus("nu_pt", 1);
tree->SetBranchStatus("nu_phi", 1);

However, being that I just added 2 new branches, these were turned off and resulted in strange errors when I would try to draw the variable like:

root [2] tree->Draw("dummy")
Error in <TTreeFormula::DefinedVariable>: the branch "nu_ptMod" has to be enabled to be used
Error in <TTreeFormula::Compile>:  Part of the Variable "nu_ptMod" exists but some of it is not accessible or useable

So the fix was to just turn on the newly created branches along with the pre-existing ones.

tree->SetBranchStatus("nu_ptMod", 1);
tree->SetBranchStatus("nuphiMod", 1);