Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-03 08:57:27

0001 
0002 #include "JInspector.h"
0003 #include <JANA/JEventSource.h>
0004 #include <stack>
0005 #include <JANA/Utils/JTablePrinter.h>
0006 
0007 
0008 JInspector::JInspector(const JEvent* event) : m_event(event) {}
0009 
0010 void JInspector::Reset() {
0011     m_indexes_built = false;
0012     m_factories.clear();
0013     m_factory_index.clear();
0014     m_discrepancies.clear();
0015 }
0016 
0017 void JInspector::SetDiscrepancies(std::set<std::string>&& diverging_factory_keys) {
0018     m_discrepancies = std::move(diverging_factory_keys);
0019 }
0020 
0021 void JInspector::BuildIndices() {
0022     if (m_indexes_built) return;
0023     for (auto fac: m_event->GetFactorySet()->GetAllFactories()) {
0024         m_factories.push_back(fac);
0025     }
0026     std::sort(m_factories.begin(), m_factories.end(), [](const JFactory* first, const JFactory* second){
0027         return std::make_pair(first->GetObjectName(), first->GetTag()) <
0028                std::make_pair(second->GetObjectName(), second->GetTag());});
0029 
0030     int i = 0;
0031     for (auto fac : m_factories) {
0032         std::string key = MakeFactoryKey(fac->GetObjectName(), fac->GetTag());
0033         std::pair<int, const JFactory*> value = {i++, fac};
0034         m_factory_index.insert({key, value});
0035         m_factory_index.insert({std::to_string(i), value});
0036     }
0037     m_indexes_built = true;
0038 }
0039 
0040 std::vector<const JObject*> JInspector::FindAllAncestors(const JObject* root) {
0041     std::vector<const JObject*> all_ancestors;
0042     std::stack<const JObject*> to_visit;
0043     to_visit.push(root);
0044     while (!to_visit.empty()) {
0045         auto current_obj = to_visit.top();
0046         to_visit.pop();
0047         all_ancestors.push_back(current_obj);
0048         std::vector<const JObject*> current_ancestors;
0049         current_obj->GetT<JObject>(current_ancestors);
0050         for (auto obj : current_ancestors) {
0051             to_visit.push(obj);
0052         }
0053     }
0054     return all_ancestors;
0055 }
0056 
0057 std::tuple<JFactory*, size_t, size_t> JInspector::LocateObject(const JEvent& event, const JObject* obj) {
0058     auto objName = obj->className();
0059     size_t fac_idx = 0;
0060     for (auto fac : event.GetAllFactories()) {
0061         if (fac->GetObjectName() == objName) {
0062             size_t obj_idx = 0;
0063             for (auto o : fac->GetAs<JObject>()) { // Won't trigger factory creation if it hasn't already happened
0064                 if (obj == o) {
0065                     return std::make_tuple(fac, fac_idx, obj_idx);
0066                 }
0067                 obj_idx++;
0068             }
0069         }
0070         fac_idx++;
0071     }
0072     return std::make_tuple(nullptr, -1, -1);
0073 }
0074 
0075 void JInspector::PrintEvent() {
0076     ToText(m_event, m_format==Format::Json, m_out);
0077 }
0078 void JInspector::PrintFactories(int filter_level=0) {
0079     BuildIndices();
0080     ToText(m_factories, m_discrepancies, filter_level, m_format==Format::Json, m_out);
0081 }
0082 
0083 void JInspector::PrintObjects(std::string factory_key) {
0084     BuildIndices();
0085     auto result = m_factory_index.find(factory_key);
0086     if (result == m_factory_index.end()) {
0087         m_out << "(Error: Invalid factory name or index)\n";
0088         return;
0089     }
0090     auto fac = const_cast<JFactory*>(result->second.second);
0091     auto objs = fac->GetAs<JObject>();
0092     ToText(objs, m_format==Format::Json, m_out);
0093 }
0094 
0095 void JInspector::PrintFactoryDetails(std::string fac_key) {
0096     BuildIndices();
0097     auto result = m_factory_index.find(fac_key);
0098     if (result == m_factory_index.end()) {
0099         m_out << "(Error: Invalid factory name or index)\n";
0100         return;
0101     }
0102     auto fac = result->second.second;
0103     ToText(fac, m_format==Format::Json, m_out);
0104 }
0105 
0106 void JInspector::PrintObject(std::string fac_key, int object_idx) {
0107     BuildIndices();
0108     auto result = m_factory_index.find(fac_key);
0109     if (result == m_factory_index.end()) {
0110         m_out << "(Error: Invalid factory name or index)\n";
0111         return;
0112     }
0113     JFactory* fac = const_cast<JFactory*>(result->second.second);
0114     auto objs = fac->GetAs<JObject>();
0115     if ((size_t) object_idx >= objs.size()) {
0116         m_out << "(Error: Object index out of range)" << std::endl;
0117         return;
0118     }
0119     auto obj = objs[object_idx];
0120     ToText(obj, m_format==Format::Json, m_out);
0121 }
0122 std::string JInspector::MakeFactoryKey(std::string name, std::string tag) {
0123     std::ostringstream ss;
0124     ss << name;
0125     if (!tag.empty()) {
0126         ss << ":" << tag;
0127     }
0128     return ss.str();
0129 }
0130 
0131 void JInspector::PrintHelp() {
0132     m_out << "  -----------------------------------------" << std::endl;
0133     m_out << "  Available commands" << std::endl;
0134     m_out << "  -----------------------------------------" << std::endl;
0135     m_out << "  pe   PrintEvent" << std::endl;
0136     m_out << "  pf   PrintFactories [filter_level <- {0,1,2,3}]" << std::endl;
0137     m_out << "  pfd  PrintFactoryDetails fac_idx" << std::endl;
0138     m_out << "  po   PrintObjects fac_idx" << std::endl;
0139     m_out << "  po   PrintObject fac_idx obj_idx" << std::endl;
0140     m_out << "  pfp  PrintFactoryParents fac_idx" << std::endl;
0141     m_out << "  pop  PrintObjectParents fac_idx obj_idx" << std::endl;
0142     m_out << "  poa  PrintObjectAncestors fac_idx obj_idx" << std::endl;
0143     m_out << "  vt   ViewAsTable" << std::endl;
0144     m_out << "  vj   ViewAsJson" << std::endl;
0145     m_out << "  x    Exit" << std::endl;
0146     m_out << "  h    Help" << std::endl;
0147     m_out << "  -----------------------------------------" << std::endl;
0148 }
0149 void JInspector::ToText(const JEvent* event, bool asJson, std::ostream& out) {
0150     auto className = event->GetJEventSource()->GetTypeName();
0151     if (className.empty()) className = "(unknown)";
0152     if (asJson) {
0153         out << "{ \"run_number\": " << event->GetRunNumber();
0154         out << ", \"event_number\": " << event->GetEventNumber();
0155         out << ", \"source\": \"" << event->GetJEventSource()->GetResourceName();
0156         out << "\", \"class_name\": \"" << className;
0157         out << "\", \"is_sequential\": \"" << event->GetSequential() << "\"}";
0158         out << std::endl;
0159     }
0160     else {
0161         out << "Run number:   " << event->GetRunNumber() << std::endl;
0162         out << "Event number: " << event->GetEventNumber() << std::endl;
0163         out << "Event source: " << event->GetJEventSource()->GetResourceName() << std::endl;
0164         out << "Class name:   " << className << std::endl;
0165         out << "Sequential:   " << event->GetSequential() << std::endl;
0166     }
0167 }
0168 
0169 void JInspector::ToText(const JFactory* fac, bool asJson, std::ostream& out) {
0170 
0171     auto pluginName = fac->GetPluginName();
0172     if (pluginName.empty()) pluginName = "(no plugin)";
0173 
0174     auto factoryName = fac->GetFactoryName();
0175     // if (factoryName.empty()) factoryName = "(no name)";
0176 
0177     auto tag = fac->GetTag();
0178     if (tag.empty()) tag = "(no tag)";
0179 
0180     std::string creationStatus;
0181     switch (fac->GetCreationStatus()) {
0182         case JFactory::CreationStatus::NotCreatedYet: creationStatus = "NotCreatedYet"; break;
0183         case JFactory::CreationStatus::Created: creationStatus = "Created"; break;
0184         case JFactory::CreationStatus::Inserted: creationStatus = "Inserted"; break;
0185         case JFactory::CreationStatus::InsertedViaGetObjects: creationStatus = "InsertedViaGetObjects"; break;
0186         case JFactory::CreationStatus::NeverCreated: creationStatus = "NeverCreated"; break;
0187         default: creationStatus = "Unknown";
0188     }
0189 
0190     if (!asJson) {
0191         out << "Plugin name          " << pluginName << std::endl;
0192         // out << "Factory name         " << factoryName << std::endl;
0193         out << "Object name          " << fac->GetObjectName() << std::endl;
0194         out << "Tag                  " << tag << std::endl;
0195         out << "Creation status      " << creationStatus << std::endl;
0196         out << "Object count         " << fac->GetNumObjects() << std::endl;
0197     }
0198     else {
0199         out << "{" << std::endl;
0200         out << "  \"plugin_name\":   \"" << pluginName << "\"," << std::endl;
0201         out << "  \"factory_name\":  \"" << factoryName << "\"," << std::endl;
0202         out << "  \"object_name\":   \"" << fac->GetObjectName() << "\"," << std::endl;
0203         out << "  \"tag\":           \"" << tag << "\"," << std::endl;
0204         out << "  \"creation\":      \"" << creationStatus << "\"," << std::endl;
0205         out << "  \"object_count\":  " << fac->GetNumObjects() << "," << std::endl;
0206         out << "}" << std::endl;
0207     }
0208 }
0209 
0210 void JInspector::ToText(const std::vector<JFactory*>& factories, const std::set<std::string>& discrepancies, int filterlevel, bool asJson, std::ostream &out) {
0211     size_t idx = -1;
0212     if (!asJson) {
0213         JTablePrinter t;
0214         t.AddColumn("Index", JTablePrinter::Justify::Right);
0215         t.AddColumn("Object");
0216         t.AddColumn("Tag");
0217         t.AddColumn("Creation status");
0218         t.AddColumn("Object count", JTablePrinter::Justify::Right);
0219         t.AddColumn("Discrepancy", JTablePrinter::Justify::Right);
0220         for (auto fac : factories) {
0221             auto tag = fac->GetTag();
0222             if (tag.empty()) tag = "(no tag)";
0223             std::string creationStatus;
0224             switch (fac->GetCreationStatus()) {
0225                 case JFactory::CreationStatus::NotCreatedYet: creationStatus = "NotCreatedYet"; break;
0226                 case JFactory::CreationStatus::Created: creationStatus = "Created"; break;
0227                 case JFactory::CreationStatus::Inserted: creationStatus = "Inserted"; break;
0228                 case JFactory::CreationStatus::InsertedViaGetObjects: creationStatus = "InsertedViaGetObjects"; break;
0229                 case JFactory::CreationStatus::NeverCreated: creationStatus = "NeverCreated"; break;
0230                 default: creationStatus = "Unknown";
0231             }
0232             idx += 1;
0233             if (filterlevel > 0 && (fac->GetCreationStatus()==JFactory::CreationStatus::NeverCreated ||
0234                            fac->GetCreationStatus()==JFactory::CreationStatus::NotCreatedYet)) continue;
0235             if (filterlevel > 1 && (fac->GetNumObjects()== 0)) continue;
0236             if (filterlevel > 2 && (fac->GetCreationStatus()==JFactory::CreationStatus::Inserted ||
0237                                     fac->GetCreationStatus()==JFactory::CreationStatus::InsertedViaGetObjects)) continue;
0238 
0239             char discrepancy = ' ';
0240             auto key = MakeFactoryKey(fac->GetObjectName(), fac->GetTag());
0241             if (discrepancies.find(key) != discrepancies.end()) discrepancy = 'x';
0242 
0243             t | idx | fac->GetObjectName() | tag | creationStatus | fac->GetNumObjects() | discrepancy;
0244         }
0245         t.Render(out);
0246     }
0247     else {
0248         out << "{" << std::endl;
0249         for (auto fac : factories) {
0250             auto facName = fac->GetFactoryName();
0251             if (facName.empty()) facName = "null";
0252             auto tag = fac->GetTag();
0253             if (tag.empty()) tag = "null";
0254             out << "  " << idx++ << ": { \"factory_name\": ";
0255             if (fac->GetFactoryName().empty()) {
0256                 out << "null, ";
0257             }
0258             else {
0259                 out << "\"" << fac->GetFactoryName() << "\", ";
0260             }
0261             out << "\"object_name\": \"" << fac->GetObjectName() << "\", \"tag\": ";
0262             if (fac->GetTag().empty()) {
0263                 out << "null, ";
0264             }
0265             else {
0266                 out << "\"" << fac->GetTag() << "\", ";
0267             }
0268             out << "\"object_count\": " << fac->GetNumObjects();
0269             out << "}," << std::endl;
0270         }
0271         out << "}" << std::endl;
0272     }
0273 }
0274 
0275 void JInspector::ToText(std::vector<JObject*> objs, bool as_json, std::ostream& out) {
0276 
0277     if (objs.empty()) {
0278         out << "(No objects found)" << std::endl;
0279         return;
0280     }
0281     std::set<std::string> objnames;
0282     for (auto obj: objs) {
0283         objnames.insert(obj->className());
0284     }
0285     if (objnames.size() == 1) {
0286         out << *(objnames.begin()) << std::endl;
0287     }
0288     else {
0289         out << "{ ";
0290         for (auto name : objnames) {
0291             out << name << ",";
0292         }
0293         out << "}" << std::endl;
0294     }
0295     if (as_json) {
0296         out << "{" << std::endl;
0297         for (size_t i = 0; i < objs.size(); ++i) {
0298             auto obj = objs[i];
0299             JObjectSummary summary;
0300             obj->Summarize(summary);
0301             out << "  " << i << ":  {";
0302             for (auto& field : summary.get_fields()) {
0303                 out << "\"" << field.name << "\": \"" << field.value << "\", ";
0304             }
0305             out << "}" << std::endl;
0306         }
0307         out << "}" << std::endl;
0308     }
0309     else {
0310         JTablePrinter t;
0311         std::set<std::string> fieldnames_seen;
0312         std::vector<std::string> fieldnames_in_order;
0313         for (auto obj : objs) {
0314             JObjectSummary summary;
0315             obj->Summarize(summary);
0316             for (auto field : summary.get_fields()) {
0317                 if (fieldnames_seen.find(field.name) == fieldnames_seen.end()) {
0318                     fieldnames_in_order.push_back(field.name);
0319                     fieldnames_seen.insert(field.name);
0320                 }
0321             }
0322         }
0323         t.AddColumn("Index");
0324         for (auto fieldname : fieldnames_in_order) {
0325             t.AddColumn(fieldname);
0326         }
0327         for (size_t i = 0; i < objs.size(); ++i) {
0328             auto obj = objs[i];
0329             t | i;
0330             std::map<std::string, std::string> summary_map;
0331             JObjectSummary summary;
0332             obj->Summarize(summary);
0333             for (auto& field : summary.get_fields()) {
0334                 summary_map[field.name] = field.value;
0335             }
0336             for (auto& fieldname : fieldnames_in_order) {
0337                 auto result = summary_map.find(fieldname);
0338                 if (result == summary_map.end()) {
0339                     t | "(missing)";
0340                 }
0341                 else {
0342                     t | result->second;
0343                 }
0344             }
0345         }
0346         t.Render(out);
0347     }
0348 }
0349 
0350 void JInspector::ToText(const JObject* obj, bool asJson, std::ostream& out) {
0351     out << obj->className() << std::endl;
0352     JObjectSummary summary;
0353     obj->Summarize(summary);
0354     if (asJson) {
0355         out << "[" << std::endl;
0356         for (auto& field : summary.get_fields()) {
0357             out << "  { \"name\": \"" << field.name << "\", ";
0358             out << "\"value\": \"" << field.value << "\", ";
0359             out << "\"description\": \"" << field.description << "\" }" << std::endl;
0360         }
0361         out << "]" << std::endl;
0362     }
0363     else {
0364         JTablePrinter t;
0365         t.AddColumn("Field name");
0366         t.AddColumn("Value");
0367         t.AddColumn("Description");
0368         for (auto& field : summary.get_fields()) {
0369             t | field.name | field.value | field.description;
0370         }
0371         t.Render(out);
0372     }
0373 }
0374 
0375 void JInspector::PrintFactoryParents(std::string factory_idx) {
0376 
0377     bool callgraph_on = m_event->GetJCallGraphRecorder()->IsEnabled();
0378     if (!callgraph_on) {
0379         m_out << "(Error: Callgraph recording is currently disabled)" << std::endl;
0380     }
0381 
0382     BuildIndices();  // So that we can retrieve the integer index given the factory name/tag pair
0383     auto result = m_factory_index.find(factory_idx);
0384     if (result == m_factory_index.end()) {
0385         m_out << "(Error: Invalid factory name or index)\n";
0386         return;
0387     }
0388     auto fac = result->second.second;
0389     auto obj_name = fac->GetObjectName();
0390     auto fac_tag = fac->GetTag();
0391     if (fac_tag.empty()) {
0392         m_out << obj_name << std::endl;
0393     }
0394     else {
0395         m_out << obj_name << ":" << fac_tag << std::endl;
0396     }
0397 
0398     if (m_format != Format::Json) {
0399         JTablePrinter t;
0400         t.AddColumn("Index", JTablePrinter::Justify::Right);
0401         t.AddColumn("Object name");
0402         t.AddColumn("Tag");
0403         auto callgraph = m_event->GetJCallGraphRecorder()->GetCallGraph();
0404         bool found_anything = false;
0405         for (const auto& node : callgraph) {
0406             if ((node.caller_name == obj_name) && (node.caller_tag == fac_tag)) {
0407                 found_anything = true;
0408                 auto idx = m_factory_index[MakeFactoryKey(node.callee_name, node.callee_tag)].first;
0409                 auto tag = node.callee_tag;
0410                 if (tag.empty()) tag = "(no tag)";
0411                 t | idx | node.callee_name | tag;
0412             }
0413         }
0414         if (!found_anything) {
0415             m_out << "(No parents found)" << std::endl;
0416             return;
0417         }
0418         t.Render(m_out);
0419     }
0420     else {
0421         auto callgraph = m_event->GetJCallGraphRecorder()->GetCallGraph();
0422         bool found_anything = false;
0423         m_out << "[" << std::endl;
0424         for (const auto& node : callgraph) {
0425             if ((node.caller_name == obj_name) && (node.caller_tag == fac_tag)) {
0426                 found_anything = true;
0427                 auto idx = m_factory_index[MakeFactoryKey(node.callee_name, node.callee_tag)].first;
0428                 auto tag = node.callee_tag;
0429                 m_out << "  { \"index\": " << idx << ", \"object_name\": \"" << node.callee_name << "\", \"tag\": ";
0430                 if (tag.empty()) {
0431                     m_out << "null }," << std::endl;
0432                 }
0433                 else {
0434                     m_out << "\"" << tag << "\" }," << std::endl;
0435                 }
0436             }
0437         }
0438         m_out << "]" << std::endl;
0439         if (!found_anything) {
0440             m_out << "(No ancestors found)" << std::endl;
0441             return;
0442         }
0443     }
0444 }
0445 
0446 void JInspector::PrintObjectParents(std::string factory_key, int object_idx) {
0447 
0448     BuildIndices();  // So that we can retrieve the integer index given the factory name/tag pair
0449     auto result = m_factory_index.find(factory_key);
0450     if (result == m_factory_index.end()) {
0451         m_out << "(Error: Invalid factory name or index)\n";
0452         return;
0453     }
0454     auto fac = const_cast<JFactory*>(result->second.second);
0455     auto objs = fac->GetAs<JObject>();
0456     if ((size_t) object_idx >= objs.size()) {
0457         m_out << "(Error: Object index out of range)" << std::endl;
0458         return;
0459     }
0460     auto obj = objs[object_idx];
0461     m_out << obj->className() << std::endl;
0462     std::vector<const JObject*> parents;
0463     obj->GetT<JObject>(parents);
0464     if (parents.empty()) {
0465         m_out << "(No parents found)" << std::endl;
0466         return;
0467     }
0468 
0469     if (m_format == Format::Table) {
0470         JTablePrinter t;
0471         t.AddColumn("Object name");
0472         t.AddColumn("Tag");
0473         t.AddColumn("Factory Index", JTablePrinter::Justify::Right);
0474         t.AddColumn("Object Index", JTablePrinter::Justify::Right);
0475         t.AddColumn("Object contents");
0476         for (auto parent : parents) {
0477             JFactory* fac;
0478             size_t fac_idx;
0479             size_t obj_idx;
0480             std::tie(fac, fac_idx, obj_idx) = LocateObject(*m_event, parent);
0481             if (fac == nullptr) {
0482                 m_out << "(Error: Unable to find factory containing object with classname '" << obj->className() << "')" << std::endl;
0483                 continue;
0484             }
0485             JObjectSummary summary;
0486             parent->Summarize(summary);
0487 
0488             auto tag = fac->GetTag();
0489             if (tag.empty()) tag = "(no tag)";
0490             std::ostringstream objval;
0491             objval << "{";
0492             for (auto& field : summary.get_fields()) {
0493                 objval << field.name << ": " << field.value << ", ";
0494             }
0495             objval << "}";
0496 
0497             t | fac->GetObjectName() | tag | fac_idx | obj_idx | objval.str();
0498         }
0499         t.Render(m_out);
0500     }
0501     else {
0502         m_out << "[" << std::endl;
0503         for (auto ancestor : FindAllAncestors(obj)) {
0504             JFactory* fac;
0505             size_t fac_idx;
0506             size_t obj_idx;
0507             std::tie(fac, fac_idx, obj_idx) = LocateObject(*m_event, ancestor);
0508             if (fac == nullptr) {
0509                 m_out << "(Error: Unable to find factory containing object with classname '" << obj->className() << "')" << std::endl;
0510                 continue;
0511             }
0512             auto tag = fac->GetTag();
0513             if (tag.empty()) tag = "(no tag)";
0514 
0515             m_out << "  " << "{ " << std::endl << "    \"object_name\": \"" << fac->GetObjectName() << "\", ";
0516             if (tag.empty()) {
0517                 m_out << "\"tag\": null, ";
0518             }
0519             else {
0520                 m_out << "\"tag\": \"" << tag << "\", ";
0521             }
0522             m_out << "\"fac_index\": " << fac_idx << ", \"obj_index\": " << obj_idx << "," << std::endl;
0523             m_out << "    \"object_contents\": ";
0524 
0525             JObjectSummary summary;
0526             ancestor->Summarize(summary);
0527             m_out << "{";
0528             for (auto& field : summary.get_fields()) {
0529                 m_out << "\"" << field.name << "\": \"" << field.value << "\", ";
0530             }
0531             m_out << "}" << std::endl;
0532             m_out << "  }, " << std::endl;
0533         }
0534         m_out << "]" << std::endl;
0535     }
0536 }
0537 
0538 void JInspector::PrintObjectAncestors(std::string factory_idx, int object_idx) {
0539 
0540     BuildIndices();  // So that we can retrieve the integer index given the factory name/tag pair
0541     auto result = m_factory_index.find(factory_idx);
0542     if (result == m_factory_index.end()) {
0543         m_out << "(Error: Invalid factory name or index)\n";
0544         return;
0545     }
0546     auto fac = const_cast<JFactory*>(result->second.second);
0547     auto objs = fac->GetAs<JObject>();
0548     if ((size_t) object_idx >= objs.size()) {
0549         m_out << "(Error: Object index out of range)" << std::endl;
0550         return;
0551     }
0552     auto obj = objs[object_idx];
0553     m_out << obj->className() << std::endl;
0554     auto ancestors = FindAllAncestors(obj);
0555     if (ancestors.empty()) {
0556          m_out << "(No ancestors found)" << std::endl;
0557          return;
0558     }
0559 
0560     if (m_format == Format::Table) {
0561         JTablePrinter t;
0562         t.AddColumn("Object name");
0563         t.AddColumn("Tag");
0564         t.AddColumn("Factory index", JTablePrinter::Justify::Right);
0565         t.AddColumn("Object index", JTablePrinter::Justify::Right);
0566         t.AddColumn("Object contents");
0567         for (auto ancestor : FindAllAncestors(obj)) {
0568             JFactory* fac;
0569             size_t fac_idx, obj_idx;
0570             std::tie(fac, fac_idx, obj_idx) = LocateObject(*m_event, ancestor);
0571             if (fac == nullptr) {
0572                 m_out << "(Error: Unable to find factory containing object with classname '" << obj->className() << "')" << std::endl;
0573                 continue;
0574             }
0575             JObjectSummary summary;
0576             ancestor->Summarize(summary);
0577 
0578             auto tag = fac->GetTag();
0579             if (tag.empty()) tag = "(no tag)";
0580             std::ostringstream objval;
0581             objval << "{";
0582             for (auto& field : summary.get_fields()) {
0583                 objval << field.name << ": " << field.value << ", ";
0584             }
0585             objval << "}";
0586 
0587             t | fac->GetObjectName() | tag | fac_idx | obj_idx | objval.str();
0588         }
0589         t.Render(m_out);
0590     }
0591     else {
0592         m_out << "[" << std::endl;
0593         for (auto ancestor : FindAllAncestors(obj)) {
0594             JFactory* fac;
0595             size_t fac_idx, obj_idx;
0596             std::tie(fac, fac_idx, obj_idx) = LocateObject(*m_event, ancestor);
0597             if (fac == nullptr) {
0598                 m_out << "(Error: Unable to find factory containing object with classname '" << obj->className() << "')" << std::endl;
0599                 continue;
0600             }
0601             auto tag = fac->GetTag();
0602             if (tag.empty()) tag = "(no tag)";
0603 
0604             m_out << "  " << "{ " << std::endl << "    \"object_name\": \"" << fac->GetObjectName() << "\", ";
0605             if (tag.empty()) {
0606                 m_out << "\"tag\": null, ";
0607             }
0608             else {
0609                 m_out << "\"tag\": \"" << tag << "\", ";
0610             }
0611             m_out << "\"fac_index\": " << fac_idx << ", \"obj_index\": " << obj_idx << "," << std::endl;
0612             m_out << "    \"object_contents\": ";
0613 
0614             JObjectSummary summary;
0615             ancestor->Summarize(summary);
0616             m_out << "{";
0617             for (auto& field : summary.get_fields()) {
0618                 m_out << "\"" << field.name << "\": \"" << field.value << "\", ";
0619             }
0620             m_out << "}" << std::endl;
0621             m_out << "  }, " << std::endl;
0622         }
0623         m_out << "]" << std::endl;
0624     }
0625 }
0626 
0627 void JInspector::Loop() {
0628     bool stay_in_loop = true;
0629     auto app = m_event->GetJApplication();
0630     m_enable_timeout_on_exit = app->IsTimeoutEnabled();
0631     m_enable_ticker_on_exit = app->IsTickerEnabled();
0632     m_event->GetJApplication()->SetTicker( false );
0633     m_event->GetJApplication()->SetTimeoutEnabled( false );
0634     m_out << std::endl;
0635     m_out << "--------------------------------------------------------------------------------------" << std::endl;
0636     m_out << "Welcome to JANA's interactive inspector! Type `Help` or `h` to see available commands." << std::endl;
0637     m_out << "--------------------------------------------------------------------------------------" << std::endl;
0638     PrintEvent();
0639     while (stay_in_loop) {
0640         std::string user_input;
0641         m_out << std::endl << "JANA: "; m_out.flush();
0642         // Obtain a single line
0643         std::getline(m_in, user_input);
0644         // Split into tokens
0645         std::stringstream ss(user_input);
0646         std::string token;
0647         ss >> token;
0648         std::vector<std::string> args;
0649         std::string arg;
0650         while (ss >> arg) {
0651             args.push_back(arg);
0652         }
0653         try {
0654             if (token == "PrintEvent" || token == "pe") {
0655                 PrintEvent();
0656             }
0657             else if ((token == "PrintFactories" || token == "pf") && args.empty()) {
0658                 PrintFactories(0);
0659             }
0660             else if ((token == "PrintFactories" || token == "pf") && args.size() == 1) {
0661                 PrintFactories(std::stoi(args[0]));
0662             }
0663             else if ((token == "PrintFactoryDetails" || token == "pfd") && (args.size() == 1)) {
0664                 PrintFactoryDetails(args[0]);
0665             }
0666             else if ((token == "PrintObjects" || token == "po") && (args.size() == 1)) {
0667                 PrintObjects(args[0]);
0668             }
0669             else if ((token == "PrintObject" || token == "po") && (args.size() == 2)) {
0670                 PrintObject(args[0], std::stoi(args[1]));
0671             }
0672             else if ((token == "PrintFactoryParents" || token == "pfp") && (args.size() == 1)) {
0673                 PrintFactoryParents(args[0]);
0674             }
0675             else if ((token == "PrintObjectParents" || token == "pop") && (args.size() == 2)) {
0676                 PrintObjectParents(args[0], std::stoi(args[1]));
0677             }
0678             else if ((token == "PrintObjectAncestors" || token == "poa") && (args.size() == 2)) {
0679                 PrintObjectAncestors(args[0], std::stoi(args[1]));
0680             }
0681             else if (token == "ViewAsTable" || token == "vt") {
0682                 m_format = Format::Table;
0683                 m_out << "(Switching to table view mode)" << std::endl;
0684             }
0685             else if (token == "ViewAsJson" || token == "vj") {
0686                 m_format = Format::Json;
0687                 m_out << "(Switching to JSON view mode)" << std::endl;
0688             }
0689             else if (token == "Continue" || token == "c") {
0690                 stay_in_loop = false;
0691             }
0692             else if (token == "Exit" || token == "x") {
0693                 stay_in_loop = false;
0694             }
0695             else if (token == "Help" || token == "h") {
0696                 PrintHelp();
0697             }
0698             else if (token == "") {
0699                 // Do nothing
0700             }
0701             else {
0702                 m_out << "(Error: Unrecognized command, or wrong argument count)" << std::endl;
0703                 PrintHelp();
0704             }
0705 
0706         }
0707         catch (JException& ex) {
0708             m_out << "(JException: Maybe you tried to print out objects that are not JObjects)" << std::endl;
0709         }
0710         catch (std::invalid_argument&) {
0711             m_out << "(Parse error: Maybe an argument needs to be an int)" << std::endl;
0712         }
0713         catch (...) {
0714             m_out << "(Unknown error)" << std::endl;
0715         }
0716     }
0717 
0718     if (m_enable_ticker_on_exit) {
0719         m_event->GetJApplication()->SetTicker( true );
0720     }
0721     if (m_enable_timeout_on_exit) {
0722         m_event->GetJApplication()->SetTimeoutEnabled( true );
0723     }
0724 }