Retrieving an object from a branch in a TNtuple


I have this code using an ntuple but I can’t seem to get the right results. I’m using this to draw hits inside a calorimeter. Each hit here has a corresponding id, which is labeled here as rid and mid. This id would then be the basis for the color of each hit. The position of a hit is labeled x, y, and z, and n is the event number.

When the hits are drawn, it should have different colors based on their rid (or mid). Unfortunately, the hits are drawn only with a single color.

My problem here now is how to get the rid (or mid) for a single hit and draw them correctly with their corresponding color (which is either rid or mid). A single event here would consist of a fewer than a hundred to more than 2000 hits.

I hope you could help. Thank you very much!

Best Regards,


TFile *infile = new TFile("$mst/new_mst/anl-uds91-jun06_1x1_2m-500-1.root","READ");
TNtuple *dat = (TNtuple*)infile->Get("nodnt");

Float_t n,rid,mid,x,y,z;


TH1D *hN = new TH1D("hN","n",100,0,100);

Int_t counter = 0;
for (Int_t ievt = 1; ievt < 2; ievt++) {
    cout << "procecssing event: " << ievt <<endl>Project("hN","n",Form("n==%i",ievt));
    cout<<"number of hits = \t"<<nPts<<endl>SetMarkerStyle(21);

    for(Int_t hit = 0; hit <nPts>GetEntry(count);
	Int_t color = rid;  // mid

	p->SetPoint(hit, x, y, z);

// p->Draw();



You should create a TPolyMarker for each color or a TMarker for each entry
or better replace your code by one single line


After a few weeks of coffee and asking from friends, the problem has been solved.

	TFile * inputFile = new TFile("$mst/new_mst/anl-uds350-aug06_1x1_2m-500-1.root","read");
	TNtuple * data = dynamic_cast<TNtuple>(inputFile->Get("nodnt"));
	Float_t x, y, z, mid, rid;
	Float_t n = 0;
	data->SetBranchAddress("n", &n);
	data->SetBranchAddress("x", &x);
	data->SetBranchAddress("y", &y);
	data->SetBranchAddress("z", &z);
	data->SetBranchAddress("mid", &mid);
	data->SetBranchAddress("rid", &rid);

	TH1D *hN = new TH1D("hN","n",100,0,100);

	Int_t counter = 0;
	for (Int_t iev = 1; iev <1>Project("hN","n",Form("n==%i",iev));
	    cout << "nPts = \t" << nPts << endl;	    

	    for (Int_t clu = 0; clu <data>GetMaximum("mid"); clu++){
		TPolyMarker3D * p = new TPolyMarker3D;

	    for (Int_t ipt = 0; ipt <nPts>GetEntry(counter++);

		//---this is it!----------------------------
		if (clu != mid) break;
		if (clu == mid) p->SetPoint(ipt, x, y, z);

		cout << "mid = \t" << mid <<endl>Draw("same");

There may be redundancy in the colors, but at least it works. :laughing: