-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Handler.cpp
71 lines (65 loc) · 2.85 KB
/
Handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CepGen/Cards/Handler.h"
#include "CepGen/Core/Exception.h"
#include "CepGen/Core/ParametersList.h"
#include "CepGen/Core/RunParameters.h"
#include "CepGen/Modules/CardsHandlerFactory.h"
#include "CepGen/Utils/Filesystem.h"
namespace cepgen {
namespace card {
Handler::Handler(const ParametersList& params)
: NamedModule(params), filename_(steer<std::string>("filename")), rt_params_(new RunParameters) {
if (!filename_.empty())
parseFile(filename_, rt_params_);
}
RunParameters* Handler::parseString(const std::string& filename) {
try {
auto parser = CardsHandlerFactory::get().build(utils::fileExtension(filename));
return parser->parseString(filename, new RunParameters);
} catch (const std::invalid_argument& err) {
throw CG_FATAL("Cards:handler") << "Failed to parse the steering card at \"" << filename << "\"! "
<< err.what();
}
}
RunParameters* Handler::parseFile(const std::string& filename) {
try {
auto parser = CardsHandlerFactory::get().build(utils::fileExtension(filename));
return parser->parseFile(filename, new RunParameters);
} catch (const std::invalid_argument& err) {
throw CG_FATAL("Cards:handler") << "Failed to parse the steering card at \"" << filename << "\"! "
<< err.what();
}
}
void Handler::write(const RunParameters* params, const std::string& filename) {
try {
auto writer = CardsHandlerFactory::get().build(utils::fileExtension(filename));
writer->pack(params);
return writer->write(filename);
} catch (const std::invalid_argument& err) {
throw CG_FATAL("Cards:handler") << "Failed to write the configuration to \"" << filename << "\"! "
<< err.what();
}
}
ParametersDescription Handler::description() {
auto desc = ParametersDescription();
desc.add<std::string>("filename", "").setDescription("Steering card to parse");
return desc;
}
} // namespace card
} // namespace cepgen