Skipping empty entries in a TBranch

Hello all,

I’m trying to read some information on a branch from one TTree (t) and write it to another TTree (t1). However, my code crashes when it finds an empty entry (I think), and the seg fault doesn’t say anything helpful. Below is my piece of code, and I have also added the result of scanning the branch I’m trying to read. You can see that there are some rows with no entries, and that I think is the problem. How can I direct my script to skip these empty entries? The input root file is on eos.

//Getting input file and tree:
TFile f1("/eos/experiment/fcc/ee/generation/DelphesEvents/fcc_v01/p8_ee_ZH_ecm240/events_000038119.root")  ;
  TTree *t = (TTree*)f1.Get("events");
  int n = t->GetEntries();

  //Create output file and tree
  TFile f("FCCee_ZH.root","recreate");
  TTree t1("events","output tree");
  float px, phi; 
  float px0, phi0;

  //Get my branches of interest
  //t->SetBranchAddress("met.phi",&phi0);
 //The code works fine when I look at met.phi because there are no empty entries there.
  t->SetBranchAddress("muons.core.p4.px",&px0);                                                                                                         

  //Create the branches on the new tree
  //t1.Branch("met_phi",&phi,"phi/F");
  t1.Branch("muon_px",&px,"px/F");        

  //Fill the new tree
  for (int i=0; i<n; i++) {                       
    t->GetEntry(i);
    if (t->GetEntry(i)<0 || t->GetEntry(i)==0) continue;                                                                                                                  
    phi = phi0;
    t1.Fill();
  }
//Write new tree to file f
t1.Write();
//Output of events->Scan("muons.core.p4.px")
/*
***********************************
*    Row   * Instance * muons.cor *
***********************************
*        0 *        0 * 8.0309610 *
*        1 *        0 *                   *
*        2 *        0 *                   *
*        3 *        0 *                   *
*        4 *        0 *                   *
*        5 *        0 *                   *
*        6 *        0 *                   *
*        7 *        0 * 9.3634538 *
*        8 *        0 * 14.266631 *
*        8 *        1 * 10.907753 *
*        9 *        0 *                   *
*       10 *        0 * 54.991462 *
*       10 *        1 * 1.7402488 *
*       11 *        0 * -0.980190 *
*       12 *        0 *                  *
*       13 *        0 *                  *
*       14 *        0 * 4.2284102 *
*       15 *        0 *                  *
*       16 *        0 * -1.219771 *
*       16 *        1 * -2.019493 *
*       17 *        0 *                  *
*       18 *        0 * -8.203149 *
*       19 *        0 * 55.774704 *
*       19 *        1 * -40.93004 *
*       20 *        0 *                  *
..............................................
*/

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Because of

***********************************
*    Row   * Instance * muons.cor *
***********************************
...
*        8 *        0 * 14.266631 *
*        8 *        1 * 10.907753 *

contains the word “Instance” and has some row with several values for the same entry/row number, we know that the muon_px branch actually contains an array.

But the code:

  float px0, phi0;
...
  t->SetBranchAddress("muons.core.p4.px",&px0);   

only gives space for one float for the branch to write its data in. The net result is that when the array has more than one element (for example the entry #8), the branch is doing a memory overwrite … resulting in random behavior … include possible crashes.

You need to use something like

  float px0[maximum_number_element_in_the_array];
  t->SetBranchAddress("muons.core.p4.px",&px0);   

Cheers,
Philippe.

PS. You could also consider using RDataFrame which will avoid having to guess/know the size of the array.

1 Like

Hi @pcanal , sorry for the delayed response. I used RDataFrame as you suggested, and it solved my problem. Thank you.

1 Like