Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:49

0001 // -*- C++ -*-
0002 //
0003 // Package:     Services
0004 // Class  :     PrintEventSetupDataRetrieval
0005 //
0006 // Implementation:
0007 //     A service which prints which data from the EventSetup have been retrieved since the last time it checked
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Thu Jul  9 14:30:13 CDT 2009
0011 //
0012 
0013 // system include files
0014 #include <iostream>
0015 #include <map>
0016 #include <memory>
0017 
0018 // user include files
0019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0020 #include "FWCore/Framework/interface/EventSetup.h"
0021 #include "FWCore/Framework/interface/EventSetupRecord.h"
0022 #include "FWCore/Framework/interface/ComponentDescription.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0026 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0027 #include "FWCore/Framework/interface/EventSetupRecordKey.h"
0028 #include "FWCore/Framework/interface/DataKey.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 
0031 namespace edm {
0032   class PrintEventSetupDataRetrieval
0033       : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
0034   public:
0035     PrintEventSetupDataRetrieval(edm::ParameterSet const&);
0036 
0037     void analyze(edm::Event const&, edm::EventSetup const&) override;
0038 
0039     void beginRun(edm::Run const&, edm::EventSetup const&) override;
0040     void endRun(edm::Run const&, edm::EventSetup const&) override;
0041 
0042     void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0043     void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0044 
0045     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0046 
0047   private:
0048     void check(EventSetup const&);
0049     // ---------- member data --------------------------------
0050     typedef std::map<eventsetup::EventSetupRecordKey,
0051                      std::pair<unsigned long long, std::map<eventsetup::DataKey, bool> > >
0052         RetrievedDataMap;
0053 
0054     RetrievedDataMap m_retrievedDataMap;
0055     std::vector<eventsetup::EventSetupRecordKey> m_recordKeys;
0056     const bool m_printProviders;
0057     const bool m_checkDuringBeginRun;
0058     const bool m_checkDuringBeginLumi;
0059     const bool m_checkDuringEvent;
0060   };
0061   //
0062   // constants, enums and typedefs
0063   //
0064 
0065   //
0066   // static data member definitions
0067   //
0068 
0069   //
0070   // constructors and destructor
0071   //
0072   PrintEventSetupDataRetrieval::PrintEventSetupDataRetrieval(const ParameterSet& iPS)
0073       : m_printProviders(iPS.getUntrackedParameter<bool>("printProviders")),
0074         m_checkDuringBeginRun(iPS.getUntrackedParameter<bool>("checkDuringBeginRun")),
0075         m_checkDuringBeginLumi(iPS.getUntrackedParameter<bool>("checkDuringBeginLumi")),
0076         m_checkDuringEvent(iPS.getUntrackedParameter<bool>("checkDuringEvent")) {}
0077 
0078   // PrintEventSetupDataRetrieval::PrintEventSetupDataRetrieval(const PrintEventSetupDataRetrieval& rhs)
0079   // {
0080   //    // do actual copying here;
0081   // }
0082 
0083   //PrintEventSetupDataRetrieval::~PrintEventSetupDataRetrieval()
0084   //{
0085   //}
0086 
0087   void PrintEventSetupDataRetrieval::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0088     edm::ParameterSetDescription desc;
0089     desc.addUntracked<bool>("printProviders", false)
0090         ->setComment("If 'true' also print which ES module provides the data");
0091     desc.addUntracked<bool>("checkDuringBeginRun", false)
0092         ->setComment("If 'true' check for retrieved data during each begin run is processed");
0093     desc.addUntracked<bool>("checkDuringBeginLumi", false)
0094         ->setComment("If 'true' check for retrieved data during each begin lumi is processed");
0095     desc.addUntracked<bool>("checkDuringEvent", true)
0096         ->setComment("If 'true' check for retrieved data during an event is processed");
0097     descriptions.add("PrintEventSetupDataRetrieval", desc);
0098     descriptions.setComment("This analyzer reports when EventSetup data is retrieved by a module in the job.");
0099   }
0100 
0101   //
0102   // assignment operators
0103   //
0104   // const PrintEventSetupDataRetrieval& PrintEventSetupDataRetrieval::operator=(const PrintEventSetupDataRetrieval& rhs)
0105   // {
0106   //   //An exception safe implementation is
0107   //   PrintEventSetupDataRetrieval temp(rhs);
0108   //   swap(rhs);
0109   //
0110   //   return *this;
0111   // }
0112 
0113   //
0114   // member functions
0115   //
0116   void PrintEventSetupDataRetrieval::analyze(Event const&, EventSetup const& iES) {
0117     if (m_checkDuringEvent) {
0118       check(iES);
0119     }
0120   }
0121 
0122   void PrintEventSetupDataRetrieval::beginRun(Run const&, EventSetup const& iES) {
0123     if (m_checkDuringBeginRun) {
0124       check(iES);
0125     }
0126   }
0127 
0128   void PrintEventSetupDataRetrieval::endRun(Run const&, EventSetup const&) {}
0129 
0130   void PrintEventSetupDataRetrieval::beginLuminosityBlock(LuminosityBlock const&, EventSetup const& iES) {
0131     if (m_checkDuringBeginLumi) {
0132       check(iES);
0133     }
0134   }
0135 
0136   void PrintEventSetupDataRetrieval::endLuminosityBlock(LuminosityBlock const&, EventSetup const&) {}
0137 
0138   void PrintEventSetupDataRetrieval::check(EventSetup const& iES) {
0139     //std::cout <<"postProcessEvent"<<std::endl;
0140     m_recordKeys.clear();
0141     iES.fillAvailableRecordKeys(m_recordKeys);
0142 
0143     std::unique_ptr<LogSystem> msg;
0144     for (std::vector<eventsetup::EventSetupRecordKey>::const_iterator it = m_recordKeys.begin(),
0145                                                                       itEnd = m_recordKeys.end();
0146          it != itEnd;
0147          ++it) {
0148       //std::cout <<"  "<<it->name()<<std::endl;
0149       auto r = iES.find(*it);
0150       assert(r);
0151 
0152       RetrievedDataMap::iterator itRetrievedData = m_retrievedDataMap.find(*it);
0153       if (itRetrievedData == m_retrievedDataMap.end()) {
0154         itRetrievedData =
0155             m_retrievedDataMap
0156                 .insert(std::make_pair(*it, std::pair<unsigned long long, std::map<eventsetup::DataKey, bool> >()))
0157                 .first;
0158         itRetrievedData->second.first = r->cacheIdentifier();
0159         std::vector<eventsetup::DataKey> keys;
0160         r->fillRegisteredDataKeys(keys);
0161         for (std::vector<eventsetup::DataKey>::const_iterator itData = keys.begin(), itDataEnd = keys.end();
0162              itData != itDataEnd;
0163              ++itData) {
0164           itRetrievedData->second.second.insert(std::make_pair(*itData, false));
0165         }
0166       }
0167       RetrievedDataMap::value_type& retrievedData = *itRetrievedData;
0168       if (itRetrievedData->second.first != r->cacheIdentifier()) {
0169         itRetrievedData->second.first = r->cacheIdentifier();
0170         for (std::map<eventsetup::DataKey, bool>::iterator itDatum = retrievedData.second.second.begin(),
0171                                                            itDatumEnd = retrievedData.second.second.end();
0172              itDatum != itDatumEnd;
0173              ++itDatum) {
0174           itDatum->second = false;
0175         }
0176       }
0177 
0178       for (std::map<eventsetup::DataKey, bool>::iterator itDatum = retrievedData.second.second.begin(),
0179                                                          itDatumEnd = retrievedData.second.second.end();
0180            itDatum != itDatumEnd;
0181            ++itDatum) {
0182         bool wasGotten = r->wasGotten(itDatum->first);
0183         if (wasGotten != itDatum->second) {
0184           if (not msg)
0185             msg = std::make_unique<LogSystem>("ESContent");
0186           else
0187             *msg << "\n";
0188           itDatum->second = wasGotten;
0189           *msg << "Retrieved> record:" << it->name() << " data:" << itDatum->first.type().name() << " '"
0190                << itDatum->first.name().value() << "'";
0191           if (m_printProviders) {
0192             const edm::eventsetup::ComponentDescription* d = r->providerDescription(itDatum->first);
0193             assert(nullptr != d);
0194             *msg << " provider:" << d->type_ << " '" << d->label_ << "'";
0195           }
0196         }
0197       }
0198     }
0199   }
0200 
0201   //
0202   // const member functions
0203   //
0204 
0205   //
0206   // static member functions
0207   //
0208 }  // namespace edm
0209 
0210 //define this as a plug-in
0211 using edm::PrintEventSetupDataRetrieval;
0212 DEFINE_FWK_MODULE(PrintEventSetupDataRetrieval);