CMS 3D CMS Logo

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/HCTypeTagTemplate.h"
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 
00008 #include "FWCore/PluginManager/interface/PluginFactory.h"
00009 
00010 /* #include "CondCore/DBCommon/interface/PoolStorageManager.h" */
00011 /* #include "CondCore/DBCommon/interface/Ref.h" */
00012 #include "CondCore/DBCommon/interface/PoolTransaction.h"
00013 #include "CondCore/DBCommon/interface/TypedRef.h"
00014 
00015 #include "CondTools/L1Trigger/interface/Exception.h"
00016 
00017 #include <string>
00018 
00019 namespace l1t
00020 {
00021 
00022 /* This is class that is used to save data to DB. Saving requires that we should know types at compile time.
00023  * This means that I cannot create simple class that saves all records. So, I create a base class, and template
00024  * version of it, that will procede with saving. This approach is the same as used in DataProxy.
00025  */
00026 class WriterProxy
00027 {
00028     public:
00029         virtual ~WriterProxy() {}
00030 
00031         /* Saves record and type from given event setup to pool DB. This method should not worry
00032          * about such things as IOV and so on. It should return new payload token and then
00033          * the framework would take care of it.
00034          *
00035          * This method should not care of pool transactions and connections management.
00036          * In case some need other methods, like delete and update, one should add more abstract
00037          * methods here.
00038          */
00039         virtual std::string save (const edm::EventSetup & setup, cond::PoolTransaction & pool) 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, cond::PoolTransaction & pool) const
00055         {
00056             // get className for the record first
00057             std::string recordName =
00058                 edm::eventsetup::heterocontainer::HCTypeTagTemplate<Record, edm::eventsetup::EventSetupRecordKey>::className ();
00059 
00060             // load record and type from EventSetup and save them in db
00061             edm::ESHandle<Type> handle;
00062 
00063             try
00064               {
00065                 setup.get<Record> ().get (handle);
00066               }
00067             catch( l1t::DataAlreadyPresentException& ex )
00068               {
00069                 return std::string() ;
00070               }
00071 
00072             // If handle is invalid, then data is already in DB
00073             cond::TypedRef<Type> ref (pool,
00074                                       new Type (*(handle.product ())));
00075             ref.markWrite (recordName);
00076 
00077             return ref.token ();
00078         }
00079 };
00080 
00081 typedef edmplugin::PluginFactory<l1t::WriterProxy * ()> WriterFactory;
00082 
00083 
00084 // Defines new type, creates static instance of this class and register it for plugin
00085 #define REGISTER_L1_WRITER(record,type) \
00086     template class l1t::WriterProxyT<record, type>; \
00087     static edm::eventsetup::heterocontainer::HCTypeTagTemplate<type, edm::eventsetup::DataKey> dummy ## _ ## record ## _ ## type ; \
00088     typedef l1t::WriterProxyT<record, type> record ## _ ## type ## _Writer; \
00089     DEFINE_EDM_PLUGIN(l1t::WriterFactory, record ## _ ## type ## _Writer, #record "@" #type "@Writer")
00090 
00091 } // ns
00092 
00093 #endif

Generated on Tue Jun 9 17:26:55 2009 for CMSSW by  doxygen 1.5.4