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