All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileCatalogMetadataSBN_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Name: FileCatalogMetadataSBN_service.cc.
3 //
4 // Purpose: Implementation for FileCatalogMetadataSBN.
5 //
6 // Created: 28-Oct-2014, H. Greenlee
7 //
8 ////////////////////////////////////////////////////////////////////////
9 
11 
12 #include "art/Framework/Services/Registry/ServiceHandle.h"
13 #include "art/Framework/Services/Registry/ServiceDefinitionMacros.h"
14 #include "art/Framework/Services/System/FileCatalogMetadata.h"
15 #include "art/Framework/Principal/SubRun.h"
16 #include "art/Framework/Principal/Handle.h"
17 #include "cetlib_except/exception.h"
19 
20 //--------------------------------------------------------------------
21 // Constructor.
22 
24 FileCatalogMetadataSBN(fhicl::ParameterSet const& pset, art::ActivityRegistry& reg) :
25  fTotPOT(0.)
26 {
27  // Insist on configuring Experiment from the fcl file (ideally) or the
28  // environment.
29  const char* expt = getenv("EXPERIMENT");
30  if(expt) fExperiment = pset.get<std::string>("Experiment", expt); else fExperiment = pset.get<std::string>("Experiment");
31  std::transform(fExperiment.begin(), fExperiment.end(), fExperiment.begin(), [](unsigned char c){return std::tolower(c);});
32 
33  // Get parameters.
34 
35  fFCLName = pset.get<std::string>("FCLName");
36  fProjectName = pset.get<std::string>("ProjectName");
37  fProjectStage = pset.get<std::string>("ProjectStage");
38  fProjectVersion = pset.get<std::string>("ProjectVersion");
39  fProjectSoftware = pset.get<std::string>("ProjectSoftware","");
40  fProductionName = pset.get<std::string>("ProductionName",""); //Leave as default value if not running a production
41  fProductionType = pset.get<std::string>("ProductionType",""); //Leave as default value if not running a production
42  fMerge = pset.get<int>("Merge", -1);
43  fParameters = pset.get<std::vector<std::pair<std::string, std::string>>>("Parameters", std::vector<std::pair<std::string, std::string>>());
44  fPOTModuleLabel = pset.get<std::string>("POTModuleLabel", "generator");
45 
46  // Register for callbacks.
47 
48  reg.sPostBeginJob.watch(this, &FileCatalogMetadataSBN::postBeginJob);
49  reg.sPostEndSubRun.watch(this, &FileCatalogMetadataSBN::postEndSubRun);
50  reg.sPreCloseOutputFile.watch(this, &FileCatalogMetadataSBN::preCloseOutputFile);
51 }
52 
53 //--------------------------------------------------------------------
54 // PostBeginJob callback.
55 // Insert per-job metadata via FileCatalogMetadata service.
57 {
58  // Get art metadata service.
59 
60  art::ServiceHandle<art::FileCatalogMetadata> mds;
61 
62  // Add metadata.
63 
64  mds->addMetadata("fcl.name", fFCLName);
65  mds->addMetadata(fExperiment + "_project.name", fProjectName);
66  mds->addMetadata(fExperiment + "_project.stage", fProjectStage);
67  mds->addMetadata(fExperiment + "_project.version", fProjectVersion);
68  mds->addMetadata(fExperiment + "_project.software", fProjectSoftware);
69  mds->addMetadata("production.name", fProductionName);
70  mds->addMetadata("production.type", fProductionType);
71  std::ostringstream ostr;
72  if(fMerge >= 0) {
73  if(fMerge > 0)
74  mds->addMetadata("merge.merge", "1");
75  else
76  mds->addMetadata("merge.merge", "0");
77  mds->addMetadata("merge.merged", "0");
78  }
79  for(auto const& param : fParameters)
80  mds->addMetadata(param.first, param.second);
81 }
82 
83 //--------------------------------------------------------------------
84 // PostEndSubrun callback.
86 {
87 
88  art::ServiceHandle<art::FileCatalogMetadata> mds;
89 
90  art::Handle< sumdata::POTSummary > potListHandle;
91  if(sr.getByLabel(fPOTModuleLabel,potListHandle)){
92  fTotPOT+=potListHandle->totpot;}
93 }
94 
95 // PreCloseOutputFile callback.
97 {
98 
99  art::ServiceHandle<art::FileCatalogMetadata> mds;
100 
101  if(fTotPOT > 0.) {
102  std::ostringstream streamObj;
103  streamObj << fTotPOT;
104  std::string strPOT = streamObj.str();
105  mds->addMetadata("mc.pot", strPOT);
106  }
107  fTotPOT = 0.;
108 }
109 
110 DEFINE_ART_SERVICE(util::FileCatalogMetadataSBN)
static constexpr Sample_t transform(Sample_t sample)
std::vector< std::pair< std::string, std::string > > fParameters
FileCatalogMetadataSBN(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
void preCloseOutputFile(const std::string &label)
void postEndSubRun(art::SubRun const &subrun)