TestMergeResults

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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

//
// Reads some simple test objects in the event, run, and lumi
// principals.  Then checks to see if the values in these
// objects match what we expect.  Intended to be used to
// test the values in a file that has merged run and lumi
// products.
//
// Original Author: David Dagenhart, Fermilab, February 2008

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/Wrapper.h"
#include "DataFormats/Provenance/interface/ProductDescription.h"
#include "DataFormats/Provenance/interface/BranchID.h"
#include "DataFormats/Provenance/interface/ProcessHistory.h"
#include "DataFormats/Provenance/interface/Provenance.h"
#include "DataFormats/TestObjects/interface/Thing.h"
#include "DataFormats/TestObjects/interface/ThingWithIsEqual.h"
#include "DataFormats/TestObjects/interface/ThingWithMerge.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/FileBlock.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/InputTag.h"

#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

namespace edm {
  class EventSetup;
}

namespace edmtest {

  class TestMergeResults : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
  public:
    explicit TestMergeResults(edm::ParameterSet const&);

    static void fillDescriptions(edm::ConfigurationDescriptions&);

    void analyze(edm::Event const& e, edm::EventSetup const& c) override;
    void beginRun(edm::Run const&, edm::EventSetup const&) override;
    void endRun(edm::Run const&, edm::EventSetup const&) override;
    void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
    void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
    void endJob() override;

  private:
    void checkExpectedLumiProducts(unsigned int index,
                                   std::vector<int> const& expectedValues,
                                   edm::InputTag const& tag,
                                   char const* functionName,
                                   edm::LuminosityBlock const& lumi,
                                   std::vector<int> const& expectedValueImproperlyMerged);

    void checkExpectedRunProducts(unsigned int index,
                                  std::vector<int> const& expectedValues,
                                  edm::InputTag const& tag,
                                  char const* functionName,
                                  edm::Run const& run,
                                  std::vector<int> const& expectedValueImproperlyMerged);

    void abortWithMessage(char const* whichFunction,
                          char const* type,
                          edm::InputTag const& tag,
                          int expectedValue,
                          int actualValue,
                          bool unexpectedImproperlyMergedValue = false) const;

    std::vector<int> const expectedBeginRunProd_;
    std::vector<int> const expectedEndRunProd_;
    std::vector<int> const expectedBeginLumiProd_;
    std::vector<int> const expectedEndLumiProd_;

    std::vector<int> const expectedBeginRunNew_;
    std::vector<int> const expectedEndRunNew_;
    std::vector<int> const expectedBeginLumiNew_;
    std::vector<int> const expectedEndLumiNew_;

    std::vector<int> const expectedEndRunProdImproperlyMerged_;
    std::vector<int> const expectedEndLumiProdImproperlyMerged_;

    std::vector<std::string> const expectedParents_;

    std::vector<std::string> const expectedProcessHistoryInRuns_;

    std::vector<int> const expectedDroppedEvent_;
    std::vector<int> const expectedDroppedEvent1_;
    std::vector<int> const expectedDroppedEvent1NEvents_;

    bool const verbose_;
    bool const testAlias_;

    unsigned int indexRun_ = 0;
    unsigned int indexLumi_ = 0;
    unsigned int parentIndex_ = 0;
    unsigned int droppedIndex1_ = 0;
    int droppedIndex1EventCount_ = 0;
    unsigned int processHistoryIndex_ = 0;

    edm::Handle<edmtest::Thing> h_thing;
    edm::Handle<edmtest::ThingWithMerge> h_thingWithMerge;
    edm::Handle<edmtest::ThingWithIsEqual> h_thingWithIsEqual;
  };

  // -----------------------------------------------------------------

  TestMergeResults::TestMergeResults(edm::ParameterSet const& ps)
      : expectedBeginRunProd_(ps.getUntrackedParameter<std::vector<int>>("expectedBeginRunProd")),
        expectedEndRunProd_(ps.getUntrackedParameter<std::vector<int>>("expectedEndRunProd")),
        expectedBeginLumiProd_(ps.getUntrackedParameter<std::vector<int>>("expectedBeginLumiProd")),
        expectedEndLumiProd_(ps.getUntrackedParameter<std::vector<int>>("expectedEndLumiProd")),

        expectedBeginRunNew_(ps.getUntrackedParameter<std::vector<int>>("expectedBeginRunNew")),
        expectedEndRunNew_(ps.getUntrackedParameter<std::vector<int>>("expectedEndRunNew")),
        expectedBeginLumiNew_(ps.getUntrackedParameter<std::vector<int>>("expectedBeginLumiNew")),
        expectedEndLumiNew_(ps.getUntrackedParameter<std::vector<int>>("expectedEndLumiNew")),

        expectedEndRunProdImproperlyMerged_(
            ps.getUntrackedParameter<std::vector<int>>("expectedEndRunProdImproperlyMerged")),
        expectedEndLumiProdImproperlyMerged_(
            ps.getUntrackedParameter<std::vector<int>>("expectedEndLumiProdImproperlyMerged")),

        expectedParents_(ps.getUntrackedParameter<std::vector<std::string>>("expectedParents")),
        expectedProcessHistoryInRuns_(
            ps.getUntrackedParameter<std::vector<std::string>>("expectedProcessHistoryInRuns")),

        expectedDroppedEvent_(ps.getUntrackedParameter<std::vector<int>>("expectedDroppedEvent")),
        expectedDroppedEvent1_(ps.getUntrackedParameter<std::vector<int>>("expectedDroppedEvent1")),
        expectedDroppedEvent1NEvents_(ps.getUntrackedParameter<std::vector<int>>("expectedDroppedEvent1NEvents")),

        verbose_(ps.getUntrackedParameter<bool>("verbose")),
        testAlias_(ps.getUntrackedParameter<bool>("testAlias")) {
    auto ap_thing = std::make_unique<edmtest::Thing>();
    edm::Wrapper<edmtest::Thing> w_thing(std::move(ap_thing));
    assert(!w_thing.isMergeable());
    assert(!w_thing.hasIsProductEqual());
    assert(!w_thing.hasSwap());

    auto ap_thingwithmerge = std::make_unique<edmtest::ThingWithMerge>();
    edm::Wrapper<edmtest::ThingWithMerge> w_thingWithMerge(std::move(ap_thingwithmerge));
    assert(w_thingWithMerge.isMergeable());
    assert(!w_thingWithMerge.hasIsProductEqual());
    assert(w_thingWithMerge.hasSwap());

    auto ap_thingwithisequal = std::make_unique<edmtest::ThingWithIsEqual>();
    edm::Wrapper<edmtest::ThingWithIsEqual> w_thingWithIsEqual(std::move(ap_thingwithisequal));
    assert(!w_thingWithIsEqual.isMergeable());
    assert(w_thingWithIsEqual.hasIsProductEqual());
    assert(!w_thingWithIsEqual.hasSwap());

    if (expectedDroppedEvent_.size() > 0) {
      consumes<edmtest::ThingWithIsEqual>(edm::InputTag{"makeThingToBeDropped", "event", "PROD"});
      consumes<edmtest::ThingWithMerge>(edm::InputTag{"makeThingToBeDropped", "event", "PROD"});

      consumes<edmtest::ThingWithIsEqual, edm::InRun>(edm::InputTag{"makeThingToBeDropped", "beginRun", "PROD"});
    }
    for (auto const& parent : expectedParents_) {
      mayConsume<edmtest::Thing>(edm::InputTag{parent, "event", "PROD"});
    }
    if (expectedDroppedEvent1_.size() > droppedIndex1_) {
      assert(expectedDroppedEvent1_.size() == expectedDroppedEvent1NEvents_.size());
      consumes<edmtest::ThingWithIsEqual>(edm::InputTag{"makeThingToBeDropped1", "event", "PROD"});
    }
    consumes<edmtest::Thing>(edm::InputTag{"thingWithMergeProducer", "event", "PROD"});

    if (testAlias_) {
      consumes<edmtest::Thing>(edm::InputTag{"aliasForThingToBeDropped2", "instance2"});
      consumes<edmtest::Thing>(edm::InputTag{"aliasForThingToBeDropped2", "instance2", "PROD"});
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "beginRun", "PROD");
      consumes<edmtest::Thing, edm::InRun>(tag);
      consumes<edmtest::ThingWithMerge, edm::InRun>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InRun>(tag);
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "beginRun");
      consumes<edmtest::Thing, edm::InRun>(tag);
      consumes<edmtest::ThingWithMerge, edm::InRun>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InRun>(tag);
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "endRun", "PROD");
      consumes<edmtest::Thing, edm::InRun>(tag);
      consumes<edmtest::ThingWithMerge, edm::InRun>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InRun>(tag);
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "endRun");
      consumes<edmtest::Thing, edm::InRun>(tag);
      consumes<edmtest::ThingWithMerge, edm::InRun>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InRun>(tag);
    }

    if (expectedDroppedEvent_.size() > 2) {
      edm::InputTag tag("makeThingToBeDropped", "endRun", "PROD");
      consumes<edmtest::ThingWithMerge, edm::InRun>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InRun>(tag);
    }

    if (testAlias_) {
      consumes<edmtest::Thing, edm::InRun>(edm::InputTag{"aliasForThingToBeDropped2", "endRun2"});
      edm::InputTag tag("aliasForThingToBeDropped2", "endRun2", "PROD");
      consumes<edmtest::Thing, edm::InRun>(tag);
    }

    if (expectedDroppedEvent_.size() > 3) {
      edm::InputTag tag("makeThingToBeDropped", "beginLumi", "PROD");
      consumes<edmtest::ThingWithIsEqual, edm::InLumi>(tag);
    }
    {
      edm::InputTag tag("thingWithMergeProducer", "endLumi", "PROD");
      consumes<edmtest::Thing, edm::InLumi>(tag);
      consumes<edmtest::ThingWithMerge, edm::InLumi>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InLumi>(tag);
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "endLumi");
      consumes<edmtest::Thing, edm::InLumi>(tag);
      consumes<edmtest::ThingWithMerge, edm::InLumi>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InLumi>(tag);
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "beginLumi", "PROD");
      consumes<edmtest::Thing, edm::InLumi>(tag);
      consumes<edmtest::ThingWithMerge, edm::InLumi>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InLumi>(tag);
    }

    {
      edm::InputTag tag("thingWithMergeProducer", "beginLumi");
      consumes<edmtest::Thing, edm::InLumi>(tag);
      consumes<edmtest::ThingWithMerge, edm::InLumi>(tag);
      consumes<edmtest::ThingWithIsEqual, edm::InLumi>(tag);
    }

    if (expectedDroppedEvent_.size() > 4) {
      edm::InputTag tag("makeThingToBeDropped", "endLumi", "PROD");
      consumes<edmtest::ThingWithIsEqual, edm::InLumi>(tag);
      consumes<edmtest::ThingWithMerge, edm::InLumi>(tag);
    }

    if (testAlias_) {
      consumes<edmtest::Thing, edm::InLumi>(edm::InputTag{"aliasForThingToBeDropped2", "endLumi2"});
      edm::InputTag tag("aliasForThingToBeDropped2", "endLumi2", "PROD");
      consumes<edmtest::Thing, edm::InLumi>(tag);
    }
  }

  // -----------------------------------------------------------------

  void TestMergeResults::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    desc.addUntracked<std::vector<int>>("expectedBeginRunProd", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from process PROD at beginRun.");
    desc.addUntracked<std::vector<int>>("expectedEndRunProd", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from process PROD at nendRun.");
    desc.addUntracked<std::vector<int>>("expectedBeginLumiProd", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from process PROD at "
            "beginLuminosityBlock.");
    desc.addUntracked<std::vector<int>>("expectedEndLumiProd", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from process PROD at "
            "endLuminosityBlock.");

    desc.addUntracked<std::vector<int>>("expectedBeginRunNew", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from the latest process at "
            "beginRun.");
    desc.addUntracked<std::vector<int>>("expectedEndRunNew", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from the latest process at endRun.");
    desc.addUntracked<std::vector<int>>("expectedBeginLumiNew", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from the latest process at "
            "beginLuminosityBlock.");
    desc.addUntracked<std::vector<int>>("expectedEndLumiNew", {})
        ->setComment(
            "Check the expected values of Thing, ThingWithMerge, ThingWithIsEqual from the latest process at "
            "endLuminosityBlock.");

    desc.addUntracked<std::vector<int>>("expectedEndRunProdImproperlyMerged", {});
    desc.addUntracked<std::vector<int>>("expectedEndLumiProdImproperlyMerged", {});
    desc.addUntracked<std::vector<std::string>>("expectedParents", {});
    desc.addUntracked<std::vector<std::string>>("expectedProcessHistoryInRuns", {});
    desc.addUntracked<std::vector<int>>("expectedDroppedEvent", {});
    desc.addUntracked<std::vector<int>>("expectedDroppedEvent1", {});
    desc.addUntracked<std::vector<int>>("expectedDroppedEvent1NEvents", {});

    desc.addUntracked<bool>("testAlias", false);
    desc.addUntracked<bool>("verbose", false);

    desc.setComment(
        "The expected{Begin,End}(Run,Lumi}{Prod,New} parameters follow the same pattern. The expected values come in "
        "sets of three: value expected in Thing, ThingWithMerge, and ThingWithIsEqual. Each set of 3 is tested at the "
        "specific transition, and then the next set of 3 is tested at the next transition, and so on. When the "
        "sequence of parameter values is exhausted, the checking is stopped. The values if 0 are just placedholders, "
        "i.e. if the value is a 0, the check is not made.");

    descriptions.addDefault(desc);
  }

  // -----------------------------------------------------------------
  void TestMergeResults::analyze(edm::Event const& e, edm::EventSetup const&) {
    assert(e.processHistory().id() == e.processHistoryID());

    if (verbose_)
      edm::LogInfo("TestMergeResults") << "analyze";

    if (expectedDroppedEvent_.size() > 0) {
      edm::InputTag tag("makeThingToBeDropped", "event", "PROD");
      e.getByLabel(tag, h_thingWithIsEqual);
      assert(h_thingWithIsEqual->a == expectedDroppedEvent_[0]);

      e.getByLabel(tag, h_thingWithMerge);
      assert(h_thingWithMerge.isValid());
    }

    // This one is used to test the merging step when a specific product
    // has been dropped or not created in some of the input files.
    if (expectedDroppedEvent1_.size() > droppedIndex1_) {
      ++droppedIndex1EventCount_;
      if (droppedIndex1EventCount_ > expectedDroppedEvent1NEvents_[droppedIndex1_]) {
        ++droppedIndex1_;
        std::cout << "advance " << droppedIndex1_ << std::endl;
        droppedIndex1EventCount_ = 1;
      }
      assert(droppedIndex1_ < expectedDroppedEvent1_.size());

      edm::InputTag tag("makeThingToBeDropped1", "event", "PROD");
      e.getByLabel(tag, h_thingWithIsEqual);
      if (expectedDroppedEvent1_[droppedIndex1_] == -1) {
        assert(!h_thingWithIsEqual.isValid());
      } else {
        assert(h_thingWithIsEqual.isValid());
        assert(h_thingWithIsEqual->a == expectedDroppedEvent1_[droppedIndex1_]);
      }
    }

    // I'm not sure this test belongs in this module.  Originally it tested
    // merging of parentage for run and lumi products, but at some point the
    // parentage for run/lumi products stopped being written at all so there was
    // nothing to test.  This was the only real test of the provenance
    // parentage, so I just converted to a test of the parentage of products
    // in the Event rather than deleting it or writing a complete new test ...
    // It is actually convenient here, so maybe it is OK even if the module name
    // has nothing to do with this test.
    if (parentIndex_ < expectedParents_.size()) {
      edm::InputTag tag("thingWithMergeProducer", "event", "PROD");
      e.getByLabel(tag, h_thing);
      std::string expectedParent = expectedParents_[parentIndex_];
      edm::BranchID actualParentBranchID = h_thing.provenance()->productProvenance()->parentage().parents()[0];

      // There ought to be a get that uses the BranchID as an argument, but
      // there is not at the moment so we get the Provenance first and use that
      // find the actual parent
      edm::Provenance prov = e.getProvenance(actualParentBranchID);
      assert(expectedParent == prov.moduleLabel());
      edm::InputTag tagparent(prov.moduleLabel(), prov.productInstanceName(), prov.processName());
      e.getByLabel(tagparent, h_thing);
      assert(h_thing->a == 11);
      ++parentIndex_;
    }

    if (testAlias_) {
      e.getByLabel("aliasForThingToBeDropped2", "instance2", h_thing);
      assert(h_thing->a == 11);
      edm::InputTag inputTag("aliasForThingToBeDropped2", "instance2", "PROD");
      e.getByLabel(inputTag, h_thing);
      assert(h_thing->a == 11);

      edm::BranchID const& originalBranchID = h_thing.provenance()->productDescription().originalBranchID();
      //this will throw if the original provenance is not available
      e.getProvenance(originalBranchID);
    }
  }

  void TestMergeResults::beginRun(edm::Run const& run, edm::EventSetup const&) {
    if (verbose_)
      edm::LogInfo("TestMergeResults") << "beginRun";

    if (expectedDroppedEvent_.size() > 1) {
      edm::InputTag tagd("makeThingToBeDropped", "beginRun", "PROD");
      run.getByLabel(tagd, h_thingWithIsEqual);
      assert(h_thingWithIsEqual->a == expectedDroppedEvent_[1]);
    }
  }

  void TestMergeResults::endRun(edm::Run const& run, edm::EventSetup const&) {
    assert(run.processHistory().id() == run.processHistoryID());

    edm::ProcessHistory const& ph = run.processHistory();
    for (edm::ProcessHistory::const_iterator iter = ph.begin(), iEnd = ph.end(); iter != iEnd; ++iter) {
      if (processHistoryIndex_ < expectedProcessHistoryInRuns_.size()) {
        assert(expectedProcessHistoryInRuns_[processHistoryIndex_] == iter->processName());
        ++processHistoryIndex_;
      }
    }

    if (verbose_)
      edm::LogInfo("TestMergeResults") << "endRun";

    std::vector<int> emptyDummy;

    edm::InputTag tag("thingWithMergeProducer", "endRun", "PROD");
    checkExpectedRunProducts(indexRun_, expectedEndRunProd_, tag, "endRun", run, expectedEndRunProdImproperlyMerged_);

    edm::InputTag tagnew("thingWithMergeProducer", "endRun");
    checkExpectedRunProducts(indexRun_, expectedEndRunNew_, tagnew, "endRun", run, emptyDummy);

    edm::InputTag tagb("thingWithMergeProducer", "beginRun", "PROD");
    checkExpectedRunProducts(indexRun_, expectedBeginRunProd_, tagb, "endRun", run, emptyDummy);

    edm::InputTag tagbnew("thingWithMergeProducer", "beginRun");
    checkExpectedRunProducts(indexRun_, expectedBeginRunNew_, tagbnew, "endRun", run, emptyDummy);

    if (expectedDroppedEvent_.size() > 2) {
      edm::InputTag tagd("makeThingToBeDropped", "endRun", "PROD");
      run.getByLabel(tagd, h_thingWithIsEqual);
      assert(h_thingWithIsEqual->a == expectedDroppedEvent_[2]);

      run.getByLabel(tagd, h_thingWithMerge);
      assert(!h_thingWithMerge.isValid());
    }

    if (testAlias_) {
      run.getByLabel("aliasForThingToBeDropped2", "endRun2", h_thing);
      assert(h_thing->a == 100001);
      edm::InputTag inputTag("aliasForThingToBeDropped2", "endRun2", "PROD");
      run.getByLabel(inputTag, h_thing);
      assert(h_thing->a == 100001);

      edm::BranchID const& originalBranchID = h_thing.provenance()->productDescription().originalBranchID();
      run.getProvenance(originalBranchID);
    }

    indexRun_ += 3;
  }

  void TestMergeResults::beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
    if (verbose_)
      edm::LogInfo("TestMergeResults") << "beginLuminosityBlock";

    if (expectedDroppedEvent_.size() > 3) {
      edm::InputTag tagd("makeThingToBeDropped", "beginLumi", "PROD");
      lumi.getByLabel(tagd, h_thingWithIsEqual);
      assert(h_thingWithIsEqual->a == expectedDroppedEvent_[3]);
    }
  }

  void TestMergeResults::endLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
    assert(lumi.processHistory().id() == lumi.processHistoryID());

    if (verbose_)
      edm::LogInfo("TestMergeResults") << "endLuminosityBlock";

    std::vector<int> emptyDummy;

    edm::InputTag tag("thingWithMergeProducer", "endLumi", "PROD");
    checkExpectedLumiProducts(
        indexLumi_, expectedEndLumiProd_, tag, "endLumi", lumi, expectedEndLumiProdImproperlyMerged_);

    edm::InputTag tagnew("thingWithMergeProducer", "endLumi");
    checkExpectedLumiProducts(indexLumi_, expectedEndLumiNew_, tagnew, "endLumi", lumi, emptyDummy);

    edm::InputTag tagb("thingWithMergeProducer", "beginLumi", "PROD");
    checkExpectedLumiProducts(indexLumi_, expectedBeginLumiProd_, tagb, "endLumi", lumi, emptyDummy);

    edm::InputTag tagbnew("thingWithMergeProducer", "beginLumi");
    checkExpectedLumiProducts(indexLumi_, expectedBeginLumiNew_, tagbnew, "endLumi", lumi, emptyDummy);

    if (expectedDroppedEvent_.size() > 4) {
      edm::InputTag tagd("makeThingToBeDropped", "endLumi", "PROD");
      lumi.getByLabel(tagd, h_thingWithIsEqual);
      assert(h_thingWithIsEqual->a == expectedDroppedEvent_[4]);

      lumi.getByLabel(tagd, h_thingWithMerge);
      assert(!h_thingWithMerge.isValid());
    }

    if (testAlias_) {
      lumi.getByLabel("aliasForThingToBeDropped2", "endLumi2", h_thing);
      assert(h_thing->a == 1001);
      edm::InputTag inputTag("aliasForThingToBeDropped2", "endLumi2", "PROD");
      lumi.getByLabel(inputTag, h_thing);
      assert(h_thing->a == 1001);

      edm::BranchID const& originalBranchID = h_thing.provenance()->productDescription().originalBranchID();
      lumi.getProvenance(originalBranchID);
    }
    indexLumi_ += 3;
  }

  void TestMergeResults::endJob() {
    if (verbose_)
      edm::LogInfo("TestMergeResults") << "endJob";
  }

  void TestMergeResults::checkExpectedRunProducts(unsigned int index,
                                                  std::vector<int> const& expectedValues,
                                                  edm::InputTag const& tag,
                                                  char const* functionName,
                                                  edm::Run const& run,
                                                  std::vector<int> const& expectedValueImproperlyMerged) {
    if ((index + 2) < expectedValues.size()) {
      int expected = expectedValues[index];
      if (expected != 0) {
        run.getByLabel(tag, h_thing);
        if (h_thing->a != expected) {
          abortWithMessage(functionName, "Thing", tag, expected, h_thing->a);
        }
        if (index < expectedValueImproperlyMerged.size()) {
          if ((expectedValueImproperlyMerged[index] != 0) != h_thing.provenance()->knownImproperlyMerged()) {
            abortWithMessage(functionName, "Thing", tag, 0, 0, true);
          }
        }
      }

      expected = expectedValues[index + 1];
      if (expected != 0) {
        run.getByLabel(tag, h_thingWithMerge);
        if (h_thingWithMerge->a != expected) {
          abortWithMessage(functionName, "ThingWithMerge", tag, expected, h_thingWithMerge->a);
        }
        if (index + 1 < expectedValueImproperlyMerged.size()) {
          if ((expectedValueImproperlyMerged[index + 1] != 0) !=
              h_thingWithMerge.provenance()->knownImproperlyMerged()) {
            abortWithMessage(functionName, "ThingWithMerge", tag, 0, 0, true);
          }
        }
        if (!h_thingWithMerge.provenance()->productDescription().isMergeable()) {
          std::cerr << "TestMergeResults::checkExpectedRunProducts isMergeable from ProductDescription returns\n"
                    << "unexpected value for ThingWithMerge type." << std::endl;
          abort();
        }
      }

      expected = expectedValues[index + 2];
      if (expected != 0) {
        run.getByLabel(tag, h_thingWithIsEqual);
        if (h_thingWithIsEqual->a != expected) {
          abortWithMessage(functionName, "ThingWithIsEqual", tag, expected, h_thingWithIsEqual->a);
        }
        if (index + 2 < expectedValueImproperlyMerged.size()) {
          if ((expectedValueImproperlyMerged[index + 2] != 0) !=
              h_thingWithIsEqual.provenance()->knownImproperlyMerged()) {
            abortWithMessage(functionName, "ThingWithIsEqual", tag, 0, 0, true);
          }
        }
        if (h_thingWithIsEqual.provenance()->productDescription().isMergeable()) {
          std::cerr << "TestMergeResults::checkExpectedRunProducts isMergeable from ProductDescription returns\n"
                    << "unexpected value for ThingWithIsEqual type." << std::endl;
          abort();
        }
      }
    }
  }

  void TestMergeResults::checkExpectedLumiProducts(unsigned int index,
                                                   std::vector<int> const& expectedValues,
                                                   edm::InputTag const& tag,
                                                   char const* functionName,
                                                   edm::LuminosityBlock const& lumi,
                                                   std::vector<int> const& expectedValueImproperlyMerged) {
    if ((index + 2) < expectedValues.size()) {
      int expected = expectedValues[index];
      if (expected != 0) {
        lumi.getByLabel(tag, h_thing);
        if (h_thing->a != expected) {
          abortWithMessage(functionName, "Thing", tag, expected, h_thing->a);
        }
        if (index < expectedValueImproperlyMerged.size()) {
          if ((expectedValueImproperlyMerged[index] != 0) != h_thing.provenance()->knownImproperlyMerged()) {
            abortWithMessage(functionName, "Thing", tag, 0, 0, true);
          }
        }
      }

      expected = expectedValues[index + 1];
      if (expected != 0) {
        lumi.getByLabel(tag, h_thingWithMerge);
        if (h_thingWithMerge->a != expected) {
          abortWithMessage(functionName, "ThingWithMerge", tag, expected, h_thingWithMerge->a);
        }
        if (index + 1 < expectedValueImproperlyMerged.size()) {
          if ((expectedValueImproperlyMerged[index + 1] != 0) !=
              h_thingWithMerge.provenance()->knownImproperlyMerged()) {
            abortWithMessage(functionName, "ThingWithMerge", tag, 0, 0, true);
          }
        }
        if (!h_thingWithMerge.provenance()->productDescription().isMergeable()) {
          std::cerr << "TestMergeResults::checkExpectedLumiProducts isMergeable from ProductDescription returns\n"
                    << "unexpected value for ThingWithMerge type." << std::endl;
          abort();
        }
      }

      expected = expectedValues[index + 2];
      if (expected != 0) {
        lumi.getByLabel(tag, h_thingWithIsEqual);
        if (h_thingWithIsEqual->a != expected) {
          abortWithMessage(functionName, "ThingWithIsEqual", tag, expected, h_thingWithIsEqual->a);
        }
        if (index + 2 < expectedValueImproperlyMerged.size()) {
          if ((expectedValueImproperlyMerged[index + 2] != 0) !=
              h_thingWithIsEqual.provenance()->knownImproperlyMerged()) {
            abortWithMessage(functionName, "ThingWithIsEqual", tag, 0, 0, true);
          }
        }
        if (h_thingWithIsEqual.provenance()->productDescription().isMergeable()) {
          std::cerr << "TestMergeResults::checkExpectedLumiProducts isMergeable from ProductDescription returns\n"
                    << "unexpected value for ThingWithIsEqual type." << std::endl;
          abort();
        }
      }
    }
  }

  void TestMergeResults::abortWithMessage(char const* whichFunction,
                                          char const* type,
                                          edm::InputTag const& tag,
                                          int expectedValue,
                                          int actualValue,
                                          bool unexpectedImproperlyMergedValue) const {
    std::cerr << "Error while testing merging of run/lumi products in TestMergeResults.cc\n"
              << "In function " << whichFunction << " looking for product of type " << type << "\n"
              << tag << std::endl;
    if (unexpectedImproperlyMergedValue) {
      std::cerr << "Unexpected value of knownImproperlyMerged from provenance" << std::endl;
    } else {
      std::cerr << "Expected value = " << expectedValue << " actual value = " << actualValue << std::endl;
    }
    abort();
  }
}  // namespace edmtest

using edmtest::TestMergeResults;

DEFINE_FWK_MODULE(TestMergeResults);