Permutator.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <momemta/ParameterSet.h>
20 #include <momemta/Module.h>
21 #include <momemta/Types.h>
22 
23 #include <algorithm>
24 #include <cmath>
25 #include <numeric>
26 
27 #include <TMath.h>
28 
60 class Permutator: public Module {
61  public:
62 
63  Permutator(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
64 
65  m_ps_point = get<double>(parameters.get<InputTag>("ps_point"));
66 
67  auto particle_tags = parameters.get<std::vector<InputTag>>("inputs");
68  for (auto& t: particle_tags)
69  m_inputs.push_back(get<LorentzVector>(t));
70 
71  std::vector<uint32_t> tmp(m_inputs.size());
72  std::iota(tmp.begin(), tmp.end(), 0);
73 
74  do {
75  perm_indices.push_back(tmp);
76  } while (std::next_permutation(tmp.begin(), tmp.end()));
77 
78  (*m_output).resize(m_inputs.size());
79  };
80 
81  virtual Status work() override {
82  double psPoint = *m_ps_point;
83 
84  size_t chosen_perm = std::lround(psPoint * (perm_indices.size() - 1));
85 
86  for (size_t i = 0; i < m_inputs.size(); i++)
87  (*m_output)[i] = *m_inputs[perm_indices[chosen_perm][i]];
88 
89  return Status::OK;
90  }
91 
92  private:
93  std::vector<std::vector<uint32_t>> perm_indices;
94 
95  // Inputs
96  Value<double> m_ps_point;
97  std::vector<Value<LorentzVector>> m_inputs;
98 
99  // Outputs
100  std::shared_ptr<std::vector<LorentzVector>> m_output = produce<std::vector<LorentzVector>>("output");
101 };
102 
103 REGISTER_MODULE(Permutator)
104  .Input("ps_point")
105  .Inputs("inputs")
106  .Output("output");
107 
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
Parent class for all the modules.
Definition: Module.h:37
A class encapsulating a lua table.
Definition: ParameterSet.h:82
virtual Status work() override
Main function.
Definition: Permutator.cc:81
Apply random permutations to a set of inputs.
Definition: Permutator.cc:60
Module(PoolPtr pool, const std::string &name)
Constructor.
Definition: Module.h:61