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 
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  }
76  cond::DbSession session = poolDb->session();
77  cond::DbScopedTransaction tr(session);
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.storeObject( pointer.get(),
84  );
85  tr.commit();
86  return payloadToken ;
87  }
88 };
89 
91 
92 // Defines new type, creates static instance of this class and register it for plugin
93 #define REGISTER_L1_WRITER(record,type) \
94  template class l1t::WriterProxyT<record, type>; \
95  typedef l1t::WriterProxyT<record, type> record ## _ ## type ## _Writer; \
96  DEFINE_EDM_PLUGIN(l1t::WriterFactory, record ## _ ## type ## _Writer, #record "@" #type "@Writer")
97 
98 } // ns
99 
100 #endif
virtual std::string save(const edm::EventSetup &setup) const
Definition: WriterProxy.h:54
virtual ~WriterProxyT()
Definition: WriterProxy.h:51
std::string classNameForTypeId(const std::type_info &typeInfo)
virtual ~WriterProxy()
Definition: WriterProxy.h:28
bool isAvailable() const
Definition: Service.h:47
tuple handle
Definition: patZpeak.py:22
int start(bool readOnly=false)
start transaction
std::string storeObject(const T *object, const std::string &containerName)
Definition: DbSession.h:131
edmplugin::PluginFactory< l1t::WriterProxy *()> WriterFactory
Definition: WriterProxy.h:90
virtual std::string save(const edm::EventSetup &setup) const =0
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
int commit()
commit transaction. Will disconnect from database if connection timeout==0 or connectted time close t...
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")