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

Constructor & Destructor Documentation

◆ ConfigJson()

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

Definition at line 366 of file IterationConfig.h.

Member Function Documentation

◆ dump()

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

Definition at line 680 of file IterationConfig.cc.

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

Referenced by initGeom().

680  {
681  nlohmann::ordered_json j = its_info;
682  std::cout << j.dump(3) << "\n";
683  }

◆ load_File()

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

Definition at line 610 of file IterationConfig.cc.

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

Referenced by MkFitIterationConfigESProducer::produce().

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

Referenced by initGeom().

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

Referenced by initGeom().

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

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

◆ test_Direct()

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

Definition at line 689 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.

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

◆ test_Patcher()

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

Definition at line 735 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.

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

Member Data Documentation

◆ m_verbose

bool mkfit::ConfigJson::m_verbose = false
private

Definition at line 404 of file IterationConfig.h.

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