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

Constructor & Destructor Documentation

◆ ConfigJson()

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

Definition at line 364 of file IterationConfig.h.

Member Function Documentation

◆ dump()

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

Definition at line 678 of file IterationConfig.cc.

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

Referenced by initGeom().

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

◆ load_File()

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

Definition at line 608 of file IterationConfig.cc.

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

Referenced by MkFitIterationConfigESProducer::produce().

608  {
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  }
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 490 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().

492  {
493  ConfigJsonPatcher cjp(m_verbose);
494  cjp.load(its_info);
495 
496  ConfigJsonPatcher::PatchReport rep;
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  }
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 547 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().

549  {
550  ConfigJsonPatcher::PatchReport rep;
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 
583  ConfigJsonPatcher cjp(m_verbose);
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  }
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 638 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().

640  {
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  }
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 687 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.

687  {
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  }
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 733 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.

733  {
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  }
float x

Member Data Documentation

◆ m_verbose

bool mkfit::ConfigJson::m_verbose = false
private

Definition at line 402 of file IterationConfig.h.

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