Filling New Branches in a Tree with Fewer Entries Than Other Branches in the Same Tree

I am writing a macro to read variables from an existing root file, create new branches, and calculate variables for those branches from variables in the existing branches. The new branches should have a smaller number of entries filled than the old branches. This is based on some event selection criteria while looping through the entries based on the values of the existing branches. For each entry, if certain event selection criteria are not met, then I have a continue command which skips the tree->Fil() command…

My results for the new branches show a distribution like I would expect, with the exception of a huge spike at a certain value. Furthermore, the new branches have the same number of values as the existing branches. My macro is populating the entries that do not meet the event selection criteria with some value, even though that loop skips the tree_>Fill() command. I am not sure where I am going wrong here. Any help would be appreciated. A simplified outline of my code is below. Thanks.

void AddVariables(){

const int cp_err = gSystem->Exec(“cp .root _NEW.root”);
if (-1 == cp_err){ printf(“Error copying input file!n”); exit(1); }

TFile input1 = TFile::Open( “_NEW.root”, “UPDATE” );
TTree
inputTree = (TTree*)input1->Get( “” );

//Declare all variables
Double_t Var;
Double_t X; //existing variable used to calculate Var

//Create new branches for calculated variables
TBranch *bVar = inputTree->Branch(“Var”,&Var,“Var/D”);

//Create branch addresses for caclulated variable and variables used to calculate it
inputTree->SetBranchAddress(“Var”,&Var); //calculated
inputTree->SetBranchAddress(“X”,&X); //existing

Long64_t nentries = inputTree->GetEntries();

//Populate tree with calculated variables
for (Long64_t i_entries=0;i_entries<nentries;i_entries++) {

inputTree->GetEntry(i_entries);

//Conditions to be met
if (condition) continue;

//calculate Var;
Var = <f(X)>;

bVar->Fill();

}

inputTree->Print();
inputTree->Write();
delete input1;

}

You must create a completely new tree for your new branch.

Thank you so much! That totally worked.