HDF5Maker_module.cc
Go to the documentation of this file.
7 #include "cetlib_except/exception.h"
10 
11 #include "HDF5Maker/HDF5Record.h"
12 
14 
17 
18 #include "hep_hpc/hdf5/File.hpp"
19 #include "hep_hpc/hdf5/Group.hpp"
20 //#include "hep_hpc/hdf5/make_column.hpp"
21 #include "hep_hpc/hdf5/write_attribute.hpp"
22 
23 namespace hdf5
24 {
25  //.......................................................................
26  class HDF5Maker: public art::EDAnalyzer
27  {
28  public:
29 
30  //explicit CAFMaker(const Parameters& params);
31  explicit HDF5Maker(const fhicl::ParameterSet& pset);
32  virtual ~HDF5Maker() noexcept;
33 
35  void InitializeOutfile();
36 
37  void beginJob();
38  void analyze(const art::Event& evt) noexcept;
39  void endJob();
40 
41  protected:
42 
44  //std::string fFileType;
46 
47  //https://bitbucket.org/fnalscdcomputationalscience/hep_hpc/src/master/hep_hpc/hdf5/File.hpp
49 
50  std::unique_ptr<HDF5Record<unsigned int, unsigned int, int, unsigned int, unsigned short>> fRec;
51  std::unique_ptr<HDF5Spill<unsigned int, unsigned int, unsigned int>> fSpillRec;
52  std::unique_ptr<HDF5Neutrino<unsigned int, unsigned int, int, unsigned int>> fNeutrinoRec;
53 
55 
57 
59  size_t fChunkSize;
63 
64  };
65 
66  //.......................................................................
68  : art::EDAnalyzer(pset),
69  fH5Filename(pset.get<std::string>("H5Filename")),
70  //fFileType(pset.get<std::string>("FileType")),
71  fFileExtension(pset.get<std::string>("FileExtension")),
72  fStandardRecordLabel(pset.get<std::string>("StandardRecordLabel")),
73  fFillNuTree(pset.get<bool>("FillNuTree")),
74  fChunkSize(pset.get<size_t>("ChunkSize")),
75  fCompressionLevel(pset.get<size_t>("CompressionLevel")),
76  fElementsPerChunk(pset.get<size_t>("ElementsPerChunk")),
77  fShuffleInts(pset.get<bool>("ShuffleInts"))
78  {
79  assert((fChunkSize == 0 || fElementsPerChunk == 0) && "ChunkSize and ElementsPerChunk fcl parameters are not compatible. Set one of them to zero");
80  }
81 
82  //......................................................................
84  {
85  }
86 
87  //......................................................................
89  {
90  if (!fH5File) {
91  if (fH5Filename == "") {
92  // Filename wasn't set in the FCL. Construct it based on the input
93  // filename.
94  char temp[fb.fileName().size() + 1];
95  std::strcpy(temp, fb.fileName().c_str());
96  fH5Filename = basename(temp);
97 
98  // if input is PID file (most likely should be), be a little careful
99  // Allows for dots in the file name than just the last bit (eg. prod5.1)
100  if(fH5Filename.find(".pid.root") != std::string::npos) {
101  const size_t pidpos = fH5Filename.find(".pid.root");
102  fH5Filename.resize(pidpos);
103  }
104  else{
105  const size_t dotpos = fH5Filename.find('.');
106  assert(dotpos != std::string::npos); // Must have a dot, surely?
107  fH5Filename.resize(dotpos);
108  }
109  }
110 
112 
114  }
115  }
116 
117  //......................................................................
119  {
120  assert(!fH5File);
121  assert(!fH5Filename.empty());
122 
123  mf::LogInfo("HDF5Maker") << "Output filename is " << fH5Filename;
124 
125  fH5File = hep_hpc::hdf5::File(fH5Filename, H5F_ACC_TRUNC);
126 
127  fRec = std::make_unique<HDF5Record<unsigned int, unsigned int, int, unsigned int, unsigned short>>(fH5File, "rec", fChunkSize, fElementsPerChunk, fShuffleInts, fCompressionLevel, "run", "subrun", "cycle", "evt", "subevt");
128  consumes<std::vector<caf::StandardRecord>>(fStandardRecordLabel);
129 
130  fSpillRec = std::make_unique<HDF5Spill<unsigned int, unsigned int, unsigned int>>(fH5File, "spill", fChunkSize, fElementsPerChunk, fShuffleInts, fCompressionLevel, "run", "subrun", "evt");
131  consumes<caf::SRSpill>(fStandardRecordLabel);
132 
133  if(fFillNuTree) {
134  fNeutrinoRec = std::make_unique<HDF5Neutrino<unsigned int, unsigned int, int, unsigned int>>(fH5File, "neutrino", fChunkSize, fElementsPerChunk, fShuffleInts, fCompressionLevel, "run", "subrun", "cycle", "evt");
135  consumes<caf::SRSpillTruthBranch>(fStandardRecordLabel);
136  }
137 
138  }
139 
140  //......................................................................
142  {
143  }
144 
145  //......................................................................
146  void HDF5Maker::analyze(const art::Event& evt) noexcept
147  {
149  evt.getByLabel(fStandardRecordLabel, srcol);
150 
151  fIsRealData = evt.isRealData();
152 
153  for(const caf::StandardRecord& sr: *srcol)
154  fRec->Fill(sr.hdr.run, sr.hdr.subrun, sr.hdr.cycle, sr.hdr.evt, sr.hdr.subevt, sr);
155 
157  evt.getByLabel(fStandardRecordLabel, spill);
158 
159  fSpillRec->Fill(spill->run, spill->subrun, spill->evt, *spill);
160 
161  if (fFillNuTree){
163  evt.getByLabel(fStandardRecordLabel, srtruthcol);
164  for(const caf::SRSpillTruthBranch& sr: *srtruthcol)
165  fNeutrinoRec->Fill(sr.nu.run, sr.nu.subrun, sr.nu.cycle, sr.nu.evt, sr.nu);
166  }
167  }
168 
169  //......................................................................
171  {
172 
173  std::map<std::string, std::string> metadata = meta::MetadataManager::getInstance().GetMetadata();
174 
175  metadata["data_tier"] = "h5";
176 
177  metadata["file_type"] = "empty";
178 
179  if (fIsRealData){
180  metadata["file_type"] = "importedDetector";
181  }else{
182  metadata["file_type"] = "importedSimulated";
183  }
184 
185  hep_hpc::hdf5::Group g_m (fH5File, "MetaData" , hep_hpc::hdf5::Group::CREATE_MODE);
186 
187  std::string JsonStr = "{";
188 
189  for(auto const& pair:metadata){
190  //this can never be true...
191  if ( JsonStr!="{" ) {
192  JsonStr = JsonStr + ",";
193  }
194 
195  std::string key = "\"" + pair.first + "\"";
196  std::string val = "\"" + pair.second + "\"";
197  if(val.find("\"[") != std::string::npos) {
198  val.replace(val.find("\"["), 2, "[");
199  }
200  if(val.find("]\"") != std::string::npos) {
201  val.replace(val.find("]\""), 2, "]");
202  }
203 
204  JsonStr = JsonStr + "\n " + key + ": " + val;
205  }
206 
207  JsonStr = JsonStr + "\n}";
208 
209  hep_hpc::hdf5::write_attribute(g_m, "JSON", JsonStr);
210 
211  std::cout << "JSon Str is:\n" << JsonStr << std::endl;
212 
213  fH5File.flush();
214  }
215 
217 
218 } // end namespace hdf5
219 ////////////////////////////////////////////////////////////////////////
static MetadataManager & getInstance()
std::unique_ptr< HDF5Spill< unsigned int, unsigned int, unsigned int > > fSpillRec
SRHeader hdr
Header branch: run, subrun, etc.
unsigned int subrun
subrun number
Definition: SRHeader.h:22
hep_hpc::hdf5::File fH5File
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::string const & fileName() const
Definition: FileBlock.h:38
Truth info for all neutrinos in the spill.
std::string fFileExtension
unsigned int run
run number
Definition: SRHeader.h:21
std::map< std::string, std::string > & GetMetadata()
unsigned int run
run number
Definition: SRSpill.h:25
DEFINE_ART_MODULE(TestTMapFile)
unsigned int evt
ART event number, indexes trigger windows.
Definition: SRHeader.h:25
unsigned int subrun
subrun number
Definition: SRSpill.h:26
void respondToOpenInputFile(const art::FileBlock &fb)
virtual ~HDF5Maker() noexcept
int evt
unsigned int evt
ART event number, indexes trigger windows.
Definition: SRSpill.h:27
std::unique_ptr< HDF5Record< unsigned int, unsigned int, int, unsigned int, unsigned short > > fRec
std::unique_ptr< HDF5Neutrino< unsigned int, unsigned int, int, unsigned int > > fNeutrinoRec
caf::StandardRecord * sr
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
OStream cout
Definition: OStream.cxx:6
std::map< TString, std::map< TString, SystShifts > > Group
Definition: syst_header.h:40
The StandardRecord is the primary top-level object in the Common Analysis File trees.
std::string fStandardRecordLabel
std::string fH5Filename
HDF5Maker(const fhicl::ParameterSet &pset)
const int File
assert(nhit_max >=nhit_nbins)
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
void analyze(const art::Event &evt) noexcept
unsigned short subevt
slice number within spill
Definition: SRHeader.h:26
int cycle
MC simulation cycle number.
Definition: SRHeader.h:23
enum BeamMode string