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 376 of file IterationConfig.h.

Constructor & Destructor Documentation

◆ ConfigJson()

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

Definition at line 378 of file IterationConfig.h.

Member Function Documentation

◆ dump()

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

Definition at line 674 of file IterationConfig.cc.

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

Referenced by initGeom().

674  {
675  nlohmann::ordered_json j = its_info;
676  std::cout << j.dump(3) << "\n";
677  }

◆ load_File()

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

Definition at line 604 of file IterationConfig.cc.

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

Referenced by MkFitIterationConfigESProducer::produce().

604  {
605  std::ifstream ifs;
606  open_ifstream(ifs, fname, __func__);
607 
608  if (m_verbose) {
609  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
610  }
611 
612  if (!skipws_ifstream(ifs))
613  throw std::runtime_error("empty file");
614 
616  ifs >> j;
617 
618  if (m_verbose) {
619  std::cout << " Read JSON entity, iteration index is " << j["m_iteration_index"] << ", track algorithm is "
620  << j["m_track_algorithm"] << ". Instantiating IterationConfig object and over-laying it with JSON.\n";
621  }
622 
623  IterationConfig *icp = new IterationConfig();
624 
625  from_json(j, *icp);
626 
627  return std::unique_ptr<IterationConfig>(icp);
628  }
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 486 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().

488  {
489  ConfigJsonPatcher cjp(m_verbose);
490  cjp.load(its_info);
491 
492  ConfigJsonPatcher::PatchReport rep;
493 
494  for (auto &fname : fnames) {
495  std::ifstream ifs;
496  open_ifstream(ifs, fname, __func__);
497 
498  if (m_verbose) {
499  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
500  }
501 
502  int n_read = 0, n_tot_replaced = 0;
503  while (skipws_ifstream(ifs)) {
505  ifs >> j;
506  ++n_read;
507 
508  if (m_verbose) {
509  std::cout << " Read JSON entity " << n_read << " -- applying patch:\n";
510  // std::cout << j.dump(3) << "\n";
511  }
512 
513  int n_replaced = cjp.replace(j);
514 
515  if (m_verbose) {
516  std::cout << " Replaced " << n_replaced << " entries.\n";
517  }
518  cjp.cd_top();
519  n_tot_replaced += n_replaced;
520  }
521 
522  if (m_verbose) {
523  printf("%s read %d JSON entities from file %s, replaced %d parameters.\n",
524  __func__,
525  n_read,
526  fname.c_str(),
527  n_tot_replaced);
528  }
529 
530  ifs.close();
531 
532  rep.inc_counts(1, n_read, n_tot_replaced);
533  }
534 
535  if (rep.n_replacements > 0) {
536  cjp.save(its_info);
537  }
538 
539  if (report)
540  report->inc_counts(rep);
541  }
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 543 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().

545  {
546  ConfigJsonPatcher::PatchReport rep;
547 
548  std::ifstream ifs;
549  open_ifstream(ifs, fname, __func__);
550 
551  if (m_verbose) {
552  printf("%s begin reading from file %s.\n", __func__, fname.c_str());
553  }
554 
555  if (!skipws_ifstream(ifs))
556  throw std::runtime_error("empty file");
557 
559  ifs >> j;
560  int track_algo = j["m_track_algorithm"];
561 
562  int iii = -1;
563  for (int i = 0; i < its_info.size(); ++i) {
564  if (its_info[i].m_track_algorithm == track_algo) {
565  iii = i;
566  break;
567  }
568  }
569  if (iii == -1)
570  throw std::runtime_error("matching IterationConfig not found");
571 
572  if (m_verbose) {
573  std::cout << " Read JSON entity, Iteration index is " << iii << " -- cloning and applying JSON patch:\n";
574  }
575 
576  IterationConfig *icp = new IterationConfig(its_info[iii]);
577  IterationConfig &ic = *icp;
578 
579  ConfigJsonPatcher cjp(m_verbose);
580  cjp.load(ic);
581 
582  int n_replaced = cjp.replace(j);
583 
584  cjp.cd_top();
585 
586  if (m_verbose) {
587  printf("%s read 1 JSON entity from file %s, replaced %d parameters.\n", __func__, fname.c_str(), n_replaced);
588  }
589 
590  ifs.close();
591 
592  rep.inc_counts(1, 1, n_replaced);
593 
594  if (rep.n_replacements > 0) {
595  cjp.save(ic);
596  }
597 
598  if (report)
599  report->inc_counts(rep);
600 
601  return std::unique_ptr<IterationConfig>(icp);
602  }
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 634 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().

636  {
637  bool has_pct_d = fname_fmt.find("%d") != std::string::npos;
638  bool has_pct_s = fname_fmt.find("%s") != std::string::npos;
639 
640  assert((has_pct_d || has_pct_s) && "JSON save filename-format must include a %d or %s substring");
641  assert(!(has_pct_d && has_pct_s) && "JSON save filename-format must include only one of %d or %s substrings");
642 
643  for (int ii = 0; ii < its_info.size(); ++ii) {
644  const IterationConfig &itconf = its_info[ii];
645 
646  char fname[1024];
647  if (has_pct_d)
648  snprintf(fname, 1024, fname_fmt.c_str(), ii);
649  else
650  snprintf(fname, 1024, fname_fmt.c_str(), TrackBase::algoint_to_cstr(itconf.m_track_algorithm));
651 
652  std::ofstream ofs;
653  open_ofstream(ofs, fname, __func__);
654 
655  if (include_iter_info_preamble) {
656  ofs << "{ \"m_iterations/" << ii << "\": ";
657  }
658 
659  nlohmann::ordered_json j;
660  to_json(j, itconf);
661 
662  ofs << std::setw(1);
663  ofs << j;
664 
665  if (include_iter_info_preamble) {
666  ofs << " }";
667  }
668 
669  ofs << "\n";
670  ofs.close();
671  }
672  }
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 683 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.

683  {
684  using nlohmann::json;
685 
686  std::string lojz("/m_select_max_dphi");
687 
688  json j = it_cfg;
689  std::cout << j.dump(1) << "\n";
690 
691  std::cout << "Layer 43, m_select_max_dphi = " << j["/m_layer_configs/43/m_select_max_dphi"_json_pointer] << "\n";
692  std::cout << "Patching it to pi ...\n";
693  json p = R"([
694  { "op": "replace", "path": "/m_layer_configs/43/m_select_max_dphi", "value": 3.141 }
695  ])"_json;
696  j = j.patch(p);
697  std::cout << "Layer 43, m_select_max_dphi = " << j["/m_layer_configs/43/m_select_max_dphi"_json_pointer] << "\n";
698 
699  auto &jx = j["/m_layer_configs/60"_json_pointer];
700  // jx["m_select_max_dphi"] = 99.876;
701  json::json_pointer jp(lojz);
702  jx[jp] = 99.876;
703 
704  // try loading it back, see what happens to vector m_layer_configs.
705 
706  from_json(j, it_cfg);
707  printf("Layer 43 : m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
708  it_cfg.m_layer_configs[43].m_select_max_dphi,
709  (int)it_cfg.m_layer_configs.size(),
710  it_cfg.m_n_regions,
711  (int)it_cfg.m_steering_params.size());
712 
713  printf("Layer 60 : m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
714  it_cfg.m_layer_configs[60].m_select_max_dphi,
715  (int)it_cfg.m_layer_configs.size(),
716  it_cfg.m_n_regions,
717  (int)it_cfg.m_steering_params.size());
718 
719  // try accessing something that does not exist
720 
721  // std::cout << "Non-existent path " << j["/m_layer_configs/143/m_select_max_dphi"_json_pointer] << "\n";
722 
723  auto &x = j["/m_layer_configs"_json_pointer];
724  std::cout << "Typename /m_layer_configs " << x.type_name() << "\n";
725  auto &y = j["/m_layer_configs/143"_json_pointer];
726  std::cout << "Typename /m_layer_configs/143 " << y.type_name() << ", is_null=" << y.is_null() << "\n";
727  }
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 729 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.

729  {
730  ConfigJsonPatcher cjp;
731  cjp.load(it_cfg);
732 
733  std::cout << cjp.dump(1) << "\n";
734 
735  {
736  cjp.cd("/m_layer_configs/43/m_select_max_dphi");
737  std::cout << "Layer 43, m_select_max_dphi = " << cjp.get("") << "\n";
738  std::cout << "Setting it to pi ...\n";
739  cjp.replace("", 3.141);
740  cjp.cd_top();
741  std::cout << "Layer 43, m_select_max_dphi = " << cjp.get("/m_layer_configs/43/m_select_max_dphi") << "\n";
742  }
743  {
744  std::cout << "Replacing layer 60 m_select_max_dphi with full path\n";
745  cjp.replace("/m_layer_configs/60/m_select_max_dphi", 99.876);
746  }
747  try {
748  std::cout << "Trying to replace an non-existent array entry\n";
749  cjp.replace("/m_layer_configs/1460/m_select_max_dphi", 666.666);
750  } catch (std::exception &exc) {
751  std::cout << "Caugth exception: " << exc.what() << "\n";
752  }
753  try {
754  std::cout << "Trying to replace an non-existent object entry\n";
755  cjp.replace("/m_layer_configs/1/moo_select_max_dphi", 666.666);
756  } catch (std::exception &exc) {
757  std::cout << "Caugth exception: " << exc.what() << "\n";
758  }
759  {
760  std::cout << "Replacing m_select_max_dphi on layers 1 to 3 to 7.7\n";
761  cjp.cd("/m_layer_configs");
762  cjp.replace(1, 3, "/m_select_max_dphi", 7.7);
763  cjp.cd_top();
764  }
765 
766  // try getting it back into c++, see what happens to vector m_layer_configs.
767 
768  cjp.save(it_cfg);
769 
770  printf("Layer 43: m_select_max_dphi = %f, size_of_layer_vec=%d, m_n_regions=%d, size_of_steering_params=%d\n",
771  it_cfg.m_layer_configs[43].m_select_max_dphi,
772  (int)it_cfg.m_layer_configs.size(),
773  it_cfg.m_n_regions,
774  (int)it_cfg.m_steering_params.size());
775 
776  printf("Layer 60: m_select_max_dphi = %f\n", it_cfg.m_layer_configs[60].m_select_max_dphi);
777  for (int i = 0; i < 5; ++i)
778  printf("Layer %2d: m_select_max_dphi = %f\n", i, it_cfg.m_layer_configs[i].m_select_max_dphi);
779 
780  // try accessing something that does not exist
781 
782  // std::cout << "Non-existent path " << j["/m_layer_configs/143/m_select_max_dphi"_json_pointer] << "\n";
783 
784  auto &j = cjp.get("");
785 
786  auto &x = j["/m_layer_configs"_json_pointer];
787  std::cout << "Typename /m_layer_configs " << x.type_name() << "\n";
788  auto &y = j["/m_layer_configs/143"_json_pointer];
789  std::cout << "Typename /m_layer_configs/143 " << y.type_name() << ", is_null=" << y.is_null() << "\n";
790  }

Member Data Documentation

◆ m_verbose

bool mkfit::ConfigJson::m_verbose = false
private

Definition at line 416 of file IterationConfig.h.

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