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