# Self-contained, runnable FGMC p_astro example.  All inputs are synthetic
# and generated locally by make_demo_inputs.py -- no cluster access or real
# analysis products are required.  See the "FGMC method (O4 low-latency)"
# section of the top-level README.md for the full preparation guide.
#
# The command variables below default to the installed console scripts; the
# test suite overrides them (e.g. CALCULATE_FGMC="python -m
# pastro.bin.calculate_pastro_fgmc") to run the source tree directly.

PYTHON ?= python3
INITIALIZE_FGMC ?= sgnl-pastro-initialize-fgmc-model
CALCULATE_FGMC ?= sgnl-pastro-calculate-fgmc
CALCULATE_FGMC_COINC ?= sgnl-pastro-calculate-fgmc-coinc

# Demo rates / sensitive volumes / FAR threshold: the O3-era production
# values (see examples/fgmc/test_model/Makefile).  In a real preparation
# these come from sgnl-pastro-calculate-rates (see Rates_for_O4.md).
RATES = '{"BBH": 18.0730098234, "NSBH": 52.9087878433, "BNS": 662.448825556}'
RATES_INJ = '{"BBH": 18.0730098234, "NSBH": 52.9087878433, "BNS": 662.448825556}'
V = '{"BBH": 3.720777380922114, "NSBH": 0.13036984478340222, "BNS": 0.01447408360378373}'
# 2 per day, in counts per Julian year
FAR_THRESHOLD = 730.5

all: single_trigger.json coinc_results.jsonl

# synthetic template weights (ceff_*.h5) and a two-event coinc document;
# the stamp file stands in for the several files one run produces
demo-inputs.stamp: make_demo_inputs.py
	$(PYTHON) make_demo_inputs.py
	touch $@

# Build the model.  In production, additionally pass
# --rankstat-filename <dist_stat_pdfs .xml.gz> so the likelihood-ratio
# normalization A is computed from the analysis' ranking-statistic PDF
# (omitted here: the demo has no analysis behind it, leaving A = 1).
fgmc_model.h5: demo-inputs.stamp
	$(INITIALIZE_FGMC) --weights-dir . --rates $(RATES) --rates-inj $(RATES_INJ) --V-new $(V) --far-threshold $(FAR_THRESHOLD) --output $@

# evaluate a single trigger from hand-entered numbers
single_trigger.json: fgmc_model.h5
	$(CALCULATE_FGMC) --fgmc-model fgmc_model.h5 --templateid 100 --snr 9.0 --lnlr 12.0 > $@
	cat $@

# evaluate every coincident event in a coinc document
coinc_results.jsonl: fgmc_model.h5 demo-inputs.stamp
	$(CALCULATE_FGMC_COINC) --fgmc-model fgmc_model.h5 coinc.xml.gz > $@
	cat $@

clean:
	rm -f ceff_*.h5 coinc.xml.gz fgmc_model.h5 single_trigger.json coinc_results.jsonl demo-inputs.stamp

.PHONY: all clean
