CMS 3D CMS Logo

OtherThingAnalyzer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include "DataFormats/TestObjects/interface/OtherThing.h"
3 #include "DataFormats/TestObjects/interface/OtherThingCollection.h"
12 
13 namespace edmtest {
14 
16  public:
17  explicit OtherThingAnalyzer(edm::ParameterSet const& pset);
18 
19  void analyze(edm::Event const& e, edm::EventSetup const& c) override;
20 
21  void doit(edm::Event const& event, std::string const& label);
22 
23  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
24 
25  private:
28  };
29 
31  : thingWasDropped_(pset.getUntrackedParameter<bool>("thingWasDropped")),
32  otherTag_(pset.getUntrackedParameter<edm::InputTag>("other")) {
33  consumes<OtherThingCollection>(otherTag_);
34  }
35 
36  void OtherThingAnalyzer::analyze(edm::Event const& e, edm::EventSetup const&) { doit(e, std::string("testUserTag")); }
37 
40  dv.getByLabel(otherTag_, otherThings);
41  edm::LogInfo("OtherThingAnalyzer") << " --------------- next event ------------ \n";
42  int i = 0;
43  for (OtherThingCollection::const_iterator it = otherThings->begin(), itEnd = otherThings->end(); it != itEnd;
44  ++it, ++i) {
45  OtherThing const& otherThing = *it;
46 
47  if (otherThing.oneNullOneNot.size() != 2) {
48  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
49  << " oneNullOneNot has wrong length: " << otherThing.oneNullOneNot.size() << " should be 2\n";
50  }
51  if (otherThing.oneNullOneNot[0].isNonnull()) {
52  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected null Ref is not null\n";
53  }
54  if (otherThing.oneNullOneNot[1].isNull()) {
55  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected non-null Ref is null\n";
56  }
57 
58  bool shouldBeTrue = otherThing.refVec[0] != otherThing.refVec[1];
59  if (!shouldBeTrue) {
60  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Inequality has incorrect value\n";
61  }
62  shouldBeTrue = otherThing.refVec[0] == otherThing.refVec[0];
63  if (!shouldBeTrue) {
64  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Equality has incorrect value\n";
65  }
66  shouldBeTrue = otherThing.refProd == otherThing.refProd;
67  if (!shouldBeTrue) {
68  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
69  << "RefProd Equality has incorrect value\n";
70  }
71  shouldBeTrue = otherThing.refVec[0].isNonnull();
72  if (!shouldBeTrue) {
73  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
74  << "Non-null check has incorrect value\n";
75  }
76  shouldBeTrue = otherThing.refProd.isNonnull();
77  if (!shouldBeTrue) {
78  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
79  << "RefProd non-null check has incorrect value\n";
80  }
81  shouldBeTrue = !(!otherThing.refVec[0]);
82  if (!shouldBeTrue) {
83  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "'!' has incorrect value\n";
84  }
85  shouldBeTrue = !(!otherThing.refProd);
86  if (!shouldBeTrue) {
87  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "RefProd '!' has incorrect value\n";
88  }
89  shouldBeTrue = !otherThing.refVec.empty();
90  if (!shouldBeTrue) {
91  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "empty() has incorrect value\n";
92  }
93  shouldBeTrue = (otherThing.refVec == otherThing.refVec);
94  if (!shouldBeTrue) {
95  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
96  << "RefVector equality has incorrect value\n";
97  }
98  shouldBeTrue = !(otherThing.refVec != otherThing.refVec);
99  if (!shouldBeTrue) {
100  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
101  << "RefVector inequality has incorrect value\n";
102  }
103  assert(otherThing.refProd.isAvailable() != thingWasDropped_);
104  assert(otherThing.ref.isAvailable() != thingWasDropped_);
105  assert(otherThing.refVec.isAvailable() != thingWasDropped_);
106 
107  if (otherThing.ptrOneNullOneNot.size() != 2) {
108  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
109  << " ptrOneNullOneNot has wrong length: " << otherThing.ptrOneNullOneNot.size() << " should be 2\n";
110  }
111  if (otherThing.ptrOneNullOneNot[0].isNonnull()) {
112  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected null Ptr is not null\n";
113  }
114  if (otherThing.ptrOneNullOneNot[1].isNull()) {
115  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected non-null Ptr is null\n";
116  }
117 
118  shouldBeTrue = otherThing.ptrVec[0] != otherThing.ptrVec[1];
119  if (!shouldBeTrue) {
120  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Inequality has incorrect value\n";
121  }
122  shouldBeTrue = otherThing.ptrVec[0] == otherThing.ptrVec[0];
123  if (!shouldBeTrue) {
124  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Equality has incorrect value\n";
125  }
126  shouldBeTrue = otherThing.ptrVec[0].isNonnull();
127  if (!shouldBeTrue) {
128  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
129  << "Non-null check has incorrect value\n";
130  }
131  shouldBeTrue = !(!otherThing.ptrVec[0]);
132  if (!shouldBeTrue) {
133  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "'!' has incorrect value\n";
134  }
135  shouldBeTrue = !otherThing.ptrVec.empty();
136  if (!shouldBeTrue) {
137  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "empty() has incorrect value\n";
138  }
139  shouldBeTrue = (otherThing.ptrVec == otherThing.ptrVec);
140  if (!shouldBeTrue) {
141  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
142  << "PtrVector equality has incorrect value\n";
143  }
144  assert(otherThing.ptr.isAvailable() != thingWasDropped_);
145  assert(otherThing.ptrVec.isAvailable() != thingWasDropped_);
146 
147  if (thingWasDropped_)
148  return;
149  {
150  ThingCollection const& tcoll = *otherThing.refProd;
151  ThingCollection::size_type size1 = tcoll.size();
152  ThingCollection::size_type size2 = otherThing.refProd->size();
153  if (size1 == 0 || size1 != size2) {
154  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
155  << " RefProd size mismatch " << std::endl;
156  }
157 
158  Thing const& tc = *otherThing.ref;
159  int const& x = otherThing.ref->a;
160  if (tc.a == i && x == i) {
161  edm::LogInfo("OtherThingAnalyzer")
162  << " ITEM " << i << " LABEL " << label << " dereferenced from edm::Ref successfully.\n";
163  } else {
164  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
165  << "ITEM " << i << " has incorrect edm::Ref value " << tc.a << '\n';
166  }
167 
168  edm::View<Thing> const& viewThing = *otherThing.refToBaseProd;
169  edm::View<Thing>::size_type const viewSize1 = viewThing.size();
170  edm::View<Thing>::size_type const viewSize2 = otherThing.refToBaseProd->size();
171  if (viewSize1 == 0 || viewSize2 != viewSize1) {
172  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
173  << " RefToBaseProd size mismatch " << std::endl;
174  }
175  if (viewSize1 != size1) {
176  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
177  << " RefToBaseProd size mismatch to RefProd size" << std::endl;
178  }
179  edm::Ref<ThingCollection> refFromCast = otherThing.refToBase.castTo<edm::Ref<ThingCollection> >();
180  edm::Ptr<Thing> ptrFromCast = otherThing.refToBase.castTo<edm::Ptr<Thing> >();
181  Thing const& tcBase = *otherThing.refToBase;
182  if (tcBase.a != refFromCast->a) {
183  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
184  << " Ref from RefToBase::castTo has incorrect value " << '\n';
185  }
186  if (tcBase.a != ptrFromCast->a) {
187  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
188  << " Ptr from RefToBase::castTo has incorrect value " << '\n';
189  }
190  int const& xBase = otherThing.refToBase->a;
191  if (tcBase.a == i && xBase == i) {
192  edm::LogInfo("OtherThingAnalyzer")
193  << " ITEM " << i << " LABEL " << label << " RefToBase dereferenced successfully.\n";
194  } else {
195  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
196  << "ITEM " << i << " RefToBase has incorrect value " << tc.a << '\n';
197  }
198 
199  Thing const& tcv = *otherThing.refVec[0];
200  int const& xv = otherThing.refVec[0]->a;
201  if (xv != tcv.a || xv != i) {
202  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
203  << "VECTOR ITEM 0 " << i << " has incorrect value " << tcv.a << '\n';
204  }
205  Thing const& tcv1 = *otherThing.refVec[1];
206  int const& xv1 = otherThing.refVec[1]->a;
207  if (xv1 != tcv1.a || xv1 != 19 - i) {
208  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
209  << "VECTOR ITEM 1 " << i << " has incorrect value " << tcv1.a << '\n';
210  }
211  for (edm::RefVector<ThingCollection>::iterator iterRefVector = otherThing.refVec.begin(),
212  iterRefVectorEnd = otherThing.refVec.end();
213  iterRefVector != iterRefVectorEnd;
214  ++iterRefVector) {
215  edm::Ref<ThingCollection> tcol = *iterRefVector;
216  Thing const& ti = **iterRefVector;
217  int const& xi = (*iterRefVector)->a;
218  if (xi != ti.a) {
219  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
220  << "iterator item " << ti.a << " " << xi << '\n';
221  } else if (iterRefVector == otherThing.refVec.begin() && xi != i) {
222  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 0" << xi << '\n';
223  } else if (iterRefVector != otherThing.refVec.begin() && xi != 19 - i) {
224  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 1" << xi << '\n';
225  }
226  }
227  edm::RefVector<ThingCollection>::iterator it0 = otherThing.refVec.begin();
228  int zero = (*it0)->a;
230  int one = (*it1)->a;
231  it1 = 1 + it0;
232  int x1 = (*it1)->a;
233  if (x1 != one)
234  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
235  << "operator+ iterator error: " << x1 << " " << one << '\n';
236  it0 = it1 - 1;
237  int x0 = (*it0)->a;
238  if (x0 != zero)
239  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
240  << "operator- iterator error: " << x0 << " " << zero << '\n';
241  x0 = (*(it0++))->a;
242  if (x0 != zero)
243  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
244  << "operator++ iterator error: " << x0 << " " << zero << '\n';
245  x1 = (*it0)->a;
246  if (x1 != one)
247  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
248  << "operator++ iterator error 2: " << x1 << " " << one << '\n';
249  x1 = (*(it0--))->a;
250  if (x1 != one)
251  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
252  << "operator-- iterator error: " << x1 << " " << one << '\n';
253  x0 = (*it0)->a;
254  if (x0 != zero)
255  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
256  << "operator-- iterator error 2: " << x0 << " " << zero << '\n';
257  x1 = it0[1]->a;
258  if (x1 != one)
259  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
260  << "operator[] iterator error: " << x1 << " " << one << '\n';
261  x1 = it1[0]->a;
262  if (x1 != one)
263  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
264  << "operator[] iterator error 2: " << x1 << " " << one << '\n';
265  }
266  {
267  Thing const& tc = *otherThing.ptr;
268  int const& x = otherThing.ptr->a;
269  if (tc.a == i && x == i) {
270  edm::LogInfo("OtherThingAnalyzer")
271  << " ITEM " << i << " LABEL " << label << " dereferenced from edm::Ptr successfully.\n";
272  } else {
273  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
274  << "ITEM " << i << " has incorrect edm::Ptr value " << tc.a << '\n';
275  }
276 
277  Thing const& tcv = *otherThing.ptrVec[0];
278  int const& xv = otherThing.ptrVec[0]->a;
279  if (xv != tcv.a || xv != i) {
280  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
281  << "VECTOR ITEM 0 " << i << " has incorrect value " << tcv.a << '\n';
282  }
283  Thing const& tcv1 = *otherThing.ptrVec[1];
284  int const& xv1 = otherThing.ptrVec[1]->a;
285  if (xv1 != tcv1.a || xv1 != 19 - i) {
286  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
287  << "VECTOR ITEM 1 " << i << " has incorrect value " << tcv1.a << '\n';
288  }
289  for (edm::PtrVector<Thing>::const_iterator iterPtrVec = otherThing.ptrVec.begin(),
290  iterPtrVecEnd = otherThing.ptrVec.end();
291  iterPtrVec != iterPtrVecEnd;
292  ++iterPtrVec) {
293  edm::Ptr<Thing> tcol = *iterPtrVec;
294  Thing const& ti = **iterPtrVec;
295  int const& xi = (*iterPtrVec)->a;
296  if (xi != ti.a) {
297  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
298  << "iterator item " << ti.a << " " << xi << '\n';
299  } else if (iterPtrVec == otherThing.ptrVec.begin() && xi != i) {
300  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 0" << xi << '\n';
301  } else if (iterPtrVec != otherThing.ptrVec.begin() && xi != 19 - i) {
302  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 1" << xi << '\n';
303  }
304  }
305  edm::PtrVector<Thing>::const_iterator it0 = otherThing.ptrVec.begin();
306  int zero = (*it0)->a;
308  int one = (*it1)->a;
309  it1 = it0 + 1;
310  int x1 = (*it1)->a;
311  if (x1 != one)
312  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
313  << "operator+ iterator error: " << x1 << " " << one << '\n';
314  it0 = it1 - 1;
315  int x0 = (*it0)->a;
316  if (x0 != zero)
317  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
318  << "operator- iterator error: " << x0 << " " << zero << '\n';
319  x0 = (*(it0++))->a;
320  if (x0 != zero)
321  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
322  << "operator++ iterator error: " << x0 << " " << zero << '\n';
323  x1 = (*it0)->a;
324  if (x1 != one)
325  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
326  << "operator++ iterator error 2: " << x1 << " " << one << '\n';
327  x1 = (*(it0--))->a;
328  if (x1 != one)
329  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
330  << "operator-- iterator error: " << x1 << " " << one << '\n';
331  x0 = (*it0)->a;
332  if (x0 != zero)
333  throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
334  << "operator-- iterator error 2: " << x0 << " " << zero << '\n';
335  }
336  }
337  }
338 
341  desc.addUntracked<bool>("thingWasDropped", false)
342  ->setComment(
343  "true if the ref to the ThingCollection in the OtherThingCollection can not be resolved since the "
344  "ThingCollection was not stored.");
345  desc.addUntracked<edm::InputTag>("other", edm::InputTag("OtherThing", "testUserTag"))
346  ->setComment("Where to get the OtherThingCollection");
347  descriptions.add("otherThingAnalyzer", desc);
348  }
349 
350 } // namespace edmtest
unsigned int size_type
Definition: View.h:90
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
assert(be >=bs)
uint16_t size_type
size_type size() const
char const * label
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
OtherThingAnalyzer(edm::ParameterSet const &pset)
Log< level::Info, false > LogInfo
void analyze(edm::Event const &e, edm::EventSetup const &c) override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
float x
void doit(edm::Event const &event, std::string const &label)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:498
Definition: event.py:1