#include <string.h>
#include <string>
#include <TFile.h>
#include <TTree.h>
#include <map>
#include <fstream>
#include <TSystemDirectory.h>
#include <iostream>

using namespace std;

Int_t main(Int_t argc, const char *argv[])
{

    const char *dirname = "/mnt/g/HIT/time_aligned/20170508/";
    const char *ext = ".csv";

    TSystemDirectory dir(dirname, dirname);
    TList *files = dir.GetListOfFiles();
    if (files)
    {
	TSystemFile *file;
	TString fname;
	TIter next(files);

	while ((file = (TSystemFile *)next()))
	{
	    fname = file->GetName();

	    if (!file->IsDirectory() && fname.EndsWith(ext))
	    {

		TString outname = TString(dirname) + fname;
		outname.Replace(outname.Length() - 3, 3, TString("root"));
		//outfile->cd();

		TTree *tree = new TTree("t", "t");
		cout << fname << endl;
		//192
		tree->ReadFile(TString(TString(dirname) + fname), "time/D:event_id:sync_out:sync_in:data_ok:led_voltage:current:ch00:ch01:ch02:ch03:ch04:ch05:ch06:ch07:ch08:ch09:ch10:ch11:ch12:ch13:ch14:ch15:ch16:ch17:ch18:ch19:ch20:ch21:ch22:ch23:ch24:ch25:ch26:ch27:ch28:ch29:ch30:ch31:ch32:ch33:ch34:ch35:ch36:ch37:ch38:ch39:ch40:ch41:ch42:ch43:ch44:ch45:ch46:ch47:ch48:ch49:ch50:ch51:ch52:ch53:ch54:ch55:ch56:ch57:ch58:ch59:ch60:ch61:ch62:ch63:ch64:ch65:ch66:ch67:ch68:ch69:ch70:ch71:ch72:ch73:ch74:ch75:ch76:ch77:ch78:ch79:ch80:ch81:ch82:ch83:ch84:ch85:ch86:ch87:ch88:ch89:ch90:ch91:ch92:ch93:ch94:ch95:ch96:ch97:ch98:ch99:ch100:ch101:ch102:ch103:ch104:ch105:ch106:ch107:ch108:ch109:ch110:ch111:ch112:ch113:ch114:ch115:ch116:ch117:ch118:ch119:ch120:ch121:ch122:ch123:ch124:ch125:ch126:ch127:ch128:ch129:ch130:ch131:ch132:ch133:ch134:ch135:ch136:ch137:ch138:ch139:ch140:ch141:ch142:ch143:ch144:ch145:ch146:ch147:ch148:ch149:ch150:ch151:ch152:ch153:ch154:ch155:ch156:ch157:ch158:ch159:ch160:ch161:ch162:ch163:ch164:ch165:ch166:ch167:ch168:ch169:ch170:ch171:ch172:ch173:ch174:ch175:ch176:ch177:ch178:ch179:ch180:ch181:ch182:ch183:ch184:ch185:ch186:ch187:ch188:ch189:ch190:ch191", '\t');
		//128
		//tree->ReadFile(TString(TString(dirname) + file->GetName()), "time/D:event_id:sync_out:sync_in:data_ok:led_voltage:current:ch00:ch01:ch02:ch03:ch04:ch05:ch06:ch07:ch08:ch09:ch10:ch11:ch12:ch13:ch14:ch15:ch16:ch17:ch18:ch19:ch20:ch21:ch22:ch23:ch24:ch25:ch26:ch27:ch28:ch29:ch30:ch31:ch32:ch33:ch34:ch35:ch36:ch37:ch38:ch39:ch40:ch41:ch42:ch43:ch44:ch45:ch46:ch47:ch48:ch49:ch50:ch51:ch52:ch53:ch54:ch55:ch56:ch57:ch58:ch59:ch60:ch61:ch62:ch63:ch64:ch65:ch66:ch67:ch68:ch69:ch70:ch71:ch72:ch73:ch74:ch75:ch76:ch77:ch78:ch79:ch80:ch81:ch82:ch83:ch84:ch85:ch86:ch87:ch88:ch89:ch90:ch91:ch92:ch93:ch94:ch95:ch96:ch97:ch98:ch99:ch100:ch101:ch102:ch103:ch104:ch105:ch106:ch107:ch108:ch109:ch110:ch111:ch112:ch113:ch114:ch115:ch116:ch117:ch118:ch119:ch120:ch121:ch122:ch123:ch124:ch125:ch126:ch127", '\t');
		//64
		//tree->ReadFile(TString(TString(dirname) + file->GetName()), "time/D:event_id:sync_out:sync_in:data_ok:led_voltage:current:ch00:ch01:ch02:ch03:ch04:ch05:ch06:ch07:ch08:ch09:ch10:ch11:ch12:ch13:ch14:ch15:ch16:ch17:ch18:ch19:ch20:ch21:ch22:ch23:ch24:ch25:ch26:ch27:ch28:ch29:ch30:ch31:ch32:ch33:ch34:ch35:ch36:ch37:ch38:ch39:ch40:ch41:ch42:ch43:ch44:ch45:ch46:ch47:ch48:ch49:ch50:ch51:ch52:ch53:ch54:ch55:ch56:ch57:ch58:ch59:ch60:ch61:ch62:ch63", '\t');
		TFile *outfile = new TFile(outname.Data(), "recreate");
		outfile->cd();
		tree->Write();
		outfile->Close();
		//delete tree;
	    }
	}
    }
    return 0;
}
