artdaq  v3_12_02
InputUtilities.hh
1 #ifndef artdaq_ArtModules_InputUtilities_hh
2 #define artdaq_ArtModules_InputUtilities_hh
3 
4 #include "TRACE/tracemf.h" // TLOG - note: no #define TRACE_NAME in .hh files -
5 // could conflict with TRACE_NAME in .cc files.
6 
7 #include "canvas/Utilities/Exception.h"
8 
9 #include <TBufferFile.h>
10 #include <TClass.h>
11 
12 #include <iostream>
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 
17 #define TLVL_READOBJANY 32
18 #define TLVL_PROCESSHISTORYID 33
19 #define TLVL_PROCESSMAP 34
20 
21 namespace art {
30 template<typename T>
31 T* ReadObjectAny(const std::unique_ptr<TBufferFile>& infile, const std::string& className, const std::string& callerName)
32 {
33  static TClass* tclassPtr = TClass::GetClass(className.c_str());
34 
35  if (tclassPtr == nullptr)
36  {
37  throw art::Exception(art::errors::DictionaryNotFound) << callerName << " call to ReadObjectAny: " // NOLINT(cert-err60-cpp)
38  "Could not get TClass for "
39  << className << "!";
40  }
41 
42  // JCF, May-24-2016
43 
44  // Be aware of the following from the TBufferFile documentation,
45  // concerning TBufferFile::ReadObjectAny:
46 
47  // " In case of multiple inheritance, the return value might not be
48  // the real beginning of the object in memory. You will need to use
49  // a dynamic_cast later if you need to retrieve it."
50 
51  T* ptr = reinterpret_cast<T*>(infile->ReadObjectAny(tclassPtr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
52  TLOG(TLVL_READOBJANY, "InputUtilities") << "ReadObjectAny: Got object of class " << className << ", located at " << static_cast<void*>(ptr) << " caller:" << callerName;
53 
54  return ptr;
55 }
56 
63 template<typename T>
64 void printProcessHistoryID(const std::string& label, const T& object)
65 {
66  if (object->processHistoryID().isValid())
67  {
68  // std::ostringstream OS;
69  // object->processHistoryID().print(OS);
70  TLOG(TLVL_PROCESSHISTORYID, "InputUtilities") << label << ": "
71  << "ProcessHistoryID: " << object->processHistoryID();
72  }
73  else
74  {
75  TLOG(TLVL_PROCESSHISTORYID, "InputUtilities") << label << ": "
76  << "ProcessHistoryID: 'INVALID'";
77  }
78 }
79 
86 template<typename T>
87 void printProcessMap(const T& mappable, const std::string& description)
88 {
89  TLOG(TLVL_PROCESSMAP, "InputUtilities") << "Got " << description;
90 
91  TLOG(TLVL_PROCESSMAP, "InputUtilities") << "Dumping " << description << "...";
92  TLOG(TLVL_PROCESSMAP, "InputUtilities") << "Size: " << mappable.size();
93  for (auto I = mappable.begin(), E = mappable.end(); I != E; ++I)
94  {
95  std::ostringstream OS;
96  I->first.print(OS);
97  TLOG(TLVL_PROCESSMAP, "InputUtilities") << description << ": id: '" << OS.str() << "'";
98  OS.str("");
99  TLOG(TLVL_PROCESSMAP, "InputUtilities") << description << ": data.size(): " << I->second.data().size();
100  I->second.data().back().id().print(OS);
101 
102  TLOG(TLVL_PROCESSMAP, "InputUtilities") << description << ": data.back().id(): '" << OS.str() << "'";
103  }
104 }
105 } // namespace art
106 
107 #endif
void printProcessHistoryID(const std::string &label, const T &object)
Print the processHistoryID from the object.
void printProcessMap(const T &mappable, const std::string &description)
Print data from a map-like class.
T * ReadObjectAny(const std::unique_ptr< TBufferFile > &infile, const std::string &className, const std::string &callerName)
ReadObjectAny reads data from a TBufferFile and casts it to the given type.