Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:28

0001 #include "CalibTracker/SiPixelConnectivity/interface/PixelToLNKAssociateFromAscii.h"
0002 
0003 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0004 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0005 #include "DataFormats/TrackerCommon/interface/PixelPannelType.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 
0008 #include <ostream>
0009 #include <fstream>
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 
0012 using namespace std;
0013 
0014 PixelToLNKAssociateFromAscii::PixelToLNKAssociateFromAscii(const string& fn, const bool phase) {
0015   phase1_ = phase;
0016   init(fn);
0017 }
0018 std::string PixelToLNKAssociateFromAscii::version() const { return theVersion; }
0019 
0020 const PixelToLNKAssociateFromAscii::CablingRocId* PixelToLNKAssociateFromAscii::operator()(
0021     const PixelToLNKAssociateFromAscii::DetectorRocId& roc) const {
0022   typedef std::vector<std::pair<DetectorRocId, CablingRocId> >::const_iterator IM;
0023   for (IM im = theConnection.begin(); im != theConnection.end(); im++) {
0024     if ((*(im->first.module) == *roc.module) && (im->first.rocDetId == roc.rocDetId)) {
0025       return &(im->second);
0026     }
0027   }
0028   return nullptr;
0029 }
0030 
0031 // This is where the reading and interpretation of the ascci cabling input file is
0032 void PixelToLNKAssociateFromAscii::init(const string& cfg_name) {
0033   edm::LogInfo(" init, input file: ") << cfg_name;
0034 
0035   std::ifstream file(cfg_name.c_str());
0036   if (!file) {
0037     edm::LogError(" ** PixelToLNKAssociateFromAscii,init ** ") << " cant open data file: " << cfg_name;
0038     return;
0039   } else {
0040     edm::LogInfo("PixelToLNKAssociateFromAscii, read data from: ") << cfg_name;
0041   }
0042 
0043   string line;
0044   int fedId = -1;
0045   int linkId = -1;
0046 
0047   try {
0048     while (getline(file, line)) {
0049       //
0050       // treat # lines
0051       //
0052       string::size_type pos = line.find('#');
0053       if (pos != string::npos)
0054         line = line.erase(pos);
0055 
0056       string::size_type posF = line.find("FED:");
0057       string::size_type posL = line.find("LNK:");
0058       string::size_type posM = line.find("MOD:");
0059       string::size_type posR = line.find("ROC:");
0060 
0061       edm::LogInfo("") << " read line: " << line;
0062 
0063       //
0064       // treat version lines, reset date
0065       //
0066       if (line.compare(0, 3, "VER") == 0) {
0067         edm::LogInfo("version: ") << line;
0068         theVersion = line;
0069       }
0070 
0071       //
0072       // fed id line
0073       //
0074       else if (posF != string::npos) {
0075         line = line.substr(posF + 4);
0076         fedId = atoi(line.c_str());
0077       }
0078 
0079       //
0080       // link id linke
0081       //
0082       else if (posL != string::npos) {
0083         string srtL = line.substr(posL + 4);
0084         linkId = atoi(srtL.c_str());
0085       }
0086 
0087       //
0088       // module description
0089       //
0090       if (posM != string::npos) {
0091         if (posR != string::npos) {
0092           string strM = line.substr(posM + 4, posR - posM - 5);
0093           string::size_type pos = strM.find(' ');
0094           if (pos != string::npos)
0095             strM = strM.substr(pos + 1);
0096           string strR = line.substr(posR + 4);
0097           Range range = readRange(strR);
0098           //cout<<" range find "<<strR<<" "<<strR.size()<<" "<<range.min()<<" "<<range.max()<<endl;
0099           addConnections(fedId, linkId, strM, range);
0100         } else {
0101           string strM = line.substr(posM + 4);
0102           string::size_type pos = strM.find(' ');
0103           if (pos != string::npos)
0104             strM = strM.substr(pos + 1);
0105           addConnections(fedId, linkId, strM, Range(0, 0));
0106         }
0107       }
0108     }
0109   } catch (exception& err) {
0110     edm::LogError("**PixelToLNKAssociateFromAscii**  exception") << err.what();
0111   }
0112 
0113   //
0114   // for debug
0115   //
0116   std::ostringstream str;
0117   str << " **PixelToLNKAssociateFromAscii ** CONNECTIONS: " << endl;
0118   typedef vector<pair<DetectorRocId, CablingRocId> >::const_iterator ICON;
0119   for (ICON ic = theConnection.begin(); ic != theConnection.end(); ic++) {
0120     str << (*ic).first.module->name() << ", rocDetId=" << (*ic).first.rocDetId << ", fedId=" << ic->second.fedId
0121         << ", linkId=" << ic->second.linkId << ", rocLinkId=" << ic->second.rocLinkId << endl;
0122   }
0123   edm::LogInfo("PixelToLNKAssociateFromAscii") << str.str();
0124 }
0125 
0126 void PixelToLNKAssociateFromAscii::addConnections(int fedId, int linkId, std::string module, Range rocDetIds) {
0127   string::size_type pos;
0128 
0129   // check for Barrel modules
0130   pos = module.find("BPix");
0131   if (pos != string::npos) {
0132     string module0 = module;
0133     // strip the trailing spaces
0134     string::size_type p = module0.find(' ');
0135     //string::size_type p1 = module0.find_first_of(" ");
0136     //string::size_type p2 = module0.find_last_not_of(" ");
0137     //cout<<p<<" "<<p1<<" "<<p2<<endl;
0138     if (p != string::npos)
0139       module0 = module0.substr(0, p);
0140     PixelBarrelName* name = new PixelBarrelName(module0, phase1_);
0141 
0142     // shell
0143     string strP = module.substr(pos + 6, 2);
0144     PixelBarrelName::Shell part;
0145     if (strP == "mO")
0146       part = PixelBarrelName::mO;
0147     else if (strP == "mI")
0148       part = PixelBarrelName::mI;
0149     else if (strP == "pO")
0150       part = PixelBarrelName::pO;
0151     else
0152       part = PixelBarrelName::pI;
0153 
0154     // // all this can be skipped -----------------------------------
0155     // module = module.substr(pos+9);
0156     // // sector
0157     // pos = module.find("_");
0158     // if (pos ==  string::npos) throw cms::Exception("problem with sector formatting");
0159     // // int sector = atoi( module.substr(3,pos-3).c_str());
0160     // module = module.substr(pos+1);
0161     // // layer
0162     // pos = module.find("_");
0163     // if (pos ==  string::npos) throw cms::Exception("problem with layer formatting");
0164     // int layer = atoi( module.substr(3,pos-3).c_str());
0165     // module = module.substr(pos+1);
0166     // // ladder
0167     // pos = module.find("_");
0168     // if (pos ==  string::npos) throw cms::Exception("problem with ladder formatting");
0169     // int ladder = atoi( module.substr(3,pos-3).c_str());
0170     // module = module.substr(pos+1);
0171     // // z-module
0172     // int zmodule = atoi( module.substr(3,pos-3).c_str());
0173     // // place modules in connections
0174     // PixelBarrelName * name0 = new PixelBarrelName(part, layer, zmodule, ladder, phase1_);
0175     // if(name->name() != module0) cout<<" wrong name "<<fedId<<" "<<linkId<<" "
0176     //                   <<module0<<" "<<name->name()<<" "<<name0->name()<<endl;
0177     // if(name->name() != name0->name()) cout<<" wrong name "<<fedId<<" "<<linkId<<" "
0178     //                   <<module0<<" "<<name->name()<<" "<<name0->name()<<endl;
0179     // //edm::LogInfo(" module ")<<fedId<<" "<<linkId<<" "<<module0<<" "
0180     // //                <<name0->name()<<" "<<rocDetIds.max()<<endl;
0181     // // until here
0182 
0183     int rocLnkId = 0;
0184     bool loopExecuted = false;
0185     for (int rocDetId = rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
0186       loopExecuted = true;
0187       rocLnkId++;
0188       DetectorRocId detectorRocId;
0189       //detectorRocId.module = name0;
0190       detectorRocId.module = name;
0191       detectorRocId.rocDetId = rocDetId;
0192 
0193       CablingRocId cablingRocId;
0194       cablingRocId.fedId = fedId;
0195       cablingRocId.linkId = linkId;
0196       cablingRocId.rocLinkId = rocLnkId;
0197       // fix for type-B modules in barrel
0198       edm::LogInfo(" roc ") << rocDetId << " " << rocLnkId << " " << name->isHalfModule() << endl;
0199       if (name->isHalfModule() && (rocDetIds.min() > 7) &&
0200           (part == PixelBarrelName::mO || part == PixelBarrelName::mI)) {
0201         //cablingRocId.rocLinkId = 9-rocLnkId;
0202         // rocDetId=8,...,15
0203         edm::LogInfo(" special for half modules ");
0204         cablingRocId.rocLinkId = rocLnkId;      // 1...8    19/11/08 d.k.
0205         detectorRocId.rocDetId = rocDetId - 8;  // 0...7
0206       }
0207       theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0208     }
0209     if (!loopExecuted)
0210       delete name;
0211   }
0212 
0213   // check for endcap modules
0214   pos = module.find("FPix");
0215   if (pos != string::npos) {
0216     string strH = module.substr(pos + 6, 2);
0217     PixelEndcapName::HalfCylinder part;
0218     if (strH == "mO")
0219       part = PixelEndcapName::mO;
0220     else if (strH == "mI")
0221       part = PixelEndcapName::mI;
0222     else if (strH == "pO")
0223       part = PixelEndcapName::pO;
0224     else
0225       part = PixelEndcapName::pI;
0226     module = module.substr(pos + 9);
0227 
0228     // disk
0229     pos = module.find('_');
0230     if (pos == string::npos)
0231       throw cms::Exception("problem with disk formatting");
0232     int disk = atoi(module.substr(1, pos - 1).c_str());
0233     module = module.substr(pos + 1);
0234 
0235     // blade
0236     pos = module.find('_');
0237     if (pos == string::npos)
0238       throw cms::Exception("problem with blade formatting");
0239     int blade = atoi(module.substr(3, pos - 3).c_str());
0240     module = module.substr(pos + 1);
0241 
0242     //pannel
0243     pos = module.find('_');
0244     if (pos == string::npos)
0245       throw cms::Exception("problem with pannel formatting");
0246     int pannel = atoi(module.substr(3, pos - 3).c_str());
0247     module = module.substr(pos + 1);
0248 
0249     // plaquete
0250     //     pos = module.find("_");
0251     //     if (pos ==  string::npos) throw cms::Exception("problem with plaquette formatting");
0252     //     int plaq = atoi( module.substr(3,pos-3).c_str());
0253 
0254     int ring = 1;  // preset to 1 so it is ok for pilot blades
0255     // pannel type
0256     PixelPannelType::PannelType pannelType;
0257     if (phase1_) {
0258       pannelType = PixelPannelType::p2x8;  // only 1 type for phase1
0259 
0260       // this is not really needed, just for testing
0261       // ring
0262       pos = module.find("RNG");
0263       if (pos == string::npos)
0264         throw cms::Exception("problem with ring  formatting");
0265       ring = atoi(module.substr(pos + 3, 1).c_str());  //
0266       //cout<<" ring "<<ring<<" "<<module<<endl;
0267 
0268     } else {  // phase0
0269       pos = module.find("TYP:");
0270       if (pos == string::npos)
0271         throw cms::Exception("problem with pannel type formatting");
0272       string strT = module.substr(pos + 5, 3);
0273       string strT4 = module.substr(pos + 5, 4);
0274       ring = 1;
0275       if (strT == "P3R")
0276         pannelType = PixelPannelType::p3R;
0277       else if (strT == "P3L")
0278         pannelType = PixelPannelType::p3L;
0279       else if (strT == "P4R")
0280         pannelType = PixelPannelType::p4R;
0281       else if (strT == "P4L")
0282         pannelType = PixelPannelType::p4L;
0283       else if (strT4 == "P2X8")
0284         pannelType = PixelPannelType::p2x8;  // for pilot blades
0285       else
0286         throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
0287     }
0288 
0289     // Cabling accoring to the panle type
0290     if (pannelType == PixelPannelType::p4L) {
0291       //     cout <<"----------- p4L"<<endl;
0292       int rocLnkId = 0;
0293       for (int plaq = 1; plaq <= 4; plaq++) {
0294         Range rocs;
0295         int firstRoc = 0;
0296         int step = 0;
0297         if (plaq == 1) {
0298           rocs = Range(0, 1);
0299           firstRoc = 1;
0300           step = -1;
0301         }
0302         if (plaq == 2) {
0303           rocs = Range(0, 5);
0304           firstRoc = 0;
0305           step = +1;
0306         }
0307         if (plaq == 3) {
0308           rocs = Range(0, 7);
0309           firstRoc = 0;
0310           step = +1;
0311         }
0312         if (plaq == 4) {
0313           rocs = Range(0, 4);
0314           firstRoc = 0;
0315           step = +1;
0316         }
0317         PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0318         for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
0319           rocLnkId++;
0320           int rocDetId = firstRoc + step * iroc;
0321 
0322           DetectorRocId detectorRocId;
0323           //detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
0324           detectorRocId.module = name;
0325           detectorRocId.rocDetId = rocDetId;
0326 
0327           CablingRocId cablingRocId;
0328           cablingRocId.fedId = fedId;
0329           cablingRocId.linkId = linkId;
0330           cablingRocId.rocLinkId = rocLnkId;
0331 
0332           theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0333           //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
0334         }
0335       }
0336     } else if (pannelType == PixelPannelType::p4R) {
0337       //     cout <<"----------- p4R"<<endl;
0338       int rocLnkId = 0;
0339       for (int plaq = 4; plaq >= 1; plaq--) {
0340         Range rocs;
0341         int firstRoc = 0;
0342         int step = 0;
0343         if (plaq == 1) {
0344           rocs = Range(0, 1);
0345           firstRoc = 1;
0346           step = -1;
0347         }
0348         if (plaq == 2) {
0349           rocs = Range(0, 5);
0350           firstRoc = 3;
0351           step = +1;
0352         }
0353         if (plaq == 3) {
0354           rocs = Range(0, 7);
0355           firstRoc = 4;
0356           step = +1;
0357         }
0358         if (plaq == 4) {
0359           rocs = Range(0, 4);
0360           firstRoc = 0;
0361           step = +1;
0362         }
0363         PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0364         for (int iroc = rocs.min(); iroc - rocs.max() <= 0; iroc++) {
0365           rocLnkId++;
0366           int rocDetId = firstRoc + step * iroc;
0367           if (rocDetId > rocs.max())
0368             rocDetId = (rocDetId - 1) % rocs.max();
0369 
0370           DetectorRocId detectorRocId;
0371           //detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
0372           detectorRocId.module = name;
0373           detectorRocId.rocDetId = rocDetId;
0374           CablingRocId cablingRocId;
0375           cablingRocId.fedId = fedId;
0376           cablingRocId.linkId = linkId;
0377           cablingRocId.rocLinkId = rocLnkId;
0378 
0379           theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0380           //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
0381         }
0382       }
0383     } else if (pannelType == PixelPannelType::p3L) {
0384       //     cout <<"----------- p3L"<<endl;
0385       int rocLnkId = 0;
0386       for (int plaq = 1; plaq <= 3; plaq++) {
0387         Range rocs;
0388         int firstRoc = 0;
0389         int step = 0;
0390         if (plaq == 1) {
0391           rocs = Range(0, 5);
0392           firstRoc = 0;
0393           step = 1;
0394         }
0395         if (plaq == 2) {
0396           rocs = Range(0, 7);
0397           firstRoc = 0;
0398           step = 1;
0399         }
0400         if (plaq == 3) {
0401           rocs = Range(0, 9);
0402           firstRoc = 0;
0403           step = 1;
0404         }
0405         PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0406         for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
0407           rocLnkId++;
0408           int rocDetId = firstRoc + step * iroc;
0409 
0410           DetectorRocId detectorRocId;
0411           detectorRocId.module = name;
0412           detectorRocId.rocDetId = rocDetId;
0413 
0414           CablingRocId cablingRocId;
0415           cablingRocId.fedId = fedId;
0416           cablingRocId.linkId = linkId;
0417           cablingRocId.rocLinkId = rocLnkId;
0418 
0419           theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0420           //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
0421         }
0422       }
0423     } else if (pannelType == PixelPannelType::p3R) {
0424       //     cout <<"----------- p3R"<<endl;
0425       int rocLnkId = 0;
0426       for (int plaq = 3; plaq >= 1; plaq--) {
0427         Range rocs;
0428         int firstRoc = 0;
0429         int step = 0;
0430         if (plaq == 1) {
0431           rocs = Range(0, 5);
0432           firstRoc = 3;
0433           step = 1;
0434         }
0435         if (plaq == 2) {
0436           rocs = Range(0, 7);
0437           firstRoc = 4;
0438           step = 1;
0439         }
0440         if (plaq == 3) {
0441           rocs = Range(0, 9);
0442           firstRoc = 5;
0443           step = 1;
0444         }
0445         PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0446         for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
0447           rocLnkId++;
0448           int rocDetId = firstRoc + step * iroc;
0449           if (rocDetId > rocs.max())
0450             rocDetId = (rocDetId - 1) % rocs.max();
0451 
0452           DetectorRocId detectorRocId;
0453           detectorRocId.module = name;
0454           detectorRocId.rocDetId = rocDetId;
0455 
0456           CablingRocId cablingRocId;
0457           cablingRocId.fedId = fedId;
0458           cablingRocId.linkId = linkId;
0459           cablingRocId.rocLinkId = rocLnkId;
0460 
0461           theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0462           //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
0463         }  // for
0464       }    // for
0465 
0466     } else if (pannelType == PixelPannelType::p2x8) {  // phase-1 blades
0467       //       cout <<"----------- p2x8"<<endl;
0468       int rocLnkId = 0;
0469       //       Range rocs = Range(0, 15);
0470       //       for (int rocDetId=rocs.min(); rocDetId <= rocs.max(); rocDetId++) {
0471       PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, ring, phase1_);
0472       bool loopExecuted = false;
0473       for (int rocDetId = rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
0474         loopExecuted = true;
0475         rocLnkId++;
0476         DetectorRocId detectorRocId;
0477         detectorRocId.module = name;
0478         detectorRocId.rocDetId = rocDetId;
0479         CablingRocId cablingRocId;
0480         cablingRocId.fedId = fedId;
0481         cablingRocId.linkId = linkId;
0482         cablingRocId.rocLinkId = rocLnkId;
0483         theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0484         edm::LogInfo("PixelToLNKAssociateFromAscii FPix ")
0485             << " rocDetId: " << rocDetId << " rocLnkId:" << rocLnkId << " fedId = " << fedId << " linkId = " << linkId
0486             << " name = " << name->name();
0487         // cout << " rocDetId: " << rocDetId
0488         //      << " rocLnkId:" << rocLnkId
0489         //      << " fedId = " << fedId
0490         //      << " linkId = " << linkId
0491         //      << " name = " << name0->name()
0492         //      << endl;
0493       }  // end for
0494       if (!loopExecuted) {
0495         delete name;
0496       }
0497 
0498     }  // end of type
0499   }
0500 }
0501 
0502 PixelToLNKAssociateFromAscii::Range PixelToLNKAssociateFromAscii::readRange(const string& l) const {
0503   //cout<<l<<" in range "<<l.size()<<endl;
0504   string l1, l2;
0505   int i1 = -1, i2 = -1;
0506   int len = l.size();
0507   //for(int i=0; i<len;i++) {
0508   // cout<<i<<" "<<l[i]<<endl;
0509   //}
0510   string::size_type p = l.find(',');
0511   if (p != string::npos) {
0512     //cout<<p<<" "<<len<<endl;
0513     l1 = l.substr(0, p - 1 + 1);
0514     l2 = l.substr(p + 1, len - 1 - p);
0515     i1 = stoi(l1);
0516     i2 = stoi(l2);
0517     //cout<<l1<<" "<<l2<<" "<<i1<<" "<<i2<<endl;
0518   }
0519 
0520   return Range(i1, i2);
0521 
0522   // this method is very stupid it relies on a space being present after the last number!
0523   // exchange with string opertaions (above)
0524   //bool first = true;
0525   //int num1 = -1;
0526   //int num2 = -1;
0527   // const char * line = l.c_str();
0528   // int i=0;
0529   // while (line) {
0530   //   i++;
0531   //   char * evp = 0;
0532   //   int num = strtol(line, &evp, 10);
0533   //   //cout<<i<<" "<<num<<" "<<evp<<" "<<line<<endl;
0534   //   //{ stringstream s; s<<"read from line: "; s<<num; LogTrace("") << s.str(); }
0535   //   if (evp != line) {
0536   //     line = evp +1;
0537   //     //cout<<i<<" "<<num<<" "<<evp<<" "<<line<<endl;
0538   //     if (first) { num1 = num; first = false;}
0539   //     num2 = num;
0540   //     //cout<<" not first "<<num2<<endl;
0541   //   } else line = 0;
0542   // }
0543   // if (first) {
0544   //   string s = "** PixelToLNKAssociateFromAscii, read data, cant intrpret: " ;
0545   //   edm::LogInfo(s) << endl
0546   //            << l << endl
0547   //             <<"=====> send exception " << endl;
0548   //   s += l;
0549   //   throw cms::Exception(s);
0550   // }
0551   //if(i1!=num1) cout<<" something wrong with min range "<<i1<<" "<<num1<<endl;
0552   //if(!phase1_ && (i2!=num2)) cout<<" something wrong with max range "<<i2<<" "<<num2<<endl;
0553   //cout<<" min max "<<num1<<" "<<num2<<endl;
0554   //return Range(num1,num2);
0555 }