CVNEventTF_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 
38 namespace cvneventtf{
39  class CVNEventTF : public art::EDProducer{
40  public:
41  explicit CVNEventTF(fhicl::ParameterSet const &pset);
42  virtual ~CVNEventTF();
43 
44  void produce(art::Event& evt);
45  bool IsRHC(const art::Event &evt);
46  tensorflow::Tensor vector_to_tensor(std::vector<unsigned char>);
48 
49  protected:
55  std::vector<std::string> fPreselectionLabels;
56  bool fUseGeV;
60  unsigned int fNOutput;
63  unsigned int fCPUlimit;
67  };
68 }
69 
70 namespace cvneventtf{
72  EDProducer(pset),
73  fSliceLabel (pset.get<std::string>("SliceLabel")),
74  fPixelMapInput(pset.get<std::string>("PixelMapInput")),
75  fGeneratorLabel (pset.get<std::string>("GeneratorLabel")),
76  fNuMILabel (pset.get<std::string>("NuMILabel")),
77  fObeyPreselection (pset.get<bool> ("ObeyPreselection" )),
78  fPreselectionLabels(pset.get<std::vector<std::string>> ("PreselectionLabels")),
79  fUseGeV (pset.get<bool> ("UseGeV")),
80  fLibPath (pset.get<std::string>("LibPath")),
81  fModelFHCName (pset.get<std::string>("ModelFHCName")),
82  fModelRHCName (pset.get<std::string>("ModelRHCName")),
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  fUseOppositeHornCurrentNetwork(pset.get<bool>("UseOppositeHornCurrentNetwork")),
88  fTFFHC(0),
89  fTFRHC(0)
90  {
92  produces< std::vector<cvn::Result> >();
93  produces< art::Assns<cvn::Result,rb::Cluster> >();
94  }
95 
97  {
98  if(fTFFHC) delete fTFFHC;
99  if(fTFRHC) delete fTFRHC;
100  }
101 
103  {
105  if (!evt.isRealData())
106  evt.getByLabel(fGeneratorLabel, spillPot);
107  else
108  evt.getByLabel(fNuMILabel, spillPot);
109 
110  if (spillPot.failedToGet())
111  {
112  mf::LogError("CVNEventTF") <<
113  "Spill Data not found, aborting without horn current information";
114  abort();
115  }
116 
117  if(fUseOppositeHornCurrentNetwork) return !spillPot->isRHC;
118 
119  // NB - the logic here will cause 0HC to use the FHC network
120  return spillPot->isRHC;
121  }
122 
124  {
125 
126  if (IsRHC(evt)) {
127  if (!fTFRHC)
129  return fTFRHC;
130  }
131  else {
132  if (!fTFFHC)
134  return fTFFHC;
135  }
136  }
137 
138  tensorflow::Tensor CVNEventTF::vector_to_tensor(std::vector<unsigned char> pm)
139  {
140  const unsigned int vectorSize = pm.size();
141 
142  // Initialize the tensors
143  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
144  auto rel = tensor.tensor<float,2>();
145 
146  // Loop over each element
147  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
148 
149  return tensor;
150  }
151 
153  {
154 
155  tensorflow::TFHandler* fTF = GetModel(evt);
156  //Containers for things we're gonna produce
157  std::unique_ptr< std::vector<cvn::Result> >
158  resultCol(new std::vector<cvn::Result>);
159  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
160  assocresult(new art::Assns<cvn::Result, rb::Cluster>);
161 
162  // Get slices
164  evt.getByLabel(fSliceLabel, slicecol);
165  art::PtrVector<rb::Cluster> slicelist;
166  for(unsigned int i = 0; i < slicecol->size(); ++i){
167  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
168  }
169 
170  // Get pixel maps
171  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
172 
173  //loop over slices
174  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
175  if(!fmPixelMap.isValid()) continue;
176  if(slicelist[iClust]->IsNoise()) continue;
177  if(fObeyPreselection && rb::IsFiltered(evt, slicecol, iClust, fPreselectionLabels)) continue;
178 
179  const std::vector<art::Ptr<cvn::PixelMap> > pixelMaps = fmPixelMap.at(iClust);
180  if(pixelMaps.empty()) continue;
181 
182  std::vector<unsigned char> pmslice = (*pixelMaps[0]).PixelMapToVector(fUseGeV);
183 
184  tensorflow::Tensor tensor = vector_to_tensor(pmslice);
185 
186  std::vector<tensorflow::Tensor> result = fTF->Predict({{fInputName,tensor}},
187  {fOutputName});
188  auto tfoutput = result[0].tensor<float,2>();
189 
190  float resultvec[fNOutput];
191 
192  for(unsigned int i = 0; i<fNOutput; i++){
193  resultvec[i] = (float)tfoutput(0,i);
194  }
195  const float* output = resultvec;
196 
197  resultCol->emplace_back(output, fNOutput);
198 
199  util::CreateAssn(evt, *(resultCol.get()),
200  slicelist[iClust], *(assocresult.get()), UINT_MAX);
201 
202  } // slices
203 
204  evt.put(std::move(resultCol));
205  evt.put(std::move(assocresult));
206 
207  } // produce
208 }
209 
bool isRHC
is the beam in antineutrino mode, aka RHC
Definition: SpillData.h:28
ofstream output
bool IsRHC(const art::Event &evt)
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.
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
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
DEFINE_ART_MODULE(TestTMapFile)
void produce(art::Event &evt)
PixelMap for CVN.
std::vector< Tensor > Predict(std::vector< std::pair< std::string, Tensor >> inputs, std::vector< std::string > outputLabels)
Definition: TFHandler.cxx:64
tensorflow::Tensor vector_to_tensor(std::vector< unsigned char >)
tensorflow::TFHandler * fTFFHC
bool isRealData() const
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
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
size_type size() const
Definition: PtrVector.h:302
tensorflow::TFHandler * GetModel(const art::Event &evt)
CVNEventTF(fhicl::ParameterSet const &pset)
std::vector< std::string > fPreselectionLabels
tensorflow::TFHandler * fTFRHC
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
bool failedToGet() const
Definition: Handle.h:190
enum BeamMode string