# File: fitWater.mac
# Brief: Macro showing how to run various fitters in the water phase
#
# Date: 19-05-2014
# Contact: Dr M Mottram, <m.mottram@qmul.ac.uk>
# Revisions:
#       19-05-2014 I. Coulter <icoulter@hep.upenn.edu>
#                  Transfered from old fitWater.mac
#
# Example Macro for simulating and fitting electrons in SNO+ in H2O.
# This macro applies some water-specific fitters.
# For a more general overview of the scintillator phase fitters,
# refer to fit.mac
#
# Note: This aims to show possible uses of fitters in the water phase.
# There is no guarantee over the accuracy of the results these fitters
# return.
# For the recommended use, refer to the RAT companion/waterFitter code
#

# Use the waterfill geometry
/rat/db/set DETECTOR geo_file "geo/snoplus_water.geo"

/run/initialize

# BEGIN EVENT LOOP
/rat/proc frontend
/rat/proc trigger
/rat/proc eventbuilder
/rat/proc count
/rat/procset update 10
/rat/proc calibratePMT

# Simple usage, this is probably what you want to do
# This produces the best possible position, direction and energy
# reconstruction values as defined by reconstruction group
/rat/proc waterFitter

## Advanced usage (if you want to try your own combination fitter)
# Start with a centroid fit
/rat/proc fitter
/rat/procset method "centroid"

# quadFitter
/rat/proc fitter
/rat/procset method "quad"

# Now use these seeds for a likelihood method position fit, optimised
# using the metaOptimiser metaSphereSeed-powell with the gv1d pdf, using
# the quadFitter as a seed and the modeCut PMT selector.
# Catchily known as
# "positionTimeLikelihood:metaSphereSeed-powell:gv1d:modeCut:quad"
# so give it the nickname "positionFit" instead...
/rat/proc fitter
/rat/procset method "positionTimeLikelihood"
/rat/procset optimiser "metaDriveCorrectSeed-powell"
/rat/procset pdf "gv1d"
/rat/procset seed "quad"
/rat/procset selector "modeCut"
/rat/procset name "positionFit"

## Add a direction fit:
# First use simpleDirection, which finds a seed direction using the
# centroid of PMT hits
/rat/proc fitter
/rat/procset method "simpleDirection"
/rat/procset seed "positionFit"

# Likelihood fit for direction using the metaDirectionSeed metaOptimiser
# with minuit, the directionPDF as a PDF, the modeCut as a PMTselector
# and positionFit as a position seed, simpleDirection as a directionSeed.
# Rather than
# "directionLikelihood:metaDirectionSeed-minuit:directionPDF:modeCut:waterFitter",
# name this "directionFit"
/rat/proc fitter
/rat/procset method "directionLikelihood"
/rat/procset optimiser "metaDirectionSeed-minuit"
/rat/procset pdf "directionPDF"
/rat/procset seed "simpleDirection:positionFit"
/rat/procset seed "positionFit"
/rat/procset selector "modeCut"
/rat/procset name "directionFit"

# Another likelihood fit for position and direction simultaneously
# using the simulated annealing optimiser, positionDirectionPDF,
# modeCut and the waterFitter as a seed.
# Name this as "positionDirectionFit"
/rat/proc fitter
/rat/procset method "positionTimeDirectionLikelihood"
/rat/procset optimiser "simulatedAnnealing"
/rat/procset pdf "positionDirectionPDF"
/rat/procset seed "waterFitter"
/rat/procset selector "modeCut"
/rat/procset name "positionDirectionFit"

## Add some energy fits:
# First using a single valued nhit per MeV conversion
/rat/proc fitter
/rat/procset method "simpleEnergy"

# Then a 2D lookup table, using nhit and radius
/rat/proc fitter
/rat/procset method "energyLookup"
/rat/procset seed "positionFit"

## Now run some classifiers:
# beta14
/rat/proc classifier
/rat/procset classifier "beta14"
/rat/procset seed "positionFit"

# ITR
/rat/proc classifier
/rat/procset classifier "ITR"
/rat/procset seed "positionFit"

# QPDT
/rat/proc classifier
/rat/procset classifier "QPDT"
/rat/procset seed "positionFit"

/rat/proclast outroot
/rat/procset file "fit_electrons_h2o.root"
# END EVENTLOOP

/generator/add combo gun:fill
/generator/vtx/set e- 0 0 0 6.0
/generator/pos/set 0 0 0
/generator/rate/set 1

/rat/run/start 10
exit