#ifndef __JAANET__JEVTWEIGHTFACTORTFORMULA__ #define __JAANET__JEVTWEIGHTFACTORTFORMULA__ #include #include "JLang/JException.hh" #include "Jeep/JProperties.hh" #include "JAAnet/JEvtWeightFactor.hh" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wall" #include "TFormula.h" #pragma GCC diagnostic pop /** * \author bjung */ namespace JAANET {} namespace JPP { using namespace JAANET; } namespace JAANET { using JLANG::JValueOutOfRange; using JEEP::JProperties; /** * Base class implementation for reweighting factor for simulated events according to a specifiable ROOT TFormula. */ class JEvtWeightFactorTFormula : public TFormula, public JEvtWeightFactor { public: /** * Default constructor. */ JEvtWeightFactorTFormula() {} /** * Virtual destructor. */ ~JEvtWeightFactorTFormula() {} /** * Get formula keyword. * * \return formula keyword */ static const std::string getFormulaKey() { return "formula"; } /** * Compile given formula. * * \param formula formula */ void compile(const char* const formula) { this->Clear(); if (this->Compile(formula)) { THROW(JValueOutOfRange, "JEvtWeightFactorTFormula::compile(): Could not compile formula: " << formula); } this->check_validity(); } /** * Compile currently stored formula. */ void compile() { compile(this->GetExpFormula()); } /** * Get properties of this class. * * \param eqpars equation parameters */ JProperties getProperties(const JEquationParameters& eqpars = JEvtWeightFactor::getEquationParameters()) override final { return JEvtWeightFactorTFormulaHelper(*this, eqpars); } /** * Get properties of this class. * * \param eqpars equation parameters */ JProperties getProperties(const JEquationParameters& eqpars = JEvtWeightFactor::getEquationParameters()) const override final { return JEvtWeightFactorTFormulaHelper(*this, eqpars); } /** * Read event-weight factor from input. * * \param in input stream * \return input stream */ std::istream& read(std::istream& in) override final { using namespace std; this->Clear(); streampos pos = in.tellg(); JProperties properties = getProperties(); in >> properties; this->compile(); in.clear(); // Rewind to read parameters in.seekg(pos); properties = getProperties(); in >> properties; return in; } protected: /** * Retrieve TFormula. * * \return TFormula */ TFormula& getFormula() { return static_cast(*this); } /** * Retrieve TFormula. * * \return TFormula */ const TFormula& getFormula() const { return static_cast(*this); } private: /** * Auxiliary class for I/O of TFormula-based event-weight factor. */ struct JEvtWeightFactorTFormulaHelper : public JProperties { /** * Constructor. * * \param formula TFormula-based event-weight factor * \param eqpars equation parameters */ template JEvtWeightFactorTFormulaHelper(JEvtWeightFactorTFormula_t& formula, const JEquationParameters& eqpars) : JProperties(eqpars, 1) { using namespace std; (*this)[JEvtWeightFactor::getTypeKey()] = "TFormula"; (*this)[getFormulaKey()] = formula.fFormula; Double_t* parameters = formula.GetParameters(); for (int i = 0; i < formula.GetNpar(); ++i) { const string parname = formula.GetParName(i); (*this)[parname] = parameters[i]; } } }; }; } #endif