Content in pad changes after left clicking

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.

I’ll check

I managed to reproduce the problem with a simpler macro:

void hist_proj() {
   auto c1 = new TCanvas("c1","c1",900,500);
   c1->Divide(2,1);

   auto h2 = new TH2F("h2","",40,-4,4,40,-5,5);
   float x,y;
   gRandom->SetSeed();

   for (int i=0; i<10000; i++) {
      gRandom->Rannor(x,y);
      h2->Fill(x,y,x*x+y*y);
   }

   c1->cd(1);
   TH1D *hpx = h2->ProjectionX("", 1, 40);
   hpx->SetFillColor(kBlue-2);
   hpx->SetTitle("X-Projection");
   hpx->Draw("hist");

   c1->cd(2);
   TH1D *hpy = h2->ProjectionY("", 1, 40);
   hpy->SetFillColor(kRed);
   hpy->SetTitle("Y-Projection");
   hpy->Draw("hist");

   c1->ls();
}

when the Y projection is created and drawn the X one on the left pad is replaced by the Y one.

@couet Do NOT use empty "" names (histograms will get overwritten if the same names are used).

Excatly I just noticed it too.

you should do:

   TH1D *hpx = h2->ProjectionX("px", 1, 40);
...
   TH1D *hpy = h2->ProjectionY("py", 1, 40);
...
1 Like

Thank You!! Got it!!

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