BuiltinIntTestAnalyzer

ConsumingOneSharedResourceAnalyzer

ConsumingStreamAnalyzer

DSVAnalyzer

GenericAnalyzerT

IntConsumingAnalyzer

IntFromRunConsumingAnalyzer

IntTestAnalyzer

MultipleIntsAnalyzer

NonAnalyzer

SCSimpleAnalyzer

SimpleViewAnalyzer

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

/*----------------------------------------------------------------------

Toy EDAnalyzers for testing purposes only.

----------------------------------------------------------------------*/

#include "DataFormats/Common/interface/DetSetVector.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/TestObjects/interface/ToyProducts.h"
//
#include "FWCore/Framework/interface/stream/EDAnalyzer.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/global/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/transform.h"
//
#include <cassert>
#include <string>
#include <vector>

namespace edmtest {

  //--------------------------------------------------------------------
  //
  // Toy analyzers
  //
  //--------------------------------------------------------------------

  //--------------------------------------------------------------------
  //
  // every call to NonAnalyzer::analyze does nothing.
  //
  class NonAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    explicit NonAnalyzer(edm::ParameterSet const& /*p*/) {}
    ~NonAnalyzer() override {}
    void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& c) const final;
  };

  void NonAnalyzer::analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const {}

  //--------------------------------------------------------------------
  //
  class IntTestAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    IntTestAnalyzer(edm::ParameterSet const& iPSet)
        : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
          token_(consumes(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel"))),
          missing_(iPSet.getUntrackedParameter<bool>("valueMustBeMissing")) {}

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<int>("valueMustMatch");
      desc.addUntracked<edm::InputTag>("moduleLabel");
      desc.addUntracked<bool>("valueMustBeMissing", false);
      descriptions.addDefault(desc);
    }

    void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
      auto const& prod = iEvent.getHandle(token_);
      if (missing_) {
        if (prod.isValid()) {
          edm::ProductLabels labels;
          labelsForToken(token_, labels);
          throw cms::Exception("ValueNotMissing")
              << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process
              << "\" is being produced, which is not expected.";
        }
        return;
      }
      if (prod->value != value_) {
        edm::ProductLabels labels;
        labelsForToken(token_, labels);
        throw cms::Exception("ValueMismatch")
            << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process << "\" is "
            << prod->value << " but it was supposed to be " << value_;
      }
    }

  private:
    int const value_;
    edm::EDGetTokenT<IntProduct> const token_;
    bool const missing_;
  };

  //--------------------------------------------------------------------
  //
  class MultipleIntsAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    MultipleIntsAnalyzer(edm::ParameterSet const& iPSet) {
      auto const& tags = iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("getFromModules");
      for (auto const& tag : tags) {
        m_tokens.emplace_back(consumes(tag));
      }
    }

    void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const override {
      for (auto const& token : m_tokens) {
        (void)iEvent.getHandle(token);
      }
    }

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<std::vector<edm::InputTag>>("getFromModules");
      descriptions.addDefault(desc);
    }

  private:
    std::vector<edm::EDGetTokenT<IntProduct>> m_tokens;
  };

  //--------------------------------------------------------------------
  //
  class BuiltinIntTestAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    BuiltinIntTestAnalyzer(edm::ParameterSet const& iPSet)
        : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
          token_(consumes(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel"))),
          missing_(iPSet.getUntrackedParameter<bool>("valueMustBeMissing")) {}

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<int>("valueMustMatch");
      desc.addUntracked<edm::InputTag>("moduleLabel");
      desc.addUntracked<bool>("valueMustBeMissing", false);
      descriptions.addDefault(desc);
    }

    void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
      auto const& prod = iEvent.getHandle(token_);
      if (missing_) {
        if (prod.isValid()) {
          edm::ProductLabels labels;
          labelsForToken(token_, labels);
          throw cms::Exception("ValueNotMissing")
              << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process
              << "\" is being produced, which is not expected.";
        }
        return;
      }
      if (*prod != value_) {
        edm::ProductLabels labels;
        labelsForToken(token_, labels);
        throw cms::Exception("ValueMismatch")
            << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process << "\" is "
            << *prod << " but it was supposed to be " << value_;
      }
    }

  private:
    int const value_;
    edm::EDGetTokenT<int> const token_;
    bool const missing_;
  };

  //--------------------------------------------------------------------
  //
  template <typename T>
  class GenericAnalyzerT : public edm::global::EDAnalyzer<> {
  public:
    GenericAnalyzerT(edm::ParameterSet const& iPSet)
        : tokensBeginProcess_(edm::vector_transform(
              iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginProcess"),
              [this](edm::InputTag const& tag) { return this->consumes<T, edm::InProcess>(tag); })),
          tokensBeginRun_(
              edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun"),
                                    [this](edm::InputTag const& tag) { return this->consumes<T, edm::InRun>(tag); })),
          tokensBeginLumi_(
              edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi"),
                                    [this](edm::InputTag const& tag) { return this->consumes<T, edm::InLumi>(tag); })),
          tokensEvent_(edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent"),
                                             [this](edm::InputTag const& tag) { return this->consumes<T>(tag); })),
          tokensEndLumi_(
              edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndLumi"),
                                    [this](edm::InputTag const& tag) { return this->consumes<T, edm::InLumi>(tag); })),
          tokensEndRun_(
              edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndRun"),
                                    [this](edm::InputTag const& tag) { return this->consumes<T, edm::InRun>(tag); })),
          tokensEndProcess_(edm::vector_transform(
              iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndProcess"),
              [this](edm::InputTag const& tag) { return this->consumes<T, edm::InProcess>(tag); })),
          shouldExist_(iPSet.getUntrackedParameter<bool>("inputShouldExist")),
          shouldBeMissing_(iPSet.getUntrackedParameter<bool>("inputShouldBeMissing")) {
      if (shouldExist_ and shouldBeMissing_) {
        throw cms::Exception("Configuration")
            << "inputShouldExist and inputShouldBeMissing both can not be set to True";
      }
    }

    void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const override {
      for (auto const& token : tokensEvent_) {
        if (shouldExist_) {
          (void)iEvent.get(token);
        } else if (shouldBeMissing_) {
          auto h = iEvent.getHandle(token);
          if (h.isValid()) {
            edm::EDConsumerBase::Labels labels;
            labelsForToken(token, labels);
            throw cms::Exception("ProductExists")
                << "Product " << labels.module << ":" << labels.productInstance << ":" << labels.process
                << " exists, but expectation was it should be missing";
          }
        }
      }
    }

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<std::vector<edm::InputTag>>("srcBeginProcess", std::vector<edm::InputTag>{});
      desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
      desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
      desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
      desc.addUntracked<std::vector<edm::InputTag>>("srcEndLumi", std::vector<edm::InputTag>{});
      desc.addUntracked<std::vector<edm::InputTag>>("srcEndRun", std::vector<edm::InputTag>{});
      desc.addUntracked<std::vector<edm::InputTag>>("srcEndProcess", std::vector<edm::InputTag>{});
      desc.addUntracked<bool>("inputShouldExist", false);
      desc.addUntracked<bool>("inputShouldBeMissing", false);
      descriptions.addDefault(desc);
    }

  private:
    const std::vector<edm::EDGetTokenT<T>> tokensBeginProcess_;
    const std::vector<edm::EDGetTokenT<T>> tokensBeginRun_;
    const std::vector<edm::EDGetTokenT<T>> tokensBeginLumi_;
    const std::vector<edm::EDGetTokenT<T>> tokensEvent_;
    const std::vector<edm::EDGetTokenT<T>> tokensEndLumi_;
    const std::vector<edm::EDGetTokenT<T>> tokensEndRun_;
    const std::vector<edm::EDGetTokenT<T>> tokensEndProcess_;
    const bool shouldExist_;
    const bool shouldBeMissing_;
  };
  using GenericIntsAnalyzer = GenericAnalyzerT<IntProduct>;

  //--------------------------------------------------------------------
  //
  class IntConsumingAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    IntConsumingAnalyzer(edm::ParameterSet const& iPSet) {
      consumes<IntProduct>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
    }

    void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<edm::InputTag>("getFromModule");
      descriptions.addDefault(desc);
    }
  };
  //--------------------------------------------------------------------
  //
  class IntFromRunConsumingAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    IntFromRunConsumingAnalyzer(edm::ParameterSet const& iPSet) {
      consumes<IntProduct, edm::InRun>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
    }

    void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<edm::InputTag>("getFromModule");
      descriptions.addDefault(desc);
    }
  };
  //--------------------------------------------------------------------
  //
  class ConsumingStreamAnalyzer : public edm::stream::EDAnalyzer<> {
  public:
    ConsumingStreamAnalyzer(edm::ParameterSet const& iPSet)
        : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
          moduleLabel_(iPSet.getUntrackedParameter<std::string>("moduleLabel"), "") {
      mayConsume<IntProduct>(moduleLabel_);
    }

    void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
      edm::Handle<IntProduct> handle;
      iEvent.getByLabel(moduleLabel_, handle);
      if (handle->value != value_) {
        throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
                                               << " but it was supposed to be " << value_;
      }
    }

  private:
    int value_;
    edm::InputTag moduleLabel_;
  };

  //--------------------------------------------------------------------
  //
  class ConsumingOneSharedResourceAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
  public:
    ConsumingOneSharedResourceAnalyzer(edm::ParameterSet const& iPSet)
        : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
          moduleLabel_(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel")) {
      mayConsume<IntProduct>(moduleLabel_);
      usesResource(iPSet.getUntrackedParameter<std::string>("resourceName"));
    }

    void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
      edm::Handle<IntProduct> handle;
      iEvent.getByLabel(moduleLabel_, handle);
      if (handle->value != value_) {
        throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
                                               << " but it was supposed to be " << value_;
      }
    }

  private:
    int value_;
    edm::InputTag moduleLabel_;
  };

  //--------------------------------------------------------------------
  //
  class SCSimpleAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    SCSimpleAnalyzer(edm::ParameterSet const&) {}

    void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
  };

  void SCSimpleAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
    // Get the product back out; it should be sorted.
    edm::Handle<SCSimpleProduct> h;
    e.getByLabel("scs", h);
    assert(h.isValid());

    // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
    // copying all the values out of the SortedCollection so we can
    // manipulate them via an interface different from
    // SortedCollection, just so that we can make sure the collection
    // is sorted.
    std::vector<Simple> after(h->begin(), h->end());
    typedef std::vector<Simple>::size_type size_type;

    // Verify that the vector *is* sorted.

    for (size_type i = 1, end = after.size(); i < end; ++i) {
      assert(after[i - 1].id() < after[i].id());
    }
  }

  //--------------------------------------------------------------------
  //
  // Consumes View<Simple>
  //
  class SimpleViewAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    explicit SimpleViewAnalyzer(edm::ParameterSet const& p)
        : token_(consumes(p.getUntrackedParameter<edm::InputTag>("label"))),
          size_(p.getUntrackedParameter<unsigned int>("sizeMustMatch")),
          checkSize_(p.getUntrackedParameter<bool>("checkSize")) {}

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.addUntracked<bool>("checkSize", true);
      desc.addUntracked<unsigned int>("sizeMustMatch");
      desc.addUntracked<edm::InputTag>("label");
      descriptions.addDefault(desc);
    }

    void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
      if (checkSize_) {
        auto const& prod = e.get(token_);
        if (prod.size() != size_) {
          throw cms::Exception("SizeMismatch")
              << "Product size " << prod.size() << " differs from expectation " << size_;
        }
      }
    }

  private:
    edm::EDGetTokenT<edm::View<Simple>> const token_;
    unsigned int const size_;
    bool const checkSize_;
  };

  //--------------------------------------------------------------------
  //
  class DSVAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    DSVAnalyzer(edm::ParameterSet const&) {}

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

  private:
    void do_sorted_stuff(edm::Event const& e) const;
    void do_unsorted_stuff(edm::Event const& e) const;
  };

  void DSVAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
    do_sorted_stuff(e);
    do_unsorted_stuff(e);
  }

  void DSVAnalyzer::do_sorted_stuff(edm::Event const& e) const {
    typedef DSVSimpleProduct product_type;
    typedef product_type::value_type detset;
    typedef detset::value_type value_type;
    // Get the product back out; it should be sorted.
    edm::Handle<product_type> h;
    e.getByLabel("dsv1", h);
    assert(h.isValid());

    // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
    // copying all the values out of the DetSetVector's first DetSet so we can
    // manipulate them via an interface different from
    // DetSet, just so that we can make sure the collection
    // is sorted.
    std::vector<value_type> const& after = (h->end() - 1)->data;
    typedef std::vector<value_type>::size_type size_type;

    // Verify that the vector *is* sorted.

    for (size_type i = 1, end = after.size(); i < end; ++i) {
      assert(after[i - 1].data < after[i].data);
    }
  }

  void DSVAnalyzer::do_unsorted_stuff(edm::Event const& e) const {
    typedef DSVWeirdProduct product_type;
    typedef product_type::value_type detset;
    typedef detset::value_type value_type;
    // Get the product back out; it should be unsorted.
    edm::Handle<product_type> h;
    e.getByLabel("dsv1", h);
    assert(h.isValid());

    // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
    // copying all the values out of the DetSetVector's first DetSet so we can
    // manipulate them via an interface different from
    // DetSet, just so that we can make sure the collection
    // is not sorted.
    std::vector<value_type> const& after = (h->end() - 1)->data;
    typedef std::vector<value_type>::size_type size_type;

    // Verify that the vector is reverse-sorted.

    for (size_type i = 1, end = after.size(); i < end; ++i) {
      assert(after[i - 1].data > after[i].data);
    }
  }
}  // namespace edmtest

using edmtest::BuiltinIntTestAnalyzer;
using edmtest::ConsumingOneSharedResourceAnalyzer;
using edmtest::ConsumingStreamAnalyzer;
using edmtest::DSVAnalyzer;
using edmtest::IntConsumingAnalyzer;
using edmtest::IntTestAnalyzer;
using edmtest::MultipleIntsAnalyzer;
using edmtest::NonAnalyzer;
using edmtest::SCSimpleAnalyzer;
using edmtest::SimpleViewAnalyzer;
DEFINE_FWK_MODULE(NonAnalyzer);
DEFINE_FWK_MODULE(IntTestAnalyzer);
DEFINE_FWK_MODULE(MultipleIntsAnalyzer);
DEFINE_FWK_MODULE(edmtest::GenericIntsAnalyzer);
DEFINE_FWK_MODULE(IntConsumingAnalyzer);
DEFINE_FWK_MODULE(edmtest::IntFromRunConsumingAnalyzer);
DEFINE_FWK_MODULE(ConsumingStreamAnalyzer);
DEFINE_FWK_MODULE(ConsumingOneSharedResourceAnalyzer);
DEFINE_FWK_MODULE(SCSimpleAnalyzer);
DEFINE_FWK_MODULE(SimpleViewAnalyzer);
DEFINE_FWK_MODULE(DSVAnalyzer);
DEFINE_FWK_MODULE(BuiltinIntTestAnalyzer);