CVNSparseROOT_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CVNSparseROOT
3 // Plugin Type: analyzer (art v3_01_02)
4 // File: CVNSparseROOT_module.cc
5 //
6 // Generated at Wed Apr 10 14:53:36 2019 by Jeremy Hewes using cetskelgen
7 // from cetlib version v3_05_01.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
19 
20 // CVN includes
22 
23 // ROOT includes
24 #include "TFile.h"
25 #include "TTree.h"
26 
27 // Boost includes
28 #include <boost/uuid/uuid.hpp> // uuid class
29 #include <boost/uuid/uuid_generators.hpp> // generators
30 #include <boost/uuid/uuid_io.hpp> // streaming
31 
32 namespace cvn {
33 
34  class CVNSparseROOT : public art::EDAnalyzer {
35  public:
36  explicit CVNSparseROOT(fhicl::ParameterSet const& p);
37 
38  CVNSparseROOT(CVNSparseROOT const&) = delete;
39  CVNSparseROOT(CVNSparseROOT&&) = delete;
40  CVNSparseROOT& operator=(CVNSparseROOT const&) = delete;
42 
43  void reconfigure(fhicl::ParameterSet const& p);
44 
45  void beginSubRun(art::SubRun const& sr) override;
46  void endSubRun(art::SubRun const& sr) override;
47  void analyze(art::Event const& e) override;
48 
49  private:
50 
51  std::string fMapModuleLabel; ///< Name of map producer module
52  std::string fMapInstanceLabel; ///< Name of sparse map instance
53  std::string fOutputName; ///< ROOT output filename
54  std::string fTreeName; ///< ROOt tree name
55  bool fIncludeGroundTruth; ///< Whether to include per-pixel ground truth
56 
57  std::vector<std::vector<float>> fCoordinates; ///< Pixel coordinates
58  std::vector<std::vector<float>> fFeatures; ///< Pixel features
59  std::vector<std::vector<int>> fPixelPDG; ///< Pixel PDG truth
60  std::vector<std::vector<int>> fPixelTrackID; ///< Pixel track ID
61  std::vector<std::vector<float>> fPixelEnergies; ///< Pixel energy
62  std::vector<std::vector<std::string>> fProcesses; // Physical process that creates the particle
63 
64  std::vector<unsigned int> fEvent; ///< Event numbers
65  unsigned int fView; ///< View numbers
66 
67  TFile* fFile; ///< Output ROOT file
68  TTree* fTree; ///< ROOT tree for writing to file
69  size_t fCacheSize; ///< Size of TTree cache
70 
71  };
72 
73 
75  : EDAnalyzer{p} {
76  this->reconfigure(p);
77  }
78 
80 
81  fMapModuleLabel = p.get<std::string>("MapModuleLabel");
82  fMapInstanceLabel = p.get<std::string>("MapInstanceLabel");
83  fOutputName = p.get<std::string>("OutputName");
84  fTreeName = p.get<std::string>("TreeName");
85  fIncludeGroundTruth = p.get<bool>("IncludeGroundTruth");
86  fCacheSize = p.get<size_t>("CacheSize");
87 
88  } // cvn::CVNSparseROOT::reconfigure
89 
91 
92  // Get the sparse maps
93  std::vector<art::Ptr<SparsePixelMap>> maps;
95  auto hMaps = e.getHandle<std::vector<SparsePixelMap>>(itag1);
96  if (!hMaps) {
98  << "Could not find SparsePixelMap vector with module label "
99  << fMapModuleLabel << " and instance label "
100  << fMapInstanceLabel << "!" << std::endl;
101  }
102  art::fill_ptr_vector(maps, hMaps);
103 
104  if (maps.size() > 1) throw art::Exception(art::errors::LogicError)
105  << "There shouldn't be more than one SparsePixelMap per producer per event,"
106  << " but here there are " << maps.size() << "." << std::endl;
107 
108  if (maps.empty()) return;
109 
110  for (unsigned int it = 0; it < maps[0]->GetViews(); ++it) {
111  fCoordinates = maps[0]->GetCoordinates(it);
112  fFeatures = maps[0]->GetFeatures(it);
113  if (fIncludeGroundTruth) {
114  fPixelPDG = maps[0]->GetPixelPDGs(it);
115  fPixelTrackID = maps[0]->GetPixelTrackIDs(it);
116  fPixelEnergies = maps[0]->GetPixelEnergies(it);
117  fProcesses = maps[0]->GetProcesses(it);
118  }
119  fEvent = std::vector<unsigned int>({e.id().run(), e.id().subRun(), e.id().event()});
120  fView = it;
121  fTree->Fill();
122  }
123 
124  } // cvn::CVNSparseROOT::analyze
125 
126  /// Beginning of a subrun, make a new file
128 
129  // Open ROOT file
130  boost::uuids::random_generator generator;
131  boost::uuids::uuid uuid = generator();
132  std::ostringstream fileName;
133  fileName << fOutputName << "_" << uuid << ".root";
134  fFile = TFile::Open(fileName.str().c_str(), "recreate");
135 
136  fTree = new TTree(fTreeName.c_str(), fTreeName.c_str());
137  fTree->SetCacheSize(fCacheSize);
138  fTree->Branch("Coordinates", &fCoordinates);
139  fTree->Branch("Features", &fFeatures);
140  if (fIncludeGroundTruth) {
141  fTree->Branch("PixelPDG", &fPixelPDG);
142  fTree->Branch("PixelTrackID", &fPixelTrackID);
143  fTree->Branch("PixelEnergy", &fPixelEnergies);
144  fTree->Branch("Process", &fProcesses);
145  }
146  fTree->Branch("Event", &fEvent);
147  fTree->Branch("View", &fView);
148 
149  } // function CVNSparseROOT::beginSubRun
150 
151  /// End of a subrun, write all events to a ROOT file
153 
154  fFile->WriteTObject(fTree, fTreeName.c_str());
155  delete fFile;
156 
157  } // cvn::CVNSparseROOT::endSubRun
158 
160 
161 } // namespace cvn
162 
unsigned int fView
View numbers.
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
CVNSparseROOT & operator=(CVNSparseROOT const &)=delete
std::string string
Definition: nybbler.cc:12
std::string fMapInstanceLabel
Name of sparse map instance.
std::vector< std::vector< std::string > > fProcesses
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Utility class for truth labels.
RunNumber_t run() const
Definition: EventID.h:98
std::vector< std::vector< int > > fPixelPDG
Pixel PDG truth.
void beginSubRun(art::SubRun const &sr) override
Beginning of a subrun, make a new file.
const double e
bool fIncludeGroundTruth
Whether to include per-pixel ground truth.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
fileName
Definition: dumpTree.py:9
TFile * fFile
Output ROOT file.
T get(std::string const &key) const
Definition: ParameterSet.h:271
CVNSparseROOT(fhicl::ParameterSet const &p)
std::vector< std::vector< int > > fPixelTrackID
Pixel track ID.
p
Definition: test.py:223
std::vector< std::vector< float > > fCoordinates
Pixel coordinates.
generator
Definition: train.py:468
void analyze(art::Event const &e) override
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< std::vector< float > > fPixelEnergies
Pixel energy.
std::string fMapModuleLabel
Name of map producer module.
std::string fOutputName
ROOT output filename.
std::vector< unsigned int > fEvent
Event numbers.
EventNumber_t event() const
Definition: EventID.h:116
size_t fCacheSize
Size of TTree cache.
Sparse pixel map for CVN.
std::string fTreeName
ROOt tree name.
void reconfigure(fhicl::ParameterSet const &p)
void endSubRun(art::SubRun const &sr) override
End of a subrun, write all events to a ROOT file.
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
static constexpr double sr
Definition: Units.h:166
TTree * fTree
ROOT tree for writing to file.
SubRunNumber_t subRun() const
Definition: EventID.h:110
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)
std::vector< std::vector< float > > fFeatures
Pixel features.