LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
FileCatalogMetadataExtras.h
Go to the documentation of this file.
1 // Name: FileCatalogMetadataExtras.h
3 //
4 // *** IMPORTANT NOTE ***
5 //
6 // This service is depracated. As of art v1_10_xx, most of the sam
7 // metadata fields generated by this service are able to be generated
8 // natively by art service FileCatalogMetadata and art module
9 // RootOutput.
10 //
11 // Purpose: This service adds user-specified per-job and per-file sam
12 // metadata. Per-job metadata is added via to art's built in
13 // FileCatalogMetadata service (which service must be configured
14 // for this service to function). Per-file sam metadata is
15 // added directly to art root files after they are closed.
16 //
17 // This service does not provide any user-callable methods.
18 // Rather it adds user-specified sam metadata simply by being
19 // configured and constructed.
20 //
21 // Art stores sam metadata in the form of (name, value)
22 // pairs. An arbitrary number of per-job (name, value) pairs
23 // can be specified using the single fcl parameter Metadata,
24 // specified as an array of strings (the length of the array
25 // must be even, obviously).
26 //
27 // Generated per-file metadata includes the following.
28 //
29 // 1. List of runs.
30 // 2. List of subruns.
31 // 3. List of parent files.
32 // 4. First event.
33 // 5. Last event.
34 // 6. Event count.
35 // 7. Start time (time when file was opened).
36 // 8. End time (time when file was closed).
37 //
38 // Per-file metadata is not configurable, except that it can be
39 // turned on or off in its entirety.
40 //
41 // This service also provides the ability to copy sam metadata
42 // attributes from an input file to an output file. The list
43 // of attributes to be copied is specified by fcl parameter
44 // CopyMetadataAttributes.
45 //
46 // This service performs a separate, but related, function of
47 // renaming output files according to a template provided via
48 // an fcl parameter. The purpose of file renaming is to
49 // generate unique output file names accross many workers
50 // from a single fcl configuration, which is something that
51 // RootOutput module can not (yet) do. Having unique file
52 // names is a sam requirement.
53 //
54 // The rename template may contain the following fields.
55 //
56 // ${num} - File sequence number (starts at 1).
57 // ${num 0} - File sequence number (starts at 0, can be any non-negative integer).
58 // ${bnum} - Same as ${num}, with or without numeric argument,
59 // except expands as empty string for first file.
60 // ${base} - Input file name (not including directory, like basename).
61 // ${base .root} - Input file name dropping final ".root".
62 // ${dir} - Input file directory path (up to, but not including final "/").
63 // ${path} - Input file path, similar as ${dir}/${file}.
64 // ${path .root} - Input file path dropping final ".root".
65 // ${date} - The current date (eight digits, YYYYMMDD).
66 // ${time} - The current time of day (six digits, HHMMSS).
67 // ${VAR def} - Substitute value of environment variable VAR,
68 // or substitute "def" if VAR is undefined.
69 //
70 // FCL parameters:
71 //
72 // Metadata - Array containing string (name, value) pairs.
73 // GeneratePerFileMetadata - Boolean controls whether to generate
74 // per-file metadata.
75 // CopyMetadataAttributes - Array of strings containing metadata
76 // attributes to copy from input file.
77 // RenameTemplate - File rename template (do not rename if blank).
78 // RenameOverwrite - Overwrite existing file if true (otherwise
79 // print a warning and don't rename).
80 //
81 //
82 // Created: 26-Apr-2013, H. Greenlee
83 //
85 #ifndef FILECATALOGMETADATAMICROBOONE_H
86 #define FILECATALOGMETADATAMICROBOONE_H
87 
88 #include "fhiclcpp/ParameterSet.h"
95 
96 namespace util{
97 
99  {
100  public:
101 
104 
105  void reconfigure(fhicl::ParameterSet const& p);
106 
107  // Embedded per-file metadata struct.
108 
110  {
111  std::set<art::RunNumber_t> fRunNumbers;
112  std::set<art::SubRunNumber_t> fSubRunNumbers;
113  std::set<std::string> fParents;
116  unsigned int fEventCount;
117  time_t fStartTime;
118  time_t fEndTime;
119  std::multimap<std::string, std::string> fNVPairs;
120 
121  // Default constructor.
122 
124  fFirstEvent(0), fLastEvent(0), fEventCount(0), fStartTime(0), fEndTime(0) {}
125 
126  // Method to convert per-file metadata to a collection of name-value pairs.
127 
128  void fillMetadata(std::multimap<std::string, std::string>& md);
129  };
130 
131  private:
132 
133  // Callbacks.
134 
135  void postBeginJob();
136  void postEndJob();
137  void postOpenFile(std::string const& fn);
138  void postCloseFile();
139  void preEvent(art::Event const& ev);
140  void postEvent(art::Event const& ev);
141 
142  // Pseudo-callbacks. These are not called via the art
143  // ActivityRegistry. We generate fake callbacks ourselves by
144  // monitoring root files.
145 
146  void postOpenOutputFile(std::string const& fn);
147  void postCloseOutputFile(art::OutputFileInfo const& finfo);
148 
149  // Private member functions.
150 
151  static bool isArtFile(std::string const& fn);
152  void checkOutputFiles(); // This method generate fake callbacks.
153  void addPerFileMetadata(std::string const& fn);
154  void renameOutputFile(std::string const& fn);
155  std::string expandTemplate() const;
156 
157  // Data members.
158 
159  // Fcl parameters.
160 
161  std::multimap<std::string, std::string> fPerJobMetadata; // (Name, value) pairs.
162  bool fGeneratePerFileMetadata; // Per-file metadata flag.
163  std::vector<std::string> fCopyMetadataAttributes; // Metadata to copy from input.
164  std::string fRenameTemplate;
166 
167  // Per-file metadata (indexed by output file name).
168 
169  std::map<std::string, PerFileMetadata> fPerFileMetadataMap;
170 
171  // Currently open output files.
172 
173  std::vector<std::string> fOutputFiles;
174 
175  // Last seen input file.
176 
177  std::string fLastInputFile;
178 
179  // Output file count (only count art files).
180 
181  unsigned int fOutputFileCount;
182 
183  }; // class FileCatalogMetadataExtras
184 
185 } //namespace util
186 
188 
189 #endif
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:17
void reconfigure(fhicl::ParameterSet const &p)
#define DECLARE_ART_SERVICE(svc, scope)
Definition: ServiceMacros.h:91
void postCloseOutputFile(art::OutputFileInfo const &finfo)
std::vector< std::string > fOutputFiles
std::multimap< std::string, std::string > fNVPairs
void fillMetadata(std::multimap< std::string, std::string > &md)
static bool isArtFile(std::string const &fn)
std::vector< std::string > fCopyMetadataAttributes
std::map< std::string, PerFileMetadata > fPerFileMetadataMap
std::multimap< std::string, std::string > fPerJobMetadata
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:117
FileCatalogMetadataExtras(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)