#include #include #include #include #include "km3net-dataformat/offline/Head.hh" #include "JSystem/JStat.hh" #include "JLang/JRedirectString.hh" #include "JLang/JStringStream.hh" #include "JLang/JNullStream.hh" #include "JLang/JLangToolkit.hh" #include "JROOT/JRootClass.hh" #include "JROOT/JRootStreamer.hh" #include "JAAnet/JHead.hh" #include "JAAnet/JAAnetDictionary.hh" #include "JAAnet/JHeadWriter.hh" /** * \author mdejong */ namespace JAANET { /** * Push all data members to Head. */ void JHead::push() { using namespace std; using namespace JPP; JRootReadableClass cls(*this); unique_ptr i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) { if (!JRootClass::is_static(*p)) { if (this->count(p->GetName()) == 0) { (*this)[p->GetName()] = ""; } } } } /** * Read header from input. * * \param in input stream * \return input stream */ std::istream& JHead::read(std::istream& in) { using namespace std; using namespace JPP; JStringStream is(in); if (getFileStatus(is.str().c_str())) { is.load(); } JRootReader reader(is, JHead::getEquationParameters(), JAAnetDictionary::getInstance()); JRootReadableClass cls(*this); for (JEquation equation; reader >> equation && equation.getKey() != end_event::Class_Name(); ) { JRedirectString redirect(reader, equation.getValue()); const JRootReadableClass abc = cls.find(equation.getKey().c_str()); if (abc.is_valid()) { reader.getObject(abc); } (*this)[equation.getKey()] = equation.getValue(); } return in; } /** * Write header to output. * * \param out output stream * \return output stream */ std::ostream& JHead::write(std::ostream& out) const { using namespace std; using namespace JPP; JRootWriter writer(out, JHead::getEquationParameters(), JAAnetDictionary::getInstance()); JRootWritableClass cls(*this); unique_ptr i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) { if (!JRootClass::is_static(*p)) { if (this->find(p->GetName()) != this->end() || cls.get(*p) == JRootClass(&JHead::start_run) || cls.get(*p) == JRootClass(&JHead::end_event)) { writer.put(p->GetName(), cls.get(*p), true); } } } return out << flush; } /** * Print header to output. * * \param out output stream * \return output stream */ std::ostream& JHead::print(std::ostream& out) const { using namespace std; using namespace JPP; JRootWriter writer(out, JHead::getEquationParameters(), JAAnetDictionary::getInstance()); JRootWritableClass cls(*this); unique_ptr i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); pair end_event("", this->end_event); for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) { if (!JRootClass::is_static(*p)) { if (cls.get(*p) != JRootClass(&JHead::end_event)) writer.put(p->GetName(), cls.get(*p), true); else end_event = make_pair(p->GetName(), cls.get(*p)); } } for (JHead::const_iterator i = this->begin(); i != this->end(); ++i) { if (!cls.find(i->first.c_str()).is_valid()) { writer.put(i->first, i->second); } } writer.put(end_event.first, end_event.second, true); return out << flush; } /** * Copy header from from to to. * * \param from header * \param to header */ void copy(const Head& from, JHead& to) { using namespace std; using namespace JPP; JRootReader reader(null, JHead::getEquationParameters(), JAAnetDictionary::getInstance()); JRootReadableClass cls(to); for (Head::const_iterator i = from.begin(); i != from.end(); ++i) { const JRootReadableClass& abc = cls.find(getTag(i->first).c_str()); const string buffer = trim(i->second); if (abc.is_valid() && buffer != "") { JRedirectString redirect(reader, buffer); reader.getObject(abc); if (i->first == getTag(i->first)) { to.insert(*i); // keep track of parsed tags } } else { to.insert(*i); // store data of unknown tags } } } /** * Copy header from from to to. * * \param from header * \param to header */ void copy(const JHead& from, Head& to) { using namespace std; using namespace JPP; to = Head(); JHeadWriter writer(to, JHead::getEquationParameters(), JAAnetDictionary::getInstance()); JRootWritableClass cls(from); unique_ptr i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) { if (!JRootClass::is_static(*p)) { if (from.find(p->GetName()) != from.end() || cls.get(*p) == JRootClass(&JHead::start_run) || cls.get(*p) == JRootClass(&JHead::end_event)) { writer.put(p->GetName(), cls.get(*p), true); } } } // copy pending data for (JHead::const_iterator i = from.begin(); i != from.end(); ++i) { if (to.find(i->first) == to.end()) { to.insert(*i); } } } }