Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:47

0001 /**
0002  *   cmsecal_write_offline_det_id.cpp
0003  *
0004  *   Reads the ECAL CondDB 'channelview' table a list of crystal IDs
0005  *   (logic_id) and writes back a mapping of EBDetId.rawId() to
0006  *   logic_id as well as a mapping of ieta, iphi to logic_id
0007  *   
0008  *   Author: Ricky Egeland, with help by Zhen Xie!
0009  */
0010 
0011 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0012 #include "RelationalAccess/IConnectionService.h"
0013 #include "RelationalAccess/IConnectionServiceConfiguration.h"
0014 #include "RelationalAccess/AccessMode.h"
0015 #include "RelationalAccess/ISessionProxy.h"
0016 #include "RelationalAccess/ITransaction.h"
0017 #include "RelationalAccess/ISchema.h"
0018 #include "RelationalAccess/ITable.h"
0019 #include "RelationalAccess/IQuery.h"
0020 #include "RelationalAccess/ICursor.h"
0021 #include "RelationalAccess/ITableDataEditor.h"
0022 #include "RelationalAccess/IBulkOperation.h"
0023 #include "RelationalAccess/SchemaException.h"
0024 #include "CoralBase/AttributeList.h"
0025 #include "CoralBase/Attribute.h"
0026 #include "CoralBase/AttributeSpecification.h"
0027 #include "CoralBase/Exception.h"
0028 #include "CoralKernel/Context.h"
0029 #include "CoralBase/MessageStream.h"
0030 
0031 #include <iostream>
0032 #include <string>
0033 #include <vector>
0034 #include <ctime>
0035 
0036 class CondDBApp {
0037 public:
0038   /**
0039    *   App constructor; Makes the database connection
0040    */
0041   CondDBApp(std::string connect, std::string user, std::string pass) {
0042     std::cout << "Loading services..." << std::flush;
0043 
0044     if (!::getenv("POOL_OUTMSG_LEVEL")) {  //if not set, default to warning
0045       coral::MessageStream::setMsgVerbosity(coral::Warning);
0046     } else {
0047       coral::MessageStream::setMsgVerbosity(coral::Debug);
0048     }
0049     coral::Context& ctx = coral::Context::instance();
0050     ctx.loadComponent("CORAL/Services/ConnectionService");
0051     coral::IHandle<coral::IConnectionService> conHandle =
0052         ctx.query<coral::IConnectionService>("CORAL/Services/ConnectionService");
0053 
0054     if (!conHandle.isValid()) {
0055       throw std::runtime_error("Could not locate the connection service");
0056     }
0057     conHandle->configuration().setAuthenticationService("CORAL/Services/EnvironmentAuthenticationService");
0058     m_proxy.reset(conHandle->connect(connect, coral::Update));
0059   }
0060   /**
0061    *  App destructor
0062    */
0063   ~CondDBApp() {}
0064 
0065   void writeMapping() {
0066     std::cout << "Starting transaction..." << std::flush;
0067     m_proxy->transaction().start();
0068 
0069     std::cout << "Setting query..." << std::flush;
0070     coral::IQuery* cvQuery = m_proxy->nominalSchema().tableHandle("CHANNELVIEW").newQuery();
0071     cvQuery->addToOutputList("ID1");
0072     cvQuery->addToOutputList("ID2");
0073     cvQuery->addToOutputList("LOGIC_ID");
0074     cvQuery->defineOutputType("ID1", "int");
0075     cvQuery->defineOutputType("ID2", "int");
0076     cvQuery->defineOutputType("LOGIC_ID", "int");
0077 
0078     cvQuery->addToOrderList("ID1");
0079     cvQuery->addToOrderList("ID2");
0080 
0081     std::string where = "NAME = :name AND MAPS_TO = :maps_to";
0082     coral::AttributeList whereData;
0083     whereData.extend<std::string>("NAME");
0084     whereData.extend<std::string>("MAPS_TO");
0085     whereData[0].data<std::string>() = "EB_crystal_number";
0086     whereData[1].data<std::string>() = "EB_crystal_number";
0087     cvQuery->setCondition(where, whereData);
0088     cvQuery->setRowCacheSize(61200);  // number of crystals in barrel
0089 
0090     std::cout << "Getting editor for CHANNELVIEW..." << std::flush;
0091     coral::ITableDataEditor& cvEditor = m_proxy->nominalSchema().tableHandle("CHANNELVIEW").dataEditor();
0092 
0093     std::cout << "Setting up buffers..." << std::flush;
0094     coral::AttributeList rowBuffer;
0095     rowBuffer.extend<std::string>("NAME");
0096     rowBuffer.extend<int>("ID1");
0097     rowBuffer.extend<int>("ID2");
0098     rowBuffer.extend<int>("ID3");
0099     rowBuffer.extend<std::string>("MAPS_TO");
0100     rowBuffer.extend<int>("LOGIC_ID");
0101 
0102     std::string& name = rowBuffer[0].data<std::string>();
0103     int& id1 = rowBuffer[1].data<int>();
0104     int& id2 = rowBuffer[2].data<int>();
0105     int& id3 = rowBuffer[3].data<int>();
0106     std::string& mapsTo = rowBuffer[4].data<std::string>();
0107     int& logicId = rowBuffer[5].data<int>();
0108 
0109     coral::IBulkOperation* bulkInserter =
0110         cvEditor.bulkInsert(rowBuffer, 37 * 1700 * 2);  // number of crystals in barrel * number of mappings
0111     std::cout << "Done." << std::endl;
0112 
0113     std::cout << "Looping over supermodule" << std::endl;
0114     EBDetId ebid;
0115     int SM = 0;
0116     int offSM = 0;  // SM slot in the offline
0117     int xtal = 0;
0118     int detId = 0;
0119     int ieta = 0;
0120     int iphi = 0;
0121     id1 = id2 = id3 = 0;
0122     coral::ICursor& cvCursor = cvQuery->execute();
0123     while (cvCursor.next()) {
0124       SM = cvCursor.currentRow()["ID1"].data<int>();
0125       xtal = cvCursor.currentRow()["ID2"].data<int>();
0126       logicId = cvCursor.currentRow()["LOGIC_ID"].data<int>();
0127 
0128       // Both construction SM 0 and 36 are mapped to
0129       // ECAL slot 36 in the offline
0130       if (SM != 0) {
0131         offSM = SM;
0132       } else {
0133         offSM = 36;
0134       }
0135 
0136       ebid = EBDetId(offSM, xtal, EBDetId::SMCRYSTALMODE);
0137       detId = ebid.rawId();
0138       ieta = ebid.ieta();
0139       iphi = ebid.iphi();
0140 
0141       std::cout << "SM " << SM << " xtal " << xtal << " logic_id " << logicId << " ieta " << ieta << " iphi " << iphi
0142                 << " det_id " << detId << std::endl;
0143       name = "Offline_det_id";
0144       mapsTo = "EB_crystal_number";
0145       id1 = detId;
0146       rowBuffer["ID2"].setNull(true);
0147       rowBuffer["ID3"].setNull(true);
0148       bulkInserter->processNextIteration();
0149 
0150       name = "EB_crystal_angle";
0151       mapsTo = "EB_crystal_number";
0152       id1 = ieta;
0153       rowBuffer["ID2"].setNull(false);
0154       id2 = iphi;
0155       rowBuffer["ID3"].setNull(true);
0156       bulkInserter->processNextIteration();
0157     }
0158     bulkInserter->flush();
0159     delete bulkInserter;
0160     std::cout << "Done." << std::endl;
0161 
0162     std::cout << "Getting editor for VIEWDESCRIPTION..." << std::flush;
0163     coral::ITableDataEditor& vdEditor = m_proxy->nominalSchema().tableHandle("VIEWDESCRIPTION").dataEditor();
0164 
0165     std::cout << "Setting up buffers..." << std::flush;
0166     coral::AttributeList rowBuffer2;
0167     rowBuffer2.extend<std::string>("NAME");
0168     rowBuffer2.extend<std::string>("ID1NAME");
0169     rowBuffer2.extend<std::string>("ID2NAME");
0170     rowBuffer2.extend<std::string>("ID3NAME");
0171     rowBuffer2.extend<std::string>("DESCRIPTION");
0172 
0173     std::string& vdname = rowBuffer2["NAME"].data<std::string>();
0174     std::string& id1name = rowBuffer2["ID1NAME"].data<std::string>();
0175     std::string& id2name = rowBuffer2["ID2NAME"].data<std::string>();
0176     std::string& id3name = rowBuffer2["ID3NAME"].data<std::string>();
0177     std::string& description = rowBuffer2["DESCRIPTION"].data<std::string>();
0178 
0179     id3name = "";  // Always null; using to get rid of warning
0180 
0181     vdname = "Offline_det_id";
0182     id1name = "det_id";
0183     rowBuffer2["ID2NAME"].setNull(true);
0184     rowBuffer2["ID3NAME"].setNull(true);
0185     description = "DetID rawid() as used in CMSSW";
0186     vdEditor.insertRow(rowBuffer2);
0187 
0188     vdname = "EB_crystal_angle";
0189     id1name = "ieta";
0190     rowBuffer2["ID2NAME"].setNull(false);
0191     id2name = "iphi";
0192     rowBuffer2["ID3NAME"].setNull(true);
0193     description = "Crystals in ECAL barrel super-modules by angle index";
0194     vdEditor.insertRow(rowBuffer2);
0195 
0196     std::cout << "Committing..." << std::flush;
0197     m_proxy->transaction().commit();
0198     std::cout << "Done." << std::endl;
0199   }
0200 
0201 private:
0202   std::unique_ptr<coral::ISessionProxy> m_proxy;
0203 };
0204 
0205 int main(int argc, char* argv[]) {
0206   std::string connect;
0207   std::string user;
0208   std::string pass;
0209 
0210   if (argc != 4) {
0211     std::cout << "Usage:" << std::endl;
0212     std::cout << "  " << argv[0] << " <connect string> <user> <pass>" << std::endl;
0213     exit(-1);
0214   }
0215 
0216   connect = argv[1];
0217   user = argv[2];
0218   pass = argv[3];
0219 
0220   std::string userenv("CORAL_AUTH_USER=");
0221   userenv += user;
0222   ::putenv(const_cast<char*>(userenv.c_str()));
0223   std::string passenv("CORAL_AUTH_PASSWORD=");
0224   passenv += pass;
0225   ::putenv(const_cast<char*>(passenv.c_str()));
0226 
0227   try {
0228     CondDBApp app(connect, user, pass);
0229     app.writeMapping();
0230   } catch (coral::Exception& e) {
0231     std::cerr << "coral::Exception:  " << e.what() << std::endl;
0232     //   } catch (seal::Exception &e) {
0233     //     std::cerr << "seal::Exception:  " << e.what() << std::endl;
0234   } catch (std::exception& e) {
0235     std::cerr << "ERROR:  " << e.what() << std::endl;
0236   } catch (...) {
0237     std::cerr << "Unknown error caught" << std::endl;
0238   }
0239 
0240   std::cout << "All Done." << std::endl;
0241 
0242   return 0;
0243 }