CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
mkfit::ConfigJson Class Reference

#include <IterationConfig.h>

Public Member Functions

 ConfigJson (bool verbose=false)
 
void dump (IterationsInfo &its_info)
 
std::unique_ptr< IterationConfigload_File (const std::string &fname)
 
void patch_Files (IterationsInfo &its_info, const std::vector< std::string > &fnames, ConfigJsonPatcher::PatchReport *report=nullptr)
 
std::unique_ptr< IterationConfigpatchLoad_File (const IterationsInfo &its_info, const std::string &fname, ConfigJsonPatcher::PatchReport *report=nullptr)
 
void save_Iterations (IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble)
 
void test_Direct (IterationConfig &it_cfg)
 
void test_Patcher (IterationConfig &it_cfg)
 

Private Attributes

bool m_verbose = false
 

Detailed Description

Definition at line 337 of file IterationConfig.h.

Constructor & Destructor Documentation

◆ ConfigJson()

mkfit::ConfigJson::ConfigJson ( bool  verbose = false)
inline

Definition at line 339 of file IterationConfig.h.

Member Function Documentation

◆ dump()

void mkfit::ConfigJson::dump ( IterationsInfo its_info)

Definition at line 568 of file IterationConfig.cc.

References gather_cfg::cout, and dqmiolumiharvest::j.

Referenced by initGeom().

568  {
569  nlohmann::ordered_json j = its_info;
570  std::cout << j.dump(3) << "\n";
571  }

◆ load_File()

std::unique_ptr< IterationConfig > mkfit::ConfigJson::load_File ( const std::string &  fname)

Definition at line 498 of file IterationConfig.cc.

References gather_cfg::cout, alignmentValidation::fname, mkfit::from_json(), dqmiolumiharvest::j, and m_verbose.

Referenced by MkFitIterationConfigESProducer::produce().

498  {
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  if (!skipws_ifstream(ifs))
507  throw std::runtime_error("empty file");
508 
510  ifs >> j;
511 
512  if (m_verbose) {
513  std::cout << " Read JSON entity, iteration index is " << j["m_iteration_index"] << ", track algorithm is "
514  << j["m_track_algorithm"] << ". Instantiating IterationConfig object and over-laying it with JSON.\n";
515  }
516 
517  IterationConfig *icp = new IterationConfig();
518 
519  from_json(j, *icp);
520 
521  return std::unique_ptr<IterationConfig>(icp);
522  }
nlohmann::json json
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
string fname
main script

◆ patch_Files()

void mkfit::ConfigJson::patch_Files ( IterationsInfo its_info,
const std::vector< std::string > &  fnames,
ConfigJsonPatcher::PatchReport report = nullptr 
)

Definition at line 380 of file IterationConfig.cc.

References mkfit::ConfigJsonPatcher::cd_top(), gather_cfg::cout, alignmentValidation::fname, SiStripCommissioningSource_FromRAW_cfg::fnames, dqmiolumiharvest::j, mkfit::ConfigJsonPatcher::load(), m_verbose, cuy::rep, mkfit::ConfigJsonPatcher::replace(), edmIntegrityCheck::report, and mkfit::ConfigJsonPatcher::save().

Referenced by initGeom().

382  {
383  ConfigJsonPatcher cjp(m_verbose);
384  cjp.load(its_info);
385 
386  ConfigJsonPatcher::PatchReport rep;
387 
388  for (auto &fname : fnames) {
389  std::ifstream ifs;
390  open_ifstream(ifs, fname, __func__);
391 
392  if (m_verbose) {
393  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
394  }
395 
396  int n_read = 0, n_tot_replaced = 0;
397  while (skipws_ifstream(ifs)) {
399  ifs >> j;
400  ++n_read;
401 
402  if (m_verbose) {
403  std::cout << " Read JSON entity " << n_read << " -- applying patch:\n";
404  // std::cout << j.dump(3) << "\n";
405  }
406 
407  int n_replaced = cjp.replace(j);
408 
409  if (m_verbose) {
410  std::cout << " Replaced " << n_replaced << " entries.\n";
411  }
412  cjp.cd_top();
413  n_tot_replaced += n_replaced;
414  }
415 
416  if (m_verbose) {
417  printf("%s read %d JSON entities from file %s, replaced %d parameters.\n",
418  __func__,
419  n_read,
420  fname.c_str(),
421  n_tot_replaced);
422  }
423 
424  ifs.close();
425 
426  rep.inc_counts(1, n_read, n_tot_replaced);
427  }
428 
429  if (rep.n_replacements > 0) {
430  cjp.save(its_info);
431  }
432 
433  if (report)
434  report->inc_counts(rep);
435  }
nlohmann::json json
rep
Definition: cuy.py:1189
string fname
main script

◆ patchLoad_File()

std::unique_ptr< IterationConfig > mkfit::ConfigJson::patchLoad_File ( const IterationsInfo its_info,
const std::string &  fname,
ConfigJsonPatcher::PatchReport report = nullptr 
)

Definition at line 437 of file IterationConfig.cc.

References mkfit::ConfigJsonPatcher::cd_top(), gather_cfg::cout, alignmentValidation::fname, mps_fire::i, dqmiolumiharvest::j, mkfit::ConfigJsonPatcher::load(), m_verbose, cuy::rep, mkfit::ConfigJsonPatcher::replace(), edmIntegrityCheck::report, mkfit::ConfigJsonPatcher::save(), and mkfit::IterationsInfo::size().

Referenced by initGeom().

439  {
440  ConfigJsonPatcher::PatchReport rep;
441 
442  std::ifstream ifs;
443  open_ifstream(ifs, fname, __func__);
444 
445  if (m_verbose) {
446  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
447  }
448 
449  if (!skipws_ifstream(ifs))
450  throw std::runtime_error("empty file");
451 
453  ifs >> j;
454  int track_algo = j["m_track_algorithm"];
455 
456  int iii = -1;
457  for (int i = 0; i < its_info.size(); ++i) {
458  if (its_info[i].m_track_algorithm == track_algo) {
459  iii = i;
460  break;
461  }
462  }
463  if (iii == -1)
464  throw std::runtime_error("matching IterationConfig not found");
465 
466  if (m_verbose) {
467  std::cout << " Read JSON entity, Iteration index is " << iii << " -- cloning and applying JSON patch:\n";
468  }
469 
470  IterationConfig *icp = new IterationConfig(its_info[iii]);
471  IterationConfig &ic = *icp;
472 
473  ConfigJsonPatcher cjp(m_verbose);
474  cjp.load(ic);
475 
476  int n_replaced = cjp.replace(j);
477 
478  cjp.cd_top();
479 
480  if (m_verbose) {
481  printf("%s read 1 JSON entity from file %s, replaced %d parameters.\n", __func__, fname.c_str(), n_replaced);
482  }
483 
484  ifs.close();
485 
486  rep.inc_counts(1, 1, n_replaced);
487 
488  if (rep.n_replacements > 0) {
489  cjp.save(ic);
490  }
491 
492  if (report)
493  report->inc_counts(rep);
494 
495  return std::unique_ptr<IterationConfig>(icp);
496  }
nlohmann::json json
rep
Definition: cuy.py:1189
string fname
main script

◆ save_Iterations()

void mkfit::ConfigJson::save_Iterations ( IterationsInfo its_info,
const std::string &  fname_fmt,
bool  include_iter_info_preamble 
)

Definition at line 528 of file IterationConfig.cc.

References mkfit::TrackBase::algoint_to_cstr(), cms::cuda::assert(), alignmentValidation::fname, cuy::ii, dqmiolumiharvest::j, mkfit::IterationConfig::m_track_algorithm, mkfit::IterationsInfo::size(), and mkfit::to_json().

Referenced by initGeom().

530  {
531  bool has_pct_d = fname_fmt.find("%d") != std::string::npos;
532  bool has_pct_s = fname_fmt.find("%s") != std::string::npos;
533 
534  assert((has_pct_d || has_pct_s) && "JSON save filename-format must include a %d or %s substring");
535  assert(!(has_pct_d && has_pct_s) && "JSON save filename-format must include only one of %d or %s substrings");
536 
537  for (int ii = 0; ii < its_info.size(); ++ii) {
538  const IterationConfig &itconf = its_info[ii];
539 
540  char fname[1024];
541  if (has_pct_d)
542  snprintf(fname, 1024, fname_fmt.c_str(), ii);
543  else
544  snprintf(fname, 1024, fname_fmt.c_str(), TrackBase::algoint_to_cstr(itconf.m_track_algorithm));
545 
546  std::ofstream ofs;
547  open_ofstream(ofs, fname, __func__);
548 
549  if (include_iter_info_preamble) {
550  ofs << "{ \"m_iterations/" << ii << "\": ";
551  }
552 
553  nlohmann::ordered_json j;
554  to_json(j, itconf);
555 
556  ofs << std::setw(1);
557  ofs << j;
558 
559  if (include_iter_info_preamble) {
560  ofs << " }";
561  }
562 
563  ofs << "\n";
564  ofs.close();
565  }
566  }
void to_json(nlohmann::json &nlohmann_json_j, const mkfit::LayerControl &nlohmann_json_t)
assert(be >=bs)
ii
Definition: cuy.py:589
static const char * algoint_to_cstr(int algo)
Definition: Track.cc:203
string fname
main script

◆ test_Direct()

void mkfit::ConfigJson::test_Direct ( IterationConfig it_cfg)

Definition at line 577 of file IterationConfig.cc.

References gather_cfg::cout, mkfit::from_json(), createfilelist::int, dqmiolumiharvest::j, mkfit::IterationConfig::m_layer_configs, mkfit::IterationConfig::m_n_regions, mkfit::IterationConfig::m_steering_params, AlCaHLTBitMon_ParallelJobs::p, AlCaHLTBitMon_QueryRunRegistry::string, and x.

577  {
578  using nlohmann::json;
579 
580  std::string lojz("/m_select_max_dphi");
581 
582  json j = it_cfg;
583  std::cout << j.dump(1) << "\n";
584 
585  std::cout << "Layer 43, m_select_max_dphi = " << j["/m_layer_configs/43/m_select_max_dphi"_json_pointer] << "\n";
586  std::cout << "Patching it to pi ...\n";
587  json p = R"([
588  { "op": "replace", "path": "/m_layer_configs/43/m_select_max_dphi", "value": 3.141 }
589  ])"_json;
590  j = j.patch(p);
591  std::cout << "Layer 43, m_select_max_dphi = " << j["/m_layer_configs/43/m_select_max_dphi"_json_pointer] << "\n";
592 
593  auto &jx = j["/m_layer_configs/60"_json_pointer];
594  // jx["m_select_max_dphi"] = 99.876;
595  json::json_pointer jp(lojz);
596  jx[jp] = 99.876;
597 
598  // try loading it back, see what happens to vector m_layer_configs.
599 
600  from_json(j, it_cfg);
601  printf("Layer 43 : m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
602  it_cfg.m_layer_configs[43].m_select_max_dphi,
603  (int)it_cfg.m_layer_configs.size(),
604  it_cfg.m_n_regions,
605  (int)it_cfg.m_steering_params.size());
606 
607  printf("Layer 60 : m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
608  it_cfg.m_layer_configs[60].m_select_max_dphi,
609  (int)it_cfg.m_layer_configs.size(),
610  it_cfg.m_n_regions,
611  (int)it_cfg.m_steering_params.size());
612 
613  // try accessing something that does not exist
614 
615  // std::cout << "Non-existent path " << j["/m_layer_configs/143/m_select_max_dphi"_json_pointer] << "\n";
616 
617  auto &x = j["/m_layer_configs"_json_pointer];
618  std::cout << "Typename /m_layer_configs " << x.type_name() << "\n";
619  auto &y = j["/m_layer_configs/143"_json_pointer];
620  std::cout << "Typename /m_layer_configs/143 " << y.type_name() << ", is_null=" << y.is_null() << "\n";
621  }
nlohmann::json json
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)

◆ test_Patcher()

void mkfit::ConfigJson::test_Patcher ( IterationConfig it_cfg)

Definition at line 623 of file IterationConfig.cc.

References mkfit::ConfigJsonPatcher::cd(), mkfit::ConfigJsonPatcher::cd_top(), gather_cfg::cout, mkfit::ConfigJsonPatcher::dump(), cppFunctionSkipper::exception, mkfit::ConfigJsonPatcher::get(), mps_fire::i, createfilelist::int, dqmiolumiharvest::j, mkfit::ConfigJsonPatcher::load(), mkfit::IterationConfig::m_layer_configs, mkfit::IterationConfig::m_n_regions, mkfit::IterationConfig::m_steering_params, mkfit::ConfigJsonPatcher::replace(), mkfit::ConfigJsonPatcher::save(), and x.

623  {
624  ConfigJsonPatcher cjp;
625  cjp.load(it_cfg);
626 
627  std::cout << cjp.dump(1) << "\n";
628 
629  {
630  cjp.cd("/m_layer_configs/43/m_select_max_dphi");
631  std::cout << "Layer 43, m_select_max_dphi = " << cjp.get("") << "\n";
632  std::cout << "Setting it to pi ...\n";
633  cjp.replace("", 3.141);
634  cjp.cd_top();
635  std::cout << "Layer 43, m_select_max_dphi = " << cjp.get("/m_layer_configs/43/m_select_max_dphi") << "\n";
636  }
637  {
638  std::cout << "Replacing layer 60 m_select_max_dphi with full path\n";
639  cjp.replace("/m_layer_configs/60/m_select_max_dphi", 99.876);
640  }
641  try {
642  std::cout << "Trying to replace an non-existent array entry\n";
643  cjp.replace("/m_layer_configs/1460/m_select_max_dphi", 666.666);
644  } catch (std::exception &exc) {
645  std::cout << "Caugth exception: " << exc.what() << "\n";
646  }
647  try {
648  std::cout << "Trying to replace an non-existent object entry\n";
649  cjp.replace("/m_layer_configs/1/moo_select_max_dphi", 666.666);
650  } catch (std::exception &exc) {
651  std::cout << "Caugth exception: " << exc.what() << "\n";
652  }
653  {
654  std::cout << "Replacing m_select_max_dphi on layers 1 to 3 to 7.7\n";
655  cjp.cd("/m_layer_configs");
656  cjp.replace(1, 3, "/m_select_max_dphi", 7.7);
657  cjp.cd_top();
658  }
659 
660  // try getting it back into c++, see what happens to vector m_layer_configs.
661 
662  cjp.save(it_cfg);
663 
664  printf("Layer 43: m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
665  it_cfg.m_layer_configs[43].m_select_max_dphi,
666  (int)it_cfg.m_layer_configs.size(),
667  it_cfg.m_n_regions,
668  (int)it_cfg.m_steering_params.size());
669 
670  printf("Layer 60: m_select_max_dphi = %f\n", it_cfg.m_layer_configs[60].m_select_max_dphi);
671  for (int i = 0; i < 5; ++i)
672  printf("Layer %2d: m_select_max_dphi = %f\n", i, it_cfg.m_layer_configs[i].m_select_max_dphi);
673 
674  // try accessing something that does not exist
675 
676  // std::cout << "Non-existent path " << j["/m_layer_configs/143/m_select_max_dphi"_json_pointer] << "\n";
677 
678  auto &j = cjp.get("");
679 
680  auto &x = j["/m_layer_configs"_json_pointer];
681  std::cout << "Typename /m_layer_configs " << x.type_name() << "\n";
682  auto &y = j["/m_layer_configs/143"_json_pointer];
683  std::cout << "Typename /m_layer_configs/143 " << y.type_name() << ", is_null=" << y.is_null() << "\n";
684  }

Member Data Documentation

◆ m_verbose

bool mkfit::ConfigJson::m_verbose = false
private

Definition at line 377 of file IterationConfig.h.

Referenced by load_File(), patch_Files(), and patchLoad_File().