CVNNeutronProngTF_module.cc
Go to the documentation of this file.
1 #include<iostream>
2 
13 #include "fhiclcpp/ParameterSet.h"
15 
16 
17 #include "RecoBase/Cluster.h"
18 #include "RecoBase/Prong.h"
19 #include "RecoBase/PID.h"
20 #include "RecoBase/FilterList.h"
21 #include "SummaryData/SpillData.h"
22 #include "CVN/func/PixelMap.h"
23 #include "CVN/func/Result.h"
25 #include "CVN/func/AssignLabels.h"
26 #include "CVN/func/TrainingData.h"
29 #include "CVN/func/ProngType.h"
31 #include "Utilities/AssociationUtil.h"
33 
35 
36 #include <memory>
37 
40  public:
41  explicit CVNNeutronProngTF(fhicl::ParameterSet const &pset);
42  virtual ~CVNNeutronProngTF();
43 
44  void produce(art::Event& evt);
45  tensorflow::Tensor vector_to_tensor(std::vector<unsigned char>);
46  tensorflow::Tensor vector_to_tensor(std::vector<unsigned char>, std::vector<unsigned char>);
49  void CalcResult( std::vector< art::Ptr<cvn::PixelMap> > pixelMaps, tensorflow::TFHandler* fTF, unsigned int NOutput,
50  std::vector<std::pair<int, double>> &outputwithpdg, std::vector<float> &result , std::vector<unsigned char> pmslc);
51 
52  protected:
57  std::vector<std::string> fPreselectionLabels;
58  bool fUseGeV;
60 
64  unsigned int fNOutput2D;
65 
68  unsigned int fNOutput3D;
69 
73  unsigned int fCPUlimit;
76 
77  };
78 
80  EDProducer(pset),
81  fSliceLabel (pset.get<std::string>("SliceLabel")),
82  fPixelMapInput(pset.get<std::string>("PixelMapInput")),
83  fGeneratorLabel (pset.get<std::string>("GeneratorLabel")),
84  fObeyPreselection (pset.get<bool> ("ObeyPreselection" )),
85  fPreselectionLabels(pset.get<std::vector<std::string>> ("PreselectionLabels")),
86  fUseGeV (pset.get<bool> ("UseGeV")),
87  fLibPath (pset.get<std::string>("LibPath")),
88 
89  fProngModLabel(pset.get<std::string>("ProngModLabel")),
90  fProng2DLabel (pset.get<std::string>("Prong2DLabel")),
91  fModel2DName (pset.get<std::string>("Model2DName")),
92  fNOutput2D (pset.get<unsigned int>("NOutput2D")),
93 
94  fProng3DLabel (pset.get<std::string>("Prong3DLabel")),
95  fModel3DName (pset.get<std::string>("Model3DName")),
96  fNOutput3D (pset.get<unsigned int>("NOutput3D")),
97 
98  fInputNameSlc (pset.get<std::string>("InputNameSlice")),
99  fInputName (pset.get<std::string>("InputName")),
100  fOutputName (pset.get<std::string>("OutputName")),
101  fCPUlimit (pset.get<unsigned int>("CPUlimit")),
102  fTF2D(0),
103  fTF3D(0)
104  {
106  produces< std::vector<cvn::Result> >();
107  produces< art::Assns<cvn::Result, rb::Cluster> >();
108  produces< art::Assns<cvn::Result, rb::Prong> >();
109  produces< std::vector<rb::PID> >();
110  produces< art::Assns<rb::PID,rb::Prong> >();
111  }
112 
114  {
115  if(fTF2D) delete fTF2D;
116  if(fTF3D) delete fTF3D;
117 
118  }
119 
121  {
122  if (!fTF2D)
124  return fTF2D;
125  }
126 
128  {
129  if (!fTF3D)
131  return fTF3D;
132  }
133 
134  tensorflow::Tensor CVNNeutronProngTF::vector_to_tensor(std::vector<unsigned char> pm)
135  {
136  const unsigned int vectorSize = pm.size();
137 
138  // Initialize the tensors
139  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
140  auto rel = tensor.tensor<float,2>();
141 
142  // Loop over each element
143  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
144 
145  return tensor;
146  }
147 
148  tensorflow::Tensor CVNNeutronProngTF::vector_to_tensor(std::vector<unsigned char> pmslice, std::vector<unsigned char> pmprong)
149  {
150  const unsigned int vectorSizeSlice = pmslice.size();
151  const unsigned int vectorSizeProng = pmprong.size();
152  const unsigned int vectorSizeTotal = vectorSizeSlice + vectorSizeProng;
153 
154  // Initialize the tensors
155  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSizeTotal});
156  auto rel = tensor.tensor<float,2>();
157 
158  // Loop over each element
159  for(unsigned int i = 0; i < vectorSizeSlice; ++i) rel(0, i) = pmslice[i];
160  for(unsigned int j = vectorSizeSlice; j < vectorSizeTotal; ++j) rel(0, j) = pmprong[j-vectorSizeSlice];
161 
162  return tensor;
163  }
164 
166  {
167 
170  //Containers for things we're gonna produce
171  std::unique_ptr< std::vector<cvn::Result> >
172  resultCol(new std::vector<cvn::Result>);
173  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
174  assocresultslice(new art::Assns<cvn::Result, rb::Cluster>);
175  std::unique_ptr< art::Assns<cvn::Result, rb::Prong> >
176  assocresultprong(new art::Assns<cvn::Result, rb::Prong>);
177  std::unique_ptr< std::vector<rb::PID> >
178  pidCol(new std::vector<rb::PID>);
179  std::unique_ptr< art::Assns<rb::PID, rb::Prong> >
180  assocpid(new art::Assns<rb::PID, rb::Prong>);
181 
182  // Get slices
184  evt.getByLabel(fSliceLabel, slicecol);
185  art::PtrVector<rb::Cluster> slicelist;
186  for(unsigned int i = 0; i < slicecol->size(); ++i){
187  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
188  }
189 
190  // Get pixel maps
191  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
192 
193  //loop over slices
194  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
195  if(!fmPixelMap.isValid()) continue;
196  if(slicelist[iClust]->IsNoise()) continue;
197  if(fObeyPreselection && rb::IsFiltered(evt, slicecol, iClust, fPreselectionLabels)) continue;
198 
199  //get slice pixel maps
200  const std::vector<art::Ptr<cvn::PixelMap> > pixelMapsSl = fmPixelMap.at(iClust);
201  if(pixelMapsSl.empty()) continue;
202  std::vector<unsigned char> pmslice = (*pixelMapsSl[0]).PixelMapToVector(fUseGeV);
203 
204 
205  // --- 2D Prongs.
207  if( fmProng2D.isValid() ) {
208  // Get everything out of the event.
209  std::vector<art::Ptr<rb::Prong>> prongs2D = fmProng2D.at(iClust);
210  art::FindManyP<cvn::PixelMap> fmPixelMap2D(prongs2D, evt, fPixelMapInput);
211  if ( fmPixelMap2D.isValid() ) {
212 
213  for( unsigned int iProng = 0; iProng < prongs2D.size(); ++iProng ){
214  // Calculate result
215  std::vector<std::pair<int, double>> outputwithpdg;
216  std::vector<float> result;
217  if ( fmPixelMap2D.at(iProng).empty() ) continue;
218  CalcResult( fmPixelMap2D.at(iProng), fTF2D, fNOutput2D, outputwithpdg, result , pmslice );
219  // Ensure that result.size() equals fNOutput
220  assert( result.size() == fNOutput2D);
221  // Make Assn between result and this prong.
222  float resultvec[fNOutput2D];
223  for(unsigned int i = 0; i < fNOutput2D; i++){
224  resultvec[i] = result[i];
225  // Make Assn.
226  pidCol->emplace_back(outputwithpdg[i].first, outputwithpdg[i].second);
227  util::CreateAssn(evt, *(pidCol.get()),
228  prongs2D[iProng], *(assocpid.get()), UINT_MAX);
229  }
230  // Make Assn between full result output and prong.
231  const float* resultoutput = resultvec;
232  resultCol->emplace_back(resultoutput, fNOutput2D);
233  util::CreateAssn(evt, *(resultCol.get()),
234  slicelist[iClust], *(assocresultslice.get()), UINT_MAX);
235  util::CreateAssn(evt, *(resultCol.get()),
236  prongs2D[iClust], *(assocresultprong.get()), UINT_MAX);
237  }
238  }
239  }
240 
241  // --- 3D Prongs.
243  std::vector<art::Ptr<rb::Prong>> prongs3D;
244  if( fmProng3D.isValid() ) {
245  // Get everything out of the event.
246  std::vector<art::Ptr<rb::Prong>> prongs3D = fmProng3D.at(iClust);
247  art::FindManyP<cvn::PixelMap> fmPixelMap3D(prongs3D, evt, fPixelMapInput);
248  if ( fmPixelMap3D.isValid() ) {
249  for( unsigned int iProng = 0; iProng < prongs3D.size(); ++iProng ){
250  // Calculate result
251  std::vector<std::pair<int, double>> outputwithpdg;
252  std::vector<float> result;
253  if ( fmPixelMap3D.at(iProng).empty() ) continue;
254  CalcResult( fmPixelMap3D.at(iProng), fTF3D, fNOutput3D, outputwithpdg, result , pmslice );
255  // Ensure that result.size() equals fNOutput
256  //assert( result.size() == fNOutput3D);
257  // Make Assn between result and this prong.
258  float resultvec[fNOutput3D];
259  for(unsigned int i = 0; i < result.size(); i++){
260  resultvec[i] = result[i];
261  // Make Assn.
262  pidCol->emplace_back(outputwithpdg[i].first, outputwithpdg[i].second);
263  util::CreateAssn(evt, *(pidCol.get()),
264  prongs3D[iProng], *(assocpid.get()), UINT_MAX);
265  }
266  // Make Assn between full result output and prong.
267  const float* resultoutput = resultvec;
268  resultCol->emplace_back(resultoutput, fNOutput3D);
269  util::CreateAssn(evt, *(resultCol.get()),
270  slicelist[iClust], *(assocresultslice.get()), UINT_MAX);
271  util::CreateAssn(evt, *(resultCol.get()),
272  prongs3D[iClust], *(assocresultprong.get()), UINT_MAX);
273  }
274  }
275  }
276 
277  } // slices
278  evt.put(std::move(resultCol));
279  evt.put(std::move(assocresultslice));
280  evt.put(std::move(assocresultprong));
281  evt.put(std::move(pidCol));
282  evt.put(std::move(assocpid));
283  } // produce
284 
285  // --- Function to calculate the CVN inference.
286  // --- Need a function so can do both 2D and 3D evaluation.
287  void CVNNeutronProngTF::CalcResult( std::vector< art::Ptr<cvn::PixelMap> > pixelMaps, tensorflow::TFHandler* fTF, unsigned int NOutput,
288  std::vector<std::pair<int, double>> &outputwithpdg, std::vector<float> &result , std::vector<unsigned char> pmslc) {
289 
290  std::vector<unsigned char> pmprong = (*pixelMaps[0]).PixelMapToVector(fUseGeV);
291 
292  tensorflow::Tensor tensorslc;
293  tensorflow::Tensor tensor;
294  tensor = vector_to_tensor(pmprong);
295  tensorslc = vector_to_tensor(pmslc);
296 
297 
298  std::vector<tensorflow::Tensor> TFResult = fTF->Predict({{fInputNameSlc, tensorslc}, {fInputName, tensor}}, {fOutputName});
299  auto tfoutput = TFResult[0].tensor<float,2>();
300 
301 
302  for(unsigned int i = 0; i < NOutput; i++){
303  //Fill and create PID assn
304  if(i==0){
305  outputwithpdg.push_back(std::make_pair(2212, (double)tfoutput(0,i)));
306  }
307  else if (i==1){
308  outputwithpdg.push_back(std::make_pair(22 , (double)tfoutput(0,i)));
309  }
310  else if (i==2){
311  outputwithpdg.push_back(std::make_pair(0 , (double)tfoutput(0,i)));
312  }
313  //Fill Result column
314  result.push_back( (float)tfoutput(0,i) );
315  }
316 
317  return;
318  }
319 }
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.
tensorflow::TFHandler * GetModel2D(const art::Event &evt)
pdg code and pid value
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 CalcResult(std::vector< art::Ptr< cvn::PixelMap > > pixelMaps, tensorflow::TFHandler *fTF, unsigned int NOutput, std::vector< std::pair< int, double >> &outputwithpdg, std::vector< float > &result, std::vector< unsigned char > pmslc)
tensorflow::TFHandler * GetModel3D(const art::Event &evt)
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
DEFINE_ART_MODULE(TestTMapFile)
PixelMap for CVN.
tensorflow::Tensor vector_to_tensor(std::vector< unsigned char >)
std::vector< Tensor > Predict(std::vector< std::pair< std::string, Tensor >> inputs, std::vector< std::string > outputLabels)
Definition: TFHandler.cxx:64
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:446
Result for CVN.
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
int evt
CVNNeutronProngTF(fhicl::ParameterSet const &pset)
std::vector< std::string > fPreselectionLabels
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
const double j
Definition: BetheBloch.cxx:29
string rel
Definition: shutoffs.py:11
size_type size() const
Definition: PtrVector.h:302
assert(nhit_max >=nhit_nbins)
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
Definition: fwd.h:29
enum BeamMode string