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  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  fObeyPreselection (pset.get<bool> ("ObeyPreselection" )),
77  fPreselectionLabels(pset.get<std::vector<std::string>> ("PreselectionLabels")),
78  fUseGeV (pset.get<bool> ("UseGeV")),
79  fLibPath (pset.get<std::string>("LibPath")),
80  fModelFHCName (pset.get<std::string>("ModelFHCName")),
81  fModelRHCName (pset.get<std::string>("ModelRHCName")),
82  fNOutput (pset.get<unsigned int>("NOutput")),
83  fInputName (pset.get<std::string>("InputName")),
84  fOutputName (pset.get<std::string>("OutputName")),
85  fCPUlimit (pset.get<unsigned int>("CPUlimit")),
86  fUseOppositeHornCurrentNetwork(pset.get<bool>("UseOppositeHornCurrentNetwork")),
87  fTFFHC(0),
88  fTFRHC(0)
89  {
91  produces< std::vector<cvn::Result> >();
92  produces< art::Assns<cvn::Result,rb::Cluster> >();
93  }
94 
96  {
97  if(fTFFHC) delete fTFFHC;
98  if(fTFRHC) delete fTFRHC;
99  }
100 
102  {
104  if (!evt.isRealData())
105  evt.getByLabel(fGeneratorLabel, spillPot);
106  else
107  evt.getByLabel(fNuMILabel, spillPot);
108 
109  if (spillPot.failedToGet())
110  {
111  mf::LogError("CVNEventTF") <<
112  "Spill Data not found, aborting without horn current information";
113  abort();
114  }
115 
116  if(fUseOppositeHornCurrentNetwork) return !spillPot->isRHC;
117 
118  // NB - the logic here will cause 0HC to use the FHC network
119  return spillPot->isRHC;
120  }
121 
123  {
124 
125  if (IsRHC(evt)) {
126  if (!fTFRHC)
128  return fTFRHC;
129  }
130  else {
131  if (!fTFFHC)
133  return fTFFHC;
134  }
135  }
136 
137  tensorflow::Tensor CVNEventTF::vector_to_tensor(std::vector<unsigned char> pm)
138  {
139  const unsigned int vectorSize = pm.size();
140 
141  // Initialize the tensors
142  tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, vectorSize});
143  auto rel = tensor.tensor<float,2>();
144 
145  // Loop over each element
146  for(unsigned int i = 0; i < vectorSize; ++i) rel(0, i) = pm[i];
147 
148  return tensor;
149  }
150 
152  {
153 
154  tensorflow::TFHandler* fTF = GetModel(evt);
155  //Containers for things we're gonna produce
156  std::unique_ptr< std::vector<cvn::Result> >
157  resultCol(new std::vector<cvn::Result>);
158  std::unique_ptr< art::Assns<cvn::Result, rb::Cluster> >
159  assocresult(new art::Assns<cvn::Result, rb::Cluster>);
160 
161  // Get slices
163  evt.getByLabel(fSliceLabel, slicecol);
164  art::PtrVector<rb::Cluster> slicelist;
165  for(unsigned int i = 0; i < slicecol->size(); ++i){
166  slicelist.push_back(art::Ptr<rb::Cluster>(slicecol, i));
167  }
168 
169  // Get pixel maps
170  art::FindManyP<cvn::PixelMap> fmPixelMap(slicecol, evt, fPixelMapInput);
171 
172  //loop over slices
173  for(size_t iClust = 0; iClust < slicelist.size(); ++iClust) {
174  if(!fmPixelMap.isValid()) continue;
175  if(slicelist[iClust]->IsNoise()) continue;
176  if(fObeyPreselection && rb::IsFiltered(evt, slicecol, iClust, fPreselectionLabels)) continue;
177 
178  const std::vector<art::Ptr<cvn::PixelMap> > pixelMaps = fmPixelMap.at(iClust);
179  if(pixelMaps.empty()) continue;
180 
181  std::vector<unsigned char> pmslice = (*pixelMaps[0]).PixelMapToVector(fUseGeV);
182 
183  tensorflow::Tensor tensor = vector_to_tensor(pmslice);
184 
185  std::vector<tensorflow::Tensor> result = fTF->Predict({{fInputName,tensor}},
186  {fOutputName});
187  auto tfoutput = result[0].tensor<float,2>();
188 
189  float resultvec[fNOutput];
190 
191  for(unsigned int i = 0; i<fNOutput; i++){
192  resultvec[i] = (float)tfoutput(0,i);
193  }
194  const float* output = resultvec;
195 
196  resultCol->emplace_back(output, fNOutput);
197 
198  util::CreateAssn(*this, evt, *(resultCol.get()),
199  slicelist[iClust], *(assocresult.get()), UINT_MAX);
200 
201  } // slices
202 
203  evt.put(std::move(resultCol));
204  evt.put(std::move(assocresult));
205 
206  } // produce
207 }
208 
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
std::string EnvExpansion(const std::string &inString)
Function to expand environment variables.
Definition: EnvExpand.cxx:8
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool isRealData() const
Definition: Event.h:83
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
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
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:308
tensorflow::TFHandler * GetModel(const art::Event &evt)
CVNEventTF(fhicl::ParameterSet const &pset)
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::vector< std::string > fPreselectionLabels
tensorflow::TFHandler * fTFRHC
Wrapper for Tensorflow which handles construction and prediction.
Definition: TFHandler.h:19
bool failedToGet() const
Definition: Handle.h:196
enum BeamMode string