REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestBrowser.cxx
1
25
26#include "TRestBrowser.h"
27
28#include <TTreeFormula.h>
29
30using namespace std;
31
32TRestBrowser::TRestBrowser() {
33 if ((TDirectory*)gDirectory != nullptr && gDirectory->GetFile() != nullptr) {
34 Initialize();
35 SetViewer("TRestEventViewer");
36 OpenFile(gDirectory->GetFile()->GetName());
37 cout << "Loaded File : " << fInputFileName << endl;
38 } else {
39 fBrowser = new TBrowser("Browser", nullptr, "REST Browser");
40 fRestRun = new TRestRun();
41 }
42}
43
44TRestBrowser::TRestBrowser(const TString& viewerName, Double_t geomScale) {
45 Initialize("I");
46 SetViewer(viewerName, geomScale);
47}
48
49TRestBrowser::~TRestBrowser() {
50 if (frmMain != nullptr) frmMain->Cleanup();
51 // delete frmMain;
52}
53
54void TRestBrowser::Initialize(const TString& opt) {
55 pureAnalysis = kFALSE;
56
57 fRestRun = new TRestRun();
58
59 fBrowser = new TBrowser("Browser", 0, "REST Browser", opt);
60 TGMainFrame* fr = fBrowser->GetBrowserImp()->GetMainFrame();
61 if (fr == nullptr) {
62 RESTWarning << "No x11 interface is available. Cannot call the browser window!" << RESTendl;
63 exit(1);
64 }
65 fr->DontCallClose();
66
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();
73
74 fBrowser->StartEmbedding(1, -1);
75 fCanDefault = new TCanvas();
76 fBrowser->StopEmbedding();
77
78 fBrowser->StartEmbedding(2, -1);
79 frmBot = new TGMainFrame(gClient->GetRoot(), 300);
80 frmBot->SetCleanup(kDeepCleanup);
81 frmBot->SetWindowName("Controller2");
83 fBrowser->StopEmbedding();
84
86 // frmMain->MapSubwindows();
88 // frmMain->Layout();
89 // frmMain->MapWindow();
90}
91
92void TRestBrowser::SetViewer(TRestEventViewer* eV, Double_t geomScale) {
93 if (fEventViewer != nullptr) {
94 cout << "Event viewer has already been set!" << endl;
95 return;
96 }
97 if (eV != nullptr) {
98 fEventViewer = eV;
99 fEventViewer->SetGeomScale(geomScale);
100 // b->StartEmbedding(1, -1);
101 eV->Embed(fBrowser);
102 // b->StopEmbedding();
103 }
104}
105
106void TRestBrowser::SetViewer(const TString& viewerName, Double_t geomScale) {
107 if (Count((string)viewerName, "Viewer") > 0) {
108 TRestEventViewer* viewer = REST_Reflection::Assembly((string)viewerName);
109 viewer->SetGeomScale(geomScale);
110 if (viewer != nullptr) {
111 SetViewer(viewer, geomScale);
112 } else {
113 RESTError << viewerName << " not recognized! Did you install the corresponding library?"
114 << RESTendl;
115 RESTError << "Also check EVE feature is turned on in REST for 3d event viewing." << RESTendl;
116 RESTWarning << "Using default event viewer" << RESTendl;
117 }
118 } else {
119 cout << "illegal viewer : " << viewerName << endl;
120 exit(0);
121 }
122}
123
124void TRestBrowser::SetLeftPanelButtons() {
125 fVFrame = new TGVerticalFrame(frmMain);
126 fVFrame->Resize(300, 200);
127
128 // row in the tree
129 fEventRowLabel = new TGLabel(fVFrame, "Entry:");
130 fVFrame->AddFrame(fEventRowLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
131
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));
135
136 // event id and sub event id
137 auto labelBar = new TGHorizontalFrame(fVFrame);
138 {
139 fEventIdLabel = new TGLabel(labelBar, "Event ID:");
140 labelBar->AddFrame(fEventIdLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
141
142 fEventSubIdLabel = new TGLabel(labelBar, "Sub ID:");
143 labelBar->AddFrame(fEventSubIdLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
144 }
145 fVFrame->AddFrame(labelBar, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
146
147 auto numberBoxBar = new TGHorizontalFrame(fVFrame);
148 {
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));
152
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));
157 }
158 fVFrame->AddFrame(numberBoxBar, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
159
160 // event type to choose
161 fEventTypeLabel = new TGLabel(fVFrame, "Event Type:");
162 fVFrame->AddFrame(fEventTypeLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
163
164 fEventTypeComboBox = new TGComboBox(fVFrame);
165 fEventTypeComboBox->Connect("Selected(Int_t)", "TRestBrowser", this, "EventTypeChangedAction(Int_t)");
166 fVFrame->AddFrame(fEventTypeComboBox, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
167
168 // plot option buttons
169 fPlotOptionLabel = new TGLabel(fVFrame, "Plot Options:");
170 fVFrame->AddFrame(fPlotOptionLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
171
172 fPlotOptionTextBox = new TGTextEntry(fVFrame, "");
173 fPlotOptionTextBox->SetText("");
174 fPlotOptionTextBox->Connect("ReturnPressed()", "TRestBrowser", this, "PlotAction()");
175 fVFrame->AddFrame(fPlotOptionTextBox, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
176
177 auto switchButtonBar = new TGHorizontalFrame(fVFrame);
178 {
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));
182
183 fButOptRefresh = new TGTextButton(switchButtonBar, "Plot");
184 fButOptRefresh->Connect("Clicked()", "TRestBrowser", this, "PlotAction()");
185 switchButtonBar->AddFrame(fButOptRefresh, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
186
187 fButOptNext = new TGPictureButton(switchButtonBar, gClient->GetPicture("bld_redo.png"));
188
189 fButOptNext->Connect("Clicked()", "TRestBrowser", this, "NextPlotOptionAction()");
190 switchButtonBar->AddFrame(fButOptNext, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
191 }
192 fVFrame->AddFrame(switchButtonBar, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
193
194 // tool buttons
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));
198
199 fExit = new TGPictureButton(fVFrame, gClient->GetPicture("bld_exit.png"));
200
201 fExit->Connect("Clicked()", "TRestBrowser", this, "ExitAction()");
202 fVFrame->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
203
204 frmMain->Resize(TGDimension(300, frmMain->GetHeight() + fVFrame->GetHeight()));
205
206 frmMain->AddFrame(fVFrame, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
207 // frmMain->DontCallClose();
208 frmMain->MapSubwindows();
209 // frmMain->Resize();
210 frmMain->Layout();
211 frmMain->MapWindow();
212}
213
215 fHFrame = new TGVerticalFrame(frmBot);
216 fHFrame->Resize(300, 100);
217
218 fSelectionTextBoxLabel = new TGLabel(fHFrame, "Selection:");
219 fHFrame->AddFrame(fSelectionTextBoxLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
220
221 fSelectionTextBox = new TGTextEntry(fHFrame, "");
222 fHFrame->AddFrame(fSelectionTextBox, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
223
224 auto bottomBar = new TGHorizontalFrame(fHFrame);
225 {
226 fButEveNext = new TGTextButton(bottomBar, "Previous Event");
227 fButEveNext->Connect("Clicked()", "TRestBrowser", this, "PreviousEventAction()");
228 bottomBar->AddFrame(fButEveNext, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
229
230 fButEvePrev = new TGTextButton(bottomBar, "Next Event");
231 fButEvePrev->Connect("Clicked()", "TRestBrowser", this, "NextEventAction()");
232 bottomBar->AddFrame(fButEvePrev, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
233 }
234 fHFrame->AddFrame(bottomBar, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
235
236 frmBot->AddFrame(fHFrame, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
237 frmBot->MapSubwindows();
238 frmBot->Layout();
239 frmBot->MapWindow();
240}
241
242void TRestBrowser::InitFromConfigFile() { cout << __PRETTY_FUNCTION__ << endl; }
243
244void TRestBrowser::SetInputEvent(TRestEvent* event) {
245 if (fRestRun != nullptr) {
246 fRestRun->SetInputEvent(event);
247 }
248}
249
250Bool_t TRestBrowser::LoadEventEntry(Int_t n) {
251 if (fRestRun->GetInputFile() == nullptr || fRestRun->GetInputFile()->IsZombie()) {
252 RESTWarning << "TRestBrowser::LoadEventEntry. No File..." << RESTendl;
253 return kFALSE;
254 }
255 if (pureAnalysis) {
256 RESTWarning << "TRestBrowser::LoadEventEntry. This is a pure analysis file..." << RESTendl;
257 return kFALSE;
258 }
259
260 if (fRestRun->GetAnalysisTree() != nullptr && n < fRestRun->GetAnalysisTree()->GetEntries() && n >= 0) {
261 fRestRun->GetEntry(n);
262 TRestEvent* ev = fRestRun->GetInputEvent();
263 if (!ev) {
264 RESTError << "internal error!" << RESTendl;
265 return kFALSE;
266 } else {
267 fEventRow = fRestRun->GetCurrentEntry();
268 fEventId = ev->GetID();
269 fEventSubId = ev->GetSubID();
270
271 fEventRowNumberBox->SetIntNumber(fEventRow);
272 fEventIdNumberBox->SetIntNumber(fEventId);
273 fEventSubIdNumberBox->SetIntNumber(fEventSubId);
274 fRestRun->GetAnalysisTree()->PrintObservables();
275 }
276 } else {
277 RESTWarning << "TRestBrowser::LoadEventEntry. Event out of limits" << RESTendl;
278 return kFALSE;
279 }
280
281 if (fEventViewer != nullptr) {
282 fEventViewer->AddEvent(fRestRun->GetInputEvent());
283 fEventViewer->Plot(fPlotOptionTextBox->GetText());
284 cout << endl;
285 }
286
287 fCanDefault->cd();
288 return kTRUE;
289}
290
291Bool_t TRestBrowser::LoadEventId(Int_t eventID, Int_t subEventID) {
292 if (fRestRun->GetInputFile() == nullptr || fRestRun->GetInputFile()->IsZombie()) {
293 RESTWarning << "TRestBrowser::LoadEventEntry. No File..." << RESTendl;
294 return kFALSE;
295 }
296 if (pureAnalysis) {
297 cout << "" << endl;
298 RESTWarning << "TRestBrowser::LoadEventEntry. This is a pure analysis file..." << RESTendl;
299 return kFALSE;
300 }
301
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!"
306 << RESTendl;
307 return kFALSE;
308 } else {
309 fEventRow = fRestRun->GetCurrentEntry();
310 fEventId = event->GetID();
311 fEventSubId = event->GetSubID();
312
313 fEventRowNumberBox->SetIntNumber(fEventRow);
314 fEventIdNumberBox->SetIntNumber(fEventId);
315 fEventSubIdNumberBox->SetIntNumber(fEventSubId);
316 fRestRun->GetAnalysisTree()->PrintObservables();
317 }
318 } else {
319 RESTWarning << "TRestBrowser::LoadEventEntry. Event out of limits" << RESTendl;
320 return kFALSE;
321 }
322
323 if (fEventViewer != nullptr) {
324 fEventViewer->AddEvent(fRestRun->GetInputEvent());
325 fEventViewer->Plot(fPlotOptionTextBox->GetText());
326 cout << endl;
327 }
328
329 fCanDefault->cd();
330 return kTRUE;
331}
332
333Bool_t TRestBrowser::OpenFile(const TString& filename) {
334 if (filename.Contains("http") || TRestTools::fileExists(filename.Data())) {
335 fInputFileName = filename;
336
337 fRestRun->OpenInputFile(fInputFileName);
338 fRestRun->GetInputFile()->cd();
339 TTree* t = fRestRun->GetEventTree();
340
341 TGeoManager* geometry = gGeoManager;
342
343 if (t != nullptr) {
344 // add entry for other event types
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());
351 // we make the entry of input event being selected
352 if (fRestRun->GetInputEvent() != nullptr &&
353 (string)fRestRun->GetInputEvent()->ClassName() == eventType) {
354 fEventTypeComboBox->Select(fEventTypeComboBox->GetNumberOfEntries() - 1, false);
355 }
356 }
357 }
358
359 // init viewer
360 pureAnalysis = kFALSE;
361 if (fEventViewer == nullptr) SetViewer("TRestEventViewer");
362 if (geometry != nullptr && fEventViewer != nullptr) fEventViewer->SetGeometry(geometry);
363 RowValueChangedAction(0);
364 } else {
365 pureAnalysis = kTRUE;
366 }
367
368 TRestEvent* ev = fRestRun->GetInputEvent();
369 if (!ev) {
370 RESTError << "internal error!" << RESTendl;
371 } else {
372 fEventRowNumberBox->SetIntNumber(fRestRun->GetCurrentEntry());
373 fEventIdNumberBox->SetIntNumber(ev->GetID());
374 fEventSubIdNumberBox->SetIntNumber(ev->GetSubID());
375 }
376 return true;
377 } else {
378 RESTError << "file: " << filename << " does not exist!" << RESTendl;
379 }
380 return false;
381}
382
383void TRestBrowser::NextPlotOptionAction() {
384 string text = fPlotOptionTextBox->GetText();
385 if (text.empty()) {
386 text = "0";
387 } else if (isANumber(text)) {
388 text = ToString(StringToInteger(text) + 1);
389 }
390
391 fPlotOptionTextBox->SetText(text.c_str());
392 PlotAction();
393}
394
395void TRestBrowser::PreviousPlotOptionAction() {
396 string text = fPlotOptionTextBox->GetText();
397 if (text.empty()) {
398 text = "0";
399 } else if (isANumber(text)) {
400 text = ToString(StringToInteger(text) - 1);
401 }
402
403 fPlotOptionTextBox->SetText(text.c_str());
404 PlotAction();
405}
406
407void TRestBrowser::PlotAction() {
408 if (fEventViewer != nullptr) {
409 fEventViewer->Plot(fPlotOptionTextBox->GetText());
410 }
411}
412
413void TRestBrowser::RowValueChangedAction(Long_t val) {
414 int eventRow = fEventRow;
415 fEventRow = (Int_t)fEventRowNumberBox->GetNumber();
416
417 RESTDebug << "TRestBrowser::LoadEventAction. Entry:" << fEventRow << RESTendl;
418
419 bool success = LoadEventEntry(fEventRow);
420
421 if (!success) {
422 fEventRow = eventRow;
423 fEventRowNumberBox->SetIntNumber(fEventRow);
424 }
425}
426
427void TRestBrowser::EventTypeChangedAction(Int_t id) {
428 string eventType = fEventTypeComboBox->GetSelectedEntry()->GetTitle();
429 TRestEvent* eve = REST_Reflection::Assembly(eventType);
430
431 if (eve != nullptr) {
432 fRestRun->SetInputEvent(eve);
433 RowValueChangedAction(0);
434 }
435}
436
437void TRestBrowser::IdValueChangedAction(Long_t val) {
438 int eventID = fEventId;
439 int subEventID = fEventSubId;
440
441 fEventId = (Int_t)fEventIdNumberBox->GetNumber();
442 fEventSubId = (Int_t)fEventSubIdNumberBox->GetNumber();
443
444 RESTDebug << "TRestBrowser::LoadEventAction. Event ID: " << fEventId << ", Sub ID: " << fEventSubId
445 << RESTendl;
446
447 bool success = LoadEventId(fEventId, fEventSubId);
448
449 if (!success) {
450 fEventId = eventID;
451 fEventSubId = subEventID;
452 fEventIdNumberBox->SetIntNumber(fEventId);
453 fEventSubIdNumberBox->SetIntNumber(fEventSubId);
454 }
455}
456
457void TRestBrowser::NextEventAction() {
458 string sel = (string)fSelectionTextBox->GetText();
459 if (sel.empty()) {
460 fEventRow++;
461 LoadEventEntry(fEventRow);
462 } else {
463 TRestAnalysisTree* tree = fRestRun->GetAnalysisTree();
464 TTreeFormula formula("Selection", sel.c_str(), tree);
465 if (formula.GetNdim() > 0) { // valid expression
466 fEventRow++;
467 while (true) {
468 tree->GetEntry(fEventRow);
469 if (formula.EvalInstance(fEventRow) == 1) {
470 LoadEventEntry(fEventRow);
471 break;
472 } else {
473 cout << fEventRow << endl;
474 fEventRow++;
475 }
476 if (fEventRow >= fRestRun->GetAnalysisTree()->GetEntries()) {
477 LoadEventEntry(fEventRow);
478 break;
479 }
480 }
481 } else {
482 cout << "invalid selection!" << endl;
483 }
484 }
485}
486
487void TRestBrowser::PreviousEventAction() {
488 string sel = (string)fSelectionTextBox->GetText();
489 if (sel.empty()) {
490 fEventRow--;
491 LoadEventEntry(fEventRow);
492 } else {
493 TRestAnalysisTree* tree = fRestRun->GetAnalysisTree();
494 TTreeFormula formula("Selection", sel.c_str(), tree);
495 if (formula.GetNdim() > 0) { // valid expression
496 fEventRow--;
497 while (true) {
498 tree->GetEntry(fEventRow);
499 if (formula.EvalInstance(fEventRow) == 1) {
500 LoadEventEntry(fEventRow);
501 break;
502 } else {
503 cout << fEventRow << endl;
504 fEventRow--;
505 }
506 if (fEventRow < 0) {
507 LoadEventEntry(fEventRow);
508 break;
509 }
510 }
511 } else {
512 cout << "invalid selection!" << endl;
513 }
514 }
515}
516
517void TRestBrowser::LoadFileAction() {
518 TGFileInfo fi;
519 new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDOpen, &fi);
520
521 TString dir = fi.fFilename;
522
523 cout << "Opening " << dir.Data() << endl;
524
525 OpenFile(dir);
526}
527
528void TRestBrowser::ExitAction() { gSystem->Exit(0); }
REST core data-saving helper based on TTree.
TGNumberEntry * fEventSubIdNumberBox
For Event number.
Definition: TRestBrowser.h:36
TGPictureButton * fExit
Open file button.
Definition: TRestBrowser.h:48
TGTextButton * fButEveNext
Previous plot option.
Definition: TRestBrowser.h:55
TGTextButton * fButOptRefresh
Previous plot option.
Definition: TRestBrowser.h:44
TGNumberEntry * fEventIdNumberBox
For row number.
Definition: TRestBrowser.h:35
TGVerticalFrame * fHFrame
Exit button.
Definition: TRestBrowser.h:51
TGLabel * fEventTypeLabel
For sub Event number.
Definition: TRestBrowser.h:38
TGPictureButton * fButOptNext
Refresh plot.
Definition: TRestBrowser.h:45
TGLabel * fEventRowLabel
< The main vertical frame for browser controlling
Definition: TRestBrowser.h:31
void SetBottomPanelButtons()
TGTextButton * fButEvePrev
TextBox for plot options.
Definition: TRestBrowser.h:54
TGPictureButton * fMenuOpen
Next plot option.
Definition: TRestBrowser.h:47
TCanvas * fCanDefault
Refresh plot.
Definition: TRestBrowser.h:57
TGPictureButton * fButOptPrev
TextBox for plot options.
Definition: TRestBrowser.h:43
A base class for any REST event.
Definition: TRestEvent.h:38
Data provider and manager in REST.
Definition: TRestRun.h:18
void OpenInputFile(int i)
Open the i th file in the file list.
Definition: TRestRun.cxx:314
TRestEvent * GetInputEvent() const
Calling GetInputEvent() will return a basic TRestEvent*
Definition: TRestRun.h:151
void SetInputEvent(TRestEvent *event)
Retarget input event in the tree.
Definition: TRestRun.cxx:1245
void GetEntry(Long64_t entry)
Calls GetEntry() for both AnalysisTree and EventTree.
Definition: TRestRun.cxx:908
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
Definition: TRestTools.cxx:728
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.