#ifndef __JSUPPORT__JEVTWEIGHTFILESCANNER__ #define __JSUPPORT__JEVTWEIGHTFILESCANNER__ #include #include #include #include #include "km3net-dataformat/offline/Head.hh" #include "km3net-dataformat/offline/Evt.hh" #include "JLang/JException.hh" #include "JAAnet/JHead.hh" #include "JAAnet/JEvtCategory.hh" #include "JAAnet/JEvtCategoryHelper.hh" #include "JAAnet/JEvtCategoryToolkit.hh" #include "JAAnet/JEvtCategoryMap.hh" #include "JAAnet/JEvtWeight.hh" #include "JAAnet/JEvtWeightHelper.hh" #include "JAAnet/JEvtWeightToolkit.hh" #include "JAAnet/JEvtWeightFactor.hh" #include "JAAnet/JEvtWeightFactorHelper.hh" #include "JAAnet/JFluxHelper.hh" #include "JSupport/JSupport.hh" #include "JSupport/JMultipleFileScanner.hh" #include "JSupport/JMonteCarloFileSupportkit.hh" /** * \author bjung */ namespace JSUPPORT {} namespace JPP { using namespace JSUPPORT; } namespace JSUPPORT { using JAANET::JHead; using JAANET::JEvtCategoryHelper; using JAANET::JEvtCategorySet; using JAANET::JEvtCategoryMap; using JAANET::JEvtWeight; using JAANET::JEvtWeightHelper; using JAANET::JEvtWeightFactorHelper; using JAANET::JFluxHelper; /** * Template event-weighter-associated file scanner. * * Note: The template class must be derived from JMultipleFileScanner */ template > struct JEvtWeightFileScanner : public JEvtWeightHelper, public JFileScanner_t { typedef typename JFileScanner_t::input_type input_type; /** * Default constructor. */ JEvtWeightFileScanner() : JEvtWeightHelper(), JFileScanner_t() {} /** * Constructor. * * \param weighter event weighter */ JEvtWeightFileScanner(const JEvtWeight& weighter) : JEvtWeightHelper(), JFileScanner_t() { reset(weighter); } /** * Constructor. * * \param input input */ JEvtWeightFileScanner(const input_type& input) : JEvtWeightHelper(), JFileScanner_t() { using namespace JPP; const JHead& header = JSUPPORT::getHeader(input); const JEvtWeight& weighter = getEventWeighter(header); reset(weighter); put (input); } /** * Get name of simulation program. * * \return unique identifier */ std::string getProgramName() const { using namespace std; using namespace JPP; static const char SEPARATOR = '.'; const JHead& header = this->getHeader(); if (header.simul.size() > 0) { string name = header.simul.cbegin()->program; for (vector::const_iterator i = next(header.simul.cbegin()); i != header.simul.cend(); ++i) { name += MAKE_STRING(SEPARATOR << i->program); } return name; } else { THROW(JNoValue, "JEvtWeightFileScanner::getProgramName(): Missing simul header-field!"); } } /** * Reset file scanner and event weighter. * * \param weighter event weighter */ void reset(const JEvtWeight& weighter) { JEvtWeightHelper::configure(weighter); JFileScanner_t ::clear(); } /** * Put files. * * \param input input files * \return number of added files */ size_t put(const input_type& input) { size_t n = 0; for (typename input_type::const_iterator i = input.begin(); i != input.end(); ++i) { n += size_t(this->put(*i)); } return n; } /** * Put file. * * \param input input file * \return true if successfully added; else false. */ bool put(const std::string& input) { using namespace JPP; const JHead& head = JSUPPORT::getHeader(input); if (this->check(head)) { JEvtWeightHelper::add(head); JFileScanner_t ::push_back(input); return true; } else { return false; } } /** * Set event-weight factor for the event-weighter associated with this file scanner. * * The template argument refers to the desired helper class for an event-weight factor\n * (c.f. `JAANET::JEvtWeightFactorHelper`, `JAANET::JFluxHelper` or `JAANET::JDiffuseFluxHelper`). * * \param factor event-weight factor * \return true if event-weight factor set successfully; else false */ template bool setEvtWeightFactor(const JEvtWeightFactorHelper_t& factor) { JEvtWeightFactorHelper_t* helper = dynamic_cast(this->get()); if (helper != NULL) { helper->configure(factor); return true; } else { return false; } } /** * Set event-weight factor corresponding to a given event category. * * \param category event category * \param factor event-weight factor * \return true if event-weight factor set successfully; else false */ bool setEvtWeightFactor(const JEvtCategoryHelper& category, const JEvtWeightFactorHelper& factor) { const JHead& header = this->getHeader(); return (category.match(header) ? setEvtWeightFactor(factor) : false); } /** * Set event-weight factor corresponding to a given set of event categories. * * Note that the given event-weight factor will only be assigned\n * if the header matches < b>all< /b> of the specified event categories. * * \param categories set of event categories * \param factor event-weight factor * \return true if event-weight factor set successfully; else false */ bool setEvtWeightFactor(const JEvtCategorySet& categories, const JEvtWeightFactorHelper& factor) { using namespace std; using namespace JPP; bool matching = (!categories.empty()); const JHead& header = this->getHeader(); const JEvtCategorySet cats = getCategories(header); for (JEvtCategorySet::const_iterator i = categories.cbegin(); i != categories.cend() && matching; ++i) { JEvtCategorySet::const_iterator j = std::find(cats.cbegin(), cats.cend(), *i); matching = (j != cats.cend()); } return (matching ? setEvtWeightFactor(factor) : false); } /** * Set event-weight factor according to a given map between event categories and event-weight factors. * * The template argument refers either to the class `JAANET::JEvtWeightFactorHelper` or `JAANET::JFluxHelper`. * * Note that the given event-weight factors will only be assigned\n * if the header matches < b>all< /b> of the event categories associated with the given map. * * \param object map between event categories and event-weight factors * \return true if event-weight factors are set successfully; else false */ template bool setEvtWeightFactor(const JEvtCategoryMap& object) { using namespace std; using namespace JPP; typedef JEvtCategoryMap JEvtCategoryMap_t; bool matching = (!object.empty()); const JHead& header = this->getHeader(); const JEvtCategorySet cats = getCategories(header); for (typename JEvtCategoryMap_t::const_iterator i = object.cbegin(); i != object.cend() && matching; ++i) { JEvtCategorySet::const_iterator j = std::find(cats.cbegin(), cats.cend(), i->first); matching = (j != cats.cend()); } return (matching ? setEvtWeightFactor(object) : false); } /** * Set flux function for the event-weighter associated with this file scanner. * * \param flux flux function * \return true if flux function set successfully; else false */ bool setFlux(const JFluxHelper& flux) { return setEvtWeightFactor(flux); } /** * Set flux function corresponding to a given event category. * * \param category event category * \param flux flux function * \return true if flux function set successfully; else false */ bool setFlux(const JEvtCategoryHelper& category, const JFluxHelper& flux) { return setEvtWeightFactor(category, flux); } /** * Set flux function corresponding to a given set of event categories. * * Note that the given flux function will only be assigned\n * if the header matches < b>all< /b> of the specified event categories. * * \param categories set of event categories * \param flux flux function * \return true if event-weight factor set successfully; else false */ bool setFlux(const JEvtCategorySet& categories, const JFluxHelper& flux) { return setEvtWeightFactor(categories, flux); } /** * Set flux function according to a given map between event categories and flux functions. * * Note that the given flux functions will only be assigned\n * if the header matches < b>all< /b> of the event categories associated with the given map. * * \param object map between event categories and flux functions * \return true if event-weight factors are set successfully; else false */ bool setFlux(const JEvtCategoryMap& object) { return setEvtWeightFactor(object); } private: using JEvtWeightHelper::reset; }; } #endif