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  fPixelMapInput(pset.get<std::string>("PixelMapInput")),
59  fCosmicCut(pset.get<float>("CosmicCut")),
60  fTF(0),
61  fTFF(0),
62  fPixelMapToken(consumes<std::vector<cvn::PixelMap>>(fPixelMapInput))
63  {
64  fhicl::ParameterSet const handlerConfig = pset.get<fhicl::ParameterSet>("TFHandler");
65  std::string const handlerMethod = handlerConfig.get<std::string>("Method");
66  if (handlerMethod == "direct") {
67  fTF = new tensorflow::TFHandler(util::EnvExpansion(handlerConfig.get<std::string>("ModelPath")));
68  useFIFO = false;
69  }
70  else if (handlerMethod == "fifo") {
71  fTFF = new tensorflow::TFFifoHandler(handlerConfig);
72  useFIFO = true;
73  }
74  else {
75  mf::LogError("CVNCosmicTF") << "Unrecognized TensorFlowHandler method: " << handlerMethod;
76  abort();
77  }
78 
79  produces<std::vector<cvntf::CVNCosmicFiltList> >();
80 
81  }
82 
84  {
85  //if(fTF) delete fTF;
86  }
87 
88  void CVNCosmicTF::pixelmap_to_tensor(cvn::PixelMap const* pip, tensorflow::Tensor *tensorX, tensorflow::Tensor* tensorY)
89  {
90  // Initialize the tensors
91  auto relX = tensorX->tensor<float,4>();
92  auto relY = tensorY->tensor<float,4>();
93 
94  int index = 0;
95 
96  // Loop over each element
97  for(unsigned int iPlane = 0; iPlane < pip->NPlanePerView(); ++iPlane){
98  for(unsigned int iCell = 0; iCell < pip->NCell(); ++iCell){
99  relX(0, iCell, iPlane, 0) = (pip->fPEX[index]>0 ? 1 : 0);
100  relY(0, iCell, iPlane, 0) = (pip->fPEY[index]>0 ? 1 : 0);
101  ++index; //unsigned int index = plane * fNCell + cell % fNCell;
102  }
103  }
104 
105  }
106 
108  {
109 
111  evt.getByToken(fPixelMapToken, pixelcol);
112  //art::PtrVector<cvn::PixelMap> pixellist;
113  std::vector<cvn::PixelMap const*> pixellist;
114 
115  auto cvnfiltlistcol = std::make_unique<std::vector<cvntf::CVNCosmicFiltList>>();
116 
117 
118  // Check for empty set of pixel maps
119  if (pixelcol->size() == 0) {
120  // If there are no pixel maps, add an empty set of filters to the event
121  evt.put(std::move(cvnfiltlistcol));
122  mf::LogWarning("CVNCosmicTF") << "No pixelmaps, storing an empty list of CosmicFilter results.";
123  return;
124  }
125 
126 
127  cvntf::CVNCosmicFiltList cosfiltlist;
128 
129  for (cvn::PixelMap const& pm: *pixelcol) {
130  pixellist.push_back(&pm);
131  }
132 
133  std::sort(pixellist.begin(), pixellist.end(), sortPixelTime);
134 
135 
136 
137 
138  //
139  // Evaluate the pixel maps and get the results back.
140  //
141  std::vector<std::vector<double> > tfoutput;
142 
143  if (useFIFO) {
144  tfoutput = fTFF->Predict(pixellist);
145  }
146  else {
147  for(size_t iP = 0; iP < pixellist.size(); ++iP) {
148  auto pip = pixellist[iP];
149 
150  // Fill the pixel map array for this slice
151  //std::vector<unsigned char> pm = pip->PixelMapToVector(false);
152 
153  const int ncells = pip->NCell();
154  const int nplanes = pip->NPlanePerView();
155  // Convert to format expected by the network
156  tensorflow::Tensor tensorX(tensorflow::DT_FLOAT, {1, ncells, nplanes, 1});
157  tensorflow::Tensor tensorY(tensorflow::DT_FLOAT, {1, ncells, nplanes, 1});
158  pixelmap_to_tensor(pip, &tensorX, &tensorY);
159 
160  std::vector<tensorflow::Tensor> result = fTF->Predict({{"xview",tensorX},{"yview",tensorY}},
161  {"output_out"});
162 
163  auto tfresult = result[0].tensor<float,2>();
164  std::vector<double> tfvals(5, 0);
165  for (int i = 0; i < 5; i++)
166  tfvals[i] = tfresult(0,i);
167 
168  tfoutput.push_back(tfvals);
169  }
170  }
171 
172 
173 
174  //
175  // loop over pixel maps again to save results
176  //
177 
178  for(size_t iP = 0; iP < pixellist.size(); ++iP) {
179  auto pip = pixellist[iP];
180 
181 
182  // Fill the cosmic filter info
183  cvntf::CVNCosmicFilt cosfilt;
184  cosfilt.timeWinMin = pip->MinTime();
185  cosfilt.timeWinMax = pip->MaxTime();
186  cosfilt.nHits = pip->fOccupiedPixels;
187  cosfilt.numuVal = tfoutput[iP][0];
188  cosfilt.nueVal = tfoutput[iP][1];
189  cosfilt.nutauVal = tfoutput[iP][2];
190  cosfilt.ncVal = tfoutput[iP][3];
191  cosfilt.cosmicVal = tfoutput[iP][4];
192  cosfilt.passSel = false;
193 
194 
195  // Apply cuts
196  if (cosfilt.cosmicVal < fCosmicCut) cosfilt.passSel = true;
197 
198 
199  // Save this CosFilt object
200  cosfiltlist.AddElement(cosfilt);
201  } // pixel maps
202 
203 
204  cvnfiltlistcol->push_back(cosfiltlist);
205 
206  evt.put(std::move(cvnfiltlistcol));
207 
208  } // produce
209 }
210 
const art::ProductToken< std::vector< cvn::PixelMap > > fPixelMapToken
std::vector< std::vector< double > > Predict(const std::vector< cvn::PixelMap const * > pixellist) const
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.
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
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
const double a
T get(std::string const &key) const
Definition: ParameterSet.h:231
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
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
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
int ncells
Definition: geom.C:124
float numuVal
Cosmic CVN numu score for each time slice.
tensorflow::TFHandler * fTF
bool getByToken(ProductToken< PROD > const &token, Handle< PROD > &result) const
Definition: DataViewImpl.h:387
static bool sortPixelTime(const cvn::PixelMap *a, const cvn::PixelMap *b)
Wrapper for Tensorflow which handles construction and prediction.
Definition: TFHandler.h:19
ProductToken< T > consumes(InputTag const &)
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)