CMS 3D CMS Logo

RntDumper.cc
Go to the documentation of this file.
1 #include "RntDumper.h"
2 
3 #include <ROOT/RNTuple.hxx>
4 #include <ROOT/RNTupleModel.hxx>
5 
6 #include "TFile.h"
7 #include "TTree.h"
8 
9 #include <vector>
10 
11 namespace REX = ROOT::Experimental;
12 
13 std::vector<RntDumper *> RntDumper::s_instances;
14 
15 RntDumper::RntDumper(const char *fname) : m_file(TFile::Open(fname, "recreate")) {
16  if (!m_file || !m_file->IsOpen()) {
17  printf("RntDumper::RntDumper() failed creeating file '%s'.\n", fname);
18  throw std::runtime_error("Failed creating file");
19  }
20  printf("RntDumper::RntDumper() succesfully opened file '%s'.\n", fname);
21 }
22 
24  printf("RntDumper::~RntDumper() destroying writers and closing file '%s'.\n", m_file->GetName());
25  // Finish up trees first, ntuple-writers seem to write everything reulting
26  // in two cycles of trees.
27  for (auto &tp : m_trees) {
28  tp->Write();
29  delete tp;
30  }
31  m_trees.clear();
32  m_writers.clear();
33  if (m_file) {
34  m_file->Close();
35  }
36 }
37 
38 std::unique_ptr<REX::RNTupleModel> RntDumper::CreateModel() { return RNTupleModel::Create(); }
39 
40 REX::RNTupleWriter *RntDumper::WritifyModel(std::unique_ptr<REX::RNTupleModel> &model, std::string_view mname) {
41  auto wup = RNTupleWriter::Append(std::move(model), mname, *m_file);
42  REX::RNTupleWriter *w = wup.get();
43  m_writers.insert({std::string(mname), std::move(wup)});
44  return w;
45 }
46 
47 void RntDumper::RegisterTree(TTree *t) { m_trees.push_back(t); }
48 
49 // === static ===
50 
52  // Should check fnames ?
53  RntDumper *d = new RntDumper(fname);
54  s_instances.push_back(d);
55  return d;
56 }
57 
59  printf("RntDumper::FinalizeAll() shutting down %d instances.\n", (int)s_instances.size());
60  for (auto &d : s_instances)
61  delete d;
62 }
~RntDumper()
Definition: RntDumper.cc:23
std::unique_ptr< TFile > m_file
Definition: RntDumper.h:36
std::unordered_map< std::string, std::unique_ptr< RNTupleWriter > > m_writers
Definition: RntDumper.h:37
T w() const
RntDumper(const char *fname)
Definition: RntDumper.cc:15
static std::vector< RntDumper * > s_instances
Definition: RntDumper.h:40
RNTupleWriter * WritifyModel(std::unique_ptr< RNTupleModel > &model, std::string_view mname)
Definition: RntDumper.cc:40
static void FinalizeAll()
Definition: RntDumper.cc:58
d
Definition: ztail.py:151
std::unique_ptr< RNTupleModel > CreateModel()
Definition: RntDumper.cc:38
std::vector< TTree * > m_trees
Definition: RntDumper.h:38
string fname
main script
static RntDumper * Create(const char *fname)
Definition: RntDumper.cc:51
void RegisterTree(TTree *t)
Definition: RntDumper.cc:47
def move(src, dest)
Definition: eostools.py:511