#ifndef __JROOTFILEREADER__ #define __JROOTFILEREADER__ #include #include #include "TFile.h" #include "TString.h" #include "JLang/JObjectIterator.hh" #include "JLang/JException.hh" #include "JLang/JType.hh" #include "JROOT/JTreeReaderObjectIterator.hh" #include "JROOT/JTreeParameters.hh" #include "JROOT/JRootFile.hh" #include "JROOT/JRootSupportkit.hh" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { using JLANG::JAccessibleObjectIterator; using JLANG::JRewindableObjectIterator; using JLANG::JType; using JLANG::skip_type; /** * Read object from ROOT file. * * \param file pointer to file * \param key key of object to be read * \param ps pointer to object */ template inline void getObject(TFile* file, const TString& key, T*& ps) { if (file != NULL) { file->GetObject(key, ps); } } /** * ROOT file reader. */ template::result> class JRootFileReader; /** * Tempate specialisation of JRootFileReader for TTree incompatible iteration. * * The methods JROOT::actionAtFileOpen and JROOT::actionAtFileRead are called * at opening of each file and reading of each object, respectively. * * This class implements the JLANG::JAccessibleObjectIterator interface. */ template class JRootFileReader : public JAccessibleObjectIterator, public JRewindableObjectIterator, public JRootInputFile { public: typedef typename JAccessibleObjectIterator::pointer_type pointer_type; /** * Default constructor. */ JRootFileReader() : cycleOld(0), cycleNew(1), __p (NULL), name(getName(JType())) {} /** * Constructor. * * \param file_name file name * \param key key of object to be read */ JRootFileReader(const char* file_name, const char* key = getName(JType())) : cycleOld(0), cycleNew(1), __p (NULL), name(key) { open(file_name); } /** * Rewind. */ virtual void rewind() override { cycleOld = 0; cycleNew = 1; } /** * Open file. * * \param file_name file name */ virtual void open(const char* file_name) override { JRootInputFile::open(file_name); actionAtFileOpen(this->getFile()); rewind(); } /** * Check availability of next element. * * \return true if the iteration has more elements; else false */ virtual bool hasNext() override { if (is_open()) { if (cycleOld != cycleNew) { TString key(name); key += ';'; key += cycleNew; cycleOld = cycleNew; __p = NULL; getObject(getFile(), key, __p); actionAtFileRead(__p); } return (cycleOld == cycleNew && __p != NULL); } return false; } /** * Get next element. * * \return pointer to element. */ virtual const pointer_type& next() override { static pointer_type ps; if (hasNext()) { ++cycleNew; ps.reset(__p); } 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 { using namespace std; if (ns < (skip_type) cycleNew + (skip_type) numeric_limits::max()) cycleNew += (Short_t) ns; else cycleNew = numeric_limits::max(); return cycleNew - cycleOld; } /** * Get cycle number of last read object. * * \return cycle number */ Short_t getCycle() const { return cycleOld; } protected: Short_t cycleOld; Short_t cycleNew; T* __p; TString name; }; /** * Tempate specialisation of JRootFileReader for TTree compatible iteration. * * The method JROOT::actionAtFileOpen is called at opening of each file. * * This class implements the JLANG::JAccessibleObjectIterator interface. */ template class JRootFileReader : public JTreeReaderAccessibleObjectIterator { /** * Open file. * * \param file_name file name */ virtual void open(const char* file_name) override { JTreeReaderAccessibleObjectIterator::open(file_name); if (this->get() != NULL) { actionAtFileOpen(this->get()->GetCurrentFile()); } } }; } #endif