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