File indexing completed on 2024-04-06 12:12:34
0001 #include <iostream>
0002 #include "DataFormats/TestObjects/interface/OtherThing.h"
0003 #include "DataFormats/TestObjects/interface/OtherThingCollection.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "DataFormats/Common/interface/Handle.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/Framework/interface/Frameworkfwd.h"
0010 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012
0013 namespace edmtest {
0014
0015 class OtherThingAnalyzer : public edm::stream::EDAnalyzer<> {
0016 public:
0017 explicit OtherThingAnalyzer(edm::ParameterSet const& pset);
0018
0019 void analyze(edm::Event const& e, edm::EventSetup const& c) override;
0020
0021 void doit(edm::Event const& event, std::string const& label);
0022
0023 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0024
0025 private:
0026 bool thingWasDropped_;
0027 edm::InputTag otherTag_;
0028 };
0029
0030 OtherThingAnalyzer::OtherThingAnalyzer(edm::ParameterSet const& pset)
0031 : thingWasDropped_(pset.getUntrackedParameter<bool>("thingWasDropped")),
0032 otherTag_(pset.getUntrackedParameter<edm::InputTag>("other")) {
0033 consumes<OtherThingCollection>(otherTag_);
0034 }
0035
0036 void OtherThingAnalyzer::analyze(edm::Event const& e, edm::EventSetup const&) { doit(e, std::string("testUserTag")); }
0037
0038 void OtherThingAnalyzer::doit(edm::Event const& dv, std::string const& label) {
0039 edm::Handle<OtherThingCollection> otherThings;
0040 dv.getByLabel(otherTag_, otherThings);
0041 edm::LogInfo("OtherThingAnalyzer") << " --------------- next event ------------ \n";
0042 int i = 0;
0043 for (OtherThingCollection::const_iterator it = otherThings->begin(), itEnd = otherThings->end(); it != itEnd;
0044 ++it, ++i) {
0045 OtherThing const& otherThing = *it;
0046
0047 if (otherThing.oneNullOneNot.size() != 2) {
0048 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0049 << " oneNullOneNot has wrong length: " << otherThing.oneNullOneNot.size() << " should be 2\n";
0050 }
0051 if (otherThing.oneNullOneNot[0].isNonnull()) {
0052 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected null Ref is not null\n";
0053 }
0054 if (otherThing.oneNullOneNot[1].isNull()) {
0055 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected non-null Ref is null\n";
0056 }
0057
0058 bool shouldBeTrue = otherThing.refVec[0] != otherThing.refVec[1];
0059 if (!shouldBeTrue) {
0060 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Inequality has incorrect value\n";
0061 }
0062 shouldBeTrue = otherThing.refVec[0] == otherThing.refVec[0];
0063 if (!shouldBeTrue) {
0064 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Equality has incorrect value\n";
0065 }
0066 shouldBeTrue = otherThing.refProd == otherThing.refProd;
0067 if (!shouldBeTrue) {
0068 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0069 << "RefProd Equality has incorrect value\n";
0070 }
0071 shouldBeTrue = otherThing.refVec[0].isNonnull();
0072 if (!shouldBeTrue) {
0073 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0074 << "Non-null check has incorrect value\n";
0075 }
0076 shouldBeTrue = otherThing.refProd.isNonnull();
0077 if (!shouldBeTrue) {
0078 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0079 << "RefProd non-null check has incorrect value\n";
0080 }
0081 shouldBeTrue = !(!otherThing.refVec[0]);
0082 if (!shouldBeTrue) {
0083 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "'!' has incorrect value\n";
0084 }
0085 shouldBeTrue = !(!otherThing.refProd);
0086 if (!shouldBeTrue) {
0087 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "RefProd '!' has incorrect value\n";
0088 }
0089 shouldBeTrue = !otherThing.refVec.empty();
0090 if (!shouldBeTrue) {
0091 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "empty() has incorrect value\n";
0092 }
0093 shouldBeTrue = (otherThing.refVec == otherThing.refVec);
0094 if (!shouldBeTrue) {
0095 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0096 << "RefVector equality has incorrect value\n";
0097 }
0098 shouldBeTrue = !(otherThing.refVec != otherThing.refVec);
0099 if (!shouldBeTrue) {
0100 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0101 << "RefVector inequality has incorrect value\n";
0102 }
0103 assert(otherThing.refProd.isAvailable() != thingWasDropped_);
0104 assert(otherThing.ref.isAvailable() != thingWasDropped_);
0105 assert(otherThing.refVec.isAvailable() != thingWasDropped_);
0106
0107 if (otherThing.ptrOneNullOneNot.size() != 2) {
0108 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0109 << " ptrOneNullOneNot has wrong length: " << otherThing.ptrOneNullOneNot.size() << " should be 2\n";
0110 }
0111 if (otherThing.ptrOneNullOneNot[0].isNonnull()) {
0112 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected null Ptr is not null\n";
0113 }
0114 if (otherThing.ptrOneNullOneNot[1].isNull()) {
0115 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << " expected non-null Ptr is null\n";
0116 }
0117
0118 shouldBeTrue = otherThing.ptrVec[0] != otherThing.ptrVec[1];
0119 if (!shouldBeTrue) {
0120 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Inequality has incorrect value\n";
0121 }
0122 shouldBeTrue = otherThing.ptrVec[0] == otherThing.ptrVec[0];
0123 if (!shouldBeTrue) {
0124 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "Equality has incorrect value\n";
0125 }
0126 shouldBeTrue = otherThing.ptrVec[0].isNonnull();
0127 if (!shouldBeTrue) {
0128 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0129 << "Non-null check has incorrect value\n";
0130 }
0131 shouldBeTrue = !(!otherThing.ptrVec[0]);
0132 if (!shouldBeTrue) {
0133 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "'!' has incorrect value\n";
0134 }
0135 shouldBeTrue = !otherThing.ptrVec.empty();
0136 if (!shouldBeTrue) {
0137 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "empty() has incorrect value\n";
0138 }
0139 shouldBeTrue = (otherThing.ptrVec == otherThing.ptrVec);
0140 if (!shouldBeTrue) {
0141 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0142 << "PtrVector equality has incorrect value\n";
0143 }
0144 assert(otherThing.ptr.isAvailable() != thingWasDropped_);
0145 assert(otherThing.ptrVec.isAvailable() != thingWasDropped_);
0146
0147 if (thingWasDropped_)
0148 return;
0149 {
0150 ThingCollection const& tcoll = *otherThing.refProd;
0151 ThingCollection::size_type size1 = tcoll.size();
0152 ThingCollection::size_type size2 = otherThing.refProd->size();
0153 if (size1 == 0 || size1 != size2) {
0154 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0155 << " RefProd size mismatch " << std::endl;
0156 }
0157
0158 Thing const& tc = *otherThing.ref;
0159 int const& x = otherThing.ref->a;
0160 if (tc.a == i && x == i) {
0161 edm::LogInfo("OtherThingAnalyzer")
0162 << " ITEM " << i << " LABEL " << label << " dereferenced from edm::Ref successfully.\n";
0163 } else {
0164 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0165 << "ITEM " << i << " has incorrect edm::Ref value " << tc.a << '\n';
0166 }
0167
0168 edm::View<Thing> const& viewThing = *otherThing.refToBaseProd;
0169 edm::View<Thing>::size_type const viewSize1 = viewThing.size();
0170 edm::View<Thing>::size_type const viewSize2 = otherThing.refToBaseProd->size();
0171 if (viewSize1 == 0 || viewSize2 != viewSize1) {
0172 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0173 << " RefToBaseProd size mismatch " << std::endl;
0174 }
0175 if (viewSize1 != size1) {
0176 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0177 << " RefToBaseProd size mismatch to RefProd size" << std::endl;
0178 }
0179 edm::Ref<ThingCollection> refFromCast = otherThing.refToBase.castTo<edm::Ref<ThingCollection> >();
0180 edm::Ptr<Thing> ptrFromCast = otherThing.refToBase.castTo<edm::Ptr<Thing> >();
0181 Thing const& tcBase = *otherThing.refToBase;
0182 if (tcBase.a != refFromCast->a) {
0183 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0184 << " Ref from RefToBase::castTo has incorrect value " << '\n';
0185 }
0186 if (tcBase.a != ptrFromCast->a) {
0187 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0188 << " Ptr from RefToBase::castTo has incorrect value " << '\n';
0189 }
0190 int const& xBase = otherThing.refToBase->a;
0191 if (tcBase.a == i && xBase == i) {
0192 edm::LogInfo("OtherThingAnalyzer")
0193 << " ITEM " << i << " LABEL " << label << " RefToBase dereferenced successfully.\n";
0194 } else {
0195 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0196 << "ITEM " << i << " RefToBase has incorrect value " << tc.a << '\n';
0197 }
0198
0199 Thing const& tcv = *otherThing.refVec[0];
0200 int const& xv = otherThing.refVec[0]->a;
0201 if (xv != tcv.a || xv != i) {
0202 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0203 << "VECTOR ITEM 0 " << i << " has incorrect value " << tcv.a << '\n';
0204 }
0205 Thing const& tcv1 = *otherThing.refVec[1];
0206 int const& xv1 = otherThing.refVec[1]->a;
0207 if (xv1 != tcv1.a || xv1 != 19 - i) {
0208 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0209 << "VECTOR ITEM 1 " << i << " has incorrect value " << tcv1.a << '\n';
0210 }
0211 for (edm::RefVector<ThingCollection>::iterator iterRefVector = otherThing.refVec.begin(),
0212 iterRefVectorEnd = otherThing.refVec.end();
0213 iterRefVector != iterRefVectorEnd;
0214 ++iterRefVector) {
0215 edm::Ref<ThingCollection> tcol = *iterRefVector;
0216 Thing const& ti = **iterRefVector;
0217 int const& xi = (*iterRefVector)->a;
0218 if (xi != ti.a) {
0219 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0220 << "iterator item " << ti.a << " " << xi << '\n';
0221 } else if (iterRefVector == otherThing.refVec.begin() && xi != i) {
0222 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 0" << xi << '\n';
0223 } else if (iterRefVector != otherThing.refVec.begin() && xi != 19 - i) {
0224 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 1" << xi << '\n';
0225 }
0226 }
0227 edm::RefVector<ThingCollection>::iterator it0 = otherThing.refVec.begin();
0228 int zero = (*it0)->a;
0229 edm::RefVector<ThingCollection>::iterator it1 = it0 + 1;
0230 int one = (*it1)->a;
0231 it1 = 1 + it0;
0232 int x1 = (*it1)->a;
0233 if (x1 != one)
0234 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0235 << "operator+ iterator error: " << x1 << " " << one << '\n';
0236 it0 = it1 - 1;
0237 int x0 = (*it0)->a;
0238 if (x0 != zero)
0239 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0240 << "operator- iterator error: " << x0 << " " << zero << '\n';
0241 x0 = (*(it0++))->a;
0242 if (x0 != zero)
0243 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0244 << "operator++ iterator error: " << x0 << " " << zero << '\n';
0245 x1 = (*it0)->a;
0246 if (x1 != one)
0247 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0248 << "operator++ iterator error 2: " << x1 << " " << one << '\n';
0249 x1 = (*(it0--))->a;
0250 if (x1 != one)
0251 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0252 << "operator-- iterator error: " << x1 << " " << one << '\n';
0253 x0 = (*it0)->a;
0254 if (x0 != zero)
0255 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0256 << "operator-- iterator error 2: " << x0 << " " << zero << '\n';
0257 x1 = it0[1]->a;
0258 if (x1 != one)
0259 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0260 << "operator[] iterator error: " << x1 << " " << one << '\n';
0261 x1 = it1[0]->a;
0262 if (x1 != one)
0263 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0264 << "operator[] iterator error 2: " << x1 << " " << one << '\n';
0265 }
0266 {
0267 Thing const& tc = *otherThing.ptr;
0268 int const& x = otherThing.ptr->a;
0269 if (tc.a == i && x == i) {
0270 edm::LogInfo("OtherThingAnalyzer")
0271 << " ITEM " << i << " LABEL " << label << " dereferenced from edm::Ptr successfully.\n";
0272 } else {
0273 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0274 << "ITEM " << i << " has incorrect edm::Ptr value " << tc.a << '\n';
0275 }
0276
0277 Thing const& tcv = *otherThing.ptrVec[0];
0278 int const& xv = otherThing.ptrVec[0]->a;
0279 if (xv != tcv.a || xv != i) {
0280 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0281 << "VECTOR ITEM 0 " << i << " has incorrect value " << tcv.a << '\n';
0282 }
0283 Thing const& tcv1 = *otherThing.ptrVec[1];
0284 int const& xv1 = otherThing.ptrVec[1]->a;
0285 if (xv1 != tcv1.a || xv1 != 19 - i) {
0286 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0287 << "VECTOR ITEM 1 " << i << " has incorrect value " << tcv1.a << '\n';
0288 }
0289 for (edm::PtrVector<Thing>::const_iterator iterPtrVec = otherThing.ptrVec.begin(),
0290 iterPtrVecEnd = otherThing.ptrVec.end();
0291 iterPtrVec != iterPtrVecEnd;
0292 ++iterPtrVec) {
0293 edm::Ptr<Thing> tcol = *iterPtrVec;
0294 Thing const& ti = **iterPtrVec;
0295 int const& xi = (*iterPtrVec)->a;
0296 if (xi != ti.a) {
0297 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0298 << "iterator item " << ti.a << " " << xi << '\n';
0299 } else if (iterPtrVec == otherThing.ptrVec.begin() && xi != i) {
0300 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 0" << xi << '\n';
0301 } else if (iterPtrVec != otherThing.ptrVec.begin() && xi != 19 - i) {
0302 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze") << "iterator item 1" << xi << '\n';
0303 }
0304 }
0305 edm::PtrVector<Thing>::const_iterator it0 = otherThing.ptrVec.begin();
0306 int zero = (*it0)->a;
0307 edm::PtrVector<Thing>::const_iterator it1 = it0 + 1;
0308 int one = (*it1)->a;
0309 it1 = it0 + 1;
0310 int x1 = (*it1)->a;
0311 if (x1 != one)
0312 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0313 << "operator+ iterator error: " << x1 << " " << one << '\n';
0314 it0 = it1 - 1;
0315 int x0 = (*it0)->a;
0316 if (x0 != zero)
0317 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0318 << "operator- iterator error: " << x0 << " " << zero << '\n';
0319 x0 = (*(it0++))->a;
0320 if (x0 != zero)
0321 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0322 << "operator++ iterator error: " << x0 << " " << zero << '\n';
0323 x1 = (*it0)->a;
0324 if (x1 != one)
0325 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0326 << "operator++ iterator error 2: " << x1 << " " << one << '\n';
0327 x1 = (*(it0--))->a;
0328 if (x1 != one)
0329 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0330 << "operator-- iterator error: " << x1 << " " << one << '\n';
0331 x0 = (*it0)->a;
0332 if (x0 != zero)
0333 throw cms::Exception("Inconsistent Data", "OtherThingAnalyzer::analyze")
0334 << "operator-- iterator error 2: " << x0 << " " << zero << '\n';
0335 }
0336 }
0337 }
0338
0339 void OtherThingAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0340 edm::ParameterSetDescription desc;
0341 desc.addUntracked<bool>("thingWasDropped", false)
0342 ->setComment(
0343 "true if the ref to the ThingCollection in the OtherThingCollection can not be resolved since the "
0344 "ThingCollection was not stored.");
0345 desc.addUntracked<edm::InputTag>("other", edm::InputTag("OtherThing", "testUserTag"))
0346 ->setComment("Where to get the OtherThingCollection");
0347 descriptions.add("otherThingAnalyzer", desc);
0348 }
0349
0350 }
0351 using edmtest::OtherThingAnalyzer;
0352 DEFINE_FWK_MODULE(OtherThingAnalyzer);