Re: How to get coordinate information of a histogram

Wei Xie (xiewei@clever.weizmann.ac.il)
Tue, 4 Aug 1998 17:15:36 +0300 (IDT)


Dear Valeri and Rooters

> > In TCanvas "option" menu, if "Event Status" is selected, coordinate
> > information can be shown at the bottom of the Canvas. It's
> > something like
> > the following exmple:
> >
> > Pad example TFrame 122,333, x=1.97,y=3.06
> >
> > In above example, "x" and "y" give the position of your mouse in a
> > TFrame that contains the histogram. Could you tell me how to get the
> > value of "x" and "y" in my code.

Concerning above question, I made the following code to see how to get
x and y, but it does not work. The reason seems that I can not find
the object of my class in TCanvas. Could you give me a little more
guidance ?

%%%%%%%%%
test.h
%%%%%%%%%%

#ifndef _test_
#define _test_

#include <stdlib.h>

#include <TROOT.h>
#include <TApplication.h>
#include <TObject.h>
#include <TCanvas.h>
#include <TH1.h>
#include <TRandom.h>

class test:public TH1 {

private:
TH1F *fHpx;
public:
test();
Int_t DistancetoPrimitive(Int_t px, Int_t py);
void ExecuteEvent(Int_t event, Int_t px, Int_t py);
void Draw();

ClassDef(test,0)
};

#endif

%%%%%%%%%%
test.c
%%%%%%%%%
#include "test.h"

ClassImp(test)

test::test(): TH1()
{
// Fill histograms till user clicks "Stop Filling" button.

static int cnt;

if (!fHpx) {
fHpx = new TH1F("hpx","",100,-4,4);
fHpx->SetFillColor(kRed);
cnt = 0;
}

int kUPDATE = 1000;
float px, py;

Int_t num=0;
while (num<1000)
{
num++;
gRandom->Rannor(px,py); //px and py will be two gaussian random numbers
fHpx->Fill(px);
}
}

Int_t test::DistancetoPrimitive(Int_t px, Int_t py)
{
return TH1::DistancetoPrimitive(px, py);
}

void test::ExecuteEvent(Int_t event, Int_t px, Int_t py)
{
Float_t x,y;

switch (event)
{
case kMouseMotion:
x = gPad->AbsPixeltoX(px);
y = gPad->AbsPixeltoY(py);
printf("x=%.3g, y=%.3g",gPad->PadtoX(x),gPad->PadtoY(y));
break;
}

}

void test::Draw()
{
fHpx->Draw();
}