GCNZlibMaker_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file GCNZlibMaker_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 <sstream>
12 #include "boost/filesystem.hpp"
13 
14 // Framework includes
20 
21 // Data products
24 
25 // CVN includes
29 
30 // Compression
31 #include "zlib.h"
32 
33 namespace fs = boost::filesystem;
34 
35 namespace cvn {
36 
37  class GCNZlibMaker : public art::EDAnalyzer {
38  public:
39 
40  explicit GCNZlibMaker(fhicl::ParameterSet const& pset);
41  ~GCNZlibMaker();
42 
43  void beginJob() override;
44  void analyze(const art::Event& evt) override;
45  void reconfigure(const fhicl::ParameterSet& pset);
46 
47  private:
48 
51  unsigned int fTopologyHitsCut;
52 
57 
59 
60  };
61 
62  //......................................................................
64  : EDAnalyzer(pset)
65  {
66  this->reconfigure(pset);
67  }
68 
69  //......................................................................
71  { }
72 
73  //......................................................................
75  {
76  fOutputDir = pset.get<std::string>("OutputDir", "");
77  fGraphLabel = pset.get<std::string>("GraphLabel");
78  fTopologyHitsCut = pset.get<unsigned int>("TopologyHitsCut");
79 
80  fGenieGenModuleLabel = pset.get<std::string>("GenieGenModuleLabel");
81  fEnergyNueLabel = pset.get<std::string>("EnergyNueLabel");
82  fEnergyNumuLabel = pset.get<std::string>("EnergyNumuLabel");
83  fEnergyNutauLabel = pset.get<std::string>("EnergyNutauLabel");
84  }
85 
86  //......................................................................
88  {
89  if (fOutputDir != "")
91 
92  else
93  out_dir = ".";
94 
95  // Throw an error if the specified output directory doesn't exist
96  if (!fs::exists(out_dir))
98  << "Output directory " << out_dir << " does not exist!" << std::endl;
99 
100  // std::cout << "Writing files to output directory " << out_dir << std::endl;
101  }
102 
103  //......................................................................
105  {
106 
107  std::cout << "GCNZlibMaker: looking for graphs with label " << fGraphLabel << std::endl;
108 
109  // Get the graphs
110  std::vector<art::Ptr<cvn::GCNGraph>> graphs;
111  auto h_graphs = evt.getHandle<std::vector<cvn::GCNGraph>>(fGraphLabel);
112  if (h_graphs)
113  art::fill_ptr_vector(graphs, h_graphs);
114 
115  // If no graphs, quit
116  std::cout << "Found " << graphs.size() << " graphs" << std::endl;
117  if (graphs.size() == 0) return;
118 
120 
121  // MC information
122  std::vector<art::Ptr<simb::MCTruth>> mctruth_list;
123  auto h_mctruth = evt.getHandle<std::vector<simb::MCTruth>>(fGenieGenModuleLabel);
124  if (h_mctruth)
125  art::fill_ptr_vector(mctruth_list, h_mctruth);
126 
127  art::Ptr<simb::MCTruth> mctruth = mctruth_list[0];
128  simb::MCNeutrino true_neutrino = mctruth->GetNeutrino();
129 
131 
132  interaction = labels.GetInteractionType(true_neutrino);
133  labels.GetTopology(mctruth, fTopologyHitsCut);
134 
135  // True lepton and neutrino energies
136  float nu_energy = true_neutrino.Nu().E();
137  float lep_energy = true_neutrino.Lepton().E();
138 
139  // We only want interactions in the fiducial volume for training
140  // Get the interaction vertex from the end point of the neutrino. This is
141  // because the start point of the lepton doesn't make sense for taus as they
142  // are decayed by the generator and not GEANT
143  TVector3 vtx = true_neutrino.Nu().EndPosition().Vect();
144  bool isFid = (fabs(vtx.X())<310. && fabs(vtx.Y())<550. && vtx.Z()>50. && vtx.Z()<1244.);
145  if(!isFid) return;
146 
147  float reco_nue_energy = 0.;
148  float reco_numu_energy = 0.;
149  float reco_nutau_energy = 0.;
150 
151  // Get nue info
152  if (fEnergyNueLabel != "") {
153  auto h_ereco = evt.getHandle<dune::EnergyRecoOutput>(fEnergyNueLabel);
154  reco_nue_energy = h_ereco->fNuLorentzVector.E();
155  }
156 
157  // Get numu info
158  if (fEnergyNueLabel != "") {
160  reco_numu_energy = h_ereco->fNuLorentzVector.E();
161  }
162 
163  // Get nutau info
164  if (fEnergyNutauLabel != "") {
166  reco_nutau_energy = h_ereco->fNuLorentzVector.E();
167  }
168 
169  float event_weight = -1.0; // We currently just set this to -1
170 
171  // If the graph has no nodes then give up
172  for(unsigned int i = 0; i < graphs.size(); ++i){
173  const art::Ptr<cvn::GCNGraph> g = graphs.at(i);
174 
175  std::cout << "GCNZlibMaker: found graph with " << g->GetNumberOfNodes() << " nodes" << std::endl;
176  if(g->GetNumberOfNodes() == 0) continue;
177 
178  // Now write the zlib file using this information
179  // We need to extract all of the information into a single vector to write
180  // into the compressed file format
181  const std::vector<float> vectorToWrite = g->ConvertGraphToVector();
182 
183  ulong src_len = vectorToWrite.size() *sizeof(float);
184  ulong dest_len = compressBound(src_len); // calculate size of the compressed data
185  char* ostream = (char *) malloc(dest_len); // allocate memory for the compressed data
186 
187  int res = compress((Bytef *) ostream, &dest_len, (Bytef *) &vectorToWrite[0], src_len);
188 
189  // Buffer error
190  if (res == Z_BUF_ERROR)
191  std::cout << "Buffer too small!" << std::endl;
192  // Memory error
193  else if (res == Z_MEM_ERROR)
194  std::cout << "Not enough memory for compression!" << std::endl;
195  // Compression ok
196  else {
197 
198  std::stringstream modifier;
199  if(graphs.size() > 1){
200  modifier << "_" << i;
201  }
202 
203  // Create output files
204  std::stringstream image_file_name;
205  image_file_name << out_dir << "/event_" << evt.event() << modifier.str() << ".gz";
206  std::stringstream info_file_name;
207  info_file_name << out_dir << "/event_" << evt.event() << modifier.str() << ".info";
208 
209  std::ofstream image_file (image_file_name.str(), std::ofstream::binary);
210  std::ofstream info_file (info_file_name.str());
211 
212  if(image_file.is_open() && info_file.is_open()) {
213 
214  // Write the graph to the file and close it
215  image_file.write(ostream, dest_len);
216  image_file.close(); // close file
217 
218  // Write the auxillary information to the text file
219  info_file << interaction << std::endl; // Interaction type first
220 
221  // True and reconstructed energy variables
222  info_file << nu_energy << std::endl;
223  info_file << lep_energy << std::endl;
224  info_file << reco_nue_energy << std::endl;
225  info_file << reco_numu_energy << std::endl;
226  info_file << reco_nutau_energy << std::endl;
227  info_file << event_weight << std::endl;
228 
229  info_file << labels.GetPDG() << std::endl;
230  info_file << labels.GetNProtons() << std::endl;
231  info_file << labels.GetNPions() << std::endl;
232  info_file << labels.GetNPizeros() << std::endl;
233  info_file << labels.GetNNeutrons() << std::endl;
234  info_file << labels.GetTopologyType() << std::endl;
235  info_file << labels.GetTopologyTypeAlt() << std::endl;
236 
237  // Number of nodes and node features is needed for unpacking
238  info_file << g->GetNumberOfNodes() << std::endl;
239  info_file << g->GetNumberOfNodeCoordinates() << std::endl;
240  info_file << g->GetNumberOfNodeFeatures() << std::endl;
241 
242  info_file.close(); // close file
243  }
244  else {
245 
246  if (image_file.is_open())
247  image_file.close();
248  else
250  << "Unable to open file " << image_file_name.str() << "!" << std::endl;
251 
252  if (info_file.is_open())
253  info_file.close();
254  else
256  << "Unable to open file " << info_file_name.str() << "!" << std::endl;
257  }
258  }
259  free(ostream); // free allocated memory
260  ostream = nullptr;
261  }
262 
263  return;
264  }
265 
267 } // namespace cvn
double E(const int i=0) const
Definition: MCParticle.h:233
const unsigned int GetNumberOfNodeCoordinates() const
Return the number of coordinates for each node.
Definition: GCNGraph.cxx:161
EventNumber_t event() const
Definition: DataViewImpl.cc:85
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
static constexpr double g
Definition: Units.h:144
const TLorentzVector & EndPosition() const
Definition: MCParticle.h:225
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
unsigned short GetNNeutrons()
Definition: AssignLabels.h:37
static constexpr double fs
Definition: Units.h:100
enum cvn::Interaction InteractionType
unsigned short GetTopologyType()
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Utility class for truth labels.
InteractionType GetInteractionType(simb::MCNeutrino &truth)
unsigned short GetNProtons()
Definition: AssignLabels.h:34
void beginJob() override
unsigned short GetTopologyTypeAlt()
bool exists(std::string path)
std::string fGenieGenModuleLabel
void GetTopology(const art::Ptr< simb::MCTruth > truth, unsigned int nTopologyHits)
const simb::MCParticle & Lepton() const
Definition: MCNeutrino.h:147
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
T get(std::string const &key) const
Definition: ParameterSet.h:271
unsigned long ulong
Definition: qglobal.h:352
void reconfigure(const fhicl::ParameterSet &pset)
void analyze(const art::Event &evt) override
Something else. Tau? Hopefully we don&#39;t use this.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string fEnergyNutauLabel
std::string fEnergyNumuLabel
GCNGraph for GCN.
const unsigned int GetNumberOfNodeFeatures() const
Return the number of features for each node.
Definition: GCNGraph.cxx:170
const std::vector< float > ConvertGraphToVector() const
Function to linearise the graph to a vector for zlib file creation.
Definition: GCNGraph.cxx:138
const unsigned int GetNumberOfNodes() const
Get the number of nodes.
Definition: GCNGraph.cxx:54
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
unsigned short GetNPions()
Definition: AssignLabels.h:35
Event generator information.
Definition: MCNeutrino.h:18
unsigned int fTopologyHitsCut
GCNZlibMaker(fhicl::ParameterSet const &pset)
QTextStream & endl(QTextStream &s)