Sorting branch entries

Hello!

I want to read different branch entries, sort them and fill them into another root file. My code looks like this:

void sort(){

  UInt_t read_branch1, read_branch2;
  UInt_t write_branch1, write_branch2;

  // 1. read root file
  TFile *file = new TFile("read.root", "OPTION");
  TTree *tree = (TTree*)file->Get("fullset");
  tree->SetBranchAddress("to_read_1", &read_branch1);
  tree->SetBranchAddress("to_read_2", &read_branch2);

  // 2. create new root file
  TFile *write_file = new TFile("write.root", "RECREATE");
  TTree *write_tree = new TTree("fullset", "fullset tree");
  write_tree->Branch("new_read_1", &write_branch1);
  write_tree->Branch("new_read_2", &write_branch2);

  TBranch *write_branch1 = (TBranch*)write_tree->GetBranch("new_read_1");
  TBranch *write_branch2 = (TBranch*)write_tree->GetBranch("new_read_1");

  // now sort entries from first file and save in second file
  for(UInt_t i = 0; i <= 10; i++){
    tree->GetEntry();
    if(read_branch1 >= 3){
      write_branch1 = read_branch1;
      std::cout << write_branch1 << " ";
      write_branch1->Fill();
    }
    if(read_branch2 == 2){
      write_branch2 = read_branch2;
      std::cout << write_branch2;
      write_branch2->Fill();
    }
    std::cout << std::endl;
  }
  write_tree->Write();
}

This works so far but I don’t know if it does the thing I intend to do. I tried to write an output but if I want to read the branch entries with something like write_branch1->GetEntry(i) I always get the same number (eg 4). Only the amount of the number changes in my written file. Which is ok because of my selection I have less entries.

I read the branch entry before and after the selektion and my output looked like this:

without selektion
branch1 branch2
4 4
4 4
4 4
4 4
4 4
4 4
4 4
4 4
4 4
4 4
4 4
output during selektion
branch1 branch2
12 2
12
12
12
12 2
12 2
12
12
12 2
12
12
with selektion
branch1
4 4 4 4 4 4 4 4 4 4 4
branch2
4 4 4 4

My output during the selektion shows that it worked. But if I want to read the entries from the file I only geht those 4. Why?

Hi,

why are you interested in sorting your dataset according to a certain column values?

Cheers,
H

I don’t understand how you are ‘sorting’ the entries in that code, but inside the ‘sorting’ loop, by this

  tree->GetEntry();

don’t you mean

  tree->GetEvent(i);

?