//************************************************
// Author: Federica Lionetto
// Created on: 07/12/2015
//************************************************

/*

   PlotCooling creates a ROOT file with the trends of the temperature measured by the theromostat sensor and the temperature measured by the Sensirion sensor (close to the silicon sensor).

   Compile with:

   make

   Run with:

   ./PlotCooling [sensor] [from] [to] [additional folder]

   where

   - [sensor] is the type of sensor (Hans410, ...);
   - [from] is the optional starting time from which one wants to display the trends, according to the "ddd mmm  d hh:mm:ss yyyy" format;
   - [to] is the optional stopping time to which one wants to display the trends, according to the "ddd mmm  d hh:mm:ss yyyy" format;
   - [additional folder] is the optional additional folder where the output will be saved.

   For example:

   ./PlotCooling Hans320

   or 

   ./PlotCooling Hans320 "Thu Apr  9 16:55:00 2015" "Thu Apr  9 16:55:52 2015"

   A folder named AnalysisResults will be created in a fixed location and a ROOT file will be saved in there. A folder named Figures will be created inside the folder named AnalysisResults, with some monitoring plots.

   If needed, an additional folder can be specified, that will be created inside the folder named AnalysisResults.
   */

#include "../Tools/Lib.C"
#include "../Tools/UsefulFunctions.C"
#include "../Tools/lhcbStyle.C"
#include "../Tools/Style.C"

#include "../Tools/Par.C"

void PlotCooling(char *sensor, char *from=0, char *to=0, char *externalPath=0);

vector<string> SelectFiles(string inputDirectory, vector<string> list_of_files, time_t t_from, time_t t_to);



int main(int argc, char *argv[])
{
    getLHCbStyle();
    PersonalStyle();

    if ((argc ==2) && (string(argv[1]) == "--info"))
    {
        cout << "**************************************************" << endl;

        cout << "PlotCooling creates a ROOT file with the trends of the temperature measured by the theromostat sensor and the temperature meas    ured by the Sensirion sensor (close to the silicon sensor)." << endl;

        cout << "Compile with:" << endl;

        cout << "make" << endl;

        cout << "Run with:" << endl;

        cout << "./PlotCooling [sensor] [from] [to] [additional folder]" << endl;

        cout << "where" << endl;

        cout << "- [sensor] is the type of sensor (Hans410, ...);" << endl;
        cout << "- [from] is the optional starting time from which one wants to display the trends, according to the 'ddd mmm  d hh:mm:ss yyyy' format;" << endl;
        cout << "- [to] is the optional stopping time to which one wants to display the trends, according to the 'ddd mmm  d hh:mm:ss yyyy' format;" << endl;
        cout << "- [additional folder] is the optional additional folder where the output will be saved." << endl;

        cout << "For example:" << endl;

        cout << "./PlotCooling Hans320" << endl;

        cout << "or" << endl; 

        cout << "./PlotCooling Hans320 'Thu Apr  9 16:55:00 2015' 'Thu Apr  9 16:55:52 2015'" << endl;

        cout << "A folder named AnalysisResults will be created in a fixed location and a ROOT file will be saved in there. A folder named Figures will be created inside the folder named AnalysisResults, with some monitoring plots." << endl;

        cout << "If needed, an additional folder can be specified, that will be created inside the folder named AnalysisResults." << endl;

        cout << "**************************************************" << endl;

        return 0;
    }
    else if (argc < 2)
    {
        cout << "**************************************************" << endl;

        cout << "Error! Input information missing..." << endl;
        cout << "Please use the following format:" << endl;
        cout << "./PlotCooling [1] [2] [3] [4]" << endl;
        cout << "with:" << endl;
        cout << "[1] = Type of sensor (Hans410, ...);" << endl;
        cout << "[2] = Starting time, optional;" << endl;
        cout << "[3] = Stopping time, optional;" << endl;
        cout << "[4] = Additional folder, optional." << endl;
        cout << "Type ./PlotCooling --info for more information." << endl;

        cout << "**************************************************" << endl;

        return 0;  
    }
    else
    {
        cout << "Type of sensor: " << argv[1] << endl;
        if (argc == 2)
            PlotCooling(argv[1]);
        else if (argc == 3)
        {
            cout << "**************************************************" << endl;

            cout << "Error! Stopping time not specified..." << endl;

            cout << "**************************************************" << endl;

            return 0;
        }
        else if (argc == 4)
        {
            cout << "Starting time: " << argv[2] << endl;
            cout << "Stopping time: " << argv[3] << endl;
            PlotCooling(argv[1],argv[2],argv[3]);
        }
        else if (argc == 5)
        {
            cout << "Staring time: " << argv[2] << endl;
            cout << "Stopping time: " << argv[3] << endl;
            cout << "Additional folder: " << argv[4] << endl;
            PlotCooling(argv[1],argv[2],argv[3],argv[4]);
        }
        else
        {
            cout << "Error! Too many arguments given..." << endl;

            return 0;
        }

        return 0;  
    }  
}

void PlotCooling(char *sensor, char *from, char *to, char *externalPath)
{
    cout << "**************************************************" << endl;
    cout << "Plotting trends..." << endl;
    cout << "**************************************************" << endl;

    // Do not comment this line.
    gROOT->ProcessLine("#include <vector>");

    string funName = "PlotCooling";

    // Adjust time format for output ROOT file and figures.
    string s_from = "";
    string s_to = "";

    struct tm tm_from;
    time_t t_from;

    struct tm tm_to;
    time_t t_to;

    if ((from != 0) && (to != 0))
    {
        s_from = string(from);
        s_to = string(to);

        // Remove white spaces from s_from and s_to.
        s_from.erase(remove(s_from.begin(), s_from.end(), ' '), s_from.end());
        s_to.erase(remove(s_to.begin(), s_to.end(), ' '), s_to.end());

        // Convert the string with date and time to something that is interpreted as date and time. tm_from is a tm object, t_from is a time_t object and corresponds to the number of seconds since epoch. The format specifier %c stands for date and time representation.

        memset(&tm_from, 0, sizeof(struct tm));
        strptime(from,"%c",&tm_from);
        t_from = mktime(&tm_from);

        /*
           cout << tm_from.tm_year << endl;
           cout << t_from << endl;
           */

        memset(&tm_to, 0, sizeof(struct tm));
        strptime(to,"%c",&tm_to);
        t_to = mktime(&tm_to);

        /*
           cout << tm_to.tm_year << endl;
           cout << t_to << endl;
           */

        // Check that the starting time is earlier than the stopping time.
        if (t_from >= t_to)
        {
            cout << "Error! Starting time earlier than stopping time..." << endl;
            return;
        }
    }

    // Do some fanciness to get the directory right.
    string inputDirectory = "/disk/groups/hep/flionett/TestStand/Data/"+string(sensor)+"/Cooling";
    string outputDirectory = "/disk/groups/hep/flionett/TestStand/AnalysisResults/"+string(sensor)+"/Cooling";
    if (externalPath!=0)
        outputDirectory = string(outputDirectory+"/"+externalPath);
    cout << "The input directory is: " << inputDirectory << endl;
    cout << "The output directory is: " << outputDirectory << endl;

    // Create the outputDirectory directory if it does not exist.
    string path_to_make = "mkdir -p "+outputDirectory;
    system(path_to_make.c_str());

    cout << "Figures stored in: " << outputDirectory+"/Figures" << endl;

    // Create a directory named Figures inside the directory named outputDirectory if it does not exist.
    string path_to_make_figures = "mkdir -p "+outputDirectory+"/Figures";
    if ((from != 0) && (to != 0))
        path_to_make_figures = path_to_make_figures+"/"+funName+"-from"+s_from+"to"+s_to;
    else
        path_to_make_figures = path_to_make_figures+"/"+funName+"-all";
    system(path_to_make_figures.c_str());

    TString path_to_figures = (string(path_to_make_figures)).substr((string(path_to_make_figures)).find_last_of(' ')+1);



    TDatime *time;
    vector<double> vtime;

    double TSi;
    vector<double> vTSi;

    double TTh;
    vector<double> vTTh;

    string trash1;
    string trash2;
    string trash3;
    string trash4;
    string trash5;

    // Given starting and stopping time, find the corresponding input text files.
    vector<string> list_of_files;
    vector<string> list_of_selected_files;

    list_of_files = open(inputDirectory);
    if ((from != 0) && (to != 0))
        list_of_selected_files = SelectFiles(inputDirectory, list_of_files, t_from, t_to);
    else
        list_of_selected_files = list_of_files;

    if (list_of_selected_files.size() == 0) {
        cout << "Error! No input text file selected..." << endl;
        return;
    }

    ifstream file;
    string header;
    string line;
    struct tm tm_time_in_line;
    time_t t_time_in_line;

    // Read data from selected input text files.
    for (vector<string>::const_iterator i = list_of_selected_files.begin(); i != list_of_selected_files.end(); ++i)
    {
        // Open file.
        cout << "Selected input text file #" << i-list_of_selected_files.begin() << ": " << *i << endl;
        file.open((inputDirectory+"/"+*i).c_str());
        if (file.is_open())
        {
            // cout << "Great! File opened..." << endl;

            // Skip header.
            if (getline(file,header)) 
            {
                // Read time in each line.
                while (getline(file,line))
                {
                    memset(&tm_time_in_line, 0, sizeof(struct tm));
                    strptime(line.c_str(),"%c",&tm_time_in_line);
                    t_time_in_line = mktime(&tm_time_in_line);

                    if ((from != 0) && (to != 0))
                    {	  
                        if ((t_time_in_line <= t_to) && (t_time_in_line >= t_from))
                        {
                            // cout << "Time in line: " << t_time_in_line << " from epoch time" << endl;
                            time = new TDatime(tm_time_in_line.tm_year,tm_time_in_line.tm_mon+1,tm_time_in_line.tm_mday,tm_time_in_line.tm_hour+1,tm_time_in_line.tm_min,tm_time_in_line.tm_sec);
                            // cout << "Time going in the ROOT file: " << time->Convert() << endl;
                            // Get time, temperature, humidity, and dew point.
                            istringstream iss(line);
                            iss >> trash1 >> trash2 >> trash3 >> trash4 >> trash5 >> TSi >> TTh;
                            vtime.push_back(time->Convert());
                            vTSi.push_back(TSi);
                            vTTh.push_back(TTh);
                        }
                    }
                    else 
                    {
                        // cout << "Time in line: " << t_time_in_line << " from epoch time" << endl;
                        time = new TDatime(tm_time_in_line.tm_year,tm_time_in_line.tm_mon+1,tm_time_in_line.tm_mday,tm_time_in_line.tm_hour+1,tm_time_in_line.tm_min,tm_time_in_line.tm_sec);
                        // cout << "Time going in the ROOT file: " << time->Convert() << endl;
                        // Get time, temperature, humidity, and dew point.
                        istringstream iss(line);
                        iss >> trash1 >> trash2 >> trash3 >> trash4 >> trash5 >> TSi >> TTh; 
                        vtime.push_back(time->Convert());
                        vTSi.push_back(TSi);
                        vTTh.push_back(TTh);
                    }
                }
            }
            else
            {
                cout << "Error! Unable to read header..." << endl;
                // return;
            }

            // Close file.
            file.close();
        }
        else {
            cout << "Error! Unable to open file." << endl; // To be modified to stop everything if there is a problem.
            // return;
        }
    }

    double *aTSi = &vTSi[0];
    double *aTTh = &vTTh[0];
    double *atime = &vtime[0];

    /*
       cout << vT.size() << endl;
       cout << vtime.size() << endl;

       cout << vT[0] << endl;
       cout << vtime[0] << endl;

       cout << vT[10] << endl;
       cout << vtime[10] << endl;
       */

    // Open output ROOT file.
    string output_ROOT;
    if ((from != 0) && (to !=0))
        output_ROOT = outputDirectory+"/"+funName+"-from"+s_from+"to"+s_to+".root";
    else
        output_ROOT = outputDirectory+"/"+funName+"-all.root";
    TFile *output = TFile::Open(TString(output_ROOT),"RECREATE");

    // Temperature trends.
    TCanvas *cTSi = new TCanvas(TString(funName)+"-TSi","",400,300);

    cTSi->SetLeftMargin(0.1363636);
    cTSi->SetRightMargin(0.08808081);
    cTSi->SetBottomMargin(0.2095588);

    TGraph *gTSi = new TGraph(vTSi.size(),atime,aTSi);
    InitGraph(gTSi,"Temperature trends","Time","Temperature (#circC)");
    gTSi->GetXaxis()->SetTimeDisplay(1);
    gTSi->GetXaxis()->SetLabelOffset(0.04);
    gTSi->GetXaxis()->SetTitleOffset(1.50);
    gTSi->GetYaxis()->SetTitleOffset(0.95);
    gTSi->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M:%S}");
    gTSi->GetXaxis()->SetNdivisions(-503);
    gTSi->GetXaxis()->SetTimeOffset(0,"gmt");
    DrawGraph(cTSi,gTSi,"AP",path_to_figures);

    TCanvas *cTTh = new TCanvas(TString(funName)+"-TTh","",400,300);

    cTTh->SetLeftMargin(0.1363636);
    cTTh->SetRightMargin(0.08808081);
    cTTh->SetBottomMargin(0.2095588);

    TGraph *gTTh = new TGraph(vTTh.size(),atime,aTTh);
    InitGraph(gTTh,"Temperature trends","Time","Temperature (#circC)");
    gTTh->GetXaxis()->SetTimeDisplay(1);
    gTTh->GetXaxis()->SetLabelOffset(0.04);
    gTTh->GetXaxis()->SetTitleOffset(1.50);
    gTTh->GetYaxis()->SetTitleOffset(0.95);
    gTTh->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M:%S}");
    gTTh->GetXaxis()->SetNdivisions(-503);
    gTTh->GetXaxis()->SetTimeOffset(0,"gmt");
    DrawGraph(cTTh,gTTh,"AP",path_to_figures);
    
    // Combined trends.
    TCanvas *cTSiAndTTh = new TCanvas(TString(funName)+"-TSiAndTTh","",400,300);

    cTSiAndTTh->SetLeftMargin(0.1363636);
    cTSiAndTTh->SetRightMargin(0.08808081);
    cTSiAndTTh->SetBottomMargin(0.2095588);

    TMultiGraph *mgTSiAndTTh = new TMultiGraph();

    gTSi->SetMarkerColor(kGreen);
    gTSi->SetLineColor(kGreen);
    gTTh->SetMarkerColor(kRed);
    gTTh->SetLineColor(kRed);

    TLegend *legTSiAndTTh = CreateLegend2(gTSi,"Si",gTTh,"thermostat","lpw",0.17,0.24,0.42,0.49);

    mgTSiAndTTh->Add(gTSi);
    mgTSiAndTTh->Add(gTTh);

    cTSiAndTTh->cd();
    mgTSiAndTTh->Draw("AP");

    mgTSiAndTTh->GetXaxis()->SetTimeDisplay(1);
    mgTSiAndTTh->GetXaxis()->SetLabelOffset(0.04);
    mgTSiAndTTh->GetXaxis()->SetTitleOffset(1.50);
    mgTSiAndTTh->GetYaxis()->SetTitleOffset(0.95);
    mgTSiAndTTh->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M:%S}");
    mgTSiAndTTh->GetXaxis()->SetNdivisions(-503);
    mgTSiAndTTh->GetXaxis()->SetTimeOffset(0,"gmt");

    DrawGraphCompare(cTSiAndTTh,mgTSiAndTTh,legTSiAndTTh,"Temperature trends","Time","Temperature (#circC)","AP",path_to_figures);

    TCanvas *cTSiVSTTh = new TCanvas(TString(funName)+"-TSiVSTTh","",400,300);

    cTSiVSTTh->SetLeftMargin(0.1363636);
    cTSiVSTTh->SetRightMargin(0.08808081);
    cTSiVSTTh->SetBottomMargin(0.2095588);

    TGraph *gTSiVSTTh = new TGraph(vTSi.size(),aTSi,aTTh);
    InitGraph(gTSiVSTTh,"Temperature trends","Temperature (#circC)","Temperature (#circC)");
    gTSiVSTTh->GetXaxis()->SetLabelOffset(0.04);
    gTSiVSTTh->GetXaxis()->SetTitleOffset(1.50);
    gTSiVSTTh->GetYaxis()->SetTitleOffset(0.95);
    gTSiVSTTh->GetXaxis()->SetNdivisions(-503);
    DrawGraph(cTSiVSTTh,gTSiVSTTh,"AP",path_to_figures);

    // Write output ROOT file.
    output->Write();

    // Close output ROOT file.
    output->Close();

    return;
}



vector<string> SelectFiles(string inputDirectory, vector<string> list_of_files, time_t t_from, time_t t_to) {
    ifstream file;

    string header;
    string first_line;
    string last_line;

    struct tm tm_time_in_first_line;
    struct tm tm_time_in_last_line;

    time_t t_time_in_first_line;
    time_t t_time_in_last_line;

    vector<string> list_of_selected_files;

    for (vector<string>::const_iterator i = list_of_files.begin(); i != list_of_files.end(); ++i)
    {
        cout << "Input text file #" << i-list_of_files.begin() << ": " << *i << endl;

        // Check whether this input text file must be selected.
        file.open((inputDirectory+"/"+*i).c_str());
        if (file.is_open())
        {
            // cout << "Great! File opened..." << endl;

            // Skip header.
            if (getline(file,header)) 
            {
                // Read time in first line.
                if (getline(file,first_line))
                {
                    memset(&tm_time_in_first_line, 0, sizeof(struct tm));
                    strptime(first_line.c_str(),"%c",&tm_time_in_first_line);
                    t_time_in_first_line = mktime(&tm_time_in_first_line);

                    // cout << "Time in first line: " << t_time_in_first_line << " from epoch time" << endl;
                }
                else
                {
                    cout << "Error! Cannot read time in first line..." << endl;
                    return vector<string>();
                }
                // Read time in last line.
                last_line = getLastLine(file);
                memset(&tm_time_in_last_line, 0, sizeof(struct tm));
                strptime(last_line.c_str(),"%c",&tm_time_in_last_line);
                t_time_in_last_line = mktime(&tm_time_in_last_line);

                // cout << "Time in last line: " << t_time_in_last_line << " from epoch time" << endl;   

                // Decide if this input text file must be selected.

                if ((t_time_in_first_line <= t_to) && (t_time_in_last_line >= t_from))
                {
                    // Select the input text file.
                    list_of_selected_files.push_back(*i);
                }

            }
            else
            {
                cout << "Error! Unable to read header..." << endl;
                return vector<string>();
            }

            file.close();
        }
        else {
            cout << "Error! Unable to open file." << endl; // To be modified to stop everything if there is a problem.
            return vector<string>();
        }
    }

    // cout << "Selected input text files" << endl;
    for (vector<string>::const_iterator i = list_of_selected_files.begin(); i != list_of_selected_files.end(); ++i)
    {
        // cout << "Selected input text file #" << i-list_of_selected_files.begin() << ": " << *i << endl;
    }

    return list_of_selected_files;
}
