00001 #include "CalibTracker/SiPixelConnectivity/interface/PixelToLNKAssociateFromAscii.h"
00002
00003 #include "DataFormats/SiPixelDetId/interface/PixelBarrelName.h"
00004 #include "DataFormats/SiPixelDetId/interface/PixelEndcapName.h"
00005 #include "DataFormats/SiPixelDetId/interface/PixelPannelType.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007
00008 #include <ostream>
00009 #include <fstream>
00010 #include "FWCore/Utilities/interface/Exception.h"
00011
00012 using namespace std;
00013
00014 PixelToLNKAssociateFromAscii::PixelToLNKAssociateFromAscii(const string & fn) {
00015 init(fn);
00016 }
00017 std::string PixelToLNKAssociateFromAscii::version() const
00018 {
00019 return theVersion;
00020 }
00021
00022 const PixelToLNKAssociateFromAscii::CablingRocId * PixelToLNKAssociateFromAscii::operator()(
00023 const PixelToLNKAssociateFromAscii::DetectorRocId& roc) const
00024 {
00025
00026
00027
00028 typedef std::vector< std::pair<DetectorRocId,CablingRocId> >::const_iterator IM;
00029 for (IM im = theConnection.begin(); im != theConnection.end(); im++) {
00030 if( ( *(im->first.module) == *roc.module ) && (im->first.rocDetId == roc.rocDetId)) {
00031 return &(im->second);
00032 }
00033 }
00034 return 0;
00035 }
00036
00037 void PixelToLNKAssociateFromAscii::init(const string & cfg_name)
00038 {
00039 LogDebug("init, input file:") << cfg_name.c_str();
00040
00041 std::ifstream file( cfg_name.c_str() );
00042 if ( !file ) {
00043 edm::LogError(" ** PixelToLNKAssociateFromAscii,init ** ")
00044 << " cant open data file: " << cfg_name;
00045 return;
00046 } else {
00047 edm::LogInfo("PixelToLNKAssociateFromAscii, read data from: ") <<cfg_name ;
00048 }
00049
00050 string line;
00051 int fedId=-1;
00052 int linkId=-1;
00053
00054 try {
00055 while (getline(file,line)) {
00056
00057
00058
00059 string::size_type pos = line.find("#");
00060 if (pos != string::npos) line = line.erase(pos);
00061
00062 string::size_type posF = line.find("FED:");
00063 string::size_type posL = line.find("LNK:");
00064 string::size_type posM = line.find("MOD:");
00065 string::size_type posR = line.find("ROC:");
00066
00067 LogTrace("") <<" read line: "<< line;
00068
00069
00070
00071
00072
00073 if ( line.compare(0,3,"VER") == 0 ) {
00074 edm::LogInfo("version: ")<<line;
00075 theVersion = line;
00076 }
00077
00078
00079
00080
00081 else if ( posF != string::npos) {
00082 line = line.substr(posF+4);
00083 fedId = atoi(line.c_str());
00084 }
00085
00086
00087
00088
00089 else if ( posL != string::npos) {
00090 string srtL = line.substr(posL+4);
00091 linkId = atoi(srtL.c_str());
00092 }
00093
00094
00095
00096
00097 if ( posM != string::npos) {
00098 if (posR != string::npos) {
00099 string strM = line.substr(posM+4, posR-posM-5);
00100 string::size_type pos = strM.find(" ");
00101 if(pos != string::npos) strM = strM.substr(pos+1);
00102 string strR = line.substr(posR+4);
00103 Range range = readRange(strR);
00104 addConnections( fedId, linkId, strM, range);
00105 } else {
00106 string strM= line.substr(posM+4);
00107 string::size_type pos = strM.find(" ");
00108 if(pos != string::npos) strM = strM.substr(pos+1);
00109 addConnections( fedId, linkId, strM, Range(0,0));
00110 }
00111 }
00112 }
00113 }
00114 catch(exception& err) {
00115 edm::LogError("**PixelToLNKAssociateFromAscii** exception")<<err.what();
00116 }
00117
00118
00119
00120
00121 std::ostringstream str;
00122 str <<" **PixelToLNKAssociateFromAscii ** CONNECTIONS: "<< endl;
00123 typedef vector< pair<DetectorRocId,CablingRocId> >::const_iterator ICON;
00124 for (ICON ic = theConnection.begin(); ic != theConnection.end(); ic++) {
00125 str<< (*ic).first.module->name()
00126 <<", rocDetId="<<(*ic).first.rocDetId
00127 <<", fedId="<<ic->second.fedId
00128 <<", linkId="<<ic->second.linkId
00129 <<", rocLinkId="<<ic->second.rocLinkId
00130 <<endl;
00131 }
00132 edm::LogInfo("PixelToLNKAssociateFromAscii")<<str.str();
00133 }
00134
00135 void PixelToLNKAssociateFromAscii::addConnections(
00136 int fedId, int linkId, std::string module, Range rocDetIds)
00137 {
00138 string::size_type pos;
00139
00140
00141 pos = module.find("BPix");
00142 if (pos != string::npos) {
00143
00144
00145 string strP = module.substr(pos+6,2);
00146 PixelBarrelName::Shell part;
00147 if (strP=="mO") part = PixelBarrelName::mO;
00148 else if(strP=="mI") part = PixelBarrelName::mI;
00149 else if(strP=="pO") part = PixelBarrelName::pO;
00150 else part = PixelBarrelName::pI;
00151 module = module.substr(pos+9);
00152
00153
00154 pos = module.find("_");
00155 if (pos == string::npos) throw cms::Exception("problem with sector formatting");
00156
00157 module = module.substr(pos+1);
00158
00159
00160 pos = module.find("_");
00161 if (pos == string::npos) throw cms::Exception("problem with layer formatting");
00162 int layer = atoi( module.substr(3,pos-3).c_str());
00163 module = module.substr(pos+1);
00164
00165
00166 pos = module.find("_");
00167 if (pos == string::npos) throw cms::Exception("problem with ladder formatting");
00168 int ladder = atoi( module.substr(3,pos-3).c_str());
00169 module = module.substr(pos+1);
00170
00171
00172 int zmodule = atoi( module.substr(3,pos-3).c_str());
00173
00174
00175 int rocLnkId = 0;
00176 for (int rocDetId=rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
00177 rocLnkId++;
00178 DetectorRocId detectorRocId;
00179 PixelBarrelName * name = new PixelBarrelName(part, layer, zmodule, ladder);
00180 detectorRocId.module = name;
00181 detectorRocId.rocDetId = rocDetId;
00182 CablingRocId cablingRocId;
00183 cablingRocId.fedId = fedId;
00184 cablingRocId.linkId = linkId;
00185 cablingRocId.rocLinkId = rocLnkId;
00186
00187 if (name->isHalfModule() && (rocDetIds.min()>7)
00188 && (part==PixelBarrelName::mO || PixelBarrelName::mI) ) {
00189
00190
00191 cablingRocId.rocLinkId = rocLnkId;
00192 detectorRocId.rocDetId = rocDetId-8;
00193 }
00194 theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00195 }
00196 }
00197
00198
00199
00200 pos = module.find("FPix");
00201 if (pos != string::npos) {
00202 string strH = module.substr(pos+6,2);
00203 PixelEndcapName::HalfCylinder part;
00204 if (strH=="mO") part = PixelEndcapName::mO;
00205 else if(strH=="mI") part = PixelEndcapName::mI;
00206 else if(strH=="pO") part = PixelEndcapName::pO;
00207 else part = PixelEndcapName::pI;
00208 module = module.substr(pos+9);
00209
00210
00211 pos = module.find("_");
00212 if (pos == string::npos) throw cms::Exception("problem with disk formatting");
00213 int disk = atoi( module.substr(1,pos-1).c_str());
00214 module = module.substr(pos+1);
00215
00216
00217 pos = module.find("_");
00218 if (pos == string::npos) throw cms::Exception("problem with blade formatting");
00219 int blade = atoi( module.substr(3,pos-3).c_str());
00220 module = module.substr(pos+1);
00221
00222
00223 pos = module.find("_");
00224 if (pos == string::npos) throw cms::Exception("problem with pannel formatting");
00225 int pannel = atoi( module.substr(3,pos-3).c_str());
00226 module = module.substr(pos+1);
00227
00228
00229
00230
00231
00232
00233
00234 pos = module.find("TYP:");
00235 if (pos == string::npos) throw cms::Exception("problem with pannel type formatting");
00236 string strT = module.substr(pos+5,3);
00237
00238 PixelPannelType::PannelType pannelType;
00239 if (strT=="P3R") pannelType=PixelPannelType::p3R;
00240 else if (strT=="P3L") pannelType=PixelPannelType::p3L;
00241 else if (strT=="P4R") pannelType=PixelPannelType::p4R;
00242 else if (strT=="P4L") pannelType=PixelPannelType::p4L;
00243 else throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
00244
00245 if ( pannelType==PixelPannelType::p4L) {
00246
00247 int rocLnkId =0;
00248 for (int plaq = 1; plaq <= 4; plaq++) {
00249 Range rocs; int firstRoc=0; int step=0;
00250 if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
00251 if (plaq==2) { rocs = Range(0,5); firstRoc=0; step=+1; }
00252 if (plaq==3) { rocs = Range(0,7); firstRoc=0; step=+1; }
00253 if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
00254 for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00255 rocLnkId++;
00256 int rocDetId = firstRoc + step*iroc;
00257
00258 DetectorRocId detectorRocId;
00259 detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00260 detectorRocId.rocDetId = rocDetId;
00261
00262 CablingRocId cablingRocId;
00263 cablingRocId.fedId = fedId;
00264 cablingRocId.linkId = linkId;
00265 cablingRocId.rocLinkId = rocLnkId;
00266
00267 theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00268
00269 }
00270 }
00271 }
00272 else if ( pannelType==PixelPannelType::p4R) {
00273
00274 int rocLnkId =0;
00275 for (int plaq = 4; plaq >= 1; plaq--) {
00276 Range rocs; int firstRoc=0; int step=0;
00277 if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
00278 if (plaq==2) { rocs = Range(0,5); firstRoc=3; step=+1; }
00279 if (plaq==3) { rocs = Range(0,7); firstRoc=4; step=+1; }
00280 if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
00281 for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00282 rocLnkId++;
00283 int rocDetId = firstRoc + step*iroc;
00284 if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
00285
00286 DetectorRocId detectorRocId;
00287 detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00288 detectorRocId.rocDetId = rocDetId;
00289
00290 CablingRocId cablingRocId;
00291 cablingRocId.fedId = fedId;
00292 cablingRocId.linkId = linkId;
00293 cablingRocId.rocLinkId = rocLnkId;
00294
00295 theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00296
00297 }
00298 }
00299 }
00300 else if ( pannelType==PixelPannelType::p3L) {
00301
00302 int rocLnkId =0;
00303 for (int plaq = 1; plaq <= 3; plaq++) {
00304 Range rocs; int firstRoc=0; int step=0;
00305 if (plaq==1) { rocs = Range(0,5); firstRoc=0; step=1; }
00306 if (plaq==2) { rocs = Range(0,7); firstRoc=0; step=1; }
00307 if (plaq==3) { rocs = Range(0,9); firstRoc=0; step=1; }
00308 for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00309 rocLnkId++;
00310 int rocDetId = firstRoc + step*iroc;
00311
00312 DetectorRocId detectorRocId;
00313 detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00314 detectorRocId.rocDetId = rocDetId;
00315
00316 CablingRocId cablingRocId;
00317 cablingRocId.fedId = fedId;
00318 cablingRocId.linkId = linkId;
00319 cablingRocId.rocLinkId = rocLnkId;
00320
00321 theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00322
00323 }
00324 }
00325 }
00326 else if ( pannelType==PixelPannelType::p3R) {
00327
00328 int rocLnkId =0;
00329 for (int plaq = 3; plaq >= 1; plaq--) {
00330 Range rocs; int firstRoc=0; int step=0;
00331 if (plaq==1) { rocs = Range(0,5); firstRoc=3; step=1; }
00332 if (plaq==2) { rocs = Range(0,7); firstRoc=4; step=1; }
00333 if (plaq==3) { rocs = Range(0,9); firstRoc=5; step=1; }
00334 for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00335 rocLnkId++;
00336 int rocDetId = firstRoc + step*iroc;
00337 if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
00338
00339 DetectorRocId detectorRocId;
00340 detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00341 detectorRocId.rocDetId = rocDetId;
00342
00343 CablingRocId cablingRocId;
00344 cablingRocId.fedId = fedId;
00345 cablingRocId.linkId = linkId;
00346 cablingRocId.rocLinkId = rocLnkId;
00347
00348 theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00349
00350 }
00351 }
00352 }
00353
00354 }
00355 }
00356
00357 PixelToLNKAssociateFromAscii::Range
00358 PixelToLNKAssociateFromAscii::readRange( const string & l) const
00359 {
00360 bool first = true;
00361 int num1 = -1;
00362 int num2 = -1;
00363 const char * line = l.c_str();
00364 while (line) {
00365 char * evp = 0;
00366 int num = strtol(line, &evp, 10);
00367 { stringstream s; s<<"raad from line: "; s<<num; LogTrace("") << s.str(); }
00368 if (evp != line) {
00369 line = evp +1;
00370 if (first) { num1 = num; first = false; }
00371 num2 = num;
00372 } else line = 0;
00373 }
00374 if (first) {
00375 string s = "** PixelToLNKAssociateFromAscii, read data, cant intrpret: " ;
00376 edm::LogInfo(s) << endl
00377 << l << endl
00378 <<"=====> send exception " << endl;
00379 s += l;
00380 throw cms::Exception(s);
00381 }
00382 return Range(num1,num2);
00383 }
00384