14 #include <boost/algorithm/string.hpp>
16 #include "yaml-cpp/yaml.h"
31 Log& SchemeDefinitions::getLog()
const {
32 return Log::getLog(
"Hammer.SchemeDefinitions");
35 void SchemeDefinitions::write(flatbuffers::FlatBufferBuilder* msgwriter,
37 schemes->reserve(_formFactorSchemes.size());
38 for (
auto& elem : _formFactorSchemes) {
43 for (
auto& elem2 : elem.second) {
44 chs.push_back(static_cast<uint16_t>(elem2.second));
45 ids.push_back(elem2.first);
47 if(elem.first ==
"Denominator") {
48 for(
auto& elem2: _formFactorBase) {
49 keys.push_back(elem2.first);
50 vals.push_back(elem2.second);
54 auto it = _formFactorSchemeNames.find(elem.first);
55 for(
auto& elem2: it->second) {
56 keys.push_back(elem2.first);
57 vals.push_back(elem2.second);
60 auto serialChs = msgwriter->CreateVector(chs);
61 auto serialIds = msgwriter->CreateVector(ids);
62 auto serialProcs = msgwriter->CreateVectorOfStrings(keys);
63 auto serialGroups = msgwriter->CreateVectorOfStrings(vals);
64 auto name = msgwriter->CreateString(elem.first);
65 Serial::FBFFSchemeBuilder serialFF{*msgwriter};
66 serialFF.add_name(name);
67 serialFF.add_ffids(serialIds);
68 serialFF.add_ffschemes(serialChs);
69 serialFF.add_ffproc(serialProcs);
70 serialFF.add_ffgroup(serialGroups);
71 auto resFF = serialFF.Finish();
72 schemes->push_back(resFF);
77 auto schemes = msgreader->ffschemes();
79 _formFactorSchemes.clear();
80 _formFactorSchemeNames.clear();
81 _formFactorBase.clear();
83 for (
unsigned int i = 0; i < schemes->size(); ++i) {
84 string name = schemes->Get(i)->name()->c_str();
85 auto it = _formFactorSchemes.find(name);
87 if(it != _formFactorSchemes.end()) {
89 auto ids = schemes->Get(i)->ffids();
90 auto chs = schemes->Get(i)->ffschemes();
91 for (
unsigned int j = 0; j < ids->size(); ++j) {
92 auto it2 = it->second.find(ids->Get(j));
93 if(it2 == it->second.end() || it2->second != chs->Get(j)) {
97 if(name ==
"Denominator") {
98 auto procs = schemes->Get(i)->ffproc();
99 auto groups = schemes->Get(i)->ffgroup();
100 for (
unsigned int j = 0; j < procs->size(); ++j) {
101 auto itd = _formFactorBase.find(procs->Get(j)->c_str());
102 if(itd != _formFactorBase.end()) {
103 if(itd->second != groups->Get(j)->c_str()) {
108 _formFactorBase.emplace(procs->Get(j)->c_str(), groups->Get(j)->c_str());
112 auto it2 = _formFactorSchemeNames.find(name);
113 if(it2 == _formFactorSchemeNames.end()) {
114 it2 = _formFactorSchemeNames.insert(make_pair(name, map<string, string>{})).first;
116 auto procs = schemes->Get(i)->ffproc();
117 auto groups = schemes->Get(i)->ffgroup();
118 for (
unsigned int j = 0; j < procs->size(); ++j) {
119 auto itn = it2->second.find(procs->Get(j)->c_str());
120 if (itn != it2->second.end()) {
121 if (itn->second != groups->Get(j)->c_str()) {
126 it2->second.emplace(procs->Get(j)->c_str(), groups->Get(j)->c_str());
129 if(!result)
return false;
132 auto res = _formFactorSchemes.insert({name, map<HashId, FFIndex>{}});
133 auto ids = schemes->Get(i)->ffids();
134 auto chs = schemes->Get(i)->ffschemes();
135 for (
unsigned int j = 0; j < ids->size(); ++j) {
136 res.first->second.insert({ids->Get(j), chs->Get(j)});
138 if (name ==
"Denominator") {
139 auto procs = schemes->Get(i)->ffproc();
140 auto groups = schemes->Get(i)->ffgroup();
141 for (
unsigned int j = 0; j < procs->size(); ++j) {
142 _formFactorBase.insert({procs->Get(j)->c_str(), groups->Get(j)->c_str()});
145 auto res2 = _formFactorSchemeNames.insert({name, map<string, string>{}});
146 auto procs = schemes->Get(i)->ffproc();
147 auto groups = schemes->Get(i)->ffgroup();
148 for (
unsigned int j = 0; j < procs->size(); ++j) {
149 res2.first->second.insert({procs->Get(j)->c_str(), groups->Get(j)->c_str()});
157 void SchemeDefinitions::addFFScheme(
const string& name,
const map<string, string>& schemes) {
158 auto it = _formFactorSchemeNames.find(name);
159 if (it != _formFactorSchemeNames.end()) {
160 it->second = schemes;
163 _formFactorSchemeNames.insert({name, schemes});
167 void SchemeDefinitions::removeFFScheme(
const string& name) {
168 auto it = _formFactorSchemeNames.find(name);
169 if (it != _formFactorSchemeNames.end()) {
170 _formFactorSchemeNames.erase(it);
174 void SchemeDefinitions::setFFInputScheme(
const map<string, string>& schemes) {
175 _formFactorBase = schemes;
178 vector<string> SchemeDefinitions::getFFSchemeNames()
const {
179 vector<string> names;
180 for (
auto& elem : _formFactorSchemeNames) {
181 names.push_back(elem.first);
186 const map<HashId, string> SchemeDefinitions::getScheme(
const string& name)
const {
187 const map<string, string>* toProcess;
188 if(name.size() == 0) {
189 toProcess = &_formFactorBase;
192 auto it = _formFactorSchemeNames.find(name);
193 if (it != _formFactorSchemeNames.end()) {
194 toProcess = &(it->second);
197 return map<HashId, string>{};
200 map<HashId, string> result;
201 PID& pdg = PID::instance();
202 for(
auto& elem: *toProcess) {
204 for(
auto elem2: tmp) {
205 result[elem2] = elem.second;
211 map<HashId, map<string, vector<string>>> SchemeDefinitions::getFFDuplicates()
const {
212 map<HashId, map<string, vector<string>>> result;
213 PID& pdg = PID::instance();
214 for(
auto& elem: _formFactorSchemeNames) {
215 for(
auto& elem2: elem.second) {
216 vector<string> chunks;
217 boost::algorithm::split(chunks, elem2.second, boost::algorithm::is_any_of(
"_"));
218 if(chunks.size() >= 2) {
219 string token = chunks[1];
220 for (
size_t idx = 2; idx < chunks.size(); ++idx) {token +=
"_" + chunks[idx];}
223 result[id][chunks[0]].push_back(token);
228 for (
auto& elem2 : _formFactorBase) {
229 vector<string> chunks;
230 boost::algorithm::split(chunks, elem2.second, boost::algorithm::is_any_of(
"_"));
231 if (chunks.size() >= 2) {
232 string token = chunks[1];
233 for (
size_t idx = 2; idx < chunks.size(); ++idx) {
234 token +=
"_" + chunks[idx];
237 for (
auto id : tmp) {
238 result[id][chunks[0]].push_back(token);
246 return _formFactorSchemes;
249 FFIndex SchemeDefinitions::getDenominatorFormFactor(
HashId processId)
const {
250 auto& den = _formFactorSchemes.find(
"Denominator")->second;
251 auto it = den.find(processId);
252 if (it != den.end()) {
259 set<FFIndex> SchemeDefinitions::getFormFactorIndices(
HashId processId)
const {
261 for (
auto& elem : _formFactorSchemes) {
262 auto it = elem.second.find(processId);
263 if (it != elem.second.end()) {
264 result.insert(it->second);
272 for (
auto& elem : _formFactorSchemes) {
273 auto it = elem.second.find(
id);
274 if (it != elem.second.end()) {
275 result.insert({elem.first, it->second});
277 MSG_ERROR(
"Process not found for scheme '" + elem.first +
"', hash Id: " + to_string(
id));
283 void SchemeDefinitions::init(map<
HashId, vector<string>> formFactGroups) {
284 _formFactorSchemes.clear();
285 auto names = getFFSchemeNames();
286 for (
auto& elem : names) {
287 auto res = _formFactorSchemes.insert({elem, map<HashId, FFIndex>{}});
289 auto dict = getScheme(elem);
290 map<HashId, string>::iterator ite;
291 for (
auto& elem2 : dict) {
292 auto it = formFactGroups.find(elem2.first);
293 if (it != formFactGroups.end()) {
294 auto it2 = find(it->second.begin(), it->second.end(), elem2.second);
295 if(it2 == it->second.end()){
296 MSG_ERROR(
"The parametrization '" + elem2.second +
"' in scheme '" + elem +
"' is unknown or misassigned. You're gonna need a bigger boat.");
299 distance(it->second.begin(), find(it->second.begin(), it->second.end(), elem2.second));
300 ptrdiff_t dim =
static_cast<ptrdiff_t
>(it->second.size());
301 if (pos >= 0 && pos < dim) {
302 res.first->second.insert({elem2.first,
static_cast<size_t>(pos)});
308 auto dict = getScheme();
309 if(dict.size() != 0){
310 auto res = _formFactorSchemes.insert({
"Denominator", map<HashId, FFIndex>{}});
312 for (
auto& elem2 : dict) {
313 auto it = formFactGroups.find(elem2.first);
314 if (it != formFactGroups.end()) {
316 distance(it->second.begin(), find(it->second.begin(), it->second.end(), elem2.second));
317 ptrdiff_t dim =
static_cast<ptrdiff_t
>(it->second.size());
318 if (pos >= 0 && pos < dim) {
319 res.first->second.insert({elem2.first,
static_cast<size_t>(pos)});
328 out << YAML::convert<SchemeDefinitions>::encode(s);
336 Node convert<::Hammer::SchemeDefinitions>::encode(const ::Hammer::SchemeDefinitions& value) {
338 if (value._formFactorSchemeNames.size() > 0) {
340 for(
auto& elem: value._formFactorSchemeNames) {
342 for(
auto& elem2: elem.second) {
343 tmpAssoc[elem2.first] = elem2.second;
345 numNode[elem.first] = tmpAssoc;
347 node[
"NumeratorSchemes"] = numNode;
350 for (
auto& elem : value._formFactorBase) {
351 tmpAssoc[elem.first] = elem.second;
353 node[
"Denominator"] = tmpAssoc;
359 for (
const auto& entry2 : node) {
360 string what = entry2.first.as<
string>();
361 if (what ==
"NumeratorSchemes") {
362 YAML::Node schemes = entry2.second;
363 if (schemes.IsMap()) {
365 for (
const auto& scheme : schemes) {
366 string name = scheme.first.as<
string>();
368 if (scheme.second.IsMap()) {
369 YAML::Node processes = scheme.second;
370 for (
const auto& process : processes) {
371 string procname = process.first.as<
string>();
372 string proctype = process.second.as<
string>();
373 res.first->second.insert({procname, proctype});
382 }
else if (what ==
"Denominator") {
384 if (entry2.second.IsMap()) {
385 YAML::Node processes = entry2.second;
386 for (
const auto& process : processes) {
387 string procname = process.first.as<
string>();
388 string proctype = process.second.as<
string>();
PDG codes to UID functions.
TensorData read(const Serial::FBTensor *msgreader)
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
std::vector< HashId > expandToValidVertexUIDs(const std::string &name, const bool &hadOnly=false) const
SchemeDict< std::map< std::string, std::string > > _formFactorSchemeNames
the Hammer numerator form factor schemes
Hammer exception definitions.
Hammer class for dealing with particle data.
Hammer particle data class.
Container class for Scheme Definitions.
std::map< std::string, std::string > _formFactorBase
the Hammer denominator form factor scheme
std::map< SchemeName, T > SchemeDict
Serialization related typedefs and includes.
Hammer settings manager class.