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