26 #include "TRestBrowser.h"
28 #include <TTreeFormula.h>
32 TRestBrowser::TRestBrowser() {
33 if ((TDirectory*)gDirectory !=
nullptr && gDirectory->GetFile() !=
nullptr) {
35 SetViewer(
"TRestEventViewer");
36 OpenFile(gDirectory->GetFile()->GetName());
37 cout <<
"Loaded File : " << fInputFileName << endl;
39 fBrowser =
new TBrowser(
"Browser",
nullptr,
"REST Browser");
44 TRestBrowser::TRestBrowser(
const TString& viewerName, Double_t geomScale) {
46 SetViewer(viewerName, geomScale);
49 TRestBrowser::~TRestBrowser() {
50 if (frmMain !=
nullptr) frmMain->Cleanup();
54 void TRestBrowser::Initialize(
const TString& opt) {
55 pureAnalysis = kFALSE;
59 fBrowser =
new TBrowser(
"Browser", 0,
"REST Browser", opt);
60 TGMainFrame* fr = fBrowser->GetBrowserImp()->GetMainFrame();
62 RESTWarning <<
"No x11 interface is available. Cannot call the browser window!" << RESTendl;
67 fBrowser->StartEmbedding(0, -1);
68 frmMain =
new TGMainFrame(gClient->GetRoot(), 300);
69 frmMain->SetCleanup(kDeepCleanup);
70 frmMain->SetWindowName(
"Controller1");
71 SetLeftPanelButtons();
72 fBrowser->StopEmbedding();
74 fBrowser->StartEmbedding(1, -1);
75 fCanDefault =
new TCanvas();
76 fBrowser->StopEmbedding();
78 fBrowser->StartEmbedding(2, -1);
79 frmBot =
new TGMainFrame(gClient->GetRoot(), 300);
80 frmBot->SetCleanup(kDeepCleanup);
81 frmBot->SetWindowName(
"Controller2");
82 SetBottomPanelButtons();
83 fBrowser->StopEmbedding();
93 if (fEventViewer !=
nullptr) {
94 cout <<
"Event viewer has already been set!" << endl;
99 fEventViewer->SetGeomScale(geomScale);
106 void TRestBrowser::SetViewer(
const TString& viewerName, Double_t geomScale) {
107 if (
Count((
string)viewerName,
"Viewer") > 0) {
109 viewer->SetGeomScale(geomScale);
110 if (viewer !=
nullptr) {
111 SetViewer(viewer, geomScale);
113 RESTError << viewerName <<
" not recognized! Did you install the corresponding library?"
115 RESTError <<
"Also check EVE feature is turned on in REST for 3d event viewing." << RESTendl;
116 RESTWarning <<
"Using default event viewer" << RESTendl;
119 cout <<
"illegal viewer : " << viewerName << endl;
124 void TRestBrowser::SetLeftPanelButtons() {
125 fVFrame =
new TGVerticalFrame(frmMain);
126 fVFrame->Resize(300, 200);
129 fEventRowLabel =
new TGLabel(fVFrame,
"Entry:");
130 fVFrame->AddFrame(fEventRowLabel,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
132 fEventRowNumberBox =
new TGNumberEntry(fVFrame, fEventRow);
133 fEventRowNumberBox->Connect(
"ValueSet(Long_t)",
"TRestBrowser",
this,
"RowValueChangedAction(Long_t)");
134 fVFrame->AddFrame(fEventRowNumberBox,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
137 auto labelBar =
new TGHorizontalFrame(fVFrame);
139 fEventIdLabel =
new TGLabel(labelBar,
"Event ID:");
140 labelBar->AddFrame(fEventIdLabel,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
142 fEventSubIdLabel =
new TGLabel(labelBar,
"Sub ID:");
143 labelBar->AddFrame(fEventSubIdLabel,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
145 fVFrame->AddFrame(labelBar,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
147 auto numberBoxBar =
new TGHorizontalFrame(fVFrame);
149 fEventIdNumberBox =
new TGNumberEntry(numberBoxBar, fEventId);
150 fEventIdNumberBox->Connect(
"ValueSet(Long_t)",
"TRestBrowser",
this,
"IdValueChangedAction(Long_t)");
151 numberBoxBar->AddFrame(fEventIdNumberBox,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
153 fEventSubIdNumberBox =
new TGNumberEntry(numberBoxBar, fEventSubId);
154 fEventSubIdNumberBox->Connect(
"ValueSet(Long_t)",
"TRestBrowser",
this,
155 "IdValueChangedAction(Long_t)");
156 numberBoxBar->AddFrame(fEventSubIdNumberBox,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
158 fVFrame->AddFrame(numberBoxBar,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
161 fEventTypeLabel =
new TGLabel(fVFrame,
"Event Type:");
162 fVFrame->AddFrame(fEventTypeLabel,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
164 fEventTypeComboBox =
new TGComboBox(fVFrame);
165 fEventTypeComboBox->Connect(
"Selected(Int_t)",
"TRestBrowser",
this,
"EventTypeChangedAction(Int_t)");
166 fVFrame->AddFrame(fEventTypeComboBox,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
169 fPlotOptionLabel =
new TGLabel(fVFrame,
"Plot Options:");
170 fVFrame->AddFrame(fPlotOptionLabel,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
172 fPlotOptionTextBox =
new TGTextEntry(fVFrame,
"");
173 fPlotOptionTextBox->SetText(
"");
174 fPlotOptionTextBox->Connect(
"ReturnPressed()",
"TRestBrowser",
this,
"PlotAction()");
175 fVFrame->AddFrame(fPlotOptionTextBox,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
177 auto switchButtonBar =
new TGHorizontalFrame(fVFrame);
179 fButOptPrev =
new TGPictureButton(switchButtonBar, gClient->GetPicture(
"bld_undo.png"));
180 fButOptPrev->Connect(
"Clicked()",
"TRestBrowser",
this,
"PreviousPlotOptionAction()");
181 switchButtonBar->AddFrame(fButOptPrev,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
183 fButOptRefresh =
new TGTextButton(switchButtonBar,
"Plot");
184 fButOptRefresh->Connect(
"Clicked()",
"TRestBrowser",
this,
"PlotAction()");
185 switchButtonBar->AddFrame(fButOptRefresh,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
187 fButOptNext =
new TGPictureButton(switchButtonBar, gClient->GetPicture(
"bld_redo.png"));
189 fButOptNext->Connect(
"Clicked()",
"TRestBrowser",
this,
"NextPlotOptionAction()");
190 switchButtonBar->AddFrame(fButOptNext,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
192 fVFrame->AddFrame(switchButtonBar,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
195 fMenuOpen =
new TGPictureButton(fVFrame, gClient->GetPicture(
"bld_open.png"));
196 fMenuOpen->Connect(
"Clicked()",
"TRestBrowser",
this,
"LoadFileAction()");
197 fVFrame->AddFrame(fMenuOpen,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
199 fExit =
new TGPictureButton(fVFrame, gClient->GetPicture(
"bld_exit.png"));
201 fExit->Connect(
"Clicked()",
"TRestBrowser",
this,
"ExitAction()");
202 fVFrame->AddFrame(fExit,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
204 frmMain->Resize(TGDimension(300, frmMain->GetHeight() + fVFrame->GetHeight()));
206 frmMain->AddFrame(fVFrame,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
208 frmMain->MapSubwindows();
211 frmMain->MapWindow();
215 fHFrame =
new TGVerticalFrame(frmBot);
216 fHFrame->Resize(300, 100);
218 fSelectionTextBoxLabel =
new TGLabel(fHFrame,
"Selection:");
219 fHFrame->AddFrame(fSelectionTextBoxLabel,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
221 fSelectionTextBox =
new TGTextEntry(fHFrame,
"");
222 fHFrame->AddFrame(fSelectionTextBox,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
224 auto bottomBar =
new TGHorizontalFrame(fHFrame);
226 fButEveNext =
new TGTextButton(bottomBar,
"Previous Event");
227 fButEveNext->Connect(
"Clicked()",
"TRestBrowser",
this,
"PreviousEventAction()");
228 bottomBar->AddFrame(fButEveNext,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
230 fButEvePrev =
new TGTextButton(bottomBar,
"Next Event");
231 fButEvePrev->Connect(
"Clicked()",
"TRestBrowser",
this,
"NextEventAction()");
232 bottomBar->AddFrame(fButEvePrev,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
234 fHFrame->AddFrame(bottomBar,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
236 frmBot->AddFrame(fHFrame,
new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
237 frmBot->MapSubwindows();
242 void TRestBrowser::InitFromConfigFile() { cout << __PRETTY_FUNCTION__ << endl; }
244 void TRestBrowser::SetInputEvent(
TRestEvent* event) {
245 if (fRestRun !=
nullptr) {
246 fRestRun->SetInputEvent(event);
250 Bool_t TRestBrowser::LoadEventEntry(Int_t n) {
251 if (fRestRun->GetInputFile() ==
nullptr || fRestRun->GetInputFile()->IsZombie()) {
252 RESTWarning <<
"TRestBrowser::LoadEventEntry. No File..." << RESTendl;
256 RESTWarning <<
"TRestBrowser::LoadEventEntry. This is a pure analysis file..." << RESTendl;
260 if (fRestRun->GetAnalysisTree() !=
nullptr && n < fRestRun->GetAnalysisTree()->GetEntries() && n >= 0) {
261 fRestRun->GetEntry(n);
264 RESTError <<
"internal error!" << RESTendl;
267 fEventRow = fRestRun->GetCurrentEntry();
268 fEventId = ev->GetID();
269 fEventSubId = ev->GetSubID();
271 fEventRowNumberBox->SetIntNumber(fEventRow);
272 fEventIdNumberBox->SetIntNumber(fEventId);
273 fEventSubIdNumberBox->SetIntNumber(fEventSubId);
274 fRestRun->GetAnalysisTree()->PrintObservables();
277 RESTWarning <<
"TRestBrowser::LoadEventEntry. Event out of limits" << RESTendl;
281 if (fEventViewer !=
nullptr) {
282 fEventViewer->AddEvent(fRestRun->GetInputEvent());
283 fEventViewer->Plot(fPlotOptionTextBox->GetText());
291 Bool_t TRestBrowser::LoadEventId(Int_t eventID, Int_t subEventID) {
292 if (fRestRun->GetInputFile() ==
nullptr || fRestRun->GetInputFile()->IsZombie()) {
293 RESTWarning <<
"TRestBrowser::LoadEventEntry. No File..." << RESTendl;
298 RESTWarning <<
"TRestBrowser::LoadEventEntry. This is a pure analysis file..." << RESTendl;
302 if (fRestRun->GetAnalysisTree() !=
nullptr && fRestRun->GetAnalysisTree()->GetEntries() > 0) {
303 TRestEvent*
event = fRestRun->GetEventWithID(eventID, subEventID);
304 if (event !=
nullptr) {
305 RESTWarning <<
"Event ID : " << eventID <<
" with sub ID : " << subEventID <<
" not found!"
309 fEventRow = fRestRun->GetCurrentEntry();
310 fEventId =
event->GetID();
311 fEventSubId =
event->GetSubID();
313 fEventRowNumberBox->SetIntNumber(fEventRow);
314 fEventIdNumberBox->SetIntNumber(fEventId);
315 fEventSubIdNumberBox->SetIntNumber(fEventSubId);
316 fRestRun->GetAnalysisTree()->PrintObservables();
319 RESTWarning <<
"TRestBrowser::LoadEventEntry. Event out of limits" << RESTendl;
323 if (fEventViewer !=
nullptr) {
324 fEventViewer->AddEvent(fRestRun->GetInputEvent());
325 fEventViewer->Plot(fPlotOptionTextBox->GetText());
333 Bool_t TRestBrowser::OpenFile(
const TString& filename) {
335 fInputFileName = filename;
337 fRestRun->OpenInputFile(fInputFileName);
338 fRestRun->GetInputFile()->cd();
339 TTree* t = fRestRun->GetEventTree();
341 TGeoManager* geometry = gGeoManager;
345 TObjArray* branches = t->GetListOfBranches();
346 for (
int i = 0; i <= branches->GetLast(); i++) {
347 auto branch = (TBranch*)branches->At(i);
348 if (((
string)branch->GetName()).find(
"EventBranch") != string::npos) {
349 string eventType =
Replace((
string)branch->GetName(),
"Branch",
"");
350 fEventTypeComboBox->AddEntry(eventType.c_str(), fEventTypeComboBox->GetNumberOfEntries());
352 if (fRestRun->GetInputEvent() !=
nullptr &&
353 (
string)fRestRun->GetInputEvent()->ClassName() == eventType) {
354 fEventTypeComboBox->Select(fEventTypeComboBox->GetNumberOfEntries() - 1,
false);
360 pureAnalysis = kFALSE;
361 if (fEventViewer ==
nullptr) SetViewer(
"TRestEventViewer");
362 if (geometry !=
nullptr && fEventViewer !=
nullptr) fEventViewer->SetGeometry(geometry);
363 RowValueChangedAction(0);
365 pureAnalysis = kTRUE;
370 RESTError <<
"internal error!" << RESTendl;
372 fEventRowNumberBox->SetIntNumber(fRestRun->GetCurrentEntry());
373 fEventIdNumberBox->SetIntNumber(ev->GetID());
374 fEventSubIdNumberBox->SetIntNumber(ev->GetSubID());
378 RESTError <<
"file: " << filename <<
" does not exist!" << RESTendl;
383 void TRestBrowser::NextPlotOptionAction() {
384 string text = fPlotOptionTextBox->GetText();
391 fPlotOptionTextBox->SetText(text.c_str());
395 void TRestBrowser::PreviousPlotOptionAction() {
396 string text = fPlotOptionTextBox->GetText();
403 fPlotOptionTextBox->SetText(text.c_str());
407 void TRestBrowser::PlotAction() {
408 if (fEventViewer !=
nullptr) {
409 fEventViewer->Plot(fPlotOptionTextBox->GetText());
413 void TRestBrowser::RowValueChangedAction(Long_t val) {
414 int eventRow = fEventRow;
415 fEventRow = (Int_t)fEventRowNumberBox->GetNumber();
417 RESTDebug <<
"TRestBrowser::LoadEventAction. Entry:" << fEventRow << RESTendl;
419 bool success = LoadEventEntry(fEventRow);
422 fEventRow = eventRow;
423 fEventRowNumberBox->SetIntNumber(fEventRow);
427 void TRestBrowser::EventTypeChangedAction(Int_t
id) {
428 string eventType = fEventTypeComboBox->GetSelectedEntry()->GetTitle();
431 if (eve !=
nullptr) {
432 fRestRun->SetInputEvent(eve);
433 RowValueChangedAction(0);
437 void TRestBrowser::IdValueChangedAction(Long_t val) {
438 int eventID = fEventId;
439 int subEventID = fEventSubId;
441 fEventId = (Int_t)fEventIdNumberBox->GetNumber();
442 fEventSubId = (Int_t)fEventSubIdNumberBox->GetNumber();
444 RESTDebug <<
"TRestBrowser::LoadEventAction. Event ID: " << fEventId <<
", Sub ID: " << fEventSubId
447 bool success = LoadEventId(fEventId, fEventSubId);
451 fEventSubId = subEventID;
452 fEventIdNumberBox->SetIntNumber(fEventId);
453 fEventSubIdNumberBox->SetIntNumber(fEventSubId);
457 void TRestBrowser::NextEventAction() {
458 string sel = (string)fSelectionTextBox->GetText();
461 LoadEventEntry(fEventRow);
464 TTreeFormula formula(
"Selection", sel.c_str(), tree);
465 if (formula.GetNdim() > 0) {
468 tree->GetEntry(fEventRow);
469 if (formula.EvalInstance(fEventRow) == 1) {
470 LoadEventEntry(fEventRow);
473 cout << fEventRow << endl;
476 if (fEventRow >= fRestRun->GetAnalysisTree()->GetEntries()) {
477 LoadEventEntry(fEventRow);
482 cout <<
"invalid selection!" << endl;
487 void TRestBrowser::PreviousEventAction() {
488 string sel = (string)fSelectionTextBox->GetText();
491 LoadEventEntry(fEventRow);
494 TTreeFormula formula(
"Selection", sel.c_str(), tree);
495 if (formula.GetNdim() > 0) {
498 tree->GetEntry(fEventRow);
499 if (formula.EvalInstance(fEventRow) == 1) {
500 LoadEventEntry(fEventRow);
503 cout << fEventRow << endl;
507 LoadEventEntry(fEventRow);
512 cout <<
"invalid selection!" << endl;
517 void TRestBrowser::LoadFileAction() {
519 new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDOpen, &fi);
521 TString dir = fi.fFilename;
523 cout <<
"Opening " << dir.Data() << endl;
528 void TRestBrowser::ExitAction() { gSystem->Exit(0); }
REST core data-saving helper based on TTree.
void SetBottomPanelButtons()
A base class for any REST event.
Data provider and manager in REST.
TRestReflector Assembly(const std::string &typeName)
Assembly an object of type: typeName, returning the allocated memory address and size.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
Int_t Count(std::string s, std::string sbstring)
Counts the number of occurences of substring inside the input string in.
Int_t isANumber(std::string in)
Returns 1 only if a valid number is found in the string in. If not it returns 0.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.