Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:08

0001 /** \class MEtoEDMConverter
0002  *
0003  *  Class to take dqm monitor elements and convert into a
0004  *  ROOT dataformat stored in Run tree of edm file
0005  *
0006  *  \author M. Strang SUNY-Buffalo
0007  */
0008 
0009 // framework & common header files
0010 #include "DataFormats/Common/interface/Handle.h"
0011 #include "DataFormats/Histograms/interface/DQMToken.h"
0012 #include "DataFormats/Histograms/interface/MEtoEDMFormat.h"
0013 #include "DataFormats/Provenance/interface/Provenance.h"
0014 #include "FWCore/Framework/interface/ESHandle.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/Framework/interface/EventSetup.h"
0017 #include "FWCore/Framework/interface/Frameworkfwd.h"
0018 #include "FWCore/Framework/interface/LuminosityBlock.h"
0019 #include "FWCore/Framework/interface/MakerMacros.h"
0020 #include "FWCore/Framework/interface/Run.h"
0021 #include "FWCore/Framework/interface/one/EDProducer.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/Framework/interface/GetterOfProducts.h"
0025 #include "FWCore/Framework/interface/ProcessMatch.h"
0026 
0027 //DQM services
0028 #include "DQMServices/Core/interface/DQMStore.h"
0029 #include "FWCore/ServiceRegistry/interface/Service.h"
0030 
0031 #include "classlib/utils/StringList.h"
0032 #include "classlib/utils/StringOps.h"
0033 
0034 // helper files
0035 #include <iostream>
0036 #include <cstdlib>
0037 #include <string>
0038 #include <memory>
0039 #include <vector>
0040 #include <map>
0041 #include <cassert>
0042 #include <cstdint>
0043 
0044 #include "TString.h"
0045 #include "TH1F.h"
0046 #include "TH1S.h"
0047 #include "TH1D.h"
0048 #include "TH1I.h"
0049 #include "TH2F.h"
0050 #include "TH2S.h"
0051 #include "TH2D.h"
0052 #include "TH2I.h"
0053 #include "TH3F.h"
0054 #include "TProfile.h"
0055 #include "TProfile2D.h"
0056 #include "TObjString.h"
0057 
0058 namespace meedm {
0059   struct Void {};
0060 }  // namespace meedm
0061 
0062 //Using RunCache and LuminosityBlockCache tells the framework the module is able to
0063 // allow multiple concurrent Runs and LuminosityBlocks.
0064 
0065 class MEtoEDMConverter : public edm::one::EDProducer<edm::RunCache<meedm::Void>,
0066                                                      edm::LuminosityBlockCache<meedm::Void>,
0067                                                      edm::EndLuminosityBlockProducer,
0068                                                      edm::EndRunProducer,
0069                                                      edm::one::SharedResources> {
0070 public:
0071   typedef dqm::legacy::DQMStore DQMStore;
0072   typedef dqm::legacy::MonitorElement MonitorElement;
0073 
0074   explicit MEtoEDMConverter(const edm::ParameterSet&);
0075   ~MEtoEDMConverter() override;
0076   void beginJob() override;
0077   void produce(edm::Event&, const edm::EventSetup&) override;
0078   std::shared_ptr<meedm::Void> globalBeginRun(edm::Run const&, const edm::EventSetup&) const override;
0079   void globalEndRun(edm::Run const&, const edm::EventSetup&) override;
0080   void endRunProduce(edm::Run&, const edm::EventSetup&) override;
0081   void endLuminosityBlockProduce(edm::LuminosityBlock&, const edm::EventSetup&) override;
0082   void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override{};
0083   std::shared_ptr<meedm::Void> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0084                                                           edm::EventSetup const&) const override;
0085 
0086   template <class T>
0087   void putData(DQMStore::IGetter& g, T& iPutTo, bool iLumiOnly, uint32_t run, uint32_t lumi);
0088 
0089   using TagList = std::vector<uint32_t>;
0090 
0091 private:
0092   std::string fName;
0093   int verbosity;
0094   int frequency;
0095   std::string path;
0096 
0097   // private statistics information
0098   std::map<int, int> iCount;
0099   edm::GetterOfProducts<DQMToken> lumigetter_;
0100   edm::GetterOfProducts<DQMToken> rungetter_;
0101 
0102 };  // end class declaration
0103 
0104 using namespace lat;
0105 
0106 MEtoEDMConverter::MEtoEDMConverter(const edm::ParameterSet& iPSet) : fName(""), verbosity(0), frequency(0) {
0107   std::string MsgLoggerCat = "MEtoEDMConverter_MEtoEDMConverter";
0108 
0109   // get information from parameter set
0110   fName = iPSet.getUntrackedParameter<std::string>("Name", "MEtoEDMConverter");
0111   verbosity = iPSet.getUntrackedParameter<int>("Verbosity", 0);
0112   frequency = iPSet.getUntrackedParameter<int>("Frequency", 50);
0113   path = iPSet.getUntrackedParameter<std::string>("MEPathToSave");
0114   // use value of first digit to determine default output level (inclusive)
0115   // 0 is none, 1 is basic, 2 is fill output, 3 is gather output
0116   verbosity %= 10;
0117 
0118   // print out Parameter Set information being used
0119   if (verbosity >= 0) {
0120     edm::LogInfo(MsgLoggerCat) << "\n===============================\n"
0121                                << "Initialized as EDProducer with parameter values:\n"
0122                                << "    Name          = " << fName << "\n"
0123                                << "    Verbosity     = " << verbosity << "\n"
0124                                << "    Frequency     = " << frequency << "\n"
0125                                << "    Path          = " << path << "\n"
0126                                << "===============================\n";
0127   }
0128 
0129   std::string sName;
0130 
0131   // create persistent objects
0132 
0133   sName = fName + "Run";
0134   produces<MEtoEDM<TH1F>, edm::Transition::EndRun>(sName);
0135   produces<MEtoEDM<TH1S>, edm::Transition::EndRun>(sName);
0136   produces<MEtoEDM<TH1D>, edm::Transition::EndRun>(sName);
0137   produces<MEtoEDM<TH1I>, edm::Transition::EndRun>(sName);
0138   produces<MEtoEDM<TH2F>, edm::Transition::EndRun>(sName);
0139   produces<MEtoEDM<TH2S>, edm::Transition::EndRun>(sName);
0140   produces<MEtoEDM<TH2D>, edm::Transition::EndRun>(sName);
0141   produces<MEtoEDM<TH2I>, edm::Transition::EndRun>(sName);
0142   produces<MEtoEDM<TH3F>, edm::Transition::EndRun>(sName);
0143   produces<MEtoEDM<TProfile>, edm::Transition::EndRun>(sName);
0144   produces<MEtoEDM<TProfile2D>, edm::Transition::EndRun>(sName);
0145   produces<MEtoEDM<double>, edm::Transition::EndRun>(sName);
0146   produces<MEtoEDM<long long>, edm::Transition::EndRun>(sName);
0147   produces<MEtoEDM<TString>, edm::Transition::EndRun>(sName);
0148 
0149   sName = fName + "Lumi";
0150   produces<MEtoEDM<TH1F>, edm::Transition::EndLuminosityBlock>(sName);
0151   produces<MEtoEDM<TH1S>, edm::Transition::EndLuminosityBlock>(sName);
0152   produces<MEtoEDM<TH1D>, edm::Transition::EndLuminosityBlock>(sName);
0153   produces<MEtoEDM<TH1I>, edm::Transition::EndLuminosityBlock>(sName);
0154   produces<MEtoEDM<TH2F>, edm::Transition::EndLuminosityBlock>(sName);
0155   produces<MEtoEDM<TH2S>, edm::Transition::EndLuminosityBlock>(sName);
0156   produces<MEtoEDM<TH2D>, edm::Transition::EndLuminosityBlock>(sName);
0157   produces<MEtoEDM<TH2I>, edm::Transition::EndLuminosityBlock>(sName);
0158   produces<MEtoEDM<TH3F>, edm::Transition::EndLuminosityBlock>(sName);
0159   produces<MEtoEDM<TProfile>, edm::Transition::EndLuminosityBlock>(sName);
0160   produces<MEtoEDM<TProfile2D>, edm::Transition::EndLuminosityBlock>(sName);
0161   produces<MEtoEDM<double>, edm::Transition::EndLuminosityBlock>(sName);
0162   produces<MEtoEDM<long long>, edm::Transition::EndLuminosityBlock>(sName);
0163   produces<MEtoEDM<TString>, edm::Transition::EndLuminosityBlock>(sName);
0164 
0165   lumigetter_ = edm::GetterOfProducts<DQMToken>(edm::ProcessMatch("*"), this, edm::InLumi);
0166   rungetter_ = edm::GetterOfProducts<DQMToken>(edm::ProcessMatch("*"), this, edm::InRun);
0167   callWhenNewProductsRegistered([this](edm::BranchDescription const& bd) {
0168     this->lumigetter_(bd);
0169     this->rungetter_(bd);
0170   });
0171   usesResource("DQMStore");
0172 
0173   static_assert(sizeof(int64_t) == sizeof(long long), "type int64_t is not the same length as long long");
0174 }
0175 
0176 MEtoEDMConverter::~MEtoEDMConverter() = default;
0177 
0178 void MEtoEDMConverter::beginJob() {}
0179 
0180 std::shared_ptr<meedm::Void> MEtoEDMConverter::globalBeginRun(edm::Run const& iRun,
0181                                                               const edm::EventSetup& iSetup) const {
0182   return std::shared_ptr<meedm::Void>();
0183 }
0184 
0185 void MEtoEDMConverter::globalEndRun(edm::Run const& iRun, const edm::EventSetup& iSetup) {}
0186 
0187 void MEtoEDMConverter::endRunProduce(edm::Run& iRun, const edm::EventSetup& iSetup) {
0188   DQMStore* store = edm::Service<DQMStore>().operator->();
0189   store->meBookerGetter([&](DQMStore::IBooker& b, DQMStore::IGetter& g) { putData(g, iRun, false, iRun.run(), 0); });
0190 }
0191 
0192 std::shared_ptr<meedm::Void> MEtoEDMConverter::globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0193                                                                           edm::EventSetup const&) const {
0194   return std::shared_ptr<meedm::Void>();
0195 }
0196 
0197 void MEtoEDMConverter::endLuminosityBlockProduce(edm::LuminosityBlock& iLumi, const edm::EventSetup& iSetup) {
0198   DQMStore* store = edm::Service<DQMStore>().operator->();
0199   store->meBookerGetter([&](DQMStore::IBooker& b, DQMStore::IGetter& g) {
0200     putData(g, iLumi, true, iLumi.run(), iLumi.id().luminosityBlock());
0201   });
0202 }
0203 
0204 template <class T>
0205 void MEtoEDMConverter::putData(DQMStore::IGetter& iGetter, T& iPutTo, bool iLumiOnly, uint32_t run, uint32_t lumi) {
0206   std::string MsgLoggerCat = "MEtoEDMConverter_putData";
0207 
0208   if (verbosity > 0)
0209     edm::LogInfo(MsgLoggerCat) << "\nStoring MEtoEDM dataformat histograms.";
0210 
0211   // extract ME information into vectors
0212   std::vector<MonitorElement*>::iterator mmi, mme;
0213   std::vector<MonitorElement*> items(iGetter.getAllContents(path, run, lumi));
0214 
0215   unsigned int n1F = 0;
0216   unsigned int n1S = 0;
0217   unsigned int n1D = 0;
0218   unsigned int n1I = 0;
0219   unsigned int n2F = 0;
0220   unsigned int n2S = 0;
0221   unsigned int n2D = 0;
0222   unsigned int n2I = 0;
0223   unsigned int n3F = 0;
0224   unsigned int nProf = 0;
0225   unsigned int nProf2 = 0;
0226   unsigned int nDouble = 0;
0227   unsigned int nInt64 = 0;
0228   unsigned int nString = 0;
0229 
0230   for (mmi = items.begin(), mme = items.end(); mmi != mme; ++mmi) {
0231     MonitorElement* me = *mmi;
0232 
0233     // store only flagged ME at endLumi transition, and Run-based
0234     // histo at endRun transition
0235     if (iLumiOnly && !me->getLumiFlag())
0236       continue;
0237     if (!iLumiOnly && me->getLumiFlag())
0238       continue;
0239 
0240     switch (me->kind()) {
0241       case MonitorElement::Kind::INT:
0242         ++nInt64;
0243         break;
0244 
0245       case MonitorElement::Kind::REAL:
0246         ++nDouble;
0247         break;
0248 
0249       case MonitorElement::Kind::STRING:
0250         ++nString;
0251         break;
0252 
0253       case MonitorElement::Kind::TH1F:
0254         ++n1F;
0255         break;
0256 
0257       case MonitorElement::Kind::TH1S:
0258         ++n1S;
0259         break;
0260 
0261       case MonitorElement::Kind::TH1D:
0262         ++n1D;
0263         break;
0264 
0265       case MonitorElement::Kind::TH1I:
0266         ++n1I;
0267         break;
0268 
0269       case MonitorElement::Kind::TH2F:
0270         ++n2F;
0271         break;
0272 
0273       case MonitorElement::Kind::TH2S:
0274         ++n2S;
0275         break;
0276 
0277       case MonitorElement::Kind::TH2D:
0278         ++n2D;
0279         break;
0280 
0281       case MonitorElement::Kind::TH2I:
0282         ++n2I;
0283         break;
0284 
0285       case MonitorElement::Kind::TH3F:
0286         ++n3F;
0287         break;
0288 
0289       case MonitorElement::Kind::TPROFILE:
0290         ++nProf;
0291         break;
0292 
0293       case MonitorElement::Kind::TPROFILE2D:
0294         ++nProf2;
0295         break;
0296 
0297       default:
0298         edm::LogError(MsgLoggerCat) << "ERROR: The DQM object '" << me->getFullname()
0299                                     << "' is neither a ROOT object nor a recognised "
0300                                     << "simple object.\n";
0301         continue;
0302     }
0303   }
0304 
0305   std::unique_ptr<MEtoEDM<long long> > pOutInt(new MEtoEDM<long long>(nInt64));
0306   std::unique_ptr<MEtoEDM<double> > pOutDouble(new MEtoEDM<double>(nDouble));
0307   std::unique_ptr<MEtoEDM<TString> > pOutString(new MEtoEDM<TString>(nString));
0308   std::unique_ptr<MEtoEDM<TH1F> > pOut1(new MEtoEDM<TH1F>(n1F));
0309   std::unique_ptr<MEtoEDM<TH1S> > pOut1s(new MEtoEDM<TH1S>(n1S));
0310   std::unique_ptr<MEtoEDM<TH1D> > pOut1d(new MEtoEDM<TH1D>(n1D));
0311   std::unique_ptr<MEtoEDM<TH1I> > pOut1i(new MEtoEDM<TH1I>(n1I));
0312   std::unique_ptr<MEtoEDM<TH2F> > pOut2(new MEtoEDM<TH2F>(n2F));
0313   std::unique_ptr<MEtoEDM<TH2S> > pOut2s(new MEtoEDM<TH2S>(n2S));
0314   std::unique_ptr<MEtoEDM<TH2D> > pOut2d(new MEtoEDM<TH2D>(n2D));
0315   std::unique_ptr<MEtoEDM<TH2I> > pOut2i(new MEtoEDM<TH2I>(n2I));
0316   std::unique_ptr<MEtoEDM<TH3F> > pOut3(new MEtoEDM<TH3F>(n3F));
0317   std::unique_ptr<MEtoEDM<TProfile> > pOutProf(new MEtoEDM<TProfile>(nProf));
0318   std::unique_ptr<MEtoEDM<TProfile2D> > pOutProf2(new MEtoEDM<TProfile2D>(nProf2));
0319 
0320   for (mmi = items.begin(), mme = items.end(); mmi != mme; ++mmi) {
0321     MonitorElement* me = *mmi;
0322 
0323     // store only flagged ME at endLumi transition, and Run-based
0324     // histo at endRun transition
0325     // DQMStore should only hand out matching MEs
0326     assert(iLumiOnly == me->getLumiFlag());
0327 
0328     // get monitor elements
0329     switch (me->kind()) {
0330       case MonitorElement::Kind::INT:
0331         pOutInt->putMEtoEdmObject(me->getFullname(), me->getIntValue());
0332         break;
0333 
0334       case MonitorElement::Kind::REAL:
0335         pOutDouble->putMEtoEdmObject(me->getFullname(), me->getFloatValue());
0336         break;
0337 
0338       case MonitorElement::Kind::STRING:
0339         pOutString->putMEtoEdmObject(me->getFullname(), me->getStringValue());
0340         break;
0341 
0342       case MonitorElement::Kind::TH1F:
0343         pOut1->putMEtoEdmObject(me->getFullname(), *me->getTH1F());
0344         break;
0345 
0346       case MonitorElement::Kind::TH1S:
0347         pOut1s->putMEtoEdmObject(me->getFullname(), *me->getTH1S());
0348         break;
0349 
0350       case MonitorElement::Kind::TH1D:
0351         pOut1d->putMEtoEdmObject(me->getFullname(), *me->getTH1D());
0352         break;
0353 
0354       case MonitorElement::Kind::TH1I:
0355         pOut1i->putMEtoEdmObject(me->getFullname(), *me->getTH1I());
0356         break;
0357 
0358       case MonitorElement::Kind::TH2F:
0359         pOut2->putMEtoEdmObject(me->getFullname(), *me->getTH2F());
0360         break;
0361 
0362       case MonitorElement::Kind::TH2S:
0363         pOut2s->putMEtoEdmObject(me->getFullname(), *me->getTH2S());
0364         break;
0365 
0366       case MonitorElement::Kind::TH2D:
0367         pOut2d->putMEtoEdmObject(me->getFullname(), *me->getTH2D());
0368         break;
0369 
0370       case MonitorElement::Kind::TH2I:
0371         pOut2i->putMEtoEdmObject(me->getFullname(), *me->getTH2I());
0372         break;
0373 
0374       case MonitorElement::Kind::TH3F:
0375         pOut3->putMEtoEdmObject(me->getFullname(), *me->getTH3F());
0376         break;
0377 
0378       case MonitorElement::Kind::TPROFILE:
0379         pOutProf->putMEtoEdmObject(me->getFullname(), *me->getTProfile());
0380         break;
0381 
0382       case MonitorElement::Kind::TPROFILE2D:
0383         pOutProf2->putMEtoEdmObject(me->getFullname(), *me->getTProfile2D());
0384         break;
0385 
0386       default:
0387         edm::LogError(MsgLoggerCat) << "ERROR: The DQM object '" << me->getFullname()
0388                                     << "' is neither a ROOT object nor a recognised "
0389                                     << "simple object.\n";
0390         continue;
0391     }
0392 
0393   }  // end loop through monitor elements
0394 
0395   std::string sName;
0396 
0397   if (iLumiOnly) {
0398     sName = fName + "Lumi";
0399   } else {
0400     sName = fName + "Run";
0401   }
0402 
0403   // produce objects to put in events
0404   iPutTo.put(std::move(pOutInt), sName);
0405   iPutTo.put(std::move(pOutDouble), sName);
0406   iPutTo.put(std::move(pOutString), sName);
0407   iPutTo.put(std::move(pOut1), sName);
0408   iPutTo.put(std::move(pOut1s), sName);
0409   iPutTo.put(std::move(pOut1d), sName);
0410   iPutTo.put(std::move(pOut2), sName);
0411   iPutTo.put(std::move(pOut2s), sName);
0412   iPutTo.put(std::move(pOut2d), sName);
0413   iPutTo.put(std::move(pOut2i), sName);
0414   iPutTo.put(std::move(pOut3), sName);
0415   iPutTo.put(std::move(pOutProf), sName);
0416   iPutTo.put(std::move(pOutProf2), sName);
0417 }
0418 
0419 void MEtoEDMConverter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {}
0420 
0421 #include "FWCore/PluginManager/interface/ModuleDef.h"
0422 #include "FWCore/Framework/interface/MakerMacros.h"
0423 DEFINE_FWK_MODULE(MEtoEDMConverter);