CVNZlibMaker_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file CVNZlibMaker_module.cc
3 // \brief Analyzer module for creating CVN gzip file objects
4 // \author Jeremy Hewes - jhewes15@fnal.gov
5 // Saul Alonso-Monsalve - saul.alonso.monsalve@cern.ch
6 // - wrote the zlib code used in this module
7 ////////////////////////////////////////////////////////////////////////
8 
9 // C/C++ includes
10 #include <iostream>
11 
12 #include "boost/filesystem.hpp"
13 
14 // Framework includes
20 
21 // Data products
24 
25 // CVN includes
31 
32 // Compression
33 #include "zlib.h"
34 
35 namespace fs = boost::filesystem;
36 
37 namespace cvn {
38 
39  class CVNZlibMaker : public art::EDAnalyzer {
40  public:
41 
42  explicit CVNZlibMaker(fhicl::ParameterSet const& pset);
43  ~CVNZlibMaker();
44 
45  void beginJob() override;
46  void analyze(const art::Event& evt) override;
47  void reconfigure(const fhicl::ParameterSet& pset);
48 
49  private:
50 
53  bool fSetLog;
54  std::vector<bool> fReverseViews;
55  unsigned int fTopologyHitsCut;
56 
61 
62  unsigned int fPlaneLimit;
63  unsigned int fTDCLimit;
64 
66 
67  void write_files(TrainingData td, unsigned int n, std::string evtid);
68 
69  };
70 
71  //......................................................................
73  : EDAnalyzer(pset)
74  {
75  this->reconfigure(pset);
76  }
77 
78  //......................................................................
80  { }
81 
82  //......................................................................
84  {
85  fOutputDir = pset.get<std::string>("OutputDir", "");
86  fPixelMapInput = pset.get<std::string>("PixelMapInput");
87  fSetLog = pset.get<bool>("SetLog");
88  fReverseViews = pset.get<std::vector<bool>>("ReverseViews");
89  fTopologyHitsCut = pset.get<unsigned int>("TopologyHitsCut");
90 
91  fGenieGenModuleLabel = pset.get<std::string>("GenieGenModuleLabel");
92  fEnergyNueLabel = pset.get<std::string>("EnergyNueLabel");
93  fEnergyNumuLabel = pset.get<std::string>("EnergyNumuLabel");
94  fEnergyNutauLabel = pset.get<std::string>("EnergyNutauLabel");
95 
96  fPlaneLimit = pset.get<unsigned int>("PlaneLimit");
97  fTDCLimit = pset.get<unsigned int>("TDCLimit");
98  }
99 
100  //......................................................................
102  {
103  // Set the output directory.
104  // First we look for CONDOR_DIR_INPUT and set it to the grid location.
105  // If it isn't there, we look for a FHICL parameter.
106  // Otherwise it just goes to the current directory.
107 /*
108  char const* grid_dir = getenv("CONDOR_DIR_INPUT");
109  if (grid_dir != NULL) {
110  char const* tmp_grid_dir = getenv("TMP");
111  if (tmp_grid_dir == NULL)
112  throw art::Exception(art::errors::NotFound)
113  << "Could not find environment variable \"TMP\" which "
114  << "is usually set for condor_lar environment." << std::endl;
115  out_dir = std::string(tmp_grid_dir) + "/out";
116  }
117 */
118  if (fOutputDir != "")
120 
121  else
122  out_dir = ".";
123 
124  // Throw an error if the specified output directory doesn't exist
125  if (!fs::exists(out_dir))
127  << "Output directory " << out_dir << " does not exist!" << std::endl;
128 
129  // std::cout << "Writing files to output directory " << out_dir << std::endl;
130  }
131 
132  //......................................................................
134  {
135 
136  // Get the pixel maps
137  std::vector<art::Ptr<cvn::PixelMap>> pixelmaps;
139  auto h_pixelmaps = evt.getHandle<std::vector<cvn::PixelMap>>(itag1);
140  if (h_pixelmaps)
141  art::fill_ptr_vector(pixelmaps, h_pixelmaps);
142 
143  // If no pixel maps, quit
144  if (pixelmaps.size() == 0) return;
145 
147 
148  // MC information
149  std::vector<art::Ptr<simb::MCTruth>> mctruth_list;
150  auto h_mctruth = evt.getHandle<std::vector<simb::MCTruth>>(fGenieGenModuleLabel);
151  if (h_mctruth)
152  art::fill_ptr_vector(mctruth_list, h_mctruth);
153 
154  art::Ptr<simb::MCTruth> mctruth = mctruth_list[0];
155  simb::MCNeutrino true_neutrino = mctruth->GetNeutrino();
156 
158 
159  interaction = labels.GetInteractionType(true_neutrino);
160  labels.GetTopology(mctruth, fTopologyHitsCut);
161 
162  // True lepton and neutrino energies
163  float nu_energy = true_neutrino.Nu().E();
164  float lep_energy = true_neutrino.Lepton().E();
165 
166  // Put a containment cut here
167 
168  float reco_nue_energy = 0;
169  float reco_numu_energy = 0;
170  float reco_nutau_energy = 0;
171 
172  // Get nue info
173  if (fEnergyNueLabel != "") {
174  auto h_ereco = evt.getHandle<dune::EnergyRecoOutput>(fEnergyNueLabel);
175  reco_nue_energy = h_ereco->fNuLorentzVector.E();
176  }
177 
178  // Get numu info
179  if (fEnergyNueLabel != "") {
181  reco_numu_energy = h_ereco->fNuLorentzVector.E();
182  }
183 
184  // Get nutau info
185  if (fEnergyNutauLabel != "") {
187  reco_nutau_energy = h_ereco->fNuLorentzVector.E();
188  }
189 
190  // Hard-coding event weight for now
191  // Should probably fix this at some point
192  int event_weight = 1;
193 
194  TrainingData train(interaction, nu_energy, lep_energy,
195  reco_nue_energy, reco_numu_energy, reco_nutau_energy,
196  event_weight, *pixelmaps[0]);
197 
198  int pdg = labels.GetPDG();
199  int n_proton = labels.GetNProtons();
200  int n_pion = labels.GetNPions();
201  int n_pi0 = labels.GetNPizeros();
202  int n_neutron = labels.GetNNeutrons();
203  int toptype = labels.GetTopologyType();
204  int toptypealt = labels.GetTopologyTypeAlt();
205 
206  train.SetTopologyInformation(pdg, n_proton, n_pion,
207  n_pi0, n_neutron, toptype, toptypealt);
208 
209  std::string evtid = "r"+std::to_string(evt.run())+"_s"+std::to_string(evt.subRun())+"_e"+std::to_string(evt.event());
210  this->write_files(train, evt.event(), evtid);
211  }
212 
213  //......................................................................
214  void CVNZlibMaker::write_files(TrainingData td, unsigned int n, std::string evtid)
215  {
216  // cropped from 2880 x 500 to 500 x 500 here
217  std::vector<unsigned char> pixel_array(3 * fPlaneLimit * fTDCLimit);
218 
219  CVNImageUtils image_utils(fPlaneLimit, fTDCLimit, 3);
220  image_utils.SetPixelMapSize(td.fPMap.NWire(), td.fPMap.NTdc());
221  image_utils.SetLogScale(fSetLog);
222  image_utils.SetViewReversal(fReverseViews);
223  image_utils.ConvertPixelMapToPixelArray(td.fPMap, pixel_array);
224 
225  ulong src_len = 3 * fPlaneLimit * fTDCLimit; // pixelArray length
226  ulong dest_len = compressBound(src_len); // calculate size of the compressed data
227  char* ostream = (char *) malloc(dest_len); // allocate memory for the compressed data
228 
229  int res = compress((Bytef *) ostream, &dest_len, (Bytef *) &pixel_array[0], src_len);
230 
231  // Buffer error
232 
233  if (res == Z_BUF_ERROR)
234  std::cout << "Buffer too small!" << std::endl;
235 
236  // Memory error
237  else if (res == Z_MEM_ERROR)
238  std::cout << "Not enough memory for compression!" << std::endl;
239 
240  // Compression ok
241  else {
242 
243  // Create output files
244  std::string image_file_name = out_dir + "/event_" + evtid + ".gz";
245  std::string info_file_name = out_dir + "/event_" + evtid + ".info";
246 
247  std::ofstream image_file (image_file_name, std::ofstream::binary);
248  std::ofstream info_file (info_file_name);
249 
250  if(image_file.is_open() && info_file.is_open()) {
251 
252  // Write compressed data to file
253 
254  image_file.write(ostream, dest_len);
255 
256  image_file.close(); // close file
257 
258  // Write records to file
259 
260  // Category
261 
262  info_file << td.fInt << std::endl;
263 
264  // Energy
265 
266  info_file << td.fNuEnergy << std::endl;
267  info_file << td.fLepEnergy << std::endl;
268  info_file << td.fRecoNueEnergy << std::endl;
269  info_file << td.fRecoNumuEnergy << std::endl;
270  info_file << td.fRecoNutauEnergy << std::endl;
271  info_file << td.fEventWeight << std::endl;
272 
273  // Topology
274 
275  info_file << td.fNuPDG << std::endl;
276  info_file << td.fNProton << std::endl;
277  info_file << td.fNPion << std::endl;
278  info_file << td.fNPizero << std::endl;
279  info_file << td.fNNeutron << std::endl;
280 
281  info_file << td.fTopologyType << std::endl;
282  info_file << td.fTopologyTypeAlt << std::endl;
283  info_file << td.fPMap.GetTotHits() << std::endl;
284 
285  info_file.close(); // close file
286  }
287  else {
288 
289  if (image_file.is_open())
290  image_file.close();
291  else
293  << "Unable to open file " << image_file_name << "!" << std::endl;
294 
295  if (info_file.is_open())
296  info_file.close();
297  else
299  << "Unable to open file " << info_file_name << "!" << std::endl;
300  }
301  }
302 
303  free(ostream); // free allocated memory
304 
305  } // cvn::CVNZlibMaker::write_files
306 
308 } // namespace cvn
double E(const int i=0) const
Definition: MCParticle.h:233
float fRecoNumuEnergy
Reconstructed energy under numu hypothesis.
Definition: TrainingData.h:45
EventNumber_t event() const
Definition: DataViewImpl.cc:85
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
InteractionType fInt
Class of the event.
Definition: TrainingData.h:41
void write_files(TrainingData td, unsigned int n, std::string evtid)
unsigned short GetNPizeros()
Definition: AssignLabels.h:36
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
void SetViewReversal(bool reverseX, bool reverseY, bool reverseZ)
Function to set any views that need reversing.
unsigned int GetTotHits()
Definition: PixelMap.h:68
unsigned short GetNNeutrons()
Definition: AssignLabels.h:37
void SetLogScale(bool setLog)
Set the log scale for charge.
static constexpr double fs
Definition: Units.h:100
enum cvn::Interaction InteractionType
std::vector< bool > fReverseViews
unsigned short GetTopologyType()
CVNZlibMaker(fhicl::ParameterSet const &pset)
void ConvertPixelMapToPixelArray(const PixelMap &pm, std::vector< unsigned char > &pix)
Convert a Pixel Map object into a single pixel array with an image size nWire x nTDC.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
PixelMap for CVN.
Utility class for truth labels.
InteractionType GetInteractionType(simb::MCNeutrino &truth)
float fRecoNueEnergy
Reconstructed energy under nue hypothesis.
Definition: TrainingData.h:44
unsigned short GetNProtons()
Definition: AssignLabels.h:34
unsigned short GetTopologyTypeAlt()
PixelMap fPMap
PixelMap for the event.
Definition: TrainingData.h:59
std::string fEnergyNutauLabel
def train(model, train_files, valid_files, maskpatterns, epochs, batchsize, info)
Definition: train.py:16
bool exists(std::string path)
std::string fEnergyNumuLabel
void GetTopology(const art::Ptr< simb::MCTruth > truth, unsigned int nTopologyHits)
float fLepEnergy
True energy of outgoing lepton.
Definition: TrainingData.h:43
const simb::MCParticle & Lepton() const
Definition: MCNeutrino.h:147
float fNuEnergy
True energy of neutrino event.
Definition: TrainingData.h:42
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
Utilities for producing images for the CVN.
std::void_t< T > n
void beginJob() override
T get(std::string const &key) const
Definition: ParameterSet.h:271
unsigned long ulong
Definition: qglobal.h:352
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
RunNumber_t run() const
Definition: DataViewImpl.cc:71
Class containing some utility functions for all things CVN.
Definition: CVNImageUtils.h:24
Something else. Tau? Hopefully we don&#39;t use this.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
unsigned int NTdc() const
Width in tdcs.
Definition: PixelMap.h:32
The TrainingData objects contains a PixelMap and the output class type, and any other bit that goes i...
Definition: TrainingData.h:20
float fRecoNutauEnergy
Reconstructed energy under nutau hypothesis.
Definition: TrainingData.h:46
void analyze(const art::Event &evt) override
unsigned int fTopologyHitsCut
void reconfigure(const fhicl::ParameterSet &pset)
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
std::string fGenieGenModuleLabel
unsigned short GetNPions()
Definition: AssignLabels.h:35
unsigned int NWire() const
Length in wires.
Definition: PixelMap.h:29
Event generator information.
Definition: MCNeutrino.h:18
float fEventWeight
The event weight (norm * oscProb)
Definition: TrainingData.h:47
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
void SetTopologyInformation(int pdg, int nproton, int npion, int npizero, int nneutron, int toptype, int toptypealt)
QTextStream & endl(QTextStream &s)
void SetPixelMapSize(unsigned int nWires, unsigned int nTDCs)
Set the input pixel map size.