TH2 with arbitrary bin shape

If it is done using using OpenGL it will be a new option in TGLHistPainter. It should not be done using the geometry package.

TCutG object ? What can be gained if TCutG is used instead of TPolyLine ?
Do you think I’d just start by using TCutG instead of TPolyLine for the honeycomb ? [/quote]
That’s not for honeycomb. It is a generalization. TCutG has the tools to know if a value is inside or outside the cut. That’s why a TCutG is better.

Hello all,

I would like to know which methods of TH2 I need to override in the derived class so that I can fill bin contents via TTree::Draw. I do not intend to make a class of arbitrary bin shapes but would like to write a class inherited from TH1/TH2/TH3.

TSelectorDraw seems to be the correct one which I should look. So what I need to do is to override

  • FillN for TH1-based class
  • Fill for TH2-based class
  • Fill for TH3-based class



I have to dig a little more into how I did it, and more experienced people can give you a better answer,
but I far as I can remember, the only thing you have to do is write the proper SetBinContent() method.

Hope it can help. Tomorrow I can post the sources of my code if you wish.



A priori, all you need to do is ‘use’ you class:MyClass *histo = new MyClass("somename",....); mytree->Draw("py:px>>somename");(assuming you overload the right methods of TH2)


Hello Julien and Philippe,

Thanks for your replies. I should have explained my situation in detail.

I am planning to write a library to handle FITS files (astronomical data format) in ROOT. Most of 2D images used in astronomy has their own coordinate system (WCS; world coordinate system) because celestial coordinates cannot be projected on 2D images without distortion. For example, pixels in a FITS image are indexed from 1 to N1 for x and 1 to N2 for y, where the total number of pixels is N1 x N2. However, if I use WCS which converts pixel coordinates (x, y) to (galactic longitude, galactic latitude), I need an automated coordinate conversion when using TTree::Draw.

Here is an example.

TNtuple nt("nt", "Celestial Gamma-ray Event List", "longitude:latitude:energy:time"); nt.Fill(.....); .... TFitsImage2D img; img.SetName("image"); nt.Draw("latitude:longitude>>image");

where, I suppose TFitsImage2D should have a wrapper like

TFitsImage2D::Fill(double x, double y, double w) { double pix[2]; Sky2Pix(x, y, pix); // a method which converts celestial coordinates to 2D pixel numbers TH2D::Fill(pix[0], pix[1], w); }
to calculate the correct pixel position from latitude and longitude.

Is this all the thing I have to write?

Very recently we have introduced a new class interfacing FITS files to ROOT graphics, thanks to Claudi Martinez.
Could you have a look at this class (see: and possibly
cooperate for further developments?

For Julien, you will be pleased to know that we are about to release a new histogram class TH2Poly with arbitrary bin shapes (hexagons, country maps, etc) and with rendering in the normal 2d pad, but also with GL.


Some examples…

Dear rooters,

This is great ! This will save (me) a lot of time, and the results look great too (way better that what I have been able to do).

As I post it yesterday, for information I will attach here the class I wrote, as it is now.
There is no much comments in it, but I have been using it for now few months and
it seems quite stable. It also have been improved for memory management and robustness by my colleagues here.

There is also analysis methods specific to the detector I am working with (see previous posts),
especially to fit tracks. This has directly nothing to do with pure TH2 with arbitrary bin shape coding, but I will keep them here FYI

As soon as the TH2Poly is out I think I will test it and probably make my TMayaHisto class inherits from it.

TMayaHisto.h (9.16 KB)
TMayaHisto.cxx (49.7 KB)

Yes it will be good to merge your work with TH2Poly.
Note that I will be on holidays from tomorrow. I’ll be back on August 23rd.


Don’t worry, this will not be before mi-september, as least !


Dear Rooters,

I finally have time to test your great TH2Poly class.
My plan is to implement my specific methods to this more general class.

I however have some (small) troubles/questions :

  • I would like to access the points defining a given bin. How can I do that ?
  • The area of the bin is automatically calculated, but I do not think the barycenter is. Is it (easily) possible to implement this ? It will ease calculations and fit procedures.

This being said the migration is rather easy.

Thank you again for the help and the development of this new class.


Use GetPolygon in TH2PolyBin.

If you have some code to provide we can add it.


[quote]Use GetPolygon in TH2PolyBin.

Yes, but then what can I do with the TObject* I obtained ? What methods can I use to get the list (array ?) of the points ?

I will as soon as it is written and tested.


Look how it is used here: … tml#XaZ4IB
The object is either a TGraph or a TMultiGraph.


Here is what I wrote :

Bool_t TMayaHisto::GetBinCenter(TH2PolyBin *bin,Double_t &Xg,Double_t &Yg){

   Int_t     gn;
   TObject *poly = bin->GetPolygon();
   Double_t *gx,*gy ;

   Xg = 0.,Yg = 0. ;

   if (poly->IsA() == TGraph::Class()) {
      TGraph *g = (TGraph*)poly;
      gn = g->GetN() ;
      if(gn<=0) return kFALSE ;

      gx = g->GetX();
      gy = g->GetY();

      for(Int_t i=0;i<gn;i++){
	Xg += (Double_t)gx[i];
	Yg += (Double_t)gy[i];
      Xg /= gn;
      Yg /= gn;
      return kTRUE ;

   return kFALSE ;

This being said I think it will more efficient to have the barycenter calculated when AddBin() is called and stored in private variables e.g. fCenterX and fCenterY , as fArea is.


Hi Julien,

I suggest you make a proposal (some prototype, modification of TH2Poly).
Is it ok for you ?


Hi Julien,

You send me an email via this forum. I see it in my CERN mail box but it does not show your email address, and I do not see the email in the forum mailbox either. So please send me a direct email at so I can answer you.


Hi everybody,

I know, the last reply is over 8 years ago and the TH2Poly class is totally normal. But what I can not reproduce is the LEGO-plot using Honeycomb binning (shown by couet)

What I do is basically the following

TCanvas *can = new TCanvas(); 
// ...
for ( int i = 0; i < binID->size(); i++ ) {
  //set histogram bins
  int bin = binID->at(i);
  myEvtHisto->SetBinContent( bin, myEvtHisto->GetBinContent(bin) + Amp->at(i) );

the output is a flat LEGO-plot: always all bins 0 and z-range from -1 to 1. The xy-range is correct. I would appreciate if someone had an advice to solve this problem. Thanks in advance!

   TH2Poly *hc = new TH2Poly();
   TRandom ran;
   for (int i = 0; i<300; i++) hc->Fill(ran.Gaus(2.,1), ran.Gaus(2.,1));
   hc->Draw("gllego ");