# Rotate a figure 90 degrees

**URL:** <https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623>\
**Category:** Newbie\
**Created:** [July 11, 2023, 2:34pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623 "2023-07-11T14:34:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![samfred](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/samfred/32/39516_2.png) [@samfred](https://root-forum.cern.ch/u/samfred)\
**Post date:** [July 11, 2023, 2:34pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/1 "2023-07-11T14:34:34Z")

</div>

I am working with a detector built out of hexagons, so to represent it in a 2D histogram I am using TH2Poly and Honeycomb, but I can only draw this histogram with the hexagons oriented 90 degrees off of what they are in the detector (the attached image would need to be rotated CW 90 degrees to match). I figure this could be solved if one could draw the hexagons in the other orientation from the start, or just rotate the figure after putting all the data in. I haven’t been able to find out how to do either of those things, any help?

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/5/1/51855fa45033c0dad66a3c439d457d92247a84ec.png)

---

<div class="post-metadata">

**Author:** ![samfred](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/samfred/32/39516_2.png) [@samfred](https://root-forum.cern.ch/u/samfred)\
**Post date:** [July 11, 2023, 4:58pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/2 "2023-07-11T16:58:16Z")

</div>

I solved this myself by just making a copy of the TH2Poly::Honeycomb function and altering it a bit to draw the other orientation of hexagons

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 13, 2023, 8:56am UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/3 "2023-07-13T08:56:01Z")

</div>

Dear @samfred ,

I am glad you could solve your problem! Can you maybe share the modifications to the functions that you did?

@couet may be interested in helping you introducing those changes to ROOT so that also other users may benefit.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![samfred](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/samfred/32/39516_2.png) [@samfred](https://root-forum.cern.ch/u/samfred)\
**Post date:** [July 13, 2023, 1:36pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/4 "2023-07-13T13:36:32Z")

</div>

Sure, here’s what I used

```auto
#include "TH2Poly.h"

void honeycomb_transpose(TH2Poly * hist, Double_t xstart, Double_t ystart, Double_t a,Int_t k, Int_t s) {

    // a is the side length of the hexagon

    Double_t numberOfHexagonsInTheRow;
    Double_t x[6], y[6];
    Double_t xloop, yloop, ytemp;
    xloop = xstart; yloop = ystart + a*TMath::Sqrt(3)/2.0;
    for (int sCounter = 0; sCounter < s; sCounter++) {

        ytemp = yloop; // Resets the temp variable

        // Determine the number of hexagons in that row
        if(sCounter%2 == 0){numberOfHexagonsInTheRow = k;}
        else{numberOfHexagonsInTheRow = k - 1;}

        for (int kCounter = 0; kCounter < numberOfHexagonsInTheRow; kCounter++) {

            // Go around the hexagon
            x[0] = xloop;
            y[0] = ytemp;
            x[1] = x[0] + a/2.0;
            y[1] = y[0] + a*TMath::Sqrt(3)/2.0;
            x[2] = x[1] + a;
            y[2] = y[1];
            x[3] = x[2] + a/2.0;
            y[3] = y[0];
            x[4] = x[2];
            y[4] = y[3] - a*TMath::Sqrt(3)/2.0;
            x[5] = x[1];
            y[5] = y[4];

            hist->AddBin(6, x, y);

            // Go up
            ytemp += a*TMath::Sqrt(3);
        }

        // Increment the starting position
        if (sCounter%2 == 0) yloop += a*TMath::Sqrt(3)/2.0;
        else yloop -= a*TMath::Sqrt(3)/2.0;
        xloop += 1.5*a;
    }
}

```

Which is, again, only a slight alteration of what the normal honeycomb function does with coordinates and whatnot.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 27, 2023, 12:20pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/5 "2023-07-27T12:20:56Z")

</div>

Thanks. I’ll look at it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 27, 2023, 2:28pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/6 "2023-07-27T14:28:14Z")

</div>

I tried your code with this small example:

```auto
{
   TCanvas *chc = new TCanvas("chc","chc",600,600);

   TH2Poly *hc = new TH2Poly();
   hc->Honeycomb(0,0,.1,25,25);
   hc->SetName("hc");
   hc->SetTitle("Option COLZ");
   hc->Fill(2.,2.,10.);
   hc->Fill(2.,1.,20.);
   hc->Fill(3.,3.,1.);

   hc->Draw("colz l");
}

```

I get this:

 ![Screenshot 2023-07-27 at 16.25.59](https://root-forum.cern.ch/uploads/default/original/3X/1/d/1dc8ba75a4687bfcd23012b95e0e8e513a53dd30.jpeg)  
On the left your code, on the right the current code. Both are valid. We can make it optional.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 28, 2023, 8:58am UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/7 "2023-07-28T08:58:19Z")

</div>

@samfred : This is what I have now:

```auto
{
   TCanvas *C = new TCanvas("C","C",1200,600);
   C->Divide(2,1);

   TH2Poly *hc1 = new TH2Poly();
   hc1->Honeycomb(0,0,.1,5,5);
   hc1->SetTitle("Option V (default)");
   hc1->SetStats(0);
   hc1->Fill(.1, .1, 15.);
   hc1->Fill(.4, .4, 10.);
   hc1->Fill(.5, .5, 20.);

   TH2Poly *hc2 = new TH2Poly();
   hc2->Honeycomb(0,0,.1,5,5,"h");
   hc2->SetTitle("Option H");
   hc2->SetStats(0);
   hc2->Fill(.1, .1, 15.);
   hc2->Fill(.4, .4, 10.);
   hc2->Fill(.5, .5, 20.);

   C->cd(1)->SetGrid(); hc1->Draw("colz L");
   C->cd(2)->SetGrid(); hc2->Draw("colz L");
}

```

 ![Screenshot 2023-07-28 at 10.55.49](https://root-forum.cern.ch/uploads/default/original/3X/9/e/9e9c8ad0564b116228662393c324f14e808b0eba.jpeg)

`Honeycomb` has now an option parameter which can be `V` or `H`. Is it ok for you ?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 28, 2023, 10:04am UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/8 "2023-07-28T10:04:41Z")

</div>

Here is the corresponding PR: [Implement options for Honeycomb by couet · Pull Request #13343 · root-project/root · GitHub](https://github.com/root-project/root/pull/13343)

---

<div class="post-metadata">

**Author:** ![samfred](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/samfred/32/39516_2.png) [@samfred](https://root-forum.cern.ch/u/samfred)\
**Post date:** [July 30, 2023, 1:40pm UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/9 "2023-07-30T13:40:41Z")

</div>

Wow that’s awesome, thank you so much!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 31, 2023, 11:56am UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/10 "2023-07-31T11:56:14Z")

</div>

I’ll merge it asap.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 1, 2023, 9:08am UTC](https://root-forum.cern.ch/t/rotate-a-figure-90-degrees/55623/11 "2023-08-01T09:08:54Z")

</div>

Done, it is now in master.
