ATLAS Offline Software
RNTupleWriterHelper.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef RNTUPLEWRITERHELPER_H
6 #define RNTUPLEWRITERHELPER_H
7 
9 #include "ROOT/REntry.hxx"
10 #include "ROOT/RField.hxx"
11 #include "ROOT/RNTuple.hxx"
12 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 31, 0)
13 #include "ROOT/RNTupleWriteOptions.hxx"
14 #include "ROOT/RNTupleWriter.hxx"
15 #else
16 #include "ROOT/RNTupleOptions.hxx"
17 #endif
18 
19 #include <tuple>
20 
21 namespace ROOT::Experimental {
22 class RNTupleModel;
23 }
24 
25 namespace RootStorageSvc {
26 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 31, 0)
28 #else
30 #endif
35 
37  public:
39  RNTupleWriterHelper(TFile* file, const std::string& ntupleName,
40  bool enableBufferedWrite, bool enableMetrics);
41 
43  ~RNTupleWriterHelper() = default;
44 
46  void makeNewEntry();
47 
49  // The convention for the tuple is <name, type, data>
50  typedef std::tuple<std::string, std::string, void*> attrDataTuple;
51  void addAttribute(const attrDataTuple& in);
52 
54  void addField(const std::string& field_name, const std::string& attr_type);
55 
57  void addFieldValue(const std::string& field_name, void* attr_data);
58 
60  int commit();
61 
63  const std::string& getName() const { return m_ntupleName; }
64 
66  size_t size() const { return m_rowN; }
67 
69  bool needsCommit() const { return m_needsCommit; }
70 
72  bool isGrouped() const { return m_clients > 1; }
73 
76 
78  void close();
79 
80  private:
82  std::map<std::string, void*> m_attrDataMap;
83 
88  std::unique_ptr<RNTupleModel> m_model;
89 
91  std::unique_ptr<REntry> m_entry;
92 
94  std::unique_ptr<RNTupleWriter> m_ntupleWriter;
95 
96  std::string m_ntupleName;
97  TFile* m_tfile;
99  int m_rowN = 0;
100 
103  int m_clients = 0;
104  bool m_needsCommit = false;
105 
108 };
109 
110 } // namespace RootStorageSvc
111 #endif
RootStorageSvc::RNTupleWriterHelper::close
void close()
Close the writer.
RootStorageSvc::RNTupleWriterHelper::~RNTupleWriterHelper
~RNTupleWriterHelper()=default
Default Destructor.
RootStorageSvc::RNTupleWriterHelper::m_ntupleName
std::string m_ntupleName
Definition: RNTupleWriterHelper.h:96
RootStorageSvc::RNTupleWriterHelper::isGrouped
bool isGrouped() const
Is this RNTuple used by more than one APR container?
Definition: RNTupleWriterHelper.h:72
RootStorageSvc::RNTupleWriterHelper::m_model
std::unique_ptr< RNTupleModel > m_model
Internal cache for the RNTuple model Before first commit the fields are added to the model At the fir...
Definition: RNTupleWriterHelper.h:88
RootStorageSvc::RNTupleWriterHelper::addField
void addField(const std::string &field_name, const std::string &attr_type)
Add a new field to the RNTuple.
RootStorageSvc::RNTupleModel
ROOT::Experimental::RNTupleModel RNTupleModel
Definition: RNTupleWriterHelper.h:32
RootStorageSvc::RNTupleWriterHelper::addFieldValue
void addFieldValue(const std::string &field_name, void *attr_data)
Supply data address for a given field.
RootStorageSvc::RNTupleWriterHelper::needsCommit
bool needsCommit() const
Check if any data needs to be committed.
Definition: RNTupleWriterHelper.h:69
RootStorageSvc::RNTupleWriterHelper::m_attrDataMap
std::map< std::string, void * > m_attrDataMap
Store data ptr for the first row, when only creating the model.
Definition: RNTupleWriterHelper.h:82
RootStorageSvc::RNTupleWriter
ROOT::Experimental::RNTupleWriter RNTupleWriter
Definition: RNTupleWriterHelper.h:31
RootStorageSvc::RNTupleWriterHelper::attrDataTuple
std::tuple< std::string, std::string, void * > attrDataTuple
Add a new field to the RNTuple, collect the data pointer for the commit.
Definition: RNTupleWriterHelper.h:50
RootStorageSvc::RNTupleWriterHelper::m_needsCommit
bool m_needsCommit
Definition: RNTupleWriterHelper.h:104
RootStorageSvc::RNTupleWriterHelper::m_entry
std::unique_ptr< REntry > m_entry
Internal cache for the RNEntry.
Definition: RNTupleWriterHelper.h:91
RootStorageSvc::RFieldBase
ROOT::Experimental::RFieldBase RFieldBase
Definition: RNTupleWriterHelper.h:27
RootStorageSvc::RNTupleWriteOptions
ROOT::Experimental::RNTupleWriteOptions RNTupleWriteOptions
Definition: RNTupleWriterHelper.h:34
RootStorageSvc::RNTupleWriterHelper::commit
int commit()
Commit the data.
RootStorageSvc::RNTupleWriterHelper::increaseClientCount
void increaseClientCount()
Keep track of how many APR containers are writing to this RNTuple.
Definition: RNTupleWriterHelper.h:75
RootStorageSvc::RNTupleWriterHelper::getName
const std::string & getName() const
Name of the RNTuple.
Definition: RNTupleWriterHelper.h:63
RootStorageSvc::RNTupleWriterHelper::m_rowN
int m_rowN
Definition: RNTupleWriterHelper.h:99
RootStorageSvc::RNTupleWriterHelper::addAttribute
void addAttribute(const attrDataTuple &in)
file
TFile * file
Definition: tile_monitor.h:29
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
RootStorageSvc::RNTupleWriterHelper::m_collectMetrics
bool m_collectMetrics
Enable/Disable Metric Collection.
Definition: RNTupleWriterHelper.h:107
RootStorageSvc::RNTupleWriterHelper
Definition: RNTupleWriterHelper.h:36
AthMessaging.h
RootStorageSvc::REntry
ROOT::Experimental::REntry REntry
Definition: RNTupleWriterHelper.h:33
RootStorageSvc::RNTupleWriterHelper::m_clients
int m_clients
Count how many APR Containers are writing to this RNTuple (more than one makes a Group)
Definition: RNTupleWriterHelper.h:103
RootStorageSvc::RNTupleWriterHelper::m_opts
RNTupleWriteOptions m_opts
Definition: RNTupleWriterHelper.h:98
ROOT::Experimental
Definition: RNTCollection.h:27
RootStorageSvc::RNTupleWriterHelper::m_ntupleWriter
std::unique_ptr< RNTupleWriter > m_ntupleWriter
Internal cache for the native RNTupleWriter.
Definition: RNTupleWriterHelper.h:94
RootStorageSvc::RNTupleWriterHelper::size
size_t size() const
Size of the RNTuple.
Definition: RNTupleWriterHelper.h:66
RootStorageSvc
Definition: RNTupleContainer.h:26
RootStorageSvc::RNTupleWriterHelper::RNTupleWriterHelper
RNTupleWriterHelper(TFile *file, const std::string &ntupleName, bool enableBufferedWrite, bool enableMetrics)
Constructor.
RootStorageSvc::RNTupleWriterHelper::m_tfile
TFile * m_tfile
Definition: RNTupleWriterHelper.h:97
RootStorageSvc::RNTupleWriterHelper::makeNewEntry
void makeNewEntry()
Create a new empty RNTuple row with the current model (fields)