Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/RPC/interface/RPCEMapSourceHandler.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0003 #include "FWCore/ServiceRegistry/interface/Service.h"
0004 #include "CondCore/CondDB/interface/ConnectionPool.h"
0005 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0006 
0007 popcon::RPCEMapSourceHandler::RPCEMapSourceHandler(const edm::ParameterSet& ps)
0008     : m_name(ps.getUntrackedParameter<std::string>("name", "RPCEMapSourceHandler")),
0009       m_dummy(ps.getUntrackedParameter<int>("WriteDummy", 0)),
0010       m_validate(ps.getUntrackedParameter<int>("Validate", 0)),
0011       m_connect(ps.getUntrackedParameter<std::string>("OnlineConn", "")),
0012       m_authpath(ps.getUntrackedParameter<std::string>("OnlineAuthPath", ".")) {}
0013 
0014 popcon::RPCEMapSourceHandler::~RPCEMapSourceHandler() {}
0015 
0016 void popcon::RPCEMapSourceHandler::getNewObjects() {
0017   //    std::cout << "RPCEMapSourceHandler: RPCEMapSourceHandler::getNewObjects begins\n";
0018 
0019   edm::Service<cond::service::PoolDBOutputService> mydbservice;
0020 
0021   // first check what is already there in offline DB
0022   Ref payload;
0023   if (m_validate == 1 && tagInfo().size > 0) {
0024     std::cout << " Validation was requested, so will check present contents" << std::endl;
0025     std::cout << "Name of tag : " << tagInfo().name << ", tag size : " << tagInfo().size << ", last object valid since "
0026               << tagInfo().lastInterval.since << std::endl;
0027     payload = lastPayload();
0028   }
0029 
0030   // now construct new cabling map from online DB
0031   time_t rawtime;
0032   time(&rawtime);              //time since January 1, 1970
0033   tm* ptm = gmtime(&rawtime);  //GMT time
0034   char buffer[20];
0035   strftime(buffer, 20, "%d/%m/%Y_%H:%M:%S", ptm);
0036   std::string eMap_version = (std::string)buffer;
0037 
0038   eMap = new RPCEMap(eMap_version);
0039   if (m_dummy == 0) {
0040     ConnectOnlineDB(m_connect, m_authpath);
0041     readEMap1();
0042     DisconnectOnlineDB();
0043   }
0044 
0045   cond::Time_t snc = mydbservice->currentTime();
0046 
0047   // look for recent changes
0048   int difference = 1;
0049   if (m_validate == 1)
0050     difference = Compare2EMaps(payload, eMap);
0051   if (!difference)
0052     std::cout << "No changes - will not write anything!!!" << std::endl;
0053   if (difference == 1) {
0054     std::cout << "Will write new object to offline DB!!!" << std::endl;
0055     m_to_transfer.push_back(std::make_pair((RPCEMap*)eMap, snc));
0056   }
0057 
0058   //    std::cout << "RPCEMapSourceHandler: RPCEMapSourceHandler::getNewObjects ends\n";
0059 }
0060 
0061 void popcon::RPCEMapSourceHandler::ConnectOnlineDB(std::string connect, std::string authPath) {
0062   cond::persistency::ConnectionPool connection;
0063   std::cout << "RPCEMapConfigSourceHandler: connecting to " << connect << "..." << std::flush;
0064   connection.setAuthenticationPath(authPath);
0065   connection.configure();
0066   session = connection.createSession(connect, true);
0067   std::cout << "Done." << std::endl;
0068 }
0069 
0070 void popcon::RPCEMapSourceHandler::DisconnectOnlineDB() { session.close(); }
0071 
0072 void popcon::RPCEMapSourceHandler::readEMap1() {
0073   session.transaction().start(true);
0074   coral::ISchema& schema = session.nominalSchema();
0075   std::string condition = "";
0076   coral::AttributeList conditionData;
0077 
0078   std::cout << std::endl << "RPCEMapSourceHandler: start to build RPC e-Map..." << std::flush << std::endl << std::endl;
0079 
0080   // Get FEDs
0081   RPCEMap::dccItem thisDcc;
0082   coral::IQuery* query1 = schema.newQuery();
0083   query1->addToTableList("DCCBOARD");
0084   query1->addToOutputList("DCCBOARD.DCCBOARDID", "DCCBOARDID");
0085   query1->addToOutputList("DCCBOARD.FEDNUMBER", "FEDNUMBER");
0086   query1->addToOrderList("FEDNUMBER");
0087   condition = "DCCBOARD.DCCBOARDID>0";
0088   query1->setCondition(condition, conditionData);
0089   //  std::cout<<"Getting DCCBOARD...";
0090   coral::ICursor& cursor1 = query1->execute();
0091   std::cout << "OK" << std::endl;
0092   std::pair<int, int> tmp_tbl;
0093   std::vector<std::pair<int, int> > theDAQ;
0094   while (cursor1.next()) {
0095     //    cursor1.currentRow().toOutputStream( std::cout ) << std::endl;
0096     const coral::AttributeList& row = cursor1.currentRow();
0097     tmp_tbl.first = row["DCCBOARDID"].data<long long>();
0098     tmp_tbl.second = row["FEDNUMBER"].data<long long>();
0099     theDAQ.push_back(tmp_tbl);
0100   }
0101   delete query1;
0102 
0103   for (unsigned int iFED = 0; iFED < theDAQ.size(); iFED++) {
0104     thisDcc.theId = theDAQ[iFED].second;
0105     std::vector<std::pair<int, int> > theTB;
0106     // get TBs
0107     RPCEMap::tbItem thisTB;
0108     coral::IQuery* query2 = schema.newQuery();
0109     query2->addToTableList("TRIGGERBOARD");
0110     query2->addToOutputList("TRIGGERBOARD.TRIGGERBOARDID", "TRIGGERBOARDID");
0111     query2->addToOutputList("TRIGGERBOARD.DCCINPUTCHANNELNUM", "DCCCHANNELNUM");
0112     query2->addToOrderList("DCCCHANNELNUM");
0113     condition = "TRIGGERBOARD.DCCBOARD_DCCBOARDID=" + IntToString(theDAQ[iFED].first);
0114     query2->setCondition(condition, conditionData);
0115     coral::ICursor& cursor2 = query2->execute();
0116     int ntbs = 0;
0117     while (cursor2.next()) {
0118       ntbs++;
0119       //      cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
0120       const coral::AttributeList& row = cursor2.currentRow();
0121       tmp_tbl.first = row["TRIGGERBOARDID"].data<long long>();
0122       tmp_tbl.second = row["DCCCHANNELNUM"].data<long long>();
0123       theTB.push_back(tmp_tbl);
0124     }
0125     delete query2;
0126     for (unsigned int iTB = 0; iTB < theTB.size(); iTB++) {
0127       thisTB.theNum = theTB[iTB].second;
0128       std::vector<std::pair<int, int> > theLink;
0129       // get links
0130       RPCEMap::linkItem thisLink;
0131       coral::IQuery* query3 = schema.newQuery();
0132       query3->addToTableList("BOARDBOARDCONN");
0133       query3->addToOutputList("BOARDBOARDCONN.BOARD_BOARDID", "BOARDID");
0134       query3->addToOutputList("BOARDBOARDCONN.COLLECTORBOARDINPUTNUM", "TBINPUTNUM");
0135       query3->addToOrderList("TBINPUTNUM");
0136       condition = "BOARDBOARDCONN.BOARD_COLLECTORBOARDID=" + IntToString(theTB[iTB].first);
0137       query3->setCondition(condition, conditionData);
0138       coral::ICursor& cursor3 = query3->execute();
0139       int nlinks = 0;
0140       while (cursor3.next()) {
0141         nlinks++;
0142         const coral::AttributeList& row = cursor3.currentRow();
0143         tmp_tbl.first = row["BOARDID"].data<long long>();
0144         tmp_tbl.second = row["TBINPUTNUM"].data<long long>();
0145         theLink.push_back(tmp_tbl);
0146       }
0147       delete query3;
0148       for (unsigned int iLink = 0; iLink < theLink.size(); iLink++) {
0149         int boardId = theLink[iLink].first;
0150         thisLink.theTriggerBoardInputNumber = theLink[iLink].second;
0151         std::vector<std::pair<int, std::string> > theLB;
0152         std::pair<int, std::string> tmpLB;
0153         // Get master LBs first...
0154         RPCEMap::lbItem thisLB;
0155         coral::IQuery* query4 = schema.newQuery();
0156         query4->addToTableList("BOARD");
0157         query4->addToOutputList("BOARD.NAME", "NAME");
0158         condition = "BOARD.BOARDID=" + IntToString(theLink[iLink].first);
0159         query4->setCondition(condition, conditionData);
0160         coral::ICursor& cursor4 = query4->execute();
0161         int nlbs = 0;
0162         while (cursor4.next()) {
0163           nlbs++;
0164           const coral::AttributeList& row = cursor4.currentRow();
0165           tmpLB.first = theLink[iLink].first;
0166           tmpLB.second = row["NAME"].data<std::string>();
0167           theLB.push_back(tmpLB);
0168         }
0169         delete query4;
0170         // then slaves
0171         coral::IQuery* query5 = schema.newQuery();
0172         query5->addToTableList("LINKBOARD");
0173         query5->addToTableList("BOARD");
0174         query5->addToOutputList("LINKBOARD.LINKBOARDID", "LINKBOARDID");
0175         query5->addToOutputList("BOARD.NAME", "NAME");
0176         query5->addToOrderList("LINKBOARDID");
0177         condition = "LINKBOARD.MASTERID=" + IntToString(theLink[iLink].first) +
0178                     " AND BOARD.BOARDID=LINKBOARD.LINKBOARDID AND LINKBOARD.MASTERID<>LINKBOARD.LINKBOARDID";
0179         query5->setCondition(condition, conditionData);
0180         coral::ICursor& cursor5 = query5->execute();
0181         while (cursor5.next()) {
0182           nlbs++;
0183           const coral::AttributeList& row = cursor5.currentRow();
0184           tmpLB.first = row["LINKBOARDID"].data<long long>();
0185           tmpLB.second = row["NAME"].data<std::string>();
0186           theLB.push_back(tmpLB);
0187         }
0188         delete query5;
0189         for (unsigned int iLB = 0; iLB < theLB.size(); iLB++) {
0190           thisLB.theMaster = (theLB[iLB].first == boardId);
0191           // extract all relevant information from BOARD.NAME
0192           std::string theName = theLB[iLB].second;
0193           int slength = theName.length();
0194           thisLB.theLinkBoardNumInLink = atoi((theName.substr(slength - 1, 1)).c_str());
0195           int wheel = atoi((theName.substr(6, 1)).c_str());
0196           std::string char1 = theName.substr(4, 1);
0197           std::string char2 = theName.substr(slength - 7, 1);
0198           int num3 = atoi((theName.substr(slength - 6, 1)).c_str());
0199           std::string char4 = theName.substr(slength - 5, 1);
0200           bool itsS1to9 = (theName.substr(slength - 11, 1) == "S");
0201           int n1 = 10;
0202           int n2 = 1;
0203           int n3 = 0;
0204           if (!itsS1to9) {
0205             n1 = 11;
0206             n2 = 2;
0207           }
0208           int sector = atoi((theName.substr(slength - n1, n2)).c_str());
0209           std::string char1Val[2] = {"B", "E"};                                     // 1,2
0210           std::string char2Val[3] = {"N", "M", "P"};                                // 0,1,2
0211           std::string char4Val[9] = {"0", "1", "2", "3", "A", "B", "C", "D", "E"};  // 0,...,8
0212           for (int i = 0; i < 2; i++)
0213             if (char1 == char1Val[i])
0214               n1 = i + 1;
0215           for (int i = 0; i < 3; i++)
0216             if (char2 == char2Val[i])
0217               n2 = i;
0218           for (int i = 0; i < 9; i++)
0219             if (char4 == char4Val[i])
0220               n3 = i;
0221           thisLB.theCode = n3 + num3 * 10 + n2 * 100 + n1 * 1000 + wheel * 10000 + sector * 100000;
0222           std::vector<FEBStruct> theFEB;
0223           // get FEBs
0224           coral::IQuery* query6 = schema.newQuery();
0225           query6->addToTableList("FEBLOCATION");
0226           query6->addToTableList("FEBCONNECTOR");
0227           query6->addToOutputList("FEBLOCATION.FEBLOCATIONID", "FEBLOCATIONID");
0228           query6->addToOutputList("FEBLOCATION.CL_CHAMBERLOCATIONID", "CHAMBERLOCATIONID");
0229           query6->addToOutputList("FEBCONNECTOR.FEBCONNECTORID", "FEBCONNECTORID");
0230           query6->addToOutputList("FEBLOCATION.FEBLOCALETAPARTITION", "LOCALETAPART");
0231           query6->addToOutputList("FEBLOCATION.POSINLOCALETAPARTITION", "POSINLOCALETAPART");
0232           query6->addToOutputList("FEBLOCATION.FEBCMSETAPARTITION", "CMSETAPART");
0233           query6->addToOutputList("FEBLOCATION.POSINCMSETAPARTITION", "POSINCMSETAPART");
0234           query6->addToOutputList("FEBCONNECTOR.LINKBOARDINPUTNUM", "LINKBOARDINPUTNUM");
0235           query6->addToOrderList("FEBLOCATIONID");
0236           query6->addToOrderList("FEBCONNECTORID");
0237           condition = "FEBLOCATION.LB_LINKBOARDID=" + IntToString(theLB[iLB].first) +
0238                       " AND FEBLOCATION.FEBLOCATIONID=FEBCONNECTOR.FL_FEBLOCATIONID";
0239           query6->setCondition(condition, conditionData);
0240           coral::ICursor& cursor6 = query6->execute();
0241           int nfebs = 0;
0242           while (cursor6.next()) {
0243             FEBStruct tmpFEB;
0244             nfebs++;
0245             const coral::AttributeList& row = cursor6.currentRow();
0246             tmpFEB.febId = row["FEBLOCATIONID"].data<long long>();
0247             tmpFEB.chamberId = row["CHAMBERLOCATIONID"].data<long long>();
0248             tmpFEB.connectorId = row["FEBCONNECTORID"].data<long long>();
0249             tmpFEB.localEtaPart = row["LOCALETAPART"].data<std::string>();
0250             tmpFEB.posInLocalEtaPart = row["POSINLOCALETAPART"].data<short>();
0251             tmpFEB.cmsEtaPart = row["CMSETAPART"].data<std::string>();
0252             tmpFEB.posInCmsEtaPart = row["POSINCMSETAPART"].data<short>();
0253             tmpFEB.lbInputNum = row["LINKBOARDINPUTNUM"].data<short>();
0254             theFEB.push_back(tmpFEB);
0255           }
0256           delete query6;
0257           for (unsigned int iFEB = 0; iFEB < theFEB.size(); iFEB++) {
0258             RPCEMap::febItem thisFeb;
0259             std::string temp = theFEB[iFEB].localEtaPart;
0260             std::string localEtaVal[6] = {"Forward", "Central", "Backward", "A", "B", "C"};
0261             char localEtaPartition = 0;
0262             for (int i = 0; i < 6; i++)
0263               if (temp == localEtaVal[i])
0264                 localEtaPartition = i + 1;
0265             char positionInLocalEtaPartition = theFEB[iFEB].posInLocalEtaPart;
0266             temp = theFEB[iFEB].cmsEtaPart;
0267             std::string cmsEtaVal[6] = {"1", "2", "3", "A", "B", "C"};
0268             char cmsEtaPartition = 0;
0269             for (int i = 0; i < 6; i++)
0270               if (temp == cmsEtaVal[i])
0271                 cmsEtaPartition = i + 1;
0272             char positionInCmsEtaPartition = theFEB[iFEB].posInCmsEtaPart;
0273             thisFeb.thePartition = positionInLocalEtaPartition + 10 * localEtaPartition +
0274                                    100 * positionInCmsEtaPartition + 1000 * cmsEtaPartition;
0275             thisFeb.theLinkBoardInputNum = theFEB[iFEB].lbInputNum;
0276             // Get chamber
0277             coral::IQuery* query7 = schema.newQuery();
0278             query7->addToTableList("CHAMBERLOCATION");
0279             query7->addToOutputList("CHAMBERLOCATION.DISKORWHEEL", "DISKORWHEEL");
0280             query7->addToOutputList("CHAMBERLOCATION.LAYER", "LAYER");
0281             query7->addToOutputList("CHAMBERLOCATION.SECTOR", "SECTOR");
0282             query7->addToOutputList("CHAMBERLOCATION.SUBSECTOR", "SUBSECTOR");
0283             query7->addToOutputList("CHAMBERLOCATION.CHAMBERLOCATIONNAME", "NAME");
0284             query7->addToOutputList("CHAMBERLOCATION.FEBZORNT", "FEBZORNT");
0285             query7->addToOutputList("CHAMBERLOCATION.FEBRADORNT", "FEBRADORNT");
0286             query7->addToOutputList("CHAMBERLOCATION.BARRELORENDCAP", "BARRELORENDCAP");
0287             condition = "CHAMBERLOCATION.CHAMBERLOCATIONID=" + IntToString(theFEB[iFEB].chamberId);
0288             query7->setCondition(condition, conditionData);
0289             coral::ICursor& cursor7 = query7->execute();
0290             thisFeb.theChamber = -1;
0291             while (cursor7.next()) {
0292               const coral::AttributeList& row = cursor7.currentRow();
0293               char diskOrWheel = row["DISKORWHEEL"].data<short>() + 3;
0294               char layer = row["LAYER"].data<short>();
0295               int sector = row["SECTOR"].data<short>();
0296               temp = row["SUBSECTOR"].data<std::string>();
0297               // TEMPORARY TO CORRECT A LITTLE BUG IN OMDS
0298               //              std::string chname=row["NAME"].data<std::string>();
0299               //              if (layer==6 && (sector==9 || sector==11)) {
0300               //                if (temp=="+") {
0301               //                  temp="";
0302               //                  std::cout<<"Changed subsector + to null for "<<chname<<std::endl;
0303               //                }
0304               //              }
0305 
0306               std::string subsVal[5] = {"--", "-", "", "+", "++"};
0307               char subsector = 0;
0308               for (int i = 0; i < 5; i++)
0309                 if (temp == subsVal[i])
0310                   subsector = i;
0311               temp = row["FEBZORNT"].data<std::string>();
0312               char febZOrnt = 0;
0313               if (temp == "+z")
0314                 febZOrnt = 1;
0315               temp = row["FEBRADORNT"].data<std::string>();
0316               char febZRadOrnt = 0;
0317               std::string febZRVal[3] = {"", "IN", "OUT"};
0318               for (int i = 0; i < 3; i++)
0319                 if (temp == febZRVal[i])
0320                   febZRadOrnt = i;
0321               temp = row["BARRELORENDCAP"].data<std::string>();
0322               char barrelOrEndcap = 0;
0323               if (temp == "Barrel")
0324                 barrelOrEndcap = 1;
0325               thisFeb.theChamber = sector + 100 * subsector + 1000 * febZRadOrnt + 5000 * febZOrnt +
0326                                    10000 * diskOrWheel + 100000 * layer + 1000000 * barrelOrEndcap;
0327             }
0328             delete query7;
0329             // Get Strips
0330             coral::IQuery* query8 = schema.newQuery();
0331             query8->addToTableList("CHAMBERSTRIP");
0332             query8->addToOutputList("CHAMBERSTRIP.CABLECHANNELNUM", "CABLECHANNELNUM");
0333             query8->addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "CHAMBERSTRIPNUM");
0334             //            query8->addToOutputList("CHAMBERSTRIP.CMSSTRIPNUMBER","CMSSTRIPNUM");
0335             query8->addToOrderList("CABLECHANNELNUM");
0336             condition = "CHAMBERSTRIP.FC_FEBCONNECTORID=" + IntToString(theFEB[iFEB].connectorId);
0337             query8->setCondition(condition, conditionData);
0338             coral::ICursor& cursor8 = query8->execute();
0339             // NEW: do not store all the strip information as goes, only the minimum data needed to
0340             // reproduce it later on
0341             int nstrips = 0;
0342             int firstChamberStrip = 0;
0343             int firstPin = 0;
0344             int lastChamberStrip = 0;
0345             int lastPin = 0;
0346             while (cursor8.next()) {
0347               const coral::AttributeList& row = cursor8.currentRow();
0348               lastChamberStrip = row["CHAMBERSTRIPNUM"].data<int>();
0349               lastPin = row["CABLECHANNELNUM"].data<short>();
0350               if (nstrips == 0) {
0351                 firstChamberStrip = lastChamberStrip;
0352                 firstPin = lastPin;
0353               }
0354               nstrips++;
0355             }
0356             delete query8;
0357             int algo = -1;
0358             if (firstPin == 1 && lastPin == nstrips) {
0359               algo = 1;
0360             } else if (firstPin == 2 && lastPin == nstrips + 1) {
0361               algo = 2;
0362             } else if (firstPin == 3 && lastPin == nstrips + 2) {
0363               algo = 3;
0364             } else if (firstPin == 2 && lastPin == nstrips + 2) {
0365               algo = 0;
0366             } else {
0367               std::cout << " Unknown algo : " << firstPin << " " << lastPin << std::endl;
0368             }
0369             if ((lastPin - firstPin) * (lastChamberStrip - firstChamberStrip) < 0)
0370               algo = algo + 4;
0371             thisFeb.theAlgo = algo + 100 * firstChamberStrip + 10000 * nstrips;
0372             eMap->theFebs.push_back(thisFeb);
0373           }
0374           thisLB.nFebs = nfebs;
0375           eMap->theLBs.push_back(thisLB);
0376         }
0377         thisLink.nLBs = nlbs;
0378         eMap->theLinks.push_back(thisLink);
0379       }
0380       thisTB.nLinks = nlinks;
0381       eMap->theTBs.push_back(thisTB);
0382     }
0383     thisDcc.nTBs = ntbs;
0384     std::cout << "DCC added" << std::endl;
0385     eMap->theDccs.push_back(thisDcc);
0386   }
0387   std::cout << std::endl << "Building RPC e-Map done!" << std::flush << std::endl << std::endl;
0388 }
0389 
0390 int popcon::RPCEMapSourceHandler::Compare2EMaps(const Ref& _map1, RPCEMap* map2) {
0391   const Ref& map1 = _map1;
0392   RPCReadOutMapping const* oldmap1 = map1->convert();
0393   RPCReadOutMapping const* oldmap2 = map2->convert();
0394   std::vector<const DccSpec*> dccs1 = oldmap1->dccList();
0395   std::vector<const DccSpec*> dccs2 = oldmap2->dccList();
0396   if (dccs1.size() != dccs2.size()) {
0397     return 1;
0398   }
0399   std::pair<int, int> dccRange1 = oldmap1->dccNumberRange();
0400   std::pair<int, int> dccRange2 = oldmap2->dccNumberRange();
0401   if (dccRange1.first != dccRange2.first) {
0402     return 1;
0403   }
0404   if (dccRange1.second != dccRange2.second) {
0405     return 1;
0406   }
0407   typedef std::vector<const DccSpec*>::const_iterator IDCC;
0408   IDCC idcc2 = dccs2.begin();
0409   for (IDCC idcc1 = dccs1.begin(); idcc1 != dccs1.end(); idcc1++) {
0410     int dccNo = (**idcc1).id();
0411     std::string dccContents = (**idcc1).print(4);
0412     if ((**idcc2).id() != dccNo) {
0413       return 1;
0414     }
0415     if ((**idcc2).print(4) != dccContents) {
0416       return 1;
0417     }
0418     idcc2++;
0419   }
0420   return 0;
0421 }