Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-14 00:30:29

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 //  infile   (const char*)   Input file from Katya (modified by Chris)
0024 //                           containing layer #. ring #, start and end
0025 //                           of ring radius, SiPM size, 4 hexadecimal
0026 //                           words specifying if the phi location in
0027 //                           120-degree sector is filled, scintillator
0028 //                           type (c: cast, m: mould)
0029 //  outfile1 (const char*)   Output file for the part to be inserted in
0030 //                           the global section
0031 //  outfile2 (const char*)   Output file for the part to be inserted in
0032 //                           the ddAlgorithm part
0033 //  laymin   (int)           First layer number of the HE part
0034 //                           (28 for versions: V14, V15; 26 for V16, V17)
0035 //  cassette (int)           Cassettes are used in geometry definition
0036 //                           (0 if none, 1 if 12 cassettes are used)
0037 //  debug    (int)           Two digit integer to set debug for each
0038 //                           of the outputs
0039 //
0040 //////////////////////////////////////////////////////////////////////////////
0041 
0042 #include "Geometry/HGCalCommonData/interface/HGCalProperty.h"
0043 #include "Geometry/HGCalCommonData/interface/HGCalTileIndex.h"
0044 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0045 #include "Geometry/HGCalCommonData/interface/HGCalWaferIndex.h"
0046 #include <algorithm>
0047 #include <cstdlib>
0048 #include <fstream>
0049 #include <iomanip>
0050 #include <iostream>
0051 #include <map>
0052 #include <string>
0053 #include <vector>
0054 
0055 struct wafer {
0056   int thick, partial, orient, cassette;
0057   wafer(int t = 0, int p = 0, int o = 0, int c = 0) : thick(t), partial(p), orient(o), cassette(c){};
0058 };
0059 
0060 struct layerInfo {
0061   int layer, type;
0062   std::vector<double> deltaR;
0063   layerInfo(int ly, int ty, std::vector<double> dR) : layer(ly), type(ty), deltaR(dR){};
0064 };
0065 
0066 struct tile {
0067   double sipm;
0068   int type, hex[4];
0069   tile(double s = 0, int h1 = 0, int h2 = 0, int h3 = 0, int h4 = 0, int t = 0) : sipm(s), type(t) {
0070     hex[0] = h1;
0071     hex[1] = h2;
0072     hex[2] = h3;
0073     hex[3] = h4;
0074   };
0075 };
0076 
0077 struct tileZone {
0078   tileZone(int l0 = 0, int r0 = 0, int r1 = 0, int f0 = 0, int f1 = 0, int c0 = 0)
0079       : layer(l0), rmin(r0), rmax(r1), phimin(f0), phimax(f1), cassette(c0){};
0080   int layer, rmin, rmax, phimin, phimax, cassette;
0081 };
0082 
0083 std::vector<std::string> splitString(const std::string& fLine);
0084 
0085 class ConvertSiliconV0 {
0086 public:
0087   ConvertSiliconV0(int layMax1 = 28, int layMax2 = 36);
0088   void convert(const char* infile,
0089                const char* outfile1,
0090                const char* outfile2,
0091                const char* outfile3,
0092                int modeGlobal = 0,
0093                int debug = 0);
0094 
0095 private:
0096   void writeSilicon(const char*, const std::map<int, wafer>&, const std::string&, const bool&, const bool&);
0097 
0098   const int layMax1_, layMax2_;
0099 };
0100 
0101 class ConvertSiliconV1 {
0102 public:
0103   ConvertSiliconV1(int layMax1 = 26, int layMax2 = 33);
0104   void convert(const char* infile,
0105                const char* outfile1,
0106                const char* outfile2,
0107                const char* outfile3,
0108                int modeGlobal = 0,
0109                int debug = 0);
0110 
0111 private:
0112   void writeSilicon(
0113       const char*, const std::vector<int>&, const std::map<int, wafer>&, const std::string&, const bool&, const bool&);
0114 
0115   const int layMax1_, layMax2_;
0116 };
0117 
0118 class ConvertSiliconV2 {
0119 public:
0120   ConvertSiliconV2(unsigned int layMax1 = 26, unsigned int layMax2 = 33, unsigned int layMax3 = 47);
0121   void convert(const char* infile,
0122                const char* outfile1,
0123                const char* outfile2,
0124                const char* outfile3,
0125                int modeGlobal = 0,
0126                int debug = 0);
0127 
0128 private:
0129   void writeSilicon(const char*,
0130                     const unsigned int,
0131                     const std::vector<layerInfo>&,
0132                     const std::map<int, wafer>&,
0133                     const std::string&,
0134                     const bool&,
0135                     const bool&);
0136 
0137   const unsigned int layMax1_, layMax2_, layMax3_;
0138 };
0139 
0140 class ConvertScintillator {
0141 public:
0142   ConvertScintillator(int layMin = 26, int cassette = 0);
0143   void convert(const char*, const char*, const char*, int debug = 0);
0144 
0145 private:
0146   void makeTitle(const char* outfile,
0147                  const std::map<int, tile>& module,
0148                  const std::map<int, std::pair<double, double> >& ringR,
0149                  int lmin,
0150                  int lmax,
0151                  bool debug);
0152 
0153   const int layMin_;
0154   const int cassette_;
0155 };
0156 
0157 int main(int argc, char* argv[]) {
0158   if (argc < 7) {
0159     std::cout << "Please give a minimum of 7 arguments \n"
0160               << "mode (0, 1:silicon; 2:scintillator)\n"
0161               << "input file name\n"
0162               << "output file name\n"
0163               << "for silicon 4 additional parameters:\n"
0164               << "  second output file name\n"
0165               << "  third output file name\n"
0166               << "  output mode (0: gobal; 1: local)\n"
0167               << "  debug (three digis to set debug for each output)\n"
0168               << "for scintillator 4 additional parameters after the first 3\n"
0169               << "  second output file name\n"
0170               << "  number of layers in the EE section: 28 or 26\n"
0171               << "  flag to utilize cassette partition or not\n"
0172               << "  debug flag\n"
0173               << std::endl;
0174     return 0;
0175   }
0176 
0177   int mode = std::atoi(argv[1]);
0178   const char* infile = argv[2];
0179   if (mode <= 2) {
0180     const char* outfile1 = argv[3];
0181     const char* outfile2 = argv[4];
0182     const char* outfile3 = argv[5];
0183     int modeGlobal = atoi(argv[6]);
0184     int debug = atoi(argv[7]);
0185     std::cout << "Calls ConvertSilicon for i/p file " << infile << " o/p files " << outfile1 << ":" << outfile2 << ":"
0186               << outfile3 << " Mode " << modeGlobal << " Debug " << debug << std::endl;
0187     if (mode == 0) {
0188       ConvertSiliconV0 c1;
0189       c1.convert(infile, outfile1, outfile2, outfile3, modeGlobal, debug);
0190     } else if (mode == 1) {
0191       ConvertSiliconV1 c1;
0192       c1.convert(infile, outfile1, outfile2, outfile3, modeGlobal, debug);
0193     } else {
0194       ConvertSiliconV2 c1;
0195       c1.convert(infile, outfile1, outfile2, outfile3, modeGlobal, debug);
0196     }
0197   } else {
0198     const char* outfile1 = argv[3];
0199     const char* outfile2 = argv[4];
0200     int laymin = atoi(argv[5]);
0201     int cassette = atoi(argv[6]);
0202     int debug = atoi(argv[7]);
0203     std::cout << "Calls ConvertScintillator for i/p file " << infile << " o/p files " << outfile1 << ":" << outfile2
0204               << " Laymin " << laymin << " Cassette " << cassette << " Debug " << debug << std::endl;
0205     ConvertScintillator c1(laymin, cassette);
0206     c1.convert(infile, outfile1, outfile2, debug);
0207   }
0208   return 0;
0209 }
0210 
0211 std::vector<std::string> splitString(const std::string& fLine) {
0212   std::vector<std::string> result;
0213   int start = 0;
0214   bool empty = true;
0215   for (unsigned i = 0; i <= fLine.size(); i++) {
0216     if (fLine[i] == ' ' || i == fLine.size()) {
0217       if (!empty) {
0218         std::string item(fLine, start, i - start);
0219         result.push_back(item);
0220         empty = true;
0221       }
0222       start = i + 1;
0223     } else {
0224       if (empty)
0225         empty = false;
0226     }
0227   }
0228   return result;
0229 }
0230 
0231 ConvertSiliconV0::ConvertSiliconV0(int layMax1, int layMax2) : layMax1_(layMax1), layMax2_(layMax2) {}
0232 
0233 void ConvertSiliconV0::convert(
0234     const char* infile, const char* outfile1, const char* outfile2, const char* outfile3, int modeGlobal, int debug) {
0235   std::ifstream fInput(infile);
0236   if (!fInput.good()) {
0237     std::cout << "Cannot open file " << infile << std::endl;
0238   } else {
0239     //First read in all records
0240     char buffer[1024];
0241     std::string thick[3] = {"120", "200", "300"};
0242     std::string partial[8] = {"F", "b", "g", "gm", "a", "d", "dm", "c"};
0243     std::map<int, wafer> module1, module2, module3;
0244     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0245     bool global = (modeGlobal < 1);
0246     while (fInput.getline(buffer, 1024)) {
0247       ++all;
0248       if (debug % 10 > 1)
0249         std::cout << "[" << all << "] " << buffer << std::endl;
0250       if (buffer[0] == '#') {
0251         ++comments;
0252       } else {
0253         ++others;
0254         std::vector<std::string> items = splitString(std::string(buffer));
0255         if (items.size() != 8) {
0256           ++bad;
0257         } else {
0258           ++good;
0259           int layer = std::atoi(items[0].c_str());
0260           int waferU = std::atoi(items[6].c_str());
0261           int waferV = std::atoi(items[7].c_str());
0262           int thck = static_cast<int>(std::find(thick, thick + 3, items[2]) - thick);
0263           int part = static_cast<int>(std::find(partial, partial + 8, items[1]) - partial);
0264           int orient = std::atoi(items[5].c_str());
0265           wafer waf(thck, part, orient, 0);
0266           if (layer <= layMax1_) {
0267             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
0268             module1[index] = waf;
0269           } else if ((layer <= layMax2_) || global) {
0270             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0271             module2[index] = waf;
0272           } else {
0273             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0274             module3[index] = waf;
0275           }
0276         }
0277       }
0278     }
0279     fInput.close();
0280     std::cout << "Read " << all << " records with " << comments << " comments " << others
0281               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size() << ":"
0282               << module3.size() << " are good and " << bad << " are bad\n"
0283               << std::endl;
0284     //Now write separately for EE, HEsil and HEmix
0285     writeSilicon(outfile1, module1, "EE", global, (debug % 10 > 0));
0286     // Next HEsil part
0287     writeSilicon(outfile2, module2, "HE", global, ((debug / 10) % 10 > 0));
0288     // Finally HEmix part
0289     if (!global)
0290       writeSilicon(outfile3, module3, "HE", global, ((debug / 100) % 10 > 0));
0291   }
0292 }
0293 
0294 void ConvertSiliconV0::writeSilicon(const char* outfile,
0295                                     const std::map<int, wafer>& module,
0296                                     const std::string& tag,
0297                                     const bool& mode,
0298                                     const bool& debug) {
0299   char apost('"');
0300   unsigned int k1(0), k2(0);
0301   std::map<int, wafer>::const_iterator itr;
0302   std::string blank("  ");
0303   std::ofstream fOut(outfile);
0304   std::vector<int> layerStart;
0305   int layer(-1);
0306   if (mode) {
0307     blank = "  ";
0308     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
0309          << " nEntries=" << apost << module.size() << apost << ">";
0310   } else {
0311     blank = "    ";
0312     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
0313          << " nEntries=" << apost << module.size() << apost << ">";
0314   }
0315   for (itr = module.begin(); itr != module.end(); ++itr) {
0316     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0317     if (k1 % 7 == 0)
0318       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
0319     else
0320       fOut << std::setw(8) << itr->first << last;
0321     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
0322       layerStart.emplace_back(k1);
0323       layer = HGCalWaferIndex::waferLayer(itr->first);
0324     }
0325     ++k1;
0326     if (debug)
0327       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
0328                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
0329                 << (itr->second).partial << " O " << (itr->second).orient << std::endl;
0330   }
0331   fOut << "\n" << blank << "</Vector>\n";
0332   if (mode)
0333     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
0334          << apost << " nEntries=" << apost << module.size() << apost << ">";
0335   else
0336     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
0337          << " nEntries=" << apost << module.size() << apost << ">";
0338   for (itr = module.begin(); itr != module.end(); ++itr) {
0339     int property = HGCalProperty::waferProperty(
0340         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
0341     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0342     if (k2 % 10 == 0)
0343       fOut << "\n  " << blank << std::setw(5) << property << last;
0344     else
0345       fOut << std::setw(5) << property << last;
0346     ++k2;
0347   }
0348   fOut << "\n" << blank << "</Vector>\n";
0349   if (mode) {
0350     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
0351          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
0352   } else {
0353     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
0354          << " nEntries=" << apost << layerStart.size() << apost << ">";
0355   }
0356   for (unsigned k3 = 0; k3 < layerStart.size(); ++k3) {
0357     std::string last = ((k3 + 1) == layerStart.size()) ? " " : ",";
0358     if (k3 % 10 == 0)
0359       fOut << "\n  " << blank << std::setw(5) << layerStart[k3] << last;
0360     else
0361       fOut << std::setw(5) << layerStart[k3] << last;
0362   }
0363   fOut << "\n" << blank << "</Vector>\n";
0364   fOut.close();
0365 }
0366 
0367 ConvertSiliconV1::ConvertSiliconV1(int layMax1, int layMax2) : layMax1_(layMax1), layMax2_(layMax2) {}
0368 
0369 void ConvertSiliconV1::convert(
0370     const char* infile, const char* outfile1, const char* outfile2, const char* outfile3, int modeGlobal, int debug) {
0371   std::ifstream fInput(infile);
0372   if (!fInput.good()) {
0373     std::cout << "Cannot open file " << infile << std::endl;
0374   } else {
0375     //First read in all records
0376     char buffer[1024];
0377     std::string thick[4] = {"h120", "l200", "l300", "h200"};
0378     int addType[4] = {HGCalTypes::WaferFineThin,
0379                       HGCalTypes::WaferCoarseThin,
0380                       HGCalTypes::WaferCoarseThick,
0381                       HGCalTypes::WaferFineThick};
0382     const int partTypeH[6] = {HGCalTypes::WaferFull,
0383                               HGCalTypes::WaferHalf2,
0384                               HGCalTypes::WaferChopTwoM,
0385                               HGCalTypes::WaferSemi2,
0386                               HGCalTypes::WaferSemi2,
0387                               HGCalTypes::WaferFive2};
0388     const int partTypeL[7] = {HGCalTypes::WaferFull,
0389                               HGCalTypes::WaferHalf,
0390                               HGCalTypes::WaferHalf,
0391                               HGCalTypes::WaferSemi,
0392                               HGCalTypes::WaferSemi,
0393                               HGCalTypes::WaferFive,
0394                               HGCalTypes::WaferThree};
0395     std::map<int, wafer> module1, module2, module3;
0396     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0397     int layers(0);
0398     std::vector<int> layer1, layer2, layer3;
0399     bool global = (modeGlobal < 1);
0400     while (fInput.getline(buffer, 1024)) {
0401       ++all;
0402       if (debug % 10 > 1)
0403         std::cout << "[" << all << "] " << buffer << std::endl;
0404       if (buffer[0] == '#') {
0405         ++comments;
0406       } else {
0407         ++others;
0408         std::vector<std::string> items = splitString(std::string(buffer));
0409         if (others == 1) {
0410           layers = std::atoi(items[0].c_str());
0411         } else if (others == 2) {
0412           if (items.size() == static_cast<unsigned int>(layers)) {
0413             for (int k = 0; k < layers; ++k) {
0414               int ltype = std::atoi(items[k].c_str());
0415               if (k < layMax1_) {
0416                 layer1.emplace_back(ltype);
0417               } else if ((k < layMax2_) || global) {
0418                 layer2.emplace_back(ltype);
0419               } else {
0420                 layer3.emplace_back(ltype);
0421               }
0422             }
0423           } else {
0424             ++bad;
0425           }
0426         } else if (items.size() != 8) {
0427           ++bad;
0428         } else {
0429           ++good;
0430           int layer = std::atoi(items[0].c_str());
0431           int waferU = std::atoi(items[6].c_str());
0432           int waferV = std::atoi(items[7].c_str());
0433           int thck = static_cast<int>(std::find(thick, thick + 4, items[2]) - thick);
0434           int part = std::atoi(items[1].c_str());
0435           if ((thck < 4) && (part >= 0)) {
0436             if ((addType[thck] == HGCalTypes::WaferFineThin) || (addType[thck] == HGCalTypes::WaferFineThick))
0437               part = partTypeH[part];
0438             else
0439               part = partTypeL[part];
0440           }
0441           int orient = std::atoi(items[5].c_str());
0442           wafer waf(thck, part, orient, 0);
0443           if (layer <= layMax1_) {
0444             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
0445             module1[index] = waf;
0446           } else if ((layer <= layMax2_) || global) {
0447             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0448             module2[index] = waf;
0449           } else {
0450             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0451             module3[index] = waf;
0452           }
0453         }
0454       }
0455     }
0456     fInput.close();
0457     std::cout << "Read " << all << " records with " << comments << " comments " << others
0458               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size() << ":"
0459               << module3.size() << " are good and " << bad << " are bad and with " << layers << " layers\n";
0460     std::cout << "\nThere are " << layer1.size() << " of types:" << std::endl;
0461     for (const auto& l : layer1)
0462       std::cout << " " << l;
0463     std::cout << "\nThere are " << layer2.size() << " of types:" << std::endl;
0464     for (const auto& l : layer2)
0465       std::cout << " " << l;
0466     if (layer3.size() > 0) {
0467       std::cout << "\nThere are " << layer3.size() << " of types:" << std::endl;
0468       for (const auto& l : layer3)
0469         std::cout << " " << l;
0470     }
0471     std::cout << std::endl << std::endl;
0472 
0473     //Now write separately for EE, HEsil and HEmix
0474     writeSilicon(outfile1, layer1, module1, "EE", global, (debug % 10 > 0));
0475     // Next HEsil part
0476     writeSilicon(outfile2, layer2, module2, "HE", global, ((debug / 10) % 10 > 0));
0477     // Finally HEmix part
0478     if (!global)
0479       writeSilicon(outfile3, layer3, module3, "HE", global, ((debug / 100) % 10 > 0));
0480   }
0481 }
0482 
0483 void ConvertSiliconV1::writeSilicon(const char* outfile,
0484                                     const std::vector<int>& layers,
0485                                     const std::map<int, wafer>& module,
0486                                     const std::string& tag,
0487                                     const bool& mode,
0488                                     const bool& debug) {
0489   char apost('"');
0490   unsigned int k0(0), k1(0), k2(0);
0491   std::map<int, wafer>::const_iterator itr;
0492   std::string blank = (mode) ? "  " : "    ";
0493   std::ofstream fOut(outfile);
0494   std::vector<int> layerStart;
0495   int layer(-1);
0496   if (mode)
0497     fOut << blank << "<Vector name=" << apost << "LayerTypes" << tag << apost << " type=" << apost << "numeric" << apost
0498          << " nEntries=" << apost << layers.size() << apost << ">";
0499   else
0500     fOut << blank << "<Vector name=" << apost << "LayerTypes" << apost << " type=" << apost << "numeric" << apost
0501          << " nEntries=" << apost << layers.size() << apost << ">";
0502   for (const auto& l : layers) {
0503     std::string last = ((k0 + 1) == layers.size()) ? " " : ",";
0504     if (k0 % 20 == 0)
0505       fOut << "\n  " << blank << std::setw(2) << l << last;
0506     else
0507       fOut << std::setw(2) << l << last;
0508     ++k0;
0509   }
0510   fOut << "\n" << blank << "</Vector>\n";
0511   if (mode) {
0512     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
0513          << " nEntries=" << apost << module.size() << apost << ">";
0514   } else {
0515     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
0516          << " nEntries=" << apost << module.size() << apost << ">";
0517   }
0518   for (itr = module.begin(); itr != module.end(); ++itr) {
0519     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0520     if (k1 % 7 == 0)
0521       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
0522     else
0523       fOut << std::setw(8) << itr->first << last;
0524     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
0525       layerStart.emplace_back(k1);
0526       layer = HGCalWaferIndex::waferLayer(itr->first);
0527     }
0528     ++k1;
0529     if (debug)
0530       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
0531                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
0532                 << (itr->second).partial << " O " << (itr->second).orient << std::endl;
0533   }
0534   fOut << "\n" << blank << "</Vector>\n";
0535   if (mode)
0536     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
0537          << apost << " nEntries=" << apost << module.size() << apost << ">";
0538   else
0539     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
0540          << " nEntries=" << apost << module.size() << apost << ">";
0541   for (itr = module.begin(); itr != module.end(); ++itr) {
0542     int property = HGCalProperty::waferProperty(
0543         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
0544     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0545     if (k2 % 10 == 0)
0546       fOut << "\n  " << blank << std::setw(5) << property << last;
0547     else
0548       fOut << std::setw(5) << property << last;
0549     ++k2;
0550   }
0551   fOut << "\n" << blank << "</Vector>\n";
0552   if (mode) {
0553     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
0554          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
0555   } else {
0556     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
0557          << " nEntries=" << apost << layerStart.size() << apost << ">";
0558   }
0559   for (unsigned k3 = 0; k3 < layerStart.size(); ++k3) {
0560     std::string last = ((k3 + 1) == layerStart.size()) ? " " : ",";
0561     if (k3 % 10 == 0)
0562       fOut << "\n  " << blank << std::setw(5) << layerStart[k3] << last;
0563     else
0564       fOut << std::setw(5) << layerStart[k3] << last;
0565   }
0566   fOut << "\n" << blank << "</Vector>\n";
0567   fOut.close();
0568 }
0569 
0570 ConvertSiliconV2::ConvertSiliconV2(unsigned int layMax1, unsigned int layMax2, unsigned int layMax3)
0571     : layMax1_(layMax1), layMax2_(layMax2), layMax3_(layMax3) {}
0572 
0573 void ConvertSiliconV2::convert(
0574     const char* infile, const char* outfile1, const char* outfile2, const char* outfile3, int modeGlobal, int debug) {
0575   std::ifstream fInput(infile);
0576   if (!fInput.good()) {
0577     std::cout << "Cannot open file " << infile << std::endl;
0578   } else {
0579     //First read in all records
0580     char buffer[1024];
0581     const int thksize = 4;
0582     std::string thick[thksize] = {"h120", "l200", "l300", "h200"};
0583     int addType[thksize] = {HGCalTypes::WaferFineThin,
0584                             HGCalTypes::WaferCoarseThin,
0585                             HGCalTypes::WaferCoarseThick,
0586                             HGCalTypes::WaferFineThick};
0587     const int partTypeH[6] = {HGCalTypes::WaferFull,
0588                               HGCalTypes::WaferHDTop,
0589                               HGCalTypes::WaferHDBottom,
0590                               HGCalTypes::WaferHDLeft,
0591                               HGCalTypes::WaferHDRight,
0592                               HGCalTypes::WaferHDFive};
0593     const int partTypeL[7] = {HGCalTypes::WaferFull,
0594                               HGCalTypes::WaferLDTop,
0595                               HGCalTypes::WaferLDBottom,
0596                               HGCalTypes::WaferLDLeft,
0597                               HGCalTypes::WaferLDRight,
0598                               HGCalTypes::WaferLDFive,
0599                               HGCalTypes::WaferLDThree};
0600     const unsigned int cassetteEE(12), cassetteHE(24);
0601     std::map<int, wafer> module1, module2, module3;
0602     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0603     unsigned int layers(layMax3_);
0604     std::vector<layerInfo> layer1, layer2, layer3;
0605     int cminEE(-1), cmaxEE(-1), cminHE1(-1), cmaxHE1(-1), cminHE2(-1), cmaxHE2(-1);
0606     bool global = (modeGlobal < 1);
0607     while (fInput.getline(buffer, 1024)) {
0608       ++all;
0609       if (debug % 10 > 1)
0610         std::cout << "[" << all << "] " << buffer << std::endl;
0611       if (buffer[0] == '#') {
0612         ++comments;
0613       } else {
0614         ++others;
0615         std::vector<std::string> items = splitString(std::string(buffer));
0616         if (others <= layMax3_) {
0617           unsigned int cassettes = (others <= layMax1_) ? cassetteEE : cassetteHE;
0618           if (items.size() < (cassettes + 2)) {
0619             ++bad;
0620           } else {
0621             int layer = std::atoi(items[0].c_str());
0622             int type = std::atoi(items[1].c_str());
0623             std::vector<double> dR;
0624             for (unsigned int k = 0; k < cassettes; ++k)
0625               dR.emplace_back(std::atof(items[k + 2].c_str()));
0626             layerInfo ltype(layer, type, dR);
0627             if (others <= layMax1_) {
0628               layer1.emplace_back(ltype);
0629             } else if ((others <= layMax2_) || global) {
0630               layer2.emplace_back(ltype);
0631             } else {
0632               layer3.emplace_back(ltype);
0633             }
0634           }
0635         } else if (items.size() != 9) {
0636           ++bad;
0637         } else {
0638           ++good;
0639           unsigned int layer = std::atoi(items[0].c_str());
0640           int waferU = std::atoi(items[6].c_str());
0641           int waferV = std::atoi(items[7].c_str());
0642           int cassette = std::atoi(items[8].c_str());
0643           int thck = static_cast<int>(std::find(thick, thick + thksize, items[2]) - thick);
0644           int part = std::atoi(items[1].c_str());
0645           if ((thck <= thksize) && (part >= 0)) {
0646             if ((addType[thck] == HGCalTypes::WaferFineThin) || (addType[thck] == HGCalTypes::WaferFineThick))
0647               part = partTypeH[part];
0648             else
0649               part = partTypeL[part];
0650           }
0651           int orient = std::atoi(items[5].c_str());
0652           wafer waf(thck, part, orient, cassette);
0653           if (layer <= layMax1_) {
0654             int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
0655             module1[index] = waf;
0656             if ((cminEE < 0) || (cassette < cminEE))
0657               cminEE = cassette;
0658             if ((cmaxEE < 0) || (cassette > cmaxEE))
0659               cmaxEE = cassette;
0660           } else if ((layer <= layMax2_) || global) {
0661             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0662             module2[index] = waf;
0663             if ((cminHE1 < 0) || (cassette < cminHE1))
0664               cminHE1 = cassette;
0665             if ((cmaxHE1 < 0) || (cassette > cmaxHE1))
0666               cmaxHE1 = cassette;
0667           } else {
0668             int index = HGCalWaferIndex::waferIndex(layer - layMax1_, waferU, waferV, false);
0669             module3[index] = waf;
0670             if ((cminHE2 < 0) || (cassette < cminHE2))
0671               cminHE2 = cassette;
0672             if ((cmaxHE2 < 0) || (cassette > cmaxHE2))
0673               cmaxHE2 = cassette;
0674           }
0675         }
0676       }
0677     }
0678     fInput.close();
0679     std::cout << "Read " << all << " records with " << comments << " comments " << others
0680               << " non-comment records out of which " << good << ":" << module1.size() << ":" << module2.size() << ":"
0681               << module3.size() << " are good and " << bad << " are bad and with " << layers << " layers\n";
0682     std::cout << "\nThere are " << layer1.size() << " of types:" << std::endl;
0683     for (const auto& l : layer1) {
0684       std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
0685       for (unsigned int k = 0; k < l.deltaR.size(); ++k)
0686         std::cout << ": " << l.deltaR[k];
0687       std::cout << std::endl;
0688     }
0689     std::cout << "\nThere are " << layer2.size() << " of types:" << std::endl;
0690     for (const auto& l : layer2) {
0691       std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
0692       for (unsigned int k = 0; k < l.deltaR.size(); ++k)
0693         std::cout << ": " << l.deltaR[k];
0694       std::cout << std::endl;
0695     }
0696     if (layer3.size() > 0) {
0697       std::cout << "\nThere are " << layer3.size() << " of types:" << std::endl;
0698       for (const auto& l : layer3) {
0699         std::cout << "Layer " << l.layer << " Type " << l.type << " DR";
0700         for (unsigned int k = 0; k < l.deltaR.size(); ++k)
0701           std::cout << ": " << l.deltaR[k];
0702         std::cout << std::endl;
0703       }
0704     }
0705     std::cout << "\nMinimum and Maximum Cassette #'s:: EE: " << cminEE << ":" << cmaxEE << " HEF: " << cminHE1 << ":"
0706               << cmaxHE1 << " HEB: " << cminHE2 << ":" << cmaxHE2 << std::endl;
0707     std::cout << std::endl << std::endl;
0708 
0709     //Now write separately for EE, HEsil and HEmix
0710     writeSilicon(outfile1, cassetteEE, layer1, module1, "EE", global, (debug % 10 > 0));
0711     // Next HEsil part
0712     writeSilicon(outfile2, cassetteHE, layer2, module2, "HE", global, ((debug / 10) % 10 > 0));
0713     // Finally HEmix part
0714     if (!global)
0715       writeSilicon(outfile3, cassetteHE, layer3, module3, "HE", global, ((debug / 100) % 10 > 0));
0716   }
0717 }
0718 
0719 void ConvertSiliconV2::writeSilicon(const char* outfile,
0720                                     const unsigned int cassettes,
0721                                     const std::vector<layerInfo>& layers,
0722                                     const std::map<int, wafer>& module,
0723                                     const std::string& tag,
0724                                     const bool& mode,
0725                                     const bool& debug) {
0726   char apost('"');
0727   unsigned int k0(0), k1(0), k2(0), k3(0);
0728   std::map<int, wafer>::const_iterator itr;
0729   std::string blank = (mode) ? "  " : "    ";
0730   std::ofstream fOut(outfile);
0731   std::vector<int> layerStart;
0732   int layer(-1);
0733   if (mode) {
0734     fOut << blank << "<Vector name=" << apost << "LayerTypes" << tag << apost << " type=" << apost << "numeric" << apost
0735          << " nEntries=" << apost << layers.size() << apost << ">";
0736   } else {
0737     fOut << blank << "<Vector name=" << apost << "LayerTypes" << apost << " type=" << apost << "numeric" << apost
0738          << " nEntries=" << apost << layers.size() << apost << ">";
0739   }
0740   for (const auto& l : layers) {
0741     std::string last = ((k0 + 1) == layers.size()) ? " " : ",";
0742     if (k0 % 20 == 0)
0743       fOut << "\n  " << blank << std::setw(2) << l.type << last;
0744     else
0745       fOut << std::setw(2) << l.type << last;
0746     ++k0;
0747   }
0748   fOut << "\n" << blank << "</Vector>\n";
0749   if (mode) {
0750     fOut << blank << "<Vector name=" << apost << "WaferIndex" << tag << apost << " type=" << apost << "numeric" << apost
0751          << " nEntries=" << apost << module.size() << apost << ">";
0752   } else {
0753     fOut << blank << "<Vector name=" << apost << "WaferIndex" << apost << " type=" << apost << "numeric" << apost
0754          << " nEntries=" << apost << module.size() << apost << ">";
0755   }
0756   for (itr = module.begin(); itr != module.end(); ++itr) {
0757     std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0758     if (k1 % 7 == 0)
0759       fOut << "\n  " << blank << std::setw(8) << itr->first << last;
0760     else
0761       fOut << std::setw(8) << itr->first << last;
0762     if (HGCalWaferIndex::waferLayer(itr->first) != layer) {
0763       layerStart.emplace_back(k1);
0764       layer = HGCalWaferIndex::waferLayer(itr->first);
0765     }
0766     ++k1;
0767     if (debug)
0768       std::cout << "Wafer " << HGCalWaferIndex::waferLayer(itr->first) << ":" << HGCalWaferIndex::waferU(itr->first)
0769                 << ":" << HGCalWaferIndex::waferV(itr->first) << " T " << (itr->second).thick << " P "
0770                 << (itr->second).partial << " O " << (itr->second).orient << " C " << (itr->second).cassette
0771                 << " Property "
0772                 << HGCalProperty::waferProperty(
0773                        (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette)
0774                 << std::endl;
0775   }
0776   fOut << "\n" << blank << "</Vector>\n";
0777   if (mode)
0778     fOut << blank << "<Vector name=" << apost << "WaferProperties" << tag << apost << " type=" << apost << "numeric"
0779          << apost << " nEntries=" << apost << module.size() << apost << ">";
0780   else
0781     fOut << blank << "<Vector name=" << apost << "WaferProperties" << apost << " type=" << apost << "numeric" << apost
0782          << " nEntries=" << apost << module.size() << apost << ">";
0783   for (itr = module.begin(); itr != module.end(); ++itr) {
0784     int property = HGCalProperty::waferProperty(
0785         (itr->second).thick, (itr->second).partial, (itr->second).orient, (itr->second).cassette);
0786     std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0787     if (k2 % 8 == 0)
0788       fOut << "\n  " << blank << std::setw(7) << property << last;
0789     else
0790       fOut << std::setw(7) << property << last;
0791     ++k2;
0792   }
0793   fOut << "\n" << blank << "</Vector>\n";
0794   if (mode) {
0795     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << tag << apost << " type=" << apost << "numeric"
0796          << apost << " nEntries=" << apost << layerStart.size() << apost << ">";
0797   } else {
0798     fOut << blank << "<Vector name=" << apost << "WaferLayerStart" << apost << " type=" << apost << "numeric" << apost
0799          << " nEntries=" << apost << layerStart.size() << apost << ">";
0800   }
0801   for (unsigned k = 0; k < layerStart.size(); ++k) {
0802     std::string last = ((k + 1) == layerStart.size()) ? " " : ",";
0803     if (k % 10 == 0)
0804       fOut << "\n  " << blank << std::setw(5) << layerStart[k] << last;
0805     else
0806       fOut << std::setw(5) << layerStart[k] << last;
0807   }
0808   fOut << "\n" << blank << "</Vector>\n";
0809   unsigned int csize = cassettes * layers.size();
0810   if (mode) {
0811     fOut << blank << "<Vector name=" << apost << "CassetteShift" << tag << apost << " type=" << apost << "numeric"
0812          << apost << " nEntries=" << apost << csize << apost << ">";
0813   } else {
0814     fOut << blank << "<Vector name=" << apost << "CassetteShift" << apost << " type=" << apost << "numeric" << apost
0815          << " nEntries=" << apost << csize << apost << ">";
0816   }
0817   for (const auto& l : layers) {
0818     ++k3;
0819     for (unsigned int k = 0; k < cassettes; ++k) {
0820       std::string last = ((k3 == layers.size()) && ((k + 1) == cassettes)) ? "*mm" : "*mm,";
0821       if ((k % 6) == 0)
0822         fOut << "\n  " << blank << std::setw(9) << l.deltaR[k] << last;
0823       else
0824         fOut << std::setw(9) << l.deltaR[k] << last;
0825     }
0826   }
0827   fOut << "\n" << blank << "</Vector>\n";
0828   fOut.close();
0829 }
0830 
0831 ConvertScintillator::ConvertScintillator(int layMin, int cassette) : layMin_(layMin), cassette_(cassette) {}
0832 
0833 void ConvertScintillator::convert(const char* infile, const char* outfile1, const char* outfile2, int debug) {
0834   std::ifstream fInput(infile);
0835   if (!fInput.good()) {
0836     std::cout << "Cannot open file " << infile << std::endl;
0837   } else {
0838     //First read in all records
0839     char buffer[1024];
0840     std::string types[2] = {"c", "m"};
0841     std::map<int, tile> module;
0842     std::map<int, std::pair<double, double> > ringR;
0843     std::map<int, std::pair<int, int> > layerRing;
0844     unsigned int all(0), comments(0), others(0), bad(0), good(0);
0845     int lmin(99), lmax(0);
0846     while (fInput.getline(buffer, 1024)) {
0847       ++all;
0848       if (buffer[0] == '#') {
0849         ++comments;
0850       } else {
0851         ++others;
0852         std::vector<std::string> items = splitString(std::string(buffer));
0853         if (items.size() != 10) {
0854           ++bad;
0855         } else {
0856           ++good;
0857           int layer, ring, hex1, hex2, hex3, hex4;
0858           float rstart, rend, sipm;
0859           sscanf(
0860               buffer, "%d %d %f %f %f %X %X %X %X", &layer, &ring, &rstart, &rend, &sipm, &hex1, &hex2, &hex3, &hex4);
0861           int type = static_cast<int>(std::find(types, types + 1, items[9]) - types);
0862           if (layer > layMin_) {
0863             tile tl(sipm, hex1, hex2, hex3, hex4, type);
0864             int index = HGCalTileIndex::tileIndex(layer - layMin_, ring + 1, 0);
0865             module[index] = tl;
0866             lmin = std::min((layer - layMin_), lmin);
0867             lmax = std::max((layer - layMin_), lmax);
0868             ringR[ring] = std::pair<double, double>(rstart, rend);
0869             if (layerRing.find(layer) == layerRing.end()) {
0870               layerRing[layer] = std::pair<int, int>(ring, ring);
0871             } else {
0872               int rmin = std::min(ring, layerRing[layer].first);
0873               int rmax = std::max(ring, layerRing[layer].second);
0874               layerRing[layer] = std::pair<int, int>(rmin, rmax);
0875             }
0876           }
0877         }
0878       }
0879     }
0880     fInput.close();
0881     std::cout << "Read " << all << " records with " << comments << " comments " << others
0882               << " non-comment records out of which " << good << ":" << module.size() << ":" << ringR.size() << ":"
0883               << layerRing.size() << " are good and " << bad << " are bad\n"
0884               << std::endl;
0885 
0886     //Now write the content in the first file
0887     std::ofstream fOut(outfile1);
0888     char apost('"');
0889     unsigned int l1(0), l2(0);
0890     std::map<int, std::pair<double, double> >::const_iterator it1;
0891     fOut << "  <Vector name=" << apost << "TileRMin" << apost << " type=" << apost << "numeric" << apost
0892          << " nEntries=" << apost << ringR.size() << apost << ">";
0893     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
0894       std::string last = ((l1 + 1) == ringR.size()) ? " " : ",";
0895       if (l1 % 6 == 0)
0896         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
0897       else
0898         fOut << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
0899       ++l1;
0900     }
0901     fOut << "\n  </Vector>\n";
0902     fOut << "  <Vector name=" << apost << "TileRMax" << apost << " type=" << apost << "numeric" << apost
0903          << " nEntries=" << apost << ringR.size() << apost << ">";
0904     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
0905       std::string last = ((l2 + 1) == ringR.size()) ? " " : ",";
0906       if (l2 % 6 == 0)
0907         fOut << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
0908       else
0909         fOut << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
0910       ++l2;
0911     }
0912     fOut << "\n  </Vector>\n";
0913 
0914     unsigned int l3(0), l4(0);
0915     std::map<int, std::pair<int, int> >::const_iterator it2;
0916     fOut << "  <Vector name=" << apost << "TileRingMin" << apost << " type=" << apost << "numeric" << apost
0917          << " nEntries=" << apost << layerRing.size() << apost << ">";
0918     for (it2 = layerRing.begin(); it2 != layerRing.end(); ++it2) {
0919       std::string last = ((l3 + 1) == layerRing.size()) ? " " : ",";
0920       if (l3 % 14 == 0)
0921         fOut << "\n    " << std::setw(4) << (it2->second).first << last;
0922       else
0923         fOut << std::setw(4) << (it2->second).first << last;
0924       ++l3;
0925     }
0926     fOut << "\n  </Vector>\n";
0927     fOut << "  <Vector name=" << apost << "TileRingMax" << apost << " type=" << apost << "numeric" << apost
0928          << " nEntries=" << apost << layerRing.size() << apost << ">";
0929     for (it2 = layerRing.begin(); it2 != layerRing.end(); ++it2) {
0930       std::string last = ((l4 + 1) == layerRing.size()) ? " " : ",";
0931       if (l4 % 14 == 0)
0932         fOut << "\n    " << std::setw(4) << (it2->second).second << last;
0933       else
0934         fOut << std::setw(4) << (it2->second).second << last;
0935       ++l4;
0936     }
0937     fOut << "\n  </Vector>\n";
0938 
0939     unsigned int k1(0), k2(0), k3(0), k4(0), k5(0), k6(0);
0940     std::map<int, tile>::const_iterator itr;
0941     fOut << "  <Vector name=" << apost << "TileIndex" << apost << " type=" << apost << "numeric" << apost
0942          << " nEntries=" << apost << module.size() << apost << ">";
0943     for (itr = module.begin(); itr != module.end(); ++itr) {
0944       std::string last = ((k1 + 1) == module.size()) ? " " : ",";
0945       if (k1 % 7 == 0)
0946         fOut << "\n    " << std::setw(8) << itr->first << last;
0947       else
0948         fOut << std::setw(8) << itr->first << last;
0949       ++k1;
0950       if ((debug % 10) > 0)
0951         std::cout << "Tile " << HGCalTileIndex::tileLayer(itr->first) << ":" << HGCalTileIndex::tileRing(itr->first)
0952                   << " Type " << (itr->second).type << " Area " << (itr->second).sipm << std::hex << " HEX "
0953                   << (itr->second).hex[0] << " " << (itr->second).hex[1] << " " << (itr->second).hex[2] << " "
0954                   << (itr->second).hex[3] << std::dec << "\n";
0955     }
0956     fOut << "\n  </Vector>\n";
0957     fOut << "  <Vector name=" << apost << "TileProperty" << apost << " type=" << apost << "numeric" << apost
0958          << " nEntries=" << apost << module.size() << apost << ">";
0959     for (itr = module.begin(); itr != module.end(); ++itr) {
0960       std::string last = ((k2 + 1) == module.size()) ? " " : ",";
0961       int property = HGCalTileIndex::tileProperty((itr->second).type, (itr->second).sipm);
0962       if (k2 % 15 == 0)
0963         fOut << "\n    " << std::setw(3) << property << last;
0964       else
0965         fOut << std::setw(3) << property << last;
0966       ++k2;
0967     }
0968     fOut << "\n  </Vector>\n";
0969     fOut << "  <Vector name=" << apost << "TileHEX1" << apost << " type=" << apost << "numeric" << apost
0970          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
0971     for (itr = module.begin(); itr != module.end(); ++itr) {
0972       std::string last = ((k3 + 1) == module.size()) ? " " : ",";
0973       if (k3 % 6 == 0)
0974         fOut << "\n     0x" << (itr->second).hex[0] << last;
0975       else
0976         fOut << " 0x" << (itr->second).hex[0] << last;
0977       ++k3;
0978     }
0979     fOut << "\n  </Vector>\n" << std::dec;
0980     fOut << "  <Vector name=" << apost << "TileHEX2" << apost << " type=" << apost << "numeric" << apost
0981          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
0982     for (itr = module.begin(); itr != module.end(); ++itr) {
0983       std::string last = ((k4 + 1) == module.size()) ? " " : ",";
0984       if (k4 % 6 == 0)
0985         fOut << "\n     0x" << (itr->second).hex[1] << last;
0986       else
0987         fOut << " 0x" << (itr->second).hex[1] << last;
0988       ++k4;
0989     }
0990     fOut << "\n  </Vector>\n" << std::dec;
0991     fOut << "  <Vector name=" << apost << "TileHEX3" << apost << " type=" << apost << "numeric" << apost
0992          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
0993     for (itr = module.begin(); itr != module.end(); ++itr) {
0994       std::string last = ((k5 + 1) == module.size()) ? " " : ",";
0995       if (k5 % 6 == 0)
0996         fOut << "\n     0x" << (itr->second).hex[2] << last;
0997       else
0998         fOut << " 0x" << (itr->second).hex[2] << last;
0999       ++k5;
1000     }
1001     fOut << "\n  </Vector>\n" << std::dec;
1002     fOut << "  <Vector name=" << apost << "TileHEX4" << apost << " type=" << apost << "numeric" << apost
1003          << " nEntries=" << apost << module.size() << apost << ">" << std::hex;
1004     for (itr = module.begin(); itr != module.end(); ++itr) {
1005       std::string last = ((k6 + 1) == module.size()) ? " " : ",";
1006       if (k6 % 6 == 0)
1007         fOut << "\n     0x" << (itr->second).hex[3] << last;
1008       else
1009         fOut << " 0x" << (itr->second).hex[3] << last;
1010       ++k6;
1011     }
1012     fOut << "\n  </Vector>\n" << std::dec;
1013     fOut.close();
1014 
1015     //Now write for the second file
1016     makeTitle(outfile2, module, ringR, lmin, lmax, (((debug / 10) % 10) > 0));
1017   }
1018 }
1019 
1020 void ConvertScintillator::makeTitle(const char* outfile,
1021                                     const std::map<int, tile>& module,
1022                                     const std::map<int, std::pair<double, double> >& ringR,
1023                                     int lmin,
1024                                     int lmax,
1025                                     bool debug) {
1026   const int zside = 1;
1027   const int phiCassette = 24;
1028   std::vector<tileZone> zones;
1029   for (int layer = lmin; layer <= lmax; ++layer) {
1030     tileZone tile0;
1031     int kk, irmin, irmax;
1032     for (int phi = 1; phi <= HGCalProperty::kHGCalTilePhis; ++phi) {
1033       kk = irmin = irmax = 0;
1034       for (std::map<int, tile>::const_iterator itr = module.begin(); itr != module.end(); ++itr) {
1035         if ((HGCalTileIndex::tileLayer(itr->first) == layer) &&
1036             HGCalTileIndex::tileExist((itr->second).hex, zside, phi)) {
1037           int ir = HGCalTileIndex::tileRing(itr->first);
1038           if (kk == 0) {
1039             irmin = irmax = ir;
1040           } else {
1041             irmax = ir;
1042           }
1043           ++kk;
1044         }
1045       }
1046       if (debug)
1047         std::cout << "Layer|Phi|Ring " << layer << ":" << phi << ":" << irmin << ":" << irmax << std::endl;
1048       if (phi == 1) {
1049         tile0.layer = layer;
1050         tile0.rmin = irmin;
1051         tile0.rmax = irmax;
1052         tile0.phimin = phi;
1053         tile0.phimax = phi;
1054         tile0.cassette = (cassette_ == 0) ? 0 : 1;
1055       } else if ((tile0.rmin != irmin) || (tile0.rmax != irmax)) {
1056         if (cassette_ != 0) {
1057           if (tile0.cassette * phiCassette < tile0.phimax) {
1058             do {
1059               int phimax = tile0.phimax;
1060               tile0.phimax = tile0.cassette * phiCassette;
1061               zones.push_back(tile0);
1062               tile0.phimin = tile0.phimax + 1;
1063               tile0.phimax = phimax;
1064               ++tile0.cassette;
1065             } while (tile0.cassette * phiCassette < tile0.phimax);
1066           }
1067         }
1068         zones.push_back(tile0);
1069         int cassette = (cassette_ == 0) ? 0 : (1 + ((phi - 1) / phiCassette));
1070         tile0.layer = layer;
1071         tile0.rmin = irmin;
1072         tile0.rmax = irmax;
1073         tile0.phimin = phi;
1074         tile0.phimax = phi;
1075         tile0.cassette = cassette;
1076         if (phi == HGCalProperty::kHGCalTilePhis)
1077           zones.push_back(tile0);
1078       } else {
1079         tile0.phimax = phi;
1080         if (phi == HGCalProperty::kHGCalTilePhis)
1081           zones.push_back(tile0);
1082       }
1083     }
1084   }
1085 
1086   int nmax = zones.size();
1087   if (debug) {
1088     std::cout << "\nA total of " << nmax << " zones " << std::endl;
1089     for (int k = 0; k < nmax; ++k)
1090       std::cout << "[" << k << "] Layer " << zones[k].layer << " Ring " << zones[k].rmin << ":" << zones[k].rmax
1091                 << " phi " << zones[k].phimin << ":" << zones[k].phimax << " Cassette " << zones[k].cassette
1092                 << std::endl;
1093   }
1094   if (nmax > 0) {
1095     std::ofstream fout(outfile);
1096     char apost('"');
1097     unsigned int l1(0), l2(0);
1098     std::map<int, std::pair<double, double> >::const_iterator it1;
1099     fout << "  <Vector name=" << apost << "TileRMin" << apost << " type=" << apost << "numeric" << apost
1100          << " nEntries=" << apost << ringR.size() << apost << ">";
1101     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1102       std::string last = ((l1 + 1) == ringR.size()) ? " " : ",";
1103       if (l1 % 6 == 0)
1104         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1105       else
1106         fout << std::setw(8) << std::setprecision(6) << (it1->second).first << "*mm" << last;
1107       ++l1;
1108     }
1109     fout << "\n  </Vector>\n";
1110     fout << "  <Vector name=" << apost << "TileRMax" << apost << " type=" << apost << "numeric" << apost
1111          << " nEntries=" << apost << ringR.size() << apost << ">";
1112     for (it1 = ringR.begin(); it1 != ringR.end(); ++it1) {
1113       std::string last = ((l2 + 1) == ringR.size()) ? " " : ",";
1114       if (l2 % 6 == 0)
1115         fout << "\n    " << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1116       else
1117         fout << std::setw(8) << std::setprecision(6) << (it1->second).second << "*mm" << last;
1118       ++l2;
1119     }
1120     fout << "\n  </Vector>\n";
1121     fout << "  <Vector name=" << apost << "TileLayerRings" << apost << " type=" << apost << "numeric" << apost
1122          << " nEntries=" << apost << nmax << apost << ">";
1123     if (debug)
1124       std::cout << "  <Vector name=" << apost << "TileLayerRings" << apost << " type=" << apost << "numeric" << apost
1125                 << " nEntries=" << apost << nmax << apost << ">";
1126     for (int k = 0; k < nmax; ++k) {
1127       std::string last = ((k + 1) == nmax) ? " " : ",";
1128       int lyr1r2 = HGCalTileIndex::tilePack(zones[k].layer, zones[k].rmin, zones[k].rmax);
1129       if (k % 7 == 0) {
1130         fout << "\n    " << std::setw(9) << lyr1r2 << last;
1131         if (debug)
1132           std::cout << "\n    " << std::setw(9) << lyr1r2 << last;
1133       } else {
1134         fout << std::setw(9) << lyr1r2 << last;
1135         if (debug)
1136           std::cout << std::setw(9) << lyr1r2 << last;
1137       }
1138     }
1139     fout << "\n  </Vector>\n";
1140     if (debug)
1141       std::cout << "\n  </Vector>\n";
1142     int layer = -1;
1143     std::vector<int> layerStart;
1144     fout << "  <Vector name=" << apost << "TilePhiRange" << apost << " type=" << apost << "numeric" << apost
1145          << " nEntries=" << apost << nmax << apost << ">";
1146     if (debug)
1147       std::cout << "  <Vector name=" << apost << "TilePhiRange" << apost << " type=" << apost << "numeric" << apost
1148                 << " nEntries=" << apost << nmax << apost << ">";
1149     for (int k = 0; k < nmax; ++k) {
1150       std::string last = ((k + 1) == nmax) ? " " : ",";
1151       int f1f2 = HGCalTileIndex::tilePack(zones[k].cassette, zones[k].phimin, zones[k].phimax);
1152       if (k % 7 == 0) {
1153         fout << "\n    " << std::setw(9) << f1f2 << last;
1154         if (debug)
1155           std::cout << "\n    " << std::setw(9) << f1f2 << last;
1156       } else {
1157         fout << std::setw(9) << f1f2 << last;
1158         if (debug)
1159           std::cout << std::setw(9) << f1f2 << last;
1160       }
1161       if (zones[k].layer != layer) {
1162         layerStart.emplace_back(k);
1163         layer = zones[k].layer;
1164       }
1165     }
1166     fout << "\n  </Vector>\n";
1167     if (debug)
1168       std::cout << "\n  </Vector>\n";
1169     fout << "  <Vector name=" << apost << "TileLayerStart" << apost << " type=" << apost << "numeric" << apost
1170          << " nEntries=" << apost << layerStart.size() << apost << ">";
1171     if (debug)
1172       std::cout << "  <Vector name=" << apost << "TileLayerStart" << apost << " type=" << apost << "numeric" << apost
1173                 << " nEntries=" << apost << layerStart.size() << apost << ">";
1174     for (unsigned int k = 0; k < layerStart.size(); ++k) {
1175       std::string last = ((k + 1) == layerStart.size()) ? " " : ",";
1176       if (k % 10 == 0) {
1177         fout << "\n    " << std::setw(5) << layerStart[k] << last;
1178         if (debug)
1179           std::cout << "\n    " << std::setw(5) << layerStart[k] << last;
1180       } else {
1181         fout << std::setw(5) << layerStart[k] << last;
1182         if (debug)
1183           std::cout << std::setw(5) << layerStart[k] << last;
1184       }
1185     }
1186     fout << "\n  </Vector>\n";
1187     if (debug)
1188       std::cout << "\n  </Vector>\n";
1189     fout.close();
1190   }
1191 }