CMS 3D CMS Logo

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 
13 
14 #include <string>
15 
16 namespace l1t
17 {
18 
19 /* This is class that is used to save data to DB. Saving requires that we should know types at compile time.
20  * This means that I cannot create simple class that saves all records. So, I create a base class, and template
21  * version of it, that will procede with saving. This approach is the same as used in DataProxy.
22  */
24 {
25  public:
26  virtual ~WriterProxy() {}
27 
28  /* Saves record and type from given event setup to pool DB. This method should not worry
29  * about such things as IOV and so on. It should return new payload token and then
30  * the framework would take care of it.
31  *
32  * This method should not care of pool transactions and connections management.
33  * In case some need other methods, like delete and update, one should add more abstract
34  * methods here.
35  */
36 
37  virtual std::string save (const edm::EventSetup & setup) const = 0;
38 
39  protected:
40 };
41 
42 /* Concrete implementation of WriteProxy. This will do all the saving, also user of new types that should be saved
43  * should instaciate a new version of this class and register it in plugin system.
44  */
45 template<class Record, class Type>
46 class WriterProxyT : public WriterProxy
47 {
48  public:
49  virtual ~WriterProxyT() {}
50 
51  /* This method requires that Record and Type supports copy constructor */
52  virtual std::string save (const edm::EventSetup & setup) const
53  {
54  // load record and type from EventSetup and save them in db
56 
57  try
58  {
59  setup.get<Record> ().get (handle);
60  }
62  {
63  return std::string() ;
64  }
65 
66  // If handle is invalid, then data is already in DB
67 
69  if (!poolDb.isAvailable())
70  {
71  throw cond::Exception( "DataWriter: PoolDBOutputService not available."
72  ) ;
73  }
74  poolDb->forceInit();
77  // if throw transaction will unroll
79 
80  std::shared_ptr<Type> pointer = std::make_shared<Type>(*(handle.product ()));
81  std::string payloadToken = session.storePayload( *pointer );
83  tr.close();
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
persistency::Exception Exception
Definition: Exception.h:25
virtual std::string save(const edm::EventSetup &setup) const
Definition: WriterProxy.h:52
cond::persistency::Session session() const
virtual ~WriterProxyT()
Definition: WriterProxy.h:49
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
delete x;
Definition: CaloConfig.h:22
Transaction & transaction()
Definition: Session.cc:66
virtual ~WriterProxy()
Definition: WriterProxy.h:26
bool isAvailable() const
Definition: Service.h:46
virtual std::string save(const edm::EventSetup &setup) const =0
edmplugin::PluginFactory< l1t::WriterProxy *()> WriterFactory
Definition: WriterProxy.h:88
const T & get() const
Definition: EventSetup.h:55
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:189
T const * product() const
Definition: ESHandle.h:86