CMS 3D CMS Logo

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 #include <optional>
50 // forward declarations
51 
52 template <class TRcd, class TData>
54 public:
56  ~L1ConfigOnlineProdBase() override;
57 
58  virtual std::unique_ptr<TData> produce(const TRcd& iRecord);
59 
60  virtual std::unique_ptr<TData> newObject(const std::string& objectKey) = 0;
61 
62 private:
63  // ----------member data ---------------------------
66 
67 protected:
69  std::optional<edm::ESConsumesCollectorT<TRcd>> m_consumesCollector;
71 
72  // Called from produce methods.
73  // bool is true if the object data should be made.
74  // If bool is false, produce method should throw
75  // DataAlreadyPresentException.
76  bool getObjectKey(const TRcd& record, std::string& objectKey);
77 
78  // For reading object directly from a CondDB w/o PoolDBOutputService
81 };
82 
83 template <class TRcd, class TData>
85  : m_omdsReader(),
86  m_forceGeneration(iConfig.getParameter<bool>("forceGeneration")),
87  m_dbSession(),
88  m_copyFromCondDB(false) {
89  //the following line is needed to tell the framework what
90  // data is being produced
91  auto cc = setWhatProduced(this);
92 
93  //now do what ever other initialization is needed
94  l1TriggerKeyListToken_ = cc.consumes();
95  l1TriggerKeyToken_ = cc.consumes();
96 
98 
99  if (iConfig.exists("copyFromCondDB")) {
100  m_copyFromCondDB = iConfig.getParameter<bool>("copyFromCondDB");
101 
102  if (m_copyFromCondDB) {
103  cond::persistency::ConnectionPool connectionPool;
104  // Connect DB Session
105  connectionPool.setAuthenticationPath(iConfig.getParameter<std::string>("onlineAuthentication"));
106  connectionPool.configure();
107  m_dbSession = connectionPool.createSession(iConfig.getParameter<std::string>("onlineDB"));
108  }
109  } else {
110  m_omdsReader.connect(iConfig.getParameter<std::string>("onlineDB"),
111  iConfig.getParameter<std::string>("onlineAuthentication"));
112  }
113 }
114 
115 template <class TRcd, class TData>
117  // do anything here that needs to be done at desctruction time
118  // (e.g. close files, deallocate resources etc.)
119 }
120 
121 template <class TRcd, class TData>
122 std::unique_ptr<TData> L1ConfigOnlineProdBase<TRcd, TData>::produce(const TRcd& iRecord) {
123  std::unique_ptr<TData> pData;
124 
125  // Get object key and check if already in ORCON
127  if (getObjectKey(iRecord, key) || m_forceGeneration) {
128  if (m_copyFromCondDB) {
129  auto keyList = iRecord.getHandle(l1TriggerKeyListToken_);
130 
131  // Find payload token
132  std::string recordName = edm::typelookup::className<TRcd>();
133  std::string dataType = edm::typelookup::className<TData>();
134  std::string payloadToken = keyList->token(recordName, dataType, key);
135 
136  edm::LogVerbatim("L1-O2O") << "Copying payload for " << recordName << "@" << dataType << " obj key " << key
137  << " from CondDB.";
138  edm::LogVerbatim("L1-O2O") << "TOKEN " << payloadToken;
139 
140  // Get object from POOL
141  // Copied from l1t::DataWriter::readObject()
142  if (!payloadToken.empty()) {
143  m_dbSession.transaction().start();
144  pData = m_dbSession.fetchPayload<TData>(payloadToken);
145  m_dbSession.transaction().commit();
146  }
147  } else {
148  pData = newObject(key);
149  }
150 
151  // if( pData.get() == 0 )
152  if (pData == std::unique_ptr<TData>()) {
153  std::string dataType = edm::typelookup::className<TData>();
154 
155  throw l1t::DataInvalidException("Unable to generate " + dataType + " for key " + key + ".");
156  }
157  } else {
158  std::string dataType = edm::typelookup::className<TData>();
159 
160  throw l1t::DataAlreadyPresentException(dataType + " for key " + key + " already in CondDB.");
161  }
162 
163  return pData;
164 }
165 
166 template <class TRcd, class TData>
167 bool L1ConfigOnlineProdBase<TRcd, TData>::getObjectKey(const TRcd& iRecord, std::string& objectKey) {
168  // Explanation of funny syntax: since record is dependent, we are not
169  // expecting getRecord to be a template so the compiler parses it
170  // as a non-template. http://gcc.gnu.org/ml/gcc-bugs/2005-11/msg03685.html
171 
172  // If L1TriggerKey is invalid, then all configuration objects are
173  // already in ORCON.
175  try {
176  key = iRecord.getHandle(l1TriggerKeyToken_);
177  } catch (l1t::DataAlreadyPresentException& ex) {
178  objectKey = std::string();
179  return false;
180  }
181 
182  // Get object key from L1TriggerKey
183  std::string recordName = edm::typelookup::className<TRcd>();
184  std::string dataType = edm::typelookup::className<TData>();
185 
186  objectKey = key->get(recordName, dataType);
187 
188  // Get L1TriggerKeyList
189  L1TriggerKeyList keyList;
190  l1t::DataWriter dataWriter;
191  if (!dataWriter.fillLastTriggerKeyList(keyList)) {
192  edm::LogError("L1-O2O") << "Problem getting last L1TriggerKeyList";
193  }
194 
195  // If L1TriggerKeyList does not contain object key, token is empty
196  return keyList.token(recordName, dataType, objectKey).empty();
197 }
198 
199 #endif
Log< level::Info, true > LogVerbatim
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:163
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
bool getObjectKey(const TRcd &record, std::string &objectKey)
cond::persistency::Session m_dbSession
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string token(const std::string &tscKey) const
std::optional< edm::ESConsumesCollectorT< TRcd > > m_consumesCollector
Log< level::Error, false > LogError
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 &)
edm::ESGetToken< L1TriggerKeyList, TRcd > l1TriggerKeyListToken_
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)
def move(src, dest)
Definition: eostools.py:511