Classes | Macros | Enumerations | Functions
cvnCreateDB.cc File Reference
#include <iostream>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "boost/program_options/options_description.hpp"
#include "boost/program_options/variables_map.hpp"
#include "boost/program_options/parsers.hpp"
#include "boost/algorithm/string/predicate.hpp"
#include "cetlib/filepath_maker.h"
#include "fhiclcpp/intermediate_table.h"
#include "fhiclcpp/make_ParameterSet.h"
#include "fhiclcpp/ParameterSet.h"
#include "TSystem.h"
#include "TFile.h"
#include "TTree.h"
#include "TChain.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "dunereco/CVN/func/CVNImageUtils.h"
#include <leveldb/db.h>
#include <leveldb/write_batch.h>
#include <lmdb.h>
#include "caffe/caffe.hpp"
#include "H5Cpp.h"

Go to the source code of this file.

Classes

class  Config
 
class  OutputDB
 

Macros

#define CPU_ONLY
 

Enumerations

enum  LabelingMode {
  kAll, kNumu, kNue, kNC,
  kEnergy
}
 

Functions

void fill (const Config &config, std::string input)
 
po::variables_map getOptions (int argc, char *argv[], std::string &config, std::string &input)
 
fhicl::ParameterSet getPSet (std::string configPath)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define CPU_ONLY

Definition at line 36 of file cvnCreateDB.cc.

Enumeration Type Documentation

Enumerator
kAll 

Label all interaction types separately.

kNumu 

Label numu:1, else 0.

kNue 

Label nue:1, else 0.

kNC 

Label NC:1, else 0.

kEnergy 

Label is conversion of fNuEnergy to int.

Definition at line 50 of file cvnCreateDB.cc.

51 {
52  kAll, ///< Label all interaction types separately
53  kNumu, ///< Label numu:1, else 0
54  kNue, ///< Label nue:1, else 0
55  kNC, ///< Label NC:1, else 0
56  kEnergy ///< Label is conversion of fNuEnergy to int
57 };
Label is conversion of fNuEnergy to int.
Definition: cvnCreateDB.cc:56
Label all interaction types separately.
Definition: cvnCreateDB.cc:52
Label NC:1, else 0.
Definition: cvnCreateDB.cc:55
Label numu:1, else 0.
Definition: cvnCreateDB.cc:53
Label nue:1, else 0.
Definition: cvnCreateDB.cc:54

Function Documentation

void fill ( const Config config,
std::string  input 
)

Definition at line 211 of file cvnCreateDB.cc.

212 {
213 
214  TChain chain(config.fTreeName.c_str());
215 
216  if (boost::ends_with(input,".list")) {
217  std::ifstream list_file(input.c_str());
218  if (!list_file.is_open()) {
219  std::cout << "Could not open " << input << std::endl;
220  exit(1);
221  }
222 
223  std::string ifname;
224  while (list_file>>ifname)
225  chain.Add(ifname.c_str());
226 
227  }//end if list file
228 
229  else if (boost::ends_with(input,".root")) {
230  chain.Add(input.c_str());
231  }//end if root file
232 
233  chain.SetMakeClass(1);
234 
235  int fInt;
236  UInt_t fPMap_fNWire;
237  UInt_t fPMap_fNTdc;
238  std::vector<float> fPMap_fPEX;
239  std::vector<float> fPMap_fPEY;
240  std::vector<float> fPMap_fPEZ;
241 
242  chain.SetBranchAddress("fInt", &fInt);
243  chain.SetBranchAddress("fPMap.fNWire", &fPMap_fNWire);
244  chain.SetBranchAddress("fPMap.fNTdc", &fPMap_fNTdc);
245  chain.SetBranchAddress("fPMap.fPEX", &fPMap_fPEX);
246  chain.SetBranchAddress("fPMap.fPEY", &fPMap_fPEY);
247  chain.SetBranchAddress("fPMap.fPEZ", &fPMap_fPEZ);
248 
249  unsigned int entries = chain.GetEntries();
250  if(config.fNEvents < entries){
251  entries = config.fNEvents;
252  }
253  if(entries <= 0){
254  std::cout << "Error: Input tree has no entries." << std::endl;
255  exit(4);
256  }
257 
258  std::cout << "- Will process " << entries << " from the input tree." << std::endl;
259 
260  OutputDB TrainDB("train",config);
261  OutputDB TestDB( "test",config);
262 
263  char* key = new char[config.fMaxKeyLength];
264  std::string serializeString;
265 
266  //need to shuffle entries...
267  std::srand ( unsigned ( std::time(0) ) );
268  std::vector<unsigned int> shuffled;
269  for (unsigned int i = 0; i < entries; ++i)
270  {
271  shuffled.push_back(i);
272  }
273 
274  std::random_shuffle( shuffled.begin(), shuffled.end() );
275 
276  // Figure out the size of the train and test samples
277  // Call a 'block' a particular set of one test and nTrainPerTest train
278  unsigned int blockSize = config.fNTrainPerTest + 1;
279  // number of test is the number of blocks, using integer division
280  unsigned int nTest = 1 + entries / blockSize;
281  // number of training samples is number of blocks times train per test
282  unsigned int nTrain = entries / blockSize * config.fNTrainPerTest;
283  // Add on the entries from the last, potentially partial block, minus test
284  if (entries % blockSize) nTrain += entries % blockSize - 1;
285 
286  // Create an array to hold regression features.
287  const unsigned int nRegressionFeatures = 2; // Currently nuEnergy, lepEnergy
288 // float regressionDataTest[nTest][nRegressionFeatures];
289 // float regressionDataTrain[nTrain][nRegressionFeatures];
290 
291  int** regressionDataTest = new int*[nTest];
292  for(unsigned int i = 0; i < nTest; ++i) {regressionDataTest[i] = new int[nRegressionFeatures];}
293  int** regressionDataTrain = new int*[nTrain];
294  for(unsigned int i = 0; i < nTrain; ++i) {regressionDataTrain[i] = new int[nRegressionFeatures];}
295 
296  int iTrain = 0;
297  int iTest = 0;
298 
299  ////hdf5////
300 
301  const char saveFilePath[] = "test.h5";
302  const hsize_t ndims = 2;
303  const hsize_t ncols = 3;
304 
305  hid_t file = H5Fcreate(saveFilePath, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
306  std::cout << "- File created" << std::endl;
307 
308  hsize_t dims[ndims] = {0, ncols};
309  hsize_t max_dims[ndims] = {H5S_UNLIMITED, ncols};
310  hid_t file_space = H5Screate_simple(ndims, dims, max_dims);
311  std::cout << "- Dataspace created" << std::endl;
312 
313  hid_t plist = H5Pcreate(H5P_DATASET_CREATE);
314  H5Pset_layout(plist, H5D_CHUNKED);
315  hsize_t chunk_dims[ndims] = {2, ncols};
316  H5Pset_chunk(plist, ndims, chunk_dims);
317  std::cout << "- Property list created" << std::endl;
318 
319  //hid_t dset = H5Dcreate(file, "dset1", H5T_NATIVE_FLOAT, file_space, H5P_DEFAULT, plist, H5P_DEFAULT);
320  H5Dcreate(file, "dset1", H5T_NATIVE_FLOAT, file_space, H5P_DEFAULT, plist, H5P_DEFAULT);
321  std::cout << "- Dataset 'dset1' created" << std::endl;
322 
323  H5Pclose(plist);
324  H5Sclose(file_space);
325 
326  if(entries > chain.GetEntries()){
327  entries = chain.GetEntries();
328  }
329 
330  for(unsigned int iEntry = 0; iEntry < entries; ++iEntry)
331  {
332  unsigned int entry = shuffled[iEntry];
333  chain.GetEntry(entry);
334 
335  unsigned int nViews = 3;
336 
337  // Create a CVNImageUtils object and use it to produce the pixels. The arguments
338  // define how large we want the output image to be
339  cvn::CVNImageUtils imageUtils(config.fPlaneLimit,config.fTDCLimit,nViews);
340  // Since we don't have a PixelMap object, we need to tell it how big it is
341  imageUtils.SetPixelMapSize(fPMap_fNWire,fPMap_fNTdc);
342 
343  std::vector<unsigned char> pixelArray(nViews * config.fPlaneLimit * config.fTDCLimit,0);
344 
345  imageUtils.SetLogScale(config.fSetLog);
346  imageUtils.SetViewReversal(config.fReverseViews);
347  imageUtils.ConvertChargeVectorsToPixelArray(fPMap_fPEX, fPMap_fPEY, fPMap_fPEZ, pixelArray);
348 
349  caffe::Datum datum;
350  datum.set_height(config.fPlaneLimit);
351  datum.set_width(config.fTDCLimit);
352 
353  datum.set_data(pixelArray.data(), nViews * config.fPlaneLimit * config.fTDCLimit);
354 
355  datum.set_label(fInt);
356 
357  datum.SerializeToString(&serializeString);
358 
359  if(iEntry % (blockSize))
360  {
361  snprintf(key, config.fMaxKeyLength, "%08lld", (long long int)iTrain);
362  std::string serializeKey(key);
363 
364  TrainDB.Put(serializeKey,serializeString);
365 
366  regressionDataTrain[iTrain][0] = 1.;
367  regressionDataTrain[iTrain][1] = 1.;
368  iTrain += 1;
369 
370  ////hdf5////
371  hsize_t nlines = 1;
372  float *buffer = new float[nlines * ncols];
373  float **b = new float*[nlines];
374  for (hsize_t i = 0; i < nlines; ++i){
375  b[i] = &buffer[i * ncols];
376  }
377 
378  b[0][0] = 0.1;
379  b[0][1] = 0.2;
380  b[0][2] = 0.3;
381 
382  }
383  else
384  {
385  snprintf(key, config.fMaxKeyLength, "%08lld", (long long int)iTest);
386  std::string serializeKey(key);
387 
388  TestDB.Put(serializeKey,serializeString);
389 
390  regressionDataTest[iTest][0] = 1.;
391  regressionDataTest[iTest][1] = 1.;
392  iTest += 1;
393  }
394  if(not (iEntry % config.fProgressInterval))
395  std::cout << "Fraction complete: "
396  << iEntry / (float)entries << std::endl;
397 
398  }
399 
400  if (config.fWriteRegressionHDF5)
401  {
402 
403  H5::FloatType type(H5::PredType::IEEE_F32LE);
404  std::cout << "Writing HDF5 regression output : "
405  << config.fRegressionHDF5NameTest << std::endl;
406  H5::H5File h5FileTest(config.fRegressionHDF5NameTest, H5F_ACC_TRUNC );
407  hsize_t shape[2];
408  shape[0] = nTest;
409  shape[1] = nRegressionFeatures;
410  H5::DataSpace spaceTest(2, shape);
411 
412  H5::DataSet datasetTest = h5FileTest.createDataSet("regression",
413  type,
414  spaceTest);
415 
416  datasetTest.write(regressionDataTest, type);
417 
418  std::cout << "Writing HDF5 regression output : "
419  << config.fRegressionHDF5NameTrain << std::endl;
420  H5::H5File h5FileTrain(config.fRegressionHDF5NameTrain, H5F_ACC_TRUNC );
421  shape[0] = nTrain;
422  H5::DataSpace spaceTrain(2, shape);
423 
424  H5::DataSet datasetTrain = h5FileTrain.createDataSet("regression",
425  type,
426  spaceTrain);
427 
428  datasetTrain.write(regressionDataTrain, type);
429 
430  }
431 
432  // Clear up
433  delete key;
434  for(unsigned int i = 0; i < nTest; ++i) {
435  delete [] regressionDataTest[i];
436  }
437  delete [] regressionDataTest;
438  for(unsigned int i = 0; i < nTrain; ++i) {
439  delete [] regressionDataTrain[i];
440  }
441  delete [] regressionDataTrain;
442 
443 }
QList< Entry > entry
std::string string
Definition: nybbler.cc:12
static int input(void)
Definition: code.cpp:15695
def key(type, name=None)
Definition: graph.py:13
static Config * config
Definition: config.cpp:1054
Class containing some utility functions for all things CVN.
Definition: CVNImageUtils.h:24
static QCString type
Definition: declinfo.cpp:672
static bool * b
Definition: config.cpp:1043
QTextStream & endl(QTextStream &s)
po::variables_map getOptions ( int  argc,
char *  argv[],
std::string config,
std::string input 
)

Definition at line 447 of file cvnCreateDB.cc.

449 {
450 
451  // Declare the supported options.
452  po::options_description desc("Allowed options");
453  desc.add_options()
454  ("help", "produce help message")
455  ("config,c", po::value<std::string>(&config)->required(),
456  "configuration file")
457  ("input,i", po::value<std::string>(&input)->required(),
458  "Input data in ROOT file.");
459  po::variables_map vm;
460 
461  try
462  {
463  po::store(po::parse_command_line(argc, argv, desc), vm);
464  po::notify(vm);
465 
466  }
467  catch(po::error& e)
468  {
469  std::cout << "ERROR: " << e.what() << std::endl;
470  exit(1);
471  }
472 
473 
474  if (vm.count("help")) {
475  std::cout << desc << "\n";
476  exit(1);
477  }
478 
479  return vm;
480 }
error
Definition: include.cc:26
const double e
static int input(void)
Definition: code.cpp:15695
static Config * config
Definition: config.cpp:1054
QTextStream & endl(QTextStream &s)
fhicl::ParameterSet getPSet ( std::string  configPath)

Definition at line 484 of file cvnCreateDB.cc.

485 {
486 
487  cet::filepath_first_absolute_or_lookup_with_dot policy("FHICL_FILE_PATH");
488 
489  // parse a configuration file; obtain intermediate form
491  fhicl::parse_document(configPath, policy, tbl);
492 
493  // convert to ParameterSet
494  fhicl::ParameterSet pset;
495  fhicl::make_ParameterSet(tbl, pset);
496 
497  return pset;
498 
499 }
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720
int main ( int  argc,
char *  argv[] 
)

Definition at line 501 of file cvnCreateDB.cc.

502 {
503 
504  std::string configPath, inputPath, outputPath, logPath;
505  po::variables_map vm = getOptions(argc, argv, configPath, inputPath);
506 
507  Config config(getPSet(configPath));
508 
509 
510  fill(config, inputPath);
511 
512  return 0;
513 
514 }
fhicl::ParameterSet getPSet(std::string configPath)
Definition: cvnCreateDB.cc:484
void fill(const Config &config, std::string input)
Definition: cvnCreateDB.cc:211
std::string string
Definition: nybbler.cc:12
typename config_impl< T >::type Config
Definition: ModuleMacros.h:52
static Config * config
Definition: config.cpp:1054
po::variables_map getOptions(int argc, char *argv[], std::string &config, std::string &input)
Definition: cvnCreateDB.cc:447