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 
7 
9 
12 
14 
15 #include <string>
16 
17 namespace l1t {
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  */
23  class WriterProxy {
24  public:
25  virtual ~WriterProxy() {}
26 
27  /* Saves record and type from given event setup to pool DB. This method should not worry
28  * about such things as IOV and so on. It should return new payload token and then
29  * the framework would take care of it.
30  *
31  * This method should not care of pool transactions and connections management.
32  * In case some need other methods, like delete and update, one should add more abstract
33  * methods here.
34  */
35 
36  virtual void setToken(edm::ConsumesCollector cc) = 0;
37 
38  virtual std::string save(const edm::EventSetup& setup) const = 0;
39 
40  protected:
41  };
42 
43  /* Concrete implementation of WriteProxy. This will do all the saving, also user of new types that should be saved
44  * should instaciate a new version of this class and register it in plugin system.
45  */
46  template <class Record, class Type>
47  class WriterProxyT : public WriterProxy {
48  private:
50 
51  public:
52  ~WriterProxyT() override {}
53 
54  void setToken(edm::ConsumesCollector cc) override { rcdToken = cc.esConsumes(); }
55 
56  /* This method requires that Record and Type supports copy constructor */
57  std::string save(const edm::EventSetup& setup) const override {
58  // load record and type from EventSetup and save them in db
60 
61  try {
62  handle = setup.getHandle(rcdToken);
63  } catch (l1t::DataAlreadyPresentException& ex) {
64  return std::string();
65  }
66  // If handle is invalid, then data is already in DB
67 
69  if (!poolDb.isAvailable()) {
70  throw cond::Exception("DataWriter: PoolDBOutputService not available.");
71  }
72  poolDb->forceInit();
73  cond::persistency::Session session = poolDb->session();
74  if (not session.transaction().isActive())
75  session.transaction().start(false); // true: read only, false: read-write
76 
77  std::shared_ptr<Type> pointer = std::make_shared<Type>(*(handle.product()));
78  std::string payloadToken = session.storePayload(*pointer);
79 
80  session.transaction().commit();
81  return payloadToken;
82  }
83  };
84 
86 
87 // Defines new type, creates static instance of this class and register it for plugin
88 #define REGISTER_L1_WRITER(record, type) \
89  template class l1t::WriterProxyT<record, type>; \
90  typedef l1t::WriterProxyT<record, type> record##_##type##_Writer; \
91  DEFINE_EDM_PLUGIN(l1t::WriterFactory, record##_##type##_Writer, #record "@" #type "@Writer")
92 
93 } // namespace l1t
94 
95 #endif
persistency::Exception Exception
Definition: Exception.h:25
void start(bool readOnly=true)
Definition: Session.cc:18
void setToken(edm::ConsumesCollector cc) override
Definition: WriterProxy.h:54
edmplugin::PluginFactory< l1t::WriterProxy *()> WriterFactory
Definition: WriterProxy.h:85
delete x;
Definition: CaloConfig.h:22
Transaction & transaction()
Definition: Session.cc:52
virtual void setToken(edm::ConsumesCollector cc)=0
virtual ~WriterProxy()
Definition: WriterProxy.h:25
std::string save(const edm::EventSetup &setup) const override
Definition: WriterProxy.h:57
virtual std::string save(const edm::EventSetup &setup) const =0
~WriterProxyT() override
Definition: WriterProxy.h:52
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:186
bool isAvailable() const
Definition: Service.h:40
edm::ESGetToken< Type, Record > rcdToken
Definition: WriterProxy.h:49
cond::persistency::Session session() const