InputTag.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 
20 #include <momemta/InputTag.h>
21 
22 std::vector<std::string> split(const std::string& s, const std::string& delimiters) {
23 
24  std::vector<std::string> result;
25 
26  size_t current;
27  size_t next = -1;
28  do
29  {
30  next = s.find_first_not_of(delimiters, next + 1);
31  if (next == std::string::npos)
32  break;
33  next -= 1;
34 
35  current = next + 1;
36  next = s.find_first_of(delimiters, current);
37  result.push_back(s.substr(current, next - current));
38  }
39  while (next != std::string::npos);
40 
41  return result;
42 }
43 
44 InputTag::InputTag(const std::string& module, const std::string& parameter):
45  module(module), parameter(parameter) {
46  string_representation = module + "::" + parameter;
47 }
48 
49 InputTag::InputTag(const std::string& module, const std::string& parameter, size_t index):
50  module(module), parameter(parameter), index(index), indexed(true) {
51  string_representation = module + "::" + parameter + "/" + std::to_string(index + 1);
52 }
53 
54 bool InputTag::isInputTag(const std::string& tag) {
55  if (tag.find("::") == std::string::npos)
56  return false;
57 
58  if (tag.find("/") != std::string::npos) {
59  auto tags = split(tag, "::");
60  auto rtags = split(tags[1], "/");
61  try {
62  int64_t index = std::stoll(rtags[1]) - 1;
63  if (index < 0)
64  return false;
65 
66  return true;
67  } catch (const std::invalid_argument& e) {
68  return false;
69  }
70  } else {
71  return true;
72  }
73 }
74 
75 InputTag InputTag::fromString(const std::string& tag) {
76  auto tags = split(tag, "::");
77  auto rtags = split(tags[1], "/");
78 
79  if (rtags.size() == 1)
80  return InputTag(tags[0], tags[1]);
81  else
82  return InputTag(tags[0], rtags[0], std::stoull(rtags[1]) - 1);
83 }
84 
85 bool InputTag::operator==(const InputTag& rhs) const {
86  return ((module == rhs.module) && (parameter == rhs.parameter));
87 }
88 
89 std::string InputTag::toString() const {
90  return string_representation;
91 }
92 
93 bool InputTag::isIndexed() const {
94  return indexed;
95 }
96 
97 bool InputTag::empty() const {
98  return module.empty() || parameter.empty();
99 }
100 
102  string_representation = module + "::" + parameter;
103  if (indexed)
104  string_representation += "/" + std::to_string(index + 1);
105 }
bool isIndexed() const
Definition: InputTag.cc:93
void update()
Update the string representation of the InputTag.
Definition: InputTag.cc:101
bool empty() const
Definition: InputTag.cc:97
bool operator==(const InputTag &rhs) const
Test equality between two InputTags.
Definition: InputTag.cc:85
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
size_t index
The index. Only meaningful if isIndexed() returns true.
Definition: InputTag_fwd.h:109
std::string parameter
The module&#39;s output.
Definition: InputTag_fwd.h:108
std::string toString() const
Convert the InputTag in its string representation.
Definition: InputTag.cc:89
static InputTag fromString(const std::string &tag)
Create a input tag from its string representation.
Definition: InputTag.cc:75
std::string module
The module&#39;s name.
Definition: InputTag_fwd.h:107
InputTag()=default
Construct an empty InputTag.
static bool isInputTag(const std::string &tag)
Check if a given string represent an InputTag.
Definition: InputTag.cc:54