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