Pointer to the event in TChain


I am using TChain to process root files. So, I get events in the chain by chain->GetEntry(i). I want to get the pointer to the event in the chain so that I can give this event to another class to do some thing. It is like this:

TChain chain(“tree”);

Event *event = new Event();
chain.SetBranchAddress(“tree”, &event);
Int_t nevent = chain.GetEntries();

Analysis *analysis = new Analysis();

for (Int_t i=0; i<nevent; i++) {
cout << "doing event " << i+1 << endl;

analysis->Make() will process that event. How can I put the pointer to the event in the class Analysis or how do the class Analysis read the event?

Thank you very much.

I am not sure to understand your question. Simply add a member
Event *event in your class Analysis.
I suggest to add the TChain parameter in the constructor of your Analysis class as we do in the code generated by TTree::MakeSelector


Hi Rene,

Thank you for your suggession. I put the Event *event member in class Analysis but it does not work. It crashed. How does the “event” in Analysis class get the event from the chain? Could you please give an example. I will appreciate it.

Thank you.

[code]Int_t nevent = chain.GetEntries();

Analysis *analysis = new Analysis();
analysis->event = new Event();
chain.SetBranchAddress(“tree”, analysis->event);


This question is similar to the above post. So, I thought to put this here.

Event *event = new Event();

chain->SetBranchAddress(“tree”, &event);

int nevent = chain->GetEntries();
std::vector<Event*> eventVtr;
std::vector<Event*>::iterator itr;

for(int i=0; i<nevent; i++){
std::cout << "doing event " << i+1 << std::endl;
cout << event->GetX() << endl; // this works fine

for(itr = eventVtr.begin(); itr != eventVtr.end(); itr++){
Event *evt = *itr;
cout << evt->GetX() << endl; // this does not work

In the above code, I tried to collect the “event” object in a vector and then print it. But this does not work. It gives me some arbitrary large (same value) number.

Any idea will be appreciated.


tree->GetEntry will either re-use the Event object currently in memory or is also allow to delete and reallocate. So you array pointer will in the best case, hold the same value nevent time or hold some pointer to delete Events …

If you intend is to keep several Event object in memory you would need to use some various of:eventVtr.push_back(new Event(*event));.
Alternatively it may sometime be more efficient (depending on the Event size, your RAM size, the speed of your disk, the amount of disk cache, the number of events, etc…) to simply record the entry number themselves and re-read the events …