From 18db9442b77d683d48c8e35c84b2577bfdfc827d Mon Sep 17 00:00:00 2001 From: Tamas Vami Date: Tue, 3 Sep 2024 12:08:46 -0700 Subject: [PATCH] Introduce fallible to getID functions --- DetDescr/include/DetDescr/EcalGeometry.h | 61 +++++++------ DetDescr/src/DetDescr/EcalGeometry.cxx | 111 ++++++++--------------- Ecal/src/Ecal/EcalVetoProcessor.cxx | 21 ++--- 3 files changed, 79 insertions(+), 114 deletions(-) diff --git a/DetDescr/include/DetDescr/EcalGeometry.h b/DetDescr/include/DetDescr/EcalGeometry.h index 2b1afdaf9..636ddd2f2 100644 --- a/DetDescr/include/DetDescr/EcalGeometry.h +++ b/DetDescr/include/DetDescr/EcalGeometry.h @@ -116,9 +116,17 @@ class EcalGeometry : public framework::ConditionsObject { * @param[in] x global x position [mm] * @param[in] y global y position [mm] * @param[in] z global z position [mm] - * @return EcalID of the cell + * @param[in] fallible bool to decide if the function should fail + * @return EcalID of the cell (null-id if fallible) + * Example for fallible use case + * ```cpp + * auto id = geometry.getID(x, y, z, true); + * if (id.null()) { + * // position (x,y) is not contained within a cell in layer at z + * } + * ``` */ - EcalID getID(double x, double y, double z) const; + EcalID getID(double x, double y, double z, bool fallible = false) const; /** * Get a cell's ID from its x,y global position and layer number @@ -129,9 +137,17 @@ class EcalGeometry : public framework::ConditionsObject { * @param[in] x global x position [mm] * @param[in] y global y position [mm] * @param[in] layer_id integer ID of the layer the hit is in - * @return EcalID of the cell + * @param[in] fallible bool to decide if the function should fail + * @return EcalID of the cell (null-id if fallible) + * Example for fallible use case + * ```cpp + * auto id = geometry.getID(x, y, ilayer, true); + * if (id.null()) { + * // position (x,y) is not contained within a cell in layer ilayer + * } + * ``` */ - EcalID getID(double x, double y, int layer_id) const; + EcalID getID(double x, double y, int layer_id, bool fallible = false) const; /** * Get a cell's ID from its x,y global position and layer/module numbers @@ -145,9 +161,19 @@ class EcalGeometry : public framework::ConditionsObject { * @param[in] y global y position [mm] * @param[in] layer_id integer ID of the layer the hit is in * @param[in] module_id integer ID of the module the hit is in - * @return EcalID of the cell + * @param[in] fallible bool to decide if the function should fail + * @return EcalID of the cell (null-id if fallible) + * Example for fallible use case + * ```cpp + * auto id = geometry.getID(x, y, ilayer, imodule, true); + * if (id.null()) { + * // position (x,y) is not contained within a cell in layer ilayer in + * module imodule + * } + * ``` */ - EcalID getID(double x, double y, int layer_id, int module_id) const; + EcalID getID(double x, double y, int layer_id, int module_id, + bool fallible = false) const; /** * Get a cell's position from its ID number @@ -309,29 +335,6 @@ class EcalGeometry : public framework::ConditionsObject { return new EcalGeometry(p); } - /** - * Is the hit based on its x,y global position and layer numbers - * fiducial in the module? - * - * @param[in] x global x position [mm] - * @param[in] y global y position [mm] - * @param[in] layer_id integer ID of the layer the hit is in - * @return bool if fiducial - */ - bool isFiducialInModule(double x, double y, int layer_id) const; - - /** - * Is the hit based on its x,y global position and layer/module numbers - * fiducial in the cell? - * - * @param[in] x global x position [mm] - * @param[in] y global y position [mm] - * @param[in] layer_id integer ID of the layer the hit is in - * @param[in] module_id integer ID of the module the hit is in - * @return bool if fiducial - */ - bool isFiducialInCell(double x, double y, int layer_id, int module_id) const; - private: /** * Class constructor, for use only by the provider diff --git a/DetDescr/src/DetDescr/EcalGeometry.cxx b/DetDescr/src/DetDescr/EcalGeometry.cxx index 83956ae08..848e093c2 100644 --- a/DetDescr/src/DetDescr/EcalGeometry.cxx +++ b/DetDescr/src/DetDescr/EcalGeometry.cxx @@ -90,7 +90,7 @@ EcalGeometry::EcalGeometry(const framework::config::Parameters& ps) std::cout << "[EcalGeometry] : fully constructed" << std::endl; } -EcalID EcalGeometry::getID(double x, double y, double z) const { +EcalID EcalGeometry::getID(double x, double y, double z, bool fallible) const { static const double tolerance = 0.3; // thickness of Si int layer_id{-1}; for (const auto& [lid, layer_xyz] : layer_pos_xy_) { @@ -100,14 +100,19 @@ EcalID EcalGeometry::getID(double x, double y, double z) const { } } if (layer_id < 0) { - EXCEPTION_RAISE("BadConf", "z = " + std::to_string(z) + - " mm is not within any" - " of the configured layers."); + if (fallible) { + return ldmx::EcalID(0); + } else { + EXCEPTION_RAISE("BadConf", "z = " + std::to_string(z) + + " mm is not within any" + " of the configured layers."); + } } - return getID(x, y, layer_id); + return getID(x, y, layer_id, fallible); } -EcalID EcalGeometry::getID(double x, double y, int layer_id) const { +EcalID EcalGeometry::getID(double x, double y, int layer_id, + bool fallible) const { // now assume we know the layer // shift to center of layer // and convert to flower coordinates @@ -129,21 +134,25 @@ EcalID EcalGeometry::getID(double x, double y, int layer_id) const { } if (module_id < 0) { - EXCEPTION_RAISE( - "BadConf", - TString::Format( - "Coordinates relative to layer (p,q) = (%.2f, %.2f) mm " - "derived from world coordinates (%.2f, %.2f) mm with layer = %d " - "are not inside any module.", - p, q, x, y, layer_id) - .Data()); + if (fallible) { + return ldmx::EcalID(0); + } else { + EXCEPTION_RAISE( + "BadConf", + TString::Format( + "Coordinates relative to layer (p,q) = (%.2f, %.2f) mm " + "derived from world coordinates (%.2f, %.2f) mm with layer = %d " + "are not inside any module.", + p, q, x, y, layer_id) + .Data()); + } } return getID(x, y, layer_id, module_id); } -EcalID EcalGeometry::getID(double x, double y, int layer_id, - int module_id) const { +EcalID EcalGeometry::getID(double x, double y, int layer_id, int module_id, + bool fallible) const { // now assume we know the layer and module // shift to center of layer and then center of module double p{x - std::get<0>(layer_pos_xy_.at(layer_id)) - @@ -158,14 +167,18 @@ EcalID EcalGeometry::getID(double x, double y, int layer_id, int cell_id = cell_id_in_module_.FindBin(p, q) - 1; if (cell_id < 0) { - EXCEPTION_RAISE( - "BadConf", - TString::Format( - "Relative cell coordinates (%.2f, %.2f) mm " - "derived from world coordinates (%.2f, %.2f) mm with layer = %d " - "and module = %d are outside module hexagon", - p, q, x, y, layer_id, module_id) - .Data()); + if (fallible) { + return ldmx::EcalID(0); + } else { + EXCEPTION_RAISE( + "BadConf", + TString::Format( + "Relative cell coordinates (%.2f, %.2f) mm " + "derived from world coordinates (%.2f, %.2f) mm with layer = %d " + "and module = %d are outside module hexagon", + p, q, x, y, layer_id, module_id) + .Data()); + } } return EcalID(layer_id, module_id, cell_id); @@ -629,54 +642,4 @@ bool EcalGeometry::isInside(double normX, double normY) const { return (dotProd > 0.); } -bool EcalGeometry::isFiducialInModule(double x, double y, int layer_id) const { - // now assume we know the layer - // shift to center of layer - // and convert to flower coordinates - double p{x - std::get<0>(layer_pos_xy_.at(layer_id))}, - q{y - std::get<1>(layer_pos_xy_.at(layer_id))}; - - // deduce module ID - // there are only 7 modules so we just loop through them - // all and pick out the module ID that we are inside of - - int module_id{-1}; - for (auto const& [mid, module_xy] : module_pos_xy_) { - double probe_x{p - module_xy.first}, probe_y{q - module_xy.second}; - if (cornersSideUp_) rotate(probe_x, probe_y); - if (isInside(probe_x / moduleR_, probe_y / moduleR_)) { - module_id = mid; - break; - } - } - - if (module_id < 0) { - return false; - } else { - return true; - } -} - -bool EcalGeometry::isFiducialInCell(double x, double y, int layer_id, - int module_id) const { - // now assume we know the layer and module - // shift to center of layer and then center of module - double p{x - std::get<0>(layer_pos_xy_.at(layer_id)) - - module_pos_xy_.at(module_id).first}, - q{y - std::get<1>(layer_pos_xy_.at(layer_id)) - - module_pos_xy_.at(module_id).second}; - - // need to rotate - if (cornersSideUp_) rotate(p, q); - - // deduce cell ID - int cell_id = cell_id_in_module_.FindBin(p, q) - 1; - - if (cell_id < 0) { - return false; - } else { - return true; - } -} - } // namespace ldmx diff --git a/Ecal/src/Ecal/EcalVetoProcessor.cxx b/Ecal/src/Ecal/EcalVetoProcessor.cxx index 4d26ce1fc..b1636714f 100644 --- a/Ecal/src/Ecal/EcalVetoProcessor.cxx +++ b/Ecal/src/Ecal/EcalVetoProcessor.cxx @@ -669,20 +669,20 @@ void EcalVetoProcessor::produce(framework::Event &event) { // Check if it's fiducial bool inside{false}; // At module level - const auto isFiducialInModule = geometry_->isFiducialInModule( - drifted_recoil_x, drifted_recoil_y, recoil_layer_index); - if (isFiducialInModule) { - const auto ecalID = geometry_->getID(drifted_recoil_x, drifted_recoil_y, - recoil_layer_index); + const auto ecalID = geometry_->getID(drifted_recoil_x, drifted_recoil_y, + recoil_layer_index, true); + if (!ecalID.null()) { // If fiducial at module level, check at cell level - std::cout << " ecalID " << ecalID << std::endl; - inside = - geometry_->isFiducialInCell(drifted_recoil_x, drifted_recoil_y, - recoil_layer_index, ecalID.getModuleID()); + const auto cellID = + geometry_->getID(drifted_recoil_x, drifted_recoil_y, recoil_layer_index, + ecalID.getModuleID(), true); + if (!cellID.null()) { + inside = true; + } } if (!inside) { - ldmx_log(debug) << "This event is non-fiducial in ECAL"; + ldmx_log(info) << "This event is non-fiducial in ECAL"; } // ------------------------------------------------------ @@ -799,7 +799,6 @@ void EcalVetoProcessor::produce(framework::Event &event) { // in v14 minR is 4.17 mm // while maxR is 4.81 mm float cellWidth = 2 * geometry_->getCellMaxR(); - std::cout << " cellWidth " << cellWidth << std::endl; for (int iHit = 0; iHit < trackingHitList.size(); iHit++) { // list of hit numbers in track (34 = maximum theoretical length) int track[34];