//  
//  STDatabase.h
//  information about the ST build up
//
//  Created by Christian Elsasser on 01.05.12.
//  University of Zurich, elsasser@cern.ch
//  Copyright only for commercial use
//

//  General include
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include <math.h>
#include <map>
#include <sstream>


#include "TROOT.h"

namespace STDatabase {
  
  // Returns a string vector with information on the sensor
  std::vector<std::string> getSensorInfo(std::string lay, std::string sector){
    std::vector<std::string> v_ret;
    
    //const char* homeVar = std::getenv ("HOME");
    const char* homeVar = "/afs/cern.ch/user/e/egraveri/cmtuser/STMonitoring/STAging";
    
    std::string dn_db(homeVar);
    dn_db+"/data/db";
    std::string fn_db = dn_db;
    
    if (lay.find("TT")!=std::string::npos) {
      Info("getSensorInfo","Read TT database");
      fn_db+"/TT_db.txt";
    }else{
      Info("getSensorInfo","Read IT database");
      fn_db+"/IT_db.txt";
    }
    
    std::ifstream f_db(fn_db.c_str());
    bool found = false;
    while (f_db) {
      std::string line;
      if (!getline(f_db,line)) {
        break;
      }
      std::istringstream s_line(line);
      std::vector<std::string> v_line;
      while (s_line) {
        std::string bit;
        if (!getline(s_line,bit,' ')) {
          break;
        }
        v_line.push_back(bit);
      }
      if(v_line.size()>0 && v_line[0]==sector){
        v_ret = v_line;
        break;
      }
    }
    if (v_ret.size()==0){
      Warning("getSensorInfo","Empty vector as output!");
    }
    return v_ret;
  }
  
  
  static std::map<int,double> runTime(){
    std::map<int,double> runMap;
    runMap[1020]      =   1270339200.0; // 2010-04-04
    runMap[1616]      =   1300060800.0; // 2011-03-14
    runMap[1944]      =   1310601600.0; // 2011-07-14
    runMap[2083]      =   1315353600.0; // 2011-09-07
    runMap[2252]      =   1319500800.0; // 2011-10-25
    runMap[2472]      =   1333584000.0; // 2012-04-05
    runMap[2797]      =   1341187200.0; // 2012-07-02
    runMap[3108]      =   1348790400.0; // 2012-09-28
    runMap[3478]      =   1358726400.0; // 2013-01-21
    runMap[3820]      =   1433289600.0; // 2015-06-03
    runMap[3960]      =   1436054400.0; // 2015-07-05
    runMap[4518]      =   1445299200.0; // 2015-10-20
    runMap[4638]      =   1447977600.0; // 2015-11-20
    runMap[4643]      =   1448150400.0; // 2015-11-22
    return runMap;
  }
  std::map<int,double> runTimeMap = runTime();
  
  
  
    
  
  
  
  
};