CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/CondTools/L1Trigger/interface/WriterProxy.h

Go to the documentation of this file.
00001 #ifndef CondTools_L1Trigger_WriterProxy_h
00002 #define CondTools_L1Trigger_WriterProxy_h
00003 
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 
00007 #include "FWCore/PluginManager/interface/PluginFactory.h"
00008 
00009 #include "FWCore/ServiceRegistry/interface/Service.h"
00010 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00011 #include "CondCore/DBCommon/interface/DbSession.h"
00012 #include "CondCore/DBCommon/interface/DbScopedTransaction.h"
00013 
00014 #include "CondTools/L1Trigger/interface/Exception.h"
00015 
00016 #include <string>
00017 
00018 namespace l1t
00019 {
00020 
00021 /* This is class that is used to save data to DB. Saving requires that we should know types at compile time.
00022  * This means that I cannot create simple class that saves all records. So, I create a base class, and template
00023  * version of it, that will procede with saving. This approach is the same as used in DataProxy.
00024  */
00025 class WriterProxy
00026 {
00027     public:
00028         virtual ~WriterProxy() {}
00029 
00030         /* Saves record and type from given event setup to pool DB. This method should not worry
00031          * about such things as IOV and so on. It should return new payload token and then
00032          * the framework would take care of it.
00033          *
00034          * This method should not care of pool transactions and connections management.
00035          * In case some need other methods, like delete and update, one should add more abstract
00036          * methods here.
00037          */
00038 
00039         virtual std::string save (const edm::EventSetup & setup) const = 0;
00040 
00041     protected:
00042 };
00043 
00044 /* Concrete implementation of WriteProxy. This will do all the saving, also user of new types that should be saved
00045  * should instaciate a new version of this class and register it in plugin system.
00046  */
00047 template<class Record, class Type>
00048 class WriterProxyT : public WriterProxy
00049 {
00050     public:
00051         virtual ~WriterProxyT() {}
00052 
00053         /* This method requires that Record and Type supports copy constructor */
00054         virtual std::string save (const edm::EventSetup & setup) const
00055         {
00056             // load record and type from EventSetup and save them in db
00057             edm::ESHandle<Type> handle;
00058 
00059             try
00060               {
00061                 setup.get<Record> ().get (handle);
00062               }
00063             catch( l1t::DataAlreadyPresentException& ex )
00064               {
00065                 return std::string() ;
00066               }
00067 
00068             // If handle is invalid, then data is already in DB
00069        
00070             edm::Service<cond::service::PoolDBOutputService> poolDb;
00071             if (!poolDb.isAvailable())
00072               {
00073                 throw cond::Exception( "DataWriter: PoolDBOutputService not available."
00074                                        ) ;
00075               }
00076             cond::DbSession session = poolDb->session();
00077             cond::DbScopedTransaction tr(session);
00078             // if throw transaction will unroll
00079             tr.start(false);
00080 
00081             boost::shared_ptr<Type> pointer(new Type (*(handle.product ())));
00082             std::string payloadToken =  session.storeObject( pointer.get(),
00083                                                              cond::classNameForTypeId(typeid(Type))
00084                                                              );
00085             tr.commit();
00086             return payloadToken ;
00087         }
00088 };
00089 
00090 typedef edmplugin::PluginFactory<l1t::WriterProxy * ()> WriterFactory;
00091 
00092 // Defines new type, creates static instance of this class and register it for plugin
00093 #define REGISTER_L1_WRITER(record,type) \
00094     template class l1t::WriterProxyT<record, type>; \
00095     typedef l1t::WriterProxyT<record, type> record ## _ ## type ## _Writer; \
00096     DEFINE_EDM_PLUGIN(l1t::WriterFactory, record ## _ ## type ## _Writer, #record "@" #type "@Writer")
00097 
00098 } // ns
00099 
00100 #endif