All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReadArtConfiguration.cxx
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/ReadArtConfiguration.cxx
3  * @brief Utilities to extract _art_ FHiCL configuration from different sources.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date February 18, 2021
6  * @see icaruscode/Utilities/ReadArtConfiguration.h
7  *
8  */
9 
10 
11 // library header
13 
14 // framework libraries
15 #include "art_root_io/RootDB/SQLite3Wrapper.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
18 
19 // -----------------------------------------------------------------------------
20 std::map<fhicl::ParameterSetID, fhicl::ParameterSet>
22 {
23  /*
24  * This code is ripped from `fhiclcpp/ParameterSetRegistry.h` and
25  * `lardata/DetectorInfoServices/DetectorClocksServiceStandard_service.cc`
26  * (LArSoft v 9.17.0).
27  *
28  * The special wrapped defines hooks to support a "virtual file system"
29  * within a ROOT file data base, in the way that art knows and I do not.
30  * So we bite it and accept a dependency against art_root_io.
31  */
32  art::SQLite3Wrapper sqliteDB(&file, "RootFileDB");
33 
34  auto* db = static_cast<sqlite3*>(sqliteDB);
35 
36  auto const throwOnSQLiteNotOK = [&db](std::string const& msg = {})
37  {
38  if (db == nullptr) {
39  throw cet::exception("readConfigurationFromArtFile")
40  << "Can't open SQLite database.";
41  }
42  auto const errcode = sqlite3_errcode(db);
43  // Caller's responsibility to make sure this really is an error
44  // and not (say) SQLITE_ROW or SQLITE_DONE:
45  if (errcode == SQLITE_OK) return;
46  throw cet::exception("readConfigurationFromArtFile")
47  << "SQLite3 error (code" << errcode << "): "
48  << sqlite3_errstr(errcode) << (msg.empty() ? "" : (": " + msg))
49  << "\n";
50  };
51 
52 
53  sqlite3_stmt* stmt = nullptr;
54  sqlite3_prepare_v2
55  (sqliteDB, "SELECT ID, PSetBlob from ParameterSets;", -1, &stmt, nullptr);
56  throwOnSQLiteNotOK("[SELECT ID, PSetBlob from ParameterSets;]");
57 
58  std::map<fhicl::ParameterSetID, fhicl::ParameterSet> config;
59  while (sqlite3_step(stmt) == SQLITE_ROW) {
60 
61  // reinterpretation: `unsigned char*` -> `char*`
62  std::string const psetIDstr
63  = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
64  auto pset = fhicl::ParameterSet::make
65  (reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
66  config.emplace(fhicl::ParameterSetID{ psetIDstr }, std::move(pset));
67 
68  } // while
69  sqlite3_finalize(stmt);
70  throwOnSQLiteNotOK("[SELECT ID, PSetBlob from ParameterSets;]");
71 
72  return config;
73 } // util::readConfigurationFromArtFile()
74 
75 
76 // -----------------------------------------------------------------------------
* file
Definition: file_to_url.sh:69
std::map< fhicl::ParameterSetID, fhicl::ParameterSet > readConfigurationFromArtFile(TFile &file)
Reads and returns the art configuration stored in sourceDir.
Utilities to extract art FHiCL configuration from different sources.
height to which particles are projected pnfs larsoft persistent physics cosmics Fermilab CORSIKA standard He_showers_ * db