#ifndef __JSUPPORT__JAUTOTREEWRITER__ #define __JSUPPORT__JAUTOTREEWRITER__ #include "JTools/JAutoMap.hh" #include "JROOT/JTreeWriter.hh" #include "JROOT/JRootFile.hh" /** * \author mdejong */ namespace JSUPPORT {} namespace JPP { using namespace JSUPPORT; } namespace JSUPPORT { using JTOOLS::JAutoMap; using JROOT::JTreeCopyWriterInterface; using JROOT::JRootOutputFile; /** * Auxiliary class to copy input data to corresponding TTree. * * This class derives from JROOT::JRootOutputFile. * It re-implements the methods open and close of the underlying JLANG::JAccessible class * so that each TTree is properly attached to and detached from the corresponding TDirectory, respectively. */ template class JAutoTreeWriter : public JAutoMap, public JRootOutputFile { public: typedef typename JAutoMap::map_type map_type; typedef typename JAutoMap::value_type value_type; typedef typename map_type::const_iterator const_iterator; typedef typename map_type::iterator iterator; typedef typename map_type::const_reverse_iterator const_reverse_iterator; typedef typename map_type::reverse_iterator reverse_iterator; using typename JAutoMap::insert; /** * Default constrtuctor. */ JAutoTreeWriter() {} /** * Constructor. * * \param type data type */ template JAutoTreeWriter(JType type) { this->insert(); } /** * Get key. * * This method should be overloaded for the requested data types. * * \param type data type * \return key */ template static JKey_t getKey(JType type); /** * Insert (list of) data type(s). */ template void insert() { insert(JTOOLS::JAutomate()); } /** * Open file. * * \param file_name file name */ virtual void open(const char* file_name) override { JRootOutputFile::open(file_name); if (is_open()) { for (iterator i = this->begin(); i != this->end(); ++i) { i->second->SetDirectory(getFile()); } } } /** * Close file. */ virtual void close() override { // Write objects in memory. if (is_open()) { getFile()->Write(); } // Detach TTree from TDirectory to avoid deletion of TTree. for (iterator i = this->begin(); i != this->end(); ++i) { i->second->SetDirectory(0); } // Close file. if (is_open()) { getFile()->Close(); } reset(); // Reset TTree for next run. for (iterator i = this->begin(); i != this->end(); ++i) { i->second->Reset(); } } protected: /** * Auxiliary class for element insertion. */ struct JElement_t : public value_type { /** * Constructor. * * \param type data type */ template JElement_t(JType type) : value_type(getKey(type), &JROOT::getTreeCopyWriter()) {} }; }; } #endif