Belle II Software  release-08-01-10
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 <optional>
22 #include <string>
23 #include <vector>
24 
25 namespace Belle2 {
36  class RootOutputModule : public Module {
37 
38  public:
39 
43 
48  virtual ~RootOutputModule();
49 
54  virtual void initialize() override;
55 
60  virtual void event() override;
61 
67  virtual void terminate() override;
68 
70  virtual std::vector<std::string> getFileNames(bool outputFiles = true) override
71  {
72  B2ASSERT("RootOutput is not an input module", outputFiles);
74  // This will warn if already consumed which is what we want
75  std::string outputFileArgument = Environment::Instance().consumeOutputFileOverride(getName());
76  if (!outputFileArgument.empty())
77  m_outputFileName = outputFileArgument;
79  }
80  return {m_outputFileName};
81  }
82 
83  private:
84 
86  void closeFile();
87 
89  void openFile();
90 
97  void fillTree(DataStore::EDurability durability);
98 
100  void fillFileMetaData();
101 
102  //first the steerable variables:
103 
109  std::string m_outputFileName;
110 
116  std::vector<std::string> m_branchNames[DataStore::c_NDurabilityTypes];
117 
122 
129 
132 
135 
141 
144 
147 
150 
153 
157 
160  std::optional<uint64_t> m_outputSplitSize{std::nullopt};
161 
162  //then those for purely internal use:
163 
165  int m_fileIndex{0};
166 
168  unsigned int m_nFullEvents{0};
169 
171  TFile* m_file;
172 
175 
177  std::vector<DataStore::StoreEntry*> m_entries[DataStore::c_NDurabilityTypes];
178 
180  std::vector<std::string> m_parentLfns;
181 
183  std::map<std::string, std::string> m_additionalDataDescription;
184 
187  unsigned long m_experimentLow;
188 
191  unsigned long m_runLow;
192 
195  unsigned long m_eventLow;
196 
199  unsigned long m_experimentHigh;
200 
203  unsigned long m_runHigh;
204 
207  unsigned long m_eventHigh;
208 
210  bool m_buildIndex{false};
211 
213  bool m_keepParents{false};
214 
216  bool m_regularFile{true};
217 
224  };
226 } // 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:67
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
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.
TTree * m_tree[DataStore::c_NDurabilityTypes]
TTree for output.
unsigned int m_nFullEvents
Number of full events (aka number of events without an error flag)
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.
FileMetaData * m_outputFileMetaData
File meta data stored in the output file.
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.
std::optional< uint64_t > m_outputSplitSize
Maximum output file size in MB.
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.
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 input file meta data.
std::string m_outputFileName
Name for output file.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Abstract base class for different kinds of events.