CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
WriterProxy.h
Go to the documentation of this file.
1 #ifndef CondTools_L1Trigger_WriterProxy_h
2 #define CondTools_L1Trigger_WriterProxy_h
3 
6 
8 
11 //#include "CondCore/DBCommon/interface/DbSession.h"
12 //#include "CondCore/DBCommon/interface/DbScopedTransaction.h"
13 
15 
16 #include <string>
17 
18 namespace l1t
19 {
20 
21 /* This is class that is used to save data to DB. Saving requires that we should know types at compile time.
22  * This means that I cannot create simple class that saves all records. So, I create a base class, and template
23  * version of it, that will procede with saving. This approach is the same as used in DataProxy.
24  */
26 {
27  public:
28  virtual ~WriterProxy() {}
29 
30  /* Saves record and type from given event setup to pool DB. This method should not worry
31  * about such things as IOV and so on. It should return new payload token and then
32  * the framework would take care of it.
33  *
34  * This method should not care of pool transactions and connections management.
35  * In case some need other methods, like delete and update, one should add more abstract
36  * methods here.
37  */
38 
39  virtual std::string save (const edm::EventSetup & setup) const = 0;
40 
41  protected:
42 };
43 
44 /* Concrete implementation of WriteProxy. This will do all the saving, also user of new types that should be saved
45  * should instaciate a new version of this class and register it in plugin system.
46  */
47 template<class Record, class Type>
48 class WriterProxyT : public WriterProxy
49 {
50  public:
51  virtual ~WriterProxyT() {}
52 
53  /* This method requires that Record and Type supports copy constructor */
54  virtual std::string save (const edm::EventSetup & setup) const
55  {
56  // load record and type from EventSetup and save them in db
58 
59  try
60  {
61  setup.get<Record> ().get (handle);
62  }
64  {
65  return std::string() ;
66  }
67 
68  // If handle is invalid, then data is already in DB
69 
71  if (!poolDb.isAvailable())
72  {
73  throw cond::Exception( "DataWriter: PoolDBOutputService not available."
74  ) ;
75  }
78  // if throw transaction will unroll
79  tr.start(false);
80 
81  boost::shared_ptr<Type> pointer(new Type (*(handle.product ())));
82  std::string payloadToken = session.storePayload( *pointer );
83  tr.commit();
84  return payloadToken ;
85  }
86 };
87 
89 
90 // Defines new type, creates static instance of this class and register it for plugin
91 #define REGISTER_L1_WRITER(record,type) \
92  template class l1t::WriterProxyT<record, type>; \
93  typedef l1t::WriterProxyT<record, type> record ## _ ## type ## _Writer; \
94  DEFINE_EDM_PLUGIN(l1t::WriterFactory, record ## _ ## type ## _Writer, #record "@" #type "@Writer")
95 
96 } // ns
97 
98 #endif
virtual std::string save(const edm::EventSetup &setup) const
Definition: WriterProxy.h:54
cond::persistency::Session session() const
virtual ~WriterProxyT()
Definition: WriterProxy.h:51
Transaction & transaction()
Definition: Session.cc:66
virtual ~WriterProxy()
Definition: WriterProxy.h:28
bool isAvailable() const
Definition: Service.h:46
tuple handle
Definition: patZpeak.py:22
void start(bool readOnly=true)
Definition: Session.cc:292
edmplugin::PluginFactory< l1t::WriterProxy *()> WriterFactory
Definition: WriterProxy.h:88
virtual std::string save(const edm::EventSetup &setup) const =0
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:202
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")