Home | News | Documentation | Download

Reading from TTree

Hello!
Im a beginner in ROOT. I have two tasks:

  1. Read from the file” model_event.root " a branch with particle collision events containing objects of the Event class (the file Event.cxx).
  2. Build a two-dimensional histogram with the number of intervals (bin) equal to 50 and for the module radius of the vertex vector(√𝑉𝑥2+𝑉𝑦2+𝑉𝑧2) and full momentum (√𝑃𝑥2+𝑃𝑦2+𝑃𝑧2).
    I attached a script with my attempt to solve problems, I can’t read the branch with Px, Py and Pz.
    Help me, please!
    https://drive.google.com/drive/folders/1jjx54Lbo5nmAB6-rPpTQ5Fib4fskPi1B?usp=sharing

Hi,
Can you attach here the script? This way we don’t need to request access to your google doc. Thanks!

I get the projections of the radius vector, but I can’t read the projections Px, Py and Pz

/**************** Task #3****************/

int myTree(void){
	
	//new TBrowser();

	TFile *f= TFile::Open("model_event.root");
	TTree *t=(TTree*)f->Get("EventTree");

	Event* myEvent = 0;
	//Track* myTracks = 0;
	//TBranch *nTrack = t->GetBranch("fTracks");

	double fVx=0,fVy=0,fVz=0,fPx=0,fPy=0,fPz=0;

	t->SetBranchAddress("ModelEvent",&myEvent);
	//t->SetBranchAddress("fTracks",&myTracks);
	
	TH2D *h1 = new TH2D("h1","title",50,-3,3,50,0,5);
	TH1D *h2 = new TH1D("h2","title",80,-3,3);

	int k=(int)t->GetEntries();
	double mod_V, mod_P;

	for (Int_t i=0;i<k;i++){ 

		t->GetEntry(i);
		fVx = myEvent->GetVx();
		fVy = myEvent->GetVy();
		fVz = myEvent->GetVz();

		fPz = myEvent->fTracks->GetPsz();
		

		//for(Int_t j=0;j<k;j++){
			
			//t->GetEntry(j);
			//fPx = myEvent->GetTrack()->GetPx();
			
		//}
	}
	// h1->Draw("lego");
	//h2->Draw();
	
	return 0;
	

}// myTree
/********************END******************/

Tree structure looks like this

Hi,

Can you also paste here the output of running TTree::Print ?

https://root.cern.ch/doc/master/classTTree.html#a7a0006d38d5066b533e040aa16f97094

What is the type of fTracks, a TClonesArray? It looks like myEvent->GetTrack() should give you back some array-like data stored for that event. So you should not run a nested loop that calls again t->GetEntry, in that nested loop you should just iterate on the array you got and get the properties of its elements.

I don’t understand how to iterate over the resulting array and get the properties of its elements.
How is this written in code?

I would try:

auto k = myEvent->fNTracks;
for(Int_t j=0;j<k;j++) {		
	fPx = myEvent->fTracks[j].fPx;
}

thank you for your help!
I finally understood how to solve my problem

this code works as it should:

TFile *f= TFile::Open(“model_event.root”);
TTree t=(TTree)f->Get(“EventTree”);

Event* myEvent = 0;
Track* myTracks = 0;

double fVx=0,fVy=0,fVz=0,fPx=0,fPy=0,fPz=0;

t->SetBranchAddress("ModelEvent",&myEvent);

TH2D *h1 = new TH2D("h1","title",50,-3,3,50,0,5);
TH1D *h2 = new TH1D("h2","title",80,-3,3);

int k=(int)t->GetEntries();
double mod_V, mod_P;
auto k1 = myEvent->GetTracks();
//t->Print();

for (Int_t i=0;i<k;i++){ 

	t->GetEntry(i);
	fVx = myEvent->GetVx();
	fVy = myEvent->GetVy();
	fVz = myEvent->GetVz();


	for(int j=0;j<myEvent->GetNTrack();j++){
		
		fPx = myEvent->GetTrack(j)->GetPx();			
		h2->Fill(fPx);
		
	}
}
	
//myEvent->GetTracks()->First()->Dump();
// h1->Draw("lego");
h2->Draw();

return 0;

Good to hear! Thanks for sharing the solution.

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