MarlinMT  0.1.0
StoreWriter.cc
Go to the documentation of this file.
2 
3 // -- std includes
4 #include <filesystem>
5 #include <unordered_map>
6 #include <vector>
7 
8 // -- MarlinBook includes
10 #include "marlinmt/book/Entry.h"
11 #include "marlinmt/book/Handle.h"
12 #include "marlinmt/book/Hist.h"
14 #include "marlinmt/book/Types.h"
15 
16 
17 // -- ROOT includes
18 #include "TDirectory.h"
19 #include "TDirectoryFile.h"
20 #include "TFile.h"
21 
22 class PathHash {
23 public:
24  std::size_t operator()(const std::filesystem::path& path) const {
25  return std::filesystem::hash_value(path);
26  }
27 };
28 using DirectoryMap = std::unordered_map<std::filesystem::path, TDirectory*, PathHash>;
29 
30 template<typename T>
31 void writeObject( TDirectory* file, const std::string_view& name, const T& obj) {
32 
33  auto root6Obj = marlinmt::book::types::toRoot6(obj, name);
34 
35  if constexpr (!std::is_same_v<decltype(toRoot6(obj,name)), decltype(nullptr)>) {
36  file->WriteTObject(
37  &root6Obj,
38  std::string(name).c_str()
39  ) ;
40  }
41 }
42 
43 
44 
45 namespace marlinmt {
46  namespace book {
47 
48  void StoreWriter::writeSelection(
49  const Selection &selection
50  ) {
51  DirectoryMap dirs{};
52 
53  TFile root(_path.string().c_str(), "UPDATE");
54  if(root.IsZombie()) {
55  MARLIN_BOOK_THROW(std::string("failed to create file: ") + _path.string());
56  }
57  for(const WeakEntry& h : selection) {
58  if(!h.valid()) { continue; }
59  const EntryKey& key = h.key();
60  const std::type_index type = key.type;
61  std::string path = std::filesystem::relative(key.path, "/").remove_filename().string();
62  path.pop_back();
63 
64  const char* c_path = path.c_str();
65  TDirectory *file = root.GetDirectory(c_path);
66  if(file == nullptr) {
67  root.mkdir(c_path);
68  file = root.GetDirectory(c_path);
69  }
70 
71  if(file == nullptr) {
72  MARLIN_BOOK_THROW( std::string("failed create: ") + path + '\n');
73  }
74  if (type == std::type_index(typeid(types::H1F))) {
75  writeObject<types::H1F>(file, key.path.filename().string(), h.handle<types::H1F>().merged());
76  } else if (type == std::type_index(typeid(types::H1D))){
77  writeObject<types::H1D>(file, key.path.filename().string(), h.handle<types::H1D>().merged());
78  } else if (type == std::type_index(typeid(types::H1I))){
79  writeObject<types::H1I>(file, key.path.filename().string(), h.handle<types::H1I>().merged());
80  } else if (type == std::type_index(typeid(types::H2F))){
81  writeObject<types::H2F>(file, key.path.filename().string(), h.handle<types::H2F>().merged());
82  } else if (type == std::type_index(typeid(types::H2D))){
83  writeObject<types::H2D>(file, key.path.filename().string(), h.handle<types::H2D>().merged());
84  } else if (type == std::type_index(typeid(types::H2I))){
85  writeObject<types::H2I>(file, key.path.filename().string(), h.handle<types::H2I>().merged());
86  } else if (type == std::type_index(typeid(types::H3F))){
87  writeObject<types::H3F>(file, key.path.filename().string(), h.handle<types::H3F>().merged());
88  } else if (type == std::type_index(typeid(types::H3D))){
89  writeObject<types::H3D>(file, key.path.filename().string(), h.handle<types::H3D>().merged());
90  } else if (type == std::type_index(typeid(types::H3I))){
91  writeObject<types::H3I>(file, key.path.filename().string(), h.handle<types::H3I>().merged());
92  } else {
93  MARLIN_BOOK_THROW( "can't store object, no known operation" );
94  }
95  }
96  root.Close();
97 
98  }
99 
100  } // end namespace book
101 } // end namespace marlinmt
102 
Data selection to identify and manage an Entry.
Definition: EntryData.h:21
#define MARLIN_BOOK_THROW(message)
Definition: Types.h:10
std::filesystem::path path
virtual Entry path
Definition: EntryData.h:30
Generalized histogram class.
Definition: Base.h:151
Wrapper for weak pointer to Entry.
Definition: Selection.h:29
std::size_t operator()(const std::filesystem::path &path) const
Definition: StoreWriter.cc:24
void writeObject(TDirectory *file, const std::string_view &name, const T &obj)
Definition: StoreWriter.cc:31
auto toRoot6(const HistT< Config > &hist, const std::string_view &name)
convert histogram to Root-6 Object for serialization
Definition: Dummy.h:71
Contains references to entries.
Definition: Selection.h:75
std::unordered_map< std::filesystem::path, TDirectory *, PathHash > DirectoryMap
Definition: StoreWriter.cc:28
std::type_index type
Type of object stored in Entry.
Definition: EntryData.h:34