Problems reading a custom Event class with a TSelector

Hello everyone, I am having a problem with something I expected to be working quite easily, and I am quite lost as to what is happening.

I have some data in a ROOT tree that i want to modify (apply calibrations, …) and store in another tree, on which I want to perform the rest of the analysis.
I used 2 different selectors, one called Filter for the first tree and another called Sort for the second.

I found it convenient to use a “CalEvent” object (defined in Event.C and Event.h) to store all the data I wanted in the new tree, so I used this syntax to create the branch (in Filter::Begin):

  ev = new CalEvent();
// ...
    outputtreefile = new TFile("work/tmptreeout.root","recreate");
    outputtree = new TTree("data","data");
    outputtree->Bronch("e.","CalEvent",&ev);

In Filter::Process I then calculate the values of the members of ev and call the Fill method of outputtree, and in Filter::End I save the file.

I made sure I could read the data stored in tmptreeout.root correctly by writing this in the ROOT command line:

TChain *c = new TChain("data")
c->Add("work/tmptreeout.root")
TBranch *b
b = c->GetBranch("e.")
.L Event.C++
CalEvent *e = 0;
b->SetAddress(&e)
c->GetEntry(6)
cout<<e->Si_E.pad[5][5]<<endl;

the last line outputs the correct value for the variable in that event.

I then made a macro called RunSort to open the tree and lauch the Process method with the Sort selector and some options, loading the Event class before that.

void RunSort()
{
  gROOT->LoadMacro("Event.C++");
  TChain *c = new TChain("data");
  inputfilename = "work/tmptreeout.root";
  files_added = c->Add(inputfilename);
  c->Process("Sort.C++",cline);
}

This is a snippet form Sort.h:

class Sort : public TSelector {
public :
   TTree          *fChain;   //!pointer to the analyzed TTree or TChain
   TBranch   *b_ev;   //!
   CalEvent *ev;

   ClassDef(Sort,0);
};

void Sort::Init(TTree *tree)
{
   if (!tree) return;
   fChain = tree;
   fChain->SetMakeClass(1);

     if (!TClassTable::GetDict("CalEvent")) {
       cout<<"Dict for CalEvent not found!"<<endl;
     }   

     b_ev = fChain->GetBranch("e.");
     if(!b_ev) cout<<"!b_ev"<<endl;
     b_ev->SetAddress(&ev);
 
     cout<<"BranchStatus "<<fChain->GetBranchStatus("e.")<<endl;
}

As far as I can see, this should be more or less equivalent to what I wrote in the ROOT command line. The problem is that if I put

in Sort::Process, the ouput value is completely wrong (it’s always 6.78869e+199, for any event).
I am completely lost as to what could be the reason, any suggestion?

I made sure that none of the pointers I am using is null (also because I don’t have any segmentation fault or other runtime error, the macro executes fine)

Thanks for the attention
Roberto

Hi,

When using a TChain (and thus this is the general recommendation in all cases), use SetBranchAddress rather than SetAddress as the branch object has a shorter lifetime. So use: fChain->SetBranchAddress("e.",&ev,&b_ev);.

Cheers,
Philippe.

PS. Also make sure that ‘ev’ is initialized to zero.

Hi Philippe,
I was using SetBranchAddress at first, I separated GetBranch and SetAddress only to make sure that the branch object was read. I also tried both initializing ev to 0 and to a new CalEvent(), but it doesn’t work either way…

[quote]I was using SetBranchAddress at first, I separated GetBranch and SetAddress only to make sure that the branch object was read.[/quote]Humm … do you mean that once you go back to SetBranchAddress (this is necessary), the problem is there?

Philippe.

Yes, I put back the SetBranchAddress but I still get 0 in all the members of the event class

Humm … Try removing all calls to SetMakeClass (and/or pass 0: fChain->SetMakeClass(0);).

Philippe.

I added fChain->SetMakeClass(0); in the Init() method and now it works!
Thank you very much!

HI,

Okay, so the problem was that the TTree was set in ‘decomposed my objects’ mode (SetMakeClass(1)) which is, of course, incompatible with being able to load the information into an object.

Cheers,
Philippe.