00001
00002
00003 #include <iostream>
00004 #include <iomanip>
00005 #include <sstream>
00006
00007
00008 #include "TCanvas.h"
00009 #include "TGClient.h"
00010 #include "TGButton.h"
00011 #include "TGFrame.h"
00012 #include "TGTab.h"
00013 #include "TGTableLayout.h"
00014 #include "TRootEmbeddedCanvas.h"
00015
00016
00017 #include "PhysicsNtuple/Factory.h"
00018
00019 #include "EventDisplay.h"
00020
00021 Anp::EventDisplay *Anp::EventDisplay::fgInstance = 0;
00022
00023
00024
00025
00026 Anp::EventDisplay& Anp::EventDisplay::Instance()
00027 {
00028 if(!fgInstance)
00029 {
00030 fgInstance = new Anp::EventDisplay();
00031 }
00032
00033 return *fgInstance;
00034 }
00035
00036 using namespace std;
00037
00038
00039 Anp::EventDisplay::EventDisplay()
00040 :fMain(NULL),
00041 fTab(NULL),
00042 fStore(),
00043 fZoom(false)
00044 {
00045 }
00046
00047
00048 void Anp::EventDisplay::Add(const Record &record)
00049 {
00050 fStore.Add(record);
00051 }
00052
00053
00054 void Anp::EventDisplay::Add(EventTabPtr tab)
00055 {
00056 if(!tab.valid())
00057 {
00058 cerr << "EventDisplay::Add() - ignoring invalid tab" << endl;
00059 }
00060
00061 fTabs.push_back(tab);
00062
00063 if(fTab && fMain)
00064 {
00065 InitTab(tab);
00066 }
00067 }
00068
00069
00070 Anp::EventTabPtr Anp::EventDisplay::Add(const string &name)
00071 {
00072 EventTabPtr tab = Factory<EventTab>::Instance().Create(name);
00073
00074 if(!tab.valid())
00075 {
00076 cerr << "EventDisplay::Init() - failed to create tab:" << name << endl;
00077 return EventTabPtr(0);
00078 }
00079
00080 tab -> SetName(name);
00081
00082 Add(tab);
00083
00084 return tab;
00085 }
00086
00087
00088 void Anp::EventDisplay::InitTab(EventTabPtr tab)
00089 {
00090 if(!fTab || !fMain)
00091 {
00092 return;
00093 }
00094
00095 string label = tab -> GetLabel();
00096 if(label.empty())
00097 {
00098 label = tab -> GetName();
00099 tab -> SetLabel(label);
00100 }
00101
00102 TGCompositeFrame *tf = fTab -> AddTab(label.c_str());
00103
00104 TGLayoutHints* lh = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY);
00105 TRootEmbeddedCanvas *rec = new TRootEmbeddedCanvas(label.c_str(), tf, 700,700);
00106
00107 tf -> AddFrame(rec, lh);
00108
00109 tab -> Init(rec -> GetCanvas());
00110 tab -> SetEmbedded(rec);
00111
00112 fTab -> MapSubwindows();
00113 fTab -> Resize(fTab -> GetDefaultSize());
00114 fTab -> Layout();
00115 }
00116
00117
00118 Anp::EventTabPtr Anp::EventDisplay::GetCurrent()
00119 {
00120 const int current = fTab -> GetCurrent();
00121
00122 if(current < static_cast<int>(fTabs.size()))
00123 {
00124 return fTabs[current];
00125 }
00126 else
00127 {
00128 cerr << "EventDisplay::GetCurrent() - requested tab does not exists" << endl;
00129 }
00130
00131 return EventTabPtr(0);
00132 }
00133
00134
00135 void Anp::EventDisplay::Selected(int tab)
00136 {
00137 if(tab < static_cast<int>(fTabs.size()))
00138 {
00139 fTabs[tab] -> Expose();
00140 }
00141 else
00142 {
00143 cerr << "EventDisplay::Selected() - tab index is out of range" << endl;
00144 }
00145 }
00146
00147
00148 void Anp::EventDisplay::Init()
00149 {
00150 TGWindow* main = const_cast<TGWindow*> (gClient->GetRoot());
00151 fMain = new TGMainFrame(main, 1000, 900, kHorizontalFrame);
00152 fMain -> SetWMSize(900, 700);
00153 fMain -> SetCleanup(kDeepCleanup);
00154 fMain -> Connect("CloseWindow()", "Anp::EventDisplay", this, "CloseWindow()");
00155
00156
00157
00158
00159
00160 fTab = new TGTab(fMain, 900, 900);
00161 TGLayoutHints *thints = new TGLayoutHints(kLHintsRight | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2);
00162 fTab -> Connect("Selected(Int_t)", "Anp::EventDisplay", this, "Selected(int)");
00163 fMain -> AddFrame(fTab, thints);
00164 cout<<"EventDisplay::Init() before for loop" <<endl;
00165 for(TabVec::const_iterator cit = fTabs.begin(); cit != fTabs.end(); ++cit)
00166 {
00167 InitTab(*cit);
00168 }
00169 cout<<" EventDisplay::Init() Add Buttons"<<endl;
00170
00171
00172
00173
00174 TGVerticalFrame *bframe = new TGVerticalFrame(fMain, 80, 1000);
00175 bframe -> SetHeight(1000);
00176 bframe -> SetWidth(80);
00177 TGLayoutHints *lh = new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 2, 2, 2, 2);
00178
00179 TGTextButton *bnevent = new TGTextButton(bframe, "Next event", 0);
00180 TGTextButton *bpevent = new TGTextButton(bframe, "Prev event", 0);
00181 TGTextButton *bnsnarl = new TGTextButton(bframe, "Next snarl", 0);
00182 TGTextButton *bpsnarl = new TGTextButton(bframe, "Prev snarl", 0);
00183
00184 TGTextButton *bzoomi = new TGTextButton(bframe, "Zoom In", 0);
00185 TGTextButton *bzoomo = new TGTextButton(bframe, "Zoom Out", 0);
00186 TGTextButton *btruth = new TGTextButton(bframe, "Print truth", 0);
00187 TGTextButton *bdata = new TGTextButton(bframe, "Print data", 0);
00188 TGTextButton *bprint = new TGTextButton(bframe, "Print tab", 0);
00189 TGTextButton *bclose = new TGTextButton(bframe, "Close", 0);
00190
00191 TGLayoutHints *tloh = new TGLayoutHints(kLHintsTop);
00192
00193 bnevent -> Connect("Clicked()", "Anp::EventDisplay", this, "NextEvent()");
00194 bpevent -> Connect("Clicked()", "Anp::EventDisplay", this, "PrevEvent()");
00195 bnsnarl -> Connect("Clicked()", "Anp::EventDisplay", this, "NextSnarl()");
00196 bpsnarl -> Connect("Clicked()", "Anp::EventDisplay", this, "PrevSnarl()");
00197
00198 bzoomi -> Connect("Clicked()", "Anp::EventDisplay", this, "ZoomIn()");
00199 bzoomo -> Connect("Clicked()", "Anp::EventDisplay", this, "ZoomOut()");
00200 btruth -> Connect("Clicked()", "Anp::EventDisplay", this, "PrintTruth()");
00201 bdata -> Connect("Clicked()", "Anp::EventDisplay", this, "PrintData()");
00202 bprint -> Connect("Clicked()", "Anp::EventDisplay", this, "PrintTab()");
00203 bclose -> Connect("Clicked()", "Anp::EventDisplay", this, "CloseWindow()");
00204
00205 bframe -> AddFrame(bnevent, tloh);
00206 bframe -> AddFrame(bpevent, tloh);
00207 bframe -> AddFrame(bnsnarl, tloh);
00208 bframe -> AddFrame(bpsnarl, tloh);
00209 bframe -> AddFrame(bzoomi, tloh);
00210 bframe -> AddFrame(bzoomo, tloh);
00211 bframe -> AddFrame(btruth, tloh);
00212 bframe -> AddFrame(bprint, tloh);
00213 bframe -> AddFrame(bclose, tloh);
00214
00215 fMain -> AddFrame(bframe, lh);
00216
00217 fMain -> SetWindowName("Event display");
00218
00219 fMain -> MapSubwindows();
00220 fMain -> Resize(fMain -> GetDefaultSize());
00221 fMain -> MapWindow();
00222 fMain -> Layout();
00223 fMain -> Print();
00224 cout<<" Done with Event Display:: Init"<<endl;
00225 }
00226
00227
00228 void Anp::EventDisplay::NextEvent()
00229 {
00230 fStore.NextEvent();
00231 NewEvent();
00232 }
00233
00234
00235 void Anp::EventDisplay::PrevEvent()
00236 {
00237 fStore.PrevEvent();
00238 NewEvent();
00239 }
00240
00241
00242 void Anp::EventDisplay::NextSnarl()
00243 {
00244 fStore.NextSnarl();
00245 NewEvent();
00246 }
00247
00248
00249 void Anp::EventDisplay::PrevSnarl()
00250 {
00251 fStore.PrevSnarl();
00252 NewEvent();
00253 }
00254
00255
00256 void Anp::EventDisplay::ZoomIn()
00257 {
00258 fZoom = true;
00259
00260 for(TabVec::const_iterator cit = fTabs.begin(); cit != fTabs.end(); ++cit)
00261 {
00262 (*cit) -> ZoomIn();
00263 }
00264
00265 Selected(fTab -> GetCurrent());
00266 }
00267
00268
00269 void Anp::EventDisplay::ZoomOut()
00270 {
00271 fZoom = false;
00272
00273 for(TabVec::const_iterator cit = fTabs.begin(); cit != fTabs.end(); ++cit)
00274 {
00275 (*cit) -> ZoomOut();
00276 }
00277
00278 Selected(fTab -> GetCurrent());
00279 }
00280
00281
00282 void Anp::EventDisplay::NewEvent()
00283 {
00284 if(!fStore.IsValid())
00285 {
00286 cerr << "EventDisplay::NewEvent() - bad RecordStore status..." << endl;
00287 return;
00288 }
00289
00290 const Header &header = fStore.CurrSnarl() -> GetHeader();
00291
00292 cout << "Run = " << setw(6) << header.Run()
00293 << " snarl = " << setw(6) << header.Snarl()
00294 << " event = " << setw(2) << fStore.CurrEvent() -> EventIndex()
00295 << endl;
00296
00297 for(TabVec::const_iterator cit = fTabs.begin(); cit != fTabs.end(); ++cit)
00298 {
00299 (*cit) -> Set(*fStore.CurrEvent(), *fStore.CurrSnarl());
00300
00301 if(fZoom)
00302 {
00303 (*cit) -> ZoomIn();
00304 }
00305 }
00306
00307 Selected(fTab -> GetCurrent());
00308 }
00309
00310
00311 void Anp::EventDisplay::PrintTruth()
00312 {
00313 if(fStore.IsValid())
00314 {
00315 fStore.PrintTruth();
00316 }
00317 }
00318
00319 void Anp::EventDisplay::PrintData()
00320 {
00321 if(fStore.IsValid())
00322 {
00323 fStore.PrintData();
00324 }
00325 }
00326
00327
00328
00329 void Anp::EventDisplay::PrintTab()
00330 {
00331 if(!fTab)
00332 {
00333 cout << "PrintTab: no tabs exists" << endl;
00334 return;
00335 }
00336
00337 if(!fStore.IsValid())
00338 {
00339 cout << "PrintTab:: bad RecordStore" << endl;
00340 return;
00341 }
00342
00343 RecordStore::SnarlIter snarl = fStore.CurrSnarl();
00344 if(snarl == fStore.SnarlEnd())
00345 {
00346 cout << "PrintTab: no snarls found" << endl;
00347 return;
00348 }
00349
00350 RecordStore::EventIter event = fStore.CurrEvent();
00351 if(event == fStore.EventEnd())
00352 {
00353 cout << "PrintTab: no events found" << endl;
00354 return;
00355 }
00356
00357 EventTabPtr tab = GetCurrent();
00358 if(!tab.valid())
00359 {
00360 cout << "PrintTab: current tab is not valid" << endl;
00361 return;
00362 }
00363
00364 TRootEmbeddedCanvas *emcan = tab -> GetEmbedded();
00365 if(!emcan)
00366 {
00367 cout << "PrintTab: invalid pointer to TRootEmbeddedCanvas" << endl;
00368 return;
00369 }
00370
00371 TCanvas *canvas = emcan -> GetCanvas();
00372 if(!canvas)
00373 {
00374 cout << "PrintTab: TRootEmbeddedCanvas has invalid TCanvas" << endl;
00375 return;
00376 }
00377
00378 const Header &header = snarl -> GetHeader();
00379
00380 stringstream name;
00381 name << tab -> GetName() << "_run_" << header.Run()
00382 << "_snarl_" << header.Snarl() << "_event_" << event -> EventIndex()
00383 << ".eps";
00384
00385 canvas -> Print(name.str().c_str());
00386 }
00387
00388
00389 void Anp::EventDisplay::CloseWindow()
00390 {
00391 if(fMain)
00392 {
00393 fMain -> CloseWindow();
00394 }
00395 }