artdaq  3.12.07
RootDAQOutFile.h
1 #ifndef art_root_io_RootDAQOutFile_h
2 #define art_root_io_RootDAQOutFile_h
3 // vim: set sw=2 expandtab :
4 
5 #include "art/Framework/Core/fwd.h"
6 #include "art/Framework/IO/ClosingCriteria.h"
7 #include "art/Framework/Principal/EventPrincipal.h"
8 #include "art/Framework/Principal/fwd.h"
9 #include "art/Framework/Services/System/FileCatalogMetadata.h"
10 #include "art_root_io/DropMetaData.h"
11 #include "art_root_io/DummyProductCache.h"
12 #include "art_root_io/FastCloningEnabled.h"
13 #include "art_root_io/RootOutputTree.h"
14 #include "canvas/Persistency/Provenance/BranchDescription.h"
15 #include "canvas/Persistency/Provenance/BranchType.h"
16 #include "canvas/Persistency/Provenance/FileIndex.h"
17 #include "canvas/Persistency/Provenance/ProductID.h"
18 #include "canvas/Persistency/Provenance/ProductProvenance.h"
19 #include "canvas/Persistency/Provenance/fwd.h"
20 #include "cetlib/sqlite/Connection.h"
21 
22 #include <array>
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 #include <string>
27 #include <vector>
28 
29 #include "TFile.h"
30 
31 class TTree;
32 
33 namespace art {
34 class FileStatsCollector;
35 class RootFileBlock;
36 
37 struct OutputItem
38 {
39  ~OutputItem();
40  explicit OutputItem(BranchDescription const& bd);
41 
42  BranchDescription const branchDescription;
43  mutable void const* product;
44 };
45 
47 {
48 public:
49  enum class ClosureRequestMode
50  {
51  MaxEvents = 0,
52  MaxSize = 1,
53  Unset = 2
54  };
55  using RootOutputTreePtrArray =
56  std::array<std::unique_ptr<RootOutputTree>, NumBranchTypes>;
57 
58  ~RootDAQOutFile();
59  explicit RootDAQOutFile(OutputModule*,
60  std::string const& fileName,
61  ClosingCriteria const& fileSwitchCriteria,
62  int compressionLevel,
63  unsigned freePercent,
64  unsigned freeMB,
65  int64_t saveMemoryObjectThreshold,
66  int64_t treeMaxVirtualSize,
67  int splitLevel,
68  int basketSize,
69  DropMetaData dropMetaData,
70  bool dropMetaDataForDroppedData);
71  RootDAQOutFile(RootDAQOutFile const&) = delete;
72  RootDAQOutFile(RootDAQOutFile&&) = delete;
73  RootDAQOutFile& operator=(RootDAQOutFile const&) = delete;
74  RootDAQOutFile& operator=(RootDAQOutFile&&) = delete;
75 
76  void createDatabaseTables();
77  void writeTTrees();
78  void writeOne(EventPrincipal const&);
79  void writeSubRun(SubRunPrincipal const&);
80  void writeRun(RunPrincipal const&);
81  void writeFileFormatVersion();
82  void writeFileIndex();
83  void writeProcessConfigurationRegistry();
84  void writeProcessHistoryRegistry();
85  void writeParameterSetRegistry();
86  void writeProductDescriptionRegistry();
87  void writeParentageRegistry();
88  void writeProductDependencies();
89  void writeFileCatalogMetadata(FileStatsCollector const& stats,
90  FileCatalogMetadata::collection_type const&,
91  FileCatalogMetadata::collection_type const&);
92  void writeResults(ResultsPrincipal& resp);
93  void setRunAuxiliaryRangeSetID(RangeSet const&);
94  void setSubRunAuxiliaryRangeSetID(RangeSet const&);
95  void beginInputFile(RootFileBlock const*,
96  FastCloningEnabled fastCloningEnabled);
97  void incrementInputFileNumber();
98  void respondToCloseInputFile(FileBlock const&);
99  bool requestsToCloseFile();
100  void setFileStatus(OutputFileStatus ofs);
101  void selectProducts();
102  std::string const& currentFileName() const;
103  bool maxEventsPerFileReached(
104  FileIndex::EntryNumber_t maxEventsPerFile) const;
105  bool maxSizeReached(unsigned maxFileSize) const;
106 
107 private:
108  template<BranchType>
109  void fillBranches(Principal const&, std::vector<ProductProvenance>*);
110  template<BranchType BT>
111  EDProduct const* getProduct(OutputHandle const&,
112  RangeSet const& productRS,
113  std::string const& wrappedName);
114 
115  mutable std::recursive_mutex mutex_{};
116  OutputModule const* om_;
117  std::string file_;
118  ClosingCriteria fileSwitchCriteria_;
119  OutputFileStatus status_{OutputFileStatus::Closed};
120  int const compressionLevel_;
121  unsigned freePercent_;
122  unsigned freeMB_;
123  int64_t const saveMemoryObjectThreshold_;
124  int64_t const treeMaxVirtualSize_;
125  int const splitLevel_;
126  int const basketSize_;
127  DropMetaData dropMetaData_;
128  bool dropMetaDataForDroppedData_;
129  bool wasFastCloned_{false};
130  std::unique_ptr<TFile> filePtr_;
131  FileIndex fileIndex_;
132  FileProperties fp_;
133  TTree* metaDataTree_;
134  TTree* fileIndexTree_;
135  TTree* parentageTree_;
136  EventAuxiliary const* pEventAux_{nullptr};
137  SubRunAuxiliary const* pSubRunAux_{nullptr};
138  RunAuxiliary const* pRunAux_{nullptr};
139  ResultsAuxiliary const* pResultsAux_{nullptr};
140  ProductProvenances eventProductProvenanceVector_{};
141  ProductProvenances subRunProductProvenanceVector_{};
142  ProductProvenances runProductProvenanceVector_{};
143  ProductProvenances resultsProductProvenanceVector_{};
144  ProductProvenances* pEventProductProvenanceVector_{
145  &eventProductProvenanceVector_};
146  ProductProvenances* pSubRunProductProvenanceVector_{
147  &subRunProductProvenanceVector_};
148  ProductProvenances* pRunProductProvenanceVector_{
149  &runProductProvenanceVector_};
150  ProductProvenances* pResultsProductProvenanceVector_{
151  &resultsProductProvenanceVector_};
152  RootOutputTreePtrArray treePointers_;
153  bool dataTypeReported_{false};
154  std::array<ProductDescriptionsByID, NumBranchTypes> descriptionsToPersist_{
155  {}};
156  std::unique_ptr<cet::sqlite::Connection> rootFileDB_;
157  std::array<std::map<ProductID, OutputItem>, NumBranchTypes>
158  selectedOutputItemList_{{}};
159  DummyProductCache dummyProductCache_;
160  unsigned subRunRSID_{-1u};
161  unsigned runRSID_{-1u};
162  std::chrono::steady_clock::time_point beginTime_;
163 };
164 
165 } // namespace art
166 
167 // Local Variables:
168 // mode: c++
169 // End:
170 
171 #endif /* art_root_io_RootDAQOutFile_h */