Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-24 04:44:57

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 //  Contains 3 macros to extract from the ASCII files from Philippe, Chris
0004 //  and Katya to make part of the xml file to be used to define the
0005 //  geometry of the HGCal detector. There are 3 versions for the silicon
0006 //  part for the format versions V14|V15, version V16 and verion V17
0007 //
0008 //  HGCalConvert 0/2 infile outfile1 outfile2 outfile3 modeGlobal debug
0009 //               0 for the V14/V15 formats; 1 for the V16 format;
0010 //               2 for the V17 format
0011 //  infile   (const char*)   Input file from Philippe conatining layer #,
0012 //                           size, depth, x, y position, orientation, u, v
0013 //  outfile1 (const char*)   Output fle for the EE part
0014 //  outfile2 (const char*)   Output fle for the HE part (full silicon)
0015 //  outfile3 (const char*)   Output fle for the HE part (mixed layers)
0016 //  modeGlobal (int)         Flag to create parts to be inserted in the
0017 //                           global section (0) or to be inserted in
0018 //                           the ddAlgorithm part (1)
0019 //  debug     (int)          Three digit integer to set debug for each
0020 //                           of the outputs
0021 //
0022 //  HGCalConvert 3 infile outfile1 outfile2 laymin cassette debug
0023 //               4 infile outfile1 outfile2 laymin cassette nlayers debug
0024 //  infile   (const char*)   Input file from Katya (modified by Chris)
0025 //                           containing layer #. ring #, start and end
0026 //                           of ring radius, SiPM size, 4 hexadecimal
0027 //                           words specifying if the phi location in
0028 //                           120-degree sector is filled, scintillator
0029 //                           type (c: cast, m: mould)
0030 //  outfile1 (const char*)   Output file for the part to be inserted in
0031 //                           the global section
0032 //  outfile2 (const char*)   Output file for the part to be inserted in
0033 //                           the ddAlgorithm part
0034 //  laymin   (int)           First layer number of the HE part
0035 //                           (28 for versions: V14, V15; 26 for V16, V17)
0036 //  cassette (int)           Cassettes are used in geometry definition
0037 //                           (0 if none, 1 if 12 cassettes are used)
0038 //  nlayers  (int)           Number of scintillator layers (14 for v19)
0039 //  debug    (int)           Two digit integer to set debug for each
0040 //                           of the outputs
0041 //
0042 //  HGCalConvert 5 infile outfile1 outfile2 maxLayEE maxLayHE modeGlobal cassetes
0043 //                  debug
0044 //               5 for the V18 formats of HFNose or for making cassette towers
0045 //  infile   (const char*)   Input file from conatining layer #, size, depth,
0046 //                           x, y position, orientation, u, v, cassette
0047 //  outfile1 (const char*)   Output fle for the EE part
0048 //  outfile2 (const char*)   Output fle for the HE part
0049 //  maxLayEE (int)           Maximum layer number of the EE part
0050 //  maxLayHE (int)           Maximum layer number of the HE part
0051 //  modeGlobal (int)         Flag to create parts to be inserted in the
0052 //                           global section (0) or to be inserted in
0053 //                           the ddAlgorithm part (1)
0054 //  cassettes (int)          Number of cassettes: -1 if chosen from defaults
0055 //                           of EE or HE;(-1)
0056 //  debug     (int)          Two digit integer to set debug for each
0057 //                           of the outputs (opional)
0058 //
0059 //  HGCalConvert 99 infile outfile minLayer maxLayer cassette debug
0060 //               99 for extracting information of a given cassette
0061 //  infile   (const char*)   Input file from conatining layer #, size, depth,
0062 //                           x, y position, orientation, u, v, cassette
0063 //  outfile  (const char*)   Output fle for the EE part
0064 //  minLayer (int)           Minimum layer number
0065 //  maxLayer (int)           Maximum layer number
0066 //  cassette (int)           Cassette number
0067 //  debug    (int)           To set debug for the output (opional)
0068 //
0069 //////////////////////////////////////////////////////////////////////////////
0070 
0071 #include "Geometry/HGCalCommonData/interface/HGCalProperty.h"
0072 #include "Geometry/HGCalCommonData/interface/HGCalTileIndex.h"
0073 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0074 #include "Geometry/HGCalCommonData/interface/HGCalWaferIndex.h"
0075 #include <algorithm>
0076 #include <cstdlib>
0077 #include <fstream>
0078 #include <iomanip>
0079 #include <iostream>
0080 #include <map>
0081 #include <string>
0082 #include <vector>
0083 
0084 struct wafer {
0085   int thick, partial, orient, cassette;
0086   wafer(int t = 0, int p = 0, int o = 0, int c = 0) : thick(t), partial(p), orient(o), cassette(c){};
0087 };
0088 
0089 struct layerInfo {
0090   int layer, type;
0091   std::vector<double> deltaR;
0092   layerInfo(int ly, int ty, std::vector<double> dR) : layer(ly), type(ty), deltaR(dR){};
0093 };
0094 
0095 struct tile {
0096   double sipm;
0097   int type, nphi, hex[6];
0098   tile(double s = 0, int t = 0, int n = 0, int h1 = 0, int h2 = 0, int h3 = 0, int h4 = 0, int h5 = 0, int h6 = 0)
0099       : sipm(s), type(t), nphi(n) {
0100     hex[0] = h1;
0101     hex[1] = h2;
0102     hex[2] = h3;
0103     hex[3] = h4;
0104     hex[4] = h5;
0105     hex[5] = h6;
0106   };
0107 };
0108 
0109 struct tileZone {
0110   tileZone(int l0 = 0, int r0 = 0, int r1 = 0, int f0 = 0, int f1 = 0, int c0 = 0)
0111       : layer(l0), rmin(r0), rmax(r1), phimin(f0), phimax(f1), cassette(c0){};
0112   int layer, rmin, rmax, phimin, phimax, cassette;
0113 };
0114 
0115 std::vector<std::string> splitString(const std::string& fLine);
0116 
0117 class ConvertSiliconV0 {
0118 public:
0119   ConvertSiliconV0(int layMax1 = 28, int layMax2 = 36);
0120   void convert(const char* infile,
0121                const char* outfile1,
0122                const char* outfile2,
0123                const char* outfile3,
0124                int modeGlobal = 0,
0125                int debug = 0);
0126 
0127 private:
0128   void writeSilicon(const char*, const std::map<int, wafer>&, const std::string&, const bool&, const bool&);
0129 
0130   const int layMax1_, layMax2_;
0131 };
0132 
0133 class ConvertSiliconV1 {
0134 public:
0135   ConvertSiliconV1(int layMax1 = 26, int layMax2 = 33);
0136   void convert(const char* infile,
0137                const char* outfile1,
0138                const char* outfile2,
0139                const char* outfile3,
0140                int modeGlobal = 0,
0141                int debug = 0);
0142 
0143 private:
0144   void writeSilicon(
0145       const char*, const std::vector<int>&, const std::map<int, wafer>&, const std::string&, const bool&, const bool&);
0146 
0147   const int layMax1_, layMax2_;
0148 };
0149 
0150 class ConvertSiliconV2 {
0151 public:
0152   ConvertSiliconV2(unsigned int layMax1 = 26, unsigned int layMax2 = 33, unsigned int layMax3 = 47);
0153   void convert(const char* infile,
0154                const char* outfile1,
0155                const char* outfile2,
0156                const char* outfile3,
0157                int modeGlobal = 0,
0158                int debug = 0);
0159 
0160 private:
0161   void writeSilicon(const char*,
0162                     const unsigned int,
0163                     const std::vector<layerInfo>&,
0164                     const std::map<int, wafer>&,
0165                     const std::string&,
0166                     const bool&,
0167                     const bool&);
0168 
0169   const unsigned int layMax1_, layMax2_, layMax3_;
0170 };
0171 
0172 class ConvertScintillator {
0173 public:
0174   ConvertScintillator(int layMin = 26, int cassette = 0);
0175   void convert(const char*, const char*, const char*, int debug = 0);
0176 
0177 private:
0178   void makeTitle(const char* outfile,
0179                  const std::map<int, tile>& module,
0180                  const std::map<int, std::pair<double, double> >& ringR,
0181                  int lmin,
0182                  int lmax,
0183                  bool debug);
0184 
0185   const int layMin_;
0186   const int cassette_;
0187 };
0188 
0189 class ConvertScintillatorV1 {
0190 public:
0191   ConvertScintillatorV1(int layMin = 26, int cassette = 0, int layers = 14);
0192   void convert(const char*, const char*, const char*, int debug = 0);
0193 
0194 private:
0195   void makeTitle(std::ofstream&,
0196                  const char*,
0197                  const std::map<int, tile>& module,
0198                  const std::map<int, std::pair<double, double> >& ringR,
0199                  int lmin,
0200                  int lmax,
0201                  int nphis,
0202                  int mode,
0203                  bool debug);
0204 
0205   const int layMin_, cassette_, layers_;
0206 };
0207 
0208 class ConvertNoseV0 {
0209 public:
0210   ConvertNoseV0(unsigned int layMax1 = 6, unsigned int layMax2 = 8);
0211   void convert(const char* infile,
0212                const char* outfile1,
0213                const char* outfile2,
0214                int modeGlobal = 0,
0215                int cassette = -1,
0216                int debug = 0);
0217 
0218 private:
0219   void writeNose(const char*,
0220                  const unsigned int,
0221                  const std::vector<layerInfo>&,
0222                  const std::map<int, wafer>&,
0223                  const std::string&,
0224                  const bool&,
0225                  const bool&);
0226 
0227   const unsigned int layMax1_, layMax2_;
0228 };
0229 
0230 class ConvertCassetteV0 {
0231 public:
0232   ConvertCassetteV0(int layMin = 1, int layMax = 10, int cassette = 1);
0233   void convert(const char* infile, const char* outfile, int debug = 0);
0234 
0235 private:
0236   const int layMin_, layMax_, cassette_;
0237 };
0238 
0239 int main(int argc, char* argv[]) {
0240   if (argc < 7) {
0241     std::cout << "Please give a minimum of 7 arguments \n"
0242               << "mode (0, 1, 2:silicon; 3, 4:scintillator, 5:nose, cassette)\n"
0243               << "input file name\n"
0244               << "output file name\n"
0245               << "for silicon 4 additional parameters:\n"
0246               << "  second output file name\n"
0247               << "  third output file name\n"
0248               << "  output mode (0: gobal; 1: local)\n"
0249               << "  debug (three digis to set debug for each output)\n"
0250               << "for scintillator 4|5 additional parameters after the first 3\n"
0251               << "  second output file name\n"
0252               << "  number of layers in the EE section: 28 or 26\n"
0253               << "  flag to utilize caseeette partition or not\n"
0254               << "  total number of layers with scintillators\n"
0255               << "  debug flag\n"
0256               << "for HFNose 6 additional parameters after the first 3\n"
0257               << "  second output file name\n"
0258               << "  maximum layer number of the EE section: 6\n"
0259               << "  maximum layer number of the HE section: 8\n"
0260               << "  output mode (0: gobal; 1: local)\n"
0261               << "  cassettes (if default for EE/HE to be overrideen)\n"
0262               << "  debug flag\n"
0263               << "for Cassette 99 additional parameters after the first 3\n"
0264               << "  minimum layer number: 1\n"
0265               << "  maximum layer number: 10\n"
0266               << "  cassette number: 1\n"
0267               << "  debug flag: 0\n"
0268               << std::endl;
0269     return 0;
0270   }
0271 
0272   int mode = std::atoi(argv[1]);
0273   const char* infile = argv[2];
0274   int code(0);
0275   if (mode <= 2) {
0276     const char* outfile1 = argv[3];
0277     const char* outfile2 = argv[4];
0278     const char* outfile3 = argv[5];
0279     int modeGlobal = atoi(argv[6]);
0280     int debug = atoi(argv[7]);
0281     std::cout << "Calls ConvertSilicon for i/p file " << infile << " o/p files " << outfile1 << ":" << outfile2 << ":"
0282               << outfile3 << " Mode " << modeGlobal << " Debug " << debug << std::endl;
0283     if (mode == 0) {
0284       ConvertSiliconV0 c1;
0285       c1.convert(infile, outfile1, outfile2, outfile3, modeGlobal, debug);
0286     } else if (mode == 1) {
0287       ConvertSiliconV1 c1;
0288       c1.convert(infile, outfile1, outfile2, outfile3, modeGlobal, debug);
0289     } else {
0290       ConvertSiliconV2 c1;
0291       c1.convert(infile, outfile1, outfile2, outfile3, modeGlobal, debug);
0292     }
0293   } else if (mode == 3) {
0294     const char* outfile1 = argv[3];
0295     const char* outfile2 = argv[4];
0296     int laymin = atoi(argv[5]);
0297     int cassette = atoi(argv[6]);
0298     int debug = atoi(argv[7]);
0299     std::cout << "Calls ConvertScintillator for i/p file " << infile << " o/p files " << outfile1 << ":" << outfile2
0300               << " Laymin " << laymin << " Cassette " << cassette << " Debug " << debug << std::endl;
0301     ConvertScintillator c1(laymin, cassette);
0302     c1.convert(infile, outfile1, outfile2, debug);
0303   } else if (mode == 4) {
0304     const char* outfile1 = argv[3];
0305     const char* outfile2 = argv[4];
0306     int laymin = (argc > 5) ? atoi(argv[5]) : 26;
0307     int cassette = (argc > 6) ? atoi(argv[6]) : 1;
0308     int layers = (argc > 7) ? atoi(argv[7]) : 14;
0309     int debug = (argc > 8) ? atoi(argv[8]) : 0;
0310     std::cout << "Calls ConvertScintillator for i/p file " << infile << " o/p files " << outfile1 << ":" << outfile2
0311               << " Laymin " << laymin << " Cassette " << cassette << " Layers " << layers << " Debug " << debug
0312               << std::endl;
0313     ConvertScintillatorV1 c1(laymin, cassette, layers);
0314     c1.convert(infile, outfile1, outfile2, debug);
0315   } else if (mode == 5) {
0316     const char* outfile1 = argv[3];
0317     const char* outfile2 = argv[4];
0318     int maxLayEE = atoi(argv[5]);
0319     int maxLayHE = atoi(argv[6]);
0320     int modeGlobal = atoi(argv[7]);
0321     int cassette = (argc > 7) ? atoi(argv[8]) : -1;
0322     int debug = (argc > 8) ? atoi(argv[9]) : 0;
0323     std::cout << "Calls ConvertNose for i/p file " << infile << " o/p files " << outfile1 << ":" << outfile2
0324               << " Layers " << maxLayEE << ":" << maxLayHE << " Mode " << modeGlobal << " Cassettes " << cassette
0325               << " Debug " << debug << std::endl;
0326     ConvertNoseV0 c1(maxLayEE, maxLayHE);
0327     c1.convert(infile, outfile1, outfile2, modeGlobal, cassette, debug);
0328   } else if (mode == 99) {
0329     const char* outfile = argv[3];
0330     int minLay = (argc > 3) ? atoi(argv[4]) : 1;
0331     int maxLay = (argc > 4) ? atoi(argv[5]) : 10;
0332     int cassette = (argc > 5) ? atoi(argv[6]) : 1;
0333     int debug = (argc > 6) ? atoi(argv[7]) : 0;
0334     std::cout << "Calls ConvertCassette for i/p file " << infile << " o/p file " << outfile << " Layers " << minLay
0335               << " : " << maxLay << " Cassette " << cassette << " Debug " << debug << std::endl;
0336     ConvertCassetteV0 c1(minLay, maxLay, cassette);
0337     c1.convert(infile, outfile, debug);
0338   } else {
0339     code = 1;
0340   }
0341   return code;
0342 }
0343 
0344 std::vector<std::string> splitString(const std::string& fLine) {
0345   std::vector<std::string> result;
0346   int start = 0;
0347   bool empty = true;
0348   for (unsigned i = 0; i <= fLine.size(); i++) {
0349     if (fLine[i] == ' ' || i == fLine.size()) {
0350       if (!empty) {
0351         std::string item(fLine, start, i - start);
0352         result.push_back(item);
0353         empty = true;
0354       }
0355       start = i + 1;
0356     } else {
0357       if (empty)
0358         empty = false;
0359     }
0360   }
0361   return result;
0362 }
0363 
0364 ConvertSiliconV0::ConvertSiliconV0(int layMax1, int layMax2) : layMax1_(layMax1), layMax2_(layMax2) {}
0365 
0366 void ConvertSiliconV0::convert(
0367     const char* infile, const char* outfile1, const char* outfile2, const char* outfile3, int modeGlobal, int debug) {
0368   std::ifstream fInput(infile);
0369   if (!fInput.good()) {
0370     std::cout << "Cannot open file " << infile << std::endl;
0371   } else {
0372     //First read in all records
0373     char buffer[1024];
0374     std::string thick[3] = {"120", "200", "300"};
0375     std::string partial[8] = {"F", "b", "g", "gm", "a", "d", "dm", "c"};
0376     std::map<int, wafer> module1, module2, module3;
0377     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0378     bool global = (modeGlobal < 1);
0379     while (fInput.getline(buffer, 1024)) {
0380       ++all;
0381       if (debug % 10 > 1)
0382         std::cout << "[" << all << "] " << buffer << std::endl;
0383       if (buffer[0] == '#') {
0384         ++comments;
0385       } else {
0386         ++others;
0387         std::vector<std::string> items = splitString(std::string(buffer));
0388         if (items.size() != 8) {
0389           ++bad;
0390         } else {
0391           ++good;
0392           int layer = std::atoi(items[0].c_str());
0393           int waferU = std::atoi(items[6].c_str());
0394           int waferV = std::atoi(items[7].c_str());
0395           int thck = static_cast<int>(std::find(thick, thick + 3, items[2]) - thick);
0396           int part = static_cast<int>(std::find(partial, partial + 8, items[1]) - partial);
0397           int orient = std::atoi(items[5].c_str());
0398           wafer waf(thck, part, orient, 0);
0399           if (layer <= layMax1_) {
0400             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
0401             module1[index] = waf;
0402           } else if ((layer <= layMax2_) || global) {
0403             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0404             module2[index] = waf;
0405           } else {
0406             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0407             module3[index] = waf;
0408           }
0409         }
0410       }
0411     }
0412     fInput.close();
0413     std::cout << "Read " << all << " records with " << comments << " comments " << others
0414               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size() << ":"
0415               << module3.size() << " are good and " << bad << " are bad\n"
0416               << std::endl;
0417     //Now write separately for EE, HEsil and HEmix
0418     writeSilicon(outfile1, module1, "EE", global, (debug % 10 > 0));
0419     // Next HEsil part
0420     writeSilicon(outfile2, module2, "HE", global, ((debug / 10) % 10 > 0));
0421     // Finally HEmix part
0422     if (!global)
0423       writeSilicon(outfile3, module3, "HE", global, ((debug / 100) % 10 > 0));
0424   }
0425 }
0426 
0427 void ConvertSiliconV0::writeSilicon(const char* outfile,
0428                                     const std::map<int, wafer>& module,
0429                                     const std::string& tag,
0430                                     const bool& mode,
0431                                     const bool& debug) {
0432   char apost('"');
0433   unsigned int k1(0), k2(0);
0434   std::map<int, wafer>::const_iterator itr;
0435   std::string blank("  ");
0436   std::ofstream fOut(outfile);
0437   std::vector<int> layerStart;
0438   int layer(-1);
0439   if (mode) {
0440     blank = "  ";
0441     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
0442          << " nEntries=" << apost << module.size() << apost << ">";
0443   } else {
0444     blank = "    ";
0445     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
0446          << " nEntries=" << apost << module.size() << apost << ">";
0447   }
0448   for (itr = module.begin(); itr != module.end(); ++itr) {
0449     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0450     if (k1 % 7 == 0)
0451       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
0452     else
0453       fOut << std::setw(8) << itr->first << last;
0454     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
0455       layerStart.emplace_back(k1);
0456       layer = HGCalWaferIndex::waferLayer(itr->first);
0457     }
0458     ++k1;
0459     if (debug)
0460       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
0461                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
0462                 << (itr->second).partial << " O " << (itr->second).orient << std::endl;
0463   }
0464   fOut << "\n" << blank << "</Vector>\n";
0465   if (mode)
0466     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
0467          << apost << " nEntries=" << apost << module.size() << apost << ">";
0468   else
0469     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
0470          << " nEntries=" << apost << module.size() << apost << ">";
0471   for (itr = module.begin(); itr != module.end(); ++itr) {
0472     int property = HGCalProperty::waferProperty(
0473         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
0474     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0475     if (k2 % 10 == 0)
0476       fOut << "\n  " << blank << std::setw(5) << property << last;
0477     else
0478       fOut << std::setw(5) << property << last;
0479     ++k2;
0480   }
0481   fOut << "\n" << blank << "</Vector>\n";
0482   if (mode) {
0483     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
0484          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
0485   } else {
0486     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
0487          << " nEntries=" << apost << layerStart.size() << apost << ">";
0488   }
0489   for (unsigned k3 = 0; k3 < layerStart.size(); ++k3) {
0490     std::string last = ((k3 + 1) == layerStart.size()) ? " " : ",";
0491     if (k3 % 10 == 0)
0492       fOut << "\n  " << blank << std::setw(5) << layerStart[k3] << last;
0493     else
0494       fOut << std::setw(5) << layerStart[k3] << last;
0495   }
0496   fOut << "\n" << blank << "</Vector>\n";
0497   fOut.close();
0498 }
0499 
0500 ConvertSiliconV1::ConvertSiliconV1(int layMax1, int layMax2) : layMax1_(layMax1), layMax2_(layMax2) {}
0501 
0502 void ConvertSiliconV1::convert(
0503     const char* infile, const char* outfile1, const char* outfile2, const char* outfile3, int modeGlobal, int debug) {
0504   std::ifstream fInput(infile);
0505   if (!fInput.good()) {
0506     std::cout << "Cannot open file " << infile << std::endl;
0507   } else {
0508     //First read in all records
0509     char buffer[1024];
0510     std::string thick[4] = {"h120", "l200", "l300", "h200"};
0511     int addType[4] = {HGCalTypes::WaferHD120, HGCalTypes::WaferLD200, HGCalTypes::WaferLD300, HGCalTypes::WaferHD200};
0512     const int partTypeH[6] = {HGCalTypes::WaferFull,
0513                               HGCalTypes::WaferHalf2,
0514                               HGCalTypes::WaferChopTwoM,
0515                               HGCalTypes::WaferSemi2,
0516                               HGCalTypes::WaferSemi2,
0517                               HGCalTypes::WaferFive2};
0518     const int partTypeL[7] = {HGCalTypes::WaferFull,
0519                               HGCalTypes::WaferHalf,
0520                               HGCalTypes::WaferHalf,
0521                               HGCalTypes::WaferSemi,
0522                               HGCalTypes::WaferSemi,
0523                               HGCalTypes::WaferFive,
0524                               HGCalTypes::WaferThree};
0525     std::map<int, wafer> module1, module2, module3;
0526     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0527     int layers(0);
0528     std::vector<int> layer1, layer2, layer3;
0529     bool global = (modeGlobal < 1);
0530     while (fInput.getline(buffer, 1024)) {
0531       ++all;
0532       if (debug % 10 > 1)
0533         std::cout << "[" << all << "] " << buffer << std::endl;
0534       if (buffer[0] == '#') {
0535         ++comments;
0536       } else {
0537         ++others;
0538         std::vector<std::string> items = splitString(std::string(buffer));
0539         if (others == 1) {
0540           layers = std::atoi(items[0].c_str());
0541         } else if (others == 2) {
0542           if (items.size() == static_cast<unsigned int>(layers)) {
0543             for (int k = 0; k < layers; ++k) {
0544               int ltype = std::atoi(items[k].c_str());
0545               if (k < layMax1_) {
0546                 layer1.emplace_back(ltype);
0547               } else if ((k < layMax2_) || global) {
0548                 layer2.emplace_back(ltype);
0549               } else {
0550                 layer3.emplace_back(ltype);
0551               }
0552             }
0553           } else {
0554             ++bad;
0555           }
0556         } else if (items.size() != 8) {
0557           ++bad;
0558         } else {
0559           ++good;
0560           int layer = std::atoi(items[0].c_str());
0561           int waferU = std::atoi(items[6].c_str());
0562           int waferV = std::atoi(items[7].c_str());
0563           int thck = static_cast<int>(std::find(thick, thick + 4, items[2]) - thick);
0564           int part = std::atoi(items[1].c_str());
0565           if ((thck < 4) && (part >= 0)) {
0566             if ((addType[thck] == HGCalTypes::WaferHD120) || (addType[thck] == HGCalTypes::WaferHD200))
0567               part = partTypeH[part];
0568             else
0569               part = partTypeL[part];
0570           }
0571           int orient = std::atoi(items[5].c_str());
0572           wafer waf(thck, part, orient, 0);
0573           if (layer <= layMax1_) {
0574             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
0575             module1[index] = waf;
0576           } else if ((layer <= layMax2_) || global) {
0577             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0578             module2[index] = waf;
0579           } else {
0580             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0581             module3[index] = waf;
0582           }
0583         }
0584       }
0585     }
0586     fInput.close();
0587     std::cout << "Read " << all << " records with " << comments << " comments " << others
0588               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size() << ":"
0589               << module3.size() << " are good and " << bad << " are bad and with " << layers << " layers\n";
0590     std::cout << "\nThere are " << layer1.size() << " of types:" << std::endl;
0591     for (const auto& l : layer1)
0592       std::cout << " " << l;
0593     std::cout << "\nThere are " << layer2.size() << " of types:" << std::endl;
0594     for (const auto& l : layer2)
0595       std::cout << " " << l;
0596     if (layer3.size() > 0) {
0597       std::cout << "\nThere are " << layer3.size() << " of types:" << std::endl;
0598       for (const auto& l : layer3)
0599         std::cout << " " << l;
0600     }
0601     std::cout << std::endl << std::endl;
0602 
0603     //Now write separately for EE, HEsil and HEmix
0604     writeSilicon(outfile1, layer1, module1, "EE", global, (debug % 10 > 0));
0605     // Next HEsil part
0606     writeSilicon(outfile2, layer2, module2, "HE", global, ((debug / 10) % 10 > 0));
0607     // Finally HEmix part
0608     if (!global)
0609       writeSilicon(outfile3, layer3, module3, "HE", global, ((debug / 100) % 10 > 0));
0610   }
0611 }
0612 
0613 void ConvertSiliconV1::writeSilicon(const char* outfile,
0614                                     const std::vector<int>& layers,
0615                                     const std::map<int, wafer>& module,
0616                                     const std::string& tag,
0617                                     const bool& mode,
0618                                     const bool& debug) {
0619   char apost('"');
0620   unsigned int k0(0), k1(0), k2(0);
0621   std::map<int, wafer>::const_iterator itr;
0622   std::string blank = (mode) ? "  " : "    ";
0623   std::ofstream fOut(outfile);
0624   std::vector<int> layerStart;
0625   int layer(-1);
0626   if (mode)
0627     fOut << blank << "<Vector name=" << apost << "LayerTypes" << tag << apost << " type=" << apost << "numeric" << apost
0628          << " nEntries=" << apost << layers.size() << apost << ">";
0629   else
0630     fOut << blank << "<Vector name=" << apost << "LayerTypes" << apost << " type=" << apost << "numeric" << apost
0631          << " nEntries=" << apost << layers.size() << apost << ">";
0632   for (const auto& l : layers) {
0633     std::string last = ((k0 + 1) == layers.size()) ? " " : ",";
0634     if (k0 % 20 == 0)
0635       fOut << "\n  " << blank << std::setw(2) << l << last;
0636     else
0637       fOut << std::setw(2) << l << last;
0638     ++k0;
0639   }
0640   fOut << "\n" << blank << "</Vector>\n";
0641   if (mode) {
0642     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
0643          << " nEntries=" << apost << module.size() << apost << ">";
0644   } else {
0645     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
0646          << " nEntries=" << apost << module.size() << apost << ">";
0647   }
0648   for (itr = module.begin(); itr != module.end(); ++itr) {
0649     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0650     if (k1 % 7 == 0)
0651       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
0652     else
0653       fOut << std::setw(8) << itr->first << last;
0654     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
0655       layerStart.emplace_back(k1);
0656       layer = HGCalWaferIndex::waferLayer(itr->first);
0657     }
0658     ++k1;
0659     if (debug)
0660       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
0661                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
0662                 << (itr->second).partial << " O " << (itr->second).orient << std::endl;
0663   }
0664   fOut << "\n" << blank << "</Vector>\n";
0665   if (mode)
0666     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
0667          << apost << " nEntries=" << apost << module.size() << apost << ">";
0668   else
0669     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
0670          << " nEntries=" << apost << module.size() << apost << ">";
0671   for (itr = module.begin(); itr != module.end(); ++itr) {
0672     int property = HGCalProperty::waferProperty(
0673         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
0674     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0675     if (k2 % 10 == 0)
0676       fOut << "\n  " << blank << std::setw(5) << property << last;
0677     else
0678       fOut << std::setw(5) << property << last;
0679     ++k2;
0680   }
0681   fOut << "\n" << blank << "</Vector>\n";
0682   if (mode) {
0683     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
0684          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
0685   } else {
0686     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
0687          << " nEntries=" << apost << layerStart.size() << apost << ">";
0688   }
0689   for (unsigned k3 = 0; k3 < layerStart.size(); ++k3) {
0690     std::string last = ((k3 + 1) == layerStart.size()) ? " " : ",";
0691     if (k3 % 10 == 0)
0692       fOut << "\n  " << blank << std::setw(5) << layerStart[k3] << last;
0693     else
0694       fOut << std::setw(5) << layerStart[k3] << last;
0695   }
0696   fOut << "\n" << blank << "</Vector>\n";
0697   fOut.close();
0698 }
0699 
0700 ConvertSiliconV2::ConvertSiliconV2(unsigned int layMax1, unsigned int layMax2, unsigned int layMax3)
0701     : layMax1_(layMax1), layMax2_(layMax2), layMax3_(layMax3) {}
0702 
0703 void ConvertSiliconV2::convert(
0704     const char* infile, const char* outfile1, const char* outfile2, const char* outfile3, int modeGlobal, int debug) {
0705   std::ifstream fInput(infile);
0706   if (!fInput.good()) {
0707     std::cout << "Cannot open file " << infile << std::endl;
0708   } else {
0709     //First read in all records
0710     char buffer[1024];
0711     const int thksize = 4;
0712     std::string thick[thksize] = {"h120", "l200", "l300", "h200"};
0713     int addType[thksize] = {
0714         HGCalTypes::WaferHD120, HGCalTypes::WaferLD200, HGCalTypes::WaferLD300, HGCalTypes::WaferHD200};
0715     const int partTypeH[6] = {HGCalTypes::WaferFull,
0716                               HGCalTypes::WaferHDTop,
0717                               HGCalTypes::WaferHDBottom,
0718                               HGCalTypes::WaferHDLeft,
0719                               HGCalTypes::WaferHDRight,
0720                               HGCalTypes::WaferHDFive};
0721     const int partTypeL[7] = {HGCalTypes::WaferFull,
0722                               HGCalTypes::WaferLDTop,
0723                               HGCalTypes::WaferLDBottom,
0724                               HGCalTypes::WaferLDLeft,
0725                               HGCalTypes::WaferLDRight,
0726                               HGCalTypes::WaferLDFive,
0727                               HGCalTypes::WaferLDThree};
0728     const unsigned int cassetteEE(12), cassetteHE(24);
0729     std::map<int, wafer> module1, module2, module3;
0730     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0731     unsigned int layers(layMax3_);
0732     std::vector<layerInfo> layer1, layer2, layer3;
0733     int cminEE(-1), cmaxEE(-1), cminHE1(-1), cmaxHE1(-1), cminHE2(-1), cmaxHE2(-1);
0734     bool global = (modeGlobal < 1);
0735     while (fInput.getline(buffer, 1024)) {
0736       ++all;
0737       if (debug % 10 > 1)
0738         std::cout << "[" << all << "] " << buffer << std::endl;
0739       if (buffer[0] == '#') {
0740         ++comments;
0741       } else {
0742         ++others;
0743         std::vector<std::string> items = splitString(std::string(buffer));
0744         if (others <= layMax3_) {
0745           unsigned int cassettes = (others <= layMax1_) ? cassetteEE : cassetteHE;
0746           if (items.size() < (cassettes + 2)) {
0747             ++bad;
0748           } else {
0749             int layer = std::atoi(items[0].c_str());
0750             int type = std::atoi(items[1].c_str());
0751             std::vector<double> dR;
0752             for (unsigned int k = 0; k < cassettes; ++k)
0753               dR.emplace_back(std::atof(items[k + 2].c_str()));
0754             layerInfo ltype(layer, type, dR);
0755             if (others <= layMax1_) {
0756               layer1.emplace_back(ltype);
0757             } else if ((others <= layMax2_) || global) {
0758               layer2.emplace_back(ltype);
0759             } else {
0760               layer3.emplace_back(ltype);
0761             }
0762           }
0763         } else if (items.size() != 9) {
0764           ++bad;
0765         } else {
0766           ++good;
0767           unsigned int layer = std::atoi(items[0].c_str());
0768           int waferU = std::atoi(items[6].c_str());
0769           int waferV = std::atoi(items[7].c_str());
0770           int cassette = std::atoi(items[8].c_str());
0771           int thck = static_cast<int>(std::find(thick, thick + thksize, items[2]) - thick);
0772           int part = std::atoi(items[1].c_str());
0773           if ((thck <= thksize) && (part >= 0)) {
0774             if ((addType[thck] == HGCalTypes::WaferHD120) || (addType[thck] == HGCalTypes::WaferHD200))
0775               part = partTypeH[part];
0776             else
0777               part = partTypeL[part];
0778           }
0779           int orient = std::atoi(items[5].c_str());
0780           wafer waf(thck, part, orient, cassette);
0781           if (layer <= layMax1_) {
0782             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
0783             module1[index] = waf;
0784             if ((cminEE < 0) || (cassette < cminEE))
0785               cminEE = cassette;
0786             if ((cmaxEE < 0) || (cassette > cmaxEE))
0787               cmaxEE = cassette;
0788           } else if ((layer <= layMax2_) || global) {
0789             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0790             module2[index] = waf;
0791             if ((cminHE1 < 0) || (cassette < cminHE1))
0792               cminHE1 = cassette;
0793             if ((cmaxHE1 < 0) || (cassette > cmaxHE1))
0794               cmaxHE1 = cassette;
0795           } else {
0796             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0797             module3[index] = waf;
0798             if ((cminHE2 < 0) || (cassette < cminHE2))
0799               cminHE2 = cassette;
0800             if ((cmaxHE2 < 0) || (cassette > cmaxHE2))
0801               cmaxHE2 = cassette;
0802           }
0803         }
0804       }
0805     }
0806     fInput.close();
0807     std::cout << "Read " << all << " records with " << comments << " comments " << others
0808               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size() << ":"
0809               << module3.size() << " are good and " << bad << " are bad and with " << layers << " layers\n";
0810     std::cout << "\nThere are " << layer1.size() << " of types:" << std::endl;
0811     for (const auto& l : layer1) {
0812       std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
0813       for (unsigned int k = 0; k < l.deltaR.size(); ++k)
0814         std::cout << ": " << l.deltaR[k];
0815       std::cout << std::endl;
0816     }
0817     std::cout << "\nThere are " << layer2.size() << " of types:" << std::endl;
0818     for (const auto& l : layer2) {
0819       std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
0820       for (unsigned int k = 0; k < l.deltaR.size(); ++k)
0821         std::cout << ": " << l.deltaR[k];
0822       std::cout << std::endl;
0823     }
0824     if (layer3.size() > 0) {
0825       std::cout << "\nThere are " << layer3.size() << " of types:" << std::endl;
0826       for (const auto& l : layer3) {
0827         std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
0828         for (unsigned int k = 0; k < l.deltaR.size(); ++k)
0829           std::cout << ": " << l.deltaR[k];
0830         std::cout << std::endl;
0831       }
0832     }
0833     std::cout << "\nMinimum and Maximum Cassette #'s:: EE: " << cminEE << ":" << cmaxEE << " HEF: " << cminHE1 << ":"
0834               << cmaxHE1 << " HEB: " << cminHE2 << ":" << cmaxHE2 << std::endl;
0835     std::cout << std::endl << std::endl;
0836 
0837     //Now write separately for EE, HEsil and HEmix
0838     writeSilicon(outfile1, cassetteEE, layer1, module1, "EE", global, (debug % 10 > 0));
0839     // Next HEsil part
0840     writeSilicon(outfile2, cassetteHE, layer2, module2, "HE", global, ((debug / 10) % 10 > 0));
0841     // Finally HEmix part
0842     if (!global)
0843       writeSilicon(outfile3, cassetteHE, layer3, module3, "HE", global, ((debug / 100) % 10 > 0));
0844   }
0845 }
0846 
0847 void ConvertSiliconV2::writeSilicon(const char* outfile,
0848                                     const unsigned int cassettes,
0849                                     const std::vector<layerInfo>& layers,
0850                                     const std::map<int, wafer>& module,
0851                                     const std::string& tag,
0852                                     const bool& mode,
0853                                     const bool& debug) {
0854   char apost('"');
0855   unsigned int k0(0), k1(0), k2(0), k3(0);
0856   std::map<int, wafer>::const_iterator itr;
0857   std::string blank = (mode) ? "  " : "    ";
0858   std::ofstream fOut(outfile);
0859   std::vector<int> layerStart;
0860   int layer(-1);
0861   if (mode) {
0862     fOut << blank << "<Vector name=" << apost << "LayerTypes" << tag << apost << " type=" << apost << "numeric" << apost
0863          << " nEntries=" << apost << layers.size() << apost << ">";
0864   } else {
0865     fOut << blank << "<Vector name=" << apost << "LayerTypes" << apost << " type=" << apost << "numeric" << apost
0866          << " nEntries=" << apost << layers.size() << apost << ">";
0867   }
0868   for (const auto& l : layers) {
0869     std::string last = ((k0 + 1) == layers.size()) ? " " : ",";
0870     if (k0 % 20 == 0)
0871       fOut << "\n  " << blank << std::setw(2) << l.type << last;
0872     else
0873       fOut << std::setw(2) << l.type << last;
0874     ++k0;
0875   }
0876   fOut << "\n" << blank << "</Vector>\n";
0877   if (mode) {
0878     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
0879          << " nEntries=" << apost << module.size() << apost << ">";
0880   } else {
0881     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
0882          << " nEntries=" << apost << module.size() << apost << ">";
0883   }
0884   for (itr = module.begin(); itr != module.end(); ++itr) {
0885     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0886     if (k1 % 7 == 0)
0887       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
0888     else
0889       fOut << std::setw(8) << itr->first << last;
0890     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
0891       layerStart.emplace_back(k1);
0892       layer = HGCalWaferIndex::waferLayer(itr->first);
0893     }
0894     ++k1;
0895     if (debug)
0896       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
0897                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
0898                 << (itr->second).partial << " O " << (itr->second).orient << " C " << (itr->second).cassette
0899                 << " Property "
0900                 << HGCalProperty::waferProperty(
0901                        (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette)
0902                 << std::endl;
0903   }
0904   fOut << "\n" << blank << "</Vector>\n";
0905   if (mode)
0906     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
0907          << apost << " nEntries=" << apost << module.size() << apost << ">";
0908   else
0909     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
0910          << " nEntries=" << apost << module.size() << apost << ">";
0911   for (itr = module.begin(); itr != module.end(); ++itr) {
0912     int property = HGCalProperty::waferProperty(
0913         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
0914     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0915     if (k2 % 8 == 0)
0916       fOut << "\n  " << blank << std::setw(7) << property << last;
0917     else
0918       fOut << std::setw(7) << property << last;
0919     ++k2;
0920   }
0921   fOut << "\n" << blank << "</Vector>\n";
0922   if (mode) {
0923     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
0924          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
0925   } else {
0926     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
0927          << " nEntries=" << apost << layerStart.size() << apost << ">";
0928   }
0929   for (unsigned k = 0; k < layerStart.size(); ++k) {
0930     std::string last = ((k + 1) == layerStart.size()) ? " " : ",";
0931     if (k % 10 == 0)
0932       fOut << "\n  " << blank << std::setw(5) << layerStart[k] << last;
0933     else
0934       fOut << std::setw(5) << layerStart[k] << last;
0935   }
0936   fOut << "\n" << blank << "</Vector>\n";
0937   unsigned int csize = cassettes * layers.size();
0938   if (mode) {
0939     fOut << blank << "<Vector name=" << apost << "CassetteShift" << tag << apost << " type=" << apost << "numeric"
0940          << apost << " nEntries=" << apost << csize << apost << ">";
0941   } else {
0942     fOut << blank << "<Vector name=" << apost << "CassetteShift" << apost << " type=" << apost << "numeric" << apost
0943          << " nEntries=" << apost << csize << apost << ">";
0944   }
0945   for (const auto& l : layers) {
0946     ++k3;
0947     for (unsigned int k = 0; k < cassettes; ++k) {
0948       std::string last = ((k3 == layers.size()) && ((k + 1) == cassettes)) ? "*mm" : "*mm,";
0949       if ((k % 6) == 0)
0950         fOut << "\n  " << blank << std::setw(9) << l.deltaR[k] << last;
0951       else
0952         fOut << std::setw(9) << l.deltaR[k] << last;
0953     }
0954   }
0955   fOut << "\n" << blank << "</Vector>\n";
0956   fOut.close();
0957 }
0958 ConvertScintillator::ConvertScintillator(int layMin, int cassette) : layMin_(layMin), cassette_(cassette) {}
0959 
0960 void ConvertScintillator::convert(const char* infile, const char* outfile1, const char* outfile2, int debug) {
0961   std::ifstream fInput(infile);
0962   if (!fInput.good()) {
0963     std::cout << "Cannot open file " << infile << std::endl;
0964   } else {
0965     //First read in all records
0966     char buffer[1024];
0967     std::string types[2] = {"c", "m"};
0968     std::map<int, tile> module;
0969     std::map<int, std::pair<double, double> > ringR;
0970     std::map<int, std::pair<int, int> > layerRing;
0971     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0972     int lmin(99), lmax(0);
0973     while (fInput.getline(buffer, 1024)) {
0974       ++all;
0975       if (buffer[0] == '#') {
0976         ++comments;
0977       } else {
0978         ++others;
0979         std::vector<std::string> items = splitString(std::string(buffer));
0980         if (items.size() != 10) {
0981           ++bad;
0982         } else {
0983           ++good;
0984           int layer, ring, hex1, hex2, hex3, hex4;
0985           float rstart, rend, sipm;
0986           sscanf(
0987               buffer, "%d %d %f %f %f %X %X %X %X", &layer, &ring, &rstart, &rend, &sipm, &hex1, &hex2, &hex3, &hex4);
0988           int type = static_cast<int>(std::find(types, types + 1, items[9]) - types);
0989           if (layer > layMin_) {
0990             tile tl(sipm, type, HGCalProperty::kHGCalTilePhis, hex1, hex2, hex3, hex4, 0, 0);
0991             int index = HGCalTileIndex::tileIndex(layer - layMin_, ring + 1, 0);
0992             module[index] = tl;
0993             lmin = std::min((layer - layMin_), lmin);
0994             lmax = std::max((layer - layMin_), lmax);
0995             ringR[ring] = std::pair<double, double>(rstart, rend);
0996             if (layerRing.find(layer) == layerRing.end()) {
0997               layerRing[layer] = std::pair<int, int>(ring, ring);
0998             } else {
0999               int rmin = std::min(ring, layerRing[layer].first);
1000               int rmax = std::max(ring, layerRing[layer].second);
1001               layerRing[layer] = std::pair<int, int>(rmin, rmax);
1002             }
1003           }
1004         }
1005       }
1006     }
1007     fInput.close();
1008     std::cout << "Read " << all << " records with " << comments << " comments " << others
1009               << " non-comment records out of which " << good << ":" << module.size() << ":" << ringR.size() << ":"
1010               << layerRing.size() << " are good and " << bad << " are bad\n"
1011               << std::endl;
1012 
1013     //Now write the content in the first file
1014     std::ofstream fOut(outfile1);
1015     char apost('"');
1016     unsigned int l1(0), l2(0);
1017     std::map<int, std::pair<double, double> >::const_iterator it1;
1018     fOut << "  <Vector name=" << apost << "TileRMin" << apost << " type=" << apost << "numeric" << apost
1019          << " nEntries=" << apost << ringR.size() << apost << ">";
1020     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1021       std::string last = ((l1 + 1) == ringR.size()) ? " " : ",";
1022       if (l1 % 6 == 0)
1023         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1024       else
1025         fOut << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1026       ++l1;
1027     }
1028     fOut << "\n  </Vector>\n";
1029     fOut << "  <Vector name=" << apost << "TileRMax" << apost << " type=" << apost << "numeric" << apost
1030          << " nEntries=" << apost << ringR.size() << apost << ">";
1031     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1032       std::string last = ((l2 + 1) == ringR.size()) ? " " : ",";
1033       if (l2 % 6 == 0)
1034         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1035       else
1036         fOut << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1037       ++l2;
1038     }
1039     fOut << "\n  </Vector>\n";
1040 
1041     unsigned int l3(0), l4(0);
1042     std::map<int, std::pair<int, int> >::const_iterator it2;
1043     fOut << "  <Vector name=" << apost << "TileRingMin" << apost << " type=" << apost << "numeric" << apost
1044          << " nEntries=" << apost << layerRing.size() << apost << ">";
1045     for (it2 = layerRing.begin(); it2 != layerRing.end(); ++it2) {
1046       std::string last = ((l3 + 1) == layerRing.size()) ? " " : ",";
1047       if (l3 % 14 == 0)
1048         fOut << "\n    " << std::setw(4) << (it2->second).first << last;
1049       else
1050         fOut << std::setw(4) << (it2->second).first << last;
1051       ++l3;
1052     }
1053     fOut << "\n  </Vector>\n";
1054     fOut << "  <Vector name=" << apost << "TileRingMax" << apost << " type=" << apost << "numeric" << apost
1055          << " nEntries=" << apost << layerRing.size() << apost << ">";
1056     for (it2 = layerRing.begin(); it2 != layerRing.end(); ++it2) {
1057       std::string last = ((l4 + 1) == layerRing.size()) ? " " : ",";
1058       if (l4 % 14 == 0)
1059         fOut << "\n    " << std::setw(4) << (it2->second).second << last;
1060       else
1061         fOut << std::setw(4) << (it2->second).second << last;
1062       ++l4;
1063     }
1064     fOut << "\n  </Vector>\n";
1065 
1066     unsigned int k1(0), k2(0), k3(0), k4(0), k5(0), k6(0);
1067     std::map<int, tile>::const_iterator itr;
1068     fOut << "  <Vector name=" << apost << "TileIndex" << apost << " type=" << apost << "numeric" << apost
1069          << " nEntries=" << apost << module.size() << apost << ">";
1070     for (itr = module.begin(); itr != module.end(); ++itr) {
1071       std::string last = ((k1 + 1) == module.size()) ? " " : ",";
1072       if (k1 % 7 == 0)
1073         fOut << "\n    " << std::setw(8) << itr->first << last;
1074       else
1075         fOut << std::setw(8) << itr->first << last;
1076       ++k1;
1077       if ((debug % 10) > 0)
1078         std::cout << "Tile " << HGCalTileIndex::tileLayer(itr->first) << ":" << HGCalTileIndex::tileRing(itr->first)
1079                   << " Type " << (itr->second).type << " Area " << (itr->second).sipm << std::hex << " HEX "
1080                   << (itr->second).hex[0] << " " << (itr->second).hex[1] << " " << (itr->second).hex[2] << " "
1081                   << (itr->second).hex[3] << std::dec << "\n";
1082     }
1083     fOut << "\n  </Vector>\n";
1084     fOut << "  <Vector name=" << apost << "TileProperty" << apost << " type=" << apost << "numeric" << apost
1085          << " nEntries=" << apost << module.size() << apost << ">";
1086     for (itr = module.begin(); itr != module.end(); ++itr) {
1087       std::string last = ((k2 + 1) == module.size()) ? " " : ",";
1088       int property = HGCalTileIndex::tileProperty((itr->second).type, (itr->second).sipm);
1089       if (k2 % 15 == 0)
1090         fOut << "\n    " << std::setw(3) << property << last;
1091       else
1092         fOut << std::setw(3) << property << last;
1093       ++k2;
1094     }
1095     fOut << "\n  </Vector>\n";
1096     fOut << "  <Vector name=" << apost << "TileHEX1" << apost << " type=" << apost << "numeric" << apost
1097          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
1098     for (itr = module.begin(); itr != module.end(); ++itr) {
1099       std::string last = ((k3 + 1) == module.size()) ? " " : ",";
1100       if (k3 % 6 == 0)
1101         fOut << "\n     0x" << (itr->second).hex[0] << last;
1102       else
1103         fOut << " 0x" << (itr->second).hex[0] << last;
1104       ++k3;
1105     }
1106     fOut << "\n  </Vector>\n" << std::dec;
1107     fOut << "  <Vector name=" << apost << "TileHEX2" << apost << " type=" << apost << "numeric" << apost
1108          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
1109     for (itr = module.begin(); itr != module.end(); ++itr) {
1110       std::string last = ((k4 + 1) == module.size()) ? " " : ",";
1111       if (k4 % 6 == 0)
1112         fOut << "\n     0x" << (itr->second).hex[1] << last;
1113       else
1114         fOut << " 0x" << (itr->second).hex[1] << last;
1115       ++k4;
1116     }
1117     fOut << "\n  </Vector>\n" << std::dec;
1118     fOut << "  <Vector name=" << apost << "TileHEX3" << apost << " type=" << apost << "numeric" << apost
1119          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
1120     for (itr = module.begin(); itr != module.end(); ++itr) {
1121       std::string last = ((k5 + 1) == module.size()) ? " " : ",";
1122       if (k5 % 6 == 0)
1123         fOut << "\n     0x" << (itr->second).hex[2] << last;
1124       else
1125         fOut << " 0x" << (itr->second).hex[2] << last;
1126       ++k5;
1127     }
1128     fOut << "\n  </Vector>\n" << std::dec;
1129     fOut << "  <Vector name=" << apost << "TileHEX4" << apost << " type=" << apost << "numeric" << apost
1130          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
1131     for (itr = module.begin(); itr != module.end(); ++itr) {
1132       std::string last = ((k6 + 1) == module.size()) ? " " : ",";
1133       if (k6 % 6 == 0)
1134         fOut << "\n     0x" << (itr->second).hex[3] << last;
1135       else
1136         fOut << " 0x" << (itr->second).hex[3] << last;
1137       ++k6;
1138     }
1139     fOut << "\n  </Vector>\n" << std::dec;
1140     fOut.close();
1141 
1142     //Now write for the second file
1143     makeTitle(outfile2, module, ringR, lmin, lmax, (((debug / 10) % 10) > 0));
1144   }
1145 }
1146 
1147 void ConvertScintillator::makeTitle(const char* outfile,
1148                                     const std::map<int, tile>& module,
1149                                     const std::map<int, std::pair<double, double> >& ringR,
1150                                     int lmin,
1151                                     int lmax,
1152                                     bool debug) {
1153   const int zside = 1;
1154   const int phiCassette = 24;
1155   std::vector<tileZone> zones;
1156   for (int layer = lmin; layer <= lmax; ++layer) {
1157     tileZone tile0;
1158     int kk, irmin, irmax;
1159     for (int phi = 1; phi <= HGCalProperty::kHGCalTilePhis; ++phi) {
1160       kk = irmin = irmax = 0;
1161       for (std::map<int, tile>::const_iterator itr = module.begin(); itr != module.end(); ++itr) {
1162         if ((HGCalTileIndex::tileLayer(itr->first) == layer) &&
1163             HGCalTileIndex::tileExist((itr->second).hex, zside, phi)) {
1164           int ir = HGCalTileIndex::tileRing(itr->first);
1165           if (kk == 0) {
1166             irmin = irmax = ir;
1167           } else {
1168             irmax = ir;
1169           }
1170           ++kk;
1171         }
1172       }
1173       if (debug)
1174         std::cout << "Layer|Phi|Ring " << layer << ":" << phi << ":" << irmin << ":" << irmax << std::endl;
1175       if (phi == 1) {
1176         tile0.layer = layer;
1177         tile0.rmin = irmin;
1178         tile0.rmax = irmax;
1179         tile0.phimin = phi;
1180         tile0.phimax = phi;
1181         tile0.cassette = (cassette_ == 0) ? 0 : 1;
1182       } else if ((tile0.rmin != irmin) || (tile0.rmax != irmax)) {
1183         if (cassette_ != 0) {
1184           if (tile0.cassette * phiCassette < tile0.phimax) {
1185             do {
1186               int phimax = tile0.phimax;
1187               tile0.phimax = tile0.cassette * phiCassette;
1188               zones.push_back(tile0);
1189               tile0.phimin = tile0.phimax + 1;
1190               tile0.phimax = phimax;
1191               ++tile0.cassette;
1192             } while (tile0.cassette * phiCassette < tile0.phimax);
1193           }
1194         }
1195         zones.push_back(tile0);
1196         int cassette = (cassette_ == 0) ? 0 : (1 + ((phi - 1) / phiCassette));
1197         tile0.layer = layer;
1198         tile0.rmin = irmin;
1199         tile0.rmax = irmax;
1200         tile0.phimin = phi;
1201         tile0.phimax = phi;
1202         tile0.cassette = cassette;
1203         if (phi == HGCalProperty::kHGCalTilePhis)
1204           zones.push_back(tile0);
1205       } else {
1206         tile0.phimax = phi;
1207         if (phi == HGCalProperty::kHGCalTilePhis)
1208           zones.push_back(tile0);
1209       }
1210     }
1211   }
1212 
1213   int nmax = zones.size();
1214   if (debug) {
1215     std::cout << "\nA total of " << nmax << " zones " << std::endl;
1216     for (int k = 0; k < nmax; ++k)
1217       std::cout << "[" << k << "] Layer " << zones[k].layer << " Ring " << zones[k].rmin << ":" << zones[k].rmax
1218                 << " phi " << zones[k].phimin << ":" << zones[k].phimax << " Cassette " << zones[k].cassette
1219                 << std::endl;
1220   }
1221   if (nmax > 0) {
1222     std::ofstream fout(outfile);
1223     char apost('"');
1224     unsigned int l1(0), l2(0);
1225     std::map<int, std::pair<double, double> >::const_iterator it1;
1226     fout << "  <Vector name=" << apost << "TileRMin" << apost << " type=" << apost << "numeric" << apost
1227          << " nEntries=" << apost << ringR.size() << apost << ">";
1228     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1229       std::string last = ((l1 + 1) == ringR.size()) ? " " : ",";
1230       if (l1 % 6 == 0)
1231         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1232       else
1233         fout << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1234       ++l1;
1235     }
1236     fout << "\n  </Vector>\n";
1237     fout << "  <Vector name=" << apost << "TileRMax" << apost << " type=" << apost << "numeric" << apost
1238          << " nEntries=" << apost << ringR.size() << apost << ">";
1239     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1240       std::string last = ((l2 + 1) == ringR.size()) ? " " : ",";
1241       if (l2 % 6 == 0)
1242         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1243       else
1244         fout << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1245       ++l2;
1246     }
1247     fout << "\n  </Vector>\n";
1248     fout << "  <Vector name=" << apost << "TileLayerRings" << apost << " type=" << apost << "numeric" << apost
1249          << " nEntries=" << apost << nmax << apost << ">";
1250     if (debug)
1251       std::cout << "  <Vector name=" << apost << "TileLayerRings" << apost << " type=" << apost << "numeric" << apost
1252                 << " nEntries=" << apost << nmax << apost << ">";
1253     for (int k = 0; k < nmax; ++k) {
1254       std::string last = ((k + 1) == nmax) ? " " : ",";
1255       int lyr1r2 = HGCalTileIndex::tilePack(zones[k].layer, zones[k].rmin, zones[k].rmax);
1256       if (k % 7 == 0) {
1257         fout << "\n    " << std::setw(9) << lyr1r2 << last;
1258         if (debug)
1259           std::cout << "\n    " << std::setw(9) << lyr1r2 << last;
1260       } else {
1261         fout << std::setw(9) << lyr1r2 << last;
1262         if (debug)
1263           std::cout << std::setw(9) << lyr1r2 << last;
1264       }
1265     }
1266     fout << "\n  </Vector>\n";
1267     if (debug)
1268       std::cout << "\n  </Vector>\n";
1269     int layer = -1;
1270     std::vector<int> layerStart;
1271     fout << "  <Vector name=" << apost << "TilePhiRange" << apost << " type=" << apost << "numeric" << apost
1272          << " nEntries=" << apost << nmax << apost << ">";
1273     if (debug)
1274       std::cout << "  <Vector name=" << apost << "TilePhiRange" << apost << " type=" << apost << "numeric" << apost
1275                 << " nEntries=" << apost << nmax << apost << ">";
1276     for (int k = 0; k < nmax; ++k) {
1277       std::string last = ((k + 1) == nmax) ? " " : ",";
1278       int f1f2 = HGCalTileIndex::tilePack(zones[k].cassette, zones[k].phimin, zones[k].phimax);
1279       if (k % 7 == 0) {
1280         fout << "\n    " << std::setw(9) << f1f2 << last;
1281         if (debug)
1282           std::cout << "\n    " << std::setw(9) << f1f2 << last;
1283       } else {
1284         fout << std::setw(9) << f1f2 << last;
1285         if (debug)
1286           std::cout << std::setw(9) << f1f2 << last;
1287       }
1288       if (zones[k].layer != layer) {
1289         layerStart.emplace_back(k);
1290         layer = zones[k].layer;
1291       }
1292     }
1293     fout << "\n  </Vector>\n";
1294     if (debug)
1295       std::cout << "\n  </Vector>\n";
1296     fout << "  <Vector name=" << apost << "TileLayerStart" << apost << " type=" << apost << "numeric" << apost
1297          << " nEntries=" << apost << layerStart.size() << apost << ">";
1298     if (debug)
1299       std::cout << "  <Vector name=" << apost << "TileLayerStart" << apost << " type=" << apost << "numeric" << apost
1300                 << " nEntries=" << apost << layerStart.size() << apost << ">";
1301     for (unsigned int k = 0; k < layerStart.size(); ++k) {
1302       std::string last = ((k + 1) == layerStart.size()) ? " " : ",";
1303       if (k % 10 == 0) {
1304         fout << "\n    " << std::setw(5) << layerStart[k] << last;
1305         if (debug)
1306           std::cout << "\n    " << std::setw(5) << layerStart[k] << last;
1307       } else {
1308         fout << std::setw(5) << layerStart[k] << last;
1309         if (debug)
1310           std::cout << std::setw(5) << layerStart[k] << last;
1311       }
1312     }
1313     fout << "\n  </Vector>\n";
1314     if (debug)
1315       std::cout << "\n  </Vector>\n";
1316     fout.close();
1317   }
1318 }
1319 
1320 ConvertScintillatorV1::ConvertScintillatorV1(int layMin, int cassette, int layers)
1321     : layMin_(layMin), cassette_(cassette), layers_(layers) {}
1322 
1323 void ConvertScintillatorV1::convert(const char* infile, const char* outfile1, const char* outfile2, int debug) {
1324   std::ifstream fInput(infile);
1325   if (!fInput.good()) {
1326     std::cout << "Cannot open file " << infile << std::endl;
1327   } else {
1328     //First read in all records
1329     char buffer[1024];
1330     std::string types[2] = {"c", "m"};
1331     std::map<int, std::pair<int, double> > layPhiS;
1332     std::map<int, unsigned int> nPhiS;
1333     std::map<int, tile> module, module6;
1334     std::map<int, std::pair<double, double> > ringR, ringR6;
1335     std::map<int, std::pair<int, int> > layerRing, layerRing6;
1336     unsigned int all(0), comments(0), bad(0), good(0);
1337     int others(0), lmin(99), lmin6(99), lmax(0), lmax6(0);
1338     while (fInput.getline(buffer, 1024)) {
1339       ++all;
1340       if (buffer[0] == '#') {
1341         ++comments;
1342       } else {
1343         ++others;
1344         std::vector<std::string> items = splitString(std::string(buffer));
1345         int layer = std::atoi(items[0].c_str());
1346         if (others <= layers_) {
1347           int nphi = std::atoi(items[1].c_str());
1348           float shift = std::atof(items[2].c_str());
1349           layPhiS[layer] = std::pair<int, float>(nphi, shift);
1350           nPhiS[layer] = (nphi > 300) ? 6 : 4;
1351         } else if (items.size() != (6 + nPhiS[layer])) {
1352           ++bad;
1353         } else {
1354           ++good;
1355           int ring, hex1, hex2, hex3, hex4, hex5, hex6;
1356           float rstart, rend, sipm;
1357           std::string typest;
1358           if (nPhiS[layer] == 4) {
1359             sscanf(
1360                 buffer, "%d %d %f %f %f %X %X %X %X", &layer, &ring, &rstart, &rend, &sipm, &hex1, &hex2, &hex3, &hex4);
1361             hex5 = hex6 = 0;
1362             typest = items[9];
1363           } else {
1364             sscanf(buffer,
1365                    "%d %d %f %f %f %X %X %X %X %X %X",
1366                    &layer,
1367                    &ring,
1368                    &rstart,
1369                    &rend,
1370                    &sipm,
1371                    &hex1,
1372                    &hex2,
1373                    &hex3,
1374                    &hex4,
1375                    &hex5,
1376                    &hex6);
1377             typest = items[11];
1378           }
1379           int type = static_cast<int>(std::find(types, types + 1, typest) - types);
1380           int nphi = layPhiS[layer].first;
1381           if (((debug / 100) % 10) > 0)
1382             std::cout << "Input Layer " << layer << " Ring " << ring << " R " << rstart << ":" << rend << " sipm "
1383                       << sipm << " type " << typest << ":" << type << " HEX " << std::hex << hex1 << " " << hex2 << " "
1384                       << hex3 << " " << hex4 << " " << hex5 << " " << hex6 << std::dec << std::endl;
1385           if (layer > layMin_) {
1386             tile tl(sipm, type, nphi, hex1, hex2, hex3, hex4, hex5, hex6);
1387             int index = HGCalTileIndex::tileIndex(layer - layMin_, ring + 1, 0);
1388             if (nPhiS[layer] == 4) {
1389               lmin = std::min((layer - layMin_), lmin);
1390               lmax = std::max((layer - layMin_), lmax);
1391               module[index] = tl;
1392               ringR[ring] = std::pair<double, double>(rstart, rend);
1393               if (layerRing.find(layer) == layerRing.end()) {
1394                 layerRing[layer] = std::pair<int, int>(ring, ring);
1395               } else {
1396                 lmin = std::min((layer - layMin_), lmin);
1397                 lmax = std::max((layer - layMin_), lmax);
1398                 int rmin = std::min(ring, layerRing[layer].first);
1399                 int rmax = std::max(ring, layerRing[layer].second);
1400                 layerRing[layer] = std::pair<int, int>(rmin, rmax);
1401               }
1402             } else {
1403               module6[index] = tl;
1404               ringR6[ring] = std::pair<double, double>(rstart, rend);
1405               if (layerRing6.find(layer) == layerRing6.end()) {
1406                 layerRing6[layer] = std::pair<int, int>(ring, ring);
1407               } else {
1408                 lmin6 = std::min((layer - layMin_), lmin6);
1409                 lmax6 = std::max((layer - layMin_), lmax6);
1410                 int rmin = std::min(ring, layerRing6[layer].first);
1411                 int rmax = std::max(ring, layerRing6[layer].second);
1412                 layerRing6[layer] = std::pair<int, int>(rmin, rmax);
1413               }
1414             }
1415           }
1416         }
1417       }
1418     }
1419     fInput.close();
1420     std::cout << "Read " << all << " records with " << comments << " comments " << others
1421               << " non-comment records out of which " << good << ":" << module.size() << ":" << ringR.size() << ":"
1422               << layerRing.size() << " are good and " << bad << " are bad\n"
1423               << std::endl;
1424 
1425     //Now write the content in the first file
1426     std::ofstream fOut(outfile1);
1427     char apost('"');
1428     unsigned int l1(0);
1429     std::map<int, std::pair<int, double> >::const_iterator it0;
1430     fOut << "  <Vector name=" << apost << "NPhiLayer" << apost << " type=" << apost << "numeric" << apost
1431          << " nEntries=" << apost << layPhiS.size() << apost << ">";
1432     for (it0 = layPhiS.begin(); it0 != layPhiS.end(); ++it0) {
1433       std::string last = ((l1 + 1) == layPhiS.size()) ? " " : ",";
1434       if (l1 % 10 == 0)
1435         fOut << "\n    " << std::setw(6) << std::setprecision(4) << (it0->second).first << last;
1436       else
1437         fOut << std::setw(6) << std::setprecision(4) << (it0->second).first << last;
1438       ++l1;
1439     }
1440     fOut << "\n  </Vector>\n";
1441     unsigned int l2(0);
1442     fOut << "  <Vector name=" << apost << "ScintRetract" << apost << " type=" << apost << "numeric" << apost
1443          << " nEntries=" << apost << layPhiS.size() << apost << ">";
1444     for (it0 = layPhiS.begin(); it0 != layPhiS.end(); ++it0) {
1445       std::string last = ((l2 + 1) == layPhiS.size()) ? " " : ",";
1446       if (l2 % 6 == 0)
1447         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it0->second).second << "*mm" << last;
1448       else
1449         fOut << std::setw(8) << std::setprecision(6) << (it0->second).second << "*mm" << last;
1450       ++l2;
1451     }
1452     fOut << "\n  </Vector>\n";
1453 
1454     unsigned int l3(0), l4(0), l5(0), l6(0);
1455     std::map<int, std::pair<double, double> >::const_iterator it1;
1456     fOut << "  <Vector name=" << apost << "TileRMin6" << apost << " type=" << apost << "numeric" << apost
1457          << " nEntries=" << apost << ringR6.size() << apost << ">";
1458     for (it1 = ringR6.begin(); it1 != ringR6.end(); ++it1) {
1459       std::string last = ((l3 + 1) == ringR6.size()) ? " " : ",";
1460       if (l3 % 6 == 0)
1461         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1462       else
1463         fOut << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1464       ++l3;
1465     }
1466     fOut << "\n  </Vector>\n";
1467     fOut << "  <Vector name=" << apost << "TileRMax6" << apost << " type=" << apost << "numeric" << apost
1468          << " nEntries=" << apost << ringR6.size() << apost << ">";
1469     for (it1 = ringR6.begin(); it1 != ringR6.end(); ++it1) {
1470       std::string last = ((l4 + 1) == ringR6.size()) ? " " : ",";
1471       if (l4 % 6 == 0)
1472         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1473       else
1474         fOut << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1475       ++l4;
1476     }
1477     fOut << "\n  </Vector>\n";
1478     fOut << "  <Vector name=" << apost << "TileRMin" << apost << " type=" << apost << "numeric" << apost
1479          << " nEntries=" << apost << ringR.size() << apost << ">";
1480     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1481       std::string last = ((l5 + 1) == ringR.size()) ? " " : ",";
1482       if (l5 % 6 == 0)
1483         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1484       else
1485         fOut << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1486       ++l5;
1487     }
1488     fOut << "\n  </Vector>\n";
1489     fOut << "  <Vector name=" << apost << "TileRMax" << apost << " type=" << apost << "numeric" << apost
1490          << " nEntries=" << apost << ringR.size() << apost << ">";
1491     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1492       std::string last = ((l6 + 1) == ringR.size()) ? " " : ",";
1493       if (l6 % 6 == 0)
1494         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1495       else
1496         fOut << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1497       ++l6;
1498     }
1499     fOut << "\n  </Vector>\n";
1500 
1501     unsigned int l7(0), l8(0), l9(0), l10(0);
1502     std::map<int, std::pair<int, int> >::const_iterator it2;
1503     fOut << "  <Vector name=" << apost << "TileRingMin6" << apost << " type=" << apost << "numeric" << apost
1504          << " nEntries=" << apost << layerRing6.size() << apost << ">";
1505     for (it2 = layerRing6.begin(); it2 != layerRing6.end(); ++it2) {
1506       std::string last = ((l7 + 1) == layerRing6.size()) ? " " : ",";
1507       if (l7 % 14 == 0)
1508         fOut << "\n    " << std::setw(4) << (it2->second).first << last;
1509       else
1510         fOut << std::setw(4) << (it2->second).first << last;
1511       ++l7;
1512     }
1513     fOut << "\n  </Vector>\n";
1514     fOut << "  <Vector name=" << apost << "TileRingMax6" << apost << " type=" << apost << "numeric" << apost
1515          << " nEntries=" << apost << layerRing6.size() << apost << ">";
1516     for (it2 = layerRing6.begin(); it2 != layerRing6.end(); ++it2) {
1517       std::string last = ((l8 + 1) == layerRing6.size()) ? " " : ",";
1518       if (l8 % 14 == 0)
1519         fOut << "\n    " << std::setw(4) << (it2->second).second << last;
1520       else
1521         fOut << std::setw(4) << (it2->second).second << last;
1522       ++l8;
1523     }
1524     fOut << "\n  </Vector>\n";
1525     fOut << "  <Vector name=" << apost << "TileRingMin" << apost << " type=" << apost << "numeric" << apost
1526          << " nEntries=" << apost << layerRing.size() << apost << ">";
1527     for (it2 = layerRing.begin(); it2 != layerRing.end(); ++it2) {
1528       std::string last = ((l9 + 1) == layerRing.size()) ? " " : ",";
1529       if (l9 % 14 == 0)
1530         fOut << "\n    " << std::setw(4) << (it2->second).first << last;
1531       else
1532         fOut << std::setw(4) << (it2->second).first << last;
1533       ++l9;
1534     }
1535     fOut << "\n  </Vector>\n";
1536     fOut << "  <Vector name=" << apost << "TileRingMax" << apost << " type=" << apost << "numeric" << apost
1537          << " nEntries=" << apost << layerRing.size() << apost << ">";
1538     for (it2 = layerRing.begin(); it2 != layerRing.end(); ++it2) {
1539       std::string last = ((l10 + 1) == layerRing.size()) ? " " : ",";
1540       if (l10 % 14 == 0)
1541         fOut << "\n    " << std::setw(4) << (it2->second).second << last;
1542       else
1543         fOut << std::setw(4) << (it2->second).second << last;
1544       ++l10;
1545     }
1546     fOut << "\n  </Vector>\n";
1547 
1548     unsigned int k1(0), k2(0), k3(0), k4(0), k5(0), k6(0), k7(0), k8(0);
1549     std::map<int, tile>::const_iterator itr;
1550     fOut << "  <Vector name=" << apost << "TileIndex" << apost << " type=" << apost << "numeric" << apost
1551          << " nEntries=" << apost << (module6.size() + module.size()) << apost << ">";
1552     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1553       std::string last = ",";
1554       if (k1 % 7 == 0)
1555         fOut << "\n    " << std::setw(8) << itr->first << last;
1556       else
1557         fOut << std::setw(8) << itr->first << last;
1558       ++k1;
1559       if ((debug % 10) > 0)
1560         std::cout << "Tile " << HGCalTileIndex::tileLayer(itr->first) << ":" << HGCalTileIndex::tileRing(itr->first)
1561                   << " Type " << (itr->second).type << " Area " << (itr->second).sipm << std::hex << " HEX "
1562                   << (itr->second).hex[0] << " " << (itr->second).hex[1] << " " << (itr->second).hex[2] << " "
1563                   << (itr->second).hex[3] << " " << (itr->second).hex[4] << " " << (itr->second).hex[5] << std::dec
1564                   << "\n";
1565     }
1566     for (itr = module.begin(); itr != module.end(); ++itr) {
1567       std::string last = ((k1 + 1) == (module6.size() + module.size())) ? " " : ",";
1568       if (k1 % 7 == 0)
1569         fOut << "\n    " << std::setw(8) << itr->first << last;
1570       else
1571         fOut << std::setw(8) << itr->first << last;
1572       ++k1;
1573       if ((debug % 10) > 0)
1574         std::cout << "Tile " << HGCalTileIndex::tileLayer(itr->first) << ":" << HGCalTileIndex::tileRing(itr->first)
1575                   << " Type " << (itr->second).type << " Area " << (itr->second).sipm << std::hex << " HEX "
1576                   << (itr->second).hex[0] << " " << (itr->second).hex[1] << " " << (itr->second).hex[2] << " "
1577                   << (itr->second).hex[3] << std::dec << "\n";
1578     }
1579     fOut << "\n  </Vector>\n";
1580     fOut << "  <Vector name=" << apost << "TileProperty" << apost << " type=" << apost << "numeric" << apost
1581          << " nEntries=" << apost << (module6.size() + module.size()) << apost << ">";
1582     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1583       std::string last = ",";
1584       int property = HGCalTileIndex::tileProperty((itr->second).type, (itr->second).sipm);
1585       if (k2 % 15 == 0)
1586         fOut << "\n    " << std::setw(3) << property << last;
1587       else
1588         fOut << std::setw(3) << property << last;
1589       ++k2;
1590     }
1591     for (itr = module.begin(); itr != module.end(); ++itr) {
1592       std::string last = ((k2 + 1) == (module6.size() + module.size())) ? " " : ",";
1593       int property = HGCalTileIndex::tileProperty((itr->second).type, (itr->second).sipm);
1594       if (k2 % 15 == 0)
1595         fOut << "\n    " << std::setw(3) << property << last;
1596       else
1597         fOut << std::setw(3) << property << last;
1598       ++k2;
1599     }
1600     fOut << "\n  </Vector>\n";
1601 
1602     fOut << "  <Vector name=" << apost << "TileHEX1" << apost << " type=" << apost << "numeric" << apost
1603          << " nEntries=" << apost << (module6.size() + module.size()) << apost << ">" << std::hex;
1604     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1605       std::string last = ",";
1606       if (k3 % 6 == 0)
1607         fOut << "\n     0x" << (itr->second).hex[0] << last;
1608       else
1609         fOut << " 0x" << (itr->second).hex[0] << last;
1610       ++k3;
1611     }
1612     for (itr = module.begin(); itr != module.end(); ++itr) {
1613       std::string last = ((k3 + 1) == (module6.size() + module.size())) ? " " : ",";
1614       if (k3 % 6 == 0)
1615         fOut << "\n     0x" << (itr->second).hex[0] << last;
1616       else
1617         fOut << " 0x" << (itr->second).hex[0] << last;
1618       ++k3;
1619     }
1620     fOut << "\n  </Vector>\n" << std::dec;
1621     fOut << "  <Vector name=" << apost << "TileHEX2" << apost << " type=" << apost << "numeric" << apost
1622          << " nEntries=" << apost << (module6.size() + module.size()) << apost << ">" << std::hex;
1623     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1624       std::string last = ",";
1625       if (k4 % 6 == 0)
1626         fOut << "\n     0x" << (itr->second).hex[1] << last;
1627       else
1628         fOut << " 0x" << (itr->second).hex[1] << last;
1629       ++k4;
1630     }
1631     for (itr = module.begin(); itr != module.end(); ++itr) {
1632       std::string last = ((k4 + 1) == (module6.size() + module.size())) ? " " : ",";
1633       if (k4 % 6 == 0)
1634         fOut << "\n     0x" << (itr->second).hex[1] << last;
1635       else
1636         fOut << " 0x" << (itr->second).hex[1] << last;
1637       ++k4;
1638     }
1639     fOut << "\n  </Vector>\n" << std::dec;
1640     fOut << "  <Vector name=" << apost << "TileHEX3" << apost << " type=" << apost << "numeric" << apost
1641          << " nEntries=" << apost << (module6.size() + module.size()) << apost << ">" << std::hex;
1642     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1643       std::string last = ",";
1644       if (k5 % 6 == 0)
1645         fOut << "\n     0x" << (itr->second).hex[2] << last;
1646       else
1647         fOut << " 0x" << (itr->second).hex[2] << last;
1648       ++k5;
1649     }
1650     for (itr = module.begin(); itr != module.end(); ++itr) {
1651       std::string last = ((k5 + 1) == (module6.size() + module.size())) ? " " : ",";
1652       if (k5 % 6 == 0)
1653         fOut << "\n     0x" << (itr->second).hex[2] << last;
1654       else
1655         fOut << " 0x" << (itr->second).hex[2] << last;
1656       ++k5;
1657     }
1658     fOut << "\n  </Vector>\n" << std::dec;
1659     fOut << "  <Vector name=" << apost << "TileHEX4" << apost << " type=" << apost << "numeric" << apost
1660          << " nEntries=" << apost << (module6.size() + module.size()) << apost << ">" << std::hex;
1661     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1662       std::string last = ",";
1663       if (k6 % 6 == 0)
1664         fOut << "\n     0x" << (itr->second).hex[3] << last;
1665       else
1666         fOut << " 0x" << (itr->second).hex[3] << last;
1667       ++k6;
1668     }
1669     for (itr = module.begin(); itr != module.end(); ++itr) {
1670       std::string last = ((k6 + 1) == (module6.size() + module.size())) ? " " : ",";
1671       if (k6 % 6 == 0)
1672         fOut << "\n     0x" << (itr->second).hex[3] << last;
1673       else
1674         fOut << " 0x" << (itr->second).hex[3] << last;
1675       ++k6;
1676     }
1677     fOut << "\n  </Vector>\n" << std::dec;
1678     fOut << "  <Vector name=" << apost << "TileHEX5" << apost << " type=" << apost << "numeric" << apost
1679          << " nEntries=" << apost << module6.size() << apost << ">" << std::hex;
1680     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1681       std::string last = ((k7 + 1) == module6.size()) ? " " : ",";
1682       if (k7 % 6 == 0)
1683         fOut << "\n     0x" << (itr->second).hex[4] << last;
1684       else
1685         fOut << " 0x" << (itr->second).hex[4] << last;
1686       ++k7;
1687     }
1688     fOut << "\n  </Vector>\n" << std::dec;
1689     fOut << "  <Vector name=" << apost << "TileHEX6" << apost << " type=" << apost << "numeric" << apost
1690          << " nEntries=" << apost << module6.size() << apost << ">" << std::hex;
1691     for (itr = module6.begin(); itr != module6.end(); ++itr) {
1692       std::string last = ((k8 + 1) == module6.size()) ? " " : ",";
1693       if (k8 % 6 == 0)
1694         fOut << "\n     0x" << (itr->second).hex[5] << last;
1695       else
1696         fOut << " 0x" << (itr->second).hex[5] << last;
1697       ++k8;
1698     }
1699     fOut << "\n  </Vector>\n" << std::dec;
1700     fOut.close();
1701 
1702     //Now write for the second file
1703     std::ofstream fout(outfile2);
1704     // Write the ring specification first
1705     l1 = 0;
1706     fout << "  <Vector name=" << apost << "NPhiLayer" << apost << " type=" << apost << "numeric" << apost
1707          << " nEntries=" << apost << layPhiS.size() << apost << ">";
1708     for (it0 = layPhiS.begin(); it0 != layPhiS.end(); ++it0) {
1709       std::string last = ((l1 + 1) == layPhiS.size()) ? " " : ",";
1710       if (l1 % 10 == 0)
1711         fout << "\n    " << std::setw(6) << std::setprecision(4) << (it0->second).first << last;
1712       else
1713         fout << std::setw(6) << std::setprecision(4) << (it0->second).first << last;
1714       ++l1;
1715     }
1716     fout << "\n  </Vector>\n";
1717     l2 = 0;
1718     fout << "  <Vector name=" << apost << "ScintRetract" << apost << " type=" << apost << "numeric" << apost
1719          << " nEntries=" << apost << layPhiS.size() << apost << ">";
1720     for (it0 = layPhiS.begin(); it0 != layPhiS.end(); ++it0) {
1721       std::string last = ((l2 + 1) == layPhiS.size()) ? " " : ",";
1722       if (l2 % 6 == 0)
1723         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it0->second).second << "*mm" << last;
1724       else
1725         fout << std::setw(8) << std::setprecision(6) << (it0->second).second << "*mm" << last;
1726       ++l2;
1727     }
1728     fout << "\n  </Vector>\n";
1729     // Now write the remaining parts
1730     makeTitle(
1731         fout, "Tile6", module6, ringR6, lmin6, lmax6, HGCalProperty::kHGCalFineTilePhis, 1, (((debug / 10) % 10) > 0));
1732     makeTitle(fout, "Tile", module, ringR, lmin, lmax, HGCalProperty::kHGCalTilePhis, 0, (((debug / 10) % 10) > 0));
1733     fout.close();
1734   }
1735 }
1736 
1737 void ConvertScintillatorV1::makeTitle(std::ofstream& fout,
1738                                       const char* head,
1739                                       const std::map<int, tile>& module,
1740                                       const std::map<int, std::pair<double, double> >& ringR,
1741                                       int lmin,
1742                                       int lmax,
1743                                       int nphis,
1744                                       int mode,
1745                                       bool debug) {
1746   const int zside = 1;
1747   std::vector<tileZone> zones;
1748   if ((debug % 10) > 0)
1749     std::cout << "makeTile called with Layer:" << lmin << ":" << lmax << " nphi " << nphis << " mode " << mode
1750               << " nmodules " << module.size() << std::endl;
1751   for (int layer = lmin; layer <= lmax; ++layer) {
1752     tileZone tile0;
1753     int kk, irmin, irmax;
1754     for (int phi = 1; phi <= nphis; ++phi) {
1755       kk = irmin = irmax = 0;
1756       for (std::map<int, tile>::const_iterator itr = module.begin(); itr != module.end(); ++itr) {
1757         bool ok(false);
1758         if (((debug / 100) % 10) > 0)
1759           std::cout << " Layer " << HGCalTileIndex::tileLayer(itr->first) << " Ring "
1760                     << HGCalTileIndex::tileRing(itr->first) << std::endl;
1761         if (mode == 0)
1762           ok = HGCalTileIndex::tileExist((itr->second).hex, zside, phi);
1763         else
1764           ok = HGCalTileIndex::tileFineExist((itr->second).hex, zside, phi);
1765         if ((HGCalTileIndex::tileLayer(itr->first) == layer) && ok) {
1766           int ir = HGCalTileIndex::tileRing(itr->first);
1767           if (kk == 0) {
1768             irmin = irmax = ir;
1769           } else {
1770             irmax = ir;
1771           }
1772           ++kk;
1773         }
1774       }
1775       if (((debug / 100) % 10) > 0)
1776         std::cout << "Layer|Phi|Ring " << layer << ":" << phi << ":" << irmin << ":" << irmax << std::endl;
1777       if (phi == 1) {
1778         tile0.layer = layer;
1779         tile0.rmin = irmin;
1780         tile0.rmax = irmax;
1781         tile0.phimin = phi;
1782         tile0.phimax = phi;
1783         tile0.cassette = (cassette_ == 0) ? 0 : 1;
1784       } else if ((tile0.rmin != irmin) || (tile0.rmax != irmax)) {
1785         if (cassette_ != 0) {
1786           if (tile0.cassette * HGCalProperty::kHGCalTilePhisWord < tile0.phimax) {
1787             do {
1788               int phimax = tile0.phimax;
1789               tile0.phimax = tile0.cassette * HGCalProperty::kHGCalTilePhisWord;
1790               zones.push_back(tile0);
1791               tile0.phimin = tile0.phimax + 1;
1792               tile0.phimax = phimax;
1793               ++tile0.cassette;
1794             } while (tile0.cassette * HGCalProperty::kHGCalTilePhisWord < tile0.phimax);
1795           }
1796         }
1797         zones.push_back(tile0);
1798         int cassette = (cassette_ == 0) ? 0 : (1 + ((phi - 1) / HGCalProperty::kHGCalTilePhisWord));
1799         tile0.layer = layer;
1800         tile0.rmin = irmin;
1801         tile0.rmax = irmax;
1802         tile0.phimin = phi;
1803         tile0.phimax = phi;
1804         tile0.cassette = cassette;
1805         if (phi == HGCalProperty::kHGCalTilePhis)
1806           zones.push_back(tile0);
1807       } else {
1808         tile0.phimax = phi;
1809         if (phi == HGCalProperty::kHGCalTilePhis)
1810           zones.push_back(tile0);
1811       }
1812     }
1813   }
1814 
1815   int nmax = zones.size();
1816   if ((debug % 10) > 0) {
1817     std::cout << "\nA total of " << nmax << " zones " << std::endl;
1818     for (int k = 0; k < nmax; ++k)
1819       std::cout << "[" << k << "] Layer " << zones[k].layer << " Ring " << zones[k].rmin << ":" << zones[k].rmax
1820                 << " phi " << zones[k].phimin << ":" << zones[k].phimax << " Cassette " << zones[k].cassette
1821                 << std::endl;
1822   }
1823   if (nmax > 0) {
1824     char apost('"');
1825     unsigned int l1(0), l2(0);
1826     std::map<int, std::pair<double, double> >::const_iterator it1;
1827     fout << "  <Vector name=" << apost << head << "RMin" << apost << " type=" << apost << "numeric" << apost
1828          << " nEntries=" << apost << ringR.size() << apost << ">";
1829     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1830       std::string last = ((l1 + 1) == ringR.size()) ? " " : ",";
1831       if (l1 % 6 == 0)
1832         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1833       else
1834         fout << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1835       ++l1;
1836     }
1837     fout << "\n  </Vector>\n";
1838     fout << "  <Vector name=" << apost << head << "RMax" << apost << " type=" << apost << "numeric" << apost
1839          << " nEntries=" << apost << ringR.size() << apost << ">";
1840     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1841       std::string last = ((l2 + 1) == ringR.size()) ? " " : ",";
1842       if (l2 % 6 == 0)
1843         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1844       else
1845         fout << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1846       ++l2;
1847     }
1848     fout << "\n  </Vector>\n";
1849     fout << "  <Vector name=" << apost << head << "LayerRings" << apost << " type=" << apost << "numeric" << apost
1850          << " nEntries=" << apost << nmax << apost << ">";
1851     if ((debug % 10) > 0)
1852       std::cout << "  <Vector name=" << apost << head << "LayerRings" << apost << " type=" << apost << "numeric"
1853                 << apost << " nEntries=" << apost << nmax << apost << ">";
1854     for (int k = 0; k < nmax; ++k) {
1855       std::string last = ((k + 1) == nmax) ? " " : ",";
1856       int lyr1r2 = HGCalTileIndex::tilePack(zones[k].layer, zones[k].rmin, zones[k].rmax);
1857       if (k % 7 == 0) {
1858         fout << "\n    " << std::setw(9) << lyr1r2 << last;
1859         if ((debug % 10) > 0)
1860           std::cout << "\n    " << std::setw(9) << lyr1r2 << last;
1861       } else {
1862         fout << std::setw(9) << lyr1r2 << last;
1863         if ((debug % 10) > 0)
1864           std::cout << std::setw(9) << lyr1r2 << last;
1865       }
1866     }
1867     fout << "\n  </Vector>\n";
1868     if ((debug % 10) > 0)
1869       std::cout << "\n  </Vector>\n";
1870     int layer = -1;
1871     std::vector<int> layerStart;
1872     fout << "  <Vector name=" << apost << head << "PhiRange" << apost << " type=" << apost << "numeric" << apost
1873          << " nEntries=" << apost << nmax << apost << ">";
1874     if ((debug % 10) > 0)
1875       std::cout << "  <Vector name=" << apost << head << "PhiRange" << apost << " type=" << apost << "numeric" << apost
1876                 << " nEntries=" << apost << nmax << apost << ">";
1877     for (int k = 0; k < nmax; ++k) {
1878       std::string last = ((k + 1) == nmax) ? " " : ",";
1879       int f1f2 = HGCalTileIndex::tilePack(zones[k].cassette, zones[k].phimin, zones[k].phimax);
1880       if (k % 7 == 0) {
1881         fout << "\n    " << std::setw(9) << f1f2 << last;
1882         if ((debug % 10) > 0)
1883           std::cout << "\n    " << std::setw(9) << f1f2 << last;
1884       } else {
1885         fout << std::setw(9) << f1f2 << last;
1886         if ((debug % 10) > 0)
1887           std::cout << std::setw(9) << f1f2 << last;
1888       }
1889       if (zones[k].layer != layer) {
1890         layerStart.emplace_back(k);
1891         layer = zones[k].layer;
1892       }
1893     }
1894     fout << "\n  </Vector>\n";
1895     if ((debug % 10) > 0)
1896       std::cout << "\n  </Vector>\n";
1897     fout << "  <Vector name=" << apost << head << "LayerStart" << apost << " type=" << apost << "numeric" << apost
1898          << " nEntries=" << apost << layerStart.size() << apost << ">";
1899     if ((debug % 10) > 0)
1900       std::cout << "  <Vector name=" << apost << head << "LayerStart" << apost << " type=" << apost << "numeric"
1901                 << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
1902     for (unsigned int k = 0; k < layerStart.size(); ++k) {
1903       std::string last = ((k + 1) == layerStart.size()) ? " " : ",";
1904       if (k % 10 == 0) {
1905         fout << "\n    " << std::setw(5) << layerStart[k] << last;
1906         if ((debug & 10) > 0)
1907           std::cout << "\n    " << std::setw(5) << layerStart[k] << last;
1908       } else {
1909         fout << std::setw(5) << layerStart[k] << last;
1910         if ((debug % 10) > 0)
1911           std::cout << std::setw(5) << layerStart[k] << last;
1912       }
1913     }
1914     fout << "\n  </Vector>\n";
1915     if ((debug & 10) > 0)
1916       std::cout << "\n  </Vector>\n";
1917   }
1918 }
1919 
1920 ConvertNoseV0::ConvertNoseV0(unsigned int layMax1, unsigned int layMax2) : layMax1_(layMax1), layMax2_(layMax2) {
1921   std::cout << "ConvertNoseV0 Iniltailized with " << layMax1_ << ":" << layMax2_ << std::endl;
1922 }
1923 
1924 void ConvertNoseV0::convert(
1925     const char* infile, const char* outfile1, const char* outfile2, int modeGlobal, int caasettesin, int debug) {
1926   std::ifstream fInput(infile);
1927   if (!fInput.good()) {
1928     std::cout << "Cannot open file " << infile << std::endl;
1929   } else {
1930     //First read in all records
1931     char buffer[1024];
1932     const int thksize = 4;
1933     std::string thick[thksize] = {"h120", "l200", "l300", "h200"};
1934     int addType[thksize] = {
1935         HGCalTypes::WaferHD120, HGCalTypes::WaferLD200, HGCalTypes::WaferLD300, HGCalTypes::WaferHD200};
1936     const int partTypeH[6] = {HGCalTypes::WaferFull,
1937                               HGCalTypes::WaferHDTop,
1938                               HGCalTypes::WaferHDBottom,
1939                               HGCalTypes::WaferHDLeft,
1940                               HGCalTypes::WaferHDRight,
1941                               HGCalTypes::WaferHDFive};
1942     const int partTypeL[7] = {HGCalTypes::WaferFull,
1943                               HGCalTypes::WaferLDTop,
1944                               HGCalTypes::WaferLDBottom,
1945                               HGCalTypes::WaferLDLeft,
1946                               HGCalTypes::WaferLDRight,
1947                               HGCalTypes::WaferLDFive,
1948                               HGCalTypes::WaferLDThree};
1949     const unsigned int cassetteEE(12), cassetteHE(24);
1950     std::map<int, wafer> module1, module2;
1951     unsigned int all(0), comments(0), others(0), bad(0), good(0);
1952     unsigned int layers(layMax2_);
1953     std::vector<layerInfo> layer1, layer2;
1954     int cminEE(-1), cmaxEE(-1), cminHE(-1), cmaxHE(-1);
1955     bool global = (modeGlobal < 1);
1956     while (fInput.getline(buffer, 1024)) {
1957       ++all;
1958       if (debug % 10 > 1)
1959         std::cout << "[" << all << "] " << buffer << std::endl;
1960       if (buffer[0] == '#') {
1961         ++comments;
1962       } else {
1963         ++others;
1964         std::vector<std::string> items = splitString(std::string(buffer));
1965         if (others <= layMax2_) {
1966           unsigned int cassettes = (others <= layMax1_) ? cassetteEE : cassetteHE;
1967           if (caasettesin > 0)
1968             cassettes = caasettesin;
1969           if (items.size() < (cassettes + 2)) {
1970             if (debug % 10 > 1)
1971               std::cout << "Size " << items.size() << " expect >= " << (cassettes + 2) << std::endl;
1972             ++bad;
1973           } else {
1974             int layer = std::atoi(items[0].c_str());
1975             int type = std::atoi(items[1].c_str());
1976             std::vector<double> dR;
1977             for (unsigned int k = 0; k < cassettes; ++k)
1978               dR.emplace_back(std::atof(items[k + 2].c_str()));
1979             layerInfo ltype(layer, type, dR);
1980             if (others <= layMax1_) {
1981               layer1.emplace_back(ltype);
1982             } else {
1983               layer2.emplace_back(ltype);
1984             }
1985           }
1986         } else if (items.size() != 9) {
1987           ++bad;
1988         } else {
1989           ++good;
1990           unsigned int layer = std::atoi(items[0].c_str());
1991           int waferU = std::atoi(items[6].c_str());
1992           int waferV = std::atoi(items[7].c_str());
1993           int cassette = std::atoi(items[8].c_str());
1994           int thck = static_cast<int>(std::find(thick, thick + thksize, items[2]) - thick);
1995           int part = std::atoi(items[1].c_str());
1996           if ((thck <= thksize) && (part >= 0)) {
1997             if ((addType[thck] == HGCalTypes::WaferHD120) || (addType[thck] == HGCalTypes::WaferHD200))
1998               part = partTypeH[part];
1999             else
2000               part = partTypeL[part];
2001           }
2002           int orient = std::atoi(items[5].c_str());
2003           wafer waf(thck, part, orient, cassette);
2004           if (layer <= layMax1_) {
2005             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
2006             module1[index] = waf;
2007             if ((cminEE < 0) || (cassette < cminEE))
2008               cminEE = cassette;
2009             if ((cmaxEE < 0) || (cassette > cmaxEE))
2010               cmaxEE = cassette;
2011           } else {
2012             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
2013             module2[index] = waf;
2014             if ((cminHE < 0) || (cassette < cminHE))
2015               cminHE = cassette;
2016             if ((cmaxHE < 0) || (cassette > cmaxHE))
2017               cmaxHE = cassette;
2018           }
2019         }
2020       }
2021     }
2022     fInput.close();
2023     std::cout << "Read " << all << " records with " << comments << " comments " << others
2024               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size()
2025               << " are good and " << bad << " are bad and with " << layers << " layers\n";
2026     std::cout << "\nThere are " << layer1.size() << " of types:" << std::endl;
2027     for (const auto& l : layer1) {
2028       std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
2029       for (unsigned int k = 0; k < l.deltaR.size(); ++k)
2030         std::cout << ": " << l.deltaR[k];
2031       std::cout << std::endl;
2032     }
2033     std::cout << "\nThere are " << layer2.size() << " of types:" << std::endl;
2034     for (const auto& l : layer2) {
2035       std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
2036       for (unsigned int k = 0; k < l.deltaR.size(); ++k)
2037         std::cout << ": " << l.deltaR[k];
2038       std::cout << std::endl;
2039     }
2040     std::cout << "\nMinimum and Maximum Cassette #'s:: EE: " << cminEE << ":" << cmaxEE << " HE: " << cminHE << ":"
2041               << cmaxHE << std::endl;
2042     std::cout << std::endl << std::endl;
2043 
2044     //Now write separately for EE and HE
2045     int csE = (caasettesin > 0) ? caasettesin : cassetteEE;
2046     writeNose(outfile1, csE, layer1, module1, "EE", global, (debug % 10 > 0));
2047     // Next HEsil part
2048     int csH = (caasettesin > 0) ? caasettesin : cassetteHE;
2049     writeNose(outfile2, csH, layer2, module2, "HE", global, ((debug / 10) % 10 > 0));
2050   }
2051 }
2052 
2053 void ConvertNoseV0::writeNose(const char* outfile,
2054                               const unsigned int cassettes,
2055                               const std::vector<layerInfo>& layers,
2056                               const std::map<int, wafer>& module,
2057                               const std::string& tag,
2058                               const bool& mode,
2059                               const bool& debug) {
2060   char apost('"');
2061   unsigned int k0(0), k1(0), k2(0), k3(0);
2062   std::map<int, wafer>::const_iterator itr;
2063   std::string blank = (mode) ? "  " : "    ";
2064   std::ofstream fOut(outfile);
2065   std::vector<int> layerStart;
2066   int layer(-1);
2067   if (mode) {
2068     fOut << blank << "<Vector name=" << apost << "LayerTypes" << tag << apost << " type=" << apost << "numeric" << apost
2069          << " nEntries=" << apost << layers.size() << apost << ">";
2070   } else {
2071     fOut << blank << "<Vector name=" << apost << "LayerTypes" << apost << " type=" << apost << "numeric" << apost
2072          << " nEntries=" << apost << layers.size() << apost << ">";
2073   }
2074   for (const auto& l : layers) {
2075     std::string last = ((k0 + 1) == layers.size()) ? " " : ",";
2076     if (k0 % 20 == 0)
2077       fOut << "\n  " << blank << std::setw(2) << l.type << last;
2078     else
2079       fOut << std::setw(2) << l.type << last;
2080     ++k0;
2081   }
2082   fOut << "\n" << blank << "</Vector>\n";
2083   if (mode) {
2084     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
2085          << " nEntries=" << apost << module.size() << apost << ">";
2086   } else {
2087     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
2088          << " nEntries=" << apost << module.size() << apost << ">";
2089   }
2090   for (itr = module.begin(); itr != module.end(); ++itr) {
2091     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
2092     if (k1 % 7 == 0)
2093       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
2094     else
2095       fOut << std::setw(8) << itr->first << last;
2096     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
2097       layerStart.emplace_back(k1);
2098       layer = HGCalWaferIndex::waferLayer(itr->first);
2099     }
2100     ++k1;
2101     if (debug)
2102       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
2103                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
2104                 << (itr->second).partial << " O " << (itr->second).orient << " C " << (itr->second).cassette
2105                 << " Property "
2106                 << HGCalProperty::waferProperty(
2107                        (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette)
2108                 << std::endl;
2109   }
2110   fOut << "\n" << blank << "</Vector>\n";
2111   if (mode)
2112     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
2113          << apost << " nEntries=" << apost << module.size() << apost << ">";
2114   else
2115     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
2116          << " nEntries=" << apost << module.size() << apost << ">";
2117   for (itr = module.begin(); itr != module.end(); ++itr) {
2118     int property = HGCalProperty::waferProperty(
2119         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
2120     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
2121     if (k2 % 8 == 0)
2122       fOut << "\n  " << blank << std::setw(7) << property << last;
2123     else
2124       fOut << std::setw(7) << property << last;
2125     ++k2;
2126   }
2127   fOut << "\n" << blank << "</Vector>\n";
2128   if (mode) {
2129     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
2130          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
2131   } else {
2132     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
2133          << " nEntries=" << apost << layerStart.size() << apost << ">";
2134   }
2135   for (unsigned k = 0; k < layerStart.size(); ++k) {
2136     std::string last = ((k + 1) == layerStart.size()) ? " " : ",";
2137     if (k % 10 == 0)
2138       fOut << "\n  " << blank << std::setw(5) << layerStart[k] << last;
2139     else
2140       fOut << std::setw(5) << layerStart[k] << last;
2141   }
2142   fOut << "\n" << blank << "</Vector>\n";
2143   unsigned int csize = cassettes * layers.size();
2144   if (mode) {
2145     fOut << blank << "<Vector name=" << apost << "CassetteShift" << tag << apost << " type=" << apost << "numeric"
2146          << apost << " nEntries=" << apost << csize << apost << ">";
2147   } else {
2148     fOut << blank << "<Vector name=" << apost << "CassetteShift" << apost << " type=" << apost << "numeric" << apost
2149          << " nEntries=" << apost << csize << apost << ">";
2150   }
2151   for (const auto& l : layers) {
2152     ++k3;
2153     for (unsigned int k = 0; k < cassettes; ++k) {
2154       std::string last = ((k3 == layers.size()) && ((k + 1) == cassettes)) ? "*mm" : "*mm,";
2155       if ((k % 6) == 0)
2156         fOut << "\n  " << blank << std::setw(9) << l.deltaR[k] << last;
2157       else
2158         fOut << std::setw(9) << l.deltaR[k] << last;
2159     }
2160   }
2161   fOut << "\n" << blank << "</Vector>\n";
2162   fOut.close();
2163 }
2164 
2165 ConvertCassetteV0::ConvertCassetteV0(int layMin, int layMax, int cassette)
2166     : layMin_(layMin), layMax_(layMax), cassette_(cassette) {
2167   std::cout << "ConvertCassetteV0:: Initializerd with layers " << layMin_ << ":" << layMax_ << " cassette " << cassette_
2168             << std::endl;
2169 }
2170 
2171 void ConvertCassetteV0::convert(const char* infile, const char* outfile, int debug) {
2172   std::ifstream fInput(infile);
2173   constexpr int layTotal = 47;
2174   int all(0), comments(0), good(0), bad(0), others(0), final(0);
2175   if (!fInput.good()) {
2176     std::cout << "Cannot open file " << infile << std::endl;
2177   } else {
2178     std::ofstream fOut(outfile);
2179     char buffer[1024];
2180     while (fInput.getline(buffer, 1024)) {
2181       ++all;
2182       if ((debug % 10) > 1)
2183         std::cout << "[" << all << "] " << buffer << std::endl;
2184       if (buffer[0] == '#') {
2185         ++comments;
2186       } else {
2187         ++others;
2188         std::vector<std::string> items = splitString(std::string(buffer));
2189         if (others <= layTotal) {
2190           if ((others >= layMin_) && (others <= layMax_)) {
2191             ++final;
2192             fOut << buffer << std::endl;
2193             if ((debug % 10) > 0)
2194               std::cout << buffer << std::endl;
2195           }
2196         } else if (items.size() != 9) {
2197           ++bad;
2198         } else {
2199           ++good;
2200           int layer = std::atoi(items[0].c_str());
2201           int cassette = std::atoi(items[8].c_str());
2202           if ((layer >= layMin_) && (layer <= layMax_) && (cassette == cassette_)) {
2203             ++final;
2204             fOut << buffer << std::endl;
2205             if ((debug % 10) > 0)
2206               std::cout << buffer << std::endl;
2207           }
2208         }
2209       }
2210     }
2211     fOut.close();
2212     fInput.close();
2213   }
2214   std::cout << "Reads a total of " << all << " lines from " << infile << " out of which " << comments << ":" << others
2215             << " are comment:non-comment lines " << good << ":" << bad << " are good:bad lines for wafers" << std::endl
2216             << "Writes " << final << " lines in output file " << outfile << std::endl;
2217 }