File indexing completed on 2024-04-06 12:32:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 #include <memory>
0043
0044
0045 #include "FWCore/Framework/interface/Frameworkfwd.h"
0046 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0047
0048 #include "FWCore/Framework/interface/Event.h"
0049 #include "FWCore/Framework/interface/MakerMacros.h"
0050
0051 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0052 #include "FWCore/ParameterSet/interface/FileInPath.h"
0053 #include "FWCore/Utilities/interface/InputTag.h"
0054 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0055
0056 #include "DetectorDescription/Core/interface/DDCompactView.h"
0057 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0058 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0059
0060 #include <fstream>
0061 #include <regex>
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 class HGCalWaferValidation : public edm::one::EDAnalyzer<> {
0073 public:
0074 explicit HGCalWaferValidation(const edm::ParameterSet&);
0075 ~HGCalWaferValidation() override = default;
0076
0077 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0078
0079 private:
0080
0081 const std::string logcat = "HGCalWaferValidation";
0082
0083
0084 using WaferCoord = std::tuple<int, int, int>;
0085
0086 std::string strWaferCoord(const WaferCoord& coord);
0087
0088
0089 using WaferShapeMap = std::map<std::string, int>;
0090
0091 const WaferShapeMap waferShapeMapDD = {{"F", HGCalTypes::WaferFull},
0092 {"a", HGCalTypes::WaferHalf},
0093 {"am", HGCalTypes::WaferHalf2},
0094 {"b", HGCalTypes::WaferFive},
0095 {"bm", HGCalTypes::WaferFive2},
0096 {"c", HGCalTypes::WaferThree},
0097 {"d", HGCalTypes::WaferSemi},
0098 {"dm", HGCalTypes::WaferSemi2},
0099 {"g", HGCalTypes::WaferChopTwo},
0100 {"gm", HGCalTypes::WaferChopTwoM}};
0101
0102 const WaferShapeMap waferShapeMapLD = {{"0", HGCalTypes::WaferFull},
0103 {"1", HGCalTypes::WaferHalf},
0104 {"2", HGCalTypes::WaferHalf},
0105 {"3", HGCalTypes::WaferSemi},
0106 {"4", HGCalTypes::WaferSemi},
0107 {"5", HGCalTypes::WaferFive},
0108 {"6", HGCalTypes::WaferThree}};
0109
0110 const WaferShapeMap waferShapeMapHD = {{"0", HGCalTypes::WaferFull},
0111 {"1", HGCalTypes::WaferHalf2},
0112 {"2", HGCalTypes::WaferChopTwoM},
0113 {"3", HGCalTypes::WaferSemi2},
0114 {"4", HGCalTypes::WaferSemi2},
0115 {"5", HGCalTypes::WaferFive2}};
0116
0117 bool DDFindHGCal(DDCompactView::GraphWalker& walker, std::string targetName);
0118 void DDFindWafers(DDCompactView::GraphWalker& walker);
0119 void ProcessWaferLayer(DDCompactView::GraphWalker& walker);
0120 bool isThicknessMatched(const int geoThickClass, const int fileThickness);
0121 bool isRotationMatched(
0122 const bool isNewFile, const int layer, const int fileShapeCode, const int geoRotCode, const int fileRotCode);
0123
0124 void beginJob() override;
0125 void analyze(const edm::Event&, const edm::EventSetup&) override;
0126 void endJob() override;
0127
0128
0129
0130 edm::FileInPath geometryFileName_;
0131
0132
0133 unsigned int layerCount_;
0134 std::vector<int> layerTypes_;
0135
0136
0137 struct WaferInfo {
0138 int thickClass;
0139 double x;
0140 double y;
0141 int shapeCode;
0142 int rotCode;
0143 std::string waferName;
0144 };
0145
0146
0147 edm::ESGetToken<DDCompactView, IdealGeometryRecord> viewToken_;
0148
0149
0150 std::map<WaferCoord, struct WaferInfo> waferData_;
0151
0152
0153 std::map<WaferCoord, bool> waferValidated_;
0154 };
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 HGCalWaferValidation::HGCalWaferValidation(const edm::ParameterSet& iConfig)
0168 : geometryFileName_(iConfig.getParameter<edm::FileInPath>("GeometryFileName")) {
0169 viewToken_ = esConsumes<DDCompactView, IdealGeometryRecord>();
0170
0171 }
0172
0173
0174
0175
0176
0177
0178 std::string HGCalWaferValidation::strWaferCoord(const WaferCoord& coord) {
0179 std::stringstream ss;
0180 ss << "(" << std::get<0>(coord) << "," << std::get<1>(coord) << "," << std::get<2>(coord) << ")";
0181 return ss.str();
0182 }
0183
0184
0185 bool HGCalWaferValidation::DDFindHGCal(DDCompactView::GraphWalker& walker, std::string targetName) {
0186 if (walker.current().first.name().name() == targetName) {
0187
0188 return true;
0189 }
0190 if (walker.firstChild()) {
0191 do {
0192 if (DDFindHGCal(walker, targetName))
0193
0194 return true;
0195 } while (walker.nextSibling());
0196 walker.parent();
0197 }
0198 return false;
0199 }
0200
0201
0202 void HGCalWaferValidation::DDFindWafers(DDCompactView::GraphWalker& walker) {
0203 if (walker.current().first.name().fullname().rfind("hgcalwafer:", 0) == 0) {
0204
0205 ProcessWaferLayer(walker);
0206 return;
0207 }
0208 if (walker.firstChild()) {
0209 do {
0210 DDFindWafers(walker);
0211 } while (walker.nextSibling());
0212 walker.parent();
0213 }
0214 }
0215
0216
0217 void HGCalWaferValidation::ProcessWaferLayer(DDCompactView::GraphWalker& walker) {
0218 int waferLayer = 0;
0219 waferLayer++;
0220 edm::LogVerbatim(logcat) << "ProcessWaferLayer: Processing layer " << waferLayer;
0221 do {
0222 if (walker.current().first.name().fullname().rfind("hgcalwafer:", 0) == 0) {
0223 auto wafer = walker.current();
0224 const std::string waferName(walker.current().first.name().fullname());
0225
0226 const int copyNo = wafer.second->copyno();
0227
0228 const int waferType = HGCalTypes::getUnpackedType(copyNo);
0229 const int waferU = HGCalTypes::getUnpackedU(copyNo);
0230 const int waferV = HGCalTypes::getUnpackedV(copyNo);
0231 const WaferCoord waferCoord(waferLayer, waferU, waferV);
0232
0233 struct WaferInfo waferInfo;
0234 waferInfo.waferName = waferName;
0235 waferInfo.thickClass = waferType;
0236 waferInfo.x = wafer.second->translation().x();
0237 waferInfo.y = wafer.second->translation().y();
0238 const std::string waferNameData =
0239 std::regex_replace(waferName,
0240 std::regex("(HGCal[EH]E)(Wafer[01])(Fine|Coarse[12])([a-z]*)([0-9]*)"),
0241 "$1 $2-$3 $4 $5",
0242 std::regex_constants::format_no_copy);
0243 std::stringstream ss(waferNameData);
0244 std::string EEorHE;
0245 std::string typeStr;
0246 std::string shapeStr;
0247 std::string rotStr;
0248 ss >> EEorHE >> typeStr >> shapeStr >> rotStr;
0249
0250 if (shapeStr.empty())
0251 shapeStr = "F";
0252 if (rotStr.empty())
0253 rotStr = "0";
0254 const int rotCode(std::stoi(rotStr));
0255
0256
0257
0258 waferInfo.shapeCode = waferShapeMapDD.at(shapeStr);
0259
0260 waferInfo.rotCode = rotCode;
0261
0262 waferData_[waferCoord] = waferInfo;
0263 waferValidated_[waferCoord] = false;
0264 }
0265 } while (walker.nextSibling());
0266 }
0267
0268
0269 bool HGCalWaferValidation::isThicknessMatched(const int geoThickClass, const int fileThickness) {
0270 if (geoThickClass == 0 && fileThickness == 120)
0271 return true;
0272 if (geoThickClass == 1 && fileThickness == 200)
0273 return true;
0274 if (geoThickClass == 2 && fileThickness == 300)
0275 return true;
0276 return false;
0277 }
0278
0279
0280 bool HGCalWaferValidation::isRotationMatched(
0281 const bool isNewFile, const int layer, const int fileShapeCode, const int geoRotCode, const int fileRotCode) {
0282 if (fileShapeCode != HGCalTypes::WaferFull && geoRotCode == fileRotCode)
0283 return true;
0284 if (fileShapeCode == HGCalTypes::WaferFull) {
0285 if (isNewFile && layerTypes_[layer - 1] == 3) {
0286 if ((geoRotCode + 1) % 2 == fileRotCode % 2)
0287 return true;
0288 } else {
0289 if (geoRotCode % 2 == fileRotCode % 2)
0290 return true;
0291 }
0292 }
0293 return false;
0294 }
0295
0296
0297 void HGCalWaferValidation::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0298 using namespace edm;
0299
0300
0301 auto viewH = iSetup.getHandle(viewToken_);
0302
0303 if (!viewH.isValid()) {
0304 edm::LogPrint(logcat) << "Error obtaining geometry handle!";
0305 return;
0306 }
0307
0308 edm::LogVerbatim(logcat) << "Root is : " << viewH->root();
0309 edm::LogVerbatim(logcat) << std::endl;
0310
0311
0312 auto eeWalker = viewH->walker();
0313 const bool eeFound = DDFindHGCal(eeWalker, "HGCalEE");
0314 if (eeFound) {
0315 edm::LogVerbatim(logcat) << "HGCalEE found!";
0316 edm::LogVerbatim(logcat) << "name = " << eeWalker.current().first.name().name();
0317 edm::LogVerbatim(logcat) << "fullname = " << eeWalker.current().first.name().fullname();
0318 } else {
0319 edm::LogPrint(logcat) << "HGCalEE not found!";
0320 }
0321 edm::LogVerbatim(logcat) << std::endl;
0322
0323
0324 auto hesilWalker = viewH->walker();
0325 const bool hesilFound = DDFindHGCal(hesilWalker, "HGCalHEsil");
0326 if (hesilFound) {
0327 edm::LogVerbatim(logcat) << "HGCalHEsil found!";
0328 edm::LogVerbatim(logcat) << "name = " << hesilWalker.current().first.name().name();
0329 edm::LogVerbatim(logcat) << "fullname = " << hesilWalker.current().first.name().fullname();
0330 } else {
0331 edm::LogPrint(logcat) << "HGCalHEsil not found!";
0332 }
0333 edm::LogVerbatim(logcat) << std::endl;
0334
0335
0336 auto hemixWalker = viewH->walker();
0337 const bool hemixFound = DDFindHGCal(hemixWalker, "HGCalHEmix");
0338 if (hemixFound) {
0339 edm::LogVerbatim(logcat) << "HGCalHEmix found!";
0340 edm::LogVerbatim(logcat) << "name = " << hemixWalker.current().first.name().name();
0341 edm::LogVerbatim(logcat) << "fullname = " << hemixWalker.current().first.name().fullname();
0342 } else {
0343 edm::LogPrint(logcat) << "HGCalHEmix not found!";
0344 }
0345 edm::LogVerbatim(logcat) << std::endl;
0346
0347
0348 if (!(eeFound || hesilFound || hemixFound)) {
0349 edm::LogPrint(logcat) << "Nothing found. Giving up.";
0350 return;
0351 }
0352
0353
0354 edm::LogVerbatim(logcat) << "Calling DDFindWafers(eeWalker);";
0355 DDFindWafers(eeWalker);
0356
0357
0358 edm::LogVerbatim(logcat) << "Calling DDFindWafers(hesilWalker);";
0359 DDFindWafers(hesilWalker);
0360
0361
0362 edm::LogVerbatim(logcat) << "Calling DDFindWafers(hemixWalker);";
0363 DDFindWafers(hemixWalker);
0364
0365
0366 edm::LogVerbatim(logcat) << "Number of wafers read from DD: " << waferData_.size();
0367
0368
0369 std::string fileName = geometryFileName_.fullPath();
0370 edm::LogVerbatim(logcat) << "Opening geometry text file: " << fileName;
0371 std::ifstream geoTxtFile(fileName);
0372
0373 if (!geoTxtFile) {
0374 edm::LogPrint(logcat) << "Cannot open geometry text file.";
0375 return;
0376 }
0377
0378
0379 int nTotalProcessed = 0;
0380
0381
0382 int nMissing = 0;
0383 int nThicknessError = 0;
0384 int nPosXError = 0;
0385 int nPosYError = 0;
0386 int nShapeError = 0;
0387 int nRotError = 0;
0388 int nUnaccounted = 0;
0389
0390 std::string buf;
0391
0392
0393 std::getline(geoTxtFile, buf);
0394 std::stringstream ss(buf);
0395 std::vector<std::string> first_tokens;
0396 while (ss >> buf)
0397 if (!buf.empty())
0398 first_tokens.push_back(buf);
0399
0400 const bool isNewFile(first_tokens.size() == 1);
0401
0402 if (isNewFile) {
0403 edm::LogVerbatim(logcat) << "Text file is of newer version.";
0404 layerCount_ = std::stoi(buf);
0405 std::getline(geoTxtFile, buf);
0406 std::stringstream layerTypesSS(buf);
0407 while (layerTypesSS >> buf)
0408 if (!buf.empty())
0409 layerTypes_.push_back(std::stoi(buf));
0410 if (layerTypes_.size() != layerCount_)
0411 edm::LogWarning(logcat) << "Number of layer types does not tally with layer count.";
0412
0413
0414 edm::LogVerbatim(logcat) << "layerCount = " << layerCount_;
0415 for (unsigned i = 0; i < layerTypes_.size(); i++) {
0416 edm::LogVerbatim(logcat) << " layerType " << i + 1 << " = " << layerTypes_[i];
0417 }
0418 } else {
0419 layerCount_ = 0;
0420
0421 geoTxtFile.clear();
0422 geoTxtFile.seekg(0);
0423 }
0424
0425
0426 while (std::getline(geoTxtFile, buf)) {
0427 std::stringstream ss(buf);
0428 std::vector<std::string> tokens;
0429 while (ss >> buf)
0430 if (!buf.empty())
0431 tokens.push_back(buf);
0432 if (tokens.size() != 8)
0433 continue;
0434
0435 nTotalProcessed++;
0436
0437
0438 const int waferLayer(std::stoi(tokens[0]));
0439 const std::string waferShapeStr(tokens[1]);
0440 const std::string waferDensityStr(isNewFile ? tokens[2].substr(0, 1) : "");
0441 const int waferThickness(isNewFile ? std::stoi(tokens[2].substr(1)) : std::stoi(tokens[2]));
0442 const double waferX(std::stod(tokens[3]));
0443 const double waferY(std::stod(tokens[4]));
0444 const int waferRotCode(std::stoi(tokens[5]));
0445 const int waferU(std::stoi(tokens[6]));
0446 const int waferV(std::stoi(tokens[7]));
0447 const int waferShapeCode(isNewFile ? (waferDensityStr == "l" ? waferShapeMapLD.at(waferShapeStr)
0448 : waferDensityStr == "h" ? waferShapeMapHD.at(waferShapeStr)
0449 : HGCalTypes::WaferOut)
0450 : waferShapeMapDD.at(waferShapeStr));
0451
0452
0453 const WaferCoord waferCoord(waferLayer, waferU, waferV);
0454
0455
0456
0457 if (waferData_.find(waferCoord) == waferData_.end()) {
0458 nMissing++;
0459 edm::LogVerbatim(logcat) << "MISSING: " << strWaferCoord(waferCoord);
0460 continue;
0461 }
0462
0463 const struct WaferInfo waferInfo = waferData_[waferCoord];
0464 waferValidated_[waferCoord] = true;
0465
0466 if (!isThicknessMatched(waferInfo.thickClass, waferThickness)) {
0467 nThicknessError++;
0468 edm::LogVerbatim(logcat) << "THICKNESS ERROR: " << strWaferCoord(waferCoord);
0469 }
0470
0471
0472 if (fabs(-waferInfo.x - waferX) > 0.015) {
0473 nPosXError++;
0474 edm::LogVerbatim(logcat) << "POSITION x ERROR: " << strWaferCoord(waferCoord);
0475 }
0476
0477 if (fabs(waferInfo.y - waferY) > 0.015) {
0478 nPosYError++;
0479 edm::LogVerbatim(logcat) << "POSITION y ERROR: " << strWaferCoord(waferCoord);
0480 }
0481
0482 if (waferInfo.shapeCode != waferShapeCode || waferShapeCode == HGCalTypes::WaferOut) {
0483 nShapeError++;
0484 edm::LogVerbatim(logcat) << "SHAPE ERROR: " << strWaferCoord(waferCoord) << " ( " << waferInfo.shapeCode
0485 << " != " << waferDensityStr << waferShapeCode << " ) name=" << waferInfo.waferName;
0486 }
0487
0488 if (!isRotationMatched(isNewFile, std::get<0>(waferCoord), waferShapeCode, waferInfo.rotCode, waferRotCode)) {
0489 nRotError++;
0490 edm::LogVerbatim(logcat) << "ROTATION ERROR: " << strWaferCoord(waferCoord) << " ( " << waferInfo.rotCode
0491 << " != " << waferRotCode << " (" << waferShapeCode
0492 << ") ) name=" << waferInfo.waferName;
0493 }
0494 }
0495
0496 geoTxtFile.close();
0497
0498
0499 for (auto const& accounted : waferValidated_) {
0500 if (!accounted.second) {
0501 nUnaccounted++;
0502 edm::LogVerbatim(logcat) << "UNACCOUNTED: " << strWaferCoord(accounted.first);
0503 }
0504 }
0505
0506
0507 edm::LogVerbatim(logcat) << std::endl;
0508 edm::LogVerbatim(logcat) << "*** ERROR COUNTS ***";
0509 edm::LogVerbatim(logcat) << "Missing : " << nMissing;
0510 edm::LogVerbatim(logcat) << "Thickness error : " << nThicknessError;
0511 edm::LogVerbatim(logcat) << "Pos-x error : " << nPosXError;
0512 edm::LogVerbatim(logcat) << "Pos-y error : " << nPosYError;
0513 edm::LogVerbatim(logcat) << "Shape error : " << nShapeError;
0514 edm::LogVerbatim(logcat) << "Rotation error : " << nRotError;
0515 edm::LogVerbatim(logcat) << "Unaccounted : " << nUnaccounted;
0516 edm::LogVerbatim(logcat) << std::endl;
0517 edm::LogVerbatim(logcat) << "Total wafers processed from geotxtfile = " << nTotalProcessed;
0518 edm::LogVerbatim(logcat) << std::endl;
0519
0520
0521 if (nMissing > 0 || nThicknessError > 0 || nPosXError > 0 || nPosYError > 0 || nShapeError > 0 || nRotError > 0 ||
0522 nUnaccounted > 0) {
0523 edm::LogPrint(logcat) << "There are at least one wafer error.";
0524 }
0525 }
0526
0527
0528 void HGCalWaferValidation::beginJob() {
0529
0530 }
0531
0532
0533 void HGCalWaferValidation::endJob() {
0534
0535 }
0536
0537
0538 void HGCalWaferValidation::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0539
0540
0541 edm::ParameterSetDescription desc;
0542 desc.add<edm::FileInPath>("GeometryFileName",
0543 edm::FileInPath("Validation/HGCalValidation/data/geomnew_corrected_360.txt"));
0544 descriptions.add("hgcalWaferValidation", desc);
0545 }
0546
0547
0548 DEFINE_FWK_MODULE(HGCalWaferValidation);