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  fSliceLabel (pset.get<std::string>("SliceLabel")),
81  fPixelMapInput(pset.get<std::string>("PixelMapInput")),
82  fGeneratorLabel (pset.get<std::string>("GeneratorLabel")),
83  fObeyPreselection (pset.get<bool> ("ObeyPreselection" )),
84  fPreselectionLabels(pset.get<std::vector<std::string>> ("PreselectionLabels")),
85  fUseGeV (pset.get<bool> ("UseGeV")),
86  fLibPath (pset.get<std::string>("LibPath")),
87 
88  fProngModLabel(pset.get<std::string>("ProngModLabel")),
89  fProng2DLabel (pset.get<std::string>("Prong2DLabel")),
90  fModel2DName (pset.get<std::string>("Model2DName")),
91  fNOutput2D (pset.get<unsigned int>("NOutput2D")),
92 
93  fProng3DLabel (pset.get<std::string>("Prong3DLabel")),
94  fModel3DName (pset.get<std::string>("Model3DName")),
95  fNOutput3D (pset.get<unsigned int>("NOutput3D")),
96 
97  fInputNameSlc (pset.get<std::string>("InputNameSlice")),
98  fInputName (pset.get<std::string>("InputName")),
99  fOutputName (pset.get<std::string>("OutputName")),
100  fCPUlimit (pset.get<unsigned int>("CPUlimit")),
101  fTF2D(0),
102  fTF3D(0)
103  {
105  produces< std::vector<cvn::Result> >();
106  produces< art::Assns<cvn::Result, rb::Cluster> >();
107  produces< art::Assns<cvn::Result, rb::Prong> >();
108  produces< std::vector<rb::PID> >();
109  produces< art::Assns<rb::PID,rb::Prong> >();
110  }
111 
113  {
114  if(fTF2D) delete fTF2D;
115  if(fTF3D) delete fTF3D;
116 
117  }
118 
120  {
121  if (!fTF2D)
123  return fTF2D;
124  }
125 
127  {
128  if (!fTF3D)
130  return fTF3D;
131  }
132 
133  tensorflow::Tensor CVNNeutronProngTF::vector_to_tensor(std::vector<unsigned char> pm)
134  {
135  const unsigned int vectorSize = pm.size();
136 
137  // Initialize the tensors
138  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
139  auto rel = tensor.tensor<float,2>();
140 
141  // Loop over each element
142  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
143 
144  return tensor;
145  }
146 
147  tensorflow::Tensor CVNNeutronProngTF::vector_to_tensor(std::vector<unsigned char> pmslice, std::vector<unsigned char> pmprong)
148  {
149  const unsigned int vectorSizeSlice = pmslice.size();
150  const unsigned int vectorSizeProng = pmprong.size();
151  const unsigned int vectorSizeTotal = vectorSizeSlice + vectorSizeProng;
152 
153  // Initialize the tensors
154  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSizeTotal});
155  auto rel = tensor.tensor<float,2>();
156 
157  // Loop over each element
158  for(unsigned int i = 0; i < vectorSizeSlice; ++i) rel(0, i) = pmslice[i];
159  for(unsigned int j = vectorSizeSlice; j < vectorSizeTotal; ++j) rel(0, j) = pmprong[j-vectorSizeSlice];
160 
161  return tensor;
162  }
163 
165  {
166 
169  //Containers for things we're gonna produce
170  std::unique_ptr< std::vector<cvn::Result> >
171  resultCol(new std::vector<cvn::Result>);
172  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
173  assocresultslice(new art::Assns<cvn::Result, rb::Cluster>);
174  std::unique_ptr< art::Assns<cvn::Result, rb::Prong> >
175  assocresultprong(new art::Assns<cvn::Result, rb::Prong>);
176  std::unique_ptr< std::vector<rb::PID> >
177  pidCol(new std::vector<rb::PID>);
178  std::unique_ptr< art::Assns<rb::PID, rb::Prong> >
179  assocpid(new art::Assns<rb::PID, rb::Prong>);
180 
181  // Get slices
183  evt.getByLabel(fSliceLabel, slicecol);
184  art::PtrVector<rb::Cluster> slicelist;
185  for(unsigned int i = 0; i < slicecol->size(); ++i){
186  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
187  }
188 
189  // Get pixel maps
190  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
191 
192  //loop over slices
193  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
194  if(!fmPixelMap.isValid()) continue;
195  if(slicelist[iClust]->IsNoise()) continue;
196  if(fObeyPreselection && rb::IsFiltered(evt, slicecol, iClust, fPreselectionLabels)) continue;
197 
198  //get slice pixel maps
199  const std::vector<art::Ptr<cvn::PixelMap> > pixelMapsSl = fmPixelMap.at(iClust);
200  if(pixelMapsSl.empty()) continue;
201  std::vector<unsigned char> pmslice = (*pixelMapsSl[0]).PixelMapToVector(fUseGeV);
202 
203 
204  // --- 2D Prongs.
206  if( fmProng2D.isValid() ) {
207  // Get everything out of the event.
208  std::vector<art::Ptr<rb::Prong>> prongs2D = fmProng2D.at(iClust);
209  art::FindManyP<cvn::PixelMap> fmPixelMap2D(prongs2D, evt, fPixelMapInput);
210  if ( fmPixelMap2D.isValid() ) {
211 
212  for( unsigned int iProng = 0; iProng < prongs2D.size(); ++iProng ){
213  // Calculate result
214  std::vector<std::pair<int, double>> outputwithpdg;
215  std::vector<float> result;
216  if ( fmPixelMap2D.at(iProng).empty() ) continue;
217  CalcResult( fmPixelMap2D.at(iProng), fTF2D, fNOutput2D, outputwithpdg, result , pmslice );
218  // Ensure that result.size() equals fNOutput
219  assert( result.size() == fNOutput2D);
220  // Make Assn between result and this prong.
221  float resultvec[fNOutput2D];
222  for(unsigned int i = 0; i < fNOutput2D; i++){
223  resultvec[i] = result[i];
224  // Make Assn.
225  pidCol->emplace_back(outputwithpdg[i].first, outputwithpdg[i].second);
226  util::CreateAssn(*this, evt, *(pidCol.get()),
227  prongs2D[iProng], *(assocpid.get()), UINT_MAX);
228  }
229  // Make Assn between full result output and prong.
230  const float* resultoutput = resultvec;
231  resultCol->emplace_back(resultoutput, fNOutput2D);
232  util::CreateAssn(*this, evt, *(resultCol.get()),
233  slicelist[iClust], *(assocresultslice.get()), UINT_MAX);
234  util::CreateAssn(*this, evt, *(resultCol.get()),
235  prongs2D[iClust], *(assocresultprong.get()), UINT_MAX);
236  }
237  }
238  }
239 
240  // --- 3D Prongs.
242  std::vector<art::Ptr<rb::Prong>> prongs3D;
243  if( fmProng3D.isValid() ) {
244  // Get everything out of the event.
245  std::vector<art::Ptr<rb::Prong>> prongs3D = fmProng3D.at(iClust);
246  art::FindManyP<cvn::PixelMap> fmPixelMap3D(prongs3D, evt, fPixelMapInput);
247  if ( fmPixelMap3D.isValid() ) {
248  for( unsigned int iProng = 0; iProng < prongs3D.size(); ++iProng ){
249  // Calculate result
250  std::vector<std::pair<int, double>> outputwithpdg;
251  std::vector<float> result;
252  if ( fmPixelMap3D.at(iProng).empty() ) continue;
253  CalcResult( fmPixelMap3D.at(iProng), fTF3D, fNOutput3D, outputwithpdg, result , pmslice );
254  // Ensure that result.size() equals fNOutput
255  //assert( result.size() == fNOutput3D);
256  // Make Assn between result and this prong.
257  float resultvec[fNOutput3D];
258  for(unsigned int i = 0; i < result.size(); i++){
259  resultvec[i] = result[i];
260  // Make Assn.
261  pidCol->emplace_back(outputwithpdg[i].first, outputwithpdg[i].second);
262  util::CreateAssn(*this, evt, *(pidCol.get()),
263  prongs3D[iProng], *(assocpid.get()), UINT_MAX);
264  }
265  // Make Assn between full result output and prong.
266  const float* resultoutput = resultvec;
267  resultCol->emplace_back(resultoutput, fNOutput3D);
268  util::CreateAssn(*this, evt, *(resultCol.get()),
269  slicelist[iClust], *(assocresultslice.get()), UINT_MAX);
270  util::CreateAssn(*this, evt, *(resultCol.get()),
271  prongs3D[iClust], *(assocresultprong.get()), UINT_MAX);
272  }
273  }
274  }
275 
276  } // slices
277  evt.put(std::move(resultCol));
278  evt.put(std::move(assocresultslice));
279  evt.put(std::move(assocresultprong));
280  evt.put(std::move(pidCol));
281  evt.put(std::move(assocpid));
282  } // produce
283 
284  // --- Function to calculate the CVN inference.
285  // --- Need a function so can do both 2D and 3D evaluation.
286  void CVNNeutronProngTF::CalcResult( std::vector< art::Ptr<cvn::PixelMap> > pixelMaps, tensorflow::TFHandler* fTF, unsigned int NOutput,
287  std::vector<std::pair<int, double>> &outputwithpdg, std::vector<float> &result , std::vector<unsigned char> pmslc) {
288 
289  std::vector<unsigned char> pmprong = (*pixelMaps[0]).PixelMapToVector(fUseGeV);
290 
291  tensorflow::Tensor tensorslc;
292  tensorflow::Tensor tensor;
293  tensor = vector_to_tensor(pmprong);
294  tensorslc = vector_to_tensor(pmslc);
295 
296 
297  std::vector<tensorflow::Tensor> TFResult = fTF->Predict({{fInputNameSlc, tensorslc}, {fInputName, tensor}}, {fOutputName});
298  auto tfoutput = TFResult[0].tensor<float,2>();
299 
300 
301  for(unsigned int i = 0; i < NOutput; i++){
302  //Fill and create PID assn
303  if(i==0){
304  outputwithpdg.push_back(std::make_pair(2212, (double)tfoutput(0,i)));
305  }
306  else if (i==1){
307  outputwithpdg.push_back(std::make_pair(22 , (double)tfoutput(0,i)));
308  }
309  else if (i==2){
310  outputwithpdg.push_back(std::make_pair(0 , (double)tfoutput(0,i)));
311  }
312  //Fill Result column
313  result.push_back( (float)tfoutput(0,i) );
314  }
315 
316  return;
317  }
318 }
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
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
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
Result for CVN.
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
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:308
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
assert(nhit_max >=nhit_nbins)
Wrapper for Tensorflow which handles construction and prediction.
Definition: TFHandler.h:19
Definition: fwd.h:28
enum BeamMode string