Search Results
LinearCombinator.cc
/*
* MoMEMta: a modular implementation of the Matrix Element Method
* Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <momemta/ParameterSet.h>
#include <momemta/Module.h>
#include <momemta/Types.h>
#include <momemta/Utils.h>
#include <stdexcept>
template<typename T>
class LinearCombinator: public Module {
public:
LinearCombinator(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
auto tags = parameters.get<std::vector<InputTag>>("inputs");
for (auto& v: tags)
m_terms.push_back(get<T>(v));
m_coefficients = parameters.get<std::vector<double>>("coefficients");
if (m_coefficients.size() == 0 || m_terms.size() == 0){
auto exception = std::invalid_argument("Tried to call LinearCombinator with an empty input.");
LOG(fatal) << exception.what();
throw exception;
}
if (m_coefficients.size() != m_terms.size()){
auto exception = std::invalid_argument("The Term and Coefficient lists passed to LinearCombinator have different sizes.");
LOG(fatal) << exception.what();
throw exception;
}
};
virtual Status work() override {
T temp_result = m_coefficients[0] * *m_terms[0];
for (std::size_t i = 1; i < m_terms.size(); i++)
temp_result += m_coefficients[i] * *m_terms[i];
*output = temp_result;
return Status::OK;
}
private:
// Inputs
std::vector<double> m_coefficients;
std::vector<Value<T>> m_terms;
// Outputs
std::shared_ptr<T> output = produce<T>("output");
};
REGISTER_MODULE_NAME("VectorLinearCombinator", LinearCombinator<LorentzVector>)
.Inputs("inputs")
.Output("output")
.Attr("coefficients:list(double)");
REGISTER_MODULE_NAME("DoubleLinearCombinator", LinearCombinator<double>)
.Inputs("inputs")
.Output("output")
.Attr("coefficients:list(double)");
REGISTER_MODULE_NAME("IntLinearCombinator", LinearCombinator<int64_t>)
.Inputs("inputs")
.Output("output")
.Attr("coefficients:list(double)");