From a04366fb18c08fd797a44da253bb6059b39c14fb Mon Sep 17 00:00:00 2001
From: Vakho Tsulaia <tsulaia@cern.ch>
Date: Fri, 31 Jan 2025 12:47:12 -0800
Subject: [PATCH] Dropped GeoModelKernel/Query.h and switched to direct usage
 std::optional

---
 .../GeoModelHelpers/tests/testFullPhysVol.cxx |   6 +-
 .../GeoModelKernel/GeoAccessVolAndSTAction.h  |   5 +-
 .../GeoModelKernel/GeoAccessVolumeAction.h    |   2 +-
 .../GeoModelKernel/GeoNodeAction.h            |   4 +-
 .../GeoModelKernel/GeoShapeAction.h           |   6 +-
 .../GeoModelKernel/GeoTraversalState.h        |   6 +-
 .../GeoModelKernel/GeoUnidentifiedShape.h     |   4 +-
 .../GeoModelKernel/GeoVFullPhysVol.h          |   2 +-
 .../GeoModelKernel/GeoVPhysVol.h              |  12 +-
 .../GeoModelKernel/GeoVolumeCursor.h          |   2 +-
 .../GeoModelKernel/GeoModelKernel/Query.h     |  49 ---
 .../src/GeoAccessVolAndSTAction.cxx           |   6 +-
 .../src/GeoAccessVolumeAction.cxx             |   6 +-
 .../GeoModelKernel/src/GeoCutVolAction.cxx    |   4 +-
 .../GeoModelKernel/src/GeoFullPhysVol.cxx     |   4 +-
 .../GeoModelKernel/src/GeoNodeAction.cxx      |   4 +-
 .../GeoModelKernel/src/GeoPhysVol.cxx         |   4 +-
 .../GeoModelKernel/src/GeoShapeAction.cxx     |   4 +-
 .../src/GeoShapeIntersection.cxx              |   2 +-
 .../GeoModelKernel/src/GeoShapeShift.cxx      |   2 +-
 .../src/GeoShapeSubtraction.cxx               |   2 +-
 .../GeoModelKernel/src/GeoShapeUnion.cxx      |   2 +-
 .../GeoModelKernel/src/GeoTraversalState.cxx  |   4 +-
 .../src/GeoUnidentifiedShape.cxx              |   3 +-
 .../GeoModelKernel/src/GeoVFullPhysVol.cxx    |   8 +-
 .../GeoModelKernel/src/GeoVPhysVol.cxx        |   8 +-
 .../GeoModelKernel/src/GeoVolumeCursor.cxx    |   6 +-
 .../GeoModelKernel/tests/testQuery.cxx        |  78 -----
 .../src/ExtParameterisedVolumeBuilder.cxx     |  14 +-
 .../GeoModelXml/src/GmxInterface.cxx          |   1 -
 .../VP1GeometrySystems/src/VolumeHandle.cxx   |   4 +-
 .../patches/0001-Drop-GeoModel-Query.patch    | 306 ++++++++++++++++++
 32 files changed, 371 insertions(+), 199 deletions(-)
 delete mode 100644 GeoModelCore/GeoModelKernel/GeoModelKernel/Query.h
 delete mode 100644 GeoModelCore/GeoModelKernel/tests/testQuery.cxx
 create mode 100644 athena_ci/patches/0001-Drop-GeoModel-Query.patch

diff --git a/GeoModelCore/GeoModelHelpers/tests/testFullPhysVol.cxx b/GeoModelCore/GeoModelHelpers/tests/testFullPhysVol.cxx
index 4b1d3e6bf..b834b3018 100644
--- a/GeoModelCore/GeoModelHelpers/tests/testFullPhysVol.cxx
+++ b/GeoModelCore/GeoModelHelpers/tests/testFullPhysVol.cxx
@@ -87,13 +87,13 @@ int main(int argc, char *argv[]){
     PVConstLink cParent{parent->getParent()};
     parent.reset();
     while (cParent) {        
-        Query<int> query = cParent->getIdOfChildVol(0);
-        if (!query.isValid()) {
+      std::optional<int> query = cParent->getIdOfChildVol(0);
+        if (!query) {
             std::cerr<<__FILE__<<":"<<__LINE__<<" Failed to obtain a valid child volume ID. Expected "<<currentK
                      <<" "<<typeid(*cParent->getChildVol(0)).name()<<std::endl;
             return EXIT_FAILURE;
         }
-        unsigned int parentID = query;
+        unsigned int parentID = *query;
         if (parentID != currentK) {
             std::cerr<<__FILE__<<":"<<__LINE__<<" Expected "<<currentK<<" but got "<<parentID;
             return EXIT_FAILURE;
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolAndSTAction.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolAndSTAction.h
index 3789197b8..e59c29131 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolAndSTAction.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolAndSTAction.h
@@ -43,7 +43,7 @@ class GeoAccessVolAndSTAction final : public GeoNodeAction {
 
   virtual void handleIdentifierTag(const GeoIdentifierTag* idTag) override final;
 
-  Query<unsigned int> getId() const;
+  std::optional<unsigned int> getId() const;
 
  private:
   GeoAccessVolAndSTAction(const GeoAccessVolAndSTAction &right);
@@ -65,9 +65,6 @@ class GeoAccessVolAndSTAction final : public GeoNodeAction {
   /// The name of the volume.  From a nametag or a serial denominator
   mutable std::string m_name;
 
-  /// The identifier of the volume.  From an identifier tag.
-  mutable Query<unsigned int> m_id;
-
   /// A pointer to a name tag.  If the volume is named.
   const GeoNameTag *m_nameTag{nullptr};
   const GeoSerialDenominator *m_serialDenominator{nullptr};
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolumeAction.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolumeAction.h
index 113568fd1..7a563dac6 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolumeAction.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoAccessVolumeAction.h
@@ -64,7 +64,7 @@ class GeoAccessVolumeAction final : public GeoNodeAction
   virtual void handleIdentifierTag (const GeoIdentifierTag *idTag) override;
 
   /// Returns the id of the volume.  From the identifier.
-  Query<int> getId () const;
+  std::optional<int> getId () const;
 
   /// Handles an Serial Identifier
   virtual void handleSerialIdentifier(const GeoSerialIdentifier *sI) override;
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoNodeAction.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoNodeAction.h
index a26100d86..b3b4b81ed 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoNodeAction.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoNodeAction.h
@@ -90,7 +90,7 @@ class GeoNodeAction
   //	Returns a pointer to the path object.
   GeoNodePath* getPath ();
   
-  Query<unsigned int>  getDepthLimit ();
+  std::optional<unsigned int>  getDepthLimit ();
   
   //	Cause the action to terminate.
   void terminate ();
@@ -115,7 +115,7 @@ class GeoNodeAction
   
   //	A limit may be placed upon the depth to which the action
   //	descends.  0 = self.  1 = self and children.
-  Query<unsigned int> m_depth{};
+  std::optional<unsigned int> m_depth{};
 
   GeoNodePath m_path{};  
 };
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoShapeAction.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoShapeAction.h
index 94280aa97..964fc544a 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoShapeAction.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoShapeAction.h
@@ -14,8 +14,8 @@
  *	This is essentially an internal class.
  */
 
-#include "GeoModelKernel/Query.h"
 #include "GeoModelKernel/GeoShapePath.h"
+#include <optional>
 
 class GeoShape;
 class GeoShapeIntersection;
@@ -52,7 +52,7 @@ class GeoShapeAction
   GeoShapeAction();
   virtual ~GeoShapeAction();
 
-  Query<unsigned int>  getDepthLimit ();
+  std::optional<unsigned int>  getDepthLimit ();
 
   //	Sets a depth limit.
   void setDepthLimit (unsigned int limit);
@@ -143,7 +143,7 @@ class GeoShapeAction
 
   //	A limit may be placed upon the depth to which the action
   //	descends.  0 = self.  1 = self and children.
-  Query<unsigned int> m_depth;
+  std::optional<unsigned int> m_depth;
   
   //	Termination flag; causes an abortion of action execution.
   bool m_terminate;
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoTraversalState.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoTraversalState.h
index 1a084ab2c..16b4f3c6b 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoTraversalState.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoTraversalState.h
@@ -70,10 +70,10 @@ class GeoTraversalState
   const GeoNodePath * getPath () const;
   
   //	Sets the identifier for the current node.
-  void setId (const Query<int> &id);
+  void setId (const std::optional<int> &id);
   
   //	Gets the id of the current node.
-  const Query<int> getId () const;
+  const std::optional<int> getId () const;
 
  private:
   GeoTraversalState(const GeoTraversalState &right);
@@ -109,7 +109,7 @@ class GeoTraversalState
   std::string m_name;
   
   //	And identifier for this volume.
-  Query<int> m_id;
+  std::optional<int> m_id;
   
   //	The path from the point at which the action started, to
   //	the current node.
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoUnidentifiedShape.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoUnidentifiedShape.h
index 47a3c5957..5271b6d55 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoUnidentifiedShape.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoUnidentifiedShape.h
@@ -17,7 +17,7 @@
 #ifndef _GeoUnidentifiedShape_h_
 #define _GeoUnidentifiedShape_h_
 #include "GeoModelKernel/GeoShape.h"
-#include "GeoModelKernel/Query.h"
+#include <optional>
 #include <string>
 
 class GeoUnidentifiedShape: public GeoShape {
@@ -79,7 +79,7 @@ class GeoUnidentifiedShape: public GeoShape {
 
   const std::string         _name;
   const std::string         _asciiData;
-  const Query<double>       _volume;
+  const std::optional<double>       _volume;
   static const std::string         _classType;
   static const ShapeType           _classTypeID;
 
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVFullPhysVol.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVFullPhysVol.h
index dabee2af5..37e28a1f9 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVFullPhysVol.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVFullPhysVol.h
@@ -27,7 +27,7 @@ class GeoVFullPhysVol : public GeoVPhysVol, public GeoNodePositioning {
   mutable std::string m_absName{};
 
   /// An identifier. This is locally cached in a full physical volume.
-  mutable std::unique_ptr<Query<int>> m_id{nullptr};
+  mutable std::unique_ptr<std::optional<int>> m_id{nullptr};
 
  
 };
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVPhysVol.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVPhysVol.h
index 4639844de..a29bbc47c 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVPhysVol.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVPhysVol.h
@@ -7,8 +7,6 @@
 
 #include "GeoModelKernel/GeoIntrusivePtr.h"
 #include "GeoModelKernel/GeoDefinitions.h"
-#include "GeoModelKernel/Query.h"
-
 
 #include "GeoModelKernel/GeoLogVol.h"
 #include "GeoModelKernel/GeoPlacement.h"
@@ -17,6 +15,8 @@
 
 #include <string>
 #include <shared_mutex>
+#include <optional>
+
 class GeoVolumeAction;
 class GeoVAlignmentStore;
 
@@ -29,10 +29,8 @@ class GeoVPhysVol: public GeoPlacement {
   
 
 
-    /// Returns the index of a specific daughter volume.  The Query class can be used
-    /// just like an unsigned int, but it provides and isValid() method to determine
-    /// whether it is valid and throws an exception if an invalid value is blithely used.
-    Query<unsigned int> indexOf(const PVConstLink& daughter) const;
+    /// Returns the index of a specific daughter volume.
+    std::optional<unsigned int> indexOf(const PVConstLink& daughter) const;
 
     /// Returns the logical volume.
     const GeoLogVol* getLogVol() const {
@@ -61,7 +59,7 @@ class GeoVPhysVol: public GeoPlacement {
 
  
     /// Returns the id of the child.
-    Query<int> getIdOfChildVol(unsigned int i) const;
+    std::optional<int> getIdOfChildVol(unsigned int i) const;
 
     /// Returns the number of child physical volumes and Serial Transformers.
     unsigned int getNChildVolAndST() const;
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVolumeCursor.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVolumeCursor.h
index bdd3db174..81af70df1 100644
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVolumeCursor.h
+++ b/GeoModelCore/GeoModelKernel/GeoModelKernel/GeoVolumeCursor.h
@@ -44,7 +44,7 @@ class GeoVolumeCursor final : public GeoNodeAction
   std::string getName () const;
       
   /// Returns the id of the volume.  From the identifier.
-  Query<int> getId () const;
+  std::optional<int> getId () const;
 
   bool hasAlignableTransform() const;
 
diff --git a/GeoModelCore/GeoModelKernel/GeoModelKernel/Query.h b/GeoModelCore/GeoModelKernel/GeoModelKernel/Query.h
deleted file mode 100644
index 60dfaf7f3..000000000
--- a/GeoModelCore/GeoModelKernel/GeoModelKernel/Query.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
-  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
-*/
-
-#ifndef GEOMODELKERNEL_QUERY_H
-#define GEOMODELKERNEL_QUERY_H
-
-/**
- * @class Query
- *
- * @brief  This class can be used to return the result of a query      
- * that can fail.  Objects of this class can convert themselves to the       
- * result type T.  Client code can check for a valid result; if it doesn't,  
- * and the result is valid, no problem.   But if the result is invalid you   
- * will print an error message and terminate.                                
- *
- * Joe Boudreau
- * based on Barton & Nackman's "Scientific and Engineering C++"              
- */
-
-#include <GeoModelKernel/throwExcept.h>
-#include <optional>
-template < class T > class Query: public std::optional<T>
-{
-
-public:
-  /// Use all constructors from the std::optional
-  using std::optional<T>::optional;
-
-  // Convert to "T"  
-  inline operator T () const;
-
-  // Test Validity 
-  inline bool isValid () const;
-
-};
-
-template < class T > inline Query < T >::operator T () const {
-  if (!this->isValid()){
-    THROW_EXCEPTION("Nothing has been saved in query of type "<<typeid(T).name());
-  }
-  return this->value_or(T{});
-}
-template < class T > inline bool Query < T >::isValid () const {
-  return this->has_value();
-}
-
-
-#endif
diff --git a/GeoModelCore/GeoModelKernel/src/GeoAccessVolAndSTAction.cxx b/GeoModelCore/GeoModelKernel/src/GeoAccessVolAndSTAction.cxx
index a7b1ac2a3..5f365e83d 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoAccessVolAndSTAction.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoAccessVolAndSTAction.cxx
@@ -164,13 +164,13 @@ void GeoAccessVolAndSTAction::handleIdentifierTag(const GeoIdentifierTag *idTag)
   m_idTag = idTag;
 }
 
-Query<unsigned int> GeoAccessVolAndSTAction::getId() const
+std::optional<unsigned int> GeoAccessVolAndSTAction::getId() const
 {
   if(m_idTag) {
-    return Query<unsigned int>(m_idTag->getIdentifier());
+    return std::optional<unsigned int>(m_idTag->getIdentifier());
   }
   else {
-    return Query<unsigned int>();
+    return std::nullopt;
   }
 }
 
diff --git a/GeoModelCore/GeoModelKernel/src/GeoAccessVolumeAction.cxx b/GeoModelCore/GeoModelKernel/src/GeoAccessVolumeAction.cxx
index 95c969fd4..251d1f925 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoAccessVolumeAction.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoAccessVolumeAction.cxx
@@ -185,12 +185,12 @@ void GeoAccessVolumeAction::handleIdentifierTag (const GeoIdentifierTag *idTag)
   m_serialIdentPosition = 0;
 }
 
-Query<int> GeoAccessVolumeAction::getId () const {
+std::optional<int> GeoAccessVolumeAction::getId () const {
   if(m_idTag) {
-    return Query<int>(m_idTag->getIdentifier());
+    return std::optional<int>(m_idTag->getIdentifier());
   }
   else if(m_serialIdentifier) {
-    return Query<int>(m_index - m_serialIdentPosition + m_serialIdentifier->getBaseId());
+    return std::optional<int>(m_index - m_serialIdentPosition + m_serialIdentifier->getBaseId());
   }
   return std::nullopt;
 }
diff --git a/GeoModelCore/GeoModelKernel/src/GeoCutVolAction.cxx b/GeoModelCore/GeoModelKernel/src/GeoCutVolAction.cxx
index 806b31604..9af0dd7fd 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoCutVolAction.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoCutVolAction.cxx
@@ -64,8 +64,8 @@ void GeoCutVolAction::handleVPhysVol(const GeoVPhysVol *vPV)
     std::string pvName = getState()->getName();
     if(!pvName.empty()) copyParent->add(make_intrusive<GeoNameTag>(pvName));
 
-    const Query<int> pvId = getState()->getId();
-    if(pvId.isValid()) copyParent->add(make_intrusive<GeoIdentifierTag>(pvId));
+    const std::optional<int> pvId = getState()->getId();
+    if(pvId) copyParent->add(make_intrusive<GeoIdentifierTag>(*pvId));
 
     copyParent->add(make_intrusive<GeoTransform>(getState()->getTransform()));
     copyParent->add(pvNew);
diff --git a/GeoModelCore/GeoModelKernel/src/GeoFullPhysVol.cxx b/GeoModelCore/GeoModelKernel/src/GeoFullPhysVol.cxx
index c3c09efc7..cab4fd21b 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoFullPhysVol.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoFullPhysVol.cxx
@@ -19,7 +19,7 @@ void GeoFullPhysVol::exec(GeoNodeAction *action) const {
   // Put this node on the head of the path:   bb 
   //    
   action->getPath()->push(this);
-  if(action->getDepthLimit().isValid()
+  if(action->getDepthLimit()
      && action->getPath()->getLength()-1 > action->getDepthLimit()) {
     action->getPath()->pop();
     return;
@@ -36,7 +36,7 @@ void GeoFullPhysVol::exec(GeoNodeAction *action) const {
   //    
   // Pass this on to the children.    
   //    
-  if(action->getDepthLimit().isValid()
+  if(action->getDepthLimit()
      && action->getPath()->getLength() > action->getDepthLimit()) {
   }
   else {
diff --git a/GeoModelCore/GeoModelKernel/src/GeoNodeAction.cxx b/GeoModelCore/GeoModelKernel/src/GeoNodeAction.cxx
index c0aced898..efa666bd2 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoNodeAction.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoNodeAction.cxx
@@ -27,7 +27,7 @@ GeoNodePath* GeoNodeAction::getPath ()
   return &m_path;
 }
 
-Query<unsigned int>  GeoNodeAction::getDepthLimit ()
+std::optional<unsigned int>  GeoNodeAction::getDepthLimit ()
 {
   return m_depth;
 }
@@ -84,4 +84,4 @@ void GeoNodeAction::handleAnnulusSurface (const GeoAnnulusSurface *annulus) {
 }
 
 void GeoNodeAction::handleDiamondSurface (const GeoDiamondSurface *diamond) {
-}
\ No newline at end of file
+}
diff --git a/GeoModelCore/GeoModelKernel/src/GeoPhysVol.cxx b/GeoModelCore/GeoModelKernel/src/GeoPhysVol.cxx
index b0c74835c..c38cc7948 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoPhysVol.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoPhysVol.cxx
@@ -21,7 +21,7 @@ void GeoPhysVol::exec(GeoNodeAction *action) const {
   // Put this node on the head of the path:    
   //    
   action->getPath()->push(this);
-  if(action->getDepthLimit().isValid()
+  if(action->getDepthLimit()
      && action->getPath()->getLength() - 1 > action->getDepthLimit()) {
     action->getPath ()->pop ();
     return;
@@ -37,7 +37,7 @@ void GeoPhysVol::exec(GeoNodeAction *action) const {
   //    
   // Pass this on to the children.    
   //    
-  if(action->getDepthLimit().isValid()
+  if(action->getDepthLimit()
      && action->getPath()->getLength() > action->getDepthLimit()) {
   }
   else {
diff --git a/GeoModelCore/GeoModelKernel/src/GeoShapeAction.cxx b/GeoModelCore/GeoModelKernel/src/GeoShapeAction.cxx
index 36b7f656c..38618ca14 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoShapeAction.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoShapeAction.cxx
@@ -39,7 +39,7 @@ GeoShapeAction::~GeoShapeAction()
 {
 }
 
-Query<unsigned int>  GeoShapeAction::getDepthLimit ()
+std::optional<unsigned int>  GeoShapeAction::getDepthLimit ()
 {
   return m_depth;
 }
@@ -51,7 +51,7 @@ void GeoShapeAction::setDepthLimit (unsigned int limit)
 
 void GeoShapeAction::clearDepthLimit ()
 {
-  m_depth=Query<unsigned int> ();
+  m_depth=std::nullopt;
 }
 
 void GeoShapeAction::terminate ()
diff --git a/GeoModelCore/GeoModelKernel/src/GeoShapeIntersection.cxx b/GeoModelCore/GeoModelKernel/src/GeoShapeIntersection.cxx
index 71b9f8d75..556c49bdf 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoShapeIntersection.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoShapeIntersection.cxx
@@ -55,7 +55,7 @@ void GeoShapeIntersection::exec (GeoShapeAction *action) const
     return;
   }
 
-  if (action->getDepthLimit ().isValid ()
+  if (action->getDepthLimit()
       && action->getPath ()->getLength () > action->getDepthLimit ())
   {
   }
diff --git a/GeoModelCore/GeoModelKernel/src/GeoShapeShift.cxx b/GeoModelCore/GeoModelKernel/src/GeoShapeShift.cxx
index 1d3c7306e..3454ffb05 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoShapeShift.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoShapeShift.cxx
@@ -77,7 +77,7 @@ void GeoShapeShift::exec (GeoShapeAction *action) const {
     return;
   }
 
-  if (action->getDepthLimit ().isValid ()
+  if (action->getDepthLimit()
       && action->getPath ()->getLength () > action->getDepthLimit ())
   {
   }
diff --git a/GeoModelCore/GeoModelKernel/src/GeoShapeSubtraction.cxx b/GeoModelCore/GeoModelKernel/src/GeoShapeSubtraction.cxx
index 7eebbfbf2..c203ad664 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoShapeSubtraction.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoShapeSubtraction.cxx
@@ -64,7 +64,7 @@ void GeoShapeSubtraction::exec (GeoShapeAction *action) const
     return;
   }
 
-  if (action->getDepthLimit ().isValid ()
+  if (action->getDepthLimit()
       && action->getPath ()->getLength () > action->getDepthLimit ())
   {
   }
diff --git a/GeoModelCore/GeoModelKernel/src/GeoShapeUnion.cxx b/GeoModelCore/GeoModelKernel/src/GeoShapeUnion.cxx
index f26055a03..86600c19e 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoShapeUnion.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoShapeUnion.cxx
@@ -71,7 +71,7 @@ void GeoShapeUnion::exec (GeoShapeAction *action) const
     return;
   }
 
-  if (action->getDepthLimit ().isValid ()
+  if (action->getDepthLimit()
       && action->getPath ()->getLength () > action->getDepthLimit ())
   {
   }
diff --git a/GeoModelCore/GeoModelKernel/src/GeoTraversalState.cxx b/GeoModelCore/GeoModelKernel/src/GeoTraversalState.cxx
index 054f2d7eb..3c7077760 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoTraversalState.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoTraversalState.cxx
@@ -95,12 +95,12 @@ const GeoNodePath * GeoTraversalState::getPath () const
   return &m_path;
 }
 
-void GeoTraversalState::setId (const Query<int> &id)
+void GeoTraversalState::setId (const std::optional<int> &id)
 {
   m_id=id;
 }
 
-const Query<int> GeoTraversalState::getId () const
+const std::optional<int> GeoTraversalState::getId () const
 {
   return m_id;
 }
diff --git a/GeoModelCore/GeoModelKernel/src/GeoUnidentifiedShape.cxx b/GeoModelCore/GeoModelKernel/src/GeoUnidentifiedShape.cxx
index 77433f524..2fd63d529 100644
--- a/GeoModelCore/GeoModelKernel/src/GeoUnidentifiedShape.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoUnidentifiedShape.cxx
@@ -2,7 +2,6 @@
   Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
 */
 #include "GeoModelKernel/GeoUnidentifiedShape.h"
-#include "GeoModelKernel/Query.h"
 #include "GeoModelKernel/GeoShapeAction.h"
 #include "GeoModelKernel/throwExcept.h"
 #include <stdexcept>
@@ -40,7 +39,7 @@ const std::string & GeoUnidentifiedShape::asciiData() const {
 
 // Returns the volume of the shape, for mass inventory
 double GeoUnidentifiedShape::volume () const {
-  return _volume;
+  return *_volume;
 }
 
 // Returns the bonding box of the shape
diff --git a/GeoModelCore/GeoModelKernel/src/GeoVFullPhysVol.cxx b/GeoModelCore/GeoModelKernel/src/GeoVFullPhysVol.cxx
index 472570979..5e5b8823c 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoVFullPhysVol.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoVFullPhysVol.cxx
@@ -31,7 +31,7 @@ const std::string &  GeoVFullPhysVol::getAbsoluteName () const
     std::string tProd;
 
     while (parent) {
-      int index = parent->indexOf (child);
+      int index = *(parent->indexOf(child));
       std::string name = parent->getNameOfChildVol (index);
 
       tProd = tProd + "/" + name;
@@ -73,9 +73,9 @@ unsigned int GeoVFullPhysVol::getId () const
       parent = child->getParent();
     }
 
-    int index = parent->indexOf(child);
-    m_id = std::make_unique<Query<int>>(parent->getIdOfChildVol(index));
+    int index = *(parent->indexOf(child));
+    m_id = std::make_unique<std::optional<int>>(parent->getIdOfChildVol(index));
   }
-  return *m_id;
+  return **m_id;
 }
 
diff --git a/GeoModelCore/GeoModelKernel/src/GeoVPhysVol.cxx b/GeoModelCore/GeoModelKernel/src/GeoVPhysVol.cxx
index 11a554b67..f446e31b4 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoVPhysVol.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoVPhysVol.cxx
@@ -15,12 +15,12 @@
 GeoVPhysVol::GeoVPhysVol(const GeoLogVol* LogVol): 
     m_logVol(LogVol) {}
 
-Query<unsigned int> GeoVPhysVol::indexOf(const PVConstLink& daughter) const {
+std::optional<unsigned int> GeoVPhysVol::indexOf(const PVConstLink& daughter) const {
   unsigned int nChildVols{getNChildVols()};
   for(unsigned int i=0; i<nChildVols; i++) { // To Do: replace this with Volume Iterator!
-    if(getChildVol(i) == daughter) return i;
+    if(getChildVol(i) == daughter) return std::optional<unsigned int>{i};
   }
-  return Query<unsigned int >();
+  return std::nullopt;
 }
 
 void GeoVPhysVol::apply(GeoVolumeAction *action) const {
@@ -111,7 +111,7 @@ std::string GeoVPhysVol::getNameOfChildVol(unsigned int i) const {
   return av.getName();
 }
 
-Query<int> GeoVPhysVol::getIdOfChildVol(unsigned int i) const {
+std::optional<int> GeoVPhysVol::getIdOfChildVol(unsigned int i) const {
   GeoAccessVolumeAction    av(i,nullptr);
   exec(&av);
   return av.getId();
diff --git a/GeoModelCore/GeoModelKernel/src/GeoVolumeCursor.cxx b/GeoModelCore/GeoModelKernel/src/GeoVolumeCursor.cxx
index c17850808..a6908b4cb 100755
--- a/GeoModelCore/GeoModelKernel/src/GeoVolumeCursor.cxx
+++ b/GeoModelCore/GeoModelKernel/src/GeoVolumeCursor.cxx
@@ -255,12 +255,12 @@ std::string  GeoVolumeCursor::getName () const
   return name;
 }
 
-Query<int> GeoVolumeCursor::getId () const {
+std::optional<int> GeoVolumeCursor::getId () const {
   if (m_idTag) {
-    return Query<int>{m_idTag->getIdentifier ()};
+    return std::optional<int>{m_idTag->getIdentifier ()};
   }
   else if (m_serialIdentifier) {
-    return Query<int>{m_volCount - m_serialIdentPosition - 1 + m_serialIdentifier->getBaseId()};
+    return std::optional<int>{m_volCount - m_serialIdentPosition - 1 + m_serialIdentifier->getBaseId()};
   }
   return std::nullopt;
 }
diff --git a/GeoModelCore/GeoModelKernel/tests/testQuery.cxx b/GeoModelCore/GeoModelKernel/tests/testQuery.cxx
deleted file mode 100644
index 61cc302d8..000000000
--- a/GeoModelCore/GeoModelKernel/tests/testQuery.cxx
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
-  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
- */
-#include "GeoModelKernel/Query.h"
-#include "GeoModelKernel/throwExcept.h"
-
-#include <stdlib.h>
-#include <iostream>
-
-
-#define RETURN_FAILURE(MSG) {                                     \
-    std::cerr<<__FILE__<<":"<<__LINE__<<" -- "<<MSG<<std::endl;   \
-    return EXIT_FAILURE;                                          \
-}
-int main() {
-
-    {
-        /// Test that the default constructor generates an invalid query
-        Query<int> emptyQuery{};
-        if (emptyQuery.isValid()){
-            RETURN_FAILURE("Empty query is not supposed to be valid." );
-        }
-    }
-    {
-        /// Test that the nullopt constructor does what it's supposed to do
-        Query<int>emptyQuery{std::nullopt};
-        if (emptyQuery.isValid()){
-            RETURN_FAILURE("std::nullopt query is not supposed to be valid." );
-        }
-    }
-
-    Query<int> initQuery{5};
-    if (!initQuery.isValid()){
-        RETURN_FAILURE("Query is supposed to be valid... And have value of 5." );
-    }
-    if (initQuery != 5) {
-        RETURN_FAILURE("Query is valid but has not 5 but instead "<<initQuery);
-    }
-    if (initQuery != 5) {
-        RETURN_FAILURE("Query is valid but has not 5 but instead "<<initQuery);
-    }
-    Query<int> copyQuery{initQuery};
-    if (!copyQuery.isValid()){
-        RETURN_FAILURE("The copy query is constructed from another valid one but is invalid...");
-    }
-    if (copyQuery.value_or(-9999) != initQuery.value_or(9999)){
-        RETURN_FAILURE("The copy query stores "<<copyQuery<<" while the other one has "<<initQuery);
-    }
-    copyQuery = std::nullopt;
-    if (copyQuery.isValid()){
-        RETURN_FAILURE("After setting back to nullopt it's supposed to be invalid." );
-    }
-    copyQuery = 7;
-    if (!copyQuery.isValid()){
-        RETURN_FAILURE("Expect the query to be valid." );
-    }
-    if (copyQuery != 7) {
-        RETURN_FAILURE("Expect the query to be 7 and not "<<copyQuery); 
-    }
-    copyQuery = std::move(initQuery);
-    if (copyQuery != 5) {
-        RETURN_FAILURE("Expect the query to be 5 and not "<<copyQuery); 
-    }
-    /// Apparently, the move on an optional does not invalidate the optional (https://stackoverflow.com/questions/51805059/why-does-moving-stdoptional-not-reset-state)
-    ///if (initQuery.isValid()) {
-    ///    RETURN_FAILURE("Initial query needs to be invalid and not "<<initQuery); 
-    ///}
-    initQuery = copyQuery;
-    if (!initQuery.isValid()) {
-        RETURN_FAILURE("Initial query needs to be valid again"); 
-    }
-    if (copyQuery.value_or(-9999) != initQuery.value_or(9999)){
-        RETURN_FAILURE("The copy query stores "<<copyQuery<<" while the other one has "<<initQuery);
-    }
-    std::cout<<__FILE__<<": - Test is passed. "<<std::endl;
-    return EXIT_SUCCESS;
-}
-
diff --git a/GeoModelG4/GeoModel2G4/src/ExtParameterisedVolumeBuilder.cxx b/GeoModelG4/GeoModel2G4/src/ExtParameterisedVolumeBuilder.cxx
index 9ba77d970..6287c9123 100644
--- a/GeoModelG4/GeoModel2G4/src/ExtParameterisedVolumeBuilder.cxx
+++ b/GeoModelG4/GeoModel2G4/src/ExtParameterisedVolumeBuilder.cxx
@@ -129,15 +129,15 @@ G4LogicalVolume* ExtParameterisedVolumeBuilder::Build(const PVConstLink theGeoPh
           // Get child phys volume
           theGeoPhysChild = av.getVolume();
           // Get its transform
-            G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(av.getTransform()));
+	  G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(av.getTransform()));
 
-          Query<int> Qint =  av.getId();
-          if(Qint.isValid()) id = Qint;
+	  std::optional<int> Qint =  av.getId();
+          if(Qint) id = *Qint;
           if(m_matEther->getName()  == theGeoPhysChild->getLogVol()->getMaterial()->getName() || hasEnding(theGeoPhysChild->getLogVol()->getMaterial()->getName(), "Ether"))
             {
               Geo2G4AssemblyVolume* assembly = BuildAssembly(theGeoPhysChild);
 
-              if(Qint.isValid())
+              if(Qint)
                 assembly->MakeImprint(theG4LogVolume,theG4Position,id);
               else
                 assembly->MakeImprint(theG4LogVolume,theG4Position);
@@ -146,7 +146,7 @@ G4LogicalVolume* ExtParameterisedVolumeBuilder::Build(const PVConstLink theGeoPh
             {
               Geo2G4AssemblyVolume* assembly = BuildAssembly(theGeoPhysChild);
 
-              if(Qint.isValid())
+              if(Qint)
                 assembly->MakeImprint(theG4LogVolume,theG4Position,id,true);
               else
                 assembly->MakeImprint(theG4LogVolume,theG4Position,0,true);
@@ -224,7 +224,7 @@ Geo2G4AssemblyVolume* ExtParameterisedVolumeBuilder::BuildAssembly(PVConstLink p
         }
       else
         {
-          Query<int> Qint =  av.getId();
+	  std::optional<int> Qint =  av.getId();
 
           // Build the child
           if(!(theG4LogChild = Build(theGeoPhysChild))) return 0;
@@ -233,7 +233,7 @@ Geo2G4AssemblyVolume* ExtParameterisedVolumeBuilder::BuildAssembly(PVConstLink p
             G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(av.getTransform()));
 
           int placedID = 0;
-          if(Qint.isValid()) placedID = Qint;
+          if(Qint) placedID = *Qint;
 
           std::string placedName = nameChild=="ANON" ? "" : nameChild;
 
diff --git a/GeoModelTools/GeoModelXML/GeoModelXml/src/GmxInterface.cxx b/GeoModelTools/GeoModelXML/GeoModelXml/src/GmxInterface.cxx
index 94d7b9a3f..33004cfb0 100644
--- a/GeoModelTools/GeoModelXML/GeoModelXml/src/GmxInterface.cxx
+++ b/GeoModelTools/GeoModelXML/GeoModelXml/src/GmxInterface.cxx
@@ -13,7 +13,6 @@
 //
 #include "GeoModelXml/GmxInterface.h"
 #include "OutputDirector.h"
-#include "GeoModelKernel/Query.h"
 #include "GeoModelKernel/GeoPhysVol.h"
 #include "GeoModelKernel/GeoVFullPhysVol.h"
 #include "GeoModelKernel/GeoPublisher.h"
diff --git a/GeoModelVisualization/VP1GeometrySystems/src/VolumeHandle.cxx b/GeoModelVisualization/VP1GeometrySystems/src/VolumeHandle.cxx
index 518786679..ed22daa81 100644
--- a/GeoModelVisualization/VP1GeometrySystems/src/VolumeHandle.cxx
+++ b/GeoModelVisualization/VP1GeometrySystems/src/VolumeHandle.cxx
@@ -597,8 +597,8 @@ int VolumeHandle::copyNumber() const
   int i(0);//We need to check the childNumber since volumes in a replica have same volume link
   while (!av.atEnd()) {
     if (m_childNumber==i&&m_d->pV==av.getVolume()) {
-       Query<int> Qint = av.getId();
-       return Qint.isValid() ? int(Qint) : -1;//-1 for "Invalid"
+      std::optional<int> Qint = av.getId();
+      return Qint ? *Qint : -1;//-1 for "Invalid"
     }
     av.next();
     ++i;
diff --git a/athena_ci/patches/0001-Drop-GeoModel-Query.patch b/athena_ci/patches/0001-Drop-GeoModel-Query.patch
new file mode 100644
index 000000000..dbe8c48e8
--- /dev/null
+++ b/athena_ci/patches/0001-Drop-GeoModel-Query.patch
@@ -0,0 +1,306 @@
+diff --git a/DetectorDescription/GeoModel/GeoModelExamples/src/ToyDetectorTool.cxx b/DetectorDescription/GeoModel/GeoModelExamples/src/ToyDetectorTool.cxx
+index cc24b6dafa6..c161a849744 100755
+--- a/DetectorDescription/GeoModel/GeoModelExamples/src/ToyDetectorTool.cxx
++++ b/DetectorDescription/GeoModel/GeoModelExamples/src/ToyDetectorTool.cxx
+@@ -67,7 +67,7 @@ void ToyDetectorTool::printVolume(GeoPVConstLink volume, int level /*= 0*/)
+     GeoPVConstLink physChild = cursor.getVolume();
+     GeoTrf::Transform3D position = cursor.getTransform();
+     for(int k{0};k<level;++k) std::cout << "... ";
+-    std::cout << cursor.getName() << " " << (cursor.getId().isValid()?std::to_string(cursor.getId()).c_str():"N/A") 
++    std::cout << cursor.getName() << " " << (cursor.getId()?std::to_string(cursor.getId().value()).c_str():"N/A") 
+ 	      << " Transform:" << "\n";
+     for(int i{0};i<3;++i) {
+       for(int j{0};j<4;++j) {
+diff --git a/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/MuonReadoutElement.cxx b/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/MuonReadoutElement.cxx
+index dc0cdd381f1..ecdace13def 100644
+--- a/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/MuonReadoutElement.cxx
++++ b/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/MuonReadoutElement.cxx
+@@ -107,11 +107,12 @@ namespace MuonGM {
+             ATH_MSG_FATAL(__FILE__<<":"<<__LINE__<<" No parent station found for "<<m_idHelperSvc->toStringDetEl(identify()));
+             throw std::runtime_error("Parent station is a nullptr");
+         }
+-        Query<unsigned int> c = par->indexOf(getMaterialGeom());
+-        if (c.isValid()) {
+-            m_indexOfREinMuonStation = (int)c;
+-        } else
++	std::optional<unsigned int> c = par->indexOf(getMaterialGeom());
++        if (c) {
++	  m_indexOfREinMuonStation = (int)(*c);
++        } else {
+             m_indexOfREinMuonStation = -999;
++	}
+     }
+ 
+     Amg::Transform3D MuonReadoutElement::toParentStation() const {
+diff --git a/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/sTgcReadoutElement.cxx b/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/sTgcReadoutElement.cxx
+index eb5c3fdff6d..26f2d085d04 100644
+--- a/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/sTgcReadoutElement.cxx
++++ b/MuonSpectrometer/MuonDetDescr/MuonReadoutGeometry/src/sTgcReadoutElement.cxx
+@@ -83,7 +83,7 @@ namespace MuonGM {
+       IRDBRecordset_ptr nswPars   = accessSvc->getRecordsetPtr("NSWPARS","","");
+ 
+       PVConstLink parent = getMaterialGeom()->getParent();
+-      unsigned int index=parent->indexOf(getMaterialGeom());
++      unsigned int index=parent->indexOf(getMaterialGeom()).value();
+       std::string pVName=parent->getNameOfChildVol(index);
+       float yCutoutCathode(0);
+       if (nswPars->size()==0) {
+diff --git a/MuonSpectrometer/MuonDetDescr/MuonTrackingGeometry/src/MuonStationBuilderImpl.cxx b/MuonSpectrometer/MuonDetDescr/MuonTrackingGeometry/src/MuonStationBuilderImpl.cxx
+index 0e222e44bcf..b3364ee51f6 100644
+--- a/MuonSpectrometer/MuonDetDescr/MuonTrackingGeometry/src/MuonStationBuilderImpl.cxx
++++ b/MuonSpectrometer/MuonDetDescr/MuonTrackingGeometry/src/MuonStationBuilderImpl.cxx
+@@ -863,17 +863,19 @@ MuonStationBuilderImpl::retrieveGMsensitive(const MuonGM::MuonDetectorManager* m
+ 
+                 if (it == sensitive.end()) {
+                     std::vector<std::pair<Amg::Transform3D, int>> cloneList;
+-                    cloneList.push_back(std::make_pair(transform, vol.getId()));
++                    cloneList.push_back(std::make_pair(transform, vol.getId().value()));
+                     sensitive.push_back(std::make_pair(tv, cloneList));
+                 } else {
+                     Amg::Transform3D transf = transform;
+                     // order transforms to position prototype at phi=0/ 0.125 pi
+                     double phiTr = transf.translation().phi();
+-                    if (phiTr > -0.001 && phiTr < 0.4)
+-                        (*it).second.insert((*it).second.begin(),
+-                                            std::make_pair(transform, vol.getId()));
+-                    else
+-                        (*it).second.push_back(std::make_pair(transform, vol.getId()));
++                    if (phiTr > -0.001 && phiTr < 0.4) {
++		      (*it).second.insert((*it).second.begin(),
++					  std::make_pair(transform, vol.getId().value()));
++		    }
++                    else {
++		      (*it).second.push_back(std::make_pair(transform, vol.getId().value()));
++		    }
+                 }
+ 
+             }  // end loop over TGC
+@@ -892,17 +894,19 @@ MuonStationBuilderImpl::retrieveGMsensitive(const MuonGM::MuonDetectorManager* m
+ 
+             if (it == sensitive.end()) {
+                 std::vector<std::pair<Amg::Transform3D, int>> cloneList;
+-                cloneList.push_back(std::make_pair(vol.getTransform(), vol.getId()));
++                cloneList.push_back(std::make_pair(vol.getTransform(), vol.getId().value()));
+                 sensitive.push_back(std::make_pair(cv, cloneList));
+             } else {
+                 Amg::Transform3D transf = vol.getTransform();
+                 // order transforms to position prototype at phi=0/ 0.125 pi
+                 double phiTr = transf.translation().phi();
+-                if (phiTr > -0.001 && phiTr < 0.4)
+-                    (*it).second.insert((*it).second.begin(),
+-                                        std::make_pair(vol.getTransform(), vol.getId()));
+-                else
+-                    (*it).second.push_back(std::make_pair(vol.getTransform(), vol.getId()));
++                if (phiTr > -0.001 && phiTr < 0.4) {
++		  (*it).second.insert((*it).second.begin(),
++				      std::make_pair(vol.getTransform(), vol.getId().value()));
++		}
++                else {
++		  (*it).second.push_back(std::make_pair(vol.getTransform(), vol.getId().value()));
++		}
+             }
+         }  // end non-TGC
+         vol.next();
+@@ -1271,4 +1275,4 @@ void MuonStationBuilderImpl::checkLayerId(std::string_view comment, const MuonGM
+         }
+     }
+ }
+-}
+\ No newline at end of file
++}
+diff --git a/MuonSpectrometer/MuonValidation/MuonDQA/MuonRawDataMonitoring/MdtRawDataMonitoring/src/MdtRawDataMonAlg.cxx b/MuonSpectrometer/MuonValidation/MuonDQA/MuonRawDataMonitoring/MdtRawDataMonitoring/src/MdtRawDataMonAlg.cxx
+index 23c3bc7d8d7..eeea37e9d5a 100755
+--- a/MuonSpectrometer/MuonValidation/MuonDQA/MuonRawDataMonitoring/MdtRawDataMonitoring/src/MdtRawDataMonAlg.cxx
++++ b/MuonSpectrometer/MuonValidation/MuonDQA/MuonRawDataMonitoring/MdtRawDataMonitoring/src/MdtRawDataMonAlg.cxx
+@@ -1244,8 +1244,8 @@ void MdtRawDataMonAlg::initDeadChannels(const MuonGM::MdtReadoutElement* mydetEl
+         for (int tube = 1; tube <= mydetEl->getNtubesperlayer(); ++tube) {
+             bool tubefound = false;
+             for (unsigned int kk = 0; kk < cv->getNChildVols(); ++kk) {
+-                int tubegeo = cv->getIdOfChildVol(kk) % maxNTubesPerLayer;
+-                int layergeo = (cv->getIdOfChildVol(kk) - tubegeo) / maxNTubesPerLayer;
++	        int tubegeo = cv->getIdOfChildVol(kk).value() % maxNTubesPerLayer;
++	        int layergeo = (cv->getIdOfChildVol(kk).value() - tubegeo) / maxNTubesPerLayer;
+                 if (tubegeo == tube && layergeo == layer) {
+                     tubefound = true;
+                     break;
+diff --git a/Simulation/G4Utilities/Geo2G4/src/ExtParameterisedVolumeBuilder.cxx b/Simulation/G4Utilities/Geo2G4/src/ExtParameterisedVolumeBuilder.cxx
+index a702e36bdc8..9d69e84c9ff 100644
+--- a/Simulation/G4Utilities/Geo2G4/src/ExtParameterisedVolumeBuilder.cxx
++++ b/Simulation/G4Utilities/Geo2G4/src/ExtParameterisedVolumeBuilder.cxx
+@@ -128,8 +128,8 @@ G4LogicalVolume* ExtParameterisedVolumeBuilder::Build(const PVConstLink theGeoPh
+           // Get its transform
+           G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(av.getTransform()));
+ 
+-          Query<int> Qint =  av.getId();
+-          if(Qint.isValid()) id = Qint;
++	  std::optional<int> Qint =  av.getId();
++          if(Qint) id = *Qint;
+ 
+ 	  bool isEther = theGeoPhysChild->getLogVol()->getMaterial()->getName().compare("special::Ether")==0;
+ 	  bool isHypUr = theGeoPhysChild->getLogVol()->getMaterial()->getName().compare("special::HyperUranium")==0;
+@@ -137,7 +137,7 @@ G4LogicalVolume* ExtParameterisedVolumeBuilder::Build(const PVConstLink theGeoPh
+ 	  if(isEther) {
+ 	    Geo2G4AssemblyVolume* assembly = BuildAssembly(theGeoPhysChild);
+ 	    
+-	    if(Qint.isValid()) {
++	    if(Qint) {
+ 	      assembly->MakeImprint(theG4LogVolume,theG4Position,id);
+ 	    }
+ 	    else {
+@@ -147,7 +147,7 @@ G4LogicalVolume* ExtParameterisedVolumeBuilder::Build(const PVConstLink theGeoPh
+           else if(isHypUr) {
+ 	    Geo2G4AssemblyVolume* assembly = BuildAssembly(theGeoPhysChild);
+ 	    
+-	    if(Qint.isValid()) {
++	    if(Qint) {
+ 	      assembly->MakeImprint(theG4LogVolume,theG4Position,id,true);
+ 	    }
+ 	    else {
+@@ -222,7 +222,7 @@ Geo2G4AssemblyVolume* ExtParameterisedVolumeBuilder::BuildAssembly(const PVConst
+ 	assemblyVolume->AddPlacedAssembly(theG4AssemblyChild,theG4Position);
+       }
+       else {
+-	Query<int> Qint =  av.getId();
++	std::optional<int> Qint =  av.getId();
+ 
+ 	// Build the child
+ 	if(!(theG4LogChild = Build(theGeoPhysChild))) return nullptr;
+@@ -231,7 +231,7 @@ Geo2G4AssemblyVolume* ExtParameterisedVolumeBuilder::BuildAssembly(const PVConst
+ 	G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(av.getTransform()));
+ 	
+ 	int placedID = 0;
+-	if(Qint.isValid()) placedID = Qint;
++	if(Qint) placedID = *Qint;
+ 	
+ 	std::string placedName = nameChild=="ANON" ? "" : nameChild;
+ 
+diff --git a/Simulation/G4Utilities/Geo2G4/src/Geo2G4Builder.cxx b/Simulation/G4Utilities/Geo2G4/src/Geo2G4Builder.cxx
+index 70c3bf4be2d..6a5e86cfdb1 100644
+--- a/Simulation/G4Utilities/Geo2G4/src/Geo2G4Builder.cxx
++++ b/Simulation/G4Utilities/Geo2G4/src/Geo2G4Builder.cxx
+@@ -123,18 +123,18 @@ G4LogicalVolume* Geo2G4Builder::BuildTree()
+       for(unsigned int i=0; i<m_treeTops.size(); i++) {
+         // Current Tree Top and its index
+         PVConstLink pv = m_treeTops[i];
+-        Query<unsigned int> childIndx = world->indexOf(pv);
++	std::optional<unsigned int> childIndx = world->indexOf(pv);
+ 
+         // Tree Top transformation
+-        G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(world->getXToChildVol(childIndx)));
++        G4Transform3D theG4Position(Amg::EigenTransformToCLHEP(world->getXToChildVol(*childIndx)));
+ 
+         // Copy number
+         int id = 16969;
+-        Query<int> Qint = world->getIdOfChildVol(childIndx);
+-        if(Qint.isValid()) id = Qint;
++	std::optional<int> Qint = world->getIdOfChildVol(*childIndx);
++        if(Qint) id = *Qint;
+ 
+         // PV Tree Top name
+-        std::string nameTT =  world->getNameOfChildVol(childIndx);
++        std::string nameTT =  world->getNameOfChildVol(*childIndx);
+         if (nameTT == "ANON") nameTT = pv->getLogVol()->getName();
+ 
+ 
+diff --git a/Simulation/ISF/ISF_Fatras/ISF_FatrasToolsMS/src/SimHitCreatorMS.cxx b/Simulation/ISF/ISF_Fatras/ISF_FatrasToolsMS/src/SimHitCreatorMS.cxx
+index 1fa804192e0..9467a63226a 100644
+--- a/Simulation/ISF/ISF_Fatras/ISF_FatrasToolsMS/src/SimHitCreatorMS.cxx
++++ b/Simulation/ISF/ISF_Fatras/ISF_FatrasToolsMS/src/SimHitCreatorMS.cxx
+@@ -535,8 +535,8 @@ void iFatras::SimHitCreatorMS::initDeadChannels(const MuonGM::MdtReadoutElement*
+     for(int tube = 1; tube <= mydetEl->getNtubesperlayer(); tube++){
+       bool tubefound = false;
+       for(unsigned int kk=0; kk < cv->getNChildVols(); kk++) {
+-        int tubegeo = cv->getIdOfChildVol(kk) % maxNTubesPerLayer;
+-        int layergeo = ( cv->getIdOfChildVol(kk) - tubegeo ) / maxNTubesPerLayer;
++        int tubegeo = cv->getIdOfChildVol(kk).value() % maxNTubesPerLayer;
++        int layergeo = ( cv->getIdOfChildVol(kk).value() - tubegeo ) / maxNTubesPerLayer;
+         if( tubegeo == tube && layergeo == layer ) {
+           tubefound=true;
+           break;
+diff --git a/graphics/VP1/VP1Systems/VP1CaloSystems/src/VP1MbtsHelper.cxx b/graphics/VP1/VP1Systems/VP1CaloSystems/src/VP1MbtsHelper.cxx
+index 88d1162a1a8..8a72e9d9784 100644
+--- a/graphics/VP1/VP1Systems/VP1CaloSystems/src/VP1MbtsHelper.cxx
++++ b/graphics/VP1/VP1Systems/VP1CaloSystems/src/VP1MbtsHelper.cxx
+@@ -151,7 +151,7 @@ void VP1MbtsHelper::systemcreate(StoreGateSvc* detstore)
+     {
+       scin1Exists = true;
+       pvScin1 = child;
+-      int copyNo=  cursor2.getId();
++      int copyNo=  cursor2.getId().value();
+ 
+       aTransforms1[copyNo] = xfLArECA * xfMbtsMother * cursor2.getTransform();
+       cTransforms1[copyNo] = xfLArECC * xfMbtsMother * cursor2.getTransform();
+@@ -160,7 +160,7 @@ void VP1MbtsHelper::systemcreate(StoreGateSvc* detstore)
+     {
+       scin2Exists = true;
+       pvScin2 = child;
+-      int copyNo=  cursor2.getId();
++      int copyNo=  cursor2.getId().value();
+ 
+       aTransforms2[copyNo] = xfLArECA * xfMbtsMother * cursor2.getTransform();
+       cTransforms2[copyNo] = xfLArECC * xfMbtsMother * cursor2.getTransform();
+@@ -173,7 +173,7 @@ void VP1MbtsHelper::systemcreate(StoreGateSvc* detstore)
+     while(!cursor2a.atEnd()) {                                                                                    
+       PVConstLink pvAirEnv = cursor2a.getVolume();                                                               
+       if(pvAirEnv->getLogVol()->getName().find("MBTSAirEnv")!=std::string::npos) {                                
+-       int copyNo =  cursor2a.getId();                                                                           
++       int copyNo =  cursor2a.getId().value();                                                                           
+                                                                                                                    
+        // **** Find Aluminun Envelope ****                                                                         
+        GeoVolumeCursor cursor3(pvAirEnv);                                                                        
+diff --git a/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VP1GeometrySystem.cxx b/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VP1GeometrySystem.cxx
+index 9ab023e1a0c..f36cb624423 100644
+--- a/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VP1GeometrySystem.cxx
++++ b/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VP1GeometrySystem.cxx
+@@ -1066,15 +1066,15 @@ void VP1GeometrySystem::userPickedNode(SoNode* , SoPath *pickedPath)
+     parentVH = parentVH->parent();
+     PVConstLink parentPVLink = parentVH ? parentVH->geoPVConstLink() : childVH->geoPVConstLink()->getParent();
+     if (parentPVLink) {
+-      int indexOfChild = parentVH ? childVH->childNumber() : (int)parentPVLink->indexOf(childVH->geoPVConstLink());
++      int indexOfChild = parentVH ? childVH->childNumber() : parentPVLink->indexOf(childVH->geoPVConstLink()).value();
+       
+       std::string childPVName = parentPVLink->getNameOfChildVol(indexOfChild);
+       QString pathEntry = childPVName=="ANON" ? detFactoryName+childVH->getName() : QString(childPVName.c_str());
+       
+-      Query<int> childCopyNo = parentPVLink->getIdOfChildVol(indexOfChild);
+-      if(childCopyNo.isValid()) {
++      std::optional<int> childCopyNo = parentPVLink->getIdOfChildVol(indexOfChild);
++      if(childCopyNo) {
+ 	    QString strCopyNo;
+-	    strCopyNo.setNum(childCopyNo);
++	    strCopyNo.setNum(*childCopyNo);
+ 	    pathEntry += ("::"+strCopyNo);
+       }
+       partspectPath.push(pathEntry);
+diff --git a/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VolumeHandle.cxx b/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VolumeHandle.cxx
+index 83f6e4decb8..dab8014c6b5 100644
+--- a/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VolumeHandle.cxx
++++ b/graphics/VP1/VP1Systems/VP1GeometrySystems/src/VolumeHandle.cxx
+@@ -469,8 +469,8 @@ int VolumeHandle::copyNumber() const
+   int i(0);//We need to check the childNumber since volumes in a replica have same volume link
+   while (!av.atEnd()) {
+     if (m_childNumber==i&&m_d->pV==av.getVolume()) {
+-       Query<int> Qint = av.getId();
+-       return Qint.isValid() ? int(Qint) : -1;//-1 for "Invalid"
++       std::optional<int> Qint = av.getId();
++       return Qint ? *Qint : -1;//-1 for "Invalid"
+     }
+     av.next();
+     ++i;
+diff --git a/graphics/VP1/VP1Systems/VP1RawDataSystems/src/VP1RawDataHandle_BCM_RDO.cxx b/graphics/VP1/VP1Systems/VP1RawDataSystems/src/VP1RawDataHandle_BCM_RDO.cxx
+index 12e57eb71f0..dd4d95a0392 100644
+--- a/graphics/VP1/VP1Systems/VP1RawDataSystems/src/VP1RawDataHandle_BCM_RDO.cxx
++++ b/graphics/VP1/VP1Systems/VP1RawDataSystems/src/VP1RawDataHandle_BCM_RDO.cxx
+@@ -137,9 +137,9 @@ void VP1RawDataHandle_BCM_RDO::Imp::ensureInitModuleInfo()
+ 	  int bcmModLogCopyNumber(-1);
+ 	  if (pv.getVolume()->getLogVol()->getName()=="bcmModLog") {
+ 	    Amg::Transform3D tr_bcmmod = pv.getTransform();
+-	    Query<int> Qint = pv.getId();
+-	    if (Qint.isValid()) {
+-	      bcmModLogCopyNumber = int(Qint);
++	    std::optional<int> Qint = pv.getId();
++	    if (Qint) {
++	      bcmModLogCopyNumber = *Qint;
+ 	      ModuleInfo * modInfo = new ModuleInfo(bcmModLogCopyNumber - 951);
+ 	      GeoVolumeCursor bv(pv.getVolume());
+ 	      while (!bv.atEnd()) {
-- 
GitLab