Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-04-22 06:27:28

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