The gh_pre_scan and gh_post_scan hook

One of the typical use cases is to take an image at each point of a scan. The detector attributes are prepared inside the gh_pre_scan hook:

 
  #
  # ScanInfo is needed by the prepareDetectorAttrs() function
  #
  ...
  scanInfo = HasyUtils.createScanInfo( self.getParentMacro().getCommand())
  self.setEnv( "ScanInfo", scanInfo)
        
  if HasyUtils.isInMg( self, detectorName):
      if not HasyUtils.prepareDetectorAttrs( self, 
                                             name = detectorName,
                                             trig_mode = 'ints',
                                             nbFrames = 1, # per trigger 
                                             acq_time = scanInfo[ 'sampleTime'], 
                                             NbTriggers = scanInfo[ 'intervals'] + 1,
                                             ArmFlag = True, 
                                             ImagesPerFile = 1000): 
  #
  #    sets these detector attributes ...
  #    CountTime  
  #    NbImages   
  #    NbTriggers 
  #    TriggerMode 
  #
  #  ... and these Filewrite attributes
  #    FilenamePattern = current/raw/<prefix>_<ScanID>/<detectorName>/<prefix>_<ScanID> 
  #      using ScanID, ScanFile
  #    ImagesPerFile   = 1000 by default
  #
  #  Eventually execute
  #    Arm(), if armFlag

To reset things the gh_post_scan hook is used. This is absolutely necessary because the next action could be a 'ct' which does not involve any hooks or other injected code.

class gh_post_scan(Macro):
    def run( self):
        if not HasyUtils.isInMg( self, detectorName):
            self.output( "general_features.gh_post_scan: %s not in the MG, DONE" % detectorName)
            return True
        self.output( "general_features.post_scan %s to %s" % (detectorName, detectorRoot))
        if not HasyUtils.resetDetectorAttrs( self, name = detectorName, rootDir = detectorRoot):
            self.output( "general_features.gh_post_scan: resetDetector: returned error")
            return False
        res = HasyUtils.storeEnv()
        self.output( "gh_post_scan: stored env in %s" % res)
        return True

This is the debug output when executing an ascan:

 
p10/door/haspp10lab.01 [7]: ascan exp_dmy01 0 10 10 0.1
Operation will be saved in /gpfs/commissioning/raw/svk_zipgrp_00010/svk_zipgrp_00010.fio (fio)
Scan #51 started at Tue Feb 23 14:21:38 2021. It will take at least 0:00:01.100696
 #Pt No   exp_dmy01  lab_t01   curpetra    e4m        dt   
general_features.pre_scan hook
scanInfo: {'sampleTime': 0.1, 'serialno': 51, 'intervals': 10, 'motors': [{'start': 0.0, 'stop': 10.0, 'name': 'exp_dmy01'}], 'title': 'ascan exp_dmy01 0.0 10.0 10 0.1'}
MsUtils.prepareEigerDectrisAttrs: CountTime to 0.1
MsUtils.prepareEigerDectrisAttrs: FrameTime to 0.1
MsUtils.prepareEigerDectrisAttrs: NbImages to 1
MsUtils.prepareEigerDectrisAttrs: TriggerMode to ints
MsUtils.prepareEigerDectrisAttrs: NbTrigger to 11
MsUtils.prepareEigerDectrisAttrs: FilenamePattern to commissioning/raw/svk_zipgrp_00051/e4m/svk_zipgrp_00010
MsUtils.prepareEigerDectrisAttrs: ImagesPerFile to 11
   0          0        0.1     0.0201445   (2, 1)   1.05812 
   1          1        0.1     0.0201445   (2, 1)   1.92005 
   2          2        0.1     0.0201445   (2, 1)   2.33897 
   3          3        0.1     0.0198458   (2, 1)    2.7986 
   4          4        0.1     0.0198458   (2, 1)   3.21271 
   5          5        0.1     0.0195969   (2, 1)   3.67174 
   6          6        0.1     0.0195969   (2, 1)   4.09654 
   7          7        0.1     0.0195573   (2, 1)   4.55709 
   8          8        0.1     0.0195573   (2, 1)   4.97387 
   9          9        0.1     0.0195573   (2, 1)   5.43166 
   10         10       0.1     0.0188995   (2, 1)   5.84431 
general_features.post_scan e4m to /home/p10user/temp
MsUtils.resetEigerDectrisAttrs: FilenamePattern to current/raw/SingleShots/svk_zipgrp_00010
MsUtils.resetEigerDectrisAttrs: ImagesPerFile to 1
MsUtils.resetEigerDectrisAttrs: NbImages to 1
MsUtils.resetEigerDectrisAttrs: NbTriggers to 1
MsUtils.resetEigerDectrisAttrs: TriggerMode to ints
gh_post_scan: stored env in /gpfs/commissioning/raw/svk_zipgrp_00010//svk_zipgrp_00051.env
Operation saved in /gpfs/commissioning/raw/svk_zipgrp_00010/svk_zipgrp_00051.fio (fio)
Scan #51 ended at Tue Feb 23 14:21:45 2021, taking 0:00:07.048096. Dead time 84.4% (motion dead time 1.4%)

Notice that FilenamePattern is reset to the SingleShots directory after the scan is finished. This way 'ct' will write to the correct directory.