ThinningTestAnalyzer

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
#include "FWCore/Framework/interface/global/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "DataFormats/Common/interface/ThinnedAssociation.h"
#include "DataFormats/TestObjects/interface/Thing.h"
#include "DataFormats/TestObjects/interface/ThingCollection.h"
#include "DataFormats/TestObjects/interface/TrackOfThings.h"
#include "DataFormats/Provenance/interface/ProductID.h"

#include <vector>

namespace edmtest {

  class ThinningTestAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    explicit ThinningTestAnalyzer(edm::ParameterSet const& pset);

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

    void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& c) const override;

  private:
    edm::Handle<ThingCollection> getParent(edm::Event const& event) const;
    std::tuple<edm::Handle<ThingCollection>, edm::RefProd<ThingCollection>> getThinned(edm::Event const& event) const;
    edm::Handle<edm::ThinnedAssociation> getAssociation(edm::Event const& event) const;
    edm::Handle<TrackOfThingsCollection> getTrackCollection(edm::Event const& event) const;

    template <typename RefT>
    void testRefToParent(RefT const& ref,
                         std::string_view refName,
                         int const expectedValue,
                         int const eventOffset) const;

    template <typename RefT>
    void testRefToThinned(RefT const& ref,
                          std::string_view refName,
                          int const expectedValue,
                          int const eventOffset,
                          edm::ProductID const thinnedCollectionID) const;

    void testVectors(TrackOfThings const& track,
                     edm::RefProd<ThingCollection> const& thinnedRefProd,
                     edm::EDProductGetter const& productGetter,
                     int const eventOffset) const;

    void incrementExpectedValue(std::vector<int>::const_iterator& iter) const;

    edm::EDGetTokenT<ThingCollection> parentToken_;
    edm::EDGetTokenT<ThingCollection> thinnedToken_;
    edm::EDGetTokenT<edm::ThinnedAssociation> associationToken_;
    edm::EDGetTokenT<TrackOfThingsCollection> trackToken_;

    bool parentWasDropped_;
    std::vector<int> expectedParentContent_;
    bool thinnedWasDropped_;
    bool thinnedIsAlias_;
    bool refToParentIsAvailable_;
    std::vector<int> expectedThinnedContent_;
    std::vector<unsigned int> expectedIndexesIntoParent_;
    bool associationShouldBeDropped_;
    std::vector<int> expectedValues_;
    int parentSlimmedValueFactor_;
    int thinnedSlimmedValueFactor_;
    int refSlimmedValueFactor_;
  };

  ThinningTestAnalyzer::ThinningTestAnalyzer(edm::ParameterSet const& pset) {
    parentToken_ = consumes<ThingCollection>(pset.getParameter<edm::InputTag>("parentTag"));
    thinnedToken_ = mayConsume<ThingCollection>(pset.getParameter<edm::InputTag>("thinnedTag"));
    associationToken_ = mayConsume<edm::ThinnedAssociation>(pset.getParameter<edm::InputTag>("associationTag"));
    trackToken_ = consumes<TrackOfThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"));
    parentWasDropped_ = pset.getParameter<bool>("parentWasDropped");
    if (!parentWasDropped_) {
      expectedParentContent_ = pset.getParameter<std::vector<int>>("expectedParentContent");
    }
    thinnedWasDropped_ = pset.getParameter<bool>("thinnedWasDropped");
    thinnedIsAlias_ = pset.getParameter<bool>("thinnedIsAlias");
    if (!thinnedWasDropped_) {
      expectedThinnedContent_ = pset.getParameter<std::vector<int>>("expectedThinnedContent");
    }
    refToParentIsAvailable_ = pset.getParameter<bool>("refToParentIsAvailable");
    associationShouldBeDropped_ = pset.getParameter<bool>("associationShouldBeDropped");
    if (!associationShouldBeDropped_) {
      expectedIndexesIntoParent_ = pset.getParameter<std::vector<unsigned int>>("expectedIndexesIntoParent");
    }
    expectedValues_ = pset.getParameter<std::vector<int>>("expectedValues");

    auto slimmedFactor = [](int count, int factor) {
      int ret = 1;
      for (int i = 0; i < count; ++i) {
        ret *= factor;
      }
      return ret;
    };
    int const slimmedValueFactor = pset.getParameter<int>("slimmedValueFactor");
    parentSlimmedValueFactor_ = slimmedFactor(pset.getParameter<int>("parentSlimmedCount"), slimmedValueFactor);
    thinnedSlimmedValueFactor_ = slimmedFactor(pset.getParameter<int>("thinnedSlimmedCount"), slimmedValueFactor);
    refSlimmedValueFactor_ = slimmedFactor(pset.getParameter<int>("refSlimmedCount"), slimmedValueFactor);
  }

  void ThinningTestAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    desc.add<edm::InputTag>("parentTag");
    desc.add<edm::InputTag>("thinnedTag");
    desc.add<edm::InputTag>("associationTag");
    desc.add<edm::InputTag>("trackTag");
    desc.add<bool>("parentWasDropped", false);
    desc.add<bool>("thinnedWasDropped", false);
    desc.add<bool>("thinnedIsAlias", false);
    desc.add<bool>("refToParentIsAvailable", true)
        ->setComment(
            "If Ref-to-parent is generally available. With thinnedRefFrom it may happen that the Ref-to-parent is not "
            "available, but the Ref-to-thinned is. In such case this parameter should be set to 'False', and the "
            "'expectedValues' should be set to correspond the values via Ref-to-thinned.");
    std::vector<int> defaultV;
    std::vector<unsigned int> defaultVU;
    desc.add<std::vector<int>>("expectedParentContent", defaultV);
    desc.add<std::vector<int>>("expectedThinnedContent", defaultV);
    desc.add<std::vector<unsigned int>>("expectedIndexesIntoParent", defaultVU);
    desc.add<bool>("associationShouldBeDropped", false);
    desc.add<std::vector<int>>("expectedValues");
    desc.add<int>("parentSlimmedCount", 0);
    desc.add<int>("thinnedSlimmedCount", 0);
    desc.add<int>("refSlimmedCount", 0);
    desc.add<int>("slimmedValueFactor", 10);
    descriptions.addDefault(desc);
  }

  void ThinningTestAnalyzer::analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const&) const {
    auto parentCollection = getParent(event);
    auto [thinnedCollection, thinnedRefProd] = getThinned(event);
    auto associationCollection = getAssociation(event);
    auto trackCollection = getTrackCollection(event);

    if (!parentWasDropped_ && !associationShouldBeDropped_) {
      if (associationCollection->parentCollectionID() != parentCollection.id()) {
        throw cms::Exception("TestFailure") << "analyze parent ProductID is not correct";
      }
    }

    if (!thinnedWasDropped_ && !associationShouldBeDropped_) {
      if (associationCollection->thinnedCollectionID() != thinnedCollection.id()) {
        throw cms::Exception("TestFailure") << "analyze thinned ProductID is not correct";
      }
    }

    int const eventOffset = static_cast<int>(event.eventAuxiliary().event() * 100 + 100);

    std::vector<int>::const_iterator expectedValue = expectedValues_.begin();
    for (auto const& track : *trackCollection) {
      testRefToParent(track.ref1, "ref1", *expectedValue, eventOffset);
      if (not thinnedWasDropped_) {
        testRefToThinned(edm::thinnedRefFrom(track.ref1, thinnedRefProd, event.productGetter()),
                         "ref1",
                         *expectedValue,
                         eventOffset,
                         thinnedCollection.id());
      }
      testRefToParent(track.refToBase1, "refToBase1", *expectedValue, eventOffset);
      incrementExpectedValue(expectedValue);

      testRefToParent(track.ref2, "ref2", *expectedValue, eventOffset);
      if (not thinnedWasDropped_) {
        testRefToThinned(edm::thinnedRefFrom(track.ref2, thinnedRefProd, event.productGetter()),
                         "ref2",
                         *expectedValue,
                         eventOffset,
                         thinnedCollection.id());
      }
      incrementExpectedValue(expectedValue);

      testRefToParent(track.ptr1, "ptr1", *expectedValue, eventOffset);
      incrementExpectedValue(expectedValue);

      testRefToParent(track.ptr2, "ptr2", *expectedValue, eventOffset);
      incrementExpectedValue(expectedValue);

      testVectors(track, thinnedRefProd, event.productGetter(), eventOffset);
    }
  }

  edm::Handle<ThingCollection> ThinningTestAnalyzer::getParent(edm::Event const& event) const {
    auto parentCollection = event.getHandle(parentToken_);

    unsigned int i = 0;
    if (parentWasDropped_) {
      if (parentCollection.isValid()) {
        throw cms::Exception("TestFailure") << "parent collection present, should have been dropped";
      }
    } else if (!expectedParentContent_.empty()) {
      if (parentCollection->size() != expectedParentContent_.size()) {
        throw cms::Exception("TestFailure") << "parent collection has unexpected size, got " << parentCollection->size()
                                            << " expected " << expectedParentContent_.size();
      }
      for (auto const& thing : *parentCollection) {
        // Just some numbers that match the somewhat arbitrary values put in
        // by the ThingProducer.
        int expected = static_cast<int>(expectedParentContent_.at(i) + event.eventAuxiliary().event() * 100 + 100) *
                       parentSlimmedValueFactor_;
        if (thing.a != expected) {
          throw cms::Exception("TestFailure")
              << "parent collection has unexpected content, got " << thing.a << " expected " << expected;
        }
        ++i;
      }
    }

    return parentCollection;
  }

  std::tuple<edm::Handle<ThingCollection>, edm::RefProd<ThingCollection>> ThinningTestAnalyzer::getThinned(
      edm::Event const& event) const {
    auto thinnedCollection = event.getHandle(thinnedToken_);

    // Check to see the content is what we expect based on what was written
    // by ThingProducer and TrackOfThingsProducer. The values are somewhat
    // arbitrary and meaningless.
    edm::RefProd<ThingCollection> thinnedRefProd;
    if (thinnedWasDropped_) {
      if (thinnedCollection.isValid()) {
        throw cms::Exception("TestFailure") << "thinned collection present, should have been dropped";
      }
    } else {
      thinnedRefProd = edm::RefProd<ThingCollection>{thinnedCollection};
      unsigned expectedIndex = 0;
      if (thinnedCollection->size() != expectedThinnedContent_.size()) {
        throw cms::Exception("TestFailure")
            << "thinned collection has unexpected size, got " << thinnedCollection->size() << " expected "
            << expectedThinnedContent_.size();
      }
      for (auto const& thing : *thinnedCollection) {
        const int expected = (expectedThinnedContent_.at(expectedIndex) + event.eventAuxiliary().event() * 100 + 100) *
                             thinnedSlimmedValueFactor_;
        if (thing.a != expected) {
          throw cms::Exception("TestFailure")
              << "thinned collection has unexpected content, got " << thing.a << " expected " << expected;
        }
        ++expectedIndex;
      }
    }
    return std::tuple(thinnedCollection, thinnedRefProd);
  }

  edm::Handle<edm::ThinnedAssociation> ThinningTestAnalyzer::getAssociation(edm::Event const& event) const {
    auto associationCollection = event.getHandle(associationToken_);
    if (associationShouldBeDropped_ && associationCollection.isValid()) {
      throw cms::Exception("TestFailure") << "association collection should have been dropped but was not";
    }
    if (!associationShouldBeDropped_) {
      unsigned int expectedIndex = 0;
      if (associationCollection->indexesIntoParent().size() != expectedIndexesIntoParent_.size()) {
        throw cms::Exception("TestFailure") << "association collection has unexpected size";
      }
      for (auto const& association : associationCollection->indexesIntoParent()) {
        if (association != expectedIndexesIntoParent_.at(expectedIndex)) {
          throw cms::Exception("TestFailure")
              << "association collection has unexpected content, for index " << expectedIndex << " got " << association
              << " expected " << expectedIndexesIntoParent_.at(expectedIndex);
        }
        ++expectedIndex;
      }
    }
    return associationCollection;
  }

  edm::Handle<TrackOfThingsCollection> ThinningTestAnalyzer::getTrackCollection(edm::Event const& event) const {
    auto trackCollection = event.getHandle(trackToken_);
    if (trackCollection->size() != 5u) {
      throw cms::Exception("TestFailure") << "unexpected Track size";
    }
    return trackCollection;
  }

  template <typename RefT>
  void ThinningTestAnalyzer::testRefToParent(RefT const& ref,
                                             std::string_view refName,
                                             int const expectedValue,
                                             int const eventOffset) const {
    if (not refToParentIsAvailable_ or expectedValue == -1) {
      if (ref.isAvailable()) {
        throw cms::Exception("TestFailure")
            << refName << " is available when it should not be, refers to " << ref.id() << " key " << ref.key();
      }
    } else {
      if (!ref.isAvailable()) {
        throw cms::Exception("TestFailure") << refName << " is not available when it should be";
      }
      // Check twice to test some possible caching problems.
      const int expected = (expectedValue + eventOffset) * refSlimmedValueFactor_;
      if (ref->a != expected) {
        throw cms::Exception("TestFailure")
            << "Unexpected values from " << refName << ", got " << ref->a << " expected " << expected;
      }
      if (ref->a != expected) {
        throw cms::Exception("TestFailure")
            << "Unexpected values from " << refName << "  (2nd try), got " << ref->a << " expected " << expected;
      }
    }
  }

  template <typename RefT>
  void ThinningTestAnalyzer::testRefToThinned(RefT const& refToThinned,
                                              std::string_view refName,
                                              int const expectedValue,
                                              int const eventOffset,
                                              edm::ProductID const thinnedCollectionID) const {
    if (expectedValue == -1) {
      if (refToThinned.isNonnull()) {
        throw cms::Exception("TestFailure") << "thinnedRefFrom(" << refName << ") is non-null when it should be null";
      }
      if (refToThinned.isAvailable()) {
        throw cms::Exception("TestFailure") << "thinnedRefFrom(" << refName << ") is available when it should not be";
      }
    } else {
      if (refToThinned.isNull()) {
        throw cms::Exception("TestFailure") << "thinnedRefFrom(" << refName << ") is null when it should not be";
      }
      if (refToThinned.id() != thinnedCollectionID) {
        throw cms::Exception("TestFailure") << "thinnedRefFrom(" << refName << ").id() " << refToThinned.id()
                                            << " differs from expectation " << thinnedCollectionID;
      }
      if (not refToThinned.isAvailable()) {
        throw cms::Exception("TestFailure") << "thinnedRefFrom(" << refName << ") is not available when it should be";
      }
      // Check twice to test some possible caching problems.
      // need to account for slimming because going through an explicit ref-to-slimmed
      const int expected = (expectedValue + eventOffset) * thinnedSlimmedValueFactor_;
      if (refToThinned->a != expected) {
        throw cms::Exception("TestFailure") << "Unexpected values from thinnedRefFrom(" << refName << "), got "
                                            << refToThinned->a << " expected " << expected;
      }
      if (refToThinned->a != expected) {
        throw cms::Exception("TestFailure") << "Unexpected values from thinnedRefFrom(" << refName << ") (2nd try) "
                                            << refToThinned->a << " expected " << expected;
      }
    }
  }

  void ThinningTestAnalyzer::testVectors(TrackOfThings const& track,
                                         edm::RefProd<ThingCollection> const& thinnedRefProd,
                                         edm::EDProductGetter const& productGetter,
                                         int const eventOffset) const {
    // Test RefVector, PtrVector, and RefToBaseVector
    unsigned int k = 0;
    bool allPresent = true;
    for (auto iExpectedValue : expectedValues_) {
      if (iExpectedValue != -1) {
        if (not thinnedWasDropped_) {
          auto refToThinned = edm::thinnedRefFrom(track.refVector1[k], thinnedRefProd, productGetter);
          // need to account for slimming because going through an explicit ref-to-slimmed
          const int expected = (iExpectedValue + eventOffset) * thinnedSlimmedValueFactor_;
          if (refToThinned->a != expected) {
            throw cms::Exception("TestFailure") << "unexpected values from thinnedRefFrom(refVector1), got "
                                                << refToThinned->a << " expected " << expected;
          }
        }
        if (refToParentIsAvailable_) {
          const int expected = (iExpectedValue + eventOffset) * refSlimmedValueFactor_;
          if (track.refVector1[k]->a != expected) {
            throw cms::Exception("TestFailure")
                << "unexpected values from refVector1, got " << track.refVector1[k]->a << " expected " << expected;
          }
          if (track.ptrVector1[k]->a != expected) {
            throw cms::Exception("TestFailure") << "unexpected values from ptrVector1";
          }
          if (track.refToBaseVector1[k]->a != expected) {
            throw cms::Exception("TestFailure") << "unexpected values from refToBaseVector1";
          }
        }
      } else {
        allPresent = false;
      }
      ++k;
    }

    if (refToParentIsAvailable_ and allPresent) {
      if (!track.refVector1.isAvailable()) {
        throw cms::Exception("TestFailure") << "unexpected value (false) from refVector::isAvailable";
      }
      if (!track.ptrVector1.isAvailable()) {
        throw cms::Exception("TestFailure") << "unexpected value (false) from ptrVector::isAvailable";
      }
      if (!track.refToBaseVector1.isAvailable()) {
        throw cms::Exception("TestFailure") << "unexpected value (false) from refToBaseVector::isAvailable";
      }
    } else {
      if (track.refVector1.isAvailable()) {
        throw cms::Exception("TestFailure") << "unexpected value (true) from refVector::isAvailable";
      }
      if (track.ptrVector1.isAvailable()) {
        throw cms::Exception("TestFailure") << "unexpected value (true) from ptrVector::isAvailable";
      }
      if (track.refToBaseVector1.isAvailable()) {
        throw cms::Exception("TestFailure") << "unexpected value (true) from refToBaseVector::isAvailable";
      }
    }
  }

  void ThinningTestAnalyzer::incrementExpectedValue(std::vector<int>::const_iterator& iter) const {
    ++iter;
    if (iter == expectedValues_.end())
      iter = expectedValues_.begin();
  }
}  // namespace edmtest
using edmtest::ThinningTestAnalyzer;
DEFINE_FWK_MODULE(ThinningTestAnalyzer);