Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:31

0001 ////////////////////////////////////////////////////////////////////////////////
0002 //
0003 //    Compares iles containing lists of Sensitive Detectors, Touchables obtaied
0004 //    from DDD and DD4hep and uses a list of volumes touched while running
0005 //    MinimumBias events and reaching partial wafers. Usage:
0006 //
0007 //    TouchFileCompare infile1 infile2 debug
0008 //    sdFileDDD       (const char*) SD file name (from DDD)
0009 //    sdFileDD4hep    (const char*) SD file name (from DD4hep)
0010 //    touchFileDDD    (const char*) Touch file name (from DDD)
0011 //    touchFileDD4hep (const char*) Touch file name (from DD4hep)
0012 //    mbFile          (const char*) MinimumBias o/p file name (grep with "Top ")
0013 //    debug           (int)         Debug flag (0 minimum printout)
0014 //
0015 ////////////////////////////////////////////////////////////////////////////////
0016 
0017 #include "SimG4Core/Geometry/interface/DD4hep2DDDName.h"
0018 #include <algorithm>
0019 #include <cstdlib>
0020 #include <fstream>
0021 #include <iostream>
0022 #include <map>
0023 #include <string>
0024 #include <vector>
0025 
0026 std::vector<std::string> splitString(const std::string& fLine) {
0027   std::vector<std::string> result;
0028   int start = 0;
0029   bool empty = true;
0030   for (unsigned i = 0; i <= fLine.size(); i++) {
0031     if (fLine[i] == ' ' || i == fLine.size()) {
0032       if (!empty) {
0033         std::string item(fLine, start, i - start);
0034         result.push_back(item);
0035         empty = true;
0036       }
0037       start = i + 1;
0038     } else {
0039       if (empty)
0040         empty = false;
0041     }
0042   }
0043   return result;
0044 }
0045 
0046 void CompareFiles(const char* sdFileDDD,
0047                   const char* sdFileDD4hep,
0048                   const char* touchFileDDD,
0049                   const char* touchFileDD4hep,
0050                   const char* mbFile,
0051                   int debug) {
0052   std::vector<std::string> sdDDD, sdDD4hep, touchDDD, touchDD4hep, mbVol;
0053   char buffer[1000];
0054   std::ifstream fInput1(sdFileDDD);
0055   int all(0), good(0), ee(0), hesil(0), hesci(0);
0056   if (!fInput1.good()) {
0057     std::cout << "Cannot open file " << sdFileDDD << std::endl;
0058   } else {
0059     while (fInput1.getline(buffer, 1000)) {
0060       std::vector<std::string> items = splitString(std::string(buffer));
0061       ++all;
0062       if (items.size() > 0) {
0063         sdDDD.emplace_back(items[0]);
0064         if (((debug / 10) % 10) > 0)
0065           std::cout << "[" << good << "] " << sdDDD.back() << std::endl;
0066         ++good;
0067         if (sdDDD.back().find("EE") != std::string::npos) {
0068           ++ee;
0069         } else if (sdDDD.back().find("HESil") != std::string::npos) {
0070           ++hesil;
0071         } else if (sdDDD.back().find("HESci") != std::string::npos) {
0072           ++hesci;
0073         }
0074       }
0075     }
0076     fInput1.close();
0077     std::cout << "Reads " << all << ":" << good << " names from " << sdFileDDD << " with " << ee << ":" << hesil << ":"
0078               << hesci << " for EE:HESilicon:HEScintillator" << std::endl;
0079   }
0080   std::ifstream fInput2(sdFileDD4hep);
0081   all = good = ee = hesil = hesci = 0;
0082   if (!fInput2.good()) {
0083     std::cout << "Cannot open file " << sdFileDD4hep << std::endl;
0084   } else {
0085     while (fInput2.getline(buffer, 1000)) {
0086       std::vector<std::string> items = splitString(std::string(buffer));
0087       ++all;
0088       if (items.size() > 0) {
0089         sdDD4hep.emplace_back(DD4hep2DDDName::noNameSpace(items[0]));
0090         if (((debug / 10) % 10) > 0)
0091           std::cout << "[" << good << "] " << sdDD4hep.back() << std::endl;
0092         ++good;
0093         if (sdDD4hep.back().find("EE") != std::string::npos) {
0094           ++ee;
0095         } else if (sdDD4hep.back().find("HESil") != std::string::npos) {
0096           ++hesil;
0097         } else if (sdDD4hep.back().find("HESci") != std::string::npos) {
0098           ++hesci;
0099         }
0100       }
0101     }
0102     fInput2.close();
0103     std::cout << "Reads " << all << ":" << good << " names from " << sdFileDD4hep << " with " << ee << ":" << hesil
0104               << ":" << hesci << " for EE:HESilicon:HEScintillator" << std::endl;
0105   }
0106   std::ifstream fInput3(touchFileDDD);
0107   all = good = 0;
0108   if (!fInput3.good()) {
0109     std::cout << "Cannot open file " << touchFileDDD << std::endl;
0110   } else {
0111     while (fInput3.getline(buffer, 1000)) {
0112       std::vector<std::string> items = splitString(std::string(buffer));
0113       ++all;
0114       if (items.size() > 0) {
0115         touchDDD.emplace_back(items[0]);
0116         if (((debug / 100) % 10) > 0)
0117           std::cout << "[" << good << "] " << touchDDD.back() << std::endl;
0118         ++good;
0119       }
0120     }
0121     fInput3.close();
0122     std::cout << "Reads " << all << ":" << good << " names from " << touchFileDDD << std::endl;
0123   }
0124   std::ifstream fInput4(touchFileDD4hep);
0125   all = good = 0;
0126   if (!fInput4.good()) {
0127     std::cout << "Cannot open file " << touchFileDD4hep << std::endl;
0128   } else {
0129     while (fInput4.getline(buffer, 1000)) {
0130       std::vector<std::string> items = splitString(std::string(buffer));
0131       ++all;
0132       if (items.size() > 0) {
0133         touchDD4hep.emplace_back(items[0]);
0134         if (((debug / 100) % 10) > 0)
0135           std::cout << "[" << good << "] " << touchDD4hep.back() << std::endl;
0136         ++good;
0137       }
0138     }
0139     fInput4.close();
0140     std::cout << "Reads " << all << ":" << good << " names from " << touchFileDD4hep << std::endl;
0141   }
0142   std::ifstream fInput5(mbFile);
0143   all = good = 0;
0144   if (!fInput5.good()) {
0145     std::cout << "Cannot open file " << mbFile << std::endl;
0146   } else {
0147     while (fInput5.getline(buffer, 1000)) {
0148       std::vector<std::string> items = splitString(std::string(buffer));
0149       ++all;
0150       if (items.size() > 1) {
0151         mbVol.emplace_back(items[1]);
0152         if (((debug / 1000) % 10) > 0)
0153           std::cout << "[" << good << "] " << mbVol.back() << std::endl;
0154         ++good;
0155       }
0156     }
0157     fInput5.close();
0158     std::cout << "Reads " << all << ":" << good << " names from " << mbFile << std::endl;
0159   }
0160   all = ee = hesil = hesci = 0;
0161   std::vector<std::string> extraSD;
0162   for (const auto& sd : sdDDD) {
0163     if (std::find(sdDD4hep.begin(), sdDD4hep.end(), sd) == sdDD4hep.end()) {
0164       extraSD.emplace_back(sd);
0165       if (((debug / 10000) % 10) > 0)
0166         std::cout << "[" << all << "] " << extraSD.back() << std::endl;
0167       ++all;
0168       if (extraSD.back().find("EE") != std::string::npos) {
0169         ++ee;
0170       } else if (extraSD.back().find("HESil") != std::string::npos) {
0171         ++hesil;
0172       } else if (extraSD.back().find("HESci") != std::string::npos) {
0173         ++hesci;
0174       }
0175     }
0176   }
0177   std::cout << "Additional " << all << " names in " << sdFileDDD << " with " << ee << ":" << hesil << ":" << hesci
0178             << " for EE:HESilicon:HEScintillator" << std::endl;
0179   all = 0;
0180   std::vector<int> ddd(extraSD.size(), 0), dd4hep(extraSD.size(), 0), mb(extraSD.size(), 0);
0181   for (unsigned int k = 0; k < extraSD.size(); ++k) {
0182     bool t1 = (std::find(touchDDD.begin(), touchDDD.end(), extraSD[k]) != touchDDD.end());
0183     bool t2 = (std::find(touchDD4hep.begin(), touchDD4hep.end(), extraSD[k]) != touchDD4hep.end());
0184     bool t3 = (std::find(mbVol.begin(), mbVol.end(), extraSD[k]) != mbVol.end());
0185     if (t1)
0186       ++ddd[k];
0187     if (t2)
0188       ++dd4hep[k];
0189     if (t3)
0190       ++mb[k];
0191     if (t1 || t2 || t3) {
0192       ++all;
0193       std::cout << "Extra SD " << extraSD[k] << " in DDD|DD4Touch|MB File " << t1 << ":" << t2 << ":" << t3
0194                 << std::endl;
0195     }
0196   }
0197   std::cout << all << " extra SD's appear in some touchable or MB file " << std::endl;
0198   if (((debug / 1) % 10) > 0) {
0199     for (unsigned int k = 0; k < extraSD.size(); ++k)
0200       std::cout << "[" << k << "] " << extraSD[k] << " Find count " << ddd[k] << ":" << dd4hep[k] << ":" << mb[k]
0201                 << std::endl;
0202   }
0203   //store sorted touch file for DDD
0204   char fname[100];
0205   std::sort(touchDDD.begin(), touchDDD.end());
0206   sprintf(fname, "sorted%s", touchFileDDD);
0207   std::ofstream fOut1(fname);
0208   all = 0;
0209   for (const auto& touch : touchDDD) {
0210     fOut1 << " " << touch << "\n";
0211     ++all;
0212   }
0213   fOut1.close();
0214   std::cout << "Writes " << all << " Touchables in " << fname << " from " << touchFileDDD << std::endl;
0215   //store sorted touch file for DDD
0216   all = 0;
0217   std::sort(touchDD4hep.begin(), touchDD4hep.end());
0218   sprintf(fname, "sorted%s", touchFileDD4hep);
0219   std::ofstream fOut2(fname);
0220   for (const auto& touch : touchDD4hep) {
0221     fOut2 << " " << touch << "\n";
0222     ++all;
0223   }
0224   fOut2.close();
0225   std::cout << "Writes " << all << " Touchables in " << fname << " from " << touchFileDD4hep << std::endl;
0226 }
0227 
0228 int main(int argc, char* argv[]) {
0229   if (argc <= 6) {
0230     std::cout << "Please give a minimum of 6 arguments \n"
0231               << "name of the SD file corresponding to DDD \n"
0232               << "name of the SD file corresponding to DD4hep \n"
0233               << "name of the Touch file corresponding to DDD \n"
0234               << "name of the Touch file corresponding to DD4hep \n"
0235               << "name of the sMnimumBias file \n"
0236               << "debug flag (0 for minimum printout)\n"
0237               << std::endl;
0238     return 0;
0239   }
0240 
0241   const char* sdFileDDD = argv[1];
0242   const char* sdFileDD4hep = argv[2];
0243   const char* touchFileDDD = argv[3];
0244   const char* touchFileDD4hep = argv[4];
0245   const char* mbFile = argv[5];
0246   const int debug = std::atoi(argv[6]);
0247   CompareFiles(sdFileDDD, sdFileDD4hep, touchFileDDD, touchFileDD4hep, mbFile, debug);
0248   return 0;
0249 }