pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTab.cc
1 #include <iostream>
2 
3 #include <TApplication.h>
4 #include <TGButton.h>
5 #include <TGToolTip.h>
6 #include <TRandom.h>
7 #include <TSystem.h>
8 #include <TCanvas.h>
9 #include <TGTab.h>
10 #include <TGLabel.h>
11 #include <TH2.h>
12 #include <TStyle.h>
13 #include <TBrowser.h>
14 
15 #include "PixTab.hh"
16 #include "log.h"
17 
18 using namespace std;
19 using namespace pxar;
20 
21 ClassImp(PixTab)
22 
23 // ----------------------------------------------------------------------
24 PixTab::PixTab(PixGui *p, PixTest *test, string tabname) {
25  init(p, test, tabname);
26 
27  fBorderN = 2;
28  fBorderL = 10;
29  fBorderT = 1;
30 
31  UInt_t w = fGui->getTabs()->GetWidth();
32  UInt_t h = fGui->getTabs()->GetHeight();
33 
34  fTabFrame = fGui->getTabs()->AddTab(fTabName.c_str());
35 
36  // fhFrame = new TGHorizontalFrame(fTabFrame, w, h);
37  fhFrame = new TGCompositeFrame(fTabFrame, w, h, kHorizontalFrame);
38 
39  // -- 2 vertical frames
40  fV1 = new TGVerticalFrame(fhFrame);
41  fhFrame->AddFrame(fV1, new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY, fBorderN, fBorderN, fBorderN, fBorderN));
42  fV2 = new TGVerticalFrame(fhFrame);
43  fhFrame->AddFrame(fV2, new TGLayoutHints(kLHintsRight | kLHintsExpandX | kLHintsExpandY, fBorderN, fBorderN, fBorderN, fBorderN));
44 
45  // -- fV1: create and add Embedded Canvas
46  fEc1 = new TRootEmbeddedCanvas(Form("%s", tabname.c_str()), fV1, 500, 500);
47  fV1->AddFrame(fEc1, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 1, 1, 1, 1));
48 
49 
50  // -- status bar
51  Int_t wid = fEc1->GetCanvasWindowId();
52  TCanvas *myc = new TCanvas(Form("%sCanvas", tabname.c_str()), 10, 10, wid);
53  fEc1->AdoptCanvas(myc);
54  myc->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)","PixTab",this, "statusBarUpdate(Int_t,Int_t,Int_t,TObject*)");
55 
56  Int_t parts[] = {45, 55};
57  fStatusBar = new TGStatusBar(fV1, 50, 10, kVerticalFrame);
58  fStatusBar->SetParts(parts, 2);
59  fStatusBar->Draw3DCorner(kFALSE);
60  fV1->AddFrame(fStatusBar, new TGLayoutHints(kLHintsExpandX, 1, 1, 1, 1));
61 
62  // -- fV2: create parameter TGText boxes for test
63  vector<pair<string, string> > amap = fTest->getParameters();
64  TGTextEntry *te(0);
65  TGLabel *tl(0);
66  TGTextBuffer *tb(0);
67  TGTextButton *tset(0);
68  TGCheckButton *tcheck(0);
69 
70  TGHorizontalFrame *hFrame(0);
71 
72  int cnt(0);
73  for (unsigned int i = 0; i < amap.size(); ++i) {
74  if (amap[i].second == "button") {
75  hFrame = new TGHorizontalFrame(fV2, 300, 30, kLHintsExpandX);
76  tset = new TGTextButton(hFrame, amap[i].first.c_str(), 1234);
77  hFrame->AddFrame(tset, new TGLayoutHints(kLHintsCenterY | kLHintsLeft, fBorderN, fBorderN, fBorderN, fBorderN));
78  tset->SetToolTipText("run this subtest");
79  tset->GetToolTip()->SetDelay(2000); // add a bit of delay to ease button hitting
80  tset->Connect("Clicked()", "PixTab", this, "buttonClicked()");
81  tset->SetBackgroundColor(fGui->fDarkSalmon);
82  fV2->AddFrame(hFrame, new TGLayoutHints(kLHintsRight | kLHintsTop));
83  continue;
84  }
85 
86  if (string::npos != amap[i].second.find("checkbox")) {
87  hFrame = new TGHorizontalFrame(fV2, 300, 30, kLHintsExpandX);
88  tcheck = new TGCheckButton(hFrame, amap[i].first.c_str(), 1234);
89  hFrame->AddFrame(tcheck, new TGLayoutHints(kLHintsRight, fBorderN, fBorderN, fBorderN, fBorderN));
90  fV2->AddFrame(hFrame, new TGLayoutHints(kLHintsRight | kLHintsTop));
91  if (string::npos != amap[i].second.find("(1)")) {
92  tcheck->SetState(kButtonDown);
93  } else if (string::npos != amap[i].second.find("(0)")) {
94  tcheck->SetState(kButtonUp);
95  } else {
96  tcheck->SetState(kButtonUp);
97  }
98  string sTitle = tcheck->GetTitle();
99  tcheck->Connect("Clicked()", "PixTab", this, "boxChecked()");
100  continue;
101  }
102 
103 
104  hFrame = new TGHorizontalFrame(fV2, 300, 30, kLHintsExpandX);
105  fV2->AddFrame(hFrame, new TGLayoutHints(kLHintsRight | kLHintsTop));
106 
107  tb = new TGTextBuffer(5);
108  tl = new TGLabel(hFrame, amap[i].first.c_str());
109  tl->SetWidth(100);
110  hFrame->AddFrame(tl, new TGLayoutHints(kLHintsCenterY | kLHintsLeft, fBorderN, fBorderN, fBorderN, fBorderN));
111 
112  te = new TGTextEntry(hFrame, tb, cnt); te->SetWidth(100);
113  hFrame->AddFrame(te, new TGLayoutHints(kLHintsCenterY | kLHintsCenterX, fBorderN, fBorderN, fBorderN, fBorderN));
114  fParIds.push_back(amap[i].first);
115  fParTextEntries.insert(make_pair(amap[i].first, te));
116 
117  te->SetText(amap[i].second.c_str());
118  te->Connect("ReturnPressed()", "PixTab", this, "setParameter()");
119  te->Connect("TextChanged(const char*)", "PixTab", this, "yellow()");
120  te->Connect("ShiftTabPressed()", "PixTab", this, "moveUp()");
121  te->Connect("TabPressed()", "PixTab", this, "moveDown()");
122 
123  tset = new TGTextButton(hFrame, "Set", cnt);
124  tset->Connect("Clicked()", "PixTab", this, "setParameter()");
125  hFrame->AddFrame(tset, new TGLayoutHints(kLHintsCenterY | kLHintsLeft, fBorderN, fBorderN, fBorderN, fBorderN));
126  tset->SetToolTipText("set the parameter\nor click *return* after changing the numerical value");
127  tset->GetToolTip()->SetDelay(2000); // add a bit of delay to ease button hitting
128 
129  ++cnt;
130  }
131 
132  hFrame = new TGHorizontalFrame(fV2);
133  TGTextButton * previous = new TGTextButton(hFrame, "Previous");
134  previous->SetToolTipText("display previous histogram in this test's list");
135  previous->Connect("Clicked()", "PixTab", this, "previousHistogram()");
136  hFrame->AddFrame(previous, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, fBorderN, fBorderN, fBorderN, fBorderN));
137 
138  TGTextButton * next = new TGTextButton(hFrame, "Next");
139  next->SetToolTipText("display next histogram in this test's list");
140  next->Connect("Clicked()", "PixTab", this, "nextHistogram()");
141  hFrame->AddFrame(next, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, fBorderN, fBorderN, fBorderN, fBorderN));
142 
143  TGTextButton * update = new TGTextButton(hFrame, "Update");
144  update->SetToolTipText("update canvas");
145  update->Connect("Clicked()", "PixTab", this, "update()");
146  hFrame->AddFrame(update, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, fBorderN, fBorderN, fBorderN, fBorderN));
147 
148  TGTextButton * clear = new TGTextButton(hFrame, "Clear");
149  clear->SetToolTipText("clear canvas and resest histogram list");
150  clear->Connect("Clicked()", "PixTab", this, "clearHistList()");
151  hFrame->AddFrame(clear, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, fBorderN, fBorderN, fBorderN, fBorderN));
152 
153  fV2->AddFrame(hFrame, new TGLayoutHints(kLHintsLeft | kLHintsBottom, fBorderN, fBorderN, fBorderN, fBorderN));
154 
155 
156  hFrame = new TGHorizontalFrame(fV2);
157  // -- create doTest Button
158  fbDoTest = new TGTextButton(hFrame, " doTest ", B_DOTEST);
159  fbDoTest->ChangeOptions(fbDoTest->GetOptions() | kFixedWidth);
160  hFrame->AddFrame(fbDoTest, new TGLayoutHints(kLHintsLeft | kLHintsTop, fBorderN, fBorderN, fBorderN, fBorderN));
161  fbDoTest->Connect("Clicked()", "PixTest", test, "doTest()");
162  fbDoTest->SetBackgroundColor(fGui->fDarkSalmon);
163 
164  // -- create stop Button
165  TGTextButton *bStop = new TGTextButton(hFrame, " stop ", B_DOSTOP);
166  bStop->SetToolTipText(fTest->getStopTip().c_str());
167  bStop->ChangeOptions(bStop->GetOptions() | kFixedWidth);
168  hFrame->AddFrame(bStop, new TGLayoutHints(kLHintsLeft | kLHintsTop, fBorderN, fBorderN, fBorderN, fBorderN));
169  bStop->Connect("Clicked()", "PixTab", this, "handleButtons(Int_t)");
170 
171  // -- create print Button
172  TGTextButton *bPrint = new TGTextButton(hFrame, " print ", B_PRINT);
173  bPrint->SetToolTipText("create a pdf of the canvas");
174  bPrint->ChangeOptions(bPrint->GetOptions() | kFixedWidth);
175  hFrame->AddFrame(bPrint, new TGLayoutHints(kLHintsLeft | kLHintsTop, fBorderN, fBorderN, fBorderN, fBorderN));
176  bPrint->Connect("Clicked()", "PixTab", this, "handleButtons(Int_t)");
177 
178  // -- create module map button
179  fbModMap = new TGTextButton(hFrame, " summary ", B_MODMAP);
180  fbModMap->ChangeOptions(fbModMap->GetOptions() | kFixedWidth);
181  hFrame->AddFrame(fbModMap, new TGLayoutHints(kLHintsLeft | kLHintsTop, fBorderN, fBorderN, fBorderN, fBorderN));
182  fbModMap->Connect("Clicked()", "PixTab", this, "handleButtons(Int_t)");
183 
184  // -- create TBrowser button
185  fbBrowser = new TGTextButton(hFrame, " browser ", B_BROWSER);
186  fbBrowser->ChangeOptions(fbBrowser->GetOptions() | kFixedWidth);
187  fbBrowser->SetToolTipText("open a TBrowser (for easier histogram navigation)");
188  hFrame->AddFrame(fbBrowser, new TGLayoutHints(kLHintsLeft | kLHintsTop, fBorderN, fBorderN, fBorderN, fBorderN));
189  fbBrowser->Connect("Clicked()", "PixTab", this, "handleButtons(Int_t)");
190 
191  // -- create close Button
192  TGTextButton *bClose = new TGTextButton(hFrame, " close ", B_CLOSETAB);
193  bClose->ChangeOptions(bClose->GetOptions() | kFixedWidth);
194  bClose->SetToolTipText("close the test tab");
195  hFrame->AddFrame(bClose, new TGLayoutHints(kLHintsRight | kLHintsTop, fBorderN, fBorderN, fBorderN, fBorderN));
196  bClose->Connect("Clicked()", "PixTab", this, "handleButtons(Int_t)");
197 
198  fV2->AddFrame(hFrame, new TGLayoutHints(kLHintsLeft | kLHintsBottom, fBorderN, fBorderN, fBorderN, fBorderN));
199 
200  updateToolTips();
201 
202 // fhFrame->MapSubwindows();
203 // fhFrame->Resize(fhFrame->GetDefaultSize());
204 // fhFrame->MapWindow();
205 
206  fTabFrame->AddFrame(fhFrame,
207  new TGLayoutHints(kLHintsRight | kLHintsExpandX | kLHintsExpandY, fBorderN, fBorderN, fBorderN, fBorderN));
208  fTabFrame->MapSubwindows();
209  fTabFrame->Resize(fTabFrame->GetDefaultSize());
210  fTabFrame->MapWindow();
211 
212 }
213 
214 
215 // ----------------------------------------------------------------------
216 PixTab::PixTab() {
217  init(0, 0, "nada");
218 }
219 
220 // ----------------------------------------------------------------------
221 void PixTab::init(PixGui *p, PixTest *test, string tabname) {
222  // LOG(logINFO) << "PixTab::init()";
223  fGui = p;
224  fTest = test;
225  fTabName = tabname;
226  fCount = 0;
227 }
228 
229 // ----------------------------------------------------------------------
230 // PixTab destructor
231 PixTab::~PixTab() {
232  // LOG(logINFO) << "PixTab destructor";
233 }
234 
235 
236 // ----------------------------------------------------------------------
237 void PixTab::handleButtons(Int_t id) {
238 
239  if (!fGui->getTabs()) return;
240 
241  if (id == -1) {
242  TGButton *btn = (TGButton*)gTQSender;
243  id = btn->WidgetId();
244  }
245 
246  switch (id) {
247  case B_DOTEST: {
248  LOG(logDEBUG) << "and now what???";
249  break;
250  }
251 
252  case B_DOSTOP: {
253  fTest->runCommand("stop");
254  LOG(logDEBUG) << "stopping...and now what???";
255  break;
256  }
257 
258  case B_BROWSER: {
259  new TBrowser(getName().c_str(), fTest->getDirectory());
260  break;
261  }
262 
263  case B_MODMAP: {
264  fTest->moduleMap("what");
265  break;
266  }
267 
268  case B_PRINT: {
269  fCount++;
270  LOG(logINFO) << Form("Canvas printed to %s-%d.pdf", fTabName.c_str(), fCount);
271  fEc1->GetCanvas()->SaveAs(Form("%s-%d.pdf", fTabName.c_str(), fCount));
272  break;
273  }
274 
275  case B_CLOSETAB: {
276  delete fEc1; fEc1 = 0;
277  LOG(logDEBUG) << Form("Tab %s closed", fTabName.c_str());
278  fGui->getTabs()->RemoveTab(fGui->getTabs()->GetCurrent());
279  fGui->getTabs()->Layout();
280  delete fTabFrame;
281  delete fhFrame;
282  delete fV1;
283  delete fV2;
284  delete this;
285  break;
286  }
287  }
288 }
289 
290 
291 // ----------------------------------------------------------------------
292 void PixTab::buttonClicked() {
293  TGButton *btn = (TGButton*)gTQSender;
294  LOG(logDEBUG) << "xxxPressed(): " << btn->GetTitle();
295  fTest->runCommand(btn->GetTitle());
296 
297 }
298 
299 // ----------------------------------------------------------------------
300 void PixTab::boxChecked() {
301  TGCheckButton *btn = (TGCheckButton*) gTQSender;
302  string sTitle = btn->GetTitle();
303  LOG(logDEBUG) << "xxxPressed(): " << btn->GetTitle();
304  fTest->setParameter(sTitle,string(btn->IsDown()?"1":"0")) ;
305 
306 }
307 
308 // ----------------------------------------------------------------------
309 void PixTab::yellow() {
310  TGButton *btn = (TGButton *) gTQSender;
311  int id(-1);
312  id = btn->WidgetId();
313  if (-1 == id) {
314  LOG(logDEBUG) << "ASLFDKHAPIUDF ";
315  return;
316  }
317 
318  string svalue = ((TGTextEntry*)(fParTextEntries[fParIds[id]]))->GetText();
319  if (fTest->getParameter(fParIds[id]).compare(svalue)) {
320  ((TGTextEntry*)(fParTextEntries[fParIds[id]]))->SetBackgroundColor(fGui->fYellow);
321  } else {
322  ((TGTextEntry*)(fParTextEntries[fParIds[id]]))->SetBackgroundColor(fGui->fWhite);
323  }
324 }
325 
326 
327 // ----------------------------------------------------------------------
328 void PixTab::moveUp() {
329  TGButton *btn = (TGButton *) gTQSender;
330  int id(-1);
331  id = btn->WidgetId();
332  if (-1 == id) {
333  LOG(logDEBUG) << "ASLFDKHAPIUDF ";
334  return;
335  }
336 
337  if (id > 0) {
338  ((TGTextEntry*)(fParTextEntries[fParIds[id-1]]))->SetFocus();
339  } else {
340  ((TGTextEntry*)(fParTextEntries[fParIds[fParIds.size()-1]]))->SetFocus();
341  }
342 }
343 
344 // ----------------------------------------------------------------------
345 void PixTab::moveDown() {
346  TGButton *btn = (TGButton *) gTQSender;
347  int id(-1);
348  id = btn->WidgetId();
349  if (-1 == id) {
350  LOG(logDEBUG) << "ASLFDKHAPIUDF ";
351  return;
352  }
353 
354  if (id < static_cast<int>(fParIds.size()) - 1) {
355  ((TGTextEntry*)(fParTextEntries[fParIds[id+1]]))->SetFocus();
356  } else {
357  ((TGTextEntry*)(fParTextEntries[fParIds[0]]))->SetFocus();
358  }
359 }
360 
361 // ----------------------------------------------------------------------
362 void PixTab::setParameter() {
363  if (!fGui->getTabs()) return;
364  // LOG(logINFO) << "PixTab::setParameter: ";
365 
366  TGButton *btn = (TGButton *) gTQSender;
367  int id(-1);
368  id = btn->WidgetId();
369  if (-1 == id) {
370  LOG(logDEBUG) << "ASLFDKHAPIUDF ";
371  return;
372  }
373 
374  string svalue = ((TGTextEntry*)(fParTextEntries[fParIds[id]]))->GetText();
375  ((TGTextEntry*)(fParTextEntries[fParIds[id]]))->SetBackgroundColor(fGui->fWhite);
376 
377  LOG(logDEBUG) << "xxxPressed(): ID = " << id
378  << " -> " << fParIds[id]
379  << " to value " << svalue;
380 
381  fTest->setParameter(fParIds[id], svalue);
382  updateToolTips();
383 }
384 
385 
386 // ----------------------------------------------------------------------
387 void PixTab::clearHistList() {
388  clearCanvas();
389  fTest->clearHistList();
390 }
391 
392 // ----------------------------------------------------------------------
393 void PixTab::clearCanvas() {
394  TCanvas *c = fEc1->GetCanvas();
395  c->Clear();
396  update();
397 }
398 
399 // ----------------------------------------------------------------------
400 void PixTab::nextHistogram() {
401  TH1 *h = fTest->nextHist();
402  if (h) {
403  string option = fTest->getHistOption(h);
404  if (string::npos == option.find("same")) clearCanvas();
405  if (h->InheritsFrom(TH2::Class())) {
406  h->Draw(option.c_str());
407  } else {
408  // cout << "h: " << h->GetName() << " option: " << option << endl;
409  h->Draw(option.c_str());
410  }
411  update();
412  } else {
413  LOG(logDEBUG) << "no previous histogram found ";
414  }
415 
416 }
417 
418 
419 // ----------------------------------------------------------------------
420 void PixTab::previousHistogram() {
421  TH1 *h = fTest->previousHist();
422  if (h) {
423  string option = fTest->getHistOption(h);
424  if (string::npos == option.find("same")) clearCanvas();
425  if (h->InheritsFrom(TH2::Class())) {
426  h->Draw(option.c_str());
427  } else {
428  // cout << "h: " << h->GetName() << " option: " << option << endl;
429  h->Draw(option.c_str());
430  }
431  update();
432  } else {
433  LOG(logDEBUG) << "no previous histogram found ";
434  }
435 }
436 
437 
438 // ----------------------------------------------------------------------
439 void PixTab::update() {
440  TCanvas *c = fEc1->GetCanvas();
441  TList* l = c->GetListOfPrimitives();
442  TIter next(l);
443  while (TObject *h = next()) {
444  if (h->InheritsFrom(TH1::Class())) {
445  if (h->InheritsFrom(TH2::Class())) {
446  gStyle->SetOptStat(0);
447  break;
448  } else {
449  gStyle->SetOptStat(1111111);
450  }
451  }
452  }
453 
454  c->cd();
455  c->Modified();
456  c->Update();
457 }
458 
459 
460 // ----------------------------------------------------------------------
461 void PixTab::statusBarUpdate(Int_t event, Int_t px, Int_t py, TObject *selected) {
462  // char text0[200], text2[200];
463  string text2;
464  const char* text0 = selected->GetName();
465  fStatusBar->SetText(text0, 0);
466 
467  if (event == kKeyPress) {
468  LOG(logDEBUG) << "key pressed?";
469  }
470 
471  // sprintf(text1, "%d,%d", px, py);
472  // fStatusBar->SetText(text1, 1);
473  if (selected->InheritsFrom(TH1::Class())) {
474  string trafo = selected->GetObjectInfo(px,py);
475  string::size_type s1 = trafo.find("binx");
476  string trafo1 = trafo.substr(s1);
477  float x, y, val;
478  if (selected->InheritsFrom(TH2::Class())) {
479  sscanf(trafo1.c_str(), "binx=%f, biny=%f, binc=%f", &x, &y, &val);
480  if (52 == ((TH2D*)selected)->GetNbinsX() && 80 == ((TH2D*)selected)->GetNbinsY()) {
481  text2 = Form("c=%.0f, r=%.0f, value=%4.3f", x-1, y-1, val);
482  } else if (160 == ((TH2D*)selected)->GetNbinsX() && 416 == ((TH2D*)selected)->GetNbinsY()) {
483  text2 = Form("x=%.0f, y=%.0f, value=%4.3f", x-1, y-1, val);
484  } else {
485  text2 = trafo1;
486  // cout << "text2: " << text2 << " trafo1: " << trafo1 << endl;
487  }
488  } else {
489  sscanf(trafo1.c_str(), "binx=%f, binc=%f", &x, &val);
490  text2 = Form("x=%.0f, value=%4.3f", x-1, val);
491  }
492  } else {
493  text2 = selected->GetObjectInfo(px,py);
494  }
495  fStatusBar->SetText(text2.c_str(), 1);
496 }
497 
498 
499 // ----------------------------------------------------------------------
500 void PixTab::updateToolTips() {
501  string tooltip = string(Form("%s test algorithm (patience may be required)", fTest->getName().c_str()))
502  + string("\n") + fTest->getTestTip()
503  ;
504 
505  fbDoTest->SetToolTipText(tooltip.c_str());
506 
507  tooltip = string(Form("summary plot for %s", fTest->getName().c_str()))
508  + string("\n") + fTest->getSummaryTip()
509  ;
510  fbModMap->SetToolTipText(tooltip.c_str());
511 
512 
513 }
Definition: PixTab.hh:22
Definition: PixGui.hh:43