Public Member Functions | Private Attributes | List of all members
cvn::GCNZlibMaker Class Reference
Inheritance diagram for cvn::GCNZlibMaker:
art::EDAnalyzer art::detail::Analyzer art::detail::LegacyModule art::Observer art::ModuleBase

Public Member Functions

 GCNZlibMaker (fhicl::ParameterSet const &pset)
 
 ~GCNZlibMaker ()
 
void beginJob () override
 
void analyze (const art::Event &evt) override
 
void reconfigure (const fhicl::ParameterSet &pset)
 
- Public Member Functions inherited from art::EDAnalyzer
 EDAnalyzer (fhicl::ParameterSet const &pset)
 
template<typename Config >
 EDAnalyzer (Table< Config > const &config)
 
std::string workerType () const
 
- Public Member Functions inherited from art::detail::Analyzer
virtual ~Analyzer () noexcept
 
 Analyzer (fhicl::ParameterSet const &pset)
 
template<typename Config >
 Analyzer (Table< Config > const &config)
 
void doBeginJob (SharedResources const &resources)
 
void doEndJob ()
 
void doRespondToOpenInputFile (FileBlock const &fb)
 
void doRespondToCloseInputFile (FileBlock const &fb)
 
void doRespondToOpenOutputFiles (FileBlock const &fb)
 
void doRespondToCloseOutputFiles (FileBlock const &fb)
 
bool doBeginRun (RunPrincipal &rp, ModuleContext const &mc)
 
bool doEndRun (RunPrincipal &rp, ModuleContext const &mc)
 
bool doBeginSubRun (SubRunPrincipal &srp, ModuleContext const &mc)
 
bool doEndSubRun (SubRunPrincipal &srp, ModuleContext const &mc)
 
bool doEvent (EventPrincipal &ep, ModuleContext const &mc, std::atomic< std::size_t > &counts_run, std::atomic< std::size_t > &counts_passed, std::atomic< std::size_t > &counts_failed)
 
- Public Member Functions inherited from art::Observer
 ~Observer () noexcept
 
 Observer (Observer const &)=delete
 
 Observer (Observer &&)=delete
 
Observeroperator= (Observer const &)=delete
 
Observeroperator= (Observer &&)=delete
 
void registerProducts (ProductDescriptions &, ModuleDescription const &)
 
void fillDescriptions (ModuleDescription const &)
 
fhicl::ParameterSetID selectorConfig () const
 
- Public Member Functions inherited from art::ModuleBase
virtual ~ModuleBase () noexcept
 
 ModuleBase ()
 
ModuleDescription const & moduleDescription () const
 
void setModuleDescription (ModuleDescription const &)
 
std::array< std::vector< ProductInfo >, NumBranchTypes > const & getConsumables () const
 
void sortConsumables (std::string const &current_process_name)
 
template<typename T , BranchType BT>
ViewToken< T > consumesView (InputTag const &tag)
 
template<typename T , BranchType BT>
ViewToken< T > mayConsumeView (InputTag const &tag)
 

Private Attributes

std::string fOutputDir
 
std::string fGraphLabel
 
unsigned int fTopologyHitsCut
 
std::string fGenieGenModuleLabel
 
std::string fEnergyNueLabel
 
std::string fEnergyNumuLabel
 
std::string fEnergyNutauLabel
 
std::string out_dir
 

Additional Inherited Members

- Public Types inherited from art::EDAnalyzer
using WorkerType = WorkerT< EDAnalyzer >
 
using ModuleType = EDAnalyzer
 
- Protected Member Functions inherited from art::Observer
std::string const & processName () const
 
bool wantAllEvents () const noexcept
 
bool wantEvent (ScheduleID id, Event const &e) const
 
Handle< TriggerResultsgetTriggerResults (Event const &e) const
 
 Observer (fhicl::ParameterSet const &config)
 
 Observer (std::vector< std::string > const &select_paths, std::vector< std::string > const &reject_paths, fhicl::ParameterSet const &config)
 
- Protected Member Functions inherited from art::ModuleBase
ConsumesCollectorconsumesCollector ()
 
template<typename T , BranchType = InEvent>
ProductToken< T > consumes (InputTag const &)
 
template<typename Element , BranchType = InEvent>
ViewToken< Element > consumesView (InputTag const &)
 
template<typename T , BranchType = InEvent>
void consumesMany ()
 
template<typename T , BranchType = InEvent>
ProductToken< T > mayConsume (InputTag const &)
 
template<typename Element , BranchType = InEvent>
ViewToken< Element > mayConsumeView (InputTag const &)
 
template<typename T , BranchType = InEvent>
void mayConsumeMany ()
 

Detailed Description

Definition at line 37 of file GCNZlibMaker_module.cc.

Constructor & Destructor Documentation

cvn::GCNZlibMaker::GCNZlibMaker ( fhicl::ParameterSet const &  pset)
explicit

Definition at line 63 of file GCNZlibMaker_module.cc.

64  : EDAnalyzer(pset)
65  {
66  this->reconfigure(pset);
67  }
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
void reconfigure(const fhicl::ParameterSet &pset)
cvn::GCNZlibMaker::~GCNZlibMaker ( )

Definition at line 70 of file GCNZlibMaker_module.cc.

71  { }

Member Function Documentation

void cvn::GCNZlibMaker::analyze ( const art::Event evt)
override

Definition at line 104 of file GCNZlibMaker_module.cc.

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 
130  AssignLabels labels;
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  }
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
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
enum cvn::Interaction InteractionType
std::string fGenieGenModuleLabel
const simb::MCParticle & Lepton() const
Definition: MCNeutrino.h:147
unsigned long ulong
Definition: qglobal.h:352
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
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
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
Event generator information.
Definition: MCNeutrino.h:18
unsigned int fTopologyHitsCut
QTextStream & endl(QTextStream &s)
void cvn::GCNZlibMaker::beginJob ( )
overridevirtual

Reimplemented from art::EDAnalyzer.

Definition at line 87 of file GCNZlibMaker_module.cc.

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  }
bool exists(std::string path)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
QTextStream & endl(QTextStream &s)
void cvn::GCNZlibMaker::reconfigure ( const fhicl::ParameterSet pset)

Definition at line 74 of file GCNZlibMaker_module.cc.

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  }
std::string string
Definition: nybbler.cc:12
std::string fGenieGenModuleLabel
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::string fEnergyNutauLabel
std::string fEnergyNumuLabel
unsigned int fTopologyHitsCut

Member Data Documentation

std::string cvn::GCNZlibMaker::fEnergyNueLabel
private

Definition at line 54 of file GCNZlibMaker_module.cc.

std::string cvn::GCNZlibMaker::fEnergyNumuLabel
private

Definition at line 55 of file GCNZlibMaker_module.cc.

std::string cvn::GCNZlibMaker::fEnergyNutauLabel
private

Definition at line 56 of file GCNZlibMaker_module.cc.

std::string cvn::GCNZlibMaker::fGenieGenModuleLabel
private

Definition at line 53 of file GCNZlibMaker_module.cc.

std::string cvn::GCNZlibMaker::fGraphLabel
private

Definition at line 50 of file GCNZlibMaker_module.cc.

std::string cvn::GCNZlibMaker::fOutputDir
private

Definition at line 49 of file GCNZlibMaker_module.cc.

unsigned int cvn::GCNZlibMaker::fTopologyHitsCut
private

Definition at line 51 of file GCNZlibMaker_module.cc.

std::string cvn::GCNZlibMaker::out_dir
private

Definition at line 58 of file GCNZlibMaker_module.cc.


The documentation for this class was generated from the following file: