Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-07 00:41:13

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