Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TF1Editor.cxx
Go to the documentation of this file.
1 // @(#)root/ged:$Id$
2 // Author: Ilka Antcheva 21/03/06
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TF1Editor //
15 // //
16 // GUI for TF1 attributes and parameters. //
17 // //
18 //////////////////////////////////////////////////////////////////////////
19 
20 #include "TF1Editor.h"
21 #include "TGedEditor.h"
22 #include "TH1.h"
23 #include "TF1.h"
24 #include "TGTextEntry.h"
25 #include "TGToolTip.h"
26 #include "TGLabel.h"
27 #include "TGDoubleSlider.h"
28 #include "TString.h"
29 #include "TGNumberEntry.h"
30 #include "TG3DLine.h"
32 #include "TCanvas.h"
33 
34 
35 ClassImp(TF1Editor);
36 
37 enum ETF1Wid {
38  kTF1_TIT, kTF1_NPX,
39  kTF1_XSLD, kTF1_XMIN, kTF1_XMAX,
40  kTF1_PAR, kTF1_DRW
41 };
42 
43 TF1Editor::TF1Editor(const TGWindow *p, Int_t width, Int_t height,
44  UInt_t options, Pixel_t back)
45  : TGedFrame(p, width, height, options | kVerticalFrame, back)
46 {
47  // Constructor of TF1 editor.
48 
49  MakeTitle("Function");
50 
51  fTitle = new TGTextEntry(this, new TGTextBuffer(50), kTF1_TIT);
52  fTitle->Resize(137, fTitle->GetDefaultHeight());
53  fTitle->SetEnabled(kFALSE);
54  fTitle->SetToolTipText(Form("Function expression or predefined name"));
55  AddFrame(fTitle, new TGLayoutHints(kLHintsLeft,3, 2, 2, 3));
56 
57  TGCompositeFrame *f3a = new TGCompositeFrame(this, 137, 20, kHorizontalFrame);
58  AddFrame(f3a, new TGLayoutHints(kLHintsTop, 0, 1, 3, 0));
59  fDrawMode = new TGCheckButton(f3a, "Update", kTF1_DRW);
60  fDrawMode->SetToolTipText("Immediate function redrawing");
61  f3a->AddFrame(fDrawMode, new TGLayoutHints(kLHintsLeft | kLHintsBottom, 3, 1, 1, 0));
62  fParLabel = new TGLabel(f3a, "");
63  f3a->AddFrame(fParLabel, new TGLayoutHints(kLHintsRight | kLHintsBottom, 25, 2, 1, 0));
64 
65  TGCompositeFrame *f3 = new TGCompositeFrame(this, 137, 20, kHorizontalFrame | kFixedWidth);
66  fSetPars = new TGTextButton(f3, "Set Parameters...", kTF1_PAR);
67  f3->AddFrame(fSetPars, new TGLayoutHints(kLHintsRight | kLHintsTop | kLHintsExpandX,
68  0, 1, 5, 0));
69  fSetPars->SetToolTipText("Open a dialog for parameter(s) settings");
70  AddFrame(f3, new TGLayoutHints(kLHintsTop | kLHintsLeft, 3, 2, 2, 3));
71 
72  MakeTitle("X-Range");
73 
74  TGCompositeFrame *f4 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
75  TGCompositeFrame *f4a = new TGCompositeFrame(f4, 66, 20, kVerticalFrame | kFixedWidth);
76  TGLabel *fNpxLabel = new TGLabel(f4a, "Points: ");
77  f4a->AddFrame(fNpxLabel, new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 3, 0, 5, 1));
78  f4->AddFrame(f4a, new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 1, 0, 0));
79 
80  TGCompositeFrame *f4b = new TGCompositeFrame(f4, 40, 20, kVerticalFrame);
81  fNXpoints = new TGNumberEntry(f4b, 100, 7, kTF1_NPX,
82  TGNumberFormat::kNESInteger,
83  TGNumberFormat::kNEANonNegative,
84  TGNumberFormat::kNELLimitMinMax,4,100000);
85  fNXpoints->GetNumberEntry()->SetToolTipText("Points along x-axis (4-100 000)");
86  f4b->AddFrame(fNXpoints, new TGLayoutHints(kLHintsLeft, 0, 0, 1, 0));
87  f4->AddFrame(f4b, new TGLayoutHints(kLHintsTop | kLHintsRight, 0, 1, 0, 0));
88  AddFrame(f4, new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 1, 0, 0));
89 
90  TGCompositeFrame *f5 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
91  fSliderX = new TGDoubleHSlider(f5, 1, 2);
92  fSliderX->Resize(137,20);
93  f5->AddFrame(fSliderX, new TGLayoutHints(kLHintsLeft));
94  AddFrame(f5, new TGLayoutHints(kLHintsTop, 3, 7, 4, 1));
95 
96  TGCompositeFrame *f6 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
97  fSldMinX = new TGNumberEntryField(f6, kTF1_XMIN, 0.0,
98  TGNumberFormat::kNESRealFour,
99  TGNumberFormat::kNEAAnyNumber);
100  ((TGTextEntry*)fSldMinX)->SetToolTipText("Lower bound along x-axis");
101  fSldMinX->Resize(65,20);
102  fSldMinX->SetState(kFALSE);
103  f6->AddFrame(fSldMinX, new TGLayoutHints(kLHintsLeft));
104  fSldMaxX = new TGNumberEntryField(f6, kTF1_XMAX, 0.0,
105  TGNumberFormat::kNESRealFour,
106  TGNumberFormat::kNEAAnyNumber);
107  ((TGTextEntry*)fSldMaxX)->SetToolTipText("Upper bound along x-axis");
108  fSldMaxX->SetState(kFALSE);
109  fSldMaxX->Resize(65,20);
110  f6->AddFrame(fSldMaxX, new TGLayoutHints(kLHintsLeft, 4, 0, 0, 0));
111  AddFrame(f6, new TGLayoutHints(kLHintsTop, 3, 3, 5, 0));
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Destructor of TF1 editor.
116 
117 TF1Editor::~TF1Editor()
118 {
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Connect signals to slots.
123 
124 void TF1Editor::ConnectSignals2Slots()
125 {
126  fNXpoints->Connect("ValueSet(Long_t)", "TF1Editor", this, "DoXPoints()");
127  (fNXpoints->GetNumberEntry())->Connect("ReturnPressed()", "TF1Editor",
128  this, "DoXPoints()");
129  fSetPars->Connect("Clicked()", "TF1Editor", this, "DoParameterSettings()");
130  fSliderX->Connect("Pressed()","TF1Editor", this,"DoSliderXPressed()");
131  fSliderX->Connect("Released()","TF1Editor", this,"DoSliderXReleased()");
132  fSliderX->Connect("PositionChanged()","TF1Editor", this,"DoSliderXMoved()");
133 
134  fInit = kFALSE;
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Pick up the function parameters and options.
139 
140 void TF1Editor::SetModel(TObject* obj)
141 {
142  if (obj == 0 || !obj->InheritsFrom(TF1::Class())) {
143  return;
144  }
145 
146  fF1 = (TF1*)obj;
147  fAvoidSignal = kTRUE;
148 
149  const char *text = fF1->GetTitle();
150  fTitle->SetText(text);
151 
152  fNP = fF1->GetNpar();
153  fParLabel->SetText(Form("Npar: %d", fNP));
154  fClient->NeedRedraw(fParLabel);
155 
156  fNXpoints->SetNumber(fF1->GetNpx());
157 
158  if (!fNP)
159  fSetPars->SetState(kButtonDisabled, kFALSE);
160  else
161  fSetPars->SetState(kButtonUp, kFALSE);
162 
163  TAxis *x = fF1->GetHistogram()->GetXaxis();
164  Int_t nx = x->GetNbins();
165  Int_t nxbinmin = x->GetFirst();
166  Int_t nxbinmax = x->GetLast();
167  fSliderX->SetRange(1,nx);
168  fSliderX->SetPosition((Double_t)nxbinmin,(Double_t)nxbinmax);
169  fSldMinX->SetNumber(x->GetBinLowEdge(nxbinmin));
170  fSldMaxX->SetNumber(x->GetBinUpEdge(nxbinmax));
171 
172  if (fInit) ConnectSignals2Slots();
173  fAvoidSignal = kFALSE;
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Slot connected to the function parameter(s) settings.
178 
179 void TF1Editor::DoParameterSettings()
180 {
181  TGMainFrame *main = (TGMainFrame *)GetMainFrame();
182  Double_t rmin = fSldMinX->GetNumber();
183  Double_t rmax = fSldMaxX->GetNumber();
184  new TFunctionParametersDialog(gClient->GetDefaultRoot(), main,
185  fF1, fGedEditor->GetPad(), rmin, rmax);
186 
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Slot connected to the number of points setting.
191 
192 void TF1Editor::DoXPoints()
193 {
194  if (fAvoidSignal) return;
195  Double_t rmin, rmax;
196  fF1->GetRange(rmin, rmax);
197  fF1->SetRange(fSldMinX->GetNumber(), fSldMaxX->GetNumber());
198  fF1->SetNpx((Int_t)fNXpoints->GetNumber());
199  fF1->GetHistogram()->GetXaxis()->Set((Int_t)fNXpoints->GetNumber(),
200  fSldMinX->GetNumber(),
201  fSldMaxX->GetNumber());
202  Update();
203  fF1->SetRange(rmin, rmax);
204 }
205 
206 ////////////////////////////////////////////////////////////////////////////////
207 /// Slot connected to the x-Slider range for function redrawing.
208 
209 void TF1Editor::DoSliderXMoved()
210 {
211  if (fAvoidSignal) return;
212 
213  TVirtualPad *save = 0;
214  save = gPad;
215  gPad = fGedEditor->GetPad();
216  fGedEditor->GetPad()->cd();
217 
218  fF1->SetNpx((Int_t)fNXpoints->GetNumber());
219  TAxis *x = fF1->GetHistogram()->GetXaxis();
220 
221  if (fDrawMode->GetState() == kButtonDown) {
222  TString opt = fF1->GetDrawOption();
223  opt.ToUpper();
224  if (!opt.Contains("SAME"))
225  opt += "SAME";
226  fF1->Draw(opt);
227 
228  x->SetRange((Int_t)((fSliderX->GetMinPosition())+0.5),
229  (Int_t)((fSliderX->GetMaxPosition())+0.5));
230  fSldMinX->SetNumber(x->GetBinLowEdge(x->GetFirst()));
231  fSldMaxX->SetNumber(x->GetBinUpEdge(x->GetLast()));
232  fClient->NeedRedraw(fSliderX,kTRUE);
233  fClient->NeedRedraw(fSldMinX,kTRUE);
234  fClient->NeedRedraw(fSldMaxX,kTRUE);
235  Update();
236 
237  } else {
238  x->SetRange((Int_t)((fSliderX->GetMinPosition())+0.5),
239  (Int_t)((fSliderX->GetMaxPosition())+0.5));
240  fSldMinX->SetNumber(x->GetBinLowEdge(x->GetFirst()));
241  fSldMaxX->SetNumber(x->GetBinUpEdge(x->GetLast()));
242  fClient->NeedRedraw(fSliderX,kTRUE);
243  fClient->NeedRedraw(fSldMinX,kTRUE);
244  fClient->NeedRedraw(fSldMaxX,kTRUE);
245  }
246  if(save) gPad = save;
247 }
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Slot connected to the x-Slider.
251 
252 void TF1Editor::DoSliderXPressed()
253 {
254  if (fAvoidSignal || (fDrawMode->GetState() == kButtonDown)) return;
255 
256  TVirtualPad *save = 0;
257  save = gPad;
258  gPad = fGedEditor->GetPad();
259  fGedEditor->GetPad()->cd();
260 
261  fF1->SetNpx((Int_t)fNXpoints->GetNumber());
262  TAxis *x = fF1->GetHistogram()->GetXaxis();
263  TString opt = fF1->GetDrawOption();
264  opt.ToUpper();
265  if (!opt.Contains("SAME"))
266  opt += "SAME";
267  fF1->Draw(opt);
268 
269  x->SetRange((Int_t)((fSliderX->GetMinPosition())+0.5),
270  (Int_t)((fSliderX->GetMaxPosition())+0.5));
271  fSldMinX->SetNumber(x->GetBinLowEdge(x->GetFirst()));
272  fSldMaxX->SetNumber(x->GetBinUpEdge(x->GetLast()));
273  fClient->NeedRedraw(fSliderX,kTRUE);
274  fClient->NeedRedraw(fSldMinX,kTRUE);
275  fClient->NeedRedraw(fSldMaxX,kTRUE);
276  Update();
277 
278  if(save) gPad = save;
279 
280 }
281 
282 ////////////////////////////////////////////////////////////////////////////////
283 /// Slot connected to the x-Slider.
284 
285 void TF1Editor::DoSliderXReleased()
286 {
287  if (fAvoidSignal || (fDrawMode->GetState() == kButtonDown)) return;
288 
289  TVirtualPad *save = 0;
290  save = gPad;
291  gPad = fGedEditor->GetPad();
292  fGedEditor->GetPad()->cd();
293 
294  fF1->SetNpx((Int_t)fNXpoints->GetNumber());
295  TAxis *x = fF1->GetHistogram()->GetXaxis();
296  TString opt = fF1->GetDrawOption();
297  opt.ToUpper();
298  if (!opt.Contains("SAME"))
299  opt += "SAME";
300  fF1->Draw(opt);
301 
302  x->SetRange((Int_t)((fSliderX->GetMinPosition())+0.5),
303  (Int_t)((fSliderX->GetMaxPosition())+0.5));
304  fSldMinX->SetNumber(x->GetBinLowEdge(x->GetFirst()));
305  fSldMaxX->SetNumber(x->GetBinUpEdge(x->GetLast()));
306  fClient->NeedRedraw(fSliderX,kTRUE);
307  fClient->NeedRedraw(fSldMinX,kTRUE);
308  fClient->NeedRedraw(fSldMaxX,kTRUE);
309  Update();
310 
311  if(save) gPad = save;
312 }
313 
314 
315 ////////////////////////////////////////////////////////////////////////////////
316 /// Slot connected to min/max settings of the slider range.
317 
318 void TF1Editor::DoXRange()
319 {
320  if (fAvoidSignal) return;
321  TAxis *x = fF1->GetHistogram()->GetXaxis();
322  Int_t nx = x->GetNbins();
323  Double_t width = x->GetBinWidth(1);
324  Double_t lowLimit = x->GetBinLowEdge(1);
325  Double_t upLimit = x->GetBinUpEdge(nx);
326  if ((fSldMinX->GetNumber()+width/2) < (lowLimit))
327  fSldMinX->SetNumber(lowLimit);
328  if ((fSldMaxX->GetNumber()-width/2) > (upLimit))
329  fSldMaxX->SetNumber(upLimit);
330  x->SetRangeUser(fSldMinX->GetNumber()+width/2,
331  fSldMaxX->GetNumber()-width/2);
332  Int_t nxbinmin = x->GetFirst();
333  Int_t nxbinmax = x->GetLast();
334  fSliderX->SetPosition((Double_t)(nxbinmin),(Double_t)(nxbinmax));
335  Update();
336 }
337 
338 ////////////////////////////////////////////////////////////////////////////////
339 /// Exclude TAttFillEditor from this interface.
340 
341 void TF1Editor::ActivateBaseClassEditors(TClass* cl)
342 {
343  fGedEditor->ExcludeClassEditor(TAttFill::Class());
344  TGedFrame::ActivateBaseClassEditors(cl);
345 }
346