From 9c11e257a06639ca9e949e517a6ee97aa388b5f7 Mon Sep 17 00:00:00 2001
From: Default Online user <Markus.Frank@cern.ch>
Date: Mon, 15 Nov 2021 18:48:01 +0100
Subject: [PATCH] Streamline OnlineAlign

---
 CMakeLists.txt                              |   6 +-
 Online/GauchoBase/CMakeLists.txt            |   3 +-
 Online/GauchoBase/Gaucho/MonHist.h          |   6 +-
 Online/OnlineAlign/CMakeLists.txt           |  21 +-
 Online/OnlineAlign/OnlineAlign/IAlignSys.h  |  55 +++--
 Online/OnlineAlign/OnlineAlign/IAlignUser.h |  40 ++--
 Online/OnlineAlign/src/AlignDrv.cpp         | 221 ++++++++++---------
 Online/OnlineAlign/src/AlignDrv.h           |  61 +++---
 Online/OnlineAlign/src/AlignWork.cpp        | 178 ++++++++--------
 Online/OnlineAlign/src/AlignWork.h          | 103 +++++----
 Online/OnlineAlign/src/Fitter.cpp           | 223 +++++++++-----------
 Online/OnlineAlign/src/Fitter.h             |  73 ++++---
 Online/OnlineAlign/src/FitterFcn.cpp        | 173 +++++++--------
 Online/OnlineAlign/src/FitterFcn.h          |  60 +++---
 Online/OnlineAlign/src/IFitter.h            |  15 --
 Online/OnlineAlign/src/IFitterFcn.h         |  23 --
 Online/RawBankSizes/src/MakePages.cpp       |  21 +-
 Online/RawBankSizes/src/ParseHistParams.cpp |  14 +-
 18 files changed, 641 insertions(+), 655 deletions(-)
 delete mode 100644 Online/OnlineAlign/src/IFitter.h
 delete mode 100644 Online/OnlineAlign/src/IFitterFcn.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0e3d6fb05..5c5387963 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,9 +21,9 @@ if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" )
 #  add_compile_options(-Wno-unknown-warning-option)
 endif()
 #
-### set(Gaudi_EXACT_VERSION "36.1" CACHE STRING "")
+set(Gaudi_EXACT_VERSION "36.1" CACHE STRING "")
 #
 # Declare project name and version
-gaudi_project(Online    v7r12
-              USE LHCb  v53r4
+gaudi_project(Online    v7r11
+              USE LHCb  v53r2
 )
diff --git a/Online/GauchoBase/CMakeLists.txt b/Online/GauchoBase/CMakeLists.txt
index cdaee36e5..e409ddab4 100755
--- a/Online/GauchoBase/CMakeLists.txt
+++ b/Online/GauchoBase/CMakeLists.txt
@@ -9,9 +9,8 @@
 # Author     : B.Jost
 #
 #===============================================================================
-################################################################################
 # Package: GauchoBase
-################################################################################
+#===============================================================================
 gaudi_subdir(GauchoBase v7r48)
 
 gaudi_depends_on_subdirs(Online/OnlineBase
diff --git a/Online/GauchoBase/Gaucho/MonHist.h b/Online/GauchoBase/Gaucho/MonHist.h
index 27d8d2c14..4658fad6e 100644
--- a/Online/GauchoBase/Gaucho/MonHist.h
+++ b/Online/GauchoBase/Gaucho/MonHist.h
@@ -9,8 +9,8 @@
 // Author     : B.Jost
 //
 //==========================================================================
-#ifndef MONHIST_H_
-#define MONHIST_H_
+#ifndef ONLINE_GAUCHO_MONHIST_H_
+#define ONLINE_GAUCHO_MONHIST_H_
 
 #include <Gaucho/MonBase.h>
 #include <TProfile.h>
@@ -199,4 +199,4 @@ namespace Online  {
     hist.cpytitle(add_ptr<char>(this, this->titoff));
   }
 }
-#endif /* MONHIST_H_ */
+#endif //  ONLINE_GAUCHO_MONHIST_H_
diff --git a/Online/OnlineAlign/CMakeLists.txt b/Online/OnlineAlign/CMakeLists.txt
index 96bb5ed06..aeaa7bba5 100644
--- a/Online/OnlineAlign/CMakeLists.txt
+++ b/Online/OnlineAlign/CMakeLists.txt
@@ -1,6 +1,19 @@
+#===============================================================================
+#  LHCb Online software suite
+#-------------------------------------------------------------------------------
+# Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+# All rights reserved.
+#
+# For the licensing terms see OnlineSys/LICENSE.
+#
+# Author     : B.Jost
+#
+#===============================================================================
+# Package OnlineAlign
+#===============================================================================
 gaudi_subdir(OnlineAlign v0r10)
 
-gaudi_depends_on_subdirs(Online/GaudiOnline
+gaudi_depends_on_subdirs(Online/GaudiKernel
                          Online/Gaucho)
 
 find_package(ROOT COMPONENTS Minuit)
@@ -8,8 +21,8 @@ find_package(ROOT COMPONENTS Minuit)
 find_package(Boost)
 include_directories(SYSTEM ${Boost_INCLUDE_DIRS} ${ROOT_INCLUDE_DIRS})
 
-gaudi_add_module(OnlineAlign src/*.cpp
-                 INCLUDE_DIRS ROOT
-                 LINK_LIBRARIES GaudiOnline GauchoLib ROOT)
+gaudi_add_module(OnlineAlign    src/*.cpp
+                 INCLUDE_DIRS   ROOT
+                 LINK_LIBRARIES GaudiKernel GauchoLib ROOT)
 
 gaudi_install_headers(OnlineAlign)
diff --git a/Online/OnlineAlign/OnlineAlign/IAlignSys.h b/Online/OnlineAlign/OnlineAlign/IAlignSys.h
index 3143c60b7..c312cf5e0 100644
--- a/Online/OnlineAlign/OnlineAlign/IAlignSys.h
+++ b/Online/OnlineAlign/OnlineAlign/IAlignSys.h
@@ -1,29 +1,38 @@
-#ifndef ALIGNSYS_H
-#define ALIGNSYS_H
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+#ifndef ONLINE_ONLINEALIGN_ALIGNSYS_H
+#define ONLINE_ONLINEALIGN_ALIGNSYS_H
 
-//#include "Gaucho/IGauchoMonitorSvc.h"
-#include "GaudiKernel/IRunable.h"
-#include "GaudiKernel/IInterface.h"
+#include <GaudiKernel/IInterface.h>
 
 class IGauchoMonitorSvc;
-namespace LHCb
-{
-  class IAlignWork: virtual public IInterface
-  {
-    public:
-      DeclareInterfaceID(IAlignWork,1,0);
-      virtual IGauchoMonitorSvc *getMonSvc()=0;
-      virtual void readReference()=0;
+
+namespace Online   {
+
+  class IAlignWork: virtual public IInterface  {
+  public:
+    DeclareInterfaceID(IAlignWork,1,0);
+    virtual IGauchoMonitorSvc *getMonSvc() = 0;
+    virtual void readReference() = 0;
   };
-  class GAUDI_API IAlignDrv: virtual public IInterface
-  {
-    public:
-      DeclareInterfaceID(IAlignDrv,1,0);
-      virtual void setReferenceBase(long)=0;
-      virtual void writeReference()=0;
-      virtual void waitRunOnce()=0;
-      virtual void doContinue()=0;
-      virtual void doStop()=0;
+
+  class GAUDI_API IAlignDrv: virtual public IInterface  {
+  public:
+    DeclareInterfaceID(IAlignDrv,1,0);
+    virtual void setReferenceBase(long) = 0;
+    virtual void writeReference() = 0;
+    virtual void waitRunOnce() = 0;
+    virtual void doContinue() = 0;
+    virtual void doStop() = 0;
   };
 }
-#endif // ALIGNSYS_H
+#endif // ONLINE_ONLINEALIGN_ALIGNSYS_H
diff --git a/Online/OnlineAlign/OnlineAlign/IAlignUser.h b/Online/OnlineAlign/OnlineAlign/IAlignUser.h
index d4cb63214..67427be34 100644
--- a/Online/OnlineAlign/OnlineAlign/IAlignUser.h
+++ b/Online/OnlineAlign/OnlineAlign/IAlignUser.h
@@ -1,26 +1,32 @@
-/*
- * IAlignUser.h
- *
- *  Created on: Aug 12, 2014
- *      Author: beat
- */
-#ifndef IALIGNUSER_H_
-#define IALIGNUSER_H_
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+#ifndef ONLINE_ONLIGNALIGN_IALIGNUSER_H_
+#define ONLINE_ONLIGNALIGN_IALIGNUSER_H_
+
 #include "GaudiKernel/IAlgTool.h"
-namespace LHCb
-{
-  class IAlignFcn : virtual public IAlgTool
-  {
+
+namespace Online  {
+
+  class IAlignFcn : virtual public IAlgTool   {
     public:
-      DeclareInterfaceID(LHCb::IAlignFcn,1,0);
+      DeclareInterfaceID(IAlignFcn,1,0);
       virtual void PubResult(long reference)=0;
       virtual void i_run()=0;
   };
-  class IAlignIterator : virtual public IAlgTool
-  {
+
+  class IAlignIterator : virtual public IAlgTool  {
     public:
-      DeclareInterfaceID(LHCb::IAlignIterator,1,0);
+      DeclareInterfaceID(IAlignIterator,1,0);
       virtual StatusCode i_start()=0;
   };
 }
-#endif /* IALIGNUSER_H_ */
+#endif /* ONLINE_ONLIGNALIGN_IALIGNUSER_H_ */
diff --git a/Online/OnlineAlign/src/AlignDrv.cpp b/Online/OnlineAlign/src/AlignDrv.cpp
index c6e0cdf84..9b9342c3f 100644
--- a/Online/OnlineAlign/src/AlignDrv.cpp
+++ b/Online/OnlineAlign/src/AlignDrv.cpp
@@ -1,67 +1,105 @@
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+
 #include "AlignDrv.h"
 
-#include <cstdio>
-#include <cstdlib>
-#include "TMinuit.h"
+namespace LHCb   {
+  class AlignDrv : public Online::AlignDrv  {
+  public:
+    using Online::AlignDrv::AlignDrv;
+  };
+}
 DECLARE_COMPONENT( LHCb::AlignDrv )
+DECLARE_COMPONENT( Online::AlignDrv )
+
+using namespace Online;
+
+namespace   {
+  long rdRef(FILE *f)   {
+    long ref = 0;
+    int stat = ::fscanf(f, "%ld", &ref);
+    if (stat == 0)  {
+      ref = 0;
+    }
+    return ref;
+  }
+}
 
-using namespace LHCb;
-static AlignDrv *DrvInstance;
+AlignDrv::AlignDrv(const std::string& name, ISvcLocator* sl)
+  : base_class(name,sl)
+{
+  declareProperty("PartitionName", m_PartitionName = "LHCbA");
+  declareProperty("FitterClass",   m_FitterClass);
+  declareProperty("FitterName",    m_FitterName);
+  declareProperty("RefFileName",   m_RefFileName = "");
+  m_runonce = false;
+  m_firstRef = true;
+  m_RefBase = 0;
+}
 
-void AlignDrv::waitRunOnce()
+AlignDrv::~AlignDrv()
 {
-  while (1)
-  {
-    if (m_runonce)
-    {
+}
+
+void AlignDrv::waitRunOnce()  {
+  while (1)  {
+    if (m_runonce)    {
       m_runonce= false;
       break;
     }
     usleep(500000);
   }
 }
-void AlignDrv::setRunOnce()
-{
+
+void AlignDrv::setRunOnce()   {
   m_runonce=true;
 }
 
-
-StatusCode AlignDrv::pause()
-{
+StatusCode AlignDrv::pause()  {
   setRunOnce();
   return StatusCode::SUCCESS;
 }
-StatusCode AlignDrv::start()
-{
-  Service::start().ignore();
+
+StatusCode AlignDrv::start()   {
+  StatusCode sc = Service::start();
   m_fitter->i_start().ignore();
-  return StatusCode::SUCCESS;
+  return sc;
 }
-StatusCode AlignDrv::stop()
-{
+
+StatusCode AlignDrv::stop()   {
   return StatusCode::SUCCESS;
 }
 
-StatusCode AlignDrv::initialize()
-{
-  StatusCode sc;
-  sc = service(m_monitorSvcType,m_MonSvc,true);
-  if ( !sc.isSuccess() )  {
-    return error("Cannot access monitoring service of type "+m_monitorSvcType+".");
+StatusCode AlignDrv::initialize()  {
+  StatusCode sc = Service::initialize().ignore();
+  if ( !sc.isSuccess() )   {
+    return sc;
+  }
+  m_monitoringSvc = service("MonitorSvc", true);
+  if ( m_monitoringSvc.get() )  {
+    error() << "Cannot access monitoring service of type MonitorSvc." << endmsg;
+    return StatusCode::FAILURE;
   }
-  OnlineService::initialize().ignore();
-//  printf("Counter DNS: %s, Counter Task: %s\n Counter Names:\n",m_CntDNS.c_str(),m_CntTask.c_str());
-//  for (int i=0;i<this->m_CounterNames.size();i++)
-//  {
-//    printf("%s\n",m_CounterNames[i].c_str());
-//  }
-  service("ToolSvc",m_ToolSvc,true).ignore();
-  sc = m_ToolSvc->retrieveTool(m_FitterClass,m_FitterName,m_fitter,this,true);
+  m_toolSvc = service("ToolSvc", true);
+  sc = m_toolSvc->retrieveTool(m_FitterClass,m_FitterName,m_fitter.pRef(),this,true);
   if ( !sc.isSuccess() )  {
-    return error("Failed to retriev Fitter Tool: "+m_FitterName+" of Class "+m_FitterClass);
+    error() << "Failed to retriev Fitter Tool: " << m_FitterName << " of Class " << m_FitterClass << endmsg;
+    return StatusCode::FAILURE;
+  }
+  m_incidentSvc = service("IncidentSvc", true);
+  if ( m_incidentSvc.get() )  {
+    error() << "Cannot access incident service of type IncidentSvc." << endmsg;
+    return StatusCode::FAILURE;
   }
-
-//  m_fitter = new Fitter(this);
   m_incidentSvc->addListener(this,"DAQ_PAUSE");
   if (m_RefFileName.empty()) {
      m_RefFileName = "/group/online/dataflow/options/"+m_PartitionName+"/Alignment_Reference_File.txt";
@@ -69,86 +107,57 @@ StatusCode AlignDrv::initialize()
   return StatusCode::SUCCESS;
 }
 
-void AlignDrv::handle(const Incident& inc)
-{
-  if (inc.type() == "DAQ_PAUSE")
-  {
+StatusCode AlignDrv::finalize()  {
+  m_monitoringSvc.reset();
+  m_fitter.reset();
+  m_toolSvc.reset();
+  m_incidentSvc.reset();
+  return Service::finalize();
+}
+
+void AlignDrv::handle(const Incident& inc)   {
+  if (inc.type() == "DAQ_PAUSE")  {
     pause().ignore();
   }
-
 }
 
-StatusCode AlignDrv::finalize()
-{
-  StatusCode sc;
-  OnlineService::finalize().ignore();
+StatusCode AlignDrv::run()   {
   return StatusCode::SUCCESS;
 }
 
-StatusCode AlignDrv::run()
-{
-  return StatusCode::SUCCESS;
+IGauchoMonitorSvc *AlignDrv::getMonSvc()   {
+  return this->m_monitoringSvc.get();
 }
 
-IGauchoMonitorSvc *AlignDrv::getMonSvc()
-{
-  return this->m_MonSvc;
-}
-void AlignDrv::setReferenceBase(long ref)
-{
-  if (m_RefBase == 0)
-  {
+void AlignDrv::setReferenceBase(long ref)   {
+  if (m_RefBase == 0)  {
     m_RefBase = new long;
   }
   *m_RefBase = ref;
 }
-long AlignDrv::rdRef(FILE *f)
-{
-  long ref;
-  int stat = fscanf(f, "%ld", &ref);
-  if (stat == 0)
-  {
-    ref = 0;
-  }
-  return ref;
-}
-void AlignDrv::writeReference()
-{
+
+void AlignDrv::writeReference()  {
   long ref=0;
   FILE *f = fopen(m_RefFileName.c_str(), "r+");
-  if (f == 0)
-  {
-    if (m_firstRef)
-    {
-      if (m_RefBase != 0)
-      {
+  if (f == 0)  {
+    if (m_firstRef)   {
+      if (m_RefBase != 0)   {
         ref = *m_RefBase;
       }
-      else
-      {
+      else    {
         ref = 0;
       }
       m_firstRef = false;
     }
     f = fopen(m_RefFileName.c_str(), "w");
   }
-  else
-  {
-    if (m_firstRef)
-    {
-      if (m_RefBase != 0)
-      {
-        ref = *m_RefBase;
-      }
-      else
-      {
-        ref = rdRef(f);
-      }
-      if (ref > 50) ref =0;
+  else    {
+    if ( m_firstRef )    {
+      ref = (m_RefBase != 0) ? *m_RefBase : rdRef(f);
+      if ( ref > 50 ) ref = 0;
       m_firstRef = false;
     }
-    else
-    {
+    else    {
       ref = rdRef(f);
     }
     rewind(f);
@@ -161,28 +170,10 @@ void AlignDrv::writeReference()
   fflush(::stdout);
 }
 
-
-AlignDrv::AlignDrv(const std::string& name, ISvcLocator* sl) : base_class(name,sl),m_MonSvc(0),m_fitter(0),m_ToolSvc(0)
-{
-  declareProperty("PartitionName", m_PartitionName = "LHCbA");
-  declareProperty("FitterClass", m_FitterClass);
-  declareProperty("FitterName", m_FitterName);
-  declareProperty("RefFileName", m_RefFileName = "");
-  m_runonce = false;
-  DrvInstance = this;
-  m_RefBase = 0;
-  m_firstRef = true;
+void AlignDrv::doContinue()   {
+  m_incidentSvc->fireIncident(Incident(name(),"DAQ_CONTINUE"));
 }
 
-AlignDrv::~AlignDrv()
-{
-}
-
-void AlignDrv::doContinue()
-{
-  incidentSvc()->fireIncident(Incident(name(),"DAQ_CONTINUE"));
-}
-void AlignDrv::doStop()
-{
-  incidentSvc()->fireIncident(Incident(name(),"DAQ_STOP"));
+void AlignDrv::doStop()   {
+  m_incidentSvc->fireIncident(Incident(name(),"DAQ_STOP"));
 }
diff --git a/Online/OnlineAlign/src/AlignDrv.h b/Online/OnlineAlign/src/AlignDrv.h
index d0d8b0d1d..fbd5d3835 100644
--- a/Online/OnlineAlign/src/AlignDrv.h
+++ b/Online/OnlineAlign/src/AlignDrv.h
@@ -1,27 +1,33 @@
-#ifndef ONLINE_GAUCHO_DUMAlignDrv_H
-#define ONLINE_GAUCHO_DUMAlignDrv_H
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+#ifndef ONLINE_GAUCHO_DUMALIGNDRV_H
+#define ONLINE_GAUCHO_DUMALIGNDRV_H
 
 #include "GaudiKernel/Service.h"
 #include "GaudiKernel/IToolSvc.h"
 #include "Gaucho/IGauchoMonitorSvc.h"
 #include "GaudiKernel/IRunable.h"
-#include "GaudiOnline/OnlineService.h"
+#include "GaudiKernel/Service.h"
 #include "GaudiKernel/IIncidentSvc.h"
-#include "RTL/rtl.h"
-#include "Gaucho/CounterTask.h"
-#include "Gaucho/BRTL_Lock.h"
+#include "GaudiKernel/IIncidentListener.h"
 #include "OnlineAlign/IAlignUser.h"
 #include "OnlineAlign/IAlignSys.h"
+
+#include "RTL/rtl.h"
 #include <cstdio>
-// Forward declarations
-//class DimService;
-//class ISimpleTrendWriter;
-namespace LHCb
-{
-  class AlignDrv: public extends2<Online::OnlineService, IRunable, IAlignDrv>
-  {
-    private:
-      long rdRef(FILE *);
+
+namespace Online  {
+
+  class AlignDrv: public extends3<Service, IRunable, IIncidentListener, IAlignDrv>  {
     public:
       AlignDrv(const std::string& name, ISvcLocator* sl);
       virtual ~AlignDrv();
@@ -30,11 +36,19 @@ namespace LHCb
       StatusCode finalize() override;
       StatusCode run() override;
       StatusCode stop() override;
+
+      void handle(const Incident& inc) override;
+
       virtual StatusCode pause();
       virtual void setRunOnce();
       void waitRunOnce() override;
-      void handle(const Incident& inc) override;
+      void writeReference() override;
+      void setReferenceBase(long) override;
+      void doContinue() override;
+      void doStop() override;
+      IGauchoMonitorSvc *getMonSvc();
 
+  private:
       std::string m_PartitionName;
       std::string m_RefFileName;
       std::string m_FitterClass;
@@ -42,15 +56,10 @@ namespace LHCb
       long *m_RefBase;
       bool m_firstRef;
       bool m_runonce;
-      IGauchoMonitorSvc *m_MonSvc;
-      IAlignIterator *m_fitter;
-      void writeReference() override;
-      void setReferenceBase(long) override;
-      void doContinue() override;
-      void doStop() override;
-      IIncidentSvc *incidentSvc () {return OnlineService::incidentSvc();};
-      IToolSvc *m_ToolSvc;
-      IGauchoMonitorSvc *getMonSvc();
+      SmartIF<IGauchoMonitorSvc> m_monitoringSvc;
+      SmartIF<IIncidentSvc>      m_incidentSvc;
+      SmartIF<IToolSvc>          m_toolSvc;
+      SmartIF<IAlignIterator>    m_fitter;
   };
 }
-#endif // ONLINE_GAUCHO_DUMAlignDrv_H
+#endif // ONLINE_GAUCHO_DUMALIGNDRV_H
diff --git a/Online/OnlineAlign/src/AlignWork.cpp b/Online/OnlineAlign/src/AlignWork.cpp
index 2f19c98eb..5428b50e6 100644
--- a/Online/OnlineAlign/src/AlignWork.cpp
+++ b/Online/OnlineAlign/src/AlignWork.cpp
@@ -1,21 +1,36 @@
-//#include "GaudiKernel/SmartIF.h"
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+
 #include "AlignWork.h"
-#include "Gaucho/Utilities.h"
-#include "Gaucho/IGauchoMonitorSvc.h"
-#include "GaudiKernel/IIncidentSvc.h"
+#include <Gaucho/Utilities.h>
+#include <Gaucho/IGauchoMonitorSvc.h>
+#include <GaudiKernel/IIncidentSvc.h>
 
+namespace LHCb   {
+  class AlignWork : public Online::AlignWork  {
+  public:
+    using Online::AlignWork::AlignWork;
+  };
+}
 DECLARE_COMPONENT( LHCb::AlignWork )
-using namespace LHCb;
+DECLARE_COMPONENT( Online::AlignWork )
 
-//static DumAligWork *WorkInstance;
-extern "C"
-{
-  int WorkerThreadFunction(void *t)
-  {
-    LHCb::AlignWork* WorkInstance = (LHCb::AlignWork*)t;
+using namespace Online;
+
+extern "C"   {
+  int WorkerThreadFunction(void *t)  {
+    Online::AlignWork* WorkInstance = (Online::AlignWork*)t;
     int run_count = 0;
-    while (1)
-    {
+    while (1)    {
       WorkInstance->waitRunOnce();
       run_count++;
       if (run_count == 1) sleep(10);
@@ -26,114 +41,109 @@ extern "C"
   }
 }
 
-StatusCode LHCb::AlignWork::stop()
+Online::AlignWork::AlignWork(const std::string& name, ISvcLocator* sl)
+  : base_class(name,sl)
 {
+  declareProperty("PartitionName",        m_partitionName= "LHCb");
+  declareProperty("ReferenceFileName",    m_refFileName);
+  declareProperty("FitterFunktionClass",  m_fitFcnClass);
+  declareProperty("FitterFunktionName",   m_fitFcnName);
+}
+
+Online::AlignWork::~AlignWork()   {
+}
+
+StatusCode Online::AlignWork::stop()   {
   return StatusCode::SUCCESS;
 }
-void LHCb::AlignWork::waitRunOnce()
-{
-  while (1)
-  {
-    if (m_runonce)
-    {
-      m_runonce= false;
+
+void Online::AlignWork::waitRunOnce()   {
+  while (1)  {
+    if ( m_runonce )    {
+      m_runonce = false;
       break;
     }
     usleep(500000);
   }
 }
-void LHCb::AlignWork::setRunOnce()
-{
-  m_runonce=true;
+void Online::AlignWork::setRunOnce()   {
+  m_runonce = true;
 }
-StatusCode LHCb::AlignWork::i_continue()
-{
+
+StatusCode Online::AlignWork::i_continue()   {
   setRunOnce();
   return StatusCode::SUCCESS;
 }
-StatusCode LHCb::AlignWork::start()
-{
-  OnlineService::start().ignore();
-  return StatusCode::SUCCESS;
+
+StatusCode Online::AlignWork::start()   {
+  return Service::start();
 }
 
-StatusCode LHCb::AlignWork::initialize()
-{
-  OnlineService::initialize().ignore();
-  service("ToolSvc",m_ToolSvc,true).ignore();
-  service("MonitorSvc",m_MonSvc,true).ignore();
-  m_incidentSvc = incidentSvc();
+StatusCode Online::AlignWork::initialize()   {
+  StatusCode sc = Service::initialize();
+  if ( !sc.isSuccess() )   {
+    return sc;
+  }
+  m_toolSvc = service("ToolSvc",true);
+
+  m_monitoringSvc = service("MonitorSvc",true);
+  if ( m_monitoringSvc.get() )  {
+    error() << "Cannot access monitoring service of type MonitorSvc." << endmsg;
+    return StatusCode::FAILURE;
+  }
+  m_incidentSvc = service("IncidentSvc", true);
+  if ( m_incidentSvc.get() )  {
+    error() << "Cannot access incident service of type IncidentSvc." << endmsg;
+    return StatusCode::FAILURE;
+  }
   m_incidentSvc->addListener(this,"DAQ_CONTINUE");
   m_incidentSvc->addListener(this,"APP_RUNNING");
   ::lib_rtl_start_thread(WorkerThreadFunction,this,&m_thread);
-  if (m_RefFileName.empty())
-  {
-    m_RefFileName = "/group/online/dataflow/options/"+m_PartitionName+"/Alignment_Reference_File.txt";
+  if ( m_refFileName.empty() )  {
+    m_refFileName = "/group/online/dataflow/options/"+m_partitionName+"/Alignment_Reference_File.txt";
+  }
+
+  sc = m_toolSvc->retrieveTool(m_fitFcnClass, m_fitFcnName, m_fitterFcn.pRef(), this, true);
+  if ( sc.isSuccess() )  {
+    error() << "Cannot access fitter tool " << m_fitFcnName << " of type " << m_fitFcnClass << "." << endmsg;
+    return sc;
   }
-  StatusCode sc=m_ToolSvc->retrieveTool(m_FitFcnClass,m_FitFcnName,m_fitterFcn,this,true);
-//  m_fitterFcn->setParent((void*)((IDumAligWork*)this));
   return sc;
 }
 
-void LHCb::AlignWork::handle(const Incident& inc)
-{
-  if (inc.type() == "APP_RUNNING")
-  {
+void Online::AlignWork::handle(const Incident& inc)    {
+  if (inc.type() == "APP_RUNNING")  {
   }
-  if (inc.type() == "DAQ_CONTINUE")
-  {
+  if (inc.type() == "DAQ_CONTINUE")  {
     setRunOnce();
   }
-
 }
 
-StatusCode LHCb::AlignWork::finalize()
-{
-  StatusCode sc;
-  if (m_thread != 0)
-  {
+StatusCode Online::AlignWork::finalize()   {
+  if ( m_thread )  {
     ::lib_rtl_cancel_thread(m_thread);
+    m_thread = nullptr;
   }
-  m_ToolSvc->release();
-  m_MonSvc->release();
-  OnlineService::finalize().ignore();
-  return StatusCode::SUCCESS;
+  m_toolSvc.reset();
+  m_monitoringSvc.reset();
+  return Service::finalize();
 }
 
-StatusCode LHCb::AlignWork::i_run()
-{
+StatusCode Online::AlignWork::i_run()   {
   readReference();
   m_fitterFcn->i_run();
-  m_fitterFcn->PubResult(m_Reference);
-//  printf("Function Result: %15g\n",m_result);
-//  fflush (stdout);
-//  m_MonSvc->updatePerSvc(m_Reference);
-  m_incidentSvc->fireIncident(Incident(name(),"DAQ_PAUSE"));
+  m_fitterFcn->PubResult(m_reference);
+  m_incidentSvc->fireIncident(Incident(name(), "DAQ_PAUSE"));
   return StatusCode::SUCCESS;
 }
-StatusCode LHCb::AlignWork::run()
-{
+
+StatusCode Online::AlignWork::run()   {
   setRunOnce();
   return StatusCode::SUCCESS;
 }
 
-
-LHCb::AlignWork::AlignWork(const std::string& name, ISvcLocator* sl) :
-        base_class(name,sl),m_MonSvc(0),m_fitterFcn(0),m_Reference(0),m_thread(0),m_runonce(false)
-{
-  declareProperty("PartitionName",   m_PartitionName= "LHCb");
-  declareProperty("ReferenceFileName",  m_RefFileName);
-  declareProperty("FitterFunktionClass",  m_FitFcnClass);
-  declareProperty("FitterFunktionName",  m_FitFcnName);
-}
-
-LHCb::AlignWork::~AlignWork()
-{
-}
-
-void LHCb::AlignWork::readReference()
-{
-  FILE *f = fopen(m_RefFileName.c_str(),"r");
-  fscanf(f,"%lu",&m_Reference);
+void Online::AlignWork::readReference()   {
+  FILE *f = fopen(m_refFileName.c_str(),"r");
+  fscanf(f,"%lu",&m_reference);
   fclose(f);
 }
diff --git a/Online/OnlineAlign/src/AlignWork.h b/Online/OnlineAlign/src/AlignWork.h
index 068cd7735..a24a35f6f 100644
--- a/Online/OnlineAlign/src/AlignWork.h
+++ b/Online/OnlineAlign/src/AlignWork.h
@@ -1,49 +1,66 @@
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
 #ifndef ONLINE_GAUCHO_ALIGNWORK_H
 #define ONLINE_GAUCHO_ALIGNWORK_H
 
-#include "GaudiKernel/Service.h"
-#include "GaudiKernel/IToolSvc.h"
-#include "Gaucho/IGauchoMonitorSvc.h"
-#include "GaudiOnline/OnlineService.h"
-#include "GaudiKernel/IIncidentSvc.h"
-#include "RTL/rtl.h"
-#include "OnlineAlign/IAlignUser.h"
-#include "OnlineAlign/IAlignSys.h"
-
-namespace LHCb
-{
-  class AlignWork: public extends2<Online::OnlineService, IAlignWork,IRunable>
-  {
-    public:
-      AlignWork(const std::string& name, ISvcLocator* sl);
-      virtual ~AlignWork();
-      StatusCode start() override;
-      StatusCode initialize() override;
-      StatusCode finalize() override;
-      StatusCode run() override;
-      virtual StatusCode i_run();
-      StatusCode stop() override;
-      virtual StatusCode i_continue();
-      IGauchoMonitorSvc *m_MonSvc;
-      IGauchoMonitorSvc *getMonSvc() override {return m_MonSvc;};
-      std::string getPartitionName(){return m_PartitionName;};
-      IAlignFcn * m_fitterFcn;
-      void readReference() override;
-      unsigned long getReference(){return m_Reference;}
-      void handle(const Incident& inc) override;
-//  unsigned long long m_prevupdate;
-
-      std::string m_PartitionName;
-      std::string m_RefFileName;
-      std::string m_FitFcnName;
-      std::string m_FitFcnClass;
-      unsigned long m_Reference;
-      lib_rtl_thread_t m_thread;
-      bool m_runonce;
-      void waitRunOnce();
-      void setRunOnce();
-      void *getThis(){return this;};
-      IToolSvc *m_ToolSvc;
+#include <GaudiKernel/Service.h>
+#include <GaudiKernel/IToolSvc.h>
+#include <GaudiKernel/IRunable.h>
+#include <GaudiKernel/IIncidentSvc.h>
+#include <GaudiKernel/IIncidentListener.h>
+
+#include <Gaucho/IGauchoMonitorSvc.h>
+
+#include <OnlineAlign/IAlignUser.h>
+#include <OnlineAlign/IAlignSys.h>
+#include <RTL/rtl.h>
+
+namespace Online   {
+
+  class AlignWork: public extends3<Service, IRunable, IIncidentListener, IAlignWork>  {
+  public:
+    AlignWork(const std::string& name, ISvcLocator* sl);
+    virtual ~AlignWork();
+    StatusCode start() override;
+    StatusCode stop() override;
+    StatusCode initialize() override;
+    StatusCode finalize() override;
+    StatusCode run() override;
+
+    void handle(const Incident& inc) override;
+
+    virtual StatusCode i_run();
+    virtual StatusCode i_continue();
+    IGauchoMonitorSvc *getMonSvc() override { return m_monitoringSvc.get(); }
+    void readReference() override;
+
+    std::string getPartitionName()          { return m_partitionName;       }
+    unsigned long getReference()            { return m_reference;           }
+
+    std::string m_partitionName;
+    std::string m_refFileName;
+    std::string m_fitFcnName;
+    std::string m_fitFcnClass;
+    lib_rtl_thread_t m_thread   { nullptr };
+
+    unsigned long m_reference { 0 };
+    bool m_runonce            { false };
+    void waitRunOnce();
+    void setRunOnce();
+
+    SmartIF<IGauchoMonitorSvc> m_monitoringSvc;
+    SmartIF<IIncidentSvc>      m_incidentSvc;
+    SmartIF<IAlignFcn>         m_fitterFcn;
+    SmartIF<IToolSvc>          m_toolSvc;
   };
 }
 #endif // ONLINE_GAUCHO_ALIGNWORK_H
diff --git a/Online/OnlineAlign/src/Fitter.cpp b/Online/OnlineAlign/src/Fitter.cpp
index 3aa09ab64..c24abdb6f 100755
--- a/Online/OnlineAlign/src/Fitter.cpp
+++ b/Online/OnlineAlign/src/Fitter.cpp
@@ -1,35 +1,53 @@
-#include <stdio.h>
-#include <stdlib.h>
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+
 #include "Fitter.h"
-#include "Gaucho/CounterTask.h"
-#include "Gaucho/MonCounter.h"
-#include "TMinuit.h"
+#include <OnlineAlign/IAlignSys.h>
+#include <Gaucho/CounterTask.h>
+#include <Gaucho/MonCounter.h>
+#include <TMinuit.h>
+
+#include <stdexcept>
+
+namespace LHCb   {
+  class Fitter : public Online::Fitter  {
+  public:
+    using Online::Fitter::Fitter;
+  };
+}
 DECLARE_COMPONENT( LHCb::Fitter )
+DECLARE_COMPONENT( Online::Fitter )
 
-using namespace LHCb;
+using namespace Online;
 
-static Fitter *FitterInstance;
+namespace   {
+  static Fitter *FitterInstance;
 
-extern "C"
-{
-  int FitterThreadFunction(void *t)
-  {
+  int FitterThreadFunction(void *t)  {
     Fitter *f = (Fitter*)t;
     FitterInstance = f;
     f->i_run().ignore();
     return 1;
   }
-  void Chi2(int &npar, double */*grad*/, double &fval, double *params, int /*flag*/)
-  {
-    FitterInstance->m_parent->writeReference();
-    FitterInstance->write_params(npar,params);
+  void Chi2(int &npar, double */*grad*/, double &fval, double *params, int /*flag*/)  {
+    std::vector<double> pars(params, params+npar);
+    FitterInstance->parent->writeReference();
+    FitterInstance->write_params(pars);
     printf("Parameters:\n");
-    for (int i= 0;i<npar;i++)
-    {
-      printf("Paramter %d %15g\n",i,params[i]);
+    for (int i = 0; i < npar; i++ )    {
+      printf("Paramter %d %15g\n", i, params[i]);
     }
-    FitterInstance->m_parent->doContinue();
-    FitterInstance->m_parent->waitRunOnce();
+    FitterInstance->parent->doContinue();
+    FitterInstance->parent->waitRunOnce();
     fval = FitterInstance->getIterationResult();
     printf ("Chi2: Function value: %15g  \n",fval);
     printf("\n");
@@ -38,140 +56,95 @@ extern "C"
   }
 }
 
-Fitter::Fitter(const std::string &  type, const std::string &  name, const IInterface *  parent  ):AlgTool(type,name,parent)
+Fitter::Fitter(const std::string& typ, const std::string& nam, const IInterface* par)
+  : base_class(typ, nam, par)
 {
-  declareProperty("PartitionName",   m_PartitionName= "LHCbA");
-  declareProperty("ParamFileName",   m_ParamFileName= "/home/beat/aligparams.dat");
-  declareProperty("CounterNames",m_CounterNames={"aaa/Chi2"});
-  declareProperty("CounterDNS",m_CntDNS="mona08");
-  declareProperty("CounterTask",m_CntTask="LHCbA_AligWrk_00");
-  m_Minuit = 0;
-  IInterface *p=(IInterface*)parent;
-  StatusCode sc = p->queryInterface(IAlignDrv::interfaceID(),(void**)(&m_parent));
-  if ( !sc.isSuccess() )   {
-  }
-  m_cntTask=0;
+  declareProperty("PartitionName", m_partitionName   =   "LHCbA");
+  declareProperty("ParamFileName", m_paramFileName   =   "/home/beat/aligparams.dat");
+  declareProperty("CounterNames",  m_counterNames    =  {"aaa/Chi2"});
+  declareProperty("CounterDNS",    m_counterTaskDNS  =   "mon01");
+  declareProperty("CounterTask",   m_counterTaskName =   "LHCbA_AligWrk_00");
+  parent = SmartIF<IAlignDrv>((IInterface*)par);
+  m_minuit = 0;
 }
 
-StatusCode Fitter::initialize()
-{
-  int npar;
-  m_Minuit = new TMinuit(3);
-  m_Minuit->SetFCN(&Chi2);
-  read_params(npar,m_params);
-  for (size_t i=0;i<m_params.size();i++)
-  {
-    char nam[2];
-    char ii;
-    ii = char(i);
-    nam[0] = 'a'+ii;
-    nam[1] = 0;
-    m_Minuit->DefineParameter(i,nam,m_params[i],10.0,0.0,0.0);
-  };
+StatusCode Fitter::initialize()   {
+  auto params = read_params();
+  m_minuit = std::make_unique<TMinuit>(3);
+  m_minuit->SetFCN(Chi2);
+  for (size_t i=0; i<params.size(); i++ )  {
+    char nam[2] = {char('a'+i), 0};
+    m_minuit->DefineParameter(i,nam,params[i],10.0,0.0,0.0);
+  }
   return StatusCode::SUCCESS;
 }
 
-StatusCode Fitter::i_run()
-{
-  int res=m_Minuit->Migrad();
-  double par[100],dpar[100];
-  printf("MIGRAD has finished with return code: %d\nParamters:\n",res);
-  for (unsigned int i=0;i<m_params.size();i++)
-  {
-    m_Minuit->GetParameter(i,par[i],dpar[i]);
-    printf("Param #%d %15g +- %15g\n",i,par[i],dpar[i]);
+StatusCode Fitter::i_run()    {
+  double par, err;
+  int res = m_minuit->Migrad();
+  always() << "MIGRAD has finished with return code: " << res << endmsg;
+  always() << "Parameters:" << endmsg;
+  for( size_t i = 0, n = m_minuit->GetNumPars(); i<n; i++ )  {
+    char text[132];
+    m_minuit->GetParameter(i, par, err);
+    std::snprintf(text, sizeof(text), "Param #%ld %15g +- %15g\n", long(i), par, err);
+    always() << text << endmsg;
   }
-  fflush(stdout);
-  m_parent->doStop();
-//  m_parent->incidentSvc()->fireIncident(Incident(m_parent->name(),"DAQ_STOP"));
+  parent->doStop();
+//  parent->incidentSvc()->fireIncident(Incident(parent->name(),"DAQ_STOP"));
   return StatusCode::SUCCESS;
 }
-StatusCode Fitter::i_start()
-{
-  /*int status = */::lib_rtl_start_thread(FitterThreadFunction,this,&m_thread);
+
+StatusCode Fitter::i_start()   {
+  ::lib_rtl_start_thread(FitterThreadFunction,this,&m_thread);
   return StatusCode::SUCCESS;
 }
 
-StatusCode Fitter::stop()
-{
+StatusCode Fitter::stop()   {
   ::lib_rtl_delete_thread(m_thread);
+  m_thread = nullptr;
   return StatusCode::SUCCESS;
 }
 
-StatusCode Fitter::finalize()
-{
-  if (m_Minuit)
-  {
-    delete m_Minuit;
-    m_Minuit = 0;
-  }
+StatusCode Fitter::finalize()   {
+  m_minuit.reset();
   return StatusCode::SUCCESS;
 }
 
-double Fitter::getIterationResult()
-{
+double Fitter::getIterationResult()   {
+  auto task = std::make_unique<CounterTask>(m_counterTaskName, m_counterTaskDNS);
   std::vector<Online::CntrDescr*> cdesc;
-  if(m_cntTask == 0)
-  {
-    m_cntTask = new Online::CounterTask(m_CntTask,m_CntDNS);
-  }
-  m_cntTask->Counters(m_CounterNames,cdesc);
-  if (cdesc.size() > 0)
-  {
+  task->Counters(m_counterNames, cdesc);
+  if ( cdesc.size() > 0 )  {
     return cdesc[0]->d_data;
   }
-  else
-  {
+  else  {
     printf("No Counters Found...\n");
   }
   return nan("");
 }
-void Fitter::write_params(int npar, std::vector<double> &params)
-{
-  FILE *f;
-  f = fopen(m_ParamFileName.c_str(),"w");
-  for (int i=0;i<npar;i++)
-  {
-    fprintf(f,"%15lf ",params[i]);
-  }
-  fprintf(f,"\n");
-  fclose(f);
-}
 
-void Fitter::write_params(int npar, double *params)
-{
-  FILE *f;
-  f = fopen(m_ParamFileName.c_str(),"w");
-  for (int i=0;i<npar;i++)
-  {
-    fprintf(f,"%15lf ",params[i]);
+void Fitter::write_params(const std::vector<double> &params)  const {
+  FILE *f = fopen(m_paramFileName.c_str(),"w");
+  for (size_t i=0; i<params.size(); i++)  {
+    ::fprintf(f,"%15lf ",params[i]);
   }
-  fprintf(f,"\n");
-  fclose(f);
+  ::fprintf(f,"\n");
+  ::fclose(f);
 }
 
-void Fitter::read_params(int &npar, std::vector<double> &params)
-{
-  FILE *f;
-  params.clear();
-  f = fopen(m_ParamFileName.c_str(),"r");
-  while (!feof(f))
-  {
-    double p;
-    fscanf(f,"%lf",&p);
-    if (feof(f)) break;
-    params.insert(params.end(),p);
-  }
-  fclose(f);
-  npar = params.size();
-}
-StatusCode Fitter::queryInterface(const InterfaceID& riid, void** ppvIF)
-{
-  if (LHCb::IAlignIterator::interfaceID().versionMatch(riid))  {
-    *ppvIF = (IAlignIterator*) this;
-    addRef();
-    return StatusCode::SUCCESS;
+std::vector<double> Fitter::read_params() const  {
+  FILE *f = ::fopen(m_paramFileName.c_str(),"r");
+  if ( f )    {
+    std::vector<double> params;
+    while (!feof(f))  {
+      double p;
+      fscanf(f,"%lf",&p);
+      if (feof(f)) break;
+      params.insert(params.end(),p);
+    }
+    fclose(f);
+    return params;
   }
-  return AlgTool::queryInterface(riid, ppvIF);
+  throw std::runtime_error("Failed to open parameter file: "+m_paramFileName);
 }
-
diff --git a/Online/OnlineAlign/src/Fitter.h b/Online/OnlineAlign/src/Fitter.h
index f1f9c7cd3..29628dfd1 100755
--- a/Online/OnlineAlign/src/Fitter.h
+++ b/Online/OnlineAlign/src/Fitter.h
@@ -1,46 +1,51 @@
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
 #ifndef ONLINE_GAUCHO_FITTER_H
 #define ONLINE_GAUCHO_FITTER_H
 
-#include "GaudiKernel/Service.h"
-#include "GaudiKernel/IToolSvc.h"
-#include "GaudiKernel/AlgTool.h"
-#include "OnlineAlign/IAlignUser.h"
-#include "OnlineAlign/IAlignSys.h"
-
-#include "RTL/rtl.h"
+#include <GaudiKernel/AlgTool.h>
+#include <OnlineAlign/IAlignUser.h>
+#include <RTL/rtl.h>
 
 // Forward declarations
 class TMinuit;
 
-namespace Online   {
+namespace Online  {
+
+  class IAlignDrv;
   class CounterTask;
-}
 
-namespace LHCb
-{
-  class Fitter : public AlgTool, virtual public LHCb::IAlignIterator   {
-    public:
-      Fitter(const std::string &  type, const std::string &  name, const IInterface *  parent  );
-      TMinuit   *m_Minuit;
-      IAlignDrv *m_parent;
-      StatusCode i_start() override;
-      StatusCode i_run();
-      StatusCode initialize() override;
-      StatusCode finalize() override;
-      StatusCode stop() override;
-      void write_params(int,std::vector<double> &params);
-      void write_params(int npar, double *params);
-      void read_params(int&,std::vector<double> &params);
-      double getIterationResult();
-      lib_rtl_thread_t m_thread;
-      Online::CounterTask *m_cntTask;
-      std::string m_ParamFileName;
-      std::vector<std::string> m_CounterNames;
-      std::string m_CntDNS;
-      std::string m_PartitionName;
-      std::string m_CntTask;
-      std::vector<double> m_params;
-      StatusCode queryInterface(const InterfaceID& riid, void** ppvIF) override;
+  class Fitter : public extends<AlgTool, IAlignIterator>   {
+  public:
+    Fitter(const std::string& type, const std::string& name, const IInterface* parent);
+    StatusCode i_start() override;
+    StatusCode i_run();
+    StatusCode initialize() override;
+    StatusCode finalize() override;
+    StatusCode stop() override;
+
+    double     getIterationResult();
+    void       write_params(const std::vector<double> &params) const;
+    std::vector<double> read_params()  const;
+
+    SmartIF<IAlignDrv>       parent;
+  protected:
+    lib_rtl_thread_t m_thread;
+    std::unique_ptr<TMinuit> m_minuit;
+    std::string              m_paramFileName;
+    std::string              m_partitionName;
+    std::string              m_counterTaskDNS;
+    std::string              m_counterTaskName;
+    std::vector<std::string> m_counterNames;
   };
 }
 #endif // ONLINE_GAUCHO_FITTER_H
diff --git a/Online/OnlineAlign/src/FitterFcn.cpp b/Online/OnlineAlign/src/FitterFcn.cpp
index d0c69a93f..75f2ee35d 100644
--- a/Online/OnlineAlign/src/FitterFcn.cpp
+++ b/Online/OnlineAlign/src/FitterFcn.cpp
@@ -1,136 +1,113 @@
-/*
- * FitterFcn.cpp
- *
- *  Created on: Aug 12, 2014
- *      Author: beat
- */
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
 #include "FitterFcn.h"
-#include "GaudiKernel/Service.h"
-#include "GaudiKernel/StatusCode.h"
-#include "OnlineAlign/IAlignSys.h"
-using namespace LHCb;
+#include <OnlineAlign/IAlignSys.h>
+#include <Gaucho/IGauchoMonitorSvc.h>
 
+namespace LHCb   {
+  class FitterFcn : public Online::FitterFcn  {
+  public:
+    using Online::FitterFcn::FitterFcn;
+  };
+}
 DECLARE_COMPONENT( LHCb::FitterFcn )
+DECLARE_COMPONENT( Online::FitterFcn )
 
-FitterFcn::FitterFcn(const std::string &  type, const std::string &  name, const IInterface *  parent  ) :
-      AlgTool(type,name,parent),m_MonSvc(0),m_Parent(0),m_result(0.0)
-{
-  declareProperty("ParameterFileName",m_ParamFileName);
-  declareProperty("DataFileName",m_DataFileName);
-  declareProperty("PartitionName",m_PartitionName);
-  IInterface *p=(IInterface*)parent;
-  StatusCode sc = p->queryInterface(IAlignWork::interfaceID(),(void**)(&m_Parent));
-  if ( !sc.isSuccess() )    {
-  }
-}
+using namespace Online;
 
-FitterFcn::~FitterFcn()
+FitterFcn::FitterFcn(const std::string &  type, const std::string &  name, const IInterface *  parent  )
+:  base_class(type,name,parent)
 {
-
+  declareProperty("ParameterFileName", m_paramFileName);
+  declareProperty("DataFileName",      m_dataFileName);
+  declareProperty("PartitionName",     m_partitionName);
 }
-
-StatusCode FitterFcn::finalize()
-{
-  return AlgTool::finalize();
-}
-
-StatusCode FitterFcn::initialize()
-{
+  
+StatusCode FitterFcn::initialize()   {
   StatusCode sc= AlgTool::initialize();
-  init();
-  return sc;
-}
-
-void FitterFcn::init()
-{
-  printf ("Parameter File Name: %s\n",m_ParamFileName.c_str());
-  printf ("Data File Name: %s\n",m_DataFileName.c_str());
-  m_MonSvc = m_Parent->getMonSvc();
-  fflush(stdout);
-  m_SvcName = "Chi2";
-  m_MonSvc->declareInfo(m_SvcName,m_result,"Chi Square",0);
-  FILE *f;
-  size_t ndat=0;
-  f = fopen(m_DataFileName.c_str(),"r");
-  std::fscanf(f,"%d",(int*)(&ndat));
+  if ( !sc.isSuccess() )    {
+    return sc;
+  }
+  unsigned long ndat = 0;
+  always() << "Parameter File Name: " << m_paramFileName << endmsg;
+  always() << "Data File Name:      " << m_dataFileName << endmsg;
+  m_monitorSvc = service("MonitorSvc", true);
+  if ( m_monitorSvc.get() )  {
+    error() << "Cannot access monitoring service of type MonitorSvc." << endmsg;
+    return StatusCode::FAILURE;
+  }
+  m_monitorSvc->declareInfo("Chi2", m_result, "Chi Square", 0);
+  FILE *f = std::fopen(m_dataFileName.c_str(),"r");
+  std::fscanf(f, "%lu", &ndat);
   m_dat_x.reserve(ndat);
   m_dat_y.reserve(ndat);
   m_dat_dy.reserve(ndat);
-  for (size_t i=0;i<ndat;i++)
-  {
-    double x,y,dy;
-    fscanf(f,"%lf %lf %lf\n",&x,&y,&dy);
+  for (size_t i=0;i<ndat;i++)     {
+    double x, y, dy;
+    std::fscanf(f, "%lf %lf %lf\n", &x, &y, &dy);
     m_dat_x.insert(m_dat_x.end(), x);
     m_dat_y.insert(m_dat_y.end(), y);
     m_dat_dy.insert(m_dat_dy.end(), dy);
-    printf("%15f %15f %15f\n",m_dat_x[i],m_dat_y[i],m_dat_y[i]);
+    std::printf("%15f %15f %15f\n", m_dat_x[i], m_dat_y[i], m_dat_y[i]);
   }
-  fclose(f);
-  fflush(stdout);
+  std::fclose(f);
+  std::fflush(stdout);
+  return StatusCode::SUCCESS;
 }
 
-void FitterFcn::analyze()
-{
+StatusCode FitterFcn::finalize()   {
+  m_monitorSvc.reset();
+  return AlgTool::finalize();
+}
+
+void FitterFcn::analyze()    {
   double result = 0.0;
-  size_t i;
-  for (i=0;i<m_dat_x.size();i++)
-  {
-    double x=m_dat_x[i];
-    double y;
-    size_t j;
-    y = 0.0;
-    for (j=0;j<m_params.size();j++)
-    {
+  for (size_t i=0; i<m_dat_x.size(); i++)   {
+    double x = m_dat_x[i];
+    double y = 0.0;
+    for ( size_t j=0; j<m_params.size(); j++ )
       y = x*y+m_params[j];
-    }
+
     double chi = (y-m_dat_y[i])/m_dat_dy[i];
     result += chi*chi;
   }
   m_result = result;
-  return ;
 }
 
-void FitterFcn::ReadParams()
-{
-  FILE *f;
+void FitterFcn::ReadParams()   {
+  FILE *f = std::fopen(m_paramFileName.c_str(),"r");
   m_params.clear();
-  f = fopen(m_ParamFileName.c_str(),"r");
-  while (!feof(f))
-  {
+  while (!feof(f))  {
     double p;
-    fscanf(f,"%lf",&p);
+    ::fscanf(f,"%lf",&p);
     if (feof(f)) break;
     m_params.insert(m_params.end(),p);
   }
-  fclose(f);
-  printf("Number of Parameters %ld\n",m_params.size());
-  for (size_t i=0;i<m_params.size();i++)
-  {
-    printf ("Parameter %ld %lf\n",i,m_params[i]);
+  std::fclose(f);
+  std::printf("Number of Parameters %ld\n",m_params.size());
+  for (size_t i=0;i<m_params.size();i++)  {
+    std::printf ("Parameter %ld %lf\n",i,m_params[i]);
   }
-  printf("\n");
-  fflush(stdout);
+  std::printf("\n");
+  std::fflush(stdout);
 }
 
-void FitterFcn::PubResult(long reference)
-{
-  m_MonSvc->updatePerSvc(reference);
+void FitterFcn::PubResult(long reference)   {
+  SmartIF<IGauchoMonitorSvc> mon(m_monitorSvc);
+  mon->updatePerSvc(reference);
 }
 
-void FitterFcn::i_run()
-{
+void FitterFcn::i_run()   {
   ReadParams();
   analyze();
-  printf("Function Result: %lf\n",m_result);
-  fflush(stdout);
-}
-
-StatusCode FitterFcn::queryInterface(const InterfaceID& riid, void** ppvIF)
-{
-  if (LHCb::IAlignFcn::interfaceID().versionMatch(riid))  {
-    *ppvIF = (IAlignFcn*) this;
-    addRef();
-    return StatusCode::SUCCESS;
-  }
-  return AlgTool::queryInterface(riid, ppvIF);
+  always() << "Function Result: " << m_result << endmsg;
 }
diff --git a/Online/OnlineAlign/src/FitterFcn.h b/Online/OnlineAlign/src/FitterFcn.h
index 7e97b3625..801b7bba7 100644
--- a/Online/OnlineAlign/src/FitterFcn.h
+++ b/Online/OnlineAlign/src/FitterFcn.h
@@ -1,43 +1,43 @@
-/*
- * FitterFcn.h
- *
- *  Created on: Aug 12, 2014
- *      Author: beat
- */
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+#ifndef ONLINE_ONLINEALGIN_FITTERFCN_H_
+#define ONLINE_ONLINEALGIN_FITTERFCN_H_
 
-#ifndef FITTERFCN_H_
-#define FITTERFCN_H_
-#include "GaudiKernel/AlgTool.h"
-#include "OnlineAlign/IAlignUser.h"
-#include "Gaucho/IGauchoMonitorSvc.h"
-#include "OnlineAlign/IAlignSys.h"
-namespace LHCb
-{
-  class FitterFcn : public AlgTool, virtual public LHCb::IAlignFcn
-  {
-    public:
-      std::string m_DataFileName;
-      std::string m_ParamFileName;
-      std::string m_SvcName;
+#include <GaudiKernel/AlgTool.h>
+#include <OnlineAlign/IAlignUser.h>
+
+namespace Online   {
+
+  class FitterFcn : public extends<AlgTool, IAlignFcn>  {
+  protected:
+      std::string m_dataFileName;
+      std::string m_paramFileName;
+      std::string m_partitionName;
       std::vector<double> m_dat_x;
       std::vector<double> m_dat_y;
       std::vector<double> m_dat_dy;
       std::vector<double> m_params;
-      std::string m_PartitionName;
-      IGauchoMonitorSvc *m_MonSvc;
-      LHCb::IAlignWork *m_Parent;
-      double m_result;
-      void init();
-      FitterFcn(const std::string &  type, const std::string &  name, const IInterface *  parent  );
-      ~FitterFcn();
+      SmartIF<IMonitorSvc> m_monitorSvc;
+      double m_result   { 0e0 };
+
+    public:
+      FitterFcn(const std::string& type, const std::string& name, const IInterface* parent);
+      ~FitterFcn() = default;
       StatusCode initialize() override;
       StatusCode finalize() override;
-      StatusCode queryInterface(const InterfaceID& riid, void** ppvIF) override;
       void analyze();
       void ReadParams();
       void PubResult(long reference) override;
       void i_run() override;
-//      void setParent(void *p);
   };
 }
-#endif /* FITTERFCN_H_ */
+#endif /* ONLINE_ONLINEALGIN_FITTERFCN_H_ */
diff --git a/Online/OnlineAlign/src/IFitter.h b/Online/OnlineAlign/src/IFitter.h
deleted file mode 100644
index fc62dba77..000000000
--- a/Online/OnlineAlign/src/IFitter.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef ONLINE_GAUCHO_IFITTER_H
-#define ONLINE_GAUCHO_IFITTER_H
-#include "GaudiKernel/IAlgTool.h"
-
-
-namespace LHCb
-{
-  class IFitter : virtual public IAlgTool
-  {
-    public:
-      DeclareInterfaceID(LHCb::IFitter,1,0);
-      virtual StatusCode i_start()=0;
-  };
-}
-#endif // ONLINE_GAUCHO_IFITTER_H
diff --git a/Online/OnlineAlign/src/IFitterFcn.h b/Online/OnlineAlign/src/IFitterFcn.h
deleted file mode 100644
index 2d2334aa7..000000000
--- a/Online/OnlineAlign/src/IFitterFcn.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * IFitterFcn.h
- *
- *  Created on: Aug 12, 2014
- *      Author: beat
- */
-#ifndef IFITTERFCN_H_
-#define IFITTERFCN_H_
-#include "GaudiKernel/IAlgTool.h"
-namespace LHCb
-{
-  class IFitterFcn : virtual public IAlgTool
-  {
-    public:
-      DeclareInterfaceID(LHCb::IFitterFcn,1,0);
-      virtual void init()=0;
-      virtual double analyze()=0;
-      virtual void ReadParams()=0;
-      virtual void PubResult(long reference)=0;
-      virtual void i_run()=0;
-  };
-}
-#endif /* IFITTERFCN_H_ */
diff --git a/Online/RawBankSizes/src/MakePages.cpp b/Online/RawBankSizes/src/MakePages.cpp
index e5dfc8a2c..8d2d6702a 100644
--- a/Online/RawBankSizes/src/MakePages.cpp
+++ b/Online/RawBankSizes/src/MakePages.cpp
@@ -1,16 +1,21 @@
-/*
- * MakePages.cpp
- *
- *  Created on: Aug 10, 2011
- *      Author: beat
- */
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
 #include <map>
 #include <string>
 #include <vector>
 #include <set>
-#include "string.h"
-#include "OnlineHistDB/OnlineHistDB.h"
+#include <cstring>
 #include <cstdio>
+#include <OnlineHistDB/OnlineHistDB.h>
 
 typedef std::map<std::string,std::vector<OnlineHistogram*> > bankmap;
 
diff --git a/Online/RawBankSizes/src/ParseHistParams.cpp b/Online/RawBankSizes/src/ParseHistParams.cpp
index c47c44f80..291da3fb0 100644
--- a/Online/RawBankSizes/src/ParseHistParams.cpp
+++ b/Online/RawBankSizes/src/ParseHistParams.cpp
@@ -1,4 +1,15 @@
-#include "RawBankSizes/Structure.h"
+//==========================================================================
+//  LHCb Online software suite
+//--------------------------------------------------------------------------
+// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+// All rights reserved.
+//
+// For the licensing terms see OnlineSys/LICENSE.
+//
+// Author     : B.Jost
+//
+//==========================================================================
+#include <RawBankSizes/Structure.h>
 #include <iostream>
 #include <string>
 
@@ -16,6 +27,5 @@ int main(int, char**)   {
   test("(50, 0.0, 1000.0, 0, 5, \"L0\")");
   test("(50,0.0,1000.0,0,5,\"L0\")");
   test("(50,0.0,1000.0,0,5,'L0')");
-
   return 0;
 }
-- 
GitLab