CVNProngTF_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 "SummaryData/SpillData.h"
21 #include "CVN/func/PixelMap.h"
22 #include "CVN/func/Result.h"
24 #include "CVN/func/AssignLabels.h"
25 #include "CVN/func/TrainingData.h"
28 #include "CVN/func/ProngType.h"
30 #include "Utilities/AssociationUtil.h"
32 
34 
35 #include <memory>
36 
37 namespace cvnprongtf{
38  class CVNProngTF : public art::EDProducer{
39  public:
40  explicit CVNProngTF(fhicl::ParameterSet const &pset);
41  virtual ~CVNProngTF();
42 
43  void produce(art::Event& evt);
44  bool IsRHC(const 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>);
48 
49  protected:
54  bool fUseGeV;
58  bool fFourView;
61  unsigned int fNOutput;
64  unsigned int fCPUlimit;
67  };
68 }
69 
70 namespace cvnprongtf{
72  fSliceLabel (pset.get<std::string>("SliceLabel")),
73  fPixelMapInput(pset.get<std::string>("PixelMapInput")),
74  fGeneratorLabel (pset.get<std::string>("GeneratorLabel")),
75  fNuMILabel (pset.get<std::string>("NuMILabel")),
76  fUseGeV (pset.get<bool> ("UseGeV")),
77  fLibPath (pset.get<std::string>("LibPath")),
78  fModelFHCName (pset.get<std::string>("ModelFHCName")),
79  fModelRHCName (pset.get<std::string>("ModelRHCName")),
80  fFourView (pset.get<bool> ("IsFourView")),
81  fProngModLabel(pset.get<std::string>("ProngModLabel")),
82  fProng3DLabel (pset.get<std::string>("Prong3DLabel")),
83  fNOutput (pset.get<unsigned int>("NOutput")),
84  fInputName (pset.get<std::string>("InputName")),
85  fOutputName (pset.get<std::string>("OutputName")),
86  fCPUlimit (pset.get<unsigned int>("CPUlimit")),
87  fTFFHC(0),
88  fTFRHC(0)
89  {
91  produces< std::vector<cvn::Result> >();
92  produces< art::Assns<cvn::Result, rb::Cluster> >();
93  produces< art::Assns<cvn::Result, rb::Prong> >();
94  produces< std::vector<rb::PID> >();
95  produces< art::Assns<rb::PID,rb::Prong> >();
96  }
97 
99  {
100  if(fTFFHC) delete fTFFHC;
101  if(fTFRHC) delete fTFRHC;
102  }
103 
105  {
107  if (!evt.isRealData())
108  evt.getByLabel(fGeneratorLabel, spillPot);
109  else
110  evt.getByLabel(fNuMILabel, spillPot);
111 
112  if (spillPot.failedToGet())
113  {
114  mf::LogError("CVNProngTF") <<
115  "Spill Data not found, aborting without horn current information";
116  abort();
117  }
118 
119  return spillPot->isRHC;
120  }
121 
123  {
124  if (IsRHC(evt)) {
125  if (!fTFRHC)
127  return fTFRHC;
128  }
129  else {
130  if (!fTFFHC)
132  return fTFFHC;
133  }
134  }
135 
136  tensorflow::Tensor CVNProngTF::vector_to_tensor(std::vector<unsigned char> pm)
137  {
138  const unsigned int vectorSize = pm.size();
139 
140  // Initialize the tensors
141  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
142  auto rel = tensor.tensor<float,2>();
143 
144  // Loop over each element
145  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
146 
147  return tensor;
148  }
149 
150  tensorflow::Tensor CVNProngTF::vector_to_tensor(std::vector<unsigned char> pmslice, std::vector<unsigned char> pmprong)
151  {
152  const unsigned int vectorSizeSlice = pmslice.size();
153  const unsigned int vectorSizeProng = pmprong.size();
154  const unsigned int vectorSizeTotal = vectorSizeSlice + vectorSizeProng;
155 
156  // Initialize the tensors
157  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSizeTotal});
158  auto rel = tensor.tensor<float,2>();
159 
160  // Loop over each element
161  for(unsigned int i = 0; i < vectorSizeSlice; ++i) rel(0, i) = pmslice[i];
162  for(unsigned int j = vectorSizeSlice; j < vectorSizeTotal; ++j) rel(0, j) = pmprong[j-vectorSizeSlice];
163 
164  return tensor;
165  }
166 
168  {
169 
170  tensorflow::TFHandler* fTF = GetModel(evt);
171  //Containers for things we're gonna produce
172  std::unique_ptr< std::vector<cvn::Result> >
173  resultCol(new std::vector<cvn::Result>);
174  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
175  assocresultslice(new art::Assns<cvn::Result, rb::Cluster>);
176  std::unique_ptr< art::Assns<cvn::Result, rb::Prong> >
177  assocresultprong(new art::Assns<cvn::Result, rb::Prong>);
178  std::unique_ptr< std::vector<rb::PID> >
179  pidCol(new std::vector<rb::PID>);
180  std::unique_ptr< art::Assns<rb::PID, rb::Prong> >
181  assocpid(new art::Assns<rb::PID, rb::Prong>);
182 
183  // Get slices
185  evt.getByLabel(fSliceLabel, slicecol);
186  art::PtrVector<rb::Cluster> slicelist;
187  for(unsigned int i = 0; i < slicecol->size(); ++i){
188  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
189  }
190 
191  // Get pixel maps
192  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
193 
194  //loop over slices
195  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
196  if(!fmPixelMap.isValid()) continue;
197  if(slicelist[iClust]->IsNoise()) continue;
198 
199  art::FindManyP<rb::Prong> fmProng3D(slicecol, evt,
201 
202  std::vector<art::Ptr<rb::Prong>> prongs3D;
203 
204  if( fmProng3D.isValid() ) prongs3D = fmProng3D.at(iClust);
205  else continue;
206 
207  art::FindManyP<cvn::PixelMap> fmPixelMap3D(prongs3D, evt, fPixelMapInput);
208 
209  const std::vector<art::Ptr<cvn::PixelMap> > pixelMaps = fmPixelMap.at(iClust);
210 
211  for( unsigned int iProng = 0; iProng < prongs3D.size(); ++iProng ){
212  if(!fmPixelMap3D.isValid()) continue;
213 
214  const std::vector< art::Ptr<cvn::PixelMap> > pixelMaps3D = fmPixelMap3D.at(iProng);
215 
216  if(pixelMaps3D.empty()) continue;
217 
218  std::vector<unsigned char> pmprong = (*pixelMaps3D[0]).PixelMapToVector(fUseGeV);
219 
220  tensorflow::Tensor tensor;
221 
222  if(fFourView){
223 
224  //make sure slice pixelmap is filled
225  if(pixelMaps.empty()) continue;
226 
227  // Fill the pixel map array for this slice
228  std::vector<unsigned char> pmslice = (*pixelMaps[0]).PixelMapToVector(fUseGeV);
229 
230  // Convert to format expected by the network
231  tensor = vector_to_tensor(pmslice,pmprong);
232  } //4View Results Fill
233  else{
234 
235  tensor = vector_to_tensor(pmprong);
236 
237  } //2View Results Fill
238 
239  std::vector<tensorflow::Tensor> result = fTF->Predict({{fInputName,tensor}},
240  {fOutputName});
241 
242  auto tfoutput = result[0].tensor<float,2>();
243 
244  std::vector<std::pair<int, double>> outputwithpdg;
245 
246  float resultvec[fNOutput];
247 
248  for(unsigned int i = 0; i < fNOutput; i++){
249 
250  //Fill and create PID assn
251  outputwithpdg.push_back(std::make_pair(GetPDGByPType((cvn::PType)i), (double)tfoutput(0,i)));
252 
253  pidCol->emplace_back(outputwithpdg[i].first, outputwithpdg[i].second);
254 
255  util::CreateAssn(*this, evt, *(pidCol.get()),
256  prongs3D[iProng], *(assocpid.get()), UINT_MAX);
257 
258  //Fill Result column
259  resultvec[i] = (float)tfoutput(0,i);
260 
261  }
262 
263  const float* resultoutput = resultvec;
264 
265  resultCol->emplace_back(resultoutput, fNOutput);
266 
267  util::CreateAssn(*this, evt, *(resultCol.get()),
268  slicelist[iClust], *(assocresultslice.get()), UINT_MAX);
269 
270  util::CreateAssn(*this, evt, *(resultCol.get()),
271  prongs3D[iClust], *(assocresultprong.get()), UINT_MAX);
272 
273  }// prongs
274  } // slices
275 
276  evt.put(std::move(resultCol));
277  evt.put(std::move(assocresultslice));
278  evt.put(std::move(assocresultprong));
279  evt.put(std::move(pidCol));
280  evt.put(std::move(assocpid));
281 
282  } // produce
283 }
284 
bool isRHC
is the beam in antineutrino mode, aka RHC
Definition: SpillData.h:28
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 * fTFFHC
pdg code and pid value
tensorflow::TFHandler * fTFRHC
std::string EnvExpansion(const std::string &inString)
Function to expand environment variables.
Definition: EnvExpand.cxx:8
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
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
Result for CVN.
CVNProngTF(fhicl::ParameterSet const &pset)
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
tensorflow::TFHandler * GetModel(const art::Event &evt)
const double j
Definition: BetheBloch.cxx:29
string rel
Definition: shutoffs.py:11
void produce(art::Event &evt)
int GetPDGByPType(PType ptype)
Definition: ProngType.cxx:5
size_type size() const
Definition: PtrVector.h:308
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool IsRHC(const art::Event &evt)
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
PType
Definition: ProngType.h:18
tensorflow::Tensor vector_to_tensor(std::vector< unsigned char >)
Wrapper for Tensorflow which handles construction and prediction.
Definition: TFHandler.h:19
bool failedToGet() const
Definition: Handle.h:196