#!/bin/bash

# Arguments passed from Condor:
# $1: Tarball Name
# $2: Number of Events
# $3: Job Number (ProcId)
# $4: Output Storage Path (root://...)

TARBALL=$1
NEVENTS=$2
JOBNUM=$3
OUTDIR=$4

hostname
date
voms-proxy-info -all

# --- Setup Random Seed ---
# Start from a base (e.g. 1001) and add the Job ID to ensure uniqueness
BASE_SEED=1001
SEED=$(($BASE_SEED + $JOBNUM))

echo "Job index: $JOBNUM"
echo "Random Seed: $SEED"

# --- Setup CMSSW ---
echo "Production step starts"
wget http://hep.baylor.edu/jsamudio/tarballs/$TARBALL
export VO_CMS_SW_DIR=/cvmfs/cms.cern.ch
source $VO_CMS_SW_DIR/cmsset_default.sh

tar xzvf $TARBALL

# ==========================================
# Step 1: LHE-GEN-SIM
# ==========================================
cd CMSSW_14_0_21/src
scram b ProjectRename
eval `scramv1 runtime -sh`

# Calculate First Event number to avoid overlaps
FIRST_EVENT=$(python3 -c "print( $JOBNUM * $NEVENTS + 1 )")

cmsDriver.py Configuration/GenProduction/python/TTH-1Jet-MadJax-SMEFTSim_fragment.py \
    --era Run3_2024 \
    --customise Configuration/DataProcessing/Utils.addMonitoring \
    --beamspot DBrealistic \
    --step LHE,GEN,SIM \
    --geometry DB:Extended \
    --conditions 140X_mcRun3_2024_realistic_v26 \
    --customise_commands "process.RandomNumberGeneratorService.externalLHEProducer.initialSeed=${SEED} \n process.source.numberEventsInLuminosityBlock=cms.untracked.uint32(10000) \n from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper ; randSvc = RandomNumberServiceHelper(process.RandomNumberGeneratorService) ; randSvc.populate()" \
    --datatier GEN-SIM,LHE \
    --eventcontent RAWSIM,LHE \
    --python_filename step1_LHEGS_cfg.py \
    --fileout file:step1_LHEGS.root \
    --number ${NEVENTS} \
    --nThreads 8 \
    --no_exec --mc

cmsRun step1_LHEGS_cfg.py

# ==========================================
# Step 2: DRPremix
# ==========================================
cd ../../
cd CMSSW_14_0_21_patch4/src/
scram b ProjectRename
eval `scramv1 runtime -sh`

# Bring the file from previous step
mv ../../CMSSW_14_0_21/src/step1_LHEGS.root .

cmsDriver.py \
    --era Run3_2024 \
    --customise Configuration/DataProcessing/Utils.addMonitoring \
    --procModifiers premix_stage2 \
    --datamix PreMix \
    --step DIGI,DATAMIX,L1,DIGI2RAW,HLT:2024v14 \
    --geometry DB:Extended \
    --conditions 140X_mcRun3_2024_realistic_v26 \
    --datatier GEN-SIM-RAW \
    --eventcontent PREMIXRAW \
    --python_filename step2_DRPremix_cfg.py \
    --fileout file:step2_DRPremix.root \
    --filein file:step1_LHEGS.root \
    --number ${NEVENTS} \
    --customise_commands "from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper ; randSvc = RandomNumberServiceHelper(process.RandomNumberGeneratorService) ; randSvc.populate()" \
    --pileup_input dbs:/Neutrino_E-10_gun/RunIIISummer24PrePremix-Premixlib2024_140X_mcRun3_2024_realistic_v26-v1/PREMIX \
    --nThreads 8 \
    --no_exec --mc

cmsRun step2_DRPremix_cfg.py

# ==========================================
# Step 3: AOD
# ==========================================
# (Stays in same directory, same CMSSW release usually fine, but assuming strict separation based on your script)

cmsDriver.py \
    --era Run3_2024 \
    --customise Configuration/DataProcessing/Utils.addMonitoring \
    --step RAW2DIGI,L1Reco,RECO,RECOSIM \
    --geometry DB:Extended \
    --conditions 140X_mcRun3_2024_realistic_v26 \
    --datatier AODSIM \
    --eventcontent AODSIM \
    --python_filename step3_AODSIM_cfg.py \
    --fileout file:step3_AODSIM.root \
    --filein file:step2_DRPremix.root \
    --number ${NEVENTS} \
    --nThreads 8 \
    --no_exec --mc

cmsRun step3_AODSIM_cfg.py

# ==========================================
# Step 4: MiniAOD
# ==========================================
cd ../..
cd CMSSW_15_0_15/src/
scram b ProjectRename
eval `scramv1 runtime -sh`

mv ../../CMSSW_14_0_21_patch4/src/step3_AODSIM.root .

cmsDriver.py \
    --era Run3_2024 \
    --customise Configuration/DataProcessing/Utils.addMonitoring \
    --step PAT \
    --geometry DB:Extended \
    --conditions 150X_mcRun3_2024_realistic_v2 \
    --datatier MINIAODSIM \
    --eventcontent MINIAODSIM1 \
    --python_filename step4_MiniAOD_cfg.py \
    --fileout file:step4_MiniAODv6.root \
    --filein file:step3_AODSIM.root \
    --number ${NEVENTS} \
    --nThreads 8 \
    --no_exec --mc

cmsRun step4_MiniAOD_cfg.py

# ==========================================
# Transfer Output
# ==========================================
echo "Copying MINIAOD file"
eval `scram unsetenv -sh`

FINAL_NAME=step_miniaodsim_${JOBNUM}.root

gfal-copy -f step4_MiniAODv6.root ${OUTDIR}/${FINAL_NAME}

echo "Copy completed"
date
