File indexing completed on 2024-09-07 04:35:04
0001 #include "CalibTracker/SiPixelConnectivity/interface/PixelToFEDAssociateFromAscii.h"
0002
0003 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0004 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006
0007 #include <ostream>
0008 #include <fstream>
0009 #include "FWCore/Utilities/interface/Exception.h"
0010
0011 using namespace std;
0012
0013 PixelToFEDAssociateFromAscii::PixelToFEDAssociateFromAscii(const string& fn) { init(fn); }
0014 std::string PixelToFEDAssociateFromAscii::version() const { return theVersion; }
0015
0016 int PixelToFEDAssociateFromAscii::operator()(const PixelModuleName& id) const {
0017 return id.isBarrel() ? operator()(dynamic_cast<const PixelBarrelName&>(id))
0018 : operator()(dynamic_cast<const PixelEndcapName&>(id));
0019 }
0020
0021 int PixelToFEDAssociateFromAscii::operator()(const PixelBarrelName& id) const {
0022 for (BarrelConnections::const_iterator ibc = theBarrel.begin(); ibc != theBarrel.end(); ibc++) {
0023 for (vector<Bdu>::const_iterator ibd = (*ibc).second.begin(); ibd != (*ibc).second.end(); ibd++) {
0024 if (ibd->b == id.shell() && ibd->l.inside(id.layerName()) && ibd->z.inside(id.moduleName()) &&
0025 ibd->f.inside(id.ladderName()))
0026 return (*ibc).first;
0027 }
0028 }
0029 edm::LogError("** PixelToFEDAssociateFromAscii WARNING, name: ") << id.name() << " not associated to FED";
0030 return -1;
0031 }
0032
0033 int PixelToFEDAssociateFromAscii::operator()(const PixelEndcapName& id) const {
0034 for (EndcapConnections::const_iterator iec = theEndcap.begin(); iec != theEndcap.end(); iec++) {
0035 for (vector<Edu>::const_iterator ied = (*iec).second.begin(); ied != (*iec).second.end(); ied++) {
0036 if (ied->e == id.halfCylinder() && ied->d.inside(id.diskName()) && ied->b.inside(id.bladeName()))
0037 return iec->first;
0038 }
0039 }
0040 edm::LogError("** PixelToFEDAssociateFromAscii WARNING, name: ") << id.name() << " not associated to FED";
0041 return -1;
0042 }
0043
0044 void PixelToFEDAssociateFromAscii::init(const string& cfg_name) {
0045 LogDebug("init, input file:") << cfg_name.c_str();
0046
0047 std::ifstream file(cfg_name.c_str());
0048 if (!file) {
0049 edm::LogError(" ** PixelToFEDAssociateFromAscii,init ** ") << " cant open data file: " << cfg_name;
0050 return;
0051 } else {
0052 edm::LogInfo("PixelToFEDAssociateFromAscii, read data from: ") << cfg_name;
0053 }
0054
0055 string line;
0056 pair<int, vector<Bdu> > barCon;
0057 pair<int, vector<Edu> > endCon;
0058
0059 try {
0060 while (getline(file, line)) {
0061
0062
0063
0064 string::size_type pos = line.find('#');
0065 if (pos != string::npos)
0066 line = line.erase(pos);
0067
0068 string::size_type posF = line.find("FED:");
0069 string::size_type posB = line.find("S:");
0070 string::size_type posE = line.find("E:");
0071
0072 LogDebug("line read") << line;
0073
0074
0075
0076
0077 if (line.compare(0, 3, "VER") == 0) {
0078 edm::LogInfo("version: ") << line;
0079 theVersion = line;
0080 send(barCon, endCon);
0081 theBarrel.clear();
0082 theEndcap.clear();
0083 }
0084
0085
0086
0087
0088 else if (posF != string::npos) {
0089 line = line.substr(posF + 4);
0090 int id = atoi(line.c_str());
0091 send(barCon, endCon);
0092 barCon.first = id;
0093 endCon.first = id;
0094 }
0095
0096
0097
0098
0099 else if (posB != string::npos) {
0100 line = line.substr(posB + 2);
0101 barCon.second.push_back(getBdu(line));
0102 }
0103
0104
0105
0106
0107 else if (posE != string::npos) {
0108 line = line.substr(posE + 2);
0109 endCon.second.push_back(getEdu(line));
0110 }
0111 }
0112 send(barCon, endCon);
0113 } catch (exception& err) {
0114 edm::LogError("**PixelToFEDAssociateFromAscii** exception") << err.what();
0115 theBarrel.clear();
0116 theEndcap.clear();
0117 }
0118
0119
0120
0121
0122 std::ostringstream str;
0123 str << " **PixelToFEDAssociateFromAscii ** BARREL FED CONNECTIONS: " << endl;
0124 for (BarrelConnections::const_iterator ibc = theBarrel.begin(); ibc != theBarrel.end(); ibc++) {
0125 str << "FED: " << ibc->first << endl;
0126 for (vector<Bdu>::const_iterator ibd = (*ibc).second.begin(); ibd != (*ibc).second.end(); ibd++) {
0127 str << "b: " << ibd->b << " l: " << ibd->l << " z: " << ibd->z << " f: " << ibd->f << endl;
0128 }
0129 }
0130 str << " **PixelToFEDAssociateFromAscii ** ENDCAP FED CONNECTIONS: " << endl;
0131 for (EndcapConnections::const_iterator iec = theEndcap.begin(); iec != theEndcap.end(); iec++) {
0132 str << "FED: " << iec->first << endl;
0133 for (vector<Edu>::const_iterator ied = (*iec).second.begin(); ied != (*iec).second.end(); ied++) {
0134 str << " e: " << ied->e << " d: " << ied->d << " b: " << ied->b << endl;
0135 }
0136 }
0137 edm::LogInfo("PixelToFEDAssociateFromAscii") << str.str();
0138 }
0139
0140 void PixelToFEDAssociateFromAscii::send(pair<int, vector<Bdu> >& b, pair<int, vector<Edu> >& e) {
0141 if (!b.second.empty())
0142 theBarrel.push_back(b);
0143 if (!e.second.empty())
0144 theEndcap.push_back(e);
0145 b.second.clear();
0146 e.second.clear();
0147 }
0148
0149 PixelToFEDAssociateFromAscii::Bdu PixelToFEDAssociateFromAscii::getBdu(string line) const {
0150 Bdu result;
0151 string::size_type pos;
0152
0153 result.b = readRange(line).first;
0154
0155 pos = line.find("L:");
0156 if (pos != string::npos)
0157 line = line.substr(pos + 2);
0158 result.l = readRange(line);
0159
0160 pos = line.find("Z:");
0161 if (pos != string::npos)
0162 line = line.substr(pos + 2);
0163 result.z = readRange(line);
0164
0165 pos = line.find("F:");
0166 if (pos != string::npos)
0167 line = line.substr(pos + 2);
0168 result.f = readRange(line);
0169
0170 return result;
0171 }
0172
0173 PixelToFEDAssociateFromAscii::Edu PixelToFEDAssociateFromAscii::getEdu(string line) const {
0174 Edu result;
0175 string::size_type pos;
0176
0177 result.e = readRange(line).first;
0178
0179 pos = line.find("D:");
0180 if (pos != string::npos)
0181 line = line.substr(pos + 2);
0182 result.d = readRange(line);
0183
0184 pos = line.find("B:");
0185 if (pos != string::npos)
0186 line = line.substr(pos + 2);
0187 result.b = readRange(line);
0188
0189 return result;
0190 }
0191
0192 PixelToFEDAssociateFromAscii::Range PixelToFEDAssociateFromAscii::readRange(const string& l) const {
0193 bool first = true;
0194 int num1 = -1;
0195 int num2 = -1;
0196 const char* line = l.c_str();
0197 while (line) {
0198 char* evp = nullptr;
0199 int num = strtol(line, &evp, 10);
0200 {
0201 stringstream s;
0202 s << "raad from line: ";
0203 s << num;
0204 LogDebug(s.str());
0205 }
0206 if (evp != line) {
0207 line = evp + 1;
0208 if (first) {
0209 num1 = num;
0210 first = false;
0211 }
0212 num2 = num;
0213 } else
0214 line = nullptr;
0215 }
0216 if (first) {
0217 string s = "** PixelToFEDAssociateFromAscii, read data, cant intrpret: ";
0218 edm::LogInfo(s) << endl << l << endl << "=====> send exception " << endl;
0219 s += l;
0220 throw cms::Exception(s);
0221 }
0222 return Range(num1, num2);
0223 }