All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AssnAnalyzer_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: AssnAnalyzer
3 // Plugin Type: analyzer (art v2_05_00)
4 // File: AssnAnalyzer_module.cc
5 //
6 // Generated at Fri Dec 9 00:12:59 2016 by Saba Sehrish using cetskelgen
7 // from cetlib version v1_21_00.
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "art/Framework/Core/EDAnalyzer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Principal/Run.h"
15 #include "art/Framework/Principal/SubRun.h"
16 #include "canvas/Persistency/Common/AssnsAlgorithms.h" // art::for_each_group()
17 #include "canvas/Persistency/Common/Assns.h"
18 #include "canvas/Persistency/Common/Ptr.h"
19 #include "canvas/Utilities/InputTag.h"
20 #include "fhiclcpp/ParameterSet.h"
21 
23 
24 #include <set>
25 #include <algorithm> // std::copy()
26 #include <iterator> // std::inserter()
27 
28 class AssnAnalyzer;
29 
30 class AssnAnalyzer : public art::EDAnalyzer {
31 public:
32  typedef std::vector<int> intvec_t;
33  typedef std::vector<std::string> strvec_t;
34  typedef art::Assns<std::string, int> strintAssns_t;
35 
36  explicit AssnAnalyzer(fhicl::ParameterSet const & p);
37  // The compiler-generated destructor is fine for non-base
38  // classes without bare pointers or other resource use.
39 
40  // Plugins should not be copied or assigned.
41  AssnAnalyzer(AssnAnalyzer const &) = delete;
42  AssnAnalyzer(AssnAnalyzer &&) = delete;
43  AssnAnalyzer & operator = (AssnAnalyzer const &) = delete;
44  AssnAnalyzer & operator = (AssnAnalyzer &&) = delete;
45 
46  // Required functions.
47  void analyze(art::Event const & e) override;
48 
49 private:
50  art::InputTag fInputLabel;
51 
52  std::set<std::string> fEnabledTests;
53 
54  void for_each_associated_group_test(art::Event const & e) const;
55  void associated_groups_test(art::Event const & e) const;
56  void associated_groups_with_left_test(art::Event const & e) const;
57 
58 };
59 
60 
61 namespace {
62 
63  bool starts_with(std::string const& s, std::string const& key)
64  { return s.substr(0, key.length()) == key; }
65 
66 } // local namespace
67 
68 
69 AssnAnalyzer::AssnAnalyzer(fhicl::ParameterSet const & p)
70  :
71  EDAnalyzer(p)
72  , fInputLabel(p.get<art::InputTag>("input_label"))
73 {
74  auto enableTests = p.get<std::vector<std::string>>("enableTests");
75  if (enableTests.empty()) {
76  fEnabledTests = {
77  "forEachAssociatedGroup", "associatedGroups", "associatedGroupsWithLeft"
78  };
79  }
80  else {
81  std::copy(enableTests.begin(), enableTests.end(),
82  std::inserter(fEnabledTests, fEnabledTests.begin()));
83  }
84 }
85 
86 
87 void AssnAnalyzer::analyze(art::Event const & e)
88 {
89  if (fEnabledTests.count("forEachAssociatedGroup"))
91  if (fEnabledTests.count("associatedGroups"))
93  if (fEnabledTests.count("associatedGroupsWithLeft"))
95 }
96 
97 void AssnAnalyzer::for_each_associated_group_test(art::Event const & e) const
98 {
99  typedef typename art::Assns<int, std::string> istr_assns;
100  auto const & int_to_str_assns = *e.getValidHandle<istr_assns> (fInputLabel);
101  auto vs = strvec_t {"one", "one-a", "two", "two-a", "three", "three-a"};
102 
103  strvec_t strvec;
104  auto strings = [&strvec](auto strs) {
105  for(auto s=begin(strs); s!=end(strs); ++s) {
106  std::cout << *s << std::flush << " \"" << **s << "\"" << std::endl;
107  strvec.push_back(**s);
108  }
109  };
110 
111  art::for_each_group(int_to_str_assns, strings);
112 
113  //strings should be same as vs
114  for(auto k=0; k<6;++k) {
115  if (strvec[k] != vs[k]) {
116  throw art::Exception(art::errors::LogicError)
117  << "String #" << k << " expected to be '" << vs[k]
118  << "', got '" << strvec[k] << "' instead!\n";
119  }
120  }
121 
122 } // for_each_associated_group_test()
123 
124 
125 void AssnAnalyzer::associated_groups_test(art::Event const & e) const
126 {
127  // this is the exact same test as for_each_associated_group_test(),
128  // but written with an explicit loop
129 
130  typedef typename art::Assns<int, std::string> istr_assns;
131  auto const & int_to_str_assns = *e.getValidHandle<istr_assns> (fInputLabel);
132  auto vs = strvec_t {"one", "one-a", "two", "two-a", "three", "three-a"};
133 
134  strvec_t strvec;
135  for (auto strs: util::associated_groups(int_to_str_assns)) {
136  for(art::Ptr<std::string> const& s: strs) {
137  std::cout << s << std::flush << " \"" << *s << "\"" << std::endl;
138  strvec.push_back(*s);
139  }
140  } // for associated groups
141 
142  //strings should be same as vs
143  for(auto k=0; k<6;++k) {
144  if (strvec[k] != vs[k]) {
145  throw art::Exception(art::errors::LogicError)
146  << "String #" << k << " expected to be '" << vs[k]
147  << "', got '" << strvec[k] << "' instead!\n";
148  }
149  }
150 
151 } // associated_groups_test()
152 
153 
154 void AssnAnalyzer::associated_groups_with_left_test(art::Event const & e) const
155 {
156  // this is the exact same test as associated_groups_test(),
157  // but passing around also the key
158 
159  typedef typename art::Assns<int, std::string> istr_assns;
160  auto const & int_to_str_assns = *e.getValidHandle<istr_assns> (fInputLabel);
161  std::vector<std::pair<int, std::string>> vs = {
162  { 1, "one" },
163  { 1, "one-a" },
164  { 2, "two" },
165  { 2, "two-a" },
166  { 3, "three" },
167  { 3, "three-a" }
168  };
169 
170  std::vector<std::pair<int, std::string>> strvec;
171  for (auto const& group: util::associated_groups_with_left(int_to_str_assns))
172  {
173  // user code here:
174  auto const& key = std::get<0>(group); // group.first also ok
175  auto const& strs = std::get<1>(group); // group.second also ok
176 
177  std::cout << "#" << (*key) << " (" << key << ")" << std::endl;
178  for(art::Ptr<std::string> const& s: strs) {
179  std::cout << " - " << s << " \"" << *s << "\"" << std::endl;
180  strvec.emplace_back(*key, *s);
181  }
182  } // for associated groups
183 
184  //strings should be same as vs
185  for(auto k=0; k<6;++k) {
186  std::string const& s = strvec[k].second;
187  int key = 0; // (unknown)
188  if (starts_with(s, "one" )) key = 1;
189  else if (starts_with(s, "two" )) key = 2;
190  else if (starts_with(s, "three")) key = 3;
191 
192  if (key != vs[k].first) {
193  throw art::Exception(art::errors::LogicError)
194  << "String #" << k << " expected to have key '" << vs[k].first
195  << "', got '" << key << "' instead!\n";
196  }
197 
198  if (s != vs[k].second) {
199  throw art::Exception(art::errors::LogicError)
200  << "String #" << k << " expected to be '" << vs[k].second
201  << "', got '" << s << "' instead!\n";
202  }
203 
204  }
205 
206 } // associated_groups_test()
207 
208 
209 DEFINE_ART_MODULE(AssnAnalyzer)
auto associated_groups_with_left(A const &assns)
Helper functions to access associations in order, also with key.
std::vector< std::string > strvec_t
pdgs p
Definition: selectors.fcl:22
void associated_groups_test(art::Event const &e) const
art::Assns< std::string, int > strintAssns_t
AssnAnalyzer & operator=(AssnAnalyzer const &)=delete
std::set< std::string > fEnabledTests
auto associated_groups(A const &assns)
Helper functions to access associations in order.
AssnAnalyzer(fhicl::ParameterSet const &p)
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
void associated_groups_with_left_test(art::Event const &e) const
void analyze(art::Event const &e) override
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
Helper functions to access associations in order.
do i e
std::vector< int > intvec_t
T copy(T const &v)
void for_each_associated_group_test(art::Event const &e) const
art::InputTag fInputLabel
pdgs k
Definition: selectors.fcl:22
BEGIN_PROLOG could also be cout