Belle II Software  release-05-02-19
ObjectInfo.cc
1 #include <display/ObjectInfo.h>
2 
3 #include <framework/logging/Logger.h>
4 #include <framework/datastore/RelationsObject.h>
5 #include <framework/utilities/HTML.h>
6 
7 #include <genfit/GFRaveVertex.h>
8 
9 #include <TString.h>
10 #include <TMath.h>
11 #include <TClass.h>
12 #include <TMethod.h>
13 #include <TInterpreter.h>
14 
15 using namespace Belle2;
16 
17 namespace {
18  std::string callStringMethod(const TObject* obj, const char* name)
19  {
20  std::string str; //empty string to hold contents. If you allocate memory here this will leak.
21  TClass* cl = obj->IsA();
22  if (!cl) {
23  B2ERROR("No class?");
24  return str;
25  }
26  TMethod* m = cl->GetMethod(name, "", true);
27  if (m) {
28  if (m->GetReturnTypeName() != std::string("string"))
29  B2WARNING(cl->GetName() << "::" << name << " has return type " << m->GetReturnTypeName() <<
30  " instead of std::string, cannot show info.");
31  else
32  gInterpreter->ExecuteWithArgsAndReturn(m, (void*)obj, 0, 0, &str);
33  }
34  return str;
35  }
36 }
37 
38 TString ObjectInfo::getName(const TObject* obj)
39 {
40  if (!obj)
41  B2ERROR("ObjectInfo::getName() got null?");
42  if (const RelationsObject* relObj = dynamic_cast<const RelationsObject*>(obj)) {
43  return relObj->getName();
44  }
45  return callStringMethod(obj, "getName");
46 }
47 
48 TString ObjectInfo::getInfo(const TObject* obj)
49 {
50  if (!obj)
51  B2ERROR("ObjectInfo::getInfo() got null?");
52  if (auto relObj = dynamic_cast<const RelationsObject*>(obj)) {
53  return relObj->getInfoHTML();
54  } else if (auto vertex = dynamic_cast<const genfit::GFRaveVertex*>(obj)) {
55  return "<b>V</b>=" + HTML::getString(vertex->getPos()) + "<br>" +
56  TString::Format("pVal=%e", TMath::Prob(vertex->getChi2(), vertex->getNdf()));
57  }
58  return callStringMethod(obj, "getInfoHTML");
59 }
60 
61 TString ObjectInfo::getTitle(const TObject* obj)
62 {
63  TString out(getIdentifier(obj));
64  const TString& name = getName(obj);
65  if (name.Length() != 0)
66  out += " - " + name;
67 
68  return out + "\n" + HTML::htmlToPlainText(getInfo(obj).Data()).c_str();
69 }
70 
71 std::pair<std::string, int> ObjectInfo::getDataStorePosition(const TObject* obj)
72 {
73  std::string name;
74  int index = -1;
75  if (const RelationsObject* relObj = dynamic_cast<const RelationsObject*>(obj)) {
76  name = relObj->getArrayName();
77  index = relObj->getArrayIndex();
78  } else {
79  //somewhat manual way to find location of object (might not inherit from RelationInterface)
80  DataStore::StoreEntry* entry = nullptr;
81  DataStore::Instance().findStoreEntry(obj, entry, index);
82  if (entry)
83  name = entry->name;
84  }
85  if (index == -1) {
86  //this thing might be in a StoreObjPtr...
87  for (const auto pair : DataStore::Instance().getStoreEntryMap(DataStore::c_Event)) {
88  if (pair.second.object == obj) {
89  name = pair.second.name;
90  break;
91  }
92  }
93  }
94 
95  return std::make_pair(name, index);
96 }
97 
98 TString ObjectInfo::getIdentifier(const TObject* obj)
99 {
100  auto pos = getDataStorePosition(obj);
101  if (pos.second != -1)
102  return TString::Format("%s[%d]", pos.first.c_str(), pos.second);
103  return pos.first;
104 }
105 
Belle2::ObjectInfo::getName
TString getName(const TObject *obj)
human-readable name (e.g.
Definition: ObjectInfo.cc:38
Belle2::HTML::htmlToPlainText
std::string htmlToPlainText(const std::string &html)
Reformat given HTML string into terminal-friendly plain text.
Definition: HTML.cc:132
genfit::GFRaveVertex
GFRaveVertex class.
Definition: GFRaveVertex.h:48
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::StoreEntry
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:15
Belle2::StoreEntry::name
std::string name
Name of the entry.
Definition: StoreEntry.h:46
Belle2::ObjectInfo::getTitle
TString getTitle(const TObject *obj)
Get plain text for TEve object titles (shown on mouse-over).
Definition: ObjectInfo.cc:61
Belle2::ObjectInfo::getInfo
TString getInfo(const TObject *obj)
Get object info HTML (e.g.
Definition: ObjectInfo.cc:48
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::HTML::getString
std::string getString(const TMatrixFBase &matrix, int precision=2, bool color=true)
get HTML table representing a matrix.
Definition: HTML.cc:18
Belle2::ObjectInfo::getIdentifier
TString getIdentifier(const TObject *obj)
Where is this object in the datastore?
Definition: ObjectInfo.cc:98
Belle2::DataStore::findStoreEntry
bool findStoreEntry(const TObject *object, StoreEntry *&entry, int &index)
Find an object in an array in the data store.
Definition: DataStore.cc:398
Belle2::ObjectInfo::getDataStorePosition
std::pair< std::string, int > getDataStorePosition(const TObject *obj)
return entry name & index for arrays, with index = -1 for objects.
Definition: ObjectInfo.cc:71
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::RelationsInterface
Defines interface for accessing relations of objects in StoreArray.
Definition: RelationsObject.h:102