CMS 3D CMS Logo

IterationConfig.cc
Go to the documentation of this file.
5 
6 //#define DEBUG
7 #include "Debug.h"
8 
9 #include "nlohmann/json.hpp"
10 
11 #include <fstream>
12 #include <mutex>
13 #include <regex>
14 #include <iostream>
15 #include <iomanip>
16 
17 // Redefine to also support ordered_json ... we want to keep variable order in JSON save files.
18 #define ITCONF_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
19  inline void to_json(nlohmann::json &nlohmann_json_j, const Type &nlohmann_json_t) { \
20  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
21  } \
22  inline void from_json(const nlohmann::json &nlohmann_json_j, Type &nlohmann_json_t) { \
23  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
24  } \
25  inline void to_json(nlohmann::ordered_json &nlohmann_json_j, const Type &nlohmann_json_t) { \
26  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
27  } \
28  inline void from_json(const nlohmann::ordered_json &nlohmann_json_j, Type &nlohmann_json_t) { \
29  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
30  }
31 
32 namespace mkfit {
33 
34  // Begin AUTO code, some members commented out.
35 
37  /* int */ m_layer)
38 
40  /* std::vector<LayerControl> */ m_layer_plan,
41  /* std::string */ m_track_scorer_name,
42  /* int */ m_region,
43  /* int */ m_fwd_search_pickup,
44  /* int */ m_bkw_fit_last,
45  /* int */ m_bkw_search_pickup)
46 
48  /* int */ m_layer,
49  /* float */ m_select_min_dphi,
50  /* float */ m_select_max_dphi,
51  /* float */ m_select_min_dq,
52  /* float */ m_select_max_dq,
53  /* std::vector<float> */ m_winpars_fwd,
54  /* std::vector<float> */ m_winpars_bkw)
55 
57  /* int */ nlayers_per_seed,
58  /* int */ maxCandsPerSeed,
59  /* int */ maxHolesPerCand,
60  /* int */ maxConsecHoles,
61  /* float */ chi2Cut_min,
62  /* float */ chi2CutOverlap,
63  /* float */ pTCutOverlap,
64  /* int */ minHitsQF,
65  /* float */ minPtCut,
66  /* unsigned int */ maxClusterSize)
67 
69  /* int */ m_iteration_index,
70  /* int */ m_track_algorithm,
71  /* std::string */ m_seed_cleaner_name,
72  /* std::string */ m_seed_partitioner_name,
73  /* std::string */ m_pre_bkfit_filter_name,
74  /* std::string */ m_post_bkfit_filter_name,
75  /* std::string */ m_duplicate_cleaner_name,
76  /* std::string */ m_default_track_scorer_name,
77  /* bool */ m_requires_seed_hit_sorting,
78  /* bool */ m_backward_search,
79  /* bool */ m_backward_drop_seed_hits,
80  /* int */ m_backward_fit_min_hits,
81  /* float */ sc_ptthr_hpt,
82  /* float */ sc_drmax_bh,
83  /* float */ sc_dzmax_bh,
84  /* float */ sc_drmax_eh,
85  /* float */ sc_dzmax_eh,
86  /* float */ sc_drmax_bl,
87  /* float */ sc_dzmax_bl,
88  /* float */ sc_drmax_el,
89  /* float */ sc_dzmax_el,
90  /* float */ dc_fracSharedHits,
91  /* float */ dc_drth_central,
92  /* float */ dc_drth_obarrel,
93  /* float */ dc_drth_forward,
94  /* mkfit::IterationParams */ m_params,
95  /* mkfit::IterationParams */ m_backward_params,
96  /* int */ m_n_regions,
97  /* vector<int> */ m_region_order,
98  /* vector<mkfit::SteeringParams> */ m_steering_params,
99  /* vector<mkfit::IterationLayerConfig> */ m_layer_configs)
100 
102  /* vector<mkfit::IterationConfig> */ m_iterations)
103 
104  // End AUTO code.
105 
106  // Begin IterationConfig function catalogs
107 
108  namespace {
109  struct FuncCatalog {
110  std::map<std::string, clean_seeds_func> seed_cleaners;
111  std::map<std::string, partition_seeds_func> seed_partitioners;
112  std::map<std::string, filter_candidates_func> candidate_filters;
113  std::map<std::string, clean_duplicates_func> duplicate_cleaners;
114  std::map<std::string, track_score_func> track_scorers;
115 
116  std::mutex catalog_mutex;
117  };
118 
119  FuncCatalog &get_catalog() {
120  CMS_SA_ALLOW static FuncCatalog func_catalog;
121  return func_catalog;
122  }
123  } // namespace
124 
125 #define GET_FC \
126  auto &fc = get_catalog(); \
127  const std::lock_guard<std::mutex> lock(fc.catalog_mutex)
128 
130  GET_FC;
131  fc.seed_cleaners.insert({name, func});
132  }
134  GET_FC;
135  fc.seed_partitioners.insert({name, func});
136  }
138  GET_FC;
139  fc.candidate_filters.insert({name, func});
140  }
142  GET_FC;
143  fc.duplicate_cleaners.insert({name, func});
144  }
146  GET_FC;
147  fc.track_scorers.insert({name, func});
148  }
149 
150  namespace {
151  template <class T>
152  typename T::mapped_type resolve_func_name(const T &cont, const std::string &name, const char *func) {
153  if (name.empty()) {
154  return nullptr;
155  }
156  auto ii = cont.find(name);
157  if (ii == cont.end()) {
158  std::string es(func);
159  es += " '" + name + "' not found in function registry.";
160  throw std::runtime_error(es);
161  }
162  return ii->second;
163  }
164  } // namespace
165 
167  GET_FC;
168  return resolve_func_name(fc.seed_cleaners, name, __func__);
169  }
171  GET_FC;
172  return resolve_func_name(fc.seed_partitioners, name, __func__);
173  }
175  GET_FC;
176  return resolve_func_name(fc.candidate_filters, name, __func__);
177  }
179  GET_FC;
180  return resolve_func_name(fc.duplicate_cleaners, name, __func__);
181  }
183  GET_FC;
184  return resolve_func_name(fc.track_scorers, name, __func__);
185  }
186 
187 #undef GET_FC
188 
189  // End IterationConfig function catalogs
190 
193  dprintf(" Set seed_cleaner for '%s' %s\n", m_seed_cleaner_name.c_str(), m_seed_cleaner ? "SET" : "NOT SET");
194 
196  dprintf(
197  " Set seed_partitioner for '%s' %s\n", m_seed_partitioner_name.c_str(), m_seed_partitioner ? "SET" : "NOT SET");
198 
200  dprintf(
201  " Set pre_bkfit_filter for '%s' %s\n", m_pre_bkfit_filter_name.c_str(), m_pre_bkfit_filter ? "SET" : "NOT SET");
202 
204  dprintf(" Set post_bkfit_filter for '%s' %s\n",
205  m_post_bkfit_filter_name.c_str(),
206  m_post_bkfit_filter ? "SET" : "NOT SET");
207 
209  dprintf(" Set duplicate_cleaner for '%s' %s\n",
210  m_duplicate_cleaner_name.c_str(),
211  m_duplicate_cleaner ? "SET" : "NOT SET");
212 
214  for (auto &sp : m_steering_params) {
215  sp.m_track_scorer =
216  sp.m_track_scorer_name.empty() ? m_default_track_scorer : get_track_scorer(sp.m_track_scorer_name);
217  }
218  }
219 
220  // ============================================================================
221  // ConfigJsonPatcher
222  // ============================================================================
223 
225 
227 
229  std::string s;
230  s.reserve(64);
231  for (auto &p : m_path_stack)
232  s += p;
233  return s;
234  }
235 
237  std::string s;
238  s.reserve(128);
239  s = "ConfigJsonPatcher";
240  if (func) {
241  s += "::";
242  s += func;
243  }
244  s += " '";
245  s += get_abs_path();
246  s += "' ";
247  return s;
248  }
249 
250  template <class T>
251  void ConfigJsonPatcher::load(const T &o) {
252  m_json = std::make_unique<nlohmann::json>();
253  *m_json = o;
254  cd_top();
255  }
256  template void ConfigJsonPatcher::load<IterationsInfo>(const IterationsInfo &o);
257  template void ConfigJsonPatcher::load<IterationConfig>(const IterationConfig &o);
258 
259  template <class T>
261  from_json(*m_json, o);
262  }
263  template void ConfigJsonPatcher::save<IterationConfig>(IterationConfig &o);
264 
265  // Must not bork the IterationConfig elements of IterationsInfo ... default
266  // deserializator apparently reinitializes the vectors with defaults c-tors.
267  template <>
268  void ConfigJsonPatcher::save<IterationsInfo>(IterationsInfo &o) {
269  auto &itc_arr = m_json->at("m_iterations");
270  for (int i = 0; i < o.size(); ++i) {
271  from_json(itc_arr[i], o[i]);
272  }
273  }
274 
276  nlohmann::json::json_pointer jp(path);
277  m_json_stack.push_back(m_current);
278  m_path_stack.push_back(path);
279  m_current = &m_current->at(jp);
280  }
281 
283  if (m_json_stack.empty())
284  throw std::runtime_error("JSON stack empty on cd_up");
285 
286  m_current = m_json_stack.back();
287  m_json_stack.pop_back();
288  m_path_stack.pop_back();
289  if (!path.empty())
290  cd(path);
291  }
292 
294  m_current = m_json.get();
295  m_json_stack.clear();
296  m_path_stack.clear();
297  if (!path.empty())
298  cd(path);
299  }
300 
301  template <typename T>
303  nlohmann::json::json_pointer jp(path);
304  m_current->at(jp) = val;
305  }
306  template void ConfigJsonPatcher::replace<int>(const std::string &path, int val);
307  template void ConfigJsonPatcher::replace<float>(const std::string &path, float val);
308  template void ConfigJsonPatcher::replace<double>(const std::string &path, double val);
309 
310  template <typename T>
312  nlohmann::json::json_pointer jp(path);
313  for (int i = first; i <= last; ++i) {
314  m_current->at(i).at(jp) = val;
315  }
316  }
317  template void ConfigJsonPatcher::replace<int>(int first, int last, const std::string &path, int val);
318  template void ConfigJsonPatcher::replace<float>(int first, int last, const std::string &path, float val);
319  template void ConfigJsonPatcher::replace<double>(int first, int last, const std::string &path, double val);
320 
322  nlohmann::json::json_pointer jp(path);
323  return m_current->at(jp);
324  }
325 
327  if (j.is_null())
328  throw std::runtime_error(exc_hdr(__func__) + "null not expected");
329 
330  if (j.is_boolean() || j.is_number() || j.is_string()) {
331  throw std::runtime_error(exc_hdr(__func__) + "value not expected on this parsing level");
332  }
333 
334  int n_replaced = 0;
335 
336  if (j.is_object()) {
337  static const std::regex index_range_re("^\\[(\\d+)..(\\d+)\\]$", std::regex::optimize);
338 
339  for (auto &[key, value] : j.items()) {
340  std::smatch m;
341  std::regex_search(key, m, index_range_re);
342 
343  if (m.size() == 3) {
344  if (!m_current->is_array())
345  throw std::runtime_error(exc_hdr(__func__) + "array range encountered when current json is not an array");
346  int first = std::stoi(m.str(1));
347  int last = std::stoi(m.str(2));
348  for (int i = first; i <= last; ++i) {
349  std::string s("/");
350  s += std::to_string(i);
351  cd(s);
352  if (value.is_array()) {
353  for (auto &el : value)
354  n_replaced += replace(el);
355  } else {
356  n_replaced += replace(value);
357  }
358  cd_up();
359  }
360  } else if (value.is_array() || value.is_object()) {
361  std::string s("/");
362  s += key;
363  cd(s);
364  n_replaced += replace(value);
365  cd_up();
366  } else if (value.is_number() || value.is_boolean() || value.is_string()) {
367  std::string s("/");
368  s += key;
369  nlohmann::json::json_pointer jp(s);
370  if (m_current->at(jp) != value) {
371  if (m_verbose)
372  std::cout << " " << get_abs_path() << s << ": " << m_current->at(jp) << " -> " << value << "\n";
373 
374  m_current->at(jp) = value;
375  ++n_replaced;
376  }
377  } else {
378  throw std::runtime_error(exc_hdr(__func__) + "unexpected value type");
379  }
380  }
381  } else if (j.is_array() && j.empty()) {
382  } else if (j.is_array()) {
383  // Arrays are somewhat tricky.
384  // At the moment all elements are expected to be objects.
385  // This means arrays of basic types are not supported (like layer index arrays).
386  // Should not be too hard to add support for this.
387  // Now, the objects in the array can be of two kinds:
388  // a) Their keys can be json_pointer strings starting with numbers or ranges [i_low..i_high].
389  // b) They can be actual elements of the array. In this case we require the length of
390  // the array to be equal to existing length in the configuration.
391  // It is not allowed for these two kinds to mix.
392 
393  // Determine the kind of array: json_ptr or object
394 
395  static const std::regex index_re("^(?:\\[\\d+..\\d+\\]|\\d+(?:/.*)?)$", std::regex::optimize);
396 
397  bool has_index = false, has_plain = false;
398  for (int i = 0; i < (int)j.size(); ++i) {
399  const nlohmann::json &el = j[i];
400 
401  if (!el.is_object())
402  throw std::runtime_error(exc_hdr(__func__) + "array elements expected to be objects");
403 
404  for (nlohmann::json::const_iterator it = el.begin(); it != el.end(); ++it) {
405  if (std::regex_search(it.key(), index_re)) {
406  has_index = true;
407  if (has_plain)
408  throw std::runtime_error(exc_hdr(__func__) + "indexed array entry following plain one");
409  } else {
410  has_plain = true;
411  if (has_index)
412  throw std::runtime_error(exc_hdr(__func__) + "plain array entry following indexed one");
413  }
414  }
415  }
416  if (has_index) {
417  for (auto &element : j) {
418  n_replaced += replace(element);
419  }
420  } else {
421  if (m_current && !m_current->is_array())
422  throw std::runtime_error(exc_hdr(__func__) + "plain array detected when current is not an array");
423  if (m_current->size() != j.size())
424  throw std::runtime_error(exc_hdr(__func__) + "plain array of different size than at current pos");
425 
426  std::string s;
427  for (int i = 0; i < (int)j.size(); ++i) {
428  s = "/";
429  s += std::to_string(i);
430  cd(s);
431  n_replaced += replace(j[i]);
432  cd_up();
433  }
434  }
435  } else {
436  throw std::runtime_error(exc_hdr(__func__) + "unexpected json type");
437  }
438 
439  return n_replaced;
440  }
441 
443 
444  // ============================================================================
445  // patch_File steering function
446  // ============================================================================
447  /*
448  See example JSON patcher input: "mkFit/config-parse/test.json"
449 
450  The file can contain several valid JSON dumps in sequence.
451 
452  '/' character can be used to descend more than one level at a time.
453 
454  A number can be used to specify an array index. This can be combined with
455  the '/' syntax.
456 
457  "[first,last]" key (as string) can be used to denote a range of array
458  elements. Such a key must not be combined with a '/' syntax.
459 */
460 
461  namespace {
462  // Open file for writing, throw exception on failure.
463  void open_ofstream(std::ofstream &ofs, const std::string &fname, const char *pfx = nullptr) {
464  ofs.open(fname, std::ofstream::trunc);
465  if (!ofs) {
466  char m[2048];
467  snprintf(m, 2048, "%s%sError opening %s for write: %m", pfx ? pfx : "", pfx ? " " : "", fname.c_str());
468  throw std::runtime_error(m);
469  }
470  }
471 
472  // Open file for reading, throw exception on failure.
473  void open_ifstream(std::ifstream &ifs, const std::string &fname, const char *pfx = nullptr) {
474  ifs.open(fname);
475  if (!ifs) {
476  char m[2048];
477  snprintf(m, 2048, "%s%sError opening %s for read: %m", pfx ? pfx : "", pfx ? " " : "", fname.c_str());
478  throw std::runtime_error(m);
479  }
480  }
481 
482  // Skip white-space, return true if more characters are available, false if eof.
483  bool skipws_ifstream(std::ifstream &ifs) {
484  while (std::isspace(ifs.peek()))
485  ifs.get();
486  return !ifs.eof();
487  }
488  } // namespace
489 
491  const std::vector<std::string> &fnames,
494  cjp.load(its_info);
495 
497 
498  for (auto &fname : fnames) {
499  std::ifstream ifs;
500  open_ifstream(ifs, fname, __func__);
501 
502  if (m_verbose) {
503  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
504  }
505 
506  int n_read = 0, n_tot_replaced = 0;
507  while (skipws_ifstream(ifs)) {
509  ifs >> j;
510  ++n_read;
511 
512  if (m_verbose) {
513  std::cout << " Read JSON entity " << n_read << " -- applying patch:\n";
514  // std::cout << j.dump(3) << "\n";
515  }
516 
517  int n_replaced = cjp.replace(j);
518 
519  if (m_verbose) {
520  std::cout << " Replaced " << n_replaced << " entries.\n";
521  }
522  cjp.cd_top();
523  n_tot_replaced += n_replaced;
524  }
525 
526  if (m_verbose) {
527  printf("%s read %d JSON entities from file %s, replaced %d parameters.\n",
528  __func__,
529  n_read,
530  fname.c_str(),
531  n_tot_replaced);
532  }
533 
534  ifs.close();
535 
536  rep.inc_counts(1, n_read, n_tot_replaced);
537  }
538 
539  if (rep.n_replacements > 0) {
540  cjp.save(its_info);
541  }
542 
543  if (report)
544  report->inc_counts(rep);
545  }
546 
547  std::unique_ptr<IterationConfig> ConfigJson::patchLoad_File(const IterationsInfo &its_info,
548  const std::string &fname,
551 
552  std::ifstream ifs;
553  open_ifstream(ifs, fname, __func__);
554 
555  if (m_verbose) {
556  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
557  }
558 
559  if (!skipws_ifstream(ifs))
560  throw std::runtime_error("empty file");
561 
563  ifs >> j;
564  int track_algo = j["m_track_algorithm"];
565 
566  int iii = -1;
567  for (int i = 0; i < its_info.size(); ++i) {
568  if (its_info[i].m_track_algorithm == track_algo) {
569  iii = i;
570  break;
571  }
572  }
573  if (iii == -1)
574  throw std::runtime_error("matching IterationConfig not found");
575 
576  if (m_verbose) {
577  std::cout << " Read JSON entity, Iteration index is " << iii << " -- cloning and applying JSON patch:\n";
578  }
579 
580  IterationConfig *icp = new IterationConfig(its_info[iii]);
581  IterationConfig &ic = *icp;
582 
584  cjp.load(ic);
585 
586  int n_replaced = cjp.replace(j);
587 
588  cjp.cd_top();
589 
590  if (m_verbose) {
591  printf("%s read 1 JSON entity from file %s, replaced %d parameters.\n", __func__, fname.c_str(), n_replaced);
592  }
593 
594  ifs.close();
595 
596  rep.inc_counts(1, 1, n_replaced);
597 
598  if (rep.n_replacements > 0) {
599  cjp.save(ic);
600  }
601 
602  if (report)
603  report->inc_counts(rep);
604 
605  return std::unique_ptr<IterationConfig>(icp);
606  }
607 
608  std::unique_ptr<IterationConfig> ConfigJson::load_File(const std::string &fname) {
609  std::ifstream ifs;
610  open_ifstream(ifs, fname, __func__);
611 
612  if (m_verbose) {
613  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
614  }
615 
616  if (!skipws_ifstream(ifs))
617  throw std::runtime_error("empty file");
618 
620  ifs >> j;
621 
622  if (m_verbose) {
623  std::cout << " Read JSON entity, iteration index is " << j["m_iteration_index"] << ", track algorithm is "
624  << j["m_track_algorithm"] << ". Instantiating IterationConfig object and over-laying it with JSON.\n";
625  }
626 
627  IterationConfig *icp = new IterationConfig();
628 
629  from_json(j, *icp);
630 
631  return std::unique_ptr<IterationConfig>(icp);
632  }
633 
634  // ============================================================================
635  // Save each IterationConfig into a separate json file
636  // ============================================================================
637 
639  const std::string &fname_fmt,
640  bool include_iter_info_preamble) {
641  bool has_pct_d = fname_fmt.find("%d") != std::string::npos;
642  bool has_pct_s = fname_fmt.find("%s") != std::string::npos;
643 
644  assert((has_pct_d || has_pct_s) && "JSON save filename-format must include a %d or %s substring");
645  assert(!(has_pct_d && has_pct_s) && "JSON save filename-format must include only one of %d or %s substrings");
646 
647  for (int ii = 0; ii < its_info.size(); ++ii) {
648  const IterationConfig &itconf = its_info[ii];
649 
650  char fname[1024];
651  if (has_pct_d)
652  snprintf(fname, 1024, fname_fmt.c_str(), ii);
653  else
654  snprintf(fname, 1024, fname_fmt.c_str(), TrackBase::algoint_to_cstr(itconf.m_track_algorithm));
655 
656  std::ofstream ofs;
657  open_ofstream(ofs, fname, __func__);
658 
659  if (include_iter_info_preamble) {
660  ofs << "{ \"m_iterations/" << ii << "\": ";
661  }
662 
663  nlohmann::ordered_json j;
664  to_json(j, itconf);
665 
666  ofs << std::setw(1);
667  ofs << j;
668 
669  if (include_iter_info_preamble) {
670  ofs << " }";
671  }
672 
673  ofs << "\n";
674  ofs.close();
675  }
676  }
677 
679  nlohmann::ordered_json j = its_info;
680  std::cout << j.dump(3) << "\n";
681  }
682 
683  // ============================================================================
684  // Tests for ConfigJson stuff
685  // ============================================================================
686 
688  using nlohmann::json;
689 
690  std::string lojz("/m_select_max_dphi");
691 
692  json j = it_cfg;
693  std::cout << j.dump(1) << "\n";
694 
695  std::cout << "Layer 43, m_select_max_dphi = " << j["/m_layer_configs/43/m_select_max_dphi"_json_pointer] << "\n";
696  std::cout << "Patching it to pi ...\n";
697  json p = R"([
698  { "op": "replace", "path": "/m_layer_configs/43/m_select_max_dphi", "value": 3.141 }
699  ])"_json;
700  j = j.patch(p);
701  std::cout << "Layer 43, m_select_max_dphi = " << j["/m_layer_configs/43/m_select_max_dphi"_json_pointer] << "\n";
702 
703  auto &jx = j["/m_layer_configs/60"_json_pointer];
704  // jx["m_select_max_dphi"] = 99.876;
705  json::json_pointer jp(lojz);
706  jx[jp] = 99.876;
707 
708  // try loading it back, see what happens to vector m_layer_configs.
709 
710  from_json(j, it_cfg);
711  printf("Layer 43 : m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
712  it_cfg.m_layer_configs[43].m_select_max_dphi,
713  (int)it_cfg.m_layer_configs.size(),
714  it_cfg.m_n_regions,
715  (int)it_cfg.m_steering_params.size());
716 
717  printf("Layer 60 : m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
718  it_cfg.m_layer_configs[60].m_select_max_dphi,
719  (int)it_cfg.m_layer_configs.size(),
720  it_cfg.m_n_regions,
721  (int)it_cfg.m_steering_params.size());
722 
723  // try accessing something that does not exist
724 
725  // std::cout << "Non-existent path " << j["/m_layer_configs/143/m_select_max_dphi"_json_pointer] << "\n";
726 
727  auto &x = j["/m_layer_configs"_json_pointer];
728  std::cout << "Typename /m_layer_configs " << x.type_name() << "\n";
729  auto &y = j["/m_layer_configs/143"_json_pointer];
730  std::cout << "Typename /m_layer_configs/143 " << y.type_name() << ", is_null=" << y.is_null() << "\n";
731  }
732 
734  ConfigJsonPatcher cjp;
735  cjp.load(it_cfg);
736 
737  std::cout << cjp.dump(1) << "\n";
738 
739  {
740  cjp.cd("/m_layer_configs/43/m_select_max_dphi");
741  std::cout << "Layer 43, m_select_max_dphi = " << cjp.get("") << "\n";
742  std::cout << "Setting it to pi ...\n";
743  cjp.replace("", 3.141);
744  cjp.cd_top();
745  std::cout << "Layer 43, m_select_max_dphi = " << cjp.get("/m_layer_configs/43/m_select_max_dphi") << "\n";
746  }
747  {
748  std::cout << "Replacing layer 60 m_select_max_dphi with full path\n";
749  cjp.replace("/m_layer_configs/60/m_select_max_dphi", 99.876);
750  }
751  try {
752  std::cout << "Trying to replace an non-existent array entry\n";
753  cjp.replace("/m_layer_configs/1460/m_select_max_dphi", 666.666);
754  } catch (std::exception &exc) {
755  std::cout << "Caugth exception: " << exc.what() << "\n";
756  }
757  try {
758  std::cout << "Trying to replace an non-existent object entry\n";
759  cjp.replace("/m_layer_configs/1/moo_select_max_dphi", 666.666);
760  } catch (std::exception &exc) {
761  std::cout << "Caugth exception: " << exc.what() << "\n";
762  }
763  {
764  std::cout << "Replacing m_select_max_dphi on layers 1 to 3 to 7.7\n";
765  cjp.cd("/m_layer_configs");
766  cjp.replace(1, 3, "/m_select_max_dphi", 7.7);
767  cjp.cd_top();
768  }
769 
770  // try getting it back into c++, see what happens to vector m_layer_configs.
771 
772  cjp.save(it_cfg);
773 
774  printf("Layer 43: m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
775  it_cfg.m_layer_configs[43].m_select_max_dphi,
776  (int)it_cfg.m_layer_configs.size(),
777  it_cfg.m_n_regions,
778  (int)it_cfg.m_steering_params.size());
779 
780  printf("Layer 60: m_select_max_dphi = %f\n", it_cfg.m_layer_configs[60].m_select_max_dphi);
781  for (int i = 0; i < 5; ++i)
782  printf("Layer %2d: m_select_max_dphi = %f\n", i, it_cfg.m_layer_configs[i].m_select_max_dphi);
783 
784  // try accessing something that does not exist
785 
786  // std::cout << "Non-existent path " << j["/m_layer_configs/143/m_select_max_dphi"_json_pointer] << "\n";
787 
788  auto &j = cjp.get("");
789 
790  auto &x = j["/m_layer_configs"_json_pointer];
791  std::cout << "Typename /m_layer_configs " << x.type_name() << "\n";
792  auto &y = j["/m_layer_configs/143"_json_pointer];
793  std::cout << "Typename /m_layer_configs/143 " << y.type_name() << ", is_null=" << y.is_null() << "\n";
794  }
795 
796 } // namespace mkfit
nlohmann::json * m_current
std::string m_pre_bkfit_filter_name
#define CMS_SA_ALLOW
static void register_seed_partitioner(const std::string &name, partition_seeds_func func)
static void register_track_scorer(const std::string &name, track_score_func func)
nlohmann::json json
bool verbose
void to_json(nlohmann::json &nlohmann_json_j, const mkfit::LayerControl &nlohmann_json_t)
static std::mutex mutex
Definition: Proxy.cc:8
std::string to_string(const V &value)
Definition: OMSAccess.h:71
std::string dump(int indent=2)
std::string m_seed_cleaner_name
std::string m_seed_partitioner_name
static void register_duplicate_cleaner(const std::string &name, clean_duplicates_func func)
assert(be >=bs)
std::function< track_score_cf > track_score_func
Definition: FunctionTypes.h:40
clean_seeds_func m_seed_cleaner
void replace(const std::string &path, T val)
#define ITCONF_DEFINE_TYPE_NON_INTRUSIVE(Type,...)
static filter_candidates_func get_candidate_filter(const std::string &name)
void test_Direct(IterationConfig &it_cfg)
void patch_Files(IterationsInfo &its_info, const std::vector< std::string > &fnames, ConfigJsonPatcher::PatchReport *report=nullptr)
std::string exc_hdr(const char *func=nullptr) const
std::function< filter_candidates_cf > filter_candidates_func
Definition: FunctionTypes.h:28
std::vector< nlohmann::json * > m_json_stack
#define GET_FC
std::vector< IterationLayerConfig > m_layer_configs
static void register_seed_cleaner(const std::string &name, clean_seeds_func func)
static partition_seeds_func get_seed_partitioner(const std::string &name)
std::string m_default_track_scorer_name
partition_seeds_func m_seed_partitioner
std::unique_ptr< IterationConfig > patchLoad_File(const IterationsInfo &its_info, const std::string &fname, ConfigJsonPatcher::PatchReport *report=nullptr)
nlohmann::json & get(const std::string &path)
std::string m_post_bkfit_filter_name
void cd(const std::string &path)
Definition: value.py:1
std::vector< std::string > m_path_stack
void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble)
rep
Definition: cuy.py:1189
void test_Patcher(IterationConfig &it_cfg)
ii
Definition: cuy.py:589
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
static const char * algoint_to_cstr(int algo)
Definition: Track.cc:204
filter_candidates_func m_post_bkfit_filter
static track_score_func get_track_scorer(const std::string &name)
static clean_duplicates_func get_duplicate_cleaner(const std::string &name)
clean_duplicates_func m_duplicate_cleaner
std::unique_ptr< IterationConfig > load_File(const std::string &fname)
void dump(IterationsInfo &its_info)
string fname
main script
std::unique_ptr< nlohmann::json > m_json
void cd_up(const std::string &path="")
float x
ConfigJsonPatcher(bool verbose=false)
track_score_func m_default_track_scorer
std::function< clean_seeds_cf > clean_seeds_func
Definition: FunctionTypes.h:22
static clean_seeds_func get_seed_cleaner(const std::string &name)
filter_candidates_func m_pre_bkfit_filter
void cd_top(const std::string &path="")
long double T
std::vector< SteeringParams > m_steering_params
std::function< clean_duplicates_cf > clean_duplicates_func
Definition: FunctionTypes.h:31
std::string m_duplicate_cleaner_name
static void register_candidate_filter(const std::string &name, filter_candidates_func func)
#define dprintf(...)
Definition: Debug.h:98
cont
load Luminosity info ##
Definition: generateEDF.py:628
std::function< partition_seeds_cf > partition_seeds_func
Definition: FunctionTypes.h:25
std::string get_abs_path() const