Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef CondTools_L1Trigger_WriterProxy_h
0002 #define CondTools_L1Trigger_WriterProxy_h
0003 
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/Framework/interface/ConsumesCollector.h"
0007 
0008 #include "FWCore/PluginManager/interface/PluginFactory.h"
0009 
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0012 
0013 #include "CondTools/L1Trigger/interface/Exception.h"
0014 
0015 #include <string>
0016 
0017 namespace l1t {
0018 
0019   /* This is class that is used to save data to DB. Saving requires that we should know types at compile time.
0020  * This means that I cannot create simple class that saves all records. So, I create a base class, and template
0021  * version of it, that will procede with saving. This approach is the same as used in DataProxy.
0022  */
0023   class WriterProxy {
0024   public:
0025     virtual ~WriterProxy() {}
0026 
0027     /* Saves record and type from given event setup to pool DB. This method should not worry
0028          * about such things as IOV and so on. It should return new payload token and then
0029          * the framework would take care of it.
0030          *
0031          * This method should not care of pool transactions and connections management.
0032          * In case some need other methods, like delete and update, one should add more abstract
0033          * methods here.
0034          */
0035 
0036     virtual void setToken(edm::ConsumesCollector cc) = 0;
0037 
0038     virtual std::string save(const edm::EventSetup& setup) const = 0;
0039 
0040   protected:
0041   };
0042 
0043   /* Concrete implementation of WriteProxy. This will do all the saving, also user of new types that should be saved
0044  * should instaciate a new version of this class and register it in plugin system.
0045  */
0046   template <class Record, class Type>
0047   class WriterProxyT : public WriterProxy {
0048   private:
0049     edm::ESGetToken<Type, Record> rcdToken;
0050 
0051   public:
0052     ~WriterProxyT() override {}
0053 
0054     void setToken(edm::ConsumesCollector cc) override { rcdToken = cc.esConsumes(); }
0055 
0056     /* This method requires that Record and Type supports copy constructor */
0057     std::string save(const edm::EventSetup& setup) const override {
0058       // load record and type from EventSetup and save them in db
0059       edm::ESHandle<Type> handle;
0060 
0061       try {
0062         handle = setup.getHandle(rcdToken);
0063       } catch (l1t::DataAlreadyPresentException& ex) {
0064         return std::string();
0065       }
0066       // If handle is invalid, then data is already in DB
0067 
0068       edm::Service<cond::service::PoolDBOutputService> poolDb;
0069       if (!poolDb.isAvailable()) {
0070         throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0071       }
0072       poolDb->forceInit();
0073       cond::persistency::Session session = poolDb->session();
0074       if (not session.transaction().isActive())
0075         session.transaction().start(false);  // true: read only, false: read-write
0076 
0077       std::shared_ptr<Type> pointer = std::make_shared<Type>(*(handle.product()));
0078       std::string payloadToken = session.storePayload(*pointer);
0079 
0080       session.transaction().commit();
0081       return payloadToken;
0082     }
0083   };
0084 
0085   typedef edmplugin::PluginFactory<l1t::WriterProxy*()> WriterFactory;
0086 
0087 // Defines new type, creates static instance of this class and register it for plugin
0088 #define REGISTER_L1_WRITER(record, type)                            \
0089   template class l1t::WriterProxyT<record, type>;                   \
0090   typedef l1t::WriterProxyT<record, type> record##_##type##_Writer; \
0091   DEFINE_EDM_PLUGIN(l1t::WriterFactory, record##_##type##_Writer, #record "@" #type "@Writer")
0092 
0093 }  // namespace l1t
0094 
0095 #endif