00001
00002 #ifndef GAUDIROOT_ROOTDATACONNECTION_H
00003 #define GAUDIROOT_ROOTDATACONNECTION_H
00004
00005
00006 #include "GaudiKernel/ClassID.h"
00007 #include "GaudiUtils/IIODataManager.h"
00008 #include <string>
00009 #include <vector>
00010 #include <map>
00011 #include <set>
00012
00013 #include "TFile.h"
00014 #include "RootCnv/RootRefs.h"
00015
00016
00017 class TTree;
00018 class TClass;
00019 class TBranch;
00020 class TTreePerfStats;
00021
00022 class MsgStream;
00023 class IRegistry;
00024 class DataObject;
00025
00026
00027
00028
00029 namespace Gaudi {
00030
00041 class GAUDI_API RootConnectionSetup {
00042 public:
00044 typedef std::vector<std::string> StringVec;
00045 protected:
00047 virtual ~RootConnectionSetup();
00049 int refCount;
00051 MsgStream* m_msgSvc;
00052
00053 public:
00055 StringVec cacheBranches;
00057 StringVec vetoBranches;
00059 std::string loadSection;
00061 int cacheSize;
00063 int learnEntries;
00065 int autoFlush;
00067 int basketSize;
00068
00070 RootConnectionSetup();
00072 void addRef();
00074 void release();
00076 void setMessageSvc(MsgStream* m);
00078 MsgStream& msgSvc() const { return *m_msgSvc; }
00079 };
00080
00089 class GAUDI_API RootDataConnection : virtual public Gaudi::IDataConnection {
00090 public:
00091
00102 struct ContainerSection {
00104 ContainerSection() : start(-1), length(0) {}
00106 ContainerSection(int s, int l) : start(s), length(l) {}
00108 ContainerSection(const ContainerSection& s) : start(s.start), length(s.length) {}
00110 ContainerSection& operator=(const ContainerSection& s) { start=s.start;length=s.length; return *this;}
00112 int start;
00114 int length;
00115 };
00116
00118 typedef std::vector<std::string> StringVec;
00120 typedef std::vector<std::pair<std::string,std::string> > ParamMap;
00122 typedef std::map<std::string,TTree*> Sections;
00124 typedef std::vector<ContainerSection> ContainerSections;
00126 typedef std::map<std::string,ContainerSections> MergeSections;
00128 typedef std::vector<RootRef> LinkSections;
00130 typedef std::set<const IInterface*> Clients;
00131
00133 MsgStream& msgSvc() const { return m_setup->msgSvc(); }
00134
00135
00136 protected:
00138 RootConnectionSetup* m_setup;
00140 TTreePerfStats* m_statistics;
00142 TFile* m_file;
00144 TTree *m_refs;
00146 Sections m_sections;
00148 StringVec m_dbs;
00150 StringVec m_conts;
00152 StringVec m_links;
00154 StringVec m_mergeFIDs;
00156 ParamMap m_params;
00158 MergeSections m_mergeSects;
00160 LinkSections m_linkSects;
00162 Clients m_clients;
00164 std::string m_empty;
00165
00167 const std::string& empty() const;
00168
00170 StatusCode saveRefs();
00171
00172 public:
00182 class Tool {
00183 protected:
00184 typedef RootDataConnection::StringVec StringVec;
00185 typedef RootDataConnection::ParamMap ParamMap;
00186 typedef RootDataConnection::Sections Sections;
00187 typedef RootDataConnection::MergeSections MergeSections;
00188 typedef RootDataConnection::LinkSections LinkSections;
00189 typedef RootDataConnection::ContainerSection ContainerSection;
00190 typedef RootDataConnection::ContainerSections ContainerSections;
00191
00193 RootDataConnection* c;
00194 public:
00195 TTree* refs() const { return c->m_refs; }
00196 StringVec& dbs() const { return c->m_dbs; }
00197 StringVec& conts() const { return c->m_conts; }
00198 StringVec& links() const { return c->m_links; }
00199 ParamMap& params() const { return c->m_params; }
00200 MsgStream& msgSvc() const { return c->msgSvc(); }
00201 const std::string& name() const { return c->m_name; }
00202 Sections& sections() const { return c->m_sections; }
00203 LinkSections& linkSections() const { return c->m_linkSects; }
00204 MergeSections& mergeSections() const { return c->m_mergeSects; }
00205
00207 virtual ~Tool() {}
00209 virtual void release() { delete this; }
00211 virtual TBranch* getBranch(const std::string& section, const std::string& n) = 0;
00213 virtual RootRef poolRef(size_t ) const { return RootRef(); }
00214
00216 virtual StatusCode readRefs() = 0;
00218 virtual StatusCode saveRefs() = 0;
00220 virtual int loadRefs(const std::string& section, const std::string& cnt, unsigned long entry, RootObjectRefs& refs) = 0;
00221 } *m_tool;
00222 friend class Tool;
00223
00225 Tool* makeTool();
00226
00227 public:
00228
00230 RootDataConnection(const IInterface* own, const std::string& nam, RootConnectionSetup* setup);
00232 virtual ~RootDataConnection();
00233
00235 TFile* file() const { return m_file; }
00237 virtual bool isConnected() const { return m_file != 0; }
00239 bool isWritable() const { return m_file != 0 && m_file->IsWritable(); }
00241 Tool* tool() const { return m_tool; }
00243 const MergeSections& mergeSections() const { return m_mergeSects; }
00245 const StringVec& mergeFIDs() const { return m_mergeFIDs; }
00246
00247
00249 void addClient(const IInterface* client);
00251 size_t removeClient(const IInterface* client);
00253 bool lookupClient(const IInterface* client) const;
00254
00256 std::pair<const RootRef*,const ContainerSection*> getMergeSection(const std::string& container, int entry) const;
00257
00259 void enableStatistics(const std::string& section);
00261 void saveStatistics(const std::string& statisticsFile);
00262
00264 int loadObj(const std::string& section, const std::string& cnt, unsigned long entry, DataObject*& pObj);
00265
00267 int loadRefs(const std::string& section, const std::string& cnt, unsigned long entry, RootObjectRefs& refs)
00268 { return m_tool->loadRefs(section,cnt,entry,refs); }
00269
00271 std::pair<int,unsigned long> saveObj(const std::string& section,const std::string& cnt, TClass* cl, DataObject* pObj,bool fill_missing=false);
00273 std::pair<int,unsigned long> save(const std::string& section,const std::string& cnt, TClass* cl, void* pObj,bool fill_missing=false);
00274
00275
00277 virtual StatusCode connectRead();
00279 virtual StatusCode connectWrite(IoType typ);
00281 virtual StatusCode disconnect();
00283 virtual StatusCode read(void* const, size_t) { return StatusCode::FAILURE; }
00285 virtual StatusCode write(const void*, int) { return StatusCode::FAILURE; }
00287 virtual long long int seek(long long int, int) { return -1; }
00288
00290 TTree* getSection(const std::string& sect, bool create=false);
00291
00293 TBranch* getBranch(const std::string& section,const std::string& branch_name)
00294 { return m_tool->getBranch(section,branch_name); }
00296 TBranch* getBranch(const std::string& section, const std::string& branch_name, TClass* cl, void* ptr);
00297
00299 void makeRef(IRegistry* pA, RootRef& ref);
00301 void makeRef(const std::string& name, long clid, int tech, const std::string& db, const std::string& cnt, int entry, RootRef& ref);
00302
00304 int makeLink(const std::string& p);
00305
00307 const std::string& getDb(int which) const;
00308
00310 const std::string& getCont(int which) const
00311 { return (which>=0)&&(size_t(which)<m_conts.size()) ? *(m_conts.begin()+which) : empty(); }
00312
00314 const std::string& getLink(int which) const
00315 { return (which>=0)&&(size_t(which)<m_links.size()) ? *(m_links.begin()+which) : empty(); }
00316 };
00317 }
00318 #endif // GAUDIROOT_ROOTDATACONNECTION_H