#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
#include "TProfile.h"
#include "TRandom.h"
#include "TTree.h"
#include <stdio.h>
#include <stdlib.h>

int plot_HV_gap(int start, int finish, int tdciwant)
{

    // Setting plots range and style
    gStyle->SetOptStat(0.1);
    gStyle->SetMarkerSize(1);
    gStyle->SetMarkerStyle(20);
    gStyle->SetMarkerColor(0);

    // Setting plots range and style
    int i, unixtime_bin = 100;
    int temp_bin = 200, temp_min = 15, temp_max = 26;
    int p_bin = 200, p_min = 920, p_max = 980;
    int rh_bin = 200, rh_min = -5, rh_max = 65;
    int histo_bin = 200, histo_min = 0, histo_max = 10000;
    int volt_bin = 200, volt_min = 0, volt_max = 10000;
    int current_bin = 200, current_min = 0, current_max = 5;
    char buffer [4], buffer2[50],buffer3[50],buffer4[4];

    // Text positioning
    Double_t x1=0.9,y1=0.6,x2=x1+0.09,y2=0.63;
    float label_font=0.025;
    float axis_offset=0.9;
    float time_bin=5;
    float label_size=0.03;

    // Defining variable already present in the dst file
    Char_t      Psupply[80];
    Char_t      Names[80];
    Int_t       unixtime;
    Int_t       tdct;
    Int_t       Slott;
    Int_t       Channelt;
    Float_t     Volt;
    Float_t     Current;
    Int_t       status;
    Float_t     env_t;
    Float_t     env_rh;
    Float_t     env_p;
    char    str[80], str2[80], name[80];

    int i = 0, tdc, slot, channel;

    TFile *hfile = 0;
    hfile = TFile::Open("904lab.root","RECREATE");


    // Opening the configuration file
    FILE *fp = fopen("/srv/www/htdocs/conf_gap.txt","r");

    // Reading header
    fscanf(fp,"%s\n",name);
    fscanf(fp,"%s\n",name);

    while (!feof(fp))
    {
        fscanf(fp,"%s %d %d %d\n",name, &slot, &channel, &tdc);

        if (tdc == tdciwant) {
            printf("%s %d\n","TDC :",tdciwant);
            strcpy (str,"/home/user/re4data/");
            strcat (str,name);
            strcat (str,"_gap.dat");
            FILE *fp2 = fopen(str,"r");
            i = i + 1;

            // Tree and Branch definitions
            TTree *rpclab = new TTree("904lab","904 RPC Lab - PLOTS");
            rpclab->Branch("Psupply",Psupply,"Psupply/C");
            rpclab->Branch("unixtime",&unixtime,"unixtime/I");
            rpclab->Branch("Names",Names,"Names/C");
            rpclab->Branch("Slott",&Slott,"Slott/I");
            rpclab->Branch("Channelt",&Channelt,"Channelt/I");
            rpclab->Branch("Volt",&Volt,"Volt/F");
            rpclab->Branch("Current",&Current,"Current/F");
            rpclab->Branch("status",&status,"status/I");
            rpclab->Branch("tdtc",&tdct,"tdct/I");
            rpclab->Branch("env_t",&env_t,"env_t/F");
            rpclab->Branch("env_rh",&env_rh,"env_rh/F");
            rpclab->Branch("env_p",&env_p,"env_p/F");

            while (!feof(fp2))
            {
                fscanf(fp2,"%s %d %s %d %d %f %f",Psupply, &unixtime, Names, &Slott, &Channelt, &Volt, &Current);
                fscanf(fp2,"%d %f %f %f %f\n",&status, &tdct, &env_t, &env_rh, &env_p);
                //printf("%d\n","unixtime");
                rpclab->Fill();
            }
            fclose(fp2);
            rpclab->Write();



            // PERFORMANCES
            TCanvas *Frameperf = new TCanvas("Frameperf","Current and Voltages",0,0,1500,900);//creating Canvas
            TPad *pad1 = new TPad("pad1","",0,0,1,1); //1st pad for T
            TPad *pad2 = new TPad("pad2","",0,0,1,1); //2nd pad for P
            pad2->SetFillStyle(4000); //will be transparent
            pad1->Draw();
            pad1->cd();

            TH2F *sens1 = new  TH2F("sens1","sens1",unixtime_bin,start,finish,current_bin,current_min,current_max);
            sens1->SetMarkerColor(1);
            sens1->SetMarkerStyle(20);
            sens1->SetMarkerSize(0.7);
            if (i==1) {
            	sens1->SetTitle("904lab System performances GAP");
            }
            if (i==2) {
            	sens1->SetTitle("904lab System performances TOP NARROW GAP");
            }
            if (i==3) {
            	sens1->SetTitle("904lab System performances BOTTOM GAP");
            }

            sens1->GetXaxis()->SetTitleOffset(axis_offset);
            sens1->GetXaxis()->SetTimeDisplay(1);
            sens1->GetXaxis()->SetTimeFormat("%Y %m %d %H:%M %F 1970-01-01 00:00:00");
            sens1->GetXaxis()->SetNdivisions(time_bin);
            sens1->GetXaxis()->SetLabelSize(label_size);
            sens1->GetXaxis()->SetTitle("time");
            sens1->GetYaxis()->SetTitleOffset(axis_offset);
            sens1->GetYaxis()->SetLabelSize(label_size);
            sens1->GetYaxis()->SetTitle("Current [uA]");
            sens1->GetYaxis()->SetTitleSize(0.03);
            gPad->SetGrid();

            TH2F *environ3 = new TH2F("environ3","environ3",unixtime_bin,start,finish,volt_bin,volt_min,volt_max);
            environ3->SetMarkerColor(2);
            environ3->SetMarkerStyle(20);
            environ3->SetMarkerSize(0.7);

            rpclab->Draw("Current:unixtime>>sens1","");
            sens1->Draw("");

            pad1->Update();
            pad1->Modified();
            Frameperf->cd();

            //compute the pad range with suitable margins
            Double_t ymin = volt_min;
            Double_t ymax = volt_max;
            Double_t dy = (ymax-ymin)/0.8; //% margins top and bottom
            Double_t xmin = start;
            Double_t xmax = finish;
            Double_t dx = (xmax-xmin)/0.8; //% margins left and right
            pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);
            pad2->Draw();
            pad2->cd();

            rpclab->Draw("Volt:unixtime>>environ3","","same");
            environ3->Draw("][sames");
            pad2->Update();

            // draw axis on the right side of the pad
            TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
            axis->SetLabelColor(2);
            axis->SetLineColor(2);
            axis->SetTitleOffset(axis_offset);
            axis->SetLabelSize(label_size);
            axis->SetLabelColor(2);
            axis->SetTitle("Volt [V]");
            axis->SetTitleSize(0.03);
            axis->SetTitleColor(2);
            axis->Draw();

            x1=0.9,y1=0.17,x2=x1+0.095,y2=y1+0.035;
            TLegend *leg1=new TLegend(x1,y1,x2,y2);
            leg1->AddEntry(sens1,"Current [uA]","p");
            leg1->SetTextColor(1);
            leg1->SetFillColor(0);
            leg1->SetTextSize(0.020);
            leg1->Draw();
            y2=y2-0.04;
            y1=y1-0.04;

            TLegend *leg2=new TLegend(x1,y1,x2,y2);
            leg2->AddEntry(sens1,"Volt [V]","p");
            leg2->SetTextColor(2);
            leg2->SetFillColor(0);
            leg2->SetTextSize(0.020);
            leg2->Draw();

            TImage *img = TImage::Create();
            img->FromPad(Frameperf);
            strcpy (str2,"/home/user/temp/gaptemp");

            //sprintf(buffer, "%i", tdciwant);
            //strcat (str2,buffer);
            //strcat (str2,"_gap_");

            sprintf(buffer, "%i", i);
            strcat (str2,buffer);
            strcat (str2,"_");

            sprintf(buffer2, "%d", start);
            strcat (str2,buffer2);
            strcat (str2,"_");

            sprintf(buffer3, "%i", finish);
            strcat (str2,buffer3);
            strcat (str2,"_");
            sprintf(buffer4, "%i", tdciwant);
            strcat (str2,buffer4);
            strcat (str2,".png");
            img->WriteImage(str2);

        }
    }
    fclose(fp);
}
