CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HLTConf2DB.cc
Go to the documentation of this file.
1 #ifndef RecoLuminosity_LumiProducer_HLTConf2DB_H
2 #define RecoLuminosity_LumiProducer_HLTConf2DB_H
3 
4 #include "CoralBase/AttributeList.h"
5 #include "CoralBase/Attribute.h"
6 #include "CoralBase/AttributeSpecification.h"
7 #include "CoralBase/Exception.h"
8 #include "RelationalAccess/ConnectionService.h"
9 #include "RelationalAccess/ISessionProxy.h"
10 #include "RelationalAccess/ITransaction.h"
11 #include "RelationalAccess/ITypeConverter.h"
12 #include "RelationalAccess/IQuery.h"
13 #include "RelationalAccess/ICursor.h"
14 #include "RelationalAccess/ISchema.h"
15 #include "RelationalAccess/IView.h"
16 #include "RelationalAccess/ITable.h"
17 #include "RelationalAccess/ITableDataEditor.h"
18 #include "RelationalAccess/IBulkOperation.h"
19 
26 #include <iostream>
27 #include <map>
28 #include <vector>
29 #include <string>
30 namespace lumi{
31  class HLTConf2DB : public DataPipe{
32  public:
33  explicit HLTConf2DB( const std::string& dest);
34  virtual void retrieveData( unsigned int );
35  virtual const std::string dataType() const;
36  virtual const std::string sourceType() const;
37  virtual ~HLTConf2DB();
38  };//cl HLTConf2DB
39  //
40  //implementation
41  //
42  HLTConf2DB::HLTConf2DB( const std::string& dest):DataPipe(dest){}
43  void HLTConf2DB::retrieveData( unsigned int runnumber ){
44  std::string runinfoschema("CMS_RUNINFO");
45  //std::string hltschema("CMS_HLT_V0");
46  std::string hltschema("CMS_HLT");
47  std::string runsessiontable("RUNSESSION_PARAMETER");
48  //
49  //must login as cms_hlt_r
50  coral::ConnectionService* svc=new coral::ConnectionService;
51  lumi::DBConfig dbconf(*svc);
52  if(!m_authpath.empty()){
54  }
55  //std::cout<<"m_source "<<m_source<<std::endl;
56  coral::ISessionProxy* srcsession=svc->connect(m_source, coral::ReadOnly);
57  coral::ITypeConverter& tpc=srcsession->typeConverter();
58  tpc.setCppTypeForSqlType("unsigned int","NUMBER(38)");
59  //
60  //select string_value from CMS_RUNINFO.runsession_parameter where name='CMS.LVL0:HLT_KEY_DESCRIPTION' and runnumber=xx;
61  //
62  std::string hltkey("");
63  std::vector< std::pair<std::string,std::string> > hlt2l1map;
64  hlt2l1map.reserve(200);
65  try{
66  srcsession->transaction().start(true);
67  coral::ISchema& runinfoSchemaHandle=srcsession->schema(runinfoschema);
68  if( !runinfoSchemaHandle.existsTable(runsessiontable) ){
69  throw lumi::Exception("missing runsession table","retrieveData","HLTConf2DB");
70  }
71  coral::AttributeList rfBindVariableList;
72  rfBindVariableList.extend("name",typeid(std::string));
73  rfBindVariableList.extend("runnumber",typeid(unsigned int));
74  rfBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
75  rfBindVariableList["runnumber"].data<unsigned int>()=runnumber;
76  coral::IQuery* kq=runinfoSchemaHandle.newQuery();
77  coral::AttributeList runinfoOut;
78  runinfoOut.extend("STRING_VALUE",typeid(std::string));
79  kq->addToTableList(runsessiontable);
80  kq->setCondition("NAME = :name AND RUNNUMBER = :runnumber",rfBindVariableList);
81  kq->addToOutputList("STRING_VALUE");
82  kq->defineOutput(runinfoOut);
83  coral::ICursor& kcursor=kq->execute();
84  unsigned int s=0;
85  while( kcursor.next() ){
86  const coral::AttributeList& row=kcursor.currentRow();
87  hltkey=row["STRING_VALUE"].data<std::string>();
88  ++s;
89  }
90  if( s==0||hltkey.empty() ){
91  kcursor.close();
92  delete kq;
93  srcsession->transaction().rollback();
94  throw lumi::Exception(std::string("requested run has no or invalid hltkey"),"retrieveData","TRG2DB");
95  }
96  if(s>1){
97  kcursor.close();
98  delete kq;
99  srcsession->transaction().rollback();
100  throw lumi::Exception(std::string("requested run has multile hltkey"),"retrieveData","TRG2DB");
101  }
102  }catch( const coral::Exception& er ){
103  std::cout<<"source runinfo database problem "<<er.what()<<std::endl;
104  srcsession->transaction().rollback();
105  delete srcsession;
106  delete svc;
107  throw er;
108  }
109  coral::ISchema& confSchemaHandle=srcsession->nominalSchema();
110  try{
111  //srcsession->transaction().start(true);
112  if( !confSchemaHandle.existsTable("PATHS") || !confSchemaHandle.existsTable("STRINGPARAMVALUES") || !confSchemaHandle.existsTable("PARAMETERS") || !confSchemaHandle.existsTable("SUPERIDPARAMETERASSOC") || !confSchemaHandle.existsTable("MODULES") || !confSchemaHandle.existsTable("MODULETEMPLATES") || !confSchemaHandle.existsTable("PATHMODULEASSOC") || !confSchemaHandle.existsTable("CONFIGURATIONPATHASSOC") || !confSchemaHandle.existsTable("CONFIGURATIONS") ){
113  throw lumi::Exception("missing hlt conf tables" ,"retrieveData","HLTConf2DB");
114  }
115  coral::AttributeList q1BindVariableList;
116  q1BindVariableList.extend("hltseed",typeid(std::string));
117  q1BindVariableList.extend("l1seedexpr",typeid(std::string));
118  q1BindVariableList.extend("hltkey",typeid(std::string));
119  q1BindVariableList["hltseed"].data<std::string>()=std::string("HLTLevel1GTSeed");
120  q1BindVariableList["l1seedexpr"].data<std::string>()=std::string("L1SeedsLogicalExpression");
121  q1BindVariableList["hltkey"].data<std::string>()=hltkey;
122  coral::IQuery* q1=confSchemaHandle.newQuery();
123  q1->addToOutputList("PATHS.NAME","hltpath");
124  q1->addToOutputList("STRINGPARAMVALUES.VALUE","l1expression");
125 
126  q1->addToTableList("PATHS");
127  q1->addToTableList("STRINGPARAMVALUES");
128  q1->addToTableList("PARAMETERS");
129  q1->addToTableList("SUPERIDPARAMETERASSOC");
130  q1->addToTableList("MODULES");
131  q1->addToTableList("MODULETEMPLATES");
132  q1->addToTableList("PATHMODULEASSOC");
133  q1->addToTableList("CONFIGURATIONPATHASSOC");
134  q1->addToTableList("CONFIGURATIONS");
135 
136  q1->setCondition("PARAMETERS.PARAMID=STRINGPARAMVALUES.PARAMID and SUPERIDPARAMETERASSOC.PARAMID=PARAMETERS.PARAMID and MODULES.SUPERID=SUPERIDPARAMETERASSOC.SUPERID and MODULETEMPLATES.SUPERID=MODULES.TEMPLATEID and PATHMODULEASSOC.MODULEID=MODULES.SUPERID and PATHS.PATHID=PATHMODULEASSOC.PATHID and CONFIGURATIONPATHASSOC.PATHID=PATHS.PATHID and CONFIGURATIONS.CONFIGID=CONFIGURATIONPATHASSOC.CONFIGID and MODULETEMPLATES.NAME = :hltseed and PARAMETERS.NAME = :l1seedexpr and CONFIGURATIONS.CONFIGDESCRIPTOR = :hltkey",q1BindVariableList);
137 
141  coral::ICursor& cursor1=q1->execute();
142  while( cursor1.next() ){
143  const coral::AttributeList& row=cursor1.currentRow();
144  std::string hltpath=row["hltpath"].data<std::string>();
145  std::string l1expression=row["l1expression"].data<std::string>();
146  hlt2l1map.push_back(std::make_pair(hltpath,l1expression));
147  }
148  cursor1.close();
149  delete q1;
150  }catch( const coral::Exception& er ){
151  std::cout<<"database problem with source hlt confdb"<<er.what()<<std::endl;
152  srcsession->transaction().rollback();
153  delete srcsession;
154  throw er;
155  }
156  srcsession->transaction().commit();
157  delete srcsession;
158  std::vector< std::pair<std::string,std::string> >::const_iterator mIt;
159  std::vector< std::pair<std::string,std::string> >::const_iterator mBeg=hlt2l1map.begin();
160  std::vector< std::pair<std::string,std::string> >::const_iterator mEnd=hlt2l1map.end();
161 
162  coral::ISessionProxy* destsession=svc->connect(m_dest, coral::Update);
163  try{
164  //check if hltkey already exists
165  destsession->transaction().start(true);
166  bool hltkeyExists=false;
167  coral::AttributeList kQueryBindList;
168  kQueryBindList.extend("hltkey",typeid(std::string));
169  coral::IQuery* kQuery=destsession->nominalSchema().tableHandle(LumiNames::trghltMapTableName()).newQuery();
170  kQuery->setCondition("HLTKEY =:hltkey",kQueryBindList);
171  kQueryBindList["hltkey"].data<std::string>()=hltkey;
172  coral::ICursor& kResult=kQuery->execute();
173  while( kResult.next() ){
174  hltkeyExists=true;
175  }
176  if(hltkeyExists){
177  std::cout<<"hltkey "<<hltkey<<" already registered , do nothing"<<std::endl;
178  destsession->transaction().commit();
179  delete kQuery;
180  delete svc;
181  return;
182  }
183  destsession->transaction().commit();
184  destsession->transaction().start(false);
185  coral::ISchema& destschema=destsession->nominalSchema();
186  coral::ITable& hltconftable=destschema.tableHandle(LumiNames::trghltMapTableName());
187  coral::AttributeList hltconfData;
188  hltconfData.extend<std::string>("HLTKEY");
189  hltconfData.extend<std::string>("HLTPATHNAME");
190  hltconfData.extend<std::string>("L1SEED");
191  coral::IBulkOperation* hltconfInserter=hltconftable.dataEditor().bulkInsert(hltconfData,200);
192  hltconfData["HLTKEY"].data<std::string>()=hltkey;
193  std::string& hltpathname=hltconfData["HLTPATHNAME"].data<std::string>();
194  std::string& l1seedname=hltconfData["L1SEED"].data<std::string>();
195  for(mIt=mBeg; mIt!=mEnd; ++mIt ){
196  hltpathname=mIt->first;
197  l1seedname=mIt->second;
198  hltconfInserter->processNextIteration();
199  //std::cout<<mIt->first<<" "<<mIt->second<<std::endl;
200  }
201  hltconfInserter->flush();
202  delete hltconfInserter;
203  destsession->transaction().commit();
204  }catch( const coral::Exception& er){
205  std::cout<<"database problem "<<er.what()<<std::endl;
206  destsession->transaction().rollback();
207  delete destsession;
208  delete svc;
209  throw er;
210  }
211  delete destsession;
212  delete svc;
213  }
214  const std::string HLTConf2DB::dataType() const{
215  return "HLTConf";
216  }
217  const std::string HLTConf2DB::sourceType() const{
218  return "DB";
219  }
221 }//ns lumi
224 #endif
virtual ~HLTConf2DB()
Definition: HLTConf2DB.cc:220
virtual void retrieveData(unsigned int)
Definition: HLTConf2DB.cc:43
virtual const std::string dataType() const
Definition: HLTConf2DB.cc:214
tuple lumi
Definition: fjr2json.py:35
std::string m_dest
Definition: DataPipe.h:27
void setAuthentication(const std::string &authPath)
Definition: DBConfig.cc:15
virtual const std::string sourceType() const
Definition: HLTConf2DB.cc:217
std::string m_source
Definition: DataPipe.h:28
double q1[4]
Definition: TauolaWrapper.h:87
static const std::string trghltMapTableName()
Definition: LumiNames.cc:20
HLTConf2DB(const std::string &dest)
Definition: HLTConf2DB.cc:42
std::string m_authpath
Definition: DataPipe.h:29
tuple cout
Definition: gather_cfg.py:121
#define DEFINE_EDM_PLUGIN(factory, type, name)
tuple destsession
Definition: DBCopy.py:270