 #include "TFile.h"
 #include "TH1.h"
 #include "TH2.h"
 #include "TProfile.h"
 #include "TRandom.h"
 #include "TTree.h"

int plot_ENVqc4(int start, int finish)
{
    // 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 = 10, rh_max = 70;
    int histo_bin = 200, histo_min = 0, histo_max = 10000;
    int volt_bin = 200, volt_min = 0, volt_max = 10;
    int current_bin = 200, current_min = 0, current_max = 1;
    
    // 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
    Int_t       unixtime;
    Float_t     env_t;
    Float_t     env_rh;
    Float_t     env_p;

    // Opening the DSTiles
    FILE *fp = fopen("/home/user/re4data/meteofullqc4.dat","r");
    TFile *hfile = 0;
    hfile = TFile::Open("/home/user/904lab_env.root","RECREATE");
    
    // Tree and Branch definitions
    TTree *rpclab = new TTree("904lab","904 RPC Lab PLOTS");    
    rpclab->Branch("unixtime",&unixtime,"unixtime/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");

    // Scanning the dst file and filling the Tree
    while (!feof(fp))
    {
        fscanf(fp,"%d %f %f %f\n",&unixtime, &env_t, &env_rh, &env_p);
        rpclab->Fill();
    }
    fclose(fp);
    rpclab->Write();


    
    //Environmental Temperature and Pressure
    TCanvas *FramexTandP = new TCanvas("FramexTandP","T & P",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,temp_bin,temp_min,temp_max);  
    sens1->SetMarkerColor(1);
    sens1->SetMarkerStyle(20);
    sens1->SetMarkerSize(0.7);
	sens1->SetTitle("904lab System Environmental T and P");
    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("Temperature [?C]");
    sens1->GetYaxis()->SetTitleSize(0.03);
	gPad->SetGrid();
	
	TH2F *environ3 = new TH2F("environ3","environ3",unixtime_bin,start,finish,p_bin,p_min,p_max);
    environ3->SetMarkerColor(860);
    environ3->SetMarkerStyle(20);
    environ3->SetMarkerSize(0.7);
    
	rpclab->Draw("env_t:unixtime>>sens1","");
    sens1->Draw("");
    
	pad1->Update();
	pad1->Modified();
    FramexTandP->cd();
	
	//compute the pad range with suitable margins
    Double_t ymin = p_min;
    Double_t ymax = p_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("env_p: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(860);
	axis->SetLineColor(860);
    axis->SetTitleOffset(axis_offset);
    axis->SetLabelSize(label_size);
	axis->SetLabelColor(860);
	axis->SetTitle("Pressure [mbar]");
	axis->SetTitleSize(0.03);
	axis->SetTitleColor(860);
	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,"T [?C]","p");
    leg1->SetTextColor(0);
    leg1->SetFillColor(1);
    leg1->SetTextSize(0.025);
    leg1->Draw();
    y2=y2-0.04;
    y1=y1-0.04;
    
    TLegend *leg2=new TLegend(x1,y1,x2,y2);
    leg2->AddEntry(sens1,"P [mbar]","p");
    leg2->SetTextColor(0);
    leg2->SetFillColor(860);
    leg2->SetTextSize(0.025);
    leg2->Draw();
    
    TImage *img = TImage::Create();
    img->FromPad(FramexTandP);
    TString fout = Form("/home/user/temp/qc4envtemp1_%d_%d.png",start,finish);
    img->WriteImage(fout);



    //Environmental RH
    TCanvas *FrameRH = new TCanvas("FrameRH","RH",0,0,1500,900);//creating Canvas
    
    TH2F *sens1 = new  TH2F("sens1","sens1",unixtime_bin,start,finish,temp_bin,rh_min,rh_max);  
    sens1->SetMarkerColor(1);
    sens1->SetMarkerStyle(20);
    sens1->SetMarkerSize(0.7);
    sens1->SetTitle("904lab System Environmental RH");
    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("RH [%]");
    sens1->GetYaxis()->SetTitleSize(0.03);
    gPad->SetGrid();
	
    
    rpclab->Draw("env_rh:unixtime>>sens1","");
    sens1->Draw("");
    
	
    // draw axis on the right side of the pad
    TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
    axis->SetLabelColor(860);
	axis->SetLineColor(860);
    axis->SetTitleOffset(axis_offset);
    axis->SetLabelSize(label_size);
	axis->SetLabelColor(860);
	axis->SetTitle("Pressure [mbar]");
	axis->SetTitleSize(0.03);
	axis->SetTitleColor(860);
	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,"RH","p");
    leg1->SetTextColor(0);
    leg1->SetFillColor(1);
    leg1->SetTextSize(0.025);
    leg1->Draw();
    y2=y2-0.04;
    y1=y1-0.04;
    
    
    TImage *img = TImage::Create();
    img->FromPad(FrameRH);
    TString fout = Form("/home/user/temp/qc4envtemp2_%d_%d.png",start,finish); 
    img->WriteImage(fout);
}
