{

#include "TF1.h"
#include "TString.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TStyle.h"
#include "TLegend.h" 
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include "TROOT.h"
#include "TApplication.h"
#include <TLorentzVector.h>        
#include <string>
  
  using namespace std;

  int nGap = 12 ; 

  int n =  0;
  double xpavemax = 0;
  double xpavemin = 2.0e+09;
  double ypavemax = 0;
  double ypavemin = 5.0;

  string str;

  string monthStart;
  string dayStart;
  string yearStart;
  string monthEnd;
  string dayEnd;
  string yearEnd;


  Float_t CurrentPl;
  int HVPl;
  int StatusPl;
  string line ;
  string month;
  string day ;
  string year;
  string hour ;
  string min;
  string sec;
  string hv ;
  string current ;
  string status ;
  string stStatus ;

  size_t found;
  size_t nchar = 2;


  c_date = new TCanvas("c_date", "c_date"); 
  multic = new TMultiGraph(); 
  multiv = new TMultiGraph(); 

  leg = new TLegend(0.91,0.5,0.99,0.99);
  leg->SetHeader(" ");
  cout << " " <<endl;
  cout << " " <<endl;
  cout << " " <<endl;  
  cout << "To plot a user-selected time interval current timeline please insert time range. "<< endl;
  cout << "To plot a whole month timeline 0  and press enter"<< endl;
  cout << " " <<endl;
  cout << " " <<endl;
  cout << " " <<endl;
  cout << "Enter start time yy/mm/dd : ";
  cin  >> str;
  cout << "\nStart time is : " << str << endl;
  cout << " " <<endl;
  found=str.find_first_of("/");
  if (found!=string::npos)
    {
      yearStart = str.substr (found-2,nchar);
      monthStart = str.substr (found+1,nchar);
      dayStart = str.substr (found+4,nchar);
      int iyearStart = atoi(yearStart.c_str()) + 2000;
      int imonthStart = atoi(monthStart.c_str());
      int idayStart = atoi(dayStart.c_str());
      
      TDatime time(iyearStart,imonthStart,idayStart,00,00,00); 
      double userTimeStart[0] = time.Convert(); 
    }
  else	{
    double userTimeStart[0] = 0.1e+09; 
  }
  cout << "Enter end time yy/mm/dd : ";
  cin  >> str;
  cout << "\nEnd time is : " << str << endl;
  cout << " " <<endl;

  found=str.find_first_of("/");
  if (found!=string::npos)
    {
      yearEnd = str.substr (found-2,nchar);
      monthEnd = str.substr (found+1,nchar);
      dayEnd = str.substr (found+4,nchar);
      int iyearEnd = atoi(yearEnd.c_str()) + 2000;
      int imonthEnd = atoi(monthEnd.c_str());
      int idayEnd = atoi(dayEnd.c_str());
      
      TDatime time(iyearEnd,imonthEnd,idayEnd,00,00,00); 
      double userTimeEnd[0] = time.Convert(); 
    }
  else {
    double userTimeEnd[0] = 2.0e+09; 
  }
  //ifstream currentfile ("del.dat");
  ifstream currentfile ("current_monitor.dat");
  if (currentfile.is_open())
    {
      int i = 0;
      n = 0;
      while (! currentfile.eof() )
	{
	  
	  getline (currentfile,line);
	  int pos = line.length();
	  
	  found=line.find_first_of("/");
	  if (found!=string::npos){
	    month = line.substr (found-2,nchar);
	    day = line.substr (found+1,nchar);
	    year = line.substr (found+4,nchar);
	  }
	  found=line.find_first_of(":");
	  if (found!=string::npos){
	    hour = line.substr (found-2,nchar);
	    min = line.substr (found+1,nchar);
	    sec = line.substr (found+4,nchar);
	  }
	  int iyear = atoi(year.c_str()) + 2000;
	  int imonth = atoi(month.c_str());
	  int iday = atoi(day.c_str());
	  int ihour = atoi(hour.c_str());
	  int imin = atoi(min.c_str());
	  int isec = atoi(sec.c_str());
	  found=line.find_first_of(".");
	  if (found!=string::npos) {
	    stStatus = line.at(pos-2);
	    int StatusPl = atoi(stStatus.c_str());
	    if (StatusPl >= 1) {
	      hv = line.substr (0,found);
	      current = line.substr (found+4,7);
	      CurrentPl= atof(current.c_str()); 
	      HVPl = atoi(hv.c_str()); 

	      TDatime time(iyear,imonth,iday,ihour,imin,isec); 
	      
	      double x[0] = time.Convert(); 
	      double yy[0] = CurrentPl;
	      if (userTimeStart[0] < x[0] && x[0] < userTimeEnd[0]) {

		//		printf("%d %d %d \n", userTimeStart, x[0], userTimeEnd);
		/*
		  if (xpavemax < x[0]) xpavemax = x[0]; // Get coordinates for T
		  if (xpavemin > x[0]) xpavemin = x[0];
		  if (ypavemax < yy[0]) ypavemax = yy[0]; // Get coordinates for T
		  if (ypavemin > yy[0]) ypavemin = yy[0];
		*/

		TGraph *gr = new TGraph(1,x,yy);	      
		int k = n + 1;
		if (n == 9) k = 28;
		if (n == 4) k = 1;
		if (n == 10) k = 2;
		if (n == 4) k = 1;
		gr->SetMarkerColor(k);
		gr->SetMarkerSize(0.9);
		gr->SetMarkerStyle(n+20);
		
		multic->Add(gr);      
	      }
	      n++;
	      
	      if (n > 10) n = 0;
	    }
	  }
	  i++;
	}
      
      for (int i=0; i<12; i++){
	TGraph *gr = new TGraph(1,x,yy);	    
	gr->SetMarkerSize(0.9);
	int k = i + 1;
	if (i == 9) k = 28;
	if (i == 4) k = 1;
	if (i == 10) k = 2;
	if (i == 4) k = 1;
	gr->SetMarkerStyle(i+20);
	gr->SetMarkerColor(k);
	Char_t channel[5] = i;
	sprintf(channel,"Ch %i",i);
	leg->AddEntry(gr,channel,"p");
      }
      multic->Draw("ap");
      multic->GetXaxis()->SetTimeDisplay(1); 
      multic->GetXaxis()->SetNdivisions(-503); 
      multic->GetXaxis()->SetTimeFormat("%d\/%m\/%y%F2000-02-28 13:00:01"); 
      multic->GetXaxis()->SetTimeOffset(0,"gmt"); 
      multic->GetXaxis()->SetLabelSize(0.03);
      leg->Draw();
      
      currentfile.close();
      
    }
  
  else cout << "Unable to open current_monitor.dat file \n"; 

  return 0;

}

