Belle II Software  release-05-01-25
RootInputModule.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2012 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Heck, Christian Pulvermacher, Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/core/Module.h>
14 #include <framework/datastore/DataStore.h>
15 #include <framework/core/Environment.h>
16 #include <framework/dataobjects/FileMetaData.h>
17 
18 #include <string>
19 #include <vector>
20 #include <set>
21 
22 #include <TChain.h>
23 #include <TFile.h>
24 
25 
26 namespace Belle2 {
43  class RootInputModule : public Module {
44  public:
45 
48 
50  virtual ~RootInputModule();
51 
53  virtual void initialize() override;
54 
56  virtual void event() override;
57 
59  virtual void terminate() override;
60 
62  virtual std::vector<std::string> getFileNames(bool outputFiles = false) override
63  {
64  B2ASSERT("RootInput is not an output module", !outputFiles);
65  std::vector<std::string> inputFiles = Environment::Instance().getInputFilesOverride();
66  if (!m_ignoreCommandLineOverride and !inputFiles.empty()) {
67  return inputFiles;
68  }
69  inputFiles = m_inputFileNames;
70  if (!m_inputFileName.empty())
71  inputFiles.push_back(m_inputFileName);
72  return inputFiles;
73  }
74 
75  protected:
76 
77 
78  private:
79  typedef std::vector<DataStore::StoreEntry*> StoreEntries;
82  void readTree();
83 
92  bool connectBranches(TTree* tree, DataStore::EDurability durability, StoreEntries* storeEntries);
93 
96 
98  bool readParentTrees();
99 
101  void readPersistentEntry(long fileEntry);
102 
104  void entryNotFound(const std::string& entryOrigin, const std::string& name, bool fileChanged = true);
105 
107  void addEventListForIndexFile(const std::string& parentLfn);
108 
110  void realDataWorkaround(FileMetaData& metaData);
111 
112  //first the steerable variables:
114  std::string m_inputFileName;
115 
117  std::vector<std::string> m_inputFileNames;
118 
122  std::vector<std::string> m_entrySequences;
123 
126 
133  std::vector<std::string> m_branchNames[DataStore::c_NDurabilityTypes];
134 
140  std::vector<std::string> m_excludeBranchNames[DataStore::c_NDurabilityTypes];
141 
142 
144  unsigned int m_skipNEvents;
145 
147  int m_parentLevel;
148 
150  bool m_collectStatistics;
151 
153  std::vector<int> m_skipToEvent;
154 
155  //then those for purely internal use:
156 
159 
162 
164  std::string m_lastParentFileLFN;
165 
166 
168  TChain* m_tree;
169 
171  TChain* m_persistent;
172 
174  std::set<std::string> m_connectedBranches[DataStore::c_NDurabilityTypes];
175 
180 
182  std::vector<StoreEntries> m_parentStoreEntries;
183 
185  std::map<std::string, TTree*> m_parentTrees;
186 
188  struct ReadStats {
189  long calls{0};
190  long bytesRead{0};
191  long bytesReadExtra{0};
193  void add(const ReadStats& b)
194  {
195  calls += b.calls;
196  bytesRead += b.bytesRead;
197  bytesReadExtra += b.bytesReadExtra;
198  }
200  void addFromFile(const TFile* f)
201  {
202  calls += f->GetReadCalls();
203  bytesRead += f->GetBytesRead();
204  bytesReadExtra += f->GetBytesReadExtra();
205  }
207  std::string getString() const
208  {
209  std::string s;
210  s += "read: " + std::to_string(bytesRead) + " Bytes";
211  s += ", overhead: " + std::to_string(bytesReadExtra) + " Bytes";
212  s += ", Read() calls: " + std::to_string(calls);
213  return s;
214  }
215  };
216 
219 
221  int m_cacheSize{0};
222 
224  bool m_discardErrorEvents{true};
225 
227  bool m_processingAllEvents{true};
228  };
230 } // end namespace Belle2
Belle2::RootInputModule::createParentStoreEntries
bool createParentStoreEntries()
Connect the parent trees and fill m_parentStoreEntries.
Definition: RootInputModule.cc:553
Belle2::RootInputModule::StoreEntries
std::vector< DataStore::StoreEntry * > StoreEntries
Vector of entries in the data store.
Definition: RootInputModule.h:87
Belle2::RootInputModule::m_inputFileNames
std::vector< std::string > m_inputFileNames
Files to read from.
Definition: RootInputModule.h:125
Belle2::RootInputModule::m_discardErrorEvents
bool m_discardErrorEvents
Discard events that have an error flag != 0.
Definition: RootInputModule.h:232
Belle2::RootInputModule::ReadStats::bytesRead
long bytesRead
total number of bytes read.
Definition: RootInputModule.h:198
Belle2::RootInputModule::readParentTrees
bool readParentTrees()
Read data of the current event from the parents.
Definition: RootInputModule.cc:610
Belle2::RootInputModule::m_ignoreCommandLineOverride
bool m_ignoreCommandLineOverride
Ignore filename override from command line.
Definition: RootInputModule.h:133
Belle2::Environment::getInputFilesOverride
const std::vector< std::string > & getInputFilesOverride() const
Return overriden input file names, or empty vector if none were set.
Definition: Environment.h:111
Belle2::RootInputModule::addEventListForIndexFile
void addEventListForIndexFile(const std::string &parentLfn)
For index files, this creates TEventList/TEntryListArray to enable better cache use.
Definition: RootInputModule.cc:669
Belle2::RootInputModule::m_persistentStoreEntries
StoreEntries m_persistentStoreEntries
Vector of DataStore entries of persistent durability that we are supposed to read in.
Definition: RootInputModule.h:187
Belle2::RootInputModule::m_storeEntries
StoreEntries m_storeEntries
Vector of DataStore entries of event durability that we are supposed to read in.
Definition: RootInputModule.h:185
Belle2::RootInputModule::m_parentTrees
std::map< std::string, TTree * > m_parentTrees
Map of file LFNs to trees.
Definition: RootInputModule.h:193
Belle2::RootInputModule::m_persistent
TChain * m_persistent
TTree for persistent input.
Definition: RootInputModule.h:179
Belle2::RootInputModule::initialize
virtual void initialize() override
Initialize the Module.
Definition: RootInputModule.cc:94
Belle2::RootInputModule::getFileNames
virtual std::vector< std::string > getFileNames(bool outputFiles=false) override
Get list of input files, taking -i command line overrides into account.
Definition: RootInputModule.h:70
Belle2::RootInputModule::entryNotFound
void entryNotFound(const std::string &entryOrigin, const std::string &name, bool fileChanged=true)
Check if we warn the user or abort after an entry was missing after changing files.
Definition: RootInputModule.cc:713
Belle2::FileMetaData
Metadata information about a file.
Definition: FileMetaData.h:39
Belle2::RootInputModule::readTree
void readTree()
Actually performs the reading from the tree.
Definition: RootInputModule.cc:407
Belle2::RootInputModule::m_collectStatistics
bool m_collectStatistics
Collect statistics on amount of data read and print statistics (seperate for input & parent files) af...
Definition: RootInputModule.h:158
Belle2::RootInputModule::m_readStats
ReadStats m_readStats
some statistics for all files read so far.
Definition: RootInputModule.h:226
Belle2::RootInputModule::ReadStats
for collecting statistics over multiple files.
Definition: RootInputModule.h:196
Belle2::RootInputModule::m_parentStoreEntries
std::vector< StoreEntries > m_parentStoreEntries
The parent DataStore entries per level.
Definition: RootInputModule.h:190
Belle2::RootInputModule::RootInputModule
RootInputModule()
Constructor.
Definition: RootInputModule.cc:41
Belle2::RootInputModule::m_tree
TChain * m_tree
TTree for event input.
Definition: RootInputModule.h:176
Belle2::RootInputModule::readPersistentEntry
void readPersistentEntry(long fileEntry)
Loads given entry from persistent tree.
Definition: RootInputModule.cc:723
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RootInputModule::m_connectedBranches
std::set< std::string > m_connectedBranches[DataStore::c_NDurabilityTypes]
Already connected branches.
Definition: RootInputModule.h:182
Belle2::RootInputModule::ReadStats::add
void add(const ReadStats &b)
add other stats object.
Definition: RootInputModule.h:201
Belle2::DataStore::c_NDurabilityTypes
const static int c_NDurabilityTypes
Number of Durability Types.
Definition: DataStore.h:65
Belle2::RootInputModule::m_inputFileName
std::string m_inputFileName
File to read from.
Definition: RootInputModule.h:122
Belle2::RootInputModule::m_skipToEvent
std::vector< int > m_skipToEvent
experiment, run, event number of first event to load
Definition: RootInputModule.h:161
Belle2::RootInputModule::m_branchNames
std::vector< std::string > m_branchNames[DataStore::c_NDurabilityTypes]
Array for names of branches, that shall be written out.
Definition: RootInputModule.h:141
Belle2::RootInputModule::m_entrySequences
std::vector< std::string > m_entrySequences
The number sequences (e.g.
Definition: RootInputModule.h:130
Belle2::RootInputModule::m_lastPersistentEntry
long m_lastPersistentEntry
last entry to be in persistent tree.
Definition: RootInputModule.h:169
Belle2::RootInputModule::m_excludeBranchNames
std::vector< std::string > m_excludeBranchNames[DataStore::c_NDurabilityTypes]
Array for names of branches that should NOT be written out.
Definition: RootInputModule.h:148
Belle2::RootInputModule::connectBranches
bool connectBranches(TTree *tree, DataStore::EDurability durability, StoreEntries *storeEntries)
Connect branches of the given tree to the data store.
Definition: RootInputModule.cc:488
Belle2::RootInputModule::ReadStats::calls
long calls
number of read calls.
Definition: RootInputModule.h:197
Belle2::RootInputModule::realDataWorkaround
void realDataWorkaround(FileMetaData &metaData)
Correct isMC flag for raw data recorded before experiment 8 run 2364.
Definition: RootInputModule.cc:763
Belle2::RootInputModule::event
virtual void event() override
Running over all events.
Definition: RootInputModule.cc:332
Belle2::RootInputModule::terminate
virtual void terminate() override
Is called at the end of your Module.
Definition: RootInputModule.cc:375
Belle2::RootInputModule::m_cacheSize
int m_cacheSize
Input ROOT File Cache size in MB, <0 means default.
Definition: RootInputModule.h:229
Belle2::RootInputModule::m_nextEntry
long m_nextEntry
Next entry to be read in event tree.
Definition: RootInputModule.h:166
Belle2::RootInputModule::~RootInputModule
virtual ~RootInputModule()
Destructor.
Belle2::RootInputModule::ReadStats::addFromFile
void addFromFile(const TFile *f)
add current statistics from TFile object.
Definition: RootInputModule.h:208
Belle2::RootInputModule::ReadStats::bytesReadExtra
long bytesReadExtra
what TFile thinks was the overhead.
Definition: RootInputModule.h:199
Belle2::RootInputModule::m_processingAllEvents
bool m_processingAllEvents
Set to true if we process the input files completely: No skip events or sequences or -n parameters.
Definition: RootInputModule.h:235
Belle2::RootInputModule::m_skipNEvents
unsigned int m_skipNEvents
Can be set from steering file to skip the first N events.
Definition: RootInputModule.h:152
Belle2::RootInputModule::m_parentLevel
int m_parentLevel
Level of parent files to be read.
Definition: RootInputModule.h:155
Belle2::Environment::Instance
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:31
Belle2::RootInputModule::ReadStats::getString
std::string getString() const
string suitable for printing.
Definition: RootInputModule.h:215
Belle2::RootInputModule::m_lastParentFileLFN
std::string m_lastParentFileLFN
last parent file LFN seen.
Definition: RootInputModule.h:172
Belle2::DataStore::EDurability
EDurability
Durability types.
Definition: DataStore.h:60