CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/CondTools/RPC/src/RPCEMapSourceHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/RPC/interface/RPCEMapSourceHandler.h"
00002 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00003 #include "FWCore/ServiceRegistry/interface/Service.h"
00004 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00005 
00006 popcon::RPCEMapSourceHandler::RPCEMapSourceHandler(const edm::ParameterSet& ps) :
00007   m_name(ps.getUntrackedParameter<std::string>("name","RPCEMapSourceHandler")),
00008   m_dummy(ps.getUntrackedParameter<int>("WriteDummy",0)),
00009   m_validate(ps.getUntrackedParameter<int>("Validate",0)),
00010   m_connect(ps.getUntrackedParameter<std::string>("OnlineConn","")),
00011   m_authpath(ps.getUntrackedParameter<std::string>("OnlineAuthPath","."))
00012 {
00013 }
00014 
00015 popcon::RPCEMapSourceHandler::~RPCEMapSourceHandler()
00016 {
00017 }
00018 
00019 void popcon::RPCEMapSourceHandler::getNewObjects()
00020 {
00021 
00022 //      std::cout << "RPCEMapSourceHandler: RPCEMapSourceHandler::getNewObjects begins\n";
00023 
00024   edm::Service<cond::service::PoolDBOutputService> mydbservice;
00025 
00026 // first check what is already there in offline DB
00027   Ref payload;
00028   if(m_validate==1 && tagInfo().size>0) {
00029     std::cout<<" Validation was requested, so will check present contents"<<std::endl;
00030     std::cout<<"Name of tag : "<<tagInfo().name << ", tag size : " << tagInfo().size 
00031             << ", last object valid since " 
00032             << tagInfo().lastInterval.first << std::endl;  
00033     payload = lastPayload();
00034   }
00035 
00036 // now construct new cabling map from online DB
00037   time_t rawtime;
00038   time(&rawtime); //time since January 1, 1970
00039   tm * ptm = gmtime(&rawtime);//GMT time
00040   char buffer[20];
00041   strftime(buffer,20,"%d/%m/%Y_%H:%M:%S",ptm);
00042   std::string eMap_version=(std::string)buffer;
00043 
00044   eMap =  new RPCEMap(eMap_version);
00045       if (m_dummy==0) {
00046         ConnectOnlineDB(m_connect,m_authpath);
00047         readEMap1();
00048         DisconnectOnlineDB();
00049       }
00050 
00051         cond::Time_t snc=mydbservice->currentTime();
00052         
00053 // look for recent changes
00054         int difference=1;
00055         if (m_validate==1) difference=Compare2EMaps(payload,eMap);
00056         if (!difference) std::cout<<"No changes - will not write anything!!!"<<std::endl;
00057         if (difference==1) {
00058           std::cout<<"Will write new object to offline DB!!!"<<std::endl;
00059           m_to_transfer.push_back(std::make_pair((RPCEMap*)eMap,snc));
00060         }
00061 
00062 //      std::cout << "RPCEMapSourceHandler: RPCEMapSourceHandler::getNewObjects ends\n";
00063 }
00064 
00065 void popcon::RPCEMapSourceHandler::ConnectOnlineDB(std::string connect, std::string authPath)
00066 {
00067   std::cout << "RPCEMapConfigSourceHandler: connecting to " << connect << "..." << std::flush;
00068   connection = new cond::DbConnection() ;
00069 //  session->configuration().setAuthenticationMethod(cond::XML);
00070   connection->configuration().setAuthenticationPath( authPath ) ;
00071   connection->configure();
00072   session = new cond::DbSession(connection->createSession());
00073   session->open(connect,true) ;
00074   std::cout << "Done." << std::endl;
00075 }
00076 
00077 void popcon::RPCEMapSourceHandler::DisconnectOnlineDB()
00078 {
00079   connection->close() ;
00080   delete connection ;
00081   session->close();
00082   delete session ;
00083 }
00084 
00085 void popcon::RPCEMapSourceHandler::readEMap1()
00086 {
00087   session->transaction().start( true );
00088   coral::ISchema& schema = session->nominalSchema();
00089   std::string condition="";
00090   coral::AttributeList conditionData;
00091 
00092   std::cout << std::endl <<"RPCEMapSourceHandler: start to build RPC e-Map..." << std::flush << std::endl << std::endl;
00093 
00094   // Get FEDs
00095   RPCEMap::dccItem thisDcc;
00096   coral::IQuery* query1 = schema.newQuery();
00097   query1->addToTableList( "DCCBOARD" );
00098   query1->addToOutputList("DCCBOARD.DCCBOARDID","DCCBOARDID");
00099   query1->addToOutputList("DCCBOARD.FEDNUMBER","FEDNUMBER");
00100   query1->addToOrderList("FEDNUMBER");
00101   condition = "DCCBOARD.DCCBOARDID>0";
00102   query1->setCondition( condition, conditionData );
00103 //  std::cout<<"Getting DCCBOARD...";
00104   coral::ICursor& cursor1 = query1->execute();
00105   std::cout<<"OK"<<std::endl;
00106   std::pair<int,int> tmp_tbl;
00107   std::vector< std::pair<int,int> > theDAQ;
00108   while ( cursor1.next() ) {
00109 //    cursor1.currentRow().toOutputStream( std::cout ) << std::endl;
00110     const coral::AttributeList& row = cursor1.currentRow();
00111     tmp_tbl.first=row["DCCBOARDID"].data<long long>();
00112     tmp_tbl.second=row["FEDNUMBER"].data<long long>();
00113     theDAQ.push_back(tmp_tbl);
00114   }
00115   delete query1;
00116 
00117   for(unsigned int iFED=0;iFED<theDAQ.size();iFED++) {
00118     thisDcc.theId=theDAQ[iFED].second;
00119     std::vector<std::pair<int,int> > theTB;
00120 // get TBs
00121     RPCEMap::tbItem thisTB;
00122     coral::IQuery* query2 = schema.newQuery();
00123     query2->addToTableList( "TRIGGERBOARD" );
00124     query2->addToOutputList("TRIGGERBOARD.TRIGGERBOARDID","TRIGGERBOARDID");
00125     query2->addToOutputList("TRIGGERBOARD.DCCINPUTCHANNELNUM","DCCCHANNELNUM");
00126     query2->addToOrderList("DCCCHANNELNUM");
00127     condition = "TRIGGERBOARD.DCCBOARD_DCCBOARDID="+IntToString(theDAQ[iFED].first);
00128     query2->setCondition( condition, conditionData );
00129     coral::ICursor& cursor2 = query2->execute();
00130     int ntbs=0;
00131     while ( cursor2.next() ) {
00132       ntbs++;
00133 //      cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
00134       const coral::AttributeList& row = cursor2.currentRow();
00135       tmp_tbl.first=row["TRIGGERBOARDID"].data<long long>();
00136       tmp_tbl.second=row["DCCCHANNELNUM"].data<long long>();
00137       theTB.push_back(tmp_tbl);
00138     }
00139     delete query2;
00140     for(unsigned int iTB=0;iTB<theTB.size();iTB++) {
00141       thisTB.theNum=theTB[iTB].second;
00142       std::vector<std::pair<int,int> > theLink;
00143 // get links
00144       RPCEMap::linkItem thisLink;
00145       coral::IQuery* query3 = schema.newQuery();
00146       query3->addToTableList("BOARDBOARDCONN");
00147       query3->addToOutputList("BOARDBOARDCONN.BOARD_BOARDID","BOARDID");
00148       query3->addToOutputList("BOARDBOARDCONN.COLLECTORBOARDINPUTNUM","TBINPUTNUM");
00149       query3->addToOrderList("TBINPUTNUM");
00150       condition = "BOARDBOARDCONN.BOARD_COLLECTORBOARDID="+IntToString(theTB[iTB].first);
00151       query3->setCondition( condition, conditionData );
00152       coral::ICursor& cursor3 = query3->execute();
00153       int nlinks=0;
00154       while (cursor3.next()) {
00155         nlinks++;
00156         const coral::AttributeList& row = cursor3.currentRow();
00157         tmp_tbl.first=row["BOARDID"].data<long long>();
00158         tmp_tbl.second=row["TBINPUTNUM"].data<long long>();
00159         theLink.push_back(tmp_tbl);
00160       }
00161       delete query3;
00162       for(unsigned int iLink=0;iLink<theLink.size();iLink++) {
00163         int boardId=theLink[iLink].first;
00164         thisLink.theTriggerBoardInputNumber=theLink[iLink].second;
00165         std::vector<std::pair<int,std::string> > theLB;
00166         std::pair<int,std::string> tmpLB;
00167         // Get master LBs first...
00168         RPCEMap::lbItem thisLB;
00169         coral::IQuery* query4 = schema.newQuery();
00170         query4->addToTableList("BOARD");
00171         query4->addToOutputList("BOARD.NAME","NAME");
00172         condition = "BOARD.BOARDID="+IntToString(theLink[iLink].first);
00173         query4->setCondition( condition, conditionData );
00174         coral::ICursor& cursor4 = query4->execute();
00175         int nlbs=0;
00176         while (cursor4.next()) {
00177           nlbs++;
00178           const coral::AttributeList& row = cursor4.currentRow();
00179           tmpLB.first=theLink[iLink].first;
00180           tmpLB.second=row["NAME"].data<std::string>();
00181           theLB.push_back(tmpLB);
00182         }
00183         delete query4;
00184         // then slaves
00185         coral::IQuery* query5 = schema.newQuery();
00186         query5->addToTableList("LINKBOARD");
00187         query5->addToTableList("BOARD");
00188         query5->addToOutputList("LINKBOARD.LINKBOARDID","LINKBOARDID");
00189         query5->addToOutputList("BOARD.NAME","NAME");
00190         query5->addToOrderList("LINKBOARDID");
00191         condition = "LINKBOARD.MASTERID="+IntToString(theLink[iLink].first)+" AND BOARD.BOARDID=LINKBOARD.LINKBOARDID AND LINKBOARD.MASTERID<>LINKBOARD.LINKBOARDID";
00192         query5->setCondition( condition, conditionData );
00193         coral::ICursor& cursor5 = query5->execute();
00194         while (cursor5.next()) {
00195           nlbs++;
00196           const coral::AttributeList& row = cursor5.currentRow();
00197           tmpLB.first=row["LINKBOARDID"].data<long long>();
00198           tmpLB.second=row["NAME"].data<std::string>();
00199           theLB.push_back(tmpLB);
00200         }
00201         delete query5;
00202         for(unsigned int iLB=0; iLB<theLB.size(); iLB++) {
00203           thisLB.theMaster = (theLB[iLB].first==boardId);
00204 // extract all relevant information from BOARD.NAME
00205           std::string theName = theLB[iLB].second;
00206           int slength = theName.length();
00207           thisLB.theLinkBoardNumInLink=atoi((theName.substr(slength-1,1)).c_str());
00208           int wheel=atoi((theName.substr(6,1)).c_str());
00209           std::string char1=(theName.substr(4,1)).c_str();
00210           std::string char2=(theName.substr(slength-7,1)).c_str();
00211           int num3=atoi((theName.substr(slength-6,1)).c_str());
00212           std::string char4=(theName.substr(slength-5,1)).c_str();
00213           bool itsS1to9=(theName.substr(slength-11,1)=="S");
00214           int n1=10;
00215           int n2=1;
00216           int n3=0;
00217           if (!itsS1to9) {
00218             n1=11;
00219             n2=2;
00220           }
00221           int sector=atoi((theName.substr(slength-n1,n2)).c_str());
00222           std::string char1Val[2]={"B","E"};                              // 1,2
00223           std::string char2Val[3]={"N","M","P"};                          // 0,1,2
00224           std::string char4Val[9]={"0","1","2","3","A","B","C","D","E"};  // 0,...,8
00225           for (int i=0; i<2; i++) if (char1==char1Val[i]) n1=i+1;
00226           for (int i=0; i<3; i++) if (char2==char2Val[i]) n2=i;
00227           for (int i=0; i<9; i++) if (char4==char4Val[i]) n3=i;
00228           thisLB.theCode=n3+num3*10+n2*100+n1*1000+wheel*10000+sector*100000;
00229           FEBStruct tmpFEB;
00230           std::vector<FEBStruct> theFEB;
00231 // get FEBs
00232           RPCEMap::febItem thisFeb;
00233           coral::IQuery* query6 = schema.newQuery();
00234           query6->addToTableList("FEBLOCATION");
00235           query6->addToTableList("FEBCONNECTOR");
00236           query6->addToOutputList("FEBLOCATION.FEBLOCATIONID","FEBLOCATIONID");
00237           query6->addToOutputList("FEBLOCATION.CL_CHAMBERLOCATIONID","CHAMBERLOCATIONID");
00238           query6->addToOutputList("FEBCONNECTOR.FEBCONNECTORID","FEBCONNECTORID");
00239           query6->addToOutputList("FEBLOCATION.FEBLOCALETAPARTITION","LOCALETAPART"); 
00240           query6->addToOutputList("FEBLOCATION.POSINLOCALETAPARTITION","POSINLOCALETAPART");
00241           query6->addToOutputList("FEBLOCATION.FEBCMSETAPARTITION","CMSETAPART");
00242           query6->addToOutputList("FEBLOCATION.POSINCMSETAPARTITION","POSINCMSETAPART");
00243           query6->addToOutputList("FEBCONNECTOR.LINKBOARDINPUTNUM","LINKBOARDINPUTNUM");
00244           query6->addToOrderList("FEBLOCATIONID");
00245           query6->addToOrderList("FEBCONNECTORID");
00246           condition = "FEBLOCATION.LB_LINKBOARDID="+IntToString(theLB[iLB].first)+" AND FEBLOCATION.FEBLOCATIONID=FEBCONNECTOR.FL_FEBLOCATIONID";
00247           query6->setCondition( condition, conditionData );
00248           coral::ICursor& cursor6 = query6->execute();
00249           int nfebs=0;
00250           while (cursor6.next()) {
00251             nfebs++;
00252             const coral::AttributeList& row = cursor6.currentRow();
00253             tmpFEB.febId=row["FEBLOCATIONID"].data<long long>();
00254             tmpFEB.chamberId=row["CHAMBERLOCATIONID"].data<long long>();
00255             tmpFEB.connectorId=row["FEBCONNECTORID"].data<long long>();
00256             tmpFEB.localEtaPart=row["LOCALETAPART"].data<std::string>();
00257             tmpFEB.posInLocalEtaPart=row["POSINLOCALETAPART"].data<short>();
00258             tmpFEB.cmsEtaPart=row["CMSETAPART"].data<std::string>();
00259             tmpFEB.posInCmsEtaPart=row["POSINCMSETAPART"].data<short>();
00260             tmpFEB.lbInputNum=row["LINKBOARDINPUTNUM"].data<short>();
00261             theFEB.push_back(tmpFEB);
00262           }
00263           delete query6;
00264           for(unsigned int iFEB=0; iFEB<theFEB.size(); iFEB++) {
00265             std::string temp=theFEB[iFEB].localEtaPart;
00266             std::string localEtaVal[6]={"Forward","Central","Backward","A","B","C"};
00267             char localEtaPartition=0;
00268             for (int i=0; i<6; i++) if (temp==localEtaVal[i]) localEtaPartition=i+1;
00269             char positionInLocalEtaPartition=theFEB[iFEB].posInLocalEtaPart;
00270             temp=theFEB[iFEB].cmsEtaPart;
00271             std::string cmsEtaVal[6]={"1","2","3","A","B","C"};
00272             char cmsEtaPartition=0;
00273             for (int i=0; i<6; i++) if (temp==cmsEtaVal[i]) cmsEtaPartition=i+1;
00274             char positionInCmsEtaPartition=theFEB[iFEB].posInCmsEtaPart;
00275             thisFeb.thePartition=positionInLocalEtaPartition+10*localEtaPartition+100*positionInCmsEtaPartition+1000*cmsEtaPartition;
00276             thisFeb.theLinkBoardInputNum=theFEB[iFEB].lbInputNum;
00277             // Get chamber 
00278             coral::IQuery* query7 = schema.newQuery();
00279             query7->addToTableList("CHAMBERLOCATION");
00280             query7->addToOutputList("CHAMBERLOCATION.DISKORWHEEL","DISKORWHEEL");
00281             query7->addToOutputList("CHAMBERLOCATION.LAYER","LAYER");
00282             query7->addToOutputList("CHAMBERLOCATION.SECTOR","SECTOR");
00283             query7->addToOutputList("CHAMBERLOCATION.SUBSECTOR","SUBSECTOR");
00284             query7->addToOutputList("CHAMBERLOCATION.CHAMBERLOCATIONNAME","NAME");
00285             query7->addToOutputList("CHAMBERLOCATION.FEBZORNT","FEBZORNT");
00286             query7->addToOutputList("CHAMBERLOCATION.FEBRADORNT","FEBRADORNT");
00287             query7->addToOutputList("CHAMBERLOCATION.BARRELORENDCAP","BARRELORENDCAP");
00288             condition = "CHAMBERLOCATION.CHAMBERLOCATIONID="+IntToString(theFEB[iFEB].chamberId);
00289             query7->setCondition( condition, conditionData );
00290             coral::ICursor& cursor7 = query7->execute();
00291             while (cursor7.next()) {
00292               const coral::AttributeList& row = cursor7.currentRow();
00293               char diskOrWheel=row["DISKORWHEEL"].data<short>()+3;
00294               char layer=row["LAYER"].data<short>();
00295               int sector=row["SECTOR"].data<short>();
00296               temp=row["SUBSECTOR"].data<std::string>();
00297 // TEMPORARY TO CORRECT A LITTLE BUG IN OMDS
00298 //              std::string chname=row["NAME"].data<std::string>();
00299 //              if (layer==6 && (sector==9 || sector==11)) {
00300 //                if (temp=="+") {
00301 //                  temp="";
00302 //                  std::cout<<"Changed subsector + to null for "<<chname<<std::endl;
00303 //                }
00304 //              }
00305 
00306               std::string subsVal[5]={"--","-","","+","++"};
00307               char subsector=0;
00308               for (int i=0; i<5; i++) if (temp==subsVal[i]) subsector=i;
00309               temp=row["FEBZORNT"].data<std::string>();
00310               char febZOrnt=0;
00311               if (temp=="+z") febZOrnt=1;
00312               temp=row["FEBRADORNT"].data<std::string>();
00313               char febZRadOrnt=0;
00314               std::string febZRVal[3]={"","IN","OUT"};
00315               for (int i=0; i<3; i++) if (temp==febZRVal[i]) febZRadOrnt=i;
00316               temp=row["BARRELORENDCAP"].data<std::string>();
00317               char barrelOrEndcap=0;
00318               if (temp=="Barrel") barrelOrEndcap=1;
00319               thisFeb.theChamber=sector+100*subsector+1000*febZRadOrnt+5000*febZOrnt+10000*diskOrWheel+100000*layer+1000000*barrelOrEndcap;
00320             }
00321             delete query7;
00322             // Get Strips
00323             coral::IQuery* query8 = schema.newQuery();
00324             query8->addToTableList("CHAMBERSTRIP");
00325             query8->addToOutputList("CHAMBERSTRIP.CABLECHANNELNUM","CABLECHANNELNUM");
00326             query8->addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER","CHAMBERSTRIPNUM");
00327 //            query8->addToOutputList("CHAMBERSTRIP.CMSSTRIPNUMBER","CMSSTRIPNUM");
00328             query8->addToOrderList("CABLECHANNELNUM");
00329             condition = "CHAMBERSTRIP.FC_FEBCONNECTORID="+IntToString(theFEB[iFEB].connectorId);
00330             query8->setCondition( condition, conditionData );
00331             coral::ICursor& cursor8 = query8->execute();
00332 // NEW: do not store all the strip information as goes, only the minimum data needed to
00333 // reproduce it later on
00334             int nstrips=0;
00335             int firstChamberStrip=0;
00336             int firstPin=0;
00337             int lastChamberStrip=0;
00338             int lastPin=0;
00339             while (cursor8.next()) {
00340               const coral::AttributeList& row = cursor8.currentRow();
00341               lastChamberStrip=row["CHAMBERSTRIPNUM"].data<int>();
00342               lastPin=row["CABLECHANNELNUM"].data<short>();
00343               if (nstrips==0) {
00344                 firstChamberStrip=lastChamberStrip;
00345                 firstPin=lastPin;
00346               }
00347               nstrips++;
00348             }
00349             delete query8;
00350             int algo = -1;
00351             if (firstPin == 1 && lastPin == nstrips) 
00352               { algo = 1; }
00353             else if (firstPin == 2 && lastPin == nstrips+1)
00354               { algo = 2; }
00355             else if (firstPin == 3 && lastPin == nstrips+2)
00356               { algo = 3; }
00357             else if (firstPin == 2 && lastPin == nstrips+2)
00358               { algo = 0;}
00359             else
00360               { std::cout<<" Unknown algo : "<<firstPin<<" "<<lastPin<<std::endl; }
00361             if ((lastPin-firstPin)*(lastChamberStrip-firstChamberStrip) < 0) algo=algo+4;
00362             thisFeb.theAlgo=algo+100*firstChamberStrip+10000*nstrips;
00363             eMap->theFebs.push_back(thisFeb);
00364           }
00365           thisLB.nFebs=nfebs;
00366           eMap->theLBs.push_back(thisLB);
00367         }
00368         thisLink.nLBs=nlbs;
00369         eMap->theLinks.push_back(thisLink);
00370       }
00371       thisTB.nLinks=nlinks;
00372       eMap->theTBs.push_back(thisTB);
00373     }
00374     thisDcc.nTBs=ntbs;
00375     std::cout<<"DCC added"<<std::endl;
00376     eMap->theDccs.push_back(thisDcc);
00377   }
00378   std::cout << std::endl <<"Building RPC e-Map done!" << std::flush << std::endl << std::endl;
00379 }
00380 
00381 int popcon::RPCEMapSourceHandler::Compare2EMaps(Ref map1, RPCEMap* map2) {
00382   RPCReadOutMapping* oldmap1 = map1->convert();
00383   RPCReadOutMapping* oldmap2 = map2->convert();
00384   std::vector<const DccSpec *> dccs1 = oldmap1->dccList();
00385   std::vector<const DccSpec *> dccs2 = oldmap2->dccList();
00386   if(dccs1.size()!=dccs2.size()) {
00387     return 1;
00388   }
00389   std::pair<int,int> dccRange1 = oldmap1->dccNumberRange();
00390   std::pair<int,int> dccRange2 = oldmap2->dccNumberRange();
00391   if(dccRange1.first!=dccRange2.first) {
00392     return 1;
00393   }
00394   if(dccRange1.second!=dccRange2.second) {
00395     return 1;
00396   }
00397   typedef std::vector<const DccSpec *>::const_iterator IDCC;
00398   IDCC idcc2 = dccs2.begin();
00399   for (IDCC idcc1 = dccs1.begin(); idcc1 != dccs1.end(); idcc1++) {
00400     int dccNo = (**idcc1).id();
00401     std::string dccContents = (**idcc1).print(4);
00402     if ((**idcc2).id()!=dccNo) {
00403       return 1;
00404     }
00405     if ((**idcc2).print(4)!=dccContents) {
00406       return 1;
00407     }
00408     idcc2++;
00409   }
00410   return 0;
00411 }