// FileName: reco_far_Alt_All_R1_18.C
// Created by: Joshua Boehm
// Last Modified: July 18, 2005
                                                                                
// Usage
// cp <path>/reco_far_Alt_All_R1_18.C reco_all.C
// loon -bq "reco_all.C()" <data_file>

/////////////////////////////////////////////////////////
// Description of this file:                           //
// This file will produce an all stream (both spill    //
//  and cosmic events) but all processed with the      //
//  settings for the cosmic reconstruction. This is    //
//  only meant to do FarDet processing in R1-18.       //
//  Processing occurs through a single path "All"      //
//  Each output stream requires its own processing     //
//   branch and so there are small offshoot branches   //
// Demuxer: Alt/Cambridge                              //
//                                                     //
// Output:                                             //
// This file produces BeamNtp Information in each      //
//   output stream                                     //
// Output Files:                                       //
//  CandA.root - candidate output file                 //
//  ntupleStA.root - NtpSt ntuple output               //
//  ntupleStA.sub.root - NtpSt trimmed of stp info     //
/////////////////////////////////////////////////////////

class JobC;

int LoadLibraries();
int GetRunNumber();

void SetCandOutputWithBNtp(JobCModule &jcm, TString file);
void SetCandOutputNoBNtp(JobCModule &jcm, TString file);
void SetNtpStOutputWithBNtp(JobCModule &jcm, TString file);
void SetNtpStOutputNoBNtp(JobCModule &jcm, TString file);

void SetMSGLevels(JobC &jc);

void reco_all(){

  // Link dynamic libraries
  LoadLibraries();
  
  JobC jc;

  //Creating Path

  jc.Path.Create("All",
                 "NoiseFilterModule::Ana "
                 "RecordSetupModule::Get "
                 "DigitListModule::Get "
                 "DigitListModule::Reco "
                 "FilterDigitListModule::Reco "
                 "FilterLIModule::Ana "
                 "DeMuxDigitListModule::Reco "
		 "StripSRListModule::Reco "
		 "SliceSRListModule::Reco "
		 "TrackSRListModule::Reco "
		 "FitTrackSRListModule::Reco "
		 "ClusterSRListModule::Reco "
		 "ShowerSRListModule::Reco "
                 "EventSRListModule::Reco "
                 "RecordSetupModule::Reco "
                 "NtpBDLite::Reco "
		 "Output::Put "
		 );


  //Input Parameters
  jc.Input.Set("Format=input");
  jc.Input.Set("Streams=DaqSnarl");  //takes data as input

  // Protection for old runs to use the correct B field settings
  int runNumber = GetRunNumber();  
  
  ///////////////////////////////////////////////////////////
  //  This checks to make sure the B field is set to zero 
  //     when sm2 had no field  
  //Set B=0 for SM2 if runNumber < 17566
  if (runNumber < 17566) {
    UgliLoanPool* uglipool = UgliLoanPool::Instance(); //Redundant after 2005-01-12
    BfldLoanPool* bfldpool = BfldLoanPool::Instance();
    bfldpool->Set("NoFieldBeyondZ=15.3797");
    bfldpool->Update();
  }
  // End of Old file protection
  ///////////////////////////////////////////////////////////


  //////////////////////////////////////////////
  // Set up the Calibrator 
  Calibrator& cal = Calibrator::Instance();
  cal.Set("TimeCalibrator", "PulserTimeCalScheme");
  cal.Set("PeCalibrator",   "PEGainCalScheme");
  cal.Set("VALinCalibrator","VaLinearityCalScheme");
  cal.Set("DriftCalibrator","SimpleCalScheme"); // -> NOT default
  cal.Set("LinCalibrator",  "SimpleCalScheme"); // -> NOT default
  cal.Set("StripCalibrator","StripToStripCalScheme");
  cal.Set("AttenCalibrator","StripAttenCalScheme");
  cal.Set("MIPCalibrator",  "MIPCalScheme");
  cal.Set("Thermometer",    "TemperatureCalScheme");

  // Sets it to use Andy Blake's timing constants
  //    the alternative being Brians 
  Calibrator::TimeCalibrator().Set("MuonTask=Andys");
  //This turns on Andy Blakes time-walk correction
  Calibrator::TimeCalibrator().Set("DoWalkCorrection=1");
  //This turns on the time jump table which corrects for hardware swaps 
  //  through Fall 2004. (AB)
  Calibrator::TimeCalibrator().Set("UseJumps=1");
  //Turns on the automatic LI timewalk correction
  Calibrator::TimeCalibrator().Set("UsePulserCalibration=0");
  //End of Calibrator Setup
  //////////////////////////////////////////////


  //////////////////////////////////////
  //Configure Reconstruction Software for Far Det Cosmic data .
  // For a full description of the different possible module options see the 
  //   Document:  ConfigOptions.txt

  jc.Path("All").SetAllFilters(1);  //This line I believe is useless at the moment

  jc.Path("All").Mod("FilterLIModule").Set("LIFinderName=SimplePatternFinder");
  jc.Path("All").Mod("SliceSRListModule").Set("SliceListAlgConfig=FarCosmic");
  jc.Path("All").Mod("ClusterSRListModule").Set("ClusterListAlgConfig=FarCosmic");
  jc.Path("All").Mod("ShowerSRListModule").Set("ShowerListAlgConfig=FarCosmic");
  jc.Path("All").Mod("TrackSRListModule").Set("TrackListAlgConfig=FarCosmic");
  jc.Path("All").Mod("FitTrackSRListModule").Set("FitTrackListAlgConfig=FarCosmic");
  jc.Path("All").Mod("EventSRListModule").Set("EventListAlgConfig=FarCosmic");
  jc.Path("All").Mod("DeMuxDigitListModule").Set("NameListOut=altdemux");
  jc.Path("All").Mod("DeMuxDigitListModule").Set("SwitchPersToTemp=1");
  //SwitchPersToTemp=1 Do not write out original CandDigitList
  //SwitchPersToTemp=0 Do write out original CandDigitList
  jc.Path("All").Mod("DeMuxDigitListModule").Set("DeMuxDigitListAlgConfig=devel");
                                                                                                   
  jc.Path("All").Mod("StripSRListModule").Set("ListIn=altdemux");
	
  //////////////////////////////////////
  //DigitListModule parameters
  jc.Path("All").Mod("DigitListModule").Set("ListsToMake=1");
  //Cosmics's filter parameters
  jc.Path("All").Mod("FilterDigitListModule").Set("FilterDigitListAlgorithm=AlgFilterDigitListSR");
  jc.Path("All").Mod("FilterDigitListModule").Set("SwitchPersToTemp=1"); 
  //Don't write out original CandDigitList.

  ////////////////////////////////////////////////////////////////

  //Display the Configuration of modules
  jc.Path("All").Mod("FilterDigitListModule").Report();
  jc.Path("All").Mod("DeMuxDigitListModule").Report();
  jc.Path("All").Mod("StripSRListModule").Report();
  jc.Path("All").Mod("ShowerSRListModule").Report();
  jc.Path("All").Mod("TrackSRListModule").Report();
  jc.Path("All").Mod("FitTrackSRListModule").Report();

  ////////////////////////////////////////////////////////////////

  /////////////////////////////////////////////////
  //    Setting the Output Paths                 //
  //  - This script produces only 3 output files // 
  //    Cand, ntp, and nts files                 //
  /////////////////////////////////////////////////

  //Set Candidate Output to CandA.root (all of the snarls included in output)
  //this will become the .cand. file

  SetCandOutputWithBNtp(jc.Path("All").Mod("Output"), "CandA.root");


 //Ntuple record has its own output file so needs its own output module
  jc.Path.Create("cNtpSR",
                 "NtpStModule::Get "
                 "NtpSRModule::Reco "
                 "Output::Put ");

  jc.Path("cNtpSR").Mod("NtpSRModule").Set("UseStandard=1"); // use NtpSt
  jc.Path("cNtpSR").SetAllFilters(false);

  //Set the ntuple output to ntupleStA.root (all of the Snarls included in output)
  // this will become the .cntp. file
  SetNtpStOutputWithBNtp(jc.Path("cNtpSR").Mod("Output"), "ntupleStA.root");
  jc.Path.Attach("All","cNtpSR");


  // Ntuple abridged record
  jc.Path.Create("cNtpSRFilter",
                 "NtpStFilterModule::Reco "
                 "Output::Put ");

  //Sets the output to ntupleStA.sub.root (all of the Snarls included)
  // this will become the .cnts. file
  SetNtpStOutputWithBNtp(jc.Path("cNtpSRFilter").Mod("Output"), "ntupleStA.sub.root");  
  jc.Path.Attach("All", "cNtpSRFilter");

  //////////////////////////////////////////////////////
  ///         End of Output Settings                ////
  //////////////////////////////////////////////////////

  SetMSGLevels(jc);

  //Other Run Options
  //  jc.Path("All").Run(100);  //Run over the first 100 snarls
  //  jc.Input.Next(10);  //Skip to Snarl 10

  jc.Path("All").Run();
  jc.Path("All").Report();
  jc.Msg.Stats(); 

}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

//  Everything from this point forward are sub-functions        //


int LoadLibraries()
{
// Link dynamic libraries

  //far Cam Libraries
  gSystem->Load("libDataUtil");
  gSystem->Load("libRecoBase");
  gSystem->Load("libNoiseFilter");
  gSystem->Load("libFarDetDataQuality");
  gSystem->Load("libFilterDigitSR");
  gSystem->Load("libBField");
  gSystem->Load("libNumericalMethods");
  gSystem->Load("libSwimmer");
  gSystem->Load("libDeMux");
  gSystem->Load("libAltDeMux");  
  gSystem->Load("libFilterLI");
  gSystem->Load("libCandStripSR");
  gSystem->Load("libCandSliceSR");
  gSystem->Load("libCandTrackSR");
  gSystem->Load("libCandClusterSR");
  gSystem->Load("libCandSubShowerSR");
  gSystem->Load("libCandShowerSR");
  gSystem->Load("libCandFitTrackSR");
  gSystem->Load("libCandEventSR");
  gSystem->Load("libVertexFinder");
  gSystem->Load("libTimeCalibratorSR");
  gSystem->Load("libAstroUtil");
  gSystem->Load("libCandNtupleSR");
  gSystem->Load("libMCNtuple");  
  gSystem->Load("libTruthHelperNtuple");
  gSystem->Load("libStandardNtuple");
  gSystem->Load("libCandNtupleSRModule");
  gSystem->Load("libTruthHelperNtupleModule");
  gSystem->Load("libStandardNtupleModule");

  gSystem->Load("libFiltration");
  gSystem->Load("libCandShield");     // Only to be used R1.17+
  gSystem->Load("libSpillTiming");
  gSystem->Load("libBeamDataUtil");   //Beam mon
  gSystem->Load("libBeamDataNtuple"); //Beam Mon
}


int GetRunNumber(){
  ////////////////////////////////////////////////////////
  //Get the run number from the Input file name
  //Allows us to set the name of the dst file
  JobCEnv& jce = JobCEnv::Instance();
  string filename = jce.GetFileName(0);
  string rnumstring = filename.substr(filename.find_last_of("_")-8,8);
  int run = atoi(rnumstring.c_str());
//  string srnstring = filename.substr(filename.find_last_of("_")+1,4);

  return run;
}

void SetCandOutputWithBNtp(JobCModule &jcm, TString file){

  jcm.Cmd("DefineStream Config ConfigRecord");
  jcm.Cmd("DefineStream NtpBDLite NtpBDLiteRecord");
  jcm.Set("Streams=NtpBDLite,Cand,Config,DaqSnarl");
  jcm.Set("FileName="+file);
}

void SetCandOutputNoBNtp(JobCModule &jcm, TString file){

  jcm.Cmd("DefineStream Config ConfigRecord");
  jcm.Set("Streams=Cand,Config,DaqSnarl");
  jcm.Set("FileName="+file);
}

void SetNtpStOutputWithBNtp(JobCModule &jcm, TString file){
  jcm.Cmd("DefineStream NtpSt NtpStRecord");
  jcm.Cmd("DefineStream NtpBDLite NtpBDLiteRecord");
  jcm.Set("Streams=NtpBDLite,NtpSt");
  jcm.Set("FileName="+file);
}

void SetNtpStOutputNoBNtp(JobCModule &jcm, TString file){
  jcm.Cmd("DefineStream NtpSt NtpStRecord");
  jcm.Set("Streams=NtpSt");
  jcm.Set("FileName="+file);
}

void SetMSGLevels(JobC &jc){

  //Configure the message service
  jc.Msg.SetLevel("NoiseFilter","Error");
  jc.Msg.SetLevel("Cand","Error");
  jc.Msg.SetLevel("Calibrator","Error");
  jc.Msg.SetLevel("Dbi","Error");
  jc.Msg.SetLevel("DetSim","Error");
  jc.Msg.SetLevel("Reroot","Error");
  jc.Msg.SetLevel("Photon","Warning");
  jc.Msg.SetLevel("Per","Error");
  jc.Msg.SetLevel("Io", "Error");
  jc.Msg.SetLevel("DeMuxDigitListModule","Error");
  jc.Msg.SetLevel("Plex","Error");
  jc.Msg.SetLevel("FitTrackSR","Warning");
  jc.Msg.SetLevel("AlgFitTrackSR","Warning");
  jc.Msg.SetLevel("FilterLI", "Error");
  jc.Msg.SetLevel("LIPatternFinder","Error");
  jc.Msg.SetLevel("TrackSR","Warning");
  jc.Msg.SetLevel("ClusterSR","Error");
  jc.Msg.SetLevel("EventSR","Error");
//  jc.Msg.SetLevel("JobC","Warning");
  jc.Msg.SetLevel("SubShowerSR","Error");
  jc.Msg.SetLevel("AltDeMuxModule","Error");
  jc.Msg.SetLevel("AltDeMux","Fatal");
  jc.Msg.SetLevel("AlgAltDeMux","Fatal");
  jc.Msg.SetLevel("AlgShowerSS", "Warning");
}
