From 2737f3b0e6c921f37a44dfd5940746ce42cc2c7a Mon Sep 17 00:00:00 2001
From: Paras Naik <paras.naik@cern.ch>
Date: Thu, 16 Mar 2017 23:30:37 +0100
Subject: [PATCH] =?UTF-8?q?RICH-7=20Updated=20RichMirrorAlignmentOnline=20?=
 =?UTF-8?q?from=20the=20version=20at=20the=20pit.=20=E2=80=9CTagged"=20as?=
 =?UTF-8?q?=20v2r3=20=20=20=20=20=20=20=20Now=20importing=20TGraph,=20TMul?=
 =?UTF-8?q?tiGraph,=20TLine=20from=20ROOT=20for=20plots=20=20=20=20=20=20?=
 =?UTF-8?q?=20=20Added=20"lastXMLFile=E2=80=9D=20which=20represents=20the?=
 =?UTF-8?q?=20most=20final=20iteration=20XML=20=20=20=20=20=20=20=20Added?=
 =?UTF-8?q?=20=E2=80=9CcompareXML=E2=80=9D=20which=20represents=20the=20cu?=
 =?UTF-8?q?rrent=20DB;=20changed=20functions=20accordingly=20=20=20=20=20?=
 =?UTF-8?q?=20=20=20We=20now=20make=20plots=20of=20primary=20and=20seconda?=
 =?UTF-8?q?ry=20mirror=20tilts=20w.r.t.=20compareXML=20=20=20=20=20=20=20?=
 =?UTF-8?q?=20stopTolerance=20and=20stopToleranceSec=20removed=20and=20rep?=
 =?UTF-8?q?laced=20with=20quad=20stop=20tolerances=20(see=20RichMirrAlign?=
 =?UTF-8?q?=20v19r1)=20=20=20=20=20=20=20=20Also=20added=20regularizationM?=
 =?UTF-8?q?ode,=20stopToleranceMode,=20nominalResolutionSigma,=20stopSigma?=
 =?UTF-8?q?Fraction=20(see=20RichMirrAlign=20v19r1)=20=20=20=20=20=20=20?=
 =?UTF-8?q?=20Changed=20DataType=20from=20=E2=80=982015'=20to=20=E2=80=982?=
 =?UTF-8?q?016=E2=80=99=20(PN:=20we=20really=20should=20un-hard-code=20thi?=
 =?UTF-8?q?s)=20=20=20=20=20=20=20=20Added=20brunel.Detectors=20list=20(PN?=
 =?UTF-8?q?:=20why=3F)=20=20=20=20=20=20=20=20Added=20turnOffL0Decode=20fu?=
 =?UTF-8?q?nction=20because=20(PN:=20why=3F)=20=20=20=20=20=20=20=20Added?=
 =?UTF-8?q?=20getDBXML=20function,=20which=20(PN=20believes)=20converts=20?=
 =?UTF-8?q?=E2=80=9COnline=E2=80=9D=20XML=20to=20=E2=80=9COffline=20XML?=
 =?UTF-8?q?=E2=80=9D=20by=20adding=20the=20right=20headers=20=20=20=20=20?=
 =?UTF-8?q?=20=20=20Coeffcalibtilt=20replaces/replaced=20coeffCalibTilt=20?=
 =?UTF-8?q?in=20the=20configurables,=20but=20only=20for=20RICH1=20(PN=20th?=
 =?UTF-8?q?inks=20this=20is=20strange,=20but=20currently=20leaves=20it)=20?=
 =?UTF-8?q?=20=20=20=20=20=20=20Changed=20CkResSummary=20to=20use=20RiCKRe?=
 =?UTF-8?q?sLongTightMirror=20instead=20of=20RiCKResLongTight=20(requires?=
 =?UTF-8?q?=20newest=20RichRecQC=20being=20delivered=20from=20Rec)=20=20?=
 =?UTF-8?q?=20=20=20=20=20=20Edited=20RichAlignmentHelper.py=20so=20that?=
 =?UTF-8?q?=20it=20waits=20for=20all=20the=20tilt=20processes=20to=20finis?=
 =?UTF-8?q?h,=20not=20just=20one=20at=20a=20time=20(PN=20thought=20this=20?=
 =?UTF-8?q?must=20have=20been=20a=20typo)=20=20=20=20=20=20=20=20For=20all?=
 =?UTF-8?q?=20files=20that=20are=20read=20with=20open(),=20print=20an=20ER?=
 =?UTF-8?q?ROR=20message=20if=20the=20file=20does=20not=20exist=20=20=20?=
 =?UTF-8?q?=20=20=20=20=20Print=20timestamps=20when=20each=20command=20is?=
 =?UTF-8?q?=20sent=20from=20the=20FSM=20to=20the=20Iterator=20=20=20=20=20?=
 =?UTF-8?q?=20=20=20Write=20elapsed=20time=20of=20the=20the=20mirror=20ali?=
 =?UTF-8?q?gnment=20(for=20a=20particular=20RICH)=20into=20summary.txt=20?=
 =?UTF-8?q?=20=20=20=20=20=20=20Now=20we=20copy=20the=20files=20from=20the?=
 =?UTF-8?q?=20workDir=20to=20the=20MirrorAlignments=20directory=20in=20a?=
 =?UTF-8?q?=20way=20that=20preserves=20the=20original=20timestamps=20of=20?=
 =?UTF-8?q?the=20files=20=20=20=20=20=20=20=20Added=20some=20draft=20code?=
 =?UTF-8?q?=20[commented]=20for=20when=20the=20new=20RunChangeIncident=20i?=
 =?UTF-8?q?s=20merged=20into=20the=20LHCb/master=20(PN=20is=20not=20sure?=
 =?UTF-8?q?=20if=20the=20Gaudi::Time=20required=20is=20correct=20yet)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Rich/RichMirrorAlignmentOnline/CMakeLists.txt |   2 +-
 .../cmt/requirements                          |   2 +-
 .../doc/release.notes                         |  24 +-
 .../python/PyMirrAlignOnline/CkResSummary.py  |  82 +++++--
 .../python/PyMirrAlignOnline/Iterator.py      |  30 ++-
 .../PyMirrAlignOnline/RichAlignmentHelper.py  |  66 +++---
 .../python/PyMirrAlignOnline/RichAnalyzer.py  |  13 +-
 .../python/PyMirrAlignOnline/SetupHelper.py   |  36 +--
 .../python/PyMirrAlignOnline/XMLFileHelper.py |  28 ++-
 .../Configuration.py                          | 213 ++++++++++--------
 .../src/MirrPauseHandler.cpp                  |   3 +
 11 files changed, 335 insertions(+), 164 deletions(-)

diff --git a/Rich/RichMirrorAlignmentOnline/CMakeLists.txt b/Rich/RichMirrorAlignmentOnline/CMakeLists.txt
index 1eabfb300..bf20c1d71 100644
--- a/Rich/RichMirrorAlignmentOnline/CMakeLists.txt
+++ b/Rich/RichMirrorAlignmentOnline/CMakeLists.txt
@@ -1,7 +1,7 @@
 ################################################################################
 # Package: RichMirrorAlignmentOnline
 ################################################################################
-gaudi_subdir(RichMirrorAlignmentOnline v2r2)
+gaudi_subdir(RichMirrorAlignmentOnline v2r3)
 
 gaudi_depends_on_subdirs(Online/Gaucho
                          Event/DAQEvent
diff --git a/Rich/RichMirrorAlignmentOnline/cmt/requirements b/Rich/RichMirrorAlignmentOnline/cmt/requirements
index 653827e27..e8f040388 100644
--- a/Rich/RichMirrorAlignmentOnline/cmt/requirements
+++ b/Rich/RichMirrorAlignmentOnline/cmt/requirements
@@ -3,7 +3,7 @@
 # Maintainer : Paras Naik, Claire Prouve
 #============================================================================
 package           RichMirrorAlignmentOnline
-version           v2r2
+version           v2r3
 
 #============================================================================
 # Structure, i.e. directories to process. 
diff --git a/Rich/RichMirrorAlignmentOnline/doc/release.notes b/Rich/RichMirrorAlignmentOnline/doc/release.notes
index 629a57b51..0780baddd 100644
--- a/Rich/RichMirrorAlignmentOnline/doc/release.notes
+++ b/Rich/RichMirrorAlignmentOnline/doc/release.notes
@@ -10,8 +10,28 @@ Get into the habit of committing on every change (that compiles of course)
 
 ! Improvements since the previous "tagged" release of this code was tagged should be noted here, if any
 
-! 2016-04-12 - Paras Naik (on behalf of Claire Prouve)
- - Tagged (internally, since only Panoptes gets a tag now) as v2r2. 
+! 2017-03-16 - Paras Naik (on behalf of Claire Prouve)
+ - “Tagged" as v2r3 (internally, since only Panoptes gets a tag now).
+ - CP: Now importing TGraph, TMultiGraph, TLine from ROOT for plots
+       Added "lastXMLFile” which represents the most final iteration XML
+       Added “compareXML” which represents the current DB; changed functions accordingly 
+       We now make plots of primary and secondary mirror tilts w.r.t. compareXML
+       stopTolerance and stopToleranceSec removed and replaced with quad stop tolerances (see RichMirrAlign v19r1)
+       Also added regularizationMode, stopToleranceMode, nominalResolutionSigma, stopSigmaFraction (see RichMirrAlign v19r1)
+       Changed DataType from ‘2015' to ‘2016’ (PN: we really should un-hard-code this)
+       Added brunel.Detectors list (PN: why?)
+       Added turnOffL0Decode function because (PN: why?)
+       Added getDBXML function, which (PN believes) converts “Online” XML to “Offline XML” by adding the right headers 
+       Coeffcalibtilt replaces/replaced coeffCalibTilt in the configurables, but only for RICH1 (PN thinks this is strange, but currently leaves it)
+ - PN: Changed CkResSummary to use RiCKResLongTightMirror instead of RiCKResLongTight (requires newest RichRecQC being delivered from Rec)
+       Edited RichAlignmentHelper.py so that it waits for all the tilt processes to finish, not just one at a time (PN thought this must have been a typo)
+       For all files that are read with open(), print an ERROR message if the file does not exist
+       Print timestamps when each command is sent from the FSM to the Iterator
+       Write elapsed time of the the mirror alignment (for a particular RICH) into summary.txt
+       Now we copy the files from the workDir to the MirrorAlignments directory in a way that preserves the original timestamps of the files
+
+! 2016-06-07 - Paras Naik (on behalf of Claire Prouve)
+ - Tagged as v2r2 (internally, since only Panoptes gets a tag now). 
  - CP: added INFO, Warning, and ERROR to printouts for more clarity
  - CP: added python/PyMirrAlignOnline/CkResSummary.py which produces 
        additional, summary plots for the alignment
diff --git a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/CkResSummary.py b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/CkResSummary.py
index 4354344c6..8a018ed94 100644
--- a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/CkResSummary.py
+++ b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/CkResSummary.py
@@ -1,41 +1,54 @@
-from ROOT import TH1D, TCanvas, TFile, TF1, gStyle
+from ROOT import TH1D, TCanvas, TFile, TF1, gStyle, TGraph, TMultiGraph, TLine
 
 class CkResSummary:
-    def __init__(self, alignConf, _maxIt):
-        self.whichRich = alignConf.getProp('Rich')
-        self.workdir = alignConf.getProp('WorkDir')
+    def __init__(self, _alignConf, _maxIt, ):
+        self.alignConf = _alignConf
+        self.whichRich = self.alignConf.getProp('Rich')
+        self.workdir = self.alignConf.getProp('WorkDir')
         self.maxIt = _maxIt
-        self.prename =  "RichRecQCHistos_rich" + str(self.whichRich) + "_" + alignConf.getProp('nameStr') + "_"
+        self.lastXMLFile = self.workdir + '/Rich' + str(self.whichRich) + 'CondDBUpdate_' + self.alignConf.getProp('nameStr') + '_i' + str(_maxIt + 1) + '.xml' 
+        self.prename =  "RichRecQCHistos_rich" + str(self.whichRich) + "_" + self.alignConf.getProp('nameStr') + "_"
         if( 1 == self.whichRich ):
             self.fitMin = -0.007
             self.fitMax = 0.007
         else:
             self.fitMin = -0.0039
             self.fitMax = 0.0035
-
-
-
-    def makeSummaryPlot(self):
+        if(self.whichRich == 1):
+            self.maxpri = 3
+            self.maxsec = 15
+        if(self.whichRich == 2):
+            self.maxpri = 55
+            self.maxsec = 39
+            
+            
+    
+    def makeSummaryPlot(self, compareXML):
         import os
         gStyle.SetOptFit(1111)
         gStyle.SetOptStat(000000000)
+        
+        from tiltObj import tiltObj
+        mirrtilts = tiltObj(self.alignConf)
+        mgraphs = mirrtilts.getChange([compareXML, self.lastXMLFile])
+        
         histograms = {}
         resHisto = TH1D('hres', 'RICH' + str(self.whichRich) + ' Cherenkov angle resolution per It. ', self.maxIt + 1, -0.5, self.maxIt + 0.5)
-
+        
         saveName = self.workdir + '/Rich' + str(self.whichRich) + '_ckResSummary.pdf'
         cPlot = TCanvas('cPlot', 'ckRes')
         cPlot.SaveAs(saveName + '[')
         
         polbkg = TF1("polBKG","pol3(0)", self.fitMin, self.fitMax)
         help = None
-
-
+        
+        
         fitRes = []
         for j in range(0, self.maxIt + 1):
             filename = self.workdir + '/' + self.prename +'i' +  str(j) + '.root'
             if os.path.exists(filename) :
                 thisFile = TFile(filename, 'read')
-                hist = thisFile.Get('RICH/RiCKResLongTight/Rich' + str(self.whichRich) + 'Gas/ckResAll')
+                hist = thisFile.Get('RICH/RiCKResLongTightMirror/Rich' + str(self.whichRich) + 'Gas/ckResAll')
                 title = 'RICH ' + str(self.whichRich) + ' Cherenkov angle resolution It. ' + str(j) 
                 fitRes = self.fitCherenkovAngle(hist)
                 histograms[j] = fitRes[0]                
@@ -63,7 +76,50 @@ class CkResSummary:
         resHisto.SetYTitle('Cherenkov angle resolution / rad')
         resHisto.Draw()
         cPlot.SaveAs(saveName)
+
+        #maxPriY = TLine(0, self.alignConf.getProp('stopTolerancePriY'), self.maxpri, self.alignConf.getProp('stopTolerancePriY'))
+        #minPriY = TLine(0, -self.alignConf.getProp('stopTolerancePriY'), self.maxpri, -self.alignConf.getProp('stopTolerancePriY')) 
+        #maxPriZ = TLine(0, self.alignConf.getProp('stopTolerancePriZ'), self.maxpri, self.alignConf.getProp('stopTolerancePriZ'))                                                                                
+        #minPriZ = TLine(0, -self.alignConf.getProp('stopTolerancePriZ'), self.maxpri, -self.alignConf.getProp('stopTolerancePriZ'))    
+        #maxSecY = TLine(0, self.alignConf.getProp('stopToleranceSecY'), self.maxpri, self.alignConf.getProp('stopToleranceSecY'))  
+        #minSecY = TLine(0, -self.alignConf.getProp('stopToleranceSecY'), self.maxpri, -self.alignConf.getProp('stopToleranceSecY'))    
+        #maxSecZ = TLine(0, self.alignConf.getProp('stopToleranceSecZ'), self.maxpri, self.alignConf.getProp('stopToleranceSecZ'))                                                                                
+        #minSecZ = TLine(0, -self.alignConf.getProp('stopToleranceSecZ'), self.maxpri, -self.alignConf.getProp('stopToleranceSecY'))    
+        #maxPriY.SetLineColor(2)
+        #minPriY.SetLineColor(2)
+        #maxPriZ.SetLineColor(4)
+        #minPriZ.SetLineColor(4)
+        #maxSecY.SetLineColor(2)
+        #minSecY.SetLineColor(2)
+        #maxSecZ.SetLineColor(4)
+        #minSecZ.SetLineColor(4)
+        
+        
+        mgraphs[0].Draw('AP')
+        #maxPriY.Draw('same')
+        #minPriY.Draw('same')
+        #maxPriZ.Draw('same')
+        #minPriZ.Draw('same')
+        mgraphs[0].SetTitle("RICH" + str(self.whichRich) +" primary mirror tilts w.r.t. to current DB")
+        mgraphs[0].GetXaxis().SetTitle("number of primary mirror")
+        mgraphs[0].GetYaxis().SetTitle("change in mirror tilt / mrad")
+        cPlot.SaveAs(saveName)
+        mgraphs[1].Draw('AP')
+        #maxSecY.Draw('same')
+        #minSecY.Draw('same')
+        #maxSecZ.Draw('same')
+        #minSecZ.Draw('same')
+        mgraphs[1].SetTitle("RICH" + str(self.whichRich) +" secondary mirror tilts w.r.t. to current DB")
+        mgraphs[1].GetXaxis().SetTitle("number of secondary mirror")
+        mgraphs[1].GetYaxis().SetTitle("change in mirror tilt / mrad")
+        cPlot.SaveAs(saveName)
+        
         cPlot.SaveAs(saveName + ']')
+        
+        mirrtilts.writeChange()
+        mgraphs[0].Delete()
+        mgraphs[1].Delete()
+
 
     def fitCherenkovAngle(self, hist):
         richStr = 'Rich' + str(self.whichRich)
diff --git a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/Iterator.py b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/Iterator.py
index f391f9032..bc974af3a 100644
--- a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/Iterator.py
+++ b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/Iterator.py
@@ -20,6 +20,7 @@ from   distutils  import dir_util
 ##### Here is where the party starts!
 ########################################################################################################################     
 def run(whichRich):
+  start_time = time.time()
   ### define the communicator who will communicate with the run-control
   com = Communicator('ALIGNITER')
   state = State.NOT_READY
@@ -34,10 +35,9 @@ def run(whichRich):
   
   from RichAlignmentHelper import RichAlignmentHelper
   alignHelper = None
-
+  
   from SetupHelper import SetupHelper
   setupHelper = None
-
   ########################################################################################################################
   #####  reading in the seetings from the Configuration file
   ########################################################################################################################
@@ -60,14 +60,15 @@ def run(whichRich):
   ### before each new minor iteration the new xml file will be copied here
   currentXML = workdir + "/CondDB_Rich" + str(whichRich) + ".xml"
   combAndMirrSubsets = workdir + "Rich" + str(whichRich) + "CombAndMirrSubsets.txt"
-  
+  compareXML = workdir + '/Rich' + str(whichRich) + '_DBXMLFile.xml' # path in the workdirectory of the database-file that the final database will be 
+                                                                     #compared to 
   ########################################################################################################################
   ##### iteration counters, n_it for major iteration, m_it for minor iteration
   ########################################################################################################################
   n_it = alignConf.getProp("MajItStart")
   m_it = alignConf.getProp("MinItStart")
   print "INFO: m_it = ", m_it
-
+  
   conv = False  ### varibale indicates if alignment converged yet
   while True:
     command = com.get_command()
@@ -75,6 +76,7 @@ def run(whichRich):
     ##### this code will be excecuted when you klick configure on the run-control
     ########################################################################################################################       
     if command.startswith('configure') and state == State.NOT_READY:
+      print "CONFIGURE command received at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " UTC"
       setupHelper = SetupHelper(alignConf)
       xmlHelper = XMLFileHelper(alignConf)
       ssretr = SaveSetRetriever("LHCbA", "AligWrk_Rich" + str(whichRich))
@@ -86,12 +88,13 @@ def run(whichRich):
     ##### setting up the alignment and starting the first minor iteration (this only for the very first iteration)           
     ######################################################################################################################## 
     elif (command.startswith('start') and state == State.READY):
+      print "START command received at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " UTC"
       ### write the file for the analyzers to know which iteration they are at
       f = open(alignConf.getProp("ItNrFile"), "w")
       f.write(str(m_it))
       f.close()
       t = time()
-
+      
       ### if you want to start the alignment from scratch (e.g. at the m_it = 0) 
       if m_it == 0:
         #clean up the working directory
@@ -107,6 +110,7 @@ def run(whichRich):
         currentMirrorXMLFile = workdir + "/Rich" + str(whichRich) + "CondDBUpdate_" + thisNameStr + "_i" + str(n_it) + ".xml"
         
         xmlHelper.getStartXML(currentMirrorXMLFile)   ### get the starting-xml from the database 
+        xmlHelper.getDBXML(compareXML)
         if not magnFactorsMode == 0:
           xmlHelper.create_coeff_calibration_xml_files(currentMirrorXMLFile, n_it)    ### create the xml files for the tilted mirror configurations 
         
@@ -114,7 +118,7 @@ def run(whichRich):
       else:
         #this method will keep all files needed when starting at m_it > 0
         setupHelper.startMinIt(m_it)
-
+        
       ### now place the xml file for the current minor iteration in the place where the analyzers will pick it up
       if m_it % 9 == 0:
         pickupXML = workdir + "Rich" + str(whichRich) + "CondDBUpdate_" + thisNameStr + "_i" + str(n_it) + ".xml"
@@ -138,6 +142,7 @@ def run(whichRich):
     ##### here the code for all the following iterations
     ########################################################################################################################  
     elif (command.startswith('pause') and state == State.RUNNING):
+      print "PAUSE command received at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " UTC"
       state = State.PAUSED
       com.set_status(state)
       
@@ -169,7 +174,7 @@ def run(whichRich):
         # ## check if the alignment has converged or failed
         conv = alignHelper.hasConverged()
         print " INFO Alignment after ", n_it, " Iteration is converged is ", conv
-
+        
         if(n_it >= 9 or conv):
           if conv and n_it >= 9:
             print "INFO Alignment has reached its maximum number of iteration and has converged!"
@@ -177,10 +182,10 @@ def run(whichRich):
             print "INFO Alignment has converged after the ", n_it, "th iteration!"
           else:
             print "INFO Alignment has reached its maximum number of iterations and has not converged. :("
-          setupHelper.finalize(conv, n_it)
+          setupHelper.finalize(conv, n_it, compareXML)
           state = State.READY
-        
-
+          
+          
       # ## if the alignment has not converged and the maximal number of iterations has not been reached,
       # ## set up everything for the next major iteration
         if n_it < 9 and not conv:
@@ -209,9 +214,12 @@ def run(whichRich):
       sleep(2)
       
     elif command.startswith('stop') and state in (State.RUNNING, State.READY):
-      setupHelper.finalize(conv, n_it)
+      print "STOP command received at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " UTC"
+      elapsed_time = time.time() - start_time # Elapsed time since run(whichRich) was called
+      setupHelper.finalize(conv, n_it, compareXML, elapsed_time)
       state = State.READY
     elif command.startswith('reset'):
+      print "RESET command received at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " UTC"
       state = State.NOT_READY
       break
     else:
diff --git a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAlignmentHelper.py b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAlignmentHelper.py
index 79d09ea98..90778565c 100644
--- a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAlignmentHelper.py
+++ b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAlignmentHelper.py
@@ -9,6 +9,7 @@ from   subprocess import *
 
 class RichAlignmentHelper:
     def __init__(self, configuration, _combAndMirrSubsets):
+        # print 'miezmiezmiez'
         self.alignConf = configuration
         self.combAndMirrSubsets = _combAndMirrSubsets
         self.thisNameStr = self.alignConf.getProp('nameStr')
@@ -24,9 +25,7 @@ class RichAlignmentHelper:
         self.magnFactorsMode = self.alignConf.getProp('magnFactorsMode')
         self.solutionMethod = self.alignConf.getProp('solutionMethod')
         self.fixSinusoidShift = self.alignConf.getProp('fixSinusoidShift')
-        self.stopTolerance = self.alignConf.getProp('stopTolerance') 
-        self.stopToleranceSec = self.alignConf.getProp('stopToleranceSec') 
-
+        
     def runMirrCombinFit(self, n_it):
         ps = []
         
@@ -34,6 +33,8 @@ class RichAlignmentHelper:
             _tiltNames = [""]
         else:
             _tiltNames = self.tiltNames
+
+        tiltNamesLength = len(_tiltNames)
             
         for tiltName in _tiltNames :
             if(tiltName == ""):
@@ -82,28 +83,29 @@ class RichAlignmentHelper:
             myStdOut.close()
             myStdErr.close()
             ps.append(p2)
-            
-            PopenCount = -1;
-            while True:
-                sleep(30)
-                ps_status = [q.poll() for q in ps]
-                if all([x is not None for x in ps_status]):
-                    PopenTotal = 0
-                    for l in ps_status:
-                        if l is not None : PopenTotal += 1
-                    print "INFO %d of 9 RichMirrCombinFit processes complete" % PopenTotal
-                    print "INFO ", strftime("%Y-%m-%d %H:%M:%S", gmtime())
+        
+        # Now wait for all subprocesses to finish    
+        PopenCount = -1;
+        while True:
+            sleep(30)
+            ps_status = [q.poll() for q in ps]
+            if all([x is not None for x in ps_status]):
+                PopenTotal = 0
+                for l in ps_status:
+                    if l is not None : PopenTotal += 1
+                print "INFO %d of %d RichMirrCombinFit processes complete" % PopenTotal, tiltNamesLength
+                print "INFO ", strftime("%Y-%m-%d %H:%M:%S", gmtime())
+                print "INFO -------------------"
+                break
+            else:
+                PopenDone = 0
+                for l in ps_status:
+                    if l is not None : PopenDone += 1
+                if PopenDone > PopenCount:
+                    print "INFO %d of %d RichMirrCombinFit processes complete" % PopenDone, tiltNamesLength
+                    print "INFO", strftime("%Y-%m-%d %H:%M:%S", gmtime())
                     print "INFO -------------------"
-                    break
-                else:
-                    PopenDone = 0
-                    for l in ps_status:
-                        if l is not None : PopenDone += 1
-                    if PopenDone > PopenCount:
-                        print "INFO %d of 9 RichMirrCombinFit processes complete" % PopenDone
-                        print "INFO", strftime("%Y-%m-%d %H:%M:%S", gmtime())
-                        print "INFO -------------------"
-                        PopenCount = 0 + PopenDone
+                    PopenCount = 0 + PopenDone
 
 
     def runRichAlign(self, n_it):
@@ -126,6 +128,8 @@ class RichAlignmentHelper:
             magnificationOuputFile = self.workdir + "Rich" + str(self.whichRich) + "MirrMagnFactors_" + self.thisNameStr + connectStr + tiltName + "_i" + str(n_it) + ".txt"
             
             p = []
+            if not os.path.exists(combinResultsFile) :
+                print "ERROR no combinResultsFile."
             with open(combinResultsFile) as fitfile:
                 line = fitfile.readline()
                 while line:
@@ -181,8 +185,14 @@ class RichAlignmentHelper:
         args += 'zerothIterationXML   = ' + zeroMirrorXMLFile + '\n'
         args += 'currentIterationXML  = ' + currentMirrorXMLFile + '\n'
         args += 'nextIterationXML     = ' + nextIterationXMLFile + '\n'
-        args += 'stopTolerance        = ' + str(self.stopTolerance) + '\n'
-        args += 'stopToleranceSec     = ' + str(self.stopToleranceSec) + '\n'        
+        args += 'regularizationMode     = ' + str(self.alignConf.getProp('regularizationMode')) + '\n'
+        args += 'stopToleranceMode      = ' + str(self.alignConf.getProp('stopToleranceMode')) + '\n'
+        args += 'nominalResolutionSigma = ' + str(self.alignConf.getProp('nominalResolutionSigma')) + '\n'
+        args += 'stopSigmaFraction      = ' + str(self.alignConf.getProp('stopSigmaFraction')) + '\n'
+        args += 'stopTolerancePriY      = ' + str(self.alignConf.getProp('stopTolerancePriY')) + '\n'
+        args += 'stopTolerancePriZ      = ' + str(self.alignConf.getProp('stopTolerancePriZ')) + '\n'
+        args += 'stopToleranceSecY      = ' + str(self.alignConf.getProp('stopToleranceSecY')) + '\n'
+        args += 'stopToleranceSecZ      = ' + str(self.alignConf.getProp('stopToleranceSecZ')) + '\n'
         f.write(args)
         f.close()
         
@@ -214,6 +224,8 @@ class RichAlignmentHelper:
             fitNGEFile = combinFitDir + "Rich" + str(self.whichRich) + "MirrCombinFitNGEOut_" + self.thisNameStr + connectStr + tiltName + "_i" + str(n_it) + ".txt"
             
             os.system('cat ' + fitOutputFile + ' | grep " not_good_enough " > ' + fitNGEFile)
+            if not os.path.exists(fitNGEFile) :
+       	       	print "ERROR no fitNGEFile."
             num_lines = sum(1 for line in open(fitNGEFile))
             if (num_lines != 0):
                 print "WARNING Not enough events in all histograms for fitting. Will perform all fits anyway."
@@ -221,6 +233,8 @@ class RichAlignmentHelper:
 
 
     def hasConverged(self):
+        if not os.path.exists(self.workdir + "/Rich" + str(self.whichRich) + "_stop_or_continue.txt") :
+       	    print "ERROR no Rich_stop_or_continue_txt file."
         Rich_stop_or_continue_txt = open(self.workdir + "/Rich" + str(self.whichRich) + "_stop_or_continue.txt")
         verdict = Rich_stop_or_continue_txt.readline().strip().upper()
         Rich_stop_or_continue_txt.close()
diff --git a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAnalyzer.py b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAnalyzer.py
index 55b5a722b..e564d7f32 100644
--- a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAnalyzer.py
+++ b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/RichAnalyzer.py
@@ -101,15 +101,17 @@ def patchBrunel(true_online_version, alignConf, n):
   #brunel.Hlt1FilterCode = "HLT_PASS('Hlt1CalibTrackingKPiDetachedDecision')"
 
   from Configurables import RecMoniConf
-  brunel.RecoSequence = ['Decoding', "VELO", "TT", "IT", "OT", "TrHLT1", "Vertex", "TrHLT2", 'RICH']
+  brunel.RecoSequence = ['Decoding', "VELO", "TT", "IT", "OT", "TrHLT1","Vertex", "TrHLT2", 'RICH']
   RecMoniConf ().MoniSequence = ["RICH"]
 
+  brunel.Detectors = ['Velo', 'PuVeto', 'Rich1', 'Rich2', 'TT', 'IT', 'OT', 'Magnet', 'Tr']
+
   import ConditionsMap
   brunel.OnlineMode = True
   brunel.DDDBtag    = ConditionsMap.DDDBTag
   brunel.CondDBtag = ConditionsMap.CondDBTag
   brunel.Histograms =  "Expert"
-  brunel.DataType = "2015"
+  brunel.DataType = "2016"
   brunel.UseDBSnapshot = True
 
   brunel.OutputType='NONE'
@@ -174,6 +176,13 @@ def patchBrunel(true_online_version, alignConf, n):
   from Gaudi.Configuration import appendPostConfigAction
   appendPostConfigAction(applyConds)
 
+  def turnOffL0Decode():
+    from Configurables import GaudiSequencer
+    GaudiSequencer("DecodeTriggerSeq").Members = []
+  appendPostConfigAction(turnOffL0Decode)
+
+
+
   from Configurables import MagneticFieldSvc
   MagneticFieldSvc().UseSetCurrent = True
 
diff --git a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/SetupHelper.py b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/SetupHelper.py
index b5326cba2..31fcaec74 100644
--- a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/SetupHelper.py
+++ b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/SetupHelper.py
@@ -24,8 +24,7 @@ class SetupHelper:
         self.coeffCalibTilt = self.alignConf.getProp('coeffCalibTilt')
         self.magnifDir = self.alignConf.getProp('magnifDir') 
         self.fixSinusoidShift = self.alignConf.getProp('fixSinusoidShift') 
-        self.stopTolerance = self.alignConf.getProp('stopTolerance') 
-        self.stopToleranceSec = self.alignConf.getProp('stopToleranceSec') 
+        
 
 
     def setupMagnifFiles(self):
@@ -57,13 +56,9 @@ class SetupHelper:
             os.remove(self.workdir + "/" + rootFile)
 
 
-    def finalize(self, conv, n_it):
-        self.writeSummary(conv, n_it)
+    def finalize(self, conv, n_it, compareXML, elapsed_time):
+        self.writeSummary(conv, n_it, elapsed_time)
         
-        from CkResSummary import CkResSummary
-        ckressum = CkResSummary(self.alignConf, n_it)
-        ckressum.makeSummaryPlot()
-
         savedir = self.alignConf.getProp('SaveDir') + '/Rich' + str(self.whichRich) + '/' + strftime("%Y%m%d_%H%M%S", gmtime())
         if not os.path.exists(savedir):
             os.makedirs(savedir)
@@ -71,9 +66,15 @@ class SetupHelper:
             if os.path.isdir(self.workdir + "/" + f):
                 shutil.copytree(self.workdir + "/" + f, savedir + '/' + f)
             else:
-                shutil.copy(self.workdir + "/" + f, savedir)
-        
-    def writeSummary(self, conv, n_it):
+                shutil.copy2(self.workdir + "/" + f, savedir)
+
+        from CkResSummary import CkResSummary
+        ckressum = CkResSummary(self.alignConf, n_it)
+        ckressum.makeSummaryPlot(compareXML)
+        shutil.copy2(self.workdir + "/Rich" + str(self.whichRich) + '_ChangeWRTDB.txt', savedir)
+        shutil.copy2(self.workdir + "/Rich" + str(self.whichRich) + '_ckResSummary.pdf', savedir)
+
+    def writeSummary(self, conv, n_it, elapsed_time):
         summaryFile = self.workdir + "/summary.txt"
 
         import OnlineEnv as Online
@@ -98,8 +99,15 @@ class SetupHelper:
         args += 'coeffCalibTilt    = ' + str(self.coeffCalibTilt) + '\n'
         args += 'magnifDir         = ' + str(self.magnifDir) + '\n'
         args += 'fixSinusoidShift  = ' + str(self.fixSinusoidShift) + '\n'
-        args += 'stopTolerance     = ' + str(self.stopTolerance) + '\n'
-        args += 'stopToleranceSec  = ' + str(self.stopToleranceSec) + '\n'
-                
+        args += 'regularizationMode     = ' + str(self.alignConf.getProp('regularizationMode')) + '\n'
+        args += 'stopToleranceMode      = ' + str(self.alignConf.getProp('stopToleranceMode')) + '\n'
+        args += 'nominalResolutionSigma = ' + str(self.alignConf.getProp('nominalResolutionSigma')) + '\n'
+        args += 'stopSigmaFraction      = ' + str(self.alignConf.getProp('stopSigmaFraction')) + '\n'
+        args += 'stopTolerancePriY      = ' + str(self.alignConf.getProp('stopTolerancePriY')) + '\n'
+        args += 'stopTolerancePriZ      = ' + str(self.alignConf.getProp('stopTolerancePriZ')) + '\n'
+        args += 'stopToleranceSecY      = ' + str(self.alignConf.getProp('stopToleranceSecY')) + '\n'
+        args += 'stopToleranceSecZ      = ' + str(self.alignConf.getProp('stopToleranceSecZ')) + '\n'
+        args += '\n'
+        args += 'Elapsed time = ' + elapsed_time + ' seconds' + '\n'
         f.write(args)
         f.close()
diff --git a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/XMLFileHelper.py b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/XMLFileHelper.py
index ac923982b..2c653a427 100644
--- a/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/XMLFileHelper.py
+++ b/Rich/RichMirrorAlignmentOnline/python/PyMirrAlignOnline/XMLFileHelper.py
@@ -91,6 +91,8 @@ class XMLFileHelper:
             outfile.write('<?xml version=\'1.0\' encoding=\'iso-8859-1\'?> \n')
             outfile.write('<!DOCTYPE DDDB SYSTEM "conddb:/DTD/structure.dtd"> \n')
             outfile.write('<DDDB> \n')
+            if not os.path.exists(latest) :
+                print "ERROR no latest file."
             with open(latest) as infile:
                 for line in infile:
                     outfile.write(line)
@@ -99,13 +101,35 @@ class XMLFileHelper:
         print "INFO This files was picked up as the starting xml: ", latest
 
 
+    def getDBXML(self, compareXML):
+        workdir = self.alignConf.getProp('WorkDir')
+        whichRich = self.alignConf.getProp('Rich')
+        dbXMLFile = self.alignConf.getProp('dbXMLFile')
+        import os, re
+        
+        thisdbxml = compareXML
+        with open(thisdbxml, 'w') as outfile:
+            outfile.write('<?xml version=\'1.0\' encoding=\'iso-8859-1\'?> \n')
+            outfile.write('<!DOCTYPE DDDB SYSTEM "conddb:/DTD/structure.dtd"> \n')
+            outfile.write('<DDDB> \n')
+            if not os.path.exists(dbXMLFile) :
+                print "ERROR no dbXMLFile."
+            with open(dbXMLFile) as infile:
+                for line in infile:
+                    outfile.write(line)
+                outfile.write('</DDDB> \n')
+
+
+
     def finalXML(self, lastIt):
         workdir = self.alignConf.getProp('WorkDir')
         whichRich = self.alignConf.getProp('Rich')
         
         finalXML = workdir + "/CondDB_Update_Rich" + str(whichRich) + ".xml"
         tocopyXML = workdir + "/CondDB_Rich" + str(whichRich) + "_i" + str(lastIt) + ".xml"
-        
+
+        if not os.path.exists(tocopyXML) :
+            print "ERROR no tocopyXML file."        
         with open(tocopyXML, 'r') as infile:
             with open(finalXML, 'w') as outfile:
                 for line in infile:
@@ -116,6 +140,8 @@ class XMLFileHelper:
     def reformatXML(self, newXMLfile, workdir):
         temp = workdir + "temp.xml"
         counter = 0
+        if not os.path.exists(newXMLfile) :
+            print "ERROR no newXMLfile."        
         with open(newXMLfile, 'r') as infile:
             with open(temp, 'w') as outfile:
                 for line in infile:
diff --git a/Rich/RichMirrorAlignmentOnline/python/RichMirrorAlignmentOnline/Configuration.py b/Rich/RichMirrorAlignmentOnline/python/RichMirrorAlignmentOnline/Configuration.py
index 90f255089..6286ad8a6 100644
--- a/Rich/RichMirrorAlignmentOnline/python/RichMirrorAlignmentOnline/Configuration.py
+++ b/Rich/RichMirrorAlignmentOnline/python/RichMirrorAlignmentOnline/Configuration.py
@@ -11,7 +11,6 @@ __author__  = "Claire Prouve <Claire.Prouve@cern.ch>"
 # ("combAndMirrSubsets"  , po::value<string>(& m_combAndMirrSubsetsFile)                      , "File with chosen mirrors and combinations subsets"                                                                               )
 # ("backgroundOrder"     , po::value<int   >(& m_backgroundOrder       )->default_value( 2   ), "order for the polynomial background"                                                                                             )
 # ("plotOutputLevel"     , po::value<int   >(& m_plotOutputLevel       )->default_value( 2   ), "what plots should be saved (0) plot nothing (1) plot ...(2) only plot fits with chi2 worse than 3.0 (3) plot everything"         )
-# ("stopTolerance"       , po::value<double>(& m_stopTolerance         )->default_value( 0.1 ), "tolerance, in mrad, to stop the alignment because all mirror tilts have converged; historically 0.1"                             )
 # ("warningFactor"       , po::value<double>(& m_warningFactor         )->default_value( 20  ), "alerts the alignment shifter if any of the mirrors have shifted more than warningFactor*stopTolerance"                           )
 ### NOTE: these are not installed yet, but they exist and can be provided to RichMirrAlign:
 # ("combAndMirrSubsets"  , po::value<string>(&combAndMirrSubsetsFile  )->default_value( ""         ), "file with subsets of mirror combinations and mirrors"                                                        )
@@ -25,58 +24,72 @@ class Rich1MirrAlignOnConf(LHCbConfigurableUser):
     __used_configurables__ = [ ]
     __slots__ = {
        #### Only change these if you want to pick up from where you left off in iterations, experts only
-        "MajItStart"      : 0
-       ,"MinItStart"      : 0
+        "MajItStart"             : 0
+       ,"MinItStart"             : 0
        #### Do not change these!!! Just DONT!
-       ,"Rich"            : 1
-       ,"HistoDir"        : ""
+       ,"Rich"                   : 1
+       ,"HistoDir"               : ""
        ### Better not change these, only for very very VERY good reasons
-       ,"WorkDir"         : "/group/online/AligWork/Rich1/"
-       ,"SaveDir"         : "/group/online/AligWork/MirrorAlignments/"
-       ,"ItNrFile"        : "/group/online/dataflow/options/LHCbA/Rich1_Alignment_Reference_File.txt"
+       ,"WorkDir"                : "/group/online/AligWork/Rich1/"
+       ,"SaveDir"                : "/group/online/AligWork/MirrorAlignments/"
+       ,"ItNrFile"               : "/group/online/dataflow/options/LHCbA/Rich1_Alignment_Reference_File.txt"
        ### May be changed if you know what you are doing, explanations below
-       ,"coeffCalibTilt"  : 0.7
-       ,"phiBinFactor"    : 3
-       ,"minAverageBinPop": 6
-       ,"deltaThetaWindow": 8.0
-       ,"combinFitMethod" : 5
-       ,"magnFactorsMode" : 0
-       ,"magnifDir"       : "/group/rich/AlignmentFiles/MagnifFactors/Rich1/"
-       ,"solutionMethod"  : 0
-       ,"EvtMax"          : -1
-       ,"stopTolerance"   : 0.1
-       ,"stopToleranceSec": 0.0
-       ,"dataVariant"     : "Collision16"
-       ,"thisCase"        : "online"
-       ,"tiltNames"       : ["", "pri_negYzerZ", "pri_posYzerZ", "pri_zerYnegZ", "pri_zerYposZ", "sec_negYzerZ", "sec_posYzerZ", "sec_zerYnegZ", "sec_zerYposZ"]
-       ,"fixSinusoidShift": 1
-       ,"nameStr"         : ""
+       ,"dbXMLFile"              : "/group/online/alignment/Rich1/MirrorAlign/v2.xml"
+       ,"coeffCalibTilt"         : 0.7
+       ,"phiBinFactor"           : 3
+       ,"minAverageBinPop"       : 6
+       ,"deltaThetaWindow"       : 8.0
+       ,"combinFitMethod"        : 5
+       ,"magnFactorsMode"        : 0
+       ,"magnifDir"              : "/group/rich/AlignmentFiles/MagnifFactors/Rich1/"
+       ,"solutionMethod"         : 0
+       ,"regularizationMode"     : 1
+       ,"stopToleranceMode"      : 1
+       ,"nominalResolutionSigma" : 0
+       ,"stopSigmaFraction"      : 0
+       ,"stopTolerancePriY"      : 0.1
+       ,"stopTolerancePriZ"      : 0.1
+       ,"stopToleranceSecY"      : 0.1
+       ,"stopToleranceSecZ"      : 0.1
+       ,"EvtMax"                 : -1
+       ,"dataVariant"            : "Collision16"
+       ,"thisCase"               : "online"
+       ,"tiltNames"              : ["", "pri_negYzerZ", "pri_posYzerZ", "pri_zerYnegZ", "pri_zerYposZ", "sec_negYzerZ", "sec_posYzerZ", "sec_zerYnegZ", "sec_zerYposZ"]
+       ,"fixSinusoidShift"       : 1
+       ,"nameStr"                : ""
         }
 
     _propertyDocDct = {
-        "MajItStart"      : """ Start the alignment at this major iteration. Please make sure this is consitent with MinItStart. """
-       ,"MinItStart"      : """ Start the alignment at this minor iteration. Please make sure this is consitent with MajItStart. """
-       ,"Rich"            : """ Rich1 or Rich2. """
-       ,"HistoDir"        : """ Directory where the savesets are being written to. """
-       ,"WorkDir"         : """ Directory in which all the output will be written. """
-       ,"SaveDir"         : """ Directory in which all output files of the alignment are saved after it has finished """
-       ,"ItNrFile"        : """ File which contains only the number of the current iteration written by the iterator, to be read by the analyzers """
-       ,"coeffCalibTilt"  : """ Mirror-tilts applied for the calculation of the magnification coefficients. """
-       ,"phiBinFactor"    : """ Factor by which the number of phi-bins is reduced. The histograms should now come with 60 phi bins, factor 3 reduced this to 20 bin in the fit."""
-       ,"minAverageBinPop": """ Demanded minimal entries per bin in x-y-bin; atm 6: for 20 phi-bins, 4.8: for 25 phi-bins. """
-       ,"deltaThetaWindow": """ dTheta-range in the histograms; 4.0 for Rich1 and 3.0 for Rich2. """
-       ,"combinFitMethod" : """ Method for fitting the 2D histograms; 1: first fit the slices of dTheta with a Gaussian and then fit to phi using only the mean of the Gaussian, 3: fit a 2D function, 5: same as 3 but with unifyWidths """
-       ,"magnFactorsMode" : """ Method for calculating the magnification coefficents; 0: magnefication coeff. pre-determined, 1: individual for all mirror pairs, determined in the 0-th iteration and not updated any further, 2: individual for all mirrorpairs, determined on-the-fly on the data. """
-       ,"magnifDir"       : """ directory that contains the pre-determined magnification coeff., files in this folder have to be named Rich[1,2]_MirrMagn _[tiltName]_predefined.txt """
-       ,"solutionMethod"  : """ Solution method for calculating the mirror tilts after the individual fits; 0: Minuit, 1: Algebraic """
-       ,"EvtMax"          : """ maximal number of events processed by brunel PER NODE (hlt fram has ~1500 nodes) """
-       ,"stopTolerance"   : """ tolerance for mirrors (primary mirrors only if stopToleranceSec = 0), in mrad, to stop the alignment because all mirror tilts have converged; historically 0.1 """
-       ,"stopToleranceSec": """ if non-zero [see above], tolerance for secondary mirrors, in mrad, to stop the alignment because all mirror tilts have converged; historically 0.1 """
-       ,"dataVariant"     : """ Right now this is used only for the naming convention. """
-       ,"thisCase"        : """ Right now this is used only for the naming convention. """
-       ,"tiltNames"       : """ Combination of tilts for the calculation and usage of the magnification factors. This is needed even when using predefined magnification factors!!! """
-       ,"fixSinusoidShift": """ 1 : fix at sinusoidShift; 0 : it is not fixed """
-       ,"nameStr"         : """ Right now this is used only for the naming convention, but will be set when initiating the Configuration. """
+        "MajItStart"             : """ Start the alignment at this major iteration. Please make sure this is consitent with MinItStart. """
+       ,"MinItStart"             : """ Start the alignment at this minor iteration. Please make sure this is consitent with MajItStart. """
+       ,"Rich"                   : """ Rich1 or Rich2. """
+       ,"HistoDir"               : """ Directory where the savesets are being written to. """
+       ,"WorkDir"                : """ Directory in which all the output will be written. """
+       ,"SaveDir"                : """ Directory in which all output files of the alignment are saved after it has finished """
+       ,"ItNrFile"               : """ File which contains only the number of the current iteration written by the iterator, to be read by the analyzers """
+       ,"dbXMLFile"              : """ XML file with mirror tilts that is currently in the DB, for comparison of mirror tilts before and after alignment """
+       ,"Coeffcalibtilt"         : """ Mirror-tilts applied for the calculation of the magnification coefficients. """
+       ,"phiBinFactor"           : """ Factor by which the number of phi-bins is reduced. The histograms should now come with 60 phi bins, factor 3 reduced this to 20 bin in the fit."""
+       ,"minAverageBinPop"       : """ Demanded minimal entries per bin in x-y-bin; atm 6: for 20 phi-bins, 4.8: for 25 phi-bins. """
+       ,"deltaThetaWindow"       : """ dTheta-range in the histograms; 4.0 for Rich1 and 3.0 for Rich2. """
+       ,"combinFitMethod"        : """ Method for fitting the 2D histograms; 1: first fit the slices of dTheta with a Gaussian and then fit to phi using only the mean of the Gaussian, 3: fit a 2D function, 5: same as 3 but with unifyWidths """
+       ,"magnFactorsMode"        : """ Method for calculating the magnification coefficents; 0: magnefication coeff. pre-determined, 1: individual for all mirror pairs, determined in the 0-th iteration and not updated any further, 2: individual for all mirrorpairs, determined on-the-fly on the data. """
+       ,"magnifDir"              : """ directory that contains the pre-determined magnification coeff., files in this folder have to be named Rich[1,2]_MirrMagn _[tiltName]_predefined.txt """
+       ,"solutionMethod"         : """ Solution method for calculating the mirror tilts after the individual fits; 0: Minuit, 1: Algebraic """
+       ,"regularizationMode"     : """ regularization mode: default 0: regularization term contains average magnification coefficients as weight; !=0: all weights in the regularization term are 1."""
+       ,"stopToleranceMode"      : """ tolerance mode: default 0: calculate the stopTolerances from nominal sigma, average magn factors and stopSigmaFraction, 1 use stopTolerances given below"""
+       ,"nominalResolutionSigma" : """ RICH-dependent nominal Cherenkov angle resolution"""
+       ,"stopSigmaFraction"      : """ tolerance for total tilt of mirror pair in terms of the fraction of the nominal Cherenkov angle resolution"""
+       ,"stopTolerancePriY"      : """ RICH-dependent tolerance for primary   mirrors rotation around Y correction in mrad to stop the alignment """
+       ,"stopTolerancePriZ"      : """ RICH-dependent tolerance for primary   mirrors rotation around Z correction in mrad to stop the alignment """
+       ,"stopToleranceSecY"      : """ RICH-dependent tolerance for secondary mirrors rotation around Y correction in mrad to stop the alignment """
+       ,"stopToleranceSecZ"      : """ RICH-dependent tolerance for secondary mirrors rotation around Z correction in mrad to stop the alignment """
+       ,"EvtMax"                 : """ maximal number of events processed by brunel PER NODE (hlt fram has ~1500 nodes) """
+       ,"dataVariant"            : """ Right now this is used only for the naming convention. """
+       ,"thisCase"               : """ Right now this is used only for the naming convention. """
+       ,"tiltNames"              : """ Combination of tilts for the calculation and usage of the magnification factors. This is needed even when using predefined magnification factors!!! """
+       ,"fixSinusoidShift"       : """ 1 : fix at sinusoidShift; 0 : it is not fixed """
+       ,"nameStr"                : """ Right now this is used only for the naming convention, but will be set when initiating the Configuration. """
         }
 
     def setNameStr(self):
@@ -126,58 +139,72 @@ class Rich2MirrAlignOnConf(LHCbConfigurableUser):
 
     __slots__ = {
        #### Only change these if you want to pick up from where you left off in iterations, experts only
-        "MajItStart"      : 0
-       ,"MinItStart"      : 0
+        "MajItStart"             : 0
+       ,"MinItStart"             : 0
        #### Do not ever change these!!! Just DONT!
-       ,"Rich"            : 2
-       ,"HistoDir"        : ""
+       ,"Rich"                   : 2
+       ,"HistoDir"               : ""
        ### Better not change these, only for very very VERY good reasons
-       ,"WorkDir"         : "/group/online/AligWork/Rich2/"
-       ,"SaveDir"         : "/group/online/AligWork/MirrorAlignments/"
-       ,"ItNrFile"        : "/group/online/dataflow/options/LHCbA/Rich2_Alignment_Reference_File.txt"
+       ,"WorkDir"                : "/group/online/AligWork/Rich2/"
+       ,"SaveDir"                : "/group/online/AligWork/MirrorAlignments/"
+       ,"ItNrFile"               : "/group/online/dataflow/options/LHCbA/Rich2_Alignment_Reference_File.txt"
        ### May be changed if you know what you are doing
-       ,"coeffCalibTilt"  : 0.3
-       ,"phiBinFactor"    : 3
-       ,"minAverageBinPop": 6
-       ,"deltaThetaWindow": 4.0
-       ,"combinFitMethod" : 5
-       ,"magnFactorsMode" : 0
-       ,"magnifDir"       : "/group/rich/AlignmentFiles/MagnifFactors/Rich2/"
-       ,"solutionMethod"  : 0
-       ,"EvtMax"          : -1
-       ,"stopTolerance"   : 0.1
-       ,"stopToleranceSec": 0.0
-       ,"dataVariant"     : "Collision16"
-       ,"thisCase"        : "online"
-       ,"tiltNames"       : ["", "pri_negYzerZ", "pri_posYzerZ", "pri_zerYnegZ", "pri_zerYposZ", "sec_negYzerZ", "sec_posYzerZ", "sec_zerYnegZ", "sec_zerYposZ"]
-       ,"fixSinusoidShift": 1
-       ,"nameStr"         : "name"
+       ,"dbXMLFile"              : "/group/online/alignment/Rich2/MirrorAlign/v3.xml"
+       ,"coeffCalibTilt"         : 0.3
+       ,"phiBinFactor"           : 3
+       ,"minAverageBinPop"       : 6
+       ,"deltaThetaWindow"       : 4.0
+       ,"combinFitMethod"        : 5
+       ,"magnFactorsMode"        : 0
+       ,"magnifDir"              : "/group/rich/AlignmentFiles/MagnifFactors/Rich2/"
+       ,"solutionMethod"         : 0
+       ,"regularizationMode"     : 1
+       ,"stopToleranceMode"      : 1
+       ,"nominalResolutionSigma" : 0
+       ,"stopSigmaFraction"      : 0
+       ,"stopTolerancePriY"      : 0.1
+       ,"stopTolerancePriZ"      : 0.1
+       ,"stopToleranceSecY"      : 0.1
+       ,"stopToleranceSecZ"      : 0.1
+       ,"EvtMax"                 : -1
+       ,"dataVariant"            : "Collision16"
+       ,"thisCase"               : "online"
+       ,"tiltNames"              : ["", "pri_negYzerZ", "pri_posYzerZ", "pri_zerYnegZ", "pri_zerYposZ", "sec_negYzerZ", "sec_posYzerZ", "sec_zerYnegZ", "sec_zerYposZ"]
+       ,"fixSinusoidShift"       : 1
+       ,"nameStr"                : "name"
         }
 
     _propertyDocDct = {
-        "MajItStart"      : """ Start the alignment at this major iteration. Please make sure this is consitent with MinItStart. """
-       ,"MinItStart"      : """ Start the alignment at this minor iteration. Please make sure this is consitent with MajItStart. """
-       ,"Rich"            : """ Rich1 or Rich2. """
-       ,"HistoDir"        : """ Directory where the savesets are being written to. """
-       ,"WorkDir"         : """ Directory in which all the output will be written. """
-       ,"SaveDir"         : """ Directory in which all output files of the alignment are saved after it has finished """
-       ,"ItNrFile"        : """ File which contains only the number of the current iteration written by the iterator, to be read by the analyzers """
-       ,"coeffCalibTilt"  : """ Mirror-tilts applied for the calculation of the magnification coefficients. """
-       ,"phiBinFactor"    : """ Factor by which the number of phi-bins is reduced. The histograms should now come with 60 phi bins, factor 3 reduced this to 20 bin in the fit."""
-       ,"minAverageBinPop": """ Demanded minimal entries per bin in x-y-bin; atm 6: for 20 phi-bins, 4.8: for 25 phi-bins. """
-       ,"deltaThetaWindow": """ dTheta-range in the histograms; 4.0 for Rich1 and 3.0 for Rich2. """
-       ,"combinFitMethod" : """ Method for fitting the 2D histograms; 1: first fit the slices of dTheta with a Gaussian and then fit to phi using only the mean of the Gaussian, 3: fit a 2D function, 5: Same as 3 but with unifyWidths """
-       ,"magnFactorsMode" : """ Method for calculating the magnification coefficents; 0: magnefication coeff. pre-determined, 1: individual for all mirror pairs, determined in the 0-th iteration and not updated any further, 2: individual for all mirrorpairs, determined on-the-fly on the data. """
-       ,"magnifDir"       : """ directory that contains the pre-determined magnification coeff., files in this folder have to be named Rich[1,2]_MirrMagn _[tiltName]_predefined.txt """
-       ,"solutionMethod"  : """ Solution method for calculating the mirror tilts after the individual fits; 0: Minuit, 1: Algebraic """
-       ,"EvtMax"          : """ maximal number of events processed by brunel PER NODE (hlt fram has ~1500 nodes) """
-       ,"stopTolerance"   : """ tolerance for mirrors (primary mirrors only if stopToleranceSec = 0), in mrad, to stop the alignment because all mirror tilts have converged; historically 0.1 """
-       ,"stopToleranceSec": """ if non-zero [see above], tolerance for secondary mirrors, in mrad, to stop the alignment because all mirror tilts have converged; historically 0.1 """
-       ,"dataVariant"     : """ Right now this is used only for the naming convention. """
-       ,"thisCase"        : """ Right now this is used only for the naming convention. """
-       ,"tiltNames"       : """ Combination of tilts for the calculation and usage of the magnification factors. This is needed even when using predefined magnification factors!!! """
-       ,"fixSinusoidShift": """ 1 : fix at sinusoidShift; 0 : it is not fixed """
-       ,"nameStr"         : """ Right now this is used only for the naming convention, but will be set when initiating the Configuration. """
+        "MajItStart"             : """ Start the alignment at this major iteration. Please make sure this is consitent with MinItStart. """
+       ,"MinItStart"             : """ Start the alignment at this minor iteration. Please make sure this is consitent with MajItStart. """
+       ,"Rich"                   : """ Rich1 or Rich2. """
+       ,"HistoDir"               : """ Directory where the savesets are being written to. """
+       ,"WorkDir"                : """ Directory in which all the output will be written. """
+       ,"SaveDir"                : """ Directory in which all output files of the alignment are saved after it has finished """
+       ,"ItNrFile"               : """ File which contains only the number of the current iteration written by the iterator, to be read by the analyzers """
+       ,"dbXMLFile"              : """ XML file with mirror tilts that is currently in the DB, for comparison of mirror tilts before and after alignment """       
+       ,"coeffCalibTilt"         : """ Mirror-tilts applied for the calculation of the magnification coefficients. """
+       ,"phiBinFactor"           : """ Factor by which the number of phi-bins is reduced. The histograms should now come with 60 phi bins, factor 3 reduced this to 20 bin in the fit."""
+       ,"minAverageBinPop"       : """ Demanded minimal entries per bin in x-y-bin; atm 6: for 20 phi-bins, 4.8: for 25 phi-bins. """
+       ,"deltaThetaWindow"       : """ dTheta-range in the histograms; 4.0 for Rich1 and 3.0 for Rich2. """
+       ,"combinFitMethod"        : """ Method for fitting the 2D histograms; 1: first fit the slices of dTheta with a Gaussian and then fit to phi using only the mean of the Gaussian, 3: fit a 2D function, 5: Same as 3 but with unifyWidths """
+       ,"magnFactorsMode"        : """ Method for calculating the magnification coefficents; 0: magnefication coeff. pre-determined, 1: individual for all mirror pairs, determined in the 0-th iteration and not updated any further, 2: individual for all mirrorpairs, determined on-the-fly on the data. """
+       ,"magnifDir"              : """ directory that contains the pre-determined magnification coeff., files in this folder have to be named Rich[1,2]_MirrMagn _[tiltName]_predefined.txt """
+       ,"solutionMethod"         : """ Solution method for calculating the mirror tilts after the individual fits; 0: Minuit, 1: Algebraic """
+       ,"regularizationMode"     : """ regularization mode: default 0: regularization term contains average magnification coefficients as weight; !=0: all weights in the regularization term are 1."""
+       ,"stopToleranceMode"      : """ tolerance mode: default 0: calculate the stopTolerances from nominal sigma, average magn factors and stopSigmaFraction, 1 use stopTolerances given below"""
+       ,"nominalResolutionSigma" : """ RICH-dependent nominal Cherenkov angle resolution"""
+       ,"stopSigmaFraction"      : """ tolerance for total tilt of mirror pair in terms of the fraction of the nominal Cherenkov angle resolution"""
+       ,"stopTolerancePriY"      : """ RICH-dependent tolerance for primary   mirrors rotation around Y correction in mrad to stop the alignment """
+       ,"stopTolerancePriZ"      : """ RICH-dependent tolerance for primary   mirrors rotation around Z correction in mrad to stop the alignment """
+       ,"stopToleranceSecY"      : """ RICH-dependent tolerance for secondary mirrors rotation around Y correction in mrad to stop the alignment """
+       ,"stopToleranceSecZ"      : """ RICH-dependent tolerance for secondary mirrors rotation around Z correction in mrad to stop the alignment """
+       ,"EvtMax"          	 : """ maximal number of events processed by brunel PER NODE (hlt fram has ~1500 nodes) """
+       ,"dataVariant"            : """ Right now this is used only for the naming convention. """
+       ,"thisCase"               : """ Right now this is used only for the naming convention. """
+       ,"tiltNames"              : """ Combination of tilts for the calculation and usage of the magnification factors. This is needed even when using predefined magnification factors!!! """
+       ,"fixSinusoidShift"       : """ 1 : fix at sinusoidShift; 0 : it is not fixed """
+       ,"nameStr"                : """ Right now this is used only for the naming convention, but will be set when initiating the Configuration. """
         }
 
     ### Dont mess with this filename!!!
diff --git a/Rich/RichMirrorAlignmentOnline/src/MirrPauseHandler.cpp b/Rich/RichMirrorAlignmentOnline/src/MirrPauseHandler.cpp
index 693c80eb9..1d9d0fec5 100644
--- a/Rich/RichMirrorAlignmentOnline/src/MirrPauseHandler.cpp
+++ b/Rich/RichMirrorAlignmentOnline/src/MirrPauseHandler.cpp
@@ -46,6 +46,7 @@ class MirrPauseHandler: public GaudiAlgorithm, virtual public IIncidentListener
     IIncidentSvc *m_is;
 //    Gaudi::Utils::TypeNameString m_monitorSvcType;
     unsigned long m_currentRun;
+    Gaudi::Time m_currentTime; // In preparation for new RunChangeIncident (https://gitlab.cern.ch/clemenci/LHCb/commit/63277a5746f32735c12ec3a9f8fa0d4264ffb5e2)
     bool m_fireRunChange;
     int m_refRunNr;
    std::string m_itNrFile;
@@ -102,6 +103,7 @@ void MirrPauseHandler::handle(const Incident& inc)
   if( m_fireRunChange && "BeginEvent"==inc.type() ) {
     // throw a runchange incident to make sure it reads the xml
     m_is->fireIncident(RunChangeIncident(name(),m_currentRun)) ;
+    // m_is->fireIncident(RunChangeIncident(name(),m_currentRun,m_currentTime)) ; // Uncomment this when using new RunChangeIncident
     m_fireRunChange = false ;
   }
 
@@ -132,6 +134,7 @@ StatusCode MirrPauseHandler::execute()
    // const LHCb::OnlineRunInfo* ori = 0;
           const LHCb::OnlineRunInfo* ori=r->begin<LHCb::OnlineRunInfo>();
           m_currentRun=ori->Run;
+          // m_currentTime=ori->GPSTime; // Uncomment this when using the new RunChangeIncident [HOWEVER: PN is not sure that GPSTime is actually the Time needed here yet]
        }
      }
    }
-- 
GitLab