RootDataConnection.h
Go to the documentation of this file.
1 #ifndef GAUDIROOT_ROOTDATACONNECTION_H
2 #define GAUDIROOT_ROOTDATACONNECTION_H
3 
4 // Framework include files
5 #include "GaudiKernel/ClassID.h"
6 #include "GaudiKernel/IIncidentSvc.h"
7 #include "GaudiKernel/SmartIF.h"
8 #include "GaudiUtils/IIODataManager.h"
9 #include <string>
10 #include <vector>
11 #include <map>
12 #include <set>
13 
14 #include "TFile.h"
15 #include "TTreePerfStats.h"
16 #include "RootCnv/RootRefs.h"
17 
18 // Forward declarations
19 class TTree;
20 class TClass;
21 class TBranch;
22 
23 class MsgStream;
24 class IRegistry;
25 class DataObject;
26 class IIncidentSvc;
27 
28 /*
29  * Gaudi namespace declaration
30  */
31 namespace Gaudi {
32 
43  class GAUDI_API RootConnectionSetup {
44  public:
46  typedef std::vector<std::string> StringVec;
47  protected:
49  virtual ~RootConnectionSetup() =default;
51  int refCount = 1;
53  std::unique_ptr<MsgStream> m_msgSvc;
55  SmartIF<IIncidentSvc> m_incidentSvc = nullptr;
56 
57  public:
59  StringVec cacheBranches;
61  StringVec vetoBranches;
63  std::string loadSection;
65  int cacheSize;
67  int learnEntries;
68 
70  RootConnectionSetup() = default;
72  void addRef();
74  void release();
75 
77  static long setCompression(const std::string& compression);
79  static int compression();
80 
82  void setMessageSvc(MsgStream* m);
84  MsgStream& msgSvc() const { return *m_msgSvc; }
85 
87  void setIncidentSvc(IIncidentSvc* m);
89  IIncidentSvc* incidentSvc() const { return m_incidentSvc.get(); }
90  };
91 
100  class GAUDI_API RootDataConnection : virtual public Gaudi::IDataConnection {
101  public:
102 
103  enum { ROOT_READ_ERROR = 0x2,
104  ROOT_OPEN_ERROR = 0x4
105  };
106 
117  struct ContainerSection {
119  ContainerSection() : start(-1), length(0) {}
121  ContainerSection(int s, int l) : start(s), length(l) {}
123  ContainerSection(const ContainerSection& s) : start(s.start), length(s.length) {}
126  if ( this != &s ) {
127  start=s.start;
128  length=s.length;
129  }
130  return *this;
131  }
133  int start;
135  int length;
136  };
137 
139  typedef std::vector<std::string> StringVec;
141  typedef std::vector<std::pair<std::string,std::string> > ParamMap;
143  typedef std::map<std::string,TTree*> Sections;
145  typedef std::vector<ContainerSection> ContainerSections;
147  typedef std::map<std::string,ContainerSections> MergeSections;
149  typedef std::vector<RootRef> LinkSections;
151  typedef std::set<const IInterface*> Clients;
152 
154  MsgStream& msgSvc() const { return m_setup->msgSvc(); }
155 
156 
157  protected:
161  std::unique_ptr<TTreePerfStats> m_statistics;
163  std::unique_ptr<TFile> m_file;
165  TTree *m_refs = nullptr;
167  Sections m_sections;
169  StringVec m_dbs;
171  StringVec m_conts;
173  StringVec m_links;
175  StringVec m_mergeFIDs;
177  ParamMap m_params;
179  MergeSections m_mergeSects;
181  LinkSections m_linkSects;
183  Clients m_clients;
185  std::string m_empty;
186 
188  const std::string& empty() const;
189 
191  StatusCode saveRefs();
192 
193  public:
203  class Tool {
204  protected:
212 
215  public:
216  TTree* refs() const { return c->m_refs; }
217  StringVec& dbs() const { return c->m_dbs; }
218  StringVec& conts() const { return c->m_conts; }
219  StringVec& links() const { return c->m_links; }
220  ParamMap& params() const { return c->m_params; }
221  MsgStream& msgSvc() const { return c->msgSvc(); }
222  const std::string& name() const { return c->m_name; }
223  Sections& sections() const { return c->m_sections; }
224  LinkSections& linkSections() const { return c->m_linkSects; }
225  MergeSections& mergeSections() const { return c->m_mergeSects; }
226 
228  virtual ~Tool() = default;
230  virtual TBranch* getBranch(const std::string& section, const std::string& n) = 0;
232  virtual RootRef poolRef(size_t /* which */) const { return RootRef(); }
233 
235  virtual StatusCode readRefs() = 0;
237  virtual StatusCode saveRefs() = 0;
239  virtual int loadRefs(const std::string& section, const std::string& cnt, unsigned long entry, RootObjectRefs& refs) = 0;
240  };
241  std::unique_ptr<Tool> m_tool;
242  friend class Tool;
243 
245  Tool* makeTool();
246 
247  public:
248 
250  RootDataConnection(const IInterface* own, const std::string& nam, RootConnectionSetup* setup);
252  ~RootDataConnection() override = default;
253 
255  TFile* file() const { return m_file.get(); }
257  virtual bool isConnected() const { return bool(m_file); }
259  bool isWritable() const { return m_file && m_file->IsWritable(); }
261  Tool* tool() const { return m_tool.get(); }
263  const MergeSections& mergeSections() const { return m_mergeSects; }
265  const StringVec& mergeFIDs() const { return m_mergeFIDs; }
266 
267 
269  void addClient(const IInterface* client);
271  size_t removeClient(const IInterface* client);
273  bool lookupClient(const IInterface* client) const;
274 
276  void badWriteError(const std::string& msg) const;
277 
279  std::pair<const RootRef*,const ContainerSection*> getMergeSection(const std::string& container, int entry) const;
280 
282  void enableStatistics(const std::string& section);
284  void saveStatistics(const std::string& statisticsFile);
285 
287  int loadObj(const std::string& section, const std::string& cnt, unsigned long entry, DataObject*& pObj);
288 
290  int loadRefs(const std::string& section, const std::string& cnt, unsigned long entry, RootObjectRefs& refs);
291 
293  std::pair<int,unsigned long> saveObj(const std::string& section,const std::string& cnt, TClass* cl, DataObject* pObj, int buff_siz, int split_lvl,bool fill_missing=false);
295  std::pair<int,unsigned long> save(const std::string& section,const std::string& cnt, TClass* cl, void* pObj, int buff_siz, int split_lvl,bool fill_missing=false);
296 
297 
299  virtual StatusCode connectRead();
301  virtual StatusCode connectWrite(IoType typ);
303  virtual StatusCode disconnect();
305  virtual StatusCode read(void* const, size_t) { return StatusCode::FAILURE; }
307  virtual StatusCode write(const void*, int) { return StatusCode::FAILURE; }
309  virtual long long int seek(long long int, int) { return -1; }
310 
312  TTree* getSection(const std::string& sect, bool create=false);
313 
315  TBranch* getBranch(const std::string& section,const std::string& branch_name)
316  { return m_tool->getBranch(section,branch_name); }
318  TBranch* getBranch(const std::string& section, const std::string& branch_name, TClass* cl, void* ptr, int buff_siz, int split_lvl);
319 
321  void makeRef(IRegistry* pA, RootRef& ref);
323  void makeRef(const std::string& name, long clid, int tech, const std::string& db, const std::string& cnt, int entry, RootRef& ref);
324 
326  int makeLink(const std::string& p);
327 
329  const std::string& getDb(int which) const;
330 
332  const std::string& getCont(int which) const
333  { return (which>=0)&&(size_t(which)<m_conts.size()) ? *(m_conts.begin()+which) : empty(); }
334 
336  const std::string& getLink(int which) const
337  { return (which>=0)&&(size_t(which)<m_links.size()) ? *(m_links.begin()+which) : empty(); }
338  };
339 } // End namespace Gaudi
340 #endif // GAUDIROOT_ROOTDATACONNECTION_H
tuple c
Definition: gaudirun.py:392
const std::string & name() const
MergeSections m_mergeSects
Database section map for merged files.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
const MergeSections & mergeSections() const
Access merged data section inventory.
virtual long long int seek(long long int, int)
Seek on the file described by ioDesc. Arguments as in ::seek()
RootDataConnection::Sections Sections
std::vector< ContainerSection > ContainerSections
Definition of container sections to handle merged files.
std::string m_name
Connection name/identifier.
TFile * file() const
Direct access to TFile structure.
virtual RootRef poolRef(size_t) const
Internal overload to facilitate the access to POOL files.
#define GAUDI_API
Definition: Kernel.h:107
RootDataConnection::StringVec StringVec
std::set< const IInterface * > Clients
Client set.
MsgStream & msgSvc() const
Retrieve message service.
const std::string & getLink(int which) const
Access link name from saved index.
std::vector< std::string > StringVec
Type definition for string maps.
RootDataConnection::ContainerSections ContainerSections
Persistent reference object containing all leafs and links corresponding to a Gaudi DataObject...
Definition: RootRefs.h:71
StringVec m_links
Map containing internal links names.
ParamMap m_params
Parameter map for file parameters.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
MergeSections & mergeSections() const
Persistent reference object.
Definition: RootRefs.h:30
virtual bool isConnected() const
Check if connected to data source.
TTree * m_refs
Pointer to the reference tree.
Helper class to facilitate an abstraction layer for reading POOL style files with this package...
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
constexpr double m
Definition: SystemOfUnits.h:93
Definition of the basic interface.
Definition: IInterface.h:234
TBranch * getBranch(const std::string &section, const std::string &branch_name)
Access data branch by name: Get existing branch in read only mode.
RootDataConnection::ParamMap ParamMap
StringVec m_conts
Map containing external container names.
Sections m_sections
Tree sections in TFile.
std::vector< std::string > StringVec
Type definition for string maps.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
std::vector< RootRef > LinkSections
Link sections definition.
RootDataConnection::MergeSections MergeSections
const std::string & getCont(int which) const
Access container name from saved index.
Tool * tool() const
Access tool.
ContainerSection(int s, int l)
Initializing constructor.
dictionary l
Definition: gaudirun.py:422
const StringVec & mergeFIDs() const
Access merged FIDs.
ContainerSection & operator=(const ContainerSection &s)
Assignment operator to copy objects.
RootDataConnection::ContainerSection ContainerSection
bool isWritable() const
Is the file writable?
virtual StatusCode read(void *const, size_t)
Read root byte buffer from input stream.
ABC describing basic data connection.
std::vector< std::pair< std::string, std::string > > ParamMap
Type definition for the parameter map.
RootDataConnection::LinkSections LinkSections
IIncidentSvc * incidentSvc() const
Retrieve incident service.
LinkSections m_linkSects
Database link sections.
def which(executable)
Definition: BaseTest.py:547
MsgStream & msgSvc() const
Allow access to printer service.
string s
Definition: gaudirun.py:246
ContainerSection(const ContainerSection &s)
Copy constructor.
virtual StatusCode write(const void *, int)
Write root byte buffer to output stream.
std::map< std::string, ContainerSections > MergeSections
Definition of database section to handle merged files.
StringVec m_dbs
Map containing external database file names (fids)
std::map< std::string, TTree * > Sections
Definition of tree sections.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Concrete implementation of the IDataConnection interface to access ROOT files.
Helper functions to set/get the application return code.
Definition: __init__.py:1
Internal helper class, which described a TBranch section in a ROOT file.
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:21
tuple start
Definition: IOTest.py:88
LinkSections & linkSections() const