All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
SaveConfigurationIntoTFile Class Reference

Writes the art configuration into the TFileService file. More...

Inheritance diagram for SaveConfigurationIntoTFile:

Classes

struct  Config
 

Public Types

using Parameters = art::EDAnalyzer::Table< Config >
 

Public Member Functions

 SaveConfigurationIntoTFile (Parameters const &config)
 Standard art analyzer moduel constructor. More...
 
virtual void beginJob () override
 Writes the configuration information of the current process. More...
 
virtual void analyze (art::Event const &event) override
 Writes the configuration information of the input processes. More...
 

Private Member Functions

void saveProcessConfiguration (std::string const &procName, fhicl::ParameterSet const &config)
 Writes the specified configuration into a ROOT directory. More...
 
bool isProcessConfigurationSaved (std::string const &procName) const
 Returns whether we have already saved configuration for procName. More...
 

Static Private Member Functions

static fhicl::ParameterSet processConfiguration (art::Event const &event, std::string const &procName)
 Extracts the configuration of the specified process from event. More...
 
static void storeString (std::string const &key, std::string const &value)
 Saves a string via TFileService (as TNamed). More...
 
static fhicl::ParameterSet currentProcessConfiguration (std::string const &procName)
 Returns the configuration of the current art process. More...
 
static bool isProcessConfiguration (fhicl::ParameterSet const &pset)
 Returns whether the specified one is a process configuration. More...
 
static bool isProcessConfiguration (fhicl::ParameterSet const &pset, std::string const &procName)
 Returns whether pset is the configuration of procName process. More...
 

Private Attributes

bool fIncludePreviousProcesses = false
 Whether to save the configuration of the input file. More...
 
std::set< std::string > fProcessedProcesses
 Process names already saved. More...
 

Detailed Description

Writes the art configuration into the TFileService file.

A TStringObj object is written into the directory assigned by TFileService to this analyzer for every selected process configuration. The TObjString will have the same ROOT name as the process itself. In addition, the following are saved:

The selection of processes is controlled by the module configuration. The current process is always selected.

This module takes a simplistic approach, and prints only the first occurrence of each process name. When saving configuration of processes from the input files, and they contain different configurations for the same process name, only one of them (the first one encountered) will be saved.

Todo:
Currently the author does not know how to extract the configuration of the current process, which is therefore a dummy

Configuration parameters

Definition at line 61 of file SaveConfigurationIntoTFile_module.cc.

Member Typedef Documentation

using SaveConfigurationIntoTFile::Parameters = art::EDAnalyzer::Table<Config>

Definition at line 78 of file SaveConfigurationIntoTFile_module.cc.

Constructor & Destructor Documentation

SaveConfigurationIntoTFile::SaveConfigurationIntoTFile ( Parameters const &  config)

Standard art analyzer moduel constructor.

Definition at line 147 of file SaveConfigurationIntoTFile_module.cc.

148  : art::EDAnalyzer(config)
149  , fIncludePreviousProcesses(config().includePreviousProcesses())
150  {}
bool fIncludePreviousProcesses
Whether to save the configuration of the input file.

Member Function Documentation

void SaveConfigurationIntoTFile::analyze ( art::Event const &  event)
overridevirtual

Writes the configuration information of the input processes.

Definition at line 178 of file SaveConfigurationIntoTFile_module.cc.

178  {
179 
180  //
181  // previous processes for input file
182  //
184  for (auto const& procConfigInfo: event.processHistory()) {
185  std::string const& procName = procConfigInfo.processName();
186  if (isProcessConfigurationSaved(procName)) continue;
187 
189  (procName, processConfiguration(event, procName));
190  mf::LogInfo("SaveConfigurationIntoTFile")
191  << "Configuration of process '" << procName
192  << "' from input file stored via TFileService.";
193 
194  } // for process config
195  } // if include history
196 
197 } // SaveConfigurationIntoTFile::analyze()
bool isProcessConfigurationSaved(std::string const &procName) const
Returns whether we have already saved configuration for procName.
void saveProcessConfiguration(std::string const &procName, fhicl::ParameterSet const &config)
Writes the specified configuration into a ROOT directory.
static fhicl::ParameterSet processConfiguration(art::Event const &event, std::string const &procName)
Extracts the configuration of the specified process from event.
bool fIncludePreviousProcesses
Whether to save the configuration of the input file.
void SaveConfigurationIntoTFile::beginJob ( )
overridevirtual

Writes the configuration information of the current process.

Definition at line 154 of file SaveConfigurationIntoTFile_module.cc.

154  {
155 
156  std::string const procName = processName();
157  //
158  // current process
159  //
160  fhicl::ParameterSet pset = currentProcessConfiguration(procName);
161 
162  MF_LOG_DEBUG("SaveConfigurationIntoTFile")
163  << "This process: '" << procName << "':\n"
164  << std::string(80, '=') << '\n'
165  << pset.to_indented_string()
166  << '\n' << std::string(80, '=');
167  saveProcessConfiguration(procName, pset);
168  storeString("current_process", procName);
169 
170  mf::LogInfo("SaveConfigurationIntoTFile")
171  << "Configuration of current process ('" << procName
172  << "') stored via TFileService.";
173 
174 } // SaveConfigurationIntoTFile::beginJob()
static fhicl::ParameterSet currentProcessConfiguration(std::string const &procName)
Returns the configuration of the current art process.
static void storeString(std::string const &key, std::string const &value)
Saves a string via TFileService (as TNamed).
void saveProcessConfiguration(std::string const &procName, fhicl::ParameterSet const &config)
Writes the specified configuration into a ROOT directory.
fhicl::ParameterSet SaveConfigurationIntoTFile::currentProcessConfiguration ( std::string const &  procName)
staticprivate

Returns the configuration of the current art process.

Definition at line 248 of file SaveConfigurationIntoTFile_module.cc.

249 {
250 
251  /*
252  * The strategy is to parse the global FHiCL registry (yes, there is such a
253  * thing), looking for the table which contains 'process_name'.
254  * This heuristic is not fool proof, since there might be other tables with
255  * the same atom. So:
256  * TODO find a way to ask art which is the root parameter set
257  * The registry contains all root-level tables, including the main
258  * configuration and other tables that are used to resolve references
259  * (internally, references are not resolved to save space).
260  * It turns out that when a fhicl::ParameterSet is created, all references
261  * are resolved, so by the time we get the parameter sets we don't have to
262  * worry to manually resolve the references. Which would not be fun.
263  *
264  */
265 
266  static std::string const rootKey { "process_name" };
267 
268  fhicl::ParameterSet const* rootConfig = nullptr;
269  for (auto const& idAndSet: fhicl::ParameterSetRegistry::get()) {
270  auto const& pset = idAndSet.second;
271  if (isProcessConfiguration(pset, procName)) {
272  if (rootConfig) {
273  throw art::Exception(art::errors::LogicError)
274  << "Found two candidate process parameter sets: with " << rootKey
275  << " '" << rootConfig->get<std::string>(rootKey) << "' and '"
276  << pset.get<std::string>(rootKey) << "'!\n";
277  }
278  rootConfig = &pset;
279  } // if found
280  }
281  if (!rootConfig) {
282  throw art::Exception(art::errors::LogicError)
283  << "No parameter set with 'process_name' atom found!\n";
284  }
285 
286  return *rootConfig;
287 } // SaveConfigurationIntoTFile::currentProcessConfiguration()
static bool isProcessConfiguration(fhicl::ParameterSet const &pset)
Returns whether the specified one is a process configuration.
bool SaveConfigurationIntoTFile::isProcessConfiguration ( fhicl::ParameterSet const &  pset)
staticprivate

Returns whether the specified one is a process configuration.

Definition at line 292 of file SaveConfigurationIntoTFile_module.cc.

293 {
294  /*
295  * We are forced to parse the parameter set heuristically.
296  * If a configuration is something like:
297  *
298  * full_configuration: { ... }
299  *
300  * @table::full_configuration
301  *
302  * the heuristic will match both the actual root configuration *and*
303  * `full_configuration` parameter set (hint: put `full_configuration` within a
304  * prolog). This may cause trouble downstream.
305  * Barred that case, the more detailed the logic here is, the less likely
306  * a false positive is to happen.
307  *
308  */
309  if (!::has_table (pset, "services" )) return false;
310  if (!::has_table (pset, "physics" )) return false;
311  if (!::has_table (pset, "source" )) return false;
312  if (!::has_atom (pset, "process_name")) return false;
313  if (!::has_sequence(pset, "physics.trigger_paths")) return false;
314  if (!::has_sequence(pset, "physics.end_paths")) return false;
315 
316  return true;
317 } // SaveConfigurationIntoTFile::isProcessConfiguration()
bool SaveConfigurationIntoTFile::isProcessConfiguration ( fhicl::ParameterSet const &  pset,
std::string const &  procName 
)
staticprivate

Returns whether pset is the configuration of procName process.

Definition at line 322 of file SaveConfigurationIntoTFile_module.cc.

323 {
324  if (!isProcessConfiguration(pset)) return false;
325  return pset.get<std::string>("process_name") == procName;
326 } // SaveConfigurationIntoTFile::isProcessConfiguration(std::string)
static bool isProcessConfiguration(fhicl::ParameterSet const &pset)
Returns whether the specified one is a process configuration.
bool SaveConfigurationIntoTFile::isProcessConfigurationSaved ( std::string const &  procName) const
inlineprivate

Returns whether we have already saved configuration for procName.

Definition at line 103 of file SaveConfigurationIntoTFile_module.cc.

104  { return fProcessedProcesses.count(procName) > 0; }
std::set< std::string > fProcessedProcesses
Process names already saved.
fhicl::ParameterSet SaveConfigurationIntoTFile::processConfiguration ( art::Event const &  event,
std::string const &  procName 
)
staticprivate

Extracts the configuration of the specified process from event.

Definition at line 216 of file SaveConfigurationIntoTFile_module.cc.

217 {
218  fhicl::ParameterSet pset;
219  event.getProcessParameterSet(procName, pset);
220  return pset;
221 } // SaveConfigurationIntoTFile::processConfiguration()
void SaveConfigurationIntoTFile::saveProcessConfiguration ( std::string const &  procName,
fhicl::ParameterSet const &  config 
)
private

Writes the specified configuration into a ROOT directory.

Definition at line 201 of file SaveConfigurationIntoTFile_module.cc.

203  {
204 
205  if (fProcessedProcesses.count(procName) > 0) return; // already saved
206 
207  storeString(procName, config.to_indented_string());
208 
209  fProcessedProcesses.insert(procName);
210 
211 } // SaveConfigurationIntoTFile::saveProcessConfiguration()
std::set< std::string > fProcessedProcesses
Process names already saved.
static void storeString(std::string const &key, std::string const &value)
Saves a string via TFileService (as TNamed).
void SaveConfigurationIntoTFile::storeString ( std::string const &  key,
std::string const &  value 
)
staticprivate

Saves a string via TFileService (as TNamed).

Definition at line 226 of file SaveConfigurationIntoTFile_module.cc.

227 {
228  /*
229  * The following `TFileService::makeAndRegister()` call does two things:
230  * 1) constructs a `TNamed` object with the _default constructor_
231  * 2) assigns it the specified name and title
232  * 3) adds the new object to the ROOT directory we are given.
233  *
234  * The first two operations might be combined by calling the proper
235  * constructor, but the generic `makeAndRegister()` will always explicitly
236  * perform action (2) anyway, while `make()` will not perform action (3)
237  * at all, which appears to be required for successfully writing `TNamed`.
238  *
239  */
240  auto const& fs = *(art::ServiceHandle<art::TFileService>());
241  fs.makeAndRegister<TNamed>(key.c_str(), value.c_str());
242 
243 } // SaveConfigurationIntoTFile::storeString()
temporary value

Member Data Documentation

bool SaveConfigurationIntoTFile::fIncludePreviousProcesses = false
private

Whether to save the configuration of the input file.

Definition at line 93 of file SaveConfigurationIntoTFile_module.cc.

std::set<std::string> SaveConfigurationIntoTFile::fProcessedProcesses
private

Process names already saved.

Definition at line 96 of file SaveConfigurationIntoTFile_module.cc.


The documentation for this class was generated from the following file: