Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-17 22:59:07

0001 #include "RntDumper.h"
0002 
0003 #include <ROOT/RNTuple.hxx>
0004 #include <ROOT/RNTupleModel.hxx>
0005 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 32, 0)
0006 #include <ROOT/RNTupleWriter.hxx>
0007 #endif
0008 
0009 #include "TFile.h"
0010 #include "TTree.h"
0011 
0012 #include <vector>
0013 
0014 namespace REX = ROOT::Experimental;
0015 
0016 std::vector<RntDumper *> RntDumper::s_instances;
0017 
0018 RntDumper::RntDumper(const char *fname) : m_file(TFile::Open(fname, "recreate")) {
0019   if (!m_file || !m_file->IsOpen()) {
0020     printf("RntDumper::RntDumper() failed creeating file '%s'.\n", fname);
0021     throw std::runtime_error("Failed creating file");
0022   }
0023   printf("RntDumper::RntDumper() succesfully opened file '%s'.\n", fname);
0024 }
0025 
0026 RntDumper::~RntDumper() {
0027   printf("RntDumper::~RntDumper() destroying writers and closing file '%s'.\n", m_file->GetName());
0028   // Finish up trees first, ntuple-writers seem to write everything reulting
0029   // in two cycles of trees.
0030   for (auto &tp : m_trees) {
0031     tp->Write();
0032     delete tp;
0033   }
0034   m_trees.clear();
0035   m_writers.clear();
0036   if (m_file) {
0037     m_file->Close();
0038   }
0039 }
0040 
0041 std::unique_ptr<REX::RNTupleModel> RntDumper::CreateModel() { return RNTupleModel::Create(); }
0042 
0043 REX::RNTupleWriter *RntDumper::WritifyModel(std::unique_ptr<REX::RNTupleModel> &model, std::string_view mname) {
0044   auto wup = RNTupleWriter::Append(std::move(model), mname, *m_file);
0045   REX::RNTupleWriter *w = wup.get();
0046   m_writers.insert({std::string(mname), std::move(wup)});
0047   return w;
0048 }
0049 
0050 void RntDumper::RegisterTree(TTree *t) { m_trees.push_back(t); }
0051 
0052 // === static ===
0053 
0054 RntDumper *RntDumper::Create(const char *fname) {
0055   // Should check fnames ?
0056   RntDumper *d = new RntDumper(fname);
0057   s_instances.push_back(d);
0058   return d;
0059 }
0060 
0061 void RntDumper::FinalizeAll() {
0062   printf("RntDumper::FinalizeAll() shutting down %d instances.\n", (int)s_instances.size());
0063   for (auto &d : s_instances)
0064     delete d;
0065 }