// FileName: reco_near_spill_garlic00_base_dogwood0.C
// Created 12 Mar 2009

// Usage
// loon -bq add_to_path(<path>) reco_near_spill_garlic00_base_dogwood0.C <ptsim_file>

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

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

class JobC;

#include "DataUtil/MasterGeVPerMip.h"

#include "headers/io/attach_output_mc_srsafitter.h"

#include "headers/near_mc/near_mc_create_path.h"
#include "headers/near_mc/near_mc_configure.h"
#include "headers/near_mc/near_mc_do_report.h"
#include "headers/near_mc/near_mc_set_msg_levels.h"

#include "headers/near/near_configure_shower.h"

#include "headers/util/run.h"

#include "headers/configure_ugli.h"
#include "headers/near_mc/near_mc_set_calibrator.h"
#include "headers/apply_spectrometer_fudge.h"
#include "headers/attach_mrcc_path_near.h"

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

void reco_near_spill_garlic00_base_dogwood0(int nSkip = 0, int nRun = 100000)
{
  test_filename('n');

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

  RecJobRecord::SetGlobalProdName("Dogwood");

  JobC jc;

  // Set up job path to read SimSnarlRecords
  JobCPath& mcpath = jc.Path.Create("Reco");
  // Could place MCMerge job module here to handle overlays
  mcpath.PushBack("PhotonTransport","Get");
  mcpath.PushBack("DetSim","Get");

  JobCPath& near_mc = near_mc_create_path(jc, "Reco1");

  mcpath.Attach(&near_mc);

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

  mcpath_configure(mcpath, Detector::kNear);

  near_mc_configure(near_mc);

  near_configure_shower(near_mc, false);

  configure_ugli();

  near_mc_set_calibrator(0);

  //SpectrometerFudge added April 23,2007
  //this should be removed after database entry is updated
  apply_spectrometer_fudge();

  mcpath.Mod("PhotonTransport").Report();
  mcpath.Mod("DetSim").Report();
  near_mc_do_report(near_mc);

  attach_output_mc_srsafitter(jc, near_mc, "CandS.root", true);

  attach_mrcc_path_near(jc, near_mc, 1, "NearBeam", false, SimFlag::kMC);

  near_mc_set_msg_levels(jc);

  // Attempt to debug database problem. If this turns out to be
  // the solution it should be moved elsewhere and used in all scripts.
  DbiTableProxyRegistry& dbiCfg = DbiTableProxyRegistry::Instance();
  dbiCfg.Set("MakeConnectionsPermanent = -1");
  dbiCfg.Update();

  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");
  }
}

