CVNZlibMakerProtoDUNE_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file CVNZlibMakerProtoDUNE_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 #include "boost/filesystem.hpp"
12 
13 // Framework includes
18 
20 // Data products
23 
24 // CVN includes
28 
29 // Compression
30 #include "zlib.h"
31 
32 namespace fs = boost::filesystem;
33 
34 namespace cvn {
35 
37  public:
38 
39  explicit CVNZlibMakerProtoDUNE(fhicl::ParameterSet const& pset);
41 
42  void beginJob() override;
43  void analyze(const art::Event& evt) override;
44  void reconfigure(const fhicl::ParameterSet& pset);
45 
47 
48  PrimaryTrainingInfo(TVector3 vtx, unsigned short inter, int pdg, float en){
49  vertex = vtx;
50  interaction = inter;
51  pdgCode = pdg;
52  energy = en;
53  };
54 
55  TVector3 vertex;
56  unsigned short interaction;
57  int pdgCode;
58  float energy;
59 
60  };
61 
62  private:
63 
66  bool fSetLog;
67  std::vector<bool> fReverseViews;
68 
70 
72 
73  void write_files(const PrimaryTrainingInfo &primary, const art::Ptr<cvn::PixelMap> pm, unsigned int n) const;
74 
75  };
76 
77  //......................................................................
79  : EDAnalyzer(pset)
80  {
81  this->reconfigure(pset);
82  }
83 
84  //......................................................................
86  { }
87 
88  //......................................................................
90  {
91  fOutputDir = pset.get<std::string>("OutputDir", "");
92  fPixelMapInput = pset.get<std::string>("PixelMapInput");
93  fSetLog = pset.get<bool>("SetLog");
94  fReverseViews = pset.get<std::vector<bool>>("ReverseViews");
95  fLArG4ModuleLabel = pset.get<std::string>("LArG4ModuleLabel");
96  }
97 
98  //......................................................................
100  {
101  // Set the output directory.
102  // First we look for CONDOR_DIR_INPUT and set it to the grid location.
103  // If it isn't there, we look for a FHICL parameter.
104  // Otherwise it just goes to the current directory.
105 /*
106  char const* grid_dir = getenv("CONDOR_DIR_INPUT");
107  if (grid_dir != NULL) {
108  char const* tmp_grid_dir = getenv("TMP");
109  if (tmp_grid_dir == NULL)
110  throw art::Exception(art::errors::NotFound)
111  << "Could not find environment variable \"TMP\" which "
112  << "is usually set for condor_lar environment." << std::endl;
113  out_dir = std::string(tmp_grid_dir) + "/out";
114  }
115 */
116  if (fOutputDir != "")
118 
119  else
120  out_dir = ".";
121 
122  // Throw an error if the specified output directory doesn't exist
123  if (!fs::exists(out_dir))
125  << "Output directory " << out_dir << " does not exist!" << std::endl;
126 
127  // std::cout << "Writing files to output directory " << out_dir << std::endl;
128  }
129 
130  //......................................................................
132  {
133 
134  // Get the pixel maps
135  std::vector<art::Ptr<cvn::PixelMap>> pixelmaps;
137  auto h_pixelmaps = evt.getHandle<std::vector<cvn::PixelMap>>(itag1);
138  if (h_pixelmaps)
139  art::fill_ptr_vector(pixelmaps, h_pixelmaps);
140 
141  // If no pixel maps, quit
142  if (pixelmaps.size() == 0) return;
143 
144  // MC information
146 
147  // The truth information we need is the interaction vertex and interaction type
149  unsigned short beamParticleInteraction;
150 
151  // We only have one beam particle called "primary", so find it. It should be the
152  // first particle, but best not to assume
153  float beamParticleEnergy = 0;
154  TVector3 beamParticleVtx; // This is the interaction vertex
155  int beamParticlePDG = 0;
156 
157  bool gotPrimary = false;
158  for(auto const m : piService->ParticleList()){
159  const simb::MCParticle* particle = m.second;
160  if(particle->Process().compare("primary")==0){
161  beamParticleEnergy = particle->E();
162  beamParticleVtx.SetXYZ(particle->EndX(),particle->EndY(),particle->EndZ());
163  beamParticlePDG = particle->PdgCode();
164  beamParticleInteraction = labels.GetProtoDUNEBeamInteractionType(*particle);
165  gotPrimary = true;
166  break;
167  }
168  }
169 
170  if(!gotPrimary) return;
171 
172  const PrimaryTrainingInfo beamPrimary(beamParticleVtx,beamParticleInteraction,beamParticlePDG,beamParticleEnergy);
173 
174  this->write_files(beamPrimary, pixelmaps.at(0), evt.event());
175  }
176 
177  //......................................................................
179  {
180  CVNImageUtils image_utils;
181  std::vector<unsigned char> pixel_array(3 * pm->NWire() * pm->NTdc());
182 
183  image_utils.DisableRegionSelection();
184  image_utils.SetLogScale(fSetLog);
185  image_utils.SetViewReversal(fReverseViews);
186  image_utils.ConvertPixelMapToPixelArray(*(pm.get()),pixel_array);
187 
188  ulong src_len = 3 * pm->NWire() * pm->NTdc(); // pixelArray length
189  ulong dest_len = compressBound(src_len); // calculate size of the compressed data
190  char* ostream = (char *) malloc(dest_len); // allocate memory for the compressed data
191 
192  int res = compress((Bytef *) ostream, &dest_len, (Bytef *) &pixel_array[0], src_len);
193 
194  // Buffer error
195 
196  if (res == Z_BUF_ERROR)
197  std::cout << "Buffer too small!" << std::endl;
198 
199  // Memory error
200  else if (res == Z_MEM_ERROR)
201  std::cout << "Not enough memory for compression!" << std::endl;
202 
203  // Compression ok
204  else {
205 
206  // Create output files
207  std::string image_file_name = out_dir + "/cvn_event_" + std::to_string(n) + ".gz";
208  std::string info_file_name = out_dir + "/cvn_event_" + std::to_string(n) + ".info";
209 
210  std::ofstream image_file (image_file_name, std::ofstream::binary);
211  std::ofstream info_file (info_file_name);
212 
213  if(image_file.is_open() && info_file.is_open()) {
214 
215  // Write compressed data to file
216 
217  image_file.write(ostream, dest_len);
218 
219  image_file.close(); // close file
220 
221  // Write truth information
222 
223  info_file << primary.vertex.X() << std::endl;
224  info_file << primary.vertex.Y() << std::endl;
225  info_file << primary.vertex.Z() << std::endl;
226  info_file << primary.energy << std::endl;
227  info_file << primary.interaction << std::endl;
228  info_file << primary.pdgCode << std::endl;
229  info_file.close(); // close file
230  }
231  else {
232 
233  if (image_file.is_open())
234  image_file.close();
235  else
237  << "Unable to open file " << image_file_name << "!" << std::endl;
238 
239  if (info_file.is_open())
240  info_file.close();
241  else
243  << "Unable to open file " << info_file_name << "!" << std::endl;
244  }
245  }
246 
247  free(ostream); // free allocated memory
248 
249  } // cvn::CVNZlibMakerProtoDUNE::write_files
250 
252 } // namespace cvn
double E(const int i=0) const
Definition: MCParticle.h:233
int PdgCode() const
Definition: MCParticle.h:212
EventNumber_t event() const
Definition: DataViewImpl.cc:85
double EndZ() const
Definition: MCParticle.h:228
void DisableRegionSelection()
Disable the selection of the wire region and just use the first 500 wires.
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
void SetViewReversal(bool reverseX, bool reverseY, bool reverseZ)
Function to set any views that need reversing.
void SetLogScale(bool setLog)
Set the log scale for charge.
static constexpr double fs
Definition: Units.h:100
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
std::string Process() const
Definition: MCParticle.h:215
PixelMap for CVN.
Utility class for truth labels.
Particle class.
double EndY() const
Definition: MCParticle.h:227
void write_files(const PrimaryTrainingInfo &primary, const art::Ptr< cvn::PixelMap > pm, unsigned int n) const
bool exists(std::string path)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
Utilities for producing images for the CVN.
unsigned short GetProtoDUNEBeamInteractionType(const simb::MCParticle &particle) const
std::void_t< T > n
void reconfigure(const fhicl::ParameterSet &pset)
T get(std::string const &key) const
Definition: ParameterSet.h:271
unsigned long ulong
Definition: qglobal.h:352
const sim::ParticleList & ParticleList() const
Class containing some utility functions for all things CVN.
Definition: CVNImageUtils.h:24
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
unsigned int NTdc() const
Width in tdcs.
Definition: PixelMap.h:32
PrimaryTrainingInfo(TVector3 vtx, unsigned short inter, int pdg, float en)
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
void analyze(const art::Event &evt) override
double EndX() const
Definition: MCParticle.h:226
T const * get() const
Definition: Ptr.h:149
unsigned int NWire() const
Length in wires.
Definition: PixelMap.h:29
CVNZlibMakerProtoDUNE(fhicl::ParameterSet const &pset)
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
Definition: fwd.h:31
QTextStream & endl(QTextStream &s)
vertex reconstruction