HcalBarrelAlgo

ddcms_det_element_DDCMS_hcal_DDHCalBarrelAlgo

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
/////////////////////////////////////////////////////////////////////////////
// File: DDHCalBarrelAlgo.cc
//   adapted from CCal(G4)HcalBarrel.cc
// Description: Geometry factory class for Hcal Barrel
///////////////////////////////////////////////////////////////////////////////

#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>

#include "DataFormats/Math/interface/angle_units.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DetectorDescription/Core/interface/DDSplit.h"
#include "DetectorDescription/DDCMS/interface/DDPlugins.h"
#include "DetectorDescription/DDCMS/interface/DDutils.h"
#include "DD4hep/DetFactoryHelper.h"

//#define EDM_ML_DEBUG
using namespace angle_units::operators;

struct HcalBarrelAlgo {
  //General Volume
  //      <----- Zmax ------>
  //Router************************-------
  //      *                      *Rstep2|        Theta angle w.r.t. vertical
  //      *                      *---------------
  //      *                     *               |
  //      *                    *Theta[i]        Rmax[i]
  //      *                   *---------------  |
  //                        *Theta[0] Rmax[0]|  |
  //Rinner*****************----------------------

  std::string genMaterial;     //General material
  int nsectors;                //Number of potenital straight edges
  int nsectortot;              //Number of straight edges (actual)
  int nhalf;                   //Number of half modules
  double rinner, router;       //See picture
  int rzones;                  //  ....
  std::vector<double> theta;   //  .... (in degrees)
  std::vector<double> rmax;    //  ....
  std::vector<double> zoff;    //  ....
  std::vector<double> ttheta;  //tan(theta)
  std::string rotHalf;         //Rotation matrix of the second half
  std::string rotns;           //Name space for Rotation matrices

  //Upper layers inside general volume
  //     <---- Zout ---->
  //  |  ****************     |
  //  |  *              *     Wstep
  //  W  *              ***** |
  //  |  *                  *
  //  |  ********************
  //     <------ Zin ------->
  // Middle layers inside general volume
  //     <------ Zout ------>         Zout = Full sector Z at position
  //  |  ********************         Zin  = Full sector Z at position
  //  |  *                 *
  //  W  *                * Angle = Theta sector
  //  |  *               *  )
  //  |  ****************--------
  //     <------ Zin ------->

  // Lower layers
  //     <------ Zout ------>         Zin(i)=Zout(i-1)
  //  |  ********************         Zout(i)=Zin(i)+W(i)/tan(Theta(i))
  //  |  *                 *
  //  W  *                *  Theta
  //  |  *               *
  //  |  ****************--------
  //     <--- Zin ------>

  int nLayers;                          //Number of layers
  std::vector<int> layerId;             //Number identification
  std::vector<std::string> layerLabel;  //String identification
  std::vector<std::string> layerMat;    //Material
  std::vector<double> layerWidth;       //W in picture
  std::vector<double> layerD1;          //d1 in front picture
  std::vector<double> layerD2;          //d2 in front picture
  std::vector<double> layerAlpha;       //Angular width of the middle tiles
  std::vector<double> layerT1;          //t in front picture (side)
  std::vector<double> layerT2;          //t in front picture (front)
  std::vector<int> layerAbsorb;         //Absorber flag
  std::vector<double> layerGap;         //Gap at the edge

  int nAbsorber;                        //Number of absorber layers in middle
  std::vector<std::string> absorbName;  //Absorber name
  std::vector<std::string> absorbMat;   //Absorber material
  std::vector<double> absorbD;          //Distance from the bottom surface
  std::vector<double> absorbT;          //Thickness
  std::string middleMat;                //Material of the detector layer
  double middleD;                       //Distance from the bottom surface
  double middleW;                       //Half width
  int nMidAbs;                          //Number of absorbers in front part
  std::vector<std::string> midName;     //Absorber names in the front part
  std::vector<std::string> midMat;      //Absorber material
  std::vector<double> midW;             //Half width
  std::vector<double> midT;             //Thickness

  std::vector<std::string> sideMat;      //Material for special side layers
  std::vector<double> sideD;             //Depth from bottom surface
  std::vector<double> sideT;             //Thickness
  int nSideAbs;                          //Number of absorbers in special side
  std::vector<std::string> sideAbsName;  //Absorber name
  std::vector<std::string> sideAbsMat;   //Absorber material
  std::vector<double> sideAbsW;          //Half width

  // Detectors. Each volume inside the layer has the shape:
  //
  // ******************************* |
  // *\\\\\\\Plastic\\\\\\\\\\\\\\\* T2
  // ******************************* |
  // *////Scintillator/////////////* Tsc
  // ******************************* |
  // *\\\\\\\Plastic\\\\\\\\\\\\\\\* T1
  // ******************************* |   |
  // *         Air                 *     dP1
  // *******************************     |
  //
  std::string detMat;    //fill material
  std::string detRot;    //Rotation matrix for the 2nd
  std::string detMatPl;  //Plastic material
  std::string detMatSc;  //Scintillator material
  std::vector<int> detType;
  std::vector<double> detdP1;     //Air gap (side)
  std::vector<double> detdP2;     //Air gap (centre)
  std::vector<double> detT11;     //Back plastic thickness (side)
  std::vector<double> detT12;     //Back plastic thickness (centre)
  std::vector<double> detTsc;     //Scintillator
  std::vector<double> detT21;     //Front plastic thickness (side)
  std::vector<double> detT22;     //Front plastic thickness (centre)
  std::vector<double> detWidth1;  //Width of phi(1,4) megatiles
  std::vector<double> detWidth2;  //Width of phi(2,3) megatiles
  std::vector<int> detPosY;       //Positioning of phi(1,4) tiles - 0 centre

  std::string idName;       //Name of the "parent" volume.
  std::string idNameSpace;  //Namespace of this and ALL sub-parts
  int idOffset;             // Geant4 ID's...    = 3000;

  HcalBarrelAlgo() = delete;

  HcalBarrelAlgo(cms::DDParsingContext& ctxt, xml_h& e) {
    cms::DDNamespace ns(ctxt, e, true);
    cms::DDAlgoArguments args(ctxt, e);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Creating an instance";
#endif

    genMaterial = args.value<std::string>("MaterialName");
    nsectors = args.value<int>("NSector");
    nsectortot = args.value<int>("NSectorTot");
    nhalf = args.value<int>("NHalf");
    rinner = args.value<double>("RIn");
    router = args.value<double>("ROut");
    rzones = args.value<int>("RZones");
    rotHalf = args.value<std::string>("RotHalf");
    rotns = args.value<std::string>("RotNameSpace");

    theta = args.value<std::vector<double> >("Theta");
    rmax = args.value<std::vector<double> >("RMax");
    zoff = args.value<std::vector<double> >("ZOff");
    for (int i = 0; i < rzones; i++) {
      ttheta.emplace_back(tan(theta[i]));  //*deg already done in XML
    }
    if (rzones > 3)
      rmax[2] = (zoff[3] - zoff[2]) / ttheta[2];

#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: General material " << genMaterial << "\tSectors " << nsectors
                                 << ", " << nsectortot << "\tHalves " << nhalf << "\tRotation matrix " << rotns << ":"
                                 << rotHalf << "\n\t\t" << cms::convert2mm(rinner) << "\t" << cms::convert2mm(router)
                                 << "\t" << rzones;
    for (int i = 0; i < rzones; i++)
      edm::LogVerbatim("HCalGeom") << "\tTheta[" << i << "] = " << theta[i] << "\trmax[" << i
                                   << "] = " << cms::convert2mm(rmax[i]) << "\tzoff[" << i
                                   << "] = " << cms::convert2mm(zoff[i]);
#endif
    ///////////////////////////////////////////////////////////////
    //Layers
    nLayers = args.value<int>("NLayers");
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Layer\t" << nLayers;
#endif
    layerId = args.value<std::vector<int> >("Id");
    layerLabel = args.value<std::vector<std::string> >("LayerLabel");
    layerMat = args.value<std::vector<std::string> >("LayerMat");
    layerWidth = args.value<std::vector<double> >("LayerWidth");
    layerD1 = args.value<std::vector<double> >("D1");
    layerD2 = args.value<std::vector<double> >("D2");
    layerAlpha = args.value<std::vector<double> >("Alpha2");
    layerT1 = args.value<std::vector<double> >("T1");
    layerT2 = args.value<std::vector<double> >("T2");
    layerAbsorb = args.value<std::vector<int> >("AbsL");
    layerGap = args.value<std::vector<double> >("Gap");
#ifdef EDM_ML_DEBUG
    for (int i = 0; i < nLayers; i++)
      edm::LogVerbatim("HCalGeom") << layerLabel[i] << "\t" << layerId[i] << "\t" << layerMat[i] << "\t"
                                   << cms::convert2mm(layerWidth[i]) << "\t" << cms::convert2mm(layerD1[i]) << "\t"
                                   << cms::convert2mm(layerD2[i]) << "\t" << layerAlpha[i] << "\t"
                                   << cms::convert2mm(layerT1[i]) << "\t" << cms::convert2mm(layerT2[i]) << "\t"
                                   << layerAbsorb[i] << "\t" << cms::convert2mm(layerGap[i]);
#endif

    ///////////////////////////////////////////////////////////////
    //Absorber Layers and middle part
    absorbName = args.value<std::vector<std::string> >("AbsorbName");
    absorbMat = args.value<std::vector<std::string> >("AbsorbMat");
    absorbD = args.value<std::vector<double> >("AbsorbD");
    absorbT = args.value<std::vector<double> >("AbsorbT");
    nAbsorber = absorbName.size();
#ifdef EDM_ML_DEBUG
    for (int i = 0; i < nAbsorber; i++)
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << absorbName[i] << " Material " << absorbMat[i] << " d "
                                   << cms::convert2mm(absorbD[i]) << " t " << cms::convert2mm(absorbT[i]);
#endif
    middleMat = args.value<std::string>("MiddleMat");
    middleD = args.value<double>("MiddleD");
    middleW = args.value<double>("MiddleW");
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Middle material " << middleMat << " d "
                                 << cms::convert2mm(middleD) << " w " << cms::convert2mm(middleW);
#endif
    midName = args.value<std::vector<std::string> >("MidAbsName");
    midMat = args.value<std::vector<std::string> >("MidAbsMat");
    midW = args.value<std::vector<double> >("MidAbsW");
    midT = args.value<std::vector<double> >("MidAbsT");
    nMidAbs = midName.size();
#ifdef EDM_ML_DEBUG
    for (int i = 0; i < nMidAbs; i++)
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << midName[i] << " Material " << midMat[i] << " W "
                                   << cms::convert2mm(midW[i]) << " T " << cms::convert2mm(midT[i]);
#endif

    //Absorber layers in the side part
    sideMat = args.value<std::vector<std::string> >("SideMat");
    sideD = args.value<std::vector<double> >("SideD");
    sideT = args.value<std::vector<double> >("SideT");
#ifdef EDM_ML_DEBUG
    for (unsigned int i = 0; i < sideMat.size(); i++)
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Side material " << sideMat[i] << " d "
                                   << cms::convert2mm(sideD[i]) << " t " << cms::convert2mm(sideT[i]);
#endif
    sideAbsName = args.value<std::vector<std::string> >("SideAbsName");
    sideAbsMat = args.value<std::vector<std::string> >("SideAbsMat");
    sideAbsW = args.value<std::vector<double> >("SideAbsW");
    nSideAbs = sideAbsName.size();
#ifdef EDM_ML_DEBUG
    for (int i = 0; i < nSideAbs; i++)
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << sideAbsName[i] << " Material " << sideAbsMat[i] << " W "
                                   << cms::convert2mm(sideAbsW[i]);
#endif

    ///////////////////////////////////////////////////////////////
    // Detectors

    detMat = args.value<std::string>("DetMat");
    detRot = args.value<std::string>("DetRot");
    detMatPl = args.value<std::string>("DetMatPl");
    detMatSc = args.value<std::string>("DetMatSc");
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Detector (" << nLayers << ") Rotation matrix " << rotns << ":"
                                 << detRot << "\n\t\t" << detMat << "\t" << detMatPl << "\t" << detMatSc;
#endif
    detType = args.value<std::vector<int> >("DetType");
    detdP1 = args.value<std::vector<double> >("DetdP1");
    detdP2 = args.value<std::vector<double> >("DetdP2");
    detT11 = args.value<std::vector<double> >("DetT11");
    detT12 = args.value<std::vector<double> >("DetT12");
    detTsc = args.value<std::vector<double> >("DetTsc");
    detT21 = args.value<std::vector<double> >("DetT21");
    detT22 = args.value<std::vector<double> >("DetT22");
    detWidth1 = args.value<std::vector<double> >("DetWidth1");
    detWidth2 = args.value<std::vector<double> >("DetWidth2");
    detPosY = args.value<std::vector<int> >("DetPosY");
#ifdef EDM_ML_DEBUG
    for (int i = 0; i < nLayers; i++)
      edm::LogVerbatim("HCalGeom") << i + 1 << "\t" << detType[i] << "\t" << cms::convert2mm(detdP1[i]) << ", "
                                   << cms::convert2mm(detdP2[i]) << "\t" << cms::convert2mm(detT11[i]) << ", "
                                   << cms::convert2mm(detT12[i]) << "\t" << cms::convert2mm(detTsc[i]) << "\t"
                                   << cms::convert2mm(detT21[i]) << ", " << cms::convert2mm(detT22[i]) << "\t"
                                   << cms::convert2mm(detWidth1[i]) << "\t" << cms::convert2mm(detWidth2[i]) << "\t"
                                   << detPosY[i];
#endif

    //  idName = parentName.name();
    idName = args.value<std::string>("MotherName");
    idNameSpace = ns.name();
    idOffset = args.value<int>("IdOffset");
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Parent " << args.parentName() << " idName " << idName
                                 << " NameSpace " << idNameSpace << " Offset " << idOffset;
    edm::LogVerbatim("HCalGeom") << "==>> Constructing DDHCalBarrelAlgo...\n"
                                 << "DDHCalBarrelAlgo: General volume...";
#endif

    double alpha = (1._pi) / nsectors;
    double dphi = nsectortot * (2._pi) / nsectors;
    int nsec, ntot(15);
    if (nhalf == 1)
      nsec = 8;
    else
      nsec = 15;
    int nf = ntot - nsec;

    //Calculate zmin... see HCalBarrel.hh picture. For polyhedra
    //Rmin and Rmax are distances to vertex
    double zmax = zoff[3];
    double zstep5 = zoff[4];
    double zstep4 = (zoff[1] + rmax[1] * ttheta[1]);
    if ((zoff[2] + rmax[1] * ttheta[2]) > zstep4)
      zstep4 = (zoff[2] + rmax[1] * ttheta[2]);
    double zstep3 = (zoff[1] + rmax[0] * ttheta[1]);
    double zstep2 = (zoff[0] + rmax[0] * ttheta[0]);
    double zstep1 = (zoff[0] + rinner * ttheta[0]);
    double rout = router;
    double rout1 = rmax[3];
    double rin = rinner;
    double rmid1 = rmax[0];
    double rmid2 = rmax[1];
    double rmid3 = (zoff[4] - zoff[2]) / ttheta[2];
    double rmid4 = rmax[2];

    std::vector<double> pgonZ = {-zmax,
                                 -zstep5,
                                 -zstep5,
                                 -zstep4,
                                 -zstep3,
                                 -zstep2,
                                 -zstep1,
                                 0,
                                 zstep1,
                                 zstep2,
                                 zstep3,
                                 zstep4,
                                 zstep5,
                                 zstep5,
                                 zmax};

    std::vector<double> pgonRmin = {
        rmid4, rmid3, rmid3, rmid2, rmid1, rmid1, rin, rin, rin, rmid1, rmid1, rmid2, rmid3, rmid3, rmid4};

    std::vector<double> pgonRmax = {
        rout1, rout1, rout, rout, rout, rout, rout, rout, rout, rout, rout, rout, rout, rout1, rout1};

    std::vector<double> pgonZHalf = {0, zstep1, zstep2, zstep3, zstep4, zstep5, zstep5, zmax};

    std::vector<double> pgonRminHalf = {rin, rin, rmid1, rmid1, rmid2, rmid3, rmid3, rmid4};

    std::vector<double> pgonRmaxHalf = {rout, rout, rout, rout, rout, rout, rout1, rout1};

    std::string name("Null");
    dd4hep::Solid solid;
    if (nf == 0) {
      solid = dd4hep::Polyhedra(ns.prepend(idName), nsectortot, -alpha, dphi, pgonZ, pgonRmin, pgonRmax);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << genMaterial
                                   << " with " << nsectortot << " sectors from " << convertRadToDeg(-alpha) << " to "
                                   << convertRadToDeg(-alpha + dphi) << " and with " << nsec << " sections ";
      for (unsigned int i = 0; i < pgonZ.size(); i++)
        edm::LogVerbatim("HCalGeom") << "\t"
                                     << "\tZ = " << cms::convert2mm(pgonZ[i])
                                     << "\tRmin = " << cms::convert2mm(pgonRmin[i])
                                     << "\tRmax = " << cms::convert2mm(pgonRmax[i]);
#endif
    } else {
      solid = dd4hep::Polyhedra(ns.prepend(idName), nsectortot, -alpha, dphi, pgonZHalf, pgonRminHalf, pgonRmaxHalf);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << genMaterial
                                   << " with " << nsectortot << " sectors from " << convertRadToDeg(-alpha) << " to "
                                   << convertRadToDeg(-alpha + dphi) << " and with " << nsec << " sections ";
      for (unsigned int i = 0; i < pgonZHalf.size(); i++)
        edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZHalf[i])
                                     << "\tRmin = " << cms::convert2mm(pgonRminHalf[i])
                                     << "\tRmax = " << cms::convert2mm(pgonRmaxHalf[i]);
#endif
    }

    dd4hep::Material matter = ns.material(genMaterial);
    dd4hep::Volume genlogic(solid.name(), solid, matter);
    dd4hep::Volume parentName = ns.volume(args.parentName());
    parentName.placeVolume(genlogic, 1);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << genlogic.name() << " number 1 positioned in "
                                 << parentName.name() << " at (0, 0, 0) with no rotation";
#endif
    //Forward and backwards halfs
    name = idName + "Half";
    nf = (ntot + 1) / 2;
    solid = dd4hep::Polyhedra(ns.prepend(name), nsectortot, -alpha, dphi, pgonZHalf, pgonRminHalf, pgonRmaxHalf);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << genMaterial
                                 << " with " << nsectortot << " sectors from " << convertRadToDeg(-alpha) << " to "
                                 << convertRadToDeg(-alpha + dphi) << " and with " << nf << " sections ";
    for (unsigned int i = 0; i < pgonZHalf.size(); i++)
      edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZHalf[i])
                                   << "\tRmin = " << cms::convert2mm(pgonRminHalf[i])
                                   << "\tRmax = " << cms::convert2mm(pgonRmaxHalf[i]);
#endif
    dd4hep::Volume genlogich(solid.name(), solid, matter);
    genlogic.placeVolume(genlogich, 1);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << genlogich.name() << " number 1 positioned in "
                                 << genlogic.name() << " at (0, 0, 0) with no rotation";
#endif
    if (nhalf != 1) {
      dd4hep::Rotation3D rot = getRotation(rotHalf, rotns, ns);
      genlogic.placeVolume(genlogich, 2, rot);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << genlogich.name() << " number 2 positioned in "
                                   << genlogic.name() << " at (0, 0, 0) with " << rot;
#endif
    }  //end if (getNhalf...

    //Construct sector (from -alpha to +alpha)
    name = idName + "Module";
    solid = dd4hep::Polyhedra(ns.prepend(name), 1, -alpha, 2 * alpha, pgonZHalf, pgonRminHalf, pgonRmaxHalf);
    dd4hep::Volume seclogic(solid.name(), solid, matter);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << matter.name()
                                 << " with 1 sector from " << convertRadToDeg(-alpha) << " to "
                                 << convertRadToDeg(alpha) << " and with " << nf << " sections";
    for (unsigned int i = 0; i < pgonZHalf.size(); i++)
      edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZHalf[i])
                                   << "\tRmin = " << cms::convert2mm(pgonRminHalf[i])
                                   << "\tRmax = " << cms::convert2mm(pgonRmaxHalf[i]);
#endif

    for (int ii = 0; ii < nsectortot; ii++) {
      double phi = ii * 2 * alpha;
      dd4hep::Rotation3D rotation;
      if (phi != 0) {
        rotation = dd4hep::RotationZ(phi);
#ifdef EDM_ML_DEBUG
        edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Creating a new "
                                     << "rotation around Z " << convertRadToDeg(phi);
#endif
      }  //if phideg!=0
      genlogich.placeVolume(seclogic, ii + 1, rotation);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << seclogic.name() << " number " << ii + 1
                                   << " positioned in " << genlogich.name() << " at (0, 0, 0) with " << rotation;
#endif
    }

    //Construct the things inside the sector
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: Layers (" << nLayers << ") ...";
#endif
    rin = rinner;
    for (int i = 0; i < nLayers; i++) {
      std::string name = idName + layerLabel[i];
      dd4hep::Material matter = ns.material(layerMat[i]);
      double width = layerWidth[i];
      double rout = rin + width;

      int in = 0, out = 0;
      for (int j = 0; j < rzones - 1; j++) {
        if (rin >= rmax[j])
          in = j + 1;
        if (rout > rmax[j])
          out = j + 1;
      }
      double zout = zoff[in] + rin * ttheta[in];

      //!!!!!!!!!!!!!!!!!Should be zero. And removed as soon as
      //vertical walls are allowed in SolidPolyhedra
#ifdef EDM_ML_DEBUG
      int nsec = 2;
#endif
      std::vector<double> pgonZ, pgonRmin, pgonRmax;
      // index 0
      pgonZ.emplace_back(0);
      pgonRmin.emplace_back(rin);
      pgonRmax.emplace_back(rout);
      // index 1
      pgonZ.emplace_back(zout);
      pgonRmin.emplace_back(rin);
      pgonRmax.emplace_back(rout);
      if (in == out) {
        if (in <= 3) {
          //index 2
          pgonZ.emplace_back(zoff[in] + rout * ttheta[in]);
          pgonRmin.emplace_back(pgonRmax[1]);
          pgonRmax.emplace_back(pgonRmax[1]);
#ifdef EDM_ML_DEBUG
          nsec++;
#endif
        }
      } else {
        if (in == 3) {
          //redo index 1, add index 2
          pgonZ[1] = (zoff[out] + rmax[out] * ttheta[out]);
          pgonZ.emplace_back(pgonZ[1]);
          pgonRmin.emplace_back(pgonRmin[1]);
          pgonRmax.emplace_back(rmax[in]);
          //index 3
          pgonZ.emplace_back(zoff[in] + rmax[in] * ttheta[in]);
          pgonRmin.emplace_back(pgonRmin[2]);
          pgonRmax.emplace_back(pgonRmax[2]);
#ifdef EDM_ML_DEBUG
          nsec += 2;
#endif
        } else {
          //index 2
          pgonZ.emplace_back(zoff[in] + rmax[in] * ttheta[in]);
          pgonRmin.emplace_back(rmax[in]);
          pgonRmax.emplace_back(pgonRmax[1]);
#ifdef EDM_ML_DEBUG
          nsec++;
#endif
          if (in == 0) {
            pgonZ.emplace_back(zoff[out] + rmax[in] * ttheta[out]);
            pgonRmin.emplace_back(pgonRmin[2]);
            pgonRmax.emplace_back(pgonRmax[2]);
#ifdef EDM_ML_DEBUG
            nsec++;
#endif
          }
          if (in <= 1) {
            pgonZ.emplace_back(zoff[out] + rout * ttheta[out]);
            pgonRmin.emplace_back(rout);
            pgonRmax.emplace_back(rout);
#ifdef EDM_ML_DEBUG
            nsec++;
#endif
          }
        }
      }
      //Solid & volume
      dd4hep::Solid solid;
      double alpha1 = alpha;
      if (layerGap[i] > 1.e-6) {
        double rmid = 0.5 * (rin + rout);
        double width = rmid * tan(alpha) - layerGap[i];
        alpha1 = atan(width / rmid);
#ifdef EDM_ML_DEBUG
        edm::LogVerbatim("HCalGeom") << "\tAlpha_1 modified from " << convertRadToDeg(alpha) << " to "
                                     << convertRadToDeg(alpha1) << " Rmid " << cms::convert2mm(rmid)
                                     << " Reduced width " << cms::convert2mm(width);
#endif
      }
      solid = dd4hep::Polyhedra(ns.prepend(name), 1, -alpha1, 2 * alpha1, pgonZ, pgonRmin, pgonRmax);
      dd4hep::Volume glog(solid.name(), solid, matter);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " (Layer " << i << ") Polyhedra made of "
                                   << matter.name() << " with 1 sector from " << convertRadToDeg(-alpha1) << " to "
                                   << convertRadToDeg(alpha1) << " and with " << nsec << " sections";
      for (unsigned int k = 0; k < pgonZ.size(); k++)
        edm::LogVerbatim("HCalGeom") << "\t\t" << cms::convert2mm(pgonZ[k]) << "\t" << cms::convert2mm(pgonRmin[k])
                                     << "\t" << cms::convert2mm(pgonRmax[k]);
#endif

      seclogic.placeVolume(glog, layerId[i]);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " number " << layerId[i]
                                   << " positioned in " << seclogic.name() << " at (0,0,0) with no rotation";
#endif
      constructInsideLayers(glog,
                            layerLabel[i],
                            layerId[i],
                            layerAbsorb[i],
                            rin,
                            layerD1[i],
                            alpha1,
                            layerD2[i],
                            layerAlpha[i],
                            layerT1[i],
                            layerT2[i],
                            ns);
      rin = rout;
    }
  }

  void constructInsideLayers(dd4hep::Volume& laylog,
                             const std::string& nm,
                             int id,
                             int nAbs,
                             double rin,
                             double d1,
                             double alpha1,
                             double d2,
                             double alpha2,
                             double t1,
                             double t2,
                             cms::DDNamespace& ns) {
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: \t\tInside layer " << id << "...";
#endif

    ///////////////////////////////////////////////////////////////
    //Pointers to the Rotation Matrices and to the Materials
    dd4hep::Rotation3D rot = getRotation(detRot, rotns, ns);

    std::string nam0 = nm + "In";
    std::string name = idName + nam0;
    dd4hep::Material matter = ns.material(detMat);

    dd4hep::Solid solid;
    dd4hep::Volume glog, mother;
    double rsi, dx, dy, dz, x, y;
    int i, in;
    //Two lower volumes
    if (alpha1 > 0) {
      rsi = rin + d1;
      in = 0;
      for (i = 0; i < rzones - 1; i++) {
        if (rsi >= rmax[i])
          in = i + 1;
      }
      dx = 0.5 * t1;
      dy = 0.5 * rsi * (tan(alpha1) - tan(alpha2));
      dz = 0.5 * (zoff[in] + rsi * ttheta[in]);
      x = rsi + dx;
      y = 0.5 * rsi * (tan(alpha1) + tan(alpha2));
      dd4hep::Position r11(x, y, dz);
      dd4hep::Position r12(x, -y, dz);

      solid = dd4hep::Box(ns.prepend(name + "1"), dx, dy, dz);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Box made of " << matter.name()
                                   << " of dimensions " << cms::convert2mm(dx) << ", " << cms::convert2mm(dy) << ", "
                                   << cms::convert2mm(dz);
#endif
      glog = dd4hep::Volume(solid.name(), solid, matter);

      if (nAbs != 0) {
        mother = constructSideLayer(laylog, name, nAbs, rin, alpha1, ns);
      } else {
        mother = laylog;
      }
      mother.placeVolume(glog, idOffset + 1, r11);
      mother.placeVolume(glog, idOffset + 2, dd4hep::Transform3D(rot, r12));
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number " << (idOffset + 1)
                                   << " positioned in " << mother.name() << " at (" << cms::convert2mm(x) << ","
                                   << cms::convert2mm(y) << "," << cms::convert2mm(dz) << ") with no rotation\n"
                                   << "DDHCalBarrelAlgo: " << glog.name() << " Number " << (idOffset + 2)
                                   << " positioned in " << mother.name() << " at (" << cms::convert2mm(x) << ","
                                   << -cms::convert2mm(y) << "," << cms::convert2mm(dz) << ") with " << rot;
#endif
      //Constructin the plastics and scintillators inside
      constructInsideDetectors(glog, nam0 + "1", id, dx, dy, dz, 1, ns);
    }

    //Upper volume
    rsi = rin + d2;
    in = 0;
    for (i = 0; i < rzones - 1; i++) {
      if (rsi >= rmax[i])
        in = i + 1;
    }
    dx = 0.5 * t2;
    dy = 0.5 * rsi * tan(alpha2);
    dz = 0.5 * (zoff[in] + rsi * ttheta[in]);
    x = rsi + dx;
    dd4hep::Position r21(x, dy, dz);
    dd4hep::Position r22(x, -dy, dz);

    solid = dd4hep::Box(ns.prepend(name + "2"), dx, dy, dz);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Box made of " << matter.name()
                                 << " of dimensions " << cms::convert2mm(dx) << ", " << cms::convert2mm(dy) << ", "
                                 << cms::convert2mm(dz);
#endif
    glog = dd4hep::Volume(solid.name(), solid, matter);

    if (nAbs < 0) {
      mother = constructMidLayer(laylog, name, rin, alpha1, ns);
    } else {
      mother = laylog;
    }
    mother.placeVolume(glog, idOffset + 3, r21);
    mother.placeVolume(glog, idOffset + 4, dd4hep::Transform3D(rot, r22));
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number " << (idOffset + 3)
                                 << " positioned in " << mother.name() << " at (" << cms::convert2mm(x) << ","
                                 << cms::convert2mm(dy) << "," << cms::convert2mm(dz)
                                 << ") with no rotation\nDDHCalBarrelAlgo: " << glog.name() << " Number "
                                 << (idOffset + 4) << " positioned in " << mother.name() << " at ("
                                 << cms::convert2mm(x) << "," << -cms::convert2mm(dy) << "," << cms::convert2mm(dz)
                                 << ") with " << rot;
#endif
    //Constructin the plastics and scintillators inside
    constructInsideDetectors(glog, nam0 + "2", id, dx, dy, dz, 2, ns);
  }

  dd4hep::Volume constructSideLayer(
      dd4hep::Volume& laylog, const std::string& nm, int nAbs, double rin, double alpha, cms::DDNamespace& ns) {
    //Extra absorber layer
    int k = abs(nAbs) - 1;
    std::string namek = nm + "Side";
    double rsi = rin + sideD[k];
    int in = 0;
    for (int i = 0; i < rzones - 1; i++) {
      if (rsi >= rmax[i])
        in = i + 1;
    }
    std::vector<double> pgonZ, pgonRmin, pgonRmax;
    // index 0
    pgonZ.emplace_back(0.0);
    pgonRmin.emplace_back(rsi);
    pgonRmax.emplace_back(rsi + sideT[k]);
    // index 1
    pgonZ.emplace_back(zoff[in] + rsi * ttheta[in]);
    pgonRmin.emplace_back(rsi);
    pgonRmax.emplace_back(pgonRmax[0]);
    // index 2
    pgonZ.emplace_back(zoff[in] + pgonRmax[0] * ttheta[in]);
    pgonRmin.emplace_back(pgonRmax[1]);
    pgonRmax.emplace_back(pgonRmax[1]);
    dd4hep::Solid solid = dd4hep::Polyhedra(ns.prepend(namek), 1, -alpha, 2 * alpha, pgonZ, pgonRmin, pgonRmax);
    dd4hep::Material matter = ns.material(sideMat[k]);
    dd4hep::Volume glog(solid.name(), solid, matter);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << sideMat[k]
                                 << " with 1 sector from " << convertRadToDeg(-alpha) << " to "
                                 << convertRadToDeg(alpha) << " and with " << pgonZ.size() << " sections";
    for (unsigned int ii = 0; ii < pgonZ.size(); ii++)
      edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZ[ii])
                                   << "\tRmin = " << cms::convert2mm(pgonRmin[ii])
                                   << "\tRmax = " << cms::convert2mm(pgonRmax[ii]);
#endif

    laylog.placeVolume(glog, 1);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number 1 positioned in " << laylog.name()
                                 << " at (0,0,0) with no rotation";
#endif
    if (nAbs < 0) {
      dd4hep::Volume mother = glog;
      double rmid = pgonRmax[0];
      for (int i = 0; i < nSideAbs; i++) {
        double alpha1 = atan(sideAbsW[i] / rmid);
        if (alpha1 > 0) {
          std::string name = namek + sideAbsName[i];
          solid = dd4hep::Polyhedra(ns.prepend(name), 1, -alpha1, 2 * alpha1, pgonZ, pgonRmin, pgonRmax);
          dd4hep::Material matter = ns.material(sideAbsMat[i]);
          dd4hep::Volume log(solid.name(), solid, matter);
#ifdef EDM_ML_DEBUG
          edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << sideAbsMat[i]
                                       << " with 1 sector from " << convertRadToDeg(-alpha1) << " to "
                                       << convertRadToDeg(alpha1) << " and with " << pgonZ.size() << " sections";
          for (unsigned int ii = 0; ii < pgonZ.size(); ii++)
            edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZ[ii])
                                         << "\tRmin = " << cms::convert2mm(pgonRmin[ii])
                                         << "\tRmax = " << cms::convert2mm(pgonRmax[ii]);
#endif

          mother.placeVolume(log, 1);
#ifdef EDM_ML_DEBUG
          edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << log.name() << " Number 1 positioned in "
                                       << mother.name() << " at (0,0,0) with no rotation";
#endif
          mother = log;
        }
      }
    }
    return glog;
  }

  dd4hep::Volume constructMidLayer(
      dd4hep::Volume laylog, const std::string& nm, double rin, double alpha, cms::DDNamespace& ns) {
    dd4hep::Solid solid;
    dd4hep::Volume log, glog;
    std::string name = nm + "Mid";
    for (int k = 0; k < nAbsorber; k++) {
      std::string namek = name + absorbName[k];
      double rsi = rin + absorbD[k];
      int in = 0;
      for (int i = 0; i < rzones - 1; i++) {
        if (rsi >= rmax[i])
          in = i + 1;
      }
      std::vector<double> pgonZ, pgonRmin, pgonRmax;
      // index 0
      pgonZ.emplace_back(0.0);
      pgonRmin.emplace_back(rsi);
      pgonRmax.emplace_back(rsi + absorbT[k]);
      // index 1
      pgonZ.emplace_back(zoff[in] + rsi * ttheta[in]);
      pgonRmin.emplace_back(rsi);
      pgonRmax.emplace_back(pgonRmax[0]);
      // index 2
      pgonZ.emplace_back(zoff[in] + pgonRmax[0] * ttheta[in]);
      pgonRmin.emplace_back(pgonRmax[1]);
      pgonRmax.emplace_back(pgonRmax[1]);
      solid = dd4hep::Polyhedra(ns.prepend(namek), 1, -alpha, 2 * alpha, pgonZ, pgonRmin, pgonRmax);
      dd4hep::Material matter = ns.material(absorbMat[k]);
      log = dd4hep::Volume(solid.name(), solid, matter);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << matter.name()
                                   << " with 1 sector from " << convertRadToDeg(-alpha) << " to "
                                   << convertRadToDeg(alpha) << " and with " << pgonZ.size() << " sections";
      for (unsigned int ii = 0; ii < pgonZ.size(); ii++)
        edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZ[ii])
                                     << "\tRmin = " << cms::convert2mm(pgonRmin[ii])
                                     << "\tRmax = " << cms::convert2mm(pgonRmax[ii]);
#endif

      laylog.placeVolume(log, 1);
#ifdef EDM_ML_DEBUG
      edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << log.name() << " Number 1 positioned in " << laylog.name()
                                   << " at (0,0,0) with no rotation";
#endif
      if (k == 0) {
        double rmin = pgonRmin[0];
        double rmax = pgonRmax[0];
        dd4hep::Volume mother = log;
        for (int i = 0; i < 1; i++) {
          double alpha1 = atan(midW[i] / rmin);
          std::string namek = name + midName[i];
          solid = dd4hep::Polyhedra(ns.prepend(namek), 1, -alpha1, 2 * alpha1, pgonZ, pgonRmin, pgonRmax);
          dd4hep::Material matter1 = ns.material(midMat[i]);
          log = dd4hep::Volume(solid.name(), solid, matter1);
#ifdef EDM_ML_DEBUG
          edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of "
                                       << matter1.name() << " with 1 sector from " << convertRadToDeg(-alpha1) << " to "
                                       << convertRadToDeg(alpha1) << " and with " << pgonZ.size() << " sections";
          for (unsigned int ii = 0; ii < pgonZ.size(); ii++)
            edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZ[ii])
                                         << "\tRmin = " << cms::convert2mm(pgonRmin[ii])
                                         << "\tRmax = " << cms::convert2mm(pgonRmax[ii]);
#endif

          mother.placeVolume(log, 1);
#ifdef EDM_ML_DEBUG
          edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << log.name() << " Number 1 positioned in "
                                       << mother.name() << " at (0,0,0) with no rotation";
#endif
          mother = log;
        }

        // Now the layer with detectors
        double rmid = rmin + middleD;
        pgonRmin[0] = rmid;
        pgonRmax[0] = rmax;
        pgonRmin[1] = rmid;
        pgonRmax[1] = rmax;
        pgonZ[1] = zoff[in] + rmid * ttheta[in];
        pgonRmin[2] = rmax;
        pgonRmax[2] = rmax;
        pgonZ[2] = zoff[in] + rmax * ttheta[in];
        double alpha1 = atan(middleW / rmin);
        solid = dd4hep::Polyhedra(ns.prepend(name), 1, -alpha1, 2 * alpha1, pgonZ, pgonRmin, pgonRmax);
        dd4hep::Material matter1 = ns.material(middleMat);
        glog = dd4hep::Volume(solid.name(), solid, matter1);
#ifdef EDM_ML_DEBUG
        edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of " << matter1.name()
                                     << " with 1 sector from " << convertRadToDeg(-alpha1) << " to "
                                     << convertRadToDeg(alpha1) << " and with " << pgonZ.size() << " sections";
        for (unsigned int ii = 0; ii < pgonZ.size(); ii++)
          edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZ[ii])
                                       << "\tRmin = " << cms::convert2mm(pgonRmin[ii])
                                       << "\tRmax = " << cms::convert2mm(pgonRmax[ii]);
#endif

        mother.placeVolume(glog, 1);
#ifdef EDM_ML_DEBUG
        edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number 1 positioned in "
                                     << mother.name() << " at (0,0,0) with no rotation";
#endif
        // Now the remaining absorber layers
        for (int i = 1; i < nMidAbs; i++) {
          namek = name + midName[i];
          rmid = rmin + midT[i];
          pgonRmin[0] = rmin;
          pgonRmax[0] = rmid;
          pgonRmin[1] = rmin;
          pgonRmax[1] = rmid;
          pgonZ[1] = zoff[in] + rmin * ttheta[in];
          pgonRmin[2] = rmid;
          pgonRmax[2] = rmid;
          pgonZ[2] = zoff[in] + rmid * ttheta[in];
          alpha1 = atan(midW[i] / rmin);
          solid = dd4hep::Polyhedra(ns.prepend(namek), 1, -alpha1, 2 * alpha1, pgonZ, pgonRmin, pgonRmax);
          dd4hep::Material matter2 = ns.material(midMat[i]);
          log = dd4hep::Volume(solid.name(), solid, matter2);
#ifdef EDM_ML_DEBUG
          edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Polyhedra made of "
                                       << matter2.name() << " with 1 sector from " << convertRadToDeg(-alpha1) << " to "
                                       << convertRadToDeg(alpha1) << " and with " << pgonZ.size() << " sections";
          for (unsigned int ii = 0; ii < pgonZ.size(); ii++)
            edm::LogVerbatim("HCalGeom") << "\t\tZ = " << cms::convert2mm(pgonZ[ii])
                                         << "\tRmin = " << cms::convert2mm(pgonRmin[ii])
                                         << "\tRmax = " << cms::convert2mm(pgonRmax[ii]);
#endif

          mother.placeVolume(log, i);
#ifdef EDM_ML_DEBUG
          edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << log.name() << " Number " << i << " positioned in "
                                       << mother.name() << " at (0,0,0) with no rotation";
#endif
          mother = log;
        }
      }
    }
    return glog;
  }

  void constructInsideDetectors(dd4hep::Volume& detector,
                                const std::string& name,
                                int id,
                                double dx,
                                double dy,
                                double dz,
                                int type,
                                cms::DDNamespace& ns) {
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: \t\tInside detector " << id << "...";
#endif

    dd4hep::Material plmatter = ns.material(detMatPl);
    dd4hep::Material scmatter = ns.material(detMatSc);
    std::string plname = DDSplit(detector.name()).first + "Plastic_";
    std::string scname = idName + "Scintillator" + name;

    id--;
    dd4hep::Solid solid;
    dd4hep::Volume glog;
    double wid, y = 0;
    double dx1, dx2, shiftX;

    if (type == 1) {
      wid = 0.5 * detWidth1[id];
      dx1 = 0.5 * detT11[id];
      dx2 = 0.5 * detT21[id];
      shiftX = detdP1[id];
      if (detPosY[id] > 0)
        y = -dy + wid;
    } else {
      wid = 0.5 * detWidth2[id];
      dx1 = 0.5 * detT12[id];
      dx2 = 0.5 * detT22[id];
      shiftX = detdP2[id];
    }

    solid = dd4hep::Box(ns.prepend(plname + "1"), dx1, wid, dz);
    glog = dd4hep::Volume(solid.name(), solid, plmatter);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Box made of " << plmatter.name()
                                 << " of dimensions " << cms::convert2mm(dx1) << ", " << cms::convert2mm(wid) << ", "
                                 << cms::convert2mm(dz);
#endif

    double x = shiftX + dx1 - dx;
    detector.placeVolume(glog, 1, dd4hep::Position(x, y, 0));
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number 1 positioned in " << detector.name()
                                 << " at (" << cms::convert2mm(x) << "," << cms::convert2mm(y)
                                 << ",0) with no rotation";
#endif
    solid = dd4hep::Box(ns.prepend(scname), 0.5 * detTsc[id], wid, dz);
    glog = dd4hep::Volume(solid.name(), solid, scmatter);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Box made of " << scmatter.name()
                                 << " of dimensions " << cms::convert2mm(0.5 * detTsc[id]) << ", "
                                 << cms::convert2mm(wid) << ", " << cms::convert2mm(dz);
#endif

    x += dx1 + 0.5 * detTsc[id];
    int copyNo = id * 10 + detType[id];
    detector.placeVolume(glog, copyNo, dd4hep::Position(x, y, 0));
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number " << copyNo << " positioned in "
                                 << detector.name() << " at (" << cms::convert2mm(x) << "," << cms::convert2mm(y)
                                 << ",0) with no rotation";
#endif
    solid = dd4hep::Box(ns.prepend(plname + "2"), dx2, wid, dz);
    glog = dd4hep::Volume(solid.name(), solid, plmatter);
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << solid.name() << " Box made of " << plmatter.name()
                                 << " of dimensions " << cms::convert2mm(dx2) << ", " << cms::convert2mm(wid) << ", "
                                 << cms::convert2mm(dz);
#endif

    x += 0.5 * detTsc[id] + dx2;
    detector.placeVolume(glog, 1, dd4hep::Position(x, y, 0));
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "DDHCalBarrelAlgo: " << glog.name() << " Number 1 positioned in " << detector.name()
                                 << " at (" << cms::convert2mm(x) << "," << cms::convert2mm(y)
                                 << ",0) with no rotation";
#endif
  }

  dd4hep::Rotation3D getRotation(std::string& rotation, std::string& rotns, cms::DDNamespace& ns) {
    std::string rot = (strchr(rotation.c_str(), NAMESPACE_SEP) == nullptr) ? (rotns + ":" + rotation) : rotation;
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("HCalGeom") << "getRotation: " << rotation << ":" << rot << ":" << ns.rotation(rot);
#endif
    return ns.rotation(rot);
  }
};

static long algorithm(dd4hep::Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
  HcalBarrelAlgo hcalbarrelalgo(ctxt, e);
#ifdef EDM_ML_DEBUG
  edm::LogVerbatim("HCalGeom") << "<<== End of DDHCalBarrelAlgo construction";
#endif
  return cms::s_executed;
}

// first argument is the type from the xml file
DECLARE_DDCMS_DETELEMENT(DDCMS_hcal_DDHCalBarrelAlgo, algorithm)