artdaq  v3_11_02
InputUtilities.hh
1 #ifndef artdaq_ArtModules_InputUtilities_hh
2 #define artdaq_ArtModules_InputUtilities_hh
3 
4 #include "art/Framework/Core/FileBlock.h"
5 #include "art/Framework/Core/InputSourceMacros.h"
6 #include "art/Framework/Core/ProductRegistryHelper.h"
7 #include "art/Framework/IO/Sources/Source.h"
8 #include "art/Framework/IO/Sources/SourceHelper.h"
9 #include "art/Framework/IO/Sources/SourceTraits.h"
10 #include "art/Framework/Services/Registry/ServiceHandle.h"
11 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
12 
13 #include "canvas/Persistency/Common/EDProduct.h"
14 #include "canvas/Persistency/Common/Wrapper.h"
15 #include "canvas/Persistency/Provenance/BranchDescription.h"
16 #include "canvas/Persistency/Provenance/BranchKey.h"
17 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
18 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
19 #include "canvas/Persistency/Provenance/ProcessHistory.h"
20 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
21 #include "canvas/Persistency/Provenance/ProductList.h"
22 #include "canvas/Persistency/Provenance/ProductProvenance.h"
23 #include "canvas/Utilities/DebugMacros.h"
24 
25 #include <TBufferFile.h>
26 
27 #define TLVL_READOBJANY 32
28 #define TLVL_PROCESSHISTORYID 33
29 #define TLVL_PROCESSMAP 34
30 
31 #include "tracemf.h" // TLOG - note: no #define TRACE_NAME in .hh files -
32 // could conflict with TRACE_NAME in .cc files.
33 #include <iostream>
34 #include <memory>
35 #include <string>
36 
37 namespace art {
46 template<typename T>
47 T* ReadObjectAny(const std::unique_ptr<TBufferFile>& infile, const std::string& className, const std::string& callerName)
48 {
49  static TClass* tclassPtr = TClass::GetClass(className.c_str());
50 
51  if (tclassPtr == nullptr)
52  {
53  throw art::Exception(art::errors::DictionaryNotFound) << callerName << " call to ReadObjectAny: " // NOLINT(cert-err60-cpp)
54  "Could not get TClass for "
55  << className << "!";
56  }
57 
58  // JCF, May-24-2016
59 
60  // Be aware of the following from the TBufferFile documentation,
61  // concerning TBufferFile::ReadObjectAny:
62 
63  // " In case of multiple inheritance, the return value might not be
64  // the real beginning of the object in memory. You will need to use
65  // a dynamic_cast later if you need to retrieve it."
66 
67  T* ptr = reinterpret_cast<T*>(infile->ReadObjectAny(tclassPtr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
68  TLOG(TLVL_READOBJANY, "InputUtilities") << "ReadObjectAny: Got object of class " << className << ", located at " << static_cast<void*>(ptr) << " caller:" << callerName;
69 
70  return ptr;
71 }
72 
79 template<typename T>
80 void printProcessHistoryID(const std::string& label, const T& object)
81 {
82  if (object->processHistoryID().isValid())
83  {
84  //std::ostringstream OS;
85  //object->processHistoryID().print(OS);
86  TLOG(TLVL_PROCESSHISTORYID, "InputUtilities") << label << ": "
87  << "ProcessHistoryID: " << object->processHistoryID();
88  }
89  else
90  {
91  TLOG(TLVL_PROCESSHISTORYID, "InputUtilities") << label << ": "
92  << "ProcessHistoryID: 'INVALID'";
93  }
94 }
95 
102 template<typename T>
103 void printProcessMap(const T& mappable, const std::string& description)
104 {
105  TLOG(TLVL_PROCESSMAP, "InputUtilities") << "Got " << description;
106 
107  TLOG(TLVL_PROCESSMAP, "InputUtilities") << "Dumping " << description << "...";
108  TLOG(TLVL_PROCESSMAP, "InputUtilities") << "Size: " << mappable.size();
109  for (auto I = mappable.begin(), E = mappable.end(); I != E; ++I)
110  {
111  std::ostringstream OS;
112  I->first.print(OS);
113  TLOG(TLVL_PROCESSMAP, "InputUtilities") << description << ": id: '" << OS.str() << "'";
114  OS.str("");
115  TLOG(TLVL_PROCESSMAP, "InputUtilities") << description << ": data.size(): " << I->second.data().size();
116  I->second.data().back().id().print(OS);
117 
118  TLOG(TLVL_PROCESSMAP, "InputUtilities") << description << ": data.back().id(): '" << OS.str() << "'";
119  }
120 }
121 } // namespace art
122 
123 #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.