ATLAS Offline Software
Loading...
Searching...
No Matches
ISelectionAccessor.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8//
9// includes
10//
11
24#include <exception>
25#include <unordered_map>
26
27//
28// method implementations
29//
30
31namespace CP
32{
33 namespace
34 {
35 std::vector<std::string>
36 splitString (const std::string& input, const std::string& separator)
37 {
38 std::vector<std::string> result;
39 std::string::size_type start = 0, split = 0;
40 while ((split = input.find (separator, start)) != std::string::npos)
41 {
42 result.emplace_back (input.substr (start, split - start));
43 start = split + separator.size();
44 }
45 result.emplace_back (input.substr (start));
46 return result;
47 }
48
49 }
50
51
52
54 makeSelectionReadAccessor(const std::string& expr,
55 std::unique_ptr<ISelectionReadAccessor>& accessor,
56 bool defaultToChar)
57 {
58 using namespace msgSelectionHelpers;
59
60 if (expr.empty())
61 {
62 accessor = std::make_unique<SelectionReadAccessorNull> (true);
63 return StatusCode::SUCCESS;
64 }
65
66 try {
67 SelectionExprParser parser(expr, defaultToChar);
68 ANA_CHECK(parser.build(accessor));
69 } catch (const std::exception& e) {
70 ANA_MSG_FATAL("Failure to parse expression: '" << expr << "': " << e.what());
71 return StatusCode::FAILURE;
72 }
73
74 return StatusCode::SUCCESS;
75 }
76
77
78 namespace
79 {
80 struct SplitData final
81 {
82 std::string var;
83 bool asChar = false;
84 bool asBits = false;
85 bool invert = false;
86
87 StatusCode fill (const std::string& name, bool defaultToChar)
88 {
89 using namespace msgSelectionHelpers;
90
91 for (const std::string& option : splitString (name, ","))
92 {
93 if (var.empty())
94 {
95 // this is a bit of a hack, it will pick up the first
96 // component as the decoration name
97 var = option;
98 } else if (option == "as_char")
99 {
100 // using ",as_char" as a postfix to indicate char decorations
101 // (and ",as_bits" as a postfix to indicate bitset
102 // decorations). I chose that suffix as it should make it
103 // easier to read in configuration files and allows for future
104 // extensions if needed.
105 asChar = true;
106 } else if (option == "as_bits")
107 {
108 asBits = true;
109 } else if (option == "invert")
110 {
111 invert = true;
112 } else
113 {
114 ANA_MSG_ERROR ("invalid option " << option << "for selection decoration");
115 return StatusCode::FAILURE;
116 }
117 }
118
119 if (asChar && asBits)
120 {
121 ANA_MSG_ERROR ("can't specify both 'as_bits' and 'as_char' for the same selection decoration, pick one!!!");
122 return StatusCode::FAILURE;
123 }
124
125 if (!asChar && !asBits)
126 {
127 if (defaultToChar)
128 asChar = true;
129 else
130 asBits = true;
131 }
132 return StatusCode::SUCCESS;
133 }
134 };
135 }
136
137
140 std::unique_ptr<ISelectionReadAccessor>& accessor,
141 bool defaultToChar)
142 {
143 using namespace msgSelectionHelpers;
144
145 if (name.find ("%SYS%") != std::string::npos)
146 {
147 accessor = std::make_unique<SelectionAccessorReadSys>(name);
148 return StatusCode::SUCCESS;
149 }
150
151 SplitData splitData;
152 ANA_CHECK (splitData.fill (name, defaultToChar));
153
154 if (splitData.asChar)
155 accessor = std::make_unique<SelectionReadAccessorChar> (splitData.var);
156 else
157 accessor = std::make_unique<SelectionReadAccessorBits> (splitData.var);
158
159 if (splitData.invert)
160 {
161 accessor = std::make_unique<SelectionReadAccessorInvert>
162 (std::move (accessor));
163 }
164
165 return StatusCode::SUCCESS;
166 }
167
168
169 StatusCode
170 makeSelectionWriteAccessor (const std::string& name,
171 std::unique_ptr<ISelectionWriteAccessor>& accessor,
172 bool defaultToChar)
173 {
174 using namespace msgSelectionHelpers;
175
176 if (name.find ("%SYS%") != std::string::npos)
177 {
178 accessor = std::make_unique<SelectionWriteAccessorSys>(name);
179 return StatusCode::SUCCESS;
180 }
181
182 SplitData splitData;
183 ANA_CHECK (splitData.fill (name, defaultToChar));
184
185 if (splitData.asChar)
186 accessor = std::make_unique<SelectionWriteAccessorChar> (splitData.var);
187 else
188 accessor = std::make_unique<SelectionWriteAccessorBits> (splitData.var);
189
190 if (splitData.invert)
191 {
192 accessor = std::make_unique<SelectionWriteAccessorInvert>
193 (std::move (accessor));
194 }
195
196 return StatusCode::SUCCESS;
197 }
198}
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
#define ANA_MSG_FATAL(xmsg)
Macro printing fatal messages.
std::vector< std::string > splitString(const std::string &in, const std::string &delim)
Public interface for the expression parsing facility.
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
Select isolated Photons, Electrons and Muons.
StatusCode makeSelectionWriteAccessor(const std::string &name, std::unique_ptr< ISelectionWriteAccessor > &accessor, bool defaultToChar)
Produces a simple ISelectionWriteAccessor accessing the given decoration.
StatusCode makeSelectionReadAccessorVar(const std::string &name, std::unique_ptr< ISelectionReadAccessor > &accessor, bool defaultToChar)
Produces a simple ISelectionReadAccessor accessing the given decoration.
StatusCode makeSelectionReadAccessor(const std::string &expr, std::unique_ptr< ISelectionReadAccessor > &accessor, bool defaultToChar)
make the ISelectionReadAccessor for the given name
::StatusCode StatusCode
StatusCode definition for legacy code.
void fill(H5::Group &out_file, size_t iterations)