Re: TGraph slider question EXAMPLE

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Oct 02 1998 - 16:32:37 MEST


Eddy Offermann wrote:
> 
> Hi Rooters,
> 
> I still did not figure out how to get the slider in the DrawPanel
> working for a TGraph.
> 
> Rene Brun wrote:
> 
> > The TGraph::DrawPanel function is currently not implemented.
> > I have added a message in the new version.
> > Currently there are two ways to zoom on a graph:
> > - via the command line by changing the pad range
> > - drawing a TH1F empty object (setting min and max), then
> >   drawing the graph on top and invoking DrawPanel on the TH1F object.
> 
> I like the second option because it seems only a few mouse clicks away.
> So I tried the following macro:
> 
> {
>    gROOT->Reset();
>    c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
> 
>    Int_t n = 10;
>    Float_t x[n], y[n];
>    for (Int_t i=0;i<n;i++) {
>      x[i] = i*0.1;
>      y[i] = x[i];
>    }
> 
>    hpx = new TH1F("hpx","This is the px distribution",10,0,1);
>    hpx->SetMinimum(0.0);
>    hpx->SetMaximum(1.0);
>    hpx->SetXTitle("X title");
>    hpx->SetYTitle("Y title");
>    hpx->Draw();
> 
>    gr = new TGraph(n,x,y);
>    gr->SetLineColor(2);
>    gr->SetMarkerColor(4);
>    gr->SetMarkerStyle(21);
>    gr->Draw("CP");
> }
> 
> The DrawPanel works for 'hpx' but I lose the data points of the graph because
> they do not get updated. Is there a statement, I should add ??
> 

Eddy,
The DrawPanel redraws the selected histogram only (not the pad).
After having moved the slider, you must type at the command line
  gr->Draw("CP");
This work should be done in TGraph::DrawPanel.
I have added an example of macro below illuistrating how to 
create your own slider (very simple) and control what you want to do
in the interpreted code. This is quite powerful.
Just do
  Root > .x gslider.C

Rene Brun

//=============macro gslider.C=============================
{
   gROOT->Reset();
   c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);

   c1->SetFillColor(42);
   c1->SetGridx();
   c1->SetGridy();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);
   c1->SetTopMargin(0.12);
   
   Int_t n = 20;
   Float_t x[n], y[n];
   for (Int_t i=0;i<n;i++) {
     x[i] = i*0.1;
     y[i] = 10*sin(x[i]+0.2);
     printf(" i %i %f %f \n",i,x[i],y[i]);
   }
   Float_t xmin = 0;
   Float_t xmax = 2;
   Float_t ymax = 11;
   TH1F *hist = new TH1F("hist","a simple graph",100,xmin,xmax);
   hist->SetMaximum(11);
   hist->Draw();
   hist->SetXTitle("X title");
   hist->SetYTitle("Y title");
   gr = new TGraph(n,x,y);
   gr->SetFillColor(19);
   gr->SetLineColor(2);
   gr->SetLineWidth(4);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->SetTitle("a simple graph");
   gr->Draw("CP");
   
   //Create one slider in main canvas
   c1->Update();
   TSlider *xslider = new
TSlider("xslider","x",xmin,ymax,xmax,ymax+0.7);
   xslider->SetMethod(".x grSliderAction.C");
}
//===========macro grSliderAction.C==============
{
   Int_t nx = hist->GetXaxis()->GetNbins();
   Int_t binxmin = nx*xslider->GetMinimum();
   Int_t binxmax = nx*xslider->GetMaximum();
   hist->GetXaxis()->SetRange(binxmin,binxmax);
   c1->Modified();
   c1->Update();
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:38 MET