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

#include <MetadataSBN.h>

Classes

struct  metadata
 

Public Member Functions

 MetadataSBN (fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
 
void GetMetadataMaps (std::map< std::string, std::string > &strs, std::map< std::string, int > &ints, std::map< std::string, double > &doubles, std::map< std::string, std::string > &objs)
 

Public Attributes

metadata md
 
std::set< art::SubRunID > fSubRunNumbers
 

Private Member Functions

void postBeginJob ()
 
void postOpenInputFile (std::string const &fn)
 
void postEvent (art::Event const &ev, art::ScheduleContext)
 
void postBeginSubRun (art::SubRun const &subrun)
 
void postEndSubRun (art::SubRun const &subrun)
 
void postCloseInputFile ()
 
std::string GetParentsString () const
 
std::string GetRunsString () const
 

Private Attributes

std::map< std::string,
std::string > 
mdmapStr
 
std::map< std::string, int > mdmapInt
 
std::map< std::string, double > mdmapDouble
 
std::map< std::string,
std::string > 
mdmapObj
 
std::string fExperiment
 
std::string frunType
 
std::vector< std::string > fJSONFileName
 
std::vector< std::string > fDataTier
 
std::vector< std::string > fFileFormat
 
std::vector< int > fMerge
 
std::string fPOTModuleLabel
 
art::FileStatsCollector fFileStats
 
art::PostCloseFileRenamer fRenamer {fFileStats}
 

Detailed Description

Definition at line 32 of file MetadataSBN.h.

Constructor & Destructor Documentation

util::MetadataSBN::MetadataSBN ( fhicl::ParameterSet const &  pset,
art::ActivityRegistry &  reg 
)

Definition at line 84 of file MetadataSBN_service.cc.

85  :
86  fFileStats{"", art::ServiceHandle<art::TriggerNamesService const>{}->getProcessName()}
art::FileStatsCollector fFileStats
Definition: MetadataSBN.h:97

Member Function Documentation

void util::MetadataSBN::GetMetadataMaps ( std::map< std::string, std::string > &  strs,
std::map< std::string, int > &  ints,
std::map< std::string, double > &  doubles,
std::map< std::string, std::string > &  objs 
)

Definition at line 310 of file MetadataSBN_service.cc.

314 {
315  strs.clear(); ints.clear(); doubles.clear(); objs.clear();
316 
317  objs["application"] = "{\"family\": \""+std::get<0>(md.fapplication)+"\", \"name\": \""+std::get<1>(md.fapplication)+"\", \"version\": \""+std::get<2>(md.fapplication)+"\"}";
318 
319  if(!md.fParents.empty()) objs["parents"] = GetParentsString();
320  if(!md.fruns.empty()) objs["runs"] = GetRunsString();
321 
322  // convert start and end times into time format: Year-Month-DayTHours:Minutes:Seconds
323  char endbuf[80], startbuf[80];
324  struct tm tstruct;
325  tstruct = *localtime(&md.fend_time);
326  strftime(endbuf,sizeof(endbuf),"%Y-%m-%dT%H:%M:%S",&tstruct);
327  tstruct = *localtime(&md.fstart_time);
328  strftime(startbuf,sizeof(startbuf),"%Y-%m-%dT%H:%M:%S",&tstruct);
329 
330  strs["start_time"] = startbuf;
331  strs["end_time"] = endbuf;
332 
333  strs["data_tier"] = md.fdata_tier;
334  ints["event_count"] = md.fevent_count;
335  strs["file_format"] = md.ffile_format;
336  ints["first_event"] = md.ffirst_event;
337  ints["last_event"] = md.flast_event;
338 
339  const std::string proj = fExperiment+"_project";
340  MaybeCopyToMap(md.fFCLName, "fcl.name", strs);
341  MaybeCopyToMap(md.fProjectName, proj+".name", strs);
342  MaybeCopyToMap(md.fProjectStage, proj+".stage", strs);
343  MaybeCopyToMap(md.fProjectVersion, proj+".version", strs);
344  MaybeCopyToMap(md.fProjectSoftware, proj+".software", strs);
345  MaybeCopyToMap(md.fProductionName, "production.name", strs);
346  MaybeCopyToMap(md.fProductionType, "production.type", strs);
347 
348  MaybeCopyToMap(md.fgroup, "group", strs);
349  MaybeCopyToMap(md.ffile_type, "file_type", strs);
350 
351  if(md.merge >= 0) {
352  ints["merge.merge"] = (md.merge==0 ? 0 : 1);
353  ints["merge.merged"] = 0;
354  }
355  doubles["mc.pot"] = md.fTotPOT;
356 }
std::string fExperiment
Definition: MetadataSBN.h:90
std::tuple< std::string, std::string, std::string > fapplication
Definition: MetadataSBN.h:38
std::vector< std::tuple< art::RunNumber_t, art::SubRunNumber_t, std::string > > fruns
Definition: MetadataSBN.h:50
std::string GetParentsString() const
art::EventNumber_t ffirst_event
Definition: MetadataSBN.h:46
void MaybeCopyToMap(const std::string &in, const std::string &key, std::map< std::string, std::string > &out)
std::set< std::string > fParents
Definition: MetadataSBN.h:49
std::string GetRunsString() const
art::EventNumber_t flast_event
Definition: MetadataSBN.h:48
std::string util::MetadataSBN::GetParentsString ( ) const
private

Definition at line 272 of file MetadataSBN_service.cc.

273 {
274  if(md.fParents.empty()) return "";
275 
276  unsigned int c = 0;
277 
278  std::string ret = "[\n";
279  for(auto parent: md.fParents) {
280  std::cout<<"Parent " << c << ": " << parent << std::endl;
281  c++;
282  size_t n = parent.find_last_of('/');
283  size_t f1 = (n == std::string::npos ? 0 : n+1);
284  ret += " {\n \"file_name\": \"" + parent.substr(f1) + "\"\n }";
285  if(md.fParents.size() == 1 || c == md.fParents.size()) ret += "\n";
286  else ret += ",\n";
287  }
288 
289  ret += " ]";
290  return ret;
291 }
std::set< std::string > fParents
Definition: MetadataSBN.h:49
BEGIN_PROLOG could also be cout
std::string util::MetadataSBN::GetRunsString ( ) const
private

Definition at line 294 of file MetadataSBN_service.cc.

295 {
296  unsigned int c = 0;
297 
298  std::string ret = "[\n";
299  for(auto&t :md.fruns){
300  c++;
301  ret += " [\n " + std::to_string(std::get<0>(t)) + ",\n " + std::to_string(std::get<1>(t)) + ",\n \"" + std::get<2>(t) + "\"\n ]";
302  if(md.fruns.size() == 1 || c == md.fruns.size()) ret += "\n";
303  else ret += ",\n";
304  }
305  ret += " ]";
306  return ret;
307 }
std::vector< std::tuple< art::RunNumber_t, art::SubRunNumber_t, std::string > > fruns
Definition: MetadataSBN.h:50
std::string to_string(WindowPattern const &pattern)
void util::MetadataSBN::postBeginJob ( )
private

Definition at line 174 of file MetadataSBN_service.cc.

175 {
176  // get the start time
177  md.fstart_time = time(0);
178 
179  // Get art metadata service and extract paramters from there
180  art::ServiceHandle<art::FileCatalogMetadata> artmds;
181 
182  art::FileCatalogMetadata::collection_type artmd;
183  artmds->getMetadata(artmd);
184 
185  std::map<std::string, std::string> mdmap;
186  for(const auto& d: artmd)
187  mdmap[d.first] = UnQuoteString(d.second);
188 
189  // if a certain paramter/key is not found, assign an empty string value to it
190  MaybeCopyFromMap(mdmap, "application.family", std::get<0>(md.fapplication));
191  MaybeCopyFromMap(mdmap, "art.process_name", std::get<1>(md.fapplication));
192  MaybeCopyFromMap(mdmap, "application.version", std::get<2>(md.fapplication));
193  MaybeCopyFromMap(mdmap, "group", md.fgroup);
194  MaybeCopyFromMap(mdmap, "file_type", md.ffile_type);
195  MaybeCopyFromMap(mdmap, "art.run_type", frunType);
196 }
std::tuple< std::string, std::string, std::string > fapplication
Definition: MetadataSBN.h:38
std::string frunType
Definition: MetadataSBN.h:91
void MaybeCopyFromMap(const std::map< std::string, std::string > &in, const std::string &key, std::string &out)
std::string UnQuoteString(std::string s)
Un-quote quoted strings.
void util::MetadataSBN::postBeginSubRun ( art::SubRun const &  subrun)
private

Definition at line 234 of file MetadataSBN_service.cc.

235 {
236  art::RunNumber_t run = sr.run();
237  art::SubRunNumber_t subrun = sr.subRun();
238  art::SubRunID srid = sr.id();
239 
240  // save run, subrun and runType information once every subrun
241  if (fSubRunNumbers.count(srid) == 0){
242  fSubRunNumbers.insert(srid);
243  md.fruns.push_back(make_tuple(run, subrun, frunType));
244  }
245 }
std::set< art::SubRunID > fSubRunNumbers
Definition: MetadataSBN.h:64
std::vector< std::tuple< art::RunNumber_t, art::SubRunNumber_t, std::string > > fruns
Definition: MetadataSBN.h:50
std::string frunType
Definition: MetadataSBN.h:91
void util::MetadataSBN::postCloseInputFile ( )
private

Definition at line 360 of file MetadataSBN_service.cc.

361 {
362  //update end time
363  md.fend_time = time(0);
364 
365  // Loop over files.
366 
367  for(unsigned int i=0; i<fJSONFileName.size(); ++i) {
368 
369  // Update per-file metadata.
370 
371  md.fdata_tier = fDataTier[i];
373  if(fMerge.size() > i)
374  md.merge = fMerge[i];
375  else
376  md.merge = -1;
377 
378 
379  std::map<std::string, std::string> strs;
380  std::map<std::string, int> ints;
381  std::map<std::string, double> doubles;
382  std::map<std::string, std::string> objs;
383  GetMetadataMaps(strs, ints, doubles, objs);
384 
385  // open a json file and write everything from the struct md complying to the
386  // samweb json format. This json file holds the below information temporarily.
387  // If you submitted a grid job invoking this service, the information from
388  // this file is appended to a final json file and this file will be removed
389 
390  if(!fJSONFileName[i].empty()){
391  std::ofstream jsonfile;
392  jsonfile.open(fJSONFileName[i]);
393  jsonfile << "{\n";
394 
395  bool once = true;
396  for(auto& it: objs){
397  if(!once) jsonfile << ",\n";
398  once = false;
399  jsonfile << " \"" << it.first << "\": " << it.second;
400  }
401  for(auto& it: strs){
402  // Have to escape string outputs
403  jsonfile << ",\n \"" << it.first << "\": \"" << it.second << "\"";
404  }
405  for(auto& it: ints){
406  jsonfile << ",\n \"" << it.first << "\": " << it.second;
407  }
408  for(auto& it: doubles){
409  jsonfile << ",\n \"" << it.first << "\": " << it.second;
410  }
411 
412  jsonfile<<"\n}\n";
413  jsonfile.close();
414  }
415  }
416 
417  fFileStats.recordFileClose();
418  //TODO figure out how to make the name identical to the TFile
419  //std::string new_name = fRenamer.maybeRenameFile("myjson.json",fJSONFileName);
420 }
void GetMetadataMaps(std::map< std::string, std::string > &strs, std::map< std::string, int > &ints, std::map< std::string, double > &doubles, std::map< std::string, std::string > &objs)
std::vector< std::string > fFileFormat
Definition: MetadataSBN.h:94
std::vector< int > fMerge
Definition: MetadataSBN.h:95
art::FileStatsCollector fFileStats
Definition: MetadataSBN.h:97
std::vector< std::string > fJSONFileName
Definition: MetadataSBN.h:92
std::vector< std::string > fDataTier
Definition: MetadataSBN.h:93
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
void util::MetadataSBN::postEndSubRun ( art::SubRun const &  subrun)
private

Definition at line 249 of file MetadataSBN_service.cc.

250 {
251  art::Handle< sumdata::POTSummary > potListHandle;
252  double fTotPOT = 0;
253  if(sr.getByLabel(fPOTModuleLabel,potListHandle)){
254  fTotPOT+=potListHandle->totpot;
255  }
256 
257  md.fTotPOT += fTotPOT;
258 }
std::string fPOTModuleLabel
Definition: MetadataSBN.h:96
void util::MetadataSBN::postEvent ( art::Event const &  ev,
art::ScheduleContext   
)
private

Definition at line 211 of file MetadataSBN_service.cc.

212 {
213  art::RunNumber_t run = evt.run();
214  art::SubRunNumber_t subrun = evt.subRun();
215  art::EventNumber_t event = evt.event();
216  art::SubRunID srid = evt.id().subRunID();
217 
218  // save run, subrun and runType information once every subrun
219  if (fSubRunNumbers.count(srid) == 0){
220  fSubRunNumbers.insert(srid);
221  md.fruns.push_back(make_tuple(run, subrun, frunType));
222  }
223 
224  // save the first event
225  if (md.fevent_count == 0) md.ffirst_event = event;
226  md.flast_event = event;
227  // event counter
228  ++md.fevent_count;
229 
230 }
unsigned int event
Definition: DataStructs.h:634
std::set< art::SubRunID > fSubRunNumbers
Definition: MetadataSBN.h:64
unsigned int run
Definition: DataStructs.h:635
std::vector< std::tuple< art::RunNumber_t, art::SubRunNumber_t, std::string > > fruns
Definition: MetadataSBN.h:50
std::string frunType
Definition: MetadataSBN.h:91
art::EventNumber_t ffirst_event
Definition: MetadataSBN.h:46
unsigned int subRun
Definition: DataStructs.h:636
TCEvent evt
Definition: DataStructs.cxx:8
art::EventNumber_t flast_event
Definition: MetadataSBN.h:48
void util::MetadataSBN::postOpenInputFile ( std::string const &  fn)
private

Definition at line 201 of file MetadataSBN_service.cc.

202 {
203  // save parent input files here
204  // 08/06 DBrailsford: Only save the parent string if the string is filled. The string still exists (with 0 characters) for generation stage files. See redmine issue 20124
205  if (fn.length() > 0) md.fParents.insert(fn);
206  fFileStats.recordInputFile(fn);
207 }
art::FileStatsCollector fFileStats
Definition: MetadataSBN.h:97
std::set< std::string > fParents
Definition: MetadataSBN.h:49

Member Data Documentation

std::vector<std::string> util::MetadataSBN::fDataTier
private

Definition at line 93 of file MetadataSBN.h.

std::string util::MetadataSBN::fExperiment
private

Definition at line 90 of file MetadataSBN.h.

std::vector<std::string> util::MetadataSBN::fFileFormat
private

Definition at line 94 of file MetadataSBN.h.

art::FileStatsCollector util::MetadataSBN::fFileStats
private

Definition at line 97 of file MetadataSBN.h.

std::vector<std::string> util::MetadataSBN::fJSONFileName
private

Definition at line 92 of file MetadataSBN.h.

std::vector<int> util::MetadataSBN::fMerge
private

Definition at line 95 of file MetadataSBN.h.

std::string util::MetadataSBN::fPOTModuleLabel
private

Definition at line 96 of file MetadataSBN.h.

art::PostCloseFileRenamer util::MetadataSBN::fRenamer {fFileStats}
private

Definition at line 98 of file MetadataSBN.h.

std::string util::MetadataSBN::frunType
private

Definition at line 91 of file MetadataSBN.h.

std::set<art::SubRunID> util::MetadataSBN::fSubRunNumbers

Definition at line 64 of file MetadataSBN.h.

metadata util::MetadataSBN::md

Definition at line 63 of file MetadataSBN.h.

std::map<std::string, double> util::MetadataSBN::mdmapDouble
private

Definition at line 86 of file MetadataSBN.h.

std::map<std::string, int> util::MetadataSBN::mdmapInt
private

Definition at line 85 of file MetadataSBN.h.

std::map<std::string, std::string> util::MetadataSBN::mdmapObj
private

Definition at line 87 of file MetadataSBN.h.

std::map<std::string,std::string> util::MetadataSBN::mdmapStr
private

Definition at line 84 of file MetadataSBN.h.


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