#ifndef __JROOTFILEWRITER__ #define __JROOTFILEWRITER__ #include "TObject.h" #include "TFile.h" #include "JLang/JObjectOutput.hh" #include "JLang/JTypeList.hh" #include "JLang/JNullType.hh" #include "JLang/JException.hh" #include "JLang/JConversion.hh" #include "JLang/JAssert.hh" #include "JROOT/JTreeWriterObjectOutput.hh" #include "JROOT/JTreeParameters.hh" #include "JROOT/JRootFile.hh" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { using JLANG::JObjectOutput; using JLANG::JAccessibleObjectOutput; using JLANG::JTypeList; using JLANG::JNullType; using JLANG::JConversion; using JLANG::JAssert; /** * Write object to ROOT directory. * * \param dir directory * \param object object * \return true if OK; else false */ inline bool putObject(TDirectory& dir, const TObject& object) { return dir.WriteTObject(&object) > 0; } /** * Write object to ROOT directory. * * \param dir directory * \param object object * \return true if OK; else false */ template inline typename JAssert::is_derived, bool>::type putObject(TDirectory& dir, const T& object) { return dir.WriteObject(&object, T::Class_Name()) > 0; } /** * Write object to ROOT directory. * * \param dir pointer to directory * \param object object * \return true if OK; else false */ inline bool putObject(TDirectory* dir, const TObject& object) { return (dir != NULL && putObject(*dir, object)); } /** * Write object to ROOT directory. * * \param dir pointer to directory * \param object object * \return true if OK; else false */ template inline typename JAssert::is_derived, bool>::type putObject(TDirectory* dir, const T& object) { return (dir != NULL && putObject(*dir, object)); } /** * Template definition of ROOT object output. */ template::result> class JRootObjectOutput; /** * Implementation of object output using TDirectory::WriteTObject method. * * This class implements the JLANG::JObjectOutput interface. */ template class JRootObjectOutput : public virtual JObjectOutput { protected: /** * Default constructor. */ JRootObjectOutput() : directory(NULL) {} /** * Set directory. * * \param dir pointer to directory */ void SetDirectory(TDirectory* dir) { this->directory = dir; } TDirectory* directory; public: /** * Object output. * * \param object object * \return true if OK; else false */ virtual bool put(const T& object) override { if (directory != NULL) return putObject(directory, object); else return false; } }; /** * Implementation of object output using TTree. * * This class implements the JLANG::JObjectOutput interface. */ template class JRootObjectOutput : public JTreeWriterObjectOutput {}; /** * Implementation of object output to ROOT file for single data type. */ template class JRootFileObjectOutput : public JRootObjectOutput {}; /** * Implementation of object output to ROOT file for multiple data types. * * This class recursively implements the JLANG::JObjectOutput interface * for all data types by deriving from: * - JRootFileObjectOutput; and * - JRootFileObjectOutput. */ template class JRootFileObjectOutput< JTypeList > : public JObjectOutput< JTypeList >, public JRootFileObjectOutput, public JRootFileObjectOutput { protected: /** * Set directory. * * \param dir pointer to directory */ void SetDirectory(TDirectory* dir) { JRootFileObjectOutput::SetDirectory(dir); JRootFileObjectOutput::SetDirectory(dir); } }; /** * Terminator class of recursive JRootObjectOutput class. */ template class JRootFileObjectOutput< JTypeList > : public JRootFileObjectOutput {}; /** * ROOT file object output. * * This class implements the JLANG::JAccessibleObjectOutput interface. */ template class JRootFileWriter : public JRootFileObjectOutput , public JAccessibleObjectOutput, public JRootOutputFile { public: /** * Default constructor. */ JRootFileWriter() : JRootFileObjectOutput (), JAccessibleObjectOutput() {} /** * Open file. * * \param file_name file name */ virtual void open(const char* file_name) override { JRootOutputFile::open(file_name); if (is_open()) { this->SetDirectory(getFile()); } } }; } #endif