Projecting single tree to two histograms

Hi experts,
I am a beginner in root. when I try to project my output root tree named events to two histograms named test2 and test3,in a single canvas. However, in output only test2 is working. I hereby give my code. can anyone help me ?

#include <iostream>
#include <vector>

void DrawTrack(const char* file_name){
       //fle_name="gun_g4sim.root"

   TCanvas *c1 = new TCanvas("c1","PionTrack",600,600);
        TDirectory *where = gDirectory;
	TH2D *test2 = new TH2D("test2","Track of pions",300,-300,300,300,-300,300);
        TH2F *test3 = new TH2F("pion decay","Decay_of_primary_pion2",300,-300,300,300,-300,300);
	TFile *_file0 = TFile::Open(file_name);
 	TTree *events = (TTree*)_file0->Get("events");
	where->cd();
	test2->SetMarkerStyle(8);
	test2->SetMarkerSize(1);
	test2->SetMarkerColor(4);
	test2->GetXaxis()->SetTitle("X [mm]");
	test2->GetYaxis()->SetTitle("Y [mm");
	events->Project("test3","tpc_positionedHits.position.y:tpc_positionedHits.position.x","tpc_positionedHits.core.bits==1","");
        test2->Draw();
        events->Project("test2","tpc_positionedHits.position.y:tpc_positionedHits.position.x","tpc_positionedHits.core.bits==1","same");
        test3->SetMarkerStyle(2);
	test3->SetMarkerSize(1);
	test3->SetMarkerColor(3);
        test3->Draw("same");
	TEllipse *el1 = new TEllipse(0.,40.,41.005,41.005,365.0,0.0);
        el1->SetFillStyle(0);
        el1->SetLineColor(2);
        el1->SetLineWidth(3);
	//el1->SetFillColorAlpha(kWhite, 1.0);
   	el1->Draw("same");

        TEllipse *beampipe = new TEllipse(0, 40, 13.6, 0.0, 0.0, 365.0, 0.0);
	beampipe->SetFillStyle(0);
	beampipe->SetLineColor(4);
	beampipe->SetLineWidth(5);
        beampipe->Draw("same");
         TLatex *lsolid = new TLatex(-278, 250, "Pions with momentus 75MeV");
         lsolid->SetTextSize(0.022);
         lsolid->Draw("same");
	
	TEllipse *el2 = new TEllipse(0.,20.,199.5,199.5,365.0,0.0);
        el2->SetFillStyle(0);
        el2->SetLineColor(3);
        el2->SetLineWidth(3);
	el2->Draw("same");

}

ROOT Version: 6.13
Platform: Ubuntu 16.04
Compiler: Not Provided


You must set the name of “test3” to “test3” (not “pion decay”) as events->Project(...); uses histogram names.
Your “test2” and “test3” histograms are exactly equal (the same selection is used in both cases).
Do not use “same” in events->Project(...);.

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Thank you very much. It’s working now. Actually the second projection have a small difference(it was typing mistake). For test2 last expression is “tpc_positionedHits.core.bits==1” and for test3 it is “tpc_positionedHits.core.bits==9”. Once again thank you very much for help and advice.

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