NuonECVNTF_module.cc
Go to the documentation of this file.
1 #include<iostream>
2 
10 #include "fhiclcpp/ParameterSet.h"
13 
14 #include "RecoBase/Cluster.h"
15 #include "RecoBase/FilterList.h"
16 #include "RecoBase/Track.h"
17 #include "SummaryData/SpillData.h"
18 #include "Utilities/AssociationUtil.h"
19 
22 #include "CVN/func/PixelMap.h"
23 #include "CVN/func/Result.h"
24 
27 #include <memory>
28 #include <chrono>
29 
30 // ROOT includes
31 #include "TTree.h"
32 
34 
35 namespace nuonecvntf{
36  class NuonECVNTF : public art::EDProducer{
37 
38  public:
39  explicit NuonECVNTF(fhicl::ParameterSet const &pset);
40  virtual ~NuonECVNTF();
41 
42  void produce(art::Event& evt);
43  void beginJob();
44  bool IsRHC(const art::Event &evt);
45  std::vector< tensorflow::Tensor > vector_to_tensor(std::vector<unsigned char>, unsigned int ncells, unsigned int nplanes);
46  tensorflow::Tensor vector_to_tensor(std::vector<unsigned char>);
47 
48  protected:
53 
58 
63 
65 
69 
70  };
71 }
72 
73 namespace nuonecvntf{
74 
76  fSliceLabel (pset.get<std::string>("SliceLabel")),
77  fPixelMapInput (pset.get<std::string>("PixelMapInput")),
78  fGeneratorLabel (pset.get<std::string>("GeneratorLabel")),
79  fNuMILabel (pset.get<std::string>("NuMILabel")),
80  fProngInput (pset.get<bool> ("ProngInput")),
81  fProngModLabel (pset.get<std::string> ("ProngModLabel")),
82  fProng3DLabel (pset.get<std::string> ("Prong3DLabel")),
83  fTrack3DLabel (pset.get<std::string> ("Track3DLabel")),
84  fLibPath (pset.get<std::string> ("LibPath")),
85  fTFProtoBufNuonEE (pset.get<std::string> ("TFProtoBufNuonEE")),
86  fTFProtoBufNuonEID(pset.get<std::string> ("TFProtoBufNuonEID")),
87  fTFProtoBufEPi0ID (pset.get<std::string> ("TFProtoBufEPi0ID")),
88  fUseOppositeHornCurrentNetwork(pset.get<bool>("UseOppositeHornCurrentNetwork"))
89  {
91 
95 
96  fTFNuonE = new RegModel(fTFProtoBufNuonEE);
97  fTFNuonEID = new RegModel(fTFProtoBufNuonEID);
98  fTFEPi0ID = new RegModel(fTFProtoBufEPi0ID);
99 
100  produces< std::vector<cvn::Result> >();
101  produces< art::Assns<cvn::Result, rb::Cluster> >();
102  }
103 
105  {
106  delete fTFNuonE;
107  delete fTFNuonEID;
108  delete fTFEPi0ID;
109  }
110 
112  {
114  if (!evt.isRealData())
115  evt.getByLabel(fGeneratorLabel, spillPot);
116  else
117  evt.getByLabel(fNuMILabel, spillPot);
118 
119  if (spillPot.failedToGet())
120  {
121  mf::LogError("NuonECVNTF") <<
122  "Spill Data not found, aborting without horn current information";
123  abort();
124  }
125 
126  if(fUseOppositeHornCurrentNetwork) return !spillPot->isRHC;
127 
128  // NB - the logic here will cause 0HC to use the FHC network
129  return spillPot->isRHC;
130  }
131 
132  std::vector< tensorflow::Tensor > NuonECVNTF::vector_to_tensor(std::vector<unsigned char> pm, unsigned int nplanes, unsigned int ncells)
133  {
134  // for models which has two input: x-view and y-view pixel maps
135 
136  std::size_t const half_size = pm.size() / 2;
137  std::vector<unsigned char> pm_x(pm.begin(), pm.begin() + half_size);
138  std::vector<unsigned char> pm_y(pm.begin() + half_size, pm.end());
139 
140  long long int samples = 1, rows = nplanes, cols = ncells;
141 
142  std::vector< tensorflow::Tensor > _x;
143  for (unsigned int ii = 0; ii < 2; ++ii){
144  tensorflow::Tensor _xtemp(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, 1 }));
145  _x.push_back(_xtemp);
146  }
147 
148  for (long long int s = 0; s < samples; ++s) {
149  for (long long int r = 0; r < rows; ++r) {
150  for (long long int c = 0; c < cols; ++c) {
151  unsigned int element = c + cols * r;
152  _x[0].tensor<float, 4>()(s, r, c, 0) = pm_x[element];
153  _x[1].tensor<float, 4>()(s, r, c, 0) = pm_y[element];
154  }
155  }
156  }
157  return _x;
158  }
159 
160  tensorflow::Tensor NuonECVNTF::vector_to_tensor(std::vector<unsigned char> pm)
161  {
162  // for models which combine two views into one vector
163 
164  const unsigned int vectorSize = pm.size();
165 
166  // Initialize the tensors
167  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
168  auto rel = tensor.tensor<float,2>();
169 
170  // Loop over each element
171  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
172 
173  return tensor;
174  }
175 
177  {
178  }
179 
181  {
182  std::unique_ptr< std::vector<cvn::Result> >
183  resultNuonECol(new std::vector<cvn::Result>);
184  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
185  assocNuonE(new art::Assns<cvn::Result, rb::Cluster>);
186 
187  RegModel* model = fTFNuonE; // event energy estimator
188  RegModel* nuone_model = fTFNuonEID; // nu-on-e classifier
189  RegModel* epi0_model = fTFEPi0ID; // epi0 classifier
190 
191  // Get slices
193  evt.getByLabel(fSliceLabel, slicecol);
194  art::PtrVector<rb::Cluster> slicelist;
195  for(unsigned int i = 0; i < slicecol->size(); ++i){
196  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
197  }
198 
199  // Get pixel maps
200  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
201 
202  // loop over slices
203  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
204  if(!fmPixelMap.isValid()) continue;
205  if(slicelist[iClust]->IsNoise()) continue;
206 
207  const std::vector<art::Ptr<cvn::PixelMap> > pixelMaps = fmPixelMap.at(iClust);
208 
209  if(pixelMaps.empty()) continue;
210 
211  std::vector<unsigned char> evtpm = (*pixelMaps[0]).PixelMapToVector(true);
212 
213  // Event energy estimator
214  std::vector<tensorflow::Tensor> tensorEvtE = vector_to_tensor(evtpm, (*pixelMaps[0]).NPlanePerView(), (*pixelMaps[0]).NCell());
215  std::vector<tensorflow::Tensor> resultEvtE = model->Predict({{"input_1",tensorEvtE[0]},{"input_2",tensorEvtE[1]}}, {"output_node0"});
216  auto tfoutputEvtE = resultEvtE[0].tensor<float,2>();
217 
218  // nu-on-e classifier
219  tensorflow::Tensor tensorNuonEID = vector_to_tensor(evtpm);
220  std::vector<tensorflow::Tensor> resultNuonEID = nuone_model->Predict({{"input_1",tensorNuonEID}},{"output_out"});
221  auto tfoutputNuonEID = resultNuonEID[0].tensor<float,2>();
222 
223  // epi0 classifier
224  std::vector<tensorflow::Tensor> resultEPi0ID = epi0_model->Predict({{"input_1",tensorNuonEID}},{"output_out"});
225  auto tfoutputEPi0ID = resultEPi0ID[0].tensor<float,2>();
226 
227  // put results in one vector
228  unsigned int fNOutput = 7;
229  float resultvec[fNOutput];
230  resultvec[0] = (float)tfoutputEvtE(0,0);
231  for (unsigned int i= 0; i< 4; ++i) {
232  resultvec[i+1] = (float)tfoutputNuonEID(0,i);
233  }
234  for (unsigned int i= 0; i< 2; ++i) {
235  resultvec[i+5] = (float)tfoutputEPi0ID(0,i);
236  }
237 
238  const float* output = resultvec;
239 
240  resultNuonECol->emplace_back(output, fNOutput);
241  util::CreateAssn(*this, evt, *(resultNuonECol.get()),
242  slicelist[iClust], *(assocNuonE.get()), UINT_MAX);
243 
244  } // slices
245 
246  evt.put(std::move(resultNuonECol));
247  evt.put(std::move(assocNuonE));
248 
249  } // produce
250 }
251 
bool isRHC
is the beam in antineutrino mode, aka RHC
Definition: SpillData.h:28
ofstream output
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
std::string EnvExpansion(const std::string &inString)
Function to expand environment variables.
Definition: EnvExpand.cxx:8
void produce(art::Event &evt)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool isRealData() const
Definition: Event.h:83
DEFINE_ART_MODULE(TestTMapFile)
PixelMap for CVN.
std::vector< Tensor > Predict(std::vector< std::pair< std::string, Tensor >> inputs, std::vector< std::string > outputLabels)
Definition: TFHandler.cxx:64
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
const XML_Char * s
Definition: expat.h:262
Result for CVN.
const int cols[3]
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
NuonECVNTF(fhicl::ParameterSet const &pset)
tensorflow::TFHandler RegModel
int evt
string rel
Definition: shutoffs.py:11
bool IsRHC(const art::Event &evt)
size_type size() const
Definition: PtrVector.h:308
int nplanes
Definition: geom.C:145
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::vector< tensorflow::Tensor > vector_to_tensor(std::vector< unsigned char >, unsigned int ncells, unsigned int nplanes)
TRandom3 r(0)
int ncells
Definition: geom.C:124
Wrapper for Tensorflow which handles construction and prediction.
Definition: TFHandler.h:19
const XML_Char XML_Content * model
Definition: expat.h:151
bool failedToGet() const
Definition: Handle.h:196