Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:33

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