All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MVAReader.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 // \version
3 //
4 // \brief Wrappers for accessing MVA results and associated data products
5 //
6 // \author robert.sulej@cern.ch
7 //
8 //////////////////////////////////////////////////////////////////////////////
9 #ifndef ANAB_MVAREADER_H
10 #define ANAB_MVAREADER_H
11 
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "canvas/Utilities/InputTag.h"
15 
17 
18 namespace anab {
19 
20 /// Helper for reading the reconstructed objects of type T together with associated
21 /// N-ellement feature vectors with their metadata (this class is not a data product).
22 template <class T, size_t N>
24 public:
25 
26  /// Create the helper for feature vectors stored in the event evt with the provided input tag
27  /// (the same tag which was used to save vectors with FVectorWriter class).
28  /// Returns nullptr if data products not found in the event.
29  static std::unique_ptr<FVectorReader> create(const art::Event & evt, const art::InputTag & tag)
30  {
31  bool success;
32  std::unique_ptr<FVectorReader> ptr(new FVectorReader(evt, tag, success));
33  if (success) { return ptr; }
34  else { return nullptr; }
35  }
36 
37  /// Create the wrapper for feature vectors stored in the event evt with the provided input tag
38  /// (the same tag which was used to save vectors with FVectorWriter class).
39  /// Throws exception if data products not found in the event.
40  FVectorReader(const art::Event & evt, const art::InputTag & tag);
41 
42  /// Access data product at index "key".
43  T const & item(size_t key) const { return (*fDataHandle)[key]; }
44  std::vector<T> const & items() const { return *fDataHandle; }
45 
46  /// Access the vector of the feature vectors.
47  std::vector< FeatureVector<N> > const & vectors() const { return *fVectors; }
48 
49  /// Access feature vector data at index "key".
50  /// *** WOULD LIKE TO CHANGE TYPE OF FVEC DATA MEMBER TO std::array AND THEN ENABLE THIS FUNCTION ***
51  //const std::array<float, N> & getVector(size_t key) const { return (*fVectors)[key].data(); }
52 
53  /// Get copy of the feature vector at index "key".
54  std::array<float, N> getVector(size_t key) const
55  {
56  std::array<float, N> vout;
57  for (size_t i = 0; i < N; ++i) vout[i] = (*fVectors)[key][i];
58  return vout;
59  }
60 
61  /// Get copy of the feature vector idicated with art::Ptr::key().
62  std::array<float, N> getVector(art::Ptr<T> const & item) const
63  { return getVector(item.key()); }
64 
65 
66  /// Get the number of contained items (no. of data product objects equal to no. of feature vectors).
67  size_t size() const { return fVectors->size(); }
68 
69  /// Get the length of a single feature vector.
70  size_t length() const { return N; }
71 
72  /// Get the input tag (string representation) of data product used to calculate feature vectors.
73  const std::string & dataTag() const { return fDescription->dataTag(); }
74 
75  /// Access the data product handle.
76  const art::Handle< std::vector<T> > & dataHandle() const { return fDataHandle; }
77 
78  /// Meaning/name of the index'th column in the collection of feature vectors.
79  const std::string & columnName(size_t index) const { return fDescription->outputName(index); }
80 
81  /// Index of column with given name, or -1 if name not found.
82  int getIndex(const std::string & name) const { return fDescription->getIndex(name); }
83 
84  friend std::ostream& operator<< (std::ostream &o, FVectorReader const& a)
85  {
86  o << "FVectorReader:" << std::endl << *(a.fDescription) << std::endl;
87  return o;
88  }
89 
90 protected:
91  /// Not-throwing constructor.
92  FVectorReader(const art::Event & evt, const art::InputTag & tag, bool & success);
93 
94 private:
96  std::vector< FeatureVector<N> > const * fVectors;
97  art::Handle< std::vector<T> > fDataHandle;
98 
99 };
100 
101 /// Helper for reading the reconstructed objects of type T together with associated
102 /// N-outputs MVA results with their metadata (this class is not a data product).
103 template <class T, size_t N>
104 class MVAReader : public FVectorReader<T, N>, public MVAWrapperBase {
105 public:
106 
107  /// Create the wrapper for MVA data stored in the event evt with the provided input tag
108  /// (the same tag which was used to save MVA results with MVAWriter class).
109  /// Returns nullptr if data products not found in the event.
110  static std::unique_ptr<MVAReader> create(const art::Event & evt, const art::InputTag & tag)
111  {
112  bool success;
113  std::unique_ptr<MVAReader> ptr(new MVAReader(evt, tag, success));
114  if (success) { return ptr; }
115  else { return nullptr; }
116  }
117 
118  /// Create the wrapper for MVA data stored in the event evt with the provided input tag
119  /// (the same tag which was used to save MVA results with MVAWriter class).
120  /// Throws exception if data products not found in the event.
121  MVAReader(const art::Event & evt, const art::InputTag & tag) :
122  FVectorReader<T, N>(evt, tag)
123  { }
124 
125  /// Access the vector of the feature vectors.
126  std::vector< FeatureVector<N> > const & outputs() const { return FVectorReader<T, N>::vectors(); }
127 
128  /// Get copy of the MVA output vector at index "key".
129  std::array<float, N> getOutput(size_t key) const
130  { return FVectorReader<T, N>::getVector(key); }
131 
132  /// Get copy of the MVA output vector idicated with art::Ptr::key().
133  std::array<float, N> getOutput(art::Ptr<T> const & item) const
134  { return FVectorReader<T, N>::getVector(item.key()); }
135 
136  /// Get MVA results accumulated over the vector of items (eg. over hits associated to a cluster).
137  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items) const
139 
140  /// Get MVA results accumulated with provided weights over the vector of items
141  /// (eg. over clusters associated to a track, weighted by the cluster size; or
142  /// over hits associated to a cluster, weighted by the hit area).
143  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
144  std::vector<float> const & weights) const
145  { return pAccumulate(items, weights, FVectorReader<T, N>::vectors()); }
146 
147  /// Get MVA results accumulated with provided weighting function over the vector
148  /// of items (eg. over clusters associated to a track, weighted by the cluster size;
149  /// or over hits associated to a cluster, weighted by the hit area).
150  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
151  std::function<float (T const &)> fweight) const
152  { return pAccumulate(items, fweight, FVectorReader<T, N>::vectors()); }
153 
154  /// Meaning/name of the index'th column in the collection of MVA output vectors.
155  const std::string & outputName(size_t index) const { return FVectorReader<T, N>::columnName(index); }
156 
157 private:
158  /// Not-throwing constructor.
159  MVAReader(const art::Event & evt, const art::InputTag & tag, bool & success) :
160  FVectorReader<T, N>(evt, tag, success)
161  { }
162 };
163 
164 } // namespace anab
165 
166 //----------------------------------------------------------------------------
167 // FVectorReader functions.
168 //
169 template <class T, size_t N>
170 anab::FVectorReader<T, N>::FVectorReader(const art::Event & evt, const art::InputTag & tag) :
171  fDescription(0)
172 {
173  if (!N) { throw cet::exception("FVectorReader") << "Vector size should be > 0." << std::endl; }
174 
175  auto descriptionHandle = evt.getValidHandle< std::vector< anab::FVecDescription<N> > >(tag);
176 
177  // search for FVecDescription<N> produced for the type T, with the instance name from the tag
178  std::string outputInstanceName = tag.instance() + getProductName(typeid(T));
179  for (auto const & dscr : *descriptionHandle)
180  {
181  if (dscr.outputInstance() == outputInstanceName)
182  {
183  fDescription = &dscr; break;
184  }
185  }
186  if (!fDescription) { throw cet::exception("FVectorReader") << "Vectors description not found for " << outputInstanceName << std::endl; }
187 
188  fVectors = &*(evt.getValidHandle< std::vector< FeatureVector<N> > >( art::InputTag(tag.label(), fDescription->outputInstance(), tag.process()) ));
189 
190  if (!evt.getByLabel( fDescription->dataTag(), fDataHandle ))
191  {
192  throw cet::exception("FVectorReader") << "Associated data product handle failed: " << *(fDataHandle.whyFailed()) << std::endl;
193  }
194 
195  if (fVectors->size() != fDataHandle->size())
196  {
197  throw cet::exception("FVectorReader") << "Feature vectors and data products sizes inconsistent: " << fVectors->size() << "!=" << fDataHandle->size() << std::endl;
198  }
199 }
200 //----------------------------------------------------------------------------
201 
202 template <class T, size_t N>
203 anab::FVectorReader<T, N>::FVectorReader(const art::Event & evt, const art::InputTag & tag, bool & success) :
204  fDescription(0)
205 {
206  success = false; // until all is done correctly
207 
208  if (!N) { std::cout << "FVectorReader: Vector size should be > 0." << std::endl; return; }
209 
210  art::Handle< std::vector< anab::FVecDescription<N> > > descriptionHandle;
211  if (!evt.getByLabel( tag, descriptionHandle )) { return; }
212 
213  // search for FVecDescription<N> produced for the type T, with the instance name from the tag
214  std::string outputInstanceName = tag.instance() + getProductName(typeid(T));
215  for (auto const & dscr : *descriptionHandle)
216  {
217  if (dscr.outputInstance() == outputInstanceName)
218  {
219  fDescription = &dscr; break;
220  }
221  }
222  if (!fDescription) { std::cout << "FVectorReader: Vectors description not found for " << outputInstanceName << std::endl; return; }
223 
224  fVectors = &*(evt.getValidHandle< std::vector< FeatureVector<N> > >( art::InputTag(tag.label(), fDescription->outputInstance(), tag.process()) ));
225 
226  if (!evt.getByLabel( fDescription->dataTag(), fDataHandle ))
227  {
228  std::cout << "FVectorReader: Associated data product handle failed: " << *(fDataHandle.whyFailed()) << std::endl; return;
229  }
230 
231  if (fVectors->size() != fDataHandle->size())
232  {
233  std::cout << "FVectorReader: Feature vectors and data products sizes inconsistent: " << fVectors->size() << "!=" << fDataHandle->size() << std::endl; return;
234  }
235 
236  success = true; // ok, all data found in the event
237 }
238 //----------------------------------------------------------------------------
239 
240 #endif //ANAB_MVAREADER
241 
const art::Handle< std::vector< T > > & dataHandle() const
Access the data product handle.
Definition: MVAReader.h:76
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector at index &quot;key&quot;.
Definition: MVAReader.h:54
static std::unique_ptr< FVectorReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:29
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::function< float(T const &)> fweight) const
Definition: MVAReader.h:150
int getIndex(const std::string &name) const
Index of column with given name, or -1 if name not found.
Definition: MVAReader.h:82
const std::string & outputName(size_t index) const
Meaning/name of the index&#39;th column in the collection of MVA output vectors.
Definition: MVAReader.h:155
std::vector< FeatureVector< N > > const & vectors() const
Access the vector of the feature vectors.
Definition: MVAReader.h:47
T const & item(size_t key) const
Access data product at index &quot;key&quot;.
Definition: MVAReader.h:43
std::string getProductName(std::type_info const &ti) const
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
std::array< float, N > getOutput(art::Ptr< T > const &item) const
Get copy of the MVA output vector idicated with art::Ptr::key().
Definition: MVAReader.h:133
process_name gaushit a
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
MVAReader(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:121
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
std::vector< T > const & items() const
Definition: MVAReader.h:44
std::array< float, N > getVector(art::Ptr< T > const &item) const
Get copy of the feature vector idicated with art::Ptr::key().
Definition: MVAReader.h:62
Helper functions for MVAReader and MVAWriter wrappers.
const std::string & columnName(size_t index) const
Meaning/name of the index&#39;th column in the collection of feature vectors.
Definition: MVAReader.h:79
std::vector< FeatureVector< N > > const & outputs() const
Access the vector of the feature vectors.
Definition: MVAReader.h:126
size_t size() const
Get the number of contained items (no. of data product objects equal to no. of feature vectors)...
Definition: MVAReader.h:67
size_t length() const
Get the length of a single feature vector.
Definition: MVAReader.h:70
FVectorReader(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:170
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
process_name largeant stream1 can override from command line with o or output physics producers generator N
then echo fcl name
std::array< float, N > pAccumulate(std::vector< art::Ptr< T > > const &items, std::vector< FeatureVector< N > > const &outs) const
friend std::ostream & operator<<(std::ostream &o, FVectorReader const &a)
Definition: MVAReader.h:84
TCEvent evt
Definition: DataStructs.cxx:8
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items) const
Get MVA results accumulated over the vector of items (eg. over hits associated to a cluster)...
Definition: MVAReader.h:137
std::array< float, N > getOutput(size_t key) const
Get copy of the MVA output vector at index &quot;key&quot;.
Definition: MVAReader.h:129
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::vector< float > const &weights) const
Definition: MVAReader.h:143
static std::unique_ptr< MVAReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:110
BEGIN_PROLOG could also be cout
MVAReader(const art::Event &evt, const art::InputTag &tag, bool &success)
Not-throwing constructor.
Definition: MVAReader.h:159
const std::string & dataTag() const
Get the input tag (string representation) of data product used to calculate feature vectors...
Definition: MVAReader.h:73