Printer.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/Module.h>
20 
21 #include <sstream>
22 
23 #include <momemta/ParameterSet.h>
24 #include <momemta/Solution.h>
25 #include <momemta/Types.h>
26 
32 template<typename T>
33 class Printer: public Module {
34  public:
35 
36  Printer(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
37  auto tag = parameters.get<InputTag>("input");
38  input = pool->get<T>(tag);
39  name = parameters.get<std::string>("name", tag.toString());
40  };
41 
42  virtual Status work() override {
43  LOG(info) << name << ": " << *input;
44 
45  return Status::OK;
46  }
47 
48  private:
49  std::string name;
50 
51  // Inputs
52  Value<T> input;
53 };
54 
58 template<typename T>
59 class Printer<std::vector<T>>: public Module {
60  public:
61 
62  Printer(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
63  auto tag = parameters.get<InputTag>("input");
64  input = pool->get<std::vector<T>>(tag);
65  name = parameters.get<std::string>("name", tag.toString());
66  };
67 
68  virtual Status work() override {
69  std::stringstream str;
70  str << name << ": {";
71 
72  size_t count = 1;
73  for (const auto& i: *input) {
74  str << i;
75  if (count < input->size())
76  str << ", ";
77  count++;
78  }
79  LOG(info) << str.str() << "}";
80 
81  return Status::OK;
82  }
83 
84  private:
85  std::string name;
86 
87  // Inputs
88  Value<std::vector<T>> input;
89 };
90 
91 REGISTER_MODULE_NAME("IntPrinter", Printer<int64_t>)
92  .Input("input")
93  .OptionalAttr("name:string")
94  .Sticky();
95 REGISTER_MODULE_NAME("DoublePrinter", Printer<double>)
96  .Input("input")
97  .OptionalAttr("name:string")
98  .Sticky();
99 REGISTER_MODULE_NAME("P4Printer", Printer<LorentzVector>)
100  .Input("input")
101  .OptionalAttr("name:string")
102  .Sticky();
103 REGISTER_MODULE_NAME("SolutionPrinter", Printer<Solution>)
104  .Input("input")
105  .OptionalAttr("name:string")
106  .Sticky();
107 
108 REGISTER_MODULE_NAME("IntVectorPrinter", Printer<std::vector<int64_t>>)
109  .Input("input")
110  .OptionalAttr("name:string")
111  .Sticky();
112 REGISTER_MODULE_NAME("DoubleVectorPrinter", Printer<std::vector<double>>)
113  .Input("input")
114  .OptionalAttr("name:string")
115  .Sticky();
116 REGISTER_MODULE_NAME("P4VectorPrinter", Printer<std::vector<LorentzVector>>)
117  .Input("input")
118  .OptionalAttr("name:string")
119  .Sticky();
120 REGISTER_MODULE_NAME("SolutionVectorPrinter", Printer<SolutionCollection>)
121  .Input("input")
122  .OptionalAttr("name:string")
123  .Sticky();
A module printing the value of an input.
Definition: Printer.cc:33
Definition: optional.h:869
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
virtual Status work() override
Main function.
Definition: Printer.cc:68
Parent class for all the modules.
Definition: Module.h:37
virtual Status work() override
Main function.
Definition: Printer.cc:42
A class encapsulating a lua table.
Definition: ParameterSet.h:82
A class representing a value produced by a module.
Definition: Value.h:30
Module(PoolPtr pool, const std::string &name)
Constructor.
Definition: Module.h:61