00001
00002
00003
00004
00005
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <memory>
00023 #include <fstream>
00024 #include <iomanip>
00025 #include <ctime>
00026
00027
00028 #include "FWCore/Framework/interface/Frameworkfwd.h"
00029 #include "FWCore/Framework/interface/EDAnalyzer.h"
00030
00031 #include "FWCore/Framework/interface/ESHandle.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h"
00033 #include "FWCore/Framework/interface/Event.h"
00034 #include "FWCore/Framework/interface/MakerMacros.h"
00035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00036 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00037
00038
00039
00040 #include "L1Trigger/RPCTrigger/interface/RPCVHDLConeMaker.h"
00041 #include "L1Trigger/RPCTrigger/interface/RPCRingFromRolls.h"
00042
00043 #include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"
00044
00045
00046 #include "CondFormats/RPCObjects/interface/RPCEMap.h"
00047 #include "CondFormats/DataRecord/interface/RPCEMapRcd.h"
00048
00049 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00050
00051
00052
00053
00054 RPCVHDLConeMaker::RPCVHDLConeMaker(const edm::ParameterSet& iConfig){
00055
00056 ::putenv("CORAL_AUTH_USER konec");
00057 ::putenv("CORAL_AUTH_PASSWORD konecPass");
00058
00059 RPCLinksDone = false;
00060
00061 minTower = iConfig.getParameter<int>("minTower");
00062 maxTower = iConfig.getParameter<int>("maxTower");
00063
00064 minSector = iConfig.getParameter<int>("minSector");
00065 maxSector = iConfig.getParameter<int>("maxSector");
00066
00067 patternsPath = iConfig.getParameter<std::string>("patternsPath");
00068 conesPath = iConfig.getParameter<std::string>("conesPath");
00069
00070 }
00071
00072
00073 RPCVHDLConeMaker::~RPCVHDLConeMaker(){}
00074
00075
00076
00077
00078
00079
00080
00081 void RPCVHDLConeMaker::initRPCLinks(const edm::EventSetup& iSetup){
00082
00083 using namespace edm;
00084 using namespace std;
00085
00086 if(!RPCLinksDone) RPCLinksDone = true;
00087 else return;
00088
00089
00090
00091
00092
00093 edm::ESHandle<RPCEMap> nmap;
00094 iSetup.get<RPCEMapRcd>().get(nmap);
00095 const RPCEMap* eMap=nmap.product();
00096 edm::ESHandle<RPCReadOutMapping> map = eMap->convert();
00097
00098 LogInfo("") << "version: " << map->version() << endl;
00099
00100
00101 if (!theLinksystem.isGeometryBuilt()){
00102 edm::LogInfo("RPC") << "Building RPC links map for a RPCTrigger";
00103 edm::ESHandle<RPCGeometry> rpcGeom;
00104 iSetup.get<MuonGeometryRecord>().get( rpcGeom );
00105 theLinksystem.buildGeometry(rpcGeom);
00106 edm::LogInfo("RPC") << "RPC links map for a RPCTrigger built";
00107 }
00108 aLinks=theLinksystem.getLinks();
00109 }
00110
00111
00112 void
00113 RPCVHDLConeMaker::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup){
00114
00115 using namespace edm;
00116 using namespace std;
00117
00118
00119
00120 writeLogCones(minTower,maxTower,minSector,maxSector,iSetup);
00121
00122
00123 return;
00124 }
00125
00126 void RPCVHDLConeMaker::writeLogCones(int towMin, int towMax, int secMin, int secMax,
00127 const edm::EventSetup& iSetup){
00128
00129 using namespace edm;
00130 using namespace std;
00131
00132
00133 for(int iTow=towMin;iTow<=towMax;iTow++){
00134 for(int iSec=secMin;iSec<=secMax;iSec++){
00135
00136
00137 string fileName=conesPath + "/pac_t";
00138 char text[100];
00139 sprintf(text,"%d",iTow);
00140 fileName.append(text);
00141 sprintf(text,"_sec%d",iSec);
00142 fileName.append(text);
00143 fileName+=".vhd";
00144 string fileName1=patternsPath;
00145
00146
00147 sprintf(text,"/t%dsc0.vhdl",abs(iTow));
00148 fileName1.append(text);
00149 std::ofstream out(fileName.c_str());
00150 writeHeader(iTow,iSec,out);
00151 writeConesDef(iTow,iSec,out,iSetup);
00152
00153
00154 out.close();
00155
00156 string command = "cat "+ fileName + " " + fileName1;
00157 command+=" > tmp.out";
00158 system(command.c_str());
00159 command = "mv tmp.out ";
00160 command+=fileName;
00161 system(command.c_str());
00162
00163
00164 std::ofstream out1(fileName.c_str(),ios::app);
00165 out1<<std::endl<<"end RPC_PAC_patt;"<<std::endl;
00166
00167 out1.close();
00168
00169 }
00170 }
00171 }
00172
00173 void RPCVHDLConeMaker::writeHeader(int aTower, int aSector, std::ofstream & out){
00174
00175 using namespace edm;
00176 using namespace std;
00177
00178
00179 int maxPAC = 12;
00180
00181
00182 bool begin = true;
00183
00184
00185 time_t rawtime;
00186 struct tm * timeinfo;
00187 time ( &rawtime );
00188 timeinfo = localtime ( &rawtime );
00189 asctime (timeinfo);
00190
00191 out<<"-- version "<<asctime(timeinfo);
00192 out<<"-- file generated by RPCVHDLConeMaker"<<endl;
00193 out<<"library ieee;"<<endl;
00194 out<<"use ieee.std_logic_1164.all;"<<endl;
00195 out<<"use work.std_logic_1164_ktp.all;"<<endl;
00196 out<<"use work.RPC_PAC_def.all;"<<endl;
00197 out<<""<<endl;
00198 out<<"package RPC_PAC_patt is"<<endl;
00199 out<<""<<endl;
00200 out<<"constant TT_EPACS_COUNT :natural := ";
00201
00202 out<<maxPAC;
00203 out<<";"<<endl;
00204 out<<"constant TT_TPACS_COUNT :natural := ";
00205 out<<0;
00206
00207 out<<";"<<endl;
00208 out<<"constant TT_REF_GROUP_NUMBERS :natural := 1;"<<endl;
00209 out<<"constant TT_GBSORT_INPUTS :natural := "
00210 <<maxPAC<<";"<<endl;
00211 out<<""<<endl;
00212 out<<"constant PACLogPlainsDecl :TPACLogPlainsDecl := ("<<endl;
00213 out<<" --PAC_INDEX"<<endl;
00214 out<<" --| PAC_MODEL"<<endl;
00215 out<<" --| | logplane 1 size .........logplane 6 size"<<endl;
00216
00217 for(int iPAC=0;iPAC<maxPAC;iPAC++){
00218 int size[6];
00219 for (int i =0; i <6; ++i){
00220 size[i]=RPCRingFromRolls::m_LOGPLANE_SIZE[abs(aTower)][i];
00221 if (size[i]==0) size[i]=1;
00222 }
00223 if(!begin) out<<",";
00224 else begin = false;
00225 out<<" ("
00226 <<iPAC
00227 <<", E, ( "
00228 <<size[0]<<", "
00229 <<size[1]<<", "
00230 <<size[2]<<", "
00231 <<size[3]<<", "
00232 <<size[4]<<", "
00233 <<size[5]
00234 <<"))";
00235 out<<"-- "<<endl;
00236 }
00237
00238 out<<");"<<endl<<endl;
00239 out<<"--PAC_INDEX PAC_MODEL Tower LogSector LogSegment"<<endl;
00240 for(int iPAC=0;iPAC<maxPAC;iPAC++){
00241 out<<"-- "
00242 <<iPAC
00243 <<" E"<<"\t"
00244 <<aTower<<"\t"
00245 <<aSector<<"\t"
00246 <<iPAC<<"\t"<<endl;
00247 }
00248
00249 out<<""<<endl;
00250 out<<""<<endl;
00251 out<<"constant LogPlainConn :TLogPlainConn := ("<<endl;
00252 out<<" --PAC_INDEX Logplane LinkChannel LinkLeftBit"<<endl;
00253 out<<" --| PAC_MODEL | Link | LogPlaneLeftBit"<<endl;
00254 out<<" --| | | | | | | LinkBitsCount"<<endl;
00255 out<<" --------------------------------------------------------------"<<endl;
00256
00257
00258 }
00259
00260
00261 void RPCVHDLConeMaker::writeConesDef(int iTower, int iSec, std::ofstream & out, const edm::EventSetup& iSetup){
00262
00263
00264 int dccInputChannel = getDCCNumber(iTower,iSec);
00265
00266 using namespace edm;
00267 using namespace std;
00268
00269
00270 int minPAC = iSec*12;
00271 int maxPAC = minPAC+11;
00272
00273
00274
00275
00276
00277
00278
00279
00280 edm::ESHandle<RPCEMap> nmap;
00281 iSetup.get<RPCEMapRcd>().get(nmap);
00282 const RPCEMap* eMap=nmap.product();
00283 edm::ESHandle<RPCReadOutMapping> map = eMap->convert();
00284
00285
00286
00287 if (!theLinksystem.isGeometryBuilt()){
00288 edm::LogInfo("RPC") << "Building RPC links map for a RPCTrigger";
00289 edm::ESHandle<RPCGeometry> rpcGeom;
00290 iSetup.get<MuonGeometryRecord>().get( rpcGeom );
00291 theLinksystem.buildGeometry(rpcGeom);
00292 edm::LogInfo("RPC") << "RPC links map for a RPCTrigger built";
00293 }
00294 RPCRingFromRolls::RPCLinks aLinks=theLinksystem.getLinks();
00295
00296 bool beg = true;
00297
00298 for(int iCone=minPAC;iCone<=maxPAC;iCone++){
00299 for(int iPlane=1;iPlane<7;iPlane++){
00300 RPCRingFromRolls::RPCLinks::const_iterator CI= aLinks.begin();
00301 for(;CI!=aLinks.end();CI++){
00302 RPCRingFromRolls::stripCords aCoords = CI->first;
00303 RPCRingFromRolls::RPCConnectionsVec aConnVec = CI->second;
00304 RPCRingFromRolls::RPCConnectionsVec::const_iterator aConnCI = aConnVec.begin();
00305 RPCDetId aId(aCoords.m_detRawId);
00306 for(;aConnCI!=aConnVec.end();aConnCI++){
00307 if(aConnCI->m_tower==iTower &&
00308 aConnCI->m_PAC==iCone &&
00309 aConnCI->m_logplane==iPlane){
00311 LinkBoardElectronicIndex a;
00312 std::pair< LinkBoardElectronicIndex, LinkBoardPackedStrip> linkStrip =
00313 std::make_pair(a, LinkBoardPackedStrip(0,0));
00314 std::pair<int,int> stripInDetUnit(aCoords.m_detRawId, aCoords.m_stripNo);
00315 std::vector< std::pair< LinkBoardElectronicIndex, LinkBoardPackedStrip> > aVec = map->rawDataFrame( stripInDetUnit);
00316 std::vector< std::pair< LinkBoardElectronicIndex, LinkBoardPackedStrip> >::const_iterator CI;
00317 if( aCoords.m_stripNo <0) continue;
00318 if( aCoords.m_isVirtual) continue;
00319 for(CI=aVec.begin();CI!=aVec.end();CI++){
00320 if(CI->first.dccInputChannelNum==dccInputChannel){
00321 linkStrip = *CI;
00322
00323 }
00324 }
00325 if(linkStrip.second.packedStrip()==-17) {
00326 cout<<" Problem: "<<aCoords.m_detRawId<<" "<<aCoords.m_stripNo<<" "<<RPCDetId(aCoords.m_detRawId)<<endl; continue;}
00327 else{
00328 if(iPlane==33)
00329 LogInfo("")<<"("<<iCone<<",\t E, \t"<<iPlane<<"\t"
00330 <<linkStrip.first.tbLinkInputNum<<"\t"
00331 <<linkStrip.first.lbNumInLink<<"\t"
00332 <<aConnCI->m_posInCone<<"\t "
00333 <<linkStrip.second.packedStrip()<<" \t"
00334 <<1<<") --"<<aId<<endl;
00335 if(!beg)out<<",";
00336 else beg = false;
00337 out<<"("<<iCone-minPAC<<",\t E, \t"<<iPlane-1<<",\t"
00338 <<linkStrip.first.tbLinkInputNum<<",\t"
00339 <<linkStrip.first.lbNumInLink<<",\t"
00340 <<aConnCI->m_posInCone<<",\t "
00341 <<linkStrip.second.packedStrip()<<", \t";
00342 out<<1<<") --"<<aId<<endl;
00343 }
00344 }
00345 }
00346 }
00347 }
00348 }
00349 out<<");"<<endl;
00350 }
00351
00352 void RPCVHDLConeMaker::writeQualityDef(std::ofstream & out){
00353
00354 using namespace std;
00355
00356 out<<endl;
00357
00358 out<<"constant PACCellQuality :TPACCellQuality := ("<<endl;
00359
00360
00361 out<<"(0,\"111111\",3),"<<endl;
00362 out<<"(0,\"111110\",2),"<<endl;
00363 out<<"(0,\"111101\",2),"<<endl;
00364 out<<"(0,\"111011\",2),"<<endl;
00365 out<<"(0,\"110111\",2),"<<endl;
00366 out<<"(0,\"101111\",2),"<<endl;
00367 out<<"(0,\"011111\",2),"<<endl;
00368 out<<"(0,\"111100\",1),"<<endl;
00369 out<<"(0,\"111010\",1),"<<endl;
00370 out<<"(0,\"110110\",1),"<<endl;
00371 out<<"(0,\"101110\",1),"<<endl;
00372 out<<"(0,\"011110\",1),"<<endl;
00373 out<<"(0,\"111001\",1),"<<endl;
00374 out<<"(0,\"110101\",1),"<<endl;
00375 out<<"(0,\"101101\",1),"<<endl;
00376 out<<"(0,\"011101\",1),"<<endl;
00377 out<<"(0,\"110011\",1),"<<endl;
00378 out<<"(0,\"101011\",1),"<<endl;
00379 out<<"(0,\"011011\",1),"<<endl;
00380 out<<"(0,\"100111\",1),"<<endl;
00381 out<<"(0,\"010111\",1),"<<endl;
00382 out<<"(0,\"001111\",1)"<<endl;
00383 out<<");"<<endl<<endl;
00384
00385 }
00386
00387
00388 void RPCVHDLConeMaker::writePatternsDef(std::ofstream & out){
00389
00390 using namespace edm;
00391 using namespace std;
00392
00393
00395 int aTower = 4;
00396 int minPAC = 0;
00397 int maxPAC = 1;
00399
00400 int iTower = aTower;
00401
00402 out<<"constant PACPattTable :TPACPattTable := ("<<endl;
00403 out<<"-- PAC_INDEX"<<endl;
00404 out<<"-- | PAC_MODEL"<<endl;
00405 out<<"-- | | Ref Group Index"<<endl;
00406 out<<"-- | | | Qualit Tab index"<<endl;
00407 out<<"-- | | | | Plane1 Plane2 Plane3 Plane4 Plane5 Plane6 sign code pat number"<<endl;
00408 for(int iPAC=minPAC;iPAC<=maxPAC;iPAC++){
00409 for(int i=0;i<8;i++){
00410 out<<"("<<iPAC<<", T, 0, 0, (";
00411 for(int iLogPlane=0;iLogPlane<6;iLogPlane++){
00412 int strip= (RPCRingFromRolls::m_LOGPLANE_SIZE[iTower][iLogPlane]-8)/2+i;
00413 out<<"( "<<setw(2)<<strip<<", "<<strip<<")";
00414 if(iLogPlane<5) out<<", ";
00415 }
00416 out<<"),";
00417 out<<" 1, "<<i+1<<")";
00418 if(i!=7 || iPAC<maxPAC-minPAC) out<<", ";
00419 out<<"--0"<<endl;
00420 }
00421 }
00422 out<<");"<<endl<<endl;
00423 }
00424
00425
00426 void RPCVHDLConeMaker::writeXMLPatternsDef(std::ofstream & out){
00427
00428 using namespace edm;
00429 using namespace std;
00430
00431
00433 int aTower = 0;
00434 int minPAC = 0;
00435 int maxPAC = 0;
00437
00438 int iTower = aTower;
00439
00440
00441 time_t rawtime;
00442 struct tm * timeinfo;
00443 time ( &rawtime );
00444 timeinfo = localtime ( &rawtime );
00445 asctime (timeinfo);
00446
00447 out<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>"<<endl;
00448 out<<"<?xml-stylesheet type=\"text/xsl\" href=\"default.xsl\"?>"<<endl;
00449 out<<"<pacdef>"<<endl;
00450 out<<"<date>"<<asctime(timeinfo);
00451 out<<"</date>"<<endl;
00452 out<<""<<endl;
00453 out<<"<descr>-- EfficiencyCut 0.9"<<endl;
00454 out<<"-- Simple patterns for the MTCC tests."<<endl;
00455 out<<"</descr>"<<endl;
00456 out<<endl;
00457 out<<"<qualitTable>"<<endl;
00458 out<<"<quality id=\"0\" planes=\"001111\" val=\"1\"/>"<<endl;
00459 out<<"<quality id=\"0\" planes=\"010111\" val=\"1\"/>"<<endl;
00460 out<<"<quality id=\"0\" planes=\"011011\" val=\"1\"/>"<<endl;
00461 out<<"<quality id=\"0\" planes=\"011101\" val=\"1\"/>"<<endl;
00462 out<<"<quality id=\"0\" planes=\"011110\" val=\"1\"/>"<<endl;
00463 out<<"<quality id=\"0\" planes=\"011111\" val=\"2\"/>"<<endl;
00464 out<<"<quality id=\"0\" planes=\"100111\" val=\"1\"/>"<<endl;
00465 out<<"<quality id=\"0\" planes=\"101011\" val=\"1\"/>"<<endl;
00466 out<<"<quality id=\"0\" planes=\"101101\" val=\"1\"/>"<<endl;
00467 out<<"<quality id=\"0\" planes=\"101110\" val=\"1\"/>"<<endl;
00468 out<<"<quality id=\"0\" planes=\"101111\" val=\"2\"/>"<<endl;
00469 out<<"<quality id=\"0\" planes=\"110011\" val=\"1\"/>"<<endl;
00470 out<<"<quality id=\"0\" planes=\"110101\" val=\"1\"/>"<<endl;
00471 out<<"<quality id=\"0\" planes=\"110110\" val=\"1\"/>"<<endl;
00472 out<<"<quality id=\"0\" planes=\"110111\" val=\"2\"/>"<<endl;
00473 out<<"<quality id=\"0\" planes=\"111001\" val=\"1\"/>"<<endl;
00474 out<<"<quality id=\"0\" planes=\"111010\" val=\"1\"/>"<<endl;
00475 out<<"<quality id=\"0\" planes=\"111011\" val=\"2\"/>"<<endl;
00476 out<<"<quality id=\"0\" planes=\"111100\" val=\"1\"/>"<<endl;
00477 out<<"<quality id=\"0\" planes=\"111101\" val=\"2\"/>"<<endl;
00478 out<<"<quality id=\"0\" planes=\"111110\" val=\"2\"/>"<<endl;
00479 out<<"<quality id=\"0\" planes=\"111111\" val=\"3\"/>"<<endl;
00480 out<<"<quality id=\"1\" planes=\"000111\" val=\"0\"/>"<<endl;
00481 out<<"<quality id=\"1\" planes=\"001011\" val=\"0\"/>"<<endl;
00482 out<<"<quality id=\"1\" planes=\"001101\" val=\"0\"/>"<<endl;
00483 out<<"<quality id=\"1\" planes=\"001110\" val=\"0\"/>"<<endl;
00484 out<<"<quality id=\"1\" planes=\"001111\" val=\"1\"/>"<<endl;
00485 out<<"</qualitTable>"<<endl;
00486 out<<endl;
00487 out<<"<pac logSector=\"0\" logSegment=\"0\" tower=\""<<iTower<<"\">"<<endl;
00488
00489 for(int i=0;i<8;i++){
00490 out<<"<pat code=\""<<i+1<<"\" grp=\"0\" num=\"0\" qual=\"0\" sign=\"1\" type=\"T\">"<<endl;
00491 for(int iLogPlane=0;iLogPlane<6;iLogPlane++){
00492 int strip= (RPCRingFromRolls::m_LOGPLANE_SIZE[iTower][iLogPlane]-8)/2+i;
00493 out<<"<str Pl=\""<<iLogPlane
00494 <<"\" f=\""<<strip
00495 <<"\" t=\""<<strip<<"\"/>"<<endl;
00496 }
00497 out<<"</pat>"<<endl;
00498 }
00499 out<<"</pac>"<<endl;
00500 out<<endl;
00501 out<<"</pacdef>"<<endl;
00502
00503 }
00504
00505
00506 void RPCVHDLConeMaker::writeSorterDef(std::ofstream & out){
00507
00508 using namespace edm;
00509 using namespace std;
00510
00511
00513 int aTower = 0;
00514 int minPAC = 0;
00515 int maxPAC = 2;
00516
00518
00519
00520 out<<"\n \n constant GBSortDecl :TGBSortDecl := ("<<endl;
00521 out<<"--PAC_INDEX"<<endl;
00522 out<<"--| PAC_MODEL"<<endl;
00523 out<<"--| | GBSORT_INPUT_INDEX"<<endl;
00524 for(int i=0;i<(maxPAC-minPAC);i++){
00525 out<<" ("<<i<<",\t T,\t"<<i<<")";
00526 if(i<(maxPAC-minPAC)-1) out<<",";
00527 out<<endl;
00528
00529 }
00530 out<<");"<<endl;
00531 out<<""<<endl;
00532 out<<"end RPC_PAC_patt;"<<endl;
00533 }
00534
00535
00536 int RPCVHDLConeMaker::getDCCNumber(int iTower, int iSec){
00537
00538 int tbNumber = 0;
00539 if(iTower<-12) tbNumber = 0;
00540 else if(-13<iTower && iTower<-8) tbNumber = 1;
00541 else if(-9<iTower && iTower<-4) tbNumber = 2;
00542 else if(-5<iTower && iTower<-1) tbNumber = 3;
00543 else if(-2<iTower && iTower<2) tbNumber = 4;
00544 else if(1<iTower && iTower<5) tbNumber = 5;
00545 else if(4<iTower && iTower<9) tbNumber = 6;
00546 else if(8<iTower && iTower<13) tbNumber = 7;
00547 else if(12<iTower) tbNumber = 8;
00548
00549 int phiFactor = iSec%4;
00550 return (tbNumber + phiFactor*9);
00551 }