Public Member Functions | Private Member Functions | Private Attributes | List of all members
cnn::RegCNNEvaluator Class Reference
Inheritance diagram for cnn::RegCNNEvaluator:
art::EDProducer art::detail::Producer art::detail::LegacyModule art::Modifier art::ModuleBase art::ProductRegistryHelper

Public Member Functions

 RegCNNEvaluator (fhicl::ParameterSet const &pset)
 
 ~RegCNNEvaluator ()
 
void produce (art::Event &evt)
 
void beginJob ()
 
void endJob ()
 
- Public Member Functions inherited from art::EDProducer
 EDProducer (fhicl::ParameterSet const &pset)
 
template<typename Config >
 EDProducer (Table< Config > const &config)
 
std::string workerType () const
 
- Public Member Functions inherited from art::detail::Producer
virtual ~Producer () noexcept
 
 Producer (fhicl::ParameterSet const &)
 
 Producer (Producer const &)=delete
 
 Producer (Producer &&)=delete
 
Produceroperator= (Producer const &)=delete
 
Produceroperator= (Producer &&)=delete
 
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::Modifier
 ~Modifier () noexcept
 
 Modifier ()
 
 Modifier (Modifier const &)=delete
 
 Modifier (Modifier &&)=delete
 
Modifieroperator= (Modifier const &)=delete
 
Modifieroperator= (Modifier &&)=delete
 
- 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 Member Functions

void PrepareEvent (const art::Event &event)
 
bool insideContVol (const double posX, const double posY, const double posZ)
 
void getCM (const RegPixelMap &pm, std::vector< float > &cm_list)
 

Private Attributes

art::ServiceHandle< geo::GeometryfGeom
 
std::string fPixelMapInput
 
std::string fResultLabel
 
std::string fCNNType
 
std::string fTarget
 
cnn::TFRegNetHandler fTFHandler
 
cnn::RegCNNVtxHandler fRegCNNVtxHandler
 
cnn::RegCNNNumuHandler fRegCNNNumuHandler
 
std::string fHitsModuleLabel
 
std::string fTrackModuleLabel
 
double fContVolCut
 
bool fLongestTrackContained
 

Additional Inherited Members

- Public Types inherited from art::EDProducer
using ModuleType = EDProducer
 
using WorkerType = WorkerT< EDProducer >
 
- Public Types inherited from art::detail::Producer
template<typename UserConfig , typename KeysToIgnore = void>
using Table = Modifier::Table< UserConfig, KeysToIgnore >
 
- Public Types inherited from art::Modifier
template<typename UserConfig , typename UserKeysToIgnore = void>
using Table = ProducerTable< UserConfig, detail::ModuleConfig, UserKeysToIgnore >
 
- Static Public Member Functions inherited from art::EDProducer
static void commitEvent (EventPrincipal &ep, Event &e)
 
- 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 56 of file RegCNNEvaluator_module.cc.

Constructor & Destructor Documentation

cnn::RegCNNEvaluator::RegCNNEvaluator ( fhicl::ParameterSet const &  pset)
explicit

Definition at line 96 of file RegCNNEvaluator_module.cc.

96  :
97  EDProducer(pset),
98  fPixelMapInput (pset.get<std::string> ("PixelMapInput")),
99  fResultLabel (pset.get<std::string> ("ResultLabel")),
100  fCNNType (pset.get<std::string> ("CNNType")),
101  fTarget (pset.get<std::string> ("Target")),
102  fTFHandler (pset.get<fhicl::ParameterSet> ("TFNetHandler")),
103  fRegCNNVtxHandler (pset.get<fhicl::ParameterSet> ("RegCNNVtxHandler")),
104  fRegCNNNumuHandler (pset.get<fhicl::ParameterSet> ("RegCNNNumuHandler")),
105  fHitsModuleLabel (pset.get<std::string> ("HitsModuleLabel")),
106  fTrackModuleLabel (pset.get<std::string> ("TrackModuleLabel")),
107  fContVolCut (pset.get<double> ("ContVolCut"))
108  {
109  produces< std::vector<cnn::RegCNNResult> >(fResultLabel);
110  }
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
cnn::RegCNNNumuHandler fRegCNNNumuHandler
cnn::RegCNNVtxHandler fRegCNNVtxHandler
cnn::TFRegNetHandler fTFHandler
cnn::RegCNNEvaluator::~RegCNNEvaluator ( )

Definition at line 113 of file RegCNNEvaluator_module.cc.

114  {
115  //======================================================================
116  // Clean up any memory allocated by your module
117  //======================================================================
118  }

Member Function Documentation

void cnn::RegCNNEvaluator::beginJob ( )
virtual

Reimplemented from art::EDProducer.

Definition at line 121 of file RegCNNEvaluator_module.cc.

122  {
123  }
void cnn::RegCNNEvaluator::endJob ( )
virtual

Reimplemented from art::EDProducer.

Definition at line 126 of file RegCNNEvaluator_module.cc.

127  {
128  }
void cnn::RegCNNEvaluator::getCM ( const RegPixelMap pm,
std::vector< float > &  cm_list 
)
private

Definition at line 131 of file RegCNNEvaluator_module.cc.

132  {
133  //std::cout << pm.fBound.fFirstWire[0]+pm.fNWire/2 << std::endl;
134  //std::cout << pm.fBound.fFirstTDC[0]+pm.fNTdc*pm.fNTRes/2 << std::endl;
135  for (int ii = 0; ii < 3; ii++){
136  float mean_wire = pm.fBound.fFirstWire[ii]+pm.fNWire*pm.fNWRes/2;
137  float mean_tdc = pm.fBound.fFirstTDC[ii]+pm.fNTdc*pm.fNTRes/2;
138  cm_list[2*ii] = mean_tdc;
139  cm_list[2*ii+1] = mean_wire;
140  }
141  }
bool cnn::RegCNNEvaluator::insideContVol ( const double  posX,
const double  posY,
const double  posZ 
)
private

Definition at line 253 of file RegCNNEvaluator_module.cc.

253  {
254  double vtx[3] = {posX, posY, posZ};
255  bool inside = false;
256 
257  geo::TPCID idtpc = fGeom->FindTPCAtPosition(vtx);
258 
259  if (fGeom->HasTPC(idtpc)) {
260  const geo::TPCGeo& tpcgeo = fGeom->GetElement(idtpc);
261  double minx = tpcgeo.MinX(); double maxx = tpcgeo.MaxX();
262  double miny = tpcgeo.MinY(); double maxy = tpcgeo.MaxY();
263  double minz = tpcgeo.MinZ(); double maxz = tpcgeo.MaxZ();
264 
265  for (size_t c = 0; c < fGeom->Ncryostats(); c++) {
266  const geo::CryostatGeo& cryostat = fGeom->Cryostat(c);
267  for (size_t t = 0; t < cryostat.NTPC(); t++) {
268  const geo::TPCGeo& tpcg = cryostat.TPC(t);
269  if (tpcg.MinX() < minx) minx = tpcg.MinX();
270  if (tpcg.MaxX() > maxx) maxx = tpcg.MaxX();
271  if (tpcg.MinY() < miny) miny = tpcg.MinY();
272  if (tpcg.MaxY() > maxy) maxy = tpcg.MaxY();
273  if (tpcg.MinZ() < minz) minz = tpcg.MinZ();
274  if (tpcg.MaxZ() > maxz) maxz = tpcg.MaxZ();
275  }
276  }
277 
278  //x
279  double dista = fabs(minx - posX);
280  double distb = fabs(posX - maxx);
281  if ((posX > minx) && (posX < maxx) &&
282  (dista > fContVolCut) && (distb > fContVolCut)) inside = true;
283  //y
284  dista = fabs(maxy - posY);
285  distb = fabs(posY - miny);
286  if (inside && (posY > miny) && (posY < maxy) &&
287  (dista > fContVolCut) && (distb > fContVolCut)) inside = true;
288  else inside = false;
289  //z
290  dista = fabs(maxz - posZ);
291  distb = fabs(posZ - minz);
292  if (inside && (posZ > minz) && (posZ < maxz) &&
293  (dista > fContVolCut) && (distb > fContVolCut)) inside = true;
294  else inside = false;
295  }
296 
297  return inside;
298  }
CryostatGeo const & GetElement(geo::CryostatID const &cryoid) const
double MinX() const
Returns the world x coordinate of the start of the box.
Definition: BoxBoundedGeo.h:88
Geometry information for a single TPC.
Definition: TPCGeo.h:38
double MaxX() const
Returns the world x coordinate of the end of the box.
Definition: BoxBoundedGeo.h:91
Geometry information for a single cryostat.
Definition: CryostatGeo.h:43
unsigned int Ncryostats() const
Returns the number of cryostats in the detector.
geo::TPCID FindTPCAtPosition(double const worldLoc[3]) const
Returns the ID of the TPC at specified location.
double MinZ() const
Returns the world z coordinate of the start of the box.
unsigned int NTPC() const
Number of TPCs in this cryostat.
Definition: CryostatGeo.h:181
CryostatGeo const & Cryostat(geo::CryostatID const &cryoid) const
Returns the specified cryostat.
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
art::ServiceHandle< geo::Geometry > fGeom
double MaxY() const
Returns the world y coordinate of the end of the box.
bool HasTPC(geo::TPCID const &tpcid) const
Returns whether we have the specified TPC.
const TPCGeo & TPC(unsigned int itpc) const
Return the itpc&#39;th TPC in the cryostat.
Definition: CryostatGeo.cxx:93
double MaxZ() const
Returns the world z coordinate of the end of the box.
double MinY() const
Returns the world y coordinate of the start of the box.
void cnn::RegCNNEvaluator::PrepareEvent ( const art::Event event)
private

Definition at line 210 of file RegCNNEvaluator_module.cc.

210  {
211  // Hits
212  auto hitListHandle = evt.getValidHandle<std::vector<recob::Hit>>(fHitsModuleLabel);
213 
214  // Tracks
215  std::vector<art::Ptr<recob::Track> > tracklist;
216  auto trackListHandle = evt.getHandle< std::vector<recob::Track> >(fTrackModuleLabel);
217  if (trackListHandle)
218  art::fill_ptr_vector(tracklist, trackListHandle);
219 
220  // Associations
221  art::FindManyP<recob::Hit> fmth(trackListHandle, evt, fTrackModuleLabel);
222  art::FindManyP<recob::SpacePoint> fmhs(hitListHandle, evt, fTrackModuleLabel);
223 
224  int ntracks = tracklist.size();
225 
226  double fMaxTrackLength = -1.0;
227  int iLongestTrack = -1;
228  // loop over tracks to find the longest track
229  for (int i = 0; i < ntracks; ++i){
230  if(tracklist[i]->Length() > fMaxTrackLength){
231  fMaxTrackLength = tracklist[i]->Length();
232  iLongestTrack = i;
233  }
234  }
235 
236  fLongestTrackContained = true;
237  if (iLongestTrack >= 0 && iLongestTrack <= ntracks-1) {
238  if (fmth.isValid()) {
239  std::vector< art::Ptr<recob::Hit> > vhit = fmth.at(iLongestTrack);
240  for (size_t h = 0; h < vhit.size(); ++h) {
241  if (vhit[h]->WireID().Plane == 2) {
242  std::vector< art::Ptr<recob::SpacePoint> > spts = fmhs.at(vhit[h].key());
243  if (spts.size()) {
244  if (!insideContVol(spts[0]->XYZ()[0], spts[0]->XYZ()[1], spts[0]->XYZ()[2]))
245  fLongestTrackContained = false;
246  }
247  }
248  }
249  }
250  } // End of search longestTrack
251  }
float Length(const PFPStruct &pfp)
Definition: PFPUtils.cxx:3303
int ntracks
Definition: tracks.py:246
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
def key(type, name=None)
Definition: graph.py:13
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
bool insideContVol(const double posX, const double posY, const double posZ)
void cnn::RegCNNEvaluator::produce ( art::Event evt)
virtual

Define containers for the things we're going to produce

Load in the pixel maps

Load 3D pixel map for direction reco.

Make sure we have a valid name for the CNN type

Implements art::EDProducer.

Definition at line 144 of file RegCNNEvaluator_module.cc.

145  {
146 
147  this->PrepareEvent(evt);
148 
149  /// Define containers for the things we're going to produce
150  std::unique_ptr< std::vector<RegCNNResult> >
151  resultCol(new std::vector<RegCNNResult>);
152 
153  /// Load in the pixel maps
154  std::vector< art::Ptr< cnn::RegPixelMap > > pixelmaplist;
156  auto pixelmapListHandle = evt.getHandle< std::vector< cnn::RegPixelMap > >(itag1);
157  if (pixelmapListHandle){
158  art::fill_ptr_vector(pixelmaplist, pixelmapListHandle);
159  }
160 
161  /// Load 3D pixel map for direction reco.
162  std::vector< art::Ptr< cnn::RegPixelMap3D > > pixelmap3Dlist;
164  auto pixelmap3DListHandle = evt.getHandle< std::vector< cnn::RegPixelMap3D > >(itag2);
165  if (pixelmap3DListHandle) {
166  art::fill_ptr_vector(pixelmap3Dlist, pixelmap3DListHandle);
167  }
168 
169  /// Make sure we have a valid name for the CNN type
170  if(fCNNType == "TF" || fCNNType == "Tensorflow" || fCNNType == "TensorFlow"){
171  // If we have a pixel map then use the TF interface to give us a prediction
172  if(pixelmaplist.size() > 0){
173  std::vector<float> networkOutput;
174  if (fTarget == "nueenergy"){
175  networkOutput = fTFHandler.Predict(*pixelmaplist[0]);
176  //std::cout << "-->" << networkOutput[0] << std::endl;
177  }
178  else if (fTarget == "nuevertex"){
179  std::vector<float> center_of_mass(6,0);
180  getCM(*pixelmaplist[0], center_of_mass);
181  std::cout << "cm: " << center_of_mass[0] << " " << center_of_mass[1] << " " << center_of_mass[2] << std::endl;
182  networkOutput = fTFHandler.Predict(*pixelmaplist[0], center_of_mass);
183  std::cout << "cnn nuevertex : "<<networkOutput[0] << " " << networkOutput[1] << " " << networkOutput[2] << std::endl;
184  }
185  else if (fTarget == "nuevertex_on_img"){
186  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
187  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(evt, clockData);
188  networkOutput = fRegCNNVtxHandler.GetVertex(clockData, detProp, evt, *pixelmaplist[0]);
189  }
190  else if (fTarget == "numuenergy") {
191  networkOutput = fRegCNNNumuHandler.Predict(*pixelmaplist[0], fLongestTrackContained);
192  }
193  else {
194  std::cout << "Wrong Target with 2D 3-view pixel maps" << std::endl;
195  abort();
196  }
197 
198  // cnn::Result can now take a vector of floats and works out the number of outputs
199  resultCol->emplace_back(networkOutput);
200  }
201  } else {
202  mf::LogError("RegCNNEvaluator::produce") << "CNN Type not in the allowed list: Tensorflow, Torch" << std::endl;
203  mf::LogError("RegCNNEvaluator::produce") << "Exiting without processing events" << std::endl;
204  return;
205  } // end fCNNType
206 
207  evt.put(std::move(resultCol), fResultLabel);
208  }
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
std::vector< float > Predict(const RegPixelMap &pm)
Return prediction arrays for RegPixelMap.
def move(depos, offset)
Definition: depos.py:107
std::vector< float > Predict(const RegPixelMap &pm, bool fLongestTrackContained)
Return prediction arrays for RegPixelMap.
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
cnn::RegCNNNumuHandler fRegCNNNumuHandler
cnn::RegCNNVtxHandler fRegCNNVtxHandler
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
std::vector< float > GetVertex(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, art::Event &evt, const RegPixelMap &pixelmap)
void PrepareEvent(const art::Event &event)
QTextStream & endl(QTextStream &s)
void getCM(const RegPixelMap &pm, std::vector< float > &cm_list)
cnn::TFRegNetHandler fTFHandler

Member Data Documentation

std::string cnn::RegCNNEvaluator::fCNNType
private

Definition at line 77 of file RegCNNEvaluator_module.cc.

double cnn::RegCNNEvaluator::fContVolCut
private

Definition at line 87 of file RegCNNEvaluator_module.cc.

art::ServiceHandle<geo::Geometry> cnn::RegCNNEvaluator::fGeom
private

Definition at line 72 of file RegCNNEvaluator_module.cc.

std::string cnn::RegCNNEvaluator::fHitsModuleLabel
private

Definition at line 84 of file RegCNNEvaluator_module.cc.

bool cnn::RegCNNEvaluator::fLongestTrackContained
private

Definition at line 90 of file RegCNNEvaluator_module.cc.

std::string cnn::RegCNNEvaluator::fPixelMapInput
private

Definition at line 75 of file RegCNNEvaluator_module.cc.

cnn::RegCNNNumuHandler cnn::RegCNNEvaluator::fRegCNNNumuHandler
private

Definition at line 82 of file RegCNNEvaluator_module.cc.

cnn::RegCNNVtxHandler cnn::RegCNNEvaluator::fRegCNNVtxHandler
private

Definition at line 81 of file RegCNNEvaluator_module.cc.

std::string cnn::RegCNNEvaluator::fResultLabel
private

Definition at line 76 of file RegCNNEvaluator_module.cc.

std::string cnn::RegCNNEvaluator::fTarget
private

Definition at line 78 of file RegCNNEvaluator_module.cc.

cnn::TFRegNetHandler cnn::RegCNNEvaluator::fTFHandler
private

Definition at line 80 of file RegCNNEvaluator_module.cc.

std::string cnn::RegCNNEvaluator::fTrackModuleLabel
private

Definition at line 85 of file RegCNNEvaluator_module.cc.


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