#ifndef __JROOT__JTREEREADEROBJECTITERATOR__ #define __JROOT__JTREEREADEROBJECTITERATOR__ #include "TFile.h" #include "TError.h" #include "JLang/JObjectIterator.hh" #include "JROOT/JTreeReader.hh" #include "JROOT/JTreeParameters.hh" #include "JROOT/JCounter.hh" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { using JLANG::JRewindableObjectIterator; using JLANG::JAccessibleObjectIterator; using JLANG::skip_type; /** * JTreeReader object iterator. * * This class implements the JLANG::JRewindableObjectIterator interface. */ template class JTreeReaderObjectIterator : public virtual JRewindableObjectIterator, public JTreeReader { public: typedef typename JRewindableObjectIterator::pointer_type pointer_type; /** * Default constructor. */ JTreeReaderObjectIterator() : JTreeReader(getTreeParameters()), counter(0) { gErrorIgnoreLevel = kError; } /** * Rewind. */ virtual void rewind() override { counter = 0; } /** * Check availability of next element. * * \return true if the iteration has more elements. */ virtual bool hasNext() override { if (this->is_valid()) return counter < this->get()->GetEntries(); else return false; } /** * Get next element. * * \return pointer to element. */ virtual const pointer_type& next() override { static pointer_type ps; if (hasNext()) { this->get()->GetEvent(counter++); ps.reset(this->getAddress()); } else { ps.reset(NULL); } return ps; } /** * Skip items. * * \param ns number of items to skip * \return number of items skipped */ virtual skip_type skip(const skip_type ns) override { if (this->is_valid()) { return advance(counter, ns, this->get()->GetEntries()); } return 0; } /** * Get internal counter. * * \return counter */ counter_type getCounter() const { return counter; } protected: counter_type counter; }; /** * JTemplateTreeReader object iterator. * * This class extends the JTreeReaderObjectIterator class and * implements the JLANG::JAccessibleObjectIterator interface. */ template class JTreeReaderAccessibleObjectIterator : public JAccessibleObjectIterator, public JTreeReaderObjectIterator { public: /** * Default constructor. */ JTreeReaderAccessibleObjectIterator() {} /** * Check is file is open. * * \return true if open; else false */ virtual bool is_open() const override { if (this->is_valid()) { const TFile* file = this->get()->GetCurrentFile(); return (file != NULL && file->IsOpen()); } return false; } /** * Open file. * * \param file_name file name */ virtual void open(const char* file_name) override { TFile* file = TFile::Open(file_name); if (file != NULL) { if (!this->load(file)) { if (this->is_valid()) { this->get()->SetDirectory(0); } file->Close(); delete file; } } this->rewind(); } /** * Close file. */ virtual void close() override { if (this->is_valid()) { TFile* file = this->get()->GetCurrentFile(); if (file != NULL) { if (this->is_valid()) { this->get()->SetDirectory(0); } file->Close(); delete file; } } this->reset(); } }; } #endif