Belle II Software  release-06-00-14
RootOutputModule.h
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 #pragma once
10 
11 #include <framework/core/Module.h>
12 #include <framework/core/Environment.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/dataobjects/FileMetaData.h>
16 #include <framework/dataobjects/EventMetaData.h>
17 
18 #include <TFile.h>
19 #include <TTree.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include <boost/optional.hpp>
25 
26 namespace Belle2 {
37  class RootOutputModule : public Module {
38 
39  public:
40 
44 
49  virtual ~RootOutputModule();
50 
55  virtual void initialize() override;
56 
61  virtual void event() override;
62 
68  virtual void terminate() override;
69 
71  virtual std::vector<std::string> getFileNames(bool outputFiles = true) override
72  {
73  B2ASSERT("RootOutput is not an input module", outputFiles);
75  // This will warn if already consumed which is what we want
76  std::string outputFileArgument = Environment::Instance().consumeOutputFileOverride(getName());
77  if (!outputFileArgument.empty())
78  m_outputFileName = outputFileArgument;
80  }
81  return {m_outputFileName};
82  }
83 
84  private:
85 
87  void closeFile();
88 
90  void openFile();
91 
98  void fillTree(DataStore::EDurability durability);
99 
101  void fillFileMetaData();
102 
103  //first the steerable variables:
104 
110  std::string m_outputFileName;
111 
117  std::vector<std::string> m_branchNames[DataStore::c_NDurabilityTypes];
118 
123 
130 
133 
136 
142 
145 
148 
151 
154 
158 
161  boost::optional<uint64_t> m_outputSplitSize{boost::none};
162 
163  //then those for purely internal use:
164 
166  int m_fileIndex{0};
167 
169  TFile* m_file;
170 
173 
175  std::vector<DataStore::StoreEntry*> m_entries[DataStore::c_NDurabilityTypes];
176 
178  std::vector<std::string> m_parentLfns;
179 
181  std::map<std::string, std::string> m_additionalDataDescription;
182 
185  unsigned long m_experimentLow;
186 
189  unsigned long m_runLow;
190 
193  unsigned long m_eventLow;
194 
197  unsigned long m_experimentHigh;
198 
201  unsigned long m_runHigh;
202 
205  unsigned long m_eventHigh;
206 
208  bool m_buildIndex{false};
209 
211  bool m_keepParents{false};
212 
214  bool m_regularFile{true};
215 
222  };
224 } // end namespace Belle2
static const int c_NDurabilityTypes
Number of Durability Types.
Definition: DataStore.h:63
EDurability
Durability types.
Definition: DataStore.h:58
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
std::string consumeOutputFileOverride(const std::string &moduleName)
Return overriden output file name, or "" if none was set.
Definition: Environment.cc:68
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:29
Metadata information about a file.
Definition: FileMetaData.h:29
Base class for Modules.
Definition: Module.h:72
const std::string & getName() const
Returns the name of the module.
Definition: Module.h:187
Write objects from DataStore into a ROOT file.
unsigned long m_experimentLow
Lowest experiment number.
std::vector< DataStore::StoreEntry * > m_entries[DataStore::c_NDurabilityTypes]
Vector of DataStore entries that are written to the output.
unsigned long m_experimentHigh
Highest experiment number.
unsigned long m_eventLow
Lowest event number in lowest run.
void fillFileMetaData()
Create and fill FileMetaData object.
int m_autosave
Number of entries (if >0) or number of bytes (if <0) after which write the tree metadata to disk.
bool m_regularFile
Whether this is a regular, local file where we can actually create directories.
unsigned long m_runLow
Lowest run number.
int m_compressionAlgorithm
TFile compression algorithm.
virtual void event() override
Write data in c_Event DataStore maps.
bool m_buildIndex
Whether or not we want to build an event index.
bool m_keepParents
Whether to keep parents same as that of input file.
virtual void terminate() override
Write data in the c_Persistent DataStore maps.
FileMetaData m_outputFileMetaData
File meta data finally stored in the output file.
TTree * m_tree[DataStore::c_NDurabilityTypes]
TTree for output.
unsigned long m_runHigh
Highest run number.
StoreObjPtr< EventMetaData > m_eventMetaData
Pointer to the event meta data.
std::vector< std::string > m_excludeBranchNames[DataStore::c_NDurabilityTypes]
Array for names of branches that should NOT be written out.
int m_basketsize
basket size for each branch in the file in bytes
std::vector< std::string > m_additionalBranchNames[DataStore::c_NDurabilityTypes]
Array of names of branches that should be written out although they are not flagged for writeout.
virtual void initialize() override
Setting up of various stuff.
TFile * m_file
TFile for output.
int m_fileIndex
Keep track of the file index: if we split files than we add '.f{fileIndex:05d}' in front of the ROOT ...
void fillTree(DataStore::EDurability durability)
Fill TTree.
bool m_ignoreCommandLineOverride
Ignore filename override from command line.
void closeFile()
Finalize the output file.
int m_compressionLevel
TFile compression level.
int m_autoflush
Number of entries (if >0) or number of bytes (if <0) after which to flush all baskets to disk.
boost::optional< uint64_t > m_outputSplitSize
Maximum output file size in MB.
bool m_updateFileCatalog
Flag to enable or disable the update of the metadata catalog.
int m_splitLevel
Branch split level.
void openFile()
Open the next output file.
unsigned long m_eventHigh
Highest event number in highest run.
virtual ~RootOutputModule()
Destructor.
std::map< std::string, std::string > m_additionalDataDescription
Map of additional metadata to be added to the output file.
virtual std::vector< std::string > getFileNames(bool outputFiles=true) override
Set the used output file, taking into account -o argument to basf2.
std::vector< std::string > m_parentLfns
Vector of parent file LFNs.
std::vector< std::string > m_branchNames[DataStore::c_NDurabilityTypes]
Array for names of branches that should be written out.
StoreObjPtr< FileMetaData > m_fileMetaData
Pointer to the file meta data.
std::string m_outputFileName
Name for output file.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Abstract base class for different kinds of events.