Skip to content

Commit 8b04734

Browse files
pnwkwdavidrohr
authored andcommitted
Make the view camera positions and visibility settings persistent in case of eve crash
1 parent 9f0ad33 commit 8b04734

2 files changed

Lines changed: 131 additions & 4 deletions

File tree

EventVisualisation/View/include/EventVisualisationView/EventManager.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class EventManager final : public TEveEventManager, public TQObject
8181
void setShowDate(bool value) { this->mShowDate = value; }
8282

8383
private:
84-
struct Settings {
84+
struct VizSettings {
8585
bool firstEvent;
8686
Bool_t trackVisibility[EVisualisationGroup::NvisualisationGroups];
8787
Color_t trackColor[EVisualisationGroup::NvisualisationGroups];
@@ -94,13 +94,15 @@ class EventManager final : public TEveEventManager, public TQObject
9494
Size_t clusterSize[EVisualisationGroup::NvisualisationGroups];
9595
};
9696

97+
static constexpr auto TEMP_SETTINGS_PATH = ".o2eve_temp_settings.json";
98+
9799
static EventManager* instance;
98100
o2::ccdb::CcdbApi ccdbApi;
99101
TEveElementList* dataTypeLists[EVisualisationDataType::NdataTypes]; // 3D
100102
TEveElementList* dataTypeListsPhi[EVisualisationDataType::NdataTypes]; // Phi
101103
DataSource* dataSource = nullptr;
102104
TString dataPath = "";
103-
Settings vizSettings;
105+
VizSettings vizSettings;
104106

105107
/// Default constructor
106108
EventManager();

EventVisualisation/View/src/EventManager.cxx

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@
3030
#include <TEveElement.h>
3131
#include <TGListTree.h>
3232
#include <TEveCalo.h>
33-
#include "FairLogger.h"
33+
#include <FairLogger.h>
34+
#include <rapidjson/document.h>
35+
#include <rapidjson/stringbuffer.h>
36+
#include <rapidjson/writer.h>
37+
#include <gsl/span>
3438

3539
#define elemof(e) (unsigned int)(sizeof(e) / sizeof(e[0]))
3640

3741
using namespace std;
42+
using namespace rapidjson;
3843

3944
namespace o2
4045
{
@@ -98,11 +103,17 @@ void EventManager::displayCurrentEvent()
98103
multiView->registerElements(dataTypeLists, dataTypeListsPhi);
99104

100105
if (vizSettings.firstEvent) {
101-
saveVisualisationSettings();
106+
ifstream s(TEMP_SETTINGS_PATH);
107+
if (s.good()) {
108+
restoreVisualisationSettings();
109+
} else {
110+
saveVisualisationSettings();
111+
}
102112
vizSettings.firstEvent = false;
103113
} else {
104114
restoreVisualisationSettings();
105115
}
116+
106117
if (this->mShowDate) {
107118
multiView->getAnnotationTop()->SetText(
108119
TString::Format("Run %d\n%s", dataSource->getRunNumber(), dataSource->getCollisionTime().c_str()));
@@ -365,10 +376,122 @@ void EventManager::saveVisualisationSettings()
365376
vizSettings.clusterSize[i] = clusterSet->GetMarkerSize();
366377
}
367378
}
379+
380+
ofstream settings(TEMP_SETTINGS_PATH);
381+
382+
if (settings.good()) {
383+
Document d;
384+
d.SetObject();
385+
auto& allocator = d.GetAllocator();
386+
387+
auto jsonArray = [](const auto& array, auto& allocator) {
388+
Value arr(kArrayType);
389+
390+
for (const auto& value : array) {
391+
arr.PushBack(value, allocator);
392+
}
393+
394+
return arr;
395+
};
396+
397+
d.AddMember("trackVisibility", jsonArray(vizSettings.trackVisibility, allocator), allocator);
398+
d.AddMember("trackColor", jsonArray(vizSettings.trackColor, allocator), allocator);
399+
d.AddMember("trackStyle", jsonArray(vizSettings.trackStyle, allocator), allocator);
400+
d.AddMember("trackWidth", jsonArray(vizSettings.trackWidth, allocator), allocator);
401+
d.AddMember("clusterVisibility", jsonArray(vizSettings.clusterVisibility, allocator), allocator);
402+
d.AddMember("clusterColor", jsonArray(vizSettings.clusterColor, allocator), allocator);
403+
d.AddMember("clusterStyle", jsonArray(vizSettings.clusterStyle, allocator), allocator);
404+
d.AddMember("clusterSize", jsonArray(vizSettings.clusterSize, allocator), allocator);
405+
406+
auto jsonCamera = [&jsonArray](MultiView::EViews view, auto& allocator) {
407+
Value obj(kObjectType);
408+
409+
auto& camera = MultiView::getInstance()->getView(view)->GetGLViewer()->CurrentCamera();
410+
411+
const gsl::span baseSpan(camera.RefCamBase().CArr(), 16);
412+
obj.AddMember("base", jsonArray(baseSpan, allocator), allocator);
413+
414+
const gsl::span transSpan(camera.GetCamTrans().CArr(), 16);
415+
obj.AddMember("trans", jsonArray(transSpan, allocator), allocator);
416+
417+
if (camera.IsOrthographic()) {
418+
obj.AddMember("zoom", dynamic_cast<TGLOrthoCamera&>(camera).GetZoom(), allocator);
419+
} else if (camera.IsPerspective()) {
420+
obj.AddMember("fov", dynamic_cast<TGLPerspectiveCamera&>(camera).GetFOV(), allocator);
421+
}
422+
423+
return obj;
424+
};
425+
426+
d.AddMember("camera3d", jsonCamera(MultiView::View3d, allocator), allocator);
427+
d.AddMember("cameraRphi", jsonCamera(MultiView::ViewRphi, allocator), allocator);
428+
d.AddMember("cameraZY", jsonCamera(MultiView::ViewZY, allocator), allocator);
429+
430+
StringBuffer strbuf;
431+
Writer<StringBuffer> writer(strbuf);
432+
d.Accept(writer);
433+
434+
settings << strbuf.GetString();
435+
}
368436
}
369437

370438
void EventManager::restoreVisualisationSettings()
371439
{
440+
ifstream settings(TEMP_SETTINGS_PATH);
441+
442+
if (settings.good()) {
443+
string json((istreambuf_iterator<char>(settings)), istreambuf_iterator<char>());
444+
Document d;
445+
d.Parse(json.c_str());
446+
447+
auto updateArray = [](auto& array, const auto& document, const char* name, const auto& accessor) {
448+
for (size_t i = 0; i < elemof(array); ++i) {
449+
array[i] = accessor(document[name][i]);
450+
}
451+
};
452+
453+
auto getBool = [](const GenericValue<UTF8<char>>& v) { return v.GetBool(); };
454+
auto getUint = [](const GenericValue<UTF8<char>>& v) { return v.GetUint(); };
455+
auto getFloat = [](const GenericValue<UTF8<char>>& v) { return v.GetFloat(); };
456+
457+
updateArray(vizSettings.trackVisibility, d, "trackVisibility", getBool);
458+
updateArray(vizSettings.trackColor, d, "trackColor", getUint);
459+
updateArray(vizSettings.trackStyle, d, "trackStyle", getUint);
460+
updateArray(vizSettings.trackWidth, d, "trackWidth", getUint);
461+
updateArray(vizSettings.clusterVisibility, d, "clusterVisibility", getBool);
462+
updateArray(vizSettings.clusterColor, d, "clusterColor", getUint);
463+
updateArray(vizSettings.clusterStyle, d, "clusterStyle", getUint);
464+
updateArray(vizSettings.clusterSize, d, "clusterSize", getFloat);
465+
466+
auto updateCamera = [getFloat](MultiView::EViews view, const auto& document, const char* name) {
467+
auto& camera = MultiView::getInstance()->getView(view)->GetGLViewer()->CurrentCamera();
468+
469+
std::array<Double_t, 16> values;
470+
471+
for (size_t i = 0; i < values.size(); ++i) {
472+
values[i] = getFloat(document[name]["base"][i]);
473+
}
474+
camera.RefCamBase() = TGLMatrix(values.data());
475+
476+
for (size_t i = 0; i < values.size(); ++i) {
477+
values[i] = getFloat(document[name]["trans"][i]);
478+
}
479+
camera.RefCamTrans() = TGLMatrix(values.data());
480+
481+
if (camera.IsOrthographic()) {
482+
dynamic_cast<TGLOrthoCamera&>(camera).SetZoom(getFloat(document[name]["zoom"]));
483+
} else if (camera.IsPerspective()) {
484+
dynamic_cast<TGLPerspectiveCamera&>(camera).SetFOV(getFloat(document[name]["fov"]));
485+
}
486+
487+
camera.IncTimeStamp();
488+
};
489+
490+
updateCamera(MultiView::View3d, d, "camera3d");
491+
updateCamera(MultiView::ViewRphi, d, "cameraRphi");
492+
updateCamera(MultiView::ViewZY, d, "cameraZY");
493+
}
494+
372495
const auto& tracks = *dataTypeLists[EVisualisationDataType::Tracks];
373496

374497
for (auto elm : tracks.RefChildren()) {
@@ -398,6 +521,8 @@ void EventManager::restoreVisualisationSettings()
398521
clusterSet->SetMarkerSize(vizSettings.clusterSize[i]);
399522
}
400523
}
524+
525+
MultiView::getInstance()->redraw3D();
401526
}
402527

403528
} // namespace event_visualisation

0 commit comments

Comments
 (0)