ATLAS Offline Software
CoolQuery.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <limits.h>
6 #include "LumiCalc/CoolQuery.h"
7 
8 //===========================================================================
9 CoolQuery::CoolQuery(const std::string& database, const std::string& triggerchain):
10  m_repsort(0),
11  m_database(database),
12  m_triggerchain(triggerchain),
13  m_VKstart(0),
14  m_VKstop(0),
15  m_logger( "CoolQuery" ),
16  m_valid(false)
17 {
18 
19 }
20 
21 //===========================================================================
23  if ( m_sourceDbPtr.use_count()>0 && m_sourceDbPtr->isOpen() ) {
24  m_logger << Root::kINFO << "Closing database '" << m_sourceDbPtr->databaseName() << Root::GEndl;
25  m_sourceDbPtr->closeDatabase();
26  }
27 }
28 
29 //===========================================================================
31 
32  m_logger << Root::kINFO << "Trying to connect to database " << m_database << "..." << Root::GEndl;
33 
34  // CoraCoolDatabaseSvc& corasvc = CoraCoolDatabaseSvcFactory::databaseService();
35 
36  cool::IDatabaseSvc& databasesvc = cool::DatabaseSvcFactory::databaseService();
37  //std::cout << "Done the CoraCool initialisation" << std::endl;
38  //std::cout << "Opening CORAL database" << std::endl;
39  try {
41  coral::IConnectionServiceConfiguration& csconfig=m_coralsvc.configuration();
42  csconfig.setReplicaSortingAlgorithm(*m_repsort);
43 
44  m_sourceDbPtr = databasesvc.openDatabase(m_database,true);// true --> readonly
45  // m_sourceCoraPtr=corasvc.openDatabase(m_database,true);// true --> readonly
46  //std::cout << "....database connections open OK" << std::endl;
47  return true;
48  }
49  catch (std::exception&e) {
50  m_logger << Root::kERROR << "Problem opening CORAL database: " << e.what() << Root::GEndl;
51  return false;
52  }
53  // std::cout << "Done the database opening" << std::endl;
54 
55  // list the COOL folders found in the database
56 
57 // const std::vector<std::string>& folders=m_sourceDbPtr->listAllNodes();
58 // std::cout << "COOL database has " << folders.size() << " folders defined"
59 // << std::endl;
60 // for (std::vector<std::string>::const_iterator itr=folders.begin();
61 // itr!=folders.end();++itr) std::cout << *itr << std::endl;
62 
63  return false;
64 
65 }
66 
67 //===========================================================================
68 std::string CoolQuery::transConn(const std::string& inconn) {
69  // translate simple connection string (no slash) to mycool.db with given
70  // instance name, all others are left alone
71  if (inconn.find('/')==std::string::npos) {
72  return "sqlite://X;schema=mycool.db;dbname="+inconn;
73  } else {
74  return inconn;
75  }
76 }
77 
78 //===========================================================================
79 unsigned int CoolQuery::getTriggerLevel(const std::string& triggername){
80 
81  size_t found = triggername.find_first_of('_');
82  if(found != std::string::npos){
83  std::string s_lvl = triggername.substr(0,found);
84  if(s_lvl == "EF") return 3;
85  if(s_lvl == "L2") return 2;
86  if(s_lvl == "L1") return 1;
87  if(s_lvl == "HLT") return 2;
88  }
89 
90  // Indicate no valid trigger name passed
91  return 0;
92 
93 }
94 
95 //===========================================================================
96 void CoolQuery::setIOV(const cool::ValidityKey start, const cool::ValidityKey stop){
97  m_VKstart = start;
98  m_VKstop = stop;
99 }
100 
101 void CoolQuery::setIOVForRun(unsigned int runnum) {
102  cool::ValidityKey run = runnum;
103  m_VKstart = (run << 32);
104  m_VKstop = ((run+1) << 32) - 1;
105 }
106 
107 cool::Int32 CoolQuery::getL1PrescaleFromChannelId(const std::string& folder_name, const cool::ChannelId& id ){
108 
109  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
110  cool::IObjectIteratorPtr itr = folder_ptr->browseObjects(m_VKstart, m_VKstop,id);
111 
112  // Need to iterate once to get to first valid record, do it this way to avoid Coverity warning
113  if (itr->goToNext()) {
114  const cool::IRecord& payload=itr->currentRef().payload();
115  return payload["Lvl1Prescale"].data<cool::Int32>();
116  }
117 
118  // Nonsense value
119  return UINT_MAX;
120 
121 }
122 
123 cool::Float CoolQuery::getHLTPrescaleFromChannelId(const std::string& folder_name, const cool::ChannelId& id ){
124 
125  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
126  cool::IObjectIteratorPtr itr = folder_ptr->browseObjects(m_VKstart, m_VKstop,id);
127 
128  // Need to iterate once to get to first valid record, do it this way to avoid Coverity warning
129  if (itr->goToNext()) {
130  const cool::IRecord& payload=itr->currentRef().payload();
131  return payload["Prescale"].data<cool::Float>();
132  }
133 
134  // Nonsense value
135  return -1.;
136 
137 }
138 
139 cool::ChannelId CoolQuery::getL1ChannelId(const std::string& trigger, const std::string& folder_name){
140 
141  // m_logger << Root::kINFO << "Getting channel id for L1 trigger [" << trigger << "] from folder [" << folder_name << "]" << Root::GEndl;
142 
143  m_valid = false;
144 
145  if (trigger == "") return UINT_MAX;
146 
147  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
148  cool::IObjectIteratorPtr obj_itr=folder_ptr->browseObjects(m_VKstart,m_VKstart, cool::ChannelSelection::all());
149  // loop through all triggers
150  while (obj_itr->goToNext()){
151  const cool::IRecord& payload=obj_itr->currentRef().payload();
152  // find the L1 trigger chain
153  if(payload["ItemName"].data<std::string>() == trigger){
154  m_valid = true;
155  // cout << "Channel id: " << obj_itr->currentRef().channelId() << endl;
156  return obj_itr->currentRef().channelId();
157  }
158  }
159 
160  if(!m_valid){
161  m_logger << Root::kERROR << "Couldn't find L1 trigger [" << trigger << "] in folder [" << folder_name << "]" << Root::GEndl;
162  }
163 
164 
165  // Nonsense value
166  return UINT_MAX;
167 }
168 
169 //===========================================================================
170 cool::ChannelId CoolQuery::getLumiChannelId(const std::string& lumimethod, const std::string& folder_name){
171 
172  m_valid = false;
173 
174  if (lumimethod == "") return UINT_MAX;
175 
176  // m_logger << Root::kINFO << "Getting channel id for Lumi method: " << lumimethod << " in folder " << folder_name << Root::GEndl;
177 
178  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
179  if(folder_ptr->existsChannel(lumimethod)){
180  m_valid = true;
181  return folder_ptr->channelId(lumimethod);
182  }else{
183  m_logger << Root::kWARNING << "Couldn't find lumimethod: " << lumimethod << " in COOL database!" << Root::GEndl;
184  }
185 
186  // Nonsense value
187  return UINT_MAX;
188 }
189 
190 //===========================================================================
191 cool::ChannelId CoolQuery::getHLTChannelId(const std::string& trigger, const std::string& folder_name){
192 
193  // m_logger << Root::kINFO << "Getting channel id for HLT trigger [" << trigger << "] from folder [" << folder_name << "]" << Root::GEndl;
194 
195  m_valid = false;
196 
197  if (trigger == "") return UINT_MAX;
198 
199  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
200  cool::IObjectIteratorPtr obj_itr=folder_ptr->browseObjects(m_VKstart,m_VKstart, cool::ChannelSelection::all());
201  // loop through all triggers
202  // loop through all triggers
203  while (obj_itr->goToNext()){
204  const cool::IRecord& payload=obj_itr->currentRef().payload();
205  if(payload["ChainName"].data<std::string>() == trigger){
206  m_valid = true;
207  // m_logger << Root::kINFO << "Found channel id: " << payload["ChainCounter"].data<cool::UInt32>() << Root::GEndl;
208  return payload["ChainCounter"].data<cool::UInt32>();
209  }
210  }
211 
212  if(!m_valid){
213  m_logger << Root::kERROR << "Couldn't find HLT trigger [" << trigger << "] in folder [" << folder_name << "]" << Root::GEndl;
214  }
215 
216  // Nonsense value
217  return UINT_MAX;
218 }
219 
220 void
221 CoolQuery::printL1Triggers(const std::string& folder_name) {
222 
223  m_logger << Root::kINFO << "Listing available triggers [triggername(prescale, chanid)]: " << Root::GEndl;
224 
225  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
226  cool::IObjectIteratorPtr obj_itr=folder_ptr->browseObjects(m_VKstart,m_VKstart, cool::ChannelSelection::all());
227  while (obj_itr->goToNext()){
228  const cool::IRecord& payload=obj_itr->currentRef().payload();
229  m_logger << Root::kINFO << payload["ItemName"].data<std::string>() << "(" << this->getL1PrescaleFromChannelId("/TRIGGER/LVL1/Prescales",this->getL1ChannelId(payload["ItemName"].data<std::string>(), folder_name)) << ", " << obj_itr->currentRef().channelId() << "), ";
230  }
232 }
233 
234 void
235 CoolQuery::printHLTTriggers(const std::string& folder_name) {
236 
237  m_logger << Root::kINFO << "Listing available triggers [triggername(prescale, chanid)]: " << Root::GEndl;
238 
239  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
240  cool::IObjectIteratorPtr obj_itr2=folder_ptr->browseObjects(m_VKstart,m_VKstart, cool::ChannelSelection::all());
241  while (obj_itr2->goToNext()){
242  const cool::IRecord& payload2=obj_itr2->currentRef().payload();
243  // m_logger << Root::kINFO << payload2["ChainName"].data<std::string>() << ", ";
244  m_logger << Root::kINFO << payload2["ChainName"].data<std::string>() << "(" << payload2["Prescale"].data<cool::Float>() << ", " << payload2["ChainCounter"].data<cool::UInt32>() << "), ";
245  }
247 }
248 
249 bool
251  // m_logger << Root::kINFO << "channelIdValid = " << m_valid << Root::GEndl;
252  return m_valid;
253 }
254 
255 //===========================================================================
256 std::string CoolQuery::getHLTLowerChainName(const std::string& trigger, const std::string& folder_name){
257 
258  // cout << "Getting lower chain name for trigger [" << trigger << "] from folder [" << folder_name << "] " << endl;
259  bool found = false;
260  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
261  cool::IObjectIteratorPtr obj_itr=folder_ptr->browseObjects(m_VKstart,m_VKstart, cool::ChannelSelection::all());
262  // loop through all triggers
263  while (obj_itr->goToNext()){
264  const cool::IRecord& payload=obj_itr->currentRef().payload();
265  if(payload["ChainName"].data<std::string>() == trigger){
266  found = true;
267  return payload["LowerChainName"].data<std::string>();
268 
269  }
270  }
271 
272  if (!found) {
273  m_logger << Root::kERROR << "Couldn't find HLT trigger [" << trigger << "] in folder [" << folder_name << "]" << Root::GEndl;
274 
275  // m_logger << Root::kINFO << "List of triggers, hth: " << Root::GEndl;
276  // cool::IObjectIteratorPtr obj_itr2=folder_ptr->browseObjects(m_VKstart,m_VKstart, cool::ChannelSelection::all());
277  // while (obj_itr2->goToNext()){
278  // const cool::IRecord& payload2=obj_itr2->currentRef().payload();
279  // m_logger << Root::kINFO << payload2["ChainName"].data<std::string>() << ", ";
280  // }
281  // m_logger << Root::kINFO << Root::GEndl;
282  }
283 
284  return "";
285 
286 }
287 
288 //===========================================================================
289 std::map<cool::ValidityKey, CoolQuery::LumiFolderData>
290 CoolQuery::getLumiFolderData(const std::string& folder_name, const std::string& tag, const cool::ChannelId& id ){
291 
292  std::map<cool::ValidityKey, LumiFolderData> mymap;
293  LumiFolderData folderData;
294 
295  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
296  // m_logger << Root::kWARNING << "Getting from database " << m_database << " tag " << tag << Root::GEndl;
297  if (!folder_ptr->existsChannel(id)) {
298  m_logger << Root::kWARNING << "Lumi channel id " << id << " does not exist in database " << folder_name << "!" << Root::GEndl;
299  return mymap;
300  }
301 
302  cool::IObjectIteratorPtr itr;
303  if(folder_ptr->existsUserTag(tag)) {
304  itr = folder_ptr->browseObjects(m_VKstart, m_VKstop, id, tag);
305  } else {
306  // Try without specifying tag
307  itr = folder_ptr->browseObjects(m_VKstart, m_VKstop, id);
308  }
309 
310  while (itr->goToNext()) {
311  const cool::IRecord& payload=itr->currentRef().payload();
312  folderData.LBAvInstLumi = payload["LBAvInstLumi"].data<float>();
313  folderData.LBAvEvtsPerBX = payload["LBAvEvtsPerBX"].data<float>();
314  folderData.Valid = payload["Valid"].data<cool::UInt32>();
315  mymap.insert( std::pair<cool::ValidityKey, LumiFolderData>(itr->currentRef().since(), folderData));
316  }
317 
318  return mymap;
319 
320 }
321 
322 //===========================================================================
323 std::map<cool::ValidityKey, CoolQuery::L1CountFolderData>
324 CoolQuery::getL1CountFolderData(const std::string& folder_name, const cool::ChannelId& id ){
325 
326  std::map<cool::ValidityKey, L1CountFolderData> mymap;
327  L1CountFolderData folderData;
328 
329  cool::IFolderPtr folder_ptr = m_sourceDbPtr->getFolder(folder_name);
330  // m_logger << Root::kWARNING << "Getting from database " << m_database << " tag " << tag << Root::GEndl;
331  if (!folder_ptr->existsChannel(id)) {
332  m_logger << Root::kWARNING << "Lumi channel id " << id << " does not exist in database " << folder_name << "!" << Root::GEndl;
333  return mymap;
334  }
335 
336  cool::IObjectIteratorPtr itr;
337 
338  itr = folder_ptr->browseObjects(m_VKstart, m_VKstop, id);
339 
340  while (itr->goToNext()) {
341  const cool::IRecord& payload=itr->currentRef().payload();
342  folderData.BeforePrescale = payload["BeforePrescale"].data<cool::UInt63>();
343  folderData.AfterPrescale = payload["AfterPrescale"].data<cool::UInt63>();
344  folderData.L1Accept = payload["L1Accept"].data<cool::UInt63>();
345  mymap.insert( std::pair<cool::ValidityKey, L1CountFolderData>(itr->currentRef().since(), folderData));
346  }
347 
348  return mymap;
349 
350 }
351 
352 
353 
CoolQuery::m_logger
Root::TMsgLogger m_logger
Definition: CoolQuery.h:334
python.utils.AtlRunQueryDQUtils.runnum
runnum
Definition: AtlRunQueryDQUtils.py:214
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
CoolQuery::LumiFolderData
Definition: CoolQuery.h:299
CoolQuery::m_database
std::string m_database
Definition: CoolQuery.h:329
Root::kWARNING
@ kWARNING
Definition: TMsgLogger.h:51
CoolQuery::LumiFolderData::LBAvInstLumi
float LBAvInstLumi
Definition: CoolQuery.h:300
CoolQuery::m_VKstop
cool::ValidityKey m_VKstop
Definition: CoolQuery.h:332
CoolQuery::CoolQuery
CoolQuery(const std::string &database, const std::string &triggerchain)
Definition: CoolQuery.cxx:9
CoolQuery::getLumiFolderData
std::map< cool::ValidityKey, LumiFolderData > getLumiFolderData(const std::string &folder_name, const std::string &tag, const cool::ChannelId &id)
Definition: CoolQuery.cxx:290
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
CoolQuery::getHLTLowerChainName
std::string getHLTLowerChainName(const std::string &trigger, const std::string &folder_name)
Definition: CoolQuery.cxx:256
CoolQuery::setIOVForRun
void setIOVForRun(unsigned int runnum)
Definition: CoolQuery.cxx:101
CoolQuery::L1CountFolderData::AfterPrescale
cool::UInt63 AfterPrescale
Definition: CoolQuery.h:312
Cut::all
@ all
Definition: SUSYToolsAlg.cxx:67
CoolQuery::getL1PrescaleFromChannelId
cool::Int32 getL1PrescaleFromChannelId(const std::string &folder_name, const cool::ChannelId &id)
Definition: CoolQuery.cxx:107
CoolQuery::L1CountFolderData::BeforePrescale
cool::UInt63 BeforePrescale
Definition: CoolQuery.h:311
Root::kERROR
@ kERROR
Definition: TMsgLogger.h:52
PixelModuleFeMask_create_db.stop
int stop
Definition: PixelModuleFeMask_create_db.py:76
GEndl
#define GEndl
Definition: TMsgLogger.h:151
CoolQuery::getL1CountFolderData
std::map< cool::ValidityKey, L1CountFolderData > getL1CountFolderData(const std::string &folder_name, const cool::ChannelId &id)
Definition: CoolQuery.cxx:324
CoolQuery::transConn
std::string transConn(const std::string &inconn)
Definition: CoolQuery.cxx:68
CoolQuery::m_coralsvc
coral::ConnectionService m_coralsvc
Definition: CoolQuery.h:326
CoolQuery::getLumiChannelId
cool::ChannelId getLumiChannelId(const std::string &lumimethod, const std::string &folder_name)
Definition: CoolQuery.cxx:170
CoolQuery::L1CountFolderData::L1Accept
cool::UInt63 L1Accept
Definition: CoolQuery.h:313
python.subdetectors.mmg.database
database
Definition: mmg.py:6
CoolQuery::~CoolQuery
~CoolQuery()
Definition: CoolQuery.cxx:22
Root::kINFO
@ kINFO
Definition: TMsgLogger.h:50
calibdata.exception
exception
Definition: calibdata.py:496
CoolQuery::m_valid
bool m_valid
Definition: CoolQuery.h:336
run
Definition: run.py:1
CoolQuery::LumiFolderData::Valid
cool::UInt32 Valid
Definition: CoolQuery.h:302
CoolQuery::printHLTTriggers
void printHLTTriggers(const std::string &folder_name)
Definition: CoolQuery.cxx:235
CoolQuery::getHLTChannelId
cool::ChannelId getHLTChannelId(const std::string &trigger, const std::string &folder_name)
Definition: CoolQuery.cxx:191
CoolQuery::LumiFolderData::LBAvEvtsPerBX
float LBAvEvtsPerBX
Definition: CoolQuery.h:301
CoolQuery::channelIdValid
bool channelIdValid()
Definition: CoolQuery.cxx:250
CoolQuery::setIOV
void setIOV(const cool::ValidityKey start, const cool::ValidityKey stop)
Definition: CoolQuery.cxx:96
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
CoolQuery::printL1Triggers
void printL1Triggers(const std::string &folder_name)
Definition: CoolQuery.cxx:221
CoolQuery::m_sourceDbPtr
cool::IDatabasePtr m_sourceDbPtr
Definition: CoolQuery.h:328
CoolQuery::openDbConn
bool openDbConn()
Definition: CoolQuery.cxx:30
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
CoolQuery::m_repsort
ReplicaSorter * m_repsort
Definition: CoolQuery.h:327
CoolQuery::getHLTPrescaleFromChannelId
cool::Float getHLTPrescaleFromChannelId(const std::string &folder_name, const cool::ChannelId &id)
Definition: CoolQuery.cxx:123
CoolQuery::L1CountFolderData
Definition: CoolQuery.h:310
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
CoolQuery.h
CoolQuery::m_VKstart
cool::ValidityKey m_VKstart
Definition: CoolQuery.h:331
ReplicaSorter
Definition: Database/CoolConvUtilities/src/ReplicaSorter.h:12
CoolQuery::getTriggerLevel
unsigned int getTriggerLevel(const std::string &triggername)
Definition: CoolQuery.cxx:79
CoolQuery::getL1ChannelId
cool::ChannelId getL1ChannelId(const std::string &trigger, const std::string &folder_name)
Definition: CoolQuery.cxx:139