#include <stdio.h>
#include <iostream.h>
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TMath.h"
#include "TString.h"

using namespace std;
     
void create_EEEtree()
{
// This macro creates a ROOT tree with the event-by-event information from a single EEE telescope
// for a data taking period of 1 day
//

//
// Open and read the configuration file
//
   ifstream config;
   config.open("C:\\root\\macros\\config_create_EEEtree.txt", ios::in);
   //Check the existence of the config file
   if(!config.is_open()){
                        cout << "Please check the config file!" << endl;
                        break;
   }
   TString tmp1, tmp2, tmp3;
   config >> tmp1; // Read the first line of the config file (telescope code)
   char *tel_code = new char[tmp1.Length() + 1];
   tel_code = tmp1.Data();
   config >> tmp2; // Read the second line of the config file (date)
   char *date = new char[tmp2.Length() + 1];
   date = tmp2.Data();
   config >> tmp3; // Read the third line of the config file (path)
   char *path = new char[tmp3.Length() + 1];
   path = tmp3.Data(); 
   cout << endl;
//
// Define output ROOT file
//
   Int_t nevt_tot=0;	
   char output[100];
   sprintf(output, "%s%s-%s.root", path, tel_code, date);
//
// Define tree structure
//	
   TFile *f = new TFile(output, "RECREATE");
   TTree *tree = new TTree("chambers", "MRPC Time");
   Double_t time,cx,cy,cz,teta,phi,chi,tof,l;
   tree->Branch("time", &time, "time/D");
   tree->Branch("teta",&teta,"teta/D");
   tree->Branch("phi",&phi,"phi/D");
   tree->Branch("chi",&chi,"chi/D");
   tree->Branch("tof",&tof,"tof/D");
   tree->Branch("l",&l,"l/D");
   
   tree->SetAutoSave(1000000000);
//
// Loop on the data files
//
   for(Int_t i = 1; i < 10000; i++){
//	
//Processing file #i
//
             char filein[200];
             sprintf(filein, "%s%s-%s-%.5d.out", path, tel_code, date, i); 
             ifstream in;
             in.open(filein, ios::in);
//
//Check the existence of the input file
//
             if(in.is_open()){
                              printf("Processing file %s-%s-%.5d.OUT\n", tel_code, date, i); 
                              Int_t nevt = 0;
                              Int_t runno, evno;
                              Double_t time_sec, time_ns, time_us; 
                              char title[182];
                              in.getline(title,182); //To skip the first line
// 
// Read event
//
                              while(in >> runno >> evno >> time_sec >> time_ns >> time_us >> cx >> cy >> cz >> chi >> tof >> l) {
                                       teta=TMath::ACos(cz)*TMath::RadToDeg();
                                       if(cx==0 & cy>0)  phi=90.0;
                                       if(cx==0 & cy<0)  phi=270.0;
                                       if(cx >0 || cx<0) phi=TMath::ATan(cy/cx)*TMath::RadToDeg();
                                       if(cx<0)          phi=phi+180.0;
                                       if(cx>0 & cy<0)   phi=360.0+phi;

                                       Double_t deltaphi=gRandom->Gaus(0.0,4.0);
                                       phi=phi+deltaphi;
                                       if(phi<0.)   phi=360+phi;
                                       if(phi>360.) phi=phi-360.0;
//
// GPS time of the event
//
                                       time = time_sec + time_ns*1.0E-9;                
                                       nevt++;
                                       nevt_tot++;
                                       tree->Fill();
                              }// end event reading
             } // end file processing
             else continue;
    in.close();
    }//end loop on data files
//
// Statistics and file closing
//
    cout << "Total number of events: " << nevt_tot << endl;
    f->cd();
    tree->Write();
    f->Close();	
    cout << "Output file in " << path << endl;
}  
