#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class MatNav { RQ_OBJECT("MatNav"); private: TGMainFrame *fMain; TRootEmbeddedCanvas *fCanvas; TGHorizontalFrame *fHorz; TGLayoutHints *fLbut; TGLayoutHints *fLhorz; TGLayoutHints *fLcan; TGNumberEntry *fRow; TGNumberEntry *fRowCount; TGTextEntry *fText; TGTextBuffer *fTextBuf; TGStatusBar *fStatusBar; TGLayoutHints *fStatusBarLayout; TH2 *fMat; TH1 *fPx; bool showmat; public: MatNav(TH2* mat, const char* fun); ~MatNav(); // slots void SelectRow(Long_t val); void CloseWindow(); void FunctionChanged(); void CanvasEvent(Int_t event, Int_t x, Int_t y, TObject* obj); }; void MatNav::SelectRow(Long_t) { int row = fRow->GetIntNumber(); int rowcount = fRowCount->GetIntNumber(); const char* text = fText->GetText(); TCanvas* c = fCanvas->GetCanvas(); c->cd(showmat ? 2 : 0); fPx = fMat->ProjectionX("_px", row, row+rowcount-1, "o"); fPx->Draw(); int l = strlen(text); if( l>0 && fPx->GetEntries() > 0 ) { if( strstr( text, "%d" ) ) { char tmp[2*l+20]; sprintf( tmp, text, row ); cout << "Calling '" << tmp << "'" << endl; gROOT->ProcessLine( tmp ); } else { cout << "Calling '" << text << "' for row " << row << endl; gROOT->ProcessLine( text ); } } c->Update(); } void MatNav::CanvasEvent(Int_t event, Int_t px, Int_t py, TObject* selected) { static int lastx, lasty; if( event == kKeyPress && px == 0 ) { int step = 0; switch(py) { case kKey_Up: step = +1; break; case kKey_Down: step = -1; break; } if( step != 0 ) { fRow->GetNumberEntry()->IncreaseNumber(TGNumberFormat::kNSSSmall,step,false); SelectRow(fRow->GetIntNumber()); // for some reason, several keys in a row only work with // the following "Pick"; this is related to the // fPx->Draw() in SelectRow() which makes the Canvas // forget about the "selected" object and without selected // object, it does not process key events; Pick selects an // object again fCanvas->GetCanvas()->Pick(lastx, lasty, 0); } } else { lastx = px; lasty = py; char atext[256]; sprintf(atext, "%d,%d", px, py); fStatusBar->SetText(atext,2); if( selected ) { fStatusBar->SetText(selected->GetTitle(),0); fStatusBar->SetText(selected->GetName(),1); fStatusBar->SetText(selected->GetObjectInfo(px,py),3); } } } MatNav::MatNav(TH2* mat, const char* fun) : fMat(mat) , fPx(0) , showmat(false) { fMain = new TGMainFrame(gClient->GetRoot(), 100, 100); fMain->Connect("CloseWindow()", "MatNav", this, "CloseWindow()"); fCanvas = new TRootEmbeddedCanvas("Canvas", fMain, 600, 400); fLcan = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,2,2,2,2); fMain->AddFrame(fCanvas, fLcan); TCanvas* c = fCanvas->GetCanvas(); c->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MatNav", this, "CanvasEvent(Int_t,Int_t,Int_t,TObject*)"); if( showmat ) { c->Divide(2,1); c->Modified(); c->Update(); c->cd(1); c->Modified(); c->Update(); fMat->Draw(); c->Modified(); c->Update(); } fLhorz = new TGLayoutHints(kLHintsExpandX,2,2,2,2); fHorz = new TGHorizontalFrame(fMain, 100, 100); fMain->AddFrame(fHorz, fLhorz); fRow = new TGNumberEntry(fHorz, 0, 12, 10); fRow->SetFormat(TGNumberFormat::kNESInteger); fRow->SetLimits( TGNumberFormat::kNELLimitMinMax, 1, fMat->GetNbinsY() ); fRow->SetIntNumber(1); fRow->Connect("ValueSet(Long_t)", "MatNav", this, "SelectRow(Long_t)"); fHorz->AddFrame(fRow); fRowCount = new TGNumberEntry(fHorz, 0, 12, 10); fRowCount->SetFormat(TGNumberFormat::kNESInteger); fRowCount->SetLimits( TGNumberFormat::kNELLimitMinMax, 1, fMat->GetNbinsY() ); fRowCount->SetIntNumber(1); fRowCount->Connect("ValueSet(Long_t)", "MatNav", this, "SelectRow(Long_t)"); fHorz->AddFrame(fRowCount); fLbut = new TGLayoutHints(kLHintsExpandX|kLHintsTop); fText = new TGTextEntry(fHorz, fTextBuf = new TGTextBuffer(40), 1); fText->SetText( fun ); fHorz->AddFrame(fText, fLbut); int parts[] = { 33, 10, 10, 47 }; fStatusBar = new TGStatusBar(fMain, 10, 10); fStatusBar->SetParts(parts, 4); fStatusBarLayout = new TGLayoutHints(kLHintsBottom | kLHintsLeft | kLHintsExpandX, 2, 2, 1, 1); fMain->AddFrame(fStatusBar, fStatusBarLayout); fMain->SetWindowName("Matrix Navigator"); fMain->MapSubwindows(); fMain->Resize(fMain->GetDefaultSize()); fMain->MapWindow(); SelectRow(0); } void MatNav::CloseWindow() { delete this; } void MatNav::FunctionChanged() { } MatNav::~MatNav() { delete fPx; delete fText; //delete fTextBuf; // not deleted in guitest example! delete fStatusBar; delete fRowCount; delete fRow; delete fHorz; delete fCanvas; delete fMain; } void matnav(TH2* mat, const char* fun="") { new MatNav(mat, fun); }