Belle II Software  release-08-01-10
PrintCollectionsModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/modules/core/PrintCollectionsModule.h>
10 
11 #include <framework/datastore/StoreObjPtr.h>
12 #include <framework/dataobjects/EventMetaData.h>
13 
14 #include <TClass.h>
15 #include <TClonesArray.h>
16 
17 #include <boost/format.hpp>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 //-----------------------------------------------------------------
23 // Register the Module
24 //-----------------------------------------------------------------
25 REG_MODULE(PrintCollections);
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 PrintCollectionsModule::PrintCollectionsModule()
32 {
33  setDescription("Prints the contents of the DataStore in an event, listing all objects and arrays (including size).");
34 
35  addParam("printForEvent", m_printForEvent,
36  "Print the collections only for a specific event number. If set to -1 (default) only the collections of the first event will be printed, if set to 0, the collections of all events will be printed, which might be a lot of output.",
37  m_printForEvent);
38 }
39 
40 PrintCollectionsModule::~PrintCollectionsModule() = default;
41 
42 void PrintCollectionsModule::initialize()
43 {
45 }
46 
47 void PrintCollectionsModule::event()
48 {
49  StoreObjPtr<EventMetaData> eventMetaDataPtr;
50 
51  // check if printing only for the first event
52  if (m_printForEvent < 0) {
53  if (m_firstEvent)
54  m_firstEvent = false;
55  else
56  return;
57  }
58  // or for a specific event
59  else if ((m_printForEvent > 0) && ((unsigned int)m_printForEvent != eventMetaDataPtr->getEvent()))
60  return;
61 
62  B2INFO("============================================================================");
63  B2INFO("DataStore collections in event " << eventMetaDataPtr->getEvent());
64  B2INFO("============================================================================");
65  B2INFO(boost::format("Type %|20t| Name %|47t| #Entries <Event>"));
66  printCollections(DataStore::c_Event);
67  B2INFO("----------------------------------------------------------------------------");
68  B2INFO(boost::format("Type %|20t| Name %|47t| #Entries <Persistent>"));
69  printCollections(DataStore::c_Persistent);
70  B2INFO("============================================================================");
71 }
72 
73 
75 std::string shorten(std::string className)
76 {
77  if (className.compare(0, 8, "Belle2::") == 0) {
78  //we know the experiment name, thanks
79  return className.substr(8);
80  }
81  return className;
82 }
83 
84 //===============================================================
85 // Protected methods
86 //===============================================================
87 
88 
89 void PrintCollectionsModule::printCollections(DataStore::EDurability durability)
90 {
91  //-----------------------------
92  //Print the object information
93  //-----------------------------
94  const DataStore::StoreEntryMap& map = DataStore::Instance().getStoreEntryMap(durability);
95  for (auto iter = map.begin(); iter != map.end(); ++iter) {
96  if (iter->second.isArray)
97  continue;
98  const TObject* currCol = iter->second.ptr;
99 
100  if (currCol != nullptr) {
101  B2INFO(boost::format("%1% %|20t| %2%") % shorten(currCol->ClassName()) % iter->first);
102  }
103  }
104 
105 
106  //-----------------------------
107  //Print the array information
108  //-----------------------------
109  for (auto iter = map.begin(); iter != map.end(); ++iter) {
110  if (!iter->second.isArray)
111  continue;
112  const TClonesArray* currCol = dynamic_cast<TClonesArray*>(iter->second.ptr);
113 
114  long entries = 0;
115  if (currCol != nullptr)
116  entries = currCol->GetEntriesFast();
117 
118  std::string type = shorten(iter->second.objClass->GetName());
119 
120  B2INFO(boost::format("%1%[] %|20t| %2% %|47t| %3%") % type % iter->first % entries);
121  }
122  B2INFO("");
123 }
EDurability
Durability types.
Definition: DataStore.h:58
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
Definition: DataStore.h:87
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.