Belle II Software  release-05-02-19
VisualRepMap.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <display/VisualRepMap.h>
12 
13 #include <display/EveTower.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/datastore/RelationVector.h>
16 #include <framework/datastore/DataStore.h>
17 
18 #include <TEveSelection.h>
19 #include <TEveCaloData.h>
20 #include <TEveManager.h>
21 
22 #include <boost/bimap/bimap.hpp>
23 #include <boost/bimap/unordered_multiset_of.hpp>
24 
25 using namespace Belle2;
26 
27 namespace {
29  typedef boost::bimaps::bimap <
30  boost::bimaps::unordered_multiset_of<const TObject*>,
31  boost::bimaps::unordered_multiset_of<TEveElement*>
32  > DataStoreEveElementMap_Base;
33 }
34 
36 class VisualRepMap::DataStoreEveElementMap : public DataStoreEveElementMap_Base { };
37 
39 {
40  static VisualRepMap instance;
41  return &instance;
42 }
43 
44 VisualRepMap::VisualRepMap() : m_currentlySelecting(false), m_dataStoreEveElementMap(new DataStoreEveElementMap) { }
45 
47 
49 {
50  m_dataStoreEveElementMap->clear();
51 
52  for (EveTower* t : m_eclTowers)
53  delete t;
54  m_eclTowers.clear();
55 }
56 
57 
58 const TObject* VisualRepMap::getDataStoreObject(TEveElement* elem) const
59 {
60  //special handling for TEveCaloData
61  if (TEveCaloData* caloData = dynamic_cast<TEveCaloData*>(elem)) {
62  const auto& selectedCells = caloData->GetCellsSelected();
63  if (selectedCells.empty())
64  return nullptr;
65  int id = selectedCells[0].fTower;
66  for (EveTower* eveTower : m_eclTowers) {
67  if (eveTower->getID() == id) {
68  elem = eveTower;
69  break;
70  }
71  }
72  }
73 
74  const auto& it = m_dataStoreEveElementMap->right.find(elem);
75  if (it != m_dataStoreEveElementMap->right.end())
76  return it->second;
77  return nullptr;
78 }
79 
80 TEveElement* VisualRepMap::getEveElement(const TObject* obj) const
81 {
82  const auto& it = m_dataStoreEveElementMap->left.find(obj);
83  if (it != m_dataStoreEveElementMap->left.end())
84  return it->second;
85  return nullptr;
86 }
87 
88 
89 void VisualRepMap::select(const TObject* object) const
90 {
92  B2FATAL("recursive select() call detected. Please check isCurrentlySelecting().");
93  m_currentlySelecting = true;
94  auto range = m_dataStoreEveElementMap->left.equal_range(object);
95  for (auto it = range.first; it != range.second; ++it) {
96  TEveElement* elem = it->second;
97  if (elem and !gEve->GetSelection()->HasChild(elem)) {
98  //select this object in addition to existing selection
99  gEve->GetSelection()->UserPickedElement(elem, true);
100  }
101  }
102  m_currentlySelecting = false;
103 }
104 
105 void VisualRepMap::selectOnly(TEveElement* eveObj) const
106 {
107  //copy current selection, then deselect each element
108  const std::list<TEveElement*> sel(gEve->GetSelection()->BeginChildren(), gEve->GetSelection()->EndChildren());
109  for (TEveElement* el : sel) {
110  if (el == eveObj or el->IsA() == EveTower::Class())
111  continue;
112  gEve->GetSelection()->UserUnPickedElement(el);
113  }
114 }
115 
116 void VisualRepMap::selectRelated(TEveElement* eveObj) const
117 {
118  const TObject* representedObject = getDataStoreObject(eveObj);
119  if (representedObject) {
120  //representedObject->Dump();
121 
122  const RelationVector<TObject>& relatedObjects = DataStore::getRelationsWithObj<TObject>(representedObject, "ALL");
123  for (const TObject& relObj : relatedObjects) {
124  select(&relObj);
125  }
126  }
127 }
128 
130 {
131  if (!gEve)
132  return;
133  //equivalent to clicking into empty space
134  gEve->GetSelection()->UserPickedElement(nullptr);
135 }
136 void VisualRepMap::add(const TObject* dataStoreObject, TEveElement* visualRepresentation)
137 {
138  if (!dataStoreObject) {
139  B2ERROR("Trying to insert NULL for object represented by " << visualRepresentation->GetElementName());
140  return;
141  }
142  auto ret = m_dataStoreEveElementMap->insert(DataStoreEveElementMap::value_type(dataStoreObject, visualRepresentation));
143  if (!ret.second) {
144  B2DEBUG(100, "Failed to insert object represented by " << visualRepresentation->GetElementName() << "! Duplicate?");
145  }
146 }
147 
148 void VisualRepMap::addCluster(const TObject* dataStoreObject, TEveCaloData* caloData, int towerID)
149 {
150  EveTower* tower = new EveTower(caloData, towerID);
151  tower->IncDenyDestroy(); //otherwise the selection wants to own this.
152  m_eclTowers.push_back(tower);
153 
154  add(dataStoreObject, tower);
155 }
Belle2::VisualRepMap::selectOnly
void selectOnly(TEveElement *eveObj) const
Deselect all other objects.
Definition: VisualRepMap.cc:105
Belle2::VisualRepMap::VisualRepMap
VisualRepMap()
default constructor
Definition: VisualRepMap.cc:44
Belle2::VisualRepMap::clear
void clear()
Remove all contents in map.
Definition: VisualRepMap.cc:48
Belle2::VisualRepMap::m_currentlySelecting
bool m_currentlySelecting
are we currently in a select() call?
Definition: VisualRepMap.h:90
Belle2::VisualRepMap::~VisualRepMap
~VisualRepMap()
destructor
Definition: VisualRepMap.cc:46
Belle2::VisualRepMap::add
void add(const TObject *dataStoreObject, TEveElement *visualRepresentation)
Generic function to keep track of which objects have which visual representation.
Definition: VisualRepMap.cc:136
Belle2::VisualRepMap::selectRelated
void selectRelated(TEveElement *eveObj) const
Select related objects.
Definition: VisualRepMap.cc:116
Belle2::VisualRepMap::getDataStoreObject
const TObject * getDataStoreObject(TEveElement *elem) const
Get object represented by given visual representation.
Definition: VisualRepMap.cc:58
Belle2::VisualRepMap::getEveElement
TEveElement * getEveElement(const TObject *obj) const
Get (first) visual representation of given object.
Definition: VisualRepMap.cc:80
Belle2::VisualRepMap
defines a bidirectional (many to many) mapping between TObjects in DataStore and their visual represe...
Definition: VisualRepMap.h:30
Belle2::VisualRepMap::select
void select(const TObject *object) const
Select the representation of the given object.
Definition: VisualRepMap.cc:89
Belle2::VisualRepMap::addCluster
void addCluster(const TObject *dataStoreObject, TEveCaloData *caloData, int towerID)
Selection inside TEveCalo* is complicated, use this to keep track of ECL clusters.
Definition: VisualRepMap.cc:148
Belle2::VisualRepMap::getInstance
static VisualRepMap * getInstance()
get instance pointer.
Definition: VisualRepMap.cc:38
Belle2::RelationVector
Class for type safe access to objects that are referred to in relations.
Definition: DataStore.h:38
Belle2::VisualRepMap::m_eclTowers
std::vector< EveTower * > m_eclTowers
individual ECL towers (getting them out of TEve is hard).
Definition: VisualRepMap.h:98
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::VisualRepMap::m_dataStoreEveElementMap
DataStoreEveElementMap * m_dataStoreEveElementMap
Map DataStore contents in current event with their visual representation.
Definition: VisualRepMap.h:95
Belle2::VisualRepMap::clearSelection
void clearSelection() const
Clear existing selection in Eve Browser.
Definition: VisualRepMap.cc:129
Belle2::VisualRepMap::DataStoreEveElementMap
Hide implementation in private class.
Definition: VisualRepMap.cc:36
Belle2::EveTower
handles selection of individual ECLClusters (getting them out of TEve is hard).
Definition: EveTower.h:13