// FileName: reco_near_Cosmic_Scaled_Chopped_R1_18_3.C
// Created by: Joshua Boehm
// Last Modified: February 10, 2006
                                                                              
// Usage
// cp <path>/reco_near_Cosmic_Scaled_Chopped_R1_18_3.C reco_all.C
// loon -bq "reco_all.C()" <data_file>

/////////////////////////////////////////////////////////
// Description of this file:                           //
// This file will produce a Cosmic stream (no spill    //
//  data included based on the spill trigger). This is //
//  only meant to do NearDet processing in R1-18-3.    //
//  Processing occurs through a single path "Cosmic"   //
//  No filtering applied outside of finding the spills //
//  Each output stream requires its own processing     //
//   branch and so there are small offshoot branches   //
//                                                     //
//  Uses: CandChop Module                              //
//        Prescaling                                   //
//                                                     //
// 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 SetCalibrator();

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(){

  LoadLibraries();  //Needs to be called first

  JobC jc;

  //Creating Path

  jc.Path.Create("Cosmic",
                 "RecordSetupModule::Get "
                 "NoiseFilterModule::Ana "			   
                 "NeardetBeamSelect::Ana "
                 "FiltTriggerPrescale::Ana "
                 "DigitListModule::Get "
                 "DigitListModule::Reco "
                 "FilterDigitListModule::Reco "
                 "ChopModule::Reco "
                 "SliceFromChopModule::Reco "
                 "TrackSRListModule::Reco "
		 "FitTrackSRListModule::Reco "
                 "ClusterSRListModule::Reco "
		 "ShowerSRListModule::Reco "                                    
		 "EventSRListModule::Reco "		 
                 "RecordSetupModule::Reco "
                 "NtpBDLite::Reco "		 
		 "Output::Put "
		 );

  // Note that the order Track/FitTrack/Cluster/Shower matters. 
 
  //Input Parameters
  jc.Input.Set("Format=input");
  jc.Input.Set("Streams=DaqSnarl");  //takes data as input

  //////////////////////////////////////////////
  // 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");
   cal.Set("TimeCalibrator=SimpleCalScheme");

  //End of Calibrator Setup
  //////////////////////////////////////////////



  jc.Path("Cosmic").SetAllFilters(1);
  ///////////////////////////////////////////////
  //  Setting Primary Filtering Configuration
  //  - Reverse the Beam Spill Filter to remove spill events   
  jc.Path("Cosmic").Node("NeardetBeamSelect::Ana").ReverseFilter();
  //  - Set Prescaling to remove half of the events based on Planes and Activity triggering
  jc.Path("Cosmic").Mod("FiltTriggerPrescale").Set("Plane_N=1    Plane_M=8");
  jc.Path("Cosmic").Mod("FiltTriggerPrescale").Set("Activity_N=1 Activity_M=8");

  jc.Path("Cosmic").Mod("FilterDigitListModule").Set("FilterDigitListAlgorithm=AlgFilterDigitList");
  jc.Path("Cosmic").Mod("FilterDigitListModule").Set("FilterDigitListAlgConfig=NearDet");
  jc.Path("Cosmic").Mod("FilterDigitListModule").Set("SwitchPersToTemp=1");


  //End of Prescaling and Beam Filtering
  ///////////////////////////////////////////////  

  ///////////////////////////////////////////////
  // Starting Configuration of the Cosmic Path //     

  //////////////////////////////////////
  //Configure Reconstruction Software for Near Det Cosmics data .
  jc.Path("Cosmic").Mod("ClusterSRListModule").Set("ClusterListAlgConfig=NearCosmic");
  jc.Path("Cosmic").Mod("ShowerSRListModule").Set("ShowerListAlgConfig=NearCosmic");
  jc.Path("Cosmic").Mod("TrackSRListModule").Set("TrackListAlgConfig=NearCosmic");
  jc.Path("Cosmic").Mod("FitTrackSRListModule").Set("FitTrackListAlgConfig=NearCosmic");
  jc.Path("Cosmic").Mod("EventSRListModule").Set("EventListAlgConfig=NearCosmic");
  //////////////////////////////////////

  //DigitListModule parameters
  jc.Path("Cosmic").Mod("DigitListModule").Set("ListsToMake=1");

  //Cosmics's filter digit list parameters
  //Don't write out original CandDigitList.

  //CandChop Package Settings
  jc.Path("Cosmic").Mod("ChopModule").Set("ChopAlgorithm=AlgChopListSharp2");


  //  End of Configuration settings for main reconstruction //
  ////////////////////////////////////////////////////////////


  ////////////////////////////////////////////////////////////////
  //Display the Configuration of modules
  jc.Path("Cosmic").Mod("ShowerSRListModule").Report();
  jc.Path("Cosmic").Mod("TrackSRListModule").Report();
  jc.Path("Cosmic").Mod("FitTrackSRListModule").Report();  
  ////////////////////////////////////////////////////////////////

  /////////////////////////////////////////////////
  //    Setting the Output Paths                 //
  //  - This script produces 3 output files      // 
  //    this will become the cand, cntp, cnts    //
  //     files for near det                      //
  /////////////////////////////////////////////////
  
  //Set Candidate Output to CandA.root (all unfiltered snarls included in output)
  //this will become the .cand. file
  SetCandOutputWithBNtp(jc.Path("Cosmic").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("Cosmic","cNtpSR");

  // Ntuple abridged record 
  jc.Path.Create("cNtpSRFilter",
                 "NtpStFilterModule::Reco "  //removes the strip info
                 "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("Cosmic", "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("Cosmic").Run();
  jc.Path("Cosmic").Report();
  jc.Msg.Stats();   //Output the message statistics to see what is generating output

}

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

//  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("libFilterDigitSR");
  gSystem->Load("libBField");
  gSystem->Load("libNumericalMethods");
  gSystem->Load("libSwimmer");
  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("libCandChop");
  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){

  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("Photon","Warning");
  jc.Msg.SetLevel("Per","Error");
  jc.Msg.SetLevel("Io", "Error");
  jc.Msg.SetLevel("Plex","Error");
  jc.Msg.SetLevel("FitTrackSR","Warning");
  jc.Msg.SetLevel("AlgFitTrackSR","Warning");
  jc.Msg.SetLevel("AlgFilterDigitList","Warning");
  jc.Msg.SetLevel("AlgShowerSS","Error");
  jc.Msg.SetLevel("SubShowerSR","Error");
//  jc.Msg.SetLevel("AlgShowerSS", "Warning");

}
