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:
54  std::vector<std::string> fPreselectionLabels;
55 
60 
65 
67 
71 
72  };
73 }
74 
75 namespace nuonecvntf{
76 
78  fSliceLabel (pset.get<std::string>("SliceLabel")),
79  fPixelMapInput (pset.get<std::string>("PixelMapInput")),
80  fGeneratorLabel (pset.get<std::string>("GeneratorLabel")),
81  fNuMILabel (pset.get<std::string>("NuMILabel")),
82  fObeyPreselection (pset.get<bool> ("ObeyPreselection" )),
83  fPreselectionLabels(pset.get<std::vector<std::string>> ("PreselectionLabels")),
84  fProngInput (pset.get<bool> ("ProngInput")),
85  fProngModLabel (pset.get<std::string> ("ProngModLabel")),
86  fProng3DLabel (pset.get<std::string> ("Prong3DLabel")),
87  fTrack3DLabel (pset.get<std::string> ("Track3DLabel")),
88  fLibPath (pset.get<std::string> ("LibPath")),
89  fTFProtoBufNuonEE (pset.get<std::string> ("TFProtoBufNuonEE")),
90  fTFProtoBufNuonEID(pset.get<std::string> ("TFProtoBufNuonEID")),
91  fTFProtoBufEPi0ID (pset.get<std::string> ("TFProtoBufEPi0ID")),
92  fUseOppositeHornCurrentNetwork(pset.get<bool>("UseOppositeHornCurrentNetwork"))
93  {
95 
99 
100  fTFNuonE = new RegModel(fTFProtoBufNuonEE);
101  fTFNuonEID = new RegModel(fTFProtoBufNuonEID);
102  fTFEPi0ID = new RegModel(fTFProtoBufEPi0ID);
103 
104  produces< std::vector<cvn::Result> >();
105  produces< art::Assns<cvn::Result, rb::Cluster> >();
106  }
107 
109  {
110  delete fTFNuonE;
111  delete fTFNuonEID;
112  delete fTFEPi0ID;
113  }
114 
116  {
118  if (!evt.isRealData())
119  evt.getByLabel(fGeneratorLabel, spillPot);
120  else
121  evt.getByLabel(fNuMILabel, spillPot);
122 
123  if (spillPot.failedToGet())
124  {
125  mf::LogError("NuonECVNTF") <<
126  "Spill Data not found, aborting without horn current information";
127  abort();
128  }
129 
130  if(fUseOppositeHornCurrentNetwork) return !spillPot->isRHC;
131 
132  // NB - the logic here will cause 0HC to use the FHC network
133  return spillPot->isRHC;
134  }
135 
136  std::vector< tensorflow::Tensor > NuonECVNTF::vector_to_tensor(std::vector<unsigned char> pm, unsigned int nplanes, unsigned int ncells)
137  {
138  // for models which has two input: x-view and y-view pixel maps
139 
140  std::size_t const half_size = pm.size() / 2;
141  std::vector<unsigned char> pm_x(pm.begin(), pm.begin() + half_size);
142  std::vector<unsigned char> pm_y(pm.begin() + half_size, pm.end());
143 
144  long long int samples = 1, rows = nplanes, cols = ncells;
145 
146  std::vector< tensorflow::Tensor > _x;
147  for (unsigned int ii = 0; ii < 2; ++ii){
148  tensorflow::Tensor _xtemp(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, 1 }));
149  _x.push_back(_xtemp);
150  }
151 
152  for (long long int s = 0; s < samples; ++s) {
153  for (long long int r = 0; r < rows; ++r) {
154  for (long long int c = 0; c < cols; ++c) {
155  unsigned int element = c + cols * r;
156  _x[0].tensor<float, 4>()(s, r, c, 0) = pm_x[element];
157  _x[1].tensor<float, 4>()(s, r, c, 0) = pm_y[element];
158  }
159  }
160  }
161  return _x;
162  }
163 
164  tensorflow::Tensor NuonECVNTF::vector_to_tensor(std::vector<unsigned char> pm)
165  {
166  // for models which combine two views into one vector
167 
168  const unsigned int vectorSize = pm.size();
169 
170  // Initialize the tensors
171  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
172  auto rel = tensor.tensor<float,2>();
173 
174  // Loop over each element
175  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
176 
177  return tensor;
178  }
179 
181  {
182  }
183 
185  {
186  std::unique_ptr< std::vector<cvn::Result> >
187  resultNuonECol(new std::vector<cvn::Result>);
188  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
189  assocNuonE(new art::Assns<cvn::Result, rb::Cluster>);
190 
191  RegModel* model = fTFNuonE; // event energy estimator
192  RegModel* nuone_model = fTFNuonEID; // nu-on-e classifier
193  RegModel* epi0_model = fTFEPi0ID; // epi0 classifier
194 
195  // Get slices
197  evt.getByLabel(fSliceLabel, slicecol);
198  art::PtrVector<rb::Cluster> slicelist;
199  for(unsigned int i = 0; i < slicecol->size(); ++i){
200  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
201  }
202 
203  // Get pixel maps
204  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
205 
206  // loop over slices
207  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
208  if(!fmPixelMap.isValid()) continue;
209  if(slicelist[iClust]->IsNoise()) continue;
210  if(fObeyPreselection && rb::IsFiltered(evt, slicecol, iClust, fPreselectionLabels)) continue;
211 
212  const std::vector<art::Ptr<cvn::PixelMap> > pixelMaps = fmPixelMap.at(iClust);
213 
214  if(pixelMaps.empty()) continue;
215 
216  std::vector<unsigned char> evtpm = (*pixelMaps[0]).PixelMapToVector(true);
217 
218  // Event energy estimator
219  std::vector<tensorflow::Tensor> tensorEvtE = vector_to_tensor(evtpm, (*pixelMaps[0]).NPlanePerView(), (*pixelMaps[0]).NCell());
220  std::vector<tensorflow::Tensor> resultEvtE = model->Predict({{"input_1",tensorEvtE[0]},{"input_2",tensorEvtE[1]}}, {"output_node0"});
221  auto tfoutputEvtE = resultEvtE[0].tensor<float,2>();
222 
223  // nu-on-e classifier
224  tensorflow::Tensor tensorNuonEID = vector_to_tensor(evtpm);
225  std::vector<tensorflow::Tensor> resultNuonEID = nuone_model->Predict({{"input_1",tensorNuonEID}},{"output_out"});
226  auto tfoutputNuonEID = resultNuonEID[0].tensor<float,2>();
227 
228  // epi0 classifier
229  std::vector<tensorflow::Tensor> resultEPi0ID = epi0_model->Predict({{"input_1",tensorNuonEID}},{"output_out"});
230  auto tfoutputEPi0ID = resultEPi0ID[0].tensor<float,2>();
231 
232  // put results in one vector
233  unsigned int fNOutput = 7;
234  float resultvec[fNOutput];
235  resultvec[0] = (float)tfoutputEvtE(0,0);
236  for (unsigned int i= 0; i< 4; ++i) {
237  resultvec[i+1] = (float)tfoutputNuonEID(0,i);
238  }
239  for (unsigned int i= 0; i< 2; ++i) {
240  resultvec[i+5] = (float)tfoutputEPi0ID(0,i);
241  }
242 
243  const float* output = resultvec;
244 
245  resultNuonECol->emplace_back(output, fNOutput);
246  util::CreateAssn(*this, evt, *(resultNuonECol.get()),
247  slicelist[iClust], *(assocNuonE.get()), UINT_MAX);
248 
249  } // slices
250 
251  evt.put(std::move(resultNuonECol));
252  evt.put(std::move(assocNuonE));
253 
254  } // produce
255 }
256 
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
bool IsFiltered(const art::Event &evt, art::Ptr< T > x, const std::vector< std::string > &labels)
Is this Ptr marked "filtered out"?
Definition: FilterList.h:96
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
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)
std::vector< std::string > fPreselectionLabels
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
enum BeamMode string