LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
LArNuCVNZlibMaker_module.cc
Go to the documentation of this file.
1 // \file LArNuCVNZlibMaker_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
8 
9 // C/C++ includes
10 #include <cstdlib>
11 #include <iostream>
12 
16 #include "art_root_io/TFileDirectory.h"
17 #include "art_root_io/TFileService.h"
18 #include "boost/filesystem.hpp"
19 
20 // Framework includes
30 
31 // Data products
36 // #include "dunereco/FDSensOpt/FDSensOptData/EnergyRecoOutput.h"
37 
38 // CVN includes
45 
46 // Compression
47 #include "math.h"
48 #include "zlib.h"
49 
50 #include "TH1.h"
52 
53 namespace lcvn {
54 
56  public:
57  explicit LArNuCVNZlibMaker(fhicl::ParameterSet const& pset);
59 
60  void beginJob();
61  void endSubRun(art::SubRun const& sr);
62  void analyze(const art::Event& evt);
63  void reconfigure(const fhicl::ParameterSet& pset);
64 
65  private:
66  unsigned int fTopologyHitsCut;
67  std::string fGenieGenModuleLabel;
69  std::vector<double> fFidMinCoords;
70  std::vector<double> fFidMaxCoords;
71 
72  void write_files(LArTrainingNuData td, std::string evtid);
73 
74  TH1D* hPOT;
75  double fPOT;
76  int fRun;
77  int fSubRun;
78  };
79 
80  //......................................................................
82  {
83  this->reconfigure(pset);
84  }
85 
86  //......................................................................
88 
89  //......................................................................
91  {
93 
94  fTopologyHitsCut = pset.get<unsigned int>("TopologyHitsCut");
95  fGenieGenModuleLabel = pset.get<std::string>("GenieGenModuleLabel");
96  fApplyFidVol = pset.get<bool>("ApplyFidVol");
97  fFidMinCoords = pset.get<std::vector<double>>("FidMinCoords");
98  fFidMaxCoords = pset.get<std::vector<double>>("FidMaxCoords");
99  }
100 
101  //......................................................................
103  {
104 
105  std::string fPOTModuleLabel = "generator";
106  fRun = sr.run();
107  fSubRun = sr.subRun();
108 
109  art::Handle<sumdata::POTSummary> potListHandle;
110  if (sr.getByLabel(fPOTModuleLabel, potListHandle))
111  fPOT = potListHandle->totpot;
112  else
113  fPOT = 0.;
114  if (hPOT) hPOT->Fill(0.5, fPOT);
115  }
116 
117  //......................................................................
119  {
121 
123  hPOT = tfs->make<TH1D>("TotalPOT", "Total POT;; POT", 1, 0, 1);
124  }
125 
126  //......................................................................
128  {
129 
130  // Get the pixel maps
131  std::vector<art::Ptr<lcvn::PixelMap>> pixelmaps;
133  auto h_pixelmaps = evt.getHandle<std::vector<lcvn::PixelMap>>(itag1);
134  if (h_pixelmaps) art::fill_ptr_vector(pixelmaps, h_pixelmaps);
135 
136  // If no pixel maps, quit
137  if (pixelmaps.size() == 0) return;
138 
140 
141  // MC information
142  std::vector<art::Ptr<simb::MCTruth>> mctruth_list;
143  auto h_mctruth = evt.getHandle<std::vector<simb::MCTruth>>(fGenieGenModuleLabel);
144  if (h_mctruth) art::fill_ptr_vector(mctruth_list, h_mctruth);
145 
146  art::Ptr<simb::MCTruth> mctruth = mctruth_list[0];
147  simb::MCNeutrino true_neutrino = mctruth->GetNeutrino();
148 
149  // Hard-coding event weight for now
150  // Should probably fix this at some point
151  double event_weight = 1.;
152 
153  AssignLabels labels;
154 
155  interaction = labels.GetInteractionType(true_neutrino);
156  labels.GetTopology(mctruth, fTopologyHitsCut);
157 
158  // True lepton and neutrino energies
159  float nu_energy = true_neutrino.Nu().E();
160  float lep_energy = true_neutrino.Lepton().E();
161 
162  // Put a containment cut here
163  // If outside the fiducial volume don't waste any time filling other variables
164  if (fApplyFidVol) {
165  // Get the interaction vertex from the end point of the neutrino. This is
166  // because the start point of the lepton doesn't make sense for taus as they
167  // are decayed by the generator and not GEANT
168  TVector3 vtx = true_neutrino.Nu().EndPosition().Vect();
169  bool isFid = (vtx.X() > fFidMinCoords[0] && vtx.X() < fFidMaxCoords[0]) &&
170  (vtx.Y() > fFidMinCoords[1] && vtx.Y() < fFidMaxCoords[1]) &&
171  (vtx.Z() > fFidMinCoords[2] && vtx.Z() < fFidMaxCoords[2]);
172  if (!isFid) return;
173  }
174 
175  TDNuInfo info;
176  info.SetTruthInfo(nu_energy, lep_energy, 0., event_weight);
177  info.SetTopologyInformation(labels.GetPDG(),
178  labels.GetNProtons(),
179  labels.GetNPions(),
180  labels.GetNPizeros(),
181  labels.GetNNeutrons(),
182  labels.GetTopologyType(),
183  labels.GetTopologyTypeAlt());
184 
185  LArTrainingNuData train(interaction, *pixelmaps[0], info);
186 
187  std::string evtid = "r" + std::to_string(evt.run()) + "_s" + std::to_string(evt.subRun()) +
188  "_e" + std::to_string(evt.event()) + "_h" + std::to_string(time(0));
189  this->write_files(train, evtid);
190  }
191 
192  //......................................................................
194  {
195  // cropped from 2880 x 500 to 500 x 500 here
196  std::vector<unsigned char> pixel_array(3 * fPlaneLimit * fTDCLimit);
197 
199  fImage.ConvertPixelMapToPixelArray(td.fPMap, pixel_array);
200 
201  ulong src_len = 3 * fPlaneLimit * fTDCLimit; // pixelArray length
202  ulong dest_len = compressBound(src_len); // calculate size of the compressed data
203  char* ostream = (char*)malloc(dest_len); // allocate memory for the compressed data
204 
205  int res = compress((Bytef*)ostream, &dest_len, (Bytef*)&pixel_array[0], src_len);
206 
207  // Buffer error
208 
209  if (res == Z_BUF_ERROR) std::cout << "Buffer too small!" << std::endl;
210 
211  // Memory error
212  else if (res == Z_MEM_ERROR)
213  std::cout << "Not enough memory for compression!" << std::endl;
214 
215  // Compression ok
216  else {
217 
218  // Create output files
219  std::string image_file_name = out_dir + "/event_" + evtid + ".gz";
220  std::string info_file_name = out_dir + "/event_" + evtid + ".info";
221 
222  std::ofstream image_file(image_file_name, std::ofstream::binary);
223  std::ofstream info_file(info_file_name);
224 
225  if (image_file.is_open() && info_file.is_open()) {
226 
227  // Write compressed data to file
228 
229  image_file.write(ostream, dest_len);
230 
231  image_file.close(); // close file
232 
233  // Write records to file
234 
235  // Category
236 
237  info_file << td.fInt << std::endl;
238  info_file << td.fInfo << std::endl;
239  info_file << td.fPMap.GetTotHits() << std::endl;
240 
241  info_file.close(); // close file
242  }
243  else {
244 
245  if (image_file.is_open())
246  image_file.close();
247  else
249  << "Unable to open file " << image_file_name << "!" << std::endl;
250 
251  if (info_file.is_open())
252  info_file.close();
253  else
255  << "Unable to open file " << info_file_name << "!" << std::endl;
256  }
257  }
258 
259  free(ostream); // free allocated memory
260 
261  } // lcvn::LArNuCVNZlibMaker::write_files
262 
264 } // namespace lcvn
double E(const int i=0) const
Definition: MCParticle.h:234
unsigned int fTDCLimit
Definition: ICVNZlibMaker.h:69
SubRunNumber_t subRun() const
Definition: Event.cc:35
unsigned int GetTotHits()
Definition: PixelMap.h:63
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
InteractionType GetInteractionType(simb::MCNeutrino &truth) const
const TLorentzVector & EndPosition() const
Definition: MCParticle.h:226
std::string out_dir
Definition: ICVNZlibMaker.h:71
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
std::vector< double > fFidMinCoords
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.
Utility class for truth labels.
PixelMap for CVN.
std::vector< double > fFidMaxCoords
unsigned short GetTopologyTypeAlt() const
void SetTopologyInformation(int pdg, int nproton, int npion, int npizero, int nneutron, int toptype, int toptypealt)
void SetPixelMapSize(unsigned int nWires, unsigned int nTDCs)
Set the input pixel map size.
object containing MC flux information
void reconfigure(const fhicl::ParameterSet &pset)
unsigned short GetTopologyType() const
LArNuCVNZlibMaker(fhicl::ParameterSet const &pset)
if(nlines<=0)
const simb::MCParticle & Lepton() const
Definition: MCNeutrino.h:147
std::string fPixelMapInput
Definition: ICVNZlibMaker.h:65
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
unsigned int fPlaneLimit
Definition: ICVNZlibMaker.h:68
Utilities for producing images for the CVN.
unsigned int NWire() const
Length in wires.
Definition: PixelMap.h:26
T get(std::string const &key) const
Definition: ParameterSet.h:314
Something else. Tau? Hopefully we don&#39;t use this.
CVNImageUtils fImage
Definition: ICVNZlibMaker.h:72
EventNumber_t event() const
Definition: Event.cc:41
void write_files(LArTrainingNuData td, std::string evtid)
void analyze(const art::Event &evt)
unsigned short GetNPions() const
Definition: AssignLabels.h:30
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void GetTopology(const art::Ptr< simb::MCTruth > truth, unsigned int nTopologyHits)
Handle< PROD > getHandle(SelectorBase const &) const
SubRunNumber_t subRun() const
Definition: SubRun.cc:37
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
The TrainingData objects contains a PixelMap and the output class type, and any other bit that goes i...
unsigned short GetNProtons() const
Definition: AssignLabels.h:29
void SetTruthInfo(float nuEnergy, float lepEnergy, float lepAngle, float weight)
short GetPDG() const
Definition: AssignLabels.h:33
unsigned short GetNNeutrons() const
Definition: AssignLabels.h:32
void reconfigure(const fhicl::ParameterSet &pset)
unsigned int NTdc() const
Width in tdcs.
Definition: PixelMap.h:29
TCEvent evt
Definition: DataStructs.cxx:8
PixelMap fPMap
PixelMap for the event.
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:306
RunNumber_t run() const
Definition: Event.cc:29
Event generator information.
Definition: MCNeutrino.h:18
void beginJob() override
InteractionType fInt
Class of the event.
unsigned short GetNPizeros() const
Definition: AssignLabels.h:31
RunNumber_t run() const
Definition: SubRun.cc:31
void endSubRun(art::SubRun const &sr)