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

**URL:** <https://root-forum.cern.ch/t/filling-new-branches-in-a-tree-with-fewer-entries-than-other-branches-in-the-same-tree/35484>\
**Category:** Newbie\
**Created:** [August 3, 2019, 2:35pm UTC](https://root-forum.cern.ch/t/filling-new-branches-in-a-tree-with-fewer-entries-than-other-branches-in-the-same-tree/35484 "2019-08-03T14:35:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![regulus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/9e8a1a/32.png) [@regulus](https://root-forum.cern.ch/u/regulus)\
**Post date:** [August 3, 2019, 2:35pm UTC](https://root-forum.cern.ch/t/filling-new-branches-in-a-tree-with-fewer-entries-than-other-branches-in-the-same-tree/35484/1 "2019-08-03T14:35:57Z")

</div>

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;

}

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 3, 2019, 2:50pm UTC](https://root-forum.cern.ch/t/filling-new-branches-in-a-tree-with-fewer-entries-than-other-branches-in-the-same-tree/35484/2 "2019-08-03T14:50:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![regulus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/9e8a1a/32.png) [@regulus](https://root-forum.cern.ch/u/regulus)\
**Post date:** [August 3, 2019, 8:23pm UTC](https://root-forum.cern.ch/t/filling-new-branches-in-a-tree-with-fewer-entries-than-other-branches-in-the-same-tree/35484/3 "2019-08-03T20:23:22Z")

</div>

Thank you so much! That totally worked.
