File indexing completed on 2025-01-18 10:17:37
0001
0002
0003
0004
0005
0006 #include "JStringification.h"
0007
0008
0009
0010
0011
0012
0013
0014 void JStringification::GetObjectSummariesAsJSON(std::vector<std::string> &json_vec, std::shared_ptr<const JEvent> &jevent, const std::string &object_name, const std::string factory_tag) const{
0015
0016
0017 std::map<std::string, JObjectSummary> objects;
0018 GetObjectSummaries(objects, jevent, object_name, factory_tag);
0019
0020
0021 for(auto p : objects){
0022 const std::string &hexaddr = p.first;
0023 JObjectSummary &summary = p.second;
0024 auto str = ObjectToJSON(hexaddr, summary);
0025 json_vec.push_back(str);
0026 }
0027 }
0028
0029
0030
0031
0032
0033
0034
0035 void JStringification::GetObjectSummaries(std::map<std::string, JObjectSummary> &objects, std::shared_ptr<const JEvent> &jevent, const std::string &object_name, const std::string factory_tag) const {
0036
0037
0038
0039
0040 if(jevent.get() == nullptr ) return;
0041 auto fac = jevent->GetFactory(object_name, factory_tag);
0042 if( fac ){
0043 for( auto jobj : fac->GetAs<JObject>()){
0044 JObjectSummary summary;
0045 jobj->Summarize(summary);
0046 std::stringstream ss;
0047 ss << "0x" << std::hex << (uint64_t)jobj << std::dec;
0048 objects[ss.str()] = summary;
0049 }
0050 #if JANA2_HAVE_ROOT
0051
0052
0053
0054 for( auto tobj : fac->GetAs<TObject>()){
0055 JObjectSummary summary;
0056 auto tclass = TClass::GetClass(tobj->ClassName());
0057 if(tclass){
0058 auto *members = tclass->GetListOfAllPublicDataMembers();
0059 for( auto item : *members){
0060 TDataMember *memitem = dynamic_cast<TDataMember*>(item);
0061 if( memitem == nullptr ) continue;
0062 if( memitem->Property() & kIsStatic ) continue;
0063 JObjectMember jObjectMember;
0064 jObjectMember.name = memitem->GetName();
0065 jObjectMember.type = memitem->GetTypeName();
0066 jObjectMember.value = GetRootObjectMemberAsString(tobj, memitem, jObjectMember.type);
0067 summary.add(jObjectMember);
0068 }
0069 }else {
0070 LOG << "Unable to get TClass for: " << object_name << LOG_END;
0071 }
0072 std::stringstream ss;
0073 ss << "0x" << std::hex << (uint64_t)tobj << std::dec;
0074 objects[ss.str()] = summary;
0075 }
0076 #endif
0077 }else{
0078 LOG <<"No factory found! object_name=" << object_name << LOG_END;
0079 }
0080 }
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 std::string JStringification::ObjectToJSON( const std::string &hexaddr, const JObjectSummary &summary ) const {
0091
0092 std::stringstream ss;
0093 ss << "{\n";
0094 ss << "\"hexaddr\":\"" << hexaddr << "\"\n";
0095 for( auto m : summary.get_fields() ){
0096 ss << ",\"" << m.name << "\":\"" << m.value << "\"\n";
0097 }
0098 ss << "}";
0099
0100 return ss.str();
0101 }
0102
0103
0104
0105
0106 #if JANA2_HAVE_ROOT
0107
0108
0109
0110 std::string JStringification::GetRootObjectMemberAsString(const TObject *tobj, const TDataMember *memitem, std::string type) const {
0111 void *addr = (void*)(((uint64_t)tobj) + memitem->GetOffset());
0112
0113
0114 std::string tmp;
0115 if( (memitem->Property()&kIsArray) && (type=="char") ){
0116 tmp = (char*)addr;
0117 addr = (void*)&tmp;
0118 type = "string";
0119 }
0120
0121 if( type == "int" ) return GetAddrAsString<int>(addr);
0122 else if( type == "double" ) return GetAddrAsString<double>(addr);
0123 else if( type == "float" ) return GetAddrAsString<float>(addr);
0124 else if( type == "char" ) return GetAddrAsString<char>(addr);
0125 else if( type == "unsigned char" ) return GetAddrAsString<unsigned char>(addr);
0126 else if( type == "unsigned int" ) return GetAddrAsString<unsigned int>(addr);
0127 else if( type == "long" ) return GetAddrAsString<long>(addr);
0128 else if( type == "unsigned long" ) return GetAddrAsString<unsigned long>(addr);
0129 else if( type == "ULong64_t" ) return GetAddrAsString<ULong64_t>(addr);
0130 else if( type == "Long64_t" ) return GetAddrAsString<Long64_t>(addr);
0131 else if( type == "string" ) return GetAddrAsString<std::string>(addr);
0132 return "unknown";
0133 }
0134 #endif