CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
13 
14 #include <string>
15 
16 namespace l1t {
17 
18  /* This is class that is used to save data to DB. Saving requires that we should know types at compile time.
19  * This means that I cannot create simple class that saves all records. So, I create a base class, and template
20  * version of it, that will procede with saving. This approach is the same as used in DataProxy.
21  */
22  class WriterProxy {
23  public:
24  virtual ~WriterProxy() {}
25 
26  /* Saves record and type from given event setup to pool DB. This method should not worry
27  * about such things as IOV and so on. It should return new payload token and then
28  * the framework would take care of it.
29  *
30  * This method should not care of pool transactions and connections management.
31  * In case some need other methods, like delete and update, one should add more abstract
32  * methods here.
33  */
34 
35  virtual std::string save(const edm::EventSetup& setup) const = 0;
36 
37  protected:
38  };
39 
40  /* Concrete implementation of WriteProxy. This will do all the saving, also user of new types that should be saved
41  * should instaciate a new version of this class and register it in plugin system.
42  */
43  template <class Record, class Type>
44  class WriterProxyT : public WriterProxy {
45  public:
46  ~WriterProxyT() override {}
47 
48  /* This method requires that Record and Type supports copy constructor */
49  std::string save(const edm::EventSetup& setup) const override {
50  // load record and type from EventSetup and save them in db
52 
53  try {
54  setup.get<Record>().get(handle);
55  } catch (l1t::DataAlreadyPresentException& ex) {
56  return std::string();
57  }
58 
59  // If handle is invalid, then data is already in DB
60 
62  if (!poolDb.isAvailable()) {
63  throw cond::Exception("DataWriter: PoolDBOutputService not available.");
64  }
65  poolDb->forceInit();
68  // if throw transaction will unroll
70 
71  std::shared_ptr<Type> pointer = std::make_shared<Type>(*(handle.product()));
72  std::string payloadToken = session.storePayload(*pointer);
74  tr.close();
75  return payloadToken;
76  }
77  };
78 
80 
81 // Defines new type, creates static instance of this class and register it for plugin
82 #define REGISTER_L1_WRITER(record, type) \
83  template class l1t::WriterProxyT<record, type>; \
84  typedef l1t::WriterProxyT<record, type> record##_##type##_Writer; \
85  DEFINE_EDM_PLUGIN(l1t::WriterFactory, record##_##type##_Writer, #record "@" #type "@Writer")
86 
87 } // namespace l1t
88 
89 #endif
persistency::Exception Exception
Definition: Exception.h:25
cond::persistency::Session session() const
edmplugin::PluginFactory< l1t::WriterProxy *()> WriterFactory
Definition: WriterProxy.h:79
Transaction & transaction()
Definition: Session.cc:52
virtual ~WriterProxy()
Definition: WriterProxy.h:24
std::string save(const edm::EventSetup &setup) const override
Definition: WriterProxy.h:49
bool isAvailable() const
Definition: Service.h:40
tuple handle
Definition: patZpeak.py:23
virtual std::string save(const edm::EventSetup &setup) const =0
~WriterProxyT() override
Definition: WriterProxy.h:46
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:186
T get() const
Definition: EventSetup.h:88