diff --git a/GaudiSvc/src/THistSvc/THistSvc.cpp b/GaudiSvc/src/THistSvc/THistSvc.cpp
index ad7b72dcd39d8a5365bd2b6d47c93f86758fe2d4..fa0f0fc9426eba85db59f5317a8444681bf699c9 100644
--- a/GaudiSvc/src/THistSvc/THistSvc.cpp
+++ b/GaudiSvc/src/THistSvc/THistSvc.cpp
@@ -1411,13 +1411,22 @@ StatusCode THistSvc::io_reinit() {
         // migrate the objects to the new file.
         // thanks to the object model of ROOT, it is super easy.
         if ( cl->InheritsFrom( "TTree" ) ) {
-          dynamic_cast<TTree*>( hid.obj )->SetDirectory( newdir );
-          dynamic_cast<TTree*>( hid.obj )->Reset();
+          TTree* tree = dynamic_cast<TTree*>( hid.obj );
+          if (tree) {
+            tree->SetDirectory( newdir );
+            tree->Reset();
+          }
         } else if ( cl->InheritsFrom( "TH1" ) ) {
-          dynamic_cast<TH1*>( hid.obj )->SetDirectory( newdir );
-          dynamic_cast<TH1*>( hid.obj )->Reset();
+          TH1* th1 = dynamic_cast<TH1*>( hid.obj );
+          if (th1) {
+            th1->SetDirectory( newdir );
+            th1->Reset();
+          }
         } else if ( cl->InheritsFrom( "TEfficiency" ) ) {
-          dynamic_cast<TEfficiency*>( hid.obj )->SetDirectory( newdir );
+          TEfficiency* teff = dynamic_cast<TEfficiency*>( hid.obj );
+          if (teff) {
+            teff->SetDirectory( newdir );
+          }
         } else if ( cl->InheritsFrom( "TGraph" ) ) {
           olddir->Remove( hid.obj );
           newdir->Append( hid.obj );
diff --git a/GaudiSvc/src/THistSvc/THistSvc.icc b/GaudiSvc/src/THistSvc/THistSvc.icc
index 1475cc4809c01a03f79d56c16ed6862e38ab010d..4e6424aa957b0475cf072c5fb79e2cfae75f8a01 100644
--- a/GaudiSvc/src/THistSvc/THistSvc.icc
+++ b/GaudiSvc/src/THistSvc/THistSvc.icc
@@ -120,12 +120,12 @@ StatusCode THistSvc::regHist_i( std::unique_ptr<T> hist_unique, const std::strin
     hid.shared      = shared;
     TDirectory* dir = changeDir( hid );
 
-    if ( dynamic_cast<TTree*>( hist ) ) {
-      dynamic_cast<TTree*>( hist )->SetDirectory( dir );
-    } else if ( dynamic_cast<TH1*>( hist ) ) {
-      dynamic_cast<TH1*>( hist )->SetDirectory( dir );
-    } else if ( dynamic_cast<TEfficiency*>( hist ) ) {
-      dynamic_cast<TEfficiency*>( hist )->SetDirectory( dir );
+    if ( TTree* tree = dynamic_cast<TTree*>( hist ) ) {
+      tree->SetDirectory( dir );
+    } else if ( TH1* th1 = dynamic_cast<TH1*>( hist ) ) {
+      th1->SetDirectory( dir );
+    } else if ( TEfficiency* teff = dynamic_cast<TEfficiency*>( hist ) ) {
+      teff->SetDirectory( dir );
     } else if ( dynamic_cast<TGraph*>( hist ) ) {
       dir->Append( hist );
     } else {