MEtoEDMConverter

Void

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
/** \class MEtoEDMConverter
 *
 *  Class to take dqm monitor elements and convert into a
 *  ROOT dataformat stored in Run tree of edm file
 *
 *  \author M. Strang SUNY-Buffalo
 */

// framework & common header files
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Histograms/interface/DQMToken.h"
#include "DataFormats/Histograms/interface/MEtoEDMFormat.h"
#include "DataFormats/Provenance/interface/Provenance.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/one/EDProducer.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/GetterOfProducts.h"
#include "FWCore/Framework/interface/ProcessMatch.h"

//DQM services
#include "DQMServices/Core/interface/DQMStore.h"
#include "FWCore/ServiceRegistry/interface/Service.h"

#include "classlib/utils/StringList.h"
#include "classlib/utils/StringOps.h"

// helper files
#include <iostream>
#include <cstdlib>
#include <string>
#include <memory>
#include <vector>
#include <map>
#include <cassert>
#include <cstdint>

#include "TString.h"
#include "TH1F.h"
#include "TH1S.h"
#include "TH1D.h"
#include "TH1I.h"
#include "TH2F.h"
#include "TH2S.h"
#include "TH2D.h"
#include "TH2I.h"
#include "TH2Poly.h"
#include "TH3F.h"
#include "TProfile.h"
#include "TProfile2D.h"
#include "TObjString.h"

namespace meedm {
  struct Void {};
}  // namespace meedm

//Using RunCache and LuminosityBlockCache tells the framework the module is able to
// allow multiple concurrent Runs and LuminosityBlocks.

class MEtoEDMConverter : public edm::one::EDProducer<edm::RunCache<meedm::Void>,
                                                     edm::LuminosityBlockCache<meedm::Void>,
                                                     edm::EndLuminosityBlockProducer,
                                                     edm::EndRunProducer,
                                                     edm::one::SharedResources> {
public:
  typedef dqm::legacy::DQMStore DQMStore;
  typedef dqm::legacy::MonitorElement MonitorElement;

  explicit MEtoEDMConverter(const edm::ParameterSet&);
  ~MEtoEDMConverter() override;
  void beginJob() override;
  void produce(edm::Event&, const edm::EventSetup&) override;
  std::shared_ptr<meedm::Void> globalBeginRun(edm::Run const&, const edm::EventSetup&) const override;
  void globalEndRun(edm::Run const&, const edm::EventSetup&) override;
  void endRunProduce(edm::Run&, const edm::EventSetup&) override;
  void endLuminosityBlockProduce(edm::LuminosityBlock&, const edm::EventSetup&) override;
  void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {}
  std::shared_ptr<meedm::Void> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
                                                          edm::EventSetup const&) const override;

  template <class T>
  void putData(DQMStore::IGetter& g, T& iPutTo, bool iLumiOnly, uint32_t run, uint32_t lumi);

  using TagList = std::vector<uint32_t>;

private:
  std::string fName;
  int verbosity;
  int frequency;
  std::string path;

  // private statistics information
  std::map<int, int> iCount;
  edm::GetterOfProducts<DQMToken> lumigetter_;
  edm::GetterOfProducts<DQMToken> rungetter_;

};  // end class declaration

using namespace lat;

MEtoEDMConverter::MEtoEDMConverter(const edm::ParameterSet& iPSet) : fName(""), verbosity(0), frequency(0) {
  std::string MsgLoggerCat = "MEtoEDMConverter_MEtoEDMConverter";

  // get information from parameter set
  fName = iPSet.getUntrackedParameter<std::string>("Name", "MEtoEDMConverter");
  verbosity = iPSet.getUntrackedParameter<int>("Verbosity", 0);
  frequency = iPSet.getUntrackedParameter<int>("Frequency", 50);
  path = iPSet.getUntrackedParameter<std::string>("MEPathToSave");
  // use value of first digit to determine default output level (inclusive)
  // 0 is none, 1 is basic, 2 is fill output, 3 is gather output
  verbosity %= 10;

  // print out Parameter Set information being used
  if (verbosity >= 0) {
    edm::LogInfo(MsgLoggerCat) << "\n===============================\n"
                               << "Initialized as EDProducer with parameter values:\n"
                               << "    Name          = " << fName << "\n"
                               << "    Verbosity     = " << verbosity << "\n"
                               << "    Frequency     = " << frequency << "\n"
                               << "    Path          = " << path << "\n"
                               << "===============================\n";
  }

  std::string sName;

  // create persistent objects

  sName = fName + "Run";
  produces<MEtoEDM<TH1F>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH1S>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH1D>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH1I>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH2F>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH2S>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH2D>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH2I>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH2Poly>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TH3F>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TProfile>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TProfile2D>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<double>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<long long>, edm::Transition::EndRun>(sName);
  produces<MEtoEDM<TString>, edm::Transition::EndRun>(sName);

  sName = fName + "Lumi";
  produces<MEtoEDM<TH1F>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH1S>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH1D>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH1I>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH2F>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH2S>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH2D>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH2I>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH2Poly>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TH3F>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TProfile>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TProfile2D>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<double>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<long long>, edm::Transition::EndLuminosityBlock>(sName);
  produces<MEtoEDM<TString>, edm::Transition::EndLuminosityBlock>(sName);

  lumigetter_ = edm::GetterOfProducts<DQMToken>(edm::ProcessMatch("*"), this, edm::InLumi);
  rungetter_ = edm::GetterOfProducts<DQMToken>(edm::ProcessMatch("*"), this, edm::InRun);
  callWhenNewProductsRegistered([this](edm::ProductDescription const& bd) {
    this->lumigetter_(bd);
    this->rungetter_(bd);
  });
  usesResource("DQMStore");

  static_assert(sizeof(int64_t) == sizeof(long long), "type int64_t is not the same length as long long");
}

MEtoEDMConverter::~MEtoEDMConverter() = default;

void MEtoEDMConverter::beginJob() {}

std::shared_ptr<meedm::Void> MEtoEDMConverter::globalBeginRun(edm::Run const& iRun,
                                                              const edm::EventSetup& iSetup) const {
  return std::shared_ptr<meedm::Void>();
}

void MEtoEDMConverter::globalEndRun(edm::Run const& iRun, const edm::EventSetup& iSetup) {}

void MEtoEDMConverter::endRunProduce(edm::Run& iRun, const edm::EventSetup& iSetup) {
  DQMStore* store = edm::Service<DQMStore>().operator->();
  store->meBookerGetter([&](DQMStore::IBooker& b, DQMStore::IGetter& g) { putData(g, iRun, false, iRun.run(), 0); });
}

std::shared_ptr<meedm::Void> MEtoEDMConverter::globalBeginLuminosityBlock(edm::LuminosityBlock const&,
                                                                          edm::EventSetup const&) const {
  return std::shared_ptr<meedm::Void>();
}

void MEtoEDMConverter::endLuminosityBlockProduce(edm::LuminosityBlock& iLumi, const edm::EventSetup& iSetup) {
  DQMStore* store = edm::Service<DQMStore>().operator->();
  store->meBookerGetter([&](DQMStore::IBooker& b, DQMStore::IGetter& g) {
    putData(g, iLumi, true, iLumi.run(), iLumi.id().luminosityBlock());
  });
}

template <class T>
void MEtoEDMConverter::putData(DQMStore::IGetter& iGetter, T& iPutTo, bool iLumiOnly, uint32_t run, uint32_t lumi) {
  std::string MsgLoggerCat = "MEtoEDMConverter_putData";

  if (verbosity > 0)
    edm::LogInfo(MsgLoggerCat) << "\nStoring MEtoEDM dataformat histograms.";

  // extract ME information into vectors
  std::vector<MonitorElement*>::iterator mmi, mme;
  std::vector<MonitorElement*> items(iGetter.getAllContents(path, run, lumi));

  unsigned int n1F = 0;
  unsigned int n1S = 0;
  unsigned int n1D = 0;
  unsigned int n1I = 0;
  unsigned int n2F = 0;
  unsigned int n2S = 0;
  unsigned int n2D = 0;
  unsigned int n2I = 0;
  unsigned int n2P = 0;
  unsigned int n3F = 0;
  unsigned int nProf = 0;
  unsigned int nProf2 = 0;
  unsigned int nDouble = 0;
  unsigned int nInt64 = 0;
  unsigned int nString = 0;

  for (mmi = items.begin(), mme = items.end(); mmi != mme; ++mmi) {
    MonitorElement* me = *mmi;

    // store only flagged ME at endLumi transition, and Run-based
    // histo at endRun transition
    if (iLumiOnly && !me->getLumiFlag())
      continue;
    if (!iLumiOnly && me->getLumiFlag())
      continue;

    switch (me->kind()) {
      case MonitorElement::Kind::INT:
        ++nInt64;
        break;

      case MonitorElement::Kind::REAL:
        ++nDouble;
        break;

      case MonitorElement::Kind::STRING:
        ++nString;
        break;

      case MonitorElement::Kind::TH1F:
        ++n1F;
        break;

      case MonitorElement::Kind::TH1S:
        ++n1S;
        break;

      case MonitorElement::Kind::TH1D:
        ++n1D;
        break;

      case MonitorElement::Kind::TH1I:
        ++n1I;
        break;

      case MonitorElement::Kind::TH2F:
        ++n2F;
        break;

      case MonitorElement::Kind::TH2S:
        ++n2S;
        break;

      case MonitorElement::Kind::TH2D:
        ++n2D;
        break;

      case MonitorElement::Kind::TH2I:
        ++n2I;
        break;

      case MonitorElement::Kind::TH2Poly:
        ++n2P;
        break;

      case MonitorElement::Kind::TH3F:
        ++n3F;
        break;

      case MonitorElement::Kind::TPROFILE:
        ++nProf;
        break;

      case MonitorElement::Kind::TPROFILE2D:
        ++nProf2;
        break;

      default:
        edm::LogError(MsgLoggerCat) << "ERROR: The DQM object '" << me->getFullname()
                                    << "' is neither a ROOT object nor a recognised "
                                    << "simple object.\n";
        continue;
    }
  }

  std::unique_ptr<MEtoEDM<long long> > pOutInt(new MEtoEDM<long long>(nInt64));
  std::unique_ptr<MEtoEDM<double> > pOutDouble(new MEtoEDM<double>(nDouble));
  std::unique_ptr<MEtoEDM<TString> > pOutString(new MEtoEDM<TString>(nString));
  std::unique_ptr<MEtoEDM<TH1F> > pOut1(new MEtoEDM<TH1F>(n1F));
  std::unique_ptr<MEtoEDM<TH1S> > pOut1s(new MEtoEDM<TH1S>(n1S));
  std::unique_ptr<MEtoEDM<TH1D> > pOut1d(new MEtoEDM<TH1D>(n1D));
  std::unique_ptr<MEtoEDM<TH1I> > pOut1i(new MEtoEDM<TH1I>(n1I));
  std::unique_ptr<MEtoEDM<TH2F> > pOut2(new MEtoEDM<TH2F>(n2F));
  std::unique_ptr<MEtoEDM<TH2S> > pOut2s(new MEtoEDM<TH2S>(n2S));
  std::unique_ptr<MEtoEDM<TH2D> > pOut2d(new MEtoEDM<TH2D>(n2D));
  std::unique_ptr<MEtoEDM<TH2I> > pOut2i(new MEtoEDM<TH2I>(n2I));
  std::unique_ptr<MEtoEDM<TH2Poly> > pOut2p(new MEtoEDM<TH2Poly>(n2P));
  std::unique_ptr<MEtoEDM<TH3F> > pOut3(new MEtoEDM<TH3F>(n3F));
  std::unique_ptr<MEtoEDM<TProfile> > pOutProf(new MEtoEDM<TProfile>(nProf));
  std::unique_ptr<MEtoEDM<TProfile2D> > pOutProf2(new MEtoEDM<TProfile2D>(nProf2));

  for (mmi = items.begin(), mme = items.end(); mmi != mme; ++mmi) {
    MonitorElement* me = *mmi;

    // store only flagged ME at endLumi transition, and Run-based
    // histo at endRun transition
    // DQMStore should only hand out matching MEs
    assert(iLumiOnly == me->getLumiFlag());

    // get monitor elements
    switch (me->kind()) {
      case MonitorElement::Kind::INT:
        pOutInt->putMEtoEdmObject(me->getFullname(), me->getIntValue());
        break;

      case MonitorElement::Kind::REAL:
        pOutDouble->putMEtoEdmObject(me->getFullname(), me->getFloatValue());
        break;

      case MonitorElement::Kind::STRING:
        pOutString->putMEtoEdmObject(me->getFullname(), me->getStringValue());
        break;

      case MonitorElement::Kind::TH1F:
        pOut1->putMEtoEdmObject(me->getFullname(), *me->getTH1F());
        break;

      case MonitorElement::Kind::TH1S:
        pOut1s->putMEtoEdmObject(me->getFullname(), *me->getTH1S());
        break;

      case MonitorElement::Kind::TH1D:
        pOut1d->putMEtoEdmObject(me->getFullname(), *me->getTH1D());
        break;

      case MonitorElement::Kind::TH1I:
        pOut1i->putMEtoEdmObject(me->getFullname(), *me->getTH1I());
        break;

      case MonitorElement::Kind::TH2F:
        pOut2->putMEtoEdmObject(me->getFullname(), *me->getTH2F());
        break;

      case MonitorElement::Kind::TH2S:
        pOut2s->putMEtoEdmObject(me->getFullname(), *me->getTH2S());
        break;

      case MonitorElement::Kind::TH2D:
        pOut2d->putMEtoEdmObject(me->getFullname(), *me->getTH2D());
        break;

      case MonitorElement::Kind::TH2I:
        pOut2i->putMEtoEdmObject(me->getFullname(), *me->getTH2I());
        break;

      case MonitorElement::Kind::TH2Poly:
        pOut2p->putMEtoEdmObject(me->getFullname(), *me->getTH2Poly());
        break;

      case MonitorElement::Kind::TH3F:
        pOut3->putMEtoEdmObject(me->getFullname(), *me->getTH3F());
        break;

      case MonitorElement::Kind::TPROFILE:
        pOutProf->putMEtoEdmObject(me->getFullname(), *me->getTProfile());
        break;

      case MonitorElement::Kind::TPROFILE2D:
        pOutProf2->putMEtoEdmObject(me->getFullname(), *me->getTProfile2D());
        break;

      default:
        edm::LogError(MsgLoggerCat) << "ERROR: The DQM object '" << me->getFullname()
                                    << "' is neither a ROOT object nor a recognised "
                                    << "simple object.\n";
        continue;
    }

  }  // end loop through monitor elements

  std::string sName;

  if (iLumiOnly) {
    sName = fName + "Lumi";
  } else {
    sName = fName + "Run";
  }

  // produce objects to put in events
  iPutTo.put(std::move(pOutInt), sName);
  iPutTo.put(std::move(pOutDouble), sName);
  iPutTo.put(std::move(pOutString), sName);
  iPutTo.put(std::move(pOut1), sName);
  iPutTo.put(std::move(pOut1s), sName);
  iPutTo.put(std::move(pOut1d), sName);
  iPutTo.put(std::move(pOut2), sName);
  iPutTo.put(std::move(pOut2s), sName);
  iPutTo.put(std::move(pOut2d), sName);
  iPutTo.put(std::move(pOut2i), sName);
  iPutTo.put(std::move(pOut2p), sName);
  iPutTo.put(std::move(pOut3), sName);
  iPutTo.put(std::move(pOutProf), sName);
  iPutTo.put(std::move(pOutProf2), sName);
}

void MEtoEDMConverter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {}

#include "FWCore/PluginManager/interface/ModuleDef.h"
#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(MEtoEDMConverter);