Question related with using TH2 class

Dear ROOTers,

Sorry if my questions had been discussed previously.

  1. My first question is related to the following problem:
    I want to display a respons of electromagnetic calorimeter (EMC) by using
    TH2 class. But my EMC has a hole inside its middle part. And I would want
    to take it into account, i.e. visualize this feature. For this, I was trying
    to apply TCutG class as follows (see macro emc.C in attachment). The
    resulting picture is not what I would want. I want the hole to be placed
    on a zero level, and a boundary of the hole to have exactly the same
    definition as made by “hole->SetPoint”. Could you advice me what
    I’m doing wrong? Maybe there exists more elegant and more inexpensive
    way (method) to implement that (a hole inside a histogram).

  2. My second question: is it feasible somehow to colour only a part
    of histogram? For instance I want to isolate different clusters by
    different colours. Again it looks like I need to use TCutG class.
    Does something else exist to implement that?

Best regards,
Alexander
emc.C (1.01 KB)

Sorry for the late answer. try this:

void emc(void)
{
  // Create 2D histogram
  TH2F *hcal = new TH2F("hcal","EMC",52,0.5,52.5,52,0.5,52.5);
  hcal->GetXaxis()->SetTitle("X");
  hcal->GetXaxis()->SetLabelSize(0.03);
  hcal->GetXaxis()->SetTickLength(0.02);
  hcal->GetXaxis()->SetTitleOffset(1.2);
  hcal->GetYaxis()->SetTitle("Y");
  hcal->GetYaxis()->SetLabelSize(0.03);
  hcal->GetYaxis()->SetTickLength(0.02);
  hcal->GetYaxis()->SetTitleOffset(1.2);
  hcal->GetZaxis()->SetTitle("E, [MeV]");
  hcal->GetZaxis()->SetLabelSize(0.03);
  hcal->GetZaxis()->SetTickLength(0.02);

  hcal->Fill(10.0,20.0,250.0);
  hcal->Fill(20.0,30.0, 50.0);
  hcal->Fill(30.0,40.0,450.0);

  hcal->Draw("lego2 fb bb");

  // Draw the hole
  gPad->Update();
  Double_t x[5] = {18, 18, 34, 34, 18};
  Double_t y[5] = {26, 27, 27, 26, 26};
  Double_t z[5] = { 0,  0 , 0,  0,  0};
  Double_t temp1[3], temp2[3];
  TView *view = gPad->GetView();
  for (Int_t i=0; i<5 ; i++) {
     temp1[0] = x[i];
     temp1[1] = y[i];
     temp1[2] = z[i];
     view->WCtoNDC(temp1, &temp2[0]);
     x[i] = temp2[0];
     y[i] = temp2[1];
  }
  TPolyLine *p = new TPolyLine(5,x,y);
  p->Draw("f");
  p->SetFillStyle(1001);
  p->SetFillColor(2);
}

Hi Olivier,

Thank you a lot for your help! I also found there is a few examples how
to make a different bin color for 1D histograms:

root.cern.ch/root/roottalk/roottalk04/0393.html
root.cern.ch/root/roottalk/roottalk04/1074.html

Is there any way to implement this thing in 2D case? This is related with
possibility to mark different clusters by different colors.

Sorry for I’m bothering you. I’m a beginer in ROOT, and still don’t know
tons of ROOT possibilities.

Best regards,
Alexander

Hi Alexander,

here is an example:

void emc(void)
{
  // Create 2D histogram
  TH2F *hcal0 = new TH2F("hcal0","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *hcal1 = new TH2F("hcal1","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *hcal2 = new TH2F("hcal2","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *top   = new TH2F("hcal2","EMC",52,0.5,52.5,52,0.5,52.5);

  THStack *hs= new THStack();
  hs->Add(hcal0,"LEGO");
  hs->Add(hcal1,"LEGO");
  hs->Add(hcal2,"LEGO");
  hs->Add(top,"LEGO");

  hcal0->SetFillColor(kYellow);
  hcal1->SetFillColor(kRed);
  hcal2->SetFillColor(kBlue);
  top->SetFillColor(kWhite);

  hcal0->Fill(10.0,20.0,250.0);
  hcal1->Fill(20.0,30.0, 50.0);
  hcal2->Fill(30.0,40.0,450.0);

  hs->Draw("lego1 fb bb");

  // Draw the hole
  gPad->Update();
  Double_t x[5] = {18, 18, 34, 34, 18};
  Double_t y[5] = {26, 27, 27, 26, 26};
  Double_t z[5] = { 0,  0 , 0,  0,  0};
  Double_t temp1[3], temp2[3];
  TView *view = gPad->GetView();
  for (Int_t i=0; i<5 ; i++) {
     temp1[0] = x[i];
     temp1[1] = y[i];
     temp1[2] = z[i];
     view->WCtoNDC(temp1, &temp2[0]);
     x[i] = temp2[0];
     y[i] = temp2[1];
  }
  TPolyLine *p = new TPolyLine(5,x,y);
  p->Draw("f");
  p->SetFillStyle(1001);
  p->SetFillColor(2);
}

Hi Olivier,

Thank you a lot indeed! :stuck_out_tongue:

Best regards, Alexander

Hi Olivier,

I’ve slightly modified your last macro and added a function
MakeHole applying your idea to use the class THStack. Now
it looks quite good but I suspect one may create the hole
without using the class TPolyLine. For this it would be good
to paint the top and lateral parts of the hole. Maybe there’s
an option in the TH classes that could make this?

Thank you in advance,
Cheers,
Alexander

void MakeHole(TH2F *hhole)
{
  const Int_t kXmin = 19;
  const Int_t kXmax = 34;
  const Int_t kYmin = 26;
  const Int_t kYmax = 27;
  for (Int_t ix = kXmin; ix <= kXmax; ix++) {
    for (Int_t iy = kYmin; iy <= kYmax; iy++) {
      hhole->Fill(Float_t(ix),Float_t(iy),-10.0);
    }
  }
}

void emc(void)
{
  // Create 2D histogram
  TH2F *hhole = new TH2F("hhole","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *hcal0 = new TH2F("hcal0","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *hcal1 = new TH2F("hcal1","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *hcal2 = new TH2F("hcal2","EMC",52,0.5,52.5,52,0.5,52.5);
  TH2F *top   = new TH2F("top","EMC",52,0.5,52.5,52,0.5,52.5);

  THStack *hs= new THStack();
  hs->Add(hhole,"lego");
  hs->Add(hcal0,"lego");
  hs->Add(hcal1,"lego");
  hs->Add(hcal2,"lego");
  hs->Add(top,"lego");

  hhole->SetFillColor(kGreen);
  hcal0->SetFillColor(kYellow);
  hcal1->SetFillColor(kRed);
  hcal2->SetFillColor(kBlue);
  top->SetFillColor(kWhite);

  hcal0->Fill(10.0,20.0,250.0);
  hcal1->Fill(20.0,30.0, 50.0);
  hcal2->Fill(30.0,40.0,450.0);
  MakeHole(hhole);

  hs->Draw("lego1 fb bb");
  
  // Draw the hole
  gPad->Update();
  Double_t x[5] = {19, 19, 34, 34, 19};
  Double_t y[5] = {26, 27, 27, 26, 26};
  Double_t z[5] = { 0,  0 , 0,  0,  0};
  Double_t temp1[3], temp2[3];
  TView *view = gPad->GetView();
  for (Int_t i=0; i<5 ; i++) {
    temp1[0] = x[i];
    temp1[1] = y[i];
    temp1[2] = z[i];
    view->WCtoNDC(temp1, &temp2[0]);
    x[i] = temp2[0];
    y[i] = temp2[1];
  }
  TPolyLine *p = new TPolyLine(5,x,y);
  p->Draw("f");
  p->SetFillStyle(1001);
  p->SetFillColor(2);
}

Hi Alexander,

When you use TStack, the bins tops have the color of the highest histogram on the stack. A stack seen from the top has one color only. There is no option which will allow to paint the bottom of the hole differently from the top of the other bins.

Cheers, Olivier