Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-20 08:47:07

0001 // Copyright (C) 2023 Sebouh Paul
0002 // SPDX-License-Identifier: LGPL-3.0-or-later
0003 
0004 // References:
0005 //   https://arxiv.org/abs/2308.06939
0006 
0007 #include <DD4hep/Alignments.h>
0008 #include <DD4hep/DetElement.h>
0009 #include <DD4hep/Objects.h>
0010 #include <DD4hep/VolumeManager.h>
0011 #include <Evaluator/DD4hepUnits.h>
0012 #include <Math/GenVector/Cartesian3D.h>
0013 #include <Math/GenVector/DisplacementVector3D.h>
0014 #include <edm4hep/Vector3f.h>
0015 #include <algorithm>
0016 #include <cmath>
0017 #include <cstdlib>
0018 #include <numbers>
0019 #include <tuple>
0020 #include <vector>
0021 
0022 #include "HEXPLIT.h"
0023 #include "algorithms/calorimetry/HEXPLITConfig.h"
0024 
0025 namespace eicrecon {
0026 
0027 //positions where the overlapping cells are relative to a given cell (in units of hexagon side length)
0028 const std::vector<double> HEXPLIT::neighbor_offsets_x_H4 = []() {
0029   std::vector<double> x;
0030   double rs[2]      = {1.5, std::numbers::sqrt3 / 2.};
0031   double offsets[2] = {0, M_PI / 2};
0032   for (int i = 0; i < 2; i++) {
0033     for (int j = 0; j < 6; j += 1) {
0034       x.push_back(rs[i] * cos(j * M_PI / 3 + offsets[i]));
0035     }
0036   }
0037   return x;
0038 }();
0039 
0040 const std::vector<double> HEXPLIT::neighbor_offsets_y_H4 = []() {
0041   std::vector<double> y;
0042   double rs[2]      = {1.5, std::numbers::sqrt3 / 2.};
0043   double offsets[2] = {0, M_PI / 2};
0044   for (int i = 0; i < 2; i++) {
0045     for (int j = 0; j < 6; j += 1) {
0046       y.push_back(rs[i] * sin(j * M_PI / 3 + offsets[i]));
0047     }
0048   }
0049   return y;
0050 }();
0051 
0052 //indices of the neighboring cells which overlap to produce a given subcell
0053 const std::vector<std::vector<int>> HEXPLIT::neighbor_indices_H4 = {
0054     {0, 11, 10}, {1, 6, 11}, {2, 7, 6}, {3, 8, 7},  {4, 9, 8},   {5, 10, 9},
0055     {6, 11, 7},  {7, 6, 8},  {8, 7, 9}, {9, 8, 10}, {10, 9, 11}, {11, 10, 6}};
0056 
0057 //positions of the centers of subcells
0058 const std::vector<double> HEXPLIT::subcell_offsets_x_H4 = []() {
0059   std::vector<double> x;
0060   double rs[2]      = {0.75, std::numbers::sqrt3 / 4.};
0061   double offsets[2] = {0, M_PI / 2};
0062   for (int i = 0; i < 2; i++) {
0063     for (int j = 0; j < 6; j += 1) {
0064       x.push_back(rs[i] * cos(j * M_PI / 3 + offsets[i]));
0065     }
0066   }
0067   return x;
0068 }();
0069 
0070 const std::vector<double> HEXPLIT::subcell_offsets_y_H4 = []() {
0071   std::vector<double> y;
0072   double rs[2]      = {0.75, std::numbers::sqrt3 / 4.};
0073   double offsets[2] = {0, M_PI / 2};
0074   for (int i = 0; i < 2; i++) {
0075     for (int j = 0; j < 6; j += 1) {
0076       y.push_back(rs[i] * sin(j * M_PI / 3 + offsets[i]));
0077     }
0078   }
0079   return y;
0080 }();
0081 
0082 const std::vector<double> HEXPLIT::neighbor_offsets_x_S2 = {1, -1, -1, 1};
0083 const std::vector<double> HEXPLIT::neighbor_offsets_y_S2 = {1, 1, -1, -1};
0084 
0085 const std::vector<std::vector<int>> HEXPLIT::neighbor_indices_S2 = {{0}, {1}, {2}, {3}};
0086 
0087 const std::vector<double> HEXPLIT::subcell_offsets_x_S2 = {0.5, -0.5, -0.5, 0.5};
0088 const std::vector<double> HEXPLIT::subcell_offsets_y_S2 = {0.5, 0.5, -0.5, -0.5};
0089 
0090 void HEXPLIT::init() {
0091   if (m_cfg.stag_type == HEXPLITConfig::StaggerType::H4) {
0092     stag = stag_H4;
0093   } else if (m_cfg.stag_type == HEXPLITConfig::StaggerType::S2) {
0094     stag = stag_S2;
0095   } else if (m_cfg.stag_type == HEXPLITConfig::StaggerType::H3) {
0096     error("H3 staggering not implemented yet in EICrecon");
0097   }
0098 }
0099 
0100 void HEXPLIT::process(const HEXPLIT::Input& input, const HEXPLIT::Output& output) const {
0101 
0102   const auto [hits]  = input;
0103   auto [subcellHits] = output;
0104 
0105   double MIP   = m_cfg.MIP / dd4hep::GeV;
0106   double delta = m_cfg.delta_in_MIPs * MIP;
0107   double Emin  = m_cfg.Emin_in_MIPs * MIP;
0108 
0109   auto volman = m_detector->volumeManager();
0110 
0111   for (const auto& hit : *hits) {
0112     //skip hits that do not pass E and t cuts
0113     if (hit.getEnergy() < Emin) {
0114       continue;
0115     }
0116 
0117     //keep track of the energy in each neighboring cell
0118     std::vector<double> Eneighbors(stag.NEIGHBORS, 0.0);
0119 
0120     double sl = hit.getDimension().x / 2.;
0121     for (const auto& other_hit : *hits) {
0122       // maximum distance between where the neighboring cell is and where it should be
0123       // based on an ideal geometry using the staggered tessellation pattern.
0124       // Deviations could arise from rounding errors or from detector misalignment.
0125       double tol = 0.1; // in units of side lengths.
0126 
0127       //only look at hits nearby within two layers of the current layer
0128       int dz = std::abs(hit.getLayer() - other_hit.getLayer());
0129       if (dz > 2 || dz == 0) {
0130         continue;
0131       }
0132       if (other_hit.getEnergy() < Emin) {
0133         continue;
0134       }
0135       //difference in transverse position (in units of side lengths)
0136       double dx = (other_hit.getLocal().x - hit.getLocal().x) / sl;
0137       double dy = (other_hit.getLocal().y - hit.getLocal().y) / sl;
0138       if (std::abs(dx) > 2 || std::abs(dy) > std::numbers::sqrt3) {
0139         continue;
0140       }
0141 
0142       //loop over locations of the neighboring cells
0143       //and check if the jth hit matches this location
0144       for (int k = 0; k < stag.NEIGHBORS; k++) {
0145         if (std::abs(dx - stag.neighbor_offsets_x[k]) < tol &&
0146             std::abs(dy - stag.neighbor_offsets_y[k]) < tol) {
0147           Eneighbors[k] += other_hit.getEnergy();
0148           break;
0149         }
0150       }
0151     }
0152 
0153     double weights[12];
0154     for (int k = 0; k < stag.NEIGHBORS; k++) {
0155       Eneighbors[k] = std::max(Eneighbors[k], delta);
0156     }
0157     double sum_weights = 0;
0158     if (m_cfg.stag_type == HEXPLITConfig::StaggerType::H4)
0159       for (int k = 0; k < stag.SUBCELLS; k++) {
0160         weights[k] = Eneighbors[stag.neighbor_indices[k][0]] *
0161                      Eneighbors[stag.neighbor_indices[k][1]] *
0162                      Eneighbors[stag.neighbor_indices[k][2]];
0163         sum_weights += weights[k];
0164       }
0165     else if (m_cfg.stag_type == HEXPLITConfig::StaggerType::S2) {
0166       for (int k = 0; k < stag.SUBCELLS; k++) {
0167         weights[k] = Eneighbors[stag.neighbor_indices[k][0]];
0168         sum_weights += weights[k];
0169       }
0170     } else if (m_cfg.stag_type == HEXPLITConfig::StaggerType::H3) {
0171       for (int k = 0; k < stag.SUBCELLS; k++) {
0172         weights[k] =
0173             Eneighbors[stag.neighbor_indices[k][0]] * Eneighbors[stag.neighbor_indices[k][1]];
0174         sum_weights += weights[k];
0175       }
0176     }
0177     for (int k = 0; k < stag.SUBCELLS; k++) {
0178 
0179       //create the subcell hits.  First determine their positions in local coordinates.
0180       const decltype(edm4eic::CalorimeterHitData::local) local(
0181           hit.getLocal().x + stag.subcell_offsets_x[k] * sl,
0182           hit.getLocal().y + stag.subcell_offsets_y[k] * sl, hit.getLocal().z);
0183 
0184       //convert this to a position object so that the global position can be determined
0185       dd4hep::Position local_position;
0186       local_position.SetX(local.x * dd4hep::mm);
0187       local_position.SetY(local.y * dd4hep::mm);
0188       local_position.SetZ(local.z * dd4hep::mm);
0189 
0190       dd4hep::Position global_position;
0191       try {
0192 
0193         //also convert this to the detector's global coordinates.  To do: check if this is correct
0194         auto alignment = volman.lookupDetElement(hit.getCellID()).nominal();
0195 
0196         global_position = alignment.localToWorld(local_position);
0197 
0198       } catch (...) {
0199         // do this to prevent errors when running the test on the mock detector
0200         warning("Cannot find transformation from local to global coordinates.");
0201         global_position = local_position;
0202       }
0203 
0204       //convert this from position object to a vector object
0205       const decltype(edm4eic::CalorimeterHitData::position) position = {
0206           static_cast<float>(global_position.X() / dd4hep::mm),
0207           static_cast<float>(global_position.Y() / dd4hep::mm),
0208           static_cast<float>(global_position.Z() / dd4hep::mm)};
0209 
0210       //bounding box dimensions depend on the orientation of the rhombus
0211       int orientation = static_cast<int>(k % 3 == 0);
0212       const decltype(edm4eic::CalorimeterHitData::dimension) dimension(
0213           sl * (orientation != 0 ? 1 : 1.5),
0214           sl * std::numbers::sqrt3 / 2. * (orientation != 0 ? 2 : 1), hit.getDimension()[2]);
0215 
0216       subcellHits->create(hit.getCellID(), hit.getEnergy() * weights[k] / sum_weights, 0,
0217                           hit.getTime(), 0, position, dimension, hit.getSector(), hit.getLayer(),
0218                           local);
0219     }
0220   }
0221 }
0222 
0223 } // namespace eicrecon