EventContentAnalyzer

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
// -*- C++ -*-
//
// Package:    Modules
// Class:      EventContentAnalyzer
//
/**
 Description: <one line class summary>

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Chris Jones
//         Created:  Mon Sep 19 11:47:28 CEST 2005
//
//

// user include files
#include "DataFormats/Provenance/interface/Provenance.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/GenericHandle.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterDescriptionNode.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/Algorithms.h"
#include "FWCore/Reflection/interface/FunctionWithDict.h"
#include "FWCore/Reflection/interface/MemberWithDict.h"
#include "FWCore/Reflection/interface/ObjectWithDict.h"
#include "FWCore/Reflection/interface/TypeWithDict.h"
#include "FWCore/Utilities/interface/TypeToGet.h"
#include "FWCore/ParameterSet/interface/Registry.h"

// system include files
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

namespace edm {
  class ConfigurationDescriptions;
  namespace {
    std::string formatClassName(std::string const& iName) { return std::string("(") + iName + ")"; }

    char const* kNameValueSep = "=";
    ///convert the object information to the correct type and print it
    template <typename T>
    void doPrint(std::string const& iName, ObjectWithDict const& iObject, std::string const& iIndent) {
      LogAbsolute("EventContent") << iIndent << iName << kNameValueSep
                                  << *reinterpret_cast<T*>(iObject.address());  // << "\n";
    }

    template <>
    void doPrint<char>(std::string const& iName, ObjectWithDict const& iObject, std::string const& iIndent) {
      LogAbsolute("EventContent") << iIndent << iName << kNameValueSep
                                  << static_cast<int>(*reinterpret_cast<char*>(iObject.address()));  // << "\n";
    }

    template <>
    void doPrint<unsigned char>(std::string const& iName, ObjectWithDict const& iObject, std::string const& iIndent) {
      LogAbsolute("EventContent") << iIndent << iName << kNameValueSep
                                  << static_cast<unsigned int>(
                                         *reinterpret_cast<unsigned char*>(iObject.address()));  // << "\n";
    }

    template <>
    void doPrint<bool>(std::string const& iName, ObjectWithDict const& iObject, std::string const& iIndent) {
      LogAbsolute("EventContent") << iIndent << iName << kNameValueSep
                                  << ((*reinterpret_cast<bool*>(iObject.address())) ? "true" : "false");  // << "\n";
    }

    typedef void (*FunctionType)(std::string const&, ObjectWithDict const&, std::string const&);
    typedef std::map<std::string, FunctionType> TypeToPrintMap;

    template <typename T>
    void addToMap(TypeToPrintMap& iMap) {
      iMap[typeid(T).name()] = doPrint<T>;
    }

    bool printAsBuiltin(std::string const& iName, ObjectWithDict const& iObject, std::string const& iIndent) {
      typedef void (*FunctionType)(std::string const&, ObjectWithDict const&, std::string const&);
      typedef std::map<std::string, FunctionType> TypeToPrintMap;
      static TypeToPrintMap s_map;
      static bool isFirst = true;
      if (isFirst) {
        addToMap<bool>(s_map);
        addToMap<char>(s_map);
        addToMap<short>(s_map);
        addToMap<int>(s_map);
        addToMap<long>(s_map);
        addToMap<unsigned char>(s_map);
        addToMap<unsigned short>(s_map);
        addToMap<unsigned int>(s_map);
        addToMap<unsigned long>(s_map);
        addToMap<float>(s_map);
        addToMap<double>(s_map);
        isFirst = false;
      }
      TypeToPrintMap::iterator itFound = s_map.find(iObject.typeOf().name());
      if (itFound == s_map.end()) {
        return false;
      }
      itFound->second(iName, iObject, iIndent);
      return true;
    }

    bool printAsContainer(std::string const& iName,
                          ObjectWithDict const& iObject,
                          std::string const& iIndent,
                          std::string const& iIndentDelta);

    void printObject(std::string const& iName,
                     ObjectWithDict const& iObject,
                     std::string const& iIndent,
                     std::string const& iIndentDelta) {
      const std::string& printName = iName;
      const ObjectWithDict& objectToPrint = iObject;
      std::string indent(iIndent);
      if (iObject.typeOf().isPointer()) {
        LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << formatClassName(iObject.typeOf().name())
                                    << std::hex << iObject.address() << std::dec;  // << "\n";
        TypeWithDict pointedType = iObject.typeOf().toType();  // for Pointers, I get the real type this way
        if (TypeWithDict::byName("void") == pointedType || pointedType.isPointer() || iObject.address() == nullptr) {
          return;
        }
        return;
        /*
          //have the code that follows print the contents of the data to which the pointer points
          objectToPrint = ObjectWithDict(pointedType, iObject.address());
          //try to convert it to its actual type (assuming the original type was a base class)
          objectToPrint = ObjectWithDict(objectToPrint.castObject(objectToPrint.dynamicType()));
          printName = std::string("*")+iName;
          indent += iIndentDelta;
          */
      }
      std::string typeName(objectToPrint.typeOf().name());
      if (typeName.empty()) {
        typeName = "<unknown>";
      }

      if (printAsBuiltin(printName, objectToPrint, indent)) {
        return;
      }
      if (printAsContainer(printName, objectToPrint, indent, iIndentDelta)) {
        return;
      }

      LogAbsolute("EventContent") << indent << printName << " " << formatClassName(typeName);  // << "\n";
      indent += iIndentDelta;
      //print all the data members
      TypeDataMembers dataMembers(objectToPrint.typeOf());
      for (auto const& dataMember : dataMembers) {
        MemberWithDict const member(dataMember);
        //LogAbsolute("EventContent") << "     debug " << member.name() << " " << member.typeName() << "\n";
        try {
          printObject(member.name(), member.get(objectToPrint), indent, iIndentDelta);
        } catch (std::exception& iEx) {
          LogAbsolute("EventContent") << indent << member.name() << " <exception caught(" << iEx.what() << ")>\n";
        }
      }
    }

    bool printAsContainer(std::string const& iName,
                          ObjectWithDict const& iObject,
                          std::string const& iIndent,
                          std::string const& iIndentDelta) {
      ObjectWithDict sizeObj;
      try {
        size_t temp;  //used to hold the memory for the return value
        FunctionWithDict sizeFunc = iObject.typeOf().functionMemberByName("size");
        assert(sizeFunc.finalReturnType() == typeid(size_t));
        sizeObj = ObjectWithDict(TypeWithDict(typeid(size_t)), &temp);
        sizeFunc.invoke(iObject, &sizeObj);
        //std::cout << "size of type '" << sizeObj.name() << "' " << sizeObj.typeName() << std::endl;
        size_t size = *reinterpret_cast<size_t*>(sizeObj.address());
        FunctionWithDict atMember;
        try {
          atMember = iObject.typeOf().functionMemberByName("at");
        } catch (std::exception const& x) {
          //std::cerr << "could not get 'at' member because " << x.what() << std::endl;
          return false;
        }
        LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << "[size=" << size << "]";  //"\n";
        ObjectWithDict contained;
        std::string indexIndent = iIndent + iIndentDelta;
        TypeWithDict atReturnType(atMember.finalReturnType());
        //std::cout << "return type " << atReturnType.name() << " size of " << atReturnType.SizeOf()
        // << " pointer? " << atReturnType.isPointer() << " ref? " << atReturnType.isReference() << std::endl;

        //Return by reference must be treated differently since reflex will not properly create
        // memory for a ref (which should just be a pointer to the object and not the object itself)
        //So we will create memory on the stack which can be used to hold a reference
        bool const isRef = atReturnType.isReference();
        void* refMemoryBuffer = nullptr;
        size_t index = 0;
        //The argument to the 'at' function is the index. Since the argument list holds pointers to the arguments
        // we only need to create it once and then when the value of index changes the pointer already
        // gets the new value
        std::vector<void*> args;
        args.push_back(&index);
        for (; index != size; ++index) {
          std::ostringstream sizeS;
          sizeS << "[" << index << "]";
          if (isRef) {
            ObjectWithDict refObject(atReturnType, &refMemoryBuffer);
            atMember.invoke(iObject, &refObject, args);
            //Although to hold the return value from a reference reflex requires you to pass it a
            // void** when it tries to call methods on the reference it expects to be given a void*
            contained = ObjectWithDict(atReturnType, refMemoryBuffer);
          } else {
            contained = atReturnType.construct();
            atMember.invoke(iObject, &contained, args);
          }
          //LogAbsolute("EventContent") << "invoked 'at'" << std::endl;
          try {
            printObject(sizeS.str(), contained, indexIndent, iIndentDelta);
          } catch (std::exception& iEx) {
            LogAbsolute("EventContent") << indexIndent << iName << " <exception caught(" << iEx.what() << ")>\n";
          }
          if (!isRef) {
            contained.destruct(true);
          }
        }
        return true;
      } catch (std::exception const& x) {
        //std::cerr << "failed to invoke 'at' because " << x.what() << std::endl;
        return false;
      }
      return false;
    }

    void printObject(Event const& iEvent,
                     std::string const& iClassName,
                     std::string const& iModuleLabel,
                     std::string const& iInstanceLabel,
                     std::string const& iProcessName,
                     std::string const& iIndent,
                     std::string const& iIndentDelta) {
      try {
        GenericHandle handle(iClassName);
      } catch (edm::Exception const&) {
        LogAbsolute("EventContent") << iIndent << " \"" << iClassName << "\""
                                    << " is an unknown type" << std::endl;
        return;
      }
      GenericHandle handle(iClassName);
      iEvent.getByLabel(InputTag(iModuleLabel, iInstanceLabel, iProcessName), handle);
      std::string className = formatClassName(iClassName);
      printObject(className, *handle, iIndent, iIndentDelta);
    }
  }  // namespace

  class EventContentAnalyzer : public one::EDAnalyzer<> {
  public:
    explicit EventContentAnalyzer(ParameterSet const&);
    ~EventContentAnalyzer() override;

    void analyze(Event const&, EventSetup const&) override;
    void endJob() override;

    static void fillDescriptions(ConfigurationDescriptions& descriptions);

  private:
    // ----------member data ---------------------------
    std::string indentation_;
    std::string verboseIndentation_;
    std::vector<std::string> moduleLabels_;
    bool verbose_;
    std::vector<std::string> getModuleLabels_;
    bool getData_;
    int evno_;
    std::map<std::string, int> cumulates_;
    bool listContent_;
    bool listProvenance_;
    bool listPathStatus_;
  };

  //
  // constructors and destructor
  //
  EventContentAnalyzer::EventContentAnalyzer(ParameterSet const& iConfig)
      : indentation_(iConfig.getUntrackedParameter<std::string>("indentation")),
        verboseIndentation_(iConfig.getUntrackedParameter<std::string>("verboseIndentation")),
        moduleLabels_(iConfig.getUntrackedParameter<std::vector<std::string>>("verboseForModuleLabels")),
        verbose_(iConfig.getUntrackedParameter<bool>("verbose") || !moduleLabels_.empty()),
        getModuleLabels_(iConfig.getUntrackedParameter<std::vector<std::string>>("getDataForModuleLabels")),
        getData_(iConfig.getUntrackedParameter<bool>("getData") || !getModuleLabels_.empty()),
        evno_(1),
        listContent_(iConfig.getUntrackedParameter<bool>("listContent")),
        listProvenance_(iConfig.getUntrackedParameter<bool>("listProvenance")),
        listPathStatus_(iConfig.getUntrackedParameter<bool>("listPathStatus")) {
    //now do what ever initialization is needed
    sort_all(moduleLabels_);
    sort_all(getModuleLabels_);
    if (getData_) {
      callWhenNewProductsRegistered([this](edm::ProductDescription const& iBranch) {
        if (getModuleLabels_.empty()) {
          const std::string kPathStatus("edm::PathStatus");
          const std::string kEndPathStatus("edm::EndPathStatus");
          if (iBranch.className() != kPathStatus && iBranch.className() != kEndPathStatus) {
            this->consumes(edm::TypeToGet{iBranch.unwrappedTypeID(), PRODUCT_TYPE},
                           edm::InputTag{iBranch.moduleLabel(), iBranch.productInstanceName(), iBranch.processName()});
          }
        } else {
          for (auto const& mod : this->getModuleLabels_) {
            if (iBranch.moduleLabel() == mod) {
              this->consumes(edm::TypeToGet{iBranch.unwrappedTypeID(), PRODUCT_TYPE},
                             edm::InputTag{mod, iBranch.productInstanceName(), iBranch.processName()});
              break;
            }
          }
        }
      });
    }
  }

  EventContentAnalyzer::~EventContentAnalyzer() {
    // do anything here that needs to be done at destruction time
    // (e.g. close files, deallocate resources etc.)
  }

  //
  // member functions
  //

  // ------------ method called to produce the data  ------------
  void EventContentAnalyzer::analyze(Event const& iEvent, EventSetup const&) {
    typedef std::vector<StableProvenance const*> Provenances;
    Provenances provenances;

    iEvent.getAllStableProvenance(provenances);

    if (listContent_) {
      LogAbsolute("EventContent") << "\n"
                                  << indentation_ << "Event " << std::setw(5) << evno_ << " contains "
                                  << provenances.size() << " product" << (provenances.size() == 1 ? "" : "s")
                                  << " with friendlyClassName, moduleLabel, productInstanceName and processName:"
                                  << std::endl;
    }

    std::string startIndent = indentation_ + verboseIndentation_;
    for (auto const& provenance : provenances) {
      std::string const& className = provenance->className();
      const std::string kPathStatus("edm::PathStatus");
      const std::string kEndPathStatus("edm::EndPathStatus");
      if (not listPathStatus_ and (className == kPathStatus || className == kEndPathStatus)) {
        continue;
      }
      std::string const& friendlyName = provenance->friendlyClassName();
      //if(friendlyName.empty())  friendlyName = std::string("||");

      std::string const& modLabel = provenance->moduleLabel();
      //if(modLabel.empty()) modLabel = std::string("||");

      std::string const& instanceName = provenance->productInstanceName();
      //if(instanceName.empty()) instanceName = std::string("||");

      std::string const& processName = provenance->processName();

      bool doVerbose = verbose_ && (moduleLabels_.empty() || binary_search_all(moduleLabels_, modLabel));

      if (listContent_ || doVerbose) {
        LogAbsolute("EventContent") << indentation_ << friendlyName << " \"" << modLabel << "\" \"" << instanceName
                                    << "\" \"" << processName << "\""
                                    << " (productId = " << provenance->productID() << ")" << std::endl;

        if (listProvenance_) {
          const bool isAlias = provenance->productDescription().isAlias();
          std::string aliasForModLabel;
          LogAbsolute("EventContent") << *provenance;
          if (isAlias) {
            aliasForModLabel = iEvent.getStableProvenance(provenance->originalBranchID()).moduleLabel();
            LogAbsolute("EventContent") << "Is an alias for " << aliasForModLabel;
          }
          ProcessHistory const& processHistory = iEvent.processHistory();
          for (ProcessConfiguration const& pc : processHistory) {
            if (pc.processName() == provenance->processName()) {
              ParameterSetID const& psetID = pc.parameterSetID();
              pset::Registry const* psetRegistry = pset::Registry::instance();
              ParameterSet const* processPset = psetRegistry->getMapped(psetID);
              if (processPset) {
                if (processPset->existsAs<ParameterSet>(modLabel)) {
                  if (isAlias) {
                    LogAbsolute("EventContent") << "Alias PSet";
                  }
                  LogAbsolute("EventContent") << processPset->getParameterSet(modLabel);
                }
                if (isAlias and processPset->existsAs<ParameterSet>(aliasForModLabel)) {
                  LogAbsolute("EventContent") << processPset->getParameterSet(aliasForModLabel);
                }
              }
            }
          }
        }
      }
      std::string key = friendlyName + std::string(" + \"") + modLabel + std::string("\" + \"") + instanceName +
                        "\" \"" + processName + "\"";
      ++cumulates_[key];

      if (doVerbose) {
        //indent one level before starting to print
        printObject(iEvent, className, modLabel, instanceName, processName, startIndent, verboseIndentation_);
        continue;
      }
      if (getData_) {
        std::string class_and_label = friendlyName + "_" + modLabel;
        if (getModuleLabels_.empty() || binary_search_all(getModuleLabels_, modLabel) ||
            binary_search_all(getModuleLabels_, class_and_label)) {
          try {
            GenericHandle handle(className);
          } catch (edm::Exception const&) {
            LogAbsolute("EventContent") << startIndent << " \"" << className << "\""
                                        << " is an unknown type" << std::endl;
            return;
          }
          GenericHandle handle(className);
          iEvent.getByLabel(InputTag(modLabel, instanceName, processName), handle);
        }
      }
    }
    //std::cout << "Mine" << std::endl;
    ++evno_;
  }

  // ------------ method called at end of job -------------------
  void EventContentAnalyzer::endJob() {
    typedef std::map<std::string, int> nameMap;

    LogAbsolute("EventContent") << "\nSummary for key being the concatenation of friendlyClassName, moduleLabel, "
                                   "productInstanceName and processName"
                                << std::endl;
    for (nameMap::const_iterator it = cumulates_.begin(), itEnd = cumulates_.end(); it != itEnd; ++it) {
      LogAbsolute("EventContent") << std::setw(6) << it->second << " occurrences of key " << it->first << std::endl;
    }
  }

  void EventContentAnalyzer::fillDescriptions(ConfigurationDescriptions& descriptions) {
    descriptions.setComment(
        "This plugin will print a list of all products in the event "
        "provenance.  It also has options to print and/or get each product.");

    ParameterSetDescription desc;

    ParameterDescriptionNode* np;

    std::string defaultString("++");
    np = desc.addUntracked<std::string>("indentation", defaultString);
    np->setComment("This string is printed at the beginning of every line printed during event processing.");

    np = desc.addUntracked<bool>("verbose", false);
    np->setComment("If true, the contents of products are printed.");

    defaultString = "  ";
    np = desc.addUntracked<std::string>("verboseIndentation", defaultString);
    np->setComment(
        "This string is used to further indent lines when printing the contents of products in verbose mode.");

    std::vector<std::string> defaultVString;

    np = desc.addUntracked<std::vector<std::string>>("verboseForModuleLabels", defaultVString);
    np->setComment("If this vector is not empty, then only products with module labels on this list are printed.");

    np = desc.addUntracked<bool>("getData", false);
    np->setComment("If true the products will be retrieved using getByLabel.");

    np = desc.addUntracked<std::vector<std::string>>("getDataForModuleLabels", defaultVString);
    np->setComment(
        "If this vector is not empty, then only products with module labels on this list are retrieved by getByLabel.");

    np = desc.addUntracked<bool>("listContent", true);
    np->setComment("If true then print a list of all the event content.");

    np = desc.addUntracked<bool>("listProvenance", false);
    np->setComment("If true, and if listContent or verbose is true, print provenance information for each product");

    desc.addUntracked<bool>("listPathStatus", false)
        ->setComment("If true, also show PathStatus/EndPathStatus data products.");
    descriptions.add("printContent", desc);
    descriptions.addDefault(desc);
  }
}  // namespace edm

using edm::EventContentAnalyzer;
DEFINE_FWK_MODULE(EventContentAnalyzer);