// FileName: reco_far_spill_garlic00_base_dogwood0.C
// Created 12 Mar 2009
//
// Usage
// loon -bq add_to_path(<path>) reco_far_spill_garlic00_base_dogwood0.C <ptsim_file>

/////////////////////////////////////////////////////////
// Output:                                             //
// Output Files:                                       //
//  CandS.root - candidate output file                 //
//  ntupleStS.root - NtpSt ntuple output               //
/////////////////////////////////////////////////////////

#include "headers/aclic.h"
#include "headers/test_filename.h"
#include "DataUtil/MasterGeVPerMip.h"

class JobC;

#include "headers/far_mc/far_mc_create_path.h"
#include "headers/far_mc/far_mc_configure.h"
#include "headers/far_mc/far_mc_do_report.h"
#include "headers/far_mc/far_mc_set_calibrator.h"
#include "headers/far_mc/far_mc_set_msg_levels.h"

#include "headers/io/attach_output_mc.h"

#include "headers/util/run.h"

#include "headers/configure_ugli.h"

void mcpath_configure(JobCPath& mcpath, Detector::Detector_t det);

void reco_far_spill_garlic00_base_dogwood0(int nSkip = 0, int nRun = 100000)
{
  test_filename('f');

  gROOT->Macro("macros/load_libs.C");

  RecJobRecord::SetGlobalProdName("Dogwood");

  JobC jc;

  // Set up job path to read SimSnarlRecords.
  JobCPath& mcpath = jc.Path.Create("Reco");
  mcpath.PushBack("PhotonTransport","Get");
  mcpath.PushBack("DetSim","Get");

  JobCPath& far_mc = far_mc_create_path(jc, "Reco1");
  mcpath.Attach(&far_mc);

  // takes SimSnarl records as input
  jc.Input.Set("Streams=SimSnarl");

  //turn off all filters - TODO, do this somwhere else
  mcpath.SetAllFilters(0);
  far_mc.SetAllFilters(0);

  mcpath_configure(mcpath, Detector::kFar);

  // TODO, do this elsewhere
  //Additional Configuration settings supplied by Nathaniel
  mcpath.Mod("DetSim").Set("applyTrigger=0");    //Turn off all triggers - soill trigger event
  mcpath.Mod("PhotonTransport").Set("NoiseWindow=40e-6"); //Increase noise window to +- 40 us of noise for spill trigger

  far_mc_configure(far_mc);

  configure_ugli();

  far_mc_set_calibrator(0);

  mcpath.Mod("PhotonTransport").Report();
  mcpath.Mod("DetSim").Report();
  far_mc_do_report(far_mc);

  attach_output_mc(jc, far_mc, "CandS.root", true, "Garlic");

  far_mc_set_msg_levels(jc);

  run(jc, mcpath, nSkip, nRun);
}

void mcpath_configure(JobCPath& mcpath, Detector::Detector_t det)
{
  // expect all file to be processed by this
  ReleaseType::Release_t official_rel = ReleaseType::kDogwood |
                                        ReleaseType::kGarlic |
                                        ReleaseType::k00;

  TString alt_rel = gSystem->Getenv("ENV_GEVPERMIP_SET");
  if(alt_rel != ""){
    // user externally chose an alternative, so be it..
    choose_gevpermip_release(alt_rel.Data());
  }else{
    // go with the official choice
    choose_gevpermip_release(official_rel);
  }


  mcpath.SetAllFilters(0);

  // explicitly set the value of GeVPerMip
  JobCModule& photon = mcpath.Mod("PhotonTransport");

  double gpm = get_gevpermip(det);
  photon.Set(TString::Format("GeVPerMip=%lf", gpm));


  // Defaults in photon transport as of 24/1/08
  // Total noise rate per second in the whole detector
  photon.Set("FDNoiseRate=3762138");
  // Set non-zero to use the old cedar FD noise model
  photon.Set("UseSimpleNoiseModel=0");


  if(det == Detector::kFar){
    JobCModule& detsim = mcpath.Mod("DetSim");
    photon.Set("FibreVelocityFudge=1.02");
    detsim.Set("vaTimingWidth=0.12");
  }
}
