CVNCosmicTF_module.cc
Go to the documentation of this file.
1 #include<iostream>
2 
10 #include "fhiclcpp/ParameterSet.h"
12 
13 #include "RecoBase/CellHit.h"
14 #include "RecoBase/Cluster.h"
15 #include "RawData/RawDigit.h"
18 #include "CVN/func/PixelMap.h"
20 
24 #include <boost/algorithm/string.hpp>
25 
26 #include <memory>
27 #include <fstream>
28 #include <iomanip>
29 #include <iostream>
30 
31 namespace cvntf{
32  class CVNCosmicTF : public art::EDProducer{
33  public:
34  explicit CVNCosmicTF(fhicl::ParameterSet const &pset);
35  virtual ~CVNCosmicTF();
36 
37  void produce(art::Event& evt);
38  void pixelmap_to_tensor(cvn::PixelMap const* pip,
39  tensorflow::Tensor* tensorX,
40  tensorflow::Tensor* tensorY);
41 
42  //static bool sortPixelTime(const art::Ptr<cvn::PixelMap>& a, const art::Ptr<cvn::PixelMap>& b) { return a->MinTime() < b->MinTime();}
43  static bool sortPixelTime(const cvn::PixelMap *a, const cvn::PixelMap *b) { return a->MinTime() < b->MinTime();}
44 
45  protected:
47  float fCosmicCut;
48  bool useFIFO;
49 
53  };
54 }
55 
56 namespace cvntf{
58  EDProducer(pset),
59  fPixelMapInput(pset.get<std::string>("PixelMapInput")),
60  fCosmicCut(pset.get<float>("CosmicCut")),
61  fTF(0),
62  fTFF(0),
63  fPixelMapToken(consumes<std::vector<cvn::PixelMap>>(fPixelMapInput))
64  {
65  fhicl::ParameterSet const handlerConfig = pset.get<fhicl::ParameterSet>("TFHandler");
66  std::string const handlerMethod = handlerConfig.get<std::string>("Method");
67  if (handlerMethod == "direct") {
68  fTF = new tensorflow::TFHandler(util::EnvExpansion(handlerConfig.get<std::string>("ModelPath")));
69  useFIFO = false;
70  }
71  else if (handlerMethod == "fifo") {
72  fTFF = new tensorflow::TFFifoHandler(handlerConfig);
73  useFIFO = true;
74  }
75  else {
76  mf::LogError("CVNCosmicTF") << "Unrecognized TensorFlowHandler method: " << handlerMethod;
77  abort();
78  }
79 
80  produces<std::vector<cvntf::CVNCosmicFiltList> >();
81 
82  }
83 
85  {
86  //if(fTF) delete fTF;
87  }
88 
89  void CVNCosmicTF::pixelmap_to_tensor(cvn::PixelMap const* pip, tensorflow::Tensor *tensorX, tensorflow::Tensor* tensorY)
90  {
91  // Initialize the tensors
92  auto relX = tensorX->tensor<float,4>();
93  auto relY = tensorY->tensor<float,4>();
94 
95  int index = 0;
96 
97  // Loop over each element
98  for(unsigned int iPlane = 0; iPlane < pip->NPlanePerView(); ++iPlane){
99  for(unsigned int iCell = 0; iCell < pip->NCell(); ++iCell){
100  relX(0, iCell, iPlane, 0) = (pip->fPEX[index]>0 ? 1 : 0);
101  relY(0, iCell, iPlane, 0) = (pip->fPEY[index]>0 ? 1 : 0);
102  ++index; //unsigned int index = plane * fNCell + cell % fNCell;
103  }
104  }
105 
106  }
107 
109  {
110 
112  evt.getByToken(fPixelMapToken, pixelcol);
113  //art::PtrVector<cvn::PixelMap> pixellist;
114  std::vector<cvn::PixelMap const*> pixellist;
115 
116  auto cvnfiltlistcol = std::make_unique<std::vector<cvntf::CVNCosmicFiltList>>();
117 
118 
119  // Check for empty set of pixel maps
120  if (pixelcol->size() == 0) {
121  // If there are no pixel maps, add an empty set of filters to the event
122  evt.put(std::move(cvnfiltlistcol));
123  mf::LogWarning("CVNCosmicTF") << "No pixelmaps, storing an empty list of CosmicFilter results.";
124  return;
125  }
126 
127 
128  cvntf::CVNCosmicFiltList cosfiltlist;
129 
130  for (cvn::PixelMap const& pm: *pixelcol) {
131  pixellist.push_back(&pm);
132  }
133 
134  std::sort(pixellist.begin(), pixellist.end(), sortPixelTime);
135 
136 
137 
138 
139  //
140  // Evaluate the pixel maps and get the results back.
141  //
142  std::vector<std::vector<double> > tfoutput;
143 
144  if (useFIFO) {
145  tfoutput = fTFF->Predict(pixellist);
146  }
147  else {
148  for(size_t iP = 0; iP < pixellist.size(); ++iP) {
149  auto pip = pixellist[iP];
150 
151  // Fill the pixel map array for this slice
152  //std::vector<unsigned char> pm = pip->PixelMapToVector(false);
153 
154  const int ncells = pip->NCell();
155  const int nplanes = pip->NPlanePerView();
156  // Convert to format expected by the network
157  tensorflow::Tensor tensorX(tensorflow::DT_FLOAT, {1, ncells, nplanes, 1});
158  tensorflow::Tensor tensorY(tensorflow::DT_FLOAT, {1, ncells, nplanes, 1});
159  pixelmap_to_tensor(pip, &tensorX, &tensorY);
160 
161  std::vector<tensorflow::Tensor> result = fTF->Predict({{"xview",tensorX},{"yview",tensorY}},
162  {"output_out"});
163 
164  auto tfresult = result[0].tensor<float,2>();
165  std::vector<double> tfvals(5, 0);
166  for (int i = 0; i < 5; i++)
167  tfvals[i] = tfresult(0,i);
168 
169  tfoutput.push_back(tfvals);
170  }
171  }
172 
173 
174 
175  //
176  // loop over pixel maps again to save results
177  //
178 
179  for(size_t iP = 0; iP < pixellist.size(); ++iP) {
180  auto pip = pixellist[iP];
181 
182 
183  // Fill the cosmic filter info
184  cvntf::CVNCosmicFilt cosfilt;
185  cosfilt.timeWinMin = pip->MinTime();
186  cosfilt.timeWinMax = pip->MaxTime();
187  cosfilt.nHits = pip->fOccupiedPixels;
188  cosfilt.numuVal = tfoutput[iP][0];
189  cosfilt.nueVal = tfoutput[iP][1];
190  cosfilt.nutauVal = tfoutput[iP][2];
191  cosfilt.ncVal = tfoutput[iP][3];
192  cosfilt.cosmicVal = tfoutput[iP][4];
193  cosfilt.passSel = false;
194 
195 
196  // Apply cuts
197  if (cosfilt.cosmicVal < fCosmicCut) cosfilt.passSel = true;
198 
199 
200  // Save this CosFilt object
201  cosfiltlist.AddElement(cosfilt);
202  } // pixel maps
203 
204 
205  cvnfiltlistcol->push_back(cosfiltlist);
206 
207  evt.put(std::move(cvnfiltlistcol));
208 
209  } // produce
210 }
211 
const art::ProductToken< std::vector< cvn::PixelMap > > fPixelMapToken
std::vector< std::vector< double > > Predict(const std::vector< cvn::PixelMap const * > pixellist) const
bool getByToken(ProductToken< PROD > const &, Handle< PROD > &result) const
Definition: DataViewImpl.h:462
CVNCosmicTF(fhicl::ParameterSet const &pset)
std::vector< float > fPEY
Vector of Y PE measurements for pixels.
Definition: PixelMap.h:129
float ncVal
Cosmic CVN nc score for each time slice.
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
std::string EnvExpansion(const std::string &inString)
Function to expand environment variables.
Definition: EnvExpand.cxx:8
float cosmicVal
Cosmic CVN cosmic score for each time slice.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
float nutauVal
Cosmic CVN nutau score for each time slice.
DEFINE_ART_MODULE(TestTMapFile)
PixelMap for CVN.
Defines an enumeration for prong classification.
std::vector< Tensor > Predict(std::vector< std::pair< std::string, Tensor >> inputs, std::vector< std::string > outputLabels)
Definition: TFHandler.cxx:64
void AddElement(CVNCosmicFilt cf)
float nueVal
Cosmic CVN nue score for each time slice.
Wrapper which handles communication with a TF script via FIFO pipes.
Definition: TFFifoHandler.h:20
const double a
T get(std::string const &key) const
Definition: ParameterSet.h:231
int evt
std::vector< float > fPEX
Vector of X PE measurements for pixels.
Definition: PixelMap.h:128
unsigned int NPlanePerView() const
Length in planes per view.
Definition: PixelMap.h:34
tensorflow::TFFifoHandler * fTFF
int nplanes
Definition: geom.C:145
Result for collection of time slices evaluated by CVN for cosmic rejectionN.
PixelMap, basic input to CVN neural net.
Definition: PixelMap.h:23
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
const hit & b
Definition: hits.cxx:21
ProductToken< T > consumes(InputTag const &)
Definition: ModuleBase.h:55
int ncells
Definition: geom.C:124
float numuVal
Cosmic CVN numu score for each time slice.
tensorflow::TFHandler * fTF
static bool sortPixelTime(const cvn::PixelMap *a, const cvn::PixelMap *b)
ProductID put(std::unique_ptr< PROD > &&edp, FullSemantic< Level::Run > const semantic)
Definition: DataViewImpl.h:730
Wrapper for Tensorflow which handles construction and prediction.
Definition: TFHandler.h:19
void pixelmap_to_tensor(cvn::PixelMap const *pip, tensorflow::Tensor *tensorX, tensorflow::Tensor *tensorY)
unsigned int nHits
number of hits in time window
float MinTime() const
Definition: PixelMap.h:46
unsigned int NCell() const
Width in cells.
Definition: PixelMap.h:37
void produce(art::Event &evt)
enum BeamMode string