CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
L1ConfigOnlineProdBase.h
Go to the documentation of this file.
1 #ifndef CondTools_L1Trigger_L1ConfigOnlineProdBase_h
2 #define CondTools_L1Trigger_L1ConfigOnlineProdBase_h
3 // -*- C++ -*-
4 //
5 // Package: L1Trigger
6 // Class : L1ConfigOnlineProdBase
7 //
18 //
19 // Original Author: Werner Sun
20 // Created: Tue Sep 2 22:48:15 CEST 2008
21 // $Id: L1ConfigOnlineProdBase.h,v 1.9 2010/02/16 21:55:43 wsun Exp $
22 //
23 
24 // system include files
25 #include <memory>
26 
27 // user include files
29 
33 
38 
42 
45 
48 
49 // forward declarations
50 
51 template <class TRcd, class TData>
53 public:
55  ~L1ConfigOnlineProdBase() override;
56 
57  virtual std::unique_ptr<TData> produce(const TRcd& iRecord);
58 
59  virtual std::unique_ptr<TData> newObject(const std::string& objectKey) = 0;
60 
61 private:
62  // ----------member data ---------------------------
65 
66 protected:
69 
70  // Called from produce methods.
71  // bool is true if the object data should be made.
72  // If bool is false, produce method should throw
73  // DataAlreadyPresentException.
74  bool getObjectKey(const TRcd& record, std::string& objectKey);
75 
76  // For reading object directly from a CondDB w/o PoolDBOutputService
79 };
80 
81 template <class TRcd, class TData>
83  : m_omdsReader(),
84  m_forceGeneration(iConfig.getParameter<bool>("forceGeneration")),
85  m_dbSession(),
86  m_copyFromCondDB(false) {
87  //the following line is needed to tell the framework what
88  // data is being produced
89  auto cc = setWhatProduced(this);
90 
91  //now do what ever other initialization is needed
92  l1TriggerKeyListToken_ = cc.consumes();
93  l1TriggerKeyToken_ = cc.consumes();
94 
95  if (iConfig.exists("copyFromCondDB")) {
96  m_copyFromCondDB = iConfig.getParameter<bool>("copyFromCondDB");
97 
98  if (m_copyFromCondDB) {
99  cond::persistency::ConnectionPool connectionPool;
100  // Connect DB Session
101  connectionPool.setAuthenticationPath(iConfig.getParameter<std::string>("onlineAuthentication"));
102  connectionPool.configure();
103  m_dbSession = connectionPool.createSession(iConfig.getParameter<std::string>("onlineDB"));
104  }
105  } else {
106  m_omdsReader.connect(iConfig.getParameter<std::string>("onlineDB"),
107  iConfig.getParameter<std::string>("onlineAuthentication"));
108  }
109 }
110 
111 template <class TRcd, class TData>
113  // do anything here that needs to be done at desctruction time
114  // (e.g. close files, deallocate resources etc.)
115 }
116 
117 template <class TRcd, class TData>
118 std::unique_ptr<TData> L1ConfigOnlineProdBase<TRcd, TData>::produce(const TRcd& iRecord) {
119  std::unique_ptr<TData> pData;
120 
121  // Get object key and check if already in ORCON
123  if (getObjectKey(iRecord, key) || m_forceGeneration) {
124  if (m_copyFromCondDB) {
125  auto keyList = iRecord.getHandle(l1TriggerKeyListToken_);
126 
127  // Find payload token
128  std::string recordName = edm::typelookup::className<TRcd>();
129  std::string dataType = edm::typelookup::className<TData>();
130  std::string payloadToken = keyList->token(recordName, dataType, key);
131 
132  edm::LogVerbatim("L1-O2O") << "Copying payload for " << recordName << "@" << dataType << " obj key " << key
133  << " from CondDB.";
134  edm::LogVerbatim("L1-O2O") << "TOKEN " << payloadToken;
135 
136  // Get object from POOL
137  // Copied from l1t::DataWriter::readObject()
138  if (!payloadToken.empty()) {
139  m_dbSession.transaction().start();
140  pData = m_dbSession.fetchPayload<TData>(payloadToken);
141  m_dbSession.transaction().commit();
142  }
143  } else {
144  pData = newObject(key);
145  }
146 
147  // if( pData.get() == 0 )
148  if (pData == std::unique_ptr<TData>()) {
149  std::string dataType = edm::typelookup::className<TData>();
150 
151  throw l1t::DataInvalidException("Unable to generate " + dataType + " for key " + key + ".");
152  }
153  } else {
154  std::string dataType = edm::typelookup::className<TData>();
155 
156  throw l1t::DataAlreadyPresentException(dataType + " for key " + key + " already in CondDB.");
157  }
158 
159  return pData;
160 }
161 
162 template <class TRcd, class TData>
163 bool L1ConfigOnlineProdBase<TRcd, TData>::getObjectKey(const TRcd& iRecord, std::string& objectKey) {
164  // Explanation of funny syntax: since record is dependent, we are not
165  // expecting getRecord to be a template so the compiler parses it
166  // as a non-template. http://gcc.gnu.org/ml/gcc-bugs/2005-11/msg03685.html
167 
168  // If L1TriggerKey is invalid, then all configuration objects are
169  // already in ORCON.
171  try {
172  key = iRecord.getHandle(l1TriggerKeyToken_);
173  } catch (l1t::DataAlreadyPresentException& ex) {
174  objectKey = std::string();
175  return false;
176  }
177 
178  // Get object key from L1TriggerKey
179  std::string recordName = edm::typelookup::className<TRcd>();
180  std::string dataType = edm::typelookup::className<TData>();
181 
182  objectKey = key->get(recordName, dataType);
183 
184  // Get L1TriggerKeyList
185  L1TriggerKeyList keyList;
186  l1t::DataWriter dataWriter;
187  if (!dataWriter.fillLastTriggerKeyList(keyList)) {
188  edm::LogError("L1-O2O") << "Problem getting last L1TriggerKeyList";
189  }
190 
191  // If L1TriggerKeyList does not contain object key, token is empty
192  return keyList.token(recordName, dataType, objectKey).empty();
193 }
194 
195 #endif
Log< level::Info, true > LogVerbatim
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:163
bool getObjectKey(const TRcd &record, std::string &objectKey)
cond::persistency::Session m_dbSession
bool exists(std::string const &parameterName) const
checks if a parameter exists
tuple recordName
Definition: align_cfg.py:66
Log< level::Error, false > LogError
tuple key
prepare the HTCondor submission files and eventually submit them
Session createSession(const std::string &connectionString, bool writeCapable=false)
bool fillLastTriggerKeyList(L1TriggerKeyList &output)
Definition: DataWriter.cc:147
void connect(const std::string &connectString, const std::string &authenticationPath)
Definition: OMDSReader.cc:43
edm::ESGetToken< L1TriggerKey, TRcd > l1TriggerKeyToken_
L1ConfigOnlineProdBase(const edm::ParameterSet &)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::ESGetToken< L1TriggerKeyList, TRcd > l1TriggerKeyListToken_
std::string token(const std::string &tscKey) const
virtual std::unique_ptr< TData > produce(const TRcd &iRecord)
virtual std::unique_ptr< TData > newObject(const std::string &objectKey)=0
void setAuthenticationPath(const std::string &p)