Belle II Software  release-05-02-19
PruneDataStoreModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2016 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Hauth *
7  * *
8  **************************************************************************/
9 
10 #include <framework/modules/core/PruneDataStoreModule.h>
11 #include <framework/datastore/DataStore.h>
12 #include <framework/logging/Logger.h>
13 
14 using namespace std;
15 using namespace Belle2;
16 
17 REG_MODULE(PruneDataStore)
18 
20  Module()
21 {
22  setDescription(R"DOC(
23  Clears the content of the DataStore while it keeps entries listed in the matchEntries option.
24  The EventMetaData object will always be kept, as it is required by the framework to properly
25  work with the DataStore.
26 
27  This logic can be inverted to remove only the entries matched by the regex
28  in the matchEntries parameter by setting the parameter to
29  keepMatchedEntries to False.
30 
31  Note:
32  Also all Relations will be cleared if they are not matched by one entry
33  in the matchEntries list. You have to ensure the objects referenced by
34  kept relations are also matched by one entry in the keepEntries list so
35  a relation does not point to nirvana.
36  )DOC");
37  addParam("matchEntries", m_matchEntries,
38  "name of all DataStore entries (with regular expression syntax) to match. "
39  "For example, you can use 'Raw.*' to match all Raw-Objects.",
40  m_matchEntries);
41  addParam("keepMatchedEntries", m_keepMatchedEntries,
42  "If true, all entries matched by the regular expression are kept. "
43  "If false, matched entries will be removed.",
44  m_keepMatchedEntries);
45  setPropertyFlags(c_ParallelProcessingCertified);
46 
47 }
48 
49 void PruneDataStoreModule::initialize()
50 {
51  // prepare the regex_matchers, otherwise this nede to be done for each DataStore item
52  for (auto& kEntry : m_matchEntries) {
53  m_compiled_regex.push_back(compileAndCatch(kEntry));
54  }
55  // also get the regex for the implicit keeps
56  for (auto& kEntry : m_keepEntriesImplicit) {
57  m_compiled_regex_implicit.push_back(compileAndCatch(kEntry));
58  }
59 }
60 
61 std::regex PruneDataStoreModule::compileAndCatch(std::string& regexString) const
62 {
63  try {
64  return std::regex(regexString);
65  } catch (const std::regex_error& e) {
66  B2FATAL("Regex '" << regexString << "' cannot be compiled: " << e.what());
67  // keep the compiler happy and return something
68  return std::regex();
69  }
70 }
71 
72 void PruneDataStoreModule::event()
73 {
74  auto& storemap = DataStore::Instance().getStoreEntryMap(DataStore::c_Event);
75 
76  // iterate through all StoreEntries and check the Regex expression for each entry
77  for (auto& datastore_item : storemap) {
78  std::string const& datastore_key = datastore_item.first;
79 
80  // check if this entry is in our to keep list
81  bool toKeep = !m_keepMatchedEntries;
82  for (auto const& regx : m_compiled_regex) {
83  if (std::regex_match(datastore_key, regx)) {
84  toKeep = m_keepMatchedEntries;
85  }
86  }
87 
88  // check for implicit keeps
89  for (auto const& regx : m_compiled_regex_implicit) {
90  if (std::regex_match(datastore_key, regx)) {
91  // always keep, no matter of the keepMatchedEntries configuration
92  toKeep = true;
93  }
94  }
95 
96  if (toKeep) {
97  B2DEBUG(100,
98  "StoreArray entry " << datastore_key << " will be not pruned from the datastore");
99  } else {
100  B2DEBUG(100,
101  "StoreArray entry " << datastore_key << " will be pruned from the datastore");
102  }
103 
104  if (!toKeep) {
105  // recreate to clear its content
106  datastore_item.second.invalidate();
107  }
108  }
109 
110 }
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::PruneDataStoreModule
Clears the content of the DataStore while it keeps entries matching the regex expression in the match...
Definition: PruneDataStoreModule.h:41
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19