I’m projecting sub-ranges of 2-D histogram on X and Y axis and plotting them separately in one single canvas. When I click on top_pad or X projection it suddenly changes to Y projection or right_pad surprisingly. Maybe I’m missing something in my code. Any help will be appreciated.
void hist_proj()
{
TCanvas *c1 = new TCanvas("c1","c1",900,900);
gStyle->SetOptStat(0);
TPad *center_pad = new TPad("center_pad","center_pad",0.0,0.0,0.6,0.6);
center_pad->Draw();
TPad *right_pad = new TPad("right_pad","right_pad",0.55,0.0,1.0,0.6);
right_pad->Draw();
TPad *top_pad = new TPad("top_pad","top_pad",0.0,0.55,0.6,1.0);
top_pad->Draw();
TH2F *h2 = new TH2F("h2","",40,-4,4,40,-5,5);
Float_t x,y,z;
Int_t nentries = 10000;
gRandom->SetSeed();
for (Int_t i=0;i<nentries;i++) {
gRandom->Rannor(x,y);
z = x*x+y*y;
h2->Fill(x,y,z);
}
center_pad->cd();
gStyle->SetPalette(1);
h2->Draw("COL");
TCutG *cutg = new TCutG("mycut",5);
cutg->SetVarX("");
cutg->SetVarY("");
cutg->SetPoint(0, -2.0, 2.0);
cutg->SetPoint(1, -2.0, -2.0);
cutg->SetPoint(2, 2.0, -2.0);
cutg->SetPoint(3, 2.0, 2.0);
cutg->SetPoint(4, -2.0, 2.0);
cutg->Draw("l");
TCutG *cutg2 = new TCutG("mycut2",5);
cutg2->SetVarX("");
cutg2->SetVarY("");
cutg2->SetPoint(0, -1.0, 1.0);
cutg2->SetPoint(1, -1.0, -1.0);
cutg2->SetPoint(2, 1.0, -1.0);
cutg2->SetPoint(3, 1.0, 1.0);
cutg2->SetPoint(4, -1.0, 1.0);
cutg2->Draw("l");
// h2->Draw("CONT [mycut]");
// h2->Draw("SURF1 FB BB A [mycut]");
c1->Update();
top_pad->cd();
TH1D *hpx = h2->ProjectionX("", 1, 40, "[mycut]");
hpx->SetFillColor(kBlue-2);
hpx->SetTitle("X-Projection");
hpx->Draw("bar");
c1->Update();
c1->cd();
TLatex *t = new TLatex();
t->SetTextFont(42);
t->SetTextSize(0.02);
t->DrawLatex(0.6,0.86,"This example demonstrate how to display");
t->DrawLatex(0.6,0.88,"a histogram and its two projections.");
c1->Update();
right_pad->cd();
TH1D *hpy = h2->ProjectionY("", 1, 40, "[mycut2]");
hpy->SetFillColor(kBlue+2);
hpy->SetTitle("Y-Projection");
hpy->Draw("hbar");
c1->Update();
}
as I explained above, after plotting it if I click on top_pad it changes to right_pad.