How to fill tree in TSelector?

I want to create tree of selected events with TSelector class. I have Selector.h with the following methods and data members:

class Selector : public TSelector {
  public :
     int good_count;
     float e_tot, p_tot;
     float p_prj[3];
     TLorentzVector pgood[5];
  
     TTree* Selected;
     ...
     TTreeReaderArray<Float_t> phen = {fReader, "phen"};
     TTreeReaderArray<Float_t> phth = {fReader, "phth"};
     TTreeReaderArray<Float_t> phphi = {fReader, "phphi"};
     ...
};

In Selector.C I have:

void Selector::Begin(TTree * /*tree*/)
  {
    TString option = GetOption();
  
    TTree *Selected = new TTree( "Selected", "Selected" );
      Selected->Branch( "phen", &phen, "phen/F" );
      Selected->Branch( "phth", &phth, "phth/F" );
      Selected->Branch( "phphi", &phphi, "phphi/F" );
      Selected->Branch( "e_tot", &e_tot, "e_tot/F" );
      Selected->Branch( "p_tot", &p_tot, "p_tot/F" );
      Selected->Branch( "p_prj", &p_prj, "p_prj[3]/F" );
 }

And

Bool_t Selector::Process(Long64_t entry)
{
//Some selection criteria
  Selected->Fill();
  return kTRUE;
}

Then after:

root>tr_ph->Process("Selector.C")

I have the following error:

Error in <HandleInterpreterException>: Trying to access a pointer that points to an invalid memory address..
Execution of your code was aborted.
In file included from input_line_51:1:
/home/sikach/Diploma/Analysis/KinFitter/Selector.C:122:2: warning: invalid memory pointer passed to a callee:
        Selected->Fill();
        ^~~~~~~~

How to fill a tree with Selector properly?

Hi,

Try with:

void Selector::Begin(TTree * /*tree*/)
  {
    TString option = GetOption();
  
      Selected = new TTree( "Selected", "Selected" );
      Selected->Branch( "phen", &phen, "phen/F" );
      Selected->Branch( "phth", &phth, "phth/F" );
      Selected->Branch( "phphi", &phphi, "phphi/F" );
      Selected->Branch( "e_tot", &e_tot, "e_tot/F" );
      Selected->Branch( "p_tot", &p_tot, "p_tot/F" );
      Selected->Branch( "p_prj", &p_prj, "p_prj[3]/F" );
 }

Cheers, Bertrand.

Seems like it solved old problem. But now there is one new:

root [2] tr_ph->Process("Selector.C")
Error in <TTree::Fill>: Failed filling branch:Selected.p_prj, nbytes=-1, entry=2660
 This error is symptomatic of a Tree created as a memory-resident Tree
 Instead of doing:
    TTree *T = new TTree(...)
    TFile *f = new TFile(...)
 you should do:
    TFile *f = new TFile(...)
    TTree *T = new TTree(...)
Error in <TTree::Fill>: Failed filling branch:Selected.p_prj, nbytes=-1, entry=5320
 This error is symptomatic of a Tree created as a memory-resident Tree
 Instead of doing:

This message repeats for each Branch and I do not even know where I use TFile at all!

I think the message is explicit enough: You must create a TFile before creating your TTree:

you should do:
    TFile *f = new TFile(...)
    TTree *T = new TTree(...)

I do not think so. But I will try.

Obviously, in your case, it should look like:

TFile *f = new TFile(...);
Selected = new TTree( "Selected", "Selected" );

Surprisingly, It works. TTree Selected was written to file I created. But there is another problem: some branches did not filled (actually, filled with very small numbers). Exactly, it were branches that initial tree has. I.e. phen,phth and phphi. I think I am not able just write:

Selected->Branch("phen", &phen, "phen[nphgood]/F");

and so on in this case.

We don’t have enough info. I don’t see where phen is declared. I don’t understand why you fill a tree (TTree::Branch()) and read it using the TTreeReader seemingly at the same time (a TSelector is used to read, not to fill).

I am confused, maybe your code is, too :slight_smile:

Axel.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.