Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:15

0001 #include "CondCore/DBCommon/interface/DBWriter.h"
0002 #include "CondCore/DBCommon/interface/DBSession.h"
0003 #include "CondCore/DBCommon/interface/Exception.h"
0004 #include "CondCore/DBCommon/interface/ServiceLoader.h"
0005 #include "CondCore/DBCommon/interface/ConnectMode.h"
0006 #include "CondFormats/HcalObjects/interface/AllObjects.h"
0007 #include "CondCore/IOVService/interface/IOV.h"
0008 #include "CondCore/MetaDataService/interface/MetaData.h"
0009 #include <string>
0010 #include <iostream>
0011 int main(){
0012   std::string db ("sqlite_file:test.db");
0013   cond::ServiceLoader* loader=new cond::ServiceLoader;
0014   //loader->loadMessageService(cond::Info);
0015   loader->loadMessageService(cond::Error);
0016   cond::DBSession* session=new cond::DBSession(db);
0017   // access metadata first
0018   std::cout << "Quering (empty yet) metadata" << std::endl;
0019   try {
0020     cond::MetaData metadata_svc(db, *loader);
0021     metadata_svc.connect();
0022     metadata_svc.addMapping ("dummy", "empty_token");
0023     std::string token = metadata_svc.getToken("mytest1");
0024     metadata_svc.disconnect();
0025   }catch(std::exception& er){
0026     std::cout<<"1"<<er.what()<<std::endl;
0027   }catch(...){
0028     std::cout<<"1"<<"Funny error"<<std::endl;
0029   }
0030   std::cout << "Writing objects" << std::endl;
0031 
0032   try{
0033     session->connect(cond::ReadWriteCreate);
0034     cond::DBWriter pwriter(*session, "HcalPedestals");
0035     cond::DBWriter iovwriter(*session, "IOV");
0036     session->startUpdateTransaction();
0037     ///init iov sequence
0038     cond::IOV* initiov=new cond::IOV;
0039     std::vector<std::string> pedtoks;
0040     for(int i=0; i<4;++i){
0041       HcalPedestals* ped=new HcalPedestals;
0042       for(int ichannel=1; ichannel<=5; ++ichannel){
0043     HcalDetId id (HcalBarrel, ichannel, 1, 1); // HB eta=ichannel, phi=1
0044     ped->addValue (id, 5, 5, 5, 5);
0045       }
0046       ped->sort ();
0047       std::string pedtok=pwriter.markWrite<HcalPedestals>(ped);
0048       pedtoks.push_back(pedtok);
0049       if(i<2){
0050     initiov->iov.insert(std::make_pair(1,pedtok));
0051       }else{
0052     initiov->iov.insert(std::make_pair(2,pedtok));
0053       }
0054     }
0055     iovwriter.markWrite<cond::IOV>(initiov);
0056     session->commit();
0057     //create a new iov sequence
0058     session->startUpdateTransaction();
0059     cond::IOV* bestiov=new cond::IOV;
0060     int counter=0;
0061     for(std::vector<std::string>::iterator it=pedtoks.begin(); 
0062     it!=pedtoks.end(); ++it){
0063       ++counter;
0064       if(counter<3){
0065     bestiov->iov.insert(std::make_pair(1,*it));
0066       }else{
0067     bestiov->iov.insert(std::make_pair(2,*it));
0068       }
0069     }
0070     std::string bestiovtok=iovwriter.markWrite<cond::IOV>(bestiov);
0071     session->commit();
0072     session->disconnect();
0073 
0074     // put metadata
0075     cond::MetaData metadata_svc(db, *loader);
0076     metadata_svc.connect();
0077     metadata_svc.addMapping("mytest1",bestiovtok);
0078     metadata_svc.disconnect();
0079   }catch(cond::Exception& er){
0080     std::cout<<"2"<<er.what()<<std::endl;
0081   }catch(std::exception& er){
0082     std::cout<<"2"<<er.what()<<std::endl;
0083   }catch(...){
0084     std::cout<<"2"<<"Funny error"<<std::endl;
0085   }
0086   delete session;
0087   delete loader;
0088 }
0089