Fill a line of bins in TH2 or TH3

Dear rooters

I want to draw a 2D histogram, in which the bins to be filled are the ones crossed by the line y=mx+b. These bins should have the value 1, while the rest remain at 0.

Lets suppose that I have a 3x3 histogram. If my slope is y=x, only the bins (1,1), (2,2) and (3,3) must be filled.

The thing gets complicated when y and x are not integers, and I’m not sure which bins should I fill.
Do you know if there is any way to fill lines of bins directly in Root, or I will need to compute a function that calculates which bins should be filled?

Thank you all


{ Double_t m = -1.0, b = 1.0; TH2D *h = new TH2D("h", "h", 40, -10, 10, 40, -10, 10); for(Int_t i = 1; i <= h->GetNbinsX(); i++) { // Double_t x = h->GetXaxis()->GetBinLowEdge(i); Double_t x = h->GetXaxis()->GetBinCenter(i); // Double_t x = h->GetXaxis()->GetBinCenterLog(i); // Double_t x = h->GetXaxis()->GetBinUpEdge(i); Double_t y = (m * x + b); h->Fill(x, y); } h->Draw("col"); }

Thanks, this works perfectly!